summaryrefslogtreecommitdiff
path: root/glob.c
blob: 1c21d25f14be1cf2aded99268961e3fa1124c729 (plain)
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
/************************************************

  glob.c -

  $Author$
  $Date$
  created at: Mon Sep 12 18:56:43 JST 1994

************************************************/

#include "ruby.h"
#include "fnmatch.h"
#include <sys/param.h>

char *strdup();

VALUE C_Glob;

struct glob_data {
    char **globs;
};

static ID id_data;

static void
glob_free(data)
    struct glob_data *data;
{
    char **globs = data->globs;
    while (*globs) {
	free(*globs);
	globs++;
    }
    free(data->globs);
}

#define isdelim(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\0')

char *strchr();
char *strdup();

static int
expand_brace(s, data, len)
    char *s;
    struct glob_data *data;
    int len;
{
    char org[MAXPATHLEN], path[MAXPATHLEN];
    char *pre, *post, *head, *p, *t;

    strcpy(org, s);
    pre  = strchr(org, '{');
    if (pre) post = strchr(pre, '}');
    if (!pre || !post) {
	data->globs[len++] = strdup(s);
	REALLOC_N(data->globs, char*, len+1);
	return len;
    }

    memcpy(path, org, pre - org);
    p = org + (pre - org) + 1;
    head = path + (pre - org);

    while (p < post) {
	t = p;
	while (t < post) {
	    if (*t == ',') break;
	    t++;
	}
	memcpy(head, p, t-p);
	strcpy(head+(t-p), post+1);
	len = expand_brace(path, data, len);
	p = t + 1;
    }
    return len;
}

static VALUE
glob_new0(class, str)
    VALUE class;
    struct RString *str;
{
    VALUE new;
    struct glob_data *data;
    char *p1, *p2, *pend, *s;
    int len = 0;

    new = obj_alloc(class);
    Make_Data_Struct(new, id_data, struct glob_data, Qnil, glob_free, data);
    data->globs = ALLOC_N(char*, 1);

    p1 = p2 = str->ptr;
    pend = p1 + str->len;
    while (p1 < pend) {
	char s[MAXPATHLEN];
	int d;

	while (isdelim(*p1)) p1++;
	p2 = p1;
	while (!isdelim(*p2)) p2++;
	d = p2 - p1;
	memcpy(s, p1, d);
	s[d] = '\0';
	len = expand_brace(s, data, len);
	p1 = p2;
    }
    data->globs[len] = Qnil;

    return new;
}

VALUE
glob_new(str)
    char *str;
{
    return glob_new0(C_Glob, str);
}

char **glob_filename();

static VALUE
Fglob_each(glob)
    VALUE glob;
{
    struct glob_data *data;
    char **patv, **fnames, **ff;

    Get_Data_Struct(glob, id_data, struct glob_data, data);
    for (patv = data->globs; *patv; patv++) {
	if (!glob_pattern_p(*patv)) {
	    rb_yield(str_new2(*patv));
	    continue;
	}
	fnames = ff = glob_filename(*patv);
	while (*ff) {
	    rb_yield(str_new2(*ff));
	    free(*ff);
	    ff++;
	}
	free(fnames);
    }
    return Qnil;
}

VALUE
Fglob_match(glob, str)
    VALUE glob;
    struct RString *str;
{
    struct glob_data *data;
    char **patv;

    Check_Type(str, T_STRING);
    Get_Data_Struct(glob, id_data, struct glob_data, data);
    patv = data->globs;
    while (*patv) {
	if (fnmatch(*patv, str->ptr, 0) != FNM_NOMATCH)
	    return TRUE;
	patv++;
    }
    return FALSE;
}

extern VALUE M_Enumerable;

Init_Glob()
{
    C_Glob = rb_define_class("Glob", C_Object);
    rb_include_module(C_Glob, M_Enumerable);

    rb_define_single_method(C_Glob, "new", glob_new0, 1);

    rb_define_method(C_Glob, "each", Fglob_each, 0);
    rb_define_method(C_Glob, "=~", Fglob_match, 1);

    id_data = rb_intern("data");
}