summaryrefslogtreecommitdiff
path: root/web/c_cpp/cnoweb/cnoweb.tex
blob: c9de58413ec5b78df7b1798edd884688646e9427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
% c-NO_WEB (a c program pretty printer)
% by Jim Fox, February 1987
% version 1.1, August 10, 1989
% version 1.2, October 30, 1989
%              fixed spacing after ?, :, etc.
% version 1.3, June 28, 1990
%              added OOdent and IIdent, added \|typewriter|
% version 1.4, January 4, 1991
%              allow /* and */ in strings
% version 1.5, December 11, 1991
%              page offset shift for two-sided page print alignment
%              nicer setting of too-long code lines
%              add \listfile and \includefile macros
% version 1.6, March 17, 1992
%              Restore plain-tex meaning of right quote in math
%
%------------------------------------------------------------------
%
% The TeX input file is a c source program.  It is assumed to have
% this structure:
%
%   /* comments ignored by c-no-web
%      \input cnoweb
%      \title{A one line title of the program (optional) }
%      \synopsis{Synopsis of the program (optional) }
%      \section{Name of the first section}
%      rest of comments */
%
% The rest of the file is normal c code.
% Within the title a \\ may be used to separate lines.
%
% Each major section begins with
%   /* \section{name of the section}    .... */
% The section names are printed boldface, marked for reference in
% the page headline, and written to the table of contents.
%
% Each minor section begins with
%   /* \subsection{name of the subsection}    .... */
% The subsection names are written to the table of contents.
%
% A new page can be forced by
%      /* \newpage */
%
% Code and other text containing special characters may be
% typed in text mode if it is bracked by *< ... >*
% e.g. /* *<inactive c-code>* */
% but this may not be used in a macro argument.
%
% You can print in bold by enclosing text in double quotes: \"text".
% You can print in typewriter by enclosing text in vertical bars: \|text|.
% You can print in italics by enclosing text in single quotes: \'text'.
%
% You can include a verbatim listing of a file with \listfile, e.g.,
%      ... \listfile{program.h} ...
% You can include a c file to be formatted with \includefile, e.g.,
%      ... \includefile{program.h} ...
%     
% The file must end with \endc in a comment. e.g.,
%      ... last c code statement ...
%      /* \endc */
%
%-----------------------------------------------------------------
%
% Make sure macros loaded only once
%
\expandafter\ifx\csname cnoweb loaded\endcsname\relax
  \expandafter\let\csname cnoweb loaded\endcsname*
  \else\endinput\fi
