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

1.29    ! jfb         1: /*     $OpenBSD: rcs.c,v 1.28 2005/03/03 21:02:23 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>
1.26      jfb        35: #include <stdarg.h>
1.1       jfb        36: #include <string.h>
                     37:
                     38: #include "rcs.h"
                     39: #include "log.h"
                     40:
1.26      jfb        41: #define RCS_BUFSIZE     16384
1.18      jfb        42: #define RCS_BUFEXTSIZE   8192
1.1       jfb        43:
                     44:
                     45: /* RCS token types */
                     46: #define RCS_TOK_ERR     -1
                     47: #define RCS_TOK_EOF      0
                     48: #define RCS_TOK_NUM      1
                     49: #define RCS_TOK_ID       2
                     50: #define RCS_TOK_STRING   3
                     51: #define RCS_TOK_SCOLON   4
                     52: #define RCS_TOK_COLON    5
                     53:
                     54:
                     55: #define RCS_TOK_HEAD     8
                     56: #define RCS_TOK_BRANCH   9
                     57: #define RCS_TOK_ACCESS   10
                     58: #define RCS_TOK_SYMBOLS  11
                     59: #define RCS_TOK_LOCKS    12
                     60: #define RCS_TOK_COMMENT  13
                     61: #define RCS_TOK_EXPAND   14
                     62: #define RCS_TOK_DATE     15
                     63: #define RCS_TOK_AUTHOR   16
                     64: #define RCS_TOK_STATE    17
                     65: #define RCS_TOK_NEXT     18
                     66: #define RCS_TOK_BRANCHES 19
                     67: #define RCS_TOK_DESC     20
                     68: #define RCS_TOK_LOG      21
                     69: #define RCS_TOK_TEXT     22
                     70: #define RCS_TOK_STRICT   23
                     71:
                     72: #define RCS_ISKEY(t)    (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
                     73:
                     74:
                     75: #define RCS_NOSCOL   0x01   /* no terminating semi-colon */
                     76: #define RCS_VOPT     0x02   /* value is optional */
                     77:
                     78:
                     79: /* opaque parse data */
                     80: struct rcs_pdata {
1.18      jfb        81:        u_int  rp_lines;
1.1       jfb        82:
                     83:        char  *rp_buf;
                     84:        size_t rp_blen;
1.18      jfb        85:        char  *rp_bufend;
1.1       jfb        86:
                     87:        /* pushback token buffer */
                     88:        char   rp_ptok[128];
                     89:        int    rp_pttype;       /* token type, RCS_TOK_ERR if no token */
                     90:
                     91:        FILE  *rp_file;
                     92: };
                     93:
                     94:
                     95: struct rcs_line {
                     96:        char *rl_line;
                     97:        int   rl_lineno;
                     98:        TAILQ_ENTRY(rcs_line) rl_list;
                     99: };
1.5       vincent   100: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1       jfb       101:
                    102: struct rcs_foo {
                    103:        int       rl_nblines;
                    104:        char     *rl_data;
1.5       vincent   105:        struct rcs_tqh rl_lines;
1.1       jfb       106: };
                    107:
                    108: #define RCS_TOKSTR(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
                    109: #define RCS_TOKLEN(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen
                    110:
                    111:
1.20      jfb       112: static struct rcs_kfl {
                    113:        char  rk_char;
                    114:        int   rk_val;
                    115: } rcs_kflags[] = {
                    116:        { 'k',   RCS_KWEXP_NAME },
                    117:        { 'v',   RCS_KWEXP_VAL  },
                    118:        { 'l',   RCS_KWEXP_LKR  },
                    119:        { 'o',   RCS_KWEXP_OLD  },
                    120:        { 'b',   RCS_KWEXP_NONE },
                    121: };
                    122:
1.1       jfb       123: static struct rcs_key {
                    124:        char  rk_str[16];
                    125:        int   rk_id;
                    126:        int   rk_val;
                    127:        int   rk_flags;
                    128: } rcs_keys[] = {
                    129:        { "access",   RCS_TOK_ACCESS,   RCS_TOK_ID,     RCS_VOPT     },
                    130:        { "author",   RCS_TOK_AUTHOR,   RCS_TOK_STRING, 0            },
                    131:        { "branch",   RCS_TOK_BRANCH,   RCS_TOK_NUM,    RCS_VOPT     },
                    132:        { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM,    RCS_VOPT     },
                    133:        { "comment",  RCS_TOK_COMMENT,  RCS_TOK_STRING, RCS_VOPT     },
                    134:        { "date",     RCS_TOK_DATE,     RCS_TOK_NUM,    0            },
                    135:        { "desc",     RCS_TOK_DESC,     RCS_TOK_STRING, RCS_NOSCOL   },
                    136:        { "expand",   RCS_TOK_EXPAND,   RCS_TOK_STRING, RCS_VOPT     },
                    137:        { "head",     RCS_TOK_HEAD,     RCS_TOK_NUM,    RCS_VOPT     },
                    138:        { "locks",    RCS_TOK_LOCKS,    RCS_TOK_ID,     0            },
                    139:        { "log",      RCS_TOK_LOG,      RCS_TOK_STRING, RCS_NOSCOL   },
                    140:        { "next",     RCS_TOK_NEXT,     RCS_TOK_NUM,    RCS_VOPT     },
                    141:        { "state",    RCS_TOK_STATE,    RCS_TOK_STRING, RCS_VOPT     },
                    142:        { "strict",   RCS_TOK_STRICT,   0,              0,           },
                    143:        { "symbols",  RCS_TOK_SYMBOLS,  0,              0            },
                    144:        { "text",     RCS_TOK_TEXT,     RCS_TOK_STRING, RCS_NOSCOL   },
                    145: };
                    146:
1.18      jfb       147: #define RCS_NKEYS   (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1       jfb       148:
                    149:
1.27      jfb       150: static int   rcs_write           (RCSFILE *);
1.26      jfb       151: static int   rcs_parse           (RCSFILE *);
                    152: static int   rcs_parse_admin     (RCSFILE *);
                    153: static int   rcs_parse_delta     (RCSFILE *);
                    154: static int   rcs_parse_deltatext (RCSFILE *);
                    155:
                    156: static int   rcs_parse_access    (RCSFILE *);
                    157: static int   rcs_parse_symbols   (RCSFILE *);
                    158: static int   rcs_parse_locks     (RCSFILE *);
                    159: static int   rcs_parse_branches  (RCSFILE *, struct rcs_delta *);
                    160: static void  rcs_freedelta       (struct rcs_delta *);
                    161: static void  rcs_freepdata       (struct rcs_pdata *);
                    162: static int   rcs_gettok          (RCSFILE *);
                    163: static int   rcs_pushtok         (RCSFILE *, const char *, int);
                    164: static int   rcs_growbuf         (RCSFILE *);
                    165: static int   rcs_patch_lines     (struct rcs_foo *, struct rcs_foo *);
                    166:
                    167: static struct rcs_delta*  rcs_findrev    (RCSFILE *, RCSNUM *);
                    168: static struct rcs_foo*    rcs_splitlines (const char *);
                    169: static void               rcs_freefoo    (struct rcs_foo *);
                    170:
                    171:
1.1       jfb       172: /*
                    173:  * rcs_open()
                    174:  *
                    175:  * Open a file containing RCS-formatted information.  The file's path is
1.26      jfb       176:  * given in <path>, and the opening flags are given in <flags>, which is either
                    177:  * RCS_READ, RCS_WRITE, or RCS_RDWR.  If the open requests write access and
                    178:  * the file does not exist, the RCS_CREATE flag must also be given, in which
                    179:  * case it will be created with the mode specified in a third argument of
                    180:  * type mode_t.  If the file exists and RCS_CREATE is passed, the open will
                    181:  * fail.
1.1       jfb       182:  * Returns a handle to the opened file on success, or NULL on failure.
                    183:  */
                    184: RCSFILE*
1.26      jfb       185: rcs_open(const char *path, int flags, ...)
1.1       jfb       186: {
1.26      jfb       187:        int ret;
                    188:        mode_t fmode;
1.1       jfb       189:        RCSFILE *rfp;
                    190:        struct stat st;
1.26      jfb       191:        va_list vap;
                    192:
                    193:        fmode = 0;
                    194:        flags &= 0xffff;        /* ditch any internal flags */
1.1       jfb       195:
1.26      jfb       196:        if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
                    197:                if (flags & RCS_CREATE) {
                    198:                        va_start(vap, flags);
                    199:                        fmode = va_arg(vap, mode_t);
                    200:                        va_end(vap);
                    201:                } else {
                    202:                        cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
                    203:                        return (NULL);
                    204:                }
                    205:        } else if ((ret == 0) && (flags & RCS_CREATE)) {
                    206:                cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1       jfb       207:                return (NULL);
                    208:        }
                    209:
1.26      jfb       210:        if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1       jfb       211:                cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
                    212:                return (NULL);
                    213:        }
                    214:        memset(rfp, 0, sizeof(*rfp));
                    215:
1.26      jfb       216:        if ((rfp->rf_branch = rcsnum_alloc()) == NULL) {
                    217:                free(rfp);
1.11      joris     218:                return (NULL);
                    219:        }
                    220:
1.26      jfb       221:        if ((rfp->rf_path = strdup(path)) == NULL) {
1.1       jfb       222:                cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26      jfb       223:                rcsnum_free(rfp->rf_branch);
                    224:                free(rfp);
1.1       jfb       225:                return (NULL);
                    226:        }
                    227:
                    228:        rfp->rf_ref = 1;
