1 | /* |
---|
2 | ** docxref.c |
---|
3 | ** |
---|
4 | ** Driver for lex based scanner. Arranges for stdin to be each named |
---|
5 | ** file in turn, so that yylex() never has to know about files. |
---|
6 | ** Some of this code is not pretty, but it's not too bad. |
---|
7 | ** |
---|
8 | ** Arnold Robbins, Information and Computer Science, Georgia Tech |
---|
9 | ** gatech!arnold |
---|
10 | ** Copyright (c) 1984 by Arnold Robbins. |
---|
11 | ** All rights reserved. |
---|
12 | ** This program may not be sold, but may be distributed |
---|
13 | ** provided this header is included. |
---|
14 | */ |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <string.h> |
---|
19 | #include <ctype.h> |
---|
20 | #include <libgen.h> |
---|
21 | |
---|
22 | #define TRUE 1 |
---|
23 | #define FALSE 0 |
---|
24 | |
---|
25 | extern char yytext[]; |
---|
26 | extern int yyleng; |
---|
27 | extern void yylex(void); |
---|
28 | |
---|
29 | int line_no = 1; /* current line number */ |
---|
30 | char *fname = NULL; /* current file name */ |
---|
31 | |
---|
32 | static void usage(char *name); |
---|
33 | |
---|
34 | int main(int argc, char **argv) |
---|
35 | { |
---|
36 | FILE saved_in, *fp; |
---|
37 | char *name; |
---|
38 | int more_input = FALSE; /* more files to process */ |
---|
39 | int read_stdin = FALSE; |
---|
40 | |
---|
41 | name = basename(argv[0]); /* save command name */ |
---|
42 | fname = "stdin"; /* assume stdin */ |
---|
43 | |
---|
44 | if(argc == 1) |
---|
45 | { |
---|
46 | yylex(); |
---|
47 | exit(0); |
---|
48 | } |
---|
49 | |
---|
50 | if(argv[1][0] == '-' && argv[1][1] != '\0') |
---|
51 | usage(argv[0]); /* will exit */ |
---|
52 | |
---|
53 | saved_in = *stdin; |
---|
54 | /* save stdin in case "-" is found in middle of command line */ |
---|
55 | |
---|
56 | for(--argc, argv++; argc > 0; --argc, argv++) |
---|
57 | { |
---|
58 | if(fileno(stdin) != fileno((&saved_in)) || read_stdin) |
---|
59 | fclose(stdin); |
---|
60 | /* free unix file descriptors */ |
---|
61 | |
---|
62 | if(strcmp(*argv, "-") == 0) |
---|
63 | { |
---|
64 | *stdin = saved_in; |
---|
65 | fname = "stdin"; |
---|
66 | read_stdin = TRUE; |
---|
67 | more_input = (argc - 1 > 0); |
---|
68 | } |
---|
69 | else if((fp = fopen(*argv,"r")) == NULL) |
---|
70 | { |
---|
71 | fprintf(stderr,"%s: can't open %s\n", name, *argv); |
---|
72 | continue; |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | *stdin = *fp; |
---|
77 | /* do it this way so that yylex() */ |
---|
78 | /* never knows about files etc. */ |
---|
79 | more_input = (argc - 1 > 0); |
---|
80 | fname = *argv; |
---|
81 | } |
---|
82 | |
---|
83 | yylex(); /* do the work */ |
---|
84 | |
---|
85 | if(more_input) |
---|
86 | line_no = 1; |
---|
87 | } |
---|
88 | return(0); |
---|
89 | } |
---|
90 | |
---|
91 | static void usage(char *name) |
---|
92 | { |
---|
93 | fprintf(stderr,"usage: %s [files]\n", name); |
---|
94 | exit(1); |
---|
95 | } |
---|