home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 216.lha / PdMake / reader.c < prev    next >
C/C++ Source or Header  |  1996-02-15  |  4KB  |  152 lines

  1. /*
  2.  *     Read in makefile
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include    <ctype.h>
  7. #include "h.h"
  8.  
  9. int            lineno;
  10.  
  11. /* DJ: this is the PAMAKE version of getline, which prints the
  12.     make file name as well as the line # (for #include)
  13.  
  14.     Notes for future: This would actually be less kludgey with format().
  15.     Also, 'stderr' has no meaning on Amiga - perhaps open "*"??
  16. */
  17.  
  18. /*
  19.  *      Syntax error handler.  Print message, with line number, and exits.
  20.  */
  21. void
  22. error(msg, a1, a2, a3)
  23. char            *msg;
  24. {    fprintf(stderr, "%s: ", myname);
  25.     fprintf(stderr, msg, a1, a2, a3);
  26.     if (lineno)
  27.         fprintf(stderr, " in file %s on line %d", fname[nestlvl], lineno);
  28.     fputc('\n', stderr);
  29.     exit(1);
  30. }
  31.  
  32. /* DJ: this is the PAMAKE version of getline. */
  33.  
  34. /*
  35.  *      Read a line into the supplied string of length LZ.  Remove
  36.  *      comments, ignore blank lines. Deal with quoted (\) #, and
  37.  *      quoted newlines.  If EOF return TRUE.
  38.  */
  39.  
  40. bool getline(str) char *str;
  41. {   register char    *p;
  42.     char            *q;
  43.     char            *a;
  44.     int                  pos = 0;
  45.     FILE            *fd;
  46.     FILE            *incf;
  47.     int                 specialhash;
  48.  
  49.     if (nestlvl < 0) return TRUE;       /* EOF */
  50.  
  51.     for (;;)
  52.     {
  53.         fd = ifile[nestlvl];                    /* file handle in nesting array */
  54.         if (fgets(str+pos, LZ-pos, fd) == (char *)0)    /* get a line */
  55.         {        /* if we couldn't get a line */
  56.             fclose(fd);                            /* close file */
  57.             if (nestlvl == 0) { nestlvl--; /* ifeof(); */ return TRUE; }    /* finish */
  58.             fln[nestlvl] = 0;                    /* init line number */
  59.             nestlvl--;                            /* dec nesting level */
  60.             continue;                            /* get another line */
  61.         }
  62.         lineno = ++fln[nestlvl];                /* bump line count */
  63.  
  64.         if ((p = strchr(str+pos, '\n')) == (char *)0)    /* look for cr */
  65.             error("Input line is too long");
  66.  
  67.         if (p[-1] == '\\')
  68.         {
  69. /*            p[-1] = '\n'; */
  70.             p--;                        /* added by DJ for AmigaDOS compatability */
  71.             pos = p - str;
  72.             continue;
  73.         }
  74.  
  75.         if (!strncmp(str,"#ifn",4)) specialhash = 2;
  76.         else if (!strncmp(str,"#if",3)) specialhash = 1;
  77.         else if (!strncmp(str,"#else",5)) specialhash = 3;
  78.         else if (!strncmp(str,"#endif",6)) specialhash = 4;
  79.         else if (!strncmp(str,"#include",8)) specialhash = 5;
  80.         else specialhash = 0;
  81.  
  82.         p = str + (specialhash != 0);
  83.         while (((q = strchr(p, '#')) != (char *)0) &&
  84.             (p != q) && (q[-1] == '\\'))
  85.         {
  86.             a = q - 1;      /*  Del \ chr; move rest back  */
  87.             p = q;
  88.             while (*a++ = *q++)
  89.                 ;
  90.         }
  91.  
  92.         if (q != (char *)0) 
  93.         {
  94.             while ( (q != str) && (isspace(q[-1])) ) q--;
  95.             q[0] = '\n';
  96.             q[1] = '\0';
  97.         }
  98.  
  99. /*        if (ifproc(str,specialhash)) {pos = 0; continue;} */
  100.  
  101.         if (specialhash == 5)
  102.         {
  103.             q = str + 8;
  104.             while (isspace(q[0])) q++;
  105.             if (nestlvl >= 3)
  106.                 fatal("Include files nested too deeply");
  107.             a = q + strlen(q) - 1;
  108.             if (*a == '\n') *a = '\0';
  109.             expand(q);
  110.             incf = fopen(q,"r");
  111.             if (incf == (FILE *)0)
  112.                 fatal("Unable to open include file %s", q);
  113.             ifile[++nestlvl] = incf;
  114.             strncpy(fname[nestlvl],q,80);
  115.             continue;
  116.         }
  117.  
  118.         p = str;
  119.         while (isspace(*p))     /*  Checking for blank  */
  120.             p++;
  121.  
  122.         if (*p != '\0')  
  123.             return FALSE;
  124.         pos = 0;
  125.     }
  126. }
  127.  
  128. /*
  129.  *     Get a word from the current line, surounded by white space.
  130.  *     return a pointer to it. String returned has no white spaces
  131.  *     in it.
  132.  */
  133. char *gettok(ptr)
  134.     char            **ptr;
  135. {    register char    *p;
  136.  
  137.     while (isspace(**ptr))             /*  Skip spaces    */
  138.         (*ptr)++;
  139.  
  140.     if (**ptr == '\0')                /*  Nothing after spaces  */
  141.         return NULL;
  142.  
  143.     p = *ptr;                        /*  word starts here  */
  144.  
  145.     while ((**ptr != '\0') && (!isspace(**ptr)))
  146.         (*ptr)++;                    /*  Find end of word  */
  147.  
  148.     *(*ptr)++ = '\0';                /*  Terminate it  */
  149.  
  150.     return(p);
  151. }
  152.