1.26      jfb       229:        rfp->rf_flags = flags | RCS_SLOCK;
                    230:        rfp->rf_mode = fmode;
1.1       jfb       231:
                    232:        TAILQ_INIT(&(rfp->rf_delta));
1.29    ! jfb       233:        TAILQ_INIT(&(rfp->rf_access));
1.1       jfb       234:        TAILQ_INIT(&(rfp->rf_symbols));
                    235:        TAILQ_INIT(&(rfp->rf_locks));
                    236:
1.26      jfb       237:        if (rfp->rf_flags & RCS_CREATE) {
                    238:        } else if (rcs_parse(rfp) < 0) {
1.1       jfb       239:                rcs_close(rfp);
                    240:                return (NULL);
                    241:        }
                    242:
                    243:        return (rfp);
                    244: }
                    245:
                    246:
                    247: /*
                    248:  * rcs_close()
                    249:  *
                    250:  * Close an RCS file handle.
                    251:  */
                    252: void
                    253: rcs_close(RCSFILE *rfp)
                    254: {
                    255:        struct rcs_delta *rdp;
1.13      jfb       256:        struct rcs_lock *rlp;
                    257:        struct rcs_sym *rsp;
1.1       jfb       258:
                    259:        if (rfp->rf_ref > 1) {
                    260:                rfp->rf_ref--;
                    261:                return;
                    262:        }
                    263:
1.26      jfb       264:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    265:                rcs_write(rfp);
                    266:
1.1       jfb       267:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    268:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    269:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    270:                rcs_freedelta(rdp);
                    271:        }
                    272:
1.13      jfb       273:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    274:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    275:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    276:                rcsnum_free(rsp->rs_num);
                    277:                free(rsp->rs_name);
                    278:                free(rsp);
                    279:        }
                    280:
                    281:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    282:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    283:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    284:                rcsnum_free(rlp->rl_num);
                    285:                free(rlp);
                    286:        }
                    287:
1.1       jfb       288:        if (rfp->rf_head != NULL)
                    289:                rcsnum_free(rfp->rf_head);
1.11      joris     290:        if (rfp->rf_branch != NULL)
                    291:                rcsnum_free(rfp->rf_branch);
1.1       jfb       292:
                    293:        if (rfp->rf_path != NULL)
                    294:                free(rfp->rf_path);
                    295:        if (rfp->rf_comment != NULL)
                    296:                free(rfp->rf_comment);
                    297:        if (rfp->rf_expand != NULL)
                    298:                free(rfp->rf_expand);
                    299:        if (rfp->rf_desc != NULL)
                    300:                free(rfp->rf_desc);
                    301:        free(rfp);
                    302: }
                    303:
                    304:
                    305: /*
                    306:  * rcs_write()
                    307:  *
                    308:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    309:  * path is in <rf_path>.
                    310:  * Returns 0 on success, or -1 on failure.
                    311:  */
1.27      jfb       312: static int
1.1       jfb       313: rcs_write(RCSFILE *rfp)
                    314: {
                    315:        FILE *fp;
1.7       jfb       316:        char buf[1024], numbuf[64], *cp;
                    317:        size_t rlen, len;
1.29    ! jfb       318:        struct rcs_access *ap;
1.1       jfb       319:        struct rcs_sym *symp;
                    320:        struct rcs_delta *rdp;
                    321:
1.28      jfb       322:        if (rfp->rf_flags & RCS_SYNCED)
1.1       jfb       323:                return (0);
                    324:
1.29    ! jfb       325:        if ((fp = fopen(rfp->rf_path, "w")) == NULL) {
1.1       jfb       326:                cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
                    327:                    rfp->rf_path);
                    328:                return (-1);
                    329:        }
                    330:
1.28      jfb       331:        if (rfp->rf_head != NULL)
                    332:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    333:        else
                    334:                numbuf[0] = '\0';
                    335:
1.1       jfb       336:        fprintf(fp, "head\t%s;\n", numbuf);
1.29    ! jfb       337:        fputs("access", fp);
        !           338:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
        !           339:                fprintf(fp, "\n\t%s", ap->ra_name);
        !           340:        }
        !           341:        fputs(";\n", fp);
1.1       jfb       342:
                    343:        fprintf(fp, "symbols\n");
                    344:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    345:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
                    346:                snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
                    347:                fprintf(fp, "\t%s", buf);
                    348:                if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
                    349:                        fputc('\n', fp);
                    350:        }
                    351:        fprintf(fp, ";\n");
                    352:
                    353:        fprintf(fp, "locks;");
                    354:
1.26      jfb       355:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       356:                fprintf(fp, " strict;");
                    357:        fputc('\n', fp);
                    358:
                    359:        if (rfp->rf_comment != NULL)
                    360:                fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);
                    361:
                    362:        if (rfp->rf_expand != NULL)
                    363:                fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);
                    364:
                    365:        fprintf(fp, "\n\n");
                    366:
                    367:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    368:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    369:                    sizeof(numbuf)));
                    370:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
                    371:                    rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
                    372:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    373:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    374:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    375:                    rdp->rd_author, rdp->rd_state);
                    376:                fprintf(fp, "branches;\n");
                    377:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    378:                    numbuf, sizeof(numbuf)));
                    379:        }
                    380:
1.26      jfb       381:        fprintf(fp, "\ndesc\n@%s@\n\n",
                    382:            (rfp->rf_desc == NULL) ? "" : rfp->rf_desc);
1.1       jfb       383:
                    384:        /* deltatexts */
                    385:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    386:                fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    387:                    sizeof(numbuf)));
1.7       jfb       388:                fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);
                    389:
                    390:                cp = rdp->rd_text;
                    391:                do {
                    392:                        len = sizeof(buf);
                    393:                        rlen = rcs_stresc(1, cp, buf, &len);
                    394:                        fprintf(fp, "%s", buf);
                    395:                        cp += rlen;
                    396:                } while (len != 0);
                    397:                fprintf(fp, "@\n\n");
1.1       jfb       398:        }
                    399:        fclose(fp);
                    400:
1.26      jfb       401:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb       402:
                    403:        return (0);
                    404: }
                    405:
                    406:
                    407: /*
1.29    ! jfb       408:  * rcs_access_add()
        !           409:  *
        !           410:  * Add the login name <login> to the access list for the RCS file <file>.
        !           411:  * Returns 0 on success, or -1 on failure.
        !           412:  */
        !           413: int
        !           414: rcs_access_add(RCSFILE *file, const char *login)
        !           415: {
        !           416:        struct rcs_access *ap;
        !           417:
        !           418:        /* first look for duplication */
        !           419:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
        !           420:                if (strcmp(ap->ra_name, login) == 0) {
        !           421:                        cvs_log(LP_ERR, "attempt to add duplicate access `%s'",
        !           422:                            login);
        !           423:                        return (-1);
        !           424:                }
        !           425:        }
        !           426:
        !           427:        ap = (struct rcs_access *)malloc(sizeof(*ap));
        !           428:        if (ap == NULL) {
        !           429:                cvs_log(LP_ERRNO, "failed to allocate RCS access entry");
        !           430:                return (-1);
        !           431:        }
        !           432:
        !           433:        ap->ra_name = strdup(login);
        !           434:        if (ap->ra_name == NULL) {
        !           435:                cvs_log(LP_ERRNO, "failed to duplicate user name");
        !           436:                free(ap);
        !           437:                return (-1);
        !           438:        }
        !           439:
        !           440:        TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
        !           441:
        !           442:        /* not synced anymore */
        !           443:        file->rf_flags &= ~RCS_SYNCED;
        !           444:        return (0);
        !           445:
        !           446:
        !           447: }
        !           448:
        !           449:
        !           450: /*
        !           451:  * rcs_access_remove()
        !           452:  *
        !           453:  * Remove an entry with login name <login> from the access list of the RCS
        !           454:  * file <file>.
        !           455:  * Returns 0 on success, or -1 on failure.
        !           456:  */
        !           457: int
        !           458: rcs_access_remove(RCSFILE *file, const char *login)
        !           459: {
        !           460:        struct rcs_access *ap;
        !           461:
        !           462:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
        !           463:                if (strcmp(ap->ra_name, login) == 0)
        !           464:                        break;
        !           465:
        !           466:        if (ap == NULL) {
        !           467:                cvs_log(LP_ERR, "%s: no access for `%s'", file->rf_path, login);
        !           468:                return (-1);
        !           469:        }
        !           470:
        !           471:        TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
        !           472:        free(ap->ra_name);
        !           473:        free(ap);
        !           474:
        !           475:        /* not synced anymore */
        !           476:        file->rf_flags &= ~RCS_SYNCED;
        !           477:        return (0);
        !           478: }
        !           479:
        !           480:
        !           481: /*
1.26      jfb       482:  * rcs_sym_add()
1.1       jfb       483:  *
                    484:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    485:  * is named <sym> and is bound to the RCS revision <snum>.
                    486:  * Returns 0 on success, or -1 on failure.
                    487:  */
                    488: int
1.26      jfb       489: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1       jfb       490: {
                    491:        struct rcs_sym *symp;
                    492:
                    493:        /* first look for duplication */
                    494:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    495:                if (strcmp(symp->rs_name, sym) == 0) {
1.17      jfb       496:                        cvs_log(LP_ERR, "attempt to add duplicate symbol `%s'",
                    497:                            sym);
1.1       jfb       498:                        return (-1);
                    499:                }
                    500:        }
                    501:
                    502:        symp = (struct rcs_sym *)malloc(sizeof(*symp));
                    503:        if (symp == NULL) {
                    504:                cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                    505:                return (-1);
                    506:        }
                    507:
                    508:        symp->rs_name = strdup(sym);