%
\catcode`\@=11
%
\message{[c-no-web vers 1.6]}
%
%
% page parameters -- most of these can be changed without harm
% The default setup is for 12 point type on an 8.5 x 11 inch page.
% Two side printing is assumed, 1-inch margins on the inside.
%
\vsize=55pc
\hsize=37pc
\topskip=2pc
\parindent=0pt
\parskip=6pt
\overfullrule=0pt
\newdimen\oddsideshift \oddsideshift=0pc % adjustment to odd, left margins
\newdimen\evensideshift \evensideshift=2pc % adjustment to even, left margins
\newdimen\rightmarginslop \rightmarginslop=3pc % right margin slop in code
\newdimen\blockindent \blockindent=18pt % '{' block indentation
\newdimen\longblockindent \longblockindent=30pt % '(' indentation
\newskip\sectionskip \sectionskip2pc
%
% fonts
%
\font\titles=cmr10 scaled\magstep3     % for titles
%
% normal commentary size is 12 point
%
\font\twelverm=cmr12 % roman text
\font\twelvei=cmmi12 % math italic
\font\twelvesy=cmsy10 scaled\magstep1 % math symbols
\font\twelvebf=cmbx12 % boldface extended
\font\twelvett=cmtt12 % typewriter
\font\twelvesl=cmsl12 % slanted roman
\font\twelveit=cmti12 % text italic
\font\twelveex=cmex10 scaled\magstep1 % math symbols
%
\font\ninerm=cmr9 % roman text
\font\ninei=cmmi9 % math italic
\font\ninesy=cmsy9 % math symbols
%
\def\normaltype{%
 \def\rm{\fam0\twelverm}%
 \textfont0=\twelverm \scriptfont0=\ninerm \scriptscriptfont0=\sevenrm
 \textfont1=\twelvei  \scriptfont1=\ninei  \scriptscriptfont1=\seveni
 \textfont2=\twelvesy \scriptfont2=\ninesy \scriptscriptfont2=\sevensy
 \textfont3=\twelveex \scriptfont3=\twelveex \scriptscriptfont3=\twelveex
 \textfont\itfam=\twelveit \def\it{\fam\itfam\twelveit}%
 \textfont\slfam=\twelvesl \def\sl{\fam\slfam\twelvesl}%
 \textfont\bffam=\twelvebf \def\bf{\fam\bffam\twelvebf}%
 \textfont\ttfam=\twelvett \def\tt{\fam\ttfam\twelvett}%
 \normalbaselineskip 14\p@
 \setbox\strutbox=\hbox{\vrule height10\p@ depth4\p@
      width0\p@}%
 \normalbaselines\rm}
\normaltype
%
% normal code size is 11 point
%
\font\elevenrm=cmr10  scaled\magstephalf % roman text
\font\eleveni=cmmi10  scaled\magstephalf % math italic
\font\elevensy=cmsy10  scaled\magstephalf % math symbols
\font\elevenbf=cmbx10  scaled\magstephalf % boldface extended
\font\eleventt=cmtt10  scaled\magstephalf % typewriter
\font\elevensl=cmsl10  scaled\magstephalf % slanted roman
\font\elevenit=cmti10  scaled\magstephalf % text italic
%
\def\codetype{%
 \def\rm{\fam0\elevenrm}%
 \textfont0=\elevenrm \scriptfont0=\sevenrm \scriptscriptfont0=\fiverm
 \textfont1=\eleveni  \scriptfont1=\seveni  \scriptscriptfont1=\fivei
 \textfont2=\elevensy \scriptfont2=\sevensy \scriptscriptfont2=\fivesy
 \textfont3=\tenex \scriptfont3=\tenex \scriptscriptfont3=\tenex
 \textfont\itfam=\elevenit \def\it{\fam\itfam\elevenit}%
 \textfont\slfam=\elevensl \def\sl{\fam\slfam\elevensl}%
 \textfont\bffam=\elevenbf \def\bf{\fam\bffam\elevenbf}%
 \textfont\ttfam=\eleventt \def\tt{\fam\ttfam\eleventt}%
 \normalbaselineskip 13\p@
 \setbox\strutbox=\hbox{\vrule height9.5\p@ depth3.5\p@ width0\p@}%
 \normalbaselines\rm}
%
% restricted code size is 10 point
%
\def\rcodetype{%
 \def\rm{\fam0\tenrm}%
 \textfont0=\tenrm \scriptfont0=\sevenrm \scriptscriptfont0=\fiverm
 \textfont1=\teni  \scriptfont1=\seveni  \scriptscriptfont1=\fivei
 \textfont2=\tensy \scriptfont2=\sevensy \scriptscriptfont2=\fivesy
 \textfont3=\tenex \scriptfont3=\tenex \scriptscriptfont3=\tenex
 \textfont\itfam=\tenit \def\it{\fam\itfam\tenit}%
 \textfont\slfam=\tensl \def\sl{\fam\slfam\tensl}%
 \textfont\bffam=\tenbf \def\bf{\fam\bffam\tenbf}%
 \textfont\ttfam=\tentt \def\tt{\fam\ttfam\tentt}%
 \normalbaselineskip 13\p@
 \setbox\strutbox=\hbox{\vrule height9.5\p@ depth3.5\p@ width0\p@}%
 \normalbaselines\rm}
%
% try using 11pt for text also
%
\let\normaltype\codetype
\let\smalltype\rcodetype
\normaltype
%
% allow underscore as char in non-math text
\def\oldun{_}
\def\specialunderscore{\ifmmode_\else{\tt\char"5F}\fi}
\catcode`\_=\active
\let_\specialunderscore
%
\def\\{{\tt\char"5C}}   % for \ in comments
\def\section#1{\mark{#1}%
  {\let\folio=\relax\let_=\oldun \edef\\{\write\cfile{\endgraf
     \tocnormaltype#1\cdots\cpageno\endgraf}}\\}%
  {\bf#1.\ }}
\def\subsection#1{%
  {\let\folio=\relax\let_=\oldun \edef\\{\write\cfile{\endgraf
     \toccodetype\hskip12pt#1\cdots\cpageno\endgraf}}\\}%
  {#1.\ }}
\def\subsubsection#1{%
  {\let\folio=\relax\let_=\oldun \edef\\{\write\cfile{\endgraf
     \tocrcodetype\hskip18pt#1\cdots\cpageno\endgraf}}\\}%
  {#1.\ }}
%
%
% headline contains (title, page number, section)
%
\def\makeheadline{\vbox to0pt{\vskip-30pt
  \ifnum\pageno=-1\else\line{\strut\normaltype
      \ifodd\pageno\firstmark\hfill\bf\the\job\rlap{\ -- \folio}%
      \else\bf\llap{\folio\ -- }\the\job\rm\hfill\firstmark\fi}\fi
  \vss}\nointerlineskip}
\def\makefootline{\line{\tenrm\vrule height30pt depth0pt width0pt
   \ifodd\pageno\today\ @\rightnow\ by \cnoweb\hfill
      \else\hfill\today\ @\rightnow\ by \cnoweb\fi}}
%
\def\plainoutput{\shipout\vbox{%
  \ifodd\pageno\moveright\oddsideshift\else\moveright\evensideshift\fi
  \vbox{\makeheadline\pagebody\makefootline}}%
  \advancepageno
  \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}

\let\oldpagebody\pagebody
\def\toprules{\vrule height4pt width.4pt\kern-.4pt\hskip\blockindent}
\def\pagebody{\ifnum\pageno>0\vbox to0pt{\hrule height.3pt width\hsize
      \leftline{\leaders\hbox{\toprules}}%
      \vss}\nointerlineskip
   \hbox{\llap{\vrule height\vsize width.3pt\hskip3pt}%
     \oldpagebody}%
   \else\oldpagebody\fi}

%
% table of contents
%
\newwrite\cfile
\immediate\openout\cfile=\jobname.toc
\def\cdots{\nobreak\leaders\hbox to1pc{\hss.\hss}
  \hskip24pt plus1fill}
\def\cpageno{\rlap{\hbox to18pt{\hfill\folio}}}
%
% these set the type size in toc entries
%
\let\tocnormaltype\relax   % de-activate
\let\toccodetype\relax
\let\tocrcodetype\relax
%
%
%-------------------------------------------------------------------
%
%
% All comment fields are formatted normally (plain TeX style).
% All c code is scanned and formatted verbatim.
%
% '/*' triggers TeX mode, '*/' triggers verbatim mode
%
\let\star=*   % a catcode 11 star
\let\slash=/  % a catcode 11 slash
\let\rarrow=> % a catcode 11 right arrow
%
% ---------------------------------------------------------------
%
% In TeX mode the * is active  and looks for */ or *<
% \setex sets TeX mode
%
\newif\ifrestricted\restrictedfalse
\def\setex{% begins TeX mode
  \ifrestricted\rcodetype
     \else\ifvmode\normaltype\filbreak\else\codetype\fi\fi
  \catcode`\*=\active
  {\tt/* }\ignorespaces}
%
{\catcode`\*=\active
 \gdef*{\futurelet\next\checkslash}
 }
\def\checkslash{%
    \ifx\next/\ifrestricted\let\next\resetrcc\else\let\next\setcc\fi
   \else\ifx\next<\let\next\setrcc
     \else\let\next\star\fi\fi\next}
%
%
% In code mode / is active and looks for /*
% \setcc sets the active code mode
%
\def\setcc/{\@setcc}
\def\@setcc{% set code mode
  \begingroup\ccverbatim\catcode`\/=\active
  */
  \parskip=0pt}
%
{\catcode`\/=\active
 \gdef/{\futurelet\next\checkstar}
 }
\def\checkstar{\ifx\next*\let\next\endcc
     \else\let\next\slash\fi\next}
\def\endcc*{\@endcc}
\def\@endcc{\endgroup\ifvmode\filbreak\fi\setex}
%
%
% In restricted code mode > is active and looks for >*
% \setrcc sets the restricted code mode
%
\def\setrcc<{% set little code mode
  \begingroup\restrictedtrue
  \begingroup\ccverbatim\rccverbatim\tt\catcode`\>=\active
  \catcode`\/=\active
  \parskip=0pt}
%
\def\resetrcc/{% reset little code mode
  \begingroup\ccverbatim\rccverbatim\tt\catcode`\>=\active
  \catcode`\/=\active
  */
  \parskip=0pt}
%
{\catcode`\>=\active
 \gdef>{\futurelet\next\checkrstar}
 }
\def\checkrstar{\ifx\next*\let\next\endrcc
     \else\let\next\rarrow\fi\next}
\def\endrcc*{\endgroup\endgroup}
%
%
% ---------------------------------------------------------------
%
% \leftskip is controlled by the presence of '{', '}',
%    '(', and ')' in the code text.
%
% \Ident increases \leftskip and takes effect after the current line
% \Odent decreases \leftskip and takes effect on the current line
% \IIdent and \OOdent do the same but use the long indentation
%
\newdimen\leftadjust % leftskip adjustment at next par
\def\Ident{\global\advance\leftadjust\blockindent}
\def\Odent{\global\advance\leftadjust-\blockindent\doOdent}
\def\IIdent{\global\advance\leftadjust\longblockindent}
\def\OOdent{\global\advance\leftadjust-\longblockindent\doOdent}
\def\doOdent{%
  \ifvmode\ifdim\leftadjust<\z@\global\advance\leftskip\leftadjust
     \global\leftadjust0pt\fi\fi}
%
\def\codepar{% this is \par in code mode
   \ifvmode\bigskip % this way blank lines are ignored at page tops
   \else\endgraf
     \ifdim\leftadjust=\z@\else\global\advance\leftskip\leftadjust
       \global\leftadjust0pt\fi\fi}
%
% ---------------------------------------------------------------
%
% Here are the catcodes and definitions for c-code verbatim
%
\chardef\other=12
\def\ccverbatim{%  set all chars to type `other' or `active'
  \catcode`\\=\other
  \catcode`\{=\active
  \catcode`\}=\active
  \catcode`\(=\active
  \catcode`\)=\active
  \catcode`\$=\other
  \catcode`\*=\other
  \catcode`\&=\other
  \catcode`\#=\active
  \catcode`\%=\other
  \catcode`\~=\other
  \catcode`\_=\other
  \catcode`\^=\other
  \catcode`\|=\other
  \catcode`\`=\active
  \catcode`\'=\active
  \catcode`\"=\active
  \let\par\codepar
  \obeyspaces \obeylines \frenchspacing \codetype
  \pretolerance10000 % don't hyphenate variable names, etc.
  \everypar{\hangindent\longblockindent\hangafter1}% indent wrapped 
  \rightskip0pt minus\rightmarginslop % allow some extension
  \tt}
\def\rccverbatim{% additional \other chars for restricted mode
  \catcode`\{=\other
  \catcode`\}=\other
  \catcode`\(=\other
  \catcode`\)=\other
  \catcode`\#=\other
  \catcode`\'=\other
  \catcode`\"=\other
  \rcodetype \tt}
%
% And here are the definitions for the c-code active characters.
% These perform the little formatting we do on c-code.
%     {, }, (, ), and # adjust the indentation
%     `, ', ", and / trigger more complete verbatim
%     
% Note that the right quote already has a plain-tex definition
%
{ \catcode`\<=1\catcode`\>=2
  \catcode`\{=\active \gdef{<<\Ident\char`\{>>
  \catcode`\}=\active \gdef}<<\Odent\char`\}>>
  \catcode`\(=\active \gdef(<<\IIdent\char`\(>>
  \catcode`\)=\active \gdef)<<\OOdent\char`\)>>
  \catcode`\|=0
  \catcode`\\=\active
  |catcode`|"=|active
  |gdef"<|char`|"|bgroup
     |catcode`|{=|other |catcode`|}=|other
     |catcode`|(=|other |catcode`|)=|other
     |catcode`|'=|other |catcode`|/=|other
     |catcode`|\=|active |let\|verbslash
     |def"<<|char`|"|egroup>>>
  |catcode`|'=|active
  |global|let|plain@rquote'
  |gdef|cnoweb@rquote<|char`|'|bgroup
     |catcode`|{=|other |catcode`|}=|other
     |catcode`|(=|other |catcode`|)=|other
     |catcode`|"=|other |catcode`|/=|other
     |catcode`|\=|active |let\|verbslash
     |def'<<|char`|'|egroup>>>
  |gdef'<|ifmmode|let|next|plain@rquote
     |else|let|next|cnoweb@rquote|fi|next>
 >
\def\verbslash{\begingroup\catcode`\'=\other\catcode`\"=\other\doverbslash}
\def\doverbslash#1{\\#1\endgroup}
{\catcode`\`=\active\gdef`{\relax\lq}}
{\catcode`\#=\active\gdef#{\ifvmode\ifdim\leftskip>\z@
   \noindent\llap{\hbox to\leftskip{\#\hss}}\ignorespaces
   \else\#\fi\else\#\fi}}
%
%
% ---------------------------------------------------------------
%
% \listfile produces a verbatim listing of a file
% \includefile formats the file (assumed to be c-code)
%
{\obeyspaces\gdef\hardspaces{\global\let =\ }}
\def\listfile#1{\par\begingroup\leftskip\blockindent\parskip0pt
    \ccverbatim\rccverbatim\hardspaces\input#1\par\endgroup}
\def\includefile#1{\@setcc\par\input#1\par\@endcc}
%
% ---------------------------------------------------------------
%
% \endc signals end of the c program
%
\def\endc{{\it end~\tt*/}\vfill\eject\dotitle\docontents\end}
%
% ---------------------------------------------------------------
%
% /* \newpage */ should give nice new page
\def\newpage{\vadjust{\vfill\eject}}
%
% some ebcdic systems get 05 for tabs
\catcode`\^^E=10  % make them spaces
%
% \"bold text" and \|typewriter text| and \'italic text'
%
\def\"#1"{{\bf#1}}
\def\|#1|{{\tt#1}}
\def\'#1'{{\it#1}}
%
% fix \item, etc. to work without \parindent
%
\newdimen\itemindent\itemindent2\blockindent
\def\hang{\hangindent\itemindent}
\def\textindent#1{\indent\hbox to\itemindent{\hss#1\enspace}\ignorespaces}
\def\item{\par\hang\textindent}
\def\textiindent#1{\indent\hbox to2\itemindent{\hss#1\enspace}\ignorespaces}
\def\itemitem{\par\indent\hangindent2\itemindent\textiindent}
\def\narrower{\advance\leftskip\itemindent
  \advance\rightskip\itemindent}
%
% c-no-web name
%
\def\cnoweb{\hbox{c-\setbox0=\hbox{web}%
  \vrule depth-.25\ht0 height.33\ht0 width\wd0\kern-\wd0\box0}}
%
% print the title and table of contents
%
\newtoks\job\job={\jobname}
\newtoks\title\title={}
\newtoks\synopsis\synopsis={}
\def\dotitle{
  \ifodd\pageno\else
     \message{page alignment}Intentionally blank \vfill\eject\fi
  \pageno=-1
  \vglue2pc
  \centerline{\titles\the\job}
  \vskip18pt
  \centerline{\bf\the\title}
  {\openup3pt\narrower\noindent\the\synopsis\par}
  \vfill}
\def\docontents{% print table of contents
  \mark{Contents}
  \immediate\closeout\cfile
  \begingroup
  \catcode`\@=11
  \leftskip=18pt\rightskip=\leftskip
  \everypar={\hang}
  \rightline{Page}
  \medskip
  \let\tocnormaltype\normaltype   % activate these type size macros
  \let\toccodetype\codetype
  \let\tocrcodetype\rcodetype
  \input \jobname.toc
  \catcode`\@=12
  \endgroup
  \vfill
  \eject}
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\def\today{\ifcase\month\or
  Jan\or Feb\or Mar\or Apr\or May\or Jun\or
  Jul\or Aug\or Sep\or Oct\or Nov\or Dec\fi
  \space\number\day, \number\year}
\def\rightnow{\count0=\time\count1=\count0  \divide\count0by60
  \ifnum\count0<10 0\fi\number\count0:% hours
  \multiply\count0by60 \advance\count1by-\count0
  \ifnum\count1<10 0\fi\number\count1% minutes
  }
%
\catcode`\@=11
%
{\hsize\maxdimen
  \output={\setbox0=\box255}\eject} % delete all before \input cnoweb
\setex  % begin with TeX mode

% end of cnoweb.tex