home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cpp2latx.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-05-16  |  9KB  |  384 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.  *
  38.  *  C++2LaTeX 2.0: Produce even more prettyprinted LaTeX files from C++ or C sources.
  39.  *
  40.  *  Copyright (C) 1991 Joerg Heitkoetter
  41.  *  Systems Analysis Research Group, University of Dortmund
  42.  *  (heitkoet@gorbi.informatik.uni-dortmund.de).
  43.  *
  44.  */
  45.  
  46. #ifdef LATTICE
  47. extern int tabtobrace, tabtotab, cplusplus_mode, complete_file, piped;
  48. extern int aligntoright, header;
  49. extern char *font_size, *indentation, *comment_font, *header_font;
  50. extern char *cpp_font, *string_font, *keyword_font;
  51.  
  52. /* Prototypes for functions defined in main.c */
  53. void substitute(char *input);
  54. void indent(char *blanks);
  55. int main(int argc, char **argv);
  56. void usage(char *name);
  57. #endif
  58.  
  59. void
  60. substitute (input)
  61. char   *input;
  62. {
  63.     while (*input) {
  64.         switch (*input) {
  65.             case '_':
  66.             case '&':
  67.             case '#':
  68.             case '$':
  69.             case '%':
  70.             case '{':
  71.             case '}':
  72.                 printf ("\\%c", *input);
  73.                 break;
  74.             case '+':
  75.             case '=':
  76.             case '<':
  77.             case '>':
  78.                 printf ("$%c$", *input);
  79.                 break;
  80.             case '*':
  81.                 printf ("$\\ast$");
  82.                 break;
  83.             case '|':
  84.                 printf ("$\\mid$");
  85.                 break;
  86.             case '\\':
  87.                 printf ("$\\backslash$");
  88.                 break;
  89.             case '^':
  90.                 printf ("$\\wedge$");
  91.                 break;
  92.             case '~':
  93.                 printf ("$\\sim$");
  94.                 break;
  95.             default:
  96.                 printf ("%c", *input);
  97.                 break;
  98.         }
  99.         input++;
  100.     }
  101. }
  102.  
  103. void
  104. indent (blanks)
  105. char   *blanks;
  106. {
  107.     int     i;
  108.  
  109.     i = 0;
  110.     while (*blanks) {
  111.         if (*blanks == ' ') {
  112.             i++;
  113.         } else {           /* *blanks == '\t' */
  114.             while (++i % tabtotab);
  115.         }
  116.         blanks++;
  117.     }
  118.     printf ("\\hspace*{%d\\indentation}", i);
  119. }
  120.  
  121. #ifdef LATTICE
  122. #include <stdio.h>
  123. #endif
  124.  
  125. #include "getopt.h"
  126. #include <string.h>
  127. #include <fcntl.h>
  128. #include <ctype.h>
  129. #include <time.h>
  130.  
  131. extern char *version_string;
  132.  
  133. static struct option opts[] =
  134. {
  135.     {"ansi-c", 0, 0, 'a'},
  136.     {"complete-file", 0, 0, 'c'},
  137.     {"font-size", 1, 0, 's'},
  138.     {"indentation", 1, 0, 'i'},
  139.     {"header", 0, 0, 'h'},
  140.     {"piped", 0, 0, 'p'},
  141.     {"alignment", 0, 0, 'n'},      /* turn off comment alignment  -joke */
  142.     {"output", 1, 0, 'o'},
  143.     {"tabstop", 1, 0, 'T'},
  144.     {"brace-tab", 1, 0, 'b'},      /* added new tabtobrace  -joke */
  145.     {"comment-font", 1, 0, 'C'},
  146.     {"string-font", 1, 0, 'S'},
  147.     {"keyword-font", 1, 0, 'K'},
  148.     {"header-font", 1, 0, 'H'},
  149.     {"cpp-font", 1, 0, 'P'},
  150.     {"version", 0, 0, 'V'},
  151.     {0, 0, 0, 0}
  152. };
  153.  
  154. char *program_name;
  155.  
  156.  
  157. main (argc, argv)
  158. int     argc;
  159. char  **argv;
  160. {
  161.     int     c;
  162.     int     index;
  163.     int     i;
  164.     int     has_filename;
  165.     char   *input_name;
  166.     char   *output_name;
  167. /*    char   *program_name;*/
  168.     long    now;
  169.     char   *today;
  170.     char   *malloc ();
  171.  
  172.     input_name = "Standard Input";
  173.     output_name = 0;
  174.  
  175.     now = time (0);
  176.     today = ctime (&now);
  177.  
  178.     program_name = strrchr (argv[0], '/');
  179.     if (program_name == NULL) {    /* no pathname */
  180.         program_name = argv[0];
  181.     } else {
  182.         program_name++;
  183.     }
  184.  
  185.  /* simple heuristic: '+' in name means C++ */
  186.     cplusplus_mode = (strchr (program_name, '+') != 0);
  187.  
  188.     if (argc == 1)
  189.         usage (program_name);  /* added exit with usage  -joke */
  190.  
  191.     while ((c = getopt_long (argc, argv,
  192.                  "acpno:s:i:b:hT:C:H:S:K:P:V", opts, &index))
  193.             != EOF) {
  194.         if (c == 0) {           /* Long option */
  195.             c = opts[index].val;
  196.         }
  197.         switch (c) {
  198.             case 'a':
  199.                 cplusplus_mode = 0;
  200.                 break;
  201.             case 'c':
  202.                 complete_file = 1;
  203.                 break;
  204.             case 'o':
  205.                 if (piped) {
  206.                     fprintf (stderr,
  207.                          "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  208.                          program_name);
  209.                     exit (5);
  210.                 }
  211.                 output_name = optarg;
  212.                 break;
  213.             case 'n':
  214.                 aligntoright = 0;
  215.                 break;
  216.             case 's':
  217.                 font_size = optarg;
  218.                 break;
  219.             case 'i':
  220.                 indentation = optarg;
  221.                 break;
  222.             case 'b':
  223.                 tabtobrace = atoi (optarg);
  224.                 break;
  225.             case 'T':
  226.                 tabtotab = atoi (optarg);
  227.                 break;
  228.             case 'p':
  229.                 if (output_name != 0) {
  230.                     fprintf (stderr,
  231.                          "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  232.                          program_name);
  233.                     exit (5);
  234.                 }
  235.                 piped = 1;
  236.                 break;
  237.             case 'h':
  238.                 header = 1;
  239.                 complete_file = 1;    /* header implies
  240.                              * complete-file */
  241.                 break;
  242.             case 'C':
  243.                 comment_font = optarg;
  244.                 break;
  245.             case 'H':
  246.                 header_font = optarg;
  247.                 break;
  248.             case 'P':
  249.                 cpp_font = optarg;
  250.                 break;
  251.             case 'S':
  252.                 string_font = optarg;
  253.                 break;
  254.             case 'K':
  255.                 keyword_font = optarg;
  256.                 break;
  257.             case 'V':
  258.                 fprintf (stderr, "%s\n", version_string);
  259.                 break;
  260.             default:
  261.                 usage (program_name);
  262.         }
  263.     }
  264.     has_filename = (argc - optind == 1);
  265.     if (has_filename) {           /* last argument is input file name */
  266.         input_name = argv[optind];
  267.         if (freopen (input_name, "r", stdin) == NULL) {
  268.             fprintf (stderr, "%s: Can't open `%s' for reading\n",
  269.                  program_name, input_name);
  270.             exit (2);
  271.         }
  272.     }
  273.     if ((output_name == 0) && !piped) {
  274.         char   *tmp;
  275.         if (has_filename) {
  276.             tmp = strrchr (input_name, '/');
  277.             if (tmp == 0) {    /* plain filename */
  278.                 tmp = input_name;
  279.             } else {
  280.                 tmp++;
  281.             }
  282.         } else {
  283.             tmp = program_name;
  284.         }
  285. #ifdef LATTICE
  286.         output_name = (char *)malloc (strlen (tmp) + 4);
  287. #else
  288.         output_name = malloc (strlen (tmp) + 4);
  289. #endif
  290.         if (output_name == 0) {
  291.             fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  292.             exit (3);
  293.         }
  294.         strcpy (output_name, tmp);
  295.         strcat (output_name, ".tex");
  296.     }
  297.     if (!piped) {
  298.         if (freopen (output_name, "w", stdout) == NULL) {
  299.             fprintf (stderr, "%s: Can't open `%s' for writing\n",
  300.                  program_name, output_name);
  301.             exit (3);
  302.         }
  303.     }
  304.     printf ("\
  305. %%\n\
  306. %% This file was automatically produced at %.24s by\n\
  307. %% %s", today, program_name);
  308.     for (i = 1; i < argc; i++) {
  309.         printf (" %s", argv[i]);
  310.     }
  311.     if (!has_filename) {
  312.         printf (" (from Standard Input)");
  313.     }
  314.     printf ("\n%%\n");
  315.     if (complete_file) {
  316.         if (header) {
  317.             if (strcmp (font_size, "10") == 0) {
  318.                 printf ("\\documentstyle[fancyheadings]{article}\n");
  319.             } else {
  320.                 printf ("\\documentstyle[%spt,fancyheadings]{article}\n",
  321.                     font_size);
  322.             }
  323.         } else {
  324.             if (strcmp (font_size, "10") == 0) {
  325.                 printf ("\\documentstyle{article}\n");
  326.             } else {
  327.                 printf ("\\documentstyle[%spt]{article}\n", font_size);
  328.             }
  329.         }
  330.         printf ("\\setlength{\\textwidth}{16cm}\n");
  331.         printf ("\\setlength{\\textheight}{23cm}\n");
  332.         printf ("\\setlength{\\hoffset}{-2cm}\n");
  333.         printf ("\\setlength{\\voffset}{-2cm}\n");
  334.         if (header) {
  335.             printf ("\\lhead{\\%s ", header_font);
  336.             substitute (input_name);
  337.             printf ("}");
  338.             printf ("\\rhead{\\rm\\thepage}\n");
  339.             printf ("\\cfoot{}\n");
  340.             printf ("\\addtolength{\\headheight}{14pt}\n");
  341.             printf ("\\pagestyle{fancy}\n");
  342.         }
  343.         printf ("\\begin{document}\n");
  344.     }
  345.     printf ("\\expandafter\\ifx\\csname indentation\\endcsname\\relax%\n");
  346.     printf ("\\newlength{\\indentation}\\fi\n");
  347.     printf ("\\setlength{\\indentation}{%s}\n", indentation);
  348.     printf ("\\begin{flushleft}\n");
  349.     yylex ();
  350.     printf ("\\end{flushleft}\n");
  351.     if (complete_file) {
  352.         printf ("\\end{document}\n");
  353.     }
  354.     exit (0);
  355. }
  356.  
  357. void
  358. usage (name)
  359. char   *name;
  360. {
  361.     fprintf (stderr, "%s\n", version_string);
  362.     fprintf (stderr, "\
  363. Usage: %s [options] file\n\n\
  364. Options:\n\
  365.     [-a]            [-b]\n\
  366.     [-c]            [-h]\n\
  367.     [-i length]        [-n]\n\
  368.     [-o file]        [-p]\n\
  369.     [-s fontsize]        [-C font]\n\
  370.     [-H font]        [-K font]\n\
  371.     [-P font]        [-S font]\n\
  372.     [-T tabulatorwidth]    [-V]\n\
  373.     \n\
  374.     [+ansi-c]        [+brace-tab indentation]\n\
  375.     [+complete-file]    [+header]\n\
  376.     [+indentation length]    [+no-alignment]\n\
  377.     [+output file]        [+pipe]\n\
  378.     [+font-size size]    [+comment-font font]\n\
  379.     [+keyword-font font]    [+cpp-font font]\n\
  380.     [+header-font font]    [+string-font font]\n\
  381.     [+tabstop width]    [+version]\n", name);
  382.     exit (1);
  383. }
  384.