1.10      joris     509:        if (symp->rs_name == NULL) {
                    510:                cvs_log(LP_ERRNO, "failed to duplicate symbol");
                    511:                free(symp);
                    512:                return (-1);
                    513:        }
                    514:
1.1       jfb       515:        symp->rs_num = rcsnum_alloc();
1.11      joris     516:        if (symp->rs_num == NULL) {
1.17      jfb       517:                free(symp->rs_name);
1.11      joris     518:                free(symp);
                    519:                return (-1);
                    520:        }
1.1       jfb       521:        rcsnum_cpy(snum, symp->rs_num, 0);
                    522:
                    523:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    524:
                    525:        /* not synced anymore */
1.26      jfb       526:        rfp->rf_flags &= ~RCS_SYNCED;
1.1       jfb       527:
                    528:        return (0);
                    529: }
                    530:
                    531:
                    532: /*
1.27      jfb       533:  * rcs_sym_remove()
                    534:  *
                    535:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    536:  * <file>.  If no such symbol is found, the call fails and returns with an
                    537:  * error.
                    538:  * Returns 0 on success, or -1 on failure.
                    539:  */
                    540: int
                    541: rcs_sym_remove(RCSFILE *file, const char *sym)
                    542: {
                    543:        struct rcs_sym *symp;
                    544:
                    545:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    546:                if (strcmp(symp->rs_name, sym) == 0)
                    547:                        break;
                    548:
                    549:        if (symp == NULL) {
                    550:                cvs_log(LP_ERR, "%s: no such symbol `%s'", file->rf_path, sym);
                    551:                return (-1);
                    552:        }
                    553:
                    554:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
                    555:        free(symp->rs_name);
                    556:        rcsnum_free(symp->rs_num);
                    557:        free(symp);
                    558:
                    559:        /* not synced anymore */
                    560:        file->rf_flags &= ~RCS_SYNCED;
                    561:
                    562:        return (0);
                    563: }
                    564:
                    565:
                    566: /*
                    567:  * rcs_sym_getrev()
                    568:  *
                    569:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    570:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    571:  * should be freed by the caller once they are done with it.
                    572:  * Returns the RCSNUM on success, or NULL on failure.
                    573:  */
                    574: RCSNUM*
                    575: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    576: {
                    577:        RCSNUM *num;
                    578:        struct rcs_sym *symp;
                    579:
                    580:        num = NULL;
                    581:
                    582:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    583:                if (strcmp(symp->rs_name, sym) == 0)
                    584:                        break;
                    585:
                    586:        if (symp == NULL) {
                    587:                /* XXX set error */
                    588:        } else if (((num = rcsnum_alloc()) != NULL) &&
                    589:            (rcsnum_cpy(symp->rs_num, num, 0) < 0)) {
                    590:                rcsnum_free(num);
                    591:                num = NULL;
                    592:        }
                    593:
                    594:        return (num);
                    595: }
                    596:
                    597:
                    598: /*
                    599:  * rcs_desc_get()
                    600:  *
                    601:  * Retrieve the description for the RCS file <file>.
                    602:  */
                    603: const char*
                    604: rcs_desc_get(RCSFILE *file)
                    605: {
                    606:        return (file->rf_desc);
                    607: }
                    608:
                    609:
                    610: /*
                    611:  * rcs_desc_set()
                    612:  *
                    613:  * Set the description for the RCS file <file>.
                    614:  * Returns 0 on success, or -1 on failure.
                    615:  */
                    616: int
                    617: rcs_desc_set(RCSFILE *file, const char *desc)
                    618: {
                    619:        char *tmp;
                    620:
                    621:        if ((tmp = strdup(desc)) == NULL)
                    622:                return (-1);
                    623:
                    624:        if (file->rf_desc != NULL)
                    625:                free(file->rf_desc);
                    626:        file->rf_desc = tmp;
                    627:        file->rf_flags &= ~RCS_SYNCED;
                    628:
                    629:        return (0);
                    630: }
                    631:
                    632:
                    633: /*
1.1       jfb       634:  * rcs_patch()
                    635:  *
                    636:  * Apply an RCS-format patch pointed to by <patch> to the file contents
                    637:  * found in <data>.
                    638:  * Returns 0 on success, or -1 on failure.
                    639:  */
                    640: BUF*
                    641: rcs_patch(const char *data, const char *patch)
                    642: {
1.5       vincent   643:        struct rcs_foo *dlines, *plines;
                    644:        struct rcs_line *lp;
1.1       jfb       645:        size_t len;
1.5       vincent   646:        int lineno;
1.1       jfb       647:        BUF *res;
                    648:
                    649:        len = strlen(data);
                    650:        res = cvs_buf_alloc(len, BUF_AUTOEXT);
                    651:        if (res == NULL)
                    652:                return (NULL);
                    653:
                    654:        dlines = rcs_splitlines(data);
1.17      jfb       655:        if (dlines == NULL) {
                    656:                cvs_buf_free(res);
1.1       jfb       657:                return (NULL);
1.17      jfb       658:        }
1.5       vincent   659:
1.1       jfb       660:        plines = rcs_splitlines(patch);
1.5       vincent   661:        if (plines == NULL) {
1.17      jfb       662:                cvs_buf_free(res);
1.5       vincent   663:                rcs_freefoo(dlines);
1.1       jfb       664:                return (NULL);
1.5       vincent   665:        }
                    666:
                    667:        if (rcs_patch_lines(dlines, plines) < 0) {
1.17      jfb       668:                cvs_buf_free(res);
1.5       vincent   669:                rcs_freefoo(plines);
                    670:                rcs_freefoo(dlines);
                    671:                return (NULL);
                    672:        }
                    673:
                    674:        lineno = 0;
                    675:        TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
                    676:                if (lineno != 0)
                    677:                        cvs_buf_fappend(res, "%s\n", lp->rl_line);
                    678:                lineno++;
                    679:        }
                    680:
                    681:        rcs_freefoo(dlines);
                    682:        rcs_freefoo(plines);
                    683:        return (res);
                    684: }
                    685:
1.7       jfb       686: static int
1.5       vincent   687: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
                    688: {
                    689:        char op, *ep;
                    690:        struct rcs_line *lp, *dlp, *ndlp;
                    691:        int i, lineno, nbln;
1.1       jfb       692:
                    693:        dlp = TAILQ_FIRST(&(dlines->rl_lines));
                    694:        lp = TAILQ_FIRST(&(plines->rl_lines));
                    695:
                    696:        /* skip first bogus line */
                    697:        for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
                    698:            lp = TAILQ_NEXT(lp, rl_list)) {
                    699:                op = *(lp->rl_line);
                    700:                lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
                    701:                if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
                    702:                    (*ep != ' ')) {
                    703:                        cvs_log(LP_ERR,
                    704:                            "invalid line specification in RCS patch");
                    705:                        return (NULL);
                    706:                }
                    707:                ep++;
                    708:                nbln = (int)strtol(ep, &ep, 10);
                    709:                if ((nbln <= 0) || (*ep != '\0')) {
                    710:                        cvs_log(LP_ERR,
                    711:                            "invalid line number specification in RCS patch");
                    712:                        return (NULL);
                    713:                }
                    714:
                    715:                /* find the appropriate line */
                    716:                for (;;) {
                    717:                        if (dlp == NULL)
                    718:                                break;
                    719:                        if (dlp->rl_lineno == lineno)
                    720:                                break;
                    721:                        if (dlp->rl_lineno > lineno) {
                    722:                                dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14      deraadt   723:                        } else if (dlp->rl_lineno < lineno) {
1.1       jfb       724:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    725:                                if (ndlp->rl_lineno > lineno)
                    726:                                        break;
                    727:                                dlp = ndlp;
                    728:                        }
                    729:                }
                    730:                if (dlp == NULL) {
                    731:                        cvs_log(LP_ERR,
                    732:                            "can't find referenced line in RCS patch");
                    733:                        return (NULL);
                    734:                }
                    735:
                    736:                if (op == 'd') {
                    737:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
                    738:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    739:                                TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
                    740:                                dlp = ndlp;
                    741:                        }
1.14      deraadt   742:                } else if (op == 'a') {
1.1       jfb       743:                        for (i = 0; i < nbln; i++) {
                    744:                                ndlp = lp;
                    745:                                lp = TAILQ_NEXT(lp, rl_list);
                    746:                                if (lp == NULL) {
                    747:                                        cvs_log(LP_ERR, "truncated RCS patch");
1.5       vincent   748:                                        return (-1);
1.1       jfb       749:                                }
                    750:                                TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
                    751:                                TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
                    752:                                    lp, rl_list);
                    753:                                dlp = lp;
                    754:
                    755:                                /* we don't want lookup to block on those */
                    756:                                lp->rl_lineno = lineno;
                    757:
                    758:                                lp = ndlp;
                    759:                        }
1.14      deraadt   760:                } else {
1.1       jfb       761:                        cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5       vincent   762:                        return (-1);
1.1       jfb       763:                }
                    764:
                    765:                /* last line of the patch, done */
                    766:                if (lp->rl_lineno == plines->rl_nblines)
                    767:                        break;
                    768:        }
                    769:
                    770:        /* once we're done patching, rebuild the line numbers */
1.2       vincent   771:        lineno = 0;
1.5       vincent   772:        TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1       jfb       773:                lp->rl_lineno = lineno++;
                    774:        dlines->rl_nblines = lineno - 1;
                    775:
