# Running Donald Knuth’s CWEB programs

I just realized that no-one documented how to quickly run Donald Knuth’s programs he publishes in his WEB system.

Suppose you want to compile a program like commafree; a program he wrote between September and December 2015 for his Annual Christmas tree lecture in 2015 about Universal Commafree Codes.

`wget http://www-cs-faculty.stanford.edu/~uno/programs/commafree-eastman.w`
2. Recognize that the programming language used is C. Hence we need to use `cweave` and `ctangle`; not the original `weave` and `tangle` designed for Pascal.
All those programs come bundled with TeX distributions like TeXLive. Once you have them installed and your system can find executables like `pdflatex`, it will also find `cweave`, etc.
3. First, we generate the documentation.
1. We run

`cweave commafree-eastman.w`

On my system this gives the output

```This is CWEAVE, Version 3.64 (TeX Live 2015/dev/Debian)
*1*3*8
Writing the output file...*1*3*8
Writing the index...
Done.
(No errors were found.)```
2. Three files are generated, namely `commafree-eastman.idx`, `commafree-eastman.scn` and `commafree-eastman.tex`.
3. As a small remark, the TeX file uses the `cwebmac` definitions. So it is a file full of definitions and this file is also provided with your TeX distribution:
```% head -n 1 commafree-eastman.tex
\input cwebmac
% locate cwebmac
/usr/share/texlive/texmf-dist/tex/plain/cweb/cwebmac.tex
/usr/share/texlive/texmf-dist/tex/plain/cweb/pdfXcwebmac.tex
/usr/share/texlive/texmf-dist/tex/plain/cweb/pdfcwebmac.tex
/usr/share/texlive/texmf-dist/tex/plain/cweb/pdfdcwebmac.tex
/usr/share/texlive/texmf-dist/tex/plain/cweb/pdffcwebmac.tex
/usr/share/texlive/texmf-dist/tex/plain/cweb/pdficwebmac.tex```
4. Then we run the original tex program to build a dvi file.

`tex commafree-eastman.tex`

This gives me the output:

```This is TeX, Version 3.14159265 (TeX Live 2015/dev/Debian) (preloaded format=tex)
(./commafree-eastman.tex
(/usr/share/texlive/texmf-dist/tex/plain/cweb/cwebmac.tex) *1 [1] *3 [2]
[3] *8 Index: (./commafree-eastman.idx) [4] Section names:
[0] )
Output written on commafree-eastman.dvi (6 pages, 16116 bytes).
Transcript written on commafree-eastman.log.```
5. Followingly, a file `commafree-eastman.dvi` is written as the output states. Opening it directly with evince 3.14.2 shows the output with some errors (the typical font encoding problems of TeX like < in #include statements is replaced by ¡). With okular 0.21.3 I can view the document without any problems.
6. We convert the DVI file using the utility `dvipdfm` provided with your TeX distribution:
`dvipdfm commafree-eastman.dvi`

For me, this gives:

```commafree-eastman.dvi -> commafree-eastman.pdf
[1][2][3][4][5][6]
45807 bytes written```

Then we have a PDF output file `commafree-eastman.pdf` containing the documentation.
I think this is the usual way to view documents, you were looking for.

4. We generate the source code:

1. First we run `ctangle`:
`ctangle commafree-eastman.w`

which gives me:

```This is CTANGLE, Version 3.64 (TeX Live 2015/dev/Debian)
*1*3*8
Writing the output file (commafree-eastman.c):
Done.
(No errors were found.)```
2. Nice! So we got a `commafree-eastman.c` file.
3. We compile the C file with a C compile like the GCC:
`gcc -Wall -o commafree-eastman commafree-eastman.c`

This gives me:

```./commafree-eastman.w:38:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main (int argc,char*argv[]) {
^
./commafree-eastman.w: In function ‘main’:
./commafree-eastman.w:42:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^```
4. We run this new executable program:
```./commafree-eastman
Usage: ./commafree-eastman x1 x2 ... xn```

This gives the same output as in the video.

So we got a `commafree-eastman.pdf` and `commafree-eastman.c` file which was what we were going for.
I will also refer here to my Literate programming talk at PyGraz back then.

## 2 thoughts on “Running Donald Knuth’s CWEB programs”

1. Heinz Prantner says:

thx for the cookbook, very helpful!
I tried to summarize in a Makefile:

```# https://www.youtube.com/watch?v=48iJx8FVuis
# http://lukas-prokop.at/blog/2016/01/running-donald-knuths-cweb-programs/

PI := 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9

commafree-eastman.w:
wget http://www-cs-faculty.stanford.edu/~uno/programs/commafree-eastman.w

commafree-eastman.tex: commafree-eastman.w
cweave commafree-eastman.w

commafree-eastman.dvi: commafree-eastman.tex
tex commafree-eastman.tex

commafree-eastman.pdf: commafree-eastman.dvi
dvipdfm commafree-eastman.dvi

commafree-eastman.c: commafree-eastman.w
ctangle commafree-eastman.w

commafree-eastman: commafree-eastman.c
gcc -Wall -o commafree-eastman commafree-eastman.c

.PHONY : run
run : commafree-eastman
./commafree-eastman \$(PI)

.PHONY : all
all: commafree-eastman.pdf  run

.PHONY : clean
clean:
rm -f commafree-eastman
rm -f commafree-eastman.c
rm -f commafree-eastman.pdf
rm -f commafree-eastman.log
rm -f commafree-eastman.toc
rm -f commafree-eastman.dvi
rm -f commafree-eastman.tex
rm -f commafree-eastman.idx
rm -f commafree-eastman.scn
rm -f commafree-eastman.w
```