czmq-ffi-gen 0.9.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/AUTHORS +1 -0
  3. data/CHANGES.md +92 -0
  4. data/Gemfile +2 -0
  5. data/LICENSE +14 -0
  6. data/README.md +112 -0
  7. data/lib/czmq-ffi-gen.rb +7 -0
  8. data/lib/czmq-ffi-gen/czmq/ffi.rb +1210 -0
  9. data/lib/czmq-ffi-gen/czmq/ffi/version.rb +15 -0
  10. data/lib/czmq-ffi-gen/czmq/ffi/zactor.rb +186 -0
  11. data/lib/czmq-ffi-gen/czmq/ffi/zarmour.rb +286 -0
  12. data/lib/czmq-ffi-gen/czmq/ffi/zcert.rb +307 -0
  13. data/lib/czmq-ffi-gen/czmq/ffi/zcertstore.rb +222 -0
  14. data/lib/czmq-ffi-gen/czmq/ffi/zchunk.rb +388 -0
  15. data/lib/czmq-ffi-gen/czmq/ffi/zclock.rb +140 -0
  16. data/lib/czmq-ffi-gen/czmq/ffi/zconfig.rb +442 -0
  17. data/lib/czmq-ffi-gen/czmq/ffi/zdigest.rb +156 -0
  18. data/lib/czmq-ffi-gen/czmq/ffi/zdir.rb +283 -0
  19. data/lib/czmq-ffi-gen/czmq/ffi/zdir_patch.rb +194 -0
  20. data/lib/czmq-ffi-gen/czmq/ffi/zfile.rb +353 -0
  21. data/lib/czmq-ffi-gen/czmq/ffi/zframe.rb +359 -0
  22. data/lib/czmq-ffi-gen/czmq/ffi/zhash.rb +416 -0
  23. data/lib/czmq-ffi-gen/czmq/ffi/zhashx.rb +659 -0
  24. data/lib/czmq-ffi-gen/czmq/ffi/ziflist.rb +189 -0
  25. data/lib/czmq-ffi-gen/czmq/ffi/zlist.rb +365 -0
  26. data/lib/czmq-ffi-gen/czmq/ffi/zlistx.rb +478 -0
  27. data/lib/czmq-ffi-gen/czmq/ffi/zloop.rb +396 -0
  28. data/lib/czmq-ffi-gen/czmq/ffi/zmsg.rb +515 -0
  29. data/lib/czmq-ffi-gen/czmq/ffi/zpoller.rb +194 -0
  30. data/lib/czmq-ffi-gen/czmq/ffi/zproc.rb +294 -0
  31. data/lib/czmq-ffi-gen/czmq/ffi/zsock.rb +3479 -0
  32. data/lib/czmq-ffi-gen/czmq/ffi/zstr.rb +203 -0
  33. data/lib/czmq-ffi-gen/czmq/ffi/ztimerset.rb +203 -0
  34. data/lib/czmq-ffi-gen/czmq/ffi/ztrie.rb +221 -0
  35. data/lib/czmq-ffi-gen/czmq/ffi/zuuid.rb +227 -0
  36. data/lib/czmq-ffi-gen/errors.rb +12 -0
  37. data/lib/czmq-ffi-gen/gem_version.rb +5 -0
  38. data/lib/czmq-ffi-gen/legacy.rb +16 -0
  39. data/lib/czmq-ffi-gen/libzmq.rb +18 -0
  40. data/lib/czmq-ffi-gen/signals.rb +27 -0
  41. data/lib/czmq-ffi-gen/vendor.rb +5 -0
  42. data/lib/czmq-ffi-gen/versions.rb +19 -0
  43. data/vendor/local/bin/inproc_lat.exe +0 -0
  44. data/vendor/local/bin/inproc_thr.exe +0 -0
  45. data/vendor/local/bin/libczmq.dll +0 -0
  46. data/vendor/local/bin/libgcc_s_seh-1.dll +0 -0
  47. data/vendor/local/bin/libstdc++-6.dll +0 -0
  48. data/vendor/local/bin/libzmq.dll +0 -0
  49. data/vendor/local/bin/local_lat.exe +0 -0
  50. data/vendor/local/bin/local_thr.exe +0 -0
  51. data/vendor/local/bin/remote_lat.exe +0 -0
  52. data/vendor/local/bin/remote_thr.exe +0 -0
  53. data/vendor/local/include/czmq.h +31 -0
  54. data/vendor/local/include/czmq_library.h +199 -0
  55. data/vendor/local/include/czmq_prelude.h +641 -0
  56. data/vendor/local/include/readme.txt +83 -0
  57. data/vendor/local/include/sha1.h +76 -0
  58. data/vendor/local/include/sha1.inc_c +335 -0
  59. data/vendor/local/include/slre.h +92 -0
  60. data/vendor/local/include/slre.inc_c +660 -0
  61. data/vendor/local/include/zactor.h +76 -0
  62. data/vendor/local/include/zarmour.h +114 -0
  63. data/vendor/local/include/zauth.h +100 -0
  64. data/vendor/local/include/zauth_v2.h +88 -0
  65. data/vendor/local/include/zbeacon.h +86 -0
  66. data/vendor/local/include/zbeacon_v2.h +75 -0
  67. data/vendor/local/include/zcert.h +136 -0
  68. data/vendor/local/include/zcertstore.h +100 -0
  69. data/vendor/local/include/zchunk.h +163 -0
  70. data/vendor/local/include/zclock.h +73 -0
  71. data/vendor/local/include/zconfig.h +185 -0
  72. data/vendor/local/include/zctx.h +107 -0
  73. data/vendor/local/include/zdigest.h +65 -0
  74. data/vendor/local/include/zdir.h +149 -0
  75. data/vendor/local/include/zdir_patch.h +82 -0
  76. data/vendor/local/include/zfile.h +177 -0
  77. data/vendor/local/include/zframe.h +176 -0
  78. data/vendor/local/include/zgossip.h +95 -0
  79. data/vendor/local/include/zgossip_engine.inc +927 -0
  80. data/vendor/local/include/zgossip_msg.h +129 -0
  81. data/vendor/local/include/zhash.h +195 -0
  82. data/vendor/local/include/zhash_primes.inc +329 -0
  83. data/vendor/local/include/zhashx.h +298 -0
  84. data/vendor/local/include/ziflist.h +77 -0
  85. data/vendor/local/include/zlist.h +158 -0
  86. data/vendor/local/include/zlistx.h +205 -0
  87. data/vendor/local/include/zloop.h +168 -0
  88. data/vendor/local/include/zmonitor.h +73 -0
  89. data/vendor/local/include/zmonitor_v2.h +56 -0
  90. data/vendor/local/include/zmq.h +617 -0
  91. data/vendor/local/include/zmq_utils.h +48 -0
  92. data/vendor/local/include/zmsg.h +280 -0
  93. data/vendor/local/include/zmutex.h +55 -0
  94. data/vendor/local/include/zpoller.h +92 -0
  95. data/vendor/local/include/zproc.h +168 -0
  96. data/vendor/local/include/zproxy.h +111 -0
  97. data/vendor/local/include/zproxy_v2.h +62 -0
  98. data/vendor/local/include/zrex.h +82 -0
  99. data/vendor/local/include/zsock.h +912 -0
  100. data/vendor/local/include/zsock_option.inc +4126 -0
  101. data/vendor/local/include/zsocket.h +110 -0
  102. data/vendor/local/include/zsockopt.h +256 -0
  103. data/vendor/local/include/zstr.h +110 -0
  104. data/vendor/local/include/zsys.h +386 -0
  105. data/vendor/local/include/zthread.h +50 -0
  106. data/vendor/local/include/ztimerset.h +90 -0
  107. data/vendor/local/include/ztrie.h +106 -0
  108. data/vendor/local/include/zuuid.h +96 -0
  109. data/vendor/local/lib/libczmq.dll.a +0 -0
  110. data/vendor/local/lib/liblibzmq.dll.a +0 -0
  111. data/vendor/local/lib/libzmq-static.a +0 -0
  112. data/vendor/local/lib/pkgconfig/libczmq.pc +23 -0
  113. data/vendor/local/lib/pkgconfig/libzmq.pc +11 -0
  114. data/vendor/local/share/zmq/AUTHORS.txt +147 -0
  115. data/vendor/local/share/zmq/COPYING.LESSER.txt +181 -0
  116. data/vendor/local/share/zmq/COPYING.txt +674 -0
  117. data/vendor/local/share/zmq/NEWS.txt +978 -0
  118. metadata +230 -0
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3
+ * All rights reserved
4
+ *
5
+ * "THE BEER-WARE LICENSE" (Revision 42):
6
+ * Sergey Lyubka wrote this file. As long as you retain this notice you
7
+ * can do whatever you want with this stuff. If we meet some day, and you think
8
+ * this stuff is worth it, you can buy me a beer in return.
9
+ */
10
+
11
+ /*
12
+ * This is a regular expression library that implements a subset of Perl RE.
13
+ * Please refer to http://slre.sourceforge.net for detailed description.
14
+ *
15
+ * Usage example (parsing HTTP request):
16
+ *
17
+ * struct slre slre;
18
+ * struct cap captures[4 + 1]; // Number of braket pairs + 1
19
+ * ...
20
+ *
21
+ * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
22
+ *
23
+ * if (slre_match(&slre, buf, len, captures)) {
24
+ * printf("Request line length: %d\n", captures[0].len);
25
+ * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
26
+ * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
27
+ * }
28
+ *
29
+ * Supported syntax:
30
+ * ^ Match beginning of a buffer
31
+ * $ Match end of a buffer
32
+ * () Grouping and substring capturing
33
+ * [...] Match any character from set
34
+ * [^...] Match any character but ones from set
35
+ * \s Match whitespace
36
+ * \S Match non-whitespace
37
+ * \d Match decimal digit
38
+ * \r Match carriage return
39
+ * \n Match newline
40
+ * + Match one or more times (greedy)
41
+ * +? Match one or more times (non-greedy)
42
+ * * Match zero or more times (greedy)
43
+ * *? Match zero or more times (non-greedy)
44
+ * ? Match zero or once
45
+ * \xDD Match byte with hex value 0xDD
46
+ * \meta Match one of the meta character: ^$().[*+?\
47
+ */
48
+
49
+ #ifndef SLRE_HEADER_DEFINED
50
+ #define SLRE_HEADER_DEFINED
51
+
52
+ /*
53
+ * Compiled regular expression
54
+ */
55
+ struct slre {
56
+ unsigned char code[256];
57
+ unsigned char data[256];
58
+ int code_size;
59
+ int data_size;
60
+ int num_caps; /* Number of bracket pairs */
61
+ int anchored; /* Must match from string start */
62
+ const char *err_str; /* Error string */
63
+ };
64
+
65
+ /*
66
+ * Captured substring
67
+ */
68
+ struct cap {
69
+ const char *ptr; /* Pointer to the substring */
70
+ size_t len; /* Substring length */
71
+ };
72
+
73
+ /*
74
+ * Compile regular expression. If success, 1 is returned.
75
+ * If error, 0 is returned and slre.err_str points to the error message.
76
+ */
77
+ int slre_compile(struct slre *, const char *re);
78
+
79
+ /*
80
+ * Return 1 if match, 0 if no match.
81
+ * If `captured_substrings' array is not NULL, then it is filled with the
82
+ * values of captured substrings. captured_substrings[0] element is always
83
+ * a full matched substring. The round bracket captures start from
84
+ * captured_substrings[1].
85
+ * It is assumed that the size of captured_substrings array is enough to
86
+ * hold all captures. The caller function must make sure it is! So, the
87
+ * array_size = number_of_round_bracket_pairs + 1
88
+ */
89
+ int slre_match(const struct slre *, const char *buf, size_t buf_len,
90
+ struct cap *captured_substrings);
91
+
92
+ #endif /* SLRE_HEADER_DEFINED */
@@ -0,0 +1,660 @@
1
+ /*
2
+ * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3
+ * All rights reserved
4
+ *
5
+ * "THE BEER-WARE LICENSE" (Revision 42):
6
+ * Sergey Lyubka wrote this file. As long as you retain this notice you
7
+ * can do whatever you want with this stuff. If we meet some day, and you think
8
+ * this stuff is worth it, you can buy me a beer in return.
9
+ */
10
+
11
+ #include <stdio.h>
12
+ #include <assert.h>
13
+ #include <ctype.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+ #include <errno.h>
17
+
18
+ #include "slre.h"
19
+
20
+ enum {
21
+ END, BRANCH, ANY, EXACT, ANYOF, ANYBUT, OPEN, CLOSE, BOL, EOL,
22
+ STAR, PLUS, STARQ, PLUSQ, QUEST,
23
+ SPACE, NONSPACE, DIGIT, NONDIGIT, ALPHA, NONALPHA, ALNUM, NONALNUM
24
+ };
25
+
26
+ static struct {
27
+ const char *name;
28
+ int narg;
29
+ const char *flags;
30
+ } opcodes [] = {
31
+ {"END", 0, ""}, /* End of code block or program */
32
+ {"BRANCH", 2, "oo"}, /* Alternative operator, "|" */
33
+ {"ANY", 0, ""}, /* Match any character, "." */
34
+ {"EXACT", 2, "d"}, /* Match exact string */
35
+ {"ANYOF", 2, "D"}, /* Match any from set, " []" */
36
+ {"ANYBUT", 2, "D"}, /* Match any but from set, " [^]"*/
37
+ {"OPEN ", 1, "i"}, /* Capture start, " (" */
38
+ {"CLOSE", 1, "i"}, /* Capture end, ")" */
39
+ {"BOL", 0, ""}, /* Beginning of string, "^" */
40
+ {"EOL", 0, ""}, /* End of string, "$" */
41
+ {"STAR", 1, "o"}, /* Match zero or more times "*" */
42
+ {"PLUS", 1, "o"}, /* Match one or more times, "+" */
43
+ {"STARQ", 1, "o"}, /* Non-greedy STAR, "*?" */
44
+ {"PLUSQ", 1, "o"}, /* Non-greedy PLUS, "+?" */
45
+ {"QUEST", 1, "o"}, /* Match zero or one time, "?" */
46
+ {"SPACE", 0, ""}, /* Match whitespace, "\s" */
47
+ {"NONSPACE",0, ""}, /* Match non-space, "\S" */
48
+ {"DIGIT", 0, ""}, /* Match digit, "\d" */
49
+ {"NONDIGIT",0, ""}, /* Match non-digit, "\D" */
50
+ {"ALPHA", 0, ""}, /* Match alpha, "\a" */
51
+ {"NONALPHA",0, ""}, /* Match non-alpha, "\A" */
52
+ {"ALNUM", 0, ""}, /* Match letter, "\w" */
53
+ {"NONALNUM", 0, ""} /* Match non-letter, "\W" */
54
+ };
55
+
56
+ /*
57
+ * Commands and operands are all unsigned char (1 byte long). All code offsets
58
+ * are relative to current address, and positive (always point forward). Data
59
+ * offsets are absolute. Commands with operands:
60
+ *
61
+ * BRANCH offset1 offset2
62
+ * Try to match the code block that follows the BRANCH instruction
63
+ * (code block ends with END). If no match, try to match code block that
64
+ * starts at offset1. If either of these match, jump to offset2.
65
+ *
66
+ * EXACT data_offset data_length
67
+ * Try to match exact string. String is recorded in data section from
68
+ * data_offset, and has length data_length.
69
+ *
70
+ * OPEN capture_number
71
+ * CLOSE capture_number
72
+ * If the user have passed 'struct cap' array for captures, OPEN
73
+ * records the beginning of the matched substring (cap->ptr), CLOSE
74
+ * sets the length (cap->len) for respective capture_number.
75
+ *
76
+ * STAR code_offset
77
+ * PLUS code_offset
78
+ * QUEST code_offset
79
+ * *, +, ?, respectively. Try to gobble as much as possible from the
80
+ * matched buffer, until code block that follows these instructions
81
+ * matches. When the longest possible string is matched,
82
+ * jump to code_offset
83
+ *
84
+ * STARQ, PLUSQ are non-greedy versions of STAR and PLUS.
85
+ */
86
+
87
+ static const char *meta_chars = "|.^$*+?()[\\";
88
+
89
+ static void
90
+ print_character_set (FILE *fp, const unsigned char *p, int len)
91
+ {
92
+ int i;
93
+
94
+ for (i = 0; i < len; i++) {
95
+ if (i > 0)
96
+ (void) fputc (',', fp);
97
+
98
+ if (p [i] == 0) {
99
+ i++;
100
+ if (p [i] == 0)
101
+ (void) fprintf (fp, "\\x%02x", p [i]);
102
+ else
103
+ (void) fprintf (fp, "%s", opcodes [p [i]].name);
104
+ }
105
+ else
106
+ if (isprint (p [i]))
107
+ (void) fputc (p [i], fp);
108
+ else
109
+ (void) fprintf (fp,"\\x%02x", p [i]);
110
+ }
111
+ }
112
+
113
+ void
114
+ slre_dump (const struct slre *r, FILE *fp)
115
+ {
116
+ int i, j, ch, op, pc;
117
+
118
+ for (pc = 0; pc < r->code_size; pc++) {
119
+
120
+ op = r->code [pc];
121
+ (void) fprintf (fp, "%3d %s ", pc, opcodes [op].name);
122
+
123
+ for (i = 0; opcodes [op].flags [i] != '\0'; i++)
124
+ switch (opcodes [op].flags [i]) {
125
+ case 'i':
126
+ (void) fprintf (fp, "%d ", r->code [pc + 1]);
127
+ pc++;
128
+ break;
129
+ case 'o':
130
+ (void) fprintf (fp, "%d ",
131
+ pc + r->code [pc + 1] - i);
132
+ pc++;
133
+ break;
134
+ case 'D':
135
+ print_character_set (fp, r->data +
136
+ r->code [pc + 1], r->code [pc + 2]);
137
+ pc += 2;
138
+ break;
139
+ case 'd':
140
+ (void) fputc ('"', fp);
141
+ for (j = 0; j < r->code [pc + 2]; j++) {
142
+ ch = r->data [r->code [pc + 1] + j];
143
+ if (isprint (ch))
144
+ (void) fputc (ch, fp);
145
+ else
146
+ (void) fprintf (fp,"\\x%02x",ch);
147
+ }
148
+ (void) fputc ('"', fp);
149
+ pc += 2;
150
+ break;
151
+ }
152
+
153
+ (void) fputc ('\n', fp);
154
+ }
155
+ }
156
+
157
+ static void
158
+ set_jump_offset (struct slre *r, int pc, int offset)
159
+ {
160
+ assert (offset < r->code_size);
161
+
162
+ if (r->code_size - offset > 0xff)
163
+ r->err_str = "Jump offset is too big";
164
+ else
165
+ r->code [pc] = (unsigned char) (r->code_size - offset);
166
+ }
167
+
168
+ static void
169
+ emit (struct slre *r, int code)
170
+ {
171
+ if (r->code_size >= (int) (sizeof (r->code) / sizeof (r->code [0])))
172
+ r->err_str = "RE is too long (code overflow)";
173
+ else
174
+ r->code [r->code_size++] = (unsigned char) code;
175
+ }
176
+
177
+ static void
178
+ store_char_in_data (struct slre *r, int ch)
179
+ {
180
+ if (r->data_size >= (int) sizeof (r->data))
181
+ r->err_str = "RE is too long (data overflow)";
182
+ else
183
+ r->data [r->data_size++] = ch;
184
+ }
185
+
186
+ static void
187
+ exact (struct slre *r, const char **re)
188
+ {
189
+ int old_data_size = r->data_size;
190
+
191
+ while (**re != '\0' && (strchr (meta_chars, **re)) == NULL)
192
+ store_char_in_data (r, *(*re)++);
193
+
194
+ emit (r, EXACT);
195
+ emit (r, old_data_size);
196
+ emit (r, r->data_size - old_data_size);
197
+ }
198
+
199
+ static int
200
+ get_escape_char (const char **re)
201
+ {
202
+ int res;
203
+
204
+ switch (*(*re)++) {
205
+ case 'n': res = '\n'; break;
206
+ case 'r': res = '\r'; break;
207
+ case 't': res = '\t'; break;
208
+ case '0': res = 0; break;
209
+ case 's': res = SPACE << 8; break;
210
+ case 'S': res = NONSPACE << 8; break;
211
+ case 'd': res = DIGIT << 8; break;
212
+ case 'D': res = NONDIGIT << 8; break;
213
+ case 'a': res = ALPHA << 8; break;
214
+ case 'A': res = NONALPHA << 8; break;
215
+ case 'w': res = ALNUM << 8; break;
216
+ case 'W': res = NONALNUM << 8; break;
217
+ default: res = (*re) [-1]; break;
218
+ }
219
+ return (res);
220
+ }
221
+
222
+ static void
223
+ anyof (struct slre *r, const char **re)
224
+ {
225
+ int esc, old_data_size = r->data_size, op = ANYOF;
226
+
227
+ if (**re == '^') {
228
+ op = ANYBUT;
229
+ (*re)++;
230
+ }
231
+ while (**re != '\0') {
232
+ switch (*(*re)++) {
233
+ case ']':
234
+ emit (r, op);
235
+ emit (r, old_data_size);
236
+ emit (r, r->data_size - old_data_size);
237
+ return;
238
+ /* NOTREACHED */
239
+ break;
240
+ case '\\':
241
+ esc = get_escape_char (re);
242
+ if ((esc & 0xff) == 0) {
243
+ store_char_in_data (r, 0);
244
+ store_char_in_data (r, esc >> 8);
245
+ }
246
+ else
247
+ store_char_in_data (r, esc);
248
+ break;
249
+ default:
250
+ store_char_in_data (r, (*re) [-1]);
251
+ break;
252
+ }
253
+ }
254
+ r->err_str = "No closing ']' bracket";
255
+ }
256
+
257
+ static void
258
+ relocate (struct slre *r, int begin, int shift)
259
+ {
260
+ emit (r, END);
261
+ memmove (r->code + begin + shift, r->code + begin, r->code_size - begin);
262
+ r->code_size += shift;
263
+ }
264
+
265
+ static void
266
+ quantifier (struct slre *r, int prev, int op)
267
+ {
268
+ if (r->code [prev] == EXACT && r->code [prev + 2] > 1) {
269
+ r->code [prev + 2]--;
270
+ emit (r, EXACT);
271
+ emit (r, r->code [prev + 1] + r->code [prev + 2]);
272
+ emit (r, 1);
273
+ prev = r->code_size - 3;
274
+ }
275
+ relocate (r, prev, 2);
276
+ r->code [prev] = op;
277
+ set_jump_offset (r, prev + 1, prev);
278
+ }
279
+
280
+ static void
281
+ exact_one_char (struct slre *r, int ch)
282
+ {
283
+ emit (r, EXACT);
284
+ emit (r, r->data_size);
285
+ emit (r, 1);
286
+ store_char_in_data (r, ch);
287
+ }
288
+
289
+ static void
290
+ fixup_branch (struct slre *r, int fixup)
291
+ {
292
+ if (fixup > 0) {
293
+ emit (r, END);
294
+ set_jump_offset (r, fixup, fixup - 2);
295
+ }
296
+ }
297
+
298
+ static void
299
+ compile (struct slre *r, const char **re)
300
+ {
301
+ int op, esc, branch_start, last_op, fixup, cap_no, level;
302
+
303
+ fixup = 0;
304
+ level = r->num_caps;
305
+ branch_start = last_op = r->code_size;
306
+
307
+ for (;;) {
308
+ switch (*(*re)++) {
309
+ case '\0':
310
+ (*re)--;
311
+ return;
312
+ case '^':
313
+ emit (r, BOL);
314
+ break;
315
+ case '$':
316
+ emit (r, EOL);
317
+ break;
318
+ case '.':
319
+ last_op = r->code_size;
320
+ emit (r, ANY);
321
+ break;
322
+ case '[':
323
+ last_op = r->code_size;
324
+ anyof (r, re);
325
+ break;
326
+ case '\\':
327
+ last_op = r->code_size;
328
+ esc = get_escape_char (re);
329
+ if (esc & 0xff00)
330
+ emit (r, esc >> 8);
331
+ else
332
+ exact_one_char (r, esc);
333
+ break;
334
+ case '(':
335
+ last_op = r->code_size;
336
+ cap_no = ++r->num_caps;
337
+ emit (r, OPEN);
338
+ emit (r, cap_no);
339
+
340
+ compile (r, re);
341
+ if (*(*re)++ != ')') {
342
+ r->err_str = "No closing bracket";
343
+ return;
344
+ }
345
+ emit (r, CLOSE);
346
+ emit (r, cap_no);
347
+ break;
348
+ case ')':
349
+ (*re)--;
350
+ fixup_branch (r, fixup);
351
+ if (level == 0) {
352
+ r->err_str = "Unbalanced brackets";
353
+ return;
354
+ }
355
+ return;
356
+ case '+':
357
+ case '*':
358
+ op = (*re) [-1] == '*' ? STAR: PLUS;
359
+ if (**re == '?') {
360
+ (*re)++;
361
+ op = op == STAR ? STARQ : PLUSQ;
362
+ }
363
+ quantifier (r, last_op, op);
364
+ break;
365
+ case '?':
366
+ quantifier (r, last_op, QUEST);
367
+ break;
368
+ case '|':
369
+ fixup_branch (r, fixup);
370
+ relocate (r, branch_start, 3);
371
+ r->code [branch_start] = BRANCH;
372
+ set_jump_offset (r, branch_start + 1, branch_start);
373
+ fixup = branch_start + 2;
374
+ r->code [fixup] = 0xff;
375
+ break;
376
+ default:
377
+ (*re)--;
378
+ last_op = r->code_size;
379
+ exact (r, re);
380
+ break;
381
+ }
382
+ }
383
+ }
384
+
385
+ int
386
+ slre_compile (struct slre *r, const char *re)
387
+ {
388
+ r->err_str = NULL;
389
+ r->code_size = r->data_size = r->num_caps = r->anchored = 0;
390
+ if (*re == '^')
391
+ r->anchored++;
392
+
393
+ emit (r, OPEN); /* This will capture what matches full RE */
394
+ emit (r, 0);
395
+ while (*re != '\0')
396
+ compile (r, &re);
397
+ if (r->code [2] == BRANCH)
398
+ fixup_branch (r, 4);
399
+ emit (r, CLOSE);
400
+ emit (r, 0);
401
+ emit (r, END);
402
+
403
+ return (r->err_str == NULL? 1: 0);
404
+ }
405
+
406
+ static int
407
+ match (const struct slre *, int, const char *, size_t, size_t *, struct cap *);
408
+
409
+ static void
410
+ loop_greedy (const struct slre *r, int pc, const char *s, size_t len, size_t *ofs)
411
+ {
412
+ size_t saved_offset, matched_offset;
413
+
414
+ saved_offset = matched_offset = *ofs;
415
+
416
+ while (match (r, pc + 2, s, len, ofs, NULL)) {
417
+ saved_offset = *ofs;
418
+ if (match (r, pc + r->code [pc + 1], s, len, ofs, NULL))
419
+ matched_offset = saved_offset;
420
+ *ofs = saved_offset;
421
+ }
422
+
423
+ *ofs = matched_offset;
424
+ }
425
+
426
+ static void
427
+ loop_non_greedy (const struct slre *r, int pc, const char *s, size_t len, size_t *ofs)
428
+ {
429
+ size_t saved_offset = *ofs;
430
+
431
+ while (match (r, pc + 2, s, len, ofs, NULL)) {
432
+ saved_offset = *ofs;
433
+ if (match (r, pc + r->code [pc + 1], s, len, ofs, NULL))
434
+ break;
435
+ }
436
+
437
+ *ofs = saved_offset;
438
+ }
439
+
440
+ static int
441
+ is_any_of (const unsigned char *p, int len, const char *s, size_t *ofs)
442
+ {
443
+ int i, ch;
444
+
445
+ ch = s [*ofs];
446
+
447
+ for (i = 0; i < len; i++)
448
+ if (p [i] == ch) {
449
+ (*ofs)++;
450
+ return (1);
451
+ }
452
+
453
+ return (0);
454
+ }
455
+
456
+ static int
457
+ is_any_but (const unsigned char *p, int len, const char *s, size_t *ofs)
458
+ {
459
+ int i, ch;
460
+
461
+ ch = s [*ofs];
462
+
463
+ for (i = 0; i < len; i++)
464
+ if (p [i] == ch)
465
+ return (0);
466
+
467
+ (*ofs)++;
468
+ return (1);
469
+ }
470
+
471
+ static int
472
+ match (const struct slre *r, int pc, const char *s, size_t len, size_t *ofs, struct cap *caps)
473
+ {
474
+ int res = 1;
475
+ size_t n, saved_offset;
476
+
477
+ while (res && r->code [pc] != END) {
478
+ assert (pc < r->code_size);
479
+ assert (pc < (int) (sizeof (r->code) / sizeof (r->code [0])));
480
+
481
+ switch (r->code [pc]) {
482
+ case BRANCH:
483
+ saved_offset = *ofs;
484
+ res = match (r, pc + 3, s, len, ofs, caps);
485
+ if (res == 0) {
486
+ *ofs = saved_offset;
487
+ res = match (r, pc + r->code [pc + 1], s, len, ofs, caps);
488
+ }
489
+ pc += r->code [pc + 2];
490
+ break;
491
+ case EXACT:
492
+ res = 0;
493
+ n = r->code [pc + 2]; /* String length */
494
+ if (n <= len - *ofs && !memcmp (s + *ofs, r->data +
495
+ r->code [pc + 1], n)) {
496
+ (*ofs) += n;
497
+ res = 1;
498
+ }
499
+ pc += 3;
500
+ break;
501
+ case QUEST:
502
+ res = 1;
503
+ saved_offset = *ofs;
504
+ if (!match (r, pc + 2, s, len, ofs, caps))
505
+ *ofs = saved_offset;
506
+ pc += r->code [pc + 1];
507
+ break;
508
+ case STAR:
509
+ res = 1;
510
+ loop_greedy (r, pc, s, len, ofs);
511
+ pc += r->code [pc + 1];
512
+ break;
513
+ case STARQ:
514
+ res = 1;
515
+ loop_non_greedy (r, pc, s, len, ofs);
516
+ pc += r->code [pc + 1];
517
+ break;
518
+ case PLUS:
519
+ if ((res = match (r, pc + 2, s, len, ofs, caps)) == 0)
520
+ break;
521
+ loop_greedy (r, pc, s, len, ofs);
522
+ pc += r->code [pc + 1];
523
+ break;
524
+ case PLUSQ:
525
+ if ((res = match (r, pc + 2, s, len, ofs, caps)) == 0)
526
+ break;
527
+
528
+ loop_non_greedy (r, pc, s, len, ofs);
529
+ pc += r->code [pc + 1];
530
+ break;
531
+ case SPACE:
532
+ res = 0;
533
+ if (*ofs < len && isspace (((unsigned char *)s) [*ofs])) {
534
+ (*ofs)++;
535
+ res = 1;
536
+ }
537
+ pc++;
538
+ break;
539
+ case NONSPACE:
540
+ res = 0;
541
+ if (*ofs <len && !isspace (((unsigned char *)s) [*ofs])) {
542
+ (*ofs)++;
543
+ res = 1;
544
+ }
545
+ pc++;
546
+ break;
547
+ case DIGIT:
548
+ res = 0;
549
+ if (*ofs < len && isdigit (((unsigned char *)s) [*ofs])) {
550
+ (*ofs)++;
551
+ res = 1;
552
+ }
553
+ pc++;
554
+ break;
555
+ case NONDIGIT:
556
+ res = 0;
557
+ if (*ofs < len && !isdigit (((unsigned char *)s) [*ofs])) {
558
+ (*ofs)++;
559
+ res = 1;
560
+ }
561
+ pc++;
562
+ break;
563
+ case ALPHA:
564
+ res = 0;
565
+ if (*ofs < len && isalpha (((unsigned char *)s) [*ofs])) {
566
+ (*ofs)++;
567
+ res = 1;
568
+ }
569
+ pc++;
570
+ break;
571
+ case NONALPHA:
572
+ res = 0;
573
+ if (*ofs < len && !isalpha (((unsigned char *)s) [*ofs])) {
574
+ (*ofs)++;
575
+ res = 1;
576
+ }
577
+ pc++;
578
+ break;
579
+ case ALNUM:
580
+ res = 0;
581
+ if (*ofs < len && isalnum (((unsigned char *)s) [*ofs])) {
582
+ (*ofs)++;
583
+ res = 1;
584
+ }
585
+ pc++;
586
+ break;
587
+ case NONALNUM:
588
+ res = 0;
589
+ if (*ofs < len && !isalnum (((unsigned char *)s) [*ofs])) {
590
+ (*ofs)++;
591
+ res = 1;
592
+ }
593
+ pc++;
594
+ break;
595
+ case ANY:
596
+ res = 0;
597
+ if (*ofs < len) {
598
+ (*ofs)++;
599
+ res = 1;
600
+ }
601
+ pc++;
602
+ break;
603
+ case ANYOF:
604
+ res = 0;
605
+ if (*ofs < len)
606
+ res = is_any_of (r->data + r->code [pc + 1], r->code [pc + 2], s, ofs);
607
+ pc += 3;
608
+ break;
609
+ case ANYBUT:
610
+ res = 0;
611
+ if (*ofs < len)
612
+ res = is_any_but (r->data + r->code [pc + 1], r->code [pc + 2], s, ofs);
613
+ pc += 3;
614
+ break;
615
+ case BOL:
616
+ res = *ofs == 0 ? 1 : 0;
617
+ pc++;
618
+ break;
619
+ case EOL:
620
+ res = *ofs == len ? 1 : 0;
621
+ pc++;
622
+ break;
623
+ case OPEN:
624
+ if (caps != NULL)
625
+ caps [r->code [pc + 1]].ptr = s + *ofs;
626
+ pc += 2;
627
+ break;
628
+ case CLOSE:
629
+ if (caps != NULL)
630
+ caps [r->code [pc + 1]].len = (s + *ofs) - caps [r->code [pc + 1]].ptr;
631
+ pc += 2;
632
+ break;
633
+ case END:
634
+ pc++;
635
+ break;
636
+ default:
637
+ printf ("unknown cmd (%d) at %d\n", r->code [pc], pc);
638
+ assert (0);
639
+ break;
640
+ }
641
+ }
642
+ return (res);
643
+ }
644
+
645
+ int
646
+ slre_match(const struct slre *r, const char *buf, size_t len, struct cap *caps)
647
+ {
648
+ int res = 0;
649
+ size_t ofs = 0;
650
+ if (r->anchored)
651
+ res = match (r, 0, buf, len, &ofs, caps);
652
+ else {
653
+ size_t i;
654
+ for (i = 0; i < len && res == 0; i++) {
655
+ ofs = i;
656
+ res = match (r, 0, buf, len, &ofs, caps);
657
+ }
658
+ }
659
+ return (res);
660
+ }