History

CWEB — Release 3.3, 1994-12-06.

Andreas Scherer committed 15 years ago

Commit 22cd55e1 parent 6f710fc16 files · +379 −303Browse files

Showing the changes to examples (2 of 16 files). Show all

Unified · Split

2 files
  1. examples/wc.w +23 −8
  2. examples/wmerge.w +1 −1
examples/wc.w+23−8
@@ -19,6 +19,9 @@ A ``word'' is a maximal sequence of consecutive characters other than · 18 unmodified lines
19newline, space, or tab, containing at least one visible ASCII code.19newline, 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
22This version of \.{wc} has a nonstandard ``silent'' option (\.{-s}),
23which suppresses printing except for the grand totals over all files.
24
22@ Most \.{CWEB} programs share a common structure. It's probably a25@ Most \.{CWEB} programs share a common structure. It's probably a
23good idea to state the overall structure explicitly at the outset,26good idea to state the overall structure explicitly at the outset,
24even though the various parts could all be introduced in unnamed27even 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
71displayed. Each selection is given by the initial character74displayed. Each selection is given by the initial character
72(lines, words, or characters). For example, `\.{-cl}' would cause75(lines, words, or characters). For example, `\.{-cl}' would cause
73just the number of characters and the number of lines to be printed,76just the number of characters and the number of lines to be printed,
74in that order.77in that order. The default, if no special argument is given, is `\.{-lwc}'.
75 78
76We do not process this string now; we simply remember where it is.79We do not process this string now; we simply remember where it is.
77 It will be used to control the formatting at output time.80It will be used to control the formatting at output time.
81
82If the `\.{-}' is immediately followed by `\.{s}', only summary totals
83are printed.
78 84
79@<Var...@>=85@<Var...@>=
80int file_count; /* how many files there are */86int file_count; /* how many files there are */
81char *which; /* which counts to print */87char *which; /* which counts to print */
88int silent=0; /* nonzero if the silent option was selected */
82 89
83@ @<Set up o...@>=90@ @<Set up o...@>=
84which="lwc"; /* if no option is given, print all three values */91which="lwc"; /* if no option is given, print all three values */
85if (argc>1 && *argv[1] == '-') { which=argv[1]+1; argc--; argv++; }92if (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}
86file_count=argc-1;98file_count=argc-1;
87 99
88@ Now we scan the remaining arguments and try to open a file, if100@ 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
183we have processed or if it was just |stdin|.195we have processed or if it was just |stdin|.
184 196
185@<Write...@>=197@<Write...@>=
186wc_print(which, char_count, word_count, line_count);198if (!silent) {
187if (file_count) printf (" %s\n", *argv); /* not |stdin| */199 wc_print(which, char_count, word_count, line_count);
188else 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...@>=
191tot_line_count+=line_count;205tot_line_count+=line_count;
@@ -196,9 +210,10 @@ tot_char_count+=char_count; · 4 unmodified lines
196number of files too.210number of files too.
197 211
198@<Print the...@>=212@<Print the...@>=
199if (file_count>1) {213if (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 the219@ Here now is the function that prints the values according to the
examples/wmerge.w+1−1
@@ -63,7 +63,7 @@ than |buf_size|, so that |buffer[buf_size-1]| is never filled. · 62 unmodified lines
63Some of the routines use the fact that it is safe to refer to63Some 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 10066@d buf_size 1024
67 67
68@<Definitions...@>=68@<Definitions...@>=
69ASCII buffer[buf_size]; /* where each line of input goes */69ASCII buffer[buf_size]; /* where each line of input goes */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t