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

Annotation of src/usr.bin/cvs/rcs.c, Revision 1.12

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <errno.h>
                     32: #include <stdio.h>
                     33: #include <ctype.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
                     36:
                     37: #include "rcs.h"
                     38: #include "log.h"
                     39:
                     40: #define RCS_BUFSIZE   8192
                     41:
                     42:
                     43: /* RCS token types */
                     44: #define RCS_TOK_ERR     -1
                     45: #define RCS_TOK_EOF      0
                     46: #define RCS_TOK_NUM      1
                     47: #define RCS_TOK_ID       2
                     48: #define RCS_TOK_STRING   3
                     49: #define RCS_TOK_SCOLON   4
                     50: #define RCS_TOK_COLON    5
                     51:
                     52:
                     53: #define RCS_TOK_HEAD     8
                     54: #define RCS_TOK_BRANCH   9
                     55: #define RCS_TOK_ACCESS   10
                     56: #define RCS_TOK_SYMBOLS  11
                     57: #define RCS_TOK_LOCKS    12
                     58: #define RCS_TOK_COMMENT  13
                     59: #define RCS_TOK_EXPAND   14
                     60: #define RCS_TOK_DATE     15
                     61: #define RCS_TOK_AUTHOR   16
                     62: #define RCS_TOK_STATE    17
                     63: #define RCS_TOK_NEXT     18
                     64: #define RCS_TOK_BRANCHES 19
                     65: #define RCS_TOK_DESC     20
                     66: #define RCS_TOK_LOG      21
                     67: #define RCS_TOK_TEXT     22
                     68: #define RCS_TOK_STRICT   23
                     69:
                     70: #define RCS_ISKEY(t)    (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
                     71:
                     72:
                     73: #define RCS_NOSCOL   0x01   /* no terminating semi-colon */
                     74: #define RCS_VOPT     0x02   /* value is optional */
                     75:
                     76:
                     77:
                     78: /* opaque parse data */
                     79: struct rcs_pdata {
                     80:        u_int  rp_line;
                     81:
                     82:        char  *rp_buf;
                     83:        size_t rp_blen;
                     84:
                     85:        /* pushback token buffer */
                     86:        char   rp_ptok[128];
                     87:        int    rp_pttype;       /* token type, RCS_TOK_ERR if no token */
                     88:
                     89:        FILE  *rp_file;
                     90: };
                     91:
                     92:
                     93: struct rcs_line {
                     94:        char *rl_line;
                     95:        int   rl_lineno;
                     96:        TAILQ_ENTRY(rcs_line) rl_list;
                     97: };
1.5       vincent    98: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1       jfb        99:
                    100: struct rcs_foo {
                    101:        int       rl_nblines;
                    102:        char     *rl_data;
1.5       vincent   103:        struct rcs_tqh rl_lines;
1.1       jfb       104: };
                    105:
                    106: static int  rcs_parse_admin     (RCSFILE *);
                    107: static int  rcs_parse_delta     (RCSFILE *);
                    108: static int  rcs_parse_deltatext (RCSFILE *);
                    109:
1.7       jfb       110: static int      rcs_parse_access    (RCSFILE *);
                    111: static int      rcs_parse_symbols   (RCSFILE *);
                    112: static int      rcs_parse_locks     (RCSFILE *);
                    113: static int      rcs_parse_branches  (RCSFILE *, struct rcs_delta *);
                    114: static void     rcs_freedelta       (struct rcs_delta *);
                    115: static void     rcs_freepdata       (struct rcs_pdata *);
                    116: static int      rcs_gettok          (RCSFILE *);
                    117: static int      rcs_pushtok         (RCSFILE *, const char *, int);
                    118: static int      rcs_patch_lines     (struct rcs_foo *, struct rcs_foo *);
                    119:
                    120: static struct rcs_delta*  rcs_findrev    (RCSFILE *, RCSNUM *);
                    121: static struct rcs_foo*    rcs_splitlines (const char *);
                    122: static void               rcs_freefoo    (struct rcs_foo *);
1.1       jfb       123:
                    124: #define RCS_TOKSTR(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
                    125: #define RCS_TOKLEN(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen
                    126:
                    127:
                    128: static struct rcs_key {
                    129:        char  rk_str[16];
                    130:        int   rk_id;
                    131:        int   rk_val;
                    132:        int   rk_flags;
                    133: } rcs_keys[] = {
                    134:        { "access",   RCS_TOK_ACCESS,   RCS_TOK_ID,     RCS_VOPT     },
                    135:        { "author",   RCS_TOK_AUTHOR,   RCS_TOK_STRING, 0            },
                    136:        { "branch",   RCS_TOK_BRANCH,   RCS_TOK_NUM,    RCS_VOPT     },
                    137:        { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM,    RCS_VOPT     },
                    138:        { "comment",  RCS_TOK_COMMENT,  RCS_TOK_STRING, RCS_VOPT     },
                    139:        { "date",     RCS_TOK_DATE,     RCS_TOK_NUM,    0            },
                    140:        { "desc",     RCS_TOK_DESC,     RCS_TOK_STRING, RCS_NOSCOL   },
                    141:        { "expand",   RCS_TOK_EXPAND,   RCS_TOK_STRING, RCS_VOPT     },
                    142:        { "head",     RCS_TOK_HEAD,     RCS_TOK_NUM,    RCS_VOPT     },
                    143:        { "locks",    RCS_TOK_LOCKS,    RCS_TOK_ID,     0            },
                    144:        { "log",      RCS_TOK_LOG,      RCS_TOK_STRING, RCS_NOSCOL   },
                    145:        { "next",     RCS_TOK_NEXT,     RCS_TOK_NUM,    RCS_VOPT     },
                    146:        { "state",    RCS_TOK_STATE,    RCS_TOK_STRING, RCS_VOPT     },
                    147:        { "strict",   RCS_TOK_STRICT,   0,              0,           },
                    148:        { "symbols",  RCS_TOK_SYMBOLS,  0,              0            },
                    149:        { "text",     RCS_TOK_TEXT,     RCS_TOK_STRING, RCS_NOSCOL   },
                    150: };
                    151:
                    152:
                    153:
                    154: /*
                    155:  * rcs_open()
                    156:  *
                    157:  * Open a file containing RCS-formatted information.  The file's path is
                    158:  * given in <path>, and the opening mode is given in <mode>, which is either
                    159:  * RCS_MODE_READ, RCS_MODE_WRITE, or RCS_MODE_RDWR.  If the mode requests write
                    160:  * access and the file does not exist, it will be created.
                    161:  * The file isn't actually parsed by rcs_open(); parsing is delayed until the
                    162:  * first operation that requires information from the file.
                    163:  * Returns a handle to the opened file on success, or NULL on failure.
                    164:  */
                    165:
                    166: RCSFILE*
                    167: rcs_open(const char *path, u_int mode)
                    168: {
                    169:        RCSFILE *rfp;
                    170:        struct stat st;
                    171:
                    172:        if ((stat(path, &st) == -1) && (errno == ENOENT) &&
                    173:           !(mode & RCS_MODE_WRITE)) {
                    174:                cvs_log(LP_ERRNO, "cannot open RCS file `%s'", path);
                    175:                return (NULL);
                    176:        }
                    177:
                    178:        rfp = (RCSFILE *)malloc(sizeof(*rfp));
                    179:        if (rfp == NULL) {
                    180:                cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
                    181:                return (NULL);
                    182:        }
                    183:        memset(rfp, 0, sizeof(*rfp));
                    184:
                    185:        rfp->rf_head = rcsnum_alloc();
                    186:        if (rfp->rf_head == NULL) {
                    187:                free(rfp);
                    188:                return (NULL);
                    189:        }
                    190:
1.11      joris     191:        rfp->rf_branch = rcsnum_alloc();
                    192:        if (rfp->rf_branch == NULL) {
                    193:                rcs_close(rfp);
                    194:                return (NULL);
                    195:        }
                    196:
1.1       jfb       197:        rfp->rf_path = strdup(path);
                    198:        if (rfp->rf_path == NULL) {
                    199:                cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
                    200:                rcs_close(rfp);
                    201:                return (NULL);
                    202:        }
                    203:
                    204:        rcsnum_aton(RCS_HEAD_INIT, NULL, rfp->rf_head);
                    205:
                    206:        rfp->rf_ref = 1;
                    207:        rfp->rf_flags |= RCS_RF_SLOCK;
                    208:        rfp->rf_mode = mode;
                    209:
                    210:        TAILQ_INIT(&(rfp->rf_delta));
                    211:        TAILQ_INIT(&(rfp->rf_symbols));
                    212:        TAILQ_INIT(&(rfp->rf_locks));
                    213:
                    214:        if (rcs_parse(rfp) < 0) {
                    215:                rcs_close(rfp);
                    216:                return (NULL);
                    217:        }
                    218:
                    219:        return (rfp);
                    220: }
                    221:
                    222:
                    223: /*
                    224:  * rcs_close()
                    225:  *
                    226:  * Close an RCS file handle.
                    227:  */
                    228:
                    229: void
                    230: rcs_close(RCSFILE *rfp)
                    231: {
                    232:        struct rcs_delta *rdp;
                    233:
                    234:        if (rfp->rf_ref > 1) {
                    235:                rfp->rf_ref--;
                    236:                return;
                    237:        }
                    238:
                    239:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    240:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    241:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    242:                rcs_freedelta(rdp);
                    243:        }
                    244:
                    245:        if (rfp->rf_head != NULL)
                    246:                rcsnum_free(rfp->rf_head);