1.5       vincent   776:        return (0);
1.1       jfb       777: }
                    778:
                    779:
                    780: /*
                    781:  * rcs_getrev()
                    782:  *
                    783:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent   784:  * returned buffer is dynamically allocated and should be released using
                    785:  * cvs_buf_free() once the caller is done using it.
1.1       jfb       786:  */
                    787: BUF*
                    788: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
                    789: {
                    790:        int res;
                    791:        size_t len;
                    792:        void *bp;
                    793:        RCSNUM *crev;
                    794:        BUF *rbuf;
                    795:        struct rcs_delta *rdp = NULL;
                    796:
1.28      jfb       797:        if (rfp->rf_head == NULL)
                    798:                return (NULL);
                    799:
1.1       jfb       800:        res = rcsnum_cmp(rfp->rf_head, rev, 0);
                    801:        if (res == 1) {
                    802:                cvs_log(LP_ERR, "sorry, can't travel in the future yet");
                    803:                return (NULL);
1.26      jfb       804:        }
                    805:
                    806:        rdp = rcs_findrev(rfp, rfp->rf_head);
                    807:        if (rdp == NULL) {
                    808:                cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                    809:                return (NULL);
                    810:        }
                    811:
                    812:        len = strlen(rdp->rd_text);
                    813:        if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
                    814:                return (NULL);
                    815:
                    816:        cvs_buf_append(rbuf, rdp->rd_text, len);
                    817:
                    818:        if (res != 0) {
                    819:                /* Apply patches backwards to get the right version.
                    820:                 * This will need some rework to support sub branches.
                    821:                 */
                    822:                if ((crev = rcsnum_alloc()) == NULL) {
                    823:                        cvs_buf_free(rbuf);
1.1       jfb       824:                        return (NULL);
                    825:                }
1.26      jfb       826:                rcsnum_cpy(rfp->rf_head, crev, 0);
                    827:                do {
                    828:                        crev->rn_id[crev->rn_len - 1]--;
                    829:                        rdp = rcs_findrev(rfp, crev);
                    830:                        if (rdp == NULL) {
                    831:                                rcsnum_free(crev);
                    832:                                cvs_buf_free(rbuf);
                    833:                                return (NULL);
                    834:                        }
1.1       jfb       835:
1.26      jfb       836:                        if (cvs_buf_putc(rbuf, '\0') < 0) {
                    837:                                rcsnum_free(crev);
1.17      jfb       838:                                cvs_buf_free(rbuf);
1.11      joris     839:                                return (NULL);
1.17      jfb       840:                        }
1.26      jfb       841:                        bp = cvs_buf_release(rbuf);
                    842:                        rbuf = rcs_patch((char *)bp, rdp->rd_text);
                    843:                        if (rbuf == NULL)
                    844:                                break;
                    845:                } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1       jfb       846:
1.26      jfb       847:                rcsnum_free(crev);
1.1       jfb       848:        }
                    849:
                    850:        return (rbuf);
1.16      jfb       851: }
                    852:
                    853:
                    854: /*
                    855:  * rcs_gethead()
                    856:  *
                    857:  * Get the head revision for the RCS file <rf>.
                    858:  */
                    859: BUF*
                    860: rcs_gethead(RCSFILE *rf)
                    861: {
                    862:        return rcs_getrev(rf, rf->rf_head);
1.1       jfb       863: }
                    864:
                    865:
                    866: /*
                    867:  * rcs_getrevbydate()
                    868:  *
                    869:  * Get an RCS revision by a specific date.
                    870:  */
                    871: RCSNUM*
                    872: rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
                    873: {
                    874:        return (NULL);
                    875: }
                    876:
                    877:
                    878: /*
                    879:  * rcs_findrev()
                    880:  *
                    881:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                    882:  * The revision number is given in <rev>.
                    883:  * Returns a pointer to the delta on success, or NULL on failure.
                    884:  */
                    885: static struct rcs_delta*
                    886: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
                    887: {
                    888:        u_int cmplen;
                    889:        struct rcs_delta *rdp;
                    890:        struct rcs_dlist *hp;
1.6       vincent   891:        int found;
1.26      jfb       892:
1.1       jfb       893:        cmplen = 2;
                    894:        hp = &(rfp->rf_delta);
                    895:
1.6       vincent   896:        do {
                    897:                found = 0;
                    898:                TAILQ_FOREACH(rdp, hp, rd_list) {
                    899:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                    900:                                if (cmplen == rev->rn_len)
                    901:                                        return (rdp);
1.1       jfb       902:
1.6       vincent   903:                                hp = &(rdp->rd_snodes);
                    904:                                cmplen += 2;
                    905:                                found = 1;
                    906:                                break;
                    907:                        }
1.1       jfb       908:                }
1.6       vincent   909:        } while (found && cmplen < rev->rn_len);
1.1       jfb       910:
                    911:        return (NULL);
1.20      jfb       912: }
                    913:
                    914:
                    915: /*
1.26      jfb       916:  * rcs_kwexp_set()
                    917:  *
                    918:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                    919:  * Returns 0 on success, or -1 on failure.
                    920:  */
                    921: int
                    922: rcs_kwexp_set(RCSFILE *file, int mode)
                    923: {
                    924:        int i;
                    925:        char *tmp, buf[8] = "";
                    926:
                    927:        if (RCS_KWEXP_INVAL(mode))
                    928:                return (-1);
                    929:
                    930:        i = 0;
                    931:        if (mode == RCS_KWEXP_NONE)
                    932:                buf[0] = 'b';
                    933:        else if (mode == RCS_KWEXP_OLD)
                    934:                buf[0] = 'o';
                    935:        else {
                    936:                if (mode & RCS_KWEXP_NAME)
                    937:                        buf[i++] = 'k';
                    938:                if (mode & RCS_KWEXP_VAL)
                    939:                        buf[i++] = 'v';
                    940:                if (mode & RCS_KWEXP_LKR)
                    941:                        buf[i++] = 'l';
                    942:        }
                    943:
                    944:        if ((tmp = strdup(buf)) == NULL) {
                    945:                cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
                    946:                    file->rf_path);
                    947:                return (-1);
                    948:        }
                    949:
1.27      jfb       950:        if (file->rf_expand != NULL)
                    951:                free(file->rf_expand);
1.26      jfb       952:        file->rf_expand = tmp;
                    953:
                    954:        return (0);
                    955: }
                    956:
                    957: /*
                    958:  * rcs_kwexp_get()
                    959:  *
                    960:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                    961:  */
                    962: int
                    963: rcs_kwexp_get(RCSFILE *file)
                    964: {
                    965:        return rcs_kflag_get(file->rf_expand);
                    966: }
                    967:
                    968: /*
1.20      jfb       969:  * rcs_kflag_get()
                    970:  *
                    971:  * Get the keyword expansion mode from a set of character flags given in
                    972:  * <flags> and return the appropriate flag mask.  In case of an error, the
                    973:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                    974:  */
                    975: int
                    976: rcs_kflag_get(const char *flags)
                    977: {
                    978:        int fl;
                    979:        size_t len;
                    980:        const char *fp;
                    981:
                    982:        fl = 0;
                    983:        len = strlen(flags);
                    984:
                    985:        for (fp = flags; *fp != '\0'; fp++) {
                    986:                if (*fp == 'k')
                    987:                        fl |= RCS_KWEXP_NAME;
                    988:                else if (*fp == 'v')
                    989:                        fl |= RCS_KWEXP_VAL;
                    990:                else if (*fp == 'l')
                    991:                        fl |= RCS_KWEXP_LKR;
                    992:                else if (*fp == 'o') {
                    993:                        if (len != 1)
                    994:                                fl |= RCS_KWEXP_ERR;
                    995:                        fl |= RCS_KWEXP_OLD;
                    996:                } else if (*fp == 'b') {
                    997:                        if (len != 1)
                    998:                                fl |= RCS_KWEXP_ERR;
                    999:                } else  /* unknown letter */
                   1000:                        fl |= RCS_KWEXP_ERR;
                   1001:        }
                   1002:
                   1003:        return (fl);
1.1       jfb      1004: }
                   1005:
