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

1.184   ! xsa         1: /*     $OpenBSD: rcs.c,v 1.183 2006/06/06 05:13:39 joris 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.131     niallo    238: static char*   rcs_expand_keywords(char *, struct rcs_delta *, char *,
1.125     niallo    239:                     size_t, int);
1.26      jfb       240:
1.60      xsa       241: RCSFILE *
1.172     joris     242: rcs_open(const char *path, int fd, int flags, ...)
1.1       jfb       243: {
1.172     joris     244:        int mode;
1.26      jfb       245:        mode_t fmode;
1.1       jfb       246:        RCSFILE *rfp;
1.26      jfb       247:        va_list vap;
1.109     joris     248:        struct rcs_delta *rdp;
                    249:        struct rcs_lock *lkr;
1.26      jfb       250:
1.152     niallo    251:        fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.26      jfb       252:        flags &= 0xffff;        /* ditch any internal flags */
1.1       jfb       253:
1.172     joris     254:        if (flags & RCS_CREATE) {
                    255:                va_start(vap, flags);
                    256:                mode = va_arg(vap, int);
                    257:                va_end(vap);
                    258:                fmode = (mode_t)mode;
1.1       jfb       259:        }
                    260:
1.161     ray       261:        rfp = xcalloc(1, sizeof(*rfp));
1.1       jfb       262:
1.110     joris     263:        rfp->rf_path = xstrdup(path);
1.142     niallo    264:        rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
1.26      jfb       265:        rfp->rf_mode = fmode;
1.172     joris     266:        rfp->fd = fd;
1.175     joris     267:        rfp->rf_dead = 0;
1.177     joris     268:        rfp->rf_inattic = 0;
1.1       jfb       269:
                    270:        TAILQ_INIT(&(rfp->rf_delta));
1.29      jfb       271:        TAILQ_INIT(&(rfp->rf_access));
1.1       jfb       272:        TAILQ_INIT(&(rfp->rf_symbols));
                    273:        TAILQ_INIT(&(rfp->rf_locks));
                    274:
1.167     ray       275:        if (!(rfp->rf_flags & RCS_CREATE))
                    276:                rcs_parse_init(rfp);
1.1       jfb       277:
1.109     joris     278:        /* fill in rd_locker */
                    279:        TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
                    280:                if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
                    281:                        rcs_close(rfp);
                    282:                        return (NULL);
                    283:                }
                    284:
1.110     joris     285:                rdp->rd_locker = xstrdup(lkr->rl_name);
1.109     joris     286:        }
                    287:
1.1       jfb       288:        return (rfp);
                    289: }
                    290:
                    291: /*
                    292:  * rcs_close()
                    293:  *
                    294:  * Close an RCS file handle.
                    295:  */
                    296: void
                    297: rcs_close(RCSFILE *rfp)
                    298: {
                    299:        struct rcs_delta *rdp;
1.71      moritz    300:        struct rcs_access *rap;
1.13      jfb       301:        struct rcs_lock *rlp;
                    302:        struct rcs_sym *rsp;
1.1       jfb       303:
1.26      jfb       304:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    305:                rcs_write(rfp);
                    306:
1.1       jfb       307:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    308:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    309:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    310:                rcs_freedelta(rdp);
1.71      moritz    311:        }
                    312:
                    313:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    314:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    315:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
1.110     joris     316:                xfree(rap->ra_name);
                    317:                xfree(rap);
1.1       jfb       318:        }
                    319:
1.13      jfb       320:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    321:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    322:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    323:                rcsnum_free(rsp->rs_num);
1.110     joris     324:                xfree(rsp->rs_name);
                    325:                xfree(rsp);
1.13      jfb       326:        }
                    327:
                    328:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    329:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    330:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    331:                rcsnum_free(rlp->rl_num);
1.110     joris     332:                xfree(rlp->rl_name);
                    333:                xfree(rlp);
1.13      jfb       334:        }
                    335:
1.1       jfb       336:        if (rfp->rf_head != NULL)
                    337:                rcsnum_free(rfp->rf_head);
1.11      joris     338:        if (rfp->rf_branch != NULL)
                    339:                rcsnum_free(rfp->rf_branch);
1.1       jfb       340:
                    341:        if (rfp->rf_path != NULL)
1.110     joris     342:                xfree(rfp->rf_path);
1.1       jfb       343:        if (rfp->rf_comment != NULL)
1.110     joris     344:                xfree(rfp->rf_comment);
1.1       jfb       345:        if (rfp->rf_expand != NULL)
1.110     joris     346:                xfree(rfp->rf_expand);
1.1       jfb       347:        if (rfp->rf_desc != NULL)
1.110     joris     348:                xfree(rfp->rf_desc);
1.118     joris     349:        if (rfp->rf_pdata != NULL)
                    350:                rcs_freepdata(rfp->rf_pdata);
1.110     joris     351:        xfree(rfp);
1.1       jfb       352: }
                    353:
                    354: /*
                    355:  * rcs_write()
                    356:  *
                    357:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    358:  * path is in <rf_path>.
                    359:  */
1.172     joris     360: void
1.1       jfb       361: rcs_write(RCSFILE *rfp)
                    362: {
                    363:        FILE *fp;
1.172     joris     364:        char buf[1024], numbuf[64], *fn;
1.29      jfb       365:        struct rcs_access *ap;
1.1       jfb       366:        struct rcs_sym *symp;
1.46      jfb       367:        struct rcs_branch *brp;
1.1       jfb       368:        struct rcs_delta *rdp;
1.75      joris     369:        struct rcs_lock *lkp;
1.100     xsa       370:        size_t len;
1.72      niallo    371:        int fd, from_fd, to_fd;
1.75      joris     372:
1.72      niallo    373:        from_fd = to_fd = fd = -1;
1.1       jfb       374:
1.137     joris     375:        if (rfp->rf_flags & RCS_SYNCED)
1.181     joris     376:                return;
                    377:
                    378:        if (cvs_noexec == 1)
1.172     joris     379:                return;
1.137     joris     380:
1.117     niallo    381:        /* Write operations need the whole file parsed */
                    382:        rcs_parse_deltatexts(rfp, NULL);
1.1       jfb       383:
1.172     joris     384:        (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", cvs_tmpdir);
                    385:
1.113     xsa       386:        if ((fd = mkstemp(fn)) == -1)
1.172     joris     387:                fatal("%s", fn);
1.113     xsa       388:
                    389:        if ((fp = fdopen(fd, "w+")) == NULL) {
1.172     joris     390:                int saved_errno;
                    391:
                    392:                saved_errno = errno;
                    393:                (void)unlink(fn);
                    394:                errno = saved_errno;
                    395:                fatal("%s", fn);
1.1       jfb       396:        }
                    397:
1.28      jfb       398:        if (rfp->rf_head != NULL)
                    399:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    400:        else
                    401:                numbuf[0] = '\0';
                    402:
1.1       jfb       403:        fprintf(fp, "head\t%s;\n", numbuf);
1.35      jfb       404:
                    405:        if (rfp->rf_branch != NULL) {
                    406:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    407:                fprintf(fp, "branch\t%s;\n", numbuf);
                    408:        }
                    409:
1.29      jfb       410:        fputs("access", fp);
                    411:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    412:                fprintf(fp, "\n\t%s", ap->ra_name);
                    413:        }
                    414:        fputs(";\n", fp);
1.1       jfb       415:
1.85      joris     416:        fprintf(fp, "symbols");
1.1       jfb       417:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    418:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.172     joris     419:                if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
                    420:                    strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
                    421:                    strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
                    422:                        fatal("rcs_write: string overflow");
1.85      joris     423:                fprintf(fp, "\n\t%s", buf);
1.1       jfb       424:        }
                    425:        fprintf(fp, ";\n");
                    426:
1.75      joris     427:        fprintf(fp, "locks");
                    428:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    429:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    430:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    431:        }
                    432:
                    433:        fprintf(fp, ";");
1.1       jfb       434:
1.26      jfb       435:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       436:                fprintf(fp, " strict;");
                    437:        fputc('\n', fp);
                    438:
1.129     xsa       439:        fputs("comment\t@", fp);
1.42      jfb       440:        if (rfp->rf_comment != NULL) {
1.58      xsa       441:                rcs_strprint((const u_char *)rfp->rf_comment,
                    442:                    strlen(rfp->rf_comment), fp);
1.42      jfb       443:                fputs("@;\n", fp);
1.129     xsa       444:        } else
                    445:                fputs("# @;\n", fp);
