diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-29 17:26:10 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-29 17:26:10 +0000 |
commit | f2f3868e0d69c586d395dc9187e0ca99d5e47880 (patch) | |
tree | 213a441d3d61141d473e9a357f02378126978df4 /util-linux/mdev.c | |
parent | aa8a601084f7f0fc74fc561e12b42f8d7e3a6fe9 (diff) | |
download | busybox-w32-f2f3868e0d69c586d395dc9187e0ca99d5e47880.tar.gz busybox-w32-f2f3868e0d69c586d395dc9187e0ca99d5e47880.tar.bz2 busybox-w32-f2f3868e0d69c586d395dc9187e0ca99d5e47880.zip |
mdev: optional support for regex pattern group substitution.
+142 bytes.
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r-- | util-linux/mdev.c | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index c8d603bcf..9d77f6a1f 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -84,6 +84,8 @@ static void make_device(char *path, int delete) | |||
84 | goto end_parse; | 84 | goto end_parse; |
85 | 85 | ||
86 | while ((line = xmalloc_fgetline(fp)) != NULL) { | 86 | while ((line = xmalloc_fgetline(fp)) != NULL) { |
87 | regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; | ||
88 | |||
87 | ++lineno; | 89 | ++lineno; |
88 | trim(line); | 90 | trim(line); |
89 | if (!line[0]) | 91 | if (!line[0]) |
@@ -95,16 +97,24 @@ static void make_device(char *path, int delete) | |||
95 | next = next_field(line); | 97 | next = next_field(line); |
96 | { | 98 | { |
97 | regex_t match; | 99 | regex_t match; |
98 | regmatch_t off; | ||
99 | int result; | 100 | int result; |
100 | 101 | ||
101 | /* Is this it? */ | 102 | /* Is this it? */ |
102 | xregcomp(&match, line, REG_EXTENDED); | 103 | xregcomp(&match, line, REG_EXTENDED); |
103 | result = regexec(&match, device_name, 1, &off, 0); | 104 | result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0); |
104 | regfree(&match); | 105 | regfree(&match); |
105 | 106 | ||
107 | //bb_error_msg("matches:"); | ||
108 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { | ||
109 | // if (off[i].rm_so < 0) continue; | ||
110 | // bb_error_msg("match %d: '%.*s'\n", i, | ||
111 | // (int)(off[i].rm_eo - off[i].rm_so), | ||
112 | // device_name + off[i].rm_so); | ||
113 | //} | ||
114 | |||
106 | /* If not this device, skip rest of line */ | 115 | /* If not this device, skip rest of line */ |
107 | if (result || off.rm_so || off.rm_eo != strlen(device_name)) | 116 | /* (regexec returns whole pattern as "range" 0) */ |
117 | if (result || off[0].rm_so || off[0].rm_eo != strlen(device_name)) | ||
108 | goto next_line; | 118 | goto next_line; |
109 | } | 119 | } |
110 | 120 | ||
@@ -152,7 +162,35 @@ static void make_device(char *path, int delete) | |||
152 | val = next; | 162 | val = next; |
153 | next = next_field(val); | 163 | next = next_field(val); |
154 | if (*val == '>') { | 164 | if (*val == '>') { |
165 | #if ENABLE_FEATURE_MDEV_RENAME_REGEXP | ||
166 | /* substitute %1..9 with off[1..9], if any */ | ||
167 | char *s, *p; | ||
168 | unsigned i, n; | ||
169 | |||
170 | n = 0; | ||
171 | s = val; | ||
172 | while (*s && *s++ == '%') | ||
173 | n++; | ||
174 | |||
175 | p = alias = xzalloc(strlen(val) + n * strlen(device_name)); | ||
176 | s = val + 1; | ||
177 | while (*s) { | ||
178 | *p = *s; | ||
179 | if ('%' == *s) { | ||
180 | i = (s[1] - '0'); | ||
181 | if (i <= 9 && off[i].rm_so >= 0) { | ||
182 | n = off[i].rm_eo - off[i].rm_so; | ||
183 | strncpy(p, device_name + off[i].rm_so, n); | ||
184 | p += n - 1; | ||
185 | s++; | ||
186 | } | ||
187 | } | ||
188 | p++; | ||
189 | s++; | ||
190 | } | ||
191 | #else | ||
155 | alias = xstrdup(val + 1); | 192 | alias = xstrdup(val + 1); |
193 | #endif | ||
156 | } | 194 | } |
157 | } | 195 | } |
158 | 196 | ||