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

1.16    ! niallo      1: /*     $OpenBSD: rcs.c,v 1.15 2006/06/04 02:56:53 niallo 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 *);
1.9       xsa       217: static int     rcs_movefile(char *, char *, mode_t, u_int);
1.1       joris     218: static void    rcs_parse_init(RCSFILE *);
                    219: static int     rcs_parse_admin(RCSFILE *);
                    220: static int     rcs_parse_delta(RCSFILE *);
                    221: static void    rcs_parse_deltas(RCSFILE *, RCSNUM *);
                    222: static int     rcs_parse_deltatext(RCSFILE *);
                    223: static void    rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
                    224: static void    rcs_parse_desc(RCSFILE *, RCSNUM *);
                    225:
                    226: static int     rcs_parse_access(RCSFILE *);
                    227: static int     rcs_parse_symbols(RCSFILE *);
                    228: static int     rcs_parse_locks(RCSFILE *);
                    229: static int     rcs_parse_branches(RCSFILE *, struct rcs_delta *);
                    230: static void    rcs_freedelta(struct rcs_delta *);
                    231: static void    rcs_freepdata(struct rcs_pdata *);
                    232: static int     rcs_gettok(RCSFILE *);
                    233: static int     rcs_pushtok(RCSFILE *, const char *, int);
                    234: static void    rcs_growbuf(RCSFILE *);
                    235: static void    rcs_strprint(const u_char *, size_t, FILE *);
                    236:
1.12      niallo    237: static BUF     *rcs_expand_keywords(char *, struct rcs_delta *, BUF *, int);
1.1       joris     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.10      ray       264:        rfp->rf_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:
1.11      ray       271:        if (!(rfp->rf_flags & RCS_CREATE)) {
1.1       joris     272:                rcs_parse_init(rfp);
                    273:
1.11      ray       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);
1.1       joris     282:                }
                    283:        }
                    284:
                    285:        return (rfp);
                    286: }
                    287:
                    288: /*
                    289:  * rcs_close()
                    290:  *
                    291:  * Close an RCS file handle.
                    292:  */
                    293: void
                    294: rcs_close(RCSFILE *rfp)
                    295: {
                    296:        struct rcs_delta *rdp;
                    297:        struct rcs_access *rap;
                    298:        struct rcs_lock *rlp;
                    299:        struct rcs_sym *rsp;
                    300:
                    301:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    302:                rcs_write(rfp);
                    303:
                    304:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    305:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    306:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    307:                rcs_freedelta(rdp);
                    308:        }
                    309:
                    310:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    311:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    312:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
                    313:                xfree(rap->ra_name);
                    314:                xfree(rap);
                    315:        }
                    316:
                    317:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    318:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    319:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    320:                rcsnum_free(rsp->rs_num);
                    321:                xfree(rsp->rs_name);
                    322:                xfree(rsp);
                    323:        }
                    324:
                    325:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    326:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    327:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    328:                rcsnum_free(rlp->rl_num);
                    329:                xfree(rlp->rl_name);
                    330:                xfree(rlp);
                    331:        }
                    332:
                    333:        if (rfp->rf_head != NULL)
                    334:                rcsnum_free(rfp->rf_head);
                    335:        if (rfp->rf_branch != NULL)
                    336:                rcsnum_free(rfp->rf_branch);
                    337:
                    338:        if (rfp->rf_path != NULL)
                    339:                xfree(rfp->rf_path);
                    340:        if (rfp->rf_comment != NULL)
                    341:                xfree(rfp->rf_comment);
                    342:        if (rfp->rf_expand != NULL)
                    343:                xfree(rfp->rf_expand);
                    344:        if (rfp->rf_desc != NULL)
                    345:                xfree(rfp->rf_desc);
                    346:        if (rfp->rf_pdata != NULL)
                    347:                rcs_freepdata(rfp->rf_pdata);
                    348:        xfree(rfp);
                    349: }
                    350:
                    351: /*
                    352:  * rcs_write()
                    353:  *
                    354:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    355:  * path is in <rf_path>.
                    356:  */
1.9       xsa       357: void
1.1       joris     358: rcs_write(RCSFILE *rfp)
                    359: {
                    360:        FILE *fp;
1.7       xsa       361:        char buf[1024], numbuf[64], *fn;
1.1       joris     362:        struct rcs_access *ap;
                    363:        struct rcs_sym *symp;
                    364:        struct rcs_branch *brp;
                    365:        struct rcs_delta *rdp;
                    366:        struct rcs_lock *lkp;
                    367:        size_t len;
1.9       xsa       368:        int fd, from_fd, to_fd;
1.1       joris     369:
                    370:        from_fd = to_fd = fd = -1;
                    371:
                    372:        if (rfp->rf_flags & RCS_SYNCED)
1.9       xsa       373:                return;
1.1       joris     374:
                    375:        /* Write operations need the whole file parsed */
                    376:        rcs_parse_deltatexts(rfp, NULL);
                    377:
1.7       xsa       378:        (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", rcs_tmpdir);
                    379:
1.1       joris     380:        if ((fd = mkstemp(fn)) == -1)
1.2       xsa       381:                err(1, "%s", fn);
1.1       joris     382:
                    383:        if ((fp = fdopen(fd, "w+")) == NULL) {
1.5       ray       384:                int saved_errno;
                    385:
                    386:                saved_errno = errno;
1.2       xsa       387:                (void)unlink(fn);
1.5       ray       388:                errno = saved_errno;
1.2       xsa       389:                err(1, "%s", fn);
1.1       joris     390:        }
                    391:
                    392:        if (rfp->rf_head != NULL)
                    393:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    394:        else
                    395:                numbuf[0] = '\0';
                    396:
                    397:        fprintf(fp, "head\t%s;\n", numbuf);
                    398:
                    399:        if (rfp->rf_branch != NULL) {
                    400:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    401:                fprintf(fp, "branch\t%s;\n", numbuf);
                    402:        }
                    403:
                    404:        fputs("access", fp);
                    405:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    406:                fprintf(fp, "\n\t%s", ap->ra_name);
                    407:        }
                    408:        fputs(";\n", fp);
                    409:
                    410:        fprintf(fp, "symbols");
                    411:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    412:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.5       ray       413:                if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
                    414:                    strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
                    415:                    strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
                    416:                        errx(1, "rcs_write: string overflow");
1.1       joris     417:                fprintf(fp, "\n\t%s", buf);
                    418:        }
                    419:        fprintf(fp, ";\n");
                    420:
                    421:        fprintf(fp, "locks");
                    422:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    423:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    424:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    425:        }
                    426:
                    427:        fprintf(fp, ";");
                    428:
                    429:        if (rfp->rf_flags & RCS_SLOCK)
                    430:                fprintf(fp, " strict;");
                    431:        fputc('\n', fp);
                    432:
                    433:        fputs("comment\t@", fp);
                    434:        if (rfp->rf_comment != NULL) {
                    435:                rcs_strprint((const u_char *)rfp->rf_comment,
                    436:                    strlen(rfp->rf_comment), fp);
                    437:                fputs("@;\n", fp);
                    438:        } else
                    439:                fputs("# @;\n", fp);
                    440:
                    441:        if (rfp->rf_expand != NULL) {
                    442:                fputs("expand @", fp);
                    443:                rcs_strprint((const u_char *)rfp->rf_expand,
                    444:                    strlen(rfp->rf_expand), fp);
                    445:                fputs("@;\n", fp);
                    446:        }
                    447:
                    448:        fputs("\n\n", fp);
                    449:
                    450:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    451:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    452:                    sizeof(numbuf)));
                    453:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
                    454:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
                    455:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    456:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    457:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    458:                    rdp->rd_author, rdp->rd_state);
                    459:                fputs("branches", fp);
                    460:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                    461:                        fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
                    462:                            sizeof(numbuf)));
                    463:                }
                    464:                fputs(";\n", fp);
                    465:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    466:                    numbuf, sizeof(numbuf)));
                    467:        }
                    468:
                    469:        fputs("\ndesc\n@", fp);
                    470:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
                    471:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    472:                if (rfp->rf_desc[len-1] != '\n')
                    473:                        fputc('\n', fp);
                    474:        }
                    475:        fputs("@\n", fp);
                    476:
                    477:        /* deltatexts */
                    478:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    479:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    480:                    sizeof(numbuf)));
                    481:                fputs("log\n@", fp);
                    482:                if (rdp->rd_log != NULL) {
                    483:                        len = strlen(rdp->rd_log);
                    484:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
                    485:                        if (rdp->rd_log[len-1] != '\n')
                    486:                                fputc('\n', fp);
                    487:                }
                    488:                fputs("@\ntext\n@", fp);
                    489:                if (rdp->rd_text != NULL) {
                    490:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
                    491:
                    492:                        if (rdp->rd_tlen != 0) {
                    493:                                if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
                    494:                                        fputc('\n', fp);
                    495:                        }
                    496:                }
                    497:                fputs("@\n", fp);
                    498:        }
1.9       xsa       499:        (void)fclose(fp);
1.1       joris     500:
1.9       xsa       501:        if (rcs_movefile(fn, rfp->rf_path, rfp->rf_mode, rfp->rf_flags) == -1) {
                    502:                (void)unlink(fn);
                    503:                errx(1, "rcs_movefile failed");
                    504:        }
1.1       joris     505:
1.9       xsa       506:        rfp->rf_flags |= RCS_SYNCED;
1.1       joris     507:
1.9       xsa       508:        if (fn != NULL)
                    509:                xfree(fn);
                    510: }
