[BACK]Return to trigger.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/trigger.c, Revision 1.9

1.9     ! tobias      1: /*     $OpenBSD: trigger.c,v 1.8 2008/06/10 04:01:41 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:
1.7       joris      81:        /*
                     82:         * check like this, add only uses loginfo for directories anyway
                     83:         */
                     84:        if (cvs_cmdop == CVS_OP_ADD) {
                     85:                cvs_buf_append(buf, "- New directory",
                     86:                    strlen("- New directory"));
1.8       joris      87:                if (quote)
                     88:                        cvs_buf_putc(buf, '"');
                     89:                return (0);
                     90:        }
                     91:
                     92:        if (cvs_cmdop == CVS_OP_IMPORT) {
                     93:                cvs_buf_append(buf, "- Imported sources",
                     94:                    strlen("- Imported sources"));
1.7       joris      95:                if (quote)
                     96:                        cvs_buf_putc(buf, '"');
                     97:                return (0);
                     98:        }
                     99:
1.1       joris     100:        for (;;) {
                    101:                for (p = format; *p != '\0';) {
                    102:                        val = NULL;
                    103:
                    104:                        switch (*p) {
                    105:                        case '%':
                    106:                                val = "%";
                    107:                                break;
                    108:                        case 'b':
                    109:                                if (fi != NULL) {
                    110:                                        valbuf[0] = fi->tag_type;
                    111:                                        val = valbuf;
                    112:                                }
                    113:                                break;
                    114:                        case 'o':
                    115:                                if (fi != NULL)
                    116:                                        val = fi->tag_op;
                    117:                                break;
                    118:                        case 'p':
                    119:                                val = current_cvsroot->cr_dir;
                    120:                                break;
                    121:                        case 'r':
                    122:                                val = repo;
                    123:                                break;
                    124:                        case 'l':
                    125:                        case 'S':
                    126:                        case 's':
1.6       joris     127:                                if (fi != NULL) {
                    128:                                        val = basename(fi->file_path);
                    129:                                        if (val == NULL) {
                    130:                                                fatal("basename: %s",
                    131:                                                    strerror(errno));
                    132:                                        }
                    133:                                }
1.1       joris     134:                                break;
                    135:                        case 't':
                    136:                                if (fi != NULL)
                    137:                                        val = fi->tag_new;
                    138:                                break;
                    139:                        case 'V':
1.5       joris     140:                                if (fi != NULL) {
1.7       joris     141:                                        if (fi->crevstr != NULL &&
                    142:                                            !strcmp(fi->crevstr,
1.5       joris     143:                                            "Non-existent"))
                    144:                                                val = "NONE";
                    145:                                        else
                    146:                                                val = fi->crevstr;
                    147:                                }
1.1       joris     148:                                break;
                    149:                        case 'v':
                    150:                                if (fi != NULL)
                    151:                                        val = fi->nrevstr;
                    152:                                break;
                    153:                        default:
                    154:                                return 1;
                    155:                        }
                    156:
                    157:                        if (val != NULL)
                    158:                                cvs_buf_append(buf, val, strlen(val));
                    159:
                    160:                        if (*(++p) != '\0')
                    161:                                cvs_buf_putc(buf, ',');
                    162:                }
                    163:
                    164:                if (fi != NULL)
                    165:                        fi = TAILQ_NEXT(fi, flist);
                    166:                if (fi == NULL)
                    167:                        break;
                    168:
                    169:                if (strlen(format) == 1 && (*format == '%' || *format == 'o' ||
                    170:                    *format == 'p' || *format == 'r' || *format == 't'))
                    171:                        break;
                    172:
                    173:                cvs_buf_putc(buf, ' ');
                    174:        }
                    175:
                    176:        if (quote)
                    177:                cvs_buf_putc(buf, '"');
                    178:
                    179:        return 0;
                    180: }
                    181:
                    182: static char *
                    183: parse_cmd(int type, char *cmd, const char *repo,
                    184:     struct file_info_list *file_info)
                    185: {
                    186:        int expanded = 0;
                    187:        char argbuf[2] = { '\0', '\0' };
1.9     ! tobias    188:        char *allowed_args, *default_args, *args, *file, *p, *q = NULL;
1.1       joris     189:        size_t pos;
                    190:        BUF *buf;
                    191:
                    192:        switch (type) {
                    193:        case CVS_TRIGGER_COMMITINFO:
                    194:                allowed_args = "prsS{}";
                    195:                default_args = " %p/%r %S";
1.9     ! tobias    196:                file = CVS_PATH_COMMITINFO;
1.1       joris     197:                break;
                    198:        case CVS_TRIGGER_LOGINFO:
1.4       joris     199:                allowed_args = "prsSvVt{}";
1.1       joris     200:                default_args = NULL;
1.9     ! tobias    201:                file = CVS_PATH_LOGINFO;
1.1       joris     202:                break;
                    203:        case CVS_TRIGGER_VERIFYMSG:
                    204:                allowed_args = "l";
                    205:                default_args = " %l";
1.9     ! tobias    206:                file = CVS_PATH_VERIFYMSG;
1.1       joris     207:                break;
                    208:        case CVS_TRIGGER_TAGINFO:
                    209:                allowed_args = "btoprsSvV{}";
                    210:                default_args = " %t %o %p/%r %{sv}";
1.9     ! tobias    211:                file = CVS_PATH_TAGINFO;
1.1       joris     212:                break;
                    213:        default:
                    214:                return (NULL);
                    215:        }
                    216:
                    217:        /* XXX move this out of this function */
                    218:        /* before doing any stuff, check if the command starts with % */
                    219:        for (p = cmd; *p != '%' && !isspace(*p) && *p != '\0'; p++)
                    220:                ;
                    221:        if (*p == '%')
                    222:                return (NULL);
                    223:
                    224:        buf = cvs_buf_alloc(1024);
                    225:
                    226:        p = cmd;
                    227: again:
                    228:        for (; *p != '\0'; p++) {
                    229:                if ((pos = strcspn(p, "%")) != 0) {
                    230:                        cvs_buf_append(buf, p, pos);
                    231:                        p += pos;
                    232:                }
                    233:
                    234:                if (*p++ == '\0')
                    235:                        break;
                    236:
                    237:                q = NULL;
                    238:                switch (*p) {
                    239:                case '\0':
                    240:                        goto bad;
                    241:                case '{':
                    242:                        if (strchr(allowed_args, '{') == NULL)
                    243:                                goto bad;
                    244:                        pos = strcspn(++p, "}");
                    245:                        if (p[pos] == '\0')
                    246:                                goto bad;
                    247:                        q = xmalloc(pos + 1);
                    248:                        memcpy(q, p, pos);
                    249:                        q[pos] = '\0';
                    250:                        args = q;
                    251:                        p += pos;
                    252:                        break;
                    253:                default:
                    254:                        argbuf[0] = *p;
                    255:                        args = argbuf;
                    256:                        break;
                    257:                }
                    258:
                    259:                if (expand_args(buf, file_info, repo, allowed_args, args))
                    260:                        goto bad;
                    261:                expanded = 1;
                    262:
                    263:                if (q != NULL)
                    264:                        xfree(q);
                    265:        }
                    266:
                    267:        if (!expanded && default_args != NULL) {
                    268:                p = default_args;
                    269:                expanded = 1;
                    270:                goto again;
                    271:        }
                    272:
                    273:        cvs_buf_putc(buf, '\0');
                    274:        return (cvs_buf_release(buf));
                    275:
                    276: bad:
                    277:        if (q != NULL)
                    278:                xfree(q);
1.9     ! tobias    279:        cvs_log(LP_NOTICE, "%s contains malformed command '%s'", file, cmd);
1.1       joris     280:        cvs_buf_free(buf);
                    281:        return (NULL);
                    282: }
                    283:
                    284: int
                    285: cvs_trigger_handle(int type, char *repo, char *in, struct trigger_list *list,
                    286:     struct file_info_list *files)
                    287: {
                    288:        int r;
                    289:        char *cmd;
                    290:        struct trigger_line *line;
                    291:
                    292:        TAILQ_FOREACH(line, list, flist) {
                    293:                cmd = parse_cmd(type, line->line, repo, files);
                    294:                if (cmd != NULL) {
                    295:                        switch(type) {
                    296:                        case CVS_TRIGGER_COMMITINFO:
                    297:                        case CVS_TRIGGER_TAGINFO:
                    298:                        case CVS_TRIGGER_VERIFYMSG:
                    299:                                if ((r = cvs_exec(cmd, NULL, 1)) != 0) {
                    300:                                        xfree(cmd);
                    301:                                        return r;
                    302:                                }
                    303:                                break;
                    304:                        default:
                    305:                                (void)cvs_exec(cmd, in, 1);
                    306:                                break;
                    307:                        }
                    308:                        xfree(cmd);
                    309:                }
                    310:        }
                    311:
                    312:        return 0;
                    313: }
                    314:
                    315: struct trigger_list *
                    316: cvs_trigger_getlines(char * file, char * repo)
                    317: {
                    318:        FILE *fp;
                    319:        int allow_all, lineno, match = 0;
                    320:        size_t len;
                    321:        regex_t preg;
                    322:        struct trigger_list *list;
                    323:        struct trigger_line *tline;
                    324:        char fpath[MAXPATHLEN];
                    325:        char *currentline, *defaultline = NULL, *nline, *p, *q, *regex;
                    326:
                    327:        list = xmalloc(sizeof(*list));
                    328:        TAILQ_INIT(list);
                    329:
                    330:        if (strcmp(file, CVS_PATH_EDITINFO) == 0 ||
                    331:            strcmp(file, CVS_PATH_VERIFYMSG) == 0)
                    332:                allow_all = 0;
                    333:        else
                    334:                allow_all = 1;
                    335:
                    336:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", current_cvsroot->cr_dir,
                    337:            file);
                    338:
                    339:        if ((fp = fopen(fpath, "r")) == NULL) {
                    340:                if (errno != ENOENT)
                    341:                        cvs_log(LP_ERRNO, "cvs_trigger_getlines: %s", file);
                    342:                return (NULL);
                    343:        }
                    344:
                    345:        lineno = 0;
                    346:        nline = NULL;
