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 | |
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.
-rwxr-xr-x | testsuite/mdev.tests | 17 | ||||
-rw-r--r-- | util-linux/Config.in | 7 | ||||
-rw-r--r-- | util-linux/mdev.c | 44 |
3 files changed, 65 insertions, 3 deletions
diff --git a/testsuite/mdev.tests b/testsuite/mdev.tests index 777c5c540..81c440d1f 100755 --- a/testsuite/mdev.tests +++ b/testsuite/mdev.tests | |||
@@ -76,6 +76,23 @@ br--r--r-- 1 0 0 sda | |||
76 | " \ | 76 | " \ |
77 | "" "" | 77 | "" "" |
78 | 78 | ||
79 | # continuing to use directory structure from prev test | ||
80 | rm -rf mdev.testdir/dev/* | ||
81 | # here we complicate things by having non-matching group 1 and using %0 | ||
82 | echo "s([0-9])*d([a-z]+) 0:0 644 >sd/%2_%0" >mdev.testdir/etc/mdev.conf | ||
83 | testing "mdev regexp substring match + replace" \ | ||
84 | "env - ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1; | ||
85 | ls -lnR mdev.testdir/dev | $FILTER_LS2" \ | ||
86 | "\ | ||
87 | mdev.testdir/dev: | ||
88 | drwxr-xr-x 2 0 0 sd | ||
89 | lrwxrwxrwx 1 0 0 sda -> sd/a_sda | ||
90 | |||
91 | mdev.testdir/dev/sd: | ||
92 | brw-r--r-- 1 0 0 a_sda | ||
93 | " \ | ||
94 | "" "" | ||
95 | |||
79 | # clean up | 96 | # clean up |
80 | rm -rf mdev.testdir | 97 | rm -rf mdev.testdir |
81 | 98 | ||
diff --git a/util-linux/Config.in b/util-linux/Config.in index 1f4322bec..869ec61d5 100644 --- a/util-linux/Config.in +++ b/util-linux/Config.in | |||
@@ -321,6 +321,13 @@ config FEATURE_MDEV_RENAME | |||
321 | 321 | ||
322 | For more information, please see docs/mdev.txt | 322 | For more information, please see docs/mdev.txt |
323 | 323 | ||
324 | config FEATURE_MDEV_RENAME_REGEXP | ||
325 | bool "Support regular expressions substitutions when renaming device" | ||
326 | default n | ||
327 | depends on FEATURE_MDEV_RENAME | ||
328 | help | ||
329 | Add support for regular expressions substitutions when renaming device. | ||
330 | |||
324 | config FEATURE_MDEV_EXEC | 331 | config FEATURE_MDEV_EXEC |
325 | bool "Support command execution at device addition/removal" | 332 | bool "Support command execution at device addition/removal" |
326 | default n | 333 | default n |
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 | ||