home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / textools / part02 / texmatch2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.1 KB  |  157 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] [parameters] [inputfiles]",
  11. "",
  12. "        flags:",
  13. "              -i   ignores TeX's and LaTeX's \input and \include commands",
  14. "",
  15. "        parameters:",
  16. "              in=filename       filename is the input file",
  17. "                                (Default: in=stdin)",
  18. ""
  19. };
  20.  
  21. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  22. /* Modified:                        6/30/86 */
  23.  
  24. int    doclength = { sizeof documentation/sizeof documentation[0] };
  25.  
  26. #include        <stdio.h>
  27. #include        <sys/ioctl.h>
  28. #include        <sgtty.h>
  29. #define    MAXLEN    100000        /* maximum characters in a document */
  30.  
  31. char string[100],filename[100];
  32. struct sgttyb ttystat;
  33. extern char *strcpy(), *mktemp();
  34. char scratch_file[100];
  35.  
  36. int wflag=0;        /* for consistency with other programs */
  37. int xargc;
  38. char **xargv;
  39.  
  40. main(argc,argv)
  41. int argc; 
  42. char *argv[];
  43. {
  44. char big[MAXLEN];
  45. FILE *temp,*scr;
  46. register char *cptr;
  47. int piped_in;
  48. int i,iflag;
  49.  
  50. /* If no arguments, and not in a pipeline, self document */
  51. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  52. if (argc == 1 && !piped_in)
  53.     {
  54.     for( i=0; i<doclength; i++)
  55.         printf("%s\n",documentation[i]);
  56.     exit (0);
  57.     }
  58.  
  59. /* process option flags */
  60. xargc = argc;
  61. xargv = argv;
  62. for (xargc--,xargv++; xargc; xargc--,xargv++)
  63.     {
  64.     cptr = *xargv; 
  65.     if( *cptr=='-' )
  66.         {
  67.         while( *(++cptr))
  68.             {
  69.             switch( *cptr )
  70.                 {
  71.                 case 'i':
  72.                     iflag=1;
  73.                     break;
  74.                 default:
  75.                          fprintf(stderr,
  76.                     "unknown flag -%c\n",*cptr);
  77.                     break;
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83. /* first process pipe input */
  84. xargc = argc;
  85. xargv = argv;
  86. if(piped_in)
  87.     {
  88.     if (iflag != 1)
  89.         {
  90. /* need to buffer; can's seek in pipes */
  91. /* make a temporary and volatile file in /tmp */
  92.         strcpy(scratch_file,"/tmp/texXXXXXX");
  93.         mktemp(scratch_file);
  94.         scr=fopen(scratch_file,"w");
  95.         scrbuf(stdin,scr);
  96.         fclose(scr);
  97.         scr=fopen(scratch_file,"r");
  98.         unlink(scratch_file);
  99.         TeXMatch(scr);
  100.         fseek(scr,0,0);
  101.         TeXExpand(scr,big,MAXLEN);
  102.         fclose(scr);
  103.         }
  104.     else
  105.         TeXMatch(stdin);
  106.     }
  107.  
  108. /* next process in=inputfiles */
  109. if(getpar_("in","s",string))
  110.     {
  111.     sscanf(string,"%s",filename);
  112.     if((temp=fopen(filename,"r")) != NULL)
  113.         {
  114.         fprintf(stderr,"%s:\n",cptr);
  115.         TeXMatch(temp);
  116.         fprintf(stderr,"\n");
  117.         if (iflag != 1)
  118.             {
  119.             fseek(temp,0,0);
  120.             TeXExpand(temp,big,MAXLEN);
  121.             }
  122.         fclose(temp);
  123.         }
  124.     else
  125.         fprintf(stderr,"texmatch: Cannot open %s\n",filename);
  126.     }
  127.  
  128. /* then process input line for arguments and assume they are input files */
  129. for (xargc--,xargv++; xargc; xargc--,xargv++)
  130.     {
  131.     cptr = *xargv; 
  132.     if( *cptr=='-' ) continue;        /* this is a flag */
  133.     while (*cptr)
  134.         {
  135.         if (*cptr == '=')  break; /* this is for getpar */
  136.         cptr++;
  137.         }       
  138.     if (*cptr)  continue;
  139.     cptr = *xargv;
  140.     if((temp=fopen(cptr,"r")) != NULL)
  141.         {
  142.         fprintf(stderr,"%s:\n",cptr);
  143.         TeXMatch(temp);
  144.         fprintf(stderr,"\n");
  145.         if (iflag != 1)
  146.             {
  147.             fseek(temp,0,0);
  148.             TeXExpand(temp,big,MAXLEN);
  149.             }
  150.         fclose(temp);
  151.         }
  152.     else
  153.         fprintf(stderr,"texmatch: Cannot open %s\n",cptr);
  154.     }
  155.  
  156. }
  157.