1.1       joris     511:
1.9       xsa       512: /*
                    513:  * rcs_movefile()
                    514:  *
                    515:  * Move a file using rename(2) if possible and copying if not.
                    516:  * Returns 0 on success, -1 on failure.
                    517:  */
                    518: static int
                    519: rcs_movefile(char *from, char *to, mode_t perm, u_int to_flags)
                    520: {
                    521:        FILE *src, *dst;
                    522:        size_t nread, nwritten;
                    523:        char *buf;
                    524:        int ret;
1.1       joris     525:
1.9       xsa       526:        ret = -1;
1.1       joris     527:
1.9       xsa       528:        if (rename(from, to) == 0) {
                    529:                if (chmod(to, perm) == -1) {
                    530:                        warn("%s", to);
                    531:                        return (-1);
1.1       joris     532:                }
1.9       xsa       533:                return (0);
                    534:        } else if (errno != EXDEV) {
                    535:                warn("failed to access temp RCS output file");
                    536:                return (-1);
1.1       joris     537:        }
                    538:
1.9       xsa       539:        if ((chmod(to, S_IWUSR) == -1) && !(to_flags & RCS_CREATE)) {
                    540:                warnx("chmod(%s, 0%o) failed", to, S_IWUSR);
                    541:                return (-1);
1.1       joris     542:        }
                    543:
1.9       xsa       544:        /* different filesystem, have to copy the file */
                    545:        if ((src = fopen(from, "r")) == NULL) {
                    546:                warn("%s", from);
                    547:                return (-1);
                    548:        }
                    549:        if ((dst = fopen(to, "w")) == NULL) {
                    550:                warn("%s", to);
                    551:                return (-1);
                    552:        }
                    553:        if (fchmod(fileno(dst), perm)) {
                    554:                warn("%s", to);
                    555:                (void)unlink(to);
                    556:                return (-1);
                    557:        }
                    558:
                    559:        buf = xmalloc(MAXBSIZE);
                    560:        while ((nread = fread(buf, sizeof(char), MAXBSIZE, src)) != 0) {
                    561:                if (ferror(src)) {
                    562:                        warnx("failed to read `%s'", from);
                    563:                        (void)unlink(to);
                    564:                        goto out;
                    565:                }
                    566:                nwritten = fwrite(buf, sizeof(char), nread, dst);
                    567:                if (nwritten != nread) {
                    568:                        warnx("failed to write `%s'", to);
                    569:                        (void)unlink(to);
                    570:                        goto out;
                    571:                }
                    572:        }
1.1       joris     573:
1.7       xsa       574:        ret = 0;
1.9       xsa       575:
                    576:        (void)fclose(src);
                    577:        (void)fclose(dst);
                    578:        (void)unlink(from);
                    579:
1.7       xsa       580: out:
1.9       xsa       581:        xfree(buf);
1.7       xsa       582:
                    583:        return (ret);
1.1       joris     584: }
                    585:
                    586: /*
                    587:  * rcs_head_get()
                    588:  *
                    589:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    590:  */
                    591: const RCSNUM *
                    592: rcs_head_get(RCSFILE *file)
                    593: {
                    594:        return (file->rf_head);
                    595: }
                    596:
                    597: /*
                    598:  * rcs_head_set()
                    599:  *
                    600:  * Set the revision number of the head revision for the RCS file <file> to
                    601:  * <rev>, which must reference a valid revision within the file.
                    602:  */
                    603: int
                    604: rcs_head_set(RCSFILE *file, RCSNUM *rev)
                    605: {
                    606:        if (rcs_findrev(file, rev) == NULL)
                    607:                return (-1);
                    608:
                    609:        if (file->rf_head == NULL)
                    610:                file->rf_head = rcsnum_alloc();
                    611:
                    612:        rcsnum_cpy(rev, file->rf_head, 0);
                    613:        file->rf_flags &= ~RCS_SYNCED;
                    614:        return (0);
                    615: }
                    616:
                    617:
                    618: /*
                    619:  * rcs_branch_get()
                    620:  *
                    621:  * Retrieve the default branch number for the RCS file <file>.
                    622:  * Returns the number on success.  If NULL is returned, then there is no
                    623:  * default branch for this file.
                    624:  */
                    625: const RCSNUM *
                    626: rcs_branch_get(RCSFILE *file)
                    627: {
                    628:        return (file->rf_branch);
                    629: }
                    630:
                    631: /*
                    632:  * rcs_branch_set()
                    633:  *
                    634:  * Set the default branch for the RCS file <file> to <bnum>.
                    635:  * Returns 0 on success, -1 on failure.
                    636:  */
                    637: int
                    638: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
                    639: {
                    640:        if (file->rf_branch == NULL)
                    641:                file->rf_branch = rcsnum_alloc();
                    642:
                    643:        rcsnum_cpy(bnum, file->rf_branch, 0);
                    644:        file->rf_flags &= ~RCS_SYNCED;
                    645:        return (0);
                    646: }
                    647:
                    648: /*
                    649:  * rcs_access_add()
                    650:  *
                    651:  * Add the login name <login> to the access list for the RCS file <file>.
                    652:  * Returns 0 on success, or -1 on failure.
                    653:  */
                    654: int
                    655: rcs_access_add(RCSFILE *file, const char *login)
                    656: {
                    657:        struct rcs_access *ap;
                    658:
                    659:        /* first look for duplication */
                    660:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
                    661:                if (strcmp(ap->ra_name, login) == 0) {
                    662:                        rcs_errno = RCS_ERR_DUPENT;
                    663:                        return (-1);
                    664:                }
                    665:        }
                    666:
                    667:        ap = xmalloc(sizeof(*ap));
                    668:        ap->ra_name = xstrdup(login);
                    669:        TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
                    670:
                    671:        /* not synced anymore */
                    672:        file->rf_flags &= ~RCS_SYNCED;
                    673:        return (0);
                    674: }
                    675:
                    676: /*
                    677:  * rcs_access_remove()
                    678:  *
                    679:  * Remove an entry with login name <login> from the access list of the RCS
                    680:  * file <file>.
                    681:  * Returns 0 on success, or -1 on failure.
                    682:  */
                    683: int
                    684: rcs_access_remove(RCSFILE *file, const char *login)
                    685: {
                    686:        struct rcs_access *ap;
                    687:
                    688:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
                    689:                if (strcmp(ap->ra_name, login) == 0)
                    690:                        break;
                    691:
                    692:        if (ap == NULL) {
                    693:                rcs_errno = RCS_ERR_NOENT;
                    694:                return (-1);
                    695:        }
                    696:
                    697:        TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
                    698:        xfree(ap->ra_name);
                    699:        xfree(ap);
                    700:
                    701:        /* not synced anymore */
                    702:        file->rf_flags &= ~RCS_SYNCED;
                    703:        return (0);
                    704: }
                    705:
                    706: /*
                    707:  * rcs_sym_add()
                    708:  *
                    709:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    710:  * is named <sym> and is bound to the RCS revision <snum>.
                    711:  * Returns 0 on success, or -1 on failure.
                    712:  */
                    713: int
                    714: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
                    715: {
                    716:        struct rcs_sym *symp;
                    717:
                    718:        if (!rcs_sym_check(sym)) {
                    719:                rcs_errno = RCS_ERR_BADSYM;
                    720:                return (-1);
                    721:        }
                    722:
                    723:        /* first look for duplication */
                    724:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    725:                if (strcmp(symp->rs_name, sym) == 0) {
                    726:                        rcs_errno = RCS_ERR_DUPENT;
                    727:                        return (-1);
                    728:                }
                    729:        }
                    730:
                    731:        symp = xmalloc(sizeof(*symp));
                    732:        symp->rs_name = xstrdup(sym);
                    733:        symp->rs_num = rcsnum_alloc();
                    734:        rcsnum_cpy(snum, symp->rs_num, 0);
                    735:
                    736:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    737:
                    738:        /* not synced anymore */
                    739:        rfp->rf_flags &= ~RCS_SYNCED;
                    740:        return (0);
                    741: }
                    742:
                    743: /*
                    744:  * rcs_sym_remove()
                    745:  *
                    746:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    747:  * <file>.  If no such symbol is found, the call fails and returns with an
                    748:  * error.
                    749:  * Returns 0 on success, or -1 on failure.
                    750:  */
                    751: int
                    752: rcs_sym_remove(RCSFILE *file, const char *sym)
                    753: {
                    754:        struct rcs_sym *symp;
                    755:
                    756:        if (!rcs_sym_check(sym)) {
                    757:                rcs_errno = RCS_ERR_BADSYM;
                    758:                return (-1);
                    759:        }
                    760:
                    761:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    762:                if (strcmp(symp->rs_name, sym) == 0)
                    763:                        break;
                    764:
                    765:        if (symp == NULL) {
                    766:                rcs_errno = RCS_ERR_NOENT;
                    767:                return (-1);
                    768:        }
                    769:
                    770:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
                    771:        xfree(symp->rs_name);
                    772:        rcsnum_free(symp->rs_num);
                    773:        xfree(symp);
                    774:
                    775:        /* not synced anymore */
                    776:        file->rf_flags &= ~RCS_SYNCED;
                    777:        return (0);
                    778: }
                    779:
                    780: /*
                    781:  * rcs_sym_getrev()
                    782:  *
                    783:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    784:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    785:  * should be freed by the caller once they are done with it.
                    786:  * Returns the RCSNUM on success, or NULL on failure.
                    787:  */
                    788: RCSNUM *
                    789: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    790: {
                    791:        RCSNUM *num;
                    792:        struct rcs_sym *symp;
                    793:
                    794:        if (!rcs_sym_check(sym)) {
                    795:                rcs_errno = RCS_ERR_BADSYM;
                    796:                return (NULL);
                    797:        }
                    798:
                    799:        num = NULL;
                    800:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    801:                if (strcmp(symp->rs_name, sym) == 0)
                    802:                        break;
                    803:
                    804:        if (symp == NULL) {
                    805:                rcs_errno = RCS_ERR_NOENT;
                    806:        } else {
                    807:                num = rcsnum_alloc();
                    808:                rcsnum_cpy(symp->rs_num, num, 0);
                    809:        }
                    810:
                    811:        return (num);
                    812: }
                    813:
                    814: /*
                    815:  * rcs_sym_check()
                    816:  *
                    817:  * Check the RCS symbol name <sym> for any unsupported characters.
                    818:  * Returns 1 if the tag is correct, 0 if it isn't valid.
                    819:  */
                    820: int
                    821: rcs_sym_check(const char *sym)
                    822: {
                    823:        int ret;
                    824:        const char *cp;
                    825:
                    826:        ret = 1;
                    827:        cp = sym;
                    828:        if (!isalpha(*cp++))
                    829:                return (0);
                    830:
                    831:        for (; *cp != '\0'; cp++)
                    832:                if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
                    833:                        ret = 0;
                    834:                        break;
                    835:                }
                    836:
                    837:        return (ret);
                    838: }
                    839:
                    840: /*
                    841:  * rcs_lock_getmode()
                    842:  *
                    843:  * Retrieve the locking mode of the RCS file <file>.
                    844:  */
                    845: int
                    846: rcs_lock_getmode(RCSFILE *file)
                    847: {
                    848:        return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
                    849: }
                    850:
                    851: /*
                    852:  * rcs_lock_setmode()
                    853:  *
                    854:  * Set the locking mode of the RCS file <file> to <mode>, which must either
                    855:  * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
                    856:  * Returns the previous mode on success, or -1 on failure.
                    857:  */
                    858: int
                    859: rcs_lock_setmode(RCSFILE *file, int mode)
                    860: {
                    861:        int pmode;
                    862:        pmode = rcs_lock_getmode(file);
                    863:
                    864:        if (mode == RCS_LOCK_STRICT)
                    865:                file->rf_flags |= RCS_SLOCK;
                    866:        else if (mode == RCS_LOCK_LOOSE)
                    867:                file->rf_flags &= ~RCS_SLOCK;
                    868:        else
                    869:                errx(1, "rcs_lock_setmode: invalid mode `%d'", mode);
                    870:
                    871:        file->rf_flags &= ~RCS_SYNCED;
                    872:        return (pmode);
                    873: }
                    874:
                    875: /*
                    876:  * rcs_lock_add()
                    877:  *
                    878:  * Add an RCS lock for the user <user> on revision <rev>.
                    879:  * Returns 0 on success, or -1 on failure.
                    880:  */
                    881: int
                    882: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
                    883: {
                    884:        struct rcs_lock *lkp;
                    885:
                    886:        /* first look for duplication */
                    887:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    888:                if (strcmp(lkp->rl_name, user) == 0 &&
                    889:                    rcsnum_cmp(rev, lkp->rl_num, 0) == 0) {
                    890:                        rcs_errno = RCS_ERR_DUPENT;
                    891:                        return (-1);
                    892:                }
                    893:        }
                    894:
                    895:        lkp = xmalloc(sizeof(*lkp));
                    896:        lkp->rl_name = xstrdup(user);
                    897:        lkp->rl_num = rcsnum_alloc();
                    898:        rcsnum_cpy(rev, lkp->rl_num, 0);
                    899:
                    900:        TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
                    901:
                    902:        /* not synced anymore */
                    903:        file->rf_flags &= ~RCS_SYNCED;
                    904:        return (0);
                    905: }
                    906:
                    907:
                    908: /*
                    909:  * rcs_lock_remove()
                    910:  *
                    911:  * Remove the RCS lock on revision <rev>.
                    912:  * Returns 0 on success, or -1 on failure.
                    913:  */
                    914: int
                    915: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
                    916: {
                    917:        struct rcs_lock *lkp;
                    918:
                    919:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    920:                if (strcmp(lkp->rl_name, user) == 0 &&
                    921:                    rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
                    922:                        break;
                    923:        }
                    924:
                    925:        if (lkp == NULL) {
                    926:                rcs_errno = RCS_ERR_NOENT;
                    927:                return (-1);
                    928:        }
                    929:
                    930:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
                    931:        rcsnum_free(lkp->rl_num);
                    932:        xfree(lkp->rl_name);
                    933:        xfree(lkp);
                    934:
                    935:        /* not synced anymore */
                    936:        file->rf_flags &= ~RCS_SYNCED;
                    937:        return (0);
                    938: }
                    939:
                    940: /*
                    941:  * rcs_desc_get()
                    942:  *
                    943:  * Retrieve the description for the RCS file <file>.
                    944:  */
                    945: const char *
                    946: rcs_desc_get(RCSFILE *file)
                    947: {
                    948:        return (file->rf_desc);
                    949: }
                    950:
                    951: /*
                    952:  * rcs_desc_set()
                    953:  *
                    954:  * Set the description for the RCS file <file>.
                    955:  */
                    956: void
                    957: rcs_desc_set(RCSFILE *file, const char *desc)
                    958: {
                    959:        char *tmp;
                    960:
                    961:        tmp = xstrdup(desc);
                    962:        if (file->rf_desc != NULL)
                    963:                xfree(file->rf_desc);
                    964:        file->rf_desc = tmp;
                    965:        file->rf_flags &= ~RCS_SYNCED;
                    966: }
                    967:
                    968: /*
                    969:  * rcs_comment_lookup()
                    970:  *
                    971:  * Lookup the assumed comment leader based on a file's suffix.
                    972:  * Returns a pointer to the string on success, or NULL on failure.
                    973:  */
                    974: const char *
                    975: rcs_comment_lookup(const char *filename)
                    976: {
                    977:        int i;
                    978:        const char *sp;
                    979:
                    980:        if ((sp = strrchr(filename, '.')) == NULL) {
                    981:                rcs_errno = RCS_ERR_NOENT;
                    982:                return (NULL);
                    983:        }
                    984:        sp++;
                    985:
                    986:        for (i = 0; i < (int)NB_COMTYPES; i++)
                    987:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                    988:                        return (rcs_comments[i].rc_cstr);
                    989:        return (NULL);
                    990: }
                    991:
                    992: /*
                    993:  * rcs_comment_get()
                    994:  *
                    995:  * Retrieve the comment leader for the RCS file <file>.
                    996:  */
                    997: const char *
                    998: rcs_comment_get(RCSFILE *file)
                    999: {
                   1000:        return (file->rf_comment);
                   1001: }
                   1002:
                   1003: /*
                   1004:  * rcs_comment_set()
                   1005:  *
                   1006:  * Set the comment leader for the RCS file <file>.
                   1007:  */
                   1008: void
                   1009: rcs_comment_set(RCSFILE *file, const char *comment)
                   1010: {
                   1011:        char *tmp;
                   1012:
                   1013:        tmp = xstrdup(comment);
                   1014:        if (file->rf_comment != NULL)
                   1015:                xfree(file->rf_comment);
                   1016:        file->rf_comment = tmp;
                   1017:        file->rf_flags &= ~RCS_SYNCED;
                   1018: }
                   1019:
                   1020: /*
                   1021:  * rcs_tag_resolve()
                   1022:  *
                   1023:  * Retrieve the revision number corresponding to the tag <tag> for the RCS
                   1024:  * file <file>.
                   1025:  */
                   1026: RCSNUM *
                   1027: rcs_tag_resolve(RCSFILE *file, const char *tag)
                   1028: {
                   1029:        RCSNUM *num;
                   1030:
                   1031:        if ((num = rcsnum_parse(tag)) == NULL) {
                   1032:                num = rcs_sym_getrev(file, tag);
                   1033:        }
                   1034:
                   1035:        return (num);
                   1036: }
                   1037:
                   1038: int
                   1039: rcs_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
                   1040: {
                   1041:        char op, *ep;
                   1042:        struct rcs_line *lp, *dlp, *ndlp;
                   1043:        int i, lineno, nbln;
                   1044:
                   1045:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1046:        lp = TAILQ_FIRST(&(plines->l_lines));
                   1047:
                   1048:        /* skip first bogus line */
                   1049:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1050:            lp = TAILQ_NEXT(lp, l_list)) {
                   1051:                op = *(lp->l_line);
                   1052:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
                   1053:                if (lineno > dlines->l_nblines || lineno < 0 ||
                   1054:                    *ep != ' ')
                   1055:                        errx(1, "invalid line specification in RCS patch");
                   1056:                ep++;
                   1057:                nbln = (int)strtol(ep, &ep, 10);
                   1058:                if (nbln < 0 || *ep != '\0')
                   1059:                        errx(1,
                   1060:                            "invalid line number specification in RCS patch");
                   1061:
                   1062:                /* find the appropriate line */
                   1063:                for (;;) {
                   1064:                        if (dlp == NULL)
                   1065:                                break;
                   1066:                        if (dlp->l_lineno == lineno)
                   1067:                                break;
                   1068:                        if (dlp->l_lineno > lineno) {
                   1069:                                dlp = TAILQ_PREV(dlp, rcs_tqh, l_list);
                   1070:                        } else if (dlp->l_lineno < lineno) {
                   1071:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
                   1072:                                    ndlp->l_lineno > lineno)
                   1073:                                        break;
                   1074:                                dlp = ndlp;
                   1075:                        }
                   1076:                }
                   1077:                if (dlp == NULL)
                   1078:                        errx(1, "can't find referenced line in RCS patch");
                   1079:
                   1080:                if (op == 'd') {
                   1081:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
                   1082:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1083:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
                   1084:                                dlp = ndlp;
                   1085:                                /* last line is gone - reset dlp */
                   1086:                                if (dlp == NULL) {
                   1087:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1088:                                            rcs_tqh);
                   1089:                                        dlp = ndlp;
                   1090:                                }
                   1091:                        }
                   1092:                } else if (op == 'a') {
                   1093:                        for (i = 0; i < nbln; i++) {
                   1094:                                ndlp = lp;
                   1095:                                lp = TAILQ_NEXT(lp, l_list);
                   1096:                                if (lp == NULL)
                   1097:                                        errx(1, "truncated RCS patch");
                   1098:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1099:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1100:                                    lp, l_list);
                   1101:                                dlp = lp;
                   1102:
                   1103:                                /* we don't want lookup to block on those */
                   1104:                                lp->l_lineno = lineno;
                   1105:
                   1106:                                lp = ndlp;
                   1107:                        }
                   1108:                } else
                   1109:                        errx(1, "unknown RCS patch operation `%c'", op);
                   1110:
                   1111:                /* last line of the patch, done */
                   1112:                if (lp->l_lineno == plines->l_nblines)
                   1113:                        break;
                   1114:        }
                   1115:
                   1116:        /* once we're done patching, rebuild the line numbers */
                   1117:        lineno = 0;
                   1118:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1119:                lp->l_lineno = lineno++;
                   1120:        dlines->l_nblines = lineno - 1;
                   1121:
                   1122:        return (0);
                   1123: }
                   1124:
                   1125: /*
                   1126:  * rcs_getrev()
                   1127:  *
                   1128:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
                   1129:  * returned buffer is dynamically allocated and should be released using
                   1130:  * rcs_buf_free() once the caller is done using it.
                   1131:  */
                   1132: BUF*
                   1133: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
                   1134: {
                   1135:        u_int i, numlen;
1.6       niallo   1136:        int isbranch, lookonbranch, found;
1.1       joris    1137:        size_t len;
                   1138:        RCSNUM *crev, *rev, *brev;
1.12      niallo   1139:        BUF *rbuf, *dtext;
1.1       joris    1140:        struct rcs_delta *rdp = NULL;
                   1141:        struct rcs_branch *rb;
                   1142:
                   1143:        if (rfp->rf_head == NULL)
                   1144:                return (NULL);
                   1145:
                   1146:        if (frev == RCS_HEAD_REV)
                   1147:                rev = rfp->rf_head;
                   1148:        else
                   1149:                rev = frev;
                   1150:
                   1151:        /* XXX rcsnum_cmp() */
                   1152:        for (i = 0; i < rfp->rf_head->rn_len; i++) {
                   1153:                if (rfp->rf_head->rn_id[i] < rev->rn_id[i]) {
                   1154:                        rcs_errno = RCS_ERR_NOENT;
                   1155:                        return (NULL);
                   1156:                }
                   1157:        }
                   1158:
                   1159:        /* No matter what, we're going to need up the the description parsed */
                   1160:        rcs_parse_desc(rfp, NULL);
                   1161:
                   1162:        rdp = rcs_findrev(rfp, rfp->rf_head);
                   1163:        if (rdp == NULL) {
                   1164:                warnx("failed to get RCS HEAD revision");
                   1165:                return (NULL);
                   1166:        }
                   1167:
                   1168:        if (rdp->rd_tlen == 0)
                   1169:                rcs_parse_deltatexts(rfp, rfp->rf_head);
                   1170:
                   1171:        len = rdp->rd_tlen;
                   1172:        if (len == 0) {
                   1173:                rbuf = rcs_buf_alloc(1, 0);
                   1174:                rcs_buf_empty(rbuf);
                   1175:                return (rbuf);
                   1176:        }
                   1177:
                   1178:        rbuf = rcs_buf_alloc(len, BUF_AUTOEXT);
                   1179:        rcs_buf_append(rbuf, rdp->rd_text, len);
                   1180:
                   1181:        isbranch = 0;
                   1182:        brev = NULL;
                   1183:
                   1184:        /*
                   1185:         * If a branch was passed, get the latest revision on it.
                   1186:         */
                   1187:        if (RCSNUM_ISBRANCH(rev)) {
                   1188:                brev = rev;
                   1189:                rdp = rcs_findrev(rfp, rev);
                   1190:                if (rdp == NULL)
                   1191:                        return (NULL);
                   1192:
                   1193:                rev = rdp->rd_num;
                   1194:        } else {
                   1195:                if (RCSNUM_ISBRANCHREV(rev)) {
                   1196:                        brev = rcsnum_revtobr(rev);
                   1197:                        isbranch = 1;
                   1198:                }
                   1199:        }
                   1200:
                   1201:        lookonbranch = 0;
                   1202:        crev = NULL;
                   1203:
                   1204:        /* Apply patches backwards to get the right version.
                   1205:         */
                   1206:        do {
1.6       niallo   1207:                found = 0;
                   1208:
1.1       joris    1209:                if (rcsnum_cmp(rfp->rf_head, rev, 0) == 0)
                   1210:                        break;
                   1211:
                   1212:                if (isbranch == 1 && rdp->rd_num->rn_len < rev->rn_len &&
                   1213:                    !TAILQ_EMPTY(&(rdp->rd_branches)))
                   1214:                        lookonbranch = 1;
                   1215:
                   1216:                if (isbranch && lookonbranch == 1) {
                   1217:                        lookonbranch = 0;
                   1218:                        TAILQ_FOREACH(rb, &(rdp->rd_branches), rb_list) {
                   1219:                                /* XXX rcsnum_cmp() is totally broken for
                   1220:                                 * this purpose.
                   1221:                                 */
                   1222:                                numlen = MIN(brev->rn_len, rb->rb_num->rn_len);
                   1223:                                for (i = 0; i < numlen; i++) {
                   1224:                                        if (rb->rb_num->rn_id[i] !=
                   1225:                                            brev->rn_id[i])
                   1226:                                                break;
                   1227:                                }
                   1228:
                   1229:                                if (i == numlen) {
                   1230:                                        crev = rb->rb_num;
1.6       niallo   1231:                                        found = 1;
1.1       joris    1232:                                        break;
                   1233:                                }
                   1234:                        }
1.6       niallo   1235:                        if (found == 0)
                   1236:                                crev = rdp->rd_next;
1.1       joris    1237:                } else {
                   1238:                        crev = rdp->rd_next;
                   1239:                }
                   1240:
                   1241:                rdp = rcs_findrev(rfp, crev);
                   1242:                if (rdp == NULL) {
                   1243:                        rcs_buf_free(rbuf);
                   1244:                        return (NULL);
                   1245:                }
                   1246:
                   1247:                rcs_buf_putc(rbuf, '\0');
                   1248:
                   1249:                /* check if we have parsed this rev's deltatext */
                   1250:                if (rdp->rd_tlen == 0)
                   1251:                        rcs_parse_deltatexts(rfp, rdp->rd_num);
                   1252:
1.12      niallo   1253:
                   1254:                dtext = rcs_buf_alloc(len, BUF_AUTOEXT);
                   1255:                rcs_buf_append(dtext, rdp->rd_text, rdp->rd_tlen);
                   1256:                rbuf = rcs_patchfile(rbuf, dtext, rcs_patch_lines);
1.1       joris    1257:
                   1258:                if (rbuf == NULL)
                   1259:                        break;
                   1260:        } while (rcsnum_cmp(crev, rev, 0) != 0);
                   1261:
                   1262:        if (rcs_buf_getc(rbuf, rcs_buf_len(rbuf)-1) != '\n' &&
                   1263:            rbuf != NULL)
                   1264:                rcs_buf_putc(rbuf, '\n');
                   1265:
                   1266:        return (rbuf);
                   1267: }
                   1268:
                   1269: /*
                   1270:  * rcs_rev_add()
                   1271:  *
                   1272:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1273:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1274:  * new revision will have a number equal to the previous head revision plus
                   1275:  * one).  The <msg> argument specifies the log message for that revision, and
                   1276:  * <date> specifies the revision's date (a value of -1 is
                   1277:  * equivalent to using the current time).
                   1278:  * If <username> is NULL, set the author for this revision to the current user.
                   1279:  * Otherwise, set it to <username>.
                   1280:  * Returns 0 on success, or -1 on failure.
                   1281:  */
                   1282: int
                   1283: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1284:     const char *username)
                   1285: {
                   1286:        time_t now;
                   1287:        struct passwd *pw;
                   1288:        struct rcs_delta *ordp, *rdp;
                   1289:
                   1290:        if (rev == RCS_HEAD_REV) {
                   1291:                if (rf->rf_flags & RCS_CREATE) {
                   1292:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1293:                                return (-1);
                   1294:                        rf->rf_head = rcsnum_alloc();
                   1295:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1296:                } else {
                   1297:                        rev = rcsnum_inc(rf->rf_head);
                   1298:                }
                   1299:        } else {
                   1300:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1301:                        rcs_errno = RCS_ERR_DUPENT;
                   1302:                        return (-1);
                   1303:                }
                   1304:        }
                   1305:
                   1306:        if ((pw = getpwuid(getuid())) == NULL)
                   1307:                errx(1, "getpwuid failed");
                   1308:
                   1309:        rdp = xcalloc(1, sizeof(*rdp));
                   1310:
                   1311:        TAILQ_INIT(&(rdp->rd_branches));
                   1312:
                   1313:        rdp->rd_num = rcsnum_alloc();
                   1314:        rcsnum_cpy(rev, rdp->rd_num, 0);
                   1315:
                   1316:        rdp->rd_next = rcsnum_alloc();
                   1317:
                   1318:        if (!(rf->rf_flags & RCS_CREATE)) {
                   1319:                /* next should point to the previous HEAD */
                   1320:                ordp = TAILQ_FIRST(&(rf->rf_delta));
                   1321:                rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
                   1322:        }
                   1323:
                   1324:
                   1325:        if (username == NULL)
                   1326:                username = pw->pw_name;
                   1327:
                   1328:        rdp->rd_author = xstrdup(username);
                   1329:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1330:        rdp->rd_log = xstrdup(msg);
                   1331:
                   1332:        if (date != (time_t)(-1))
                   1333:                now = date;
                   1334:        else
                   1335:                time(&now);
                   1336:        gmtime_r(&now, &(rdp->rd_date));
                   1337:
                   1338:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1339:        rf->rf_ndelta++;
                   1340:
                   1341:        /* not synced anymore */
                   1342:        rf->rf_flags &= ~RCS_SYNCED;
                   1343:
                   1344:        return (0);
                   1345: }
                   1346:
                   1347: /*
                   1348:  * rcs_rev_remove()
                   1349:  *
                   1350:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1351:  */
                   1352: int
                   1353: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1354: {
1.12      niallo   1355:        char *path_tmp1, *path_tmp2;
1.1       joris    1356:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
1.12      niallo   1357:        BUF *newdeltatext, *nextbuf, *prevbuf, *newdiff;
                   1358:
                   1359:        nextrdp = prevrdp = NULL;
1.1       joris    1360:
                   1361:        if (rev == RCS_HEAD_REV)
                   1362:                rev = rf->rf_head;
                   1363:
                   1364:        /* do we actually have that revision? */
                   1365:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1366:                rcs_errno = RCS_ERR_NOENT;
                   1367:                return (-1);
                   1368:        }
                   1369:
                   1370:        /*
                   1371:         * This is confusing, the previous delta is next in the TAILQ list.
                   1372:         * the next delta is the previous one in the TAILQ list.
                   1373:         *
                   1374:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1375:         * When the first revision got specified, prevrdp will be NULL.
                   1376:         */
                   1377:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
                   1378:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, rcs_tqh, rd_list);
                   1379:
