home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d671 / tr2tex.lha / tr2tex / tr2tex.zoo / tr2tex.c (.txt) < prev    next >
LaTeX Document  |  1990-12-07  |  5KB  |  153 lines

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