home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PAMAKE18.ZIP / READER.C < prev    next >
C/C++ Source or Header  |  1989-09-30  |  6KB  |  186 lines

  1. /*************************************************************************
  2. |                                                                        |
  3. |   READER.C                                                    31.08.89 |
  4. |   PAMAKE Utility:  read in the makefile                                |
  5. |                                                                        |
  6. *************************************************************************/
  7.  
  8. #ifdef VMS
  9. #include stdio
  10. #include string
  11. #include stdlib
  12. #include ctype
  13. #include "h.h"
  14. #else
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19. #include "h.h"
  20. #endif
  21.  
  22. int                     lineno;
  23.  
  24. /*****  syntax error handler - print message, with line number, and exit */
  25.  
  26. #ifndef __TSC__
  27. void
  28. error(msg, a1, a2, a3)
  29. char *          msg;
  30. {
  31.     fprintf(stderr, "%s: ", myname);
  32.     fprintf(stderr, msg, a1, a2, a3);
  33.     if (lineno)
  34.         fprintf(stderr, " in file %s on line %d", fname[nestlvl], lineno);
  35.     fputc('\n', stderr);
  36.     exit(PXERROR);
  37. }
  38. #endif
  39.  
  40. /*****  read a line */
  41.  
  42. /*************************************************************************
  43. |                                                                        |
  44. |   Read a line into the supplied string of length LZ.  Remove           |
  45. |   comments, ignore blank lines. Deal with quoted (\) #, and            |
  46. |   quoted newlines.  If EOF return TRUE.                                |
  47. |                                                                        |
  48. *************************************************************************/
  49.  
  50. bool
  51. getline(str)
  52. char *          str;
  53. {
  54.     register char *         p;
  55.     char *                  q;
  56.     char *                  a;
  57.     int                     pos = 0;
  58.     FILE *                  fd;
  59.     FILE *                  incf;
  60.     int                     specialhash;
  61.  
  62.     if (nestlvl < 0) return TRUE;           /* EOF */
  63.  
  64.     for (;;)
  65.     {
  66.         fd = ifile[nestlvl];
  67.         if (fgets(str+pos, LZ-pos, fd) == (char *)0)
  68.         {
  69.             fclose(fd);
  70.             if (nestlvl == 0) 
  71.             {
  72.                 nestlvl--;
  73.                 ifeof();
  74.                 return TRUE;
  75.             }
  76.             fln[nestlvl] = 0;
  77.             nestlvl--;
  78.             continue;
  79.         }
  80.         lineno = ++fln[nestlvl];
  81.  
  82.         if ((p = strchr(str+pos, '\n')) == (char *)0)
  83.             error("Input line is too long");
  84.  
  85.         if (p[-1] == '\\')
  86.         {
  87.             p[-1] = '\n';
  88.             pos = (int)(p - str);
  89.             continue;
  90.         }
  91.  
  92.         if (!strncmp(str,"#ifn",4)) specialhash = 2;
  93.         else if (!strncmp(str,"#if",3)) specialhash = 1;
  94.         else if (!strncmp(str,"#else",5)) specialhash = 3;
  95.         else if (!strncmp(str,"#endif",6)) specialhash = 4;
  96.         else if (!strncmp(str,"#include",8)) specialhash = 5;
  97.         else specialhash = 0;
  98.  
  99.         p = str + (specialhash != 0);
  100.         while (((q = strchr(p, '#')) != (char *)0) &&
  101.             (p != q) && (q[-1] == '\\'))
  102.         {
  103.             a = q - 1;      /*  Del \ chr; move rest back  */
  104.             p = q;
  105.             while ((*a++ = *q++) != '\0')
  106.                 ;
  107.         }
  108.  
  109.         if (q != (char *)0) 
  110.         {
  111.             while ( (q != str) && (pspace(q[-1])) ) q--;
  112.             q[0] = '\n';
  113.             q[1] = '\0';
  114.         }
  115.  
  116.         if (ifproc(str,specialhash)) 
  117.         {
  118.             pos = 0;
  119.             continue;
  120.         }
  121.  
  122.         if (specialhash == 5)
  123.         {
  124.             q = str + 8;
  125.             while (pspace(q[0])) q++;
  126.             if (nestlvl >= 3)
  127.                 fatal("Include files nested too deeply");
  128.             a = q + strlen(q) - 1;
  129.             if (*a == '\n') *a = '\0';
  130.             expand(q);
  131.             incf = fopen(dollar(q),"r");
  132.             if (incf == (FILE *)0)
  133.                 fatal("Unable to open include file %s", dollar(q));
  134.             ifile[++nestlvl] = incf;
  135.             strncpy(fname[nestlvl],q,80);
  136.             continue;
  137.         }
  138.  
  139.         p = str;
  140.         while (pspace(*p))     /*  Checking for blank  */
  141.             p++;
  142.  
  143.         if (*p != '\0')  
  144.             return FALSE;
  145.         pos = 0;
  146.     }
  147. }
  148.  
  149. /*****  get token */
  150.  
  151. /*************************************************************************
  152. |                                                                        |
  153. |   Get a word from the current line, surounded by white space.          |
  154. |   return a pointer to it. String returned has no white spaces          |
  155. |   in it.                                                               |
  156. |                                                                        |
  157. *************************************************************************/
  158.  
  159. char *
  160. gettok(ptr)
  161. char * *        ptr;
  162. {
  163.     register char *         p;
  164.  
  165.     while (pspace(**ptr)) (*ptr)++;         /* skip spaces  */
  166.     if (**ptr == '\0') return NULL;         /* nothing after spaces  */
  167.     p = *ptr;                               /* word starts here  */
  168.     while ((**ptr != '\0') && (!pspace(**ptr))) (*ptr)++;  /* find word end */
  169.     *(*ptr)++ = '\0';                       /* terminate it  */
  170.     return(p);
  171. }
  172.  
  173. /*************************************************************************
  174. |                                                                        |
  175. |   Check that the character is whitespace.  Note that isspace gives an  |
  176. |   undefined result if the character is not ASCII.                      |
  177. |                                                                        |
  178. *************************************************************************/
  179.  
  180. int pspace (c)
  181. int c;
  182.  
  183. {
  184.     return (isascii(c & 0xff) && isspace(c & 0xff));
  185. }
  186.