1 | #ifndef lint |
---|
2 | #define _NOTICE static char |
---|
3 | _NOTICE N1[] = "Copyright (c) 1985,1987,1990,1991,1992 Adobe Systems Incorporated"; |
---|
4 | _NOTICE N2[] = "GOVERNMENT END USERS: See Notice file in TranScript library directory"; |
---|
5 | _NOTICE N3[] = "-- probably /usr/lib/ps/Notice"; |
---|
6 | _NOTICE RCSID[]="$Header: /afs/dev.mit.edu/source/repository/third/transcript/src/psutil.c,v 1.2 1996-10-14 05:04:43 ghudson Exp $"; |
---|
7 | #endif |
---|
8 | /* psutil.c |
---|
9 | * |
---|
10 | * Copyright (C) 1985,1987,1990,1991,1992 Adobe Systems Incorporated. All |
---|
11 | * rights reserved. |
---|
12 | * GOVERNMENT END USERS: See Notice file in TranScript library directory |
---|
13 | * -- probably /usr/lib/ps/Notice |
---|
14 | * |
---|
15 | * common utility subroutines |
---|
16 | * |
---|
17 | * RCSLOG: |
---|
18 | * $/Log: psutil.c,v/$ |
---|
19 | * Revision 3.10 1993/05/25 21:42:27 snichols |
---|
20 | * cleanup for Solaris |
---|
21 | * |
---|
22 | * Revision 3.9 1992/11/25 20:17:05 snichols |
---|
23 | * Quotestring should be set back to false if no quote if found. |
---|
24 | * |
---|
25 | * Revision 3.8 1992/11/23 23:17:00 snichols |
---|
26 | * fixed bug in multi-line values. |
---|
27 | * |
---|
28 | * Revision 3.7 1992/08/21 16:26:32 snichols |
---|
29 | * Release 4.0 |
---|
30 | * |
---|
31 | * Revision 3.6 1992/07/14 22:11:41 snichols |
---|
32 | * Updated copyrights. |
---|
33 | * |
---|
34 | * Revision 3.5 1992/06/01 21:32:58 snichols |
---|
35 | * fixed bug in composing ppd file name under certain circumstances. |
---|
36 | * |
---|
37 | * Revision 3.4 1992/05/18 20:31:13 snichols |
---|
38 | * Support for SymbolValues in PPD files. |
---|
39 | * |
---|
40 | * Revision 3.3 1992/04/21 21:00:17 snichols |
---|
41 | * Fixed infinite loop bug in GetPPD. |
---|
42 | * |
---|
43 | * Revision 3.2 1992/04/10 16:28:27 snichols |
---|
44 | * exported ParseLine so print panel code could use it. |
---|
45 | * |
---|
46 | * Revision 3.1 1992/01/16 23:27:27 snichols |
---|
47 | * added a null return at end of parseppd; this was the source of |
---|
48 | * unexplained core dumps on sparcs. |
---|
49 | * |
---|
50 | * Revision 3.0 1991/06/17 16:52:41 snichols |
---|
51 | * Release 3.0 |
---|
52 | * |
---|
53 | * Revision 2.12 1991/06/11 23:25:38 snichols |
---|
54 | * needed to compare to *mainkey, not mainkey, in ParseLine. |
---|
55 | * |
---|
56 | * Revision 2.11 1991/05/13 23:54:36 snichols |
---|
57 | * slashes inside quotes do not signal translations. |
---|
58 | * |
---|
59 | * Revision 2.10 1991/04/19 22:56:06 snichols |
---|
60 | * initialize "slash" before using it in ParseLine; not all architectures |
---|
61 | * like it otherwise. |
---|
62 | * |
---|
63 | * Revision 2.9 1991/03/28 23:47:32 snichols |
---|
64 | * isolated code for finding PPD files to one routine, in psutil.c. |
---|
65 | * |
---|
66 | * Revision 2.8 1991/03/14 22:33:49 snichols |
---|
67 | * special case code for distinguishing filename slashes from translation |
---|
68 | * slashes in PPD files. |
---|
69 | * |
---|
70 | * Revision 2.7 1991/03/06 21:53:03 snichols |
---|
71 | * left out arg to strncmp. |
---|
72 | * |
---|
73 | * Revision 2.6 1991/02/07 13:52:01 snichols |
---|
74 | * rewrote parser; now handles 4.0 PPD Files. |
---|
75 | * |
---|
76 | * Revision 2.5 90/12/12 10:46:26 snichols |
---|
77 | * Changed everything to use strchr instead of index. |
---|
78 | * |
---|
79 | * Revision 2.4 90/10/11 15:05:26 snichols |
---|
80 | * fixed minor bug in processing mainkeyword. |
---|
81 | * |
---|
82 | * Revision 2.3 90/08/08 17:34:34 snichols |
---|
83 | * Added parseppd routine, which parses printer description files. |
---|
84 | * |
---|
85 | * Revision 2.2 87/11/17 16:52:49 byron |
---|
86 | * Release 2.1 |
---|
87 | * |
---|
88 | * Revision 2.1.1.2 87/11/12 13:42:10 byron |
---|
89 | * Changed Government user's notice. |
---|
90 | * |
---|
91 | * Revision 2.1.1.1 87/04/23 10:26:58 byron |
---|
92 | * Copyright notice. |
---|
93 | * |
---|
94 | * Revision 2.1 85/11/24 11:51:18 shore |
---|
95 | * Product Release 2.0 |
---|
96 | * |
---|
97 | * Revision 1.3 85/11/20 00:57:02 shore |
---|
98 | * Support for System V |
---|
99 | * fixed bug in copyfile |
---|
100 | * added envget and Sys V gethostname |
---|
101 | * |
---|
102 | * Revision 1.2 85/05/14 11:28:42 shore |
---|
103 | * |
---|
104 | * |
---|
105 | * |
---|
106 | */ |
---|
107 | |
---|
108 | #include "config-defs.h" |
---|
109 | #include <stdio.h> |
---|
110 | #ifdef HAVE_FCNTL_H |
---|
111 | #include <fcntl.h> |
---|
112 | #else |
---|
113 | #include <sys/file.h> |
---|
114 | #endif |
---|
115 | #include <string.h> |
---|
116 | #include "transcript.h" |
---|
117 | |
---|
118 | extern char *getenv(); |
---|
119 | |
---|
120 | /* copy from file named fn to stream stm */ |
---|
121 | /* use read and write in hopes that it goes fast */ |
---|
122 | copyfile(fn, stm) |
---|
123 | char *fn; |
---|
124 | register FILE *stm; |
---|
125 | { |
---|
126 | int fd, fo; |
---|
127 | register int pcnt; |
---|
128 | char buf[BUFSIZ]; |
---|
129 | |
---|
130 | VOIDC fflush(stm); |
---|
131 | fo = fileno(stm); |
---|
132 | if ((fd = open(fn, O_RDONLY, 0)) < 0) return(-1); |
---|
133 | while ((pcnt = read(fd, buf, sizeof buf)) > 0) { |
---|
134 | if (write(fo, buf, (unsigned) pcnt) != pcnt) return(-2); |
---|
135 | } |
---|
136 | if (pcnt < 0) {perror("copyfile"); return (-1);} |
---|
137 | VOIDC close(fd); |
---|
138 | VOIDC fflush(stm); |
---|
139 | return(0); |
---|
140 | } |
---|
141 | |
---|
142 | /* exit with message and code */ |
---|
143 | VOID pexit(message, code) |
---|
144 | char *message; |
---|
145 | int code; |
---|
146 | { |
---|
147 | perror(message); |
---|
148 | exit(code); |
---|
149 | } |
---|
150 | /* exit with message and code */ |
---|
151 | VOID pexit2(prog, message, code) |
---|
152 | char *prog, *message; |
---|
153 | int code; |
---|
154 | { |
---|
155 | fprintf(stderr,"%s: ",prog); |
---|
156 | VOIDC perror(message); |
---|
157 | VOIDC exit(code); |
---|
158 | } |
---|
159 | |
---|
160 | /* concatenate s1 and s2 into s0 (of length l0), die if too long |
---|
161 | * returns a pointer to the null-terminated result |
---|
162 | */ |
---|
163 | char *mstrcat(s0,s1,s2,l0) |
---|
164 | char *s0,*s1,*s2; |
---|
165 | int l0; |
---|
166 | { |
---|
167 | if (((int)strlen(s1) + (int)strlen(s2)) >= l0) { |
---|
168 | fprintf(stderr,"concatenate overflow %s%s\n",s1,s2); |
---|
169 | VOIDC exit(2); |
---|
170 | } |
---|
171 | return strcat(((s0 == s1) ? s0 : strcpy(s0,s1)),s2); |
---|
172 | } |
---|
173 | |
---|
174 | /* envget is a getenv |
---|
175 | * if the variable is not present in the environment or |
---|
176 | * it has the null string as value envget returns NULL |
---|
177 | * otherwise it returns the value from the environment |
---|
178 | */ |
---|
179 | |
---|
180 | char *envget(var) |
---|
181 | char *var; |
---|
182 | { |
---|
183 | register char *val; |
---|
184 | if (((val = getenv(var)) == NULL) || (*val == '\0')) |
---|
185 | return ((char *) NULL); |
---|
186 | else return (val); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /* System V specific compatibility stuff */ |
---|
191 | |
---|
192 | #ifdef SYSV |
---|
193 | |
---|
194 | #include <sys/types.h> |
---|
195 | #include <sys/utsname.h> |
---|
196 | |
---|
197 | #define SYSVNAMELIMIT 9 |
---|
198 | |
---|
199 | gethostname(name, namelen) |
---|
200 | char *name; |
---|
201 | int namelen; |
---|
202 | { |
---|
203 | struct utsname uts; |
---|
204 | uname(&uts); |
---|
205 | VOIDC strncpy(name,uts.sysname,SYSVNAMELIMIT); |
---|
206 | return(0); |
---|
207 | } |
---|
208 | |
---|
209 | #endif |
---|
210 | |
---|
211 | FILE *GetPPD(printer) |
---|
212 | char *printer; |
---|
213 | { |
---|
214 | FILE *fp; |
---|
215 | char ppdfile[TSPATHSIZE]; |
---|
216 | char *ppd; |
---|
217 | char *p, *q; |
---|
218 | |
---|
219 | if (printer == NULL) |
---|
220 | return NULL; |
---|
221 | ppd = getenv("PPDPATH"); |
---|
222 | if (ppd) { |
---|
223 | q = ppd; |
---|
224 | while (q != NULL) { |
---|
225 | p = strchr(q, ':'); |
---|
226 | if (p) { |
---|
227 | *p = '\0'; |
---|
228 | p++; |
---|
229 | } |
---|
230 | strncpy(ppdfile, q, TSPATHSIZE); |
---|
231 | strncat(ppdfile, "/", TSPATHSIZE); |
---|
232 | strncat(ppdfile,printer,TSPATHSIZE); |
---|
233 | strncat(ppdfile,".ppd",TSPATHSIZE); |
---|
234 | if ((fp = fopen(ppdfile, "r")) != NULL) |
---|
235 | return fp; |
---|
236 | if ((int)strlen(printer) > 10) { |
---|
237 | strncpy(ppdfile, q, TSPATHSIZE); |
---|
238 | strncat(ppdfile, "/", TSPATHSIZE); |
---|
239 | strncat(ppdfile, printer, 10); |
---|
240 | strncat(ppdfile, ".ppd", TSPATHSIZE); |
---|
241 | if ((fp = fopen(ppdfile, "r")) != NULL) |
---|
242 | return fp; |
---|
243 | } |
---|
244 | q = p; |
---|
245 | } |
---|
246 | } |
---|
247 | else { |
---|
248 | strncpy(ppdfile,PPDDir,TSPATHSIZE); |
---|
249 | strncat(ppdfile,printer,TSPATHSIZE); |
---|
250 | strncat(ppdfile,".ppd",TSPATHSIZE); |
---|
251 | if ((fp = fopen(ppdfile,"r")) != NULL) |
---|
252 | return fp; |
---|
253 | /* try shorter name */ |
---|
254 | if ((int)strlen(printer) > 10) { |
---|
255 | strncpy(ppdfile,PPDDir,TSPATHSIZE); |
---|
256 | strncat(ppdfile,printer,10); |
---|
257 | strncat(ppdfile,".ppd",TSPATHSIZE); |
---|
258 | if ((fp = fopen(ppdfile, "r")) != NULL) |
---|
259 | return fp; |
---|
260 | } |
---|
261 | } |
---|
262 | return NULL; |
---|
263 | } |
---|
264 | |
---|
265 | int ParseLine(line, mainkey, option, opttran, val, valtran) |
---|
266 | char *line; |
---|
267 | char **mainkey; |
---|
268 | char **option; |
---|
269 | char **opttran; |
---|
270 | char **val; |
---|
271 | char **valtran; |
---|
272 | { |
---|
273 | char *blank, *colon, *value, *slash; |
---|
274 | char *endofquote; |
---|
275 | char *q; |
---|
276 | |
---|
277 | blank = strchr(line,' '); |
---|
278 | colon = strchr(line,':'); |
---|
279 | if (blank) { |
---|
280 | *blank = '\0'; |
---|
281 | blank++; |
---|
282 | } |
---|
283 | else return -1; |
---|
284 | if (colon) { |
---|
285 | *colon = '\0'; |
---|
286 | colon++; |
---|
287 | } |
---|
288 | else return -2; |
---|
289 | |
---|
290 | *mainkey = line; |
---|
291 | *option = NULL; |
---|
292 | *opttran = NULL; |
---|
293 | *val = NULL; |
---|
294 | *valtran = NULL; |
---|
295 | |
---|
296 | slash = NULL; |
---|
297 | |
---|
298 | if (colon < blank) { |
---|
299 | /* no option, get value */ |
---|
300 | value = colon; |
---|
301 | value++; /* need to skip over extra null */ |
---|
302 | while (*value == ' ') value++; |
---|
303 | if (*value == '"') { |
---|
304 | q = value; |
---|
305 | q++; |
---|
306 | if (q) |
---|
307 | endofquote = strchr(q,'"'); |
---|
308 | } |
---|
309 | else endofquote = value; |
---|
310 | if (strcmp(*mainkey, "*Include")) { |
---|
311 | /* special case include, and don't search for value |
---|
312 | translations */ |
---|
313 | if (endofquote) |
---|
314 | slash = strchr(endofquote,'/'); |
---|
315 | if (slash) { |
---|
316 | /* value translation */ |
---|
317 | *slash = '\0'; |
---|
318 | slash++; |
---|
319 | *valtran = slash; |
---|
320 | } |
---|
321 | } |
---|
322 | *val = value; |
---|
323 | return 1; |
---|
324 | } |
---|
325 | /* there is an option keyword */ |
---|
326 | while (*blank == ' ') blank++; |
---|
327 | /* blank is now pointing to the beginning of option */ |
---|
328 | *option = blank; |
---|
329 | while (*colon == ' ') colon++; |
---|
330 | /* colon is now pointing to beginning of value */ |
---|
331 | value = colon; |
---|
332 | slash = strchr(blank,'/'); |
---|
333 | if (slash) { |
---|
334 | /* option translation */ |
---|
335 | *slash = '\0'; |
---|
336 | slash++; |
---|
337 | *opttran = slash; |
---|
338 | } |
---|
339 | if (*value == '"') { |
---|
340 | q = value; |
---|
341 | q++; |
---|
342 | if (q) |
---|
343 | endofquote = strchr(q,'"'); |
---|
344 | } |
---|
345 | else endofquote = value; |
---|
346 | if (endofquote) |
---|
347 | slash = strchr(endofquote,'/'); |
---|
348 | if (slash) { |
---|
349 | /* value translation */ |
---|
350 | *slash = '\0'; |
---|
351 | slash++; |
---|
352 | *valtran = slash; |
---|
353 | } |
---|
354 | *val = value; |
---|
355 | return 1; |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | char *parseppd(fptr,mainkeyword, optionkeyword) |
---|
361 | FILE *fptr; |
---|
362 | char *mainkeyword, *optionkeyword; |
---|
363 | { |
---|
364 | /* looks for mainkeyword optionkeyword, returns the value, without the |
---|
365 | quote marks */ |
---|
366 | |
---|
367 | char buf[255]; |
---|
368 | char mkey[255]; |
---|
369 | char *p,*q; |
---|
370 | char *vstring; |
---|
371 | char *key, *option, *value, *otran, *vtran; |
---|
372 | int ret; |
---|
373 | int multiline; |
---|
374 | int quotestring; |
---|
375 | int sizeRemaining; |
---|
376 | FILE *incfile; |
---|
377 | char incname[TSPATHSIZE]; |
---|
378 | char *temp; |
---|
379 | char *beg, *end; |
---|
380 | |
---|
381 | if ((vstring = (char *)malloc(255)) == NULL) |
---|
382 | return NULL; |
---|
383 | vstring[0] = '\0'; |
---|
384 | sizeRemaining = 255; |
---|
385 | quotestring = FALSE; |
---|
386 | multiline = FALSE; |
---|
387 | strncpy(mkey, mainkeyword, 255); |
---|
388 | while (fgets(buf,255,fptr)) { |
---|
389 | if (buf[0] == '*') { |
---|
390 | if (buf[1] == '%') /* discard comments */ |
---|
391 | continue; |
---|
392 | p = strchr(buf,'\n'); |
---|
393 | if (p) *p = '\0'; |
---|
394 | ret = ParseLine(buf, &key, &option, &otran, &value, &vtran); |
---|
395 | if (ret > 0) { |
---|
396 | if (!strncmp(key,"*Include",8)) { |
---|
397 | /* check include files, too */ |
---|
398 | /* +++ check on file name definition */ |
---|
399 | /* strip off delimiters */ |
---|
400 | beg = value; |
---|
401 | beg++; |
---|
402 | end = strrchr(beg, *value); |
---|
403 | if (end) *end = '\0'; |
---|
404 | if (*beg != '/') { |
---|
405 | /* tack on ppddir */ |
---|
406 | strncpy(incname, PPDDir, TSPATHSIZE); |
---|
407 | strncat(incname, beg, TSPATHSIZE); |
---|
408 | } |
---|
409 | else |
---|
410 | strncpy(incname, beg, TSPATHSIZE); |
---|
411 | if ((incfile = fopen(incname,"r")) == NULL) |
---|
412 | continue; |
---|
413 | /* oooo, recursion! */ |
---|
414 | temp = parseppd(incfile, mkey, optionkeyword); |
---|
415 | if (temp) |
---|
416 | return temp; |
---|
417 | continue; |
---|
418 | } |
---|
419 | if (!strncmp(key, "*End", 4)) { |
---|
420 | if (multiline) |
---|
421 | return vstring; |
---|
422 | } |
---|
423 | if (*value == '"') { |
---|
424 | /* if there's no closing ", multi-line value */ |
---|
425 | q = value; q++; |
---|
426 | if ((p = strrchr(q,'"')) == NULL) |
---|
427 | quotestring = TRUE; |
---|
428 | else |
---|
429 | *p = '\0'; |
---|
430 | *value = '\0'; |
---|
431 | value++; |
---|
432 | } |
---|
433 | else { |
---|
434 | quotestring = FALSE; |
---|
435 | } |
---|
436 | if (!strcmp(key, mkey)) { |
---|
437 | /* matched main keyword */ |
---|
438 | if (optionkeyword) { |
---|
439 | if (option) { |
---|
440 | if (!strcmp(option, optionkeyword)) { |
---|
441 | /* matched both, set vstring */ |
---|
442 | if (*value == '^') { |
---|
443 | /* SymbolValue, actual value later in |
---|
444 | file */ |
---|
445 | temp = parseppd(fptr, "*SymbolValue", |
---|
446 | value); |
---|
447 | if (temp) |
---|
448 | return temp; |
---|
449 | else return NULL; |
---|
450 | } |
---|
451 | else { |
---|
452 | strcpy(vstring,value); |
---|
453 | sizeRemaining -= (int)strlen(value); |
---|
454 | if (quotestring) |
---|
455 | multiline = TRUE; |
---|
456 | else |
---|
457 | return vstring; |
---|
458 | } |
---|
459 | } |
---|
460 | else if (otran) { |
---|
461 | if (!strcmp(optionkeyword,otran)) { |
---|
462 | /* matched translation */ |
---|
463 | strcpy(vstring,value); |
---|
464 | sizeRemaining -= (int)strlen(value); |
---|
465 | if (quotestring) |
---|
466 | multiline = TRUE; |
---|
467 | else |
---|
468 | return vstring; |
---|
469 | } |
---|
470 | } |
---|
471 | else |
---|
472 | continue; |
---|
473 | } |
---|
474 | else return NULL; |
---|
475 | } |
---|
476 | else { |
---|
477 | /* no options */ |
---|
478 | strcpy(vstring,value); |
---|
479 | sizeRemaining -= (int)strlen(value); |
---|
480 | if (quotestring) |
---|
481 | multiline = TRUE; |
---|
482 | else |
---|
483 | return vstring; |
---|
484 | } |
---|
485 | } |
---|
486 | else if (!strncmp(key, "*OpenUI", 7)) { |
---|
487 | /* check for match to translation of mkey */ |
---|
488 | if (vtran) { |
---|
489 | if (!strcmp(mkey, vtran)) { |
---|
490 | /* matched translation, replace with real key */ |
---|
491 | strncpy(mkey, value, 255); |
---|
492 | continue; |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | else continue; |
---|
497 | } |
---|
498 | else continue; |
---|
499 | } |
---|
500 | else { |
---|
501 | if (multiline) { |
---|
502 | /* check for closing ", and dump into vstring */ |
---|
503 | if (p = strrchr(buf,'"')) { |
---|
504 | *p = '\0'; |
---|
505 | multiline = FALSE; |
---|
506 | } |
---|
507 | if ((int)strlen(buf) >= sizeRemaining) { |
---|
508 | vstring = (char *)realloc(vstring, |
---|
509 | (int)strlen(vstring)+255); |
---|
510 | sizeRemaining = 255; |
---|
511 | } |
---|
512 | strncat(vstring, buf, 255); |
---|
513 | sizeRemaining -= (int)strlen(buf); |
---|
514 | if (!multiline) |
---|
515 | return vstring; |
---|
516 | } |
---|
517 | } |
---|
518 | } |
---|
519 | return NULL; |
---|
520 | } |
---|
521 | |
---|
522 | |
---|
523 | |
---|