Listing 2: Filter program converted to HTML

<pre><b>
</b><i>/**** C/C++ to HTML Converter Filter ******/</i><b>

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

</b><i>/* Output chars and convert various
   HTML-specific characters */</i><b>
void outchar (char ch)
{ switch (ch) {
    case '&lt;'  : printf (&quot;&amp;lt;&quot;); break;
    case '&gt;'  : printf (&quot;&amp;gt;&quot;); break;
    case '&amp;'  : printf (&quot;&amp;amp;&quot;); break;
    case '\&quot;' : printf (&quot;&amp;quot;&quot;); break;
    default   : putchar (ch);
    }
} </b><i>/* outchar */</i><b>

void main (void)
{ char line[255+1];
  int i, </b><i>/* counter for each char in line */</i><b>
      inEOLcomment=0, </b><i>/* flag to indicate in C++
                         &quot;to EOL&quot; comment */</i><b>
      incomment=0, </b><i>/* flag to indicate if
                      currently in comment */</i><b>
      inquote=0; </b><i>/* flag to indicate if currently
                    in quoted text */</i><b>

  </b><i>/* start HTML output; indicate text is
     preformatted and will begin in bold */</i><b>
  printf (&quot;&lt;pre&gt;&lt;b&gt;&quot;);

  </b><i>/* process each line from stdin */</i><b>
  while (gets(line) != NULL) {

    i = inEOLcomment = 0;
    </b><i>/* look at each char on line */</i><b>
    while (line[i]) {
      if (!inEOLcomment) {
        if (!incomment &amp;&amp;
             (line[i] == '\'' || line[i] == '\&quot;'))
          if (line[i-1] != '\\' ||
             (line[i-1] == '\\' &amp;&amp; line[i-2] == '\\'))
            inquote = !inquote; </b><i>/* toggle quote status */</i><b>
        if (!inquote)
      if (!incomment &amp;&amp; strncmp(line+i,&quot;//&quot;,2) == 0) {
            </b><i>/* beginning of C++ line comment */</i><b>
            inEOLcomment = 1;
            printf (&quot;&lt;/b&gt;&lt;i&gt;//&quot;);  i += 2;
            }
          else if (strncmp(line+i,&quot;/*&quot;,2) == 0) {
            </b><i>/* beginning of comment.. */</i><b>
            if (!incomment) printf (&quot;&lt;/b&gt;&lt;i&gt;&quot;);
            incomment++;
            }
          else if (incomment &amp;&amp; strncmp(line+i,&quot;*/&quot;,2) == 0) {
            </b><i>/* end of comment... */</i><b>
            printf (&quot;*/&quot;);
            incomment--;  i += 2;
            if (!incomment) printf (&quot;&lt;/i&gt;&lt;b&gt;&quot;);
            continue;
            }
        }

      outchar (line[i++]);

      } </b><i>/* end of line */</i><b>
    if (inEOLcomment) printf (&quot;&lt;/i&gt;&lt;b&gt;&quot;);
    putchar ('\n');
    } </b><i>/* end of input */</i><b>

  </b><i>/* finish up HTML output */</i><b>
  printf (&quot;&lt;/b&gt;&lt;/pre&gt;\n&quot;);
}

</b></pre>