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

  1. /* texexpand: to expand TeX and LaTeX \input and include files
  2.  * to compile:   cc texexpand.c -o texexpand 
  3.  */
  4.  
  5. char *documentation[] = {
  6. " SYNTAX",
  7. "        texexpand [-w] file1 [file2 .....]",
  8. "     or texexpand [-w] < file1 [file2 ....]",
  9. "",
  10. "        Flags:",
  11. "              -w    maching is not checked",
  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 character in a document */
  26.  
  27. struct sgttyb ttystat;
  28. extern char *strcpy(), *mktemp();
  29. char scratch_file[100];
  30.  
  31. int wflag;
  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,w;
  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 'w':
  67.                     wflag=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 (wflag != 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.         fprintf(stderr,"Checking matching...\n");
  93.         TeXMatch(scr);
  94.         fseek(scr,0,0);
  95.         TeXExpand(scr,big,MAXLEN);
  96.         fprintf(stderr,"Checking matching done\n\n");
  97.         fclose(scr);
  98.         }
  99.     else
  100.         TeXExpand(stdin,big,MAXLEN);
  101.  
  102.     fprintf(stdout,"%s\n",big);
  103.     }
  104.  
  105. /* then process input line for arguments and assume they are input files */
  106. xargc = argc;
  107. xargv = argv;
  108. for (xargc--,xargv++; xargc; xargc--,xargv++)
  109.     {
  110.     cptr = *xargv; 
  111.     if((temp=fopen(cptr,"r")) != NULL)
  112.         {
  113.         if (wflag != 1)
  114.             {
  115.             fprintf(stderr,"Checking matching...\n");
  116.             fprintf(stderr,"%s:\n",cptr);
  117.             TeXMatch(temp);
  118.             fprintf(stderr,"\n");
  119.             fseek(temp,0,0);
  120.             }
  121.         TeXExpand(temp,big,MAXLEN);
  122.         if (wflag != 1)
  123.             fprintf(stderr,"Checking matching done\n\n");
  124.         fprintf(stdout,"%s",big);
  125.         fclose(temp);
  126.         }
  127.     else
  128.         fprintf(stderr,"texexpand: Cannot open %s\n",cptr);
  129.     }
  130.  
  131. }
  132.