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

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