Debian Codegraph @

ondir.c

raw symbols SCIP
1 #include <ctype.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <regex.h>
10 #include "conf.h"
11
12 /* Declarations */
13 struct odpath_t *root;
14
15 char *substitute(const char *in, const char *var, const char *val);
16 int add_envar(const char *var, const char *val);
17 int check_regex(const char *regex, const char *line, regmatch_t match[10]);
18 struct odpath_t *find_path(struct odpath_t *last, const char *path, pathtype_t type, regmatch_t match[10]);
19 void usage(const char *msg);
20
21 #ifdef USE_ONENTERLEAVE
22 void display_file(const char *path);
23 int exec_ok(const char *path);
24 #endif
25
26 /* Definitions */
27 int main(int argc, const char **argv) {
28 #ifdef USE_ONENTERLEAVE
29 char onenter[PATH_MAX + 1];
30 #endif
31 char working[PATH_MAX + 1], cwd[PATH_MAX + 1];
32 int len, i;
33 const char *src = NULL, *dst = NULL, *home = NULL;
34
35 if (argc < 2)
36 usage("Not enough arguments");
37
38 for (i = 1; i < argc; ++i) {
39 if (!strcmp(argv[i], "-V")) {
40 printf("This is OnDir version %s\n", VERSION);
41 return 0;
42 }
43
44 if (argv[i][0] == '-')
45 usage("Unknown argument");
46
47 if (!src) src = argv[i];
48 else
49 if (!dst) dst = argv[i];
50 else
51 usage("You have already specified src and dst directories");
52 }
53
54 if (!dst) {
55 getcwd(cwd, PATH_MAX);
56 dst = cwd;
57 }
58
59 if (src[0] != '/' || dst[0] != '/')
60 fatal("either the source or destination directory is not absolute");
61
62 root = load_conf(GLOBAL_CONF, root);
63
64 if ((home = getenv("HOME"))) {
65 snprintf(working, PATH_MAX, "%s/.ondirrc", home);
66 root = load_conf(working, root);
67 }
68
69 /*
70 The basic algorithm is thus:
71
72 1. Traverse up source path until it matches the lead of dest path
73 2. Traverse from the join to the end of dest path
74 */
75
76 strcpy(working, src);
77 len = strlen(working);
78
79 /* Traverse up source path */
80 while (strncmp(working, dst, len)) {
81 regmatch_t match[10];
82 struct odpath_t *p;
83
84 for (p = find_path(NULL, working, PT_LEAVE, match); p;
85 p = find_path(p, working, PT_LEAVE, match)) {
86 char *sub;
87
88 /* Construct temporary envars */
89 add_envar("ONDIRWD", working);
90 for (i = 0; match[i].rm_so != -1 && i < 10; ++i) {
91 int mlen = match[i].rm_eo - match[i].rm_so;
92 char var[4], val[mlen];
93
94 sprintf(var, "%i", i);
95 strncpy(val, working + match[i].rm_so, mlen);
96 val[mlen] = 0;
97 add_envar(var, val);
98 }
99 /* Expand envars */
100 sub = expand_envars(p->content);
101 /* Destroy temporary envars */
102 putenv("ONDIRWD");
103 for (i = 0; match[i].rm_so != -1 && i < 10; ++i) {
104 char var[4];
105
106 sprintf(var, "%i", i);
107 putenv(var);
108 }
109 printf("%s\n", sub);
110 free(sub);
111 if (p->final)
112 break;
113 }
114 #ifdef USE_ONENTERLEAVE
115 snprintf(onenter, PATH_MAX, "%s/.onleave", working);
116 if (exec_ok(onenter))
117 display_file(onenter);
118 #endif
119 while (len && working[len] != '/')
120 --len;
121 working[len] = 0;
122 }
123
124 if (dst[len]) ++len;
125
126 /* Move up to destination */
127 while (dst[len] != 0) {
128 regmatch_t match[10];
129 struct odpath_t *p;
130
131 while (dst[len] != 0 && dst[len] != '/')
132 ++len;
133 strncpy(working, dst, len);
134 working[len] = 0;
135 for (p = find_path(NULL, working, PT_ENTER, match); p;
136 p = find_path(p, working, PT_ENTER, match)) {
137 char *sub;
138 int i = 0;
139
140 /* Construct temporary envars */
141 add_envar("ONDIRWD", working);
142 for (i = 0; match[i].rm_so != -1 && i < 10; ++i) {
143 int mlen = match[i].rm_eo - match[i].rm_so;
144 char var[4], val[mlen];
145
146 sprintf(var, "%i", i);
147 strncpy(val, working + match[i].rm_so, mlen);
148 val[mlen] = 0;
149 add_envar(var, val);
150 }
151 /* Expand envars */
152 sub = expand_envars(p->content);
153 /* Destroy temporary envars */
154 putenv("ONDIRWD");
155 for (i = 0; match[i].rm_so != -1 && i < 10; ++i) {
156 char var[4];
157
158 sprintf(var, "%i", i);
159 putenv(var);
160 }
161 printf("%s", sub);
162 free(sub);
163 if (p->final)
164 break;
165 }
166 #ifdef USE_ONENTERLEAVE
167 snprintf(onenter, PATH_MAX, "%s/.onenter", working);
168 if (exec_ok(onenter))
169 display_file(onenter);
170 #endif
171 if (dst[len] == '/') ++len;
172 }
173
174 return 0;
175 }
176
177 char *substitute(const char *in, const char *var, const char *val) {
178 int inlen = strlen(in), outmax = inlen * 2, varlen = strlen(var),
179 vallen = strlen(val), outlen = 0;
180 char *out = calloc(1, outmax),
181 *var1 = calloc(1, varlen + 2),
182 *var2 = calloc(1, varlen + 4),
183 *offset1 = NULL, *offset2 = NULL;
184
185 /* Construct $VAR */
186 strcpy(var1, "$"); strcat(var1, var);
187
188 /* Construct ${VAR} */
189 strcpy(var2, "${"); strcat(var2, var); strcat(var2, "}");
190
191 /* Do the replacement */
192 while ((offset1 = strstr(in, var1)) || (offset2 = strstr(in, var2))) {
193 char *offset = offset1;
194 int vlen = varlen + 1;
195
196 if (!offset || (offset2 && offset2 < offset1)) offset = offset2;
197
198 if (offset[1] == '{') vlen += 2;
199
200 if (outlen + vlen > outmax) {
201 outmax *= 2;
202 out = realloc(out, outmax);
203 }
204
205 out[outlen] = 0;
206
207 /* Add difference between beginning of 'in' and 'offset' */
208 strncat(out, in, offset - in);
209 outlen += offset - in;
210 in = offset;
211
212 out[outlen] = 0;
213
214 strcat(out, val);
215 outlen += vallen;
216 in += vlen;
217
218 offset1 = offset2 = NULL;
219 }
220
221 strcat(out, in);
222 return out;
223 }
224
225 /* Check if a given regex matches a given line.
226 *
227 * Returns:
228 * 0 if they don't match.
229 * 1 if they match.
230 */
231 int check_regex(const char *regex, const char *line, regmatch_t match[10]) {
232 regex_t preg;
233 int retval = 0;
234
235 if (!regex || !line) {
236 /* FAILURE */
237 return (0);
238 }
239
240 if ((retval = regcomp(&preg, regex, REG_EXTENDED))) {
241 char err_buf[512];
242
243 regerror(retval, &preg, err_buf, 512);
244 fprintf(stderr, "ondir: regcomp failed, %s\n", err_buf);
245 return 0;
246 }
247
248 retval = regexec(&preg, line, 10, match, 0);
249 regfree(&preg);
250
251 return retval == 0 && match[0].rm_so == 0 && match[0].rm_eo == strlen(line);
252 }
253
254 struct odpath_t *find_path(struct odpath_t *last, const char *path, pathtype_t type, regmatch_t match[10]) {
255 struct odpath_t *i;
256 int j;
257
258 if (last)
259 last = last->next;
260 else
261 last = root;
262
263 for (i = last; i != NULL; i = i->next)
264 if (i->type == type) {
265 for (j = 0; j < i->npaths; ++j)
266 if (!strcmp(i->paths[j], path)) {
267 /* Emulate regex sub-pattern capturing */
268 match[0].rm_so = 0;
269 match[0].rm_eo = strlen(path);
270 match[1].rm_so = match[1].rm_eo = -1;
271 return i;
272 } else
273 if (check_regex(i->paths[j], path, match))
274 return i;
275 }
276 return NULL;
277 }
278
279 int add_envar(const char *var, const char *val) {
280 char *set = malloc(strlen(var) + strlen(val) + 2);
281
282 strcpy(set, var);
283 strcat(set, "=");
284 strcat(set, val);
285 return putenv(set);
286 }
287
288 #ifdef USE_ONENTERLEAVE
289 void display_file(const char *path) {
290 FILE *fp;
291
292 if ((fp = fopen(path, "r"))) {
293 char buffer[512];
294 int len;
295
296 while ((len = fread(buffer, 1, 512, fp)) > 0)
297 fwrite(buffer, len, 1, stdout);
298 fclose(fp);
299 }
300 }
301
302 int exec_ok(const char *path) {
303 struct stat s;
304
305 return
306 stat(path, &s) == 0 && /* exists.. */
307 s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH) && /* ...readable... */
308 s.st_uid == getuid() && /* ...owned by me... */
309 !(s.st_mode & (S_IWGRP | S_IWOTH)); /* ...not group/world writeable */
310 }
311 #endif
312
313 void usage(const char *msg) {
314 printf(
315 "%s\n"
316 "usage: ondir [-V] <old-directory> [<new-directory>]\n"
317 "\n"
318 " -V Displays version information.\n"
319 "\n"
320 " <old-directory> is the last working directory.\n"
321 "\n"
322 " <new-directory> is the current working directory. If <new-directory> is\n"
323 " omitted, the current working directory is obtained via a call to getcwd().\n"
324 "\n", msg
325 );
326 exit(1);
327 }
328