1.12      niallo   1380:        newdeltatext = prevbuf = nextbuf = NULL;
1.1       joris    1381:
                   1382:        if (prevrdp != NULL) {
                   1383:                if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
                   1384:                        errx(1, "error getting revision");
                   1385:        }
                   1386:
                   1387:        if (prevrdp != NULL && nextrdp != NULL) {
                   1388:                if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
                   1389:                        errx(1, "error getting revision");
                   1390:
                   1391:                newdiff = rcs_buf_alloc(64, BUF_AUTOEXT);
                   1392:
                   1393:                /* calculate new diff */
1.7       xsa      1394:                (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.1       joris    1395:                rcs_buf_write_stmp(nextbuf, path_tmp1, 0600);
                   1396:                rcs_buf_free(nextbuf);
                   1397:
1.7       xsa      1398:                (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.1       joris    1399:                rcs_buf_write_stmp(prevbuf, path_tmp2, 0600);
                   1400:                rcs_buf_free(prevbuf);
                   1401:
                   1402:                diff_format = D_RCSDIFF;
1.8       xsa      1403:                if (rcs_diffreg(path_tmp1, path_tmp2, newdiff) == D_ERROR)
                   1404:                        errx(1, "rcs_diffreg failed");
1.1       joris    1405:
1.12      niallo   1406:                newdeltatext = newdiff;
1.1       joris    1407:        } else if (nextrdp == NULL && prevrdp != NULL) {
1.12      niallo   1408:                newdeltatext = prevbuf;
1.1       joris    1409:        }
                   1410:
                   1411:        if (newdeltatext != NULL) {
                   1412:                if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
                   1413:                        errx(1, "error setting new deltatext");
                   1414:        }
                   1415:
                   1416:        TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1417:
                   1418:        /* update pointers */
                   1419:        if (prevrdp != NULL && nextrdp != NULL) {
                   1420:                rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
                   1421:        } else if (prevrdp != NULL) {
                   1422:                if (rcs_head_set(rf, prevrdp->rd_num) < 0)
                   1423:                        errx(1, "rcs_head_set failed");
                   1424:        } else if (nextrdp != NULL) {
                   1425:                rcsnum_free(nextrdp->rd_next);
                   1426:                nextrdp->rd_next = rcsnum_alloc();
                   1427:        } else {
                   1428:                rcsnum_free(rf->rf_head);
                   1429:                rf->rf_head = NULL;
                   1430:        }
                   1431:
                   1432:        rf->rf_ndelta--;
                   1433:        rf->rf_flags &= ~RCS_SYNCED;
                   1434:
                   1435:        rcs_freedelta(rdp);
                   1436:
1.7       xsa      1437:        if (path_tmp1 != NULL)
                   1438:                xfree(path_tmp1);
                   1439:        if (path_tmp2 != NULL)
                   1440:                xfree(path_tmp2);
1.1       joris    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.10      ray      1720:        if ((pdp->rp_file = fdopen(rfp->rf_fd, "r")) == NULL)
1.3       joris    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:
1.12      niallo   2092:        if (RCS_TOKLEN(rfp) == 0) {
                   2093:                rdp->rd_text = xmalloc(1);
                   2094:                rdp->rd_text[0] = '\0';
                   2095:                rdp->rd_tlen = 0;
                   2096:        } else {
                   2097:                rdp->rd_text = xmalloc(RCS_TOKLEN(rfp));
                   2098:                memcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp)));
                   2099:                rdp->rd_tlen = RCS_TOKLEN(rfp);
                   2100:        }
