home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / textools2 / part01 / detex2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-12  |  4.5 KB  |  222 lines

  1. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  2. /* detex: strips TeX's and LaTeX's commands */
  3.  
  4.  
  5. char *documentation[] = {
  6. " SYNTAX",
  7. "        detex [-iw] [parameters] [inputfiles]",
  8. "",
  9. "        flags:",
  10. "              -i   ignores TeX's and LaTeX's \input and \include commands",
  11. "              -w   does not check matching",
  12. "",
  13. "        parameters:",
  14. "              in=filename       filename is the input file",
  15. "                                (Default: in=stdin)",
  16. "",
  17. "              out=filename      filename is the output file",
  18. "                                (Default: out=stdout)",
  19. ""
  20. };
  21.  
  22. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  23. /* Last modified:                    1/25/87 */
  24.  
  25. int    doclength = { sizeof documentation/sizeof documentation[0] };
  26.  
  27. #include        "setups.h"
  28.  
  29. #ifdef tops20
  30. #define TEMPFILE "texXXXXXX"
  31. #else
  32. #define TEMPFILE "/tmp/texXXXXXX"
  33. #endif
  34.  
  35. char string[MAXWORD], filename[MAXWORD], scratch_file[MAXWORD];
  36. FILE *out_file;
  37. extern char *mktemp();
  38.  
  39. #ifdef MSC
  40. #else
  41. struct sgttyb ttystat;
  42. #endif
  43.  
  44. int wflag;
  45. int xargc;
  46. char **xargv;
  47.  
  48. main(argc,argv)
  49. int argc; 
  50. char *argv[];
  51. {
  52. char *buf;
  53. FILE *temp,*scr;
  54. register char *cptr;
  55. int piped_in;
  56. int iflag,i;
  57.  
  58. if (((buf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  59.     {
  60.         fprintf(stderr,"detex: Cannot malloc() internal buffer space\n\
  61. Need an array of %d characters\n",MAXLEN);
  62.     exit(-1);
  63.     }
  64.  
  65. /* If no arguments, and not in a pipeline, self document */
  66. #ifdef MSC    /* MS-DOS cannot distinguish piped input from no input */
  67. piped_in = (argc == 1);
  68. #else
  69. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  70. #endif
  71. if (argc == 1 && !piped_in)
  72.     {
  73.     for( i=0; i<doclength; i++)
  74.         printf("%s\n",documentation[i]);
  75.     exit (0);
  76.     }
  77.  
  78. out_file = stdout;        /* default output */
  79.  
  80. /* process option flags */
  81. xargc = argc;
  82. xargv = argv;
  83. for (xargc--,xargv++; xargc; xargc--,xargv++)
  84.     {
  85.     cptr = *xargv; 
  86.     if( *cptr=='-' )
  87.         {
  88.         while( *(++cptr))
  89.             {
  90.             switch( *cptr )
  91.                 {
  92.                 case 'i':
  93.                     iflag=1;
  94.                     break;
  95.                 case 'w':
  96.                     wflag=1;
  97.                     break;
  98.                 default:
  99.                          fprintf(stderr,
  100.                         "detex: unknown flag -%c\n",*cptr);
  101.                     break;
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107. /* process getpar parameters */
  108. xargc = argc;
  109. xargv = argv;
  110.  
  111. if(getpar_("out","s",string))
  112.     {
  113.     sscanf(string,"%s",filename);
  114.     if((temp=fopen(filename,"w")) == NULL)
  115.         fprintf(stderr,"detex: Cannot open output file %s\n",filename);
  116.     else
  117.         out_file = temp;
  118.     }
  119.  
  120. /* first process pipe input */
  121. if(piped_in)
  122.     {
  123. /* need to buffer; can't seek in pipes */
  124. /* make a temporary and volatile file in /tmp */
  125.     strcpy(scratch_file,TEMPFILE);
  126.     mktemp(scratch_file);
  127.     if ((scr=fopen(scratch_file,"w")) == (FILE *)NULL)
  128.         {
  129.         fprintf(stderr,
  130.             "detex: Cannot open scratch file [%s]\n",scratch_file);
  131.         exit(-1);
  132.         }
  133.     scrbuf(stdin,scr);
  134.     fclose(scr);
  135.     scr=fopen(scratch_file,"r");
  136.     unlink(scratch_file);
  137.     if (wflag != 1)
  138.         {
  139.         fprintf(stderr,"Checking matching...\n");
  140.         Match(scr);
  141.         fseek(scr,0,0);
  142.         }
  143. /* either expand or buffer */
  144.     if (iflag != 1)
  145.         { Expand(scr,buf);    fclose(scr); }
  146.     else
  147.         { tmpbuf(scr,buf);    fclose(scr); }
  148.     if (wflag != 1)
  149.         fprintf(stderr,"Checking matching done\n\n");
  150.     DeTeX(buf,out_file);
  151.     fclose(scr);
  152.     }
  153.  
  154. /* next process in=inputfiles */
  155. if(getpar_("in","s",string))
  156.     {
  157.     sscanf(string,"%s",filename);
  158.     if((temp=fopen(filename,"r")) != NULL)
  159.         {
  160.         if (wflag != 1)
  161.             {
  162.             fprintf(stderr,"Checking matching...\n");
  163.             fprintf(stderr,"%s:\n",filename);
  164.             Match(temp);
  165.             fprintf(stderr,"\n");
  166.             fseek(temp,0,0);
  167.             }
  168. /* either expand or buffer */
  169.         if (iflag != 1)
  170.             { Expand(temp,buf);    fclose(temp); }
  171.         else
  172.             { tmpbuf(temp,buf);    fclose(temp); }
  173.         if (wflag != 1)
  174.             fprintf(stderr,"Checking matching done\n\n");
  175.         DeTeX(buf,out_file);
  176.         fclose(temp);
  177.         }
  178.     else
  179.         fprintf(stderr,"detex: Cannot open %s\n",filename);
  180.     }
  181.  
  182. /* then process input line for arguments and assume they are input files */
  183. xargc = argc;
  184. xargv = argv;
  185.  
  186. for (xargc--,xargv++; xargc; xargc--,xargv++)
  187.     {
  188.     cptr = *xargv; 
  189.     if( *cptr=='-' ) continue; /* this is a flag */
  190.     while (*cptr)
  191.         {
  192.         if (*cptr == '=')  break; /* this is for getpar */
  193.         cptr++;
  194.         }       
  195.     if (*cptr)  continue;
  196.     cptr = *xargv;
  197.     if((temp=fopen(cptr,"r")) != (FILE *)NULL)
  198.         {
  199.         if (wflag != 1)
  200.             {
  201.             fprintf(stderr,"Checking matching...\n");
  202.             fprintf(stderr,"%s:\n",cptr);
  203.             Match(temp);
  204.             fprintf(stderr,"\n");
  205.             fseek(temp,0,0);
  206.             }
  207. /* either expand or buffer */
  208.         if (iflag != 1)
  209.             { Expand(temp,buf);    fclose(temp); }
  210.         else
  211.             { tmpbuf(temp,buf);    fclose(temp); }
  212.         if (wflag != 1)
  213.             fprintf(stderr,"Checking matching done\n\n");
  214.         DeTeX(buf,out_file);
  215.         fclose(temp);
  216.         }
  217.     else
  218.         fprintf(stderr,"detex: Cannot open %s\n",cptr);
  219.     }
  220.  
  221. }
  222.