~ubuntu-branches/debian/sid/tintin++/sid

« back to all changes in this revision

Viewing changes to src/tinexp.c

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-11-12 22:57:57 UTC
  • mfrom: (1.2.19 upstream) (2.2.9 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091112225757-thi53t10ywdoc63i
* New upstream release.
* Switch to 3.0 (quilt) source format. No changes required further adding
  debian/source/format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
*                     recoded by Igor van den Hoven 2004                      *
27
27
******************************************************************************/
28
28
 
29
 
#include <regex.h>
30
29
#include <sys/types.h>
31
30
#include <pcre.h>
32
31
 
35
34
 
36
35
int match(struct session *ses, char *str, char *exp)
37
36
{
38
 
        char expbuf[BUFFER_SIZE], tmpbuf[BUFFER_SIZE];
39
 
 
40
 
        substitute(ses, exp, tmpbuf, SUB_VAR|SUB_FUN);
41
 
 
42
 
        sprintf(expbuf, "^%s$", tmpbuf);
43
 
 
 
37
        char expbuf[BUFFER_SIZE];
 
38
 
 
39
        if (ses)
 
40
        {
 
41
                sprintf(expbuf, "^%s$", exp);
 
42
                
 
43
                substitute(ses, expbuf, expbuf, SUB_VAR|SUB_FUN);
 
44
        }
 
45
        else
 
46
        {
 
47
                sprintf(expbuf, "^%s$", exp);
 
48
        }
44
49
        return tintin_regexp(NULL, str, expbuf, 0, 0);
45
50
}
46
51
 
48
53
{
49
54
        char expbuf[BUFFER_SIZE];
50
55
 
51
 
        substitute(ses, exp, expbuf, SUB_VAR|SUB_FUN);
 
56
        if (ses)
 
57
        {
 
58
                substitute(ses, exp, expbuf, SUB_VAR|SUB_FUN);
52
59
 
53
 
        return tintin_regexp(NULL, str, expbuf, 0, 0);
 
60
                return tintin_regexp(NULL, str, expbuf, 0, 0);
 
61
        }
 
62
        else
 
63
        {
 
64
                return tintin_regexp(NULL, str, exp, 0, 0);
 
65
        }
54
66
}
55
67
 
56
68
DO_COMMAND(do_regexp)
249
261
                                                {
250
262
                                                        *pto++ = *pti++;
251
263
                                                }
252
 
                                                pti++;
253
264
 
 
265
                                                if (pti[1] == DEFAULT_OPEN || isalnum(pti[1]))
 
266
                                                {
 
267
                                                        pti++;
 
268
                                                }
 
269
                                                else
 
270
                                                {
 
271
                                                        *pto++ = *pti++;
 
272
                                                }
254
273
                                                continue;
255
274
                                        }
256
275
 
583
602
                                                {
584
603
                                                        *pto++ = *pti++;
585
604
                                                }
586
 
                                                pti++;
587
605
 
 
606
                                                if (pti[1] == DEFAULT_OPEN || isalnum(pti[1]))
 
607
                                                {
 
608
                                                        pti++;
 
609
                                                }
 
610
                                                else
 
611
                                                {
 
612
                                                        *pto++ = *pti++;
 
613
                                                }
588
614
                                                continue;
589
615
                                        }
590
616
 
730
756
}
731
757
 
732
758
/*
733
 
        Keep synched with tintin_regexp_compile
 
759
        Keep synched with tintin_regexp and tintin_regexp_compile
734
760
*/
735
761
 
 
762
int tintin_regexp_check(char *exp)
 
