File: config.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (517 lines) | stat: -rw-r--r-- 10,864 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* config.c:
 *
 * functions that deal with the particular configuration.
 *
 * jim frost 10.03.89
 *
 * Copyright 1989, 1990, 1991, 1993 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include <ctype.h> /* must here for debugging version under AIX */
#include "xloadimage.h"
#include <X11/Xos.h>
#include <sys/stat.h>
#ifndef VMS
#include <pwd.h>
#include <sys/types.h>
#endif
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>

/* SUPPRESS 530 */
/* SUPPRESS 560 */

struct filter *Filters = (struct filter *)NULL;

static unsigned int  NumPaths= 0;
static unsigned int  NumExts= 0;
static char         *Paths[BUFSIZ];
static char         *Exts[BUFSIZ];
static char         *PathToken= "path";
static char         *ExtToken= "extension";
static char         *FilterToken= "filter";

static void addFilter(extension, filter)
     char *extension, *filter;
{ struct filter *f, *t;

  f= (struct filter *)lmalloc(sizeof(struct filter));
  f->extension= dupString(extension);
  f->filter= dupString(filter);
  f->next= NULL;
  if (Filters) {
    for (t= Filters; t->next; t= t->next)
      /* EMPTY */
      ;
    t->next= f;
  }
  else
    Filters= f;
}

enum parse_state {
  parse_void,
  parse_state_token,
  parse_path,
  parse_extension,
  parse_filter_name,
  parse_filter_extension
};

/* this function eats up whitespace, incrementing the line number at
 * each newline
 */
static void skip_whitespace(f, linenum)
     FILE *f;
     unsigned int *linenum;
{
  int c;

  for (;;) {
    c = fgetc(f);
    switch (c) {
    case EOF:
      return;
    case '\n':
      (*linenum)++;
      /* FALLTHRU */
    case '\r':
    case ' ':
    case '\t':
      continue;
    default:
      ungetc(c, f);
      return;
    }
  }
}


static void readPathsAndExts(name)
     char *name;
{ FILE         *f;
  char          buf[BUFSIZ];
  char          filter_name[BUFSIZ];
  enum parse_state state;
  unsigned int  linenum;
  unsigned int  a;
  int           c;

  if (! (f= fopen(name, "r")))
    return;

  state = parse_void;
  linenum= 0;

  /* this is the token scanner.  i suppose i could have used lex.
   */
  a = 0;
  for (;;) {
    c = fgetc(f);
    if (a >= BUFSIZ) {
      fprintf(stderr, "%s: %d: Buffer overflow (token too long, sorry).\n", name, linenum);
      fclose(f);
      return;
    }
    switch (c) {
    case EOF:
      return;
    case '\n': /* increment line count */
      linenum++;
      /* FALLTHRU */
    case '\r': /* treated as generic white space */
    case ' ':
    case '\t':
      buf[a] = '\0';
      a = 0;
      /* skip forward until next character */
      skip_whitespace(f, &linenum);
      c = fgetc(f);
      if (c == '=') {
	state = parse_state_token;
	skip_whitespace(f, &linenum);
      }
      else if (c != EOF)
	ungetc(c, f);
      break;
    case '=':
      buf[a] = '\0';
      a = 0;
      state = parse_state_token;
      skip_whitespace(f, &linenum);
      break;
    case '#': /* comment */
      for (;;) {
	c = fgetc(f);
	switch (c) { /* eat everything to newline or EOF */
	case '\n':
	  linenum++;
	case EOF:
	  break;
	default:
	  continue;
	}
	break;
      }

      /* terminate token
       */
      buf[a] = '\0';
      a = 0;
      break;
    case '"': /* search for end quote */
      while ((c = fgetc(f)) != EOF) {
	if (a >= BUFSIZ) {
	  fprintf(stderr, "%s: %d: Buffer overflow while reading string literal.\n",
		  name, linenum);
	  fclose(f);
	  return;
	}
	switch (c) {
	case '"':
	  goto done_quote;
	case '\r':
	  fprintf(stderr, "%s: %d: Unquoted return character inside string literal.\n",
		  name, linenum);
	  fclose(f);
	  return;
	case '\n':
	  fprintf(stderr, "%s: %d: Unquoted newline inside string literal.\n", name,
		  linenum);
	  fclose(f);
	  return;
	case '\\':
	  c = fgetc(f);
	  if (c == EOF)
	    goto done_quote;
	  if (c == '\n')
	    linenum++;
	  /* FALLTHRU */
	default:
	  buf[a++] = c;
	  continue;
	}
      }
    done_quote:
      continue;
    case '\\': /* literal quote */
      c = fgetc(f);
      if (c == EOF)
	continue;
      if (c == '\n')
	linenum++;
      /* FALLTHRU */
    default:
      buf[a++] = c;
      continue;
    }

    /* this disallows nil-string tokens.  thus "" is always ignored.
     */
    if (buf[0] == '\0')
      continue;

    /* this handles a token depending on the parser state
     */
    switch (state) {
    case parse_void:
      fprintf(stderr, "%s: %d: Syntax error in defaults file\n", name, linenum);
      fclose(f);
      return;

    case parse_state_token:
      if (!strncmp(buf, PathToken, strlen(PathToken)))
	state= parse_path;
      else if (!strncmp(buf, ExtToken, strlen(ExtToken)))
	state= parse_extension;
      else if (!strncmp(buf, FilterToken, strlen(FilterToken)))
	state= parse_filter_name;
      else {
	fprintf(stderr, "%s: %d: Unknown section specifier '%s'.\n",
		name, linenum, buf);
	return;
      }
      break;
    case parse_path:
      if (NumPaths < BUFSIZ - 1)
	Paths[NumPaths++]= expandPath(buf);
      else {
	fprintf(stderr, "%s: %d: Path table overflow\n", name, linenum);
	fclose(f);
	return;
      }
      break;
    case parse_extension:
      if (NumExts < BUFSIZ - 1)
	Exts[NumExts++]= dupString(buf);
      else {
	fprintf(stderr, "%s: %d: Extension table overflow\n", name, linenum);
	fclose(f);
	return;
      }
      break;
    case parse_filter_name: /* name of filter program */
      strncpy(filter_name, buf, BUFSIZ - 1);
      filter_name[BUFSIZ - 1] = '\0';
      state= parse_filter_extension;
      break;
    case parse_filter_extension:
      addFilter(buf, filter_name);
      break;
    }
  }
}

