Blame


1 5cedca1b 2004-05-15 devnull /*
2 5cedca1b 2004-05-15 devnull *
3 5cedca1b 2004-05-15 devnull * Pathname management routines for DWB C programs.
4 5cedca1b 2004-05-15 devnull *
5 5cedca1b 2004-05-15 devnull * Applications should initialize a dwbinit array with the string
6 5cedca1b 2004-05-15 devnull * pointers and arrays that need to be updated, and then hand that
7 5cedca1b 2004-05-15 devnull * array to DWBinit before much else happens in their main program.
8 5cedca1b 2004-05-15 devnull * DWBinit calls DWBhome to get the current home directory. DWBhome
9 5cedca1b 2004-05-15 devnull * uses the last definition of DWBENV (usually "DWBHOME") in file
10 5cedca1b 2004-05-15 devnull * DWBCONFIG (e.g., /usr/lib/dwb3.4) or the value assigned to that
11 5cedca1b 2004-05-15 devnull * variable in the environment if the DWBCONFIG file doesn't exist,
12 5cedca1b 2004-05-15 devnull * can't be read, or doesn't define DWBENV.
13 5cedca1b 2004-05-15 devnull *
14 5cedca1b 2004-05-15 devnull * DWBCONFIG must be a simple shell script - comments, a definition
15 5cedca1b 2004-05-15 devnull * of DWBHOME, and perhaps an export or echo is about all that's
16 5cedca1b 2004-05-15 devnull * allowed. The parsing in DWBhome is simple and makes no attempt
17 5cedca1b 2004-05-15 devnull * to duplicate the shell. It only looks for DWBHOME= as the first
18 5cedca1b 2004-05-15 devnull * non-white space string on a line, so
19 5cedca1b 2004-05-15 devnull *
20 5cedca1b 2004-05-15 devnull * #
21 5cedca1b 2004-05-15 devnull * # A sample DWBCONFIG shell script
22 5cedca1b 2004-05-15 devnull * #
23 5cedca1b 2004-05-15 devnull *
24 5cedca1b 2004-05-15 devnull * DWBHOME=/usr/add-on/dwb3.4
25 5cedca1b 2004-05-15 devnull * export DWBHOME
26 5cedca1b 2004-05-15 devnull *
27 5cedca1b 2004-05-15 devnull * means DWBhome would return "/usr/add-on/dwb3.4" for the DWB home
28 5cedca1b 2004-05-15 devnull * directory. A DWBCONFIG file means there can only be one working
29 5cedca1b 2004-05-15 devnull * copy of a DWB release on a system, which seems like a good idea.
30 5cedca1b 2004-05-15 devnull * Using DWBCONFIG also means programs will always include correct
31 5cedca1b 2004-05-15 devnull * versions of files (e.g., prologues or macro packages).
32 5cedca1b 2004-05-15 devnull *
33 5cedca1b 2004-05-15 devnull * Relying on an environment variable guarantees nothing. You could
34 5cedca1b 2004-05-15 devnull * execute a version of dpost, but your environment might point at
35 5cedca1b 2004-05-15 devnull * incorrect font tables or prologues. Despite the obvious problems
36 5cedca1b 2004-05-15 devnull * we've also implemented an environment variable approach, but it's
37 5cedca1b 2004-05-15 devnull * only used if there's no DWBCONFIG file.
38 5cedca1b 2004-05-15 devnull *
39 5cedca1b 2004-05-15 devnull * DWBinit calls DWBhome to get the DWB home directory prefix and
40 5cedca1b 2004-05-15 devnull * then marches through its dwbinit argument, removing the default
41 5cedca1b 2004-05-15 devnull * home directory and prepending the new home. DWBinit stops when
42 5cedca1b 2004-05-15 devnull * it reaches an element that has NULL for its address and value
43 5cedca1b 2004-05-15 devnull * fields. Pointers in a dwbinit array are reallocated and properly
44 5cedca1b 2004-05-15 devnull * initialized; arrays are simply reinitialized if there's room.
45 5cedca1b 2004-05-15 devnull * All pathnames that are to be adjusted should be relative. For
46 5cedca1b 2004-05-15 devnull * example,
47 5cedca1b 2004-05-15 devnull *
48 5cedca1b 2004-05-15 devnull * char *fontdir = "lib/font";
49 5cedca1b 2004-05-15 devnull * char xyzzy[25] = "etc/xyzzy";
50 5cedca1b 2004-05-15 devnull *
51 5cedca1b 2004-05-15 devnull * would be represented in a dwbinit array as,
52 5cedca1b 2004-05-15 devnull *
53 5cedca1b 2004-05-15 devnull * dwbinit allpaths[] = {
54 5cedca1b 2004-05-15 devnull * &fontdir, NULL, 0,
55 5cedca1b 2004-05-15 devnull * NULL, xyzzy, sizeof(xyzzy),
56 5cedca1b 2004-05-15 devnull * NULL, NULL, 0
57 5cedca1b 2004-05-15 devnull * };
58 fa325e9b 2020-01-10 cross *
59 5cedca1b 2004-05-15 devnull * The last element must have NULL entries for the address and
60 5cedca1b 2004-05-15 devnull * value fields. The main() routine would then do,
61 5cedca1b 2004-05-15 devnull *
62 5cedca1b 2004-05-15 devnull * #include "dwbinit.h"
63 5cedca1b 2004-05-15 devnull *
64 5cedca1b 2004-05-15 devnull * main() {
65 5cedca1b 2004-05-15 devnull *
66 5cedca1b 2004-05-15 devnull * DWBinit("program name", allpaths);
67 5cedca1b 2004-05-15 devnull * ...
68 5cedca1b 2004-05-15 devnull * }
69 5cedca1b 2004-05-15 devnull *
70 5cedca1b 2004-05-15 devnull * Debugging is enabled if DWBDEBUG is in the environment and has
71 5cedca1b 2004-05-15 devnull * the value ON. Output is occasionally useful and probably should
72 5cedca1b 2004-05-15 devnull * be documented.
73 5cedca1b 2004-05-15 devnull *
74 5cedca1b 2004-05-15 devnull */
75 5cedca1b 2004-05-15 devnull
76 74374cc8 2005-10-31 devnull #include <u.h>
77 5cedca1b 2004-05-15 devnull #include <stdio.h>
78 5cedca1b 2004-05-15 devnull #include <ctype.h>
79 5cedca1b 2004-05-15 devnull #include <string.h>
80 5cedca1b 2004-05-15 devnull #include <stdlib.h>
81 5cedca1b 2004-05-15 devnull
82 5cedca1b 2004-05-15 devnull #include "dwbinit.h"
83 5cedca1b 2004-05-15 devnull
84 5cedca1b 2004-05-15 devnull #ifndef DWBCONFIG
85 5cedca1b 2004-05-15 devnull #define DWBCONFIG "/dev/null"
86 5cedca1b 2004-05-15 devnull #endif
87 5cedca1b 2004-05-15 devnull
88 5cedca1b 2004-05-15 devnull #ifndef DWBENV
89 5cedca1b 2004-05-15 devnull #define DWBENV "DWBHOME"
90 5cedca1b 2004-05-15 devnull #endif
91 5cedca1b 2004-05-15 devnull
92 5cedca1b 2004-05-15 devnull #ifndef DWBHOME
93 5cedca1b 2004-05-15 devnull #define DWBHOME ""
94 5cedca1b 2004-05-15 devnull #endif
95 5cedca1b 2004-05-15 devnull
96 5cedca1b 2004-05-15 devnull #ifndef DWBDEBUG
97 5cedca1b 2004-05-15 devnull #define DWBDEBUG "DWBDEBUG"
98 5cedca1b 2004-05-15 devnull #endif
99 5cedca1b 2004-05-15 devnull
100 5cedca1b 2004-05-15 devnull #ifndef DWBPREFIX
101 5cedca1b 2004-05-15 devnull #define DWBPREFIX "\\*(.P"
102 5cedca1b 2004-05-15 devnull #endif
103 5cedca1b 2004-05-15 devnull
104 5cedca1b 2004-05-15 devnull /*****************************************************************************/
105 5cedca1b 2004-05-15 devnull
106 5cedca1b 2004-05-15 devnull void DWBdebug(dwbinit *ptr, int level)
107 5cedca1b 2004-05-15 devnull {
108 5cedca1b 2004-05-15 devnull
109 5cedca1b 2004-05-15 devnull char *path;
110 5cedca1b 2004-05-15 devnull char *home;
111 5cedca1b 2004-05-15 devnull static char *debug = NULL;
112 5cedca1b 2004-05-15 devnull
113 5cedca1b 2004-05-15 devnull /*
114 5cedca1b 2004-05-15 devnull *
115 5cedca1b 2004-05-15 devnull * Debugging output, but only if DWBDEBUG is defined to be ON in the
116 5cedca1b 2004-05-15 devnull * environment. Dumps general info the first time through.
117 5cedca1b 2004-05-15 devnull *
118 5cedca1b 2004-05-15 devnull */
119 5cedca1b 2004-05-15 devnull
120 5cedca1b 2004-05-15 devnull if ( debug == NULL && (debug = getenv(DWBDEBUG)) == NULL )
121 5cedca1b 2004-05-15 devnull debug = "OFF";
122 5cedca1b 2004-05-15 devnull
123 5cedca1b 2004-05-15 devnull if ( strcmp(debug, "ON") == 0 ) {
124 5cedca1b 2004-05-15 devnull if ( level == 0 ) {
125 5cedca1b 2004-05-15 devnull fprintf(stderr, "Environment variable: %s\n", DWBENV);
126 5cedca1b 2004-05-15 devnull fprintf(stderr, "Configuration file: %s\n", DWBCONFIG);
127 5cedca1b 2004-05-15 devnull fprintf(stderr, "Default home: %s\n", DWBHOME);
128 5cedca1b 2004-05-15 devnull if ( (home = DWBhome()) != NULL )
129 5cedca1b 2004-05-15 devnull fprintf(stderr, "Current home: %s\n", home);
130 5cedca1b 2004-05-15 devnull } /* End if */
131 5cedca1b 2004-05-15 devnull
132 5cedca1b 2004-05-15 devnull fprintf(stderr, "\n%s pathnames:\n", level == 0 ? "Original" : "Final");
133 5cedca1b 2004-05-15 devnull for ( ; ptr->value != NULL || ptr->address != NULL; ptr++ ) {
134 5cedca1b 2004-05-15 devnull if ( (path = ptr->value) == NULL ) {
135 5cedca1b 2004-05-15 devnull path = *ptr->address;
136 5cedca1b 2004-05-15 devnull fprintf(stderr, " pointer: %s\n", path);
137 5cedca1b 2004-05-15 devnull } else fprintf(stderr, " array[%d]: %s\n", ptr->length, path);
138 5cedca1b 2004-05-15 devnull if ( level == 0 && *path == '/' )
139 5cedca1b 2004-05-15 devnull fprintf(stderr, " WARNING - absolute path\n");
140 5cedca1b 2004-05-15 devnull } /* End for */
141 5cedca1b 2004-05-15 devnull } /* End if */
142 5cedca1b 2004-05-15 devnull
143 5cedca1b 2004-05-15 devnull } /* End of DWBdebug */
144 5cedca1b 2004-05-15 devnull
145 5cedca1b 2004-05-15 devnull /*****************************************************************************/
146 5cedca1b 2004-05-15 devnull
147 c5561c23 2004-05-16 devnull extern char *unsharp(char*);
148 c5561c23 2004-05-16 devnull
149 5cedca1b 2004-05-15 devnull char *DWBhome(void)
150 5cedca1b 2004-05-15 devnull {
151 5cedca1b 2004-05-15 devnull
152 5cedca1b 2004-05-15 devnull FILE *fp;
153 5cedca1b 2004-05-15 devnull char *ptr;
154 5cedca1b 2004-05-15 devnull char *path;
155 5cedca1b 2004-05-15 devnull int len;
156 5cedca1b 2004-05-15 devnull char buf[200];
157 5cedca1b 2004-05-15 devnull char *home = NULL;
158 5cedca1b 2004-05-15 devnull
159 5cedca1b 2004-05-15 devnull /*
160 5cedca1b 2004-05-15 devnull *
161 5cedca1b 2004-05-15 devnull * Return the DWB home directory. Uses the last definition of DWBENV
162 5cedca1b 2004-05-15 devnull * (usually "DWBHOME") in file DWBCONFIG (perhaps /usr/lib/dwb3.4) or
163 5cedca1b 2004-05-15 devnull * the value assigned to the variable named by the DWBENV string in
164 5cedca1b 2004-05-15 devnull * the environment if DWBCONFIG doesn't exist or doesn't define DWBENV.
165 5cedca1b 2004-05-15 devnull * Skips the file lookup if DWBCONFIG can't be read. Returns NULL if
166 5cedca1b 2004-05-15 devnull * there's no home directory.
167 5cedca1b 2004-05-15 devnull *
168 5cedca1b 2004-05-15 devnull */
169 5cedca1b 2004-05-15 devnull
170 5cedca1b 2004-05-15 devnull if ( (fp = fopen(DWBCONFIG, "r")) != NULL ) {
171 5cedca1b 2004-05-15 devnull len = strlen(DWBENV);
172 5cedca1b 2004-05-15 devnull while ( fgets(buf, sizeof(buf), fp) != NULL ) {
173 74374cc8 2005-10-31 devnull for ( ptr = buf; isspace((uchar)*ptr); ptr++ ) ;
174 5cedca1b 2004-05-15 devnull if ( strncmp(ptr, DWBENV, len) == 0 && *(ptr+len) == '=' ) {
175 5cedca1b 2004-05-15 devnull path = ptr + len + 1;
176 74374cc8 2005-10-31 devnull for ( ptr = path; !isspace((uchar)*ptr) && *ptr != ';'; ptr++ ) ;
177 5cedca1b 2004-05-15 devnull *ptr = '\0';
178 5cedca1b 2004-05-15 devnull if ( home != NULL )
179 5cedca1b 2004-05-15 devnull free(home);
180 5cedca1b 2004-05-15 devnull if ( (home = malloc(strlen(path)+1)) != NULL )
181 5cedca1b 2004-05-15 devnull strcpy(home, path);
182 5cedca1b 2004-05-15 devnull } /* End if */
183 5cedca1b 2004-05-15 devnull } /* End while */
184 5cedca1b 2004-05-15 devnull fclose(fp);
185 5cedca1b 2004-05-15 devnull } /* End if */
186 5cedca1b 2004-05-15 devnull
187 5cedca1b 2004-05-15 devnull if ( home == NULL ) {
188 5cedca1b 2004-05-15 devnull if ( (home = getenv(DWBENV)) == NULL ) {
189 5cedca1b 2004-05-15 devnull if ( (home = DWBHOME) == NULL || *home == '\0' || *home == ' ' )
190 5cedca1b 2004-05-15 devnull home = NULL;
191 5cedca1b 2004-05-15 devnull } /* End if */
192 6530d37d 2009-12-27 rsc if ( home != NULL )
193 6530d37d 2009-12-27 rsc home = unsharp(home);
194 5cedca1b 2004-05-15 devnull } /* End if */
195 5cedca1b 2004-05-15 devnull
196 5cedca1b 2004-05-15 devnull while (home && *home == '/' && *(home +1) == '/') /* remove extra slashes */
197 5cedca1b 2004-05-15 devnull home++;
198 5cedca1b 2004-05-15 devnull return(home);
199 5cedca1b 2004-05-15 devnull
200 5cedca1b 2004-05-15 devnull } /* End of DWBhome */
201 5cedca1b 2004-05-15 devnull
202 5cedca1b 2004-05-15 devnull /*****************************************************************************/
203 5cedca1b 2004-05-15 devnull
204 5cedca1b 2004-05-15 devnull void DWBinit(char *prog, dwbinit *paths)
205 5cedca1b 2004-05-15 devnull {
206 5cedca1b 2004-05-15 devnull
207 5cedca1b 2004-05-15 devnull char *prefix;
208 5cedca1b 2004-05-15 devnull char *value;
209 5cedca1b 2004-05-15 devnull char *path;
210 5cedca1b 2004-05-15 devnull int plen;
211 5cedca1b 2004-05-15 devnull int length;
212 5cedca1b 2004-05-15 devnull dwbinit *opaths = paths;
213 5cedca1b 2004-05-15 devnull
214 5cedca1b 2004-05-15 devnull /*
215 5cedca1b 2004-05-15 devnull *
216 5cedca1b 2004-05-15 devnull * Adjust the pathnames listed in paths, using the home directory
217 5cedca1b 2004-05-15 devnull * returned by DWBhome(). Stops when it reaches an element that has
218 5cedca1b 2004-05-15 devnull * NULL address and value fields. Assumes pathnames are relative,
219 5cedca1b 2004-05-15 devnull * but changes everything. DWBdebug issues a warning if an original
220 5cedca1b 2004-05-15 devnull * path begins with a /.
221 5cedca1b 2004-05-15 devnull *
222 5cedca1b 2004-05-15 devnull * A non-NULL address refers to a pointer, which is reallocated and
223 5cedca1b 2004-05-15 devnull * then reinitialized. A NULL address implies a non-NULL value field
224 5cedca1b 2004-05-15 devnull * and describes a character array that we only reinitialize. The
225 5cedca1b 2004-05-15 devnull * length field for an array is the size of that array. The length
226 5cedca1b 2004-05-15 devnull * field of a pointer is an increment that's added to the length
227 5cedca1b 2004-05-15 devnull * required to store the new pathname string - should help when we
228 5cedca1b 2004-05-15 devnull * want to change character arrays to pointers in applications like
229 5cedca1b 2004-05-15 devnull * troff.
230 5cedca1b 2004-05-15 devnull *
231 5cedca1b 2004-05-15 devnull */
232 5cedca1b 2004-05-15 devnull
233 5cedca1b 2004-05-15 devnull if ( (prefix = DWBhome()) == NULL ) {
234 5cedca1b 2004-05-15 devnull fprintf(stderr, "%s: no DWB home directory\n", prog);
235 5cedca1b 2004-05-15 devnull exit(1);
236 5cedca1b 2004-05-15 devnull } /* End if */
237 5cedca1b 2004-05-15 devnull
238 5cedca1b 2004-05-15 devnull DWBdebug(opaths, 0);
239 5cedca1b 2004-05-15 devnull plen = strlen(prefix);
240 5cedca1b 2004-05-15 devnull
241 5cedca1b 2004-05-15 devnull for ( ; paths->value != NULL || paths->address != NULL; paths++ ) {
242 5cedca1b 2004-05-15 devnull if ( paths->address == NULL ) {
243 5cedca1b 2004-05-15 devnull length = 0;
244 5cedca1b 2004-05-15 devnull value = paths->value;
245 5cedca1b 2004-05-15 devnull } else {
246 5cedca1b 2004-05-15 devnull length = paths->length;
247 5cedca1b 2004-05-15 devnull value = *paths->address;
248 5cedca1b 2004-05-15 devnull } /* End else */
249 5cedca1b 2004-05-15 devnull
250 5cedca1b 2004-05-15 devnull length += plen + 1 + strlen(value); /* +1 is for the '/' */
251 5cedca1b 2004-05-15 devnull
252 5cedca1b 2004-05-15 devnull if ( (path = malloc(length+1)) == NULL ) {
253 5cedca1b 2004-05-15 devnull fprintf(stderr, "%s: can't allocate pathname memory\n", prog);
254 5cedca1b 2004-05-15 devnull exit(1);
255 5cedca1b 2004-05-15 devnull } /* End if */
256 5cedca1b 2004-05-15 devnull
257 5cedca1b 2004-05-15 devnull if ( *value != '\0' ) {
258 5cedca1b 2004-05-15 devnull char *eop = prefix;
259 5cedca1b 2004-05-15 devnull while(*eop++)
260 5cedca1b 2004-05-15 devnull ;
261 5cedca1b 2004-05-15 devnull eop -= 2;
262 5cedca1b 2004-05-15 devnull if (*value != '/' && *eop != '/') {
263 5cedca1b 2004-05-15 devnull sprintf(path, "%s/%s", prefix, value);
264 5cedca1b 2004-05-15 devnull } else if (*value == '/' && *eop == '/') {
265 5cedca1b 2004-05-15 devnull value++;
266 5cedca1b 2004-05-15 devnull sprintf(path, "%s%s", prefix, value);
267 5cedca1b 2004-05-15 devnull } else
268 5cedca1b 2004-05-15 devnull sprintf(path, "%s%s", prefix, value);
269 5cedca1b 2004-05-15 devnull } else
270 5cedca1b 2004-05-15 devnull sprintf(path, "%s", prefix);
271 5cedca1b 2004-05-15 devnull
272 5cedca1b 2004-05-15 devnull if ( paths->address == NULL ) {
273 5cedca1b 2004-05-15 devnull if ( strlen(path) >= paths->length ) {
274 5cedca1b 2004-05-15 devnull fprintf(stderr, "%s: no room for %s\n", prog, path);
275 5cedca1b 2004-05-15 devnull exit(1);
276 5cedca1b 2004-05-15 devnull } /* End if */
277 5cedca1b 2004-05-15 devnull strcpy(paths->value, path);
278 5cedca1b 2004-05-15 devnull free(path);
279 5cedca1b 2004-05-15 devnull } else *paths->address = path;
280 5cedca1b 2004-05-15 devnull } /* End for */
281 5cedca1b 2004-05-15 devnull
282 5cedca1b 2004-05-15 devnull DWBdebug(opaths, 1);
283 5cedca1b 2004-05-15 devnull
284 5cedca1b 2004-05-15 devnull } /* End of DWBinit */
285 5cedca1b 2004-05-15 devnull
286 5cedca1b 2004-05-15 devnull /*****************************************************************************/
287 5cedca1b 2004-05-15 devnull
288 5cedca1b 2004-05-15 devnull void DWBprefix( char *prog, char *path, int length)
289 5cedca1b 2004-05-15 devnull {
290 5cedca1b 2004-05-15 devnull
291 5cedca1b 2004-05-15 devnull char *home;
292 5cedca1b 2004-05-15 devnull char buf[512];
293 5cedca1b 2004-05-15 devnull int len = strlen(DWBPREFIX);
294 5cedca1b 2004-05-15 devnull
295 5cedca1b 2004-05-15 devnull /*
296 5cedca1b 2004-05-15 devnull *
297 5cedca1b 2004-05-15 devnull * Replace a leading DWBPREFIX string in path by the current DWBhome().
298 5cedca1b 2004-05-15 devnull * Used by programs that pretend to handle .so requests. Assumes path
299 5cedca1b 2004-05-15 devnull * is an array with room for length characters. The implementation is
300 5cedca1b 2004-05-15 devnull * not great, but should be good enough for now. Also probably should
301 5cedca1b 2004-05-15 devnull * have DWBhome() only do the lookup once, and remember the value if
302 5cedca1b 2004-05-15 devnull * called again.
303 fa325e9b 2020-01-10 cross *
304 5cedca1b 2004-05-15 devnull */
305 5cedca1b 2004-05-15 devnull
306 5cedca1b 2004-05-15 devnull if ( strncmp(path, DWBPREFIX, len) == 0 ) {
307 5cedca1b 2004-05-15 devnull if ( (home = DWBhome()) != NULL ) {
308 5cedca1b 2004-05-15 devnull if ( strlen(home) + strlen(path+len) < length ) {
309 5cedca1b 2004-05-15 devnull sprintf(buf, "%s%s", home, path+len);
310 5cedca1b 2004-05-15 devnull strcpy(path, buf); /* assuming there's room in path */
311 5cedca1b 2004-05-15 devnull } else fprintf(stderr, "%s: no room to grow path %s", prog, path);
312 5cedca1b 2004-05-15 devnull } /* End if */
313 5cedca1b 2004-05-15 devnull } /* End if */
314 5cedca1b 2004-05-15 devnull
315 5cedca1b 2004-05-15 devnull } /* End of DWBprefix */
316 5cedca1b 2004-05-15 devnull
317 5cedca1b 2004-05-15 devnull /*****************************************************************************/