scan.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* (C) C.D.F. Miller, Heriot-Watt University, March 1984
  2. *
  3. * Permission is hereby given to reproduce or modify this
  4. * software freely, provided that this notice be retained,
  5. * and that no use be made of the software for commercial
  6. * purposes without the express written permission of the
  7. * author.
  8. */
  9. #include <lbl.h>
  10. #include <ctype.h>
  11. #include <bsd/string.h>
  12. #include <err.h>
  13. #include "build.h"
  14. #include "scan.h"
  15. void process(char *);
  16. extern char macroname[];
  17. extern char *filename;
  18. extern int sflag;
  19. extern FILE *tempfile;
  20. extern long fileline;
  21. void
  22. scan(char *fname, FILE *f)
  23. {
  24. char line[BUFSIZ];
  25. rg int line_complete = 1;
  26. /*
  27. * Mark start of file in temp file
  28. */
  29. if (!sflag)
  30. fprintf(tempfile, "%c%c%c%s\n", MAGIC1, MAGIC2, M_FILE, fname);
  31. filename = fname;
  32. fileline = 0L;
  33. while (fgets(line, BUFSIZ, f) != NULL) {
  34. if (!sflag)
  35. fputs(line, tempfile);
  36. if (line_complete) {
  37. fileline++;
  38. line_complete = line[strlen(line) - 1] == '\n';
  39. if (line[0] == '.' && line[1] == macroname[0] &&
  40. line[2] == macroname[1]) {
  41. if (line_complete)
  42. process(line + 3);
  43. else
  44. warnx("%s.%s line too long",
  45. maybe_loc(), macroname);
  46. }
  47. }
  48. else
  49. line_complete = line[strlen(line) - 1] == '\n';
  50. }
  51. }
  52. void
  53. process(char *line)
  54. {
  55. us int split();
  56. Keyword *findkeyword();
  57. us int nargs;
  58. char *argvec[NLEVELS + 2];
  59. Keyword *key;
  60. if ((nargs = split(line, argvec)) == 0) {
  61. warnx("%sempty .%s line", maybe_loc(), macroname);
  62. return;
  63. }
  64. /*
  65. * Check for and act upon reserved words
  66. */
  67. if ((key = findkeyword(argvec[0])) != NULL) {
  68. if (nargs < key->k_minargs) {
  69. warnx("%s%s - too few arguments", maybe_loc(),
  70. key->k_name);
  71. return;
  72. }
  73. if (nargs > key->k_maxargs) {
  74. warnx("%s%s - too many arguments", maybe_loc(),
  75. key->k_name);
  76. return;
  77. }
  78. (*(key->k_action)) (nargs, argvec);
  79. return;
  80. }
  81. /*
  82. * Process a label definition
  83. */
  84. if (nargs != 3) {
  85. warnx("%stoo %s arguments in label definition",
  86. maybe_loc(), nargs < 3 ? "few" : "many");
  87. return;
  88. }
  89. addlbl(argvec[0], argvec[1], argvec[2]);
  90. }
  91. /* split - split a line into arguments
  92. * N.B. successive calls of this invalidate earlier calls;
  93. * argvec is set to point to strings within the
  94. * static local buffer "copybuf" which is overwritten on
  95. * each call.
  96. */
  97. us int split(char *line, char *argvec[]);
  98. us int
  99. split(char *line, char *argvec[])
  100. {
  101. us int nargs = 0;
  102. static char copybuf[BUFSIZ];
  103. char *ln = copybuf;
  104. strlcpy(ln, line, BUFSIZ);
  105. while (*ln) {
  106. while (isspace(*ln))
  107. ln++;
  108. if (*ln) {
  109. if (nargs > 2 + NLEVELS) {
  110. warnx("%ssurplus arguments to .%s ignored",
  111. maybe_loc(), macroname);
  112. break;
  113. }
  114. argvec[nargs++] = ln;
  115. while (*ln && !isspace(*ln))
  116. ln++;
  117. if (*ln)
  118. *ln++ = '\0';
  119. }
  120. }
  121. if (nargs > 2 + NLEVELS)
  122. nargs = 2 + NLEVELS;
  123. return nargs;
  124. }