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

  1. /* expand TeX and LaTeX's \input and \include */
  2.  
  3. #include        <stdio.h>
  4. TeXExpand(fp,big,maxlen)
  5.  
  6. FILE *fp;
  7. char *big;
  8. int maxlen;
  9. {
  10. char bigg[50000];
  11. FILE *fpp;
  12. int c,c1;
  13. char w[100];
  14. int i,j;
  15. extern wflag;
  16.  
  17. while ((c = getc(fp)) != EOF)
  18.     {
  19.     if (c == '%' || c1 == '%')
  20.         {
  21.         while ((c =getc(fp)) != EOF)
  22.             if (c == '\n')
  23.                 {
  24.                 *big++=c;
  25.                 break;
  26.                 }
  27.         c1=c;
  28.         continue;
  29.         }
  30.     if (c != '\\')
  31.         *big++=c;
  32.     else            /* detect TeX commands (backslash) */
  33.         {
  34.         /* see if \input or \include is the control sequence */
  35.         i=0;
  36.         c1=c;        /* update last character */
  37.         while ((c = getc(fp)) != EOF)
  38.             {
  39.             if(c == ' ' || c=='\n' || c=='$' || c=='#' || c=='%'
  40.             || c=='{' || c=='(' || c==')' || c == '\\') break;
  41.             w[i++]=c;
  42.             }
  43.             if(w[1]=='n' && w[2]=='p' && w[3]=='u' && w[4]=='t'
  44.                || w[1]=='n' && w[2]=='c' && w[3]=='l' && w[4]=='u'
  45.                && w[5]=='d' && w[6]=='e')
  46.                     {
  47. /* if it is \input or \include , get the file name */
  48.                     i=0;
  49.                     while (( c = getc(fp)) != EOF)
  50.                         {
  51.                         if (c==' ' || c=='\n' || c=='}')
  52.                             break;
  53.                         w[i++]=c;
  54.                         }
  55.                     w[i]='\0';
  56.                     fpp=fopen(w, "r"); /* open the new file */
  57.                     if( fpp == NULL )
  58.                          {
  59. /* if file is not found, try file.tex  */
  60.                          strcat(w,".tex");
  61.                          fpp=fopen(w, "r");
  62.                          if( fpp == NULL )
  63.                         {
  64.                         fprintf(stderr,
  65.                         "TeXExpand: Cannot open %s\n",w);
  66.                         bigg[0]='\0';
  67.                         }
  68.                          else
  69.                         {
  70.                         if (wflag != 1)
  71.                         {
  72.                         fprintf(stderr,"%s:\n",w);
  73.                         TeXMatch(fpp);
  74.                         fprintf(stderr,"\n");
  75.                         fseek(fpp,0,0);
  76.                         }
  77.                         TeXExpand(fpp,bigg,maxlen);
  78.                         fclose(fpp);
  79.                         }
  80.                          }
  81.                     else
  82.                         {
  83.                         if (wflag != 1)
  84.                         {
  85.                         fprintf(stderr,"%s:\n",w);
  86.                         TeXMatch(fpp);
  87.                         fprintf(stderr,"\n");
  88.                         fseek(fpp,0,0);
  89.                         }
  90.                         TeXExpand(fpp,bigg,maxlen);
  91.                         fclose(fpp);
  92.                         }
  93.                     strcat(big,bigg);
  94.                     big += strlen(bigg);
  95.                     w[0]='\0';
  96.                     }
  97.                 else
  98. /* if the control sequence is not \input or \include write it out */
  99.                     {
  100.                     *big++='\\';
  101.                     for (j=0; j < i; j++)
  102.                         *big++=w[j];
  103. /* do not write commented text */
  104.                     if (c1 != '\\' && c != '%')
  105.                         *big++=c;
  106.                     else
  107.                         if (c1 == '\\')
  108.                             {
  109.                             *big++=c;
  110.                             c=' ';
  111.                             }
  112.                     }
  113.         }
  114.     c1=c;                /* update last character */
  115.     }
  116. *big++ = '\0';
  117. }
  118.