{"id":2529,"date":"2013-02-02T16:00:57","date_gmt":"2013-02-02T16:00:57","guid":{"rendered":"http:\/\/www.readytext.co.uk\/?p=2529"},"modified":"2013-02-13T17:05:53","modified_gmt":"2013-02-13T17:05:53","slug":"porting-and-building-web2c-exe-for-windows","status":"publish","type":"post","link":"https:\/\/www.readytext.co.uk\/?p=2529","title":{"rendered":"Porting and building Web2C.exe for Windows"},"content":{"rendered":"<h1>Introduction<\/h1>\n<p>This post is, once again, an aide-m\u00e9moire to record a work-in-progress: porting the tools that convert Knuth&#8217;s original Pascal-based WEB source to C &ndash; to create a native build of <code>Web2C.exe<\/code>, <code>fixwrites.exe<\/code> and other tools using Microsoft&#8217;s Visual Studio (and not using <a href=\"http:\/\/www.linfo.org\/pipe.html\">pipes<\/a>). My apologies if this post is a little unstructured but the whole task is somewhat convoluted, which may be reflected in my writing style for this post! However, I&#8217;d like to record it whilst it is fresh in my memory.<\/p>\n<p>Why would anyone want to do this when there are ready-made, reliable, TeX distributions freely available? Good question. Well, for me, it&#8217;s nothing more than pure curiosity &ndash; and the fact that most British TV programs are now such mind-numbing drivel that I might as well do something productive in the evenings!<\/p>\n<blockquote><p><strong>Join TUG<\/strong>: Just as an aside, I&#8217;m a member of the TeX User Group, TUG, so if you too would like to support TeX why not <a href=\"http:\/\/tug.org\/join.html\">consider joining<\/a>?<\/p><\/blockquote>\n<p>Another reason for writing this post is that I could not find much documentation on <em>how<\/em> to build <code>Web2C.exe<\/code> from source code &ndash; apart from these <a href=\"https:\/\/www.readytext.co.uk\/files\/ctowebstuff.pdf\"> notes by Timothy Murphy<\/a>, detailing the process for Macintosh-based port. Even though they were written in 1992 they were extremely helpful in filling in some of the details, so a belated thank you to Timothy Murphy &ndash; much of this post draws inspiration from that document. Piecing together the Web2c build process has been somewhat of a &#8220;programming jigsaw&#8221;  &ndash; there are still gaps in my understanding but, I think, I can see the big picture even if it&#8217;s still a little hazy in some areas.\n<\/p>\n<h1>The Big Picture<\/h1>\n<p>The source files for TeX, and other TeX-related programs and utilities, are written using Professor Donald Knuth&#8217;s <a href=\"http:\/\/en.wikipedia.org\/wiki\/Literate_programming\">literate programming<\/a> methodology. In essence, the program code (in Pascal) and documentation of the source code (in TeX) are contained within a single file, with extension <code>.web<\/code>. For example, Professor Knuth&#8217;s source code of the latest version of TeX is contained in a file called <code>tex.web<\/code>. Similarly, within the TeXLive repository (<a href=\"https:\/\/www.readytext.co.uk\/?p=365\">see a previous post<\/a>) or on CTAN, you can find the WEB source code for the latest versions of other programs; for example:  <\/p>\n<ul>\n<li><strong>bibtex.web<\/strong>: the source code\/documentation of <code>BiBTeX<\/code>, for formatting and producing reference lists, as widely used within academic journal papers.<\/li>\n<li><strong>mf.web<\/strong>: the source code\/documentation of MetaFont.<\/li>\n<li><strong>patgen.web<\/strong>: the source code\/documentation of <code>patgen<\/code> which &#8220;&#8230; takes list of hyphenated words and generates a set of patterns that can be used by the TeX 82 hyphenation algorithm.&#8221; <\/li>\n<li><strong>tangle.web<\/strong>: the source code\/documentation of <code>tangle<\/code>, which converts a WEB file to a Pascal (i.e., extracts  the source code in Pascal, not in C &ndash; that&#8217;s why Web2C exists).<\/li>\n<li><strong>weave.web<\/strong>: the source code\/documentation of <code>weave<\/code>, which converts a WEB file to TeX (i.e., extracts the documentation of the program&#8217;s Pascal source code).<\/li>\n<\/ul>\n<p>and other programs\/utilities such as <code>dvicopy.web<\/code>, <code>pltotf.web<\/code>, <code>tftopl.web<\/code> and so forth.<\/p>\n<blockquote><p><strong>What&#8217;s in a name: tangle, web and weave?<\/strong> I&#8217;ve not researched to find out, but I cannot help thinking that Professor Knuth drew inspiration from Sir Walter Scott when naming these programs. Scott&#8217;s poem <a href=\"http:\/\/en.wikipedia.org\/wiki\/Marmion_%28poem%29\">Marmion<\/a> contains the line(s) &#8220;O, what a tangled web we weave when we practice to decieve&#8221;. Maybe these programs are as literary as they are literate?<\/p><\/blockquote>\n<h1>TeXLive as the source of the files for building <code>Web2C.exe<\/code><\/h1>\n<p>The files I reference throughout this post can be downloaded via SVN from the TeXLive repository. If you want to browse the TeXLive repository, using the TortoiseSVN program on Windows, <a href=\"https:\/\/www.readytext.co.uk\/?p=365\">this post<\/a> may be of help. The following screenshots show the TeXLive folders you&#8217;ll need to access for acquiring the various files I mention in this post.<\/p>\n<ul>\n<li><strong>svn:\/\/tug.org\/texlive\/trunk\/Build\/source\/texk\/web2c<\/strong>: this folder contains, for example, <code>tangleboot.pin<\/code> (see below) and all the <code>*.web<\/code> files listed above, plus many other essential files.\n<p><img decoding=\"async\" src=\"http:\/\/readytext.co.uk\/files\/texlive1.png\" width=\"100%\"\/><\/p>\n<\/li>\n<li><strong>svn:\/\/tug.org\/texlive\/trunk\/Build\/source\/texk\/web2c\/web2c<\/strong>: this folder contains the source files needed to build the actual <code>Web2C.exe<\/code> program. Note carefully it <strong>does not<\/strong> contain a file called <code> Web2C.c<\/code>, more on that below.\n<p><img decoding=\"async\" src=\"http:\/\/readytext.co.uk\/files\/texlive2.png\" width=\"100%\"\/><\/p>\n<\/li>\n<\/ul>\n<p>TeXLive has an advanced build-process for compiling\/building all the tools and software it contains and I, for one, am in awe of the skills and expertise of its maintainers. In describing my explorations of building <code>Web2C.exe<\/code> as a Windows-based executable you need to realize that I am taking the source code files of <code>Web2C.exe<\/code> out of their &#8220;natural build environment&#8221;. What do I mean by this? Building the Web2C executable program is usually part of the much bigger TeXLive build\/compilation process so you should be prepared for a little extra complexity to create <code>Web2C.exe<\/code> as a &#8220;standalone&#8221; Windows program. Note that &#8220;standalone&#8221; is in quotes because converting WEB-generated Pascal into C code requires other tools in addition to <code>Web2C.exe<\/code>: it is not fully accomplished by <code>Web2C.exe<\/code> alone.<\/p>\n<h1>A note about Kpathsea<\/h1>\n<p>The <a href=\"http:\/\/tug.org\/kpathsea\/\">Kpathsea (path-searching) C library<\/a> in an integral part of most TeX-related software and the Web2C C source files <code>#include<\/code> a number of Kpathsea headers. However, for my own purposes\/experiments I&#8217;ve decided to decouple my build of the <code>Web2C.exe<\/code> executable from the need to include Kpathsea&#8217;s headers &ndash; the resulting C files generated by <code>Web2C.exe<\/code> will, of course, still depend on Kpathsea. If you grab the Web2C source files (see below) then &#8220;out of the box&#8221; you&#8217;ll need to checkout the Kpathsea library from:<\/p>\n<p>svn:\/\/tug.org\/texlive\/trunk\/Build\/source\/texk\/kpathsea<\/p>\n<p>I&#8217;ve simply not got the time to document everything I had to do to decouple Kpathsea when building <code>Web2C.exe<\/code>. It mainly involved commenting out various <code>#include<\/code> lines that pulled in Kpathsea headers and placing a few <code>#define<\/code> statements into my local version of <code>web2c.h<\/code> &ndash; plus creating some typedefs and adding a few macros. If you&#8217;re an experienced C programmer it is unlikely to present difficulties. As mentioned, this post describes a work-in-progress to satisfy my own curiosity and is meant to share a few of the things I&#8217;ve learnt, should they be useful to anyone as a starting point for their own work.<\/p>\n<h1>Web2C: so <em>what is it<\/em>?<\/h1>\n<p>Let me be clear that when I refer to Web2C I am referring to the executable program which undertakes the first (main) step in converting Pascal code into to C. So let&#8217;s now start to take a look at the details but start with a summary of &#8220;Where are we?&#8221;<\/p>\n<h2>Where are we?<\/h2>\n<p>The starting point for generating C code is to extract the Pascal code from WEB source files and that is accomplished using the <code>tangle<\/code> program. However, where do we get a working <code>tangle<\/code> program from to start with &ndash; do we have a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chicken_or_the_egg\">chicken and egg<\/a> problem?<\/strong> <code>tangle<\/code> is itself distributed in WEB source code (<code>tangle.web<\/code>), so if I need <code>tangle<\/code> to extract tangle&#8217;s source code from <code>tangle.web<\/code>, how do I create a working tangle program? Well, of course, this is solved by the distribution of tangle&#8217;s Pascal code in a file called <code>tangleboot.pin<\/code> within the Web2C directory of the TeXLive repository (see above). In essence, <code>tangleboot.pin<\/code> let&#8217;s you &#8220;bootstrap&#8221; the whole Web2C process by creating a working <code>tangle.exe<\/code> which you can use to generate the Pascal from WEB source files. Hence the name tangle<strong>boot<\/strong>.pin<\/p><\/blockquote>\n<p>So, how do I go from <code>tangleboot.pin<\/code> to a working tangle.exe? You need to build <code>Web2C.exe<\/code> and some associated utility programs (e.g., <code>fixwrites.exe<\/code>).<\/p>\n<h2>Where are the <code>Web2C.exe<\/code> source files?<\/h2>\n<p>As mentioned above, the TeXLive folder containing the source files needed to build <code>Web2C.exe<\/code> is<\/p>\n<ul>\n<li>svn:\/\/tug.org\/texlive\/trunk\/Build\/source\/texk\/web2c\/web2c<\/li>\n<\/ul>\n<p>The C source files you need to compile\/build <code>Web2C.exe<\/code> are:<\/p>\n<ul>\n<li>kps.c<\/li>\n<li>main.c<\/li>\n<li>web2c-lexer.c<\/li>\n<li>web2c-parser.c<\/li>\n<\/ul>\n<h2>Some notes on these files<\/h2>\n<p>These C files <code>#include<\/code> a number of header files from the TeXLive distribution, notably from the Kpathsea library, so you should definitely look through them to determine any additional files you need.<\/p>\n<p>The files <code>web2c-parser.c<\/code> and <code>web2c-lexer.c<\/code> are worthy of some explanation because they are the core files which drive the Pascal &#8211;> C conversion process. However, these two C source files are not hand-coded but are <em>generated<\/em> from two further source files with similar names. If you look among the source files you will also notice these two additional files:<\/p>\n<ul>\n<li><code>web2c-lexer.l<\/code><\/li>\n<li><code>web2c-parser.y<\/code><\/li>\n<\/ul>\n<p>What are these files with similar names? As you may infer from their names, these files are a lexical analyser and a parser generator and require additional tools to process them:<\/p>\n<ul>\n<li><code>web2c-lexer.l --> web2c-lexer.c <\/code> using a tool called <a href=\"http:\/\/flex.sourceforge.net\/\">flex<\/a>.<\/li>\n<li><code>web2c-parser.y --> web2c-parser.c + web2c-parser.h<\/code> using a tool called <a href=\"http:\/\/www.gnu.org\/software\/bison\/\">bison<\/a>.<\/li>\n<\/ul>\n<h3>Are bison\/flex available for Windows?<\/h3>\n<p>Fortunately they are and, at the time of writing (February 2013), you can download Windows ports of <code>bison 2.7<\/code> and <code>flex 2.5.37<\/code> from <a href=\"http:\/\/sourceforge.net\/projects\/winflexbison\/ \">http:\/\/sourceforge.net\/projects\/winflexbison\/<\/a>. The executables are called <code>win_bison.exe<\/code> and <code>win_flex.exe<\/code> respectively. The <code>win_flex.exe<\/code> port of flex adds an extra command-line switch (<code>--wincompat<\/code>) so that the C code it generates uses the standard Windows header <code>io.h<\/code> instead of <code>unistd.h<\/code> (which is used on Linux). You can also download older versions of bison and flex for Windows from the <a href=\"http:\/\/gnuwin32.sourceforge.net\/packages.html\">GnuWin32 project<\/a>.<\/p>\n<p>I have not yet tried to use the code generated by <code>win_flex.exe<\/code> and <code>win_bison.exe<\/code> but to the best of my (current) knowledge the command-line options you need are:<\/p>\n<ul>\n<li><code>win_bison -y -d web2c-parser.y<\/code> to generate the parser (you&#8217;ll get different file names on output: <code>y.tab.c<\/code> and <code>y.tab.h<\/code>)<\/li>\n<li><code>win_flex --wincompat web2c-lexer.l<\/code> to generate the lexical analyser (you&#8217;ll get a different file name on output: <code>lex.yy.c<\/code>)<\/li>\n<\/ul>\n<h1>You need more than just <code>Web2c.exe<\/code>&#8230;<\/h1>\n<p>Assuming that you successfully build <code>Web2c.exe<\/code>, it is still not the end of the story. Although <code>Web2c.exe<\/code> does the <em>bulk<\/em> of the work in converting the Pascal to C, some initial pre-processing of the Pascal source file is needed before you can run it through <code>Web2C.exe<\/code>, and some further post-processing of the C code output by <code>Web2C.exe<\/code> is also needed. The details of how these pre- and post-processing steps actually work are contained within an important BASH shell script called <code>convert<\/code> (it has no extension) &ndash; <code>convert<\/code> is located within the TeXLive folder containing the Web2C source files. I readily confess that I know very little about Linux shell scripting so if you are well-versed in shell scripts no doubt you can easily understand what is going on in the <code>convert<\/code> file. However, here are pointers to get you started.<\/p>\n<h3>Pre-processing: adding the <code>*.defines<\/code> files to the Pascal file<\/h3>\n<p>Before you can actually run <code>Web2C.exe<\/code> on the Pascal file generated from WEB sources you need to concatenate the Pascal source file with some files having the extension &#8220;<code>.defines<\/code>&#8220;: you add these files to the <strong>start<\/strong> of the Pascal file before running <code>Web2C.exe<\/code>. There are several <code>.defines<\/code> contained in the Web2C source directory including:<\/p>\n<ul>\n<li><code>common.defines<\/code><\/li>\n<li><code>mfmp.defines<\/code><\/li>\n<li><code>texmf.defines<\/code><\/li>\n<\/ul>\n<p>The <code>convert<\/code> script checks which program, and its options, (TeX, MetaFont, BiBTeX etc) is being built and concatenates the appropriate <code>*.defines<\/code> file(s) to the start of the corresponding Pascal file. At this time, I don&#8217;t quite fully understand how\/why these files are needed, but for the full details you need to read <code>convert<\/code>. By way of an example, when processing <code>tangleboot.pin<\/code> I added the file <code>common.defines<\/code> to the beginning of <code>tangleboot.pin<\/code>.  <\/p>\n<h3>Post-processing: <code>fixwrites.exe<\/code><\/h3>\n<p><code>Web2C.exe<\/code>&#8216;s output is not quite pure C source code &ndash; it may still contain some fragments of Pascal which need a specialist post-processing step to fully convert them to C: enter <code>fixwrites.exe<\/code>. <code>fixwrites.exe<\/code> post-processes <code>Web2C.exe<\/code>&#8216;s C output to &#8220;&#8230;convert Pascal write\/writeln&#8217;s into fprintf&#8217;s or putc&#8217;s&#8221; (see <code>fixwrites.c<\/code>). <\/p>\n<h3>Notes on <code>web2c-parser.c<\/code>, <code>web2c-lexer.c<\/code> and <\/p>\n<li>main.c<\/li>\n<\/h3>\n<p>Upon reading the <code>convert<\/code> script, and when I first ran <code>Web2C.exe<\/code>, it became readily apparent that the whole Pascal &#8211;> C tool chain (driven by <code>convert<\/code>) communicates using <a href=\"http:\/\/www.linfo.org\/pipe.html\">pipes<\/a>) with stdout\/stderr. The output of one program is &#8220;piped&#8221; into the input to another, rather than writing the data out to a physical disc file and then reading it back in. My personal preference, certainly whilst learning, is to output data to a file so that I can capture what&#8217;s going on. <\/p>\n<h4><code>main.c<\/code> and <code>yyin<\/code><\/h4>\n<p>Without going into too much detail, I needed to make a number of changes in <code>main.c<\/code> so that the lexical analyzer <code>web2c-lexer.c<\/code> was set to read it&#8217;s data from a disc file rather than through pipes\/stdin. The <code>FILE*<\/code> variable you need to set\/define is called <code>yyin<\/code>. For example, within <code>main.c<\/code> there is a function called <code>initialize ()<\/code> which can be used to set <code>yyin<\/code>. For example:<\/p>\n<p><code>void initialize (void)<br \/>\n{<br \/>\n  register int i;<br \/>\n  for (i = 0; i < hash_prime; hash_list[i++] = -1)\n    ;\n\n yyin = xfopen(\"your_path_to\\\\tangleboot.p\",\"r\");\n ...\n ...\n}<\/code><\/p>\n<p>In addition, within <code>main.c<\/code> there's a small function called <code>normal ()<\/code> which does the following:<\/op><\/p>\n<p><code><br \/>\nvoid normal (void)<br \/>\n{<br \/>\n  out = stdout;<br \/>\n}<\/code><\/p>\n<p>The <code>normal ()<\/code> function is called from within <code>web2c-parser.c<\/code> to set the output file (<code>FILE *out<\/code>) to stdout. At present, I'm not sure precisely why this is done, but I guess it is part of the piping between programs as driven by the <code>convert<\/code> process. For example, code within <code>convert<\/code> uses <a href=\"http:\/\/www.gnu.org\/software\/sed\/manual\/sed.html\">sed<\/a> (the stream editor).  <\/p>\n<p>Other output redirections happen in <code>web2c-parser.c<\/code> and you can search for these by looking for <code>out = 0<\/code>. Tracking down and locating these output redirections certainly helped me to better understand the flow of the programs.<\/p>\n<h1>In conclusion<\/h1>\n<p>This post is a little disjointed in places and light on detail in a number of areas, reflecting my own (currently) incomplete understanding of the relatively complex processes involved in converting WEB\/Pascal to C. Nevertheless, I hope that it is of some use to someone, at some point. As my understanding develops I'll try to fill in the gaps with future posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This post is, once again, an aide-m\u00e9moire to record a work-in-progress: porting the tools that convert Knuth&#8217;s original Pascal-based WEB source to C &ndash; to create a native build of Web2C.exe, fixwrites.exe and other tools using Microsoft&#8217;s Visual Studio (and not using pipes). My apologies if this post is a little unstructured but the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,27],"tags":[],"class_list":["post-2529","post","type-post","status-publish","format-standard","hentry","category-c-programming-miscellaneous","category-web2c-tangle-weave-web"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2529"}],"version-history":[{"count":82,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2529\/revisions"}],"predecessor-version":[{"id":2669,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2529\/revisions\/2669"}],"wp:attachment":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}