1.21      jfb      1006: void
                   1007: rcs_kflag_usage(void)
                   1008: {
                   1009:        fprintf(stderr, "Valid expansion modes include:\n"
1.22      jfb      1010:            "\t-kkv\tGenerate keywords using the default form.\n"
                   1011:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                   1012:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                   1013:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                   1014:            "\t-ko\tGenerate old keyword string "
1.21      jfb      1015:            "(no changes from checked in file).\n"
1.22      jfb      1016:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21      jfb      1017: }
1.1       jfb      1018:
1.26      jfb      1019:
1.1       jfb      1020: /*
                   1021:  * rcs_parse()
                   1022:  *
                   1023:  * Parse the contents of file <path>, which are in the RCS format.
                   1024:  * Returns 0 on success, or -1 on failure.
                   1025:  */
1.26      jfb      1026: static int
1.1       jfb      1027: rcs_parse(RCSFILE *rfp)
                   1028: {
                   1029:        int ret;
                   1030:        struct rcs_pdata *pdp;
                   1031:
1.26      jfb      1032:        if (rfp->rf_flags & RCS_PARSED)
1.1       jfb      1033:                return (0);
                   1034:
1.26      jfb      1035:        if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.1       jfb      1036:                cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
                   1037:                return (-1);
                   1038:        }
                   1039:        memset(pdp, 0, sizeof(*pdp));
                   1040:
1.18      jfb      1041:        pdp->rp_lines = 0;
1.1       jfb      1042:        pdp->rp_pttype = RCS_TOK_ERR;
                   1043:
                   1044:        pdp->rp_file = fopen(rfp->rf_path, "r");
                   1045:        if (pdp->rp_file == NULL) {
                   1046:                cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
                   1047:                rcs_freepdata(pdp);
                   1048:                return (-1);
                   1049:        }
                   1050:
                   1051:        pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
                   1052:        if (pdp->rp_buf == NULL) {
                   1053:                cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
                   1054:                rcs_freepdata(pdp);
                   1055:                return (-1);
                   1056:        }
                   1057:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1058:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1059:
                   1060:        /* ditch the strict lock */
1.26      jfb      1061:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1062:        rfp->rf_pdata = pdp;
                   1063:
                   1064:        if (rcs_parse_admin(rfp) < 0) {
                   1065:                rcs_freepdata(pdp);
                   1066:                return (-1);
                   1067:        }
                   1068:
                   1069:        for (;;) {
                   1070:                ret = rcs_parse_delta(rfp);
                   1071:                if (ret == 0)
                   1072:                        break;
                   1073:                else if (ret == -1) {
                   1074:                        rcs_freepdata(pdp);
                   1075:                        return (-1);
                   1076:                }
                   1077:        }
                   1078:
                   1079:        ret = rcs_gettok(rfp);
                   1080:        if (ret != RCS_TOK_DESC) {
                   1081:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1082:                    RCS_TOKSTR(rfp));
                   1083:                rcs_freepdata(pdp);
                   1084:                return (-1);
                   1085:        }
                   1086:
                   1087:        ret = rcs_gettok(rfp);
                   1088:        if (ret != RCS_TOK_STRING) {
                   1089:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1090:                    RCS_TOKSTR(rfp));
                   1091:                rcs_freepdata(pdp);
                   1092:                return (-1);
                   1093:        }
                   1094:
                   1095:        rfp->rf_desc = strdup(RCS_TOKSTR(rfp));
1.10      joris    1096:        if (rfp->rf_desc == NULL) {
                   1097:                cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1098:                rcs_freepdata(pdp);
                   1099:                return (-1);
                   1100:        }
1.1       jfb      1101:
                   1102:        for (;;) {
                   1103:                ret = rcs_parse_deltatext(rfp);
                   1104:                if (ret == 0)
                   1105:                        break;
                   1106:                else if (ret == -1) {
                   1107:                        rcs_freepdata(pdp);
                   1108:                        return (-1);
                   1109:                }
                   1110:        }
                   1111:
                   1112:        cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
1.18      jfb      1113:            pdp->rp_lines);
1.1       jfb      1114:
                   1115:        rcs_freepdata(pdp);
                   1116:
                   1117:        rfp->rf_pdata = NULL;
1.26      jfb      1118:        rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1       jfb      1119:
                   1120:        return (0);
                   1121: }
                   1122:
                   1123:
                   1124: /*
                   1125:  * rcs_parse_admin()
                   1126:  *
                   1127:  * Parse the administrative portion of an RCS file.
                   1128:  * Returns 0 on success, or -1 on failure.
                   1129:  */
                   1130: static int
                   1131: rcs_parse_admin(RCSFILE *rfp)
                   1132: {
                   1133:        u_int i;
                   1134:        int tok, ntok, hmask;
                   1135:        struct rcs_key *rk;
                   1136:
                   1137:        /* hmask is a mask of the headers already encountered */
                   1138:        hmask = 0;
                   1139:        for (;;) {
                   1140:                tok = rcs_gettok(rfp);
                   1141:                if (tok == RCS_TOK_ERR) {
                   1142:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                   1143:                        return (-1);
1.14      deraadt  1144:                } else if (tok == RCS_TOK_NUM) {
1.1       jfb      1145:                        /* assume this is the start of the first delta */
                   1146:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1147:                        return (0);
                   1148:                }
                   1149:
                   1150:                rk = NULL;
1.18      jfb      1151:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1152:                        if (rcs_keys[i].rk_id == tok)
                   1153:                                rk = &(rcs_keys[i]);
                   1154:
                   1155:                if (hmask & (1 << tok)) {
                   1156:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1157:                        return (-1);
                   1158:                }
                   1159:                hmask |= (1 << tok);
                   1160:
                   1161:                switch (tok) {
                   1162:                case RCS_TOK_HEAD:
                   1163:                case RCS_TOK_BRANCH:
                   1164:                case RCS_TOK_COMMENT:
                   1165:                case RCS_TOK_EXPAND:
                   1166:                        ntok = rcs_gettok(rfp);
                   1167:                        if (ntok == RCS_TOK_SCOLON)
                   1168:                                break;
                   1169:                        if (ntok != rk->rk_val) {
                   1170:                                cvs_log(LP_ERR,
                   1171:                                    "invalid value type for RCS key `%s'",
                   1172:                                    rk->rk_str);
                   1173:                        }
                   1174:
                   1175:                        if (tok == RCS_TOK_HEAD) {
1.28      jfb      1176:                                if (rfp->rf_head == NULL) {
                   1177:                                        rfp->rf_head = rcsnum_alloc();
                   1178:                                        if (rfp->rf_head == NULL)
                   1179:                                                return (-1);
                   1180:                                }
1.1       jfb      1181:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1182:                                    rfp->rf_head);
1.14      deraadt  1183:                        } else if (tok == RCS_TOK_BRANCH) {
1.1       jfb      1184:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1185:                                    rfp->rf_branch);
1.14      deraadt  1186:                        } else if (tok == RCS_TOK_COMMENT) {
1.1       jfb      1187:                                rfp->rf_comment = strdup(RCS_TOKSTR(rfp));
1.10      joris    1188:                                if (rfp->rf_comment == NULL) {
                   1189:                                        cvs_log(LP_ERRNO,
                   1190:                                            "failed to duplicate rcs token");
                   1191:                                        return (-1);
                   1192:                                }
1.14      deraadt  1193:                        } else if (tok == RCS_TOK_EXPAND) {
1.1       jfb      1194:                                rfp->rf_expand = strdup(RCS_TOKSTR(rfp));
1.10      joris    1195:                                if (rfp->rf_expand == NULL) {
                   1196:                                        cvs_log(LP_ERRNO,
                   1197:                                            "failed to duplicate rcs token");
                   1198:                                        return (-1);
                   1199:                                }
1.1       jfb      1200:                        }
                   1201:
                   1202:                        /* now get the expected semi-colon */
                   1203:                        ntok = rcs_gettok(rfp);
                   1204:                        if (ntok != RCS_TOK_SCOLON) {
                   1205:                                cvs_log(LP_ERR,
                   1206:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1207:                                    rk->rk_str);
1.1       jfb      1208:                                return (-1);
                   1209:                        }
                   1210:                        break;
                   1211:                case RCS_TOK_ACCESS:
1.29    ! jfb      1212:                        if (rcs_parse_access(rfp) < 0)
        !          1213:                                return (-1);
1.1       jfb      1214:                        break;
                   1215:                case RCS_TOK_SYMBOLS:
1.29    ! jfb      1216:                        if (rcs_parse_symbols(rfp) < 0)
        !          1217:                                return (-1);
1.1       jfb      1218:                        break;
                   1219:                case RCS_TOK_LOCKS:
1.29    ! jfb      1220:                        if (rcs_parse_locks(rfp) < 0)
        !          1221:                                return (-1);
1.1       jfb      1222:                        break;
                   1223:                default:
                   1224:                        cvs_log(LP_ERR,
                   1225:                            "unexpected token `%s' in RCS admin section",
                   1226:                            RCS_TOKSTR(rfp));
                   1227:                        return (-1);
                   1228:                }
                   1229:        }
                   1230:
                   1231:        return (0);
                   1232: }
                   1233:
                   1234:
                   1235: /*
                   1236:  * rcs_parse_delta()
                   1237:  *
                   1238:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1239:  * information in the <rfp> delta list.
                   1240:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1241:  * -1 on error.
                   1242:  */
                   1243: static int
                   1244: rcs_parse_delta(RCSFILE *rfp)
                   1245: {
                   1246:        int ret, tok, ntok, hmask;
                   1247:        u_int i;
                   1248:        char *tokstr;
1.3       vincent  1249:        RCSNUM *datenum;
1.1       jfb      1250:        struct rcs_delta *rdp;
                   1251:        struct rcs_key *rk;
                   1252:
                   1253:        rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
                   1254:        if (rdp == NULL) {
                   1255:                cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
                   1256:                return (-1);
                   1257:        }
                   1258:        memset(rdp, 0, sizeof(*rdp));
                   1259:
                   1260:        rdp->rd_num = rcsnum_alloc();
1.11      joris    1261:        if (rdp->rd_num == NULL) {
                   1262:                rcs_freedelta(rdp);
                   1263:                return (-1);
                   1264:        }
1.1       jfb      1265:        rdp->rd_next = rcsnum_alloc();
1.11      joris    1266:        if (rdp->rd_next == NULL) {
                   1267:                rcs_freedelta(rdp);
                   1268:                return (-1);
                   1269:        }
1.1       jfb      1270:
                   1271:        TAILQ_INIT(&(rdp->rd_branches));
                   1272:
                   1273:        tok = rcs_gettok(rfp);
                   1274:        if (tok != RCS_TOK_NUM) {
                   1275:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1276:                    RCS_TOKSTR(rfp));
                   1277:                rcs_freedelta(rdp);
                   1278:                return (-1);
                   1279:        }
                   1280:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1281:
                   1282:        hmask = 0;
                   1283:        ret = 0;
                   1284:        tokstr = NULL;
                   1285:
                   1286:        for (;;) {
                   1287:                tok = rcs_gettok(rfp);
                   1288:                if (tok == RCS_TOK_ERR) {
                   1289:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1290:                        rcs_freedelta(rdp);
                   1291:                        return (-1);
1.14      deraadt  1292:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1293:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1294:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1295:                        break;
                   1296:                }
                   1297:
                   1298:                rk = NULL;
1.18      jfb      1299:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1300:                        if (rcs_keys[i].rk_id == tok)
                   1301:                                rk = &(rcs_keys[i]);
                   1302:
                   1303:                if (hmask & (1 << tok)) {
                   1304:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1305:                        rcs_freedelta(rdp);
                   1306:                        return (-1);
                   1307:                }
                   1308:                hmask |= (1 << tok);
                   1309:
                   1310:                switch (tok) {
                   1311:                case RCS_TOK_DATE:
                   1312:                case RCS_TOK_AUTHOR:
                   1313:                case RCS_TOK_STATE:
                   1314:                case RCS_TOK_NEXT:
                   1315:                        ntok = rcs_gettok(rfp);
                   1316:                        if (ntok == RCS_TOK_SCOLON) {
                   1317:                                if (rk->rk_flags & RCS_VOPT)
                   1318:                                        break;
                   1319:                                else {
                   1320:                                        cvs_log(LP_ERR, "missing mandatory "
                   1321:                                            "value to RCS key `%s'",
                   1322:                                            rk->rk_str);
                   1323:                                        rcs_freedelta(rdp);
                   1324:                                        return (-1);
                   1325:                                }
                   1326:                        }
                   1327:
                   1328:                        if (ntok != rk->rk_val) {
                   1329:                                cvs_log(LP_ERR,
                   1330:                                    "invalid value type for RCS key `%s'",
                   1331:                                    rk->rk_str);
                   1332:                                rcs_freedelta(rdp);
                   1333:                                return (-1);
                   1334:                        }
                   1335:
                   1336:                        if (tokstr != NULL)
                   1337:                                free(tokstr);
                   1338:                        tokstr = strdup(RCS_TOKSTR(rfp));
1.10      joris    1339:                        if (tokstr == NULL) {
1.15      tedu     1340:                                cvs_log(LP_ERRNO,
1.10      joris    1341:                                    "failed to duplicate rcs token");
                   1342:                                rcs_freedelta(rdp);
                   1343:                                return (-1);
                   1344:                        }
1.1       jfb      1345:
                   1346:                        /* now get the expected semi-colon */
                   1347:                        ntok = rcs_gettok(rfp);
                   1348:                        if (ntok != RCS_TOK_SCOLON) {
                   1349:                                cvs_log(LP_ERR,
                   1350:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1351:                                    rk->rk_str);
1.1       jfb      1352:                                rcs_freedelta(rdp);
                   1353:                                return (-1);
                   1354:                        }
                   1355:
                   1356:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1357:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.11      joris    1358:                                        rcs_freedelta(rdp);
                   1359:                                        return (-1);
                   1360:                                }
1.3       vincent  1361:                                if (datenum->rn_len != 6) {
1.1       jfb      1362:                                        cvs_log(LP_ERR,
                   1363:                                            "RCS date specification has %s "
                   1364:                                            "fields",
1.3       vincent  1365:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1366:                                            "missing");
                   1367:                                        rcs_freedelta(rdp);
                   1368:                                }
1.3       vincent  1369:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1370:                                if (rdp->rd_date.tm_year >= 1900)
                   1371:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1372:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1373:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1374:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1375:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1376:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1377:                                rcsnum_free(datenum);
1.14      deraadt  1378:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1379:                                rdp->rd_author = tokstr;
                   1380:                                tokstr = NULL;
1.14      deraadt  1381:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1382:                                rdp->rd_state = tokstr;
                   1383:                                tokstr = NULL;
1.14      deraadt  1384:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      1385:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1386:                        }
                   1387:                        break;
                   1388:                case RCS_TOK_BRANCHES:
                   1389:                        rcs_parse_branches(rfp, rdp);
                   1390:                        break;
                   1391:                default:
                   1392:                        cvs_log(LP_ERR,
                   1393:                            "unexpected token `%s' in RCS delta",
                   1394:                            RCS_TOKSTR(rfp));
                   1395:                        rcs_freedelta(rdp);
                   1396:                        return (-1);
                   1397:                }
                   1398:        }
                   1399:
1.13      jfb      1400:        if (tokstr != NULL)
                   1401:                free(tokstr);
                   1402:
1.1       jfb      1403:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      1404:        rfp->rf_ndelta++;
1.1       jfb      1405:
                   1406:        return (ret);
                   1407: }
                   1408:
                   1409:
                   1410: /*
                   1411:  * rcs_parse_deltatext()
                   1412:  *
                   1413:  * Parse an RCS delta text section and fill in the log and text field of the
                   1414:  * appropriate delta section.
                   1415:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1416:  * -1 on error.
                   1417:  */
                   1418: static int
                   1419: rcs_parse_deltatext(RCSFILE *rfp)
                   1420: {
                   1421:        int tok;
                   1422:        RCSNUM *tnum;
                   1423:        struct rcs_delta *rdp;
                   1424:
                   1425:        tok = rcs_gettok(rfp);
                   1426:        if (tok == RCS_TOK_EOF)
                   1427:                return (0);
                   1428:
                   1429:        if (tok != RCS_TOK_NUM) {
                   1430:                cvs_log(LP_ERR,
                   1431:                    "unexpected token `%s' at start of RCS delta text",
                   1432:                    RCS_TOKSTR(rfp));
                   1433:                return (-1);
                   1434:        }
1.13      jfb      1435:
                   1436:        tnum = rcsnum_alloc();
                   1437:        if (tnum == NULL)
                   1438:                return (-1);
1.1       jfb      1439:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   1440:
                   1441:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1442:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   1443:                        break;
                   1444:        }
1.13      jfb      1445:        rcsnum_free(tnum);
                   1446:
1.1       jfb      1447:        if (rdp == NULL) {
                   1448:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   1449:                    RCS_TOKSTR(rfp));
                   1450:                return (-1);
                   1451:        }
                   1452:
                   1453:        tok = rcs_gettok(rfp);
                   1454:        if (tok != RCS_TOK_LOG) {
                   1455:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1456:                    RCS_TOKSTR(rfp));
                   1457:                return (-1);
                   1458:        }
                   1459:
                   1460:        tok = rcs_gettok(rfp);
                   1461:        if (tok != RCS_TOK_STRING) {
                   1462:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1463:                    RCS_TOKSTR(rfp));
                   1464:                return (-1);
                   1465:        }
                   1466:        rdp->rd_log = strdup(RCS_TOKSTR(rfp));
                   1467:        if (rdp->rd_log == NULL) {
                   1468:                cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
                   1469:                return (-1);
                   1470:        }
                   1471:
                   1472:        tok = rcs_gettok(rfp);
                   1473:        if (tok != RCS_TOK_TEXT) {
                   1474:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1475:                    RCS_TOKSTR(rfp));
                   1476:                return (-1);
                   1477:        }
                   1478:
                   1479:        tok = rcs_gettok(rfp);
                   1480:        if (tok != RCS_TOK_STRING) {
                   1481:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1482:                    RCS_TOKSTR(rfp));
                   1483:                return (-1);
                   1484:        }
                   1485:
                   1486:        rdp->rd_text = strdup(RCS_TOKSTR(rfp));
                   1487:        if (rdp->rd_text == NULL) {
                   1488:                cvs_log(LP_ERRNO, "failed to copy RCS delta text");
                   1489:                return (-1);
                   1490:        }
                   1491:
                   1492:        return (1);
                   1493: }
                   1494:
                   1495:
                   1496: /*
                   1497:  * rcs_parse_access()
                   1498:  *
                   1499:  * Parse the access list given as value to the `access' keyword.
                   1500:  * Returns 0 on success, or -1 on failure.
                   1501:  */
                   1502: static int
                   1503: rcs_parse_access(RCSFILE *rfp)
                   1504: {
                   1505:        int type;
                   1506:
                   1507:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   1508:                if (type != RCS_TOK_ID) {
                   1509:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   1510:                            RCS_TOKSTR(rfp));
                   1511:                        return (-1);
                   1512:                }
1.29    ! jfb      1513:
        !          1514:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
        !          1515:                        return (-1);
1.1       jfb      1516:        }
                   1517:
                   1518:        return (0);
                   1519: }
                   1520:
                   1521:
                   1522: /*
                   1523:  * rcs_parse_symbols()
                   1524:  *
                   1525:  * Parse the symbol list given as value to the `symbols' keyword.
                   1526:  * Returns 0 on success, or -1 on failure.
                   1527:  */
                   1528: static int
                   1529: rcs_parse_symbols(RCSFILE *rfp)
                   1530: {
                   1531:        int type;
                   1532:        struct rcs_sym *symp;
                   1533:
                   1534:        for (;;) {
                   1535:                type = rcs_gettok(rfp);
                   1536:                if (type == RCS_TOK_SCOLON)
                   1537:                        break;
                   1538:
                   1539:                if (type != RCS_TOK_STRING) {
                   1540:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1541:                            RCS_TOKSTR(rfp));
                   1542:                        return (-1);
                   1543:                }
                   1544:
                   1545:                symp = (struct rcs_sym *)malloc(sizeof(*symp));
                   1546:                if (symp == NULL) {
                   1547:                        cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                   1548:                        return (-1);
                   1549:                }
                   1550:                symp->rs_name = strdup(RCS_TOKSTR(rfp));
1.10      joris    1551:                if (symp->rs_name == NULL) {
                   1552:                        cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1553:                        free(symp);
                   1554:                        return (-1);
                   1555:                }
                   1556:
1.1       jfb      1557:                symp->rs_num = rcsnum_alloc();
1.11      joris    1558:                if (symp->rs_num == NULL) {
                   1559:                        cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
                   1560:                        free(symp);
                   1561:                        return (-1);
                   1562:                }
1.1       jfb      1563:
                   1564:                type = rcs_gettok(rfp);
                   1565:                if (type != RCS_TOK_COLON) {
                   1566:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1567:                            RCS_TOKSTR(rfp));
1.11      joris    1568:                        rcsnum_free(symp->rs_num);
1.1       jfb      1569:                        free(symp->rs_name);
                   1570:                        free(symp);
                   1571:                        return (-1);
                   1572:                }
                   1573:
                   1574:                type = rcs_gettok(rfp);
                   1575:                if (type != RCS_TOK_NUM) {
                   1576:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1577:                            RCS_TOKSTR(rfp));
1.11      joris    1578:                        rcsnum_free(symp->rs_num);
1.1       jfb      1579:                        free(symp->rs_name);
                   1580:                        free(symp);
                   1581:                        return (-1);
                   1582:                }
                   1583:
                   1584:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   1585:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1586:                            RCS_TOKSTR(rfp));
1.11      joris    1587:                        rcsnum_free(symp->rs_num);
1.1       jfb      1588:                        free(symp->rs_name);
                   1589:                        free(symp);
                   1590:                        return (-1);
                   1591:                }
                   1592:
                   1593:                TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                   1594:        }
                   1595:
                   1596:        return (0);
                   1597: }
                   1598:
                   1599:
                   1600: /*
                   1601:  * rcs_parse_locks()
                   1602:  *
                   1603:  * Parse the lock list given as value to the `locks' keyword.
                   1604:  * Returns 0 on success, or -1 on failure.
                   1605:  */
                   1606: static int
                   1607: rcs_parse_locks(RCSFILE *rfp)
                   1608: {
                   1609:        int type;
                   1610:        struct rcs_lock *lkp;
                   1611:
                   1612:        for (;;) {
                   1613:                type = rcs_gettok(rfp);
                   1614:                if (type == RCS_TOK_SCOLON)
                   1615:                        break;
                   1616:
                   1617:                if (type != RCS_TOK_ID) {
                   1618:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   1619:                            RCS_TOKSTR(rfp));
                   1620:                        return (-1);
                   1621:                }
                   1622:
                   1623:                lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
                   1624:                if (lkp == NULL) {
                   1625:                        cvs_log(LP_ERRNO, "failed to allocate RCS lock");
                   1626:                        return (-1);
                   1627:                }
                   1628:                lkp->rl_num = rcsnum_alloc();
1.11      joris    1629:                if (lkp->rl_num == NULL) {
                   1630:                        free(lkp);
                   1631:                        return (-1);
                   1632:                }
1.1       jfb      1633:
                   1634:                type = rcs_gettok(rfp);
                   1635:                if (type != RCS_TOK_COLON) {
                   1636:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1637:                            RCS_TOKSTR(rfp));
                   1638:                        free(lkp);
                   1639:                        return (-1);
                   1640:                }
                   1641:
                   1642:                type = rcs_gettok(rfp);
                   1643:                if (type != RCS_TOK_NUM) {
                   1644:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1645:                            RCS_TOKSTR(rfp));
                   1646:                        free(lkp);
                   1647:                        return (-1);
                   1648:                }
                   1649:
                   1650:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   1651:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1652:                            RCS_TOKSTR(rfp));
                   1653:                        free(lkp);
                   1654:                        return (-1);
                   1655:                }
                   1656:
                   1657:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   1658:        }
                   1659:
                   1660:        /* check if we have a `strict' */
                   1661:        type = rcs_gettok(rfp);
                   1662:        if (type != RCS_TOK_STRICT) {
                   1663:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  1664:        } else {
1.26      jfb      1665:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      1666:
                   1667:                type = rcs_gettok(rfp);
                   1668:                if (type != RCS_TOK_SCOLON) {
                   1669:                        cvs_log(LP_ERR,
                   1670:                            "missing semi-colon after `strict' keyword");
                   1671:                        return (-1);
                   1672:                }
                   1673:        }
                   1674:
                   1675:        return (0);
                   1676: }
                   1677:
                   1678: /*
                   1679:  * rcs_parse_branches()
                   1680:  *
                   1681:  * Parse the list of branches following a `branches' keyword in a delta.
                   1682:  * Returns 0 on success, or -1 on failure.
                   1683:  */
                   1684: static int
                   1685: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   1686: {
                   1687:        int type;
                   1688:        struct rcs_branch *brp;
                   1689:
                   1690:        for (;;) {
                   1691:                type = rcs_gettok(rfp);
                   1692:                if (type == RCS_TOK_SCOLON)
                   1693:                        break;
                   1694:
                   1695:                if (type != RCS_TOK_NUM) {
                   1696:                        cvs_log(LP_ERR,
                   1697:                            "unexpected token `%s' in list of branches",
                   1698:                            RCS_TOKSTR(rfp));
                   1699:                        return (-1);
                   1700:                }
                   1701:
                   1702:                brp = (struct rcs_branch *)malloc(sizeof(*brp));
                   1703:                if (brp == NULL) {
                   1704:                        cvs_log(LP_ERRNO, "failed to allocate RCS branch");
                   1705:                        return (-1);
                   1706:                }
                   1707:                brp->rb_num = rcsnum_alloc();
1.11      joris    1708:                if (brp->rb_num == NULL) {
                   1709:                        free(brp);
                   1710:                        return (-1);
                   1711:                }
                   1712:
1.1       jfb      1713:                rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
                   1714:
                   1715:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   1716:        }
                   1717:
                   1718:        return (0);
                   1719: }
                   1720:
                   1721:
                   1722: /*
                   1723:  * rcs_freedelta()
                   1724:  *
                   1725:  * Free the contents of a delta structure.
                   1726:  */
1.18      jfb      1727: static void
1.1       jfb      1728: rcs_freedelta(struct rcs_delta *rdp)
                   1729: {
1.12      jfb      1730:        struct rcs_branch *rb;
1.1       jfb      1731:        struct rcs_delta *crdp;
                   1732:
1.12      jfb      1733:        if (rdp->rd_num != NULL)
                   1734:                rcsnum_free(rdp->rd_num);
                   1735:        if (rdp->rd_next != NULL)
                   1736:                rcsnum_free(rdp->rd_next);
                   1737:
1.1       jfb      1738:        if (rdp->rd_author != NULL)
                   1739:                free(rdp->rd_author);
                   1740:        if (rdp->rd_state != NULL)
                   1741:                free(rdp->rd_state);
                   1742:        if (rdp->rd_log != NULL)
                   1743:                free(rdp->rd_log);
                   1744:        if (rdp->rd_text != NULL)
                   1745:                free(rdp->rd_text);
1.12      jfb      1746:
                   1747:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   1748:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   1749:                rcsnum_free(rb->rb_num);
                   1750:                free(rb);
                   1751:        }
1.1       jfb      1752:
                   1753:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   1754:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   1755:                rcs_freedelta(crdp);
                   1756:        }
                   1757:
                   1758:        free(rdp);
                   1759: }
                   1760:
                   1761:
                   1762: /*
                   1763:  * rcs_freepdata()
                   1764:  *
                   1765:  * Free the contents of the parser data structure.
                   1766:  */
                   1767: static void
                   1768: rcs_freepdata(struct rcs_pdata *pd)
                   1769: {
                   1770:        if (pd->rp_file != NULL)
                   1771:                (void)fclose(pd->rp_file);
                   1772:        if (pd->rp_buf != NULL)
                   1773:                free(pd->rp_buf);
                   1774:        free(pd);
                   1775: }
                   1776:
                   1777:
                   1778: /*
                   1779:  * rcs_gettok()
                   1780:  *
                   1781:  * Get the next RCS token from the string <str>.
                   1782:  */
                   1783: static int
                   1784: rcs_gettok(RCSFILE *rfp)
                   1785: {
                   1786:        u_int i;
                   1787:        int ch, last, type;
1.18      jfb      1788:        size_t len;
                   1789:        char *bp;
1.1       jfb      1790:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1791:
                   1792:        type = RCS_TOK_ERR;
                   1793:        bp = pdp->rp_buf;
                   1794:        *bp = '\0';
                   1795:
                   1796:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   1797:                type = pdp->rp_pttype;
                   1798:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   1799:                pdp->rp_pttype = RCS_TOK_ERR;
                   1800:                return (type);
                   1801:        }
                   1802:
                   1803:        /* skip leading whitespace */
                   1804:        /* XXX we must skip backspace too for compatibility, should we? */
                   1805:        do {
                   1806:                ch = getc(pdp->rp_file);
                   1807:                if (ch == '\n')
1.18      jfb      1808:                        pdp->rp_lines++;
1.1       jfb      1809:        } while (isspace(ch));
                   1810:
                   1811:        if (ch == EOF) {
                   1812:                type = RCS_TOK_EOF;
1.14      deraadt  1813:        } else if (ch == ';') {
1.1       jfb      1814:                type = RCS_TOK_SCOLON;
1.14      deraadt  1815:        } else if (ch == ':') {
1.1       jfb      1816:                type = RCS_TOK_COLON;
1.14      deraadt  1817:        } else if (isalpha(ch)) {
1.18      jfb      1818:                type = RCS_TOK_STRING;
1.1       jfb      1819:                *(bp++) = ch;
1.18      jfb      1820:                for (;;) {
1.1       jfb      1821:                        ch = getc(pdp->rp_file);
1.11      joris    1822:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      1823:                                ungetc(ch, pdp->rp_file);
                   1824:                                break;
                   1825:                        }
                   1826:                        *(bp++) = ch;
1.18      jfb      1827:                        if (bp == pdp->rp_bufend - 1) {
                   1828:                                len = bp - pdp->rp_buf;
                   1829:                                if (rcs_growbuf(rfp) < 0) {
                   1830:                                        type = RCS_TOK_ERR;
                   1831:                                        break;
                   1832:                                }
                   1833:                                bp = pdp->rp_buf + len;
                   1834:                        }
1.1       jfb      1835:                }
                   1836:                *bp = '\0';
                   1837:
1.18      jfb      1838:                if (type != RCS_TOK_ERR) {
                   1839:                        for (i = 0; i < RCS_NKEYS; i++) {
                   1840:                                if (strcmp(rcs_keys[i].rk_str,
                   1841:                                    pdp->rp_buf) == 0) {
                   1842:                                        type = rcs_keys[i].rk_id;
                   1843:                                        break;
                   1844:                                }
1.1       jfb      1845:                        }
                   1846:                }
1.14      deraadt  1847:        } else if (ch == '@') {
1.1       jfb      1848:                /* we have a string */
1.18      jfb      1849:                type = RCS_TOK_STRING;
1.1       jfb      1850:                for (;;) {
                   1851:                        ch = getc(pdp->rp_file);
                   1852:                        if (ch == '@') {
                   1853:                                ch = getc(pdp->rp_file);
                   1854:                                if (ch != '@') {
                   1855:                                        ungetc(ch, pdp->rp_file);
                   1856:                                        break;
                   1857:                                }
1.14      deraadt  1858:                        } else if (ch == '\n')
1.18      jfb      1859:                                pdp->rp_lines++;
1.1       jfb      1860:
                   1861:                        *(bp++) = ch;
1.18      jfb      1862:                        if (bp == pdp->rp_bufend - 1) {
                   1863:                                len = bp - pdp->rp_buf;
                   1864:                                if (rcs_growbuf(rfp) < 0) {
                   1865:                                        type = RCS_TOK_ERR;
                   1866:                                        break;
                   1867:                                }
                   1868:                                bp = pdp->rp_buf + len;
                   1869:                        }
1.1       jfb      1870:                }
                   1871:
                   1872:                *bp = '\0';
1.14      deraadt  1873:        } else if (isdigit(ch)) {
1.1       jfb      1874:                *(bp++) = ch;
                   1875:                last = ch;
                   1876:                type = RCS_TOK_NUM;
                   1877:
                   1878:                for (;;) {
                   1879:                        ch = getc(pdp->rp_file);
1.18      jfb      1880:                        if (bp == pdp->rp_bufend)
1.1       jfb      1881:                                break;
                   1882:                        if (!isdigit(ch) && ch != '.') {
                   1883:                                ungetc(ch, pdp->rp_file);
                   1884:                                break;
                   1885:                        }
                   1886:
                   1887:                        if (last == '.' && ch == '.') {
                   1888:                                type = RCS_TOK_ERR;
                   1889:                                break;
                   1890:                        }
                   1891:                        last = ch;
                   1892:                        *(bp++) = ch;
                   1893:                }
1.18      jfb      1894:                *bp = '\0';
1.1       jfb      1895:        }
                   1896:
                   1897:        return (type);
                   1898: }
                   1899:
                   1900:
                   1901: /*
                   1902:  * rcs_pushtok()
                   1903:  *
                   1904:  * Push a token back in the parser's token buffer.
                   1905:  */
                   1906: static int
                   1907: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   1908: {
                   1909:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1910:
                   1911:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   1912:                return (-1);
                   1913:
                   1914:        pdp->rp_pttype = type;
                   1915:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   1916:        return (0);
                   1917: }
                   1918:
                   1919:
                   1920: /*
                   1921:  * rcs_stresc()
                   1922:  *
                   1923:  * Performs either escaping or unescaping of the string stored in <str>.
                   1924:  * The operation is to escape special RCS characters if the <esc> argument
                   1925:  * is 1, or unescape otherwise.  The result is stored in the <buf> destination
                   1926:  * buffer, and <blen> must originally point to the size of <buf>.
                   1927:  * Returns the number of bytes which have been read from the source <str> and
                   1928:  * operated on.  The <blen> parameter will contain the number of bytes
                   1929:  * actually copied in <buf>.
                   1930:  */
                   1931: size_t
                   1932: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
                   1933: {
                   1934:        size_t rlen;
                   1935:        const char *sp;
                   1936:        char *bp, *bep;
                   1937:
                   1938:        rlen = 0;
                   1939:        bp = buf;
                   1940:        bep = buf + *blen - 1;
                   1941:
                   1942:        for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
                   1943:                if (*sp == '@') {
                   1944:                        if (esc) {
                   1945:                                if (bp > (bep - 2))
                   1946:                                        break;
                   1947:                                *(bp++) = '@';
1.14      deraadt  1948:                        } else {
1.1       jfb      1949:                                sp++;
                   1950:                                if (*sp != '@') {
                   1951:                                        cvs_log(LP_WARN,
                   1952:                                            "unknown escape character `%c' in "
                   1953:                                            "RCS file", *sp);
                   1954:                                        if (*sp == '\0')
                   1955:                                                break;
                   1956:                                }
                   1957:                        }
                   1958:                }
                   1959:
                   1960:                *(bp++) = *sp;
                   1961:        }
                   1962:
                   1963:        *bp = '\0';
                   1964:        *blen = (bp - buf);
                   1965:        return (sp - str);
                   1966: }
                   1967:
                   1968:
                   1969: /*
                   1970:  * rcs_splitlines()
                   1971:  *
                   1972:  * Split the contents of a file into a list of lines.
                   1973:  */
                   1974: static struct rcs_foo*
                   1975: rcs_splitlines(const char *fcont)
                   1976: {
                   1977:        char *dcp;
                   1978:        struct rcs_foo *foo;
                   1979:        struct rcs_line *lp;
                   1980:
                   1981:        foo = (struct rcs_foo *)malloc(sizeof(*foo));
                   1982:        if (foo == NULL) {
                   1983:                cvs_log(LP_ERR, "failed to allocate line structure");
                   1984:                return (NULL);
                   1985:        }
                   1986:        TAILQ_INIT(&(foo->rl_lines));
                   1987:        foo->rl_nblines = 0;
                   1988:        foo->rl_data = strdup(fcont);
                   1989:        if (foo->rl_data == NULL) {
                   1990:                cvs_log(LP_ERRNO, "failed to copy file contents");
                   1991:                free(foo);
                   1992:                return (NULL);
                   1993:        }
                   1994:
                   1995:        /*
                   1996:         * Add a first bogus line with line number 0.  This is used so we
                   1997:         * can position the line pointer before 1 when changing the first line
                   1998:         * in rcs_patch().
                   1999:         */
                   2000:        lp = (struct rcs_line *)malloc(sizeof(*lp));
1.5       vincent  2001:        if (lp == NULL)
1.1       jfb      2002:                return (NULL);
1.5       vincent  2003:
1.1       jfb      2004:        lp->rl_line = NULL;
                   2005:        lp->rl_lineno = 0;
                   2006:        TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   2007:
                   2008:
                   2009:        for (dcp = foo->rl_data; *dcp != '\0';) {
                   2010:                lp = (struct rcs_line *)malloc(sizeof(*lp));
                   2011:                if (lp == NULL) {
                   2012:                        cvs_log(LP_ERR, "failed to allocate line entry");
                   2013:                        return (NULL);
                   2014:                }
                   2015:
                   2016:                lp->rl_line = dcp;
                   2017:                lp->rl_lineno = ++(foo->rl_nblines);
                   2018:                TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   2019:
                   2020:                dcp = strchr(dcp, '\n');
                   2021:                if (dcp == NULL) {
                   2022:                        break;
                   2023:                }
                   2024:                *(dcp++) = '\0';
                   2025:        }
                   2026:
                   2027:        return (foo);
1.5       vincent  2028: }
                   2029:
                   2030: static void
                   2031: rcs_freefoo(struct rcs_foo *fp)
                   2032: {
                   2033:        struct rcs_line *lp;
                   2034:
                   2035:        while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
                   2036:                TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
                   2037:                free(lp);
                   2038:        }
                   2039:        free(fp->rl_data);
                   2040:        free(fp);
1.18      jfb      2041: }
                   2042:
                   2043: /*
                   2044:  * rcs_growbuf()
                   2045:  *
                   2046:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2047:  * RCS_BUFEXTSIZE.
                   2048:  * In case of failure, the original buffer is left unmodified.
                   2049:  * Returns 0 on success, or -1 on failure.
                   2050:  */
                   2051: static int
                   2052: rcs_growbuf(RCSFILE *rf)
                   2053: {
                   2054:        void *tmp;
                   2055:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2056:
                   2057:        tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
                   2058:        if (tmp == NULL) {
                   2059:                cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
                   2060:                return (-1);
                   2061:        }
                   2062:
                   2063:        pdp->rp_buf = (char *)tmp;
                   2064:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2065:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
                   2066:
                   2067:        return (0);
1.1       jfb      2068: }