components/libtecla/files/ef_expand_file.3tecla
changeset 681 7c568e880861
child 5810 76203f2cf39d
equal deleted inserted replaced
680:ea0143278d5f 681:7c568e880861
       
     1 '\" te
       
     2 .\" Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd. All Rights Reserved.
       
     3 .\" Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder.
       
     4 .\" Portions Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
       
     5 .TH ef_expand_file 3TECLA "13 May 2011" "SunOS 5.11" "Interactive Command-line Input Library Functions"
       
     6 .SH NAME
       
     7 ef_expand_file, del_ExpandFile, ef_last_error, ef_list_expansions, new_ExpandFile \- expand filename and wildcard expressions
       
     8 .SH SYNOPSIS
       
     9 .LP
       
    10 .nf
       
    11 cc [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-ltecla\fR [ \fIlibrary\fR\&.\|.\|. ] 
       
    12 #include <libtecla.h>
       
    13 
       
    14 \fBExpandFile *\fR\fBef_expand_file\fR(\fBvoid\fR);
       
    15 .fi
       
    16 
       
    17 .LP
       
    18 .nf
       
    19 \fBExpandFile *\fR\fBdel_ExpandFile\fR(\fBExpandFile *\fR\fIef\fR);
       
    20 .fi
       
    21 
       
    22 .LP
       
    23 .nf
       
    24 \fBFileExpansion *\fR\fBef_last_error\fR(\fBExpandFile *\fR\fIef\fR, \fBconst char *\fR\fIpath\fR,
       
    25      \fBint\fR \fIpathlen\fR);
       
    26 .fi
       
    27 
       
    28 .LP
       
    29 .nf
       
    30 \fBint\fR \fBef_list_expansions\fR(\fBFileExpansion *\fR\fIresult\fR, \fBFILE *\fR\fIfp\fR, \fBint\fR \fIterm_width\fR);
       
    31 .fi
       
    32 
       
    33 .LP
       
    34 .nf
       
    35 \fBconst char *\fR\fBnew_ExpandFile\fR(\fBExpandFile *\fR\fIef\fR);
       
    36 .fi
       
    37 
       
    38 .SH DESCRIPTION
       
    39 .sp
       
    40 .LP
       
    41 The \fBef_expand_file()\fR function is part of the \fBlibtecla\fR(3LIB) library. It expands a specified filename, converting ~user/ and ~/ expressions at the start of the filename to the corresponding home directories, replacing $\fBenvvar\fR with the value of the corresponding environment variable, and then, if there are any wildcards, matching these against existing filenames. Backslashes in the input filename are interpreted as escaping any special meanings of the characters that follow them. Only backslashes that are themselves preceded by backslashes are preserved in the expanded filename.
       
    42 .sp
       
    43 .LP
       
    44 In the presence of wildcards, the returned list of filenames includes only the names of existing files which match the wildcards. Otherwise, the original filename is returned after expansion of tilde and dollar expressions, and the result is not checked against existing files. This mimics the file-globbing behavior of the UNIX \fBtcsh\fR shell.
       
    45 .sp
       
    46 .LP
       
    47 The supported wildcards and their meanings are:
       
    48 .sp
       
    49 .ne 2
       
    50 .mk
       
    51 .na
       
    52 \fB\fB*\fR\fR
       
    53 .ad
       
    54 .RS 12n
       
    55 .rt  
       
    56 Match any sequence of zero or more characters.
       
    57 .RE
       
    58 
       
    59 .sp
       
    60 .ne 2
       
    61 .mk
       
    62 .na
       
    63 \fB\fB?\fR\fR
       
    64 .ad
       
    65 .RS 12n
       
    66 .rt  
       
    67 Match any single character.
       
    68 .RE
       
    69 
       
    70 .sp
       
    71 .ne 2
       
    72 .mk
       
    73 .na
       
    74 \fB[\fB\fIchars\fR\fR]\fR
       
    75 .ad
       
    76 .RS 12n
       
    77 .rt  
       
    78 Match any single character that appears in \fIchars\fR. If \fIchars\fR contains an expression of the form a-b, then any character between a and b, including a and b, matches. The '-' character loses its special meaning as a range specifier when it appears at the start of the sequence of characters. The ']' character also loses its significance as the terminator of the range expression if it appears immediately after the opening '[', at which point it is treated one of the characters of the range. If you want both '-' and ']' to be part of the range, the '-' should come first and the ']' second.
       
    79 .RE
       
    80 
       
    81 .sp
       
    82 .ne 2
       
    83 .mk
       
    84 .na
       
    85 \fB[^\fIchars\fR]\fR
       
    86 .ad
       
    87 .RS 12n
       
    88 .rt  
       
    89 The same as [\fIchars\fR] except that it matches any single character that does not appear in \fIchars\fR.
       
    90 .RE
       
    91 
       
    92 .sp
       
    93 .LP
       
    94 Note that wildcards never match the initial dot in filenames that start with '.'. The initial '.' must be explicitly specified in the filename. This again mimics the globbing behavior of most UNIX shells, and its rational is based in the fact that in UNIX, files with names that start with \&'.' are usually hidden configuration files, which are not listed by default by the \fBls\fR(1) command.
       
    95 .sp
       
    96 .LP
       
    97 The \fBnew_ExpandFile()\fR function creates the resources used by the \fBef_expand_file()\fR function. In particular, it maintains the memory that is used to record the array of matching file names that is returned by \fBef_expand_file()\fR. This array is expanded as needed, so there is no builtin limit to the number of files that can be matched.
       
    98 .sp
       
    99 .LP
       
   100 The \fBdel_ExpandFile()\fR function deletes the resources that were returned by a previous call to \fBnew_ExpandFile()\fR. It always returns NULL (that is, a deleted object). It does nothing if the \fIef\fR argument is NULL.
       
   101 .sp
       
   102 .LP
       
   103 The \fBef_expand_file()\fR function performs filename expansion. Its first argument is a resource object returned by \fBnew_ExpandFile()\fR. A pointer to the start of the filename to be matched is passed by the \fIpath\fR argument. This must be a normal null-terminated string, but unless a length of -1 is passed in \fIpathlen\fR, only the first \fIpathlen\fR characters will be used in the filename expansion. If the length is specified as -1, the whole of the string will be expanded. A container of the following type is returned by \fBef_expand_file()\fR.
       
   104 .sp
       
   105 .in +2
       
   106 .nf
       
   107 typedef struct {
       
   108     int exists;   /* True if the files in files[] exist */
       
   109     int nfile;    /* The number of files in files[] */
       
   110     char **files; /* An array of 'nfile' filenames. */
       
   111 } FileExpansion;
       
   112 .fi
       
   113 .in -2
       
   114 
       
   115 .sp
       
   116 .LP
       
   117 The \fBef_expand_file()\fR function returns a pointer to a container whose contents are the results of the expansion. If there were no wildcards in the filename, the \fInfile\fR member will be 1, and the \fIexists\fR member should be queried if it is important to know if the expanded file currently exists. If there were wild cards, then the contained \fIfiles\fR[] array will contain the names of the \fInfile\fR existing files that matched the wild-carded filename, and the \fIexists\fR member will have the value 1. Note that the returned container belongs to the specified ef object, and its contents will change on each call, so if you need to retain the results of more than one call to \fBef_expand_file()\fR, you should either make a private copy of the returned results, or create multiple file-expansion resource objects with multiple calls to \fBnew_ExpandFile()\fR.
       
   118 .sp
       
   119 .LP
       
   120 On error, \fINULL\fR is returned, and an explanation of the error can be determined by calling \fBef_last_error\fR(\fIef\fR).
       
   121 .sp
       
   122 .LP
       
   123 The \fBef_last_error()\fR function returns the message which describes the error that occurred on the last call to \fBef_expand_file()\fR, for the given (\fBExpandFile *\fR\fIef\fR) resource object.
       
   124 .sp
       
   125 .LP
       
   126 The \fBef_list_expansions()\fR function provides a convenient way to list the filename expansions returned by \fBef_expand_file()\fR. Like the \fBls\fR utility, it arranges the filenames into equal width columns, each column having the width of the largest file. The number of columns used is thus determined by the length of the longest filename, and the specified terminal width. Beware that filenames that are longer than the specified terminal width are printed without being truncated, so output longer than the specified terminal width can occur. The list is written to the \fBstdio\fR stream specified by the \fIfp\fR argument.
       
   127 .SS "Thread Safety"
       
   128 .sp
       
   129 .LP
       
   130 It is safe to use the facilities of this module in multiple threads, provided that each thread uses a separately allocated \fBExpandFile\fR object. In other words, if two threads want to do file expansion, they should each call \fBnew_ExpandFile()\fR to allocate their own file-expansion objects.
       
   131 .SH EXAMPLES
       
   132 .LP
       
   133 \fBExample 1 \fRUse of file expansion function.
       
   134 .sp
       
   135 .LP
       
   136 The following is a complete example of how to use the file expansion function.
       
   137 
       
   138 .sp
       
   139 .in +2
       
   140 .nf
       
   141 #include <stdio.h>
       
   142 #include <libtecla.h>
       
   143  
       
   144 int main(int argc, char *argv[])
       
   145 {
       
   146     ExpandFile *ef;      /* The expansion resource object */
       
   147     char *filename;      /* The filename being expanded */
       
   148     FileExpansion *expn; /* The results of the expansion */
       
   149     int i;
       
   150  
       
   151     ef = new_ExpandFile();
       
   152     if(!ef)
       
   153         return 1;
       
   154  
       
   155     for(arg = *(argv++); arg; arg = *(argv++)) {
       
   156        if((expn = ef_expand_file(ef, arg, -1)) == NULL) {
       
   157           fprintf(stderr, "Error expanding %s (%s).\en", arg,
       
   158               ef_last_error(ef));
       
   159        } else {
       
   160           printf("%s matches the following files:\en", arg);
       
   161           for(i=0; i<expn->nfile; i++)
       
   162               printf(" %s\en", expn->files[i]);
       
   163        }
       
   164     }
       
   165  
       
   166     ef = del_ExpandFile(ef);
       
   167     return 0;
       
   168 }
       
   169 .fi
       
   170 .in -2
       
   171 
       
   172 .SH ATTRIBUTES
       
   173 .sp
       
   174 .LP
       
   175 See \fBattributes\fR(5) for descriptions of the following attributes:
       
   176 .sp
       
   177 
       
   178 .sp
       
   179 .TS
       
   180 tab() box;
       
   181 cw(2.75i) |cw(2.75i) 
       
   182 lw(2.75i) |lw(2.75i) 
       
   183 .
       
   184 ATTRIBUTE TYPEATTRIBUTE VALUE
       
   185 _
       
   186 Interface StabilityCommitted
       
   187 _
       
   188 MT-LevelMT-Safe
       
   189 .TE
       
   190 
       
   191 .SH SEE ALSO
       
   192 .sp
       
   193 .LP
       
   194 \fBcpl_complete_word\fR(3TECLA), \fBgl_get_line\fR(3TECLA), \fBlibtecla\fR(3LIB), \fBpca_lookup_file\fR(3TECLA), \fBattributes\fR(5)