Debian Codegraph @

texmf-dist/doc/latex/cweb-latex/examples/compare/wcltx.tex

raw
1 \input cwebmac
2 % $Id: wcltx.w,v 1.4 1995/08/29 17:27:57 schrod Exp $
3 %------------------------------------------------------------
4
5 %
6 % wc: An example of CWEB by Silvio Levy and Donald E. Knuth
7 %
8 % [LaTeX]
9 % (history at end)
10
11
12 %% -js:
13 %% should rework it: ANSI C compliance, POSIX.2 compliant option
14 %% parsing with getopt(), including *all* required header files (ie,
15 %% POSIX.1 compliance), using POSIX return codes and types, etc.
16
17
18
19 \documentclass{cweb}
20
21 \usepackage{rcs}
22
23
24 %
25 % some tags for this document
26 %
27 \def\CEE/{C\spacefactor 1000 }
28 \def\cweb{{\tt CWEB\/}}
29 \def\SPARC{SPARC\-\kern.1em station}
30 \def\UNIX/{{\small UNIX\/}}
31 \def\wc{{\tt wc\/}}
32 \def\WEB{{\tt WEB\/}}
33
34 \hfuzz=0.5pt
35
36
37
38
39 \begin{document}
40
41
42
43 \title{Counting words}
44 \author{Silvio Levy\and Donald E. Knuth}
45 \begingroup
46 \def\thefootnote{\hbox{$^*$}}
47 \footnotetext[0]{\LaTeX{} markup by Joachim Schrod.}
48 \endgroup
49 \RCSdate $Date: 1995/08/29 17:27:57 $
50
51 \maketitle
52
53 % not very interesting, only one starred section
54 % \tableofcontents
55
56
57
58
59 \N{1}{1}An example of \cweb{}. This example, based on a program by
60 Klaus Guntermann and Joachim Schrod~\cite{litprog:schrod:cweb}
61 presents the ``word count'' program from \UNIX/, rewritten in
62 \cweb{} to demonstrate literate programming in \CEE/.%
63 \footnote{%
64 Incidentally, Klaus Guntermann and Joachim Schrod are now
65 using this \cweb{} and have stopped the support for their version.
66 Joachim Schrod contributed the \LaTeX{} style used for the markup of
67 this document.
68 }
69 The level of detail in this document is intentionally high, for
70 didactic purposes; many of the things spelled out here don't need to
71 be explained in other programs.
72
73 The purpose of \wc{} is to count lines, words, and/or characters in a
74 list of files. The number of lines in a file is the number of newline
75 characters it contains. The number of characters is the file length in bytes.
76 A ``word'' is a maximal sequence of consecutive characters other than
77 newline, space, or tab, containing at least one visible ASCII code.
78 (We assume that the standard ASCII code is in use.)
79
80 \fi
81
82 \M{2}Most \cweb{} programs share a common structure. It's probably a
83 good idea to state the overall structure explicitly at the outset,
84 even though the various parts could all be introduced in unnamed
85 sections of the code if we wanted to add them piecemeal.
86
87 Here, then, is an overview of the file \texttt{wcltx.c} that is
88 defined by this \cweb{} program \texttt{wcltx.w}:
89
90 \Y\B\X3:Header files to include\X\6
91 \X4:Global variables\X\6
92 \X20:Functions\X\6
93 \X5:The main program\X\par
94 \fi
95
96 \M{3}We must include the standard I/O definitions, since we want to send
97 formatted output to \PB{\\{stdout}} and \PB{\\{stderr}}.
98
99 \Y\B\4\X3:Header files to include\X${}\E{}$\6
100 \8\#\&{include} \.{<stdio.h>}\par
101 \U2.\fi
102
103 \M{4} The \PB{\\{status}} variable will tell the operating system if the run
104 was
105 successful or not, and \PB{\\{prog\_name}} is used in case there's an error
106 message to
107 be printed.
108
109 \Y\B\4\D$\.{OK}$ \5
110 \T{0}\C{ \PB{\\{status}} code for successful run }\par
111 \B\4\D$\\{usage\_error}$ \5
112 \T{1}\C{ \PB{\\{status}} code for improper syntax }\par
113 \B\4\D$\\{cannot\_open\_file}$ \5
114 \T{2}\C{ \PB{\\{status}} code for file access error }\par
115 \Y\B\4\X4:Global variables\X${}\E{}$\6
116 \&{int} \\{status}${}\K\.{OK}{}$;\C{ exit status of command, initially \PB{%
117 \.{OK}} }\6
118 \&{char} ${}{*}\\{prog\_name}{}$;\C{ who we are }\par
119 \A14.
120 \U2.\fi
121
122 \M{5}Now we come to the general layout of the \PB{\\{main}} function.
123
124 \Y\B\4\X5:The main program\X${}\E{}$\6
125 $\\{main}(\\{argc},\39\\{argv}){}$\1\1\6
126 \&{int} \\{argc};\C{ the number of arguments on the \UNIX/ command line }\6
127 \&{char} ${}{*}{*}\\{argv}{}$;\C{ the arguments themselves, an array of strings
128 }\2\2\6
129 ${}\{{}$\1\6
130 \X6:Variables local to \PB{\\{main}}\X\6
131 ${}\\{prog\_name}\K\\{argv}[\T{0}];{}$\6
132 \X7:Set up option selection\X;\6
133 \X8:Process all the files\X;\6
134 \X19:Print the grand totals if there were multiple files\X;\6
135 \\{exit}(\\{status});\6
136 \4${}\}{}$\2\par
137 \U2.\fi
138
139 \M{6}If the first argument begins with a `\texttt{-}', the user is choosing
140 the desired counts and specifying the order in which they should be
141 displayed. Each selection is given by the initial character
142 (lines, words, or characters). For example, `\texttt{-cl}' would cause
143 just the number of characters and the number of lines to be printed,
144 in that order.
145
146 We do not process this string now; we simply remember where it is.
147 It will be used to control the formatting at output time.
148
149 \Y\B\4\X6:Variables local to \PB{\\{main}}\X${}\E{}$\6
150 \&{int} \\{file\_count};\C{ how many files there are }\6
151 \&{char} ${}{*}\\{which}{}$;\C{ which counts to print }\par
152 \As9\ET12.
153 \U5.\fi
154
155 \M{7}\B\X7:Set up option selection\X${}\E{}$\6
156 $\\{which}\K\.{"lwc"}{}$;\C{ if no option is given, print all three values }\6
157 \&{if} ${}(\\{argc}>\T{1}\W{*}\\{argv}[\T{1}]\E\.{'-'}){}$\5
158 ${}\{{}$\1\6
159 ${}\\{which}\K\\{argv}[\T{1}]+\T{1};{}$\6
160 ${}\\{argc}\MM;{}$\6
161 ${}\\{argv}\PP;{}$\6
162 \4${}\}{}$\2\6
163 ${}\\{file\_count}\K\\{argc}-\T{1}{}$;\par
164 \U5.\fi
165
166 \M{8}Now we scan the remaining arguments and try to open a file, if
167 possible. The file is processed and its statistics are given.
168 We use a \PB{\&{do}}~\dots~\PB{\&{while}} loop because we should read from the
169 standard input if no file name is given.
170
171 \Y\B\4\X8:Process all the files\X${}\E{}$\6
172 $\\{argc}\MM;{}$\6
173 \&{do}\5
174 ${}\{{}$\1\6
175 \X10:If a file is given, try to open \PB{${*}(\PP\\{argv})$}; \PB{\&{continue}}
176 if unsuccessful\X;\6
177 \X13:Initialize pointers and counters\X;\6
178 \X15:Scan file\X;\6
179 \X17:Write statistics for file\X;\6
180 \X11:Close file\X;\6
181 \X18:Update grand totals\X;\C{ even if there is only one file }\6
182 \4${}\}{}$\5
183 \2\5
184 \&{while} ${}(\MM\\{argc}>\T{0}){}$;\par
185 \U5.\fi
186
187 \M{9}Here's the code to open the file. A special trick allows us to
188 handle input from \PB{\\{stdin}} when no name is given.
189 Recall that the file descriptor to \PB{\\{stdin}} is~0; that's what we
190 use as the default initial value.
191
192 \Y\B\4\X6:Variables local to \PB{\\{main}}\X${}\mathrel+\E{}$\6
193 \&{int} \\{fd}${}\K\T{0}{}$;\C{ file descriptor, initialized to \PB{\\{stdin}}
194 }\par
195 \fi
196
197 \M{10}\B\D$\.{READ\_ONLY}$ \5
198 \T{0}\C{ read access code for system \PB{\\{open}} routine }\par
199 \Y\B\4\X10:If a file is given, try to open \PB{${*}(\PP\\{argv})$}; \PB{%
200 \&{continue}} if unsuccessful\X${}\E{}$\6
201 \&{if} ${}(\\{file\_count}>\T{0}\W(\\{fd}\K\\{open}({*}(\PP\\{argv}),\39\.{READ%
202 \_ONLY}))<\T{0}){}$\5
203 ${}\{{}$\1\6
204 ${}\\{fprintf}(\\{stderr},\39\.{"\%s:\ cannot\ open\ fil}\)\.{e\ \%s\\n"},\39%
205 \\{prog\_name},\39{*}\\{argv});{}$\6
206 ${}\\{status}\MRL{{\OR}{\K}}\\{cannot\_open\_file};{}$\6
207 ${}\\{file\_count}\MM;{}$\6
208 \&{continue};\6
209 \4${}\}{}$\2\par
210 \U8.\fi
211
212 \M{11}\B\X11:Close file\X${}\E{}$\6
213 \\{close}(\\{fd});\par
214 \U8.\fi
215
216 \M{12}We will do some homemade buffering in order to speed things up:
217 Characters
218 will be read into the \PB{\\{buffer}} array before we process them.
219 To do this we set up appropriate pointers and counters.
220
221 \Y\B\4\D$\\{buf\_size}$ \5
222 \.{BUFSIZ}\C{ \texttt{stdio.h}'s \PB{\.{BUFSIZ}} is chosen for efficiency }\par
223 \Y\B\4\X6:Variables local to \PB{\\{main}}\X${}\mathrel+\E{}$\6
224 \&{char} \\{buffer}[\\{buf\_size}];\C{ we read the input into this array }\6
225 \&{register} \&{char} ${}{*}\\{ptr}{}$;\C{ the first unprocessed character in %
226 \PB{\\{buffer}} }\6
227 \&{register} \&{char} ${}{*}\\{buf\_end}{}$;\C{ the first unused position in %
228 \PB{\\{buffer}} }\6
229 \&{register} \&{int} \|c;\C{ current character, or number of characters just
230 read }\6
231 \&{int} \\{in\_word};\C{ are we within a word? }\6
232 \&{long} \\{word\_count}${},{}$ \\{line\_count}${},{}$ \\{char\_count};\C{
233 number of words, lines, and characters found in the file so far }\par
234 \fi
235
236 \M{13}\B\X13:Initialize pointers and counters\X${}\E{}$\6
237 $\\{ptr}\K\\{buf\_end}\K\\{buffer};{}$\6
238 ${}\\{line\_count}\K\\{word\_count}\K\\{char\_count}\K\T{0};{}$\6
239 ${}\\{in\_word}\K\T{0}{}$;\par
240 \U8.\fi
241
242 \M{14}The grand totals must be initialized to zero at the beginning of the
243 program. If we made these variables local to \PB{\\{main}}, we would have to
244 do this initialization explicitly; however, \CEE/'s globals are automatically
245 zeroed. (Or rather, ``statically zeroed.'') (Get it?)
246 %% -js: The joke would be better if the vars would be static, which
247 %% they aren't in the C sense...
248
249 \Y\B\4\X4:Global variables\X${}\mathrel+\E{}$\6
250 \&{long} \\{tot\_word\_count}${},{}$ \\{tot\_line\_count}${},{}$ \\{tot\_char%
251 \_count};\C{ total number of words, lines, and chars }\par
252 \fi
253
254 \M{15}The present section, which does the counting that is \wc{}'s \emph{raison
255 d'\^etre}, was actually one of the simplest to write. We look at each
256 character and change state if it begins or ends a word.
257
258 \Y\B\4\X15:Scan file\X${}\E{}$\6
259 \&{while} (\T{1})\5
260 ${}\{{}$\1\6
261 \X16:Fill \PB{\\{buffer}} if it is empty; \PB{\&{break}} at end of file\X;\6
262 ${}\|c\K{*}\\{ptr}\PP;{}$\6
263 \&{if} ${}(\|c>\.{'\ '}\W\|c<\T{\~177}){}$\5
264 ${}\{{}$\C{ visible ASCII codes }\1\6
265 \&{if} ${}(\R\\{in\_word}){}$\5
266 ${}\{{}$\1\6
267 ${}\\{word\_count}\PP;{}$\6
268 ${}\\{in\_word}\K\T{1};{}$\6
269 \4${}\}{}$\2\6
270 \&{continue};\6
271 \4${}\}{}$\2\6
272 \&{if} ${}(\|c\E\.{'\\n'}){}$\1\5
273 ${}\\{line\_count}\PP;{}$\2\6
274 \&{else} \&{if} ${}(\|c\I\.{'\ '}\W\|c\I\.{'\\t'}){}$\1\5
275 \&{continue};\2\6
276 ${}\\{in\_word}\K\T{0}{}$;\C{ \PB{\|c} is newline, space, or tab }\6
277 \4${}\}{}$\2\par
278 \U8.\fi
279
280 \M{16}Buffered I/O allows us to count the number of characters almost for free.
281
282 \Y\B\4\X16:Fill \PB{\\{buffer}} if it is empty; \PB{\&{break}} at end of file%
283 \X${}\E{}$\6
284 \&{if} ${}(\\{ptr}\G\\{buf\_end}){}$\5
285 ${}\{{}$\1\6
286 ${}\\{ptr}\K\\{buffer};{}$\6
287 ${}\|c\K\\{read}(\\{fd},\39\\{ptr},\39\\{buf\_size});{}$\6
288 \&{if} ${}(\|c\Z\T{0}){}$\1\5
289 \&{break};\2\6
290 ${}\\{char\_count}\MRL{+{\K}}\|c;{}$\6
291 ${}\\{buf\_end}\K\\{buffer}+\|c;{}$\6
292 \4${}\}{}$\2\par
293 \U15.\fi
294
295 \M{17}It's convenient to output the statistics by defining a new function
296 \PB{\\{wc\_print}}; then the same function can be used for the totals.
297 Additionally we must decide here if we know the name of the file
298 we have processed or if it was just \PB{\\{stdin}}.
299
300 \Y\B\4\X17:Write statistics for file\X${}\E{}$\6
301 $\\{wc\_print}(\\{which},\39\\{char\_count},\39\\{word\_count},\39\\{line%
302 \_count});{}$\6
303 \&{if} (\\{file\_count})\1\5
304 ${}\\{printf}(\.{"\ \%s\\n"},\39{*}\\{argv}){}$;\C{ not \PB{\\{stdin}} }\2\6
305 \&{else}\1\5
306 \\{printf}(\.{"\\n"});\C{ \PB{\\{stdin}} }\2\par
307 \U8.\fi
308
309 \M{18}\B\X18:Update grand totals\X${}\E{}$\6
310 $\\{tot\_line\_count}\MRL{+{\K}}\\{line\_count};{}$\6
311 ${}\\{tot\_word\_count}\MRL{+{\K}}\\{word\_count};{}$\6
312 ${}\\{tot\_char\_count}\MRL{+{\K}}\\{char\_count}{}$;\par
313 \U8.\fi
314
315 \M{19}We might as well improve a bit on \UNIX/'s \wc{} by displaying the
316 number of files too.
317
318 \Y\B\4\X19:Print the grand totals if there were multiple files\X${}\E{}$\6
319 \&{if} ${}(\\{file\_count}>\T{1}){}$\5
320 ${}\{{}$\1\6
321 ${}\\{wc\_print}(\\{which},\39\\{tot\_char\_count},\39\\{tot\_word\_count},\39%
322 \\{tot\_line\_count});{}$\6
323 ${}\\{printf}(\.{"\ total\ in\ \%d\ files\\}\)\.{n"},\39\\{file\_count});{}$\6
324 \4${}\}{}$\2\par
325 \U5.\fi
326
327 \M{20}Here now is the function that prints the values according to the
328 specified options. The calling routine is supposed to supply a
329 newline. If an invalid option character is found we inform
330 the user about proper usage of the command. Counts are printed in
331 8-digit fields so that they will line up in columns.
332
333 \Y\B\4\D$\\{print\_count}(\|n)$ \5
334 $\\{printf}(\.{"\%8ld"},\39\|n{}$)\par
335 \Y\B\4\X20:Functions\X${}\E{}$\6
336 $\\{wc\_print}(\\{which},\39\\{char\_count},\39\\{word\_count},\39\\{line%
337 \_count}){}$\1\1\6
338 \&{char} ${}{*}\\{which}{}$;\C{ which counts to print }\6
339 \&{long} \\{char\_count}${},{}$ \\{word\_count}${},{}$ \\{line\_count};\C{
340 given totals }\2\2\6
341 ${}\{{}$\1\6
342 \&{while} ${}({*}\\{which}){}$\1\6
343 \&{switch} ${}({*}\\{which}\PP){}$\5
344 ${}\{{}$\1\6
345 \4\&{case} \.{'l'}:\5
346 \\{print\_count}(\\{line\_count});\6
347 \&{break};\6
348 \4\&{case} \.{'w'}:\5
349 \\{print\_count}(\\{word\_count});\6
350 \&{break};\6
351 \4\&{case} \.{'c'}:\5
352 \\{print\_count}(\\{char\_count});\6
353 \&{break};\6
354 \4\&{default}:\6
355 \&{if} ${}((\\{status}\AND\\{usage\_error})\E\T{0}){}$\5
356 ${}\{{}$\1\6
357 ${}\\{fprintf}(\\{stderr},\39\.{"\\nUsage:\ \%s\ [-lwc]\ }\)\.{[filename\ ...]%
358 \\n"},\39\\{prog\_name});{}$\6
359 ${}\\{status}\MRL{{\OR}{\K}}\\{usage\_error};{}$\6
360 \4${}\}{}$\2\6
361 \4${}\}{}$\2\2\6
362 \4${}\}{}$\2\par
363 \U2.\fi
364
365 \M{21}Incidentally, a test of this program against the system \wc{}
366 command on a \SPARC\ showed that the ``official'' \wc{} was slightly
367 slower. Furthermore, although that \wc{} gave an appropriate error
368 message for the options `\texttt{-abc}', it made no complaints about the
369 options `\texttt{-labc}'! Dare we suggest that the system routine
370 might have been
371 better if its programmer had used a more literate approach?
372
373
374
375 \bibliographystyle{plain}
376 \bibliography{wcltx}
377
378
379 \fi
380
381 \M{22}
382
383 \cwebIndexIntro{%
384 Here is a list of the identifiers used, and where they appear.
385 Underlined entries indicate the place of definition. Error messages
386 are also shown.
387 }
388
389 \end{document}
390
391
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %
394 % $Log: wcltx.w,v $
395 % Revision 1.4 1995/08/29 17:27:57 schrod
396 % Recoded to LaTeX2e markup.
397 %
398 % Revision 1.3 1993/06/15 14:01:14 schrod
399 % Renamed document to wcltx, must change \bibliography tag, too.
400 %
401 % Revision 1.2 1993/06/15 13:35:12 schrod
402 % `cweb' is now a style, was an option formerly.
403 %
404 % Revision 1.1 1993/04/09 15:00:37 schrod
405 % Initial revision
406 %
407 \fi
408
409
410 \inx
411 \fin
412 \con