1.1       joris    2101:
                   2102:        return (1);
                   2103: }
                   2104:
                   2105: /*
                   2106:  * rcs_parse_access()
                   2107:  *
                   2108:  * Parse the access list given as value to the `access' keyword.
                   2109:  * Returns 0 on success, or -1 on failure.
                   2110:  */
                   2111: static int
                   2112: rcs_parse_access(RCSFILE *rfp)
                   2113: {
                   2114:        int type;
                   2115:
                   2116:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2117:                if (type != RCS_TOK_ID) {
                   2118:                        rcs_errno = RCS_ERR_PARSE;
                   2119:                        warnx("unexpected token `%s' in access list",
                   2120:                            RCS_TOKSTR(rfp));
                   2121:                        return (-1);
                   2122:                }
                   2123:
                   2124:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2125:                        return (-1);
                   2126:        }
                   2127:
                   2128:        return (0);
                   2129: }
                   2130:
                   2131: /*
                   2132:  * rcs_parse_symbols()
                   2133:  *
                   2134:  * Parse the symbol list given as value to the `symbols' keyword.
                   2135:  * Returns 0 on success, or -1 on failure.
                   2136:  */
                   2137: static int
                   2138: rcs_parse_symbols(RCSFILE *rfp)
                   2139: {
                   2140:        int type;
                   2141:        struct rcs_sym *symp;
                   2142:
                   2143:        for (;;) {
                   2144:                type = rcs_gettok(rfp);
                   2145:                if (type == RCS_TOK_SCOLON)
                   2146:                        break;
                   2147:
                   2148:                if (type != RCS_TOK_ID) {
                   2149:                        rcs_errno = RCS_ERR_PARSE;
                   2150:                        warnx("unexpected token `%s' in symbol list",
                   2151:                            RCS_TOKSTR(rfp));
                   2152:                        return (-1);
                   2153:                }
                   2154:
                   2155:                symp = xmalloc(sizeof(*symp));
                   2156:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
                   2157:                symp->rs_num = rcsnum_alloc();
                   2158:
                   2159:                type = rcs_gettok(rfp);
                   2160:                if (type != RCS_TOK_COLON) {
                   2161:                        rcs_errno = RCS_ERR_PARSE;
                   2162:                        warnx("unexpected token `%s' in symbol list",
                   2163:                            RCS_TOKSTR(rfp));
                   2164:                        rcsnum_free(symp->rs_num);
                   2165:                        xfree(symp->rs_name);
                   2166:                        xfree(symp);
                   2167:                        return (-1);
                   2168:                }
                   2169:
                   2170:                type = rcs_gettok(rfp);
                   2171:                if (type != RCS_TOK_NUM) {
                   2172:                        rcs_errno = RCS_ERR_PARSE;
                   2173:                        warnx("unexpected token `%s' in symbol list",
                   2174:                            RCS_TOKSTR(rfp));
                   2175:                        rcsnum_free(symp->rs_num);
                   2176:                        xfree(symp->rs_name);
                   2177:                        xfree(symp);
                   2178:                        return (-1);
                   2179:                }
                   2180:
                   2181:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2182:                        warnx("failed to parse RCS NUM `%s'",
                   2183:                            RCS_TOKSTR(rfp));
                   2184:                        rcsnum_free(symp->rs_num);
                   2185:                        xfree(symp->rs_name);
                   2186:                        xfree(symp);
                   2187:                        return (-1);
                   2188:                }
                   2189:
                   2190:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
                   2191:        }
                   2192:
                   2193:        return (0);
                   2194: }
                   2195:
                   2196: /*
                   2197:  * rcs_parse_locks()
                   2198:  *
                   2199:  * Parse the lock list given as value to the `locks' keyword.
                   2200:  * Returns 0 on success, or -1 on failure.
                   2201:  */
                   2202: static int
                   2203: rcs_parse_locks(RCSFILE *rfp)
                   2204: {
                   2205:        int type;
                   2206:        struct rcs_lock *lkp;
                   2207:
                   2208:        for (;;) {
                   2209:                type = rcs_gettok(rfp);
                   2210:                if (type == RCS_TOK_SCOLON)
                   2211:                        break;
                   2212:
                   2213:                if (type != RCS_TOK_ID) {
                   2214:                        rcs_errno = RCS_ERR_PARSE;
                   2215:                        warnx("unexpected token `%s' in lock list",
                   2216:                            RCS_TOKSTR(rfp));
                   2217:                        return (-1);
                   2218:                }
                   2219:
                   2220:                lkp = xmalloc(sizeof(*lkp));
                   2221:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
                   2222:                lkp->rl_num = rcsnum_alloc();
                   2223:
                   2224:                type = rcs_gettok(rfp);
                   2225:                if (type != RCS_TOK_COLON) {
                   2226:                        rcs_errno = RCS_ERR_PARSE;
                   2227:                        warnx("unexpected token `%s' in symbol list",
                   2228:                            RCS_TOKSTR(rfp));
                   2229:                        rcsnum_free(lkp->rl_num);
                   2230:                        xfree(lkp->rl_name);
                   2231:                        xfree(lkp);
                   2232:                        return (-1);
                   2233:                }
                   2234:
                   2235:                type = rcs_gettok(rfp);
                   2236:                if (type != RCS_TOK_NUM) {
                   2237:                        rcs_errno = RCS_ERR_PARSE;
                   2238:                        warnx("unexpected token `%s' in symbol list",
                   2239:                            RCS_TOKSTR(rfp));
                   2240:                        rcsnum_free(lkp->rl_num);
                   2241:                        xfree(lkp->rl_name);
                   2242:                        xfree(lkp);
                   2243:                        return (-1);
                   2244:                }
                   2245:
                   2246:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2247:                        warnx("failed to parse RCS NUM `%s'",
                   2248:                            RCS_TOKSTR(rfp));
                   2249:                        rcsnum_free(lkp->rl_num);
                   2250:                        xfree(lkp->rl_name);
                   2251:                        xfree(lkp);
                   2252:                        return (-1);
                   2253:                }
                   2254:
                   2255:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2256:        }
                   2257:
                   2258:        /* check if we have a `strict' */
                   2259:        type = rcs_gettok(rfp);
                   2260:        if (type != RCS_TOK_STRICT) {
                   2261:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
                   2262:        } else {
                   2263:                rfp->rf_flags |= RCS_SLOCK;
                   2264:
                   2265:                type = rcs_gettok(rfp);
                   2266:                if (type != RCS_TOK_SCOLON) {
                   2267:                        rcs_errno = RCS_ERR_PARSE;
                   2268:                        warnx("missing semi-colon after `strict' keyword");
                   2269:                        return (-1);
                   2270:                }
                   2271:        }
                   2272:
                   2273:        return (0);
                   2274: }
                   2275:
                   2276: /*
                   2277:  * rcs_parse_branches()
                   2278:  *
                   2279:  * Parse the list of branches following a `branches' keyword in a delta.
                   2280:  * Returns 0 on success, or -1 on failure.
                   2281:  */
                   2282: static int
                   2283: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2284: {
                   2285:        int type;
                   2286:        struct rcs_branch *brp;
                   2287:
                   2288:        for (;;) {
                   2289:                type = rcs_gettok(rfp);
                   2290:                if (type == RCS_TOK_SCOLON)
                   2291:                        break;
                   2292:
                   2293:                if (type != RCS_TOK_NUM) {
                   2294:                        rcs_errno = RCS_ERR_PARSE;
                   2295:                        warnx("unexpected token `%s' in list of branches",
                   2296:                            RCS_TOKSTR(rfp));
                   2297:                        return (-1);
                   2298:                }
                   2299:
                   2300:                brp = xmalloc(sizeof(*brp));
                   2301:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
                   2302:                if (brp->rb_num == NULL) {
                   2303:                        xfree(brp);
                   2304:                        return (-1);
                   2305:                }
                   2306:
                   2307:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2308:        }
                   2309:
                   2310:        return (0);
                   2311: }
                   2312:
                   2313: /*
                   2314:  * rcs_freedelta()
                   2315:  *
                   2316:  * Free the contents of a delta structure.
                   2317:  */
                   2318: static void
                   2319: rcs_freedelta(struct rcs_delta *rdp)
                   2320: {
                   2321:        struct rcs_branch *rb;
                   2322:
                   2323:        if (rdp->rd_num != NULL)
                   2324:                rcsnum_free(rdp->rd_num);
                   2325:        if (rdp->rd_next != NULL)
                   2326:                rcsnum_free(rdp->rd_next);
                   2327:
                   2328:        if (rdp->rd_author != NULL)
                   2329:                xfree(rdp->rd_author);
                   2330:        if (rdp->rd_locker != NULL)
                   2331:                xfree(rdp->rd_locker);
                   2332:        if (rdp->rd_state != NULL)
                   2333:                xfree(rdp->rd_state);
                   2334:        if (rdp->rd_log != NULL)
                   2335:                xfree(rdp->rd_log);
                   2336:        if (rdp->rd_text != NULL)
                   2337:                xfree(rdp->rd_text);
                   2338:
                   2339:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2340:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2341:                rcsnum_free(rb->rb_num);
                   2342:                xfree(rb);
                   2343:        }
                   2344:
                   2345:        xfree(rdp);
                   2346: }
                   2347:
                   2348: /*
                   2349:  * rcs_freepdata()
                   2350:  *
                   2351:  * Free the contents of the parser data structure.
                   2352:  */
                   2353: static void
                   2354: rcs_freepdata(struct rcs_pdata *pd)
                   2355: {
                   2356:        if (pd->rp_file != NULL)
                   2357:                (void)fclose(pd->rp_file);
                   2358:        if (pd->rp_buf != NULL)
                   2359:                xfree(pd->rp_buf);
                   2360:        xfree(pd);
                   2361: }
                   2362:
                   2363: /*
                   2364:  * rcs_gettok()
                   2365:  *
                   2366:  * Get the next RCS token from the string <str>.
                   2367:  */
                   2368: static int
                   2369: rcs_gettok(RCSFILE *rfp)
                   2370: {
                   2371:        u_int i;
                   2372:        int ch, last, type;
                   2373:        size_t len;
                   2374:        char *bp;
                   2375:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2376:
                   2377:        type = RCS_TOK_ERR;
                   2378:        bp = pdp->rp_buf;
                   2379:        pdp->rp_tlen = 0;
                   2380:        *bp = '\0';
                   2381:
                   2382:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2383:                type = pdp->rp_pttype;
1.5       ray      2384:                if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
                   2385:                    pdp->rp_blen)
                   2386:                        errx(1, "rcs_gettok: strlcpy");
