home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 32 / hot34.iso / ficheros / DTOOL / CPP2HTML.ZIP / CPP2HTML / CPP2LATE.L < prev    next >
Text File  |  1998-04-04  |  14KB  |  612 lines

  1. /*
  2.  *  This is a flex input file but should be edited in -*-C-*- mode
  3.  *
  4.  *  C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  5.  *  Copyright (C) 1990 Norbert Kiesel
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 1, or (at your option)
  10.  *  any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  Norbert Kiesel     
  22.  *  RWTH Aachen / Institut f. Informatik III
  23.  *  Ahornstr. 55
  24.  *  D-5100 Aachen
  25.  *  West Germany  
  26.  *
  27.  *  Phone:  +49 241 80-7266
  28.  *  EUNET:  norbert@rwthi3.uucp
  29.  *  USENET: ...!mcvax!unido!rwthi3!norbert
  30.  *  X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  31.  *
  32.  *  Please contact me for any bugs you find in this code or any
  33.  *  improvements! I'd also be very happy to get feedback where and
  34.  *  how frequently this program is used (just drop a little mail :-).
  35.  */
  36.  
  37. %x STRING BCOMMENT INCLUDE
  38. %s CPLUSPLUS
  39.  
  40. %{
  41. #define KEY    printf ("{\\%s %s}", keyword_font, yytext)
  42. #define CPP    printf ("{\\%s \\%s}", cpp_font, yytext)
  43. #define SYM(x)    printf ("$\\%s$", x)
  44. #define OUT(x)    printf ("%s", x)
  45. #define SUB(x)    substitute(x)
  46. #define IND    indent(yytext)
  47. #define INIT    BEGIN (cplusplus_mode ? CPLUSPLUS : INITIAL);
  48.  
  49. #include <stdio.h>
  50.  
  51. #ifdef ANSI_C
  52.   #ifdef C_PLUSPLUS
  53.     #error ANSI_C and C_PLUSPLUS are mutually exclusive
  54.   #else
  55.     int cplusplus_mode = 0;
  56.   #endif
  57. #else /* CPLUSPLUS or default */
  58.   int cplusplus_mode = 1;
  59. #endif
  60.  
  61. int complete_file = 0;
  62. int header = 0;
  63. int tabtotab = 8;
  64. int piped = 0;
  65. int want_index = 0;
  66. char * font_size = "11";
  67. char * indentation = "0.5em";
  68. char * comment_font = "it";
  69. char * keyword_font = "bf";
  70. char * header_font = "sl";
  71. char * cpp_font = "tt";
  72. char * string_font = "tt";
  73. void substitute(const char *);
  74. void indent(const char *);
  75. void newpage(int);
  76. void usage(const char *);
  77. %}
  78.  
  79. %%
  80.  
  81.     INIT;
  82.  
  83. "#"[ \t]*"include"    { CPP; BEGIN (INCLUDE); }
  84. "#"[ \t]*"define"    |
  85. "#"[ \t]*"undef"    |
  86. "#"[ \t]*"pragma"    |
  87. "#"[ \t]*"if"        |
  88. "#"[ \t]*"ifdef"    |
  89. "#"[ \t]*"ifndef"       |
  90. "#"[ \t]*"elif"        |
  91. "#"[ \t]*"else"        |
  92. "#"[ \t]*"error"    |
  93. "#"[ \t]*"endif"    |
  94. "#"[ \t]*"line"        CPP;
  95.  
  96.  
  97. <INCLUDE>"<"[^>]*/">"    { OUT ("$<${\\"); OUT (string_font);
  98.               OUT ("{}"); SUB (yytext+1); OUT ("}$>$");
  99.               if (want_index) {
  100.                             OUT ("\\index{"); SUB(yytext+1); OUT ("}"); }
  101.               input(); INIT; }
  102. <INCLUDE>\"[^\"]*/\"    { OUT ("\"{\\"); OUT (string_font);
  103.               OUT ("{}"); SUB (yytext+1); OUT ("}\"");
  104.               if (want_index) {
  105.                             OUT ("\\index{"); SUB(yytext+1); OUT ("}"); }
  106.               input(); INIT; }
  107. <INCLUDE>[ \t]+        ECHO;
  108. <INCLUDE>[\n]        OUT ("\\mbox{}\\\\\n");
  109. <INCLUDE>.        { yyless (0); INIT; }
  110.  
  111. "auto"            |
  112. "double"        |
  113. "int"            |
  114. "struct"        |
  115. "break"            |
  116. "else"            |
  117. "long"            |
  118. "switch"        |
  119. "case"            |
  120. "enum"            |
  121. "register"        |
  122. "typedef"        |
  123. "char"            |
  124. "extern"        |
  125. "return"        |
  126. "union"            |
  127. "const"            |
  128. "float"            |
  129. "short"            |
  130. "unsigned"        |
  131. "continue"        |
  132. "for"            |
  133. "signed"        |
  134. "void"            |
  135. "default"        |
  136. "goto"            |
  137. "sizeof"        |
  138. "volatile"        |
  139. "do"            |
  140. "if"            |
  141. "static"        |
  142. "while"            |
  143. <CPLUSPLUS>"new"    |
  144. <CPLUSPLUS>"delete"    |
  145. <CPLUSPLUS>"this"    |
  146. <CPLUSPLUS>"operator"    |
  147. <CPLUSPLUS>"class"    |
  148. <CPLUSPLUS>"public"    |
  149. <CPLUSPLUS>"protected"    |
  150. <CPLUSPLUS>"private"    |
  151. <CPLUSPLUS>"virtual"    |
  152. <CPLUSPLUS>"friend"    |
  153. <CPLUSPLUS>"inline"    |
  154. <CPLUSPLUS>"overload"    KEY;
  155. "->"            SYM ("rightarrow");
  156. "<<"            SYM ("ll");
  157. ">>"            SYM ("gg");
  158. "<="            SYM ("leq");
  159. ">="            SYM ("geq");
  160. "!="            SYM ("neq");
  161. "||"            SYM ("mid\\mid");
  162. "..."            SYM ("ldots");
  163. "*="            SYM ("ast=");
  164. "<<="            SYM ("ll=");
  165. ">>="            SYM ("gg=");
  166. "^="            SYM ("vee=");
  167. "|="            SYM ("mid=");
  168. "~"            SYM ("sim");
  169. "*"            SYM ("ast");
  170. "^"            SYM ("wedge");
  171. "|"            SYM ("mid");
  172. <CPLUSPLUS>"->*"    SYM ("rightarrow\\ast");
  173. "/"            OUT ("$/$");
  174. "<"            OUT ("$<$");
  175. ">"            OUT ("$>$");
  176. "&&"            OUT ("\\&\\&");
  177. "%="            OUT ("\\%=");
  178. "&="            OUT ("\\&=");
  179. "{"            OUT ("\\{");
  180. "}"            OUT ("\\}");
  181. "&"            OUT ("\\&");
  182. "%"            OUT ("\\%");
  183. "--"            OUT ("-{}-");
  184. <CPLUSPLUS>".*"        OUT (".$\\ast$");
  185. "?"            |
  186. ":"            |
  187. "="            |
  188. ","            |
  189. "."            |
  190. ";"            |
  191. "!"            |
  192. "-"            |
  193. "+"            |
  194. "/="            |
  195. "=="            |
  196. "++"            |
  197. "+="            |
  198. "-="            |
  199. "("            |
  200. ")"            |
  201. "["            |
  202. "]"            |
  203. <CPLUSPLUS>"::"        ECHO;
  204.  
  205. [a-zA-Z_$][a-zA-Z_$0-9]*    |
  206. [a-zA-Z_][a-zA-Z_0-9]*            { SUB (yytext);
  207.                       if (want_index) {
  208.                         OUT ("\\index{"); SUB(yytext);
  209.                         OUT("}"); } }
  210.  
  211. "/*"                { BEGIN (BCOMMENT); OUT ("{$/\\ast$\\");
  212.                   OUT (comment_font); OUT ("{}"); }
  213. <BCOMMENT>"*/"            { INIT; OUT ("$\\ast/$}"); }
  214. <BCOMMENT>"\n"            OUT ("\\mbox{}\\\\\n");
  215. <BCOMMENT>^[ \t]+        IND;
  216. <BCOMMENT>.            SUB (yytext);
  217.  
  218. <CPLUSPLUS>"//".*$        { OUT ("{$//$\\"); 
  219.                   OUT (comment_font); OUT ("{}");
  220.                       SUB (yytext + 2);
  221.                   OUT ("}\\mbox{}\\\\\n"); }
  222.  
  223. L?\"                 { BEGIN (STRING); OUT ("{\\");
  224.                   OUT (string_font); OUT ("\""); }
  225. <STRING>\\\\            OUT ("$\\backslash\\backslash$");
  226. <STRING>\\[bfnrtv'"]        { OUT ("$\\backslash$"); SUB (yytext+1); }
  227. <STRING>\"            { INIT; OUT ("\"}"); }
  228. <STRING>"\n"            OUT ("\\mbox{}\\\\\n");
  229. <STRING>^[ \t]+            IND;
  230. <STRING>.            SUB (yytext);
  231.  
  232. ([0-9]*\.[0-9]+[fFlL]?)         |
  233. ([0-9]+\.[0-9]*[fFlL]?)         |
  234. ([0-9]*\.?[0-9]+[eE][+-]?[0-9]+) |
  235. ([0-9]+\.?[0-9]*[eE][+-]?[0-9]+) ECHO;
  236.  
  237. [0-9]+[uUlL]?            ECHO;
  238.  
  239. L?'[ -~]'            |
  240. L?'\\[ntvbrfa\\?'"]'        |
  241. L?'\\[0-7]{1,3}'        |
  242. L?'\\x[0-9a-fA-F]{1,2}'        SUB (yytext);
  243.  
  244. 0[0-7]+[uUlL]?            ECHO;
  245.  
  246. 0x[0-9a-fA-F]+[uUlL]?        ECHO;
  247.  
  248. "\\\n"                OUT ("$\\backslash$\\\\\n");
  249. ^[ \t]+                IND;
  250. [ \t]+                ECHO;
  251. "\f"[\n]?            OUT ("\\newpage\n");
  252. "\n"                OUT ("\\mbox{}\\\\\n");
  253.  
  254. %%
  255.  
  256. void substitute (const char * input)
  257. {
  258.   while (*input)
  259.     {
  260.       switch (*input)
  261.     {
  262.     case '_':
  263.     case '&':
  264.     case '#':
  265.     case '$':
  266.     case '%':
  267.     case '{':
  268.     case '}':
  269.       printf ("\\%c", *input);
  270.       break;
  271.     case '+':
  272.     case '=':
  273.     case '<':
  274.     case '>':
  275.       printf ("$%c$", *input);
  276.       break;
  277.     case '*':
  278.       printf ("$\\ast$");
  279.       break;
  280.     case '|':
  281.       printf ("$\\mid$");
  282.       break;
  283.     case '\\':
  284.       printf ("$\\backslash$");
  285.       break;
  286.     case '^':
  287.       printf ("$\\wedge$");
  288.       break;
  289.     case '~':
  290.       printf ("$\\sim$");
  291.       break;
  292.     default:
  293.       printf ("%c", *input);
  294.       break;
  295.     }
  296.       input++;
  297.     }
  298. }
  299.  
  300. void
  301. indent(const char * blanks)
  302. {
  303.   int i;
  304.   
  305.   i = 0;
  306.   while (*blanks)
  307.     {
  308.       if (*blanks == ' ')
  309.     {
  310.       i++;
  311.     }
  312.       else            /* *blanks == '\t' */
  313.     {
  314.       while (++i % tabtotab) ;
  315.     }
  316.       blanks++;
  317.     }
  318.   printf ("\\hspace*{%d\\indentation}", i);
  319. }
  320.  
  321. #include "getopt.h"
  322. #include <string.h>
  323. #include <fcntl.h>
  324. #include <ctype.h>
  325. #include <time.h>
  326.  
  327. extern char * version_string;
  328.  
  329. static struct option opts[] =
  330. {
  331.   {"ansi-c", 0, 0, 'a'},
  332.   {"c-plusplus", 0, 0, 'p'},
  333.   {"complete-file", 0, 0, 'c'},
  334.   {"font-size", 1, 0, 's'},
  335.   {"indentation", 1, 0, 'i'},
  336.   {"header", 0, 0, 'h'},
  337.   {"piped", 0, 0, 't'},
  338.   {"output", 1, 0, 'o'},
  339.   {"tabstop", 1, 0, 'T'},
  340.   {"comment-font", 1, 0, 'C'},
  341.   {"string-font", 1, 0, 'S'},
  342.   {"keyword-font", 1, 0, 'K'},
  343.   {"header-font", 1, 0, 'H'},
  344.   {"cpp-font", 1, 0, 'P'},
  345.   {"version", 0, 0, 'V'},
  346.   {"index", 0, 0, 'x'},
  347.   {0, 0, 0, 0}
  348. };
  349.  
  350.  
  351. main (int argc, char** argv)
  352. {
  353.   int c;
  354.   int index;
  355.   int i;
  356.   int has_filename;
  357.   char * input_name;
  358.   char * output_name;
  359.   char * program_name;
  360.   long now;
  361.   char * today;
  362.  
  363.   input_name = "Standard Input";
  364.   output_name = 0;
  365.  
  366.   now = time(0);
  367.   today = ctime(&now);
  368.  
  369.   now = time(0);
  370.   today = ctime(&now);
  371.  
  372.   program_name = strrchr (argv[0], '/');
  373.   if (program_name == NULL)    /* no pathname */
  374.     {
  375.       program_name = argv[0];
  376.     }
  377.   else
  378.     {
  379.       program_name++;
  380.     }
  381.   
  382. #ifdef USE_NAME 
  383. #if defined(ANSI_C) || defined(C_PLUSPLUS)
  384. #error USE_NAME, ANSI_C and C_PLUSPLUS are mutually exclusive
  385. #else
  386.   /* simple heuristic: '+' in name means C++ */
  387.   cplusplus_mode = (strchr (program_name, '+') != 0);
  388. #endif
  389. #endif
  390.   
  391.   while ((c = getopt_long (argc, argv,
  392.                "acpo:s:i:thT:C:H:S:K:P:Vx", opts, &index))
  393.      != EOF)
  394.     {
  395.       if (c == 0)        /* Long option */
  396.     {
  397.       c = opts[index].val;
  398.     }
  399.       switch (c)
  400.     {
  401.     case 'a':
  402.       cplusplus_mode = 0;
  403.       break;
  404.     case 'p':
  405.       cplusplus_mode = 1;
  406.       break;
  407.     case 'c':
  408.       complete_file = 1;
  409.       break;
  410.     case 'o':
  411.       if (piped)
  412.         {
  413.           fprintf (stderr,
  414.                "%s: Can't use {-t,+pipe} and {-o,+output} together\n",
  415.                program_name);
  416.           exit(5);
  417.         }
  418.       output_name = optarg;
  419.       break;
  420.     case 's':
  421.       font_size = optarg;
  422.       break;
  423.     case 'i':
  424.       indentation = optarg;
  425.       break;
  426.     case 'T':
  427.       tabtotab = atoi(optarg);
  428.       break;
  429.         case 't':
  430.           if (output_name != 0)
  431.         {
  432.           fprintf (stderr,
  433.                "%s: Can't use {-t,+pipe} and {-o,+output} together\n",
  434.                program_name);
  435.           exit(5);
  436.         }
  437.       piped = 1;
  438.       break;
  439.     case 'h':
  440.       header = 1;
  441.       complete_file = 1;    /* header implies complete-file */
  442.       break;
  443.     case 'C':
  444.       comment_font = optarg;
  445.       break;
  446.     case 'H':
  447.       header_font = optarg;
  448.       break;
  449.     case 'P':
  450.       cpp_font = optarg;
  451.       break;
  452.     case 'S':
  453.       string_font = optarg;
  454.       break;
  455.     case 'K':
  456.       keyword_font = optarg;
  457.       break;
  458.     case 'V':
  459.       fprintf (stderr, "%s\n", version_string);
  460.       break;
  461.         case 'x':
  462.           want_index = 1;
  463.           break;
  464.     default:
  465.       usage(program_name);
  466.     }
  467.     }
  468.   has_filename = (argc - optind == 1);
  469.   if (has_filename)        /* last argument is input file name */
  470.     {
  471.       input_name = argv[optind];
  472.       if (freopen (input_name, "r", stdin) == NULL)
  473.     {
  474.       fprintf (stderr, "%s: Can't open `%s' for reading\n",
  475.            program_name, input_name);
  476.       exit (2);
  477.     }
  478.     }
  479.   if ((output_name == 0) && !piped)
  480.     {
  481.       char * tmp;
  482.       if (has_filename)
  483.     {
  484.       char * point;
  485.       
  486.       point = strrchr (input_name, '/');
  487.       if (point == 0)    /* plain filename */
  488.         {
  489.           point = input_name;
  490.         }
  491.       else
  492.         {
  493.           point++;
  494.         }
  495.       tmp = malloc (strlen (point) + 1);
  496.       if (tmp == 0)
  497.         {
  498.           fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  499.           exit (3);
  500.         }
  501.       strcpy (tmp, point);
  502.       point = strrchr (tmp, '.');
  503.       if (point != 0)
  504.         {
  505.           *point = '\0';
  506.         }
  507.     }
  508.       else
  509.     {
  510.       tmp = program_name;
  511.     }
  512.       output_name = malloc (strlen (tmp) + 4);
  513.       if (output_name == 0)
  514.     {
  515.       fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  516.       exit (3);
  517.     }
  518.       strcpy (output_name, tmp);
  519.       strcat (output_name, ".tex");
  520.     }
  521.   if (!piped)
  522.     {
  523.       if (freopen (output_name, "w", stdout) == NULL)
  524.     {
  525.       fprintf (stderr, "%s: Can't open `%s' for writing\n",
  526.            program_name, output_name);
  527.       exit (4);
  528.     }
  529.     }
  530.   printf ("\
  531. %%\n\
  532. %% This file was automatically produced at %.24s by\n\
  533. %% %s", today, program_name);
  534.   for (i = 1; i < argc; i++)
  535.     {
  536.       printf(" %s", argv[i]);
  537.     }
  538.   if (!has_filename)
  539.     {
  540.       printf(" (from Standard Input)");
  541.     }
  542.   printf("\n%%\n");
  543.   if (complete_file)
  544.     {
  545.       if (header)
  546.     {
  547.       if (strcmp (font_size, "10") == 0)
  548.         {
  549.           printf ("\\documentstyle[fancyheadings]{article}\n");
  550.         }
  551.       else
  552.         {
  553.           printf ("\\documentstyle[%spt,fancyheadings]{article}\n",
  554.               font_size);
  555.         }
  556.     }
  557.       else
  558.     {
  559.       if (strcmp (font_size, "10") == 0)
  560.         {
  561.           printf ("\\documentstyle{article}\n");
  562.         }
  563.       else
  564.         {
  565.           printf ("\\documentstyle[%spt]{article}\n", font_size);
  566.         }
  567.     }
  568.       printf ("\\setlength{\\textwidth}{15cm}\n");
  569.       printf ("\\setlength{\\textheight}{22.5cm}\n");
  570.       printf ("\\setlength{\\hoffset}{-2cm}\n");
  571.       printf ("\\setlength{\\voffset}{-2cm}\n");
  572.       if (want_index)
  573.         printf("\\makeindex\n");
  574.       if (header)
  575.     {
  576.       printf ("\\chead{\\%s Produced from ", header_font);
  577.       substitute(input_name);
  578.       printf (" at %.24s}\n", today);
  579.       printf ("\\cfoot{\\rm\\thepage}\n");
  580.       printf ("\\addtolength{\\headheight}{14pt}\n");
  581.       printf ("\\pagestyle{fancy}\n");
  582.     }
  583.       printf ("\\begin{document}\n");
  584.     }
  585.   printf ("\\expandafter\\ifx\\csname indentation\\endcsname\\relax%\n");
  586.   printf ("\\newlength{\\indentation}\\fi\n");
  587.   printf ("\\setlength{\\indentation}{%s}\n", indentation);
  588.   printf ("\\begin{flushleft}\n");
  589.   yylex();
  590.   printf ("\\end{flushleft}\n");
  591.   if (complete_file)
  592.     {
  593.       printf ("\\end{document}\n");
  594.     }
  595.   exit(0);
  596. }
  597.  
  598. void
  599. usage(const char * name)
  600. {
  601.   fprintf (stderr, "%s\n", version_string);
  602.   fprintf (stderr, "\
  603. Usage: %s [-a] [-c] [-h] [-i length] [-o path] [-p] [-s size] [-t]\n\
  604.        [-C font][-H font] [-K font] [-P font] [-S font] [-T wide] [-V] [-x]\n\
  605.        [+ansi-c] [+complete-file] [+header] [+indentation length]\n\
  606.        [+output path] [+c-plusplus] [+font-size size] [+pipe]\n\
  607.        [+comment-font font] [+keyword-font font] [+cpp-font font]\n\
  608.        [+header-font font] [+string-font font] [+tabstop wide]\n\
  609.        [+version] [+index] [path]\n", name);
  610.   exit (1);
  611. }
  612.