1.11      joris     247:        if (rfp->rf_branch != NULL)
                    248:                rcsnum_free(rfp->rf_branch);
1.1       jfb       249:
                    250:        if (rfp->rf_path != NULL)
                    251:                free(rfp->rf_path);
                    252:        if (rfp->rf_comment != NULL)
                    253:                free(rfp->rf_comment);
                    254:        if (rfp->rf_expand != NULL)
                    255:                free(rfp->rf_expand);
                    256:        if (rfp->rf_desc != NULL)
                    257:                free(rfp->rf_desc);
                    258:        free(rfp);
                    259: }
                    260:
                    261:
                    262: /*
                    263:  * rcs_write()
                    264:  *
                    265:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    266:  * path is in <rf_path>.
                    267:  * Returns 0 on success, or -1 on failure.
                    268:  */
                    269:
                    270: int
                    271: rcs_write(RCSFILE *rfp)
                    272: {
                    273:        FILE *fp;
1.7       jfb       274:        char buf[1024], numbuf[64], *cp;
                    275:        size_t rlen, len;
1.1       jfb       276:        struct rcs_sym *symp;
                    277:        struct rcs_delta *rdp;
                    278:
                    279:        if (rfp->rf_flags & RCS_RF_SYNCED)
                    280:                return (0);
                    281:
                    282:        fp = fopen(rfp->rf_path, "w");
                    283:        if (fp == NULL) {
                    284:                cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
                    285:                    rfp->rf_path);
                    286:                return (-1);
                    287:        }
                    288:
                    289:        rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    290:        fprintf(fp, "head\t%s;\n", numbuf);
                    291:        fprintf(fp, "access;\n");
                    292:
                    293:        fprintf(fp, "symbols\n");
                    294:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    295:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
                    296:                snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
                    297:                fprintf(fp, "\t%s", buf);
                    298:                if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
                    299:                        fputc('\n', fp);
                    300:        }
                    301:        fprintf(fp, ";\n");
                    302:
                    303:        fprintf(fp, "locks;");
                    304:
                    305:        if (rfp->rf_flags & RCS_RF_SLOCK)
                    306:                fprintf(fp, " strict;");
                    307:        fputc('\n', fp);
                    308:
                    309:        if (rfp->rf_comment != NULL)
                    310:                fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);
                    311:
                    312:        if (rfp->rf_expand != NULL)
                    313:                fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);
                    314:
                    315:        fprintf(fp, "\n\n");
                    316:
                    317:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    318:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    319:                    sizeof(numbuf)));
                    320:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
                    321:                    rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
                    322:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    323:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    324:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    325:                    rdp->rd_author, rdp->rd_state);
                    326:                fprintf(fp, "branches;\n");
                    327:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    328:                    numbuf, sizeof(numbuf)));
                    329:        }
                    330:
                    331:        fprintf(fp, "\ndesc\n@%s@\n\n", rfp->rf_desc);
                    332:
                    333:        /* deltatexts */
                    334:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    335:                fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    336:                    sizeof(numbuf)));
1.7       jfb       337:                fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);
                    338:
                    339:                cp = rdp->rd_text;
                    340:                do {
                    341:                        len = sizeof(buf);
                    342:                        rlen = rcs_stresc(1, cp, buf, &len);
                    343:                        fprintf(fp, "%s", buf);
                    344:                        cp += rlen;
                    345:                } while (len != 0);
                    346:                fprintf(fp, "@\n\n");
1.1       jfb       347:        }
                    348:        fclose(fp);
                    349:
                    350:        rfp->rf_flags |= RCS_RF_SYNCED;
                    351:
                    352:        return (0);
                    353: }
                    354:
                    355:
                    356: /*
                    357:  * rcs_addsym()
                    358:  *
                    359:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    360:  * is named <sym> and is bound to the RCS revision <snum>.
                    361:  * Returns 0 on success, or -1 on failure.
                    362:  */
                    363:
                    364: int
                    365: rcs_addsym(RCSFILE *rfp, const char *sym, RCSNUM *snum)
                    366: {
                    367:        struct rcs_sym *symp;
                    368:
                    369:        /* first look for duplication */
                    370:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    371:                if (strcmp(symp->rs_name, sym) == 0) {
                    372:                        return (-1);
                    373:                }
                    374:        }
                    375:
                    376:        symp = (struct rcs_sym *)malloc(sizeof(*symp));
                    377:        if (symp == NULL) {
                    378:                cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                    379:                return (-1);
                    380:        }
                    381:
                    382:        symp->rs_name = strdup(sym);
1.10      joris     383:        if (symp->rs_name == NULL) {
                    384:                cvs_log(LP_ERRNO, "failed to duplicate symbol");
                    385:                free(symp);
                    386:                return (-1);
                    387:        }
                    388:
1.1       jfb       389:        symp->rs_num = rcsnum_alloc();
1.11      joris     390:        if (symp->rs_num == NULL) {
                    391:                free(symp);
                    392:                return (-1);
                    393:        }