1.1       jfb       446:
1.42      jfb       447:        if (rfp->rf_expand != NULL) {
                    448:                fputs("expand @", fp);
1.58      xsa       449:                rcs_strprint((const u_char *)rfp->rf_expand,
                    450:                    strlen(rfp->rf_expand), fp);
1.42      jfb       451:                fputs("@;\n", fp);
                    452:        }
1.1       jfb       453:
1.46      jfb       454:        fputs("\n\n", fp);
1.1       jfb       455:
                    456:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    457:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    458:                    sizeof(numbuf)));
                    459:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44      jfb       460:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1       jfb       461:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    462:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    463:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    464:                    rdp->rd_author, rdp->rd_state);
1.46      jfb       465:                fputs("branches", fp);
                    466:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                    467:                        fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
                    468:                            sizeof(numbuf)));
                    469:                }
                    470:                fputs(";\n", fp);
1.1       jfb       471:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    472:                    numbuf, sizeof(numbuf)));
                    473:        }
                    474:
1.42      jfb       475:        fputs("\ndesc\n@", fp);
1.141     ray       476:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
1.100     xsa       477:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    478:                if (rfp->rf_desc[len-1] != '\n')
                    479:                        fputc('\n', fp);
                    480:        }
                    481:        fputs("@\n", fp);
1.1       jfb       482:
                    483:        /* deltatexts */
                    484:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.100     xsa       485:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
1.1       jfb       486:                    sizeof(numbuf)));
1.42      jfb       487:                fputs("log\n@", fp);
1.100     xsa       488:                if (rdp->rd_log != NULL) {
                    489:                        len = strlen(rdp->rd_log);
                    490:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
                    491:                        if (rdp->rd_log[len-1] != '\n')
                    492:                                fputc('\n', fp);
                    493:                }
1.42      jfb       494:                fputs("@\ntext\n@", fp);
1.100     xsa       495:                if (rdp->rd_text != NULL) {
                    496:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
1.103     joris     497:
                    498:                        if (rdp->rd_tlen != 0) {
                    499:                                if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
                    500:                                        fputc('\n', fp);
                    501:                        }
1.100     xsa       502:                }
                    503:                fputs("@\n", fp);
1.1       jfb       504:        }
1.172     joris     505:        (void)fclose(fp);
1.73      joris     506:
1.172     joris     507:        if (rcs_movefile(fn, rfp->rf_path, rfp->rf_mode, rfp->rf_flags) == -1) {
                    508:                (void)unlink(fn);
                    509:                fatal("rcs_movefile failed");
                    510:        }
1.73      joris     511:
1.172     joris     512:        rfp->rf_flags |= RCS_SYNCED;
1.73      joris     513:
1.172     joris     514:        if (fn != NULL)
                    515:                xfree(fn);
                    516: }
1.73      joris     517:
1.172     joris     518: /*
                    519:  * rcs_movefile()
                    520:  *
                    521:  * Move a file using rename(2) if possible and copying if not.
                    522:  * Returns 0 on success, -1 on failure.
                    523:  */
                    524: static int
                    525: rcs_movefile(char *from, char *to, mode_t perm, u_int to_flags)
                    526: {
                    527:        FILE *src, *dst;
                    528:        size_t nread, nwritten;
                    529:        char *buf;
                    530:        int ret;
1.73      joris     531:
1.172     joris     532:        ret = -1;
1.73      joris     533:
1.172     joris     534:        if (rename(from, to) == 0) {
                    535:                if (chmod(to, perm) == -1) {
                    536:                        cvs_log(LP_ERRNO, "%s", to);
1.72      niallo    537:                        return (-1);
                    538:                }
1.172     joris     539:                return (0);
                    540:        } else if (errno != EXDEV) {
                    541:                cvs_log(LP_NOTICE, "failed to access temp RCS output file");
                    542:                return (-1);
                    543:        }
                    544:
                    545:        if ((chmod(to, S_IWUSR) == -1) && !(to_flags & RCS_CREATE)) {
                    546:                cvs_log(LP_ERR, "chmod(%s, 0%o) failed", to, S_IWUSR);
                    547:                return (-1);
1.72      niallo    548:        }
1.73      joris     549:
1.172     joris     550:        /* different filesystem, have to copy the file */
                    551:        if ((src = fopen(from, "r")) == NULL) {
                    552:                cvs_log(LP_ERRNO, "%s", from);
                    553:                return (-1);
                    554:        }
                    555:        if ((dst = fopen(to, "w")) == NULL) {
                    556:                cvs_log(LP_ERRNO, "%s", to);
                    557:                return (-1);
                    558:        }
                    559:        if (fchmod(fileno(dst), perm)) {
                    560:                cvs_log(LP_ERR, "%s", to);
                    561:                (void)unlink(to);
1.72      niallo    562:                return (-1);
                    563:        }
1.73      joris     564:
1.172     joris     565:        buf = xmalloc(MAXBSIZE);
                    566:        while ((nread = fread(buf, sizeof(char), MAXBSIZE, src)) != 0) {
                    567:                if (ferror(src)) {
                    568:                        cvs_log(LP_ERRNO, "failed to read `%s'", from);
                    569:                        (void)unlink(to);
                    570:                        goto out;
                    571:                }
                    572:                nwritten = fwrite(buf, sizeof(char), nread, dst);
                    573:                if (nwritten != nread) {
                    574:                        cvs_log(LP_ERRNO, "failed to write `%s'", to);
                    575:                        (void)unlink(to);
                    576:                        goto out;
                    577:                }
                    578:        }
                    579:
                    580:        ret = 0;
                    581:
                    582:        (void)fclose(src);
                    583:        (void)fclose(dst);
                    584:        (void)unlink(from);
                    585:
                    586: out:
                    587:        xfree(buf);
1.1       jfb       588:
1.172     joris     589:        return (ret);
1.1       jfb       590: }
                    591:
                    592: /*
1.43      jfb       593:  * rcs_head_get()
                    594:  *
                    595:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    596:  */
1.180     joris     597: RCSNUM *
1.43      jfb       598: rcs_head_get(RCSFILE *file)
                    599: {
1.180     joris     600:        char br[16];
                    601:
                    602:        if (file->rf_branch != NULL) {
                    603:                rcsnum_tostr(file->rf_branch, br, sizeof(br));
                    604:                return (rcs_translate_tag(br, file));
                    605:        }
                    606:
1.43      jfb       607:        return (file->rf_head);
                    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.1       jfb      1082:
1.94      joris    1083:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1084:        lp = TAILQ_FIRST(&(plines->l_lines));
1.1       jfb      1085:
                   1086:        /* skip first bogus line */
1.94      joris    1087:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1088:            lp = TAILQ_NEXT(lp, l_list)) {
                   1089:                op = *(lp->l_line);
                   1090:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
1.168     deraadt  1091:                if (lineno > dlines->l_nblines || lineno < 0 ||
                   1092:                    *ep != ' ')
1.180     joris    1093:                        fatal("invalid line specification in RCS patch: %s",
                   1094:                            ep);
1.1       jfb      1095:                ep++;
                   1096:                nbln = (int)strtol(ep, &ep, 10);
1.168     deraadt  1097:                if (nbln < 0 || *ep != '\0')
1.132     niallo   1098:                        fatal("invalid line number specification in RCS patch");
1.1       jfb      1099:
                   1100:                /* find the appropriate line */
                   1101:                for (;;) {
                   1102:                        if (dlp == NULL)
                   1103:                                break;
1.94      joris    1104:                        if (dlp->l_lineno == lineno)
1.1       jfb      1105:                                break;
1.94      joris    1106:                        if (dlp->l_lineno > lineno) {
                   1107:                                dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
                   1108:                        } else if (dlp->l_lineno < lineno) {
1.133     niallo   1109:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.168     deraadt  1110:                                    ndlp->l_lineno > lineno)
1.1       jfb      1111:                                        break;
                   1112:                                dlp = ndlp;
                   1113:                        }
                   1114:                }
1.132     niallo   1115:                if (dlp == NULL)
                   1116:                        fatal("can't find referenced line in RCS patch");
