| 1 |
% This file generates the user manual; TeX it, don't read it! |
| 2 |
|
| 3 |
\def\tangref{3} % where the main explanation of CTANGLing is given |
| 4 |
\input cwebmac |
| 5 |
\pdffalse\acrohintfalse |
| 6 |
\def\page{\box255 } \normalbottom |
| 7 |
\parskip 0pt plus 1pt |
| 8 |
\def\RA{\char'31 } % right arrow |
| 9 |
\def\hang{\hangindent 4em\ignorespaces} |
| 10 |
\font\eighttt=cmtt8 |
| 11 |
\font\ninett=cmtt9 |
| 12 |
\font\twelvett=cmtt12 |
| 13 |
\font\authorfont=cmr12 |
| 14 |
\font\sectionfont=cmbx12 |
| 15 |
|
| 16 |
\def\pb{\.{|...|}} |
| 17 |
\def\v{\.{\char'174}} % vertical (|) in typewriter font |
| 18 |
\def\lpile{\def\cr{\hfill\endline}\matrix} % I only use \lpile by itself |
| 19 |
\abovedisplayskip=.5\abovedisplayskip |
| 20 |
\belowdisplayskip=.5\belowdisplayskip |
| 21 |
\abovedisplayshortskip=.5\abovedisplayshortskip |
| 22 |
\belowdisplayshortskip=.5\belowdisplayshortskip |
| 23 |
\advance\baselineskip by -.5pt |
| 24 |
\advance\pageheight by \baselineskip % the manual just got a bit longer |
| 25 |
\advance\fullpageheight by \baselineskip |
| 26 |
\setpage |
| 27 |
|
| 28 |
\outer\def\section #1.{\penalty-500\bigskip |
| 29 |
\centerline{\sectionfont\def\.##1{{\twelvett##1}} #1}\nobreak\vskip 6pt |
| 30 |
\everypar{\hskip-\parindent\everypar{}}} |
| 31 |
|
| 32 |
\def\lheader{\mainfont\the\pageno\hfill\sc\runninghead\hfill} |
| 33 |
\def\rheader{\hfill\sc\runninghead\hfill\mainfont\the\pageno} |
| 34 |
\def\runninghead{{\ninett CWEB} USER MANUAL (VERSION 4.12.2)} |
| 35 |
|
| 36 |
% This verbatim mode assumes that ! marks are !! in the text being copied. |
| 37 |
\def\verbatim{\begingroup |
| 38 |
\def\do##1{\catcode`##1=12 } \dospecials |
| 39 |
\parskip 0pt \parindent 0pt \let\!=! |
| 40 |
\catcode`\ =13 \catcode`\^^M=13 |
| 41 |
\tt \catcode`\!=0 \verbatimdefs \verbatimgobble} |
| 42 |
{\catcode`\^^M=13{\catcode`\ =13\gdef\verbatimdefs{\def^^M{\ \par}\let =\ }} % |
| 43 |
\gdef\verbatimgobble#1^^M{}} |
| 44 |
|
| 45 |
\null\vfill |
| 46 |
\centerline{\titlefont The {\ttitlefont CWEB} System of |
| 47 |
Structured Documentation} |
| 48 |
\vskip 18pt\centerline{(Version 4.12.2 --- July 2025)} |
| 49 |
\vskip 24pt |
| 50 |
\centerline{\authorfont Donald E. Knuth and Silvio Levy} |
| 51 |
\vfill |
| 52 |
|
| 53 |
\noindent |
| 54 |
\TeX\ is a trademark of the American Mathematical Society. |
| 55 |
|
| 56 |
\bigskip\noindent |
| 57 |
The printed form of this manual is copyright \copyright\ 1994 |
| 58 |
by Addison-Wesley Publishing Company, Inc. All rights reserved. |
| 59 |
\smallskip\noindent |
| 60 |
The electronic form is copyright \copyright\ 1987, 1990, 1993, 2000 |
| 61 |
by Silvio Levy and Donald E. Knuth. |
| 62 |
|
| 63 |
\bigskip\noindent |
| 64 |
Permission is granted to make and distribute verbatim copies of the |
| 65 |
electronic form of this document provided that the electronic copyright |
| 66 |
notice and this permission notice are preserved on all copies. |
| 67 |
|
| 68 |
\smallskip\noindent |
| 69 |
Permission is granted to copy and distribute modified versions of the |
| 70 |
electronic form of this document under the conditions for verbatim copying, |
| 71 |
provided that the entire resulting derived work is distributed under the terms |
| 72 |
of a permission notice identical to this one. |
| 73 |
|
| 74 |
\smallskip\noindent |
| 75 |
Individuals may make copies of the documentation from the electronic files |
| 76 |
for their own personal use. |
| 77 |
|
| 78 |
\smallskip\noindent |
| 79 |
Internet page \.{http://www-cs-faculty.stanford.edu/\char`\~knuth/cweb.html} |
| 80 |
contains current info about \.{CWEB} and related topics. |
| 81 |
|
| 82 |
\smallskip\noindent |
| 83 |
From there you can also reach the \.{CWEB} development page |
| 84 |
\.{https://github.com/ascherer/cweb} with the really current news. |
| 85 |
|
| 86 |
\pageno=0 \titletrue\eject |
| 87 |
|
| 88 |
\titletrue |
| 89 |
\centerline{\titlefont The {\ttitlefont CWEB} System of |
| 90 |
Structured Documentation} |
| 91 |
|
| 92 |
\vskip 15pt plus 3pt minus 3pt |
| 93 |
\centerline{\authorfont Donald E. Knuth and Silvio Levy} |
| 94 |
|
| 95 |
\vskip 24pt plus 3pt minus 3pt |
| 96 |
\noindent |
| 97 |
This document describes a version of Don Knuth's \.{WEB} system, |
| 98 |
adapted to \CEE/ by Silvio Levy. Since its creation in 1987, \.{CWEB} |
| 99 |
has been revised and enhanced in various ways, by both Knuth and Levy. |
| 100 |
We now believe that its evolution is near an end; however, bug |
| 101 |
reports, suggestions and comments are still welcome, and |
| 102 |
should be sent to the \TeX-related mailing list \.{[email protected]}. |
| 103 |
|
| 104 |
Readers who are familiar with Knuth's memo ``The \.{WEB} System of Structured |
| 105 |
Documentation'' will be able |
| 106 |
to skim this material rapidly, because \.{CWEB} and \.{WEB} share |
| 107 |
the same philosophy and (essentially) the same syntax. In some respects |
| 108 |
\.{CWEB} is a simplification |
| 109 |
of \.{WEB}: for example, \.{CWEB} does not need \.{WEB}'s features |
| 110 |
for macro definition and string handling, because \CEE/ and its |
| 111 |
preprocessor already take care of macros and strings. Similarly, the \.{WEB} |
| 112 |
conventions of denoting octal and hexadecimal constants by \.{@'77} |
| 113 |
and \.{@"3f} are replaced by \CEE/'s conventions \.{077} and |
| 114 |
\.{0x3f}. All other features of \.{WEB} have been |
| 115 |
retained, and new features have been added. |
| 116 |
|
| 117 |
We thank all who contributed suggestions and criticism to |
| 118 |
the development of \.{CWEB}. We are especially grateful to Steve |
| 119 |
Avery, Nelson Beebe, Hans-Hermann Bode, Klaus Guntermann, Norman Ramsey, |
| 120 |
Joachim Schnitter, and Saroj Mahapatra, |
| 121 |
who contributed code, and to Cameron Smith, who |
| 122 |
made many suggestions improving the manual. Ramsey has made |
| 123 |
literate programming accessible to users of yet other languages by means of |
| 124 |
his \.{SPIDER} system [see {\sl Communications of the ACM\/ \bf32} (1989), |
| 125 |
1051--1055]. The book {\sl Literate Programming\/} by Knuth (1992) contains |
| 126 |
a comprehensive bibliography of related early work. |
| 127 |
Bode, Schnitter, and Mahapatra adapted \.{CWEB} so that it works |
| 128 |
for \CPLUSPLUS/ as well; therefore in the text below you can read \CPLUSPLUS/ |
| 129 |
for \CEE/ if you so desire. |
| 130 |
|
| 131 |
\section Introduction. |
| 132 |
The philosophy behind \.{CWEB} is |
| 133 |
that programmers who want to provide the best |
| 134 |
possible documentation for their programs need two things |
| 135 |
simultaneously: a language like \TEX/ for formatting, and a language like |
| 136 |
\CEE/ for programming. Neither type of language can provide the |
| 137 |
best documentation by itself. But when both are appropriately combined, we |
| 138 |
obtain a system that is much more useful than either language separately. |
| 139 |
|
| 140 |
The structure of a software program may be thought of as a ``web'' that is |
| 141 |
made up of many interconnected pieces. To document such a program, we want |
| 142 |
to explain each individual part of the web and how it relates to its |
| 143 |
neighbors. The typographic tools provided by \TEX/ give us an opportunity |
| 144 |
to explain the local structure of each part by making that structure |
| 145 |
visible, and the programming tools provided by \CEE/ make it possible |
| 146 |
for us to specify the algorithms formally and unambiguously. By combining |
| 147 |
the two, we can develop a style of programming that maximizes our ability |
| 148 |
to perceive the structure of a complex piece of software, and at the same |
| 149 |
time the documented programs can be mechanically translated into a working |
| 150 |
software system that matches the documentation. |
| 151 |
|
| 152 |
The \.{CWEB} system consists of two programs named \.{CWEAVE} and \.{CTANGLE}. |
| 153 |
When writing a \.{CWEB} program the user keeps the |
| 154 |
\CEE/ code and the documentation in the same file, called the \.{CWEB} |
| 155 |
file and generally named \.{something.w}. The command |
| 156 |
`\.{cweave} \.{something}' creates an output file \.{something.tex}, which |
| 157 |
can then be fed to \TEX/, yielding a ``pretty printed'' version of |
| 158 |
\.{something.w} that correctly handles |
| 159 |
typographic details like page layout and the use of indentation, |
| 160 |
italics, boldface, and mathematical symbols. The typeset output also |
| 161 |
includes extensive cross-index |
| 162 |
information that is gathered automatically. Similarly, if you run the |
| 163 |
command `\.{ctangle} \.{something}' you will get a \CEE/ file \.{something.c}, |
| 164 |
which can then be compiled to yield executable code. |
| 165 |
|
| 166 |
Besides providing a documentation tool, \.{CWEB} enhances the \CEE/ |
| 167 |
language by providing the |
| 168 |
ability to permute pieces of the program text, so that a large system can |
| 169 |
be understood entirely in terms of small sections and their local |
| 170 |
interrelationships. The \.{CTANGLE} program is so named because it takes a |
| 171 |
given web and moves the sections from their web structure into the order |
| 172 |
required by \CEE/; the advantage of programming in \.{CWEB} is that the |
| 173 |
algorithms can be expressed in ``untangled'' form, with each section |
| 174 |
explained separately. The \.{CWEAVE} program is so named because it takes |
| 175 |
a given web and intertwines the \TEX/ and \CEE/ portions contained in |
| 176 |
each section, then it knits the whole fabric into a structured document. |
| 177 |
(Get it? Wow.) Perhaps there is some deep connection here with the fact |
| 178 |
that the German word for ``weave'' is ``{\it webe\/}'', and the |
| 179 |
corresponding Latin imperative is ``{\it texe\/}''! |
| 180 |
|
| 181 |
A user of \.{CWEB} should be fairly familiar with the \CEE/ |
| 182 |
programming language. A minimal amount of acquaintance with \TEX/ is also |
| 183 |
desirable, but in fact it can be acquired as one uses \.{CWEB}, since |
| 184 |
straight text can be typeset in \TEX/ with virtually no knowledge of |
| 185 |
that language. To someone familiar with both \CEE/ and \TEX/ the amount of |
| 186 |
effort necessary to learn the commands of \.{CWEB} is small. |
| 187 |
|
| 188 |
\section Overview. |
| 189 |
Two kinds of material go into \.{CWEB} files: \TEX/ text and \CEE/ text. |
| 190 |
A programmer writing in \.{CWEB} should be thinking both of the |
| 191 |
documentation and of the \CEE/ program being created; |
| 192 |
i.e., the programmer should be instinctively aware of the different |
| 193 |
actions that \.{CWEAVE} and \.{CTANGLE} will perform on the \.{CWEB} file. |
| 194 |
\TEX/ text is essentially copied without change by \.{CWEAVE}, and it is |
| 195 |
entirely deleted by \.{CTANGLE}; the \TEX/ text is ``pure |
| 196 |
documentation.'' \CEE/ text, on the other hand, is formatted by |
| 197 |
\.{CWEAVE} and it is shuffled around by \.{CTANGLE}, according to rules that |
| 198 |
will become clear later. For now the important point to keep in mind is |
| 199 |
that there are two kinds of text. Writing \.{CWEB} programs is something |
| 200 |
like writing \TEX/ documents, but with an additional ``\CEE/ mode'' |
| 201 |
that is added to \TEX/'s horizontal mode, vertical mode, and math mode. |
| 202 |
|
| 203 |
A \.{CWEB} file is built up from units called {\sl sections\/} that are more |
| 204 |
or less self-contained. Each section has three parts: |
| 205 |
|
| 206 |
\yskip\item{$\bullet$} A \TEX/ part, containing explanatory material about what |
| 207 |
is going on in the section. |
| 208 |
|
| 209 |
\item{$\bullet$} A middle part, containing macro definitions that serve as |
| 210 |
abbreviations for \CEE/ constructions that would be less comprehensible |
| 211 |
if written out in full each time. They are turned by \.{CTANGLE} into |
| 212 |
preprocessor macro definitions. |
| 213 |
|
| 214 |
\item{$\bullet$} A \CEE/ part, containing a piece of the program that |
| 215 |
\.{CTANGLE} will produce. This \CEE/ code should ideally be about a |
| 216 |
dozen lines long, so that it is easily comprehensible as a unit and so |
| 217 |
that its structure is readily perceived. |
| 218 |
|
| 219 |
\yskip\noindent The three parts of each section must appear in this order; |
| 220 |
i.e., the \TEX/ commentary must come first, then the middle part, and |
| 221 |
finally the \CEE/ code. Any of the parts may be empty. |
| 222 |
|
| 223 |
A section begins with either of the symbols `\.{@\ }' or `\.{@*}', where |
| 224 |
`\.{\ }' denotes a blank space. A section ends |
| 225 |
at the beginning of the next section (i.e., at the next |
| 226 |
`\.{@\ }' or `\.{@*}'), or at the end of the file, whichever comes first. |
| 227 |
The \.{CWEB} file may also contain material that is not part of any section |
| 228 |
at all, namely the text (if any) that occurs before the first section. |
| 229 |
Such text is said to be ``in limbo''; it is ignored by \.{CTANGLE} |
| 230 |
and copied essentially verbatim by \.{CWEAVE}, so its function is to |
| 231 |
provide any additional formatting instructions that may be desired in the |
| 232 |
\TEX/ output. Indeed, it is customary to begin a \.{CWEB} file with |
| 233 |
\TEX/ code in limbo that loads special fonts, defines special macros, |
| 234 |
changes the page sizes, and/or produces a title page. |
| 235 |
|
| 236 |
Sections are numbered consecutively, starting with 1. These numbers appear |
| 237 |
at the beginning of each section of the \TEX/ documentation output by |
| 238 |
\.{CWEAVE}, and they appear |
| 239 |
as bracketed comments at the beginning and end of the code generated by that |
| 240 |
section in the \CEE/ program output by \.{CTANGLE}. |
| 241 |
|
| 242 |
\section Section Names. |
| 243 |
Fortunately, you never mention these numbers yourself when you are writing |
| 244 |
in \.{CWEB}. You just say `\.{@\ }' or `\.{@*}' at the beginning of each |
| 245 |
new section, and the numbers are supplied automatically by \.{CWEAVE} and |
| 246 |
\.{CTANGLE}. As far as you are concerned, a section has a |
| 247 |
{\sl name\/} instead of a number; its name is specified by writing |
| 248 |
`\.{@<}' followed by \TEX/ text followed by `\.{@>}'. When \.{CWEAVE} |
| 249 |
outputs a section name, it replaces the `\.{@<}' and `\.{@>}' by |
| 250 |
angle brackets and inserts the section number in small type. Thus, when you |
| 251 |
read the output of \.{CWEAVE} it is easy to locate any section that is |
| 252 |
referred to in another section. |
| 253 |
|
| 254 |
For expository purposes, a section name should be a good description of the |
| 255 |
contents of that section; i.e., it should stand for the abstraction |
| 256 |
represented by the section. Then the section can be ``plugged into'' one or |
| 257 |
more other sections in such a way |
| 258 |
that unimportant details of its inner workings |
| 259 |
are suppressed. A section name therefore ought to be long enough to convey |
| 260 |
the necessary meaning. |
| 261 |
|
| 262 |
Unfortunately, it is laborious to type |
| 263 |
such long names over and over again, and it is also difficult to specify a |
| 264 |
long name twice in exactly the same way so that \.{CWEAVE} and \.{CTANGLE} |
| 265 |
will be able to match the names to the sections. To ameliorate this situation, |
| 266 |
\.{CWEAVE} and \.{CTANGLE} let you abbreviate a section name, so long as |
| 267 |
the full name appears somewhere in the \.{CWEB} file; you can type simply |
| 268 |
`\.{@<$\alpha$...@>}', where $\alpha$ is any string that is a prefix of |
| 269 |
exactly one section name appearing in the file. For example, `\.{@<Clear |
| 270 |
the arrays@>}' can be abbreviated to `\.{@<Clear...@>}' if no other section |
| 271 |
name begins with the five letters `\.{Clear}'. Elsewhere |
| 272 |
you might use the abbreviation `\.{@<Clear t...@>}', and so on. |
| 273 |
|
| 274 |
Section names must otherwise match character for character, except |
| 275 |
that consecutive characters of white space (spaces, tab marks, newlines, and/or |
| 276 |
form feeds) are treated as equivalent to a single space, and such spaces are |
| 277 |
deleted at the beginning and end of the name. Thus, `\.{@< Clear { }the |
| 278 |
arrays @>}' will also match the name in the previous example. |
| 279 |
Spaces following the ellipsis in abbreviations are ignored as well, but |
| 280 |
not those before, so that `\.{@<Clear t ...@>}' would not match |
| 281 |
`\.{@<Clear the arrays@>}'. |
| 282 |
|
| 283 |
\section What \.{CTANGLE} Does. |
| 284 |
We have said that a section begins with `\.{@\ }' or `\.{@*}', but we |
| 285 |
didn't say how it gets divided up into a \TEX/ part, a middle part, |
| 286 |
and a \CEE/ part. The middle part begins with the first appearance of |
| 287 |
`\.{@d}' or `\.{@f}' in the section, and the \CEE/ part begins with |
| 288 |
the first appearance of `\.{@c}' or `\.{@<section name@>=}'. In the |
| 289 |
latter case you are saying, in effect, that the section name stands |
| 290 |
for the \CEE/ text that follows. Alternatively, if the \CEE/ part |
| 291 |
begins with `\.{@c}' instead of a section name, the current section is |
| 292 |
said to be {\sl unnamed}. |
| 293 |
|
| 294 |
The construct `\.{@<section name@>}' can appear |
| 295 |
any number of times in the \CEE/ part of a section: |
| 296 |
Subsequent appearances indicate that a named section is being |
| 297 |
``used'' rather than ``defined.'' In other words, the |
| 298 |
\CEE/ code for the named section, presumably defined elsewhere, should be |
| 299 |
spliced in at this point in the \CEE/ program. Indeed, |
| 300 |
the main idea of \.{CTANGLE} is to make a \CEE/ program out of |
| 301 |
individual sections, named and unnamed. The exact way in which this is done |
| 302 |
is this: First all the macro definitions |
| 303 |
indicated by `\.{@d}' are turned into \CEE/ preprocessor macro definitions |
| 304 |
and copied at the beginning. |
| 305 |
Then the \CEE/ parts of unnamed sections are copied down, |
| 306 |
in order; this constitutes the first-order |
| 307 |
approximation to the text of the program. (There should be at least |
| 308 |
one unnamed section, otherwise there will be no program.) Then all section |
| 309 |
names that appear in the first-order approximation are replaced by the \CEE/ |
| 310 |
parts of the corresponding sections, and this substitution process |
| 311 |
continues until no section names remain. All comments are removed, because |
| 312 |
the \CEE/ program is intended only for the eyes of the \CEE/ compiler. |
| 313 |
|
| 314 |
If the same name has been given to more than one section, the \CEE/ text |
| 315 |
for that name is obtained by putting together all of the \CEE/ parts in |
| 316 |
the corresponding sections. This feature is useful, for example, in a |
| 317 |
section named `Global variables', since one can then |
| 318 |
declare global variables in whatever sections those variables are |
| 319 |
introduced. When several sections have the same name, \.{CWEAVE} assigns the |
| 320 |
first section number as the number corresponding to that name, and it |
| 321 |
inserts a note at the bottom of that section telling the reader to `See |
| 322 |
also sections so-and-so'; this footnote gives the numbers of all the other |
| 323 |
sections having the same name as the present one. The \CEE/ text |
| 324 |
corresponding to a section is usually formatted by \.{CWEAVE} so that the |
| 325 |
output has an equivalence sign in place of the equals sign in the \.{CWEB} |
| 326 |
file; i.e., the output says `$\langle\,$section |
| 327 |
name$\,\rangle\equiv\null$\CEE/ text'. However, in the case of the second |
| 328 |
and subsequent appearances of a section with the same name, this `$\equiv$' |
| 329 |
sign is replaced by `$\mathrel+\equiv$', as an indication that the |
| 330 |
following \CEE/ text is being appended to the \CEE/ text of another section. |
| 331 |
|
| 332 |
As \.{CTANGLE} enters and leaves sections, it inserts preprocessor |
| 333 |
\.{\#line} commands into the \CEE/ output file. This means that |
| 334 |
when the compiler gives you error messages, or when you debug your program, |
| 335 |
the messages refer to line numbers in the \.{CWEB} file, and not in the |
| 336 |
\CEE/ file. In most cases you can therefore |
| 337 |
forget about the \CEE/ file altogether. |
| 338 |
|
| 339 |
\section What \.{CWEAVE} Does. |
| 340 |
The general idea of \.{CWEAVE} is to make a \.{.tex} file from the \.{CWEB} |
| 341 |
file in the following way: The first line of the \.{.tex} file |
| 342 |
tells \TEX/ to input a file with macros that |
| 343 |
define \.{CWEB}'s documentation conventions. The next lines of the file |
| 344 |
will be copied from whatever \TEX/ text is in limbo before the first |
| 345 |
section. Then comes the output for each section in turn, possibly |
| 346 |
interspersed with end-of-page marks. Finally, \.{CWEAVE} will generate a |
| 347 |
cross-reference index that lists each section number in which each \CEE/ |
| 348 |
identifier appears, and it will also generate an alphabetized list |
| 349 |
of the section names, as well as a table of contents that |
| 350 |
shows the page and section numbers for each ``starred'' section. |
| 351 |
|
| 352 |
What is a ``starred'' section, you ask? A section that begins with `\.{@*}' |
| 353 |
instead of `\.{@\ }' is slightly special in that it denotes a new major |
| 354 |
group of sections. The `\.{@*}' should be followed by the title of this |
| 355 |
group, followed by a period. Such sections will always start on a new page |
| 356 |
in the \TEX/ output, and the group title will appear as a running headline |
| 357 |
on all subsequent pages until the next starred section. The title will also |
| 358 |
appear in the table of contents, and in boldface type at the beginning of |
| 359 |
its section. Caution: Do not use \TEX/ control sequences in such titles, |
| 360 |
unless you know that the \.{cwebmac} macros will do the right thing with |
| 361 |
them. The reason is that these titles are converted to uppercase when |
| 362 |
they appear as running heads, and they are converted to boldface when they |
| 363 |
appear at the beginning of their sections, and they are also written out to |
| 364 |
a table-of-contents file used for temporary storage while \TEX/ is |
| 365 |
working; whatever control sequences you use must be meaningful in all |
| 366 |
three of these modes. |
| 367 |
|
| 368 |
The \TEX/ output produced by \.{CWEAVE} for each section consists of |
| 369 |
the following: First comes the section number (e.g., `\.{\\M123.}' |
| 370 |
at the beginning of section 123, except that `\.{\\N}' appears in place of |
| 371 |
`\.{\\M}' at the beginning of a starred section). Then comes the |
| 372 |
\TEX/ part of the section, copied almost verbatim except as noted |
| 373 |
below. Then comes the middle part and the \CEE/ part, formatted |
| 374 |
so that there will be a little extra space between them if both are |
| 375 |
nonempty. The middle and \CEE/ parts are obtained by inserting |
| 376 |
a bunch of funny-looking \TEX/ macros into the \CEE/ program; these |
| 377 |
macros handle typographic details about fonts and proper math spacing, |
| 378 |
as well as line breaks and indentation. |
| 379 |
|
| 380 |
\section C Code in \TEX/ Text and Vice Versa. |
| 381 |
When you are typing \TEX/ text, you will probably want to make frequent |
| 382 |
reference to variables and other quantities in your \CEE/ code, and you |
| 383 |
will want those variables to have the same typographic treatment |
| 384 |
when they appear in your text as when they appear in your |
| 385 |
program. Therefore the \.{CWEB} language allows you to get the effect of |
| 386 |
\CEE/ editing within \TEX/ text, if you place `\.|' marks before and |
| 387 |
after the \CEE/ material. For example, suppose you want to say something |
| 388 |
like this: |
| 389 |
$$\hbox{If \PB{\\{pa}} is declared as `\PB{\&{int} ${}{*}\\{pa}$}', |
| 390 |
the assignment \PB{$\\{pa}\K{\AND}\|a[\T{0}]$} makes \PB{\\{pa}} |
| 391 |
point to the zeroth element of \PB{\|a}.}$$ |
| 392 |
The \TEX/ text would look like this in your \.{CWEB} file: |
| 393 |
$$\lpile{\.{If |pa| is declared as `|int *pa|', the assignment}\cr |
| 394 |
\.{|pa=\&a[0]| makes |pa| point to the zeroth element of |a|.}\cr}$$ |
| 395 |
And \.{CWEAVE} translates this into something |
| 396 |
you are glad you didn't have to type: |
| 397 |
$$\lpile{\.{If \\PB\{\\\\\{pa\}\} is declared as `\\PB\{\\\&\{int\} |
| 398 |
\$\{\}\{*\}\\\\\{pa\}\$\}', the}\cr |
| 399 |
\.{assignment \\PB\{\$\\\\\{pa\}\\K\{\\AND\}\\|a[\\T\{0\}]\$\} |
| 400 |
makes \\PB\{\\\\\{pa\}\} point}\cr |
| 401 |
\.{to the zeroth element of \\PB\{\\|a\}.}\cr}$$ |
| 402 |
Incidentally, the cross-reference index that \.{CWEAVE} would make, in |
| 403 |
the presence of a comment like this, would include |
| 404 |
the current section number as one of the index entries for \\{pa}, |
| 405 |
even though \\{pa} might not appear in the \CEE/ part of |
| 406 |
this section. Thus, the index covers references to identifiers in |
| 407 |
the explanatory comments as well as in the program itself; you will |
| 408 |
soon learn to appreciate this feature. However, the identifiers |
| 409 |
\&{int} and \|a\ would not be indexed, |
| 410 |
because \.{CWEAVE} does not make index entries for reserved words or |
| 411 |
single-letter identifiers. Such identifiers are felt to be so ubiquitous |
| 412 |
that it would be pointless to mention every place where they occur. |
| 413 |
|
| 414 |
Although a section begins with \TEX/ text and ends with \CEE/ text, we |
| 415 |
have noted that the dividing line isn't sharp, since \CEE/ text can be |
| 416 |
included in \TEX/ text if it is enclosed in `\pb'. Conversely, \TEX/ text |
| 417 |
appears frequently within \CEE/ text, because everything in |
| 418 |
comments (i.e., between \.{/*} and \.{*/}, or following \.{//}) |
| 419 |
is treated as \TEX/ text. |
| 420 |
Likewise, the text of a section name consists of \TEX/ text, but |
| 421 |
the construct \.{@<section name@>} as a whole is expected to be found |
| 422 |
in \CEE/ text; thus, one typically goes back and forth |
| 423 |
between the \CEE/ and \TEX/ environments in a natural way, as in these |
| 424 |
examples: |
| 425 |
$$ |
| 426 |
\displaylines{ |
| 427 |
\hbox{\.{if} \.{(x==0)} \.{@<Empty} \.{the} \.{|buffer|} \.{array@>}} \cr |
| 428 |
\hbox{\.{...} \.{using} \.{the} \.{algorithm} |
| 429 |
\.{in} \.{|@<Empty} \.{the} \.{|buffer|} \.{array@>|.}} } |
| 430 |
$$ |
| 431 |
The first of these excerpts |
| 432 |
would be found in the \CEE/ part of a section, into which the code |
| 433 |
from the section |
| 434 |
named ``Empty the \\{buffer} array'' is being spliced. The second excerpt |
| 435 |
would be found in the \TEX/ part of the section, and the named section |
| 436 |
is being ``cited'', rather than defined or used. |
| 437 |
(Note the `\pb' surrounding the section name in this case.) |
| 438 |
|
| 439 |
\section Macros. |
| 440 |
The control code \.{@d} followed by |
| 441 |
$$\\{identifier}\.{ }\hbox{\CEE/ text}\qquad\hbox{or by}\qquad |
| 442 |
\\{identifier}\.(\\{par}_1,\ldots,\\{par}_n\.{) }\hbox{\CEE/ text}$$ |
| 443 |
(where there is no blank between the |
| 444 |
\\{identifier} and the parentheses in the second case) is |
| 445 |
transformed by \.{CTANGLE} into a preprocessor command, starting with |
| 446 |
\.{\#define}, which is printed at the top of the \CEE/ output file |
| 447 |
as explained earlier. |
| 448 |
|
| 449 |
A `\.{@d}' macro definition can go on for several lines, and the |
| 450 |
newlines don't have to be protected by backslashes, since \.{CTANGLE} |
| 451 |
itself inserts the backslashes. If |
| 452 |
for any reason you need a \.{\#define} command at a specific spot in |
| 453 |
your \CEE/ file, you can treat it as \CEE/ code, instead of as a |
| 454 |
\.{CWEB} macro; but then you do have to protect newlines yourself. |
| 455 |
|
| 456 |
\section Strings and constants. |
| 457 |
If you want a string to appear in the \CEE/ file, delimited by pairs of |
| 458 |
\.' or \." marks as usual, you can type it exactly so in the \.{CWEB} file, |
| 459 |
except that the character `\.@' should be typed `\.{@@}' (it becomes a |
| 460 |
control code, the only one that can appear in strings; see below). |
| 461 |
Strings should end on the same line as they begin, unless there's a |
| 462 |
backslash at the end of lines within them. |
| 463 |
|
| 464 |
\TEX/ and \CEE/ have different ways to refer to octal and hex constants, |
| 465 |
because \TEX/ is oriented to technical writing while \CEE/ is oriented to |
| 466 |
computer processing. In \TEX/ you |
| 467 |
make a constant octal or hexadecimal by prepending \.' or \.", |
| 468 |
respectively, to it; in \CEE/ the constant should be preceded by \.0 |
| 469 |
or \.{0x}. In \.{CWEB} it seems reasonable to let each convention hold |
| 470 |
in its respective realm; so in \CEE/ text you get $40_8$ by typing |
| 471 |
`\.{040}', which \.{CTANGLE} faithfully copies into the \CEE/ file (for |
| 472 |
the compiler's benefit) and which \.{CWEAVE} prints as $\T{\~40}$. |
| 473 |
Similarly, \.{CWEAVE} prints the hexadecimal \CEE/ constant `\.{0x20}' |
| 474 |
as \T{\^20}. The use of italic font for octal digits and typewriter font |
| 475 |
for hexadecimal digits makes the meaning of such constants clearer in |
| 476 |
a document. For consistency, then, you should type `\.{|040|}' or |
| 477 |
`\.{|0x20|}' in the \TEX/ part of the section. |
| 478 |
|
| 479 |
And if you type a binary constant like `\.{0b00101010}', \.{CWEAVE} prints |
| 480 |
it as $\T{\\00101010}$. In all numeric literals you may add \.' separators |
| 481 |
for improved readability. |
| 482 |
|
| 483 |
\section Control codes. |
| 484 |
A \.{CWEB} {\sl control code\/} |
| 485 |
is a two-character combination of which the first is `\.@'. |
| 486 |
We've already seen the meaning of several control codes; it's time to |
| 487 |
list them more methodically. |
| 488 |
|
| 489 |
In the following list, |
| 490 |
the letters in brackets after a control code indicate in what contexts that |
| 491 |
code is allowed. $L$ indicates that the code is allowed in limbo; $T$ |
| 492 |
(for \TEX/), $M$ (for middle), and $C$ (for \CEE/) mean that the code is |
| 493 |
allowed in each of the three parts of a section, at top level---that |
| 494 |
is, outside such constructs as `\pb' and section names. An arrow $\to$ |
| 495 |
means that the control code terminates the present part of the \.{CWEB} |
| 496 |
file, and inaugurates the part indicated by the letter following the |
| 497 |
arrow. Thus $[LTMC\to T]$ next to \.{@\ } indicates that this control |
| 498 |
code can occur in limbo, or in any of the three parts of a section, and |
| 499 |
that it starts the (possibly empty) \TEX/ part of the following section. |
| 500 |
|
| 501 |
Two other abbreviations can occur in these brackets: The letter $r$ stands for |
| 502 |
{\it restricted context}, that is, material inside \CEE/ comments, section |
| 503 |
names, \CEE/ strings and control texts (defined below); the letter |
| 504 |
$c$ stands for {\it inner \CEE/ context}, that is, \CEE/ material |
| 505 |
inside `\pb' (including `\pb's inside comments, but not those |
| 506 |
occurring in other restricted contexts). An asterisk $*$ following |
| 507 |
the brackets means |
| 508 |
that the context from this control code to the matching \.{@>} is |
| 509 |
restricted. |
| 510 |
|
| 511 |
Control codes involving letters are case-insensitive; thus \.{@d} and |
| 512 |
\.{@D} are equivalent. Only the lowercase versions are mentioned |
| 513 |
specifically below. |
| 514 |
|
| 515 |
\gdef\@#1[#2] {\penalty-50\yskip\hangindent 2em\noindent\.{@#1\unskip |
| 516 |
\spacefactor1000{ }}$[#2]$\quad} |
| 517 |
\def\more{\hangindent 2em \hangafter0} |
| 518 |
\def\subsec{\penalty-300\medskip\noindent} |
| 519 |
|
| 520 |
\@@ [LTMCrc] A double \.@ denotes the single character `\.@'. This is |
| 521 |
the only control code that is legal everywhere. |
| 522 |
Note that you must use this convention if you are giving an internet |
| 523 |
email address in a \.{CWEB} file (e.g., \.{tex-k@@tug.org}). |
| 524 |
|
| 525 |
\subsec |
| 526 |
Here are the codes that introduce the \TEX/ part of a section. |
| 527 |
|
| 528 |
\@\ [LTMC\to T] This denotes the beginning of a new (unstarred) |
| 529 |
section. A tab mark or form feed or |
| 530 |
end-of-line character is equivalent to a space when it follows an \.@ |
| 531 |
sign (and in most other cases). |
| 532 |
|
| 533 |
\@* [LTMC\to T] This denotes the beginning of a new starred |
| 534 |
section, i.e., a section that begins a new major group. The title of the new |
| 535 |
group should appear after the \.{@*}, followed by a period. As explained |
| 536 |
above, \TEX/ control sequences should be avoided in such titles unless |
| 537 |
they are quite simple. When \.{CWEAVE} and \.{CTANGLE} read a \.{@*}, they |
| 538 |
print an asterisk on the terminal |
| 539 |
followed by the current section number, so that the user |
| 540 |
can see some indication of progress. The very first section should be starred. |
| 541 |
|
| 542 |
\more You can specify the ``depth'' of a starred section by typing \.* or a |
| 543 |
decimal number after the \.{@*}; this indicates the relative ranking |
| 544 |
of the current group of sections in the program hierarchy. Top-level |
| 545 |
portions of the program, introduced by \.{@**}, get their names typeset |
| 546 |
in boldface type in the table of contents; they are said to have |
| 547 |
depth~$-1$. Otherwise the depth is a nonnegative number, which governs |
| 548 |
the amount of indentation on the contents page. Such indentation helps |
| 549 |
clarify the structure of a long program. The depth is assumed to be 0 |
| 550 |
if it is not specified explicitly; when your program is short, you |
| 551 |
might as well leave all depths zero. A starred section always begins |
| 552 |
a new page in the output, unless the depth is greater than~1. |
| 553 |
|
| 554 |
\subsec |
| 555 |
The middle part of each section consists of any number of |
| 556 |
macro definitions (beginning with \.{@d}) and format definitions (beginning |
| 557 |
with \.{@f} or \.{@s}), intermixed in any order. |
| 558 |
|
| 559 |
\@d [TM\to M] Macro definitions begin with \.{@d}, followed by |
| 560 |
an identifier and optional parameters and \CEE/ text as explained earlier. |
| 561 |
|
| 562 |
\@f [TM\to M] Format definitions begin with \.{@f}; they cause |
| 563 |
\.{CWEAVE} to treat identifiers in a special way when they appear in |
| 564 |
\CEE/ text. The general form of a format definition is `\.{@f} \|l |
| 565 |
\|r', followed by an optional comment enclosed between |
| 566 |
\.{/*} and \.{*/}, where \|l and \|r |
| 567 |
are identifiers; \.{CWEAVE} will subsequently treat identifier \|l as it |
| 568 |
currently treats \|r. This feature allows a \.{CWEB} programmer to invent |
| 569 |
new reserved words and/or to unreserve some of \CEE/'s reserved |
| 570 |
identifiers. For example, the common words `error' and `line' |
| 571 |
have been given a special meaning in the \CEE/ preprocessor, |
| 572 |
so \.{CWEAVE} is set up to format them specially; if you want a variable |
| 573 |
named \\{error} or \\{line}, you should say |
| 574 |
$$\.{@f error normal}\qquad\qquad\.{@f line normal}$$ |
| 575 |
somewhere in your program. |
| 576 |
|
| 577 |
\more If \|r is the special identifier `\\{TeX}', identifier \|l |
| 578 |
will be formatted as a \TEX/ control sequence; for example, |
| 579 |
`\.{@f foo TeX}' in the \.{CWEB} file will cause identifier \\{foo} to |
| 580 |
be output as \.{\\foo} by \.{CWEAVE}. The programmer should define |
| 581 |
\.{\\foo} to have whatever custom format is desired, assuming \TEX/ |
| 582 |
math mode. (Each underline |
| 583 |
character is converted to \.{x} when making the \TEX/ control sequence, |
| 584 |
and each dollar sign is converted to~\.X; |
| 585 |
thus \\{foo\_bar} becomes \.{\\fooxbar}. Other characters, including digits, |
| 586 |
are left untranslated, so \TEX/ will consider them as macro parameters, |
| 587 |
not as part of the control sequence itself. For example, |
| 588 |
$$\.{\\def\\x\#1\{x\_\{\#1\}\} @f x1 TeX @f x2 TeX}$$ |
| 589 |
will format \.{x1} and \.{x2} not as \\{x1} and \\{x2} but as $x_1$ and $x_2$.) |
| 590 |
|
| 591 |
\more If \|r is the special identifier `\\{make\_pair}', identifier \|l will |
| 592 |
be treated as a \CPLUSPLUS/ function template. For example, after |
| 593 |
\.{@f}~\.{convert}~\.{make\_pair} one can say `\.{convert<int>(2.5)}' without |
| 594 |
having \.< and \.> misunderstood as less-than and greater-than signs. |
| 595 |
|
| 596 |
\more \.{CWEAVE} knows that identifiers being |
| 597 |
defined with a \&{typedef} should become reserved words; thus you |
| 598 |
don't need format definitions very often. |
| 599 |
|
| 600 |
\@s [TM\to M;\;L] Same as \.{@f}, but \.{CWEAVE} does not show the format |
| 601 |
definition in the output, and the optional \CEE/ comment is not |
| 602 |
allowed. This is used mostly in \.{@i} files. |
| 603 |
|
| 604 |
\subsec |
| 605 |
Next come the codes that govern the \CEE/ part of a section. |
| 606 |
|
| 607 |
\@{c @p} [TM\to C] The \CEE/ part of an unnamed section begins with \.{@c} |
| 608 |
(or with \.{@p} for ``program''; both control codes do the same thing). |
| 609 |
This causes \.{CTANGLE} to append the following \CEE/ code |
| 610 |
to the first-order program text, as explained on page~\tangref. |
| 611 |
Note that \.{CWEAVE} does not print a `\.{@c}' in the \TEX/ |
| 612 |
output, so if you are creating a \.{CWEB} file based on a \TEX/-printed |
| 613 |
\.{CWEB} documentation you have to remember to insert \.{@c} in the |
| 614 |
appropriate places of the unnamed sections. |
| 615 |
|
| 616 |
\@< [TM\to C;\; C;\; c] $*$ This control code introduces a |
| 617 |
section name (or unambiguous prefix, as discussed above), which |
| 618 |
consists of \TEX/ text and extends to the matching \.{@>}. |
| 619 |
The whole construct \.{@<...@>} is conceptually a \CEE/ element. |
| 620 |
The behavior is different depending on the context: |
| 621 |
|
| 622 |
\more A \.{@<} appearing in contexts $T$ and $M$ attaches the |
| 623 |
following section name to the current section, and inaugurates the |
| 624 |
\CEE/ part of the section. The closing \.{@>} should be followed by |
| 625 |
\.{=} or \.{+=}. |
| 626 |
|
| 627 |
\more |
| 628 |
In context $C$, \.{@<} indicates that the named |
| 629 |
section is being used---its \CEE/ definition is spliced in by |
| 630 |
\.{CTANGLE}, as explained on page~\tangref. |
| 631 |
As an error-detection measure, |
| 632 |
\.{CTANGLE} and \.{CWEAVE} complain if such a section name is followed |
| 633 |
by \.=, because most likely this is meant as the definition of a new |
| 634 |
section, and so should be preceded by \.{@\ }. If you really want to |
| 635 |
say $\langle\,$foo$\,\rangle=\\{bar}$, where $\langle\,$foo$\,\rangle$ |
| 636 |
is being used and not defined, put a newline before the \.=. |
| 637 |
|
| 638 |
\more |
| 639 |
Finally, in inner \CEE/ context (that is, within `\pb' in the \TEX/ part |
| 640 |
of a section or in a comment), \.{@<...@>} |
| 641 |
means that the named section is being |
| 642 |
cited. Such an occurrence is ignored by \.{CTANGLE}. Note that |
| 643 |
even here we think of the section name as being a \CEE/ element, hence the \pb. |
| 644 |
|
| 645 |
\@( [TM\to C;\;C;\;c] $*$ A section name can begin with \.{@(}. |
| 646 |
Everything works just as for \.{@<}, except that the \CEE/ code |
| 647 |
of the section named \.{@(foo@>} is written by \.{CTANGLE} |
| 648 |
to file \.{foo}. In this way you can get multiple-file output from |
| 649 |
a single \.{CWEB} file. (The \.{@d} definitions are not output |
| 650 |
to such files, only to the master \.{.c} file.) One use of this feature |
| 651 |
is to produce header files for other program modules that will be loaded |
| 652 |
with the present one. Another use is to produce a test routine that |
| 653 |
goes with your program. By keeping the sources for a program and its |
| 654 |
header and test routine together, you are more likely to keep |
| 655 |
all three consistent with each other. Notice that the output of a named |
| 656 |
section can be incorporated in several different output files, because |
| 657 |
you can mention \.{@<foo@>} in both \.{@(bar1@>} and \.{@(bar2@>}. |
| 658 |
|
| 659 |
\@h [Cc] Causes \.{CTANGLE} |
| 660 |
to insert at the current spot the \.{\#define} statements |
| 661 |
from the middle parts of all sections, |
| 662 |
and {\it not\/} to write them at the beginning of the \CEE/ file. |
| 663 |
Useful when you want the macro definitions to come after the include files, |
| 664 |
say. (Ignored by \.{CTANGLE} inside `\pb'.) |
| 665 |
|
| 666 |
\subsec |
| 667 |
The next several control codes introduce ``control |
| 668 |
texts,'' which end with the next `\.{@>}'. The closing `\.{@>}' must be on |
| 669 |
the same line of the \.{CWEB} file as the line where the control text began. |
| 670 |
The context from each of these control codes to the matching \.{@>} is |
| 671 |
restricted. |
| 672 |
|
| 673 |
\@\^ [TMCc] $*$ The control text that follows, up to the next |
| 674 |
`\.{@>}', will be entered into the index together with the identifiers of |
| 675 |
the \CEE/ program; this text will appear in roman type. For example, to |
| 676 |
put the phrase ``system dependencies'' into the index that is output by |
| 677 |
\.{CWEAVE}, type |
| 678 |
`\.{@\^system dependencies@>}' in each section |
| 679 |
that you want to index as system dependent. |
| 680 |
|
| 681 |
\@. [TMCc] $*$ The control text that follows will be entered into the index |
| 682 |
in \.{typewriter} \.{type}. |
| 683 |
|
| 684 |
\@: [TMCc] $*$ The control text that follows will be entered into the index |
| 685 |
in a format controlled by the \TEX/ macro `\.{\\9}', which you |
| 686 |
should define as desired. |
| 687 |
|
| 688 |
\@t [MCc] $*$ The control text that follows will |
| 689 |
be put into a \TEX/ \.{\\hbox} and formatted along with the neighboring |
| 690 |
\CEE/ program. This text is ignored by \.{CTANGLE}, but it can be used |
| 691 |
for various purposes within \.{CWEAVE}. For example, you can make comments |
| 692 |
that mix \CEE/ and classical mathematics, as in `$\\{size}<2^{15}$', by |
| 693 |
typing `\.{|size < 2@t\$\^\{15\}\$@>|}'. |
| 694 |
|
| 695 |
\@= [MCc] $*$ The control text that follows will |
| 696 |
be passed verbatim to the \CEE/ program. |
| 697 |
|
| 698 |
\@q [LTMCc] $*$ The control text that follows will |
| 699 |
be totally ignored---it's a comment for readers of the \.{CWEB} file only. |
| 700 |
A file intended to be included in |
| 701 |
limbo, with \.{@i}, can identify itself with \.{@q} comments. |
| 702 |
Another use is to balance unbalanced parentheses in \CEE/ strings, |
| 703 |
so that your text editor's parenthesis matcher doesn't go into a tailspin. |
| 704 |
|
| 705 |
\@! [TMCc] $*$ |
| 706 |
The section number in an index entry will be underlined if `\.{@!}' |
| 707 |
immediately precedes the identifier or control text being indexed. This |
| 708 |
convention is used to distinguish the sections where an identifier is |
| 709 |
defined, or where it is explained in some special way, from the sections |
| 710 |
where it is used. A~reserved word or an identifier of length one will not |
| 711 |
be indexed except for underlined entries. An `\.{@!}' is implicitly inserted |
| 712 |
by \.{CWEAVE} when an identifier is being defined or declared in \CEE/ |
| 713 |
code; for example, the definition |
| 714 |
$$\hbox{\&{int} \\{array}[\\{max\_dim}], \\{count}${}=\\{old\_count};$}$$ |
| 715 |
makes the names \\{array} and \\{count} get an underlined entry in the |
| 716 |
index. Statement labels, function definitions like |
| 717 |
\\{main}(\&{int}~\\{argc},\,\&{char}~$*$\\{argv}[\,]), |
| 718 |
and \&{typedef} definitions also |
| 719 |
imply underlining. An old-style |
| 720 |
function definition (without prototyping) doesn't define its arguments; |
| 721 |
the arguments will, however, be considered to be defined |
| 722 |
(i.e., their index entries will be underlined) if their types are |
| 723 |
declared before the body of the function in the usual way |
| 724 |
(e.g., `\&{int}~\\{argc}; \&{char}~${*}\\{argv}[\,]$; $\{\,\ldots\,\}$'). |
| 725 |
Thus \.{@!} is not needed very often, except in unusual constructions |
| 726 |
or in cases like |
| 727 |
$$\.{enum boolean \{@!false, @!true\};}$$ |
| 728 |
here \.{@!} gives the best results because individual constants enumerated |
| 729 |
by \.{enum} are not automatically underlined in the index at their |
| 730 |
point of definition. |
| 731 |
|
| 732 |
\subsec |
| 733 |
We now turn to control codes that affect only the operation of |
| 734 |
\.{CTANGLE}. |
| 735 |
|
| 736 |
\@' [MCc] This control code is dangerous because it has quite different |
| 737 |
meanings in \.{CWEB} and the original \.{WEB}. In \.{CWEB} it produces the |
| 738 |
decimal constant corresponding to the ASCII code for a string of length~1 |
| 739 |
(e.g., \.{@'a'} is \.{CTANGLE}d into \.{97} and \.{@'\\t'} into |
| 740 |
\.9). You might want to use this if you need to work in ASCII on a |
| 741 |
non-ASCII machine; but in most cases the \CEE/ conventions of |
| 742 |
\.{<ctype.h>} are adequate for character-set-independent programming. |
| 743 |
|
| 744 |
\@\& [MCc] The \.{@\&} operation causes whatever is on its left to be |
| 745 |
adjacent to whatever is on its right, in the \CEE/ output. No spaces or |
| 746 |
line breaks will separate these two items. |
| 747 |
|
| 748 |
\@l [L] \.{CWEB} programmers have the option of using any 8-bit character |
| 749 |
code from the often-forbidden range 128--255 within \TEX/ text; such |
| 750 |
characters are also permitted in strings and even in identifiers of the |
| 751 |
\CEE/ program. |
| 752 |
Under various extensions of the basic |
| 753 |
ASCII standard, the higher 8-bit codes correspond |
| 754 |
to accented letters, letters from non-Latin alphabets, |
| 755 |
and so on. When such characters occur in identifiers, \.{CTANGLE} must replace |
| 756 |
them by standard ASCII alphanumeric characters or |
| 757 |
\.{\_}, in order to generate legal \CEE/ code. It does this by means |
| 758 |
of a transliteration table, which by default associates the string |
| 759 |
\.{Xab} to the character with ASCII code \T{\^}$ab$ (where $a$ and $b$ are |
| 760 |
hexadecimal digits, and $a\ge8$). By placing the |
| 761 |
construction \.{@l\ ab\ newstring} in limbo, you are telling |
| 762 |
\.{CTANGLE} to replace this character by \.{newstring} instead. |
| 763 |
For example, the ISO Latin-1 code for the letter `\"u' is \T{\^FC} |
| 764 |
(or \.{'\char`\\374'}), |
| 765 |
and \.{CTANGLE} will normally change this code to the |
| 766 |
three-character sequence \.{XFC} if it |
| 767 |
appears in an identifier. If you say \.{@l} \.{fc} \.{ue}, the code will |
| 768 |
be transliterated into \.{ue} instead. |
| 769 |
|
| 770 |
\more |
| 771 |
\.{CWEAVE} passes 8-bit characters straight through to \TEX/ without |
| 772 |
transliteration; therefore \TEX/ must be prepared to receive them. |
| 773 |
If you are formatting all your nonstandard identifiers as ``custom'' |
| 774 |
control sequences, you should |
| 775 |
make \TEX/ treat all their characters as letters. Otherwise you should either |
| 776 |
make your 8-bit codes ``active'' in \TEX/, or load fonts that |
| 777 |
contain the special characters you need in the correct positions. |
| 778 |
(The font selected by \TEX/ control sequence \.{\\it} is used for |
| 779 |
identifiers.) |
| 780 |
Look for special macro packages designed for \.{CWEB} users in your language; |
| 781 |
or, if you are brave, write one yourself. |
| 782 |
|
| 783 |
\subsec |
| 784 |
The next eight control codes (namely `\.{@,}', `\.{@/}', `\.{@|}', `\.{@\#}', |
| 785 |
`\.{@+}', `\.{@;}', `\.{@[}', and `\.{@]}') have no effect on the \CEE/ |
| 786 |
program output by \.{CTANGLE}; they merely help to improve the readability |
| 787 |
of the \TEX/-formatted \CEE/ that is output by \.{CWEAVE}, in unusual |
| 788 |
circumstances. \.{CWEAVE}'s built-in formatting method is fairly good |
| 789 |
when dealing with syntactically correct \CEE/ text, but |
| 790 |
it is incapable of handling all possible cases, because it must deal with |
| 791 |
fragments of text involving macros and section names; these fragments do |
| 792 |
not necessarily obey \CEE/'s syntax. Although \.{CWEB} allows you to |
| 793 |
override the automatic formatting, your best strategy is not to worry |
| 794 |
about such things until you have seen what \.{CWEAVE} produces automatically, |
| 795 |
since you will probably need to make only a few corrections when you are |
| 796 |
touching up your documentation. |
| 797 |
|
| 798 |
\@, [MCc] This control code inserts a thin space in \.{CWEAVE}'s output. |
| 799 |
Sometimes you need this extra space if you are using |
| 800 |
macros in an unusual way, e.g., if two identifiers are adjacent. |
| 801 |
|
| 802 |
\@/ [MC] This control code causes a line break to occur within a \CEE/ |
| 803 |
program formatted by \.{CWEAVE}. Line breaks |
| 804 |
are chosen automatically by \TEX/ according to a scheme that works 99\%\ |
| 805 |
of the time, but sometimes you will prefer to force a line break so that |
| 806 |
the program is segmented according to logical rather than visual |
| 807 |
criteria. If a comment follows, say `\.{@/@,}' to break the line |
| 808 |
before the comment. |
| 809 |
|
| 810 |
\@| [MC] This control code specifies an optional line break in the midst of |
| 811 |
an expression. For example, if you have |
| 812 |
a long expression on the right-hand side of an assignment |
| 813 |
statement, you can use `\.{@|}' to specify breakpoints more logical than |
| 814 |
the ones that \TEX/ might choose on visual grounds. |
| 815 |
|
| 816 |
\@\# [MC] This control code forces a line break, like \.{@/} does, |
| 817 |
and it also causes a little extra white space to appear between the lines at |
| 818 |
this break. You might use it, for example, |
| 819 |
between groups of macro definitions that are logically separate but within |
| 820 |
the same section. \.{CWEB} automatically inserts this extra space |
| 821 |
between functions, between external declarations and functions, and |
| 822 |
between declarations and statements within a function. |
| 823 |
|
| 824 |
\@+ [MC] This control code cancels a line break that might otherwise be |
| 825 |
inserted by \.{CWEAVE}, e.g., before the word `\&{else}', if you want to |
| 826 |
put a short if--else construction on a single line. |
| 827 |
If you say `\.{\{@+}' at the beginning of a compound statement |
| 828 |
that is the body of a function, the first declaration or |
| 829 |
statement of the function will appear on the same line as the |
| 830 |
left brace, and it will be indented by the same amount as the |
| 831 |
second declaration or statement on the next line. |
| 832 |
|
| 833 |
\@; [MC] This control code is treated like a semicolon, for formatting |
| 834 |
purposes, except that it is invisible. You can use it, for example, after |
| 835 |
a section name or macro when the \CEE/ text represented by that section or macro |
| 836 |
is a compound statement or ends |
| 837 |
with a semicolon. Consider constructions like |
| 838 |
$$\lpile{\.{if (condition) macro @;}\cr |
| 839 |
\.{else break;}\cr}$$ |
| 840 |
where \\{macro} is defined to be a compound statement (enclosed in braces). |
| 841 |
This is a well-known infelicity of \CEE/ syntax. You can add a visible |
| 842 |
semicolon with \.{@t;@>} (before \.{@;}). |
| 843 |
|
| 844 |
\@{[} [MC] See \.{@]}. |
| 845 |
|
| 846 |
\@] [MC] Place \.{@[...@]} brackets around program text that \.{CWEAVE} is |
| 847 |
supposed to format as an expression, if it doesn't already do so. (This |
| 848 |
occasionally applies to unusual macro arguments.) Also |
| 849 |
insert `\.{@[@]}' between a simple type name and a left parenthesis |
| 850 |
when declaring a pointer to a function, as in |
| 851 |
$$\.{int @[@] (*f)();}$$ |
| 852 |
otherwise \.{CWEAVE} will confuse the first part of that declaration with |
| 853 |
the \CPLUSPLUS/ expression `\&{int}($*f$)'. Another example, for people |
| 854 |
who want to use low-level \.{\#define} commands in the midst of \CEE/ code |
| 855 |
and the definition begins with a cast: |
| 856 |
$$\.{\#define foo @[(int)(bar)@]}$$ |
| 857 |
|
| 858 |
\subsec |
| 859 |
The remaining control codes govern the input that \.{CWEB} sees. |
| 860 |
|
| 861 |
\@{x @y @z}[\\{change\_file}] |
| 862 |
\.{CWEAVE} and \.{CTANGLE} are designed to work with two input files, |
| 863 |
called \\{web\_file} and \\{change\_file}, where \\{change\_file} contains |
| 864 |
data that overrides selected portions of \\{web\_file}. The resulting merged |
| 865 |
text is actually what has been called the \.{CWEB} file elsewhere in this |
| 866 |
report. |
| 867 |
|
| 868 |
\more Here's how it works: The change file consists of zero or more ``changes,'' |
| 869 |
where a change has the form `\.{@x}$\langle$old lines$\rangle$\.{@y}$\langle$% |
| 870 |
new lines$\rangle$\.{@z}'. The special control codes \.{@x}, \.{@y}, \.{@z}, |
| 871 |
which are allowed only in change files, must appear at the beginning of a line; |
| 872 |
the remainder of such a line is ignored. |
| 873 |
The $\langle$old lines$\rangle$ represent material that exactly matches |
| 874 |
consecutive lines of the \\{web\_file}; the $\langle$new lines$\rangle$ |
| 875 |
represent zero or more lines that are supposed to replace the old. Whenever |
| 876 |
the first ``old line'' of a change is found to match a line in the |
| 877 |
\\{web\_file}, all the other lines in that change must match too. |
| 878 |
|
| 879 |
\more Between changes, before the first change, and after the last change, |
| 880 |
the change file can have any number of lines that do not begin with |
| 881 |
`\.{@x}', `\.{@y}', or~`\.{@z}'. Such lines are bypassed and not used for |
| 882 |
matching purposes. |
| 883 |
|
| 884 |
\more This dual-input feature is useful when working with a master \.{CWEB} |
| 885 |
file that has been received from elsewhere (e.g., \.{ctangle.w} or |
| 886 |
\.{cweave.w} or \.{ctex.w}), when changes are desirable to customize the |
| 887 |
program for your local computer system. You will be able to debug your |
| 888 |
system-dependent changes without clobbering the master web file; and once |
| 889 |
your changes are working, you will be able to incorporate them readily |
| 890 |
into new releases of the master web file that you might receive from time |
| 891 |
to time. |
| 892 |
|
| 893 |
\@i [\\{web\_file}] |
| 894 |
Furthermore the \\{web\_file} itself can be a combination of |
| 895 |
several files. When either \.{CWEAVE} or \.{CTANGLE} is reading a file and |
| 896 |
encounters the control code \.{@i} at the beginning of a line, it |
| 897 |
interrupts normal reading and starts looking at the file named after the |
| 898 |
\.{@i}, much as the \CEE/ preprocessor does when it encounters an \.{\#include} |
| 899 |
line. After the included file has been entirely read, the program |
| 900 |
goes back to the next line |
| 901 |
of the original file. The file name following \.{@i} can be |
| 902 |
surrounded by \." characters, but such delimiters are |
| 903 |
optional. Include files can nest. |
| 904 |
|
| 905 |
\more |
| 906 |
Change files can have lines starting with \.{@i}. In this way |
| 907 |
you can replace one included file with another. Conceptually, the |
| 908 |
replacement mechanism described above does its work first, and |
| 909 |
its output is then checked for \.{@i} lines. If \.{@i} \.{foo} |
| 910 |
occurs between \.{@y} and \.{@z} in a change file, individual lines |
| 911 |
of file \.{foo} and files it includes are not changeable; but changes |
| 912 |
can be made to lines from files that were included by unchanged input. |
| 913 |
|
| 914 |
\more On \UNIX/ systems (and others that support environment variables), |
| 915 |
if the environment variable \.{CWEBINPUTS} is set, or if the compiler flag |
| 916 |
of the same name was defined at compile time, |
| 917 |
\.{CWEB} will look for include files in the directory thus named, if |
| 918 |
it cannot find them in the current directory. |
| 919 |
|
| 920 |
\section Additional features and caveats. |
| 921 |
|
| 922 |
1. In certain installations of \.{CWEB} that |
| 923 |
{\def\\#1#2{`{\tentex\char'#1#2}'}% |
| 924 |
have an extended character set, the characters |
| 925 |
\\13, \\01, \\31, \\32, \\34, \\35, |
| 926 |
\\36, \\37, \\04, \\20, and \\21} |
| 927 |
can be typed as abbreviations for `\.{++}', `\.{--}', `\.{->}', |
| 928 |
`\.{!=}', `\.{<=}', `\.{>=}', `\.{==}', `\.{\v\v}', `\.{\&\&}', |
| 929 |
`\.{<<}', and `\.{>>}', |
| 930 |
respectively. |
| 931 |
|
| 932 |
2. If you have an extended character set, you can use it with only minimal |
| 933 |
restrictions, as discussed under the rules for \.{@l} above. But you should |
| 934 |
stick to standard ASCII characters if you want to write programs that will |
| 935 |
be useful to all the poor souls out there who don't have extended |
| 936 |
character sets. |
| 937 |
|
| 938 |
3. The \TEX/ file output by \.{CWEAVE} is broken into lines having at most |
| 939 |
80 characters each. When |
| 940 |
\TEX/ text is being copied, the existing line breaks are copied as well. |
| 941 |
If you aren't doing anything too tricky, \.{CWEAVE} will recognize when |
| 942 |
a \TEX/ comment is being split across two or more lines, and it will |
| 943 |
append `\.\%' to the beginning of such continued comments. |
| 944 |
|
| 945 |
4. \CEE/ text is translated by a ``bottom up'' procedure that |
| 946 |
identifies each token as a ``part of speech'' and combines parts of speech |
| 947 |
into larger and larger phrases as much as possible according to a special |
| 948 |
grammar that is explained in the documentation of \.{CWEAVE}. It is easy to |
| 949 |
learn the translation scheme for simple constructions like single |
| 950 |
identifiers and short expressions, just by looking at a few examples of |
| 951 |
what \.{CWEAVE} does, but the general mechanism is somewhat complex because |
| 952 |
it must handle much more than \CEE/ itself. Furthermore the output |
| 953 |
contains embedded codes that cause \TEX/ to indent and break lines as |
| 954 |
necessary, depending on the fonts used and the desired page width. For |
| 955 |
best results it is wise to avoid enclosing long \CEE/ texts in \pb, since the |
| 956 |
indentation and line breaking codes are omitted when the \pb\ text is |
| 957 |
translated from \CEE/ to \TEX/. Stick to simple expressions or |
| 958 |
statements. If a \CEE/ preprocessor command is enclosed in \pb, |
| 959 |
the \.\# that introduces it must be at the beginning of a line, |
| 960 |
or \.{CWEAVE} won't print it correctly. |
| 961 |
|
| 962 |
5. Comments are not permitted in \pb\ text. After a `\.|' |
| 963 |
signals the change from \TEX/ text to \CEE/ text, the next `\.|' that is |
| 964 |
not part of a string or control text or section name ends the \CEE/ text. |
| 965 |
|
| 966 |
6. A comment must have properly nested occurrences of left and right |
| 967 |
braces, otherwise \.{CWEAVE} will complain. But it |
| 968 |
does try to balance the braces, so that \TEX/ won't foul up too much. |
| 969 |
|
| 970 |
7. When you're debugging a program and decide to omit some of your |
| 971 |
\CEE/ code, do NOT simply ``comment it out.'' Such comments are not |
| 972 |
in the spirit of \.{CWEB} documentation; they will appear to readers |
| 973 |
as if they were explanations of the uncommented-out instructions. |
| 974 |
Furthermore, comments of a program must be valid \TEX/ text; hence |
| 975 |
\.{CWEAVE} will get confused if you enclose \CEE/ statements in |
| 976 |
\.{/*...*/} instead of in \.{/*|...|*/}. If you must comment out |
| 977 |
\CEE/ code, you can surround it with preprocessor commands |
| 978 |
like \.{\#if 0==1} and \.{\#endif}. |
| 979 |
|
| 980 |
8. The \.{@f} feature allows you to define one identifier to act like |
| 981 |
another, and these format definitions are carried out sequentially. |
| 982 |
In general, a given identifier has only one printed format |
| 983 |
throughout the entire document, and this format is used even before |
| 984 |
the \.{@f} that defines it. The reason is that \.{CWEAVE} operates in two |
| 985 |
passes; it processes \.{@f}'s and cross-references on the first pass and |
| 986 |
it does the output on the second. (However, identifiers that |
| 987 |
implicitly get a boldface format, thanks to a \.{typedef} declaration, |
| 988 |
don't obey this rule; they are printed differently before and after |
| 989 |
the relevant \.{typedef}. This is unfortunate, but hard to fix. You can |
| 990 |
get around the restriction by saying, say, `\.{@s} \.{foo} \.{int}', |
| 991 |
before or after the \.{typedef}.) |
| 992 |
|
| 993 |
9. Sometimes it is desirable to insert spacing into formatted \CEE/ code that |
| 994 |
is more general than the thin space provided by `\.{@,}'. The \.{@t} feature |
| 995 |
can be used for this purpose; e.g., `\.{@t\\hskip 1in@>}' will |
| 996 |
leave one inch of blank space. Furthermore, `\.{@t\\4@>}' can be |
| 997 |
used to backspace by one unit of indentation, since the control sequence |
| 998 |
\.{\\4} is defined in \.{cwebmac} to be such a backspace. (This |
| 999 |
control sequence is used, for example, at the beginning of lines that |
| 1000 |
contain labeled statements, so that the label will stick out a little at |
| 1001 |
the left.) You can also use `\.{@t\}\\3\{-5@>}' to force a break |
| 1002 |
in the middle of an expression. |
| 1003 |
|
| 1004 |
10. Each identifier in \.{CWEB} has a single formatting convention. Therefore |
| 1005 |
you shouldn't use the same identifier to denote, say, both a type name and |
| 1006 |
part of a \.{struct}, even though \CEE/ does allow this. |
| 1007 |
|
| 1008 |
\section Running the programs. |
| 1009 |
The \UNIX/ command line for \.{CTANGLE} is |
| 1010 |
$$\.{ctangle [options] webfile[.w] [\{changefile[.ch]|-\} [outfile[.c]]]}$$ |
| 1011 |
and the same conventions apply to \.{CWEAVE}. If `\.-' or no change file is |
| 1012 |
specified, the change file is null. The extensions \.{.w} and \.{.ch} |
| 1013 |
are appended only if the given file names contain no dot. If the |
| 1014 |
web file defined in this way cannot be found, the extension \.{.web} |
| 1015 |
will be tried. For example, `\.{cweave} \.{cob}' will try to read |
| 1016 |
\.{cob.w}; failing that, it will try \.{cob.web} before giving up. |
| 1017 |
If no output file name is specified, the name of the \CEE/ file output by |
| 1018 |
\.{CTANGLE} is obtained by appending the extension \.{.c}; |
| 1019 |
the name of the \TEX/ file output by \.{CWEAVE} gets the extension \.{.tex}. |
| 1020 |
Index files output by \.{CWEAVE} replace \.{.tex} by \.{.idx} and \.{.scn}. |
| 1021 |
|
| 1022 |
Programmers who like terseness might choose to set up their |
| 1023 |
operating shell so that `\.{wv}' expands to |
| 1024 |
`\.{cweave -bhp}'; this will suppress most terminal output from \.{CWEAVE} |
| 1025 |
except for error messages. |
| 1026 |
|
| 1027 |
Options are introduced either by a \.- sign, to turn an option off, |
| 1028 |
or by a \.+ sign to turn one on. For example, `\.{-fb}' turns off |
| 1029 |
options \.f and \.b; `\.{+s}' turns on option \.s. Options can be |
| 1030 |
specified before the file names, after the file names, or both. The following |
| 1031 |
options are currently implemented: |
| 1032 |
|
| 1033 |
\yskip |
| 1034 |
\def\option#1 {\textindent{\.#1}\hangindent2\parindent} |
| 1035 |
|
| 1036 |
\option b Print a banner line at the beginning of execution. (On |
| 1037 |
by default.) |
| 1038 |
|
| 1039 |
\option e Enclose \CEE/ material formatted by \.{CWEAVE} in |
| 1040 |
brackets \.{\\PB\{...\}}, so that special hooks can be used. |
| 1041 |
(On by default.) (Has no effect on \.{CTANGLE}.) |
| 1042 |
|
| 1043 |
\option f Force line breaks after each \CEE/ statement formatted |
| 1044 |
by \.{CWEAVE}. (On by default; \.{-f} saves paper but looks less \CEE/-like |
| 1045 |
to some people.) (Has no effect on \.{CTANGLE}.) |
| 1046 |
|
| 1047 |
\option F Force the first statement in a `compound statement' (a.k.a.\ |
| 1048 |
`block') on a line of its own. (On by default; some people prefer the |
| 1049 |
\.{-F} layout.) (Has no effect on \.{CTANGLE}.) |
| 1050 |
|
| 1051 |
\option h Print a happy message at the conclusion of a successful |
| 1052 |
run. (On by default.) |
| 1053 |
|
| 1054 |
\option k Keep single quotes (\.') in numeric literals in the |
| 1055 |
\CEE//\CPLUSPLUS/ output. (Off by default.) (\.{CTANGLE} only.) |
| 1056 |
|
| 1057 |
\option p Give progress reports as the program runs. (On by default.) |
| 1058 |
|
| 1059 |
\option s Show statistics about memory usage after the program |
| 1060 |
runs to completion. (Off by default.) |
| 1061 |
If you have large \.{CWEB} files or sections, you may need to see how close |
| 1062 |
you come to exceeding the capacity of \.{CTANGLE} and/or \.{CWEAVE}. |
| 1063 |
|
| 1064 |
\option t Treat \&{typename} in a template like \&{typedef}. |
| 1065 |
(Off by default.) (Has no effect on \.{CTANGLE}.) |
| 1066 |
|
| 1067 |
\option x Include indexes and a table of contents in the \TEX/ file |
| 1068 |
output by \.{CWEAVE}. (On by default.) (Has no effect on \.{CTANGLE}.) |
| 1069 |
|
| 1070 |
\section Further details about formatting. |
| 1071 |
You may not like the way \.{CWEAVE} handles certain |
| 1072 |
situations. If you're desperate, you can customize \.{CWEAVE} |
| 1073 |
by changing its grammar. This means changing the source code, |
| 1074 |
a task that you might find amusing. A table of grammar rules |
| 1075 |
appears in the \.{CWEAVE} source listing, and you can make a separate |
| 1076 |
copy of that table by copying the file \.{prod.w} found in the \.{CWEB} |
| 1077 |
sources and saying `\.{cweave}~\.{-x}~\.{prod}', followed by |
| 1078 |
`\.{tex}~\.{prod}'. |
| 1079 |
|
| 1080 |
You can see exactly |
| 1081 |
how \.{CWEAVE} is parsing your \CEE/ code by preceding |
| 1082 |
it with the line `\.{@ @c @2}'. (The control code `\.{@2}' |
| 1083 |
turns on a ``peeping'' mode, and `\.{@0}' turns it off.) |
| 1084 |
For example, if you run \.{CWEAVE} on the file |
| 1085 |
\medskip |
| 1086 |
\begingroup |
| 1087 |
\verbatim |
| 1088 |
@ @c @2 |
| 1089 |
main (argc,argv) |
| 1090 |
char **argv; |
| 1091 |
{ for (;argc>0;argc--) printf("%s\n",argv[argc-1]); } |
| 1092 |
!endgroup |
| 1093 |
\endgroup |
| 1094 |
\medskip\noindent |
| 1095 |
you get the following gibberish on your screen: |
| 1096 |
\medskip |
| 1097 |
\begingroup |
| 1098 |
\verbatim |
| 1099 |
[...] |
| 1100 |
14:*exp +(+ exp... |
| 1101 |
11:*exp +exp+ raw... |
| 1102 |
10:*+exp+ raw +ubinop?+... |
| 1103 |
[...] |
| 1104 |
60: +fn_decl+*+{+ -stmt- +}- |
| 1105 |
55:*+fn_decl+ -stmt- |
| 1106 |
52:*+function- |
| 1107 |
[...] |
| 1108 |
!endgroup |
| 1109 |
\endgroup |
| 1110 |
\medskip |
| 1111 |
The first line says that grammar rule 14 has just been applied, and \.{CWEAVE} |
| 1112 |
currently has in its memory a sequence of chunks of \TEX/ code (called |
| 1113 |
``scraps'') that are respectively |
| 1114 |
of type \\{exp} (for expression), open-parenthesis, |
| 1115 |
\\{exp} again, close-parenthesis, and further scraps that haven't yet |
| 1116 |
been considered by the parser. (The \.+ and \.- signs stipulate that |
| 1117 |
\TEX/ should be in or out of math mode at the scrap boundaries. The \.* shows |
| 1118 |
the parser's current position.) |
| 1119 |
Then rule 11 is applied, and |
| 1120 |
the sequence $(\,exp\,)$ becomes an \\{exp} and so on. In the |
| 1121 |
end the whole \CEE/ text has become one big scrap of type \\{function}. |
| 1122 |
|
| 1123 |
Sometimes things don't work as smoothly, and you get a bunch of |
| 1124 |
lines lumped together. This means that \.{CWEAVE} could not |
| 1125 |
digest something in your \CEE/ code. For instance, suppose |
| 1126 |
`\.{@<Argument definitions@>}' had appeared instead of |
| 1127 |
`\.{char **argv;}' in the program above. Then \.{CWEAVE} would have |
| 1128 |
been somewhat mystified, since it thinks that section names |
| 1129 |
are just \\{exp}s. Thus it would tell \TEX/ to format |
| 1130 |
`\X2:Argument declarations\X' on the same line as |
| 1131 |
`$\\{main}(\\{argc},\39\\{argv}{}$)'. |
| 1132 |
In this case you should help \.{CWEAVE} by putting `\.{@/}' after |
| 1133 |
`\.{main(argc,argv)}' (plus `\.{@t\\qquad@>}' for consistent indentation). |
| 1134 |
|
| 1135 |
\.{CWEAVE} automatically inserts a bit of extra space between declarations |
| 1136 |
and the first apparent statement of a block. One way to defeat this |
| 1137 |
spacing locally is |
| 1138 |
$$\vbox{\halign{#\hfil\cr |
| 1139 |
\.{int x;@+@t\}\\6\{@>}\cr |
| 1140 |
\.{@<Other locals@>@;@\#}\cr}}$$ |
| 1141 |
the `\.{@\#}' will put extra space after `$\langle\,$Other locals$\,\rangle$'. |
| 1142 |
|
| 1143 |
\section Hypertext and hyperdocumentation. |
| 1144 |
|
| 1145 |
Many people have of course noticed analogies between \.{CWEB} and the |
| 1146 |
World Wide Web. The \.{CWEB} macros are in fact set up so that the output of |
| 1147 |
\.{CWEAVE} can be converted easily into Portable Document Format, with |
| 1148 |
clickable hyperlinks that can be read with your favorite {\mc PDF} viewer, |
| 1149 |
using a widely available open-source program called \.{dvipdfm} developed by |
| 1150 |
Mark~A. Wicks. After using \.{CWEAVE} to convert \.{cob.w} into \.{cob.tex}, |
| 1151 |
you can prepare a hypertext version of the program by giving the commands |
| 1152 |
$$\vbox{\halign{\.{#}\hfil\cr |
| 1153 |
tex "\\let\\pdf+ \\input cob"\cr |
| 1154 |
dvipdfm cob\cr}}$$ |
| 1155 |
instead of invoking \TeX\ in the normal way. (Thanks to Hans Hagen, |
| 1156 |
C\'esar Augusto Rorato Crusius, and Julian Gilbey |
| 1157 |
for the macros that make this work.) |
| 1158 |
Alternatively, thanks to H\`an Th\^e\kern-.3em\raise.3ex\hbox{\'{}} Th\`anh |
| 1159 |
and Andreas Scherer, you can generate \.{cob.pdf} in one step by simply |
| 1160 |
saying `\.{pdftex}~\.{cob}'. |
| 1161 |
Alternative ways to create {\mc PDF} output from \.{CWEB} input are to say |
| 1162 |
`\.{xetex}~\.{cob}' or `\.{luatex}~\.{cob}'. |
| 1163 |
|
| 1164 |
Similar output for ``smart'' devices can be created with Martin Ruckert's |
| 1165 |
Hi\TeX\ and its dynamic \.{HINT} format; just say `\.{hitex}~\.{cob}'. |
| 1166 |
\.{HINT} files can be viewed with the \.{hintview} program, which is available |
| 1167 |
from \.{https://hint.userweb.mwn.de/hint/hintview.html}. |
| 1168 |
|
| 1169 |
A more elaborate system called \.{CTWILL}, which extends the usual cross |
| 1170 |
references of \.{CWEAVE} by preparing links from the uses of identifiers |
| 1171 |
to their definitions, is also available---provided that you are willing |
| 1172 |
to work a bit harder in cases where an identifier is multiply defined. |
| 1173 |
\.{CTWILL} is intended primarily for hardcopy output, but its principles |
| 1174 |
could be used for hypertext as well. |
| 1175 |
See Chapter 11 of {\sl Digital Typography\/} by D.~E. Knuth (1999), and |
| 1176 |
the program sources at \.{ftp://ftp.cs.stanford.edu/pub/ctwill}. |
| 1177 |
|
| 1178 |
\section Appendices. |
| 1179 |
|
| 1180 |
As an example of a real program written in \.{CWEB}, Appendix~A |
| 1181 |
contains an excerpt from the \.{CWEB} program itself. The reader who |
| 1182 |
examines the listings in this appendix carefully will get a good |
| 1183 |
feeling for the basic ideas of \.{CWEB}. |
| 1184 |
|
| 1185 |
Appendix B displays the files that set \TEX/ up to accept |
| 1186 |
the output of \.{CWEAVE}, and Appendix~C discusses how to use some of those |
| 1187 |
macros to vary the output formats. |
| 1188 |
|
| 1189 |
A ``long'' version of this manual, which can be produced from the \.{CWEB} |
| 1190 |
sources via the \UNIX/ command \.{make} \.{fullmanual}, also contains |
| 1191 |
appendices D, E, and~F, which exhibit the complete source code for |
| 1192 |
\.{CTANGLE} and \.{CWEAVE}. |
| 1193 |
|
| 1194 |
\vfil\eject\titletrue |
| 1195 |
|
| 1196 |
\def\runninghead{APPENDIX A --- {\ninett CWEB} FILE FORMAT} |
| 1197 |
\section Appendix A: Excerpts from a \.{CWEB} Program. |
| 1198 |
|
| 1199 |
This appendix consists |
| 1200 |
of four listings. The first shows the \.{CWEB} input that |
| 1201 |
generated sections 27--31 of the file \.{common.w}, which contains |
| 1202 |
routines common to \.{CWEAVE} and \.{CTANGLE}. |
| 1203 |
Note that some of the lines are indented to show the program structure; |
| 1204 |
the indentation is ignored by \.{CWEAVE} and \.{CTANGLE}, but users find |
| 1205 |
that \.{CWEB} files are quite readable if they have some such indentation. |
| 1206 |
|
| 1207 |
The second and third listings show corresponding parts of |
| 1208 |
the \CEE/ code output by \.{CTANGLE} and of |
| 1209 |
the \TEX/ code output by \.{CWEAVE}, when run on \.{common.w}. |
| 1210 |
The fourth listing shows how that output looks in print. |
| 1211 |
|
| 1212 |
\vskip 6pt |
| 1213 |
\begingroup \def\tt{\eighttt} \baselineskip9pt |
| 1214 |
\verbatim |
| 1215 |
@ Procedure |prime_the_change_buffer| |
| 1216 |
sets |change_buffer| in preparation for the next matching operation. |
| 1217 |
Since blank lines in the change file are not used for matching, we have |
| 1218 |
|(change_limit==change_buffer && !!changing)| if and only if |
| 1219 |
the change file is exhausted. This procedure is called only when |
| 1220 |
|changing| is |true|; hence error messages will be reported correctly. |
| 1221 |
|
| 1222 |
@c |
| 1223 |
static void |
| 1224 |
prime_the_change_buffer(void) |
| 1225 |
{ |
| 1226 |
change_limit=change_buffer; /* this value is used if the change file ends */ |
| 1227 |
@<Skip over comment lines in the change file; |return| if end of file@>@; |
| 1228 |
@<Skip to the next nonblank line; |return| if end of file@>@; |
| 1229 |
@<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>@; |
| 1230 |
} |
| 1231 |
|
| 1232 |
@ @<Predecl...@>=@+static void prime_the_change_buffer(void); |
| 1233 |
|
| 1234 |
@ While looking for a line that begins with \.{@@x} in the change file, we |
| 1235 |
allow lines that begin with \.{@@}, as long as they don't begin with \.{@@y}, |
| 1236 |
\.{@@z}, or \.{@@i} (which would probably mean that the change file is fouled up). |
| 1237 |
|
| 1238 |
@<Skip over comment lines in the change file...@>= |
| 1239 |
while(true) { |
| 1240 |
change_line++; |
| 1241 |
if (!!input_ln(change_file)) return; |
| 1242 |
if (limit<buffer+2) continue; |
| 1243 |
if (buffer[0]!!='@@') continue; |
| 1244 |
if (xisupper(buffer[1])) buffer[1]=tolower((int)buffer[1]); |
| 1245 |
if (buffer[1]=='x') break; |
| 1246 |
if (buffer[1]=='y' || buffer[1]=='z' || buffer[1]=='i') { |
| 1247 |
loc=buffer+2; |
| 1248 |
err_print("!! Missing @@x in change file"); |
| 1249 |
@.Missing @@x...@> |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
@ Here we are looking at lines following the \.{@@x}. |
| 1254 |
|
| 1255 |
@<Skip to the next nonblank line...@>= |
| 1256 |
do { |
| 1257 |
change_line++; |
| 1258 |
if (!!input_ln(change_file)) { |
| 1259 |
err_print("!! Change file ended after @@x"); |
| 1260 |
@.Change file ended...@> |
| 1261 |
return; |
| 1262 |
} |
| 1263 |
} while (limit==buffer); |
| 1264 |
|
| 1265 |
@ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>= |
| 1266 |
change_limit=change_buffer+(ptrdiff_t)(limit-buffer); |
| 1267 |
strncpy(change_buffer,buffer,(size_t)(limit-buffer+1)); |
| 1268 |
!endgroup |
| 1269 |
\endgroup |
| 1270 |
\vfil\eject |
| 1271 |
|
| 1272 |
\def\runninghead{APPENDIX A --- TRANSLATION BY {\ninett CTANGLE}} |
| 1273 |
|
| 1274 |
Here's the portion of the \CEE/ code generated by \.{CTANGLE} that corresponds |
| 1275 |
to the source on the preceding page. Notice that sections~29, 30 and~31 |
| 1276 |
have been tangled into section~27. |
| 1277 |
|
| 1278 |
\vskip6pt |
| 1279 |
\begingroup \def\tt{\eighttt} \baselineskip9pt |
| 1280 |
\verbatim |
| 1281 |
/*:23*//*27:*/ |
| 1282 |
#line 227 "common.w" |
| 1283 |
|
| 1284 |
static void |
| 1285 |
prime_the_change_buffer(void) |
| 1286 |
{ |
| 1287 |
change_limit= change_buffer; |
| 1288 |
/*29:*/ |
| 1289 |
#line 243 "common.w" |
| 1290 |
|
| 1291 |
while(true){ |
| 1292 |
change_line++; |
| 1293 |
if(!!input_ln(change_file))return; |
| 1294 |
if(limit<buffer+2)continue; |
| 1295 |
if(buffer[0]!!='@')continue; |
| 1296 |
if(xisupper(buffer[1]))buffer[1]= tolower((int)buffer[1]); |
| 1297 |
if(buffer[1]=='x')break; |
| 1298 |
if(buffer[1]=='y'||buffer[1]=='z'||buffer[1]=='i'){ |
| 1299 |
loc= buffer+2; |
| 1300 |
err_print("!! Missing @x in change file"); |
| 1301 |
|
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
/*:29*/ |
| 1306 |
#line 232 "common.w" |
| 1307 |
|
| 1308 |
/*30:*/ |
| 1309 |
#line 260 "common.w" |
| 1310 |
|
| 1311 |
do{ |
| 1312 |
change_line++; |
| 1313 |
if(!!input_ln(change_file)){ |
| 1314 |
err_print("!! Change file ended after @x"); |
| 1315 |
|
| 1316 |
return; |
| 1317 |
} |
| 1318 |
}while(limit==buffer); |
| 1319 |
|
| 1320 |
/*:30*/ |
| 1321 |
#line 233 "common.w" |
| 1322 |
|
| 1323 |
/*31:*/ |
| 1324 |
#line 270 "common.w" |
| 1325 |
|
| 1326 |
change_limit= change_buffer+(ptrdiff_t)(limit-buffer); |
| 1327 |
strncpy(change_buffer,buffer,(size_t)(limit-buffer+1)); |
| 1328 |
|
| 1329 |
/*:31*/ |
| 1330 |
#line 234 "common.w" |
| 1331 |
|
| 1332 |
} |
| 1333 |
|
| 1334 |
/*:27*//*32:*/ |
| 1335 |
#line 296 "common.w" |
| 1336 |
!endgroup |
| 1337 |
\endgroup |
| 1338 |
\vfil\eject |
| 1339 |
|
| 1340 |
\def\runninghead{APPENDIX A --- TRANSLATION BY {\ninett CWEAVE}} |
| 1341 |
|
| 1342 |
Here is the corresponding excerpt from \.{common.tex}. |
| 1343 |
\ifhint\relax\else(Code for section 31 is omitted for space reasons.)\fi |
| 1344 |
|
| 1345 |
\vskip6pt |
| 1346 |
\begingroup \def\tt{\eighttt} \baselineskip9pt |
| 1347 |
\verbatim |
| 1348 |
\M{27}Procedure \PB{\\{prime\_the\_change\_buffer}} |
| 1349 |
sets \PB{\\{change\_buffer}} in preparation for the next matching operation. |
| 1350 |
Since blank lines in the change file are not used for matching, we have |
| 1351 |
\PB{$(\\{change\_limit}\E\\{change\_buffer}\W\R\\{changing})$} if and only if |
| 1352 |
the change file is exhausted. This procedure is called only when |
| 1353 |
\PB{\\{changing}} is \PB{\\{true}}; hence error messages will be reported |
| 1354 |
correctly. |
| 1355 |
|
| 1356 |
\Y\B\1\1\&{static} \&{void} \\{prime\_the\_change\_buffer}(\&{void})\2\2\6 |
| 1357 |
${}\{{}$\1\6 |
| 1358 |
${}\\{change\_limit}\K\\{change\_buffer}{}$;\C{ this value is used if the |
| 1359 |
change file ends }\6 |
| 1360 |
\X29:Skip over comment lines in the change file; \PB{\&{return}} if end of file% |
| 1361 |
\X\6 |
| 1362 |
\X30:Skip to the next nonblank line; \PB{\&{return}} if end of file\X\6 |
| 1363 |
\X31:Move \PB{\\{buffer}} and \PB{\\{limit}} to \PB{\\{change\_buffer}} and % |
| 1364 |
\PB{\\{change\_limit}}\X\6 |
| 1365 |
\4${}\}{}$\2\par |
| 1366 |
\fi |
| 1367 |
|
| 1368 |
\M{28}\B\X3:Predeclaration of procedures\X${}\mathrel+\E{}$\5 |
| 1369 |
\&{static} \&{void} \\{prime\_the\_change\_buffer}(\&{void});\par |
| 1370 |
\fi |
| 1371 |
|
| 1372 |
\M{29}While looking for a line that begins with \.{@x} in the change file, we |
| 1373 |
allow lines that begin with \.{@}, as long as they don't begin with \.{@y}, |
| 1374 |
\.{@z}, or \.{@i} (which would probably mean that the change file is fouled |
| 1375 |
up). |
| 1376 |
|
| 1377 |
\Y\B\4\X29:Skip over comment lines in the change file; \PB{\&{return}} if end |
| 1378 |
of file\X${}\E{}$\6 |
| 1379 |
\&{while} (\\{true})\5 |
| 1380 |
${}\{{}$\1\6 |
| 1381 |
${}\\{change\_line}\PP;{}$\6 |
| 1382 |
\&{if} ${}(\R\\{input\_ln}(\\{change\_file})){}$\1\5 |
| 1383 |
\&{return};\2\6 |
| 1384 |
\&{if} ${}(\\{limit}<\\{buffer}+\T{2}){}$\1\5 |
| 1385 |
\&{continue};\2\6 |
| 1386 |
\&{if} ${}(\\{buffer}[\T{0}]\I\.{'@'}){}$\1\5 |
| 1387 |
\&{continue};\2\6 |
| 1388 |
\&{if} (\\{xisupper}(\\{buffer}[\T{1}]))\1\5 |
| 1389 |
${}\\{buffer}[\T{1}]\K\\{tolower}((\&{int})\,\\{buffer}[\T{1}]);{}$\2\6 |
| 1390 |
\&{if} ${}(\\{buffer}[\T{1}]\E\.{'x'}){}$\1\5 |
| 1391 |
\&{break};\2\6 |
| 1392 |
\&{if} ${}(\\{buffer}[\T{1}]\E\.{'y'}\V\\{buffer}[\T{1}]\E\.{'z'}\V\\{buffer}[% |
| 1393 |
\T{1}]\E\.{'i'}){}$\5 |
| 1394 |
${}\{{}$\1\6 |
| 1395 |
${}\\{loc}\K\\{buffer}+\T{2};{}$\6 |
| 1396 |
\\{err\_print}(\.{"!!\ Missing\ @x\ in\ cha}\)\.{nge\ file"});\6 |
| 1397 |
\4${}\}{}$\2\6 |
| 1398 |
\4${}\}{}$\2\par |
| 1399 |
\U27.\fi |
| 1400 |
|
| 1401 |
\M{30}Here we are looking at lines following the \.{@x}. |
| 1402 |
|
| 1403 |
\Y\B\4\X30:Skip to the next nonblank line; \PB{\&{return}} if end of file\X${}% |
| 1404 |
\E{}$\6 |
| 1405 |
\&{do}\5 |
| 1406 |
${}\{{}$\1\6 |
| 1407 |
${}\\{change\_line}\PP;{}$\6 |
| 1408 |
\&{if} ${}(\R\\{input\_ln}(\\{change\_file})){}$\5 |
| 1409 |
${}\{{}$\1\6 |
| 1410 |
\\{err\_print}(\.{"!!\ Change\ file\ ended}\)\.{\ after\ @x"});\6 |
| 1411 |
\&{return};\6 |
| 1412 |
\4${}\}{}$\2\6 |
| 1413 |
\4${}\}{}$\2\5 |
| 1414 |
\&{while} ${}(\\{limit}\E\\{buffer}){}$;\par |
| 1415 |
\U27.\fi |
| 1416 |
!ifhint |
| 1417 |
\M{31}\B\X31:Move \PB{\\{buffer}} and \PB{\\{limit}} to \PB{\\{change\_buffer}} |
| 1418 |
and \PB{\\{change\_limit}}\X${}\E{}$\6 |
| 1419 |
$\\{change\_limit}\K\\{change\_buffer}+(\&{ptrdiff\_t})(\\{limit}-% |
| 1420 |
\\{buffer});{}$\6 |
| 1421 |
${}\\{strncpy}(\\{change\_buffer},\39\\{buffer},\39(\&{size\_t})(\\{limit}-% |
| 1422 |
\\{buffer}+\T{1})){}$;\par |
| 1423 |
\Us27\ET32.\fi |
| 1424 |
!fi |
| 1425 |
!endgroup |
| 1426 |
\endgroup |
| 1427 |
\vfil\eject |
| 1428 |
|
| 1429 |
\def\runninghead{APPENDIX A --- FINAL DOCUMENT} |
| 1430 |
|
| 1431 |
And here's what the same excerpt looks like when typeset. |
| 1432 |
(Can you spot the typographical nicety used in the \.{CWEB} code?) |
| 1433 |
\let\K=\leftarrow |
| 1434 |
|
| 1435 |
\M{27}Procedure \PB{\\{prime\_the\_change\_buffer}} |
| 1436 |
sets \PB{\\{change\_buffer}} in preparation for the next matching operation. |
| 1437 |
Since blank lines in the change file are not used for matching, we have |
| 1438 |
\PB{$(\\{change\_limit}\E\\{change\_buffer}\W\R\\{changing})$} if and only if |
| 1439 |
the change file is exhausted. This procedure is called only when |
| 1440 |
\PB{\\{changing}} is \PB{\\{true}}; hence error messages will be reported |
| 1441 |
correctly. |
| 1442 |
|
| 1443 |
\Y\B\1\1\&{static} \&{void} \\{prime\_the\_change\_buffer}(\&{void})\2\2\6 |
| 1444 |
${}\{{}$\1\6 |
| 1445 |
${}\\{change\_limit}\K\\{change\_buffer}{}$;\C{ this value is used if the |
| 1446 |
change file ends }\6 |
| 1447 |
\X29:Skip over comment lines in the change file; \PB{\&{return}} if end of file% |
| 1448 |
\X\6 |
| 1449 |
\X30:Skip to the next nonblank line; \PB{\&{return}} if end of file\X\6 |
| 1450 |
\X31:Move \PB{\\{buffer}} and \PB{\\{limit}} to \PB{\\{change\_buffer}} and % |
| 1451 |
\PB{\\{change\_limit}}\X\6 |
| 1452 |
\4${}\}{}$\2\par |
| 1453 |
\fi |
| 1454 |
|
| 1455 |
\M{28}\B\X3:Predeclaration of procedures\X${}\mathrel+\E{}$\5 |
| 1456 |
\&{static} \&{void} \\{prime\_the\_change\_buffer}(\&{void});\par |
| 1457 |
\fi |
| 1458 |
|
| 1459 |
\M{29}While looking for a line that begins with \.{@x} in the change file, we |
| 1460 |
allow lines that begin with \.{@}, as long as they don't begin with \.{@y}, |
| 1461 |
\.{@z}, or \.{@i} (which would probably mean that the change file is fouled |
| 1462 |
up). |
| 1463 |
|
| 1464 |
\Y\B\4\X29:Skip over comment lines in the change file; \PB{\&{return}} if end |
| 1465 |
of file\X${}\E{}$\6 |
| 1466 |
\&{while} (\\{true})\5 |
| 1467 |
${}\{{}$\1\6 |
| 1468 |
${}\\{change\_line}\PP;{}$\6 |
| 1469 |
\&{if} ${}(\R\\{input\_ln}(\\{change\_file})){}$\1\5 |
| 1470 |
\&{return};\2\6 |
| 1471 |
\&{if} ${}(\\{limit}<\\{buffer}+\T{2}){}$\1\5 |
| 1472 |
\&{continue};\2\6 |
| 1473 |
\&{if} ${}(\\{buffer}[\T{0}]\I\.{'@'}){}$\1\5 |
| 1474 |
\&{continue};\2\6 |
| 1475 |
\&{if} (\\{xisupper}(\\{buffer}[\T{1}]))\1\5 |
| 1476 |
${}\\{buffer}[\T{1}]\K\\{tolower}((\&{int})\,\\{buffer}[\T{1}]);{}$\2\6 |
| 1477 |
\&{if} ${}(\\{buffer}[\T{1}]\E\.{'x'}){}$\1\5 |
| 1478 |
\&{break};\2\6 |
| 1479 |
\&{if} ${}(\\{buffer}[\T{1}]\E\.{'y'}\V\\{buffer}[\T{1}]\E\.{'z'}\V\\{buffer}[% |
| 1480 |
\T{1}]\E\.{'i'}){}$\5 |
| 1481 |
${}\{{}$\1\6 |
| 1482 |
${}\\{loc}\K\\{buffer}+\T{2};{}$\6 |
| 1483 |
\\{err\_print}(\.{"!\ Missing\ @x\ in\ cha}\)\.{nge\ file"});\6 |
| 1484 |
\4${}\}{}$\2\6 |
| 1485 |
\4${}\}{}$\2\par |
| 1486 |
\U27.\fi |
| 1487 |
|
| 1488 |
\M{30}Here we are looking at lines following the \.{@x}. |
| 1489 |
|
| 1490 |
\Y\B\4\X30:Skip to the next nonblank line; \PB{\&{return}} if end of file\X${}% |
| 1491 |
\E{}$\6 |
| 1492 |
\&{do}\5 |
| 1493 |
${}\{{}$\1\6 |
| 1494 |
${}\\{change\_line}\PP;{}$\6 |
| 1495 |
\&{if} ${}(\R\\{input\_ln}(\\{change\_file})){}$\5 |
| 1496 |
${}\{{}$\1\6 |
| 1497 |
\\{err\_print}(\.{"!\ Change\ file\ ended}\)\.{\ after\ @x"});\6 |
| 1498 |
\&{return};\6 |
| 1499 |
\4${}\}{}$\2\6 |
| 1500 |
\4${}\}{}$\2\5 |
| 1501 |
\&{while} ${}(\\{limit}\E\\{buffer}){}$;\par |
| 1502 |
\U27.\fi |
| 1503 |
|
| 1504 |
\M{31}\B\X31:Move \PB{\\{buffer}} and \PB{\\{limit}} to \PB{\\{change\_buffer}} |
| 1505 |
and \PB{\\{change\_limit}}\X${}\E{}$\6 |
| 1506 |
$\\{change\_limit}\K\\{change\_buffer}+(\&{ptrdiff\_t})(\\{limit}-% |
| 1507 |
\\{buffer});{}$\6 |
| 1508 |
${}\\{strncpy}(\\{change\_buffer},\39\\{buffer},\39(\&{size\_t})(\\{limit}-% |
| 1509 |
\\{buffer}+\T{1})){}$;\par |
| 1510 |
\Us27\ET32.\fi |
| 1511 |
\vfil\eject\titletrue |
| 1512 |
\rightskip=0pt % get out of C mode (cf. \B) |
| 1513 |
\sfcode`;=1500 \pretolerance 200 \hyphenpenalty 50 \exhyphenpenalty 50 |
| 1514 |
|
| 1515 |
\def\runninghead{APPENDIX B --- MACROS FOR FORMATTING} |
| 1516 |
\section Appendix B: The \.{cwebmac.tex} file. |
| 1517 |
This is the file that extends ``plain \TEX/'' format in order to support the |
| 1518 |
features needed by the output of \.{CWEAVE}. |
| 1519 |
|
| 1520 |
\vskip6pt |
| 1521 |
\begingroup \def\tt{\eighttt} \baselineskip9pt |
| 1522 |
\def\printmacs#1{\begingroup |
| 1523 |
\def\do##1{\catcode`##1=12 } \dospecials |
| 1524 |
\parskip 0pt \parindent 0pt |
| 1525 |
\catcode`\ =13 \catcode`\^^M=13 |
| 1526 |
\tt \verbatimdefs \input cweb#1mac \endgroup} |
| 1527 |
\printmacs{\empty} |
| 1528 |
|
| 1529 |
\vskip6pt \baselineskip12pt |
| 1530 |
\noindent And here comes the file \.{cwebacromac.tex} that extends |
| 1531 |
\.{cwebmac.tex} in order to support the ``hypertext'' features needed |
| 1532 |
by the enhanced \TEX/ engines when processing the output of \.{CWEAVE}. |
| 1533 |
|
| 1534 |
\vskip6pt \baselineskip9pt |
| 1535 |
\printmacs{acro} |
| 1536 |
\endgroup |
| 1537 |
\vfil\eject\titletrue |
| 1538 |
|
| 1539 |
\def\runninghead{APPENDIX C --- NOTES ON FORMATTING} |
| 1540 |
\section Appendix C: How to use \.{CWEB} macros. |
| 1541 |
The macros in \.{cwebmac} and \.{cwebacromac} make it possible to produce a |
| 1542 |
variety of formats without editing the output of \.{CWEAVE}, and the purpose |
| 1543 |
of this appendix is to explain some of the possibilities. |
| 1544 |
|
| 1545 |
\def\point#1.{\yskip\indent#1.\quad\ignorespaces} |
| 1546 |
|
| 1547 |
\point 1. Four fonts have been declared in addition to the standard fonts of |
| 1548 |
\.{PLAIN} format: You can say `\.{\{\\mc UNIX\}}' to get {\mc UNIX} in |
| 1549 |
medium-size caps; you can say `\.{\{\\sc STUFF\}}' to get {\sc STUFF} |
| 1550 |
in small caps; and you can select the largish fonts \.{\\titlefont} |
| 1551 |
and \.{\\ttitlefont} in the title of your document, where \.{\\ttitlefont} |
| 1552 |
is a typewriter style of type. There are macros \.{\\UNIX/} and \.{\\CEE/} |
| 1553 |
to refer to \UNIX/ and \CEE/ with medium-size caps. |
| 1554 |
|
| 1555 |
\point 2. When you mention an identifier in \TEX/ text, you normally call |
| 1556 |
it `\.{|identifier|}'. But you can also say `\.{\\\\\{identifier\}}'. The |
| 1557 |
output will look the same in both cases, but the second alternative |
| 1558 |
doesn't put \\{identifier} into the index, since |
| 1559 |
it bypasses \.{CWEAVE}'s translation from \CEE/ mode. In the second |
| 1560 |
case you must put a backslash before each underline character |
| 1561 |
in the identifier. |
| 1562 |
|
| 1563 |
\point 3. To get typewriter-like type, as when referring to `\.{CWEB}', you |
| 1564 |
can use the `\.{\\.}' macro (e.g., `\.{\\.\{CWEB\}}'). In the argument to |
| 1565 |
this macro you should insert an additional backslash before the symbols |
| 1566 |
listed as `special string characters' in the index to \.{CWEAVE}, i.e., |
| 1567 |
before backslashes and dollar signs and the like. |
| 1568 |
A `\.{\\\ }' here will result in the visible space symbol; to get an |
| 1569 |
invisible space following a control sequence you can say `\.{\{\ \}}'. |
| 1570 |
If the string is long, you can break it up into substrings that |
| 1571 |
are separated by `\.{\\)}'\,; the latter gives a discretionary backslash |
| 1572 |
if \TEX/ has to break a line here. |
| 1573 |
|
| 1574 |
\point 4. The three control sequences \.{\\pagewidth}, \.{\\pageheight}, |
| 1575 |
and \.{\\fullpageheight} can be redefined in the limbo section at the |
| 1576 |
beginning of your \.{CWEB} file, to change the dimensions of each page. |
| 1577 |
The default settings |
| 1578 |
$$\lpile{\.{\\pagewidth=6.5in}\cr |
| 1579 |
\.{\\pageheight=8.7in}\cr |
| 1580 |
\.{\\fullpageheight=9in}\cr}$$ |
| 1581 |
were used to prepare this manual; \.{\\fullpageheight} is |
| 1582 |
\.{\\pageheight} plus room for the additional heading and page numbers at |
| 1583 |
the top of each page. If you change any of these quantities, you should |
| 1584 |
call the macro \.{\\setpage} immediately after making the change. |
| 1585 |
|
| 1586 |
\point 5. The \.{\\pageshift} macro defines an amount by which right-hand |
| 1587 |
pages (i.e., odd-numbered pages) are shifted right with respect to |
| 1588 |
left-hand (even-numbered) ones. By adjusting this amount you may be |
| 1589 |
able to get two-sided output in which the page numbers line up on |
| 1590 |
opposite sides of each sheet. |
| 1591 |
|
| 1592 |
\point 6. The \.{\\title} macro will appear at the top of each page |
| 1593 |
in small caps; it is the job name unless redefined. |
| 1594 |
|
| 1595 |
\point 7. The first page usually is assigned page |
| 1596 |
number 1. To start on page 16, with contents |
| 1597 |
on page 15, say this: `\.{\\def\\contentspagenumber\{15\}} |
| 1598 |
\.{\\pageno=\\contentspagenumber} \.{\\advance\\pageno by 1}'. |
| 1599 |
|
| 1600 |
\point 8. The macro \.{\\iftitle} will suppress the header line if it is |
| 1601 |
defined by `\.{\\titletrue}'. The normal value is \.{\\titlefalse} |
| 1602 |
except for the table of contents; thus, the contents |
| 1603 |
page is usually unnumbered. |
| 1604 |
|
| 1605 |
Two macros are provided to give flexibility to the table of |
| 1606 |
contents: \.{\\topofcontents} is invoked just before the contents |
| 1607 |
info is read, and \.{\\botofcontents} is invoked just after. |
| 1608 |
Here's a typical definition: |
| 1609 |
$$\lpile{\.{\\def\\topofcontents\{\\null\\vfill}\cr |
| 1610 |
\.{ { }\\titlefalse \% include headline on the contents page}\cr |
| 1611 |
\.{ { }\\def\\rheader\{\\mainfont The \{\\tt CWEAVE\}{ }processor\\hfil\}}\cr |
| 1612 |
\.{ { }\\centerline\{\\titlefont The \{\\ttitlefont CWEAVE\}{ }processor\}}\cr |
| 1613 |
\.{ { }\\vskip 15pt \\centerline\{(Version 4.12.2)\}{ }\\vfill\}}\cr}$$ |
| 1614 |
Redefining \.{\\rheader}, which is the headline for right-hand pages, |
| 1615 |
suffices in this case to put the desired information at the top of the |
| 1616 |
contents page. |
| 1617 |
|
| 1618 |
\point 9. Data for the table of contents is written to a file that |
| 1619 |
is read after the indexes have been \TEX/ed; there's one line of data |
| 1620 |
for every starred section. The file \.{common.toc} might look like this: |
| 1621 |
$$\lpile{\.{\\ZZ \{Introduction\}\{1\}\{1\}\{30\}\{\}}\cr |
| 1622 |
\.{\\ZZ \{The character set\}\{1\}\{21\}\{36\}\{\}}\cr}$$ |
| 1623 |
and so on. The \.{\\topofcontents} macro could |
| 1624 |
redefine \.{\\ZZ} so that the information appears in any desired format. |
| 1625 |
(See also point~19 below.) |
| 1626 |
|
| 1627 |
\point 10. Sometimes it is necessary or desirable to divide the output of |
| 1628 |
\.{CWEAVE} into subfiles that can be processed separately. For example, |
| 1629 |
the listing of \TEX/ runs to more than 500 pages, and that is enough to |
| 1630 |
exceed the capacity of many printing devices and/or their software. |
| 1631 |
When an extremely large job isn't cut into smaller pieces, the entire |
| 1632 |
process might be spoiled by a single error of some sort, making it |
| 1633 |
necessary to start everything over. |
| 1634 |
|
| 1635 |
Here's a safe way to break a woven file into three parts: |
| 1636 |
Say the pieces are $\alpha$, |
| 1637 |
$\beta$, and $\gamma$, where each piece begins with a starred section. |
| 1638 |
All macros should be defined in the opening limbo section of $\alpha$, |
| 1639 |
and copies of this \TEX/ code should be placed at the |
| 1640 |
beginning of $\beta$ and of $\gamma$. In order to process the parts |
| 1641 |
separately, we need to take care of two things: The starting page |
| 1642 |
numbers of $\beta$ and $\gamma$ need to be set up properly, and |
| 1643 |
the table of contents data from all three runs needs to be |
| 1644 |
accumulated. |
| 1645 |
|
| 1646 |
The \.{cwebmac} macros include two control sequences \.{\\contentsfile} and |
| 1647 |
\.{\\readcontents} that facilitate the necessary processing. We include |
| 1648 |
`\.{\\def\\contentsfile\{cont1\}}' in the limbo section of $\alpha$, and |
| 1649 |
we include `\.{\\def\\contentsfile\{cont2\}}' in the limbo section of |
| 1650 |
$\beta$; this causes \TEX/ to write the contents data for $\alpha$ and $\beta$ |
| 1651 |
into \.{cont1.tex} and \.{cont2.tex}. Now in $\gamma$ we say |
| 1652 |
$$\.{\\def\\readcontents\{\\input cont1 \\input cont2 |
| 1653 |
\\input \\contentsfile\}}\,;$$ |
| 1654 |
this brings in the data from all three pieces, in the proper order. |
| 1655 |
|
| 1656 |
However, we still need to solve the page-numbering problem. One way to |
| 1657 |
do it is to include the following in the limbo material for $\beta$: |
| 1658 |
$$\lpile{\.{\\message\{Please type the last page number of part 1: \}}\cr |
| 1659 |
\.{\\read -1 to \\temp \\pageno=\\temp \\advance\\pageno by 1}\cr}$$ |
| 1660 |
Then you simply provide the necessary data when \TEX/ requests |
| 1661 |
it; a similar construction is used at the beginning of $\gamma$. |
| 1662 |
|
| 1663 |
This method can, of course, be used to divide a woven file into |
| 1664 |
any number of pieces. |
| 1665 |
|
| 1666 |
\point 11. Sometimes it is nice to include things in the index that are |
| 1667 |
typeset in a special way. For example, we might want to have an |
| 1668 |
index entry for `\TeX'. \.{CWEAVE} provides two simple ways to |
| 1669 |
typeset an index entry (unless the entry is an identifier or a reserved word): |
| 1670 |
`\.{@\^}' gives roman type, and `\.{@.}' gives typewriter type. |
| 1671 |
But if we try to typeset `\TeX' in roman type by saying, e.g., |
| 1672 |
`\.{@\^\\TeX@>}', the backslash character gets in the way, |
| 1673 |
and this entry wouldn't appear in the index with the T's. |
| 1674 |
|
| 1675 |
The solution is to use the `\.{@:}' feature, declaring a macro that |
| 1676 |
simply removes a sort key as follows: |
| 1677 |
$$\.{\\def\\9\#1\{\}}$$ |
| 1678 |
Now you can say, e.g., `\.{@:TeX\}\{\\TeX@>}' in your \.{CWEB} file; \.{CWEAVE} |
| 1679 |
puts it into the index alphabetically, based on the sort key, and |
| 1680 |
produces the macro call `\.{\\9\{TeX\}\{\\TeX\}}' which will ensure that |
| 1681 |
the sort key isn't printed. |
| 1682 |
|
| 1683 |
A similar idea can be used to insert hidden material into section |
| 1684 |
names so that they are alphabetized in whatever way you might wish. |
| 1685 |
Some people call these tricks ``special refinements''; others call |
| 1686 |
them ``kludges.'' |
| 1687 |
|
| 1688 |
\point 12. The control sequence \.{\\secno} is set to the number of the |
| 1689 |
section being typeset. |
| 1690 |
|
| 1691 |
\point 13. If you want to list only the sections that have changed, |
| 1692 |
together with the index, put the command `\.{\\let\\maybe=\\iffalse}' in |
| 1693 |
the limbo section before the first section of your \.{CWEB} file. It's |
| 1694 |
customary to make this the first change in your change file. |
| 1695 |
|
| 1696 |
This feature has a \TeX nical limitation, however: You cannot use it |
| 1697 |
together with control sequences like \.{\\proclaim} or \.{\\+} or |
| 1698 |
\.{\\newcount} that plain \TeX\ has declared to be `\.{\\outer}', |
| 1699 |
because \TeX\ refuses to skip silently over such control sequences. |
| 1700 |
One way to work around this limitation is to say |
| 1701 |
$$\.{\\fi \\let\\proclaim\\relax \\def\\proclaim\{...\} \\ifon}$$ |
| 1702 |
where \.{\\proclaim} is redefined to be the same as usual but without |
| 1703 |
an \.{\\outer} qualification. (The \.{\\fi} here stops the conditional |
| 1704 |
skipping, and the \.{\\ifon} turns it back on again.) Similarly, |
| 1705 |
$$\.{\\fi \\newcount\\n \\ifon}$$ |
| 1706 |
is a safe way to use \.{\\newcount}. Plain \TeX\ already provides a |
| 1707 |
non-outer macro \.{\\tabalign} that does the work of \.{\\+}; you can say |
| 1708 |
$$\postdisplaypenalty=10000 |
| 1709 |
\.{\\fi \\let\\+\\tabalign \\ifon}$$ |
| 1710 |
if you prefer the shorter notation \.{\\+}. |
| 1711 |
|
| 1712 |
\point 14. To get output in languages other than English, redefine the |
| 1713 |
macros \.{\\A}, \.{\\As}, \.{\\ATH}, |
| 1714 |
\.{\\ET}, \.{\\ETs}, \.{\\Q}, \.{\\Qs}, \.{\\U}, |
| 1715 |
\.{\\Us}, \.{\\ch}, \.{\\fin}, \.{\\con}, \.{\\today}, \.{\\datethis}, and |
| 1716 |
\.{\\datecontentspage}. \.{CWEAVE} itself need not be changed. |
| 1717 |
|
| 1718 |
\point 15. Some output can be selectively suppressed with the macros |
| 1719 |
\.{\\noatl}, \.{\\noinx}, \.{\\nosecs}, \.{\\nocon}. |
| 1720 |
|
| 1721 |
\point 16. All accents and special text symbols of plain \TEX/ format |
| 1722 |
will work in \.{CWEB} documents just as they are described in |
| 1723 |
Chapter~9 of {\sl The \TEX/book}, with one exception. |
| 1724 |
The dot accent (normally \.{\\.}) must be typed \.{\\:} instead. |
| 1725 |
|
| 1726 |
\point 17. Several commented-out lines in \.{cwebmac.tex} are suggestions |
| 1727 |
that users may wish to adopt. For example, one such line inserts a blank page |
| 1728 |
if you have a duplex printer. Appendices D, E, and F of the complete |
| 1729 |
version of this manual are printed using a commented-out option that |
| 1730 |
substitutes `$\gets$' for `$=$' in the program listings. Looking at those |
| 1731 |
appendices might help you decide which format you like better. |
| 1732 |
|
| 1733 |
\point 18. Andreas Scherer has contributed a macro called \.{\\pdfURL} |
| 1734 |
with which one can say things like the following, anywhere in the \TeX\ parts |
| 1735 |
or the \CEE/ comments of a \.{CWEB} file: |
| 1736 |
$$\vbox{\halign{\.{#}\hfil\cr |
| 1737 |
You can send email to |
| 1738 |
\\pdfURL\{the author\}\{mailto:andreas\\UNDER/github@@freenet.de\}\cr |
| 1739 |
or visit \\pdfURL\{his home page\}\{https://github.com/ascherer\}\cr}}$$ |
| 1740 |
In a {\mc PDF} document, the first argument will appear in blue as |
| 1741 |
clickable text; the {\mc PDF} viewer, if correctly configured, will then |
| 1742 |
redirect those links to the user's browser and open either the email client |
| 1743 |
or the {\mc HTML} viewer. In a hardcopy document, both arguments |
| 1744 |
will be printed ({\tt the second in parentheses and typewriter type}). |
| 1745 |
Certain special characters in an Internet address need to be handled |
| 1746 |
in a somewhat awkward way, so that \.{CWEAVE} and/or \TeX\ |
| 1747 |
will not confuse them with formatting controls: Use \.{@@} for \.@ |
| 1748 |
and \.{\\TILDE/} for \.\~ and \.{\\UNDER/} for \.\_. |
| 1749 |
|
| 1750 |
\point 19. {\mc PDF} documents contain bookmarks that list all the major group |
| 1751 |
titles in the table of contents, some of which will be subsidiary to |
| 1752 |
others if the depth feature of \.{@*} has been used. |
| 1753 |
Such bookmark entries are also known as ``outlines.'' |
| 1754 |
Moreover, the final group title, `Names of the sections', |
| 1755 |
can be opened up to list every section name; users can therefore |
| 1756 |
navigate easily to any desired section. |
| 1757 |
|
| 1758 |
The macros of \.{cwebacromac.tex} are careful to ``sanitize'' all the names |
| 1759 |
that appear as bookmarks, by removing special characters and |
| 1760 |
formatting codes that are inappropriate for the limited typographic |
| 1761 |
capabilities of {\mc PDF} outlines. For example, one section of \.{CWEAVE} |
| 1762 |
is named `Cases for \\{case\_like}', which is represented by the \TeX\ code |
| 1763 |
`\.{Cases} \.{for} \.{\\PB\{\\\\\{case\\\_like\}\}}' in \.{cweave.tex}; its |
| 1764 |
sanitized name is simply `\.{Cases} \.{for} \.{case\_like}'. |
| 1765 |
(When \.{.pdf} files are produced, the fifth parameter of every \.{\\ZZ} in |
| 1766 |
the \.{.toc} file is set to the sanitized form of the first parameter; |
| 1767 |
see point~9 above and point~20 below.) |
| 1768 |
|
| 1769 |
In general, sanitization removes \TeX\ control sequences and braces, |
| 1770 |
except for control sequences defined by \.{CWEB} itself. Such a translation |
| 1771 |
works most of the time, but you can override the defaults and obtain any |
| 1772 |
translation that you want by using \TeX nical tricks. For example, after |
| 1773 |
$$\.{\\sanitizecommand\\foo\{bar\}}$$ |
| 1774 |
the control sequence \.{\\foo} will sanitize to `\.{bar}'. And after |
| 1775 |
$$\.{\\def\\kluj\#1\\\\\{foo\}}$$ |
| 1776 |
the \TeX\ code `\.{\\kluj bar\\\\}' will print as `foo' but sanitize |
| 1777 |
to `\.{bar}', because the control sequences \.{\\kluj} and |
| 1778 |
\.{\\\\} are removed by sanitization. |
| 1779 |
|
| 1780 |
\point 20. Furthermore, group titles can be converted to an arbitrary |
| 1781 |
sanitized text while also changing their form in running headlines, by |
| 1782 |
using \.{\\ifheader}. Consider, for example, a \.{CWEB} source file that |
| 1783 |
begins with the two lines |
| 1784 |
$$\lpile{\.{\\def\\klujj\#1\\\\\{\\ifheader FOO\\else foo\\fi\}}\cr |
| 1785 |
\.{@*Chinese \\klujj bar\\.}\cr}$$ |
| 1786 |
This coding introduces a major group entitled `{\bf Chinese foo}', with |
| 1787 |
running headline `{\eightrm CHINESE FOO}' and table-of-contents entry |
| 1788 |
`Chinese foo'. The corresponding bookmark is, however, `\.{Chinese} \.{bar}'. |
| 1789 |
And the corresponding \.{.toc} file entry is |
| 1790 |
`\.{\\ZZ \{Chinese \\klujj bar\\\\\}\{1\}\{1\}\{1\}\{Chinese bar\}}'. |
| 1791 |
\bye |