1.1       jfb       394:        rcsnum_cpy(snum, symp->rs_num, 0);
                    395:
                    396:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    397:
                    398:        /* not synced anymore */
                    399:        rfp->rf_flags &= ~RCS_RF_SYNCED;
                    400:
                    401:        return (0);
                    402: }
                    403:
                    404:
                    405: /*
                    406:  * rcs_patch()
                    407:  *
                    408:  * Apply an RCS-format patch pointed to by <patch> to the file contents
                    409:  * found in <data>.
                    410:  * Returns 0 on success, or -1 on failure.
                    411:  */
                    412:
                    413: BUF*
                    414: rcs_patch(const char *data, const char *patch)
                    415: {
1.5       vincent   416:        struct rcs_foo *dlines, *plines;
                    417:        struct rcs_line *lp;
1.1       jfb       418:        size_t len;
1.5       vincent   419:        int lineno;
1.1       jfb       420:        BUF *res;
                    421:
                    422:        len = strlen(data);
                    423:        res = cvs_buf_alloc(len, BUF_AUTOEXT);
                    424:        if (res == NULL)
                    425:                return (NULL);
                    426:
                    427:        dlines = rcs_splitlines(data);
                    428:        if (dlines == NULL)
                    429:                return (NULL);
1.5       vincent   430:
1.1       jfb       431:        plines = rcs_splitlines(patch);
1.5       vincent   432:        if (plines == NULL) {
                    433:                rcs_freefoo(dlines);
1.1       jfb       434:                return (NULL);
1.5       vincent   435:        }
                    436:
                    437:        if (rcs_patch_lines(dlines, plines) < 0) {
                    438:                rcs_freefoo(plines);
                    439:                rcs_freefoo(dlines);
                    440:                return (NULL);
                    441:        }
                    442:
                    443:        lineno = 0;
                    444:        TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
                    445:                if (lineno != 0)
                    446:                        cvs_buf_fappend(res, "%s\n", lp->rl_line);
                    447:                lineno++;
                    448:        }
                    449:
                    450:        rcs_freefoo(dlines);
                    451:        rcs_freefoo(plines);
                    452:        return (res);
                    453: }
                    454:
1.7       jfb       455: static int
1.5       vincent   456: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
                    457: {
                    458:        char op, *ep;
                    459:        struct rcs_line *lp, *dlp, *ndlp;
                    460:        int i, lineno, nbln;
1.1       jfb       461:
                    462:        dlp = TAILQ_FIRST(&(dlines->rl_lines));
                    463:        lp = TAILQ_FIRST(&(plines->rl_lines));
                    464:
                    465:        /* skip first bogus line */
                    466:        for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
                    467:            lp = TAILQ_NEXT(lp, rl_list)) {
                    468:                op = *(lp->rl_line);
                    469:                lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
                    470:                if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
                    471:                    (*ep != ' ')) {
                    472:                        cvs_log(LP_ERR,
                    473:                            "invalid line specification in RCS patch");
                    474:                        return (NULL);
                    475:                }
                    476:                ep++;
                    477:                nbln = (int)strtol(ep, &ep, 10);
                    478:                if ((nbln <= 0) || (*ep != '\0')) {
                    479:                        cvs_log(LP_ERR,
                    480:                            "invalid line number specification in RCS patch");
                    481:                        return (NULL);
                    482:                }
                    483:
                    484:                /* find the appropriate line */
                    485:                for (;;) {
                    486:                        if (dlp == NULL)
                    487:                                break;
                    488:                        if (dlp->rl_lineno == lineno)
                    489:                                break;
                    490:                        if (dlp->rl_lineno > lineno) {
                    491:                                dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
                    492:                        }
                    493:                        else if (dlp->rl_lineno < lineno) {
                    494:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    495:                                if (ndlp->rl_lineno > lineno)
                    496:                                        break;
                    497:                                dlp = ndlp;
                    498:                        }
                    499:                }
                    500:                if (dlp == NULL) {
                    501:                        cvs_log(LP_ERR,
                    502:                            "can't find referenced line in RCS patch");
                    503:                        return (NULL);
                    504:                }
                    505:
                    506:                if (op == 'd') {
                    507:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
                    508:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    509:                                TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
                    510:                                dlp = ndlp;
                    511:                        }
                    512:                }
                    513:                else if (op == 'a') {
                    514:                        for (i = 0; i < nbln; i++) {
                    515:                                ndlp = lp;
                    516:                                lp = TAILQ_NEXT(lp, rl_list);
                    517:                                if (lp == NULL) {
                    518:                                        cvs_log(LP_ERR, "truncated RCS patch");
1.5       vincent   519:                                        return (-1);
1.1       jfb       520:                                }
                    521:                                TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
                    522:                                TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
                    523:                                    lp, rl_list);
                    524:                                dlp = lp;
                    525:
                    526:                                /* we don't want lookup to block on those */
                    527:                                lp->rl_lineno = lineno;
                    528:
                    529:                                lp = ndlp;
                    530:                        }
                    531:                }
                    532:                else {
                    533:                        cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5       vincent   534:                        return (-1);
1.1       jfb       535:                }
                    536:
                    537:                /* last line of the patch, done */
                    538:                if (lp->rl_lineno == plines->rl_nblines)
                    539:                        break;
                    540:        }
                    541:
                    542:        /* once we're done patching, rebuild the line numbers */
1.2       vincent   543:        lineno = 0;
1.5       vincent   544:        TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1       jfb       545:                lp->rl_lineno = lineno++;
                    546:        dlines->rl_nblines = lineno - 1;
                    547:
