home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_01 / doc2tex.c < prev    next >
Text File  |  1991-02-05  |  5KB  |  234 lines

  1. /*
  2.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  3.  * Also will work for VMS .HLP files. 
  4.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  5.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  6.  *
  7.  * usage:  doc2tex < file.doc > file.tex
  8.  *
  9.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  10.  *     article document suitable for printing with LaTeX.
  11.  *
  12.  * typical usage for GNUPLOT:
  13.  *
  14.  *   doc2tex < gnuplot.doc > gnuplot.tex 
  15.  *   latex gnuplot.tex ; latex gnuplot.tex
  16.  */
  17.  
  18. static char rcsid[] = "$Id: doc2tex.c,v 1.3 90/12/19 10:01:14 dfk Patch2 $";
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22.  
  23. #define MAX_NAME_LEN    256
  24. #define MAX_LINE_LEN    256
  25. #define TRUE 1
  26. #define FALSE 0
  27.  
  28. typedef int boolean;
  29.  
  30. boolean intable = FALSE;
  31. boolean verb = FALSE;
  32.  
  33. main()
  34. {
  35.     init(stdout);
  36.     convert(stdin,stdout);
  37.     finish(stdout);
  38.     exit(0);
  39. }
  40.  
  41.  
  42. init(b)
  43. FILE *b;
  44. {
  45.     (void) fputs("\\input{titlepage.tex}\n",b);
  46. }
  47.  
  48.  
  49. convert(a,b)
  50.     FILE *a,*b;
  51. {
  52.     static char line[MAX_LINE_LEN];
  53.  
  54.     while (fgets(line,MAX_LINE_LEN,a)) {
  55.        process_line(line, b);
  56.     }
  57. }
  58.  
  59. process_line(line, b)
  60.     char *line;
  61.     FILE *b;
  62. {
  63.     switch(line[0]) {        /* control character */
  64.        case '?': {            /* interactive help entry */
  65.           break;            /* ignore */
  66.        }
  67.        case '@': {            /* start/end table */
  68.           if (intable) {
  69.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  70.              (void) fputs("\\end{center}\n",b);
  71.              intable = FALSE;
  72.           } else {
  73.              if (verb) {
  74.                 (void) fputs("\\end{verbatim}\n",b);
  75.                 verb=FALSE;
  76.              } 
  77.              (void) fputs("\n\\begin{center}\n", b);
  78.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  79.              intable = TRUE;
  80.           }
  81.           /* ignore rest of line */
  82.           break;
  83.        }
  84.        case '#': {            /* latex table entry */
  85.           if (intable)
  86.             (void) fputs(line+1, b); /* copy directly */
  87.           else
  88.             fprintf(stderr, "error: # line found outside of table\n");
  89.           break;
  90.        }
  91.        case '%': {            /* troff table entry */
  92.           break;            /* ignore */
  93.        }
  94.        case '\n':            /* empty text line */
  95.        case ' ': {            /* normal text line */
  96.           if (intable)
  97.             break;        /* ignore while in table */
  98.           if (line[1] == ' ') {
  99.              /* verbatim mode */
  100.              if (!verb) {
  101.                 (void) fputs("\\begin{verbatim}\n",b);
  102.                 verb=TRUE;
  103.              }
  104.              (void) fputs(line+1,b); 
  105.           } else {
  106.              if (verb) {
  107.                 (void) fputs("\\end{verbatim}\n",b);
  108.                 verb=FALSE;
  109.              } 
  110.              if (line[0] == '\n')
  111.                puttex(line,b); /* handle totally blank line */
  112.              else
  113.                puttex(line+1,b);
  114.           }
  115.           break;
  116.        }
  117.        default: {
  118.           if (isdigit(line[0])) { /* start of section */
  119.              if (!intable)    /* ignore while in table */
  120.                section(line, b);
  121.           } else
  122.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  123.                   line[0]);
  124.           break;
  125.        }
  126.     }
  127. }
  128.  
  129. /* process a line with a digit control char */
  130. /* starts a new [sub]section */
  131.  
  132. section(line, b)
  133.     char *line;
  134.     FILE *b;
  135. {
  136.     static char string[MAX_LINE_LEN];
  137.     int sh_i;
  138.  
  139.     if (verb) {
  140.        (void) fputs("\\end{verbatim}\n",b);
  141.        verb=FALSE;
  142.     } 
  143.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  144.     switch(sh_i)
  145.      {
  146.         case 1: 
  147.         (void) fprintf(b,"\\section{");
  148.         break;
  149.         case 2: 
  150.         (void) fprintf(b,"\\section{");
  151.         break;
  152.         case 3:
  153.         (void) fprintf(b,"\\subsection{");
  154.         break;
  155.         case 4: 
  156.         (void) fprintf(b,"\\subsubsection{");
  157.         break;
  158.         default:
  159.         case 5: 
  160.         (void) fprintf(b,"\\paragraph{");
  161.         break;
  162.      }
  163.     if (islower(string[0]))
  164.      string[0] = toupper(string[0]);
  165.     puttex(string,b);
  166.     (void) fprintf(b,"}\n");
  167. }
  168.  
  169. /* put text in string str to file while buffering special TeX characters */
  170. puttex(str,file)
  171. FILE *file;
  172. register char *str;
  173. {
  174. register char ch;
  175. static boolean inquote = FALSE;
  176.  
  177.      while( (ch = *str++) != '\0') {
  178.          switch(ch) {
  179.              case '#':
  180.              case '$':
  181.              case '%':
  182.              case '&':
  183.              case '_':
  184.              case '{':
  185.              case '}':
  186.                  (void) fputc('\\',file);
  187.                  (void) fputc(ch,file);
  188.                  break;
  189.              case '\\':
  190.                  (void) fputs("$\\backslash$",file);
  191.                  break;
  192.              case '~':
  193.                  (void) fputs("\\~{\\ }",file);
  194.                  break;
  195.              case '^':
  196.                  (void) fputs("\\verb+^+",file);
  197.                  break;
  198.              case '>':
  199.              case '<':
  200.              case '|':
  201.                  (void) fputc('$',file);
  202.                  (void) fputc(ch,file);
  203.                  (void) fputc('$',file);
  204.                  break;
  205.              case '"': 
  206.                  /* peek at next character: if space, end of quote */
  207.                  if (*str == NULL || isspace(*str) || ispunct(*str))
  208.                    (void) fputs("''", file);
  209.                  else
  210.                    (void) fputs("``", file);
  211.                  break;
  212.              case '`':    /* backquotes mean boldface */
  213.                  if (inquote) {
  214.                     fputs("}", file);
  215.                     inquote = FALSE;
  216.                  } else {
  217.                     fputs("{\\bf ", file);
  218.                     inquote = TRUE;
  219.                  }
  220.                  break;
  221.              default:
  222.                  (void) fputc(ch,file);
  223.                  break;
  224.          }
  225.      }
  226. }
  227.  
  228.  
  229. finish(b)
  230. FILE *b;
  231. {
  232.     (void) fputs("\\end{document}\n",b);
  233. }
  234.