home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / tr2latex_2 / c / tr2latex < prev    next >
Encoding:
Text File  |  1992-05-16  |  7.5 KB  |  308 lines

  1. /*
  2. ** tr2latex - troff to LaTeX converter
  3. ** $Id: tr2latex.c,v 2.2 1992/04/27 15:13:26 Christian_Engel Dist krischan $
  4. ** COPYRIGHT (C) 1987 Kamal Al-Yahya, 1991,1992 Christian Engel
  5. ** 
  6. ** Module: tr2latex.c
  7. **
  8. ** This module contains the main function inititating the translation
  9. ** and supporting the Usage.
  10. */
  11.  
  12. char *usage_doc[] = {
  13. "tr2latex - troff to LaTeX converter, $Revision: 2.2 $",
  14. "SYNTAX:  tr2latex [-m] [-t] [-<n>] [-s <style>] [-o <outfile>] [<file>...]",
  15. "options: -m            for manual",
  16. "         -t            twoside page style",
  17. "         -h            use helvetica style option",
  18. "         -<n>          a number n gives the font size (default is 12pt",
  19. "                       for man, 11pt otherwise)",
  20. "         -s <style>    use documentstyle <style> (default is article)",
  21. "         -o <outfile>  send output to <outfile> (default is stdout)",
  22. #ifdef DEBUG
  23. "         -do           debug output",
  24. "         -dv           verbose informations",
  25. #endif
  26. 0
  27. };
  28.  
  29. #define MAIN
  30.  
  31. #include    "setups.h"
  32. #include    "protos.h"
  33.  
  34. #ifdef __TURBOC__
  35. /*--- The Turbo C stack needs to be increased in size,
  36.       6k seems to be enough; 4k is not                 ---*/
  37. extern unsigned _stklen = 0x1800 ;
  38. #endif
  39.  
  40.  
  41. extern char *mktemp();
  42. char scratch_file[MAXWORD];
  43.  
  44. bool    man,        /* option -m: manual page */
  45.         fontsize,    /* option -9/-10/-11/-12: font size */
  46.         twoside,    /* option -t: twoside */
  47.         piped_in,
  48.         helvetica;
  49.  
  50. char *document = "article";    /* document type, see also -s option */
  51. extern char version [];
  52.  
  53. FILE *out_file;        /* in case they can't use *NIX redirecting or piping */
  54.  
  55. char *prgname;
  56. char inbuf[MAXLEN],
  57.      outbuf[MAXLEN];
  58.  
  59.  
  60.  
  61. int main (int argc, char *argv[])
  62. {
  63.     char *pin = inbuf,
  64.          *pout = outbuf;
  65.     FILE *in_file;
  66.     long timeval;        /* clock value from time() for ctime()    */
  67.     int argi;
  68.  
  69.     prgname = argv [0];
  70.     out_file = stdout;        /* default output */
  71.  
  72.     /* process option flags */
  73.     getopts (&argc, argv);
  74.  
  75. #ifdef DEBUG
  76.     if (debug_v) {
  77.         printf ("Arguments:");
  78.         for (argi = 1; argi < argc; argi++)
  79.             printf (" %s", argv [argi]);
  80.         putchar ('\n');
  81.     }
  82. #endif
  83.  
  84. #ifdef    VMS
  85.     if ((out_file == stdout) &&
  86.         ((out_file = freopen ("Sys$Output:", "w", stdout,
  87.                               "rat=cr", "rfm=var")) == NULL)) {
  88.         fprintf (stderr, "Can't reopen stdout\n");
  89.         errexit (GOOD);
  90.     }
  91. #endif
  92.  
  93.     /* initialize spacing and indentation parameters */
  94.     strcpy(linespacing.def_units,"\\normalbaselineskip");
  95.     strcpy(linespacing.old_units,"\\normalbaselineskip");
  96.     strcpy(indent.def_units,"em");
  97.     strcpy(indent.old_units,"em");
  98.     strcpy(tmpind.def_units,"em");
  99.     strcpy(tmpind.old_units,"em");
  100.     strcpy(space.def_units,"\\baselineskip");
  101.     strcpy(space.old_units,"\\baselineskip");
  102.     strcpy(vspace.def_units,"pt");
  103.     strcpy(vspace.old_units,"pt");
  104.     linespacing.value = 1.;
  105.     linespacing.old_value = 1.;
  106.     indent.value = 0.;
  107.     indent.old_value = 0.;
  108.     tmpind.value = 0.;
  109.     tmpind.old_value = 0.;
  110.     space.value = 1.;
  111.     space.old_value = 1.;
  112.     vspace.value = 1.;
  113.     vspace.old_value = 1.;
  114.     linespacing.def_value = 0;
  115.     indent.def_value = 0;
  116.     tmpind.def_value = 0;
  117.     space.def_value = 1;
  118.     vspace.def_value = 1;
  119.     
  120.     math_mode = 0;                    /* start with non-math mode */
  121.     de_arg = 0;                     /* not a .de argument */
  122.     
  123.     /* start of translated document */
  124.     
  125.     timeval = time(0);
  126.     fprintf (out_file,
  127. "%% -*-LaTeX-*-\n\
  128. %% Converted automatically from troff to LaTeX\n\
  129. %% by %s\n\
  130. %% on %s\
  131. %% tr2latex was written by Kamal Al-Yahya at Stanford University\n\
  132. %% (Kamal%%Hanauma@SU-SCORE.ARPA)\n\
  133. %% and substantially enhanced by Christian Engel at RWTH Aachen\n\
  134. %% (krischan@informatik.rwth-aachen.de).\n\
  135. %%\n\
  136. %% troff input file%s:%s",
  137.              version, ctime(&timeval),
  138.              argc>2?"s":"",
  139.              argc==1?" <stdin>":"");
  140.     for (argi = 1; argi < argc; argi++) {
  141.         if (strcmp (argv [argi], "-") == 0)
  142.             fprintf (out_file, " <stdin>");
  143.         else
  144.             fprintf (out_file, " %s", argv[argi]);
  145.     }
  146.  
  147.     /* document style and options */
  148.     fprintf (out_file,"\n\n\\documentstyle[%s", man? "troffman": "troffms");
  149.     if (fontsize == 0 && !man)
  150.         fontsize = 11;
  151.     if (fontsize != 0)
  152.     fprintf (out_file,",%dpt", fontsize);
  153.     if (helvetica)
  154.         fputs (",helvetica", out_file);
  155.     if (twoside)
  156.         fputs (",twoside", out_file);
  157.     fprintf (out_file,"]{%s}\n\\begin{document}\n", document);
  158.  
  159.     if (argc == 1)
  160.         process (stdin, "<stdin>", pin, pout);
  161.     else {
  162.         for (argv++; --argc; argv++) {
  163.             if (strcmp (*argv, "-") == 0)
  164.                 process (stdin, "<stdin>", pin, pout);
  165.             else if ((in_file = fopen(*argv,"r")) == NULL)
  166.                 fprintf(stderr,"%s: Cannot open input file `%s'\n",
  167.                         prgname,*argv);
  168.             else {
  169.                 process (in_file, *argv, pin, pout);
  170.                 fclose(in_file);
  171.             }
  172.         }
  173.     }
  174.     /* close translated document */
  175.     fputs("\\end{document}\n",out_file);
  176.  
  177.     exit(GOOD);
  178. }
  179.  
  180.  
  181. void process (FILE *in_file, char *f_name, char *pin, char *pout)
  182. {
  183.     static char sep[] = "--------------------------------------------------";
  184.  
  185.     tmpbuf (in_file, pin);
  186.     fprintf (out_file, "%%%s\n%% start of input file: %s\n%%\n", sep, f_name);
  187.     troff_tex (pin, pout, 0, 0);
  188.     fputs (pout, out_file);
  189.     fprintf (out_file, "%%\n%% end of input file: %s\n%%%s\n", f_name, sep);
  190. }
  191.  
  192.  
  193. # define shift_arg(cnt)         for (i = argind; i < *p_argc - cnt; i++)    \
  194.                                     argv [i] = argv [i + cnt];              \
  195.                                 argind -= cnt;                              \
  196.                                 *p_argc -= cnt;
  197.  
  198. void getopts (int *p_argc, char *argv[])
  199. {
  200.     char *p_opt;
  201.     int argind, i;
  202.  
  203.     for (argind = 1; argind < *p_argc; argind ++)
  204.         if (*argv [argind] == '-') {
  205.             if (strcmp (argv+1, "help") == 0)
  206.                 usage (0);
  207.             for (p_opt = argv [argind] + 1; ; p_opt++) {
  208.                 switch (*p_opt) {
  209.                 case '\0':
  210.                     if (p_opt != argv [argind] + 1) {
  211.                         shift_arg (1);
  212.                     }
  213.                     break;
  214.                 case 'h':
  215.                     helvetica = 1;
  216.                     continue;
  217.                 case 'm':
  218.                     man = 1;
  219.                     continue;
  220.                 case 't':
  221.                     twoside = 1;
  222.                     continue;
  223.                 case 's':
  224.                     document = argv [argind+1];
  225.                     shift_arg(2);
  226.                     continue;
  227.                 case '9':
  228.                     fontsize = 9;
  229.                     continue;
  230.                 case 'o':
  231.                     if (argind + 1 >= *p_argc) {
  232.                         fprintf (stderr, "%s: Missing output file name\n",
  233.                                  prgname);
  234.                         usage (1);
  235.                     }
  236. #ifdef    VMS
  237.                     if ((out_file = fopen(argv[argind+1],"w",
  238.                                           "rat=cr","rfm=var")) == NULL)
  239. #else
  240.                     if ((out_file = fopen(argv[argind+1],"w")) == NULL)
  241. #endif
  242.                     {
  243.                         fprintf(stderr, "%s: can't open %s\n",
  244.                           prgname, argv [argind+1]);
  245.                         usage (errno);
  246.                     }
  247.                     shift_arg(2);
  248.                     break;
  249. #ifdef DEBUG
  250.                 case 'd':
  251.                     if (*++p_opt == '\0') {
  252.                         fprintf (stderr, "%s: missing debug option\n",
  253.                                  prgname);
  254.                         usage (1);
  255.                     }
  256.                     switch (*p_opt) {
  257.                     case 'o':
  258.                         debug_o = 1;
  259.                         break;
  260.                     case 'v':
  261.                         debug_v = 1;
  262.                         break;
  263.                     default:
  264.                         fprintf (stderr, "%s: unknown debug option %c\n",
  265.                                  prgname, *p_opt);
  266.                         usage (1);
  267.                     }
  268.                     continue;
  269. #endif
  270.                 case '?':
  271.                     usage (0);
  272.  
  273.                 case '1':
  274.                     if (isdigit (p_opt [1])) {
  275.                         fontsize = 10 + *++p_opt - '0';
  276.                         continue;
  277.                     }
  278.                     /* NO BREAK */
  279.                 default:
  280.                     fprintf (stderr,"%s: Unknown option %c\n",prgname,*p_opt);
  281.                     usage (1);
  282.                 }
  283.                 break;
  284.             }
  285.         }
  286. }
  287.  
  288.  
  289. void usage (int exitcode)
  290. {
  291.     int i;
  292.  
  293.     printf ("tr2latex (c) 1986/1987 Kamal Al-Yahya, 1991 C. Engel\nVersion %s\n",
  294.             version);
  295.     for (i=0; usage_doc [i]; i++)
  296.         printf ("%s\n", usage_doc [i]);
  297.     exit (exitcode);
  298. }
  299.     
  300.  
  301. void errexit (int exitcode)
  302. {
  303.     fprintf (stderr, "%s: Error #%03d ", prgname, exitcode);
  304.     exit (exitcode);
  305. }
  306.  
  307.  
  308.