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