1.1       jfb      1117:
                   1118:                if (op == 'd') {
                   1119:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94      joris    1120:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1121:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.1       jfb      1122:                                dlp = ndlp;
1.133     niallo   1123:                                /* last line is gone - reset dlp */
                   1124:                                if (dlp == NULL) {
                   1125:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1126:                                            cvs_tqh);
                   1127:                                        dlp = ndlp;
                   1128:                                }
1.1       jfb      1129:                        }
1.14      deraadt  1130:                } else if (op == 'a') {
1.1       jfb      1131:                        for (i = 0; i < nbln; i++) {
                   1132:                                ndlp = lp;
1.94      joris    1133:                                lp = TAILQ_NEXT(lp, l_list);
1.132     niallo   1134:                                if (lp == NULL)
                   1135:                                        fatal("truncated RCS patch");
1.94      joris    1136:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1137:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1138:                                    lp, l_list);
1.1       jfb      1139:                                dlp = lp;
                   1140:
                   1141:                                /* we don't want lookup to block on those */
1.94      joris    1142:                                lp->l_lineno = lineno;
1.1       jfb      1143:
                   1144:                                lp = ndlp;
                   1145:                        }
1.132     niallo   1146:                } else
                   1147:                        fatal("unknown RCS patch operation `%c'", op);
1.1       jfb      1148:
                   1149:                /* last line of the patch, done */
1.94      joris    1150:                if (lp->l_lineno == plines->l_nblines)
1.1       jfb      1151:                        break;
                   1152:        }
                   1153:
                   1154:        /* once we're done patching, rebuild the line numbers */
1.2       vincent  1155:        lineno = 0;
1.94      joris    1156:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1157:                lp->l_lineno = lineno++;
                   1158:        dlines->l_nblines = lineno - 1;
1.1       jfb      1159:
1.5       vincent  1160:        return (0);
1.1       jfb      1161: }
                   1162:
                   1163: /*
                   1164:  * rcs_getrev()
                   1165:  *
                   1166:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent  1167:  * returned buffer is dynamically allocated and should be released using
                   1168:  * cvs_buf_free() once the caller is done using it.
1.1       jfb      1169:  */
                   1170: BUF*
1.66      joris    1171: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1       jfb      1172: {
1.180     joris    1173:        size_t i;
                   1174:        int done, nextroot, found;
                   1175:        BUF *rcsbuf;
                   1176:        RCSNUM *tnum, *bnum;
                   1177:        struct rcs_branch *brp;
                   1178:        struct rcs_delta *hrdp, *trdp, *rdp;
                   1179:        char *data;
1.1       jfb      1180:
1.180     joris    1181:        if ((hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
                   1182:                fatal("rcs_getrev: no HEAD revision");
1.66      joris    1183:
1.180     joris    1184:        tnum = frev;
                   1185:        rcs_parse_deltatexts(rfp, hrdp->rd_num);
1.28      jfb      1186:
1.180     joris    1187:        /* revision on branch, get the branch root */
                   1188:        nextroot = 2;
                   1189:        if (RCSNUM_ISBRANCHREV(tnum)) {
                   1190:                bnum = rcsnum_alloc();
                   1191:                rcsnum_cpy(tnum, bnum, nextroot);
                   1192:        } else {
                   1193:                bnum = tnum;
1.26      jfb      1194:        }
                   1195:
1.180     joris    1196:        rcsbuf = cvs_buf_alloc(hrdp->rd_tlen, BUF_AUTOEXT);
                   1197:        cvs_buf_append(rcsbuf, hrdp->rd_text, hrdp->rd_tlen);
                   1198:
                   1199:        done = 0;
                   1200:
                   1201:        rdp = hrdp;
                   1202:        if (!rcsnum_differ(rdp->rd_num, bnum))
                   1203:                goto next;
1.117     niallo   1204:
1.180     joris    1205:        if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
                   1206:                return (rcsbuf);
1.156     joris    1207:
1.180     joris    1208: again:
                   1209:        for (;;) {
                   1210:                if (rdp->rd_next->rn_len != 0) {
                   1211:                        trdp = rcs_findrev(rfp, rdp->rd_next);
                   1212:                        if (trdp == NULL)
                   1213:                                fatal("failed to grab next revision");
                   1214:                }
1.156     joris    1215:
1.180     joris    1216:                if (rdp->rd_tlen == 0) {
                   1217:                        rcs_parse_deltatexts(rfp, rdp->rd_num);
                   1218:                        if (rdp->rd_tlen == 0) {
                   1219:                                if (!rcsnum_differ(rdp->rd_num, bnum))
                   1220:                                        break;
                   1221:                                rdp = trdp;
                   1222:                                continue;
                   1223:                        }
                   1224:                }
1.26      jfb      1225:
1.180     joris    1226:                cvs_buf_putc(rcsbuf, '\0');
                   1227:                data = cvs_buf_release(rcsbuf);
1.26      jfb      1228:
1.180     joris    1229:                rcsbuf = cvs_patchfile(data, rdp->rd_text, rcs_patch_lines);
                   1230:                if (rcsbuf == NULL)
                   1231:                        fatal("rcs_getrev: failed to apply rcsdiff");
                   1232:                xfree(data);
1.156     joris    1233:
1.180     joris    1234:                if (!rcsnum_differ(rdp->rd_num, bnum))
                   1235:                        break;
1.156     joris    1236:
1.180     joris    1237:                rdp = trdp;
1.156     joris    1238:        }
                   1239:
1.180     joris    1240: next:
                   1241:        if (!rcsnum_differ(rdp->rd_num, frev))
                   1242:                done = 1;
1.156     joris    1243:
1.180     joris    1244:        if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
                   1245:                nextroot += 2;
                   1246:                rcsnum_cpy(frev, bnum, nextroot);
1.156     joris    1247:
1.180     joris    1248:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                   1249:                        found = 1;
                   1250:                        for (i = 0; i < nextroot - 1; i++) {
                   1251:                                if (brp->rb_num->rn_id[i] != bnum->rn_id[i]) {
                   1252:                                        found = 0;
1.156     joris    1253:                                        break;
                   1254:                                }
                   1255:                        }
1.180     joris    1256:
                   1257:                        break;
1.156     joris    1258:                }
                   1259:
1.180     joris    1260:                if (brp == NULL)
                   1261:                        fatal("expected branch not found on branch list");
1.156     joris    1262:
1.180     joris    1263:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
                   1264:                        fatal("rcs_getrev: failed to get delta for target rev");
1.156     joris    1265:
1.180     joris    1266:                goto again;
                   1267:        }
1.156     joris    1268:
1.180     joris    1269:        if (bnum != tnum)
                   1270:                rcsnum_free(bnum);
1.156     joris    1271:
1.180     joris    1272:        return (rcsbuf);
1.1       jfb      1273: }
                   1274:
                   1275: /*
1.52      jfb      1276:  * rcs_rev_add()
                   1277:  *
1.53      jfb      1278:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1279:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1280:  * new revision will have a number equal to the previous head revision plus
                   1281:  * one).  The <msg> argument specifies the log message for that revision, and
                   1282:  * <date> specifies the revision's date (a value of -1 is
                   1283:  * equivalent to using the current time).
1.96      xsa      1284:  * If <username> is NULL, set the author for this revision to the current user.
1.90      niallo   1285:  * Otherwise, set it to <username>.
1.52      jfb      1286:  * Returns 0 on success, or -1 on failure.
                   1287:  */
                   1288: int
1.90      niallo   1289: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1290:     const char *username)
1.52      jfb      1291: {
                   1292:        time_t now;
                   1293:        struct passwd *pw;
1.180     joris    1294:        struct rcs_branch *brp;
1.83      joris    1295:        struct rcs_delta *ordp, *rdp;
                   1296:
1.52      jfb      1297:        if (rev == RCS_HEAD_REV) {
1.101     niallo   1298:                if (rf->rf_flags & RCS_CREATE) {
                   1299:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1300:                                return (-1);
1.111     joris    1301:                        rf->rf_head = rcsnum_alloc();
1.101     niallo   1302:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1303:                } else {
                   1304:                        rev = rcsnum_inc(rf->rf_head);
                   1305:                }
1.83      joris    1306:        } else {
                   1307:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1308:                        rcs_errno = RCS_ERR_DUPENT;
                   1309:                        return (-1);
                   1310:                }
1.52      jfb      1311:        }
                   1312:
