grep.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #ifndef EXTERN
  5. #define EXTERN extern
  6. #endif
  7. typedef struct Re Re;
  8. typedef struct Re2 Re2;
  9. typedef struct State State;
  10. struct State
  11. {
  12. int count;
  13. int match;
  14. Re** re;
  15. State* linkleft;
  16. State* linkright;
  17. State* next[256];
  18. };
  19. struct Re2
  20. {
  21. Re* beg;
  22. Re* end;
  23. };
  24. struct Re
  25. {
  26. uchar type;
  27. ushort gen;
  28. union
  29. {
  30. Re* alt; /* Talt */
  31. Re** cases; /* case */
  32. struct /* class */
  33. {
  34. Rune lo;
  35. Rune hi;
  36. };
  37. Rune val; /* char */
  38. };
  39. Re* next;
  40. };
  41. enum
  42. {
  43. Talt = 1,
  44. Tbegin,
  45. Tcase,
  46. Tclass,
  47. Tend,
  48. Tor,
  49. Caselim = 7,
  50. Nhunk = 1<<16,
  51. Cbegin = 0x10000,
  52. Flshcnt = (1<<9)-1,
  53. Cflag = 1<<0,
  54. Hflag = 1<<1,
  55. Iflag = 1<<2,
  56. Llflag = 1<<3,
  57. LLflag = 1<<4,
  58. Nflag = 1<<5,
  59. Sflag = 1<<6,
  60. Vflag = 1<<7,
  61. Bflag = 1<<8
  62. };
  63. EXTERN union
  64. {
  65. char string[16*1024];
  66. struct
  67. {
  68. /*
  69. * if a line requires multiple reads, we keep shifting
  70. * buf down into pre and then do another read into
  71. * buf. so you'll get the last 16-32k of the matching line.
  72. * if pre were smaller than buf you'd get a suffix of the
  73. * line with a hole cut out.
  74. */
  75. uchar pre[16*1024]; /* to save to previous '\n' */
  76. uchar buf[16*1024]; /* input buffer */
  77. };
  78. } u;
  79. EXTERN char *filename;
  80. EXTERN char *pattern;
  81. EXTERN Biobuf bout;
  82. EXTERN char flags[256];
  83. EXTERN Re** follow;
  84. EXTERN ushort gen;
  85. EXTERN char* input;
  86. EXTERN long lineno;
  87. EXTERN int literal;
  88. EXTERN int matched;
  89. EXTERN long maxfollow;
  90. EXTERN long nfollow;
  91. EXTERN int peekc;
  92. EXTERN Biobuf* rein;
  93. EXTERN State* state0;
  94. EXTERN Re2 topre;
  95. extern Re* addcase(Re*);
  96. extern void appendnext(Re*, Re*);
  97. extern void error(char*);
  98. extern int fcmp(void*, void*); /* (Re**, Re**) */
  99. extern void fol1(Re*, int);
  100. extern int getrec(void);
  101. extern void increment(State*, int);
  102. extern State* initstate(Re*);
  103. extern void* mal(int);
  104. extern void patchnext(Re*, Re*);
  105. extern Re* ral(int);
  106. extern Re2 re2cat(Re2, Re2);
  107. extern Re2 re2class(char*);
  108. extern Re2 re2or(Re2, Re2);
  109. extern Re2 re2char(int, int);
  110. extern Re2 re2star(Re2);
  111. extern State* sal(int);
  112. extern int search(char*, int);
  113. extern void str2top(char*);
  114. extern int yyparse(void);
  115. extern void reprint(char*, Re*);
  116. extern void yyerror(char*, ...);