home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / textools2 / part01 / Expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-12  |  3.3 KB  |  166 lines

  1. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  2.  
  3. #include    "setups.h"
  4. unsigned int len=0;        /* length of document */
  5.  
  6. Expand(fp,buf)    /* expand TeX and LaTeX's \input and \include */
  7.  
  8. FILE *fp;
  9. char *buf;
  10. {
  11. char *buf2;
  12. FILE *fpp;
  13. int c;
  14. int c1=' ';                /* previous character */
  15. char w[MAXWORD];
  16. int i,j;
  17. extern wflag;
  18.  
  19. if (((buf2 = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  20.     {
  21.         fprintf(stderr,"Expand: Cannot malloc() internal buffer space\n\
  22. Need an arrays of %d characters\n",MAXLEN);
  23.     exit(-1);
  24.     }
  25.  
  26. while ((c = getc(fp)) != EOF)
  27.     {
  28.     if (++len >= MAXLEN)
  29.         {
  30.         fprintf(stderr,"Document is too large\n");
  31.         exit(-1);
  32.         }
  33.     if (c == '%' || c1 == '%')
  34.         {
  35.         *buf++ = c;
  36.         while ((c =getc(fp)) != EOF)
  37.             {
  38.             if (++len >= MAXLEN)
  39.                 {
  40.                 fprintf(stderr,"Sorry: document is too large\n");
  41.                 exit(-1);
  42.                 }
  43.             *buf++=c;
  44.             if (c == '\n')        break;
  45.             }
  46.         c1=c;
  47.         continue;
  48.         }
  49.     if (c != '\\')
  50.         *buf++ = c;
  51.     else            /* detect TeX commands (backslash) */
  52.         {
  53.         /* see if \input or \include is the control sequence */
  54.         i=0;
  55.         c1=c;        /* update last character */
  56.         while ((c = getc(fp)) != EOF && i < MAXWORD)
  57.             {
  58.             if (++len >= MAXLEN)
  59.                 {
  60.                 fprintf(stderr,"Document is too large\n");
  61.                 exit(-1);
  62.                 }
  63.             if (c == ' ' || c=='\n' || c=='$' || c=='#' || c=='%'
  64.                 || c=='{' || c=='(' || c==')' || c == '\\')
  65.                 break;
  66.             w[i++] = (char)c;
  67.             }
  68.         if (strncmp(w,"input",5) == 0 || (strncmp(w,"include",7) == 0
  69.             && strcmp(w,"includeonly") !=0))
  70.             {
  71. /* if it is \input or \include , get the file name */
  72.             i=0;
  73.             while ((c=getc(fp)) != EOF && i < MAXWORD)
  74.                 {
  75.                 if (c == ' ' || c == '\n'
  76.                     || c == '\t' || c == '}' || c == '%')
  77.                     break;
  78.                 w[i++] = (char)c;
  79.                 }
  80.             w[i] = NULL;
  81.             fpp=fopen(w, "r"); /* open the new file */
  82.             if( fpp == NULL )
  83.                 {
  84. /* if file is not found, try file.tex  */
  85.                 strcat(w,".tex");
  86.                 fpp=fopen(w, "r");
  87.                 if( fpp == NULL )
  88.                     {
  89.                     fprintf(stderr,
  90.                     "TeXExpand: Cannot open %s\n",w);
  91.                     buf2[0] = NULL;
  92.                     }
  93.                 else
  94.                     {
  95.                     if (wflag != 1)
  96.                         {
  97.                         fprintf(stderr,"%s:\n",w);
  98.                         Match(fpp);
  99.                         fprintf(stderr,"\n");
  100.                         fseek(fpp,0,0);
  101.                         }
  102.                     Expand(fpp,buf2);
  103.                     fclose(fpp);
  104.                     }
  105.                 }
  106.             else
  107.                 {
  108.                 if (wflag != 1)
  109.                     {
  110.                     fprintf(stderr,"%s:\n",w);
  111.                     Match(fpp);
  112.                     fprintf(stderr,"\n");
  113.                     fseek(fpp,0,0);
  114.                     }
  115.                 Expand(fpp,buf2);
  116.                 fclose(fpp);
  117.                 }
  118.             strcat(buf,buf2);
  119.             buf += strlen(buf2);
  120.             w[0] = NULL;
  121.             }
  122.         else
  123. /* if the control sequence is not \input or \include write it out */
  124.             {
  125. /* if it is \def, \newcommand, or \newenvironment, write the full command */
  126.             if (strncmp(w,"def",3) == 0)
  127.                 {
  128.                 i = def_file(fp,&j,0);
  129.                 fseek(fp,-i,1);
  130.                 strcat(buf,"\\def\\");
  131.                 buf += 5;
  132.                 for (j=0; j < i; j++)
  133.                     *buf++=getc(fp);
  134.                 }
  135.             else if (strncmp(w,"newcommand",10) == 0)
  136.                 {
  137.                 i = comm_file(fp,&j,0);
  138.                 fseek(fp,-i,1);
  139.                 strcat(buf,"\\newcommand{");
  140.                 buf += 12;
  141.                 for (j=0; j < i; j++)
  142.                     *buf++=getc(fp);
  143.                 }
  144.             else if (strncmp(w,"newenvironment",14)==0)
  145.                 {
  146.                 i = getenv_file(fp,&j,0);
  147.                 fseek(fp,-i,1);
  148.                 strcat(buf,"\\newenvironment{");
  149.                 buf += 16;
  150.                 for (j=0; j < i; j++)
  151.                     *buf++=getc(fp);
  152.                 }
  153.             else
  154.                 {
  155.                 *buf++='\\';
  156.                 for (j=0; j < i; j++)
  157.                     *buf++ = w[j];
  158.                 *buf++ = c;
  159.                 }
  160.             }
  161.         }
  162.     c1 = c;                /* update last character */
  163.     }
  164. *buf = NULL;                /* terminate it with a null */
  165. }
  166.