1.5       vincent   548:        return (0);
1.1       jfb       549: }
                    550:
                    551:
                    552: /*
                    553:  * rcs_getrev()
                    554:  *
                    555:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent   556:  * returned buffer is dynamically allocated and should be released using
                    557:  * cvs_buf_free() once the caller is done using it.
1.1       jfb       558:  */
                    559:
                    560: BUF*
                    561: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
                    562: {
                    563:        int res;
                    564:        size_t len;
                    565:        void *bp;
                    566:        RCSNUM *crev;
                    567:        BUF *rbuf;
                    568:        struct rcs_delta *rdp = NULL;
                    569:
                    570:        res = rcsnum_cmp(rfp->rf_head, rev, 0);
                    571:        if (res == 1) {
                    572:                cvs_log(LP_ERR, "sorry, can't travel in the future yet");
                    573:                return (NULL);
                    574:        }
                    575:        else {
                    576:                rdp = rcs_findrev(rfp, rfp->rf_head);
                    577:                if (rdp == NULL) {
                    578:                        cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                    579:                        return (NULL);
                    580:                }
                    581:
                    582:                len = strlen(rdp->rd_text);
                    583:                rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
                    584:                if (rbuf == NULL)
                    585:                        return (NULL);
                    586:                cvs_buf_append(rbuf, rdp->rd_text, len);
                    587:
                    588:                if (res != 0) {
                    589:                        /* Apply patches backwards to get the right version.
                    590:                         * This will need some rework to support sub branches.
                    591:                         */
                    592:                        crev = rcsnum_alloc();
1.11      joris     593:                        if (crev == NULL)
                    594:                                return (NULL);
1.1       jfb       595:                        rcsnum_cpy(rfp->rf_head, crev, 0);
                    596:                        do {
                    597:                                crev->rn_id[crev->rn_len - 1]--;
                    598:                                rdp = rcs_findrev(rfp, crev);
                    599:                                if (rdp == NULL)
                    600:                                        return (NULL);
                    601:
                    602:                                cvs_buf_putc(rbuf, '\0');
                    603:                                bp = cvs_buf_release(rbuf);
                    604:                                rbuf = rcs_patch((char *)bp, rdp->rd_text);
                    605:                                if (rbuf == NULL)
                    606:                                        break;
                    607:                        } while (rcsnum_cmp(crev, rev, 0) != 0);
                    608:
                    609:                        rcsnum_free(crev);
                    610:                }
                    611:        }
                    612:
                    613:
                    614:        return (rbuf);
                    615: }
                    616:
                    617:
                    618: /*
                    619:  * rcs_getrevbydate()
                    620:  *
                    621:  * Get an RCS revision by a specific date.
                    622:  */
                    623:
                    624: RCSNUM*
                    625: rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
                    626: {
                    627:        return (NULL);
                    628: }
                    629:
                    630:
                    631: /*
                    632:  * rcs_findrev()
                    633:  *
                    634:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                    635:  * The revision number is given in <rev>.
                    636:  * Returns a pointer to the delta on success, or NULL on failure.
                    637:  */
                    638:
                    639: static struct rcs_delta*
                    640: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
                    641: {
                    642:        u_int cmplen;
                    643:        struct rcs_delta *rdp;
                    644:        struct rcs_dlist *hp;
1.6       vincent   645:        int found;
                    646:
1.1       jfb       647:        cmplen = 2;
                    648:        hp = &(rfp->rf_delta);
                    649:
1.6       vincent   650:        do {
                    651:                found = 0;
                    652:                TAILQ_FOREACH(rdp, hp, rd_list) {
                    653:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                    654:                                if (cmplen == rev->rn_len)
                    655:                                        return (rdp);
1.1       jfb       656:
1.6       vincent   657:                                hp = &(rdp->rd_snodes);
                    658:                                cmplen += 2;
                    659:                                found = 1;
                    660:                                break;
                    661:                        }
1.1       jfb       662:                }
1.6       vincent   663:        } while (found && cmplen < rev->rn_len);
1.1       jfb       664:
                    665:        return (NULL);
                    666: }
                    667:
                    668:
                    669: /*
                    670:  * rcs_parse()
                    671:  *
                    672:  * Parse the contents of file <path>, which are in the RCS format.
                    673:  * Returns 0 on success, or -1 on failure.
                    674:  */
                    675:
                    676: int
                    677: rcs_parse(RCSFILE *rfp)
                    678: {
                    679:        int ret;
                    680:        struct rcs_pdata *pdp;
                    681:
                    682:        if (rfp->rf_flags & RCS_RF_PARSED)
                    683:                return (0);
                    684:
                    685:        pdp = (struct rcs_pdata *)malloc(sizeof(*pdp));
                    686:        if (pdp == NULL) {
                    687:                cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
                    688:                return (-1);
                    689:        }
                    690:        memset(pdp, 0, sizeof(*pdp));
                    691:
                    692:        pdp->rp_line = 1;
                    693:        pdp->rp_pttype = RCS_TOK_ERR;
                    694:
                    695:        pdp->rp_file = fopen(rfp->rf_path, "r");
                    696:        if (pdp->rp_file == NULL) {
                    697:                cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
                    698:                rcs_freepdata(pdp);
                    699:                return (-1);
                    700:        }
                    701:
                    702:        pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
                    703:        if (pdp->rp_buf == NULL) {
                    704:                cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
                    705:                rcs_freepdata(pdp);
                    706:                return (-1);
                    707:        }
                    708:        pdp->rp_blen = RCS_BUFSIZE;
                    709:
                    710:        /* ditch the strict lock */
                    711:        rfp->rf_flags &= ~RCS_RF_SLOCK;
                    712:        rfp->rf_pdata = pdp;
                    713:
                    714:        if (rcs_parse_admin(rfp) < 0) {
                    715:                rcs_freepdata(pdp);
                    716:                return (-1);
                    717:        }
                    718:
                    719:        for (;;) {
                    720:                ret = rcs_parse_delta(rfp);
                    721:                if (ret == 0)
                    722:                        break;
                    723:                else if (ret == -1) {
                    724:                        rcs_freepdata(pdp);
                    725:                        return (-1);
                    726:                }
                    727:        }
                    728:
                    729:        ret = rcs_gettok(rfp);
                    730:        if (ret != RCS_TOK_DESC) {
                    731:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                    732:                    RCS_TOKSTR(rfp));
                    733:                rcs_freepdata(pdp);
                    734:                return (-1);
                    735:        }
                    736:
                    737:        ret = rcs_gettok(rfp);
                    738:        if (ret != RCS_TOK_STRING) {
                    739:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                    740:                    RCS_TOKSTR(rfp));
                    741:                rcs_freepdata(pdp);
                    742:                return (-1);
                    743:        }
                    744:
                    745:        rfp->rf_desc = strdup(RCS_TOKSTR(rfp));
1.10      joris     746:        if (rfp->rf_desc == NULL) {
                    747:                cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                    748:                rcs_freepdata(pdp);
                    749:                return (-1);
                    750:        }