1.112     xsa      1313:        if ((pw = getpwuid(getuid())) == NULL)
                   1314:                fatal("getpwuid failed");
1.52      jfb      1315:
1.161     ray      1316:        rdp = xcalloc(1, sizeof(*rdp));
1.52      jfb      1317:
                   1318:        TAILQ_INIT(&(rdp->rd_branches));
                   1319:
1.111     joris    1320:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1321:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1322:
1.111     joris    1323:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1324:
1.90      niallo   1325:        if (username == NULL)
                   1326:                username = pw->pw_name;
                   1327:
1.110     joris    1328:        rdp->rd_author = xstrdup(username);
                   1329:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1330:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1331:
1.53      jfb      1332:        if (date != (time_t)(-1))
                   1333:                now = date;
                   1334:        else
                   1335:                time(&now);
1.52      jfb      1336:        gmtime_r(&now, &(rdp->rd_date));
                   1337:
1.180     joris    1338:        if (RCSNUM_ISBRANCHREV(rev))
                   1339:                TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
                   1340:        else
                   1341:                TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1.52      jfb      1342:        rf->rf_ndelta++;
1.81      niallo   1343:
1.180     joris    1344:        if (!(rf->rf_flags & RCS_CREATE)) {
                   1345:                if (RCSNUM_ISBRANCHREV(rev)) {
                   1346:                        brp = xmalloc(sizeof(*brp));
                   1347:                        brp->rb_num = rcsnum_alloc();
                   1348:                        rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
                   1349:                        TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   1350:
                   1351:                        ordp = TAILQ_PREV(rdp, cvs_tqh, rd_list);
                   1352:                        rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
                   1353:                } else {
                   1354:                        ordp = TAILQ_NEXT(rdp, rd_list);
                   1355:                        rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
                   1356:                }
                   1357:        }
                   1358:
1.64      niallo   1359:        /* not synced anymore */
                   1360:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1361:
                   1362:        return (0);
                   1363: }
                   1364:
                   1365: /*
                   1366:  * rcs_rev_remove()
                   1367:  *
                   1368:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1369:  */
                   1370: int
                   1371: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1372: {
1.172     joris    1373:        char *newdeltatext, *path_tmp1, *path_tmp2;
1.166     joris    1374:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
                   1375:        BUF *nextbuf, *prevbuf, *newdiff;
                   1376:
1.52      jfb      1377:        if (rev == RCS_HEAD_REV)
                   1378:                rev = rf->rf_head;
                   1379:
                   1380:        /* do we actually have that revision? */
                   1381:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1382:                rcs_errno = RCS_ERR_NOENT;
1.166     joris    1383:                return (-1);
                   1384:        }
                   1385:
                   1386:        /*
                   1387:         * This is confusing, the previous delta is next in the TAILQ list.
                   1388:         * the next delta is the previous one in the TAILQ list.
                   1389:         *
                   1390:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1391:         * When the first revision got specified, prevrdp will be NULL.
                   1392:         */
                   1393:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
                   1394:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
                   1395:
                   1396:        newdeltatext = NULL;
                   1397:        prevbuf = nextbuf = NULL;
                   1398:
                   1399:        if (prevrdp != NULL) {
                   1400:                if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
                   1401:                        fatal("error getting revision");
                   1402:        }
                   1403:
1.168     deraadt  1404:        if (prevrdp != NULL && nextrdp != NULL) {
1.166     joris    1405:                if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
                   1406:                        fatal("error getting revision");
                   1407:
                   1408:                newdiff = cvs_buf_alloc(64, BUF_AUTOEXT);
                   1409:
                   1410:                /* calculate new diff */
1.172     joris    1411:                (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
                   1412:                cvs_buf_write_stmp(nextbuf, path_tmp1, 0600, NULL);
1.166     joris    1413:                cvs_buf_free(nextbuf);
                   1414:
1.172     joris    1415:                (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                   1416:                cvs_buf_write_stmp(prevbuf, path_tmp2, 0600, NULL);
1.166     joris    1417:                cvs_buf_free(prevbuf);
                   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:
                   1423:                newdeltatext = cvs_buf_release(newdiff);
1.168     deraadt  1424:        } else if (nextrdp == NULL && prevrdp != NULL) {
1.166     joris    1425:                newdeltatext = cvs_buf_release(prevbuf);
                   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:
1.161     ray      1877:        rdp = xcalloc(1, sizeof(*rdp));
1.1       jfb      1878:
                   1879:        rdp->rd_num = rcsnum_alloc();
                   1880:        rdp->rd_next = rcsnum_alloc();
                   1881:
                   1882:        TAILQ_INIT(&(rdp->rd_branches));
                   1883:
                   1884:        tok = rcs_gettok(rfp);
1.154     ray      1885:        if (tok == RCS_TOK_DESC) {
                   1886:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1887:                return (0);
                   1888:        } else if (tok != RCS_TOK_NUM) {
1.52      jfb      1889:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1890:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1891:                    RCS_TOKSTR(rfp));
                   1892:                rcs_freedelta(rdp);
                   1893:                return (-1);
                   1894:        }
                   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.161     ray      2105:        rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.172     joris    2106:        if (strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1)) >=
                   2107:            RCS_TOKLEN(rfp) + 1)
                   2108:                fatal("rcs_parse_deltatext: strlcpy");
1.42      jfb      2109:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2110:
                   2111:        return (1);
                   2112: }
                   2113:
                   2114: /*
                   2115:  * rcs_parse_access()
                   2116:  *
                   2117:  * Parse the access list given as value to the `access' keyword.
                   2118:  * Returns 0 on success, or -1 on failure.
                   2119:  */
                   2120: static int
                   2121: rcs_parse_access(RCSFILE *rfp)
                   2122: {
                   2123:        int type;
                   2124:
                   2125:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2126:                if (type != RCS_TOK_ID) {
1.50      jfb      2127:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2128:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2129:                            RCS_TOKSTR(rfp));
                   2130:                        return (-1);
                   2131:                }
1.29      jfb      2132:
                   2133:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2134:                        return (-1);
1.1       jfb      2135:        }
                   2136:
                   2137:        return (0);
                   2138: }
                   2139:
                   2140: /*
                   2141:  * rcs_parse_symbols()
                   2142:  *
                   2143:  * Parse the symbol list given as value to the `symbols' keyword.
                   2144:  * Returns 0 on success, or -1 on failure.
                   2145:  */
                   2146: static int
                   2147: rcs_parse_symbols(RCSFILE *rfp)
                   2148: {
                   2149:        int type;
                   2150:        struct rcs_sym *symp;
                   2151:
                   2152:        for (;;) {
                   2153:                type = rcs_gettok(rfp);
                   2154:                if (type == RCS_TOK_SCOLON)
                   2155:                        break;
                   2156:
1.41      jfb      2157:                if (type != RCS_TOK_ID) {
1.50      jfb      2158:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2159:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2160:                            RCS_TOKSTR(rfp));
                   2161:                        return (-1);
                   2162:                }
                   2163:
1.161     ray      2164:                symp = xmalloc(sizeof(*symp));
1.110     joris    2165:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2166:                symp->rs_num = rcsnum_alloc();
                   2167:
                   2168:                type = rcs_gettok(rfp);
                   2169:                if (type != RCS_TOK_COLON) {
1.50      jfb      2170:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2171:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2172:                            RCS_TOKSTR(rfp));
1.11      joris    2173:                        rcsnum_free(symp->rs_num);
1.110     joris    2174:                        xfree(symp->rs_name);
                   2175:                        xfree(symp);
1.1       jfb      2176:                        return (-1);
                   2177:                }
                   2178:
                   2179:                type = rcs_gettok(rfp);
                   2180:                if (type != RCS_TOK_NUM) {
1.50      jfb      2181:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2182:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2183:                            RCS_TOKSTR(rfp));
1.11      joris    2184:                        rcsnum_free(symp->rs_num);
1.110     joris    2185:                        xfree(symp->rs_name);
                   2186:                        xfree(symp);
1.1       jfb      2187:                        return (-1);
                   2188:                }
                   2189:
                   2190:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2191:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2192:                            RCS_TOKSTR(rfp));
1.11      joris    2193:                        rcsnum_free(symp->rs_num);
1.110     joris    2194:                        xfree(symp->rs_name);
                   2195:                        xfree(symp);
1.1       jfb      2196:                        return (-1);
                   2197:                }
                   2198:
