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
4 .\" "Software"), to deal in the Software without restriction, including
5 .\" without limitation the rights to use, copy, modify, merge, publish,
6 .\" distribute, and/or sell copies of the Software, and to permit persons
7 .\" to whom the Software is furnished to do so, provided that the above
8 .\" copyright notice(s) and this permission notice appear in all copies of
9 .\" the Software and that both the above copyright notice(s) and this
10 .\" permission notice appear in supporting documentation.
12 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
15 .\" OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16 .\" HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
17 .\" INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
18 .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
19 .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
20 .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 .\" Except as contained in this notice, the name of a copyright holder
23 .\" shall not be used in advertising or otherwise to promote the sale, use
24 .\" or other dealings in this Software without prior written authorization
25 .\" of the copyright holder.
26 .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
27 .TH EF_EXPAND_FILE 3TECLA "Jun 1, 2004"
29 ef_expand_file, del_ExpandFile, ef_last_error, ef_list_expansions,
30 new_ExpandFile \- expand filename and wildcard expressions
34 cc [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-ltecla\fR [ \fIlibrary\fR\&.\|.\|. ]
37 \fBExpandFile *\fR\fBef_expand_file\fR(\fBvoid\fR);
42 \fBExpandFile *\fR\fBdel_ExpandFile\fR(\fBExpandFile *\fR\fIef\fR);
47 \fBFileExpansion *\fR\fBef_last_error\fR(\fBExpandFile *\fR\fIef\fR, \fBconst char *\fR\fIpath\fR,
48 \fBint\fR \fIpathlen\fR);
53 \fBint\fR \fBef_list_expansions\fR(\fBFileExpansion *\fR\fIresult\fR, \fBFILE *\fR\fIfp\fR, \fBint\fR \fIterm_width\fR);
58 \fBconst char *\fR\fBnew_ExpandFile\fR(\fBExpandFile *\fR\fIef\fR);
64 The \fBef_expand_file()\fR function is part of the \fBlibtecla\fR(3LIB)
65 library. It expands a specified filename, converting ~user/ and ~/ expressions
66 at the start of the filename to the corresponding home directories, replacing
67 $\fBenvvar\fR with the value of the corresponding environment variable, and
68 then, if there are any wildcards, matching these against existing filenames.
69 Backslashes in the input filename are interpreted as escaping any special
70 meanings of the characters that follow them. Only backslashes that are
71 themselves preceded by backslashes are preserved in the expanded filename.
74 In the presence of wildcards, the returned list of filenames includes only the
75 names of existing files which match the wildcards. Otherwise, the original
76 filename is returned after expansion of tilde and dollar expressions, and the
77 result is not checked against existing files. This mimics the file-globbing
78 behavior of the UNIX \fBtcsh\fR shell.
81 The supported wildcards and their meanings are:
88 Match any sequence of zero or more characters.
97 Match any single character.
103 \fB[\fB\fIchars\fR\fR]\fR
106 Match any single character that appears in \fIchars\fR. If \fIchars\fR contains
107 an expression of the form a-b, then any character between a and b, including a
108 and b, matches. The '-' character loses its special meaning as a range
109 specifier when it appears at the start of the sequence of characters. The ']'
110 character also looses its significance as the terminator of the range
111 expression if it appears immediately after the opening '[', at which point it
112 is treated one of the characters of the range. If you want both '-' and ']' to
113 be part of the range, the '-' should come first and the ']' second.
122 The same as [\fIchars\fR] except that it matches any single character that does
123 not appear in \fIchars\fR.
128 Note that wildcards never match the initial dot in filenames that start
129 with '.'. The initial '.' must be explicitly specified in the filename. This again
130 mimics the globbing behavior of most UNIX shells, and its rational is based in
131 the fact that in UNIX, files with names that start with \&'.' are usually
132 hidden configuration files, which are not listed by default by the \fBls\fR(1)
136 The \fBnew_ExpandFile()\fR function creates the resources used by the
137 \fBef_expand_file()\fR function. In particular, it maintains the memory that is
138 used to record the array of matching file names that is returned by
139 \fBef_expand_file()\fR. This array is expanded as needed, so there is no
140 builtin limit to the number of files that can be matched.
143 The \fBdel_ExpandFile()\fR function deletes the resources that were returned by
144 a previous call to \fBnew_ExpandFile()\fR. It always returns NULL (that is, a
145 deleted object). It does nothing if the \fIef\fR argument is NULL.
148 The \fBef_expand_file()\fR function performs filename expansion. Its first
149 argument is a resource object returned by \fBnew_ExpandFile()\fR. A pointer to
150 the start of the filename to be matched is passed by the \fIpath\fR argument.
151 This must be a normal null-terminated string, but unless a length of -1 is
152 passed in \fIpathlen\fR, only the first \fIpathlen\fR characters will be used
153 in the filename expansion. If the length is specified as -1, the whole of the
154 string will be expanded. A container of the following type is returned by
155 \fBef_expand_file()\fR.
160 int exists; /* True if the files in files[] exist */
161 int nfile; /* The number of files in files[] */
162 char **files; /* An array of 'nfile' filenames. */
169 The \fBef_expand_file()\fR function returns a pointer to a container whose
170 contents are the results of the expansion. If there were no wildcards in the
171 filename, the \fInfile\fR member will be 1, and the \fIexists\fR member should
172 be queried if it is important to know if the expanded file currently exists. If
173 there were wild cards, then the contained \fIfiles\fR[] array will contain the
174 names of the \fInfile\fR existing files that matched the wild-carded filename,
175 and the \fIexists\fR member will have the value 1. Note that the returned
176 container belongs to the specified ef object, and its contents will change on
177 each call, so if you need to retain the results of more than one call to
178 \fBef_expand_file()\fR, you should either make a private copy of the returned
179 results, or create multiple file-expansion resource objects with multiple calls
180 to \fBnew_ExpandFile()\fR.
183 On error, \fINULL\fR is returned, and an explanation of the error can be
184 determined by calling \fBef_last_error\fR(\fIef\fR).
187 The \fBef_last_error()\fR function returns the message which describes the
188 error that occurred on the last call to \fBef_expand_file()\fR, for the given
189 (\fBExpandFile *\fR\fIef\fR) resource object.
192 The \fBef_list_expansions()\fR function provides a convenient way to list the
193 filename expansions returned by \fBef_expand_file()\fR. Like the \fBls\fR
194 utility, it arranges the filenames into equal width columns, each column having
195 the width of the largest file. The number of columns used is thus determined by
196 the length of the longest filename, and the specified terminal width. Beware
197 that filenames that are longer than the specified terminal width are printed
198 without being truncated, so output longer than the specified terminal width can
199 occur. The list is written to the \fBstdio\fR stream specified by the \fIfp\fR
204 It is safe to use the facilities of this module in multiple threads, provided
205 that each thread uses a separately allocated \fBExpandFile\fR object. In other
206 words, if two threads want to do file expansion, they should each call
207 \fBnew_ExpandFile()\fR to allocate their own file-expansion objects.
210 \fBExample 1 \fRUse of file expansion function.
213 The following is a complete example of how to use the file expansion function.
219 #include <libtecla.h>
221 int main(int argc, char *argv[])
223 ExpandFile *ef; /* The expansion resource object */
224 char *filename; /* The filename being expanded */
225 FileExpansion *expn; /* The results of the expansion */
228 ef = new_ExpandFile();
232 for(arg = *(argv++); arg; arg = *(argv++)) {
233 if((expn = ef_expand_file(ef, arg, -1)) == NULL) {
234 fprintf(stderr, "Error expanding %s (%s).\en", arg,
237 printf("%s matches the following files:\en", arg);
238 for(i=0; i<expn->nfile; i++)
239 printf(" %s\en", expn->files[i]);
243 ef = del_ExpandFile(ef);
252 See \fBattributes\fR(5) for descriptions of the following attributes:
260 ATTRIBUTE TYPE ATTRIBUTE VALUE
262 Interface Stability Evolving
270 \fBcpl_complete_word\fR(3TECLA), \fBgl_get_line\fR(3TECLA),
271 \fBlibtecla\fR(3LIB), \fBpca_lookup_file\fR(3TECLA), \fBattributes\fR(5)