1.1       joris    2387:                pdp->rp_pttype = RCS_TOK_ERR;
                   2388:                return (type);
                   2389:        }
                   2390:
                   2391:        /* skip leading whitespace */
                   2392:        /* XXX we must skip backspace too for compatibility, should we? */
                   2393:        do {
                   2394:                ch = getc(pdp->rp_file);
                   2395:                if (ch == '\n')
                   2396:                        pdp->rp_lines++;
                   2397:        } while (isspace(ch));
                   2398:
                   2399:        if (ch == EOF) {
                   2400:                type = RCS_TOK_EOF;
                   2401:        } else if (ch == ';') {
                   2402:                type = RCS_TOK_SCOLON;
                   2403:        } else if (ch == ':') {
                   2404:                type = RCS_TOK_COLON;
                   2405:        } else if (isalpha(ch)) {
                   2406:                type = RCS_TOK_ID;
                   2407:                *(bp++) = ch;
                   2408:                for (;;) {
                   2409:                        ch = getc(pdp->rp_file);
                   2410:                        if (!isalnum(ch) && ch != '_' && ch != '-' &&
                   2411:                            ch != '/') {
                   2412:                                ungetc(ch, pdp->rp_file);
                   2413:                                break;
                   2414:                        }
                   2415:                        *(bp++) = ch;
                   2416:                        pdp->rp_tlen++;
                   2417:                        if (bp == pdp->rp_bufend - 1) {
                   2418:                                len = bp - pdp->rp_buf;
                   2419:                                rcs_growbuf(rfp);
                   2420:                                bp = pdp->rp_buf + len;
                   2421:                        }
                   2422:                }
                   2423:                *bp = '\0';
                   2424:
                   2425:                if (type != RCS_TOK_ERR) {
                   2426:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2427:                                if (strcmp(rcs_keys[i].rk_str,
                   2428:                                    pdp->rp_buf) == 0) {
                   2429:                                        type = rcs_keys[i].rk_id;
                   2430:                                        break;
                   2431:                                }
                   2432:                        }
                   2433:                }
                   2434:        } else if (ch == '@') {
                   2435:                /* we have a string */
                   2436:                type = RCS_TOK_STRING;
                   2437:                for (;;) {
                   2438:                        ch = getc(pdp->rp_file);
                   2439:                        if (ch == '@') {
                   2440:                                ch = getc(pdp->rp_file);
                   2441:                                if (ch != '@') {
                   2442:                                        ungetc(ch, pdp->rp_file);
                   2443:                                        break;
                   2444:                                }
                   2445:                        } else if (ch == '\n')
                   2446:                                pdp->rp_lines++;
                   2447:
                   2448:                        *(bp++) = ch;
                   2449:                        pdp->rp_tlen++;
                   2450:                        if (bp == pdp->rp_bufend - 1) {
                   2451:                                len = bp - pdp->rp_buf;
                   2452:                                rcs_growbuf(rfp);
                   2453:                                bp = pdp->rp_buf + len;
                   2454:                        }
                   2455:                }
                   2456:
                   2457:                *bp = '\0';
                   2458:        } else if (isdigit(ch)) {
                   2459:                *(bp++) = ch;
                   2460:                last = ch;
                   2461:                type = RCS_TOK_NUM;
                   2462:
                   2463:                for (;;) {
                   2464:                        ch = getc(pdp->rp_file);
                   2465:                        if (bp == pdp->rp_bufend)
                   2466:                                break;
                   2467:                        if (!isdigit(ch) && ch != '.') {
                   2468:                                ungetc(ch, pdp->rp_file);
                   2469:                                break;
                   2470:                        }
                   2471:
                   2472:                        if (last == '.' && ch == '.') {
                   2473:                                type = RCS_TOK_ERR;
                   2474:                                break;
                   2475:                        }
                   2476:                        last = ch;
                   2477:                        *(bp++) = ch;
                   2478:                        pdp->rp_tlen++;
                   2479:                }
                   2480:                *bp = '\0';
                   2481:        }
                   2482:
                   2483:        return (type);
                   2484: }
                   2485:
                   2486: /*
                   2487:  * rcs_pushtok()
                   2488:  *
                   2489:  * Push a token back in the parser's token buffer.
                   2490:  */
                   2491: static int
                   2492: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2493: {
                   2494:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2495:
                   2496:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2497:                return (-1);
                   2498:
                   2499:        pdp->rp_pttype = type;
1.5       ray      2500:        if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
                   2501:            sizeof(pdp->rp_ptok))
                   2502:                errx(1, "rcs_pushtok: strlcpy");