1.1       jfb       751:
                    752:        for (;;) {
                    753:                ret = rcs_parse_deltatext(rfp);
                    754:                if (ret == 0)
                    755:                        break;
                    756:                else if (ret == -1) {
                    757:                        rcs_freepdata(pdp);
                    758:                        return (-1);
                    759:                }
                    760:        }
                    761:
                    762:        cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
                    763:            pdp->rp_line);
                    764:
                    765:        rcs_freepdata(pdp);
                    766:
                    767:        rfp->rf_pdata = NULL;
                    768:        rfp->rf_flags |= RCS_RF_PARSED|RCS_RF_SYNCED;
                    769:
                    770:        return (0);
                    771: }
                    772:
                    773:
                    774: /*
                    775:  * rcs_parse_admin()
                    776:  *
                    777:  * Parse the administrative portion of an RCS file.
                    778:  * Returns 0 on success, or -1 on failure.
                    779:  */
                    780:
                    781: static int
                    782: rcs_parse_admin(RCSFILE *rfp)
                    783: {
                    784:        u_int i;
                    785:        int tok, ntok, hmask;
                    786:        struct rcs_key *rk;
                    787:
                    788:        /* hmask is a mask of the headers already encountered */
                    789:        hmask = 0;
                    790:        for (;;) {
                    791:                tok = rcs_gettok(rfp);
                    792:                if (tok == RCS_TOK_ERR) {
                    793:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                    794:                        return (-1);
                    795:                }
                    796:                else if (tok == RCS_TOK_NUM) {
                    797:                        /* assume this is the start of the first delta */
                    798:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                    799:                        return (0);
                    800:                }
                    801:
                    802:                rk = NULL;
                    803:                for (i = 0; i < sizeof(rcs_keys)/sizeof(rcs_keys[0]); i++)
                    804:                        if (rcs_keys[i].rk_id == tok)
                    805:                                rk = &(rcs_keys[i]);
                    806:
                    807:                if (hmask & (1 << tok)) {
                    808:                        cvs_log(LP_ERR, "duplicate RCS key");
                    809:                        return (-1);
                    810:                }
                    811:                hmask |= (1 << tok);
                    812:
                    813:                switch (tok) {
                    814:                case RCS_TOK_HEAD:
                    815:                case RCS_TOK_BRANCH:
                    816:                case RCS_TOK_COMMENT:
                    817:                case RCS_TOK_EXPAND:
                    818:                        ntok = rcs_gettok(rfp);
                    819:                        if (ntok == RCS_TOK_SCOLON)
                    820:                                break;
                    821:                        if (ntok != rk->rk_val) {
                    822:                                cvs_log(LP_ERR,
                    823:                                    "invalid value type for RCS key `%s'",
                    824:                                    rk->rk_str);
                    825:                        }
                    826:
                    827:                        if (tok == RCS_TOK_HEAD) {
                    828:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                    829:                                    rfp->rf_head);
                    830:                        }
                    831:                        else if (tok == RCS_TOK_BRANCH) {
                    832:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                    833:                                    rfp->rf_branch);
                    834:                        }
                    835:                        else if (tok == RCS_TOK_COMMENT) {
                    836:                                rfp->rf_comment = strdup(RCS_TOKSTR(rfp));
1.10      joris     837:                                if (rfp->rf_comment == NULL) {
                    838:                                        cvs_log(LP_ERRNO,
                    839:                                            "failed to duplicate rcs token");
                    840:                                        return (-1);
                    841:                                }
1.1       jfb       842:                        }
                    843:                        else if (tok == RCS_TOK_EXPAND) {
                    844:                                rfp->rf_expand = strdup(RCS_TOKSTR(rfp));
1.10      joris     845:                                if (rfp->rf_expand == NULL) {
                    846:                                        cvs_log(LP_ERRNO,
                    847:                                            "failed to duplicate rcs token");
                    848:                                        return (-1);
                    849:                                }
1.1       jfb       850:                        }
                    851:
                    852:                        /* now get the expected semi-colon */
                    853:                        ntok = rcs_gettok(rfp);
                    854:                        if (ntok != RCS_TOK_SCOLON) {
                    855:                                cvs_log(LP_ERR,
                    856:                                    "missing semi-colon after RCS `%s' key",
                    857:                                    rk->rk_str);
                    858:                                return (-1);
                    859:                        }
                    860:                        break;
                    861:                case RCS_TOK_ACCESS:
                    862:                        rcs_parse_access(rfp);
                    863:                        break;
                    864:                case RCS_TOK_SYMBOLS:
                    865:                        rcs_parse_symbols(rfp);
                    866:                        break;
                    867:                case RCS_TOK_LOCKS:
                    868:                        rcs_parse_locks(rfp);
                    869:                        break;
                    870:                default:
                    871:                        cvs_log(LP_ERR,
                    872:                            "unexpected token `%s' in RCS admin section",
                    873:                            RCS_TOKSTR(rfp));
                    874:                        return (-1);
                    875:                }
                    876:        }
                    877:
                    878:        return (0);
                    879: }
                    880:
                    881:
                    882: /*
                    883:  * rcs_parse_delta()
                    884:  *
                    885:  * Parse an RCS delta section and allocate the structure to store that delta's
                    886:  * information in the <rfp> delta list.
                    887:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                    888:  * -1 on error.
                    889:  */
                    890:
                    891: static int
                    892: rcs_parse_delta(RCSFILE *rfp)
                    893: {
                    894:        int ret, tok, ntok, hmask;
                    895:        u_int i;
                    896:        char *tokstr;
1.3       vincent   897:        RCSNUM *datenum;
1.1       jfb       898:        struct rcs_delta *rdp;
                    899:        struct rcs_key *rk;
                    900:
                    901:        rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
                    902:        if (rdp == NULL) {
                    903:                cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
                    904:                return (-1);
                    905:        }
                    906:        memset(rdp, 0, sizeof(*rdp));
                    907:
                    908:        rdp->rd_num = rcsnum_alloc();
1.11      joris     909:        if (rdp->rd_num == NULL) {
                    910:                rcs_freedelta(rdp);
                    911:                return (-1);
                    912:        }
1.1       jfb       913:        rdp->rd_next = rcsnum_alloc();
1.11      joris     914:        if (rdp->rd_next == NULL) {
                    915:                rcs_freedelta(rdp);
                    916:                return (-1);
                    917:        }
1.1       jfb       918:
                    919:        TAILQ_INIT(&(rdp->rd_branches));
                    920:
                    921:        tok = rcs_gettok(rfp);
                    922:        if (tok != RCS_TOK_NUM) {
                    923:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                    924:                    RCS_TOKSTR(rfp));
                    925:                rcs_freedelta(rdp);
                    926:                return (-1);
                    927:        }
                    928:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                    929:
                    930:        hmask = 0;
                    931:        ret = 0;
                    932:        tokstr = NULL;
                    933:
                    934:        for (;;) {
                    935:                tok = rcs_gettok(rfp);
                    936:                if (tok == RCS_TOK_ERR) {
                    937:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                    938:                        rcs_freedelta(rdp);
                    939:                        return (-1);
                    940:                }
                    941:                else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
                    942:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                    943:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                    944:                        break;
                    945:                }
                    946:
                    947:                rk = NULL;
                    948:                for (i = 0; i < sizeof(rcs_keys)/sizeof(rcs_keys[0]); i++)
                    949:                        if (rcs_keys[i].rk_id == tok)
                    950:                                rk = &(rcs_keys[i]);
                    951:
                    952:                if (hmask & (1 << tok)) {
                    953:                        cvs_log(LP_ERR, "duplicate RCS key");
                    954:                        rcs_freedelta(rdp);
                    955:                        return (-1);
                    956:                }
                    957:                hmask |= (1 << tok);
                    958:
                    959:                switch (tok) {
                    960:                case RCS_TOK_DATE:
                    961:                case RCS_TOK_AUTHOR:
                    962:                case RCS_TOK_STATE:
                    963:                case RCS_TOK_NEXT:
                    964:                        ntok = rcs_gettok(rfp);
                    965:                        if (ntok == RCS_TOK_SCOLON) {
                    966:                                if (rk->rk_flags & RCS_VOPT)
                    967:                                        break;
                    968:                                else {
                    969:                                        cvs_log(LP_ERR, "missing mandatory "
                    970:                                            "value to RCS key `%s'",
                    971:                                            rk->rk_str);
                    972:                                        rcs_freedelta(rdp);
                    973:                                        return (-1);
                    974:                                }
                    975:                        }
                    976:
                    977:                        if (ntok != rk->rk_val) {
                    978:                                cvs_log(LP_ERR,
                    979:                                    "invalid value type for RCS key `%s'",
                    980:                                    rk->rk_str);
                    981:                                rcs_freedelta(rdp);
                    982:                                return (-1);
                    983:                        }
                    984:
                    985:                        if (tokstr != NULL)
                    986:                                free(tokstr);
                    987:                        tokstr = strdup(RCS_TOKSTR(rfp));
1.10      joris     988:                        if (tokstr == NULL) {
                    989:                                cvs_log(LP_ERRNO,
                    990:                                    "failed to duplicate rcs token");
                    991:                                rcs_freedelta(rdp);
                    992:                                return (-1);
                    993:                        }
1.1       jfb       994:
                    995:                        /* now get the expected semi-colon */
                    996:                        ntok = rcs_gettok(rfp);
                    997:                        if (ntok != RCS_TOK_SCOLON) {
                    998:                                cvs_log(LP_ERR,
                    999:                                    "missing semi-colon after RCS `%s' key",
                   1000:                                    rk->rk_str);
                   1001:                                rcs_freedelta(rdp);
                   1002:                                return (-1);
                   1003:                        }
                   1004:
                   1005:                        if (tok == RCS_TOK_DATE) {
1.3       vincent  1006:                                datenum = rcsnum_alloc();
1.11      joris    1007:                                if (datenum == NULL) {
                   1008:                                        rcs_freedelta(rdp);
                   1009:                                        return (-1);
                   1010:                                }
1.3       vincent  1011:                                rcsnum_aton(tokstr, NULL, datenum);
                   1012:                                if (datenum->rn_len != 6) {
1.1       jfb      1013:                                        cvs_log(LP_ERR,
                   1014:                                            "RCS date specification has %s "
                   1015:                                            "fields",
1.3       vincent  1016:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1017:                                            "missing");
                   1018:                                        rcs_freedelta(rdp);
                   1019:                                }
1.3       vincent  1020:                                rdp->rd_date.tm_year = datenum->rn_id[0];
                   1021:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1022:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1023:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1024:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1025:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1026:                                rcsnum_free(datenum);
1.1       jfb      1027:                        }
                   1028:                        else if (tok == RCS_TOK_AUTHOR) {
                   1029:                                rdp->rd_author = tokstr;
                   1030:                                tokstr = NULL;
                   1031:                        }
                   1032:                        else if (tok == RCS_TOK_STATE) {
                   1033:                                rdp->rd_state = tokstr;
                   1034:                                tokstr = NULL;
                   1035:                        }
                   1036:                        else if (tok == RCS_TOK_NEXT) {
                   1037:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1038:                        }
                   1039:                        break;
                   1040:                case RCS_TOK_BRANCHES:
                   1041:                        rcs_parse_branches(rfp, rdp);
                   1042:                        break;
                   1043:                default:
                   1044:                        cvs_log(LP_ERR,
                   1045:                            "unexpected token `%s' in RCS delta",
                   1046:                            RCS_TOKSTR(rfp));
                   1047:                        rcs_freedelta(rdp);
                   1048:                        return (-1);
                   1049:                }
                   1050:        }
                   1051:
                   1052:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
                   1053:
                   1054:        return (ret);
                   1055: }
                   1056:
                   1057:
                   1058: /*
                   1059:  * rcs_parse_deltatext()
                   1060:  *
                   1061:  * Parse an RCS delta text section and fill in the log and text field of the
                   1062:  * appropriate delta section.
                   1063:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1064:  * -1 on error.
                   1065:  */
                   1066:
                   1067: static int
                   1068: rcs_parse_deltatext(RCSFILE *rfp)
                   1069: {
                   1070:        int tok;
                   1071:        RCSNUM *tnum;
                   1072:        struct rcs_delta *rdp;
                   1073:
                   1074:        tnum = rcsnum_alloc();
                   1075:        if (tnum == NULL)
                   1076:                return (-1);
                   1077:
                   1078:        tok = rcs_gettok(rfp);
                   1079:        if (tok == RCS_TOK_EOF)
                   1080:                return (0);
                   1081:
                   1082:        if (tok != RCS_TOK_NUM) {
                   1083:                cvs_log(LP_ERR,
                   1084:                    "unexpected token `%s' at start of RCS delta text",
                   1085:                    RCS_TOKSTR(rfp));
                   1086:                return (-1);
                   1087:        }
                   1088:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   1089:
                   1090:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1091:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   1092:                        break;
                   1093:        }
                   1094:        if (rdp == NULL) {
                   1095:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   1096:                    RCS_TOKSTR(rfp));
                   1097:                return (-1);
                   1098:        }
                   1099:
                   1100:        tok = rcs_gettok(rfp);
                   1101:        if (tok != RCS_TOK_LOG) {
                   1102:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1103:                    RCS_TOKSTR(rfp));
                   1104:                return (-1);
                   1105:        }
                   1106:
                   1107:        tok = rcs_gettok(rfp);
                   1108:        if (tok != RCS_TOK_STRING) {
                   1109:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1110:                    RCS_TOKSTR(rfp));
                   1111:                return (-1);
                   1112:        }
                   1113:        rdp->rd_log = strdup(RCS_TOKSTR(rfp));
                   1114:        if (rdp->rd_log == NULL) {
                   1115:                cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
                   1116:                return (-1);
                   1117:        }
                   1118:
                   1119:        tok = rcs_gettok(rfp);
                   1120:        if (tok != RCS_TOK_TEXT) {
                   1121:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1122:                    RCS_TOKSTR(rfp));
                   1123:                return (-1);
                   1124:        }
                   1125:
                   1126:        tok = rcs_gettok(rfp);
                   1127:        if (tok != RCS_TOK_STRING) {
                   1128:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1129:                    RCS_TOKSTR(rfp));
                   1130:                return (-1);
                   1131:        }
                   1132:
                   1133:        rdp->rd_text = strdup(RCS_TOKSTR(rfp));
                   1134:        if (rdp->rd_text == NULL) {
                   1135:                cvs_log(LP_ERRNO, "failed to copy RCS delta text");
                   1136:                return (-1);
                   1137:        }
                   1138:
                   1139:        return (1);
                   1140: }
                   1141:
                   1142:
                   1143: /*
                   1144:  * rcs_parse_access()
                   1145:  *
                   1146:  * Parse the access list given as value to the `access' keyword.
                   1147:  * Returns 0 on success, or -1 on failure.
                   1148:  */
                   1149:
                   1150: static int
                   1151: rcs_parse_access(RCSFILE *rfp)
                   1152: {
                   1153:        int type;
                   1154:
                   1155:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   1156:                if (type != RCS_TOK_ID) {
                   1157:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   1158:                            RCS_TOKSTR(rfp));
                   1159:                        return (-1);
                   1160:                }
                   1161:        }
                   1162:
                   1163:        return (0);
                   1164: }
                   1165:
                   1166:
                   1167: /*
                   1168:  * rcs_parse_symbols()
                   1169:  *
                   1170:  * Parse the symbol list given as value to the `symbols' keyword.
                   1171:  * Returns 0 on success, or -1 on failure.
                   1172:  */
                   1173:
                   1174: static int
                   1175: rcs_parse_symbols(RCSFILE *rfp)
                   1176: {
                   1177:        int type;
                   1178:        struct rcs_sym *symp;
                   1179:
                   1180:        for (;;) {
                   1181:                type = rcs_gettok(rfp);
                   1182:                if (type == RCS_TOK_SCOLON)
                   1183:                        break;
                   1184:
                   1185:                if (type != RCS_TOK_STRING) {
                   1186:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1187:                            RCS_TOKSTR(rfp));
                   1188:                        return (-1);
                   1189:                }
                   1190:
                   1191:                symp = (struct rcs_sym *)malloc(sizeof(*symp));
                   1192:                if (symp == NULL) {
                   1193:                        cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                   1194:                        return (-1);
                   1195:                }
                   1196:                symp->rs_name = strdup(RCS_TOKSTR(rfp));
1.10      joris    1197:                if (symp->rs_name == NULL) {
                   1198:                        cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1199:                        free(symp);
                   1200:                        return (-1);
                   1201:                }
                   1202:
1.1       jfb      1203:                symp->rs_num = rcsnum_alloc();
1.11      joris    1204:                if (symp->rs_num == NULL) {
                   1205:                        cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
                   1206:                        free(symp);
                   1207:                        return (-1);
                   1208:                }
1.1       jfb      1209:
                   1210:                type = rcs_gettok(rfp);
                   1211:                if (type != RCS_TOK_COLON) {
                   1212:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1213:                            RCS_TOKSTR(rfp));
1.11      joris    1214:                        rcsnum_free(symp->rs_num);
1.1       jfb      1215:                        free(symp->rs_name);
                   1216:                        free(symp);
                   1217:                        return (-1);
                   1218:                }
                   1219:
                   1220:                type = rcs_gettok(rfp);
                   1221:                if (type != RCS_TOK_NUM) {
                   1222:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1223:                            RCS_TOKSTR(rfp));
