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

Annotation of src/usr.bin/cvs/rcs.c, Revision 1.197

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