1.43      jfb      2199:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2200:        }
                   2201:
                   2202:        return (0);
                   2203: }
                   2204:
                   2205: /*
                   2206:  * rcs_parse_locks()
                   2207:  *
                   2208:  * Parse the lock list given as value to the `locks' keyword.
                   2209:  * Returns 0 on success, or -1 on failure.
                   2210:  */
                   2211: static int
                   2212: rcs_parse_locks(RCSFILE *rfp)
                   2213: {
                   2214:        int type;
                   2215:        struct rcs_lock *lkp;
                   2216:
                   2217:        for (;;) {
                   2218:                type = rcs_gettok(rfp);
                   2219:                if (type == RCS_TOK_SCOLON)
                   2220:                        break;
                   2221:
                   2222:                if (type != RCS_TOK_ID) {
1.50      jfb      2223:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2224:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2225:                            RCS_TOKSTR(rfp));
                   2226:                        return (-1);
                   2227:                }
                   2228:
1.161     ray      2229:                lkp = xmalloc(sizeof(*lkp));
1.110     joris    2230:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2231:                lkp->rl_num = rcsnum_alloc();
                   2232:
                   2233:                type = rcs_gettok(rfp);
                   2234:                if (type != RCS_TOK_COLON) {
1.50      jfb      2235:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2236:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2237:                            RCS_TOKSTR(rfp));
1.37      tedu     2238:                        rcsnum_free(lkp->rl_num);
1.110     joris    2239:                        xfree(lkp->rl_name);
                   2240:                        xfree(lkp);
1.1       jfb      2241:                        return (-1);
                   2242:                }
                   2243:
                   2244:                type = rcs_gettok(rfp);
                   2245:                if (type != RCS_TOK_NUM) {
1.50      jfb      2246:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2247:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2248:                            RCS_TOKSTR(rfp));
1.37      tedu     2249:                        rcsnum_free(lkp->rl_num);
1.110     joris    2250:                        xfree(lkp->rl_name);
                   2251:                        xfree(lkp);
1.1       jfb      2252:                        return (-1);
                   2253:                }
                   2254:
                   2255:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2256:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2257:                            RCS_TOKSTR(rfp));
1.37      tedu     2258:                        rcsnum_free(lkp->rl_num);
1.110     joris    2259:                        xfree(lkp->rl_name);
                   2260:                        xfree(lkp);
1.1       jfb      2261:                        return (-1);
                   2262:                }
                   2263:
                   2264:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2265:        }
                   2266:
                   2267:        /* check if we have a `strict' */
                   2268:        type = rcs_gettok(rfp);
                   2269:        if (type != RCS_TOK_STRICT) {
                   2270:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2271:        } else {
1.26      jfb      2272:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2273:
                   2274:                type = rcs_gettok(rfp);
                   2275:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2276:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2277:                        cvs_log(LP_ERR,
                   2278:                            "missing semi-colon after `strict' keyword");
                   2279:                        return (-1);
                   2280:                }
                   2281:        }
                   2282:
                   2283:        return (0);
                   2284: }
                   2285:
                   2286: /*
                   2287:  * rcs_parse_branches()
                   2288:  *
                   2289:  * Parse the list of branches following a `branches' keyword in a delta.
                   2290:  * Returns 0 on success, or -1 on failure.
                   2291:  */
                   2292: static int
                   2293: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2294: {
                   2295:        int type;
                   2296:        struct rcs_branch *brp;
                   2297:
                   2298:        for (;;) {
                   2299:                type = rcs_gettok(rfp);
                   2300:                if (type == RCS_TOK_SCOLON)
                   2301:                        break;
                   2302:
                   2303:                if (type != RCS_TOK_NUM) {
1.50      jfb      2304:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2305:                        cvs_log(LP_ERR,
                   2306:                            "unexpected token `%s' in list of branches",
                   2307:                            RCS_TOKSTR(rfp));
                   2308:                        return (-1);
                   2309:                }
                   2310:
1.161     ray      2311:                brp = xmalloc(sizeof(*brp));
1.46      jfb      2312:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2313:                if (brp->rb_num == NULL) {
1.110     joris    2314:                        xfree(brp);
1.11      joris    2315:                        return (-1);
                   2316:                }
1.1       jfb      2317:
                   2318:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2319:        }
                   2320:
                   2321:        return (0);
                   2322: }
                   2323:
                   2324: /*
                   2325:  * rcs_freedelta()
                   2326:  *
                   2327:  * Free the contents of a delta structure.
                   2328:  */
1.18      jfb      2329: static void
1.1       jfb      2330: rcs_freedelta(struct rcs_delta *rdp)
                   2331: {
1.12      jfb      2332:        struct rcs_branch *rb;
1.1       jfb      2333:
1.12      jfb      2334:        if (rdp->rd_num != NULL)
                   2335:                rcsnum_free(rdp->rd_num);
                   2336:        if (rdp->rd_next != NULL)
                   2337:                rcsnum_free(rdp->rd_next);
                   2338:
1.1       jfb      2339:        if (rdp->rd_author != NULL)
1.110     joris    2340:                xfree(rdp->rd_author);
1.109     joris    2341:        if (rdp->rd_locker != NULL)
1.110     joris    2342:                xfree(rdp->rd_locker);
1.1       jfb      2343:        if (rdp->rd_state != NULL)
1.110     joris    2344:                xfree(rdp->rd_state);
1.1       jfb      2345:        if (rdp->rd_log != NULL)
1.110     joris    2346:                xfree(rdp->rd_log);
1.1       jfb      2347:        if (rdp->rd_text != NULL)
1.110     joris    2348:                xfree(rdp->rd_text);
1.12      jfb      2349:
                   2350:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2351:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2352:                rcsnum_free(rb->rb_num);
1.110     joris    2353:                xfree(rb);
1.1       jfb      2354:        }
                   2355:
1.110     joris    2356:        xfree(rdp);
1.1       jfb      2357: }
                   2358:
                   2359: /*
                   2360:  * rcs_freepdata()
                   2361:  *
                   2362:  * Free the contents of the parser data structure.
                   2363:  */
                   2364: static void
                   2365: rcs_freepdata(struct rcs_pdata *pd)
                   2366: {
                   2367:        if (pd->rp_file != NULL)
                   2368:                (void)fclose(pd->rp_file);
                   2369:        if (pd->rp_buf != NULL)
1.110     joris    2370:                xfree(pd->rp_buf);
                   2371:        xfree(pd);
1.1       jfb      2372: }
                   2373:
                   2374: /*
                   2375:  * rcs_gettok()
                   2376:  *
                   2377:  * Get the next RCS token from the string <str>.
                   2378:  */
                   2379: static int
                   2380: rcs_gettok(RCSFILE *rfp)
                   2381: {
                   2382:        u_int i;
                   2383:        int ch, last, type;
1.18      jfb      2384:        size_t len;
                   2385:        char *bp;
1.1       jfb      2386:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2387:
                   2388:        type = RCS_TOK_ERR;
                   2389:        bp = pdp->rp_buf;
1.42      jfb      2390:        pdp->rp_tlen = 0;
1.1       jfb      2391:        *bp = '\0';
                   2392:
                   2393:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2394:                type = pdp->rp_pttype;
1.172     joris    2395:                if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
                   2396:                    pdp->rp_blen)
                   2397:                        fatal("rcs_gettok: strlcpy");
