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 <ctype.h> |
---|
18 | |
---|
19 | #define TRUE 1 |
---|
20 | #define FALSE 0 |
---|
21 | |
---|
22 | extern char yytext[]; |
---|
23 | extern int yyleng; |
---|
24 | |
---|
25 | int line_no = 1; /* current line number */ |
---|
26 | char *fname = NULL; /* current file name */ |
---|
27 | |
---|
28 | char *basename(); /* strips leading path of a file name */ |
---|
29 | |
---|
30 | main(argc,argv) |
---|
31 | int argc; |
---|
32 | char **argv; |
---|
33 | { |
---|
34 | FILE saved_in, *fp; |
---|
35 | char *name; |
---|
36 | int more_input = FALSE; /* more files to process */ |
---|
37 | int read_stdin = FALSE; |
---|
38 | |
---|
39 | name = basename(argv[0]); /* save command name */ |
---|
40 | fname = "stdin"; /* assume stdin */ |
---|
41 | |
---|
42 | if(argc == 1) |
---|
43 | { |
---|
44 | yylex(); |
---|
45 | exit(0); |
---|
46 | } |
---|
47 | |
---|
48 | if(argv[1][0] == '-' && argv[1][1] != '\0') |
---|
49 | usage(argv[0]); /* will exit */ |
---|
50 | |
---|
51 | saved_in = *stdin; |
---|
52 | /* save stdin in case "-" is found in middle of command line */ |
---|
53 | |
---|
54 | for(--argc, argv++; argc > 0; --argc, argv++) |
---|
55 | { |
---|
56 | if(fileno(stdin) != fileno((&saved_in)) || read_stdin) |
---|
57 | fclose(stdin); |
---|
58 | /* free unix file descriptors */ |
---|
59 | |
---|
60 | if(strcmp(*argv, "-") == 0) |
---|
61 | { |
---|
62 | *stdin = saved_in; |
---|
63 | fname = "stdin"; |
---|
64 | read_stdin = TRUE; |
---|
65 | more_input = (argc - 1 > 0); |
---|
66 | } |
---|
67 | else if((fp = fopen(*argv,"r")) == NULL) |
---|
68 | { |
---|
69 | fprintf(stderr,"%s: can't open %s\n", name, *argv); |
---|
70 | continue; |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | *stdin = *fp; |
---|
75 | /* do it this way so that yylex() */ |
---|
76 | /* never knows about files etc. */ |
---|
77 | more_input = (argc - 1 > 0); |
---|
78 | fname = *argv; |
---|
79 | } |
---|
80 | |
---|
81 | yylex(); /* do the work */ |
---|
82 | |
---|
83 | if(more_input) |
---|
84 | line_no = 1; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | outid() |
---|
89 | { |
---|
90 | char *basename(); |
---|
91 | |
---|
92 | printf("%s\t%s\t%d\n", yytext, basename(fname), line_no); |
---|
93 | } |
---|
94 | |
---|
95 | usage(name) |
---|
96 | char *name; |
---|
97 | { |
---|
98 | fprintf(stderr,"usage: %s [files]\n", name); |
---|
99 | exit(1); |
---|
100 | } |
---|
101 | |
---|
102 | #include "basename.c" |
---|