1.2       joris     347:        while ((currentline = fgetln(fp, &len)) != NULL) {
1.1       joris     348:                if (currentline[len - 1] == '\n') {
                    349:                        currentline[len - 1] = '\0';
                    350:                } else {
                    351:                        nline = xmalloc(len + 1);
                    352:                        memcpy(nline, currentline, len);
                    353:                        nline[len] = '\0';
                    354:                        currentline = nline;
                    355:                }
                    356:
                    357:                lineno++;
                    358:
                    359:                for (p = currentline; isspace(*p); p++)
                    360:                        ;
                    361:
                    362:                if (*p == '\0' || *p == '#')
                    363:                        continue;
                    364:
                    365:                for (q = p; !isspace(*q) && *q != '\0'; q++)
                    366:                        ;
                    367:
                    368:                if (*q == '\0')
                    369:                        goto bad;
                    370:
                    371:                /* XXX why do you check *p (regex)? */
                    372:                if (*p == '%')
                    373:                        goto bad;
                    374:
                    375:                *q++ = '\0';
                    376:                regex = p;
                    377:
                    378:                for (; isspace(*q); q++)
                    379:                        ;
                    380:
                    381:                if (*q == '\0')
                    382:                        goto bad;
                    383:
                    384:                if (strcmp(regex, "ALL") == 0 && allow_all) {
                    385:                        tline = xmalloc(sizeof(*tline));
                    386:                        tline->line = xstrdup(q);
                    387:                        TAILQ_INSERT_TAIL(list, tline, flist);
                    388:                } else if (defaultline == NULL && !match &&
                    389:                    strcmp(regex, "DEFAULT") == 0) {
                    390:                        defaultline = xstrdup(q);
                    391:                } else if (!match) {
                    392:                        if (regcomp(&preg, regex, REG_NOSUB|REG_EXTENDED))
                    393:                                goto bad;
                    394:
                    395:                        if (regexec(&preg, repo, 0, NULL, 0) != REG_NOMATCH) {
                    396:                                match = 1;
                    397:
                    398:                                tline = xmalloc(sizeof(*tline));
                    399:                                tline->line = xstrdup(q);
                    400:                                TAILQ_INSERT_HEAD(list, tline, flist);
                    401:                        }
                    402:                        regfree(&preg);
                    403:                }
                    404:        }