1.1       jfb      2398:                pdp->rp_pttype = RCS_TOK_ERR;
                   2399:                return (type);
                   2400:        }
                   2401:
                   2402:        /* skip leading whitespace */
                   2403:        /* XXX we must skip backspace too for compatibility, should we? */
                   2404:        do {
                   2405:                ch = getc(pdp->rp_file);
                   2406:                if (ch == '\n')
1.18      jfb      2407:                        pdp->rp_lines++;
1.1       jfb      2408:        } while (isspace(ch));
                   2409:
                   2410:        if (ch == EOF) {
                   2411:                type = RCS_TOK_EOF;
1.14      deraadt  2412:        } else if (ch == ';') {
1.1       jfb      2413:                type = RCS_TOK_SCOLON;
1.14      deraadt  2414:        } else if (ch == ':') {
1.1       jfb      2415:                type = RCS_TOK_COLON;
1.14      deraadt  2416:        } else if (isalpha(ch)) {
1.31      jfb      2417:                type = RCS_TOK_ID;
1.1       jfb      2418:                *(bp++) = ch;
1.18      jfb      2419:                for (;;) {
1.1       jfb      2420:                        ch = getc(pdp->rp_file);
1.160     joris    2421:                        if (!isalnum(ch) && ch != '_' && ch != '-' &&
                   2422:                            ch != '/') {
1.1       jfb      2423:                                ungetc(ch, pdp->rp_file);
                   2424:                                break;
                   2425:                        }
                   2426:                        *(bp++) = ch;
1.42      jfb      2427:                        pdp->rp_tlen++;
1.18      jfb      2428:                        if (bp == pdp->rp_bufend - 1) {
                   2429:                                len = bp - pdp->rp_buf;
1.151     xsa      2430:                                rcs_growbuf(rfp);
1.18      jfb      2431:                                bp = pdp->rp_buf + len;
                   2432:                        }
1.1       jfb      2433:                }
                   2434:                *bp = '\0';
                   2435:
1.18      jfb      2436:                if (type != RCS_TOK_ERR) {
                   2437:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2438:                                if (strcmp(rcs_keys[i].rk_str,
                   2439:                                    pdp->rp_buf) == 0) {
                   2440:                                        type = rcs_keys[i].rk_id;
                   2441:                                        break;
                   2442:                                }
1.1       jfb      2443:                        }
                   2444:                }
1.14      deraadt  2445:        } else if (ch == '@') {
1.1       jfb      2446:                /* we have a string */
1.18      jfb      2447:                type = RCS_TOK_STRING;
1.1       jfb      2448:                for (;;) {
                   2449:                        ch = getc(pdp->rp_file);
                   2450:                        if (ch == '@') {
                   2451:                                ch = getc(pdp->rp_file);
                   2452:                                if (ch != '@') {
                   2453:                                        ungetc(ch, pdp->rp_file);
                   2454:                                        break;
                   2455:                                }
1.14      deraadt  2456:                        } else if (ch == '\n')
1.18      jfb      2457:                                pdp->rp_lines++;
1.1       jfb      2458:
                   2459:                        *(bp++) = ch;
1.42      jfb      2460:                        pdp->rp_tlen++;
1.18      jfb      2461:                        if (bp == pdp->rp_bufend - 1) {
                   2462:                                len = bp - pdp->rp_buf;
1.151     xsa      2463:                                rcs_growbuf(rfp);
1.18      jfb      2464:                                bp = pdp->rp_buf + len;
                   2465:                        }
1.1       jfb      2466:                }
                   2467:
                   2468:                *bp = '\0';
1.14      deraadt  2469:        } else if (isdigit(ch)) {
1.1       jfb      2470:                *(bp++) = ch;
                   2471:                last = ch;
                   2472:                type = RCS_TOK_NUM;
                   2473:
                   2474:                for (;;) {
                   2475:                        ch = getc(pdp->rp_file);
1.18      jfb      2476:                        if (bp == pdp->rp_bufend)
1.1       jfb      2477:                                break;
                   2478:                        if (!isdigit(ch) && ch != '.') {
                   2479:                                ungetc(ch, pdp->rp_file);
                   2480:                                break;
                   2481:                        }
                   2482:
                   2483:                        if (last == '.' && ch == '.') {
                   2484:                                type = RCS_TOK_ERR;
                   2485:                                break;
                   2486:                        }
                   2487:                        last = ch;
                   2488:                        *(bp++) = ch;
1.42      jfb      2489:                        pdp->rp_tlen++;
1.1       jfb      2490:                }
1.18      jfb      2491:                *bp = '\0';
1.1       jfb      2492:        }
                   2493:
                   2494:        return (type);
                   2495: }
                   2496:
                   2497: /*
                   2498:  * rcs_pushtok()
                   2499:  *
                   2500:  * Push a token back in the parser's token buffer.
                   2501:  */
                   2502: static int
                   2503: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2504: {
                   2505:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2506:
                   2507:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2508:                return (-1);
                   2509:
                   2510:        pdp->rp_pttype = type;
1.172     joris    2511:        if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
                   2512:            sizeof(pdp->rp_ptok))
                   2513:                fatal("rcs_pushtok: strlcpy");
1.1       jfb      2514:        return (0);
                   2515: }
                   2516:
1.18      jfb      2517:
                   2518: /*
                   2519:  * rcs_growbuf()
                   2520:  *
                   2521:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2522:  * RCS_BUFEXTSIZE.
                   2523:  * In case of failure, the original buffer is left unmodified.
                   2524:  */
1.151     xsa      2525: static void
1.18      jfb      2526: rcs_growbuf(RCSFILE *rf)
                   2527: {
                   2528:        void *tmp;
                   2529:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2530:
1.153     ray      2531:        tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
1.161     ray      2532:        pdp->rp_buf = tmp;
1.18      jfb      2533:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2534:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2535: }
                   2536:
                   2537: /*
                   2538:  * rcs_strprint()
                   2539:  *
                   2540:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2541:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2542:  * binary data.
                   2543:  */
1.155     ray      2544: static void
1.42      jfb      2545: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2546: {
                   2547:        const u_char *ap, *ep, *sp;
1.52      jfb      2548:
                   2549:        if (slen == 0)
1.155     ray      2550:                return;
1.42      jfb      2551:
                   2552:        ep = str + slen - 1;
                   2553:
                   2554:        for (sp = str; sp <= ep;)  {
                   2555:                ap = memchr(sp, '@', ep - sp);
                   2556:                if (ap == NULL)
                   2557:                        ap = ep;
1.155     ray      2558:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42      jfb      2559:
                   2560:                if (*ap == '@')
                   2561:                        putc('@', stream);
                   2562:                sp = ap + 1;
1.63      joris    2563:        }
                   2564: }
                   2565:
                   2566: /*
                   2567:  * rcs_expand_keywords()
                   2568:  *
1.125     niallo   2569:  * Return expansion any RCS keywords in <data>
                   2570:  *
                   2571:  * On error, return NULL.
1.63      joris    2572:  */
1.125     niallo   2573: static char *
                   2574: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
1.63      joris    2575:     size_t len, int mode)
                   2576: {
1.155     ray      2577:        ptrdiff_t c_offset, sizdiff, start_offset;
1.147     ray      2578:        size_t i;
1.155     ray      2579:        int kwtype;
                   2580:        u_int j, found;
1.140     xsa      2581:        char *c, *kwstr, *start, *end, *tbuf;
1.125     niallo   2582:        char expbuf[256], buf[256];
1.134     joris    2583:        char *fmt;
1.63      joris    2584:
1.65      niallo   2585:        kwtype = 0;
                   2586:        kwstr = NULL;
1.63      joris    2587:        i = 0;
                   2588:
                   2589:        /*
                   2590:         * Keyword formats:
                   2591:         * $Keyword$
                   2592:         * $Keyword: value$
                   2593:         */
1.144     deraadt  2594:        for (c = data; *c != '\0' && i < len; c++) {
1.63      joris    2595:                if (*c == '$') {
                   2596:                        /* remember start of this possible keyword */
                   2597:                        start = c;
1.125     niallo   2598:                        start_offset = start - data;
1.63      joris    2599:
                   2600:                        /* first following character has to be alphanumeric */
1.144     deraadt  2601:                        c++;
1.63      joris    2602:                        if (!isalpha(*c)) {
                   2603:                                c = start;
                   2604:                                continue;
                   2605:                        }
                   2606:
                   2607:                        /* look for any matching keywords */
                   2608:                        found = 0;
                   2609:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2610:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2611:                                    strlen(rcs_expkw[j].kw_str))) {
                   2612:                                        found = 1;
                   2613:                                        kwstr = rcs_expkw[j].kw_str;
                   2614:                                        kwtype = rcs_expkw[j].kw_type;
                   2615:                                        break;
                   2616:                                }
1.174     joris    2617:                        }
                   2618:
                   2619:                        if (cvs_tagname != NULL &&
                   2620:                            !strncmp(c, cvs_tagname, strlen(cvs_tagname)) &&
                   2621:                            found != 1) {
                   2622:                                found = 1;
                   2623:                                kwstr = cvs_tagname;
                   2624:                                kwtype = RCS_KW_ID;
1.63      joris    2625:                        }
                   2626:
                   2627:                        /* unknown keyword, continue looking */
                   2628:                        if (found == 0) {
                   2629:                                c = start;
                   2630:                                continue;
                   2631:                        }
                   2632:
                   2633:                        /* next character has to be ':' or '$' */
                   2634:                        c += strlen(kwstr);
                   2635:                        if (*c != ':' && *c != '$') {
                   2636:                                c = start;
                   2637:                                continue;
                   2638:                        }
                   2639:
                   2640:                        /*
                   2641:                         * if the next character was ':' we need to look for
                   2642:                         * an '$' before the end of the line to be sure it is
                   2643:                         * in fact a keyword.
                   2644:                         */
                   2645:                        if (*c == ':') {
                   2646:                                while (*c++) {
                   2647:                                        if (*c == '$' || *c == '\n')
                   2648:                                                break;
                   2649:                                }
                   2650:
                   2651:                                if (*c != '$') {
                   2652:                                        c = start;
                   2653:                                        continue;
                   2654:                                }
                   2655:                        }