void loadPathsAndExts()
{ static int     havepaths= 0;
#ifndef VMS
  struct passwd *pw;
#endif
  char           buf[BUFSIZ];

  if (havepaths)
    return;
  havepaths= 1;

#ifdef VMS
  sprintf(buf, "/sys$login:xloadimage.rc");
#else /* !VMS */
  addFilter(".Z", "uncompress -c"); /* std UNIX uncompress */
  addFilter(".gz", "gzip -cd");     /* same, new extension */
  if (! (pw= (struct passwd *)getpwuid(getuid()))) {
    printf("Can't find your password file entry?!?\n");
    return;
  }
  sprintf(buf, "%s/.xloadimagerc", pw->pw_dir);
#endif /* !VMS */
  if (! access(buf, R_OK)) {
    readPathsAndExts(buf);
    return; /* don't read system file if user has one */
  }
#ifdef SYSPATHFILE
  readPathsAndExts(SYSPATHFILE);
#endif
}

static int fileIsOk(fullname, sbuf)
     char *fullname;
     struct stat *sbuf;
{
  if ((sbuf->st_mode & S_IFMT) == S_IFDIR) /* is a directory */
    return(0);
  return(access(fullname, R_OK)); /* we can read it */
}

/* find an image with paths and extensions from defaults files.  returns
 * -1 if access denied or not found, 0 if ok.
 */