763
{
 
764
        if (*exp == '^')
 
765
        {
 
766
                return TRUE;
 
767
        }
 
768
 
 
769
        while (*exp)
 
770
        {
 
771
#ifdef BIG5
 
772
                if (*exp & 0x80)
 
773
                {
 
774
                        exp += 2;
 
775
                        continue;
 
776
                }
 
777
#endif          
 
778
                switch (exp[0])
 
779
                {
 
780
                        case '\\':
 
781
                        case '{':
 
782
                                return TRUE;
 
783
 
 
784
                        case '$':
 
785
                                if (exp[1] == 0)
 
786
                                {
 
787
                                        return TRUE;
 
788
                                }
 
789
                                break;
 
790
 
 
791
                        case '%':
 
792
                                switch (exp[1])
 
793
                                {
 
794
                                        case '0':
 
795
                                        case '1':
 
796
                                        case '2':
 
797
                                        case '3':
 
798
                                        case '4':
 
799
                                        case '5':
 
800
                                        case '6':
 
801
                                        case '7':
 
802
                                        case '8':
 
803
                                        case '9':
 
804
                                        case 'd':
 
805
                                        case 'D':
 
806
                                        case 'i':
 
807
                                        case 'I':
 
808
                                        case 's':
 
809
                                        case 'S':
 
810
                                        case 'w':
 
811
                                        case 'W':
 
812
                                        case '?':
 
813
                                        case '*':
 
814
                                        case '+':
 
815
                                        case '.':
 
816
                                        case '%':
 
817
                                                return TRUE;
 
818
                                }
 
819
                                break;
 
820
                }
 
821
                exp++;
 
822
        }
 
823
        return FALSE;
 
824
}
 
825
                                
736
826
int tintin_regexp(pcre *nodepcre, char *str, char *exp, int option, int flag)
737
827
{
738
828
        char out[BUFFER_SIZE], *pti, *pto;
741
831
        pti = exp;
742
832
        pto = out;
743
833
 
 
834
        while (*pti == '^')
 
835
        {
 
836
                *pto++ = *pti++;
 
837
        }
 
838
 
744
839
        while (*pti)
745
840
        {
746
841
#ifdef BIG5
775
870
                        case '?':
776
871
                        case '+':
777
872
                        case '*':
 
873
                        case '^':
778
874
                                *pto++ = '\\';
779
875
                                *pto++ = *pti++;
780
876
                                break;
781
877
 
 
878
                        case '$':
 
879
                                if (pti[1] != DEFAULT_OPEN && !isalnum(pti[1]))
 
880
                                {
 
881
                                        int i = 0;
 
882
 
 
883
                                        while (pti[++i] == '$')
 
884
                                        {
 
885
                                                continue;
 
886
                                        }
 
887
 
 
888
                                        if (pti[i])
 
889
                                        {
 
890
                                                *pto++ = '\\';
 
891
                                        }
 
892
                                }
 
893
                                *pto++ = *pti++;
 
894
                                break;
 
895
 
782
896
                        case '%':
783
897
                                switch (pti[1])
784
898
                                {
916
1030
                SET_BIT(node->flags, NODE_FLAG_META);
917
1031
        }
918
1032
 
 
1033
        while (*pti == '^')
 
1034
        {
 
1035
                *pto++ = *pti++;
 
1036
        }
 
1037
 
919
1038
        while (*pti)
920
1039
        {
921
1040
 
952
1071
                                break;
953
1072
 
954
1073
                        case '&':
 
1074
                                if (pti[1] == DEFAULT_OPEN || isalnum(pti[1]) || pti[1] == '&')
 
1075
                                {
 
1076
                                        return NULL;
 
1077
                                }
 
1078
                                *pto++ = *pti++;
 
1079
                                break;
 
1080
 
 
1081
                        case '@':
 
1082
                                if (pti[1] == DEFAULT_OPEN || isalnum(pti[1]) || pti[1] == '@')
 
1083
                                {
 
1084
                                        return NULL;
 
1085
                                }
 
1086
                                *pto++ = *pti++;
 
1087
                                break;
 
1088
 
955
1089
                        case '$':
956
 
                        case '@':
957
 
                                if (pti[1])
 
1090
                                if (pti[1] == DEFAULT_OPEN || isalnum(pti[1]))
958
1091
                                {
959
1092
                                        return NULL;
960
1093
                                }
 
1094
                                {
 
1095
                                        int i = 0;
 
1096
 
 
1097
                                        while (pti[++i] == '$')
 
1098
                                        {
 
1099
                                                continue;
 
1100
                                        }
 
1101
 
 
1102
                                        if (pti[i])
 
1103
                                        {
 
1104
                                                *pto++ = '\\';
 
1105
                                        }
 
1106
                                }
961
1107
                                *pto++ = *pti++;
962
1108
                                break;
963
1109
 
970
1116
                        case '?':
971
1117
                        case '+':
972
1118
                        case '*':
 
1119
                        case '^':
973
1120
                                *pto++ = '\\';
974
1121
                                *pto++ = *pti++;
975
1122
                                break;