home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / tr2latex / tr2tex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-30  |  4.4 KB  |  186 lines

  1. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  2.  
  3. /* tr2tex: troff to tex translator */
  4. /* Author: Kamal Al-Yahya, Stanford University,        9/4/86 */
  5. /* Last modified:                    1/1/87 */
  6. /* Keyword: convert translate tex troff */
  7.  
  8. char *documentation[] = {
  9. " SYNTAX",
  10. "        tr2tex [-m] file1 file2 ...",
  11. "or",
  12. "        tr2tex [-m] < file1 file2 ...",
  13. "",
  14. " Use the -m flag for manual",
  15. "",
  16. };
  17.  
  18. int    doclength = { sizeof documentation/sizeof documentation[0] };
  19.  
  20. #include        "setups.h"
  21.  
  22. #ifdef tops20
  23. #define TEMPFILE "texXXXXXX"
  24. #else
  25. #define TEMPFILE "/tmp/texXXXXXX"
  26. #endif
  27.  
  28. FILE *out_file;
  29.  
  30. #if HAVE_SGTTY
  31. struct sgttyb ttystat;
  32. #endif
  33. extern char *mktemp();
  34. char scratch_file[MAXWORD];
  35.  
  36. int man;
  37. int xargc;
  38. char **xargv;
  39.  
  40. int
  41. main(argc,argv)
  42. int argc; 
  43. char *argv[];
  44. {
  45.  
  46. char *inbuf, *outbuf;
  47.  
  48. FILE *temp,*scr;
  49. register char *cptr;
  50. int piped_in;
  51. int i;
  52. long timeval;        /* clock value from time() for ctime()    */
  53. char *document = "article";            /* document type */
  54. char *options = "[troffms,11pt]";        /* style options */
  55.  
  56. /* Allocate large arrays dynamically to conserve stack space */
  57.  
  58. if (((inbuf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL) ||
  59.     ((outbuf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  60.     {
  61.         fprintf(stderr,"tr2tex: Cannot malloc() internal buffer space\n\
  62. Need two arrays of %d characters each\n",MAXLEN);
  63.     exit(-1);
  64.     }
  65.  
  66. /* If no arguments, and not in a pipeline, self document */
  67. #if HAVE_SGTTY
  68. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  69. #else    /* if no sggty, it cannot distinguish piped input from no input */
  70. piped_in = (argc == 1);
  71. #endif
  72.  
  73. if (argc == 1 && !piped_in)
  74.     {
  75.     for( i=0; i<doclength; i++)
  76.         printf("%s\n",documentation[i]);
  77.     exit (0);
  78.     }
  79.  
  80. /* initialize spacing and indentation parameters */
  81. strcpy(linespacing.def_units,"\\normalbaselineskip");
  82. strcpy(linespacing.old_units,"\\normalbaselineskip");
  83. strcpy(indent.def_units,"em");        strcpy(indent.old_units,"em");
  84. strcpy(tmpind.def_units,"em");        strcpy(tmpind.old_units,"em");
  85. strcpy(space.def_units,"\\baselineskip");
  86. strcpy(space.old_units,"\\baselineskip");
  87. strcpy(vspace.def_units,"pt");        strcpy(vspace.old_units,"pt");
  88. linespacing.value = 1.;            linespacing.old_value = 1.;
  89. indent.value = 0.;            indent.old_value = 0.;
  90. tmpind.value = 0.;            tmpind.old_value = 0.;
  91. space.value = 1.;            space.old_value = 1.;
  92. vspace.value = 1.;            vspace.old_value = 1.;
  93. linespacing.def_value = 0;
  94. indent.def_value = 0;
  95. tmpind.def_value = 0;
  96. space.def_value = 1;
  97. vspace.def_value = 1;
  98.  
  99. out_file = stdout;        /* default output */
  100. math_mode = 0;            /* start with non-math mode */
  101. de_arg = 0;            /* not a .de argument */
  102.  
  103. /* process option flags */
  104. xargc = argc;
  105. xargv = argv;
  106. for (xargc--,xargv++; xargc; xargc--,xargv++)
  107.     {
  108.     cptr = *xargv; 
  109.     if( *cptr=='-' )
  110.         {
  111.         while( *(++cptr))
  112.             {
  113.             switch( *cptr )
  114.                 {
  115.                 case 'm':
  116.                     man = 1;
  117.                     strcpy(options,"[troffman]");
  118.                     break;
  119.                 default:
  120.                          fprintf(stderr,
  121.                         "tr2tex: unknown flag -%c\n",*cptr);
  122.                     break;
  123.                 }
  124.             }
  125.         }
  126.     }
  127. /* start of translated document */
  128.  
  129. timeval = time(0);
  130. fprintf(out_file,"%% -*-LaTeX-*-\n\
  131. %% Converted automatically from troff to LaTeX by tr2tex on %s",ctime(&timeval));
  132. fprintf(out_file,"%% tr2tex was written by Kamal Al-Yahya at Stanford University\n\
  133. %% (Kamal%%Hanauma@SU-SCORE.ARPA)\n\n\n");
  134.  
  135. /* document style and options */
  136. fprintf(out_file,"\\documentstyle%s{%s}\n\\begin{document}\n",options,document);
  137.  
  138. /* first process pipe input */
  139. if(piped_in)
  140.     {
  141. /* need to buffer; can't seek in pipes */
  142. /* make a temporary and volatile file */
  143.     strcpy(scratch_file,TEMPFILE);
  144.     mktemp(scratch_file);
  145.     if ((scr=fopen(scratch_file,"w")) == (FILE *)NULL)
  146.         {
  147.         fprintf(stderr,
  148.             "tr2tex: Cannot open scratch file [%s]\n",scratch_file);
  149.         exit(-1);
  150.         }
  151.     scrbuf(stdin,scr);
  152.     fclose(scr);
  153.     scr=fopen(scratch_file,"r");
  154.     unlink(scratch_file);
  155.     tmpbuf(scr,inbuf);
  156.     fclose(scr);
  157.     troff_tex(inbuf,outbuf,0,0);
  158.     fprintf(out_file,"%%\n%% input file: stdin\n%%\n");
  159.     fputs(outbuf,out_file);
  160.     }
  161.  
  162. /* then process input line for arguments and assume they are input files */
  163. xargc = argc;
  164. xargv = argv;
  165.  
  166. for (xargc--,xargv++; xargc; xargc--,xargv++)
  167.     {
  168.     cptr = *xargv; 
  169.     if( *cptr=='-' ) continue; /* this is a flag */
  170.     if((temp=fopen(cptr,"r")) != (FILE *)NULL)
  171.         {
  172.         tmpbuf(temp,inbuf);
  173.         fclose(temp);
  174.         troff_tex(inbuf,outbuf,0,0);
  175.         fprintf(out_file,"%%\n%% input file: %s\n%%\n",cptr);
  176.         fputs(outbuf,out_file);
  177.         }
  178.     else
  179.         fprintf(stderr,"tr2tex: Cannot open %s\n",cptr);
  180.     }
  181. /* close translated document */
  182. fputs("\\end{document}\n",out_file);
  183.  
  184. exit(0);
  185. }
  186.