1.1       joris    2503:        return (0);
                   2504: }
                   2505:
                   2506:
                   2507: /*
                   2508:  * rcs_growbuf()
                   2509:  *
                   2510:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2511:  * RCS_BUFEXTSIZE.
                   2512:  * In case of failure, the original buffer is left unmodified.
                   2513:  */
                   2514: static void
                   2515: rcs_growbuf(RCSFILE *rf)
                   2516: {
                   2517:        void *tmp;
                   2518:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2519:
                   2520:        tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
                   2521:        pdp->rp_buf = tmp;
                   2522:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2523:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
                   2524: }
                   2525:
                   2526: /*
                   2527:  * rcs_strprint()
                   2528:  *
                   2529:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2530:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2531:  * binary data.
                   2532:  */
                   2533: static void
                   2534: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2535: {
                   2536:        const u_char *ap, *ep, *sp;
                   2537:
                   2538:        if (slen == 0)
                   2539:                return;
                   2540:
                   2541:        ep = str + slen - 1;
                   2542:
                   2543:        for (sp = str; sp <= ep;)  {
                   2544:                ap = memchr(sp, '@', ep - sp);
                   2545:                if (ap == NULL)
                   2546:                        ap = ep;
                   2547:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
                   2548:
                   2549:                if (*ap == '@')
                   2550:                        putc('@', stream);
                   2551:                sp = ap + 1;
                   2552:        }
                   2553: }
                   2554:
                   2555: /*
                   2556:  * rcs_expand_keywords()
                   2557:  *
                   2558:  * Return expansion any RCS keywords in <data>
                   2559:  *
                   2560:  * On error, return NULL.
                   2561:  */
1.12      niallo   2562: static BUF *
                   2563: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, BUF *bp, int mode)
1.1       joris    2564: {
                   2565:        ptrdiff_t c_offset, sizdiff, start_offset;
                   2566:        size_t i;
                   2567:        int kwtype;
                   2568:        u_int j, found;
1.12      niallo   2569:        u_char *c, *data, *kwstr, *start, *end, *tbuf, *fin;
1.1       joris    2570:        char expbuf[256], buf[256];
                   2571:        struct tm tb;
                   2572:        char *fmt;
1.12      niallo   2573:        size_t len, newlen, tbuflen;
1.1       joris    2574:
                   2575:        kwtype = 0;
                   2576:        kwstr = NULL;
                   2577:        i = 0;
                   2578:
                   2579:        /*
                   2580:         * -z support for RCS
                   2581:         */
                   2582:        tb = rdp->rd_date;
                   2583:        if (timezone_flag != NULL)
                   2584:                rcs_set_tz(timezone_flag, rdp, &tb);
                   2585:
1.12      niallo   2586:        len = rcs_buf_len(bp);
                   2587:        newlen = 0;
                   2588:
                   2589:        c = rcs_buf_get(bp);
                   2590:        found = 0;
                   2591:        for (i = 0; i < len; i++) {
                   2592:                if (*c == '$') {
                   2593:                        c++;
                   2594:                        i++;
                   2595:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2596:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2597:                                    strlen(rcs_expkw[j].kw_str))) {
                   2598:                                        found = 1;
                   2599:                                        kwstr = rcs_expkw[j].kw_str;
                   2600:                                        kwtype = rcs_expkw[j].kw_type;
                   2601:                                        break;
                   2602:                                }
                   2603:                        }
                   2604:                }
                   2605:                c++;
                   2606:        }
                   2607:        if (found == 0)
                   2608:                return (bp);
                   2609:
                   2610:        rcs_buf_putc(bp, '\0');
                   2611:        data = rcs_buf_release(bp);
                   2612:        c = data;
                   2613:        fin = c + len;
                   2614:        len++;
1.1       joris    2615:        /*
                   2616:         * Keyword formats:
                   2617:         * $Keyword$
                   2618:         * $Keyword: value$
                   2619:         */
1.12      niallo   2620:        for (; c != fin; c++) {
1.1       joris    2621:                if (*c == '$') {
                   2622:                        /* remember start of this possible keyword */
                   2623:                        start = c;
                   2624:                        start_offset = start - data;
                   2625:
                   2626:                        /* first following character has to be alphanumeric */
                   2627:                        c++;
1.12      niallo   2628:                        i++;
1.1       joris    2629:                        if (!isalpha(*c)) {
                   2630:                                c = start;
                   2631:                                continue;
                   2632:                        }
                   2633:
                   2634:                        /* look for any matching keywords */
                   2635:                        found = 0;
                   2636:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2637:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2638:                                    strlen(rcs_expkw[j].kw_str))) {
                   2639:                                        found = 1;
                   2640:                                        kwstr = rcs_expkw[j].kw_str;
                   2641:                                        kwtype = rcs_expkw[j].kw_type;
                   2642:                                        break;
                   2643:                                }
                   2644:                        }
                   2645:
                   2646:                        /* unknown keyword, continue looking */
                   2647:                        if (found == 0) {
                   2648:                                c = start;
                   2649:                                continue;
                   2650:                        }
                   2651:
                   2652:                        /* next character has to be ':' or '$' */
                   2653:                        c += strlen(kwstr);
                   2654:                        if (*c != ':' && *c != '$') {
                   2655:                                c = start;
                   2656:                                continue;
                   2657:                        }
                   2658:
                   2659:                        /*
                   2660:                         * if the next character was ':' we need to look for
                   2661:                         * an '$' before the end of the line to be sure it is
                   2662:                         * in fact a keyword.
                   2663:                         */
                   2664:                        if (*c == ':') {
                   2665:                                while (*c++) {
                   2666:                                        if (*c == '$' || *c == '\n')
                   2667:                                                break;
                   2668:                                }
                   2669:
                   2670:                                if (*c != '$') {
                   2671:                                        c = start;
                   2672:                                        continue;
                   2673:                                }
                   2674:                        }
                   2675:                        c_offset = c - data;
                   2676:                        end = c + 1;
                   2677:
                   2678:                        /* start constructing the expansion */
                   2679:                        expbuf[0] = '\0';
                   2680:
                   2681:                        if (mode & RCS_KWEXP_NAME) {
1.5       ray      2682:                                if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf) ||
                   2683:                                    strlcat(expbuf, kwstr, sizeof(expbuf)) >= sizeof(expbuf))
                   2684:                                        errx(1, "rcs_expand_keywords: string truncated");
                   2685:                                if ((mode & RCS_KWEXP_VAL) &&
                   2686:                                    strlcat(expbuf, ": ", sizeof(expbuf)) >= sizeof(expbuf))
                   2687:                                        errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2688:                        }
                   2689:
                   2690:                        /*
                   2691:                         * order matters because of RCS_KW_ID and
                   2692:                         * RCS_KW_HEADER here
                   2693:                         */
                   2694:                        if (mode & RCS_KWEXP_VAL) {
                   2695:                                if (kwtype & RCS_KW_RCSFILE) {
                   2696:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.5       ray      2697:                                                (void)strlcat(expbuf, basename(rcsfile), sizeof(expbuf));
1.1       joris    2698:                                        else
1.5       ray      2699:                                                (void)strlcat(expbuf, rcsfile, sizeof(expbuf));
                   2700:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2701:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2702:                                }
                   2703:
                   2704:                                if (kwtype & RCS_KW_REVISION) {
1.5       ray      2705:                                        rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
                   2706:                                        if (strlcat(buf, " ", sizeof(buf)) >= sizeof(buf) ||
                   2707:                                            strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(buf))
                   2708:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2709:                                }
                   2710:
                   2711:                                if (kwtype & RCS_KW_DATE) {
                   2712:                                        if (timezone_flag != NULL)
                   2713:                                                fmt = "%Y/%m/%d %H:%M:%S%z ";
                   2714:                                        else
                   2715:                                                fmt = "%Y/%m/%d %H:%M:%S ";
                   2716:
                   2717:                                        strftime(buf, sizeof(buf), fmt, &tb);
1.5       ray      2718:                                        if (strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(expbuf))
                   2719:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2720:                                }
                   2721:
                   2722:                                if (kwtype & RCS_KW_AUTHOR) {
1.5       ray      2723:                                        if (strlcat(expbuf, rdp->rd_author, sizeof(expbuf)) >= sizeof(expbuf) ||
                   2724:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2725:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2726:                                }
                   2727:
                   2728:                                if (kwtype & RCS_KW_STATE) {
1.5       ray      2729:                                        if (strlcat(expbuf, rdp->rd_state, sizeof(expbuf)) >= sizeof(expbuf) ||
                   2730:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2731:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2732:                                }
                   2733:
                   2734:                                /* order does not matter anymore below */
                   2735:                                if (kwtype & RCS_KW_LOG)
1.5       ray      2736:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2737:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2738:
                   2739:                                if (kwtype & RCS_KW_SOURCE) {
1.5       ray      2740:                                        if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >= sizeof(expbuf) ||
                   2741:                                            strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2742:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2743:                                }
                   2744:
                   2745:                                if (kwtype & RCS_KW_NAME)
1.5       ray      2746:                                        if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
                   2747:                                                errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2748:                        }
                   2749:
                   2750:                        /* end the expansion */
                   2751:                        if (mode & RCS_KWEXP_NAME)
1.5       ray      2752:                                if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf))
                   2753:                                        errx(1, "rcs_expand_keywords: string truncated");
1.1       joris    2754:
                   2755:                        sizdiff = strlen(expbuf) - (end - start);
1.12      niallo   2756:                        tbuflen = len - (end - start);
                   2757:                        tbuf = xmalloc(tbuflen);
                   2758:                        memcpy(tbuf, end, tbuflen);
1.1       joris    2759:                        /* only realloc if we have to */
                   2760:                        if (sizdiff > 0) {
                   2761:                                char *newdata;
                   2762:
                   2763:                                len += sizdiff;
                   2764:                                newdata = xrealloc(data, 1, len);
                   2765:                                data = newdata;
                   2766:                                /*
                   2767:                                 * ensure string pointers are not invalidated
                   2768:                                 * after realloc()
                   2769:                                 */
                   2770:                                start = data + start_offset;
1.15      niallo   2771:                                fin = data + len;
1.1       joris    2772:                        }
1.12      niallo   2773:                        memcpy(start, expbuf, strlen(expbuf));
1.16    ! niallo   2774:                        start += sizdiff;
1.12      niallo   2775:                        memcpy(start, tbuf, tbuflen);
1.1       joris    2776:                        xfree(tbuf);
1.16    ! niallo   2777:                        i += sizdiff;
1.1       joris    2778:                }
                   2779:        }
1.13      niallo   2780:        bp = rcs_buf_alloc(len - 1, BUF_AUTOEXT);
                   2781:        rcs_buf_set(bp, data, len - 1, 0);
1.14      niallo   2782:        xfree(data);
1.1       joris    2783:
1.12      niallo   2784:        return (bp);
1.1       joris    2785: }
                   2786:
                   2787: /*
                   2788:  * rcs_deltatext_set()
                   2789:  *
                   2790:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
                   2791:  * Returns -1 on error, 0 on success.
                   2792:  */
                   2793: int
1.12      niallo   2794: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
1.1       joris    2795: {
                   2796:        size_t len;
1.12      niallo   2797:        u_char *dtext;
1.1       joris    2798:        struct rcs_delta *rdp;
                   2799:
                   2800:        /* Write operations require full parsing */
                   2801:        rcs_parse_deltatexts(rfp, NULL);
                   2802:
                   2803:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2804:                return (-1);
                   2805:
                   2806:        if (rdp->rd_text != NULL)
                   2807:                xfree(rdp->rd_text);
                   2808:
1.12      niallo   2809:        len = rcs_buf_len(bp);
                   2810:        dtext = rcs_buf_release(bp);
                   2811:        bp = NULL;
1.1       joris    2812:        if (len != 0) {
1.12      niallo   2813:                rdp->rd_text = xmalloc(len);
1.1       joris    2814:                rdp->rd_tlen = len;
1.12      niallo   2815:                (void)memcpy(rdp->rd_text, dtext, len);
                   2816:                xfree(dtext);
1.1       joris    2817:        } else {
                   2818:                rdp->rd_text = NULL;
                   2819:                rdp->rd_tlen = 0;
                   2820:        }
                   2821:
                   2822:        return (0);
                   2823: }
                   2824:
                   2825: /*
                   2826:  * rcs_rev_setlog()
                   2827:  *
                   2828:  * Sets the log message of revision <rev> to <logtext>
                   2829:  */
                   2830: int
                   2831: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2832: {
                   2833:        struct rcs_delta *rdp;
                   2834:        char buf[16];
                   2835:
                   2836:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2837:
                   2838:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2839:                return (-1);
                   2840:
                   2841:        if (rdp->rd_log != NULL)
                   2842:                xfree(rdp->rd_log);
                   2843:
                   2844:        rdp->rd_log = xstrdup(logtext);
                   2845:        rfp->rf_flags &= ~RCS_SYNCED;
                   2846:        return (0);
                   2847: }
                   2848: /*
                   2849:  * rcs_rev_getdate()
                   2850:  *
                   2851:  * Get the date corresponding to a given revision.
                   2852:  * Returns the date on success, -1 on failure.
                   2853:  */
                   2854: time_t
                   2855: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2856: {
                   2857:        struct rcs_delta *rdp;
                   2858:
                   2859:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2860:                return (-1);
                   2861:
                   2862:        return (mktime(&rdp->rd_date));
                   2863: }
                   2864:
                   2865: /*
                   2866:  * rcs_state_set()
                   2867:  *
                   2868:  * Sets the state of revision <rev> to <state>
                   2869:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2870:  *
                   2871:  * Returns -1 on failure, 0 on success.
                   2872:  */
                   2873: int
                   2874: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2875: {
                   2876:        struct rcs_delta *rdp;
                   2877:
                   2878:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2879:                return (-1);
                   2880:
                   2881:        if (rdp->rd_state != NULL)
                   2882:                xfree(rdp->rd_state);
                   2883:
                   2884:        rdp->rd_state = xstrdup(state);
                   2885:
                   2886:        rfp->rf_flags &= ~RCS_SYNCED;
                   2887:
                   2888:        return (0);
                   2889: }
                   2890:
                   2891: /*
                   2892:  * rcs_state_check()
                   2893:  *
                   2894:  * Check if string <state> is valid.
                   2895:  *
                   2896:  * Returns 0 if the string is valid, -1 otherwise.
                   2897:  */
                   2898: int
                   2899: rcs_state_check(const char *state)
                   2900: {
                   2901:        if (strchr(state, ' ') != NULL)
                   2902:                return (-1);
                   2903:
                   2904:        return (0);
                   2905: }
                   2906:
                   2907: /*
                   2908:  * rcs_state_get()
                   2909:  *
                   2910:  * Get the state for a given revision of a specified RCSFILE.
                   2911:  *
                   2912:  * Returns NULL on failure.
                   2913:  */
                   2914: const char *
                   2915: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2916: {
                   2917:        struct rcs_delta *rdp;
                   2918:
                   2919:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2920:                return (NULL);
                   2921:
                   2922:        return (rdp->rd_state);
                   2923: }
                   2924:
                   2925: /*
                   2926:  * rcs_kwexp_buf()
                   2927:  *
                   2928:  * Do keyword expansion on a buffer if necessary
                   2929:  *
                   2930:  */
                   2931: BUF *
                   2932: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2933: {
                   2934:        struct rcs_delta *rdp;
                   2935:        int expmode;
                   2936:
                   2937:        /*
                   2938:         * Do keyword expansion if required.
                   2939:         */
                   2940:        if (rf->rf_expand != NULL)
                   2941:                expmode = rcs_kwexp_get(rf);
                   2942:        else
                   2943:                expmode = RCS_KWEXP_DEFAULT;
                   2944:
                   2945:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2946:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.5       ray      2947:                        errx(1, "could not fetch revision");
1.12      niallo   2948:                return (rcs_expand_keywords(rf->rf_path, rdp, bp, expmode));
1.1       joris    2949:        }
                   2950:        return (bp);
                   2951: }