1.11      joris    1224:                        rcsnum_free(symp->rs_num);
1.1       jfb      1225:                        free(symp->rs_name);
                   1226:                        free(symp);
                   1227:                        return (-1);
                   1228:                }
                   1229:
                   1230:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   1231:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1232:                            RCS_TOKSTR(rfp));
1.11      joris    1233:                        rcsnum_free(symp->rs_num);
1.1       jfb      1234:                        free(symp->rs_name);
                   1235:                        free(symp);
                   1236:                        return (-1);
                   1237:                }
                   1238:
                   1239:                TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                   1240:        }
                   1241:
                   1242:        return (0);
                   1243: }
                   1244:
                   1245:
                   1246: /*
                   1247:  * rcs_parse_locks()
                   1248:  *
                   1249:  * Parse the lock list given as value to the `locks' keyword.
                   1250:  * Returns 0 on success, or -1 on failure.
                   1251:  */
                   1252:
                   1253: static int
                   1254: rcs_parse_locks(RCSFILE *rfp)
                   1255: {
                   1256:        int type;
                   1257:        struct rcs_lock *lkp;
                   1258:
                   1259:        for (;;) {
                   1260:                type = rcs_gettok(rfp);
                   1261:                if (type == RCS_TOK_SCOLON)
                   1262:                        break;
                   1263:
                   1264:                if (type != RCS_TOK_ID) {
                   1265:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   1266:                            RCS_TOKSTR(rfp));
                   1267:                        return (-1);
                   1268:                }
                   1269:
                   1270:                lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
                   1271:                if (lkp == NULL) {
                   1272:                        cvs_log(LP_ERRNO, "failed to allocate RCS lock");
                   1273:                        return (-1);
                   1274:                }
                   1275:                lkp->rl_num = rcsnum_alloc();
1.11      joris    1276:                if (lkp->rl_num == NULL) {
                   1277:                        free(lkp);
                   1278:                        return (-1);
                   1279:                }
1.1       jfb      1280:
                   1281:                type = rcs_gettok(rfp);
                   1282:                if (type != RCS_TOK_COLON) {
                   1283:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1284:                            RCS_TOKSTR(rfp));
                   1285:                        free(lkp);
                   1286:                        return (-1);
                   1287:                }
                   1288:
                   1289:                type = rcs_gettok(rfp);
                   1290:                if (type != RCS_TOK_NUM) {
                   1291:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1292:                            RCS_TOKSTR(rfp));
                   1293:                        free(lkp);
                   1294:                        return (-1);
                   1295:                }
                   1296:
                   1297:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   1298:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1299:                            RCS_TOKSTR(rfp));
                   1300:                        free(lkp);
                   1301:                        return (-1);
                   1302:                }
                   1303:
                   1304:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   1305:        }
                   1306:
                   1307:        /* check if we have a `strict' */
                   1308:        type = rcs_gettok(rfp);
                   1309:        if (type != RCS_TOK_STRICT) {
                   1310:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
                   1311:        }
                   1312:        else {
                   1313:                rfp->rf_flags |= RCS_RF_SLOCK;
                   1314:
                   1315:                type = rcs_gettok(rfp);
                   1316:                if (type != RCS_TOK_SCOLON) {
                   1317:                        cvs_log(LP_ERR,
                   1318:                            "missing semi-colon after `strict' keyword");
                   1319:                        return (-1);
                   1320:                }
                   1321:        }
                   1322:
                   1323:        return (0);
                   1324: }
                   1325:
                   1326: /*
                   1327:  * rcs_parse_branches()
                   1328:  *
                   1329:  * Parse the list of branches following a `branches' keyword in a delta.
                   1330:  * Returns 0 on success, or -1 on failure.
                   1331:  */
                   1332:
                   1333: static int
                   1334: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   1335: {
                   1336:        int type;
                   1337:        struct rcs_branch *brp;
                   1338:
                   1339:        for (;;) {
                   1340:                type = rcs_gettok(rfp);
                   1341:                if (type == RCS_TOK_SCOLON)
                   1342:                        break;
                   1343:
                   1344:                if (type != RCS_TOK_NUM) {
                   1345:                        cvs_log(LP_ERR,
                   1346:                            "unexpected token `%s' in list of branches",
                   1347:                            RCS_TOKSTR(rfp));
                   1348:                        return (-1);
                   1349:                }
                   1350:
                   1351:                brp = (struct rcs_branch *)malloc(sizeof(*brp));
                   1352:                if (brp == NULL) {
                   1353:                        cvs_log(LP_ERRNO, "failed to allocate RCS branch");
                   1354:                        return (-1);
                   1355:                }
                   1356:                brp->rb_num = rcsnum_alloc();
1.11      joris    1357:                if (brp->rb_num == NULL) {
                   1358:                        free(brp);
                   1359:                        return (-1);
                   1360:                }
                   1361:
1.1       jfb      1362:                rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
                   1363:
                   1364:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   1365:        }
                   1366:
                   1367:        return (0);
                   1368: }
                   1369:
                   1370:
                   1371: /*
                   1372:  * rcs_freedelta()
                   1373:  *
                   1374:  * Free the contents of a delta structure.
                   1375:  */
                   1376:
                   1377: void
                   1378: rcs_freedelta(struct rcs_delta *rdp)
                   1379: {
1.12    ! jfb      1380:        struct rcs_branch *rb;
1.1       jfb      1381:        struct rcs_delta *crdp;
                   1382:
1.12    ! jfb      1383:        if (rdp->rd_num != NULL)
        !          1384:                rcsnum_free(rdp->rd_num);
        !          1385:        if (rdp->rd_next != NULL)
        !          1386:                rcsnum_free(rdp->rd_next);
        !          1387:
1.1       jfb      1388:        if (rdp->rd_author != NULL)
                   1389:                free(rdp->rd_author);
                   1390:        if (rdp->rd_state != NULL)
                   1391:                free(rdp->rd_state);
                   1392:        if (rdp->rd_log != NULL)
                   1393:                free(rdp->rd_log);
                   1394:        if (rdp->rd_text != NULL)
                   1395:                free(rdp->rd_text);
1.12    ! jfb      1396:
        !          1397:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
        !          1398:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
        !          1399:                rcsnum_free(rb->rb_num);
        !          1400:                free(rb);
        !          1401:        }
