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

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