1.125     niallo   2656:                        c_offset = c - data;
                   2657:                        end = c + 1;
1.63      joris    2658:
                   2659:                        /* start constructing the expansion */
                   2660:                        expbuf[0] = '\0';
                   2661:
                   2662:                        if (mode & RCS_KWEXP_NAME) {
1.172     joris    2663:                                if (strlcat(expbuf, "$", sizeof(expbuf))
                   2664:                                    >= sizeof(expbuf) ||
                   2665:                                    strlcat(expbuf, kwstr, sizeof(expbuf))
                   2666:                                    >= sizeof(expbuf))
                   2667:                                        fatal("rcs_expand_keywords: truncated");
                   2668:                                if ((mode & RCS_KWEXP_VAL) &&
                   2669:                                    strlcat(expbuf, ": ", sizeof(expbuf))
                   2670:                                    >= sizeof(expbuf))
                   2671:                                        fatal("rcs_expand_keywords: truncated");
1.63      joris    2672:                        }
                   2673:
                   2674:                        /*
1.80      reyk     2675:                         * order matters because of RCS_KW_ID and
                   2676:                         * RCS_KW_HEADER here
1.63      joris    2677:                         */
                   2678:                        if (mode & RCS_KWEXP_VAL) {
                   2679:                                if (kwtype & RCS_KW_RCSFILE) {
                   2680:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.172     joris    2681:                                                (void)strlcat(expbuf,
1.80      reyk     2682:                                                    basename(rcsfile),
1.63      joris    2683:                                                    sizeof(expbuf));
                   2684:                                        else
1.172     joris    2685:                                                (void)strlcat(expbuf,
                   2686:                                                    rcsfile, sizeof(expbuf));
                   2687:                                        if (strlcat(expbuf, " ",
                   2688:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2689:                                                fatal("rcs_expand_keywords: "
                   2690:                                                    "truncated");
1.63      joris    2691:                                }
                   2692:
                   2693:                                if (kwtype & RCS_KW_REVISION) {
1.80      reyk     2694:                                        rcsnum_tostr(rdp->rd_num, buf,
                   2695:                                            sizeof(buf));
1.172     joris    2696:                                        if (strlcat(buf, " ", sizeof(buf))
                   2697:                                            >= sizeof(buf) ||
                   2698:                                            strlcat(expbuf, buf,
                   2699:                                            sizeof(expbuf)) >= sizeof(buf))
                   2700:                                                fatal("rcs_expand_keywords: "
                   2701:                                                    "truncated");
1.63      joris    2702:                                }
                   2703:
                   2704:                                if (kwtype & RCS_KW_DATE) {
1.172     joris    2705:                                        fmt = "%Y/%m/%d %H:%M:%S ";
1.134     joris    2706:
1.173     joris    2707:                                        strftime(buf, sizeof(buf), fmt,
                   2708:                                            &rdp->rd_date);
1.172     joris    2709:                                        if (strlcat(expbuf, buf,
                   2710:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2711:                                                fatal("rcs_expand_keywords: "
                   2712:                                                    "string truncated");
1.63      joris    2713:                                }
                   2714:
                   2715:                                if (kwtype & RCS_KW_AUTHOR) {
1.172     joris    2716:                                        if (strlcat(expbuf, rdp->rd_author,
                   2717:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2718:                                            strlcat(expbuf, " ",
                   2719:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2720:                                                fatal("rcs_expand_keywords: "
                   2721:                                                    "string truncated");
1.63      joris    2722:                                }
                   2723:
                   2724:                                if (kwtype & RCS_KW_STATE) {
1.172     joris    2725:                                        if (strlcat(expbuf, rdp->rd_state,
                   2726:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2727:                                            strlcat(expbuf, " ",
                   2728:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2729:                                                fatal("rcs_expand_keywords: "
                   2730:                                                    "string truncated");
1.63      joris    2731:                                }
                   2732:
                   2733:                                /* order does not matter anymore below */
                   2734:                                if (kwtype & RCS_KW_LOG)
1.172     joris    2735:                                        if (strlcat(expbuf, " ",
                   2736:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2737:                                                fatal("rcs_expand_keywords: "
                   2738:                                                    "string truncated");
1.63      joris    2739:
                   2740:                                if (kwtype & RCS_KW_SOURCE) {
1.172     joris    2741:                                        if (strlcat(expbuf, rcsfile,
                   2742:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2743:                                            strlcat(expbuf, " ",
                   2744:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2745:                                                fatal("rcs_expand_keywords: "
                   2746:                                                    "string truncated");
1.63      joris    2747:                                }
                   2748:
                   2749:                                if (kwtype & RCS_KW_NAME)
1.172     joris    2750:                                        if (strlcat(expbuf, " ",
                   2751:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2752:                                                fatal("rcs_expand_keywords: "
                   2753:                                                    "string truncated");
1.63      joris    2754:                        }
                   2755:
                   2756:                        /* end the expansion */
                   2757:                        if (mode & RCS_KWEXP_NAME)
1.172     joris    2758:                                if (strlcat(expbuf, "$",
                   2759:                                    sizeof(expbuf)) >= sizeof(expbuf))
                   2760:                                        fatal("rcs_expand_keywords: truncated");
1.138     deraadt  2761:
1.125     niallo   2762:                        sizdiff = strlen(expbuf) - (end - start);
1.161     ray      2763:                        tbuf = xstrdup(end);
1.172     joris    2764:
1.125     niallo   2765:                        /* only realloc if we have to */
                   2766:                        if (sizdiff > 0) {
1.161     ray      2767:                                char *newdata;
                   2768:
1.125     niallo   2769:                                len += sizdiff;
1.161     ray      2770:                                newdata = xrealloc(data, 1, len);
                   2771:                                data = newdata;
1.172     joris    2772:
1.125     niallo   2773:                                /*
                   2774:                                 * ensure string pointers are not invalidated
                   2775:                                 * after realloc()
                   2776:                                 */
                   2777:                                start = data + start_offset;
                   2778:                                c = data + c_offset;
                   2779:                        }
1.172     joris    2780:                        if (strlcpy(start, expbuf, len) >= len ||
                   2781:                            strlcat(data, tbuf, len) >= len)
                   2782:                                fatal("rcs_expand_keywords: string truncated");
1.125     niallo   2783:                        xfree(tbuf);
1.63      joris    2784:                        i += strlen(expbuf);
                   2785:                }
1.42      jfb      2786:        }
1.81      niallo   2787:
1.125     niallo   2788:        return (data);
1.81      niallo   2789: }
                   2790:
                   2791: /*
                   2792:  * rcs_deltatext_set()
                   2793:  *
                   2794:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      2795:  * Returns -1 on error, 0 on success.
1.81      niallo   2796:  */
                   2797: int
                   2798: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2799: {
                   2800:        size_t len;
                   2801:        struct rcs_delta *rdp;
1.117     niallo   2802:
                   2803:        /* Write operations require full parsing */
                   2804:        rcs_parse_deltatexts(rfp, NULL);
1.81      niallo   2805:
                   2806:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2807:                return (-1);
                   2808:
                   2809:        if (rdp->rd_text != NULL)
1.110     joris    2810:                xfree(rdp->rd_text);
1.81      niallo   2811:
                   2812:        len = strlen(dtext);
1.103     joris    2813:        if (len != 0) {
1.161     ray      2814:                /* XXX - use xstrdup() if rd_text changes to char *. */
                   2815:                rdp->rd_text = xmalloc(len + 1);
1.155     ray      2816:                rdp->rd_tlen = len;
                   2817:                (void)memcpy(rdp->rd_text, dtext, len + 1);
1.103     joris    2818:        } else {
                   2819:                rdp->rd_text = NULL;
                   2820:                rdp->rd_tlen = 0;
                   2821:        }
1.18      jfb      2822:
1.86      joris    2823:        return (0);
                   2824: }
                   2825:
                   2826: /*
                   2827:  * rcs_rev_setlog()
                   2828:  *
                   2829:  * Sets the log message of revision <rev> to <logtext>
                   2830:  */
                   2831: int
                   2832: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2833: {
                   2834:        struct rcs_delta *rdp;
                   2835:        char buf[16];
                   2836:
                   2837:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2838:
                   2839:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2840:                return (-1);
                   2841:
                   2842:        if (rdp->rd_log != NULL)
1.110     joris    2843:                xfree(rdp->rd_log);
1.86      joris    2844:
1.110     joris    2845:        rdp->rd_log = xstrdup(logtext);
1.86      joris    2846:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   2847:        return (0);
                   2848: }
1.97      niallo   2849: /*
                   2850:  * rcs_rev_getdate()
                   2851:  *
                   2852:  * Get the date corresponding to a given revision.
                   2853:  * Returns the date on success, -1 on failure.
                   2854:  */
                   2855: time_t
                   2856: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2857: {
                   2858:        struct rcs_delta *rdp;
                   2859:
                   2860:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2861:                return (-1);
                   2862:
                   2863:        return (mktime(&rdp->rd_date));
                   2864: }
1.95      niallo   2865:
                   2866: /*
                   2867:  * rcs_state_set()
                   2868:  *
                   2869:  * Sets the state of revision <rev> to <state>
                   2870:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2871:  *
                   2872:  * Returns -1 on failure, 0 on success.
                   2873:  */
                   2874: int
                   2875: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2876: {
                   2877:        struct rcs_delta *rdp;
                   2878:
                   2879:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2880:                return (-1);
                   2881:
                   2882:        if (rdp->rd_state != NULL)
1.110     joris    2883:                xfree(rdp->rd_state);
1.95      niallo   2884:
1.110     joris    2885:        rdp->rd_state = xstrdup(state);
1.95      niallo   2886:
                   2887:        rfp->rf_flags &= ~RCS_SYNCED;
                   2888:
                   2889:        return (0);
                   2890: }
                   2891:
                   2892: /*
                   2893:  * rcs_state_check()
                   2894:  *
                   2895:  * Check if string <state> is valid.
                   2896:  *
1.96      xsa      2897:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   2898:  */
                   2899: int
                   2900: rcs_state_check(const char *state)
                   2901: {
                   2902:        if (strchr(state, ' ') != NULL)
                   2903:                return (-1);
                   2904:
1.18      jfb      2905:        return (0);
1.1       jfb      2906: }
1.97      niallo   2907:
                   2908: /*
                   2909:  * rcs_state_get()
                   2910:  *
                   2911:  * Get the state for a given revision of a specified RCSFILE.
                   2912:  *
                   2913:  * Returns NULL on failure.
                   2914:  */
                   2915: const char *
                   2916: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2917: {
                   2918:        struct rcs_delta *rdp;
                   2919:
                   2920:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2921:                return (NULL);
                   2922:
                   2923:        return (rdp->rd_state);
                   2924: }
                   2925:
1.131     niallo   2926: /*
                   2927:  * rcs_kwexp_buf()
                   2928:  *
                   2929:  * Do keyword expansion on a buffer if necessary
                   2930:  *
                   2931:  */
                   2932: BUF *
                   2933: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2934: {
                   2935:        struct rcs_delta *rdp;
                   2936:        char *expanded, *tbuf;
                   2937:        int expmode;
                   2938:        size_t len;
                   2939:
                   2940:        /*
                   2941:         * Do keyword expansion if required.
                   2942:         */
                   2943:        if (rf->rf_expand != NULL)
                   2944:                expmode = rcs_kwexp_get(rf);
                   2945:        else
                   2946:                expmode = RCS_KWEXP_DEFAULT;
                   2947:
                   2948:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2949:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.172     joris    2950:                        fatal("could not fetch revision");
1.131     niallo   2951:                cvs_buf_putc(bp, '\0');
                   2952:                len = cvs_buf_len(bp);
                   2953:                tbuf = cvs_buf_release(bp);
                   2954:                expanded = rcs_expand_keywords(rf->rf_path, rdp,
                   2955:                    tbuf, len, expmode);
                   2956:                bp = cvs_buf_alloc(len, BUF_AUTOEXT);
1.138     deraadt  2957:                cvs_buf_set(bp, expanded, strlen(expanded), 0);
1.131     niallo   2958:                xfree(expanded);
                   2959:        }
                   2960:        return (bp);
1.176     joris    2961: }
                   2962:
                   2963: RCSNUM *
                   2964: rcs_translate_tag(const char *revstr, RCSFILE *rfp)
                   2965: {
1.178     joris    2966:        size_t i;
1.183     joris    2967:        char *sdate;
1.178     joris    2968:        RCSNUM *rev, *brev;
                   2969:        struct rcs_branch *brp;
1.183     joris    2970:        struct rcs_delta *rdp;
                   2971:        time_t givendate, rcsdate;
                   2972:
                   2973:        rdp = NULL;
1.176     joris    2974:
                   2975:        rev = rcs_sym_getrev(rfp, revstr);
                   2976:        if (rev == NULL) {
1.183     joris    2977:                if ((rev = rcsnum_parse(revstr)) == NULL) {
                   2978:                        if ((givendate = cvs_date_parse(revstr)) == -1)
                   2979:                                fatal("tag %s does not exist (0)", revstr);
                   2980:
                   2981:                        rcs_parse_deltas(rfp, NULL);
                   2982:
                   2983:                        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2984:                                sdate = asctime(&rdp->rd_date);
                   2985:                                if (sdate == NULL)
                   2986:                                        fatal("failed to parse rcs date");
                   2987:                                rcsdate = cvs_date_parse(sdate);
                   2988:                                if (rcsdate == -1)
                   2989:                                        fatal("failed to parse %s", sdate);
                   2990:                                if (givendate <= rcsdate)
                   2991:                                        continue;
                   2992:                                break;
                   2993:                        }
                   2994:
                   2995:                        if (rdp == NULL)
                   2996:                                fatal("no revision that matches date %s",
                   2997:                                    revstr);
                   2998:
                   2999:                        rev = rdp->rd_num;
                   3000:                }
1.178     joris    3001:        }
                   3002:
                   3003:        if (RCSNUM_ISBRANCH(rev)) {
                   3004:                brev = rcsnum_alloc();
1.180     joris    3005:                rcsnum_cpy(rev, brev, rev->rn_len - 1);
                   3006:        } else {
                   3007:                brev = rev;
                   3008:        }
1.178     joris    3009:
1.180     joris    3010:        if ((rdp = rcs_findrev(rfp, brev)) == NULL)
                   3011:                fatal("tag %s does not exist (1)", revstr);
1.178     joris    3012:
1.180     joris    3013:        if (RCSNUM_ISBRANCH(rev)) {
1.178     joris    3014:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                   3015:                        for (i = 0; i < rev->rn_len; i++) {
1.180     joris    3016:                                if (brp->rb_num->rn_id[i] != rev->rn_id[i])
                   3017:                                        break;
1.178     joris    3018:                        }
                   3019:
1.180     joris    3020:                        if (i != rev->rn_len)
                   3021:                                continue;
                   3022:
1.178     joris    3023:                        break;
                   3024:                }
                   3025:
1.180     joris    3026:                if (brp == NULL)
                   3027:                        return (NULL);
1.178     joris    3028:
1.180     joris    3029:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
                   3030:                        fatal("tag %s does not exist (3)", revstr);
1.178     joris    3031:
1.180     joris    3032:                while (rdp->rd_next->rn_len != 0) {
                   3033:                        if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
                   3034:                                fatal("tag %s does not exist (4)", revstr);
1.178     joris    3035:                }
                   3036:
1.180     joris    3037:                rcsnum_cpy(rdp->rd_num, rev, 0);
1.176     joris    3038:        }
                   3039:
                   3040:        return (rev);
1.131     niallo   3041: }