1.1       jfb      1402:
                   1403:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   1404:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   1405:                rcs_freedelta(crdp);
                   1406:        }
                   1407:
                   1408:        free(rdp);
                   1409: }
                   1410:
                   1411:
                   1412: /*
                   1413:  * rcs_freepdata()
                   1414:  *
                   1415:  * Free the contents of the parser data structure.
                   1416:  */
                   1417:
                   1418: static void
                   1419: rcs_freepdata(struct rcs_pdata *pd)
                   1420: {
                   1421:        if (pd->rp_file != NULL)
                   1422:                (void)fclose(pd->rp_file);
                   1423:        if (pd->rp_buf != NULL)
                   1424:                free(pd->rp_buf);
                   1425:        free(pd);
                   1426: }
                   1427:
                   1428:
                   1429: /*
                   1430:  * rcs_gettok()
                   1431:  *
                   1432:  * Get the next RCS token from the string <str>.
                   1433:  */
                   1434:
                   1435: static int
                   1436: rcs_gettok(RCSFILE *rfp)
                   1437: {
                   1438:        u_int i;
                   1439:        int ch, last, type;
                   1440:        char *bp, *bep;
                   1441:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1442:
                   1443:        type = RCS_TOK_ERR;
                   1444:        bp = pdp->rp_buf;
                   1445:        bep = pdp->rp_buf + pdp->rp_blen - 1;
                   1446:        *bp = '\0';
                   1447:
                   1448:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   1449:                type = pdp->rp_pttype;
                   1450:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   1451:                pdp->rp_pttype = RCS_TOK_ERR;
                   1452:                return (type);
                   1453:        }
                   1454:
                   1455:        /* skip leading whitespace */
                   1456:        /* XXX we must skip backspace too for compatibility, should we? */
                   1457:        do {
                   1458:                ch = getc(pdp->rp_file);
                   1459:                if (ch == '\n')
                   1460:                        pdp->rp_line++;
                   1461:        } while (isspace(ch));
                   1462:
                   1463:        if (ch == EOF) {
                   1464:                type = RCS_TOK_EOF;
                   1465:        }
                   1466:        else if (ch == ';') {
                   1467:                type = RCS_TOK_SCOLON;
                   1468:        }
                   1469:        else if (ch == ':') {
                   1470:                type = RCS_TOK_COLON;
                   1471:        }
                   1472:        else if (isalpha(ch)) {
                   1473:                *(bp++) = ch;
                   1474:                while (bp <= bep - 1) {
                   1475:                        ch = getc(pdp->rp_file);
1.11      joris    1476:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      1477:                                ungetc(ch, pdp->rp_file);
                   1478:                                break;
                   1479:                        }
                   1480:                        *(bp++) = ch;
                   1481:                }
                   1482:                *bp = '\0';
                   1483:
                   1484:                for (i = 0; i < sizeof(rcs_keys)/sizeof(rcs_keys[0]); i++) {
                   1485:                        if (strcmp(rcs_keys[i].rk_str, pdp->rp_buf) == 0) {
                   1486:                                type = rcs_keys[i].rk_id;
                   1487:                                break;
                   1488:                        }
                   1489:                }
                   1490:
                   1491:                /* not a keyword, assume it's just a string */
                   1492:                if (type == RCS_TOK_ERR)
                   1493:                        type = RCS_TOK_STRING;
