rescan.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <string.h>
  12. #include <err.h>
  13. #include "find.h"
  14. #include "printl.h"
  15. #include "rescan.h"
  16. #include "externs.h"
  17. void doreplace(void);
  18. static void domagic(void);
  19. static void getword(rg char *buffer);
  20. static int translate = 1;
  21. void
  22. rescan()
  23. {
  24. rg int c;
  25. /*
  26. * Keep tempfile, delimiter and translate flag in registers to speed
  27. * up this routine; the last two need to be reloaded after and only
  28. * after calls to domagic()
  29. */
  30. rg FILE *tf = tempfile;
  31. rg int dlm = delimiter;
  32. rg int tr = translate;
  33. while ((c = getc(tf)) != EOF) {
  34. if (c == MAGIC1) {
  35. if ((c = getc(tf)) == MAGIC2) {
  36. domagic();
  37. dlm = delimiter;
  38. tr = translate;
  39. continue;
  40. }
  41. else
  42. ungetc(c, tf);
  43. }
  44. if (c == dlm && tr) {
  45. doreplace();
  46. continue;
  47. }
  48. if (c == '\n')
  49. fileline++;
  50. putchar(c);
  51. }
  52. }
  53. void
  54. domagic()
  55. {
  56. int c;
  57. static char buffer[BUFSIZ];
  58. switch (getc(tempfile)) {
  59. case M_DELIM:
  60. c = getc(tempfile);
  61. if (c == '\n')
  62. translate = 0;
  63. else {
  64. translate = 1;
  65. delimiter = (char) c;
  66. getc(tempfile);
  67. }
  68. break;
  69. case M_FILE:
  70. if (fgets(buffer, BUFSIZ, tempfile) == NULL)
  71. buffer[0] = '\0';
  72. else
  73. buffer[strlen(buffer) - 1] = '\0';
  74. filename = buffer;
  75. fileline = 1;
  76. break;
  77. }
  78. }
  79. void
  80. doreplace()
  81. {
  82. rg int c;
  83. char typename[BUFSIZ];
  84. char labelname[BUFSIZ];
  85. rg Type *tp;
  86. rg Label *lp;
  87. /*
  88. * <delimiter><delimiter> gives <delimiter> as output
  89. */
  90. if ((c = getc(tempfile)) == delimiter) {
  91. putchar(delimiter);
  92. return;
  93. }
  94. ungetc(c, tempfile);
  95. getword(typename);
  96. getword(labelname);
  97. if ((c = getc(tempfile)) != delimiter) {
  98. warnx("%smalformed label reference", maybe_loc());
  99. while (c != delimiter) {
  100. if (c == EOF || c == '\n')
  101. break;
  102. c = getc(tempfile);
  103. }
  104. return;
  105. }
  106. if ((tp = findtype(typename, 0)) == NULL) {
  107. warnx("%sundefined type '%s'", maybe_loc(), typename);
  108. printf("<<%s %s>>", typename, labelname);
  109. return;
  110. }
  111. if ((lp = typeFindlabel(tp, labelname)) == NULL) {
  112. warnx("%sundefined label '%s'", maybe_loc(), labelname);
  113. printf("<<%s %s>>", typename, labelname);
  114. return;
  115. }
  116. labelPrint(lp, stdout);
  117. }
  118. void
  119. getword(char *buffer)
  120. {
  121. rg int c;
  122. rg int dlm = delimiter;
  123. rg FILE *tf = tempfile;
  124. c = getc(tf);
  125. while (isspace(c))
  126. c = getc(tf);
  127. while (c != dlm && !isspace(c) && c != '\n' && c != EOF) {
  128. *buffer++ = (char) c;
  129. c = getc(tf);
  130. }
  131. *buffer = '\0';
  132. if (!isspace(c) && c != EOF)
  133. ungetc(c, tf);
  134. }