int findImage(name, fullname)
     char *name, *fullname;
{ unsigned int   p, e;
  struct stat    sbuf;

  strcpy(fullname, name);
  if (!strcmp(name, "stdin")) /* stdin is special name */
    return(0);

  /* look for name and name with compress extension
   */
  if (! stat(fullname, &sbuf))
      return(fileIsOk(fullname, &sbuf));
#ifndef NO_COMPRESS
  strcat(fullname, ".Z");
  if (! stat(fullname, &sbuf))
      return(fileIsOk(fullname, &sbuf));
#endif

  for (p= 0; p < NumPaths; p++) {
#ifdef VMS
    sprintf(fullname, "%s%s", Paths[p], name);
#else
    sprintf(fullname, "%s/%s", Paths[p], name);
#endif
    if (! stat(fullname, &sbuf))
      return(fileIsOk(fullname, &sbuf));
#ifndef NO_COMPRESS
    strcat(fullname, ".Z");
    if (! stat(fullname, &sbuf))
#endif
      return(fileIsOk(fullname, &sbuf));
    for (e= 0; e < NumExts; e++) {
#ifdef VMS
      sprintf(fullname, "%s%s%s", Paths[p], name, Exts[e]);
#else
      sprintf(fullname, "%s/%s%s", Paths[p], name, Exts[e]);
#endif
      if (! stat(fullname, &sbuf))
	return(fileIsOk(fullname, &sbuf));
#ifndef NO_COMPRESS
      strcat(fullname, ".Z");
      if (! stat(fullname, &sbuf))
	return(fileIsOk(fullname, &sbuf));
#endif
    }
  }

  for (e= 0; e < NumExts; e++) {
    sprintf(fullname, "%s%s", name, Exts[e]);
    if (! stat(fullname, &sbuf))
      return(fileIsOk(fullname, &sbuf));
#ifndef NO_COMPRESS
    strcat(fullname, ".Z");
    if (! stat(fullname, &sbuf))
      return(fileIsOk(fullname, &sbuf));
#endif
  }
  errno= ENOENT; /* file not found */
  return(-1);
}

/* list images along our path
 */

void listImages()
{ unsigned int a;
  char         buf[BUFSIZ];

  if (!NumPaths) {
    printf("No image path\n");
    return;
  }
  for (a= 0; a < NumPaths; a++) {
    printf("%s:\n", Paths[a]);
    fflush(stdout);
#ifdef VMS
    sprintf(buf, "directory %s", Paths[a]);
#else
    sprintf(buf, "ls %s", Paths[a]);
#endif
    if (system(buf) < 0) {
#ifdef VMS
      perror("directory");
#else
      perror("ls");
#endif
      return;
    }
  }
  return;
}

void showConfiguration()
{ int a;
  struct filter *f;

  if (NumPaths) {
    printf("Image path:");
    for (a= 0; a < NumPaths; a++)
      printf(" %s", Paths[a]);
    printf("\n");
  }
  else
    printf("No image path\n");
  if (NumExts) {
    printf("Image extensions:");
    for (a= 0; a < NumExts; a++)
      printf(" %s", Exts[a]);
    printf("\n");
  }
  else
    printf("No image extensions\n");
  if (Filters) {
    printf("Filters:\n");
    for (f= Filters; f; f= f->next)
      printf(" \"%s\" -> \"%s\"\n", f->extension, f->filter);
    printf("\n");
  }
  else
    printf("No filters\n");
}

/* Work out where this user's home directory is, or default to '/' */
/* XXX needs a VMS guru to supply something plausable for VMS */
static char *homedir()
{ char *p;
  struct passwd *pw;

  p = getenv("HOME");
  if (p) return p;

  /* try for a password file lookup instead */
  pw = getpwuid(getuid());
  if (!pw)
    return "/"; /* XXX maybe print message? */
  else
    return pw->pw_dir;
}

char *expandPath(p)
     char *p;
{ char buf1[BUFSIZ], buf2[BUFSIZ];
  int b1, b2, var;
  char *ptr;

  buf1[0] = '\0';
  buf2[0] = '\0';
  b1 = 0;
  b2 = 0;
  var = 0;

  while(*p) {
    if(isspace(*p)) break;
#ifndef VMS
    if (*p == '$') var++;
#endif
    else if(*p == '~') {
      buf1[b1] = '\0';
      strncat(buf1, homedir(), BUFSIZ - strlen(buf1) - 1);
      buf1[BUFSIZ - 1] = '\0';
      b1 = strlen(buf1);
      var = 0;
    }
    else if(*p == '/' || *p == '}') {
      if(var) {
	buf1[b1] = '\0';
	buf2[b2] = '\0';
	strncat(buf1, getenv(buf2), BUFSIZ - strlen (buf1) - 1);
	buf1[BUFSIZ - 1] = '\0';
	b1 = strlen(buf1);
	buf2[0] = '\0';
	b2 = 0;
	var = 0;
      }
      if(*p == '/') {
	buf1[b1] = *p;
	b1++;
      }
    }
    else if(var) {
      if(*p != '{') {
	buf2[b2] = *p;
	b2++;
      }
    }
    else {
      buf1[b1] = *p;
      b1++;
    }
    p++;
  }

  buf1[b1] = '\0';
  
  if((b2 = strlen(buf1)) > 0) {
    ptr = (char *)lmalloc((unsigned) b2+1);
    strcpy(ptr, buf1);
    return(ptr);
  }
  else
    return(NULL);

}