sub.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "grep.h"
  2. void*
  3. mal(int n)
  4. {
  5. static char *s;
  6. static int m = 0;
  7. void *v;
  8. n = (n+3) & ~3;
  9. if(m < n) {
  10. if(n > Nhunk) {
  11. v = sbrk(n);
  12. memset(v, 0, n);
  13. return v;
  14. }
  15. s = sbrk(Nhunk);
  16. m = Nhunk;
  17. }
  18. v = s;
  19. s += n;
  20. m -= n;
  21. memset(v, 0, n);
  22. return v;
  23. }
  24. State*
  25. sal(int n)
  26. {
  27. State *s;
  28. s = mal(sizeof(*s));
  29. // s->next = mal(256*sizeof(*s->next));
  30. s->count = n;
  31. s->re = mal(n*sizeof(*state0->re));
  32. return s;
  33. }
  34. Re*
  35. ral(int type)
  36. {
  37. Re *r;
  38. r = mal(sizeof(*r));
  39. r->type = type;
  40. maxfollow++;
  41. return r;
  42. }
  43. void
  44. error(char *s)
  45. {
  46. fprint(2, "grep: internal error: %s\n", s);
  47. exits(s);
  48. }
  49. int
  50. countor(Re *r)
  51. {
  52. int n;
  53. n = 0;
  54. loop:
  55. switch(r->type) {
  56. case Tor:
  57. n += countor(r->alt);
  58. r = r->next;
  59. goto loop;
  60. case Tclass:
  61. return n + r->hi - r->lo + 1;
  62. }
  63. return n;
  64. }
  65. Re*
  66. oralloc(int t, Re *r, Re *b)
  67. {
  68. Re *a;
  69. if(b == 0)
  70. return r;
  71. a = ral(t);
  72. a->alt = r;
  73. a->next = b;
  74. return a;
  75. }
  76. void
  77. case1(Re *c, Re *r)
  78. {
  79. int n;
  80. loop:
  81. switch(r->type) {
  82. case Tor:
  83. case1(c, r->alt);
  84. r = r->next;
  85. goto loop;
  86. case Tclass: /* add to character */
  87. for(n=r->lo; n<=r->hi; n++)
  88. c->cases[n] = oralloc(Tor, r->next, c->cases[n]);
  89. break;
  90. default: /* add everything unknown to next */
  91. c->next = oralloc(Talt, r, c->next);
  92. break;
  93. }
  94. }
  95. Re*
  96. addcase(Re *r)
  97. {
  98. int i, n;
  99. Re *a;
  100. if(r->gen == gen)
  101. return r;
  102. r->gen = gen;
  103. switch(r->type) {
  104. default:
  105. error("addcase");
  106. case Tor:
  107. n = countor(r);
  108. if(n >= Caselim) {
  109. a = ral(Tcase);
  110. a->cases = mal(256*sizeof(*a->cases));
  111. case1(a, r);
  112. for(i=0; i<256; i++)
  113. if(a->cases[i]) {
  114. r = a->cases[i];
  115. if(countor(r) < n)
  116. a->cases[i] = addcase(r);
  117. }
  118. return a;
  119. }
  120. return r;
  121. case Talt:
  122. r->next = addcase(r->next);
  123. r->alt = addcase(r->alt);
  124. return r;
  125. case Tbegin:
  126. case Tend:
  127. case Tclass:
  128. return r;
  129. }
  130. }
  131. void
  132. str2top(char *p)
  133. {
  134. Re2 oldtop;
  135. oldtop = topre;
  136. input = p;
  137. if (*p == '\0')
  138. yyerror("empty pattern"); /* can't be a file name here */
  139. if (!flags['f'])
  140. pattern = p;
  141. topre.beg = 0;
  142. topre.end = 0;
  143. yyparse();
  144. gen++;
  145. if(topre.beg == 0)
  146. yyerror("syntax");
  147. if(oldtop.beg)
  148. topre = re2or(oldtop, topre);
  149. }
  150. void
  151. appendnext(Re *a, Re *b)
  152. {
  153. Re *n;
  154. while(n = a->next)
  155. a = n;
  156. a->next = b;
  157. }
  158. void
  159. patchnext(Re *a, Re *b)
  160. {
  161. Re *n;
  162. while(a) {
  163. n = a->next;
  164. a->next = b;
  165. a = n;
  166. }
  167. }
  168. int
  169. getrec(void)
  170. {
  171. int c;
  172. if(flags['f']) {
  173. c = Bgetc(rein);
  174. if(c <= 0)
  175. return 0;
  176. } else
  177. c = *input++ & 0xff;
  178. if(flags['i'] && c >= 'A' && c <= 'Z')
  179. c += 'a'-'A';
  180. if(c == '\n')
  181. lineno++;
  182. return c;
  183. }
  184. Re2
  185. re2cat(Re2 a, Re2 b)
  186. {
  187. Re2 c;
  188. c.beg = a.beg;
  189. c.end = b.end;
  190. patchnext(a.end, b.beg);
  191. return c;
  192. }
  193. Re2
  194. re2star(Re2 a)
  195. {
  196. Re2 c;
  197. c.beg = ral(Talt);
  198. c.beg->alt = a.beg;
  199. patchnext(a.end, c.beg);
  200. c.end = c.beg;
  201. return c;
  202. }
  203. Re2
  204. re2or(Re2 a, Re2 b)
  205. {
  206. Re2 c;
  207. c.beg = ral(Tor);
  208. c.beg->alt = b.beg;
  209. c.beg->next = a.beg;
  210. c.end = b.end;
  211. appendnext(c.end, a.end);
  212. return c;
  213. }
  214. Re2
  215. re2char(int c0, int c1)
  216. {
  217. Re2 c;
  218. c.beg = ral(Tclass);
  219. c.beg->lo = c0 & 0xff;
  220. c.beg->hi = c1 & 0xff;
  221. c.end = c.beg;
  222. return c;
  223. }
  224. void
  225. reprint1(Re *a)
  226. {
  227. int i, j;
  228. loop:
  229. if(a == 0)
  230. return;
  231. if(a->gen == gen)
  232. return;
  233. a->gen = gen;
  234. print("%p: ", a);
  235. switch(a->type) {
  236. default:
  237. print("type %d\n", a->type);
  238. error("print1 type");
  239. case Tcase:
  240. print("case ->%p\n", a->next);
  241. for(i=0; i<256; i++)
  242. if(a->cases[i]) {
  243. for(j=i+1; j<256; j++)
  244. if(a->cases[i] != a->cases[j])
  245. break;
  246. print(" [%.2x-%.2x] ->%p\n", i, j-1, a->cases[i]);
  247. i = j-1;
  248. }
  249. for(i=0; i<256; i++)
  250. reprint1(a->cases[i]);
  251. break;
  252. case Tbegin:
  253. print("^ ->%p\n", a->next);
  254. break;
  255. case Tend:
  256. print("$ ->%p\n", a->next);
  257. break;
  258. case Tclass:
  259. print("[%.2x-%.2x] ->%p\n", a->lo, a->hi, a->next);
  260. break;
  261. case Tor:
  262. case Talt:
  263. print("| %p ->%p\n", a->alt, a->next);
  264. reprint1(a->alt);
  265. break;
  266. }
  267. a = a->next;
  268. goto loop;
  269. }
  270. void
  271. reprint(char *s, Re *r)
  272. {
  273. print("%s:\n", s);
  274. gen++;
  275. reprint1(r);
  276. print("\n\n");
  277. }