1 | /* |
---|
2 | * Copyright (c) 1996 Paul Vojta. |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included |
---|
12 | * in all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
17 | * THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
---|
18 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | */ |
---|
21 | |
---|
22 | /* |
---|
23 | * filefind.h - Define the data structures that specify how to search |
---|
24 | * for various types of files. |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | /* |
---|
29 | * First, some macro definitions. |
---|
30 | * Optionally, filf_app.h may define these. |
---|
31 | */ |
---|
32 | |
---|
33 | #ifndef True |
---|
34 | typedef char Boolean; |
---|
35 | #define True 1 |
---|
36 | #define False 0 |
---|
37 | #endif |
---|
38 | |
---|
39 | #ifndef ARGS |
---|
40 | #if NeedFunctionPrototypes |
---|
41 | #define ARGS(x) x |
---|
42 | #else |
---|
43 | #define ARGS(x) () |
---|
44 | #endif |
---|
45 | #endif |
---|
46 | |
---|
47 | /* |
---|
48 | * Record describing how to search for this type of file. Fields are |
---|
49 | * described further below. |
---|
50 | */ |
---|
51 | |
---|
52 | struct findrec { |
---|
53 | _Xconst char *path1; |
---|
54 | #if CFGFILE |
---|
55 | _Xconst struct envrec *envptr; |
---|
56 | #endif |
---|
57 | _Xconst char *path2; |
---|
58 | _Xconst char *type; |
---|
59 | _Xconst char *fF_etc; |
---|
60 | char x_var_char; |
---|
61 | int n_var_opts; |
---|
62 | _Xconst char *no_f_str; |
---|
63 | _Xconst char *no_f_str_end; |
---|
64 | _Xconst char *abs_str; |
---|
65 | #ifdef PK_AND_GF |
---|
66 | int no_f_str_flags; |
---|
67 | int abs_str_flags; |
---|
68 | char pk_opt_char; |
---|
69 | _Xconst char **pk_gf_addr; |
---|
70 | #endif |
---|
71 | _Xconst char *pct_s_str; |
---|
72 | |
---|
73 | struct { |
---|
74 | struct steprec *stephead; |
---|
75 | struct steprec *pct_s_head; |
---|
76 | int pct_s_count; |
---|
77 | _Xconst struct atomrec **pct_s_atom; |
---|
78 | struct rootrec *rootp; |
---|
79 | } v; |
---|
80 | }; |
---|
81 | |
---|
82 | /* |
---|
83 | * And now, the star of our show. |
---|
84 | */ |
---|
85 | |
---|
86 | extern FILE *filefind ARGS((_Xconst char *, struct findrec *, |
---|
87 | _Xconst char **)); |
---|
88 | |
---|
89 | /* |
---|
90 | * Here are the meanings of the various fields in struct findrec: |
---|
91 | * |
---|
92 | * const char *path1 |
---|
93 | * Colon-separated list of path components to use in the search. |
---|
94 | * This can be obtained, for example, from the environment |
---|
95 | * variable 'XDVIFONTS.' |
---|
96 | * |
---|
97 | * const struct envrec *envptr |
---|
98 | * Pointer to linked list of local environment variable records |
---|
99 | * for the appropriate variable. These variables are only the |
---|
100 | * ones defined in some config file. |
---|
101 | * |
---|
102 | * const char *path2 |
---|
103 | * Secondary list of path components to use in the search. |
---|
104 | * Normally, this is the compiled-in default, and is used at the |
---|
105 | * point (if any) where an extra colon appears in path1. |
---|
106 | * |
---|
107 | * const char *type |
---|
108 | * The type of the search (font, vf, etc.). |
---|
109 | * |
---|
110 | * const char *fF_etc |
---|
111 | * Certain of the characters following '%' that are to be |
---|
112 | * substituted with strings. This should begin with 'f' and 'F'. |
---|
113 | * It should not include 'q', 'Q', 't', 's', or 'S'. |
---|
114 | * |
---|
115 | * char x_var_char |
---|
116 | * If there is another character besides 'f' and 'F' whose |
---|
117 | * corresponding string may change for different files of this |
---|
118 | * type, then it should be third in fF_etc, and x_var_char |
---|
119 | * should contain its value. Otherwise, x_var_char should be 'f'. |
---|
120 | * |
---|
121 | * int n_var_opts |
---|
122 | * x_var_char == 'f' ? 2 : 3 |
---|
123 | * |
---|
124 | * const char *no_f_str |
---|
125 | * The address of the string to be implicitly added to the path |
---|
126 | * component if %f was not used. |
---|
127 | * |
---|
128 | * const char *no_f_str_end |
---|
129 | * The address of '\0' terminating the above string. |
---|
130 | * |
---|
131 | * int no_f_str_flags |
---|
132 | * Flags to be set if no_f_str is used. |
---|
133 | * |
---|
134 | * const char *abs_str |
---|
135 | * String to use for the path in case the font/file name begins |
---|
136 | * with a '/' (indicating an absolute path). |
---|
137 | * |
---|
138 | * int abs_str_flags |
---|
139 | * Flags to be set if abs_str is used. |
---|
140 | * |
---|
141 | * char pk_opt_char |
---|
142 | * 'p' for pk/gf file searching (signifying that %p is to be |
---|
143 | * replaced alternately with "pk" and "gf"; 'f' otherwise |
---|
144 | * (signifying that there is no such character). |
---|
145 | * |
---|
146 | * const char **pk_gf_addr |
---|
147 | * The address of a pointer that is to be alternately changed |
---|
148 | * to point to "pk" or "gf", as above. |
---|
149 | * |
---|
150 | * const char *pct_s_str |
---|
151 | * The string to be substituted for %s occurring at the end of |
---|
152 | * a path component. This may not contain braces ({}), but it |
---|
153 | * may consist of several strings, separated by colons. |
---|
154 | * |
---|
155 | * The remaining fields store information about past searches. They |
---|
156 | * should be initialized to NULL or 0, as appropriate. |
---|
157 | * |
---|
158 | * struct steprec *stephead; |
---|
159 | * Head of the first linked list of steprecs. |
---|
160 | * |
---|
161 | * struct steprec *pct_s_head; |
---|
162 | * Head of the linked list of steprecs for %s. |
---|
163 | * |
---|
164 | * int pct_s_count; |
---|
165 | * Length of the above linked list. |
---|
166 | * |
---|
167 | * const struct atomrec **pct_s_atom; |
---|
168 | * An array containing linked list of atomrecs for %s. |
---|
169 | * |
---|
170 | * struct rootrec *rootp; |
---|
171 | * Head of the linked list of rootrecs. |
---|
172 | */ |
---|
173 | |
---|
174 | /* |
---|
175 | * Values for flags. |
---|
176 | */ |
---|
177 | |
---|
178 | #define F_FILE_USED 1 /* if %f was used */ |
---|
179 | #define F_VARIABLE 2 /* if the string may change next time */ |
---|
180 | #define F_PK_USED 4 /* if %p was used */ |
---|
181 | #define F_PCT_T 8 /* component began with %t */ |
---|
182 | #define F_PCT_S 16 /* put %s after this string of atoms */ |
---|
183 | #define F_SLASH_SLASH 32 /* (atomrec) if this began with // */ |
---|
184 | #define F_WILD 64 /* (atomrec) if wild cards present */ |
---|
185 | #define F_QUICKFIND 128 /* (atomrec, rootrec) if %q or %Q */ |
---|
186 | #define F_QUICKONLY 256 /* if %Q */ |
---|
187 | |
---|
188 | /* |
---|
189 | * Values to put in for %x specifiers. |
---|
190 | */ |
---|
191 | |
---|
192 | #ifndef MAX_N_OPTS |
---|
193 | #define MAX_N_OPTS 6 |
---|
194 | #endif |
---|
195 | |
---|
196 | extern _Xconst char *fF_values[MAX_N_OPTS]; |
---|
197 | |
---|
198 | /* |
---|
199 | * Additional data structures. These are included because they are |
---|
200 | * referenced in the above structure, but really they are internal |
---|
201 | * to filefind.c. |
---|
202 | */ |
---|
203 | |
---|
204 | /* |
---|
205 | * This is the main workhorse data structure. It is used (1) in a linked |
---|
206 | * list, one for each component of the path; (2) in linked lists of |
---|
207 | * {} alternatives; and (3) in linked lists of alternatives for %s. |
---|
208 | * It contains precomputed (via lazy evaluation) information about how |
---|
209 | * to lay down the candidate file name. |
---|
210 | */ |
---|
211 | |
---|
212 | struct steprec { |
---|
213 | struct steprec *next; /* link to next record in chain */ |
---|
214 | _Xconst char *str; /* the string we're now looking at */ |
---|
215 | _Xconst char *strend; |
---|
216 | int flags; /* not cumulative */ |
---|
217 | struct atomrec *atom; |
---|
218 | struct steprec *nextstep; |
---|
219 | _Xconst char *nextpart; /* only for the main linked list */ |
---|
220 | _Xconst char *home; /* ~ expansion result (main list only)*/ |
---|
221 | }; |
---|
222 | |
---|
223 | /* |
---|
224 | * Components of a path following a // or wild card. |
---|
225 | */ |
---|
226 | |
---|
227 | struct atomrec { |
---|
228 | struct atomrec *next; |
---|
229 | _Xconst char *p; /* first char. of dir. name */ |
---|
230 | _Xconst char *p_end; /* last + 1 char. */ |
---|
231 | int flags; /* F_PCT_S or F_SLASH_SLASH (so far) */ |
---|
232 | }; |
---|
233 | |
---|
234 | /* |
---|
235 | * Roots of tree structures containing information on subdirectories. |
---|
236 | */ |
---|
237 | |
---|
238 | struct rootrec { |
---|
239 | struct rootrec *next; /* link to next in list */ |
---|
240 | struct treerec *tree; /* the tree */ |
---|
241 | int flags; /* F_QUICKFIND and F_QUICKONLY */ |
---|
242 | _Xconst char *fullpat; /* xlat pattern for use with ls-R */ |
---|
243 | _Xconst char *fullpat_end; |
---|
244 | _Xconst char *keypat; /* key for ls-R database search */ |
---|
245 | _Xconst char *keypat_end; |
---|
246 | }; |
---|
247 | |
---|
248 | /* |
---|
249 | * Nodes in the above-mentioned tree structure. |
---|
250 | */ |
---|
251 | |
---|
252 | struct treerec { |
---|
253 | _Xconst char *dirname; |
---|
254 | struct treerec *next; /* link to next sibling */ |
---|
255 | struct treerec *child; /* link to subdirectories */ |
---|
256 | Boolean tried_already; |
---|
257 | Boolean isdir; |
---|
258 | }; |
---|
259 | |
---|
260 | /* |
---|
261 | * Config file environment variable. |
---|
262 | */ |
---|
263 | |
---|
264 | struct envrec { |
---|
265 | struct envrec *next; |
---|
266 | _Xconst char *key; |
---|
267 | _Xconst char *value; |
---|
268 | Boolean flag; |
---|
269 | }; |
---|
270 | |
---|
271 | extern struct envrec *ffgetenv ARGS((_Xconst char *)); |
---|