reader.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Read in makefile
  3. *
  4. * $Header$
  5. */
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include "h.h"
  9. int lineno;
  10. /*
  11. * Syntax error handler. Print message, with line number, and exits.
  12. */
  13. /*VARARGS1*/
  14. void
  15. error(msg, a1, a2, a3)
  16. char * msg;
  17. {
  18. fprintf(stderr, "%s: ", myname);
  19. fprintf(stderr, msg, a1, a2, a3);
  20. if (lineno)
  21. fprintf(stderr, " near line %d", lineno);
  22. fputc('\n', stderr);
  23. exit(1);
  24. }
  25. /*
  26. * Read a line into the supplied string of length LZ. Remove
  27. * comments, ignore blank lines. Deal with quoted (\) #, and
  28. * quoted newlines. If EOF return TRUE.
  29. */
  30. bool
  31. getline(str, fd)
  32. char * str;
  33. FILE * fd;
  34. {
  35. register char * p;
  36. char * q;
  37. int pos = 0;
  38. for (;;)
  39. {
  40. if (fgets(str+pos, LZ-pos, fd) == (char *)0)
  41. return TRUE; /* EOF */
  42. lineno++;
  43. if ((p = index(str+pos, '\n')) == (char *)0)
  44. error("Line too long");
  45. if (p[-1] == '\\')
  46. {
  47. p[-1] = '\n';
  48. pos = p - str;
  49. continue;
  50. }
  51. p = str;
  52. while (((q = index(p, '#')) != (char *)0) &&
  53. (p != q) && (q[-1] == '\\'))
  54. {
  55. char *a;
  56. a = q - 1; /* Del \ chr; move rest back */
  57. p = q;
  58. while (*a++ = *q++)
  59. ;
  60. }
  61. if (q != (char *)0)
  62. {
  63. q[0] = '\n';
  64. q[1] = '\0';
  65. }
  66. p = str;
  67. while (isspace(*p)) /* Checking for blank */
  68. p++;
  69. if (*p != '\0')
  70. return FALSE;
  71. pos = 0;
  72. }
  73. }
  74. /*
  75. * Get a word from the current line, surounded by white space.
  76. * return a pointer to it. String returned has no white spaces
  77. * in it.
  78. */
  79. char *
  80. gettok(ptr)
  81. char **ptr;
  82. {
  83. register char * p;
  84. while (isspace(**ptr)) /* Skip spaces */
  85. (*ptr)++;
  86. if (**ptr == '\0') /* Nothing after spaces */
  87. return NULL;
  88. p = *ptr; /* word starts here */
  89. while ((**ptr != '\0') && (!isspace(**ptr)))
  90. (*ptr)++; /* Find end of word */
  91. *(*ptr)++ = '\0'; /* Terminate it */
  92. return(p);
  93. }