Annotation of src/usr.bin/cvs/trigger.c, Revision 1.5
1.5 ! joris 1: /* $OpenBSD: trigger.c,v 1.4 2008/06/10 02:36:31 joris Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2008 Tobias Stoeckmann <tobias@openbsd.org>
4: * Copyright (c) 2008 Jonathan Armani <dbd@asystant.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/queue.h>
20: #include <sys/types.h>
21:
22: #include <ctype.h>
23: #include <errno.h>
1.3 joris 24: #include <libgen.h>
1.1 joris 25: #include <regex.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
31: #include "config.h"
32: #include "cvs.h"
33:
34: static int expand_args(BUF *, struct file_info_list *, const char *,
35: const char *, char *);
36: static char *parse_cmd(int, char *, const char *, struct file_info_list *);
37:
38: static int
39: expand_args(BUF *buf, struct file_info_list *file_info, const char *repo,
40: const char *allowed_args, char *format)
41: {
42: int oldstyle, quote;
43: struct file_info *fi = NULL;
44: char *p, valbuf[2] = { '\0', '\0' };
45: const char *val;
46:
47: if (file_info != NULL && !TAILQ_EMPTY(file_info))
48: fi = TAILQ_FIRST(file_info);
49:
50: quote = oldstyle = 0;
51:
52: /* Why does GNU cvs print something if it encounters %{}? */
53: if (*format == '\0')
54: oldstyle = 1;
55:
56: for (p = format; *p != '\0'; p++) {
57: if (*p != '%' && strchr(allowed_args, *p) == NULL)
58: return 1;
59:
60: switch (*p) {
61: case 's':
62: case 'V':
63: case 'v':
64: quote = 1;
65: oldstyle = 1;
66: break;
67: default:
68: break;
69: }
70: }
71: if (quote)
72: cvs_buf_putc(buf, '"');
73: if (oldstyle) {
74: cvs_buf_append(buf, repo, strlen(repo));
75: cvs_buf_putc(buf, ' ');
76: }
77:
78: if (*format == '\0')
79: return 0;
80:
81: for (;;) {
82: for (p = format; *p != '\0';) {
83: val = NULL;
84:
85: switch (*p) {
86: case '%':
87: val = "%";
88: break;
89: case 'b':
90: if (fi != NULL) {
91: valbuf[0] = fi->tag_type;
92: val = valbuf;
93: }
94: break;
95: case 'o':
96: if (fi != NULL)
97: val = fi->tag_op;
98: break;
99: case 'p':
100: val = current_cvsroot->cr_dir;
101: break;
102: case 'r':
103: val = repo;
104: break;
105: case 'l':
106: case 'S':
107: case 's':
108: if (fi != NULL)
109: val = fi->file_path;
110: break;
111: case 't':
112: if (fi != NULL)
113: val = fi->tag_new;
114: break;
115: case 'V':
1.5 ! joris 116: if (fi != NULL) {
! 117: if (!strcmp(fi->crevstr,
! 118: "Non-existent"))
! 119: val = "NONE";
! 120: else
! 121: val = fi->crevstr;
! 122: }
1.1 joris 123: break;
124: case 'v':
125: if (fi != NULL)
126: val = fi->nrevstr;
127: break;
128: default:
129: return 1;
130: }
131:
132: if (val != NULL)
133: cvs_buf_append(buf, val, strlen(val));
134:
135: if (*(++p) != '\0')
136: cvs_buf_putc(buf, ',');
137: }
138:
139: if (fi != NULL)
140: fi = TAILQ_NEXT(fi, flist);
141: if (fi == NULL)
142: break;
143:
144: if (strlen(format) == 1 && (*format == '%' || *format == 'o' ||
145: *format == 'p' || *format == 'r' || *format == 't'))
146: break;
147:
148: cvs_buf_putc(buf, ' ');
149: }
150:
151: if (quote)
152: cvs_buf_putc(buf, '"');
153:
154: return 0;
155: }
156:
157: static char *
158: parse_cmd(int type, char *cmd, const char *repo,
159: struct file_info_list *file_info)
160: {
161: int expanded = 0;
162: char argbuf[2] = { '\0', '\0' };
163: char *allowed_args, *default_args, *args, *p, *q = NULL;
164: size_t pos;
165: BUF *buf;
166:
167: switch (type) {
168: case CVS_TRIGGER_COMMITINFO:
169: allowed_args = "prsS{}";
170: default_args = " %p/%r %S";
171: break;
172: case CVS_TRIGGER_LOGINFO:
1.4 joris 173: allowed_args = "prsSvVt{}";
1.1 joris 174: default_args = NULL;
175: break;
176: case CVS_TRIGGER_VERIFYMSG:
177: allowed_args = "l";
178: default_args = " %l";
179: break;
180: case CVS_TRIGGER_TAGINFO:
181: allowed_args = "btoprsSvV{}";
182: default_args = " %t %o %p/%r %{sv}";
183: break;
184: default:
185: return (NULL);
186: }
187:
188: /* XXX move this out of this function */
189: /* before doing any stuff, check if the command starts with % */
190: for (p = cmd; *p != '%' && !isspace(*p) && *p != '\0'; p++)
191: ;
192: if (*p == '%')
193: return (NULL);
194:
195: buf = cvs_buf_alloc(1024);
196:
197: p = cmd;
198: again:
199: for (; *p != '\0'; p++) {
200: if ((pos = strcspn(p, "%")) != 0) {
201: cvs_buf_append(buf, p, pos);
202: p += pos;
203: }
204:
205: if (*p++ == '\0')
206: break;
207:
208: q = NULL;
209: switch (*p) {
210: case '\0':
211: goto bad;
212: case '{':
213: /* XXX do we realy have to check for { in there? */
214: if (strchr(allowed_args, '{') == NULL)
215: goto bad;
216: pos = strcspn(++p, "}");
217: if (p[pos] == '\0')
218: goto bad;
219: q = xmalloc(pos + 1);
220: memcpy(q, p, pos);
221: q[pos] = '\0';
222: args = q;
223: p += pos;
224: break;
225: default:
226: argbuf[0] = *p;
227: args = argbuf;
228: break;
229: }
230:
231: if (expand_args(buf, file_info, repo, allowed_args, args))
232: goto bad;
233: expanded = 1;
234:
235: if (q != NULL)
236: xfree(q);
237: }
238:
239: if (!expanded && default_args != NULL) {
240: p = default_args;
241: expanded = 1;
242: goto again;
243: }
244:
245: cvs_buf_putc(buf, '\0');
246: return (cvs_buf_release(buf));
247:
248: bad:
249: if (q != NULL)
250: xfree(q);
251: cvs_log(LP_NOTICE, "CVSROOT: malformed line %s", cmd);
252: cvs_buf_free(buf);
253: return (NULL);
254: }
255:
256: int
257: cvs_trigger_handle(int type, char *repo, char *in, struct trigger_list *list,
258: struct file_info_list *files)
259: {
260: int r;
261: char *cmd;
262: struct trigger_line *line;
263:
264: TAILQ_FOREACH(line, list, flist) {
265: cmd = parse_cmd(type, line->line, repo, files);
266: if (cmd != NULL) {
267: switch(type) {
268: case CVS_TRIGGER_COMMITINFO:
269: case CVS_TRIGGER_TAGINFO:
270: case CVS_TRIGGER_VERIFYMSG:
271: if ((r = cvs_exec(cmd, NULL, 1)) != 0) {
272: xfree(cmd);
273: return r;
274: }
275: break;
276: default:
277: (void)cvs_exec(cmd, in, 1);
278: break;
279: }
280: xfree(cmd);
281: }
282: }
283:
284: return 0;
285: }
286:
287: struct trigger_list *
288: cvs_trigger_getlines(char * file, char * repo)
289: {
290: FILE *fp;
291: int allow_all, lineno, match = 0;
292: size_t len;
293: regex_t preg;
294: struct trigger_list *list;
295: struct trigger_line *tline;
296: char fpath[MAXPATHLEN];
297: char *currentline, *defaultline = NULL, *nline, *p, *q, *regex;
298:
299: list = xmalloc(sizeof(*list));
300: TAILQ_INIT(list);
301:
302: if (strcmp(file, CVS_PATH_EDITINFO) == 0 ||
303: strcmp(file, CVS_PATH_VERIFYMSG) == 0)
304: allow_all = 0;
305: else
306: allow_all = 1;
307:
308: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", current_cvsroot->cr_dir,
309: file);
310:
311: if ((fp = fopen(fpath, "r")) == NULL) {
312: if (errno != ENOENT)
313: cvs_log(LP_ERRNO, "cvs_trigger_getlines: %s", file);
314: return (NULL);
315: }
316:
317: lineno = 0;
318: nline = NULL;
1.2 joris 319: while ((currentline = fgetln(fp, &len)) != NULL) {
1.1 joris 320: if (currentline[len - 1] == '\n') {
321: currentline[len - 1] = '\0';
322: } else {
323: nline = xmalloc(len + 1);
324: memcpy(nline, currentline, len);
325: nline[len] = '\0';
326: currentline = nline;
327: }
328:
329: lineno++;
330:
331: for (p = currentline; isspace(*p); p++)
332: ;
333:
334: if (*p == '\0' || *p == '#')
335: continue;
336:
337: for (q = p; !isspace(*q) && *q != '\0'; q++)
338: ;
339:
340: if (*q == '\0')
341: goto bad;
342:
343: /* XXX why do you check *p (regex)? */
344: if (*p == '%')
345: goto bad;
346:
347: *q++ = '\0';
348: regex = p;
349:
350: for (; isspace(*q); q++)
351: ;
352:
353: if (*q == '\0')
354: goto bad;
355:
356: if (strcmp(regex, "ALL") == 0 && allow_all) {
357: tline = xmalloc(sizeof(*tline));
358: tline->line = xstrdup(q);
359: TAILQ_INSERT_TAIL(list, tline, flist);
360: } else if (defaultline == NULL && !match &&
361: strcmp(regex, "DEFAULT") == 0) {
362: defaultline = xstrdup(q);
363: } else if (!match) {
364: if (regcomp(&preg, regex, REG_NOSUB|REG_EXTENDED))
365: goto bad;
366:
367: if (regexec(&preg, repo, 0, NULL, 0) != REG_NOMATCH) {
368: match = 1;
369:
370: tline = xmalloc(sizeof(*tline));
371: tline->line = xstrdup(q);
372: TAILQ_INSERT_HEAD(list, tline, flist);
373: }
374: regfree(&preg);
375: }
376: }
1.2 joris 377:
1.1 joris 378: if (nline != NULL)
379: xfree(nline);
380:
381: if (defaultline != NULL) {
382: if (!match) {
383: tline = xmalloc(sizeof(*tline));
384: tline->line = defaultline;
385: TAILQ_INSERT_HEAD(list, tline, flist);
386: } else
387: xfree(defaultline);
388: }
389:
390: if (TAILQ_EMPTY(list)) {
391: xfree(list);
392: list = NULL;
393: }
394:
395: return (list);
396:
397: bad:
398: cvs_log(LP_NOTICE, "%s: malformed line %d", file, lineno);
399:
400: if (defaultline != NULL)
401: xfree(defaultline);
402: cvs_trigger_freelist(list);
403:
404: return (NULL);
405: }
406:
407: void
408: cvs_trigger_freelist(struct trigger_list * list)
409: {
410: struct trigger_line *line;
411:
412: while ((line = TAILQ_FIRST(list)) != NULL) {
413: TAILQ_REMOVE(list, line, flist);
414: xfree(line->line);
415: xfree(line);
416: }
417:
418: xfree(list);
419: }
420:
421: void
422: cvs_trigger_freeinfo(struct file_info_list * list)
423: {
424: struct file_info * fi;
425:
426: while ((fi = TAILQ_FIRST(list)) != NULL) {
427: TAILQ_REMOVE(list, fi, flist);
428:
429: if (fi->file_path != NULL)
430: xfree(fi->file_path);
431: if (fi->file_wd != NULL)
432: xfree(fi->file_wd);
433: if (fi->crevstr != NULL)
434: xfree(fi->crevstr);
435: if (fi->nrevstr != NULL)
436: xfree(fi->nrevstr);
437: if (fi->tag_new != NULL)
438: xfree(fi->tag_new);
439: if (fi->tag_old != NULL)
440: xfree(fi->tag_old);
441:
442: xfree(fi);
443: }
444: }
445:
446: void
447: cvs_trigger_loginfo_header(BUF *buf, char *repo)
448: {
449: char *dir, pwd[MAXPATHLEN];
450: char hostname[MAXHOSTNAMELEN];
451:
452: if (gethostname(hostname, sizeof(hostname)) == -1) {
453: fatal("cvs_trigger_loginfo_header: gethostname failed %s",
454: strerror(errno));
455: }
456:
457: if (getcwd(pwd, sizeof(pwd)) == NULL)
458: fatal("cvs_trigger_loginfo_header: Cannot get working "
459: "directory");
460:
461: if ((dir = dirname(pwd)) == NULL) {
462: fatal("cvs_trigger_loginfo_header: dirname failed %s",
463: strerror(errno));
464: }
465:
466: cvs_buf_puts(buf, "Update of ");
467: cvs_buf_puts(buf, current_cvsroot->cr_dir);
468: cvs_buf_putc(buf, '/');
469: cvs_buf_puts(buf, repo);
470: cvs_buf_putc(buf, '\n');
471:
472: cvs_buf_puts(buf, "In directory ");
473: cvs_buf_puts(buf, hostname);
474: cvs_buf_puts(buf, ":");
475: cvs_buf_puts(buf, dirname(pwd));
476: cvs_buf_putc(buf, '/');
477: cvs_buf_puts(buf, repo);
478: cvs_buf_putc(buf, '\n');
479: cvs_buf_putc(buf, '\n');
480: }
481: