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

Annotation of src/usr.bin/rcs/rcs.c, Revision 1.5

1.5     ! ray         1: /*     $OpenBSD: rcs.c,v 1.4 2006/04/27 07:59:33 xsa Exp $     */
1.1       joris       2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include "includes.h"
                     28:
                     29: #include "diff.h"
                     30: #include "rcs.h"
                     31: #include "rcsprog.h"
1.4       xsa        32: #include "rcsutil.h"
1.1       joris      33: #include "xmalloc.h"
                     34:
                     35: #define RCS_BUFSIZE    16384
                     36: #define RCS_BUFEXTSIZE 8192
                     37: #define RCS_KWEXP_SIZE  1024
                     38:
                     39: /* RCS token types */
                     40: #define RCS_TOK_ERR    -1
                     41: #define RCS_TOK_EOF    0
                     42: #define RCS_TOK_NUM    1
                     43: #define RCS_TOK_ID     2
                     44: #define RCS_TOK_STRING 3
                     45: #define RCS_TOK_SCOLON 4
                     46: #define RCS_TOK_COLON  5
                     47:
                     48: #define RCS_TOK_HEAD           8
                     49: #define RCS_TOK_BRANCH         9
                     50: #define RCS_TOK_ACCESS         10
                     51: #define RCS_TOK_SYMBOLS                11
                     52: #define RCS_TOK_LOCKS          12
                     53: #define RCS_TOK_COMMENT                13
                     54: #define RCS_TOK_EXPAND         14
                     55: #define RCS_TOK_DATE           15
                     56: #define RCS_TOK_AUTHOR         16
                     57: #define RCS_TOK_STATE          17
                     58: #define RCS_TOK_NEXT           18
                     59: #define RCS_TOK_BRANCHES       19
                     60: #define RCS_TOK_DESC           20
                     61: #define RCS_TOK_LOG            21
                     62: #define RCS_TOK_TEXT           22
                     63: #define RCS_TOK_STRICT         23
                     64:
                     65: #define RCS_ISKEY(t)   (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
                     66:
                     67: #define RCS_NOSCOL     0x01    /* no terminating semi-colon */
                     68: #define RCS_VOPT       0x02    /* value is optional */
                     69:
                     70: /* opaque parse data */
                     71: struct rcs_pdata {
                     72:        u_int   rp_lines;
                     73:
                     74:        char    *rp_buf;
                     75:        size_t   rp_blen;
                     76:        char    *rp_bufend;
                     77:        size_t   rp_tlen;
                     78:
                     79:        /* pushback token buffer */
                     80:        char    rp_ptok[128];
                     81:        int     rp_pttype;      /* token type, RCS_TOK_ERR if no token */
                     82:
                     83:        FILE    *rp_file;
                     84: };
                     85:
                     86: #define RCS_TOKSTR(rfp)        ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
                     87: #define RCS_TOKLEN(rfp)        ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
                     88:
                     89: /* invalid characters in RCS symbol names */
                     90: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
                     91:
                     92: /* comment leaders, depending on the file's suffix */
                     93: static const struct rcs_comment {
                     94:        const char      *rc_suffix;
                     95:        const char      *rc_cstr;
                     96: } rcs_comments[] = {
                     97:        { "1",    ".\\\" " },
                     98:        { "2",    ".\\\" " },
                     99:        { "3",    ".\\\" " },
                    100:        { "4",    ".\\\" " },
                    101:        { "5",    ".\\\" " },
                    102:        { "6",    ".\\\" " },
                    103:        { "7",    ".\\\" " },
                    104:        { "8",    ".\\\" " },
                    105:        { "9",    ".\\\" " },
                    106:        { "a",    "-- "    },   /* Ada           */
                    107:        { "ada",  "-- "    },
                    108:        { "adb",  "-- "    },
                    109:        { "asm",  ";; "    },   /* assembler (MS-DOS) */
                    110:        { "ads",  "-- "    },   /* Ada */
                    111:        { "bat",  ":: "    },   /* batch (MS-DOS) */
                    112:        { "body", "-- "    },   /* Ada */
                    113:        { "c",    " * "    },   /* C */
                    114:        { "c++",  "// "    },   /* C++ */
                    115:        { "cc",   "// "    },
                    116:        { "cpp",  "// "    },
                    117:        { "cxx",  "// "    },
                    118:        { "m",    "// "    },   /* Objective-C */
                    119:        { "cl",   ";;; "   },   /* Common Lisp   */
                    120:        { "cmd",  ":: "    },   /* command (OS/2) */
                    121:        { "cmf",  "c "     },   /* CM Fortran    */
                    122:        { "csh",  "# "     },   /* shell         */
                    123:        { "e",    "# "     },   /* efl           */
                    124:        { "epsf", "% "     },   /* encapsulated postscript */
                    125:        { "epsi", "% "     },   /* encapsulated postscript */
                    126:        { "el",   "; "     },   /* Emacs Lisp    */
                    127:        { "f",    "c "     },   /* Fortran       */
                    128:        { "for",  "c "     },
                    129:        { "h",    " * "    },   /* C-header      */
                    130:        { "hh",   "// "    },   /* C++ header    */
                    131:        { "hpp",  "// "    },
                    132:        { "hxx",  "// "    },
                    133:        { "in",   "# "     },   /* for Makefile.in */
                    134:        { "l",    " * "    },   /* lex */
                    135:        { "mac",  ";; "    },   /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
                    136:        { "mak",  "# "     },   /* makefile, e.g. Visual C++ */
                    137:        { "me",   ".\\\" " },   /* me-macros    t/nroff  */
                    138:        { "ml",   "; "     },   /* mocklisp      */
                    139:        { "mm",   ".\\\" " },   /* mm-macros    t/nroff  */
                    140:        { "ms",   ".\\\" " },   /* ms-macros    t/nroff  */
                    141:        { "man",  ".\\\" " },   /* man-macros   t/nroff  */
                    142:        { "p",    " * "    },   /* pascal        */
                    143:        { "pas",  " * "    },
                    144:        { "pl",   "# "     },   /* Perl (conflict with Prolog) */
                    145:        { "pm",   "# "     },   /* Perl module */
                    146:        { "ps",   "% "     },   /* postscript */
                    147:        { "psw",  "% "     },   /* postscript wrap */
                    148:        { "pswm", "% "     },   /* postscript wrap */
                    149:        { "r",    "# "     },   /* ratfor        */
                    150:        { "rc",   " * "    },   /* Microsoft Windows resource file */
                    151:        { "red",  "% "     },   /* psl/rlisp     */
                    152:        { "sh",   "# "     },   /* shell         */
                    153:        { "sl",   "% "     },   /* psl           */
                    154:        { "spec", "-- "    },   /* Ada           */
                    155:        { "tex",  "% "     },   /* tex           */
                    156:        { "y",    " * "    },   /* yacc          */
                    157:        { "ye",   " * "    },   /* yacc-efl      */
                    158:        { "yr",   " * "    },   /* yacc-ratfor   */
                    159: };
                    160:
                    161: struct rcs_kw rcs_expkw[] =  {
                    162:        { "Author",     RCS_KW_AUTHOR   },
                    163:        { "Date",       RCS_KW_DATE     },
                    164:        { "Header",     RCS_KW_HEADER   },
                    165:        { "Id",         RCS_KW_ID       },
                    166:        { "Log",        RCS_KW_LOG      },
                    167:        { "Name",       RCS_KW_NAME     },
                    168:        { "RCSfile",    RCS_KW_RCSFILE  },
                    169:        { "Revision",   RCS_KW_REVISION },
                    170:        { "Source",     RCS_KW_SOURCE   },
                    171:        { "State",      RCS_KW_STATE    },
                    172: };
                    173:
                    174: #define NB_COMTYPES    (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
                    175:
                    176: static struct rcs_key {
                    177:        char    rk_str[16];
                    178:        int     rk_id;
                    179:        int     rk_val;
                    180:        int     rk_flags;
                    181: } rcs_keys[] = {
                    182:        { "access",   RCS_TOK_ACCESS,   RCS_TOK_ID,     RCS_VOPT     },
                    183:        { "author",   RCS_TOK_AUTHOR,   RCS_TOK_ID,     0            },
                    184:        { "branch",   RCS_TOK_BRANCH,   RCS_TOK_NUM,    RCS_VOPT     },
                    185:        { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM,    RCS_VOPT     },
                    186:        { "comment",  RCS_TOK_COMMENT,  RCS_TOK_STRING, RCS_VOPT     },
                    187:        { "date",     RCS_TOK_DATE,     RCS_TOK_NUM,    0            },
                    188:        { "desc",     RCS_TOK_DESC,     RCS_TOK_STRING, RCS_NOSCOL   },
                    189:        { "expand",   RCS_TOK_EXPAND,   RCS_TOK_STRING, RCS_VOPT     },
                    190:        { "head",     RCS_TOK_HEAD,     RCS_TOK_NUM,    RCS_VOPT     },
                    191:        { "locks",    RCS_TOK_LOCKS,    RCS_TOK_ID,     0            },
                    192:        { "log",      RCS_TOK_LOG,      RCS_TOK_STRING, RCS_NOSCOL   },
                    193:        { "next",     RCS_TOK_NEXT,     RCS_TOK_NUM,    RCS_VOPT     },
                    194:        { "state",    RCS_TOK_STATE,    RCS_TOK_ID,     RCS_VOPT     },
                    195:        { "strict",   RCS_TOK_STRICT,   0,              0,           },
                    196:        { "symbols",  RCS_TOK_SYMBOLS,  0,              0            },
                    197:        { "text",     RCS_TOK_TEXT,     RCS_TOK_STRING, RCS_NOSCOL   },
                    198: };
                    199:
                    200: #define RCS_NKEYS      (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
                    201:
                    202: static const char *rcs_errstrs[] = {
                    203:        "No error",
                    204:        "No such entry",
                    205:        "Duplicate entry found",
                    206:        "Bad RCS number",
                    207:        "Invalid RCS symbol",
                    208:        "Parse error",
                    209: };
                    210:
                    211: #define RCS_NERR   (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
                    212:
                    213: int rcs_errno = RCS_ERR_NOERR;
                    214: char *timezone_flag = NULL;
                    215:
                    216: int            rcs_patch_lines(struct rcs_lines *, struct rcs_lines *);
                    217: static void    rcs_parse_init(RCSFILE *);
                    218: static int     rcs_parse_admin(RCSFILE *);
                    219: static int     rcs_parse_delta(RCSFILE *);
                    220: static void    rcs_parse_deltas(RCSFILE *, RCSNUM *);
                    221: static int     rcs_parse_deltatext(RCSFILE *);
                    222: static void    rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
                    223: static void    rcs_parse_desc(RCSFILE *, RCSNUM *);
                    224:
                    225: static int     rcs_parse_access(RCSFILE *);
                    226: static int     rcs_parse_symbols(RCSFILE *);
                    227: static int     rcs_parse_locks(RCSFILE *);
                    228: static int     rcs_parse_branches(RCSFILE *, struct rcs_delta *);
                    229: static void    rcs_freedelta(struct rcs_delta *);
                    230: static void    rcs_freepdata(struct rcs_pdata *);
                    231: static int     rcs_gettok(RCSFILE *);
                    232: static int     rcs_pushtok(RCSFILE *, const char *, int);
                    233: static void    rcs_growbuf(RCSFILE *);
                    234: static void    rcs_strprint(const u_char *, size_t, FILE *);
                    235:
                    236: static char*   rcs_expand_keywords(char *, struct rcs_delta *, char *,
                    237:                     size_t, int);
                    238:
                    239: RCSFILE *
1.3       joris     240: rcs_open(const char *path, int fd, int flags, ...)
1.1       joris     241: {
1.3       joris     242:        int mode;
1.1       joris     243:        mode_t fmode;
                    244:        RCSFILE *rfp;
                    245:        va_list vap;
                    246:        struct rcs_delta *rdp;
                    247:        struct rcs_lock *lkr;
                    248:
                    249:        fmode = S_IRUSR|S_IRGRP|S_IROTH;
                    250:        flags &= 0xffff;        /* ditch any internal flags */
                    251:
1.3       joris     252:        if (flags & RCS_CREATE) {
                    253:                va_start(vap, flags);
                    254:                mode = va_arg(vap, int);
                    255:                va_end(vap);
                    256:                fmode = (mode_t)mode;
1.1       joris     257:        }
                    258:
                    259:        rfp = xcalloc(1, sizeof(*rfp));
                    260:
                    261:        rfp->rf_path = xstrdup(path);
                    262:        rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
                    263:        rfp->rf_mode = fmode;
1.3       joris     264:        rfp->fd = fd;
1.1       joris     265:
                    266:        TAILQ_INIT(&(rfp->rf_delta));
                    267:        TAILQ_INIT(&(rfp->rf_access));
                    268:        TAILQ_INIT(&(rfp->rf_symbols));
                    269:        TAILQ_INIT(&(rfp->rf_locks));
                    270:
                    271:        if (!(rfp->rf_flags & RCS_CREATE))
                    272:                rcs_parse_init(rfp);
                    273:
                    274:        /* fill in rd_locker */
                    275:        TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
                    276:                if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
                    277:                        rcs_close(rfp);
                    278:                        return (NULL);
                    279:                }
                    280:
                    281:                rdp->rd_locker = xstrdup(lkr->rl_name);
                    282:        }
                    283:
                    284:        return (rfp);
                    285: }
                    286:
                    287: /*
                    288:  * rcs_close()
                    289:  *
                    290:  * Close an RCS file handle.
                    291:  */
                    292: void
                    293: rcs_close(RCSFILE *rfp)
                    294: {
                    295:        struct rcs_delta *rdp;
                    296:        struct rcs_access *rap;
                    297:        struct rcs_lock *rlp;
                    298:        struct rcs_sym *rsp;
                    299:
                    300:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    301:                rcs_write(rfp);
                    302:
                    303:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    304:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    305:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    306:                rcs_freedelta(rdp);
                    307:        }
                    308:
                    309:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    310:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    311:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
                    312:                xfree(rap->ra_name);
                    313:                xfree(rap);
                    314:        }
                    315:
                    316:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    317:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    318:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    319:                rcsnum_free(rsp->rs_num);
                    320:                xfree(rsp->rs_name);
                    321:                xfree(rsp);
                    322:        }
                    323:
                    324:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    325:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    326:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    327:                rcsnum_free(rlp->rl_num);
                    328:                xfree(rlp->rl_name);
                    329:                xfree(rlp);
                    330:        }
                    331:
                    332:        if (rfp->rf_head != NULL)
                    333:                rcsnum_free(rfp->rf_head);
                    334:        if (rfp->rf_branch != NULL)
                    335:                rcsnum_free(rfp->rf_branch);
                    336:
                    337:        if (rfp->rf_path != NULL)
                    338:                xfree(rfp->rf_path);
                    339:        if (rfp->rf_comment != NULL)
                    340:                xfree(rfp->rf_comment);
                    341:        if (rfp->rf_expand != NULL)
                    342:                xfree(rfp->rf_expand);
                    343:        if (rfp->rf_desc != NULL)
                    344:                xfree(rfp->rf_desc);
                    345:        if (rfp->rf_pdata != NULL)
                    346:                rcs_freepdata(rfp->rf_pdata);
                    347:        xfree(rfp);
                    348: }
                    349:
                    350: /*
                    351:  * rcs_write()
                    352:  *
                    353:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    354:  * path is in <rf_path>.
                    355:  * Returns 0 on success, or -1 on failure.
                    356:  */
                    357: int
                    358: rcs_write(RCSFILE *rfp)
                    359: {
                    360:        FILE *fp;
1.5     ! ray       361:        char buf[1024], numbuf[64], fn[20] = "";
1.1       joris     362:        void *bp;
                    363:        struct rcs_access *ap;
                    364:        struct rcs_sym *symp;
                    365:        struct rcs_branch *brp;
                    366:        struct rcs_delta *rdp;
                    367:        struct rcs_lock *lkp;
                    368:        ssize_t nread, nwritten;
                    369:        size_t len;
                    370:        int fd, from_fd, to_fd;
                    371:
                    372:        from_fd = to_fd = fd = -1;
                    373:
                    374:        if (rfp->rf_flags & RCS_SYNCED)
                    375:                return (0);
                    376:
                    377:        /* Write operations need the whole file parsed */
                    378:        rcs_parse_deltatexts(rfp, NULL);
                    379:
1.5     ! ray       380:        if (strlcpy(fn, "/tmp/rcs.XXXXXXXXXX", sizeof(fn)) >= sizeof(fn))
        !           381:                errx(1, "rcs_write: string truncated");
1.1       joris     382:        if ((fd = mkstemp(fn)) == -1)
1.2       xsa       383:                err(1, "%s", fn);
1.1       joris     384:
                    385:        if ((fp = fdopen(fd, "w+")) == NULL) {
1.5     ! ray       386:                int saved_errno;
        !           387:
        !           388:                saved_errno = errno;
1.2       xsa       389:                (void)unlink(fn);
1.5     ! ray       390:                errno = saved_errno;
1.2       xsa       391:                err(1, "%s", fn);
1.1       joris     392:        }
                    393:
                    394:        if (rfp->rf_head != NULL)
                    395:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    396:        else
                    397:                numbuf[0] = '\0';
                    398:
                    399:        fprintf(fp, "head\t%s;\n", numbuf);
                    400:
                    401:        if (rfp->rf_branch != NULL) {
                    402:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    403:                fprintf(fp, "branch\t%s;\n", numbuf);
                    404:        }
                    405:
                    406:        fputs("access", fp);
                    407:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    408:                fprintf(fp, "\n\t%s", ap->ra_name);
                    409:        }
                    410:        fputs(";\n", fp);
                    411:
                    412:        fprintf(fp, "symbols");
                    413:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    414:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.5     ! ray       415:                if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
        !           416:                    strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
        !           417:                    strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
        !           418:                        errx(1, "rcs_write: string overflow");
1.1       joris     419:                fprintf(fp, "\n\t%s", buf);
                    420:        }
                    421:        fprintf(fp, ";\n");
                    422:
                    423:        fprintf(fp, "locks");
                    424:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    425:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    426:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    427:        }
                    428:
                    429:        fprintf(fp, ";");
                    430:
                    431:        if (rfp->rf_flags & RCS_SLOCK)
                    432:                fprintf(fp, " strict;");
                    433:        fputc('\n', fp);
                    434:
                    435:        fputs("comment\t@", fp);
                    436:        if (rfp->rf_comment != NULL) {
                    437:                rcs_strprint((const u_char *)rfp->rf_comment,
                    438:                    strlen(rfp->rf_comment), fp);
                    439:                fputs("@;\n", fp);
                    440:        } else
                    441:                fputs("# @;\n", fp);
                    442:
                    443:        if (rfp->rf_expand != NULL) {
                    444:                fputs("expand @", fp);
                    445:                rcs_strprint((const u_char *)rfp->rf_expand,
                    446:                    strlen(rfp->rf_expand), fp);
                    447:                fputs("@;\n", fp);
                    448:        }
                    449:
                    450:        fputs("\n\n", fp);
                    451:
                    452:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    453:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    454:                    sizeof(numbuf)));
                    455:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
                    456:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
                    457:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    458:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    459:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    460:                    rdp->rd_author, rdp->rd_state);
                    461:                fputs("branches", fp);
                    462:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                    463:                        fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
                    464:                            sizeof(numbuf)));
                    465:                }
                    466:                fputs(";\n", fp);
                    467:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    468:                    numbuf, sizeof(numbuf)));
                    469:        }
                    470:
                    471:        fputs("\ndesc\n@", fp);
                    472:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
                    473:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    474:                if (rfp->rf_desc[len-1] != '\n')
                    475:                        fputc('\n', fp);
                    476:        }
                    477:        fputs("@\n", fp);
                    478:
                    479:        /* deltatexts */
                    480:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    481:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    482:                    sizeof(numbuf)));
                    483:                fputs("log\n@", fp);
                    484:                if (rdp->rd_log != NULL) {
                    485:                        len = strlen(rdp->rd_log);
                    486:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
                    487:                        if (rdp->rd_log[len-1] != '\n')
                    488:                                fputc('\n', fp);
                    489:                }
                    490:                fputs("@\ntext\n@", fp);
                    491:                if (rdp->rd_text != NULL) {
                    492:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
                    493:
                    494:                        if (rdp->rd_tlen != 0) {
                    495:                                if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
                    496:                                        fputc('\n', fp);
                    497:                        }
                    498:                }
                    499:                fputs("@\n", fp);
                    500:        }
                    501:        fclose(fp);
                    502:
                    503:        /*
                    504:         * We try to use rename() to atomically put the new file in place.
                    505:         * If that fails, we try a copy.
                    506:         */
                    507:        if (rename(fn, rfp->rf_path) == -1) {
                    508:                if (errno == EXDEV) {
                    509:                        /* rename() not supported so we have to copy. */
                    510:                        if (chmod(rfp->rf_path, S_IWUSR) == -1 &&
                    511:                            !(rfp->rf_flags & RCS_CREATE)) {
                    512:                                errx(1, "chmod(%s, 0%o) failed",
                    513:                                    rfp->rf_path, S_IWUSR);
                    514:                        }
                    515:
                    516:                        if ((from_fd = open(fn, O_RDONLY)) == -1) {
                    517:                                warn("failed to open `%s'",
                    518:                                    rfp->rf_path);
                    519:                                return (-1);
                    520:                        }
                    521:
                    522:                        if ((to_fd = open(rfp->rf_path,
                    523:                            O_WRONLY|O_TRUNC|O_CREAT)) == -1) {
                    524:                                warn("failed to open `%s'", fn);
                    525:                                close(from_fd);
                    526:                                return (-1);
                    527:                        }
                    528:
                    529:                        bp = xmalloc(MAXBSIZE);
                    530:                        for (;;) {
                    531:                                if ((nread = read(from_fd, bp, MAXBSIZE)) == 0)
                    532:                                        break;
                    533:                                if (nread == -1)
                    534:                                        goto err;
                    535:                                nwritten = write(to_fd, bp, (size_t)nread);
                    536:                                if (nwritten == -1 || nwritten != nread)
                    537:                                        goto err;
                    538:                        }
                    539:
                    540:                        if (nread < 0) {
                    541: err:                           if (unlink(rfp->rf_path) == -1)
                    542:                                        warn("failed to unlink `%s'",
                    543:                                            rfp->rf_path);
                    544:                                close(from_fd);
                    545:                                close(to_fd);
                    546:                                xfree(bp);
                    547:                                return (-1);
                    548:                        }
                    549:
                    550:                        close(from_fd);
                    551:                        close(to_fd);
                    552:                        xfree(bp);
                    553:
                    554:                        if (unlink(fn) == -1) {
                    555:                                warn("failed to unlink `%s'", fn);
                    556:                                return (-1);
                    557:                        }
                    558:                } else {
                    559:                        warn("failed to access temp RCS output file");
                    560:                        return (-1);
                    561:                }
                    562:        }
                    563:
                    564:        if (chmod(rfp->rf_path, rfp->rf_mode) == -1) {
                    565:                warn("failed to chmod `%s'", rfp->rf_path);
                    566:                return (-1);
                    567:        }
                    568:
                    569:        rfp->rf_flags |= RCS_SYNCED;
                    570:
                    571:        return (0);
                    572: }
                    573:
                    574: /*
                    575:  * rcs_head_get()
                    576:  *
                    577:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    578:  */
                    579: const RCSNUM *
                    580: rcs_head_get(RCSFILE *file)
                    581: {
                    582:        return (file->rf_head);
                    583: }
                    584:
                    585: /*
                    586:  * rcs_head_set()
                    587:  *
                    588:  * Set the revision number of the head revision for the RCS file <file> to
                    589:  * <rev>, which must reference a valid revision within the file.
                    590:  */
                    591: int
                    592: rcs_head_set(RCSFILE *file, RCSNUM *rev)
                    593: {
                    594:        if (rcs_findrev(file, rev) == NULL)
                    595:                return (-1);
                    596:
                    597:        if (file->rf_head == NULL)
                    598:                file->rf_head = rcsnum_alloc();
                    599:
                    600:        rcsnum_cpy(rev, file->rf_head, 0);
                    601:        file->rf_flags &= ~RCS_SYNCED;
                    602:        return (0);
                    603: }
                    604:
                    605:
                    606: /*
                    607:  * rcs_branch_get()
                    608:  *
                    609:  * Retrieve the default branch number for the RCS file <file>.
                    610:  * Returns the number on success.  If NULL is returned, then there is no
                    611:  * default branch for this file.
                    612:  */
                    613: const RCSNUM *
                    614: rcs_branch_get(RCSFILE *file)
                    615: {
                    616:        return (file->rf_branch);
                    617: }
                    618:
                    619: /*
                    620:  * rcs_branch_set()
                    621:  *
                    622:  * Set the default branch for the RCS file <file> to <bnum>.
                    623:  * Returns 0 on success, -1 on failure.
                    624:  */
                    625: int
                    626: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
                    627: {
                    628:        if (file->rf_branch == NULL)
                    629:                file->rf_branch = rcsnum_alloc();
                    630:
                    631:        rcsnum_cpy(bnum, file->rf_branch, 0);
                    632:        file->rf_flags &= ~RCS_SYNCED;
                    633:        return (0);
                    634: }
                    635:
                    636: /*
                    637:  * rcs_access_add()
                    638:  *
                    639:  * Add the login name <login> to the access list for the RCS file <file>.
                    640:  * Returns 0 on success, or -1 on failure.
                    641:  */
                    642: int
                    643: rcs_access_add(RCSFILE *file, const char *login)
                    644: {
                    645:        struct rcs_access *ap;
                    646:
                    647:        /* first look for duplication */
                    648:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
                    649:                if (strcmp(ap->ra_name, login) == 0) {
                    650:                        rcs_errno = RCS_ERR_DUPENT;
                    651:                        return (-1);
                    652:                }
                    653:        }
                    654:
                    655:        ap = xmalloc(sizeof(*ap));
                    656:        ap->ra_name = xstrdup(login);
                    657:        TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
                    658:
                    659:        /* not synced anymore */
                    660:        file->rf_flags &= ~RCS_SYNCED;
                    661:        return (0);
                    662: }
                    663:
                    664: /*
                    665:  * rcs_access_remove()
                    666:  *
                    667:  * Remove an entry with login name <login> from the access list of the RCS
                    668:  * file <file>.
                    669:  * Returns 0 on success, or -1 on failure.
                    670:  */
                    671: int
                    672: rcs_access_remove(RCSFILE *file, const char *login)
                    673: {
                    674:        struct rcs_access *ap;
                    675:
                    676:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
                    677:                if (strcmp(ap->ra_name, login) == 0)
                    678:                        break;
                    679:
                    680:        if (ap == NULL) {
                    681:                rcs_errno = RCS_ERR_NOENT;
                    682:                return (-1);
                    683:        }
                    684:
                    685:        TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
                    686:        xfree(ap->ra_name);
                    687:        xfree(ap);
                    688:
                    689:        /* not synced anymore */
                    690:        file->rf_flags &= ~RCS_SYNCED;
                    691:        return (0);
                    692: }
                    693:
                    694: /*
                    695:  * rcs_sym_add()
                    696:  *
                    697:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    698:  * is named <sym> and is bound to the RCS revision <snum>.
                    699:  * Returns 0 on success, or -1 on failure.
                    700:  */
                    701: int
                    702: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
                    703: {
                    704:        struct rcs_sym *symp;
                    705:
                    706:        if (!rcs_sym_check(sym)) {
                    707:                rcs_errno = RCS_ERR_BADSYM;
                    708:                return (-1);
                    709:        }
                    710:
                    711:        /* first look for duplication */
                    712:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    713:                if (strcmp(symp->rs_name, sym) == 0) {
                    714:                        rcs_errno = RCS_ERR_DUPENT;
                    715:                        return (-1);
                    716:                }
                    717:        }
                    718:
                    719:        symp = xmalloc(sizeof(*symp));
                    720:        symp->rs_name = xstrdup(sym);
                    721:        symp->rs_num = rcsnum_alloc();
                    722:        rcsnum_cpy(snum, symp->rs_num, 0);
                    723:
                    724:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    725:
                    726:        /* not synced anymore */
                    727:        rfp->rf_flags &= ~RCS_SYNCED;
                    728:        return (0);
                    729: }
                    730:
                    731: /*
                    732:  * rcs_sym_remove()
                    733:  *
                    734:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    735:  * <file>.  If no such symbol is found, the call fails and returns with an
                    736:  * error.
                    737:  * Returns 0 on success, or -1 on failure.
                    738:  */
                    739: int
                    740: rcs_sym_remove(RCSFILE *file, const char *sym)
                    741: {
                    742:        struct rcs_sym *symp;
                    743:
                    744:        if (!rcs_sym_check(sym)) {
                    745:                rcs_errno = RCS_ERR_BADSYM;
                    746:                return (-1);
                    747:        }
                    748:
                    749:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    750:                if (strcmp(symp->rs_name, sym) == 0)
                    751:                        break;
                    752:
                    753:        if (symp == NULL) {
                    754:                rcs_errno = RCS_ERR_NOENT;
                    755:                return (-1);
                    756:        }
                    757:
                    758:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
                    759:        xfree(symp->rs_name);
                    760:        rcsnum_free(symp->rs_num);
                    761:        xfree(symp);
                    762:
                    763:        /* not synced anymore */
                    764:        file->rf_flags &= ~RCS_SYNCED;
                    765:        return (0);
                    766: }
                    767:
                    768: /*
                    769:  * rcs_sym_getrev()
                    770:  *
                    771:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    772:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    773:  * should be freed by the caller once they are done with it.
                    774:  * Returns the RCSNUM on success, or NULL on failure.
                    775:  */
                    776: RCSNUM *
                    777: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    778: {
                    779:        RCSNUM *num;
                    780:        struct rcs_sym *symp;
                    781:
                    782:        if (!rcs_sym_check(sym)) {
                    783:                rcs_errno = RCS_ERR_BADSYM;
                    784:                return (NULL);
                    785:        }
                    786:
                    787:        num = NULL;
                    788:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    789:                if (strcmp(symp->rs_name, sym) == 0)
                    790:                        break;
                    791:
                    792:        if (symp == NULL) {
                    793:                rcs_errno = RCS_ERR_NOENT;
                    794:        } else {
                    795:                num = rcsnum_alloc();
                    796:                rcsnum_cpy(symp->rs_num, num, 0);
                    797:        }
                    798:
                    799:        return (num);
                    800: }
                    801:
                    802: /*
                    803:  * rcs_sym_check()
                    804:  *
                    805:  * Check the RCS symbol name <sym> for any unsupported characters.
                    806:  * Returns 1 if the tag is correct, 0 if it isn't valid.
                    807:  */
                    808: int
                    809: rcs_sym_check(const char *sym)
                    810: {
                    811:        int ret;
                    812:        const char *cp;
                    813:
                    814:        ret = 1;
                    815:        cp = sym;
                    816:        if (!isalpha(*cp++))
                    817:                return (0);
                    818:
                    819:        for (; *cp != '\0'; cp++)
                    820:                if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
                    821:                        ret = 0;
                    822:                        break;
                    823:                }
                    824:
                    825:        return (ret);
                    826: }
                    827:
                    828: /*
                    829:  * rcs_lock_getmode()
                    830:  *
                    831:  * Retrieve the locking mode of the RCS file <file>.
                    832:  */
                    833: int
                    834: rcs_lock_getmode(RCSFILE *file)
                    835: {
                    836:        return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
                    837: }
                    838:
                    839: /*
                    840:  * rcs_lock_setmode()
                    841:  *
                    842:  * Set the locking mode of the RCS file <file> to <mode>, which must either
                    843:  * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
                    844:  * Returns the previous mode on success, or -1 on failure.
                    845:  */
                    846: int
                    847: rcs_lock_setmode(RCSFILE *file, int mode)
                    848: {
                    849:        int pmode;
                    850:        pmode = rcs_lock_getmode(file);
                    851:
                    852:        if (mode == RCS_LOCK_STRICT)
                    853:                file->rf_flags |= RCS_SLOCK;
                    854:        else if (mode == RCS_LOCK_LOOSE)
                    855:                file->rf_flags &= ~RCS_SLOCK;
                    856:        else
                    857:                errx(1, "rcs_lock_setmode: invalid mode `%d'", mode);
                    858:
                    859:        file->rf_flags &= ~RCS_SYNCED;
                    860:        return (pmode);
                    861: }
                    862:
                    863: /*
                    864:  * rcs_lock_add()
                    865:  *
                    866:  * Add an RCS lock for the user <user> on revision <rev>.
                    867:  * Returns 0 on success, or -1 on failure.
                    868:  */
                    869: int
                    870: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
                    871: {
                    872:        struct rcs_lock *lkp;
                    873:
                    874:        /* first look for duplication */
                    875:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    876:                if (strcmp(lkp->rl_name, user) == 0 &&
                    877:                    rcsnum_cmp(rev, lkp->rl_num, 0) == 0) {
                    878:                        rcs_errno = RCS_ERR_DUPENT;
                    879:                        return (-1);
                    880:                }
                    881:        }
                    882:
                    883:        lkp = xmalloc(sizeof(*lkp));
                    884:        lkp->rl_name = xstrdup(user);
                    885:        lkp->rl_num = rcsnum_alloc();
                    886:        rcsnum_cpy(rev, lkp->rl_num, 0);
                    887:
                    888:        TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
                    889:
                    890:        /* not synced anymore */
                    891:        file->rf_flags &= ~RCS_SYNCED;
                    892:        return (0);
                    893: }
                    894:
                    895:
                    896: /*
                    897:  * rcs_lock_remove()
                    898:  *
                    899:  * Remove the RCS lock on revision <rev>.
                    900:  * Returns 0 on success, or -1 on failure.
                    901:  */
                    902: int
                    903: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
                    904: {
                    905:        struct rcs_lock *lkp;
                    906:
                    907:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    908:                if (strcmp(lkp->rl_name, user) == 0 &&
                    909:                    rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
                    910:                        break;
                    911:        }
                    912:
                    913:        if (lkp == NULL) {
                    914:                rcs_errno = RCS_ERR_NOENT;
                    915:                return (-1);
                    916:        }
                    917:
                    918:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
                    919:        rcsnum_free(lkp->rl_num);
                    920:        xfree(lkp->rl_name);
                    921:        xfree(lkp);
                    922:
                    923:        /* not synced anymore */
                    924:        file->rf_flags &= ~RCS_SYNCED;
                    925:        return (0);
                    926: }
                    927:
                    928: /*
                    929:  * rcs_desc_get()
                    930:  *
                    931:  * Retrieve the description for the RCS file <file>.
                    932:  */
                    933: const char *
                    934: rcs_desc_get(RCSFILE *file)
                    935: {
                    936:        return (file->rf_desc);
                    937: }
                    938:
                    939: /*
                    940:  * rcs_desc_set()
                    941:  *
                    942:  * Set the description for the RCS file <file>.
                    943:  */
                    944: void
                    945: rcs_desc_set(RCSFILE *file, const char *desc)
                    946: {
                    947:        char *tmp;
                    948:
                    949:        tmp = xstrdup(desc);
                    950:        if (file->rf_desc != NULL)
                    951:                xfree(file->rf_desc);
                    952:        file->rf_desc = tmp;
                    953:        file->rf_flags &= ~RCS_SYNCED;
                    954: }
                    955:
                    956: /*
                    957:  * rcs_comment_lookup()
                    958:  *
                    959:  * Lookup the assumed comment leader based on a file's suffix.
                    960:  * Returns a pointer to the string on success, or NULL on failure.
                    961:  */
                    962: const char *
                    963: rcs_comment_lookup(const char *filename)
                    964: {
                    965:        int i;
                    966:        const char *sp;
                    967:
                    968:        if ((sp = strrchr(filename, '.')) == NULL) {
                    969:                rcs_errno = RCS_ERR_NOENT;
                    970:                return (NULL);
                    971:        }
                    972:        sp++;
                    973:
                    974:        for (i = 0; i < (int)NB_COMTYPES; i++)
                    975:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                    976:                        return (rcs_comments[i].rc_cstr);
                    977:        return (NULL);
                    978: }
                    979:
                    980: /*
                    981:  * rcs_comment_get()
                    982:  *
                    983:  * Retrieve the comment leader for the RCS file <file>.
                    984:  */
                    985: const char *
                    986: rcs_comment_get(RCSFILE *file)
                    987: {
                    988:        return (file->rf_comment);
                    989: }
                    990:
                    991: /*
                    992:  * rcs_comment_set()
                    993:  *
                    994:  * Set the comment leader for the RCS file <file>.
                    995:  */
                    996: void
                    997: rcs_comment_set(RCSFILE *file, const char *comment)
                    998: {
                    999:        char *tmp;
                   1000:
                   1001:        tmp = xstrdup(comment);
                   1002:        if (file->rf_comment != NULL)
                   1003:                xfree(file->rf_comment);
                   1004:        file->rf_comment = tmp;
                   1005:        file->rf_flags &= ~RCS_SYNCED;
                   1006: }
                   1007:
                   1008: /*
                   1009:  * rcs_tag_resolve()
                   1010:  *
                   1011:  * Retrieve the revision number corresponding to the tag <tag> for the RCS
                   1012:  * file <file>.
                   1013:  */
                   1014: RCSNUM *
                   1015: rcs_tag_resolve(RCSFILE *file, const char *tag)
                   1016: {
                   1017:        RCSNUM *num;
                   1018:
                   1019:        if ((num = rcsnum_parse(tag)) == NULL) {
                   1020:                num = rcs_sym_getrev(file, tag);
                   1021:        }
                   1022:
                   1023:        return (num);
                   1024: }
                   1025:
                   1026: int
                   1027: rcs_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
                   1028: {
                   1029:        char op, *ep;
                   1030:        struct rcs_line *lp, *dlp, *ndlp;
                   1031:        int i, lineno, nbln;
                   1032:
                   1033:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1034:        lp = TAILQ_FIRST(&(plines->l_lines));
                   1035:
                   1036:        /* skip first bogus line */
                   1037:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1038:            lp = TAILQ_NEXT(lp, l_list)) {
                   1039:                op = *(lp->l_line);
                   1040:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
                   1041:                if (lineno > dlines->l_nblines || lineno < 0 ||
                   1042:                    *ep != ' ')
                   1043:                        errx(1, "invalid line specification in RCS patch");
                   1044:                ep++;
                   1045:                nbln = (int)strtol(ep, &ep, 10);
                   1046:                if (nbln < 0 || *ep != '\0')
                   1047:                        errx(1,
                   1048:                            "invalid line number specification in RCS patch");
                   1049:
                   1050:                /* find the appropriate line */
                   1051:                for (;;) {
                   1052:                        if (dlp == NULL)
                   1053:                                break;
                   1054:                        if (dlp->l_lineno == lineno)
                   1055:                                break;
                   1056:                        if (dlp->l_lineno > lineno) {
                   1057:                                dlp = TAILQ_PREV(dlp, rcs_tqh, l_list);
                   1058:                        } else if (dlp->l_lineno < lineno) {
                   1059:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
                   1060:                                    ndlp->l_lineno > lineno)
                   1061:                                        break;
                   1062:                                dlp = ndlp;
                   1063:                        }
                   1064:                }
                   1065:                if (dlp == NULL)
                   1066:                        errx(1, "can't find referenced line in RCS patch");
                   1067:
                   1068:                if (op == 'd') {
                   1069:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
                   1070:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1071:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
                   1072:                                dlp = ndlp;
                   1073:                                /* last line is gone - reset dlp */
                   1074:                                if (dlp == NULL) {
                   1075:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1076:                                            rcs_tqh);
                   1077:                                        dlp = ndlp;
                   1078:                                }
                   1079:                        }
                   1080:                } else if (op == 'a') {
                   1081:                        for (i = 0; i < nbln; i++) {
                   1082:                                ndlp = lp;
                   1083:                                lp = TAILQ_NEXT(lp, l_list);
                   1084:                                if (lp == NULL)
                   1085:                                        errx(1, "truncated RCS patch");
                   1086:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1087:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1088:                                    lp, l_list);
                   1089:                                dlp = lp;
                   1090:
                   1091:                                /* we don't want lookup to block on those */
                   1092:                                lp->l_lineno = lineno;
                   1093:
                   1094:                                lp = ndlp;
                   1095:                        }
                   1096:                } else
                   1097:                        errx(1, "unknown RCS patch operation `%c'", op);
                   1098:
                   1099:                /* last line of the patch, done */
                   1100:                if (lp->l_lineno == plines->l_nblines)
                   1101:                        break;
                   1102:        }
                   1103:
                   1104:        /* once we're done patching, rebuild the line numbers */
                   1105:        lineno = 0;
                   1106:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1107:                lp->l_lineno = lineno++;
                   1108:        dlines->l_nblines = lineno - 1;
                   1109:
                   1110:        return (0);
                   1111: }
                   1112:
                   1113: /*
                   1114:  * rcs_getrev()
                   1115:  *
                   1116:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
                   1117:  * returned buffer is dynamically allocated and should be released using
                   1118:  * rcs_buf_free() once the caller is done using it.
                   1119:  */
                   1120: BUF*
                   1121: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
                   1122: {
                   1123:        u_int i, numlen;
                   1124:        int isbranch, lookonbranch;
                   1125:        size_t len;
                   1126:        void *bp;
                   1127:        RCSNUM *crev, *rev, *brev;
                   1128:        BUF *rbuf;
                   1129:        struct rcs_delta *rdp = NULL;
                   1130:        struct rcs_branch *rb;
                   1131:
                   1132:        if (rfp->rf_head == NULL)
                   1133:                return (NULL);
                   1134:
                   1135:        if (frev == RCS_HEAD_REV)
                   1136:                rev = rfp->rf_head;
                   1137:        else
                   1138:                rev = frev;
                   1139:
                   1140:        /* XXX rcsnum_cmp() */
                   1141:        for (i = 0; i < rfp->rf_head->rn_len; i++) {
                   1142:                if (rfp->rf_head->rn_id[i] < rev->rn_id[i]) {
                   1143:                        rcs_errno = RCS_ERR_NOENT;
                   1144:                        return (NULL);
                   1145:                }
                   1146:        }
                   1147:
                   1148:        /* No matter what, we're going to need up the the description parsed */
                   1149:        rcs_parse_desc(rfp, NULL);
                   1150:
                   1151:        rdp = rcs_findrev(rfp, rfp->rf_head);
                   1152:        if (rdp == NULL) {
                   1153:                warnx("failed to get RCS HEAD revision");
                   1154:                return (NULL);
                   1155:        }
                   1156:
                   1157:        if (rdp->rd_tlen == 0)
                   1158:                rcs_parse_deltatexts(rfp, rfp->rf_head);
                   1159:
                   1160:        len = rdp->rd_tlen;
                   1161:        if (len == 0) {
                   1162:                rbuf = rcs_buf_alloc(1, 0);
                   1163:                rcs_buf_empty(rbuf);
                   1164:                return (rbuf);
                   1165:        }
                   1166:
                   1167:        rbuf = rcs_buf_alloc(len, BUF_AUTOEXT);
                   1168:        rcs_buf_append(rbuf, rdp->rd_text, len);
                   1169:
                   1170:        isbranch = 0;
                   1171:        brev = NULL;
                   1172:
                   1173:        /*
                   1174:         * If a branch was passed, get the latest revision on it.
                   1175:         */
                   1176:        if (RCSNUM_ISBRANCH(rev)) {
                   1177:                brev = rev;
                   1178:                rdp = rcs_findrev(rfp, rev);
                   1179:                if (rdp == NULL)
                   1180:                        return (NULL);
                   1181:
                   1182:                rev = rdp->rd_num;
                   1183:        } else {
                   1184:                if (RCSNUM_ISBRANCHREV(rev)) {
                   1185:                        brev = rcsnum_revtobr(rev);
                   1186:                        isbranch = 1;
                   1187:                }
                   1188:        }
                   1189:
                   1190:        lookonbranch = 0;
                   1191:        crev = NULL;
                   1192:
                   1193:        /* Apply patches backwards to get the right version.
                   1194:         */
                   1195:        do {
                   1196:                if (rcsnum_cmp(rfp->rf_head, rev, 0) == 0)
                   1197:                        break;
                   1198:
                   1199:                if (isbranch == 1 && rdp->rd_num->rn_len < rev->rn_len &&
                   1200:                    !TAILQ_EMPTY(&(rdp->rd_branches)))
                   1201:                        lookonbranch = 1;
                   1202:
                   1203:                if (isbranch && lookonbranch == 1) {
                   1204:                        lookonbranch = 0;
                   1205:                        TAILQ_FOREACH(rb, &(rdp->rd_branches), rb_list) {
                   1206:                                /* XXX rcsnum_cmp() is totally broken for
                   1207:                                 * this purpose.
                   1208:                                 */
                   1209:                                numlen = MIN(brev->rn_len, rb->rb_num->rn_len);
                   1210:                                for (i = 0; i < numlen; i++) {
                   1211:                                        if (rb->rb_num->rn_id[i] !=
                   1212:                                            brev->rn_id[i])
                   1213:                                                break;
                   1214:                                }
                   1215:
                   1216:                                if (i == numlen) {
                   1217:                                        crev = rb->rb_num;
                   1218:                                        break;
                   1219:                                }
                   1220:                        }
                   1221:                } else {
                   1222:                        crev = rdp->rd_next;
                   1223:                }
                   1224:
                   1225:                rdp = rcs_findrev(rfp, crev);
                   1226:                if (rdp == NULL) {
                   1227:                        rcs_buf_free(rbuf);
                   1228:                        return (NULL);
                   1229:                }
                   1230:
                   1231:                rcs_buf_putc(rbuf, '\0');
                   1232:
                   1233:                /* check if we have parsed this rev's deltatext */
                   1234:                if (rdp->rd_tlen == 0)
                   1235:                        rcs_parse_deltatexts(rfp, rdp->rd_num);
                   1236:
                   1237:                bp = rcs_buf_release(rbuf);
                   1238:                rbuf = rcs_patchfile((char *)bp, (char *)rdp->rd_text,
                   1239:                    rcs_patch_lines);
                   1240:                xfree(bp);
                   1241:
                   1242:                if (rbuf == NULL)
                   1243:                        break;
                   1244:        } while (rcsnum_cmp(crev, rev, 0) != 0);
                   1245:
                   1246:        if (rcs_buf_getc(rbuf, rcs_buf_len(rbuf)-1) != '\n' &&
                   1247:            rbuf != NULL)
                   1248:                rcs_buf_putc(rbuf, '\n');
                   1249:
                   1250:        return (rbuf);
                   1251: }
                   1252:
                   1253: /*
                   1254:  * rcs_rev_add()
                   1255:  *
                   1256:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1257:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1258:  * new revision will have a number equal to the previous head revision plus
                   1259:  * one).  The <msg> argument specifies the log message for that revision, and
                   1260:  * <date> specifies the revision's date (a value of -1 is
                   1261:  * equivalent to using the current time).
                   1262:  * If <username> is NULL, set the author for this revision to the current user.
                   1263:  * Otherwise, set it to <username>.
                   1264:  * Returns 0 on success, or -1 on failure.
                   1265:  */
                   1266: int
                   1267: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1268:     const char *username)
                   1269: {
                   1270:        time_t now;
                   1271:        struct passwd *pw;
                   1272:        struct rcs_delta *ordp, *rdp;
                   1273:
                   1274:        if (rev == RCS_HEAD_REV) {
                   1275:                if (rf->rf_flags & RCS_CREATE) {
                   1276:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1277:                                return (-1);
                   1278:                        rf->rf_head = rcsnum_alloc();
                   1279:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1280:                } else {
                   1281:                        rev = rcsnum_inc(rf->rf_head);
                   1282:                }
                   1283:        } else {
                   1284:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1285:                        rcs_errno = RCS_ERR_DUPENT;
                   1286:                        return (-1);
                   1287:                }
                   1288:        }
                   1289:
                   1290:        if ((pw = getpwuid(getuid())) == NULL)
                   1291:                errx(1, "getpwuid failed");
                   1292:
                   1293:        rdp = xcalloc(1, sizeof(*rdp));
                   1294:
                   1295:        TAILQ_INIT(&(rdp->rd_branches));
                   1296:
                   1297:        rdp->rd_num = rcsnum_alloc();
                   1298:        rcsnum_cpy(rev, rdp->rd_num, 0);
                   1299:
                   1300:        rdp->rd_next = rcsnum_alloc();
                   1301:
                   1302:        if (!(rf->rf_flags & RCS_CREATE)) {
                   1303:                /* next should point to the previous HEAD */
                   1304:                ordp = TAILQ_FIRST(&(rf->rf_delta));
                   1305:                rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
                   1306:        }
                   1307:
                   1308:
                   1309:        if (username == NULL)
                   1310:                username = pw->pw_name;
                   1311:
                   1312:        rdp->rd_author = xstrdup(username);
                   1313:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1314:        rdp->rd_log = xstrdup(msg);
                   1315:
                   1316:        if (date != (time_t)(-1))
                   1317:                now = date;
                   1318:        else
                   1319:                time(&now);
                   1320:        gmtime_r(&now, &(rdp->rd_date));
                   1321:
                   1322:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1323:        rf->rf_ndelta++;
                   1324:
                   1325:        /* not synced anymore */
                   1326:        rf->rf_flags &= ~RCS_SYNCED;
                   1327:
                   1328:        return (0);
                   1329: }
                   1330:
                   1331: /*
                   1332:  * rcs_rev_remove()
                   1333:  *
                   1334:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1335:  */
                   1336: int
                   1337: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1338: {
                   1339:        size_t len;
                   1340:        char *tmpdir;
                   1341:        char *newdeltatext, path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
                   1342:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
                   1343:        BUF *nextbuf, *prevbuf, *newdiff;
                   1344:
                   1345:        tmpdir = rcs_tmpdir;
                   1346:
                   1347:        if (rev == RCS_HEAD_REV)
                   1348:                rev = rf->rf_head;
                   1349:
                   1350:        /* do we actually have that revision? */
                   1351:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1352:                rcs_errno = RCS_ERR_NOENT;
                   1353:                return (-1);
                   1354:        }
                   1355:
                   1356:        /*
                   1357:         * This is confusing, the previous delta is next in the TAILQ list.
                   1358:         * the next delta is the previous one in the TAILQ list.
                   1359:         *
                   1360:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1361:         * When the first revision got specified, prevrdp will be NULL.
                   1362:         */
                   1363:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
                   1364:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, rcs_tqh, rd_list);
                   1365:
                   1366:        newdeltatext = NULL;
                   1367:        prevbuf = nextbuf = NULL;
                   1368:
                   1369:        if (prevrdp != NULL) {
                   1370:                if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
                   1371:                        errx(1, "error getting revision");
                   1372:        }
                   1373:
                   1374:        if (prevrdp != NULL && nextrdp != NULL) {
                   1375:                if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
                   1376:                        errx(1, "error getting revision");
                   1377:
                   1378:                newdiff = rcs_buf_alloc(64, BUF_AUTOEXT);
                   1379:
                   1380:                /* calculate new diff */
                   1381:                len = strlcpy(path_tmp1, tmpdir, sizeof(path_tmp1));
                   1382:                if (len >= sizeof(path_tmp1))
                   1383:                        errx(1, "path truncation in rcs_rev_remove");
                   1384:
                   1385:                len = strlcat(path_tmp1, "/diff1.XXXXXXXXXX",
                   1386:                    sizeof(path_tmp1));
                   1387:                if (len >= sizeof(path_tmp1))
                   1388:                        errx(1, "path truncation in rcs_rev_remove");
                   1389:
                   1390:                rcs_buf_write_stmp(nextbuf, path_tmp1, 0600);
                   1391:                rcs_buf_free(nextbuf);
                   1392:
                   1393:                len = strlcpy(path_tmp2, tmpdir, sizeof(path_tmp2));
                   1394:                if (len >= sizeof(path_tmp2))
                   1395:                        errx(1, "path truncation in rcs_rev_remove");
                   1396:
                   1397:                len = strlcat(path_tmp2, "/diff2.XXXXXXXXXX",
                   1398:                    sizeof(path_tmp2));
                   1399:                if (len >= sizeof(path_tmp2))
                   1400:                        errx(1, "path truncation in rcs_rev_remove");
                   1401:
                   1402:                rcs_buf_write_stmp(prevbuf, path_tmp2, 0600);
                   1403:                rcs_buf_free(prevbuf);
                   1404:
                   1405:                diff_format = D_RCSDIFF;
                   1406:                rcs_diffreg(path_tmp1, path_tmp2, newdiff);
                   1407:
                   1408:                newdeltatext = rcs_buf_release(newdiff);
                   1409:        } else if (nextrdp == NULL && prevrdp != NULL) {
                   1410:                newdeltatext = rcs_buf_release(prevbuf);
                   1411:        }
                   1412:
                   1413:        if (newdeltatext != NULL) {
                   1414:                if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
                   1415:                        errx(1, "error setting new deltatext");
                   1416:        }
                   1417:
                   1418:        TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1419:
                   1420:        /* update pointers */
                   1421:        if (prevrdp != NULL && nextrdp != NULL) {
                   1422:                rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
                   1423:        } else if (prevrdp != NULL) {
                   1424:                if (rcs_head_set(rf, prevrdp->rd_num) < 0)
                   1425:                        errx(1, "rcs_head_set failed");
                   1426:        } else if (nextrdp != NULL) {
                   1427:                rcsnum_free(nextrdp->rd_next);
                   1428:                nextrdp->rd_next = rcsnum_alloc();
                   1429:        } else {
                   1430:                rcsnum_free(rf->rf_head);
                   1431:                rf->rf_head = NULL;
                   1432:        }
                   1433:
                   1434:        rf->rf_ndelta--;
                   1435:        rf->rf_flags &= ~RCS_SYNCED;
                   1436:
                   1437:        rcs_freedelta(rdp);
                   1438:
                   1439:        if (newdeltatext != NULL)
                   1440:                xfree(newdeltatext);
                   1441:
                   1442:        return (0);
                   1443: }
                   1444:
                   1445: /*
                   1446:  * rcs_findrev()
                   1447:  *
                   1448:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1449:  * The revision number is given in <rev>.
                   1450:  *
                   1451:  * If the given revision is a branch number, we translate it into the latest
                   1452:  * revision on the branch.
                   1453:  *
                   1454:  * Returns a pointer to the delta on success, or NULL on failure.
                   1455:  */
                   1456: struct rcs_delta *
                   1457: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
                   1458: {
                   1459:        u_int cmplen;
                   1460:        struct rcs_delta *rdp;
                   1461:        RCSNUM *brev, *frev;
                   1462:
                   1463:        /*
                   1464:         * We need to do more parsing if the last revision in the linked list
                   1465:         * is greater than the requested revision.
                   1466:         */
                   1467:        rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
                   1468:        if (rdp == NULL ||
                   1469:            rcsnum_cmp(rdp->rd_num, rev, 0) == -1) {
                   1470:                rcs_parse_deltas(rfp, rev);
                   1471:        }
                   1472:
                   1473:        /*
                   1474:         * Translate a branch into the latest revision on the branch itself.
                   1475:         */
                   1476:        if (RCSNUM_ISBRANCH(rev)) {
                   1477:                brev = rcsnum_brtorev(rev);
                   1478:                frev = brev;
                   1479:                for (;;) {
                   1480:                        rdp = rcs_findrev(rfp, frev);
                   1481:                        if (rdp == NULL)
                   1482:                                return (NULL);
                   1483:
                   1484:                        if (rdp->rd_next->rn_len == 0)
                   1485:                                break;
                   1486:
                   1487:                        frev = rdp->rd_next;
                   1488:                }
                   1489:
                   1490:                rcsnum_free(brev);
                   1491:                return (rdp);
                   1492:        }
                   1493:
                   1494:        cmplen = rev->rn_len;
                   1495:
                   1496:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1497:                if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0)
                   1498:                        return (rdp);
                   1499:        }
                   1500:
                   1501:        return (NULL);
                   1502: }
                   1503:
                   1504: /*
                   1505:  * rcs_kwexp_set()
                   1506:  *
                   1507:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1508:  */
                   1509: void
                   1510: rcs_kwexp_set(RCSFILE *file, int mode)
                   1511: {
                   1512:        int i;
                   1513:        char *tmp, buf[8] = "";
                   1514:
                   1515:        if (RCS_KWEXP_INVAL(mode))
                   1516:                return;
                   1517:
                   1518:        i = 0;
                   1519:        if (mode == RCS_KWEXP_NONE)
                   1520:                buf[0] = 'b';
                   1521:        else if (mode == RCS_KWEXP_OLD)
                   1522:                buf[0] = 'o';
                   1523:        else {
                   1524:                if (mode & RCS_KWEXP_NAME)
                   1525:                        buf[i++] = 'k';
                   1526:                if (mode & RCS_KWEXP_VAL)
                   1527:                        buf[i++] = 'v';
                   1528:                if (mode & RCS_KWEXP_LKR)
                   1529:                        buf[i++] = 'l';
                   1530:        }
                   1531:
                   1532:        tmp = xstrdup(buf);
                   1533:        if (file->rf_expand != NULL)
                   1534:                xfree(file->rf_expand);
                   1535:        file->rf_expand = tmp;
                   1536:        /* not synced anymore */
                   1537:        file->rf_flags &= ~RCS_SYNCED;
                   1538: }
                   1539:
                   1540: /*
                   1541:  * rcs_kwexp_get()
                   1542:  *
                   1543:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1544:  */
                   1545: int
                   1546: rcs_kwexp_get(RCSFILE *file)
                   1547: {
                   1548:        return rcs_kflag_get(file->rf_expand);
                   1549: }
                   1550:
                   1551: /*
                   1552:  * rcs_kflag_get()
                   1553:  *
                   1554:  * Get the keyword expansion mode from a set of character flags given in
                   1555:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1556:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1557:  */
                   1558: int
                   1559: rcs_kflag_get(const char *flags)
                   1560: {
                   1561:        int fl;
                   1562:        size_t len;
                   1563:        const char *fp;
                   1564:
                   1565:        fl = 0;
                   1566:        len = strlen(flags);
                   1567:
                   1568:        for (fp = flags; *fp != '\0'; fp++) {
                   1569:                if (*fp == 'k')
                   1570:                        fl |= RCS_KWEXP_NAME;
                   1571:                else if (*fp == 'v')
                   1572:                        fl |= RCS_KWEXP_VAL;
                   1573:                else if (*fp == 'l')
                   1574:                        fl |= RCS_KWEXP_LKR;
                   1575:                else if (*fp == 'o') {
                   1576:                        if (len != 1)
                   1577:                                fl |= RCS_KWEXP_ERR;
                   1578:                        fl |= RCS_KWEXP_OLD;
                   1579:                } else if (*fp == 'b') {
                   1580:                        if (len != 1)
                   1581:                                fl |= RCS_KWEXP_ERR;
                   1582:                } else  /* unknown letter */
                   1583:                        fl |= RCS_KWEXP_ERR;
                   1584:        }
                   1585:
                   1586:        return (fl);
                   1587: }
                   1588:
                   1589: /*
                   1590:  * rcs_errstr()
                   1591:  *
                   1592:  * Get the error string matching the RCS error code <code>.
                   1593:  */
                   1594: const char *
                   1595: rcs_errstr(int code)
                   1596: {
                   1597:        const char *esp;
                   1598:
                   1599:        if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
                   1600:                esp = NULL;
                   1601:        else if (code == RCS_ERR_ERRNO)
                   1602:                esp = strerror(errno);
                   1603:        else
                   1604:                esp = rcs_errstrs[code];
                   1605:        return (esp);
                   1606: }
                   1607:
                   1608: /* rcs_parse_deltas()
                   1609:  *
                   1610:  * Parse deltas. If <rev> is not NULL, parse only as far as that
                   1611:  * revision. If <rev> is NULL, parse all deltas.
                   1612:  */
                   1613: static void
                   1614: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
                   1615: {
                   1616:        int ret;
                   1617:        struct rcs_delta *enddelta;
                   1618:
                   1619:        if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
                   1620:                return;
                   1621:
                   1622:        for (;;) {
                   1623:                ret = rcs_parse_delta(rfp);
                   1624:                if (rev != NULL) {
                   1625:                        enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
                   1626:                        if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
                   1627:                                break;
                   1628:                }
                   1629:                if (ret == 0) {
                   1630:                        rfp->rf_flags |= PARSED_DELTAS;
                   1631:                        break;
                   1632:                }
                   1633:                else if (ret == -1)
                   1634:                        errx(1, "error parsing deltas");
                   1635:        }
                   1636: }
                   1637:
                   1638: /* rcs_parse_deltatexts()
                   1639:  *
                   1640:  * Parse deltatexts. If <rev> is not NULL, parse only as far as that
                   1641:  * revision. If <rev> is NULL, parse everything.
                   1642:  */
                   1643: static void
                   1644: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
                   1645: {
                   1646:        int ret;
                   1647:        struct rcs_delta *rdp;
                   1648:
                   1649:        if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
                   1650:            (rfp->rf_flags & RCS_CREATE))
                   1651:                return;
                   1652:
                   1653:        if (!(rfp->rf_flags & PARSED_DESC))
                   1654:                rcs_parse_desc(rfp, rev);
                   1655:        for (;;) {
                   1656:                if (rev != NULL) {
                   1657:                        rdp = rcs_findrev(rfp, rev);
                   1658:                        if (rdp->rd_text != NULL)
                   1659:                                break;
                   1660:                        else
                   1661:                                ret = rcs_parse_deltatext(rfp);
                   1662:                } else
                   1663:                        ret = rcs_parse_deltatext(rfp);
                   1664:                if (ret == 0) {
                   1665:                        rfp->rf_flags |= PARSED_DELTATEXTS;
                   1666:                        break;
                   1667:                }
                   1668:                else if (ret == -1)
                   1669:                        errx(1, "problem parsing deltatexts");
                   1670:        }
                   1671: }
                   1672:
                   1673: /* rcs_parse_desc()
                   1674:  *
                   1675:  * Parse RCS description.
                   1676:  */
                   1677: static void
                   1678: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
                   1679: {
                   1680:        int ret = 0;
                   1681:
                   1682:        if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
                   1683:                return;
                   1684:        if (!(rfp->rf_flags & PARSED_DELTAS))
                   1685:                rcs_parse_deltas(rfp, rev);
                   1686:        /* do parsing */
                   1687:        ret = rcs_gettok(rfp);
                   1688:        if (ret != RCS_TOK_DESC)
                   1689:                errx(1, "token `%s' found where RCS desc expected",
                   1690:                    RCS_TOKSTR(rfp));
                   1691:
                   1692:        ret = rcs_gettok(rfp);
                   1693:        if (ret != RCS_TOK_STRING)
                   1694:                errx(1, "token `%s' found where RCS desc expected",
                   1695:                    RCS_TOKSTR(rfp));
                   1696:
                   1697:        rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
                   1698:        rfp->rf_flags |= PARSED_DESC;
                   1699: }
                   1700:
                   1701: /*
                   1702:  * rcs_parse_init()
                   1703:  *
                   1704:  * Initial parsing of file <path>, which are in the RCS format.
                   1705:  * Just does admin section.
                   1706:  */
                   1707: static void
                   1708: rcs_parse_init(RCSFILE *rfp)
                   1709: {
                   1710:        struct rcs_pdata *pdp;
                   1711:
                   1712:        if (rfp->rf_flags & RCS_PARSED)
                   1713:                return;
                   1714:
                   1715:        pdp = xcalloc(1, sizeof(*pdp));
                   1716:
                   1717:        pdp->rp_lines = 0;
                   1718:        pdp->rp_pttype = RCS_TOK_ERR;
                   1719:
1.3       joris    1720:        if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
                   1721:                err(1, "fopen: `%s'", rfp->rf_path);
1.1       joris    1722:
                   1723:        pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
                   1724:        pdp->rp_blen = RCS_BUFSIZE;
                   1725:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
                   1726:
                   1727:        /* ditch the strict lock */
                   1728:        rfp->rf_flags &= ~RCS_SLOCK;
                   1729:        rfp->rf_pdata = pdp;
                   1730:
                   1731:        if (rcs_parse_admin(rfp) < 0) {
                   1732:                rcs_freepdata(pdp);
                   1733:                errx(1, "could not parse admin data");
                   1734:        }
                   1735:
                   1736:        if (rfp->rf_flags & RCS_PARSE_FULLY)
                   1737:                rcs_parse_deltatexts(rfp, NULL);
                   1738:
                   1739:        rfp->rf_flags |= RCS_SYNCED;
                   1740: }
                   1741:
                   1742: /*
                   1743:  * rcs_parse_admin()
                   1744:  *
                   1745:  * Parse the administrative portion of an RCS file.
                   1746:  * Returns the type of the first token found after the admin section on
                   1747:  * success, or -1 on failure.
                   1748:  */
                   1749: static int
                   1750: rcs_parse_admin(RCSFILE *rfp)
                   1751: {
                   1752:        u_int i;
                   1753:        int tok, ntok, hmask;
                   1754:        struct rcs_key *rk;
                   1755:
                   1756:        /* hmask is a mask of the headers already encountered */
                   1757:        hmask = 0;
                   1758:        for (;;) {
                   1759:                tok = rcs_gettok(rfp);
                   1760:                if (tok == RCS_TOK_ERR) {
                   1761:                        rcs_errno = RCS_ERR_PARSE;
                   1762:                        warnx("parse error in RCS admin section");
                   1763:                        goto fail;
                   1764:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
                   1765:                        /*
                   1766:                         * Assume this is the start of the first delta or
                   1767:                         * that we are dealing with an empty RCS file and
                   1768:                         * we just found the description.
                   1769:                         */
                   1770:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1771:                        return (tok);
                   1772:                }
                   1773:
                   1774:                rk = NULL;
                   1775:                for (i = 0; i < RCS_NKEYS; i++)
                   1776:                        if (rcs_keys[i].rk_id == tok)
                   1777:                                rk = &(rcs_keys[i]);
                   1778:
                   1779:                if (hmask & (1 << tok)) {
                   1780:                        rcs_errno = RCS_ERR_PARSE;
                   1781:                        warnx("duplicate RCS key");
                   1782:                        goto fail;
                   1783:                }
                   1784:                hmask |= (1 << tok);
                   1785:
                   1786:                switch (tok) {
                   1787:                case RCS_TOK_HEAD:
                   1788:                case RCS_TOK_BRANCH:
                   1789:                case RCS_TOK_COMMENT:
                   1790:                case RCS_TOK_EXPAND:
                   1791:                        ntok = rcs_gettok(rfp);
                   1792:                        if (ntok == RCS_TOK_SCOLON)
                   1793:                                break;
                   1794:                        if (ntok != rk->rk_val) {
                   1795:                                rcs_errno = RCS_ERR_PARSE;
                   1796:                                warnx("invalid value type for RCS key `%s'",
                   1797:                                    rk->rk_str);
                   1798:                        }
                   1799:
                   1800:                        if (tok == RCS_TOK_HEAD) {
                   1801:                                if (rfp->rf_head == NULL)
                   1802:                                        rfp->rf_head = rcsnum_alloc();
                   1803:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1804:                                    rfp->rf_head);
                   1805:                        } else if (tok == RCS_TOK_BRANCH) {
                   1806:                                if (rfp->rf_branch == NULL)
                   1807:                                        rfp->rf_branch = rcsnum_alloc();
                   1808:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1809:                                    rfp->rf_branch) < 0)
                   1810:                                        goto fail;
                   1811:                        } else if (tok == RCS_TOK_COMMENT) {
                   1812:                                rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
                   1813:                        } else if (tok == RCS_TOK_EXPAND) {
                   1814:                                rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
                   1815:                        }
                   1816:
                   1817:                        /* now get the expected semi-colon */
                   1818:                        ntok = rcs_gettok(rfp);
                   1819:                        if (ntok != RCS_TOK_SCOLON) {
                   1820:                                rcs_errno = RCS_ERR_PARSE;
                   1821:                                warnx("missing semi-colon after RCS `%s' key",
                   1822:                                    rk->rk_str);
                   1823:                                goto fail;
                   1824:                        }
                   1825:                        break;
                   1826:                case RCS_TOK_ACCESS:
                   1827:                        if (rcs_parse_access(rfp) < 0)
                   1828:                                goto fail;
                   1829:                        break;
                   1830:                case RCS_TOK_SYMBOLS:
                   1831:                        if (rcs_parse_symbols(rfp) < 0)
                   1832:                                goto fail;
                   1833:                        break;
                   1834:                case RCS_TOK_LOCKS:
                   1835:                        if (rcs_parse_locks(rfp) < 0)
                   1836:                                goto fail;
                   1837:                        break;
                   1838:                default:
                   1839:                        rcs_errno = RCS_ERR_PARSE;
                   1840:                        warnx("unexpected token `%s' in RCS admin section",
                   1841:                            RCS_TOKSTR(rfp));
                   1842:                        goto fail;
                   1843:                }
                   1844:        }
                   1845:
                   1846: fail:
                   1847:        return (-1);
                   1848: }
                   1849:
                   1850: /*
                   1851:  * rcs_parse_delta()
                   1852:  *
                   1853:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1854:  * information in the <rfp> delta list.
                   1855:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1856:  * -1 on error.
                   1857:  */
                   1858: static int
                   1859: rcs_parse_delta(RCSFILE *rfp)
                   1860: {
                   1861:        int ret, tok, ntok, hmask;
                   1862:        u_int i;
                   1863:        char *tokstr;
                   1864:        RCSNUM *datenum;
                   1865:        struct rcs_delta *rdp;
                   1866:        struct rcs_key *rk;
                   1867:
                   1868:        rdp = xcalloc(1, sizeof(*rdp));
                   1869:
                   1870:        rdp->rd_num = rcsnum_alloc();
                   1871:        rdp->rd_next = rcsnum_alloc();
                   1872:
                   1873:        TAILQ_INIT(&(rdp->rd_branches));
                   1874:
                   1875:        tok = rcs_gettok(rfp);
                   1876:        if (tok == RCS_TOK_DESC) {
                   1877:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1878:                return (0);
                   1879:        } else if (tok != RCS_TOK_NUM) {
                   1880:                rcs_errno = RCS_ERR_PARSE;
                   1881:                warnx("unexpected token `%s' at start of delta",
                   1882:                    RCS_TOKSTR(rfp));
                   1883:                rcs_freedelta(rdp);
                   1884:                return (-1);
                   1885:        }
                   1886:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1887:
                   1888:        hmask = 0;
                   1889:        ret = 0;
                   1890:        tokstr = NULL;
                   1891:
                   1892:        for (;;) {
                   1893:                tok = rcs_gettok(rfp);
                   1894:                if (tok == RCS_TOK_ERR) {
                   1895:                        rcs_errno = RCS_ERR_PARSE;
                   1896:                        warnx("parse error in RCS delta section");
                   1897:                        rcs_freedelta(rdp);
                   1898:                        return (-1);
                   1899:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
                   1900:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1901:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1902:                        break;
                   1903:                }
                   1904:
                   1905:                rk = NULL;
                   1906:                for (i = 0; i < RCS_NKEYS; i++)
                   1907:                        if (rcs_keys[i].rk_id == tok)
                   1908:                                rk = &(rcs_keys[i]);
                   1909:
                   1910:                if (hmask & (1 << tok)) {
                   1911:                        rcs_errno = RCS_ERR_PARSE;
                   1912:                        warnx("duplicate RCS key");
                   1913:                        rcs_freedelta(rdp);
                   1914:                        return (-1);
                   1915:                }
                   1916:                hmask |= (1 << tok);
                   1917:
                   1918:                switch (tok) {
                   1919:                case RCS_TOK_DATE:
                   1920:                case RCS_TOK_AUTHOR:
                   1921:                case RCS_TOK_STATE:
                   1922:                case RCS_TOK_NEXT:
                   1923:                        ntok = rcs_gettok(rfp);
                   1924:                        if (ntok == RCS_TOK_SCOLON) {
                   1925:                                if (rk->rk_flags & RCS_VOPT)
                   1926:                                        break;
                   1927:                                else {
                   1928:                                        rcs_errno = RCS_ERR_PARSE;
                   1929:                                        warnx("missing mandatory "
                   1930:                                            "value to RCS key `%s'",
                   1931:                                            rk->rk_str);
                   1932:                                        rcs_freedelta(rdp);
                   1933:                                        return (-1);
                   1934:                                }
                   1935:                        }
                   1936:
                   1937:                        if (ntok != rk->rk_val) {
                   1938:                                rcs_errno = RCS_ERR_PARSE;
                   1939:                                warnx("invalid value type for RCS key `%s'",
                   1940:                                    rk->rk_str);
                   1941:                                rcs_freedelta(rdp);
                   1942:                                return (-1);
                   1943:                        }
                   1944:
                   1945:                        if (tokstr != NULL)
                   1946:                                xfree(tokstr);
                   1947:                        tokstr = xstrdup(RCS_TOKSTR(rfp));
                   1948:                        /* now get the expected semi-colon */
                   1949:                        ntok = rcs_gettok(rfp);
                   1950:                        if (ntok != RCS_TOK_SCOLON) {
                   1951:                                rcs_errno = RCS_ERR_PARSE;
                   1952:                                warnx("missing semi-colon after RCS `%s' key",
                   1953:                                    rk->rk_str);
                   1954:                                xfree(tokstr);
                   1955:                                rcs_freedelta(rdp);
                   1956:                                return (-1);
                   1957:                        }
                   1958:
                   1959:                        if (tok == RCS_TOK_DATE) {
                   1960:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
                   1961:                                        xfree(tokstr);
                   1962:                                        rcs_freedelta(rdp);
                   1963:                                        return (-1);
                   1964:                                }
                   1965:                                if (datenum->rn_len != 6) {
                   1966:                                        rcs_errno = RCS_ERR_PARSE;
                   1967:                                        warnx("RCS date specification has %s "
                   1968:                                            "fields",
                   1969:                                            (datenum->rn_len > 6) ? "too many" :
                   1970:                                            "missing");
                   1971:                                        xfree(tokstr);
                   1972:                                        rcs_freedelta(rdp);
                   1973:                                        rcsnum_free(datenum);
                   1974:                                        return (-1);
                   1975:                                }
                   1976:                                rdp->rd_date.tm_year = datenum->rn_id[0];
                   1977:                                if (rdp->rd_date.tm_year >= 1900)
                   1978:                                        rdp->rd_date.tm_year -= 1900;
                   1979:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1980:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1981:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1982:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1983:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1984:                                rcsnum_free(datenum);
                   1985:                        } else if (tok == RCS_TOK_AUTHOR) {
                   1986:                                rdp->rd_author = tokstr;
                   1987:                                tokstr = NULL;
                   1988:                        } else if (tok == RCS_TOK_STATE) {
                   1989:                                rdp->rd_state = tokstr;
                   1990:                                tokstr = NULL;
                   1991:                        } else if (tok == RCS_TOK_NEXT) {
                   1992:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1993:                        }
                   1994:                        break;
                   1995:                case RCS_TOK_BRANCHES:
                   1996:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   1997:                                rcs_freedelta(rdp);
                   1998:                                return (-1);
                   1999:                        }
                   2000:                        break;
                   2001:                default:
                   2002:                        rcs_errno = RCS_ERR_PARSE;
                   2003:                        warnx("unexpected token `%s' in RCS delta",
                   2004:                            RCS_TOKSTR(rfp));
                   2005:                        rcs_freedelta(rdp);
                   2006:                        return (-1);
                   2007:                }
                   2008:        }
                   2009:
                   2010:        if (tokstr != NULL)
                   2011:                xfree(tokstr);
                   2012:
                   2013:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
                   2014:        rfp->rf_ndelta++;
                   2015:
                   2016:        return (ret);
                   2017: }
                   2018:
                   2019: /*
                   2020:  * rcs_parse_deltatext()
                   2021:  *
                   2022:  * Parse an RCS delta text section and fill in the log and text field of the
                   2023:  * appropriate delta section.
                   2024:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   2025:  * -1 on error.
                   2026:  */
                   2027: static int
                   2028: rcs_parse_deltatext(RCSFILE *rfp)
                   2029: {
                   2030:        int tok;
                   2031:        RCSNUM *tnum;
                   2032:        struct rcs_delta *rdp;
                   2033:
                   2034:        tok = rcs_gettok(rfp);
                   2035:        if (tok == RCS_TOK_EOF)
                   2036:                return (0);
                   2037:
                   2038:        if (tok != RCS_TOK_NUM) {
                   2039:                rcs_errno = RCS_ERR_PARSE;
                   2040:                warnx("unexpected token `%s' at start of RCS delta text",
                   2041:                    RCS_TOKSTR(rfp));
                   2042:                return (-1);
                   2043:        }
                   2044:
                   2045:        tnum = rcsnum_alloc();
                   2046:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   2047:
                   2048:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2049:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   2050:                        break;
                   2051:        }
                   2052:        rcsnum_free(tnum);
                   2053:
                   2054:        if (rdp == NULL) {
                   2055:                warnx("RCS delta text `%s' has no matching delta",
                   2056:                    RCS_TOKSTR(rfp));
                   2057:                return (-1);
                   2058:        }
                   2059:
                   2060:        tok = rcs_gettok(rfp);
                   2061:        if (tok != RCS_TOK_LOG) {
                   2062:                rcs_errno = RCS_ERR_PARSE;
                   2063:                warnx("unexpected token `%s' where RCS log expected",
                   2064:                    RCS_TOKSTR(rfp));
                   2065:                return (-1);
                   2066:        }
                   2067:
                   2068:        tok = rcs_gettok(rfp);
                   2069:        if (tok != RCS_TOK_STRING) {
                   2070:                rcs_errno = RCS_ERR_PARSE;
                   2071:                warnx("unexpected token `%s' where RCS log expected",
                   2072:                    RCS_TOKSTR(rfp));
                   2073:                return (-1);
                   2074:        }
                   2075:        rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
                   2076:        tok = rcs_gettok(rfp);
                   2077:        if (tok != RCS_TOK_TEXT) {
                   2078:                rcs_errno = RCS_ERR_PARSE;
                   2079:                warnx("unexpected token `%s' where RCS text expected",
                   2080:                    RCS_TOKSTR(rfp));
                   2081:                return (-1);
                   2082:        }
                   2083:
                   2084:        tok = rcs_gettok(rfp);
                   2085:        if (tok != RCS_TOK_STRING) {
                   2086:                rcs_errno = RCS_ERR_PARSE;
                   2087:                warnx("unexpected token `%s' where RCS text expected",
                   2088:                    RCS_TOKSTR(rfp));
                   2089:                return (-1);
                   2090:        }
                   2091:
                   2092:        rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.5     ! ray      2093:        if (strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1)) >=
        !          2094:            RCS_TOKLEN(rfp) + 1)
        !          2095:                errx(1, "rcs_parse_deltatext: strlcpy");
1.1       joris    2096:        rdp->rd_tlen = RCS_TOKLEN(rfp);
                   2097:
                   2098:        return (1);
                   2099: }
                   2100:
                   2101: /*
                   2102:  * rcs_parse_access()
                   2103:  *
                   2104:  * Parse the access list given as value to the `access' keyword.
                   2105:  * Returns 0 on success, or -1 on failure.
                   2106:  */
                   2107: static int
                   2108: rcs_parse_access(RCSFILE *rfp)
                   2109: {
                   2110:        int type;
                   2111:
                   2112:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2113:                if (type != RCS_TOK_ID) {
                   2114:                        rcs_errno = RCS_ERR_PARSE;
                   2115:                        warnx("unexpected token `%s' in access list",
                   2116:                            RCS_TOKSTR(rfp));
                   2117:                        return (-1);
                   2118:                }
                   2119:
                   2120:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2121:                        return (-1);
                   2122:        }
                   2123:
                   2124:        return (0);
                   2125: }
                   2126:
                   2127: /*
                   2128:  * rcs_parse_symbols()
                   2129:  *
                   2130:  * Parse the symbol list given as value to the `symbols' keyword.
                   2131:  * Returns 0 on success, or -1 on failure.
                   2132:  */
                   2133: static int
                   2134: rcs_parse_symbols(RCSFILE *rfp)
                   2135: {
                   2136:        int type;
                   2137:        struct rcs_sym *symp;
                   2138:
                   2139:        for (;;) {
                   2140:                type = rcs_gettok(rfp);
                   2141:                if (type == RCS_TOK_SCOLON)
                   2142:                        break;
                   2143:
                   2144:                if (type != RCS_TOK_ID) {
                   2145:                        rcs_errno = RCS_ERR_PARSE;
                   2146:                        warnx("unexpected token `%s' in symbol list",
                   2147:                            RCS_TOKSTR(rfp));
                   2148:                        return (-1);
                   2149:                }
                   2150:
                   2151:                symp = xmalloc(sizeof(*symp));
                   2152:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
                   2153:                symp->rs_num = rcsnum_alloc();
                   2154:
                   2155:                type = rcs_gettok(rfp);
                   2156:                if (type != RCS_TOK_COLON) {
                   2157:                        rcs_errno = RCS_ERR_PARSE;
                   2158:                        warnx("unexpected token `%s' in symbol list",
                   2159:                            RCS_TOKSTR(rfp));
                   2160:                        rcsnum_free(symp->rs_num);
                   2161:                        xfree(symp->rs_name);
                   2162:                        xfree(symp);
                   2163:                        return (-1);
                   2164:                }
                   2165:
                   2166:                type = rcs_gettok(rfp);
                   2167:                if (type != RCS_TOK_NUM) {
                   2168:                        rcs_errno = RCS_ERR_PARSE;
                   2169:                        warnx("unexpected token `%s' in symbol list",
                   2170:                            RCS_TOKSTR(rfp));
                   2171:                        rcsnum_free(symp->rs_num);
                   2172:                        xfree(symp->rs_name);
                   2173:                        xfree(symp);
                   2174:                        return (-1);
                   2175:                }
                   2176:
                   2177:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2178:                        warnx("failed to parse RCS NUM `%s'",
                   2179:                            RCS_TOKSTR(rfp));
                   2180:                        rcsnum_free(symp->rs_num);
                   2181:                        xfree(symp->rs_name);
                   2182:                        xfree(symp);
                   2183:                        return (-1);
                   2184:                }
                   2185:
                   2186:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
                   2187:        }
                   2188:
                   2189:        return (0);
                   2190: }
                   2191:
                   2192: /*
                   2193:  * rcs_parse_locks()
                   2194:  *
                   2195:  * Parse the lock list given as value to the `locks' keyword.
                   2196:  * Returns 0 on success, or -1 on failure.
                   2197:  */
                   2198: static int
                   2199: rcs_parse_locks(RCSFILE *rfp)
                   2200: {
                   2201:        int type;
                   2202:        struct rcs_lock *lkp;
                   2203:
                   2204:        for (;;) {
                   2205:                type = rcs_gettok(rfp);
                   2206:                if (type == RCS_TOK_SCOLON)
                   2207:                        break;
                   2208:
                   2209:                if (type != RCS_TOK_ID) {
                   2210:                        rcs_errno = RCS_ERR_PARSE;
                   2211:                        warnx("unexpected token `%s' in lock list",
                   2212:                            RCS_TOKSTR(rfp));
                   2213:                        return (-1);
                   2214:                }
                   2215:
                   2216:                lkp = xmalloc(sizeof(*lkp));
                   2217:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
                   2218:                lkp->rl_num = rcsnum_alloc();
                   2219:
                   2220:                type = rcs_gettok(rfp);
                   2221:                if (type != RCS_TOK_COLON) {
                   2222:                        rcs_errno = RCS_ERR_PARSE;
                   2223:                        warnx("unexpected token `%s' in symbol list",
                   2224:                            RCS_TOKSTR(rfp));
                   2225:                        rcsnum_free(lkp->rl_num);
                   2226:                        xfree(lkp->rl_name);
                   2227:                        xfree(lkp);
                   2228:                        return (-1);
                   2229:                }
                   2230:
                   2231:                type = rcs_gettok(rfp);
                   2232:                if (type != RCS_TOK_NUM) {
                   2233:                        rcs_errno = RCS_ERR_PARSE;
                   2234:                        warnx("unexpected token `%s' in symbol list",
                   2235:                            RCS_TOKSTR(rfp));
                   2236:                        rcsnum_free(lkp->rl_num);
                   2237:                        xfree(lkp->rl_name);
                   2238:                        xfree(lkp);
                   2239:                        return (-1);
                   2240:                }
                   2241:
                   2242:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2243:                        warnx("failed to parse RCS NUM `%s'",
                   2244:                            RCS_TOKSTR(rfp));
                   2245:                        rcsnum_free(lkp->rl_num);
                   2246:                        xfree(lkp->rl_name);
                   2247:                        xfree(lkp);
                   2248:                        return (-1);
                   2249:                }
                   2250:
                   2251:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2252:        }
                   2253:
                   2254:        /* check if we have a `strict' */
                   2255:        type = rcs_gettok(rfp);
                   2256:        if (type != RCS_TOK_STRICT) {
                   2257:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
                   2258:        } else {
                   2259:                rfp->rf_flags |= RCS_SLOCK;
                   2260:
                   2261:                type = rcs_gettok(rfp);
                   2262:                if (type != RCS_TOK_SCOLON) {
                   2263:                        rcs_errno = RCS_ERR_PARSE;
                   2264:                        warnx("missing semi-colon after `strict' keyword");
                   2265:                        return (-1);
                   2266:                }
                   2267:        }
                   2268:
                   2269:        return (0);
                   2270: }
                   2271:
                   2272: /*
                   2273:  * rcs_parse_branches()
                   2274:  *
                   2275:  * Parse the list of branches following a `branches' keyword in a delta.
                   2276:  * Returns 0 on success, or -1 on failure.
                   2277:  */
                   2278: static int
                   2279: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2280: {
                   2281:        int type;
                   2282:        struct rcs_branch *brp;
                   2283:
                   2284:        for (;;) {
                   2285:                type = rcs_gettok(rfp);
                   2286:                if (type == RCS_TOK_SCOLON)
                   2287:                        break;
                   2288:
                   2289:                if (type != RCS_TOK_NUM) {
                   2290:                        rcs_errno = RCS_ERR_PARSE;
                   2291:                        warnx("unexpected token `%s' in list of branches",
                   2292:                            RCS_TOKSTR(rfp));
                   2293:                        return (-1);
                   2294:                }
                   2295:
                   2296:                brp = xmalloc(sizeof(*brp));
                   2297:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
                   2298:                if (brp->rb_num == NULL) {
                   2299:                        xfree(brp);
                   2300:                        return (-1);
                   2301:                }
                   2302:
                   2303:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2304:        }
                   2305:
                   2306:        return (0);
                   2307: }
                   2308:
                   2309: /*
                   2310:  * rcs_freedelta()
                   2311:  *
                   2312:  * Free the contents of a delta structure.
                   2313:  */
                   2314: static void
                   2315: rcs_freedelta(struct rcs_delta *rdp)
                   2316: {
                   2317:        struct rcs_branch *rb;
                   2318:
                   2319:        if (rdp->rd_num != NULL)
                   2320:                rcsnum_free(rdp->rd_num);
                   2321:        if (rdp->rd_next != NULL)
                   2322:                rcsnum_free(rdp->rd_next);
                   2323:
                   2324:        if (rdp->rd_author != NULL)
                   2325:                xfree(rdp->rd_author);
                   2326:        if (rdp->rd_locker != NULL)
                   2327:                xfree(rdp->rd_locker);
                   2328:        if (rdp->rd_state != NULL)
                   2329:                xfree(rdp->rd_state);
                   2330:        if (rdp->rd_log != NULL)
                   2331:                xfree(rdp->rd_log);
                   2332:        if (rdp->rd_text != NULL)
                   2333:                xfree(rdp->rd_text);
                   2334:
                   2335:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2336:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2337:                rcsnum_free(rb->rb_num);
                   2338:                xfree(rb);
                   2339:        }
                   2340:
                   2341:        xfree(rdp);
                   2342: }
                   2343:
                   2344: /*
                   2345:  * rcs_freepdata()
                   2346:  *
                   2347:  * Free the contents of the parser data structure.
                   2348:  */
                   2349: static void
                   2350: rcs_freepdata(struct rcs_pdata *pd)
                   2351: {
                   2352:        if (pd->rp_file != NULL)
                   2353:                (void)fclose(pd->rp_file);
                   2354:        if (pd->rp_buf != NULL)
                   2355:                xfree(pd->rp_buf);
                   2356:        xfree(pd);
                   2357: }
                   2358:
                   2359: /*
                   2360:  * rcs_gettok()
                   2361:  *
                   2362:  * Get the next RCS token from the string <str>.
                   2363:  */
                   2364: static int
                   2365: rcs_gettok(RCSFILE *rfp)
                   2366: {
                   2367:        u_int i;
                   2368:        int ch, last, type;
                   2369:        size_t len;
                   2370:        char *bp;
                   2371:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2372:
                   2373:        type = RCS_TOK_ERR;
                   2374:        bp = pdp->rp_buf;
                   2375:        pdp->rp_tlen = 0;
                   2376:        *bp = '\0';
                   2377:
                   2378:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2379:                type = pdp->rp_pttype;
1.5     ! ray      2380:                if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
        !          2381:                    pdp->rp_blen)
        !          2382:                        errx(1, "rcs_gettok: strlcpy");
1.1       joris    2383:                pdp->rp_pttype = RCS_TOK_ERR;
                   2384:                return (type);
                   2385:        }
                   2386:
                   2387:        /* skip leading whitespace */
                   2388:        /* XXX we must skip backspace too for compatibility, should we? */
                   2389:        do {
                   2390:                ch = getc(pdp->rp_file);
                   2391:                if (ch == '\n')
                   2392:                        pdp->rp_lines++;
                   2393:        } while (isspace(ch));
                   2394:
                   2395:        if (ch == EOF) {
                   2396:                type = RCS_TOK_EOF;
                   2397:        } else if (ch == ';') {
                   2398:                type = RCS_TOK_SCOLON;
                   2399:        } else if (ch == ':') {
                   2400:                type = RCS_TOK_COLON;
                   2401:        } else if (isalpha(ch)) {
                   2402:                type = RCS_TOK_ID;
                   2403:                *(bp++) = ch;
                   2404:                for (;;) {
                   2405:                        ch = getc(pdp->rp_file);
                   2406:                        if (!isalnum(ch) && ch != '_' && ch != '-' &&
                   2407:                            ch != '/') {
                   2408:                                ungetc(ch, pdp->rp_file);
                   2409:                                break;
                   2410:                        }
                   2411:                        *(bp++) = ch;
                   2412:                        pdp->rp_tlen++;
                   2413:                        if (bp == pdp->rp_bufend - 1) {
                   2414:                                len = bp - pdp->rp_buf;
                   2415:                                rcs_growbuf(rfp);
                   2416:                                bp = pdp->rp_buf + len;
                   2417:                        }
                   2418:                }
                   2419:                *bp = '\0';
                   2420:
                   2421:                if (type != RCS_TOK_ERR) {
                   2422:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2423:                                if (strcmp(rcs_keys[i].rk_str,
                   2424:                                    pdp->rp_buf) == 0) {
                   2425:                                        type = rcs_keys[i].rk_id;
                   2426:                                        break;
                   2427:                                }
                   2428:                        }
                   2429:                }
                   2430:        } else if (ch == '@') {
                   2431:                /* we have a string */
                   2432:                type = RCS_TOK_STRING;
                   2433:                for (;;) {
                   2434:                        ch = getc(pdp->rp_file);
                   2435:                        if (ch == '@') {
                   2436:                                ch = getc(pdp->rp_file);
                   2437:                                if (ch != '@') {
                   2438:                                        ungetc(ch, pdp->rp_file);
                   2439:                                        break;
                   2440:                                }
                   2441:                        } else if (ch == '\n')
                   2442:                                pdp->rp_lines++;
                   2443:
                   2444:                        *(bp++) = ch;
                   2445:                        pdp->rp_tlen++;
                   2446:                        if (bp == pdp->rp_bufend - 1) {
                   2447:                                len = bp - pdp->rp_buf;
                   2448:                                rcs_growbuf(rfp);
                   2449:                                bp = pdp->rp_buf + len;
                   2450:                        }
                   2451:                }
                   2452:
                   2453:                *bp = '\0';
                   2454:        } else if (isdigit(ch)) {
                   2455:                *(bp++) = ch;
                   2456:                last = ch;
                   2457:                type = RCS_TOK_NUM;
                   2458:
                   2459:                for (;;) {
                   2460:                        ch = getc(pdp->rp_file);
                   2461:                        if (bp == pdp->rp_bufend)
                   2462:                                break;
                   2463:                        if (!isdigit(ch) && ch != '.') {
                   2464:                                ungetc(ch, pdp->rp_file);
                   2465:                                break;
                   2466:                        }
                   2467:
                   2468:                        if (last == '.' && ch == '.') {
                   2469:                                type = RCS_TOK_ERR;
                   2470:                                break;
                   2471:                        }
                   2472:                        last = ch;
                   2473:                        *(bp++) = ch;
                   2474:                        pdp->rp_tlen++;
                   2475:                }
                   2476:                *bp = '\0';
                   2477:        }
                   2478:
                   2479:        return (type);
                   2480: }
                   2481:
                   2482: /*
                   2483:  * rcs_pushtok()
                   2484:  *
                   2485:  * Push a token back in the parser's token buffer.
                   2486:  */
                   2487: static int
                   2488: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2489: {
                   2490:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2491:
                   2492:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2493:                return (-1);
                   2494:
                   2495:        pdp->rp_pttype = type;
1.5     ! ray      2496:        if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
        !          2497:            sizeof(pdp->rp_ptok))
        !          2498:                errx(1, "rcs_pushtok: strlcpy");
1.1       joris    2499:        return (0);
                   2500: }
                   2501:
                   2502:
                   2503: /*
                   2504:  * rcs_growbuf()
                   2505:  *
                   2506:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2507:  * RCS_BUFEXTSIZE.
                   2508:  * In case of failure, the original buffer is left unmodified.
                   2509:  */
                   2510: static void
                   2511: rcs_growbuf(RCSFILE *rf)
                   2512: {
                   2513:        void *tmp;
                   2514:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2515:
                   2516:        tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
                   2517:        pdp->rp_buf = tmp;
                   2518:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2519:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
                   2520: }
                   2521:
                   2522: /*
                   2523:  * rcs_strprint()
                   2524:  *
                   2525:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2526:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2527:  * binary data.
                   2528:  */
                   2529: static void
                   2530: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2531: {
                   2532:        const u_char *ap, *ep, *sp;
                   2533:
                   2534:        if (slen == 0)
                   2535:                return;
                   2536:
                   2537:        ep = str + slen - 1;
                   2538:
                   2539:        for (sp = str; sp <= ep;)  {
                   2540:                ap = memchr(sp, '@', ep - sp);
                   2541:                if (ap == NULL)
                   2542:                        ap = ep;
                   2543:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
                   2544:
                   2545:                if (*ap == '@')
                   2546:                        putc('@', stream);
                   2547:                sp = ap + 1;
                   2548:        }
                   2549: }
                   2550:
                   2551: /*
                   2552:  * rcs_expand_keywords()
                   2553:  *
                   2554:  * Return expansion any RCS keywords in <data>
                   2555:  *
                   2556:  * On error, return NULL.
                   2557:  */
                   2558: static char *
                   2559: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
                   2560:     size_t len, int mode)
                   2561: {
                   2562:        ptrdiff_t c_offset, sizdiff, start_offset;
                   2563:        size_t i;
                   2564:        int kwtype;
                   2565:        u_int j, found;
                   2566:        char *c, *kwstr, *start, *end, *tbuf;
                   2567:        char expbuf[256], buf[256];
                   2568:        struct tm tb;
                   2569:        char *fmt;
                   2570:
                   2571:        kwtype = 0;
                   2572:        kwstr = NULL;
                   2573:        i = 0;
                   2574:
                   2575:        /*
                   2576:         * -z support for RCS
                   2577:         */
                   2578:        tb = rdp->rd_date;
                   2579:        if (timezone_flag != NULL)
                   2580:                rcs_set_tz(timezone_flag, rdp, &tb);
                   2581:
                   2582:        /*
                   2583:         * Keyword formats:
                   2584:         * $Keyword$
                   2585:         * $Keyword: value$
                   2586:         */
                   2587:        for (c = data; *c != '\0' && i < len; c++) {
                   2588:                if (*c == '$') {
                   2589:                        /* remember start of this possible keyword */
                   2590:                        start = c;
                   2591:                        start_offset = start - data;
                   2592:
                   2593:                        /* first following character has to be alphanumeric */
                   2594:                        c++;
                   2595:                        if (!isalpha(*c)) {
                   2596:                                c = start;
                   2597:                                continue;
                   2598:                        }
                   2599:
                   2600:                        /* look for any matching keywords */
                   2601:                        found = 0;
                   2602:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2603:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2604:                                    strlen(rcs_expkw[j].kw_str))) {
                   2605:                                        found = 1;
                   2606:                                        kwstr = rcs_expkw[j].kw_str;
                   2607:                                        kwtype = rcs_expkw[j].kw_type;
                   2608:                                        break;
                   2609:                                }
                   2610:                        }
                   2611:
                   2612:                        /* unknown keyword, continue looking */
                   2613:                        if (found == 0) {
                   2614:                                c = start;
                   2615:                                continue;
                   2616:                        }
                   2617:
                   2618:                        /* next character has to be ':' or '$' */
                   2619:                        c += strlen(kwstr);
                   2620:                        if (*c != ':' && *c != '$') {
                   2621:                                c = start;
                   2622:                                continue;
                   2623:                        }
                   2624:
                   2625:                        /*
                   2626:                         * if the next character was ':' we need to look for
                   2627:                         * an '$' before the end of the line to be sure it is
                   2628:                         * in fact a keyword.
                   2629:                         */
                   2630:                        if (*c == ':') {
                   2631:                                while (*c++) {
                   2632:                                        if (*c == '$' || *c == '\n')
                   2633:                                                break;
                   2634:                                }
                   2635:
                   2636:                                if (*c != '$') {
                   2637:                                        c = start;
                   2638:                                        continue;
                   2639:                                }
                   2640:                        }
                   2641:                        c_offset = c - data;
                   2642:                        end = c + 1;
                   2643:
                   2644:                        /* start constructing the expansion */
                   2645:                        expbuf[0] = '\0';
                   2646:
                   2647:                        if (mode & RCS_KWEXP_NAME) {
1.5     ! ray      2648:                                if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf) ||
        !          2649:                                    strlcat(expbuf, kwstr, sizeof(expbuf)) >= sizeof(expbuf))
        !          2650:                                        errx(1, "rcs_expand_keywords: string truncated");
        !          2651:                                if ((mode & RCS_KWEXP_VAL) &&
        !          2652:                                    strlcat(expbuf, ": ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2653:                                        errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2654:                        }
                   2655:
                   2656:                        /*
                   2657:                         * order matters because of RCS_KW_ID and
                   2658:                         * RCS_KW_HEADER here
                   2659:                         */
                   2660:                        if (mode & RCS_KWEXP_VAL) {
                   2661:                                if (kwtype & RCS_KW_RCSFILE) {
                   2662:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.5     ! ray      2663:                                                (void)strlcat(expbuf, basename(rcsfile), sizeof(expbuf));
1.1       joris    2664:                                        else
1.5     ! ray      2665:                                                (void)strlcat(expbuf, rcsfile, sizeof(expbuf));
        !          2666:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2667:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2668:                                }
                   2669:
                   2670:                                if (kwtype & RCS_KW_REVISION) {
1.5     ! ray      2671:                                        rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
        !          2672:                                        if (strlcat(buf, " ", sizeof(buf)) >= sizeof(buf) ||
        !          2673:                                            strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(buf))
        !          2674:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2675:                                }
                   2676:
                   2677:                                if (kwtype & RCS_KW_DATE) {
                   2678:                                        if (timezone_flag != NULL)
                   2679:                                                fmt = "%Y/%m/%d %H:%M:%S%z ";
                   2680:                                        else
                   2681:                                                fmt = "%Y/%m/%d %H:%M:%S ";
                   2682:
                   2683:                                        strftime(buf, sizeof(buf), fmt, &tb);
1.5     ! ray      2684:                                        if (strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(expbuf))
        !          2685:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2686:                                }
                   2687:
                   2688:                                if (kwtype & RCS_KW_AUTHOR) {
1.5     ! ray      2689:                                        if (strlcat(expbuf, rdp->rd_author, sizeof(expbuf)) >= sizeof(expbuf) ||
        !          2690:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2691:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2692:                                }
                   2693:
                   2694:                                if (kwtype & RCS_KW_STATE) {
1.5     ! ray      2695:                                        if (strlcat(expbuf, rdp->rd_state, sizeof(expbuf)) >= sizeof(expbuf) ||
        !          2696:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2697:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2698:                                }
                   2699:
                   2700:                                /* order does not matter anymore below */
                   2701:                                if (kwtype & RCS_KW_LOG)
1.5     ! ray      2702:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2703:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2704:
                   2705:                                if (kwtype & RCS_KW_SOURCE) {
1.5     ! ray      2706:                                        if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >= sizeof(expbuf) ||
        !          2707:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2708:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2709:                                }
                   2710:
                   2711:                                if (kwtype & RCS_KW_NAME)
1.5     ! ray      2712:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
        !          2713:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2714:                        }
                   2715:
                   2716:                        /* end the expansion */
                   2717:                        if (mode & RCS_KWEXP_NAME)
1.5     ! ray      2718:                                if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf))
        !          2719:                                        errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2720:
                   2721:                        sizdiff = strlen(expbuf) - (end - start);
                   2722:                        tbuf = xstrdup(end);
                   2723:                        /* only realloc if we have to */
                   2724:                        if (sizdiff > 0) {
                   2725:                                char *newdata;
                   2726:
                   2727:                                len += sizdiff;
                   2728:                                newdata = xrealloc(data, 1, len);
                   2729:                                data = newdata;
                   2730:                                /*
                   2731:                                 * ensure string pointers are not invalidated
                   2732:                                 * after realloc()
                   2733:                                 */
                   2734:                                start = data + start_offset;
                   2735:                                c = data + c_offset;
                   2736:                        }
1.5     ! ray      2737:                        if (strlcpy(start, expbuf, len) >= len ||
        !          2738:                            strlcat(data, tbuf, len) >= len)
        !          2739:                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2740:                        xfree(tbuf);
                   2741:                        i += strlen(expbuf);
                   2742:                }
                   2743:        }
                   2744:
                   2745:        return (data);
                   2746: }
                   2747:
                   2748: /*
                   2749:  * rcs_deltatext_set()
                   2750:  *
                   2751:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
                   2752:  * Returns -1 on error, 0 on success.
                   2753:  */
                   2754: int
                   2755: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2756: {
                   2757:        size_t len;
                   2758:        struct rcs_delta *rdp;
                   2759:
                   2760:        /* Write operations require full parsing */
                   2761:        rcs_parse_deltatexts(rfp, NULL);
                   2762:
                   2763:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2764:                return (-1);
                   2765:
                   2766:        if (rdp->rd_text != NULL)
                   2767:                xfree(rdp->rd_text);
                   2768:
                   2769:        len = strlen(dtext);
                   2770:        if (len != 0) {
                   2771:                /* XXX - use xstrdup() if rd_text changes to char *. */
                   2772:                rdp->rd_text = xmalloc(len + 1);
                   2773:                rdp->rd_tlen = len;
                   2774:                (void)memcpy(rdp->rd_text, dtext, len + 1);
                   2775:        } else {
                   2776:                rdp->rd_text = NULL;
                   2777:                rdp->rd_tlen = 0;
                   2778:        }
                   2779:
                   2780:        return (0);
                   2781: }
                   2782:
                   2783: /*
                   2784:  * rcs_rev_setlog()
                   2785:  *
                   2786:  * Sets the log message of revision <rev> to <logtext>
                   2787:  */
                   2788: int
                   2789: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2790: {
                   2791:        struct rcs_delta *rdp;
                   2792:        char buf[16];
                   2793:
                   2794:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2795:
                   2796:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2797:                return (-1);
                   2798:
                   2799:        if (rdp->rd_log != NULL)
                   2800:                xfree(rdp->rd_log);
                   2801:
                   2802:        rdp->rd_log = xstrdup(logtext);
                   2803:        rfp->rf_flags &= ~RCS_SYNCED;
                   2804:        return (0);
                   2805: }
                   2806: /*
                   2807:  * rcs_rev_getdate()
                   2808:  *
                   2809:  * Get the date corresponding to a given revision.
                   2810:  * Returns the date on success, -1 on failure.
                   2811:  */
                   2812: time_t
                   2813: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2814: {
                   2815:        struct rcs_delta *rdp;
                   2816:
                   2817:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2818:                return (-1);
                   2819:
                   2820:        return (mktime(&rdp->rd_date));
                   2821: }
                   2822:
                   2823: /*
                   2824:  * rcs_state_set()
                   2825:  *
                   2826:  * Sets the state of revision <rev> to <state>
                   2827:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2828:  *
                   2829:  * Returns -1 on failure, 0 on success.
                   2830:  */
                   2831: int
                   2832: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2833: {
                   2834:        struct rcs_delta *rdp;
                   2835:
                   2836:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2837:                return (-1);
                   2838:
                   2839:        if (rdp->rd_state != NULL)
                   2840:                xfree(rdp->rd_state);
                   2841:
                   2842:        rdp->rd_state = xstrdup(state);
                   2843:
                   2844:        rfp->rf_flags &= ~RCS_SYNCED;
                   2845:
                   2846:        return (0);
                   2847: }
                   2848:
                   2849: /*
                   2850:  * rcs_state_check()
                   2851:  *
                   2852:  * Check if string <state> is valid.
                   2853:  *
                   2854:  * Returns 0 if the string is valid, -1 otherwise.
                   2855:  */
                   2856: int
                   2857: rcs_state_check(const char *state)
                   2858: {
                   2859:        if (strchr(state, ' ') != NULL)
                   2860:                return (-1);
                   2861:
                   2862:        return (0);
                   2863: }
                   2864:
                   2865: /*
                   2866:  * rcs_state_get()
                   2867:  *
                   2868:  * Get the state for a given revision of a specified RCSFILE.
                   2869:  *
                   2870:  * Returns NULL on failure.
                   2871:  */
                   2872: const char *
                   2873: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2874: {
                   2875:        struct rcs_delta *rdp;
                   2876:
                   2877:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2878:                return (NULL);
                   2879:
                   2880:        return (rdp->rd_state);
                   2881: }
                   2882:
                   2883: /*
                   2884:  * rcs_kwexp_buf()
                   2885:  *
                   2886:  * Do keyword expansion on a buffer if necessary
                   2887:  *
                   2888:  */
                   2889: BUF *
                   2890: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2891: {
                   2892:        struct rcs_delta *rdp;
                   2893:        char *expanded, *tbuf;
                   2894:        int expmode;
                   2895:        size_t len;
                   2896:
                   2897:        /*
                   2898:         * Do keyword expansion if required.
                   2899:         */
                   2900:        if (rf->rf_expand != NULL)
                   2901:                expmode = rcs_kwexp_get(rf);
                   2902:        else
                   2903:                expmode = RCS_KWEXP_DEFAULT;
                   2904:
                   2905:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2906:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.5     ! ray      2907:                        errx(1, "could not fetch revision");
1.1       joris    2908:                rcs_buf_putc(bp, '\0');
                   2909:                len = rcs_buf_len(bp);
                   2910:                tbuf = rcs_buf_release(bp);
                   2911:                expanded = rcs_expand_keywords(rf->rf_path, rdp,
                   2912:                    tbuf, len, expmode);
                   2913:                bp = rcs_buf_alloc(len, BUF_AUTOEXT);
                   2914:                rcs_buf_set(bp, expanded, strlen(expanded), 0);
                   2915:                xfree(expanded);
                   2916:        }
                   2917:        return (bp);
                   2918: }