[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.15

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