File: glob.c

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (160 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download
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
/*
 * match -- returns 1 if `string' satisfised `regex' and 0 otherwise
 * stolen from Spencer Sun: only recognizes * and \ as special characters
 */

#include "tintin.h"

bool match(const char *regex, const char *string)
{
    const char *rp = regex, *sp = string, *save;
    char ch;

    while (*rp)
    {
        switch (ch = *rp++)
        {
        case '*':
            if (!*sp)           /* match empty string at end of `string' */
                return !*rp;    /* but only if we're done with the pattern */
            /* greedy algorithm: save starting location, then find end of string */
            save = sp;
            sp += strlen(sp);
            do
            {
                if (match(rp, sp))     /* return success if we can match here */
                    return 1;
            } while (--sp >= save);    /* otherwise back up and try again */
            /*
             * Backed up all the way to starting location (i.e. `*' matches
             * empty string) and we _still_ can't match here.  Give up.
             */
            return false;
        case '\\':
            if ((ch = *rp++))
            {
                /* if not end of pattern, match next char explicitly */
                if (ch != *sp++)
                    return false;
                break;
            }
            /* else FALL THROUGH to match a backslash */
        default:                       /* normal character */
            if (ch != *sp++)
                return false;
            break;
        }
    }
    /*
     * OK, we successfully matched the pattern if we got here.  Now return
     * a match if we also reached end of string, otherwise failure
     */
    return !*sp;
}


bool is_literal(const char *txt)
{
    return !strchr(txt, '*') && !strchr(txt, '\\');
}


bool find(const char *text, const char *pattern, int *from, int *to, const char *fastener)
{
    const char *txt;
    char *a, *b, *pat, m1[BUFFER_SIZE], m2[BUFFER_SIZE];
    int i;

    if (fastener)
    {
        txt=strstr(text, fastener);
        if (!txt)
            return false;
        *from=txt-text;
        if (strchr(pattern, '*'))
            *to=strlen(text)-1;
        else
            *to=*from+strlen(fastener)-1;
        return true;
    }

    txt=text;
    if (*pattern=='^')
    {
        for (pattern++;(*pattern)&&(*pattern!='*');)
            if (*(pattern++)!=*(txt++))
                return false;
        if (!*pattern)
        {
            *from=0;
            *to=txt-text-1;
            return true;
        }
        strcpy(m1, pattern);
        pat=m1;
        goto start;
    }
    if (!(b=strchr(pattern, '*')))
    {
        a=strstr(txt, pattern);
        if (a)
        {
            *from=a-text;
            *to=*from+strlen(pattern)-1;
            return true;
        }
        else
            return false;
    }
    i=b-pattern;
    strcpy(m1, pattern);
    m1[i]=0;
    pat=m1;
    txt=strstr(txt, pat);
    if (!txt)
        return false;
    *from=txt-text;
    txt+=i;
    pat+=i+1;
    while (*pat=='*')
        pat++;
start:
    i=strlen(pat);
    if (!*pat)
    {
        *to=strlen(text)-1;
        return true;
    }
    a=pat;
    b=pat+i-1;
    while (a<b)
    {
        char c=*a;
        *a++=*b;
        *b--=c;
    }
    i=strlen(txt);
    for (a=m2+i-1;*txt;)
        *a--=*txt++;
    m2[i]=0;
    txt=m2;
    *to=-1;
    do
    {
        b=strchr(pat, '*');
        if (b)
            *b=0;
        a=strstr(txt, pat);
        if (!a)
            return false;
        if (*to==-1)
            *to=strlen(text)-(a-txt)-1;
        int len=strlen(pat);
        txt=a+len;
        if (b)
            pat=b;
        else
            pat+=len;
    } while (*pat);
    return true;
}