home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / textools / part02 / texmatch1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.4 KB  |  125 lines

  1. /*
  2.  * texmatch: checks matching parantheses, braces, brackets, and dollar signs
  3.  * in TeX documents.
  4.  *
  5.  * to compile:  cc texmatch.c -lsep -o texmatch
  6.  */
  7.  
  8. char *documentation[] = {
  9. " SYNTAX",
  10. "        texmatch [-i] file1 [file2 .....]",
  11. "     or texmatch [-i]  < file1 [file2 ....]",
  12. "",
  13. "See the manual page for more details.",
  14. "",
  15. };
  16.  
  17. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  18. /* Modified:                        6/30/86 */
  19.  
  20. int    doclength = { sizeof documentation/sizeof documentation[0] };
  21.  
  22. #include        <stdio.h>
  23. #include        <sys/ioctl.h>
  24. #include        <sgtty.h>
  25. #define    MAXLEN    100000        /* maximum characters in a document */
  26.  
  27. struct sgttyb ttystat;
  28. extern char *strcpy(), *mktemp();
  29. char scratch_file[100];
  30.  
  31. int wflag=0;        /* for consistency with other programs */
  32. int xargc;
  33. char **xargv;
  34.  
  35. main(argc,argv)
  36. int argc; 
  37. char *argv[];
  38. {
  39. char big[MAXLEN];
  40. FILE *temp,*scr;
  41. register char *cptr;
  42. int piped_in;
  43. int i,iflag;
  44.  
  45. /* If no arguments, and not in a pipeline, self document */
  46. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  47. if (argc == 1 && !piped_in)
  48.     {
  49.     for( i=0; i<doclength; i++)
  50.         printf("%s\n",documentation[i]);
  51.     exit (0);
  52.     }
  53.  
  54. /* process option flags */
  55. xargc = argc;
  56. xargv = argv;
  57. for (xargc--,xargv++; xargc; xargc--,xargv++)
  58.     {
  59.     cptr = *xargv; 
  60.     if( *cptr=='-' )
  61.         {
  62.         while( *(++cptr))
  63.             {
  64.             switch( *cptr )
  65.                 {
  66.                 case 'i':
  67.                     iflag=1;
  68.                     break;
  69.                 default:
  70.                          fprintf(stderr,
  71.                     "unknown flag -%c\n",*cptr);
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78. /* first process pipe input */
  79. if(piped_in)
  80.     {
  81.     if (iflag != 1)
  82.         {
  83. /* need to buffer; can's seek in pipes */
  84. /* make a temporary and volatile file in /tmp */
  85.         strcpy(scratch_file,"/tmp/texXXXXXX");
  86.         mktemp(scratch_file);
  87.         scr=fopen(scratch_file,"w");
  88.         scrbuf(stdin,scr);
  89.         fclose(scr);
  90.         scr=fopen(scratch_file,"r");
  91.         unlink(scratch_file);
  92.         TeXMatch(scr);
  93.         fseek(scr,0,0);
  94.         TeXExpand(scr,big,MAXLEN);
  95.         fclose(scr);
  96.         }
  97.     else
  98.         TeXMatch(stdin);
  99.     }
  100.  
  101. /* then process input line for arguments and assume they are input files */
  102. xargc = argc;
  103. xargv = argv;
  104. for (xargc--,xargv++; xargc; xargc--,xargv++)
  105.     {
  106.     cptr = *xargv; 
  107.     if( *cptr=='-' ) continue;        /* this is a flag */
  108.     if((temp=fopen(cptr,"r")) != NULL)
  109.         {
  110.         fprintf(stderr,"%s:\n",cptr);
  111.         TeXMatch(temp);
  112.         fprintf(stderr,"\n");
  113.         if (iflag != 1)
  114.             {
  115.             fseek(temp,0,0);
  116.             TeXExpand(temp,big,MAXLEN);
  117.             }
  118.         fclose(temp);
  119.         }
  120.     else
  121.         fprintf(stderr,"texmatch: Cannot open %s\n",cptr);
  122.     }
  123.  
  124. }
  125.