Showing the changes to examples (2 of 16 files). Show all
Unified · Split
| @@ -19,6 +19,9 @@ A ``word'' is a maximal sequence of consecutive characters other than · 18 unmodified lines | |||
| 19 | newline, space, or tab, containing at least one visible ASCII code. | 19 | newline, space, or tab, containing at least one visible ASCII code. |
| 20 | (We assume that the standard ASCII code is in use.) | 20 | (We assume that the standard ASCII code is in use.) |
| 21 | 21 | ||
| 22 | This version of \.{wc} has a nonstandard ``silent'' option (\.{-s}), | ||
| 23 | which suppresses printing except for the grand totals over all files. | ||
| 24 | |||
| 22 | @ Most \.{CWEB} programs share a common structure. It's probably a | 25 | @ Most \.{CWEB} programs share a common structure. It's probably a |
| 23 | good idea to state the overall structure explicitly at the outset, | 26 | good idea to state the overall structure explicitly at the outset, |
| 24 | even though the various parts could all be introduced in unnamed | 27 | even though the various parts could all be introduced in unnamed |
| @@ -71,18 +74,27 @@ the desired counts and specifying the order in which they should be · 46 unmodified lines | |||
| 71 | displayed. Each selection is given by the initial character | 74 | displayed. Each selection is given by the initial character |
| 72 | (lines, words, or characters). For example, `\.{-cl}' would cause | 75 | (lines, words, or characters). For example, `\.{-cl}' would cause |
| 73 | just the number of characters and the number of lines to be printed, | 76 | just the number of characters and the number of lines to be printed, |
| 74 | in that order. | 77 | in that order. The default, if no special argument is given, is `\.{-lwc}'. |
| 75 | 78 | ||
| 76 | We do not process this string now; we simply remember where it is. | 79 | We do not process this string now; we simply remember where it is. |
| 77 | It will be used to control the formatting at output time. | 80 | It will be used to control the formatting at output time. |
| 81 | |||
| 82 | If the `\.{-}' is immediately followed by `\.{s}', only summary totals | ||
| 83 | are printed. | ||
| 78 | 84 | ||
| 79 | @<Var...@>= | 85 | @<Var...@>= |
| 80 | int file_count; /* how many files there are */ | 86 | int file_count; /* how many files there are */ |
| 81 | char *which; /* which counts to print */ | 87 | char *which; /* which counts to print */ |
| 88 | int silent=0; /* nonzero if the silent option was selected */ | ||
| 82 | 89 | ||
| 83 | @ @<Set up o...@>= | 90 | @ @<Set up o...@>= |
| 84 | which="lwc"; /* if no option is given, print all three values */ | 91 | which="lwc"; /* if no option is given, print all three values */ |
| 85 | if (argc>1 && *argv[1] == '-') { which=argv[1]+1; argc--; argv++; } | 92 | if (argc>1 && *argv[1] == '-') { |
| 93 | argv[1]++; | ||
| 94 | if (*argv[1]=='s') silent=1,argv[1]++; | ||
| 95 | if (*argv[1]) which=argv[1]; | ||
| 96 | argc--; argv++; | ||
| 97 | } | ||
| 86 | file_count=argc-1; | 98 | file_count=argc-1; |
| 87 | 99 | ||
| 88 | @ Now we scan the remaining arguments and try to open a file, if | 100 | @ Now we scan the remaining arguments and try to open a file, if |
| @@ -183,9 +195,11 @@ Additionally we must decide here if we know the name of the file · 94 unmodified lines | |||
| 183 | we have processed or if it was just |stdin|. | 195 | we have processed or if it was just |stdin|. |
| 184 | 196 | ||
| 185 | @<Write...@>= | 197 | @<Write...@>= |
| 186 | wc_print(which, char_count, word_count, line_count); | 198 | if (!silent) { |
| 187 | if (file_count) printf (" %s\n", *argv); /* not |stdin| */ | 199 | wc_print(which, char_count, word_count, line_count); |
| 188 | else printf ("\n"); /* |stdin| */ | 200 | if (file_count) printf (" %s\n", *argv); /* not |stdin| */ |
| 201 | else printf ("\n"); /* |stdin| */ | ||
| 202 | } | ||
| 189 | 203 | ||
| 190 | @ @<Upda...@>= | 204 | @ @<Upda...@>= |
| 191 | tot_line_count+=line_count; | 205 | tot_line_count+=line_count; |
| @@ -196,9 +210,10 @@ tot_char_count+=char_count; · 4 unmodified lines | |||
| 196 | number of files too. | 210 | number of files too. |
| 197 | 211 | ||
| 198 | @<Print the...@>= | 212 | @<Print the...@>= |
| 199 | if (file_count>1) { | 213 | if (file_count>1 || silent) { |
| 200 | wc_print(which, tot_char_count, tot_word_count, tot_line_count); | 214 | wc_print(which, tot_char_count, tot_word_count, tot_line_count); |
| 201 | printf(" total in %d files\n",file_count); | 215 | if (!file_count) printf("\n"); |
| 216 | else printf(" total in %d file%s\n",file_count,file_count>1?"s":""); | ||
| 202 | } | 217 | } |
| 203 | 218 | ||
| 204 | @ Here now is the function that prints the values according to the | 219 | @ Here now is the function that prints the values according to the |
| @@ -63,7 +63,7 @@ than |buf_size|, so that |buffer[buf_size-1]| is never filled. · 62 unmodified lines | |||
| 63 | Some of the routines use the fact that it is safe to refer to | 63 | Some of the routines use the fact that it is safe to refer to |
| 64 | |*(limit+2)| without overstepping the bounds of the array. | 64 | |*(limit+2)| without overstepping the bounds of the array. |
| 65 | 65 | ||
| 66 | @d buf_size 100 | 66 | @d buf_size 1024 |
| 67 | 67 | ||
| 68 | @<Definitions...@>= | 68 | @<Definitions...@>= |
| 69 | ASCII buffer[buf_size]; /* where each line of input goes */ | 69 | ASCII buffer[buf_size]; /* where each line of input goes */ |