1.2       joris     405:
1.1       joris     406:        if (nline != NULL)
                    407:                xfree(nline);
                    408:
                    409:        if (defaultline != NULL) {
                    410:                if (!match) {
                    411:                        tline = xmalloc(sizeof(*tline));
                    412:                        tline->line = defaultline;
                    413:                        TAILQ_INSERT_HEAD(list, tline, flist);
                    414:                } else
                    415:                        xfree(defaultline);
                    416:        }
                    417:
                    418:        if (TAILQ_EMPTY(list)) {
                    419:                xfree(list);
                    420:                list = NULL;
                    421:        }
                    422:
                    423:        return (list);
                    424:
                    425: bad:
                    426:        cvs_log(LP_NOTICE, "%s: malformed line %d", file, lineno);
                    427:
                    428:        if (defaultline != NULL)
                    429:                xfree(defaultline);
                    430:        cvs_trigger_freelist(list);
                    431:
                    432:        return (NULL);
                    433: }
                    434:
                    435: void
                    436: cvs_trigger_freelist(struct trigger_list * list)
                    437: {
                    438:        struct trigger_line *line;
                    439:
                    440:        while ((line = TAILQ_FIRST(list)) != NULL) {
                    441:                TAILQ_REMOVE(list, line, flist);
                    442:                xfree(line->line);
                    443:                xfree(line);
                    444:        }
                    445:
                    446:        xfree(list);
                    447: }
                    448:
                    449: void
                    450: cvs_trigger_freeinfo(struct file_info_list * list)
                    451: {
                    452:        struct file_info * fi;
                    453:
                    454:        while ((fi = TAILQ_FIRST(list)) != NULL) {
                    455:                TAILQ_REMOVE(list, fi, flist);
                    456:
                    457:                if (fi->file_path != NULL)
                    458:                        xfree(fi->file_path);
                    459:                if (fi->file_wd != NULL)
                    460:                        xfree(fi->file_wd);
                    461:                if (fi->crevstr != NULL)
                    462:                        xfree(fi->crevstr);
                    463:                if (fi->nrevstr != NULL)
                    464:                        xfree(fi->nrevstr);
                    465:                if (fi->tag_new != NULL)
                    466:                        xfree(fi->tag_new);
                    467:                if (fi->tag_old != NULL)
                    468:                        xfree(fi->tag_old);
                    469:
                    470:                xfree(fi);
                    471:        }
                    472: }
                    473:
                    474: void
                    475: cvs_trigger_loginfo_header(BUF *buf, char *repo)
                    476: {
                    477:        char *dir, pwd[MAXPATHLEN];
                    478:        char hostname[MAXHOSTNAMELEN];
                    479:
                    480:        if (gethostname(hostname, sizeof(hostname)) == -1) {
                    481:                fatal("cvs_trigger_loginfo_header: gethostname failed %s",
                    482:                    strerror(errno));
                    483:        }
                    484:
                    485:        if (getcwd(pwd, sizeof(pwd)) == NULL)
                    486:                fatal("cvs_trigger_loginfo_header: Cannot get working "
                    487:                    "directory");
                    488:
                    489:        if ((dir = dirname(pwd)) == NULL) {
                    490:                fatal("cvs_trigger_loginfo_header: dirname failed %s",
                    491:                    strerror(errno));
                    492:        }
                    493:
                    494:        cvs_buf_puts(buf, "Update of ");
                    495:        cvs_buf_puts(buf, current_cvsroot->cr_dir);
                    496:        cvs_buf_putc(buf, '/');
                    497:        cvs_buf_puts(buf, repo);
                    498:        cvs_buf_putc(buf, '\n');
                    499:
                    500:        cvs_buf_puts(buf, "In directory ");
                    501:        cvs_buf_puts(buf, hostname);
                    502:        cvs_buf_puts(buf, ":");
                    503:        cvs_buf_puts(buf, dirname(pwd));
                    504:        cvs_buf_putc(buf, '/');
                    505:        cvs_buf_puts(buf, repo);
                    506:        cvs_buf_putc(buf, '\n');
                    507:        cvs_buf_putc(buf, '\n');
                    508: }
                    509: