home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / textools / part02 / texexpand2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.8 KB  |  184 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] [parameters] [inputfiles]",
  8. "",
  9. "        flags:",
  10. "              -w   does not check matching",
  11. "",
  12. "        parameters:",
  13. "              in=filename       filename is the input file",
  14. "                                (Default: in=stdin)",
  15. "",
  16. "              out=filename      filename is the output file",
  17. "                                (Default: out=stdout)",
  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 character 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. FILE *out_file;
  36.  
  37. int wflag;
  38. int xargc;
  39. char **xargv;
  40.  
  41. main(argc,argv)
  42. int argc; 
  43. char *argv[];
  44. {
  45. char big[MAXLEN];
  46. FILE *temp,*scr;
  47. register char *cptr;
  48. int piped_in;
  49. int i,w;
  50.  
  51. /* If no arguments, and not in a pipeline, self document */
  52. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  53. if (argc == 1 && !piped_in)
  54.     {
  55.     for( i=0; i<doclength; i++)
  56.         printf("%s\n",documentation[i]);
  57.     exit (0);
  58.     }
  59.  
  60. out_file=stdout;            /* default standard output */
  61.  
  62. /* process option flags */
  63. xargc = argc;
  64. xargv = argv;
  65. for (xargc--,xargv++; xargc; xargc--,xargv++)
  66.     {
  67.     cptr = *xargv; 
  68.     if( *cptr=='-' )
  69.         {
  70.         while( *(++cptr))
  71.             {
  72.             switch( *cptr )
  73.                 {
  74.                 case 'w':
  75.                     wflag=1;
  76.                     break;
  77.                 default:
  78.                          fprintf(stderr,
  79.                     "unknown flag -%c\n",*cptr);
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86. /* process getpar parameters */
  87. xargc = argc;
  88. xargv = argv;
  89.  
  90. if(getpar_("out","s",string))
  91.     {
  92.     sscanf(string,"%s",filename);
  93.     if((temp=fopen(filename,"w")) == NULL)
  94.         fprintf(stderr,"texexpand: Cannot open output file %s\n",filename);
  95.     else
  96.         out_file = temp;
  97.     }
  98.  
  99.  
  100. /* first process pipe input */
  101. if(piped_in)
  102.     {
  103.     if (wflag != 1)
  104.         {
  105. /* need to buffer; can's seek in pipes */
  106. /* make a temporary and volatile file in /tmp */
  107.         strcpy(scratch_file,"/tmp/texXXXXXX");
  108.         mktemp(scratch_file);
  109.         scr=fopen(scratch_file,"w");
  110.         scrbuf(stdin,scr);
  111.         fclose(scr);
  112.         scr=fopen(scratch_file,"r");
  113.         unlink(scratch_file);
  114.         fprintf(stderr,"Checking matching...\n");
  115.         TeXMatch(scr);
  116.         fseek(scr,0,0);
  117.         TeXExpand(scr,big,MAXLEN);
  118.         fprintf(stderr,"Checking matching done\n\n");
  119.         fclose(scr);
  120.         }
  121.     else
  122.         TeXExpand(stdin,big,MAXLEN);
  123.  
  124.     fprintf(stdout,"%s\n",big);
  125.     }
  126.  
  127. /* next process in=inputfiles */
  128. if(getpar_("in","s",string))
  129.     {
  130.     sscanf(string,"%s",filename);
  131.     if((temp=fopen(filename,"r")) != NULL)
  132.         {
  133.         if (wflag != 1)
  134.             {
  135.             fprintf(stderr,"Checking matching...\n");
  136.             fprintf(stderr,"%s:\n",cptr);
  137.             TeXMatch(temp);
  138.             fprintf(stderr,"\n");
  139.             fseek(temp,0,0);
  140.             }
  141.         TeXExpand(temp,big,MAXLEN);
  142.         if (wflag != 1)
  143.             fprintf(stderr,"Checking matching done\n\n");
  144.         fprintf(stdout,"%s",big);
  145.         fclose(temp);
  146.         }
  147.         else
  148.             fprintf(stderr,"texexpand: Cannot open %s\n",filename);
  149.     }
  150.  
  151. /* then process input line for arguments and assume they are input files */
  152. for (xargc--,xargv++; xargc; xargc--,xargv++)
  153.     {
  154.     cptr = *xargv; 
  155.     if( *cptr=='-' ) continue;    /* this is a flag */
  156.     while (*cptr)
  157.         {
  158.         if (*cptr == '=')  break; /* this is for getpar */
  159.         cptr++;
  160.         }       
  161.     if (*cptr)  continue;
  162.     cptr = *xargv;
  163.     if((temp=fopen(cptr,"r")) != NULL)
  164.         {
  165.         if (wflag != 1)
  166.             {
  167.             fprintf(stderr,"Checking matching...\n");
  168.             fprintf(stderr,"%s:\n",cptr);
  169.             TeXMatch(temp);
  170.             fprintf(stderr,"\n");
  171.             fseek(temp,0,0);
  172.             }
  173.         TeXExpand(temp,big,MAXLEN);
  174.         if (wflag != 1)
  175.             fprintf(stderr,"Checking matching done\n\n");
  176.         fprintf(stdout,"%s",big);
  177.         fclose(temp);
  178.         }
  179.     else
  180.         fprintf(stderr,"texexpand: Cannot open %s\n",cptr);
  181.     }
  182.  
  183. }
  184.