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

  1. /*
  2.  *  This is a flex input file but should be edited in -*-C-*- mode
  3.  *  C++2HTML: Produce HTML files from  C++ or C sources.
  4.  *  Copyright (C) 1994 Dimitry Kloper
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 1, or (at your option)
  9.  *  any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * ===========================================================================
  21.  *                          dimka@tochna.technion.ac.il    
  22.  *                                Dimitry Kloper            
  23.  *                           ISRAEL , Haifa , Technion                    
  24.  *                              Canada Dorm. 44/6/2        
  25.  *                              phone :  04-29-4643        
  26.  *           http://nearnet.gnn.com/gnn/netizens/dir/kloper-dimitry.html
  27.  * ===========================================================================
  28.  * 
  29.  * Any commens , bug reports and suggestions are welcome .
  30.  * You can obtain last version of the c++2html from
  31.  *   ftp://tochna.technion.ac.il/pub/staff/dimka/c++2html.html
  32.  * or
  33.  *   ftp://tochna.technion.ac.il/pub/staff/dimka/c++2html/c++2html.shar.gz
  34.  *
  35.  */
  36. /*
  37.  *  This is a flex input file but should be edited in -*-C-*- mode
  38.  *
  39.  *  C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  40.  *  Copyright (C) 1990 Norbert Kiesel
  41.  *
  42.  *  This program is free software; you can redistribute it and/or modify
  43.  *  it under the terms of the GNU General Public License as published by
  44.  *  the Free Software Foundation; either version 1, or (at your option)
  45.  *  any later version.
  46.  *
  47.  *  This program is distributed in the hope that it will be useful,
  48.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  49.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  50.  *  GNU General Public License for more details.
  51.  *
  52.  *  You should have received a copy of the GNU General Public License
  53.  *  along with this program; if not, write to the Free Software
  54.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  55.  *
  56.  *  Norbert Kiesel     
  57.  *  RWTH Aachen / Institut f. Informatik III
  58.  *  Ahornstr. 55
  59.  *  D-5100 Aachen
  60.  *  West Germany  
  61.  *
  62.  *  Phone:  +49 241 80-7266
  63.  *  EUNET:  norbert@rwthi3.uucp
  64.  *  USENET: ...!mcvax!unido!rwthi3!norbert
  65.  *  X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  66.  *
  67.  *  Please contact me for any bugs you find in this code or any
  68.  *  improvements! I'd also be very happy to get feedback where and
  69.  *  how frequently this program is used (just drop a little mail :-).
  70.  */
  71.  
  72. %x STRING BCOMMENT INCLUDE
  73. %s CPLUSPLUS
  74.  
  75. %{
  76. #define TAG(font,line) printf ("<%s>%s</%s>",font,line,font)
  77. #define OTAG(tag)      printf ("<%s>",tag)
  78. #define CTAG(tag)      printf ("</%s>",tag)
  79. #define KEY           TAG(keyword_tag,yytext);
  80. #define CPP           TAG(cpp_tag,yytext);
  81. #define SYM(x)    symbols(x)
  82. #define SUB(x)    substitute(x)
  83. #define OUT(x)  printf("%s",x)
  84. #define IND    indent(yytext)
  85. #define INIT    BEGIN (cplusplus_mode ? CPLUSPLUS : INITIAL);
  86.  
  87. #include <stdio.h>
  88.  
  89. #ifdef ANSI_C
  90.   #ifdef C_PLUSPLUS
  91.     #error ANSI_C and C_PLUSPLUS are mutually exclusive
  92.   #else
  93.     int cplusplus_mode = 0;
  94.   #endif
  95. #else /* CPLUSPLUS or default */
  96.   int cplusplus_mode = 1;
  97. #endif
  98.  
  99. unsigned long linecount = 0;
  100. int complete_file = 0;
  101. int header = 0;
  102. int tabtotab = 8;
  103. int piped = 0;
  104. int want_index = 0;
  105. int want_indent_links = 0;
  106. char * comment_tag = "EM";
  107. char * keyword_tag = "STRONG";
  108. char * header_tag = "KBD";
  109. char * cpp_tag = "STRONG";
  110. char * string_tag = "EM";
  111. char * operator_tag = "STRONG";
  112. char * include_html = "includes.html";
  113. char * indent_html = "indent.html";
  114. void substitute(const char *);
  115. void indent(const char *);
  116. void newpage(int);
  117. void usage(const char *);
  118. char *basename(char *);
  119. %}
  120.  
  121. %%
  122.  
  123.     INIT;
  124.  
  125. "#"[ \t]*"include"    { CPP; BEGIN (INCLUDE); }
  126. "#"[ \t]*"define"    |
  127. "#"[ \t]*"undef"    |
  128. "#"[ \t]*"pragma"    |
  129. "#"[ \t]*"if"        |
  130. "#"[ \t]*"ifdef"    |
  131. "#"[ \t]*"ifndef"       |
  132. "#"[ \t]*"elif"        |
  133. "#"[ \t]*"else"        |
  134. "#"[ \t]*"error"    |
  135. "#"[ \t]*"endif"    |
  136. "#"[ \t]*"line"        CPP;
  137.  
  138.  
  139. <INCLUDE>"<"[^>]*/">"    { SUB ("<");
  140.                printf("<A HREF=\"%s#%s\">",include_html,yytext+1);
  141.                            SUB (yytext+1);
  142.                            printf("</A>"); 
  143.                           SUB(">");
  144.               input(); INIT; }
  145. <INCLUDE>\"[^\"]*/\"    { SUB ("\"");
  146.                printf("<A HREF=\"%s#%s\">",include_html,yytext+1);
  147.                            SUB (yytext+1);
  148.                            printf("</A>"); 
  149.                           SUB("\"");
  150.               input(); INIT; }
  151. <INCLUDE>[ \t]+        SUB(yytext);
  152. <INCLUDE>[\n]        OUT ("<P>");
  153. <INCLUDE>.        { yyless (0); INIT; }
  154.  
  155. "auto"            |
  156. "double"        |
  157. "int"            |
  158. "struct"        |
  159. "break"            |
  160. "else"            |
  161. "long"            |
  162. "switch"        |
  163. "case"            |
  164. "enum"            |
  165. "register"        |
  166. "typedef"        |
  167. "char"            |
  168. "extern"        |
  169. "return"        |
  170. "union"            |
  171. "const"            |
  172. "float"            |
  173. "short"            |
  174. "unsigned"        |
  175. "continue"        |
  176. "for"            |
  177. "signed"        |
  178. "void"            |
  179. "default"        |
  180. "goto"            |
  181. "sizeof"        |
  182. "volatile"        |
  183. "do"            |
  184. "if"            |
  185. "static"        |
  186. "while"            |
  187. <CPLUSPLUS>"new"    |
  188. <CPLUSPLUS>"delete"    |
  189. <CPLUSPLUS>"this"    |
  190. <CPLUSPLUS>"operator"    |
  191. <CPLUSPLUS>"class"    |
  192. <CPLUSPLUS>"catch"      |
  193. <CPLUSPLUS>"public"    |
  194. <CPLUSPLUS>"protected"    |
  195. <CPLUSPLUS>"private"    |
  196. <CPLUSPLUS>"virtual"    |
  197. <CPLUSPLUS>"friend"    |
  198. <CPLUSPLUS>"inline"    |
  199. <CPLUSPLUS>"template"   |
  200. <CPLUSPLUS>"overload"    KEY;
  201.  
  202. "->"                |
  203. "<<"            |
  204. ">>"            |
  205. "<="            |
  206. ">="            |
  207. "!="            |
  208. "||"            |
  209. "..."            |
  210. "*="            |
  211. "<<="            |
  212. ">>="            |
  213. "^="            |
  214. "|="            |
  215. "~"            |
  216. "*"            |
  217. "^"            |
  218. "|"            |
  219. <CPLUSPLUS>"->*"    |
  220. "/"            |
  221. "<"            |
  222. ">"            |
  223. "&&"            |
  224. "%="            |
  225. "&="            |
  226. "{"            |
  227. "}"            |
  228. "&"            |
  229. "%"            |
  230. "--"            |
  231. "?"            |
  232. ":"            |
  233. "="            |
  234. ","            |
  235. "."            |
  236. ";"            |
  237. "!"            |
  238. "-"            |
  239. "+"            |
  240. "/="            |
  241. "=="            |
  242. "++"            |
  243. "+="            |
  244. "-="            |
  245. "("            |
  246. ")"            |
  247. "["            |
  248. "]"            |
  249. <CPLUSPLUS>"::"        |
  250. <CPLUSPLUS>".*"        { OTAG(operator_tag);
  251.                           SUB(yytext);
  252.                           CTAG(operator_tag); }
  253.  
  254.  
  255.                                         
  256. <CPLUSPLUS>[a-zA-Z_$][a-zA-Z_$0-9:~]*    |
  257. [a-zA-Z_][a-zA-Z_0-9]*               { if(want_indent_links) 
  258.                                      printf("<A HREF=\"%s#%s\">",
  259.                                             indent_html,yytext);
  260.                                   SUB (yytext); 
  261.                                   if(want_indent_links)
  262.                                     CTAG("A"); }
  263.  
  264. "/*"                { BEGIN (BCOMMENT); 
  265.                                   TAG(operator_tag,"/*");
  266.                                   OTAG(comment_tag); }
  267.  
  268. <BCOMMENT>"*/"            { INIT; 
  269.                                   CTAG(comment_tag); 
  270.                                   TAG(operator_tag,"*/");
  271.                                 }
  272.  
  273. <BCOMMENT>^[ \t]+        IND;
  274. <BCOMMENT>.            OUT (yytext);
  275.  
  276. <CPLUSPLUS>"//".*$        { 
  277.                                   OTAG(comment_tag);
  278.                                   TAG(operator_tag,"//");
  279.                       OUT (yytext+2);
  280.                   CTAG(comment_tag);
  281.                                 }
  282.  
  283. L?\"                 { BEGIN (STRING); 
  284.                                   SUB("\"");
  285.                   OTAG(string_tag); }
  286. <STRING>\"            { INIT;
  287.                                   CTAG(string_tag);
  288.                                   SUB("\""); }
  289.                                   
  290. <STRING>^[ \t]+            IND;
  291. <STRING>.            SUB (yytext);
  292.  
  293. ([0-9]*\.[0-9]+[fFlL]?)         |
  294. ([0-9]+\.[0-9]*[fFlL]?)         |
  295. ([0-9]*\.?[0-9]+[eE][+-]?[0-9]+) |
  296. ([0-9]+\.?[0-9]*[eE][+-]?[0-9]+) ECHO;
  297.  
  298. [0-9]+[uUlL]?            ECHO;
  299.  
  300. L?'[ -~]'            |
  301. L?'\\[ntvbrfa\\?'"]'        |
  302. L?'\\[0-7]{1,3}'        |
  303. L?'\\x[0-9a-fA-F]{1,2}'        SUB (yytext);
  304.  
  305. 0[0-7]+[uUlL]?            ECHO;
  306.  
  307. 0x[0-9a-fA-F]+[uUlL]?        ECHO;
  308.  
  309. ^[ \t]+                    IND;
  310. [ \t]+                IND;
  311.                               
  312. "\n"                            {linecount++;ECHO;}
  313.  
  314.  
  315. %%
  316.  
  317. void substitute (const char * input)
  318. {
  319.   while (*input)
  320.     {
  321.       switch (*input)
  322.     {
  323.     case '<':
  324.           printf("<");
  325.           break;
  326.     case '>':
  327.       printf (">");
  328.       break;
  329.     case '&':
  330.       printf ("&");
  331.       break;
  332.     case '"':
  333.       printf (""");
  334.       break;
  335.     default:
  336.       printf ("%c", *input);
  337.       break;
  338.     }
  339.       input++;
  340.     }
  341. }
  342.  
  343. void
  344. indent(const char * blanks)
  345. {
  346.   int i;
  347.   
  348.   while (*blanks)
  349.     {
  350.       if (*blanks == ' ')
  351.     {
  352.       printf(" "); 
  353.     }
  354.       else            /* *blanks == '\t' */
  355.     {
  356.       i = tabtotab ; 
  357.           while (i--) printf(" ");          
  358.     }
  359.       blanks++;
  360.     }
  361. }
  362.  
  363. #undef getopt
  364. #include "getopt.h"
  365. #include <string.h>
  366. #include <fcntl.h>
  367. #include <ctype.h>
  368. #include <time.h>
  369.  
  370.  
  371.  
  372. extern char * version_string;
  373.  
  374. static struct option opts[] =
  375. {
  376.   {"ansi-c", 0, 0, 'a'},
  377.   {"c-plusplus", 0, 0, 'p'},
  378.   {"complete-file", 0, 0, 'c'},
  379.   {"header", 0, 0, 'h'},
  380.   {"output", 1, 0, 'o'},
  381.   {"tabstop", 1, 0, 'T'},
  382.   {"comment-tag", 1, 0, 'C'},
  383.   {"string-tag", 1, 0, 'S'},
  384.   {"keyword-tag", 1, 0, 'K'},
  385.   {"header-tag", 1, 0, 'H'},
  386.   {"cpp-tag", 1, 0, 'P'},
  387.   {"operator-tag", 1, 0, 'O'},
  388.   {"indent-links",0,0,'l'},
  389.   {"version", 0, 0, 'V'},
  390.   {"index", 0, 0, 'x'},
  391.   {"includes-html",1,0,'I'},
  392.   {"ident-html",1,0,'D'},
  393.   {0, 0, 0, 0}
  394. };
  395.  
  396.  
  397. main (int argc, char** argv)
  398. {
  399.   int c;
  400.   int index;
  401.   int i;
  402.   int has_filename;
  403.   char * input_name;
  404.   char * output_name;
  405.   char * program_name;
  406.  
  407.   time_t now;
  408.   char * today;
  409.  
  410.   input_name = NULL ;
  411.   output_name = NULL ;
  412.  
  413.   now = time(0);
  414.   today = ctime(&now);
  415.  
  416.  
  417.   program_name = basename(argv[0]);
  418.  
  419. #ifdef USE_NAME 
  420. #if defined(ANSI_C) || defined(C_PLUSPLUS)
  421. #error USE_NAME, ANSI_C and C_PLUSPLUS are mutually exclusive
  422. #else
  423.   /* simple heuristic: '+' in name means C++ */
  424.   cplusplus_mode = (strchr (program_name, '+') != 0);
  425. #endif
  426. #endif
  427.   
  428.   while ((c = getopt_long (argc, argv,
  429.                "lacpo:O:I:D:hT:C:H:S:K:P:Vx", opts, &index))
  430.      != EOF)
  431.     {
  432.       if (c == 0)        /* Long option */
  433.     {
  434.       c = opts[index].val;
  435.     }
  436.       switch (c)
  437.     {
  438.         case 'I':
  439.           include_html = optarg;
  440.           break;
  441.         case 'D':
  442.           indent_html = optarg;
  443.           break;
  444.         case 'l':
  445.           want_indent_links = 1;
  446.           break;
  447.     case 'a':
  448.       cplusplus_mode = 0;
  449.       break;
  450.     case 'p':
  451.       cplusplus_mode = 1;
  452.       break;
  453.     case 'c':
  454.       complete_file = 1;
  455.       break;
  456.     case 'o':
  457.       output_name = optarg;
  458.       break;
  459.         case 'O':
  460.       operator_tag = optarg;
  461.       break;
  462.     case 'T':
  463.       tabtotab = atoi(optarg);
  464.       break;
  465.     case 'h':
  466.       header = 1;
  467.       complete_file = 1;    /* header implies complete-file */
  468.       break;
  469.     case 'C':
  470.       comment_tag = optarg;
  471.       break;
  472.     case 'H':
  473.       header_tag = optarg;
  474.       break;
  475.     case 'P':
  476.       cpp_tag = optarg;
  477.       break;
  478.     case 'S':
  479.       string_tag = optarg;
  480.       break;
  481.     case 'K':
  482.       keyword_tag = optarg;
  483.       break;
  484.     case 'V':
  485.       fprintf (stderr, "%s\n", version_string);
  486.       break;
  487.         case 'x':
  488.           want_index = 1;
  489.           break;
  490.     default:
  491.       usage(program_name);
  492.     }
  493.     }
  494.  
  495.   has_filename = (argc - optind == 1);
  496.   if (has_filename)        /* last argument is input file name */
  497.     {
  498.       input_name = argv[optind];
  499.       if (freopen (input_name, "r", stdin) == NULL)
  500.     {
  501.       fprintf (stderr, "%s: Can't open `%s' for reading\n",
  502.            program_name, input_name);
  503.       exit (2);
  504.     }
  505.     }
  506.   else
  507.      input_name = "StdIn"; 
  508.  
  509.   if (output_name != NULL)
  510.     {
  511.       if (freopen (output_name, "w", stdout) == NULL)
  512.     {
  513.       fprintf (stderr, "%s: Can't open `%s' for writing\n",
  514.            program_name, output_name);
  515.       exit (4);
  516.     }
  517.     }
  518.   else
  519.      output_name = "StdOut"; 
  520.  
  521.  
  522.  
  523.  
  524.   if(complete_file) 
  525.     {
  526.       printf ("<HTML>\n");
  527.       printf ("<HEAD>\n");
  528.       printf ("<TITLE>%s</TITLE>\n",input_name);
  529.     }
  530.   printf("<! %s>\n",today);
  531.   printf("<! This file created from \"%s\" by %s converter >\n",input_name,
  532.           program_name);
  533.   printf("<! %s is written by Dimitry Kloper (dimka@tochna.technion.ac.il)>\n",
  534.           program_name);
  535.   printf("<! actually it is rewritten c++2latex (1.1) by Norbert Kiesel.>\n"); 
  536.   printf("<! See COPYING file for license information .>\n"); 
  537.   if(header)
  538.     {
  539.       printf("<H2>%s</H2>\n<STRONG>Composed on %s</STRONG>\n<HR>\n",
  540.              output_name,today);
  541.     }
  542.  
  543.   if(complete_file) 
  544.      printf ("</HEAD>\n<BODY>\n");
  545.  
  546.   printf ("<H1>%s</H1><HR><P>\n",input_name);
  547.  
  548.   printf ("<PRE>\n");
  549.   yylex();
  550.  
  551.   printf ("</PRE>\n");
  552.   if(complete_file) 
  553.     {
  554.       printf ("</BODY>\n");
  555.       printf ("</HTML>\n");
  556.     }
  557.  
  558.   exit(0);
  559. }
  560.  
  561. void
  562. usage(const char * name)
  563. {
  564.   fprintf (stderr, "%s\n", version_string);
  565.   fprintf (stderr, "\
  566. \n
  567.   {-a,+ansi-c} 
  568.        The input is an ANSI-C program. Default is C++, so don't give this
  569.        option for C++ programs and always give this option for ANSI-C pro-
  570.        grams.
  571.  
  572.   {-c,+complete-file}
  573.        The output is enveloped in commands which produce a complete HTML 
  574.        source. 
  575.  
  576.   {-h,+header}
  577.        Each page of output has a header giving the input file name and the
  578.        date the file was produced. This option implies the {-c,+complete-
  579.        file} option.
  580.  
  581.   {-o,+output} path 
  582.        The output is written in the file pointed to by path.
  583.  
  584.   {-C,+comment-tag} tag 
  585.        The comments are set in the given tag. Default is `EM'.
  586.  
  587.   {-H,+header-tag} tag 
  588.        The headers are set in the given tag. Default is `KBD'.
  589.  
  590.   {-K,+keyword-tag} tag 
  591.        The keywords are set in the given font. Default is `STRONG'.
  592.  
  593.   {-P,+cpp-tag} tag
  594.        The preprocessor directives are set in the given tag. Default is
  595.        `STRONG'.
  596.  
  597.   {-S,+string-tag} tag
  598.        The string and character constants are set in the given tag.
  599.        Default is `EM'.
  600.  
  601.   {-O,+operator-tag} tag
  602.        The operators are set in the given tag.
  603.        Default is `STRONG'.
  604.  
  605.   {-T,+tabstop} wide
  606.        The wide of tabs is wide. Default is `8'.
  607.  
  608.   {-V,+version}
  609.        Prints the version number on stderr.
  610.  
  611.   {-x,+index}
  612.        Creates index commands for all identifiers.
  613.        NOT IMPLEMENTED YET ! 
  614.  
  615.   {-l,+indent-links}
  616.        Whether to mark all identifiers as hyper-links . 
  617.        Default is `false' . 
  618.  
  619.   {-I,+include-html}
  620.        Set file name for HTML index for all included files . 
  621.  
  622.   {-D,+ident-html}
  623.        Set file name for HTML index for all  identifiers.
  624. \n");
  625.  
  626.  
  627.   exit (1);
  628. }
  629.  
  630. char *
  631. basename (s)
  632.      char *s;
  633. {
  634.   char *this;
  635.  
  636.   this = strrchr (s, '/');
  637.   if (this == NULL)
  638.     {                /* no path separator */
  639.       this = s;
  640.     }
  641.   else
  642.     {
  643.       this++;
  644.     }
  645.   return this;
  646. }
  647.