1.11      joris    1494:
1.1       jfb      1495:        }
                   1496:        else if (ch == '@') {
                   1497:                /* we have a string */
                   1498:                for (;;) {
                   1499:                        ch = getc(pdp->rp_file);
                   1500:                        if (ch == '@') {
                   1501:                                ch = getc(pdp->rp_file);
                   1502:                                if (ch != '@') {
                   1503:                                        ungetc(ch, pdp->rp_file);
                   1504:                                        break;
                   1505:                                }
                   1506:                        }
                   1507:                        else if (ch == '\n')
                   1508:                                pdp->rp_line++;
                   1509:
                   1510:                        *(bp++) = ch;
                   1511:                        if (bp == bep)
                   1512:                                break;
                   1513:                }
                   1514:
                   1515:                *bp = '\0';
                   1516:                type = RCS_TOK_STRING;
                   1517:        }
                   1518:        else if (isdigit(ch)) {
                   1519:                *(bp++) = ch;
                   1520:                last = ch;
                   1521:                type = RCS_TOK_NUM;
                   1522:
                   1523:                for (;;) {
                   1524:                        ch = getc(pdp->rp_file);
                   1525:                        if (bp == bep)
                   1526:                                break;
                   1527:                        if (!isdigit(ch) && ch != '.') {
                   1528:                                ungetc(ch, pdp->rp_file);
                   1529:                                break;
                   1530:                        }
                   1531:
                   1532:                        if (last == '.' && ch == '.') {
                   1533:                                type = RCS_TOK_ERR;
                   1534:                                break;
                   1535:                        }
                   1536:                        last = ch;
                   1537:                        *(bp++) = ch;
                   1538:                }
                   1539:                *(bp) = '\0';
                   1540:        }
                   1541:
                   1542:        return (type);
                   1543: }
                   1544:
                   1545:
                   1546: /*
                   1547:  * rcs_pushtok()
                   1548:  *
                   1549:  * Push a token back in the parser's token buffer.
                   1550:  */
                   1551:
                   1552: static int
                   1553: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   1554: {
                   1555:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1556:
                   1557:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   1558:                return (-1);
                   1559:
                   1560:        pdp->rp_pttype = type;
                   1561:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   1562:        return (0);
                   1563: }
                   1564:
                   1565:
                   1566: /*
                   1567:  * rcs_stresc()
                   1568:  *
                   1569:  * Performs either escaping or unescaping of the string stored in <str>.
                   1570:  * The operation is to escape special RCS characters if the <esc> argument
                   1571:  * is 1, or unescape otherwise.  The result is stored in the <buf> destination
                   1572:  * buffer, and <blen> must originally point to the size of <buf>.
                   1573:  * Returns the number of bytes which have been read from the source <str> and
                   1574:  * operated on.  The <blen> parameter will contain the number of bytes
                   1575:  * actually copied in <buf>.
                   1576:  */
                   1577:
                   1578: size_t
                   1579: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
                   1580: {
                   1581:        size_t rlen;
                   1582:        const char *sp;
                   1583:        char *bp, *bep;
                   1584:
                   1585:        rlen = 0;
                   1586:        bp = buf;
                   1587:        bep = buf + *blen - 1;
                   1588:
                   1589:        for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
                   1590:                if (*sp == '@') {
                   1591:                        if (esc) {
                   1592:                                if (bp > (bep - 2))
                   1593:                                        break;
                   1594:                                *(bp++) = '@';
                   1595:                        }
                   1596:                        else {
                   1597:                                sp++;
                   1598:                                if (*sp != '@') {
                   1599:                                        cvs_log(LP_WARN,
                   1600:                                            "unknown escape character `%c' in "
                   1601:                                            "RCS file", *sp);
                   1602:                                        if (*sp == '\0')
                   1603:                                                break;
                   1604:                                }
                   1605:                        }
                   1606:                }
                   1607:
                   1608:                *(bp++) = *sp;
                   1609:        }
                   1610:
                   1611:        *bp = '\0';
                   1612:        *blen = (bp - buf);
                   1613:        return (sp - str);
                   1614: }
                   1615:
                   1616:
                   1617: /*
                   1618:  * rcs_splitlines()
                   1619:  *
                   1620:  * Split the contents of a file into a list of lines.
                   1621:  */
                   1622:
                   1623: static struct rcs_foo*
                   1624: rcs_splitlines(const char *fcont)
                   1625: {
                   1626:        char *dcp;
                   1627:        struct rcs_foo *foo;
                   1628:        struct rcs_line *lp;
                   1629:
                   1630:        foo = (struct rcs_foo *)malloc(sizeof(*foo));
                   1631:        if (foo == NULL) {
                   1632:                cvs_log(LP_ERR, "failed to allocate line structure");
                   1633:                return (NULL);
                   1634:        }
                   1635:        TAILQ_INIT(&(foo->rl_lines));
                   1636:        foo->rl_nblines = 0;
                   1637:        foo->rl_data = strdup(fcont);
                   1638:        if (foo->rl_data == NULL) {
                   1639:                cvs_log(LP_ERRNO, "failed to copy file contents");
                   1640:                free(foo);
                   1641:                return (NULL);
                   1642:        }
                   1643:
                   1644:        /*
                   1645:         * Add a first bogus line with line number 0.  This is used so we
                   1646:         * can position the line pointer before 1 when changing the first line
                   1647:         * in rcs_patch().
                   1648:         */
                   1649:        lp = (struct rcs_line *)malloc(sizeof(*lp));
1.5       vincent  1650:        if (lp == NULL)
1.1       jfb      1651:                return (NULL);
1.5       vincent  1652:
1.1       jfb      1653:        lp->rl_line = NULL;
                   1654:        lp->rl_lineno = 0;
                   1655:        TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   1656:
                   1657:
                   1658:        for (dcp = foo->rl_data; *dcp != '\0';) {
                   1659:                lp = (struct rcs_line *)malloc(sizeof(*lp));
                   1660:                if (lp == NULL) {
                   1661:                        cvs_log(LP_ERR, "failed to allocate line entry");
                   1662:                        return (NULL);
                   1663:                }
                   1664:
                   1665:                lp->rl_line = dcp;
                   1666:                lp->rl_lineno = ++(foo->rl_nblines);
                   1667:                TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   1668:
                   1669:                dcp = strchr(dcp, '\n');
                   1670:                if (dcp == NULL) {
                   1671:                        break;
                   1672:                }
                   1673:                *(dcp++) = '\0';
                   1674:        }
                   1675:
                   1676:        return (foo);
1.5       vincent  1677: }
                   1678:
                   1679: static void
                   1680: rcs_freefoo(struct rcs_foo *fp)
                   1681: {
                   1682:        struct rcs_line *lp;
                   1683:
                   1684:        while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
                   1685:                TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
                   1686:                free(lp);
                   1687:        }
                   1688:        free(fp->rl_data);
                   1689:        free(fp);
1.1       jfb      1690: }