| 1 |
%{ |
| 2 |
|
| 3 |
/* |
| 4 |
* wc.lex : A simple example of using FLEX |
| 5 |
* to create a wc-like utility. |
| 6 |
* |
| 7 |
* See MISC/fastwc/ in the flex distribution for examples |
| 8 |
* of how to write this scanner for maximum performance. |
| 9 |
*/ |
| 10 |
|
| 11 |
int numchars = 0; |
| 12 |
int numwords = 0; |
| 13 |
int numlines = 0; |
| 14 |
int totchars = 0; |
| 15 |
int totwords = 0; |
| 16 |
int totlines = 0; |
| 17 |
|
| 18 |
/* |
| 19 |
* rules start from here |
| 20 |
*/ |
| 21 |
|
| 22 |
%} |
| 23 |
|
| 24 |
%% |
| 25 |
|
| 26 |
[\n] { numchars++; numlines++; } |
| 27 |
[\r] { numchars++; } |
| 28 |
[^ \t\n]+ { numwords++; numchars += yyleng; } |
| 29 |
. { numchars++; } |
| 30 |
|
| 31 |
%% |
| 32 |
|
| 33 |
/* |
| 34 |
* additional C code start from here. This supplies |
| 35 |
* all the argument processing etc. |
| 36 |
*/ |
| 37 |
|
| 38 |
int main(int argc, char *argv[]) |
| 39 |
{ |
| 40 |
int loop,first=1; |
| 41 |
int lflag = 0; /* 1 if we count # of lines */ |
| 42 |
int wflag = 0; /* 1 if we count # of words */ |
| 43 |
int cflag = 0; /* 1 if we count # of characters */ |
| 44 |
int fflag = 0; /* 1 if we have a file name */ |
| 45 |
|
| 46 |
for(loop=1; loop<argc; loop++){ |
| 47 |
if(argv[loop][0] == '-'){ |
| 48 |
switch(argv[loop][1]){ |
| 49 |
case 'l': |
| 50 |
lflag = 1; |
| 51 |
break; |
| 52 |
case 'w': |
| 53 |
wflag = 1; |
| 54 |
break; |
| 55 |
case 'c': |
| 56 |
cflag = 1; |
| 57 |
break; |
| 58 |
default: |
| 59 |
fprintf(stderr,"unknown option -%c\n", |
| 60 |
argv[loop][1]); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
if(lflag == 0 && wflag == 0 && cflag == 0){ |
| 65 |
lflag = wflag = cflag = 1; /* default to all on */ |
| 66 |
} |
| 67 |
|
| 68 |
for(loop=1; loop<argc; loop++){ |
| 69 |
if(argv[loop][0] != '-'){ |
| 70 |
fflag = 1; |
| 71 |
numlines = numchars = numwords = 0; |
| 72 |
if((yyin = fopen(argv[loop],"rb")) != NULL){ |
| 73 |
if(first){ |
| 74 |
first = 0; |
| 75 |
} else { |
| 76 |
YY_NEW_FILE; |
| 77 |
} |
| 78 |
(void) yylex(); |
| 79 |
fclose(yyin); |
| 80 |
totwords += numwords; |
| 81 |
totchars += numchars; |
| 82 |
totlines += numlines; |
| 83 |
printf("file : %25s :",argv[loop]) ; |
| 84 |
if(lflag){ |
| 85 |
fprintf(stdout,"lines %5d ",numlines); |
| 86 |
} |
| 87 |
if(cflag){ |
| 88 |
fprintf(stdout,"characters %5d ",numchars); |
| 89 |
} |
| 90 |
if(wflag){ |
| 91 |
fprintf(stdout,"words %5d ",numwords); |
| 92 |
} |
| 93 |
fprintf(stdout,"\n"); |
| 94 |
}else{ |
| 95 |
fprintf(stderr,"wc : file not found %s\n",argv[loop]); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
if(!fflag){ |
| 100 |
fprintf(stderr,"usage : wc [-l -w -c] file [file...]\n"); |
| 101 |
fprintf(stderr,"-l = count lines\n"); |
| 102 |
fprintf(stderr,"-c = count characters\n"); |
| 103 |
fprintf(stderr,"-w = count words\n"); |
| 104 |
exit(1); |
| 105 |
} |
| 106 |
for(loop=0;loop<79; loop++){ |
| 107 |
fprintf(stdout,"-"); |
| 108 |
} |
| 109 |
fprintf(stdout,"\n"); |
| 110 |
fprintf(stdout,"total : %25s ","") ; |
| 111 |
if(lflag){ |
| 112 |
fprintf(stdout,"lines %5d ",totlines); |
| 113 |
} |
| 114 |
if(cflag){ |
| 115 |
fprintf(stdout,"characters %5d ",totchars); |
| 116 |
} |
| 117 |
if(wflag){ |
| 118 |
fprintf(stdout,"words %5d ",totwords); |
| 119 |
} |
| 120 |
fprintf(stdout,"\n"); |
| 121 |
return(0); |
| 122 |
} |