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

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