-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyREGEX_pats.c
839 lines (792 loc) · 62.8 KB
/
yREGEX_pats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*===============================[[ beg code ]]===============================*/
#include "yREGEX.h"
#include "yREGEX_priv.h"
/*> c = ,x···0·vf"··hd··f"··lD··0i { BASE_ENTRY, '-', ¥··A , 0 , ¥··40|··dw··0··,y <*/
/*> d = ,x···0·vf"··hd··$hD··k··dd··kÕJ··A , 0, NULL, NULL },¥··204|··dw··0··j·dd·dd·k··,y <*/
/*---(patterns)-------------*/
#define MAX_PATS 20
static const tPATS s_pats [MAX_PATS] = {
/* 0 1 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 1234 01234567890123456789 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
/*--source-- abbr ---name------------- ---pattern------------------------------------------------------------------------------------------------------------------------------------------------------- ln sz */
{ BASE_ENTRY, 'f', "float" , 0 , "(+|-)?(0|[1-9][0-9]*)([.][0-9]+)?" , 0, NULL, NULL },
{ BASE_ENTRY, 'b', "byte" , 0 , "25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]" , 0, NULL, NULL },
{ BASE_ENTRY, 'i', "int" , 0 , "(+|-)?(0|[1-9][0-9]*)" , 0, NULL, NULL },
{ BASE_ENTRY, 'w', "word" , 0 , "<\\w+>" , 0, NULL, NULL },
{ BASE_ENTRY, 'F', "field" , 0 , "\\F*\\f" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "path" , 0 , "[/]?[A-Za-z][/A-Za-z0-9_.-]*" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "mypath" , 0 , "[/]?[A-Za-z][/a-zA-Z0-9_.]*" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "email" , 0 , "[A-Za-z0-9_.-]+[@][A-Za-z0-9_.-]+.[A-Za-z]{2,4}" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "file" , 0 , "[.]?[A-Za-z][A-Za-z0-9_.-]{0,62}[A-Za-z0-9]" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "phone" , 0 , "([(]?[2-9][0-9]{2}[)]?[-. ])?[0-9]{3}[-. ][0-9]{4}" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "zip" , 0 , "[1-9][0-9]{4}([-][0-9]{4})?" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "ip" , 0 , "(&b)\\.(&b)\\.(&b)\\.(&b)" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "alnum" , 0 , "^[a-zA-Z0-9]+$" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "basic" , 0 , "^[A-Za-z0-9][a-zA-Z0-9_ -]*$" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "punct" , 0 , "^[A-Za-z0-9][A-Za-z0-9_ .,:;!?-]*$" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "exten" , 0 , "^[A-Za-z0-9][A-Za-z0-9_ .,:;@()-]*$" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "field" , 0 , "^[a-z][a-z0-9_]{0,10}[a-z0-9]$" , 0, NULL, NULL },
{ BASE_ENTRY, '-', "string" , 0 , "^[A-Za-z0-9][A-Za-z0-9+=&%][A-Za-z0-9]*$" , 0, NULL, NULL },
{ 0 , 0 , NULL , 0 , NULL , 0, NULL, NULL },
};
/*---(static.vars)------+-----------+-*//*-+----------------------------------*/
static tPATS *s_head = NULL; /* head of list */
static tPATS *s_tail = NULL; /* tail of list */
static tPATS *s_curr = NULL; /* curent cursor */
static int s_count = 0; /* count of sets */
/*====================------------------------------------====================*/
/*===---- allocation/memory ----===*/
/*====================------------------------------------====================*/
static void o___SUPPORT_________o () { return; }
char
yregex_pats__wipe (void *a_cur)
{
/*---(locals)-----------+-----+-----+-*/
tPATS *x_cur = NULL;
/*---(cast)---------------------------*/
x_cur = (tPATS *) a_cur;
/*---(wipe)---------------------------*/
x_cur->source = '-';
x_cur->abbr = '-';
x_cur->name = NULL;
x_cur->nlen = 0;
x_cur->pat = NULL;
x_cur->plen = 0;
x_cur->m_prev = NULL;
x_cur->m_next = NULL;
/*---(complete)-----------------------*/
return 0;
}
/*====================------------------------------------====================*/
/*===---- allocation/memory ----===*/
/*====================------------------------------------====================*/
static void o___MEMORY__________o () { return; }
char yregex_pats__new (void **r_new) { return yregex_share_new (TYPE_PATS, r_new, &s_head, &s_tail, &s_count); }
char yregex_pats__free (void **r_old) { return yregex_share_free (TYPE_PATS, r_old, &s_head, &s_tail, &s_count); }
/*====================------------------------------------====================*/
/*===---- program level ----===*/
/*====================------------------------------------====================*/
static void o___PROGRAM_________________o (void) {;}
char yregex_pats_init (void) { return yregex_share_init (TYPE_PATS, &s_head, &s_tail, &s_curr, &s_count); }
char yregex_pats__purge (void) { return yregex_share_purge (TYPE_PATS, &s_head, &s_tail, &s_curr, &s_count); }
char yregex_pats_wrap (void) { return yregex_share_wrap (TYPE_PATS, &s_head, &s_tail, &s_curr, &s_count); }
char
yregex_pats_reset (void)
{
yregex_pats__purge ();
yregex_pats__base ();
return 0;
}
/*====================------------------------------------====================*/
/*===---- creating new sets ----===*/
/*====================------------------------------------====================*/
static void o___CREATE__________________o (void) {;}
char
yregex_pats__base (void)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
int i = 0;
tPATS *x_new = NULL;
char t [LEN_LABEL];
int j = 0;
/*---(defense)------------------------*/
if (s_head != NULL) return 0;
/*---(header)-------------------------*/
DEBUG_YREGEX yLOG_enter (__FUNCTION__);
/*---(walk base)----------------------*/
for (i = 0; i < MAX_PATS; ++i) {
/*---(check for end)---------------*/
if (s_pats [i].abbr == 0) break;
/*---(add a new one)---------------*/
x_new = NULL;
rc = yregex_pats__new (&x_new);
if (x_new == NULL) continue;
/*---(populate)--------------------*/
x_new->source = s_pats [i].source;
x_new->abbr = s_pats [i].abbr;
x_new->name = s_pats [i].name;
x_new->nlen = strlen (s_pats [i].name);
x_new->pat = s_pats [i].pat;
x_new->plen = strlen (s_pats [i].pat);
/*---(done)------------------------*/
}
/*---(complete)-----------------------*/
DEBUG_YREGEX yLOG_exit (__FUNCTION__);
return 0;
}
char /*-> tbd --------------------------------[ leaf [fe.943.034.30]*/ /*-[01.0000.01#.!]-*/ /*-[--.---.---.--]-*/
yregex_pats__save (cchar *a_pattern, int a_num)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
int x_len = 0;
tPATS *x_new = NULL;
/*---(header)-------------------------*/
DEBUG_YREGEX yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
x_len = strllen (a_pattern, LEN_PAT );
DEBUG_YREGEX yLOG_value ("x_len" , x_len);
--rce; if (x_len < 1) {
DEBUG_YREGEX yLOG_note ("too short");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
--rce; if (x_len >= LEN_PAT) {
DEBUG_YREGEX yLOG_note ("too long");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(add a new one)------------------*/
rc = yregex_pats__new (&x_new);
DEBUG_YREGEX yLOG_point ("x_new" , x_new);
--rce; if (x_new == NULL) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(populate)-----------------------*/
x_new->source = 's';
x_new->abbr = '0' + a_num;
x_new->pat = strdup (a_pattern);
x_new->plen = strlen (a_pattern);
/*---(complete)-----------------------*/
DEBUG_YREGEX yLOG_exit (__FUNCTION__);
return s_count - 1;
}
/*====================------------------------------------====================*/
/*===---- finding patterns ----===*/
/*====================------------------------------------====================*/
static void o___FINDING_________________o (void) {;}
char yregex_pats__by_cursor (char a_move , tPATS **r_back) { return yregex_share__by_cursor (TYPE_PATS, &s_head, &s_tail, &s_curr, r_back, a_move); }
char yregex_pats__by_index (int a_index , tPATS **r_back) { return yregex_share__by_index (TYPE_PATS, &s_head, &s_curr, r_back, a_index); }
int yregex_pats__by_abbr (char a_abbr , tPATS **r_back) { return yregex_share__by_abbr (TYPE_PATS, &s_head, r_back, a_abbr); }
int yregex_pats__by_name (char *a_name, tPATS **r_back) { return yregex_share__by_name (TYPE_PATS, &s_head, r_back, a_name); }
/*> int /+-> retrieve pattern by abbeviation ----[ leaf [fc.632.122.40]+/ /+-[01.0000.01#.8]-+/ /+-[--.---.---.--]-+/ <*
*> yregex_pats__by_abbr (cchar a_abbr, tPATS **r_pat) <*
*> { <*
*> /+---(locals)-----------+-----+-----+-+/ <*
*> char rce = -10; <*
*> int n = 0; <*
*> char *x_valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; <*
*> tPATS *x_curr = NULL; <*
*> /+---(header)-------------------------+/ <*
*> DEBUG_YREGEX yLOG_senter (__FUNCTION__); <*
*> DEBUG_YREGEX yLOG_sint (a_abbr); <*
*> /+---(default)------------------------+/ <*
*> if (r_pat != NULL) *r_pat = NULL; <*
*> /+---(defense)------------------------+/ <*
*> --rce; if (a_abbr == 0) { <*
*> DEBUG_YREGEX yLOG_snote ("can not be null"); <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> --rce; if (strchr (x_valid, a_abbr) == NULL) { <*
*> DEBUG_YREGEX yLOG_snote ("abbr must be [A-Za-z]"); <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> DEBUG_YREGEX yLOG_schar (a_abbr); <*
*> /+---(walk through sets)--------------+/ <*
*> x_curr = s_head; <*
*> while (x_curr != NULL) { <*
*> if (x_curr->abbr == a_abbr) break; <*
*> ++n; <*
*> x_curr = x_curr->m_next; <*
*> } <*
*> /+---(check)--------------------------+/ <*
*> DEBUG_YREGEX yLOG_spoint (x_curr); <*
*> --rce; if (x_curr == NULL) { <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(saveback)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_snote ("found"); <*
*> if (r_pat != NULL) *r_pat = x_curr; <*
*> /+---(complete)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_sexit (__FUNCTION__); <*
*> return n; <*
*> } <*/
/*> int /+-> retrieve pattern by name -----------[ leaf [fe.A53.145.A0]+/ /+-[01.0000.02#.E]-+/ /+-[--.---.---.--]-+/ <*
*> yregex_pats__by_name (cchar *a_name, tPATS **r_pat) <*
*> { <*
*> /+---(locals)-----------+-----+-----+-+/ <*
*> char rce = -10; <*
*> int x_len = 0; <*
*> int n = 0; <*
*> tPATS *x_curr = NULL; <*
*> /+---(header)-------------------------+/ <*
*> DEBUG_YREGEX yLOG_senter (__FUNCTION__); <*
*> DEBUG_YREGEX yLOG_spoint (a_name); <*
*> /+---(default)------------------------+/ <*
*> if (r_pat != NULL) *r_pat = NULL; <*
*> /+---(defense)------------------------+/ <*
*> --rce; if (a_name == NULL) { <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> DEBUG_YREGEX yLOG_snote (a_name); <*
*> x_len = strllen (a_name, LEN_NAME); <*
*> DEBUG_YREGEX yLOG_sint (x_len); <*
*> --rce; if (x_len < 2) { <*
*> DEBUG_YREGEX yLOG_snote ("too short"); <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> --rce; if (x_len > 15) { <*
*> DEBUG_YREGEX yLOG_snote ("too long"); <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(walk through sets)--------------+/ <*
*> x_curr = s_head; <*
*> while (x_curr != NULL) { <*
*> if (strcmp (x_curr->name, a_name) == 0) break; <*
*> ++n; <*
*> x_curr = x_curr->m_next; <*
*> } <*
*> /+---(check)--------------------------+/ <*
*> DEBUG_YREGEX yLOG_spoint (x_curr); <*
*> --rce; if (x_curr == NULL) { <*
*> DEBUG_YREGEX yLOG_sexitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(saveback)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_snote ("found"); <*
*> if (r_pat != NULL) *r_pat = x_curr; <*
*> /+---(complete)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_sexit (__FUNCTION__); <*
*> return n; <*
*> } <*/
/*====================------------------------------------====================*/
/*===---- compilation support ----===*/
/*====================------------------------------------====================*/
static void o___COMPILE_________________o (void) {;}
/*> char /+-> compile a pattern ------------------[ ------ [fe.D54.156.65]+/ /+-[02.0000.01#.!]-+/ /+-[--.---.---.--]-+/ <*
*> yregex_pats__abbr_ref (int *a_rpos) <*
*> { <*
*> /+---(locals)-----------+-----+-----+-+/ <*
*> char rce = -10; <*
*> char rc = -1; <*
*> int i = 0; <*
*> int x_pat = 0; <*
*> char x_abbr = ' '; <*
*> char x_ch = ' '; <*
*> char x_pch = 0; <*
*> char x_ppch = 0; <*
*> /+---(header)-------------------------+/ <*
*> DEBUG_YREGEX yLOG_enter (__FUNCTION__); <*
*> DEBUG_YREGEX yLOG_value ("*a_rpos" , *a_rpos); <*
*> x_ch = myREGEX.orig [*a_rpos]; <*
*> DEBUG_YREGEX yLOG_char ("x_ch" , x_ch); <*
*> --rce; if (x_ch != '&') { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "no lead ampersand (&´)"); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> if (*a_rpos > 0) x_pch = myREGEX.orig [*a_rpos - 1]; <*
*> if (*a_rpos > 1) x_ppch = myREGEX.orig [*a_rpos - 2]; <*
*> /+---(find name)----------------------+/ <*
*> ++(*a_rpos); <*
*> x_abbr = myREGEX.orig [*a_rpos]; <*
*> /+---(abbreviated name)---------------+/ <*
*> DEBUG_YREGEX yLOG_note ("using abbreviated name"); <*
*> x_pat = yregex_pats__by_abbr (x_abbr, NULL); <*
*> DEBUG_YREGEX yLOG_value ("x_pat" , x_pat); <*
*> if (x_pat < 0) { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "not valid pattern abbreviation"); <*
*> ++(*a_rpos); <*
*> strlcat (myREGEX.regex, CAT_PAT, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(full name)----------------------+/ <*
*> ++(*a_rpos); <*
*> x_ch = myREGEX.orig [*a_rpos]; <*
*> if (x_pch == '(' && x_ch == ')') { <*
*> strlcat (myREGEX.regex, s_pats [x_pat].pat, LEN_REGEX); <*
*> } else if (x_ppch == '(' && x_pch == '#' && x_ch == ')') { <*
*> strlcat (myREGEX.regex, s_pats [x_pat].pat, LEN_REGEX); <*
*> } else { <*
*> strlcat (myREGEX.regex, "(" , LEN_REGEX); <*
*> strlcat (myREGEX.regex, s_pats [x_pat].pat, LEN_REGEX); <*
*> strlcat (myREGEX.regex, ")" , LEN_REGEX); <*
*> } <*
*> /+---(complete)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_exit (__FUNCTION__); <*
*> return 1; <*
*> } <*/
/*> char /+-> compile a pattern ------------------[ ------ [fe.D54.156.65]+/ /+-[02.0000.01#.!]-+/ /+-[--.---.---.--]-+/ <*
*> yregex_pats__named_ref (int *a_rpos) <*
*> { <*
*> /+---(locals)-----------+-----+-----+-+/ <*
*> char rce = -10; <*
*> char rc = -1; <*
*> int i = 0; <*
*> int x_pat = 0; <*
*> char x_name [LEN_NAME] = ""; <*
*> char x_ch = ' '; <*
*> int x_len = 0; <*
*> int x_beg = -1; <*
*> /+---(header)-------------------------+/ <*
*> DEBUG_YREGEX yLOG_enter (__FUNCTION__); <*
*> DEBUG_YREGEX yLOG_value ("*a_rpos" , *a_rpos); <*
*> x_ch = myREGEX.orig [*a_rpos]; <*
*> DEBUG_YREGEX yLOG_value ("x_ch" , x_ch); <*
*> --rce; if (x_ch != '&') { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "does not lead with an ampersand"); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> x_beg = *a_rpos; <*
*> /+---(find name)----------------------+/ <*
*> ++(*a_rpos); <*
*> --rce; for (i = 0; i < LEN_NAME; ++i) { <*
*> x_ch = myREGEX.orig [*a_rpos + i]; <*
*> DEBUG_YREGEX yLOG_value ("x_ch" , x_ch); <*
*> if (x_ch == ')') break; <*
*> x_name [i] = x_ch; <*
*> x_name [i + 1] = 0; <*
*> DEBUG_YREGEX yLOG_info ("x_name" , x_name); <*
*> ++x_len; <*
*> } <*
*> /+---(troubles)-----------------------+/ <*
*> --rce; if (x_len == 0) { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "could not parse name"); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> --rce; if (x_ch != ')') { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "could not find close paren"); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(abbreviated name)---------------+/ <*
*> --rce; if (x_len == 1) { <*
*> DEBUG_YREGEX yLOG_note ("using abbreviated name"); <*
*> x_pat = yregex_pats__by_abbr (x_name [0], NULL); <*
*> DEBUG_YREGEX yLOG_value ("x_pat" , x_pat); <*
*> if (x_pat < 0) { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "not a valid pattern abbreviation"); <*
*> ++(*a_rpos); <*
*> strlcat (myREGEX.regex, CAT_PAT, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> ++(*a_rpos); <*
*> strlcat (myREGEX.regex, s_pats [x_pat].pat, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_exit (__FUNCTION__); <*
*> return 1; <*
*> } <*
*> /+---(full name)----------------------+/ <*
*> --rce; <*
*> DEBUG_YREGEX yLOG_note ("trying pattern name"); <*
*> x_pat = yregex_pats__by_name (x_name, NULL); <*
*> DEBUG_YREGEX yLOG_value ("x_pat" , x_pat); <*
*> if (x_pat < 0) { <*
*> yregex_error_add ('F', CAT_PAT, 0, 0, "not a valid pattern name"); <*
*> *a_rpos += x_len; <*
*> strlcat (myREGEX.regex, CAT_PAT, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce); <*
*> return rce; <*
*> } <*
*> /+---(full name)----------------------+/ <*
*> *a_rpos += x_len; <*
*> strlcat (myREGEX.regex, s_pats [x_pat].pat, LEN_REGEX); <*
*> /+---(complete)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_exit (__FUNCTION__); <*
*> return 1; <*
*> } <*/
char
yregex_pats__replace (char *a_src, char a_run)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
int i = 0;
int j = 0;
int l = 0;
uchar x_char = ' '; /* curr character */
uchar x_near = ' '; /* prev character */
uchar x_prev = ' '; /* prev character */
uchar x_next = ' '; /* prev-prev character */
uchar x_far = ' '; /* prev-prev character */
int x_len = 0;
char t [LEN_REGEX] = "";
char x_done = '-';
char x_name [LEN_LABEL] = "";
tPATS *x_pat = NULL;
char x_dst [LEN_REGEX] = "";
char s [LEN_LABEL] = "";
/*---(header)-------------------------*/
DEBUG_YREGEX yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
DEBUG_YREGEX yLOG_point ("a_src" , a_src);
--rce; if (a_src == NULL) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
l = strlen (a_src);
DEBUG_YREGEX yLOG_complex ("a_src" , "%3då%sæ", l, a_src);
--rce; if (l <= 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
strlcpy (x_dst, "", LEN_REGEX);
/*---(walk regex)---------------------*/
--rce; for (i = 0; i < l; ++i) {
/*---(prepare)---------------------*/
if (i > 1) x_near = a_src [i - 2];
if (i > 0) x_prev = a_src [i - 1];
x_char = a_src [i];
x_next = a_src [i + 1];
x_far = a_src [i + 2];
DEBUG_YREGEX yLOG_complex ("LOOP" , "%3d, %c, %c, %c", i, x_prev, x_char, x_next);
sprintf (t, "%c", x_char);
/*---(filter)----------------------*/
if (x_char != '&') { strlcat (x_dst, t, LEN_REGEX); continue; }
if (x_prev == '(') ;
else if (x_prev == '#' && x_near == '(') ;
else { strlcat (x_dst, t, LEN_REGEX); continue; }
/*---(abbr)------------------------*/
if (x_far == ')') {
DEBUG_YREGEX yLOG_note ("finding an abbreviation-based pattern");
if (a_run == 0) {
if (strchr (YSTR_NUMBER, x_next) != NULL) {
DEBUG_YREGEX yLOG_note ("named, skipping in normal run");
strlcat (x_dst, t, LEN_REGEX);
continue;
}
if (strchr (YSTR_UPLOW, x_next) == NULL) {
yregex_error_add ('F', CAT_PAT, i - 1, 4, "invalid pattern abbreviation");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
} else {
if (strchr (YSTR_CHARS, x_next) == NULL) {
yregex_error_add ('F', CAT_PAT, i - 1, 4, "invalid pattern abbreviation");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
}
yregex_pats__by_abbr (x_next, &x_pat);
if (x_pat == NULL) {
yregex_error_add ('F', CAT_PAT, i - 1, 4, "pattern abbreviation not found");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
strlcat (x_dst, x_pat->pat, LEN_REGEX);
++i;
continue;
}
/*---(named)-----------------------*/
DEBUG_YREGEX yLOG_note ("finding a name-based pattern");
x_done = '-';
x_len = 0;
strlcpy (x_name, "", LEN_LABEL);
for (j = i + 1; j <= i + 11; ++j) {
if (a_src [j] == ')') { x_done = 'y'; break; }
if (strchr (YSTR_ALNUM, a_src [j]) == NULL) break;
sprintf (t, "%c", a_src [j]);
strlcat (x_name, t, LEN_LABEL);
++x_len;
}
DEBUG_YREGEX yLOG_info ("x_name" , x_name);
if (x_done != 'y') {
if (x_len >= 10) yregex_error_add ('F', CAT_PAT, i, 10, "pattern name too long (>10)");
else yregex_error_add ('F', CAT_PAT, j, 1, "illegal pattern name character");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
yregex_pats__by_name (x_name, &x_pat);
DEBUG_YREGEX yLOG_point ("x_pat" , x_pat);
if (x_pat == NULL) {
yregex_error_add ('F', CAT_PAT, i - 1, x_len + 3, "pattern name not found");
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
strlcat (x_dst, x_pat->pat, LEN_REGEX);
i += x_len;
continue;
/*---(done)------------------------*/
}
/*---(save-back)----------------------*/
strlcpy (myREGEX.regex, x_dst, LEN_REGEX);
myREGEX.rlen = strllen (myREGEX.regex, LEN_REGEX);
DEBUG_YREGEX yLOG_complex ("regex" , "%3då%sæ", myREGEX.rlen, myREGEX.regex);
/*---(complete)-----------------------*/
DEBUG_YREGEX yLOG_exit (__FUNCTION__);
return 0;
}
char
yregex_pats__named (char *a_src)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
int l = 0;
int i = 0;
int j = 0;
uchar x_char = ' '; /* curr character */
uchar x_prev = ' '; /* prev character */
uchar x_next = ' '; /* next character */
int x_len = 0;
char r [LEN_LABEL] = "";
char s [LEN_LABEL] = "";
char t [LEN_REGEX] = "";
int x_beg = 0;
int x_end = 0;
int n = 1;
int x_num = 0;
int x_nest = 0;
/*---(header)-------------------------*/
DEBUG_YREGEX yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
DEBUG_YREGEX yLOG_point ("a_src" , a_src);
--rce; if (a_src == NULL) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
l = strlen (a_src);
DEBUG_YREGEX yLOG_complex ("a_src" , "%3då%sæ", l, a_src);
--rce; if (l <= 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(walk regex)---------------------*/
--rce; for (i = 0; i < l; ++i) {
/*---(prepare)---------------------*/
if (i > 0) x_prev = a_src [i - 1];
x_char = a_src [i];
x_next = a_src [i + 1];
DEBUG_YREGEX yLOG_complex ("LOOP" , "%3d, %c, %c, %c", i, x_prev, x_char, x_next);
sprintf (t, "%c", x_char);
/*---(filter)----------------------*/
if (x_prev != '(') continue;
if (strchr ("#>", x_char) == NULL) continue;
/*---(find beg)--------------------*/
DEBUG_YREGEX yLOG_note ("found capture group, save pattern");
x_beg = i + 1;
DEBUG_YREGEX yLOG_value ("x_beg" , x_beg);
x_end = -1;
x_nest = 0;
strlcpy (r, "", LEN_REGEX);
/*---(find end)-----------------*/
for (j = x_beg; j < l; ++j) {
/*---(prepare)---------------*/
x_prev = a_src [j - 1];
x_char = a_src [j];
x_next = a_src [j + 1];
/*---(grouping)--------------*/
switch (x_char) {
case '(' :
++x_nest;
break;
case ')' :
if (x_nest == 0) {
if (x_prev == '<') { x_end = j - 2; x_num = 0; }
else { x_end = j - 1; x_num = n++; }
}
--x_nest;
}
if (x_end >= 0) break;
/*---(skipping)--------------*/
if (x_prev == '(' && strchr ("#>", x_char) != NULL) continue;
if (x_next == ')' && strchr ("<" , x_char) != NULL) continue;
/*---(adding chars)----------*/
sprintf (s, "%c", x_char);
strlcat (r, s, LEN_REGEX);
/*---(done)------------------*/
}
/*---(save pattern)-------------*/
DEBUG_YREGEX yLOG_value ("x_end" , x_end);
if (x_end >= 0) {
x_len = x_end - x_beg + 1;
DEBUG_YREGEX yLOG_value ("x_len" , x_len);
DEBUG_YREGEX yLOG_info ("r" , r);
yregex_pats__save (r, x_num);
}
}
/*---(complete)-----------------------*/
DEBUG_YREGEX yLOG_exit (__FUNCTION__);
return 0;
}
char
yregex_pats_comp (void)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
char t [LEN_REGEX] = "";
/*---(header)-------------------------*/
DEBUG_YREGEX yLOG_enter (__FUNCTION__);
yregex_pats_reset ();
/*---(double clean)-------------------*/
rc = yregex_pats__replace (myREGEX.regex, 0);
--rce; if (rc < 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
rc = yregex_pats__replace (myREGEX.regex, 0);
--rce; if (rc < 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(save named)---------------------*/
rc = yregex_pats__named (myREGEX.regex);
--rce; if (rc < 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(replace named)------------------*/
rc = yregex_pats__replace (myREGEX.regex, 1);
--rce; if (rc < 0) {
DEBUG_YREGEX yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(complete)-----------------------*/
DEBUG_YREGEX yLOG_exit (__FUNCTION__);
return 0;
}
/*> char <*
*> yregex_pats_comp_OLD (void) <*
*> { <*
*> /+---(locals)-----------+-----+-----+-+/ <*
*> char rce = -10; <*
*> char rc = 0; <*
*> int i = 0; <*
*> uchar x_ch = ' '; /+ curr character +/ <*
*> uchar x_pch = ' '; /+ prev character +/ <*
*> uchar x_ppch = ' '; /+ prev-prev character +/ <*
*> int x_prev = 0; <*
*> int x_len = 0; <*
*> char t [LEN_REGEX] = ""; <*
*> /+---(header)-------------------------+/ <*
*> DEBUG_YREGEX yLOG_enter (__FUNCTION__); <*
*> yregex_pats_reset (); <*
*> /+> yregex_pats__save_run (); <+/ <*
*> /+---(parse)--------------------------+/ <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.orig" , myREGEX.orig); <*
*> DEBUG_YREGEX yLOG_value ("myREGEX.olen" , myREGEX.olen); <*
*> strlcpy (myREGEX.regex , "", LEN_REGEX); <*
*> for (i = 0; i < myREGEX.olen; ++i) { <*
*> DEBUG_YREGEX yLOG_value ("LOOP" , i); <*
*> /+---(prepare)---------------------+/ <*
*> x_ppch = x_pch = x_ch = 0; <*
*> if (i > 1) x_ppch = myREGEX.orig [i - 2]; <*
*> if (i > 0) x_pch = myREGEX.orig [i - 1]; <*
*> x_ch = myREGEX.orig [i]; <*
*> /+---(named pattern)---------------+/ <*
*> if (( x_ppch == '(' && x_pch == '#' && x_ch == '&') || <*
*> (x_pch == '(' && x_ch == '&')) { <*
*> DEBUG_YREGEX yLOG_note ("found named potenial pattern"); <*
*> x_len = i - x_prev; <*
*> DEBUG_YREGEX yLOG_complex ("stats" , "beg %3d, end %3d, len %3d", x_prev, i - 1, x_len); <*
*> sprintf (t, "%-*.*s", x_len, x_len, myREGEX.orig + x_prev); <*
*> DEBUG_YREGEX yLOG_info ("t" , t); <*
*> strlcat (myREGEX.regex, t, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.regex" , myREGEX.regex); <*
*> rc = yregex_pats__named_ref (&i); <*
*> x_prev = i; <*
*> DEBUG_YREGEX yLOG_value ("x_prev" , x_prev); <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.regex" , myREGEX.regex); <*
*> myREGEX.rlen = strllen (myREGEX.regex, LEN_REGEX); <*
*> } <*
*> /+---(named pattern)---------------+/ <*
*> else if (x_pch != '\\' && x_ch == '&') { <*
*> DEBUG_YREGEX yLOG_note ("found abbr pattern"); <*
*> x_len = i - x_prev; <*
*> DEBUG_YREGEX yLOG_complex ("stats" , "beg %3d, end %3d, len %3d", x_prev, i - 1, x_len); <*
*> sprintf (t, "%-*.*s", x_len, x_len, myREGEX.orig + x_prev); <*
*> DEBUG_YREGEX yLOG_info ("t" , t); <*
*> strlcat (myREGEX.regex, t, LEN_REGEX); <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.regex" , myREGEX.regex); <*
*> rc = yregex_pats__abbr_ref (&i); <*
*> x_prev = i; <*
*> DEBUG_YREGEX yLOG_value ("x_prev" , x_prev); <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.regex" , myREGEX.regex); <*
*> myREGEX.rlen = strllen (myREGEX.regex, LEN_REGEX); <*
*> } <*
*> /+---(done)------------------------+/ <*
*> } <*
*> /+---(copy last)----------------------+/ <*
*> DEBUG_YREGEX yLOG_complex ("stats" , "beg %3d", x_prev); <*
*> sprintf (t, "%s", myREGEX.orig + x_prev); <*
*> DEBUG_YREGEX yLOG_info ("t" , t); <*
*> strlcat (myREGEX.regex, t, LEN_REGEX); <*
*> myREGEX.rlen = strllen (myREGEX.regex, LEN_REGEX); <*
*> /+---(show)---------------------------+/ <*
*> DEBUG_YREGEX yLOG_info ("myREGEX.regex" , myREGEX.regex); <*
*> DEBUG_YREGEX yLOG_value ("myREGEX.rlen" , myREGEX.rlen); <*
*> /+---(complete)-----------------------+/ <*
*> DEBUG_YREGEX yLOG_exit (__FUNCTION__); <*
*> return 0; <*
*> } <*/
/*====================------------------------------------====================*/
/*===---- unit testing ----===*/
/*====================------------------------------------====================*/
static void o___UNITTEST________________o (void) {;}
char*
yregex_pats__memory (void *a_cur)
{
/*---(locals)-----------+-----+-----+-*/
int n = 0;
tPATS *x_cur = NULL;
/*---(cast)---------------------------*/
x_cur = (tPATS *) a_cur;
/*---(defense)------------------------*/
if (x_cur == NULL) {
strlcpy (g_print, "n/a", LEN_RECD);
return g_print;
}
/*---(defense)------------------------*/
strlcpy (g_print, "å_.___.__.__æ", LEN_RECD);
++n; if (x_cur->source != '-') g_print [n] = 'X';
++n;
++n; if (x_cur->abbr != '-') g_print [n] = 'X';
++n; if (x_cur->name != NULL) g_print [n] = 'X';
++n; if (x_cur->nlen != 0) g_print [n] = 'X';
++n;
++n; if (x_cur->pat != NULL) g_print [n] = 'X';
++n; if (x_cur->plen != 0) g_print [n] = 'X';
++n;
++n; if (x_cur->m_prev != NULL) g_print [n] = 'X';
++n; if (x_cur->m_next != NULL) g_print [n] = 'X';
return g_print;
}
char* /*-> unit test accessor -----------------[ light [us.D90.241.L0]*/ /*-[03.0000.00#.#]-*/ /*-[--.---.---.--]-*/
yregex_pats__unit (char *a_question, int a_num)
{
/*---(locals)-----------+-----+-----+-*/
int i = 0;
char r [LEN_TERSE] = "";
char s [LEN_TERSE] = "";
char t [LEN_HUND] = "";
int c = 0;
tPATS *x_curr = NULL;
/*---(initialize)---------------------*/
strlcpy (unit_answer, "PATS unit, unknown request", 100);
/*---(mapping)------------------------*/
if (strcmp (a_question, "count" ) == 0) {
yregex_share__unit (TYPE_PATS, s_head, s_tail, s_count, "count", 0);
}
else if (strcmp (a_question, "list" ) == 0) {
yregex_share__unit (TYPE_PATS, s_head, s_tail, s_count, "list" , 0);
}
else if (strcmp (a_question, "entry" ) == 0) {
yregex_pats__by_index (a_num, &x_curr);
if (x_curr != NULL) {
sprintf (r, "%2då%.10sæ", x_curr->nlen, x_curr->name);
sprintf (s, "%2då%.40sæ", x_curr->plen , x_curr->pat);
snprintf (unit_answer, LEN_RECD, "PATS entry (%2d) : %c %c %-14.14s %s",
a_num, x_curr->source, x_curr->abbr, r, s);
} else {
snprintf (unit_answer, LEN_RECD, "PATS entry (%2d) : - - -åæ -åæ", a_num);
}
return unit_answer;
}
/*---(complete)-----------------------*/
return unit_answer;
}
/*===============================[[ end code ]]===============================*/