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

1.180   ! joris       1: /*     $OpenBSD: rcs.c,v 1.179 2006/05/31 22:25:59 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.172     joris     376:                return;
1.137     joris     377:
1.117     niallo    378:        /* Write operations need the whole file parsed */
                    379:        rcs_parse_deltatexts(rfp, NULL);
1.1       jfb       380:
1.172     joris     381:        (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", cvs_tmpdir);
                    382:
1.113     xsa       383:        if ((fd = mkstemp(fn)) == -1)
1.172     joris     384:                fatal("%s", fn);
1.113     xsa       385:
                    386:        if ((fp = fdopen(fd, "w+")) == NULL) {
1.172     joris     387:                int saved_errno;
                    388:
                    389:                saved_errno = errno;
                    390:                (void)unlink(fn);
                    391:                errno = saved_errno;
                    392:                fatal("%s", fn);
1.1       jfb       393:        }
                    394:
1.28      jfb       395:        if (rfp->rf_head != NULL)
                    396:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    397:        else
                    398:                numbuf[0] = '\0';
                    399:
1.1       jfb       400:        fprintf(fp, "head\t%s;\n", numbuf);
1.35      jfb       401:
                    402:        if (rfp->rf_branch != NULL) {
                    403:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    404:                fprintf(fp, "branch\t%s;\n", numbuf);
                    405:        }
                    406:
1.29      jfb       407:        fputs("access", fp);
                    408:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    409:                fprintf(fp, "\n\t%s", ap->ra_name);
                    410:        }
                    411:        fputs(";\n", fp);
1.1       jfb       412:
1.85      joris     413:        fprintf(fp, "symbols");
1.1       jfb       414:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    415:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.172     joris     416:                if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
                    417:                    strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
                    418:                    strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
                    419:                        fatal("rcs_write: string overflow");
1.85      joris     420:                fprintf(fp, "\n\t%s", buf);
1.1       jfb       421:        }
                    422:        fprintf(fp, ";\n");
                    423:
1.75      joris     424:        fprintf(fp, "locks");
                    425:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    426:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    427:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    428:        }
                    429:
                    430:        fprintf(fp, ";");
1.1       jfb       431:
1.26      jfb       432:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       433:                fprintf(fp, " strict;");
                    434:        fputc('\n', fp);
                    435:
1.129     xsa       436:        fputs("comment\t@", fp);
1.42      jfb       437:        if (rfp->rf_comment != NULL) {
1.58      xsa       438:                rcs_strprint((const u_char *)rfp->rf_comment,
                    439:                    strlen(rfp->rf_comment), fp);
1.42      jfb       440:                fputs("@;\n", fp);
1.129     xsa       441:        } else
                    442:                fputs("# @;\n", fp);
1.1       jfb       443:
1.42      jfb       444:        if (rfp->rf_expand != NULL) {
                    445:                fputs("expand @", fp);
1.58      xsa       446:                rcs_strprint((const u_char *)rfp->rf_expand,
                    447:                    strlen(rfp->rf_expand), fp);
1.42      jfb       448:                fputs("@;\n", fp);
                    449:        }
1.1       jfb       450:
1.46      jfb       451:        fputs("\n\n", fp);
1.1       jfb       452:
                    453:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    454:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    455:                    sizeof(numbuf)));
                    456:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44      jfb       457:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1       jfb       458:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    459:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    460:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    461:                    rdp->rd_author, rdp->rd_state);
1.46      jfb       462:                fputs("branches", fp);
                    463:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                    464:                        fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
                    465:                            sizeof(numbuf)));
                    466:                }
                    467:                fputs(";\n", fp);
1.1       jfb       468:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    469:                    numbuf, sizeof(numbuf)));
                    470:        }
                    471:
1.42      jfb       472:        fputs("\ndesc\n@", fp);
1.141     ray       473:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
1.100     xsa       474:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    475:                if (rfp->rf_desc[len-1] != '\n')
                    476:                        fputc('\n', fp);
                    477:        }
                    478:        fputs("@\n", fp);
1.1       jfb       479:
                    480:        /* deltatexts */
                    481:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.100     xsa       482:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
1.1       jfb       483:                    sizeof(numbuf)));
1.42      jfb       484:                fputs("log\n@", fp);
1.100     xsa       485:                if (rdp->rd_log != NULL) {
                    486:                        len = strlen(rdp->rd_log);
                    487:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
                    488:                        if (rdp->rd_log[len-1] != '\n')
                    489:                                fputc('\n', fp);
                    490:                }
1.42      jfb       491:                fputs("@\ntext\n@", fp);
1.100     xsa       492:                if (rdp->rd_text != NULL) {
                    493:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
1.103     joris     494:
                    495:                        if (rdp->rd_tlen != 0) {
                    496:                                if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
                    497:                                        fputc('\n', fp);
                    498:                        }
1.100     xsa       499:                }
                    500:                fputs("@\n", fp);
1.1       jfb       501:        }
1.172     joris     502:        (void)fclose(fp);
1.73      joris     503:
1.172     joris     504:        if (rcs_movefile(fn, rfp->rf_path, rfp->rf_mode, rfp->rf_flags) == -1) {
                    505:                (void)unlink(fn);
                    506:                fatal("rcs_movefile failed");
                    507:        }
1.73      joris     508:
1.172     joris     509:        rfp->rf_flags |= RCS_SYNCED;
1.73      joris     510:
1.172     joris     511:        if (fn != NULL)
                    512:                xfree(fn);
                    513: }
1.73      joris     514:
1.172     joris     515: /*
                    516:  * rcs_movefile()
                    517:  *
                    518:  * Move a file using rename(2) if possible and copying if not.
                    519:  * Returns 0 on success, -1 on failure.
                    520:  */
                    521: static int
                    522: rcs_movefile(char *from, char *to, mode_t perm, u_int to_flags)
                    523: {
                    524:        FILE *src, *dst;
                    525:        size_t nread, nwritten;
                    526:        char *buf;
                    527:        int ret;
1.73      joris     528:
1.172     joris     529:        ret = -1;
1.73      joris     530:
1.172     joris     531:        if (rename(from, to) == 0) {
                    532:                if (chmod(to, perm) == -1) {
                    533:                        cvs_log(LP_ERRNO, "%s", to);
1.72      niallo    534:                        return (-1);
                    535:                }
1.172     joris     536:                return (0);
                    537:        } else if (errno != EXDEV) {
                    538:                cvs_log(LP_NOTICE, "failed to access temp RCS output file");
                    539:                return (-1);
                    540:        }
                    541:
                    542:        if ((chmod(to, S_IWUSR) == -1) && !(to_flags & RCS_CREATE)) {
                    543:                cvs_log(LP_ERR, "chmod(%s, 0%o) failed", to, S_IWUSR);
                    544:                return (-1);
1.72      niallo    545:        }
1.73      joris     546:
1.172     joris     547:        /* different filesystem, have to copy the file */
                    548:        if ((src = fopen(from, "r")) == NULL) {
                    549:                cvs_log(LP_ERRNO, "%s", from);
                    550:                return (-1);
                    551:        }
                    552:        if ((dst = fopen(to, "w")) == NULL) {
                    553:                cvs_log(LP_ERRNO, "%s", to);
                    554:                return (-1);
                    555:        }
                    556:        if (fchmod(fileno(dst), perm)) {
                    557:                cvs_log(LP_ERR, "%s", to);
                    558:                (void)unlink(to);
1.72      niallo    559:                return (-1);
                    560:        }
1.73      joris     561:
1.172     joris     562:        buf = xmalloc(MAXBSIZE);
                    563:        while ((nread = fread(buf, sizeof(char), MAXBSIZE, src)) != 0) {
                    564:                if (ferror(src)) {
                    565:                        cvs_log(LP_ERRNO, "failed to read `%s'", from);
                    566:                        (void)unlink(to);
                    567:                        goto out;
                    568:                }
                    569:                nwritten = fwrite(buf, sizeof(char), nread, dst);
                    570:                if (nwritten != nread) {
                    571:                        cvs_log(LP_ERRNO, "failed to write `%s'", to);
                    572:                        (void)unlink(to);
                    573:                        goto out;
                    574:                }
                    575:        }
                    576:
                    577:        ret = 0;
                    578:
                    579:        (void)fclose(src);
                    580:        (void)fclose(dst);
                    581:        (void)unlink(from);
                    582:
                    583: out:
                    584:        xfree(buf);
1.1       jfb       585:
1.172     joris     586:        return (ret);
1.1       jfb       587: }
                    588:
                    589: /*
1.43      jfb       590:  * rcs_head_get()
                    591:  *
                    592:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    593:  */
1.180   ! joris     594: RCSNUM *
1.43      jfb       595: rcs_head_get(RCSFILE *file)
                    596: {
1.180   ! joris     597:        char br[16];
        !           598:
        !           599:        if (file->rf_branch != NULL) {
        !           600:                rcsnum_tostr(file->rf_branch, br, sizeof(br));
        !           601:                return (rcs_translate_tag(br, file));
        !           602:        }
        !           603:
1.43      jfb       604:        return (file->rf_head);
                    605: }
                    606:
                    607: /*
                    608:  * rcs_head_set()
                    609:  *
                    610:  * Set the revision number of the head revision for the RCS file <file> to
                    611:  * <rev>, which must reference a valid revision within the file.
                    612:  */
                    613: int
1.117     niallo    614: rcs_head_set(RCSFILE *file, RCSNUM *rev)
1.43      jfb       615: {
1.167     ray       616:        if (rcs_findrev(file, rev) == NULL)
1.43      jfb       617:                return (-1);
                    618:
1.111     joris     619:        if (file->rf_head == NULL)
                    620:                file->rf_head = rcsnum_alloc();
1.43      jfb       621:
1.111     joris     622:        rcsnum_cpy(rev, file->rf_head, 0);
1.70      moritz    623:        file->rf_flags &= ~RCS_SYNCED;
1.43      jfb       624:        return (0);
                    625: }
                    626:
                    627:
                    628: /*
1.35      jfb       629:  * rcs_branch_get()
                    630:  *
                    631:  * Retrieve the default branch number for the RCS file <file>.
                    632:  * Returns the number on success.  If NULL is returned, then there is no
                    633:  * default branch for this file.
                    634:  */
1.60      xsa       635: const RCSNUM *
1.35      jfb       636: rcs_branch_get(RCSFILE *file)
                    637: {
                    638:        return (file->rf_branch);
                    639: }
                    640:
                    641: /*
                    642:  * rcs_branch_set()
                    643:  *
                    644:  * Set the default branch for the RCS file <file> to <bnum>.
                    645:  * Returns 0 on success, -1 on failure.
                    646:  */
                    647: int
                    648: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
                    649: {
1.111     joris     650:        if (file->rf_branch == NULL)
                    651:                file->rf_branch = rcsnum_alloc();
1.35      jfb       652:
1.111     joris     653:        rcsnum_cpy(bnum, file->rf_branch, 0);
1.70      moritz    654:        file->rf_flags &= ~RCS_SYNCED;
1.35      jfb       655:        return (0);
                    656: }
                    657:
                    658: /*
1.29      jfb       659:  * rcs_access_add()
                    660:  *
                    661:  * Add the login name <login> to the access list for the RCS file <file>.
                    662:  * Returns 0 on success, or -1 on failure.
                    663:  */
                    664: int
                    665: rcs_access_add(RCSFILE *file, const char *login)
                    666: {
                    667:        struct rcs_access *ap;
                    668:
                    669:        /* first look for duplication */
                    670:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
                    671:                if (strcmp(ap->ra_name, login) == 0) {
1.32      jfb       672:                        rcs_errno = RCS_ERR_DUPENT;
1.29      jfb       673:                        return (-1);
                    674:                }
                    675:        }
                    676:
1.161     ray       677:        ap = xmalloc(sizeof(*ap));
1.110     joris     678:        ap->ra_name = xstrdup(login);
1.29      jfb       679:        TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
                    680:
                    681:        /* not synced anymore */
                    682:        file->rf_flags &= ~RCS_SYNCED;
                    683:        return (0);
                    684: }
                    685:
                    686: /*
                    687:  * rcs_access_remove()
                    688:  *
                    689:  * Remove an entry with login name <login> from the access list of the RCS
                    690:  * file <file>.
                    691:  * Returns 0 on success, or -1 on failure.
                    692:  */
                    693: int
                    694: rcs_access_remove(RCSFILE *file, const char *login)
                    695: {
                    696:        struct rcs_access *ap;
                    697:
                    698:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
                    699:                if (strcmp(ap->ra_name, login) == 0)
                    700:                        break;
                    701:
                    702:        if (ap == NULL) {
1.32      jfb       703:                rcs_errno = RCS_ERR_NOENT;
1.29      jfb       704:                return (-1);
                    705:        }
                    706:
                    707:        TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.110     joris     708:        xfree(ap->ra_name);
                    709:        xfree(ap);
1.29      jfb       710:
                    711:        /* not synced anymore */
                    712:        file->rf_flags &= ~RCS_SYNCED;
                    713:        return (0);
                    714: }
                    715:
                    716: /*
1.26      jfb       717:  * rcs_sym_add()
1.1       jfb       718:  *
                    719:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    720:  * is named <sym> and is bound to the RCS revision <snum>.
                    721:  * Returns 0 on success, or -1 on failure.
                    722:  */
                    723: int
1.26      jfb       724: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1       jfb       725: {
                    726:        struct rcs_sym *symp;
                    727:
1.47      jfb       728:        if (!rcs_sym_check(sym)) {
                    729:                rcs_errno = RCS_ERR_BADSYM;
1.69      moritz    730:                return (-1);
1.47      jfb       731:        }
                    732:
1.1       jfb       733:        /* first look for duplication */
                    734:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    735:                if (strcmp(symp->rs_name, sym) == 0) {
1.32      jfb       736:                        rcs_errno = RCS_ERR_DUPENT;
1.1       jfb       737:                        return (-1);
                    738:                }
                    739:        }
                    740:
1.161     ray       741:        symp = xmalloc(sizeof(*symp));
1.110     joris     742:        symp->rs_name = xstrdup(sym);
1.111     joris     743:        symp->rs_num = rcsnum_alloc();
1.1       jfb       744:        rcsnum_cpy(snum, symp->rs_num, 0);
                    745:
                    746:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    747:
                    748:        /* not synced anymore */
1.26      jfb       749:        rfp->rf_flags &= ~RCS_SYNCED;
1.1       jfb       750:        return (0);
                    751: }
                    752:
                    753: /*
1.27      jfb       754:  * rcs_sym_remove()
                    755:  *
                    756:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    757:  * <file>.  If no such symbol is found, the call fails and returns with an
                    758:  * error.
                    759:  * Returns 0 on success, or -1 on failure.
                    760:  */
                    761: int
                    762: rcs_sym_remove(RCSFILE *file, const char *sym)
                    763: {
                    764:        struct rcs_sym *symp;
                    765:
1.47      jfb       766:        if (!rcs_sym_check(sym)) {
                    767:                rcs_errno = RCS_ERR_BADSYM;
1.69      moritz    768:                return (-1);
1.47      jfb       769:        }
                    770:
1.27      jfb       771:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    772:                if (strcmp(symp->rs_name, sym) == 0)
                    773:                        break;
                    774:
                    775:        if (symp == NULL) {
1.32      jfb       776:                rcs_errno = RCS_ERR_NOENT;
1.27      jfb       777:                return (-1);
                    778:        }
                    779:
                    780:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.110     joris     781:        xfree(symp->rs_name);
1.27      jfb       782:        rcsnum_free(symp->rs_num);
1.110     joris     783:        xfree(symp);
1.27      jfb       784:
                    785:        /* not synced anymore */
                    786:        file->rf_flags &= ~RCS_SYNCED;
                    787:        return (0);
                    788: }
                    789:
                    790: /*
                    791:  * rcs_sym_getrev()
                    792:  *
                    793:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    794:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    795:  * should be freed by the caller once they are done with it.
                    796:  * Returns the RCSNUM on success, or NULL on failure.
                    797:  */
1.60      xsa       798: RCSNUM *
1.27      jfb       799: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    800: {
                    801:        RCSNUM *num;
                    802:        struct rcs_sym *symp;
                    803:
1.47      jfb       804:        if (!rcs_sym_check(sym)) {
                    805:                rcs_errno = RCS_ERR_BADSYM;
                    806:                return (NULL);
                    807:        }
                    808:
1.176     joris     809:        if (!strcmp(sym, RCS_HEAD_BRANCH)) {
                    810:                num = rcsnum_alloc();
                    811:                rcsnum_cpy(file->rf_head, num, 0);
                    812:                return (num);
                    813:        }
                    814:
1.27      jfb       815:        num = NULL;
                    816:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    817:                if (strcmp(symp->rs_name, sym) == 0)
                    818:                        break;
                    819:
1.111     joris     820:        if (symp == NULL) {
1.36      jfb       821:                rcs_errno = RCS_ERR_NOENT;
1.111     joris     822:        } else {
                    823:                num = rcsnum_alloc();
                    824:                rcsnum_cpy(symp->rs_num, num, 0);
1.27      jfb       825:        }
                    826:
                    827:        return (num);
1.47      jfb       828: }
                    829:
                    830: /*
                    831:  * rcs_sym_check()
                    832:  *
                    833:  * Check the RCS symbol name <sym> for any unsupported characters.
                    834:  * Returns 1 if the tag is correct, 0 if it isn't valid.
                    835:  */
                    836: int
                    837: rcs_sym_check(const char *sym)
                    838: {
                    839:        int ret;
                    840:        const char *cp;
                    841:
                    842:        ret = 1;
                    843:        cp = sym;
                    844:        if (!isalpha(*cp++))
                    845:                return (0);
                    846:
                    847:        for (; *cp != '\0'; cp++)
                    848:                if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
                    849:                        ret = 0;
                    850:                        break;
                    851:                }
                    852:
                    853:        return (ret);
1.30      jfb       854: }
                    855:
                    856: /*
                    857:  * rcs_lock_getmode()
                    858:  *
                    859:  * Retrieve the locking mode of the RCS file <file>.
                    860:  */
                    861: int
                    862: rcs_lock_getmode(RCSFILE *file)
                    863: {
                    864:        return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
                    865: }
                    866:
                    867: /*
                    868:  * rcs_lock_setmode()
                    869:  *
                    870:  * Set the locking mode of the RCS file <file> to <mode>, which must either
                    871:  * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
                    872:  * Returns the previous mode on success, or -1 on failure.
                    873:  */
                    874: int
                    875: rcs_lock_setmode(RCSFILE *file, int mode)
                    876: {
                    877:        int pmode;
                    878:        pmode = rcs_lock_getmode(file);
                    879:
                    880:        if (mode == RCS_LOCK_STRICT)
                    881:                file->rf_flags |= RCS_SLOCK;
                    882:        else if (mode == RCS_LOCK_LOOSE)
                    883:                file->rf_flags &= ~RCS_SLOCK;
1.145     xsa       884:        else
                    885:                fatal("rcs_lock_setmode: invalid mode `%d'", mode);
1.30      jfb       886:
1.70      moritz    887:        file->rf_flags &= ~RCS_SYNCED;
1.30      jfb       888:        return (pmode);
1.27      jfb       889: }
                    890:
                    891: /*
1.40      jfb       892:  * rcs_lock_add()
                    893:  *
                    894:  * Add an RCS lock for the user <user> on revision <rev>.
                    895:  * Returns 0 on success, or -1 on failure.
                    896:  */
                    897: int
                    898: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
                    899: {
                    900:        struct rcs_lock *lkp;
                    901:
                    902:        /* first look for duplication */
                    903:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168     deraadt   904:                if (strcmp(lkp->rl_name, user) == 0 &&
                    905:                    rcsnum_cmp(rev, lkp->rl_num, 0) == 0) {
1.40      jfb       906:                        rcs_errno = RCS_ERR_DUPENT;
                    907:                        return (-1);
                    908:                }
                    909:        }
                    910:
1.161     ray       911:        lkp = xmalloc(sizeof(*lkp));
1.110     joris     912:        lkp->rl_name = xstrdup(user);
1.111     joris     913:        lkp->rl_num = rcsnum_alloc();
1.68      moritz    914:        rcsnum_cpy(rev, lkp->rl_num, 0);
1.40      jfb       915:
                    916:        TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
                    917:
                    918:        /* not synced anymore */
                    919:        file->rf_flags &= ~RCS_SYNCED;
                    920:        return (0);
                    921: }
                    922:
                    923:
                    924: /*
                    925:  * rcs_lock_remove()
                    926:  *
                    927:  * Remove the RCS lock on revision <rev>.
                    928:  * Returns 0 on success, or -1 on failure.
                    929:  */
                    930: int
1.109     joris     931: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
1.40      jfb       932: {
                    933:        struct rcs_lock *lkp;
                    934:
1.109     joris     935:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168     deraadt   936:                if (strcmp(lkp->rl_name, user) == 0 &&
                    937:                    rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
1.40      jfb       938:                        break;
1.109     joris     939:        }
1.40      jfb       940:
                    941:        if (lkp == NULL) {
                    942:                rcs_errno = RCS_ERR_NOENT;
                    943:                return (-1);
                    944:        }
                    945:
                    946:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
                    947:        rcsnum_free(lkp->rl_num);
1.110     joris     948:        xfree(lkp->rl_name);
                    949:        xfree(lkp);
1.40      jfb       950:
                    951:        /* not synced anymore */
                    952:        file->rf_flags &= ~RCS_SYNCED;
                    953:        return (0);
                    954: }
                    955:
                    956: /*
1.27      jfb       957:  * rcs_desc_get()
                    958:  *
                    959:  * Retrieve the description for the RCS file <file>.
                    960:  */
1.57      xsa       961: const char *
1.27      jfb       962: rcs_desc_get(RCSFILE *file)
                    963: {
                    964:        return (file->rf_desc);
                    965: }
                    966:
                    967: /*
                    968:  * rcs_desc_set()
                    969:  *
                    970:  * Set the description for the RCS file <file>.
                    971:  */
1.149     xsa       972: void
1.27      jfb       973: rcs_desc_set(RCSFILE *file, const char *desc)
                    974: {
                    975:        char *tmp;
                    976:
1.110     joris     977:        tmp = xstrdup(desc);
1.27      jfb       978:        if (file->rf_desc != NULL)
1.110     joris     979:                xfree(file->rf_desc);
1.27      jfb       980:        file->rf_desc = tmp;
                    981:        file->rf_flags &= ~RCS_SYNCED;
1.51      jfb       982: }
                    983:
                    984: /*
                    985:  * rcs_comment_lookup()
                    986:  *
                    987:  * Lookup the assumed comment leader based on a file's suffix.
                    988:  * Returns a pointer to the string on success, or NULL on failure.
                    989:  */
1.57      xsa       990: const char *
1.51      jfb       991: rcs_comment_lookup(const char *filename)
                    992: {
                    993:        int i;
                    994:        const char *sp;
                    995:
                    996:        if ((sp = strrchr(filename, '.')) == NULL) {
                    997:                rcs_errno = RCS_ERR_NOENT;
                    998:                return (NULL);
                    999:        }
                   1000:        sp++;
                   1001:
                   1002:        for (i = 0; i < (int)NB_COMTYPES; i++)
                   1003:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                   1004:                        return (rcs_comments[i].rc_cstr);
                   1005:        return (NULL);
1.27      jfb      1006: }
                   1007:
1.33      jfb      1008: /*
                   1009:  * rcs_comment_get()
                   1010:  *
                   1011:  * Retrieve the comment leader for the RCS file <file>.
                   1012:  */
1.57      xsa      1013: const char *
1.33      jfb      1014: rcs_comment_get(RCSFILE *file)
                   1015: {
                   1016:        return (file->rf_comment);
                   1017: }
                   1018:
                   1019: /*
                   1020:  * rcs_comment_set()
                   1021:  *
                   1022:  * Set the comment leader for the RCS file <file>.
                   1023:  */
1.150     xsa      1024: void
1.33      jfb      1025: rcs_comment_set(RCSFILE *file, const char *comment)
                   1026: {
                   1027:        char *tmp;
                   1028:
1.110     joris    1029:        tmp = xstrdup(comment);
1.33      jfb      1030:        if (file->rf_comment != NULL)
1.110     joris    1031:                xfree(file->rf_comment);
1.33      jfb      1032:        file->rf_comment = tmp;
                   1033:        file->rf_flags &= ~RCS_SYNCED;
                   1034: }
1.40      jfb      1035:
                   1036: /*
                   1037:  * rcs_tag_resolve()
                   1038:  *
                   1039:  * Retrieve the revision number corresponding to the tag <tag> for the RCS
                   1040:  * file <file>.
                   1041:  */
1.60      xsa      1042: RCSNUM *
1.40      jfb      1043: rcs_tag_resolve(RCSFILE *file, const char *tag)
                   1044: {
                   1045:        RCSNUM *num;
                   1046:
                   1047:        if ((num = rcsnum_parse(tag)) == NULL) {
                   1048:                num = rcs_sym_getrev(file, tag);
                   1049:        }
                   1050:
                   1051:        return (num);
                   1052: }
                   1053:
1.94      joris    1054: int
                   1055: rcs_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
1.5       vincent  1056: {
                   1057:        char op, *ep;
1.94      joris    1058:        struct cvs_line *lp, *dlp, *ndlp;
1.5       vincent  1059:        int i, lineno, nbln;
1.1       jfb      1060:
1.94      joris    1061:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1062:        lp = TAILQ_FIRST(&(plines->l_lines));
1.1       jfb      1063:
                   1064:        /* skip first bogus line */
1.94      joris    1065:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1066:            lp = TAILQ_NEXT(lp, l_list)) {
                   1067:                op = *(lp->l_line);
                   1068:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
1.168     deraadt  1069:                if (lineno > dlines->l_nblines || lineno < 0 ||
                   1070:                    *ep != ' ')
1.180   ! joris    1071:                        fatal("invalid line specification in RCS patch: %s",
        !          1072:                            ep);
1.1       jfb      1073:                ep++;
                   1074:                nbln = (int)strtol(ep, &ep, 10);
1.168     deraadt  1075:                if (nbln < 0 || *ep != '\0')
1.132     niallo   1076:                        fatal("invalid line number specification in RCS patch");
1.1       jfb      1077:
                   1078:                /* find the appropriate line */
                   1079:                for (;;) {
                   1080:                        if (dlp == NULL)
                   1081:                                break;
1.94      joris    1082:                        if (dlp->l_lineno == lineno)
1.1       jfb      1083:                                break;
1.94      joris    1084:                        if (dlp->l_lineno > lineno) {
                   1085:                                dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
                   1086:                        } else if (dlp->l_lineno < lineno) {
1.133     niallo   1087:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.168     deraadt  1088:                                    ndlp->l_lineno > lineno)
1.1       jfb      1089:                                        break;
                   1090:                                dlp = ndlp;
                   1091:                        }
                   1092:                }
1.132     niallo   1093:                if (dlp == NULL)
                   1094:                        fatal("can't find referenced line in RCS patch");
1.1       jfb      1095:
                   1096:                if (op == 'd') {
                   1097:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94      joris    1098:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1099:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.1       jfb      1100:                                dlp = ndlp;
1.133     niallo   1101:                                /* last line is gone - reset dlp */
                   1102:                                if (dlp == NULL) {
                   1103:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1104:                                            cvs_tqh);
                   1105:                                        dlp = ndlp;
                   1106:                                }
1.1       jfb      1107:                        }
1.14      deraadt  1108:                } else if (op == 'a') {
1.1       jfb      1109:                        for (i = 0; i < nbln; i++) {
                   1110:                                ndlp = lp;
1.94      joris    1111:                                lp = TAILQ_NEXT(lp, l_list);
1.132     niallo   1112:                                if (lp == NULL)
                   1113:                                        fatal("truncated RCS patch");
1.94      joris    1114:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1115:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1116:                                    lp, l_list);
1.1       jfb      1117:                                dlp = lp;
                   1118:
                   1119:                                /* we don't want lookup to block on those */
1.94      joris    1120:                                lp->l_lineno = lineno;
1.1       jfb      1121:
                   1122:                                lp = ndlp;
                   1123:                        }
1.132     niallo   1124:                } else
                   1125:                        fatal("unknown RCS patch operation `%c'", op);
1.1       jfb      1126:
                   1127:                /* last line of the patch, done */
1.94      joris    1128:                if (lp->l_lineno == plines->l_nblines)
1.1       jfb      1129:                        break;
                   1130:        }
                   1131:
                   1132:        /* once we're done patching, rebuild the line numbers */
1.2       vincent  1133:        lineno = 0;
1.94      joris    1134:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1135:                lp->l_lineno = lineno++;
                   1136:        dlines->l_nblines = lineno - 1;
1.1       jfb      1137:
1.5       vincent  1138:        return (0);
1.1       jfb      1139: }
                   1140:
                   1141: /*
                   1142:  * rcs_getrev()
                   1143:  *
                   1144:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent  1145:  * returned buffer is dynamically allocated and should be released using
                   1146:  * cvs_buf_free() once the caller is done using it.
1.1       jfb      1147:  */
                   1148: BUF*
1.66      joris    1149: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1       jfb      1150: {
1.180   ! joris    1151:        size_t i;
        !          1152:        int done, nextroot, found;
        !          1153:        BUF *rcsbuf;
        !          1154:        RCSNUM *tnum, *bnum;
        !          1155:        struct rcs_branch *brp;
        !          1156:        struct rcs_delta *hrdp, *trdp, *rdp;
        !          1157:        char *data;
1.1       jfb      1158:
1.180   ! joris    1159:        if ((hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
        !          1160:                fatal("rcs_getrev: no HEAD revision");
1.66      joris    1161:
1.180   ! joris    1162:        tnum = frev;
        !          1163:        rcs_parse_deltatexts(rfp, hrdp->rd_num);
1.28      jfb      1164:
1.180   ! joris    1165:        /* revision on branch, get the branch root */
        !          1166:        nextroot = 2;
        !          1167:        if (RCSNUM_ISBRANCHREV(tnum)) {
        !          1168:                bnum = rcsnum_alloc();
        !          1169:                rcsnum_cpy(tnum, bnum, nextroot);
        !          1170:        } else {
        !          1171:                bnum = tnum;
1.26      jfb      1172:        }
                   1173:
1.180   ! joris    1174:        rcsbuf = cvs_buf_alloc(hrdp->rd_tlen, BUF_AUTOEXT);
        !          1175:        cvs_buf_append(rcsbuf, hrdp->rd_text, hrdp->rd_tlen);
        !          1176:
        !          1177:        done = 0;
        !          1178:
        !          1179:        rdp = hrdp;
        !          1180:        if (!rcsnum_differ(rdp->rd_num, bnum))
        !          1181:                goto next;
1.117     niallo   1182:
1.180   ! joris    1183:        if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
        !          1184:                return (rcsbuf);
1.156     joris    1185:
1.180   ! joris    1186: again:
        !          1187:        for (;;) {
        !          1188:                if (rdp->rd_next->rn_len != 0) {
        !          1189:                        trdp = rcs_findrev(rfp, rdp->rd_next);
        !          1190:                        if (trdp == NULL)
        !          1191:                                fatal("failed to grab next revision");
        !          1192:                }
1.156     joris    1193:
1.180   ! joris    1194:                if (rdp->rd_tlen == 0) {
        !          1195:                        rcs_parse_deltatexts(rfp, rdp->rd_num);
        !          1196:                        if (rdp->rd_tlen == 0) {
        !          1197:                                if (!rcsnum_differ(rdp->rd_num, bnum))
        !          1198:                                        break;
        !          1199:                                rdp = trdp;
        !          1200:                                continue;
        !          1201:                        }
        !          1202:                }
1.26      jfb      1203:
1.180   ! joris    1204:                cvs_buf_putc(rcsbuf, '\0');
        !          1205:                data = cvs_buf_release(rcsbuf);
1.26      jfb      1206:
1.180   ! joris    1207:                rcsbuf = cvs_patchfile(data, rdp->rd_text, rcs_patch_lines);
        !          1208:                if (rcsbuf == NULL)
        !          1209:                        fatal("rcs_getrev: failed to apply rcsdiff");
        !          1210:                xfree(data);
1.156     joris    1211:
1.180   ! joris    1212:                if (!rcsnum_differ(rdp->rd_num, bnum))
        !          1213:                        break;
1.156     joris    1214:
1.180   ! joris    1215:                rdp = trdp;
1.156     joris    1216:        }
                   1217:
1.180   ! joris    1218: next:
        !          1219:        if (!rcsnum_differ(rdp->rd_num, frev))
        !          1220:                done = 1;
1.156     joris    1221:
1.180   ! joris    1222:        if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
        !          1223:                nextroot += 2;
        !          1224:                rcsnum_cpy(frev, bnum, nextroot);
1.156     joris    1225:
1.180   ! joris    1226:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
        !          1227:                        found = 1;
        !          1228:                        for (i = 0; i < nextroot - 1; i++) {
        !          1229:                                if (brp->rb_num->rn_id[i] != bnum->rn_id[i]) {
        !          1230:                                        found = 0;
1.156     joris    1231:                                        break;
                   1232:                                }
                   1233:                        }
1.180   ! joris    1234:
        !          1235:                        /* XXX */
1.171     niallo   1236:                        if (found == 0)
1.180   ! joris    1237:                                fatal("no matching branch on list");
        !          1238:                        break;
1.156     joris    1239:                }
                   1240:
1.180   ! joris    1241:                if (brp == NULL)
        !          1242:                        fatal("expected branch not found on branch list");
1.156     joris    1243:
1.180   ! joris    1244:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
        !          1245:                        fatal("rcs_getrev: failed to get delta for target rev");
1.156     joris    1246:
1.180   ! joris    1247:                goto again;
        !          1248:        }
1.156     joris    1249:
1.180   ! joris    1250:        if (bnum != tnum)
        !          1251:                rcsnum_free(bnum);
1.156     joris    1252:
1.180   ! joris    1253:        return (rcsbuf);
1.1       jfb      1254: }
                   1255:
                   1256: /*
1.52      jfb      1257:  * rcs_rev_add()
                   1258:  *
1.53      jfb      1259:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1260:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1261:  * new revision will have a number equal to the previous head revision plus
                   1262:  * one).  The <msg> argument specifies the log message for that revision, and
                   1263:  * <date> specifies the revision's date (a value of -1 is
                   1264:  * equivalent to using the current time).
1.96      xsa      1265:  * If <username> is NULL, set the author for this revision to the current user.
1.90      niallo   1266:  * Otherwise, set it to <username>.
1.52      jfb      1267:  * Returns 0 on success, or -1 on failure.
                   1268:  */
                   1269: int
1.90      niallo   1270: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1271:     const char *username)
1.52      jfb      1272: {
                   1273:        time_t now;
                   1274:        struct passwd *pw;
1.180   ! joris    1275:        struct rcs_branch *brp;
1.83      joris    1276:        struct rcs_delta *ordp, *rdp;
                   1277:
1.52      jfb      1278:        if (rev == RCS_HEAD_REV) {
1.101     niallo   1279:                if (rf->rf_flags & RCS_CREATE) {
                   1280:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1281:                                return (-1);
1.111     joris    1282:                        rf->rf_head = rcsnum_alloc();
1.101     niallo   1283:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1284:                } else {
                   1285:                        rev = rcsnum_inc(rf->rf_head);
                   1286:                }
1.83      joris    1287:        } else {
                   1288:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1289:                        rcs_errno = RCS_ERR_DUPENT;
                   1290:                        return (-1);
                   1291:                }
1.52      jfb      1292:        }
                   1293:
1.112     xsa      1294:        if ((pw = getpwuid(getuid())) == NULL)
                   1295:                fatal("getpwuid failed");
1.52      jfb      1296:
1.161     ray      1297:        rdp = xcalloc(1, sizeof(*rdp));
1.52      jfb      1298:
                   1299:        TAILQ_INIT(&(rdp->rd_branches));
                   1300:
1.111     joris    1301:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1302:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1303:
1.111     joris    1304:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1305:
1.90      niallo   1306:        if (username == NULL)
                   1307:                username = pw->pw_name;
                   1308:
1.110     joris    1309:        rdp->rd_author = xstrdup(username);
                   1310:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1311:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1312:
1.53      jfb      1313:        if (date != (time_t)(-1))
                   1314:                now = date;
                   1315:        else
                   1316:                time(&now);
1.52      jfb      1317:        gmtime_r(&now, &(rdp->rd_date));
                   1318:
1.180   ! joris    1319:        if (RCSNUM_ISBRANCHREV(rev))
        !          1320:                TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
        !          1321:        else
        !          1322:                TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1.52      jfb      1323:        rf->rf_ndelta++;
1.81      niallo   1324:
1.180   ! joris    1325:        if (!(rf->rf_flags & RCS_CREATE)) {
        !          1326:                if (RCSNUM_ISBRANCHREV(rev)) {
        !          1327:                        brp = xmalloc(sizeof(*brp));
        !          1328:                        brp->rb_num = rcsnum_alloc();
        !          1329:                        rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
        !          1330:                        TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
        !          1331:
        !          1332:                        ordp = TAILQ_PREV(rdp, cvs_tqh, rd_list);
        !          1333:                        rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
        !          1334:                } else {
        !          1335:                        ordp = TAILQ_NEXT(rdp, rd_list);
        !          1336:                        rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
        !          1337:                }
        !          1338:        }
        !          1339:
1.64      niallo   1340:        /* not synced anymore */
                   1341:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1342:
                   1343:        return (0);
                   1344: }
                   1345:
                   1346: /*
                   1347:  * rcs_rev_remove()
                   1348:  *
                   1349:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1350:  */
                   1351: int
                   1352: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1353: {
1.172     joris    1354:        char *newdeltatext, *path_tmp1, *path_tmp2;
1.166     joris    1355:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
                   1356:        BUF *nextbuf, *prevbuf, *newdiff;
                   1357:
1.52      jfb      1358:        if (rev == RCS_HEAD_REV)
                   1359:                rev = rf->rf_head;
                   1360:
                   1361:        /* do we actually have that revision? */
                   1362:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1363:                rcs_errno = RCS_ERR_NOENT;
1.166     joris    1364:                return (-1);
                   1365:        }
                   1366:
                   1367:        /*
                   1368:         * This is confusing, the previous delta is next in the TAILQ list.
                   1369:         * the next delta is the previous one in the TAILQ list.
                   1370:         *
                   1371:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1372:         * When the first revision got specified, prevrdp will be NULL.
                   1373:         */
                   1374:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
                   1375:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
                   1376:
                   1377:        newdeltatext = NULL;
                   1378:        prevbuf = nextbuf = NULL;
                   1379:
                   1380:        if (prevrdp != NULL) {
                   1381:                if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
                   1382:                        fatal("error getting revision");
                   1383:        }
                   1384:
1.168     deraadt  1385:        if (prevrdp != NULL && nextrdp != NULL) {
1.166     joris    1386:                if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
                   1387:                        fatal("error getting revision");
                   1388:
                   1389:                newdiff = cvs_buf_alloc(64, BUF_AUTOEXT);
                   1390:
                   1391:                /* calculate new diff */
1.172     joris    1392:                (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
                   1393:                cvs_buf_write_stmp(nextbuf, path_tmp1, 0600, NULL);
1.166     joris    1394:                cvs_buf_free(nextbuf);
                   1395:
1.172     joris    1396:                (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                   1397:                cvs_buf_write_stmp(prevbuf, path_tmp2, 0600, NULL);
1.166     joris    1398:                cvs_buf_free(prevbuf);
                   1399:
                   1400:                diff_format = D_RCSDIFF;
1.172     joris    1401:                if (cvs_diffreg(path_tmp1, path_tmp2, newdiff) == D_ERROR)
                   1402:                        fatal("rcs_diffreg failed");
1.166     joris    1403:
                   1404:                newdeltatext = cvs_buf_release(newdiff);
1.168     deraadt  1405:        } else if (nextrdp == NULL && prevrdp != NULL) {
1.166     joris    1406:                newdeltatext = cvs_buf_release(prevbuf);
                   1407:        }
                   1408:
                   1409:        if (newdeltatext != NULL) {
                   1410:                if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
                   1411:                        fatal("error setting new deltatext");
                   1412:        }
                   1413:
                   1414:        TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1415:
                   1416:        /* update pointers */
1.168     deraadt  1417:        if (prevrdp != NULL && nextrdp != NULL) {
1.166     joris    1418:                rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
                   1419:        } else if (prevrdp != NULL) {
1.170     xsa      1420:                if (rcs_head_set(rf, prevrdp->rd_num) < 0)
                   1421:                        fatal("rcs_head_set failed");
1.166     joris    1422:        } else if (nextrdp != NULL) {
                   1423:                rcsnum_free(nextrdp->rd_next);
                   1424:                nextrdp->rd_next = rcsnum_alloc();
1.52      jfb      1425:        } else {
1.166     joris    1426:                rcsnum_free(rf->rf_head);
                   1427:                rf->rf_head = NULL;
1.52      jfb      1428:        }
                   1429:
1.166     joris    1430:        rf->rf_ndelta--;
                   1431:        rf->rf_flags &= ~RCS_SYNCED;
                   1432:
                   1433:        rcs_freedelta(rdp);
                   1434:
                   1435:        if (newdeltatext != NULL)
                   1436:                xfree(newdeltatext);
1.52      jfb      1437:
1.172     joris    1438:        if (path_tmp1 != NULL)
                   1439:                xfree(path_tmp1);
                   1440:        if (path_tmp2 != NULL)
                   1441:                xfree(path_tmp2);
                   1442:
1.166     joris    1443:        return (0);
1.52      jfb      1444: }
                   1445:
                   1446: /*
1.1       jfb      1447:  * rcs_findrev()
                   1448:  *
                   1449:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1450:  * The revision number is given in <rev>.
1.156     joris    1451:  *
1.1       jfb      1452:  * Returns a pointer to the delta on success, or NULL on failure.
                   1453:  */
1.102     xsa      1454: struct rcs_delta *
1.117     niallo   1455: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1       jfb      1456: {
1.178     joris    1457:        int isbrev;
1.1       jfb      1458:        u_int cmplen;
1.156     joris    1459:        struct rcs_delta *rdp;
1.178     joris    1460:
                   1461:        isbrev = RCSNUM_ISBRANCHREV(rev);
1.26      jfb      1462:
1.117     niallo   1463:        /*
                   1464:         * We need to do more parsing if the last revision in the linked list
                   1465:         * is greater than the requested revision.
                   1466:         */
1.156     joris    1467:        rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.168     deraadt  1468:        if (rdp == NULL ||
1.178     joris    1469:            (!isbrev && rcsnum_cmp(rdp->rd_num, rev, 0) == -1) ||
                   1470:            ((isbrev && rdp->rd_num->rn_len < 4) ||
                   1471:            (isbrev && rcsnum_differ(rev, rdp->rd_num)))) {
1.117     niallo   1472:                rcs_parse_deltas(rfp, rev);
                   1473:        }
                   1474:
1.156     joris    1475:        cmplen = rev->rn_len;
                   1476:
                   1477:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.178     joris    1478:                if (rcsnum_differ(rdp->rd_num, rev))
                   1479:                        continue;
                   1480:                else
1.156     joris    1481:                        return (rdp);
                   1482:        }
1.1       jfb      1483:
                   1484:        return (NULL);
1.20      jfb      1485: }
                   1486:
                   1487: /*
1.26      jfb      1488:  * rcs_kwexp_set()
                   1489:  *
                   1490:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1491:  */
1.162     xsa      1492: void
1.26      jfb      1493: rcs_kwexp_set(RCSFILE *file, int mode)
                   1494: {
                   1495:        int i;
                   1496:        char *tmp, buf[8] = "";
                   1497:
                   1498:        if (RCS_KWEXP_INVAL(mode))
1.162     xsa      1499:                return;
1.26      jfb      1500:
                   1501:        i = 0;
                   1502:        if (mode == RCS_KWEXP_NONE)
                   1503:                buf[0] = 'b';
                   1504:        else if (mode == RCS_KWEXP_OLD)
                   1505:                buf[0] = 'o';
                   1506:        else {
                   1507:                if (mode & RCS_KWEXP_NAME)
                   1508:                        buf[i++] = 'k';
                   1509:                if (mode & RCS_KWEXP_VAL)
                   1510:                        buf[i++] = 'v';
                   1511:                if (mode & RCS_KWEXP_LKR)
                   1512:                        buf[i++] = 'l';
                   1513:        }
                   1514:
1.110     joris    1515:        tmp = xstrdup(buf);
1.27      jfb      1516:        if (file->rf_expand != NULL)
1.147     ray      1517:                xfree(file->rf_expand);
1.26      jfb      1518:        file->rf_expand = tmp;
1.64      niallo   1519:        /* not synced anymore */
                   1520:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1521: }
                   1522:
                   1523: /*
                   1524:  * rcs_kwexp_get()
                   1525:  *
                   1526:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1527:  */
                   1528: int
                   1529: rcs_kwexp_get(RCSFILE *file)
                   1530: {
                   1531:        return rcs_kflag_get(file->rf_expand);
                   1532: }
                   1533:
                   1534: /*
1.20      jfb      1535:  * rcs_kflag_get()
                   1536:  *
                   1537:  * Get the keyword expansion mode from a set of character flags given in
                   1538:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1539:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1540:  */
                   1541: int
                   1542: rcs_kflag_get(const char *flags)
                   1543: {
                   1544:        int fl;
                   1545:        size_t len;
                   1546:        const char *fp;
                   1547:
                   1548:        fl = 0;
                   1549:        len = strlen(flags);
                   1550:
                   1551:        for (fp = flags; *fp != '\0'; fp++) {
                   1552:                if (*fp == 'k')
                   1553:                        fl |= RCS_KWEXP_NAME;
                   1554:                else if (*fp == 'v')
                   1555:                        fl |= RCS_KWEXP_VAL;
                   1556:                else if (*fp == 'l')
                   1557:                        fl |= RCS_KWEXP_LKR;
                   1558:                else if (*fp == 'o') {
                   1559:                        if (len != 1)
                   1560:                                fl |= RCS_KWEXP_ERR;
                   1561:                        fl |= RCS_KWEXP_OLD;
                   1562:                } else if (*fp == 'b') {
                   1563:                        if (len != 1)
                   1564:                                fl |= RCS_KWEXP_ERR;
                   1565:                } else  /* unknown letter */
                   1566:                        fl |= RCS_KWEXP_ERR;
                   1567:        }
                   1568:
                   1569:        return (fl);
1.32      jfb      1570: }
                   1571:
                   1572: /*
                   1573:  * rcs_errstr()
                   1574:  *
                   1575:  * Get the error string matching the RCS error code <code>.
                   1576:  */
1.57      xsa      1577: const char *
1.32      jfb      1578: rcs_errstr(int code)
                   1579: {
1.50      jfb      1580:        const char *esp;
                   1581:
1.168     deraadt  1582:        if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
1.50      jfb      1583:                esp = NULL;
                   1584:        else if (code == RCS_ERR_ERRNO)
                   1585:                esp = strerror(errno);
                   1586:        else
                   1587:                esp = rcs_errstrs[code];
                   1588:        return (esp);
1.1       jfb      1589: }
                   1590:
1.117     niallo   1591: /* rcs_parse_deltas()
                   1592:  *
                   1593:  * Parse deltas. If <rev> is not NULL, parse only as far as that
                   1594:  * revision. If <rev> is NULL, parse all deltas.
                   1595:  */
1.146     xsa      1596: static void
1.117     niallo   1597: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
                   1598: {
                   1599:        int ret;
                   1600:        struct rcs_delta *enddelta;
1.146     xsa      1601:
                   1602:        if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
                   1603:                return;
                   1604:
1.117     niallo   1605:        for (;;) {
                   1606:                ret = rcs_parse_delta(rfp);
                   1607:                if (rev != NULL) {
                   1608:                        enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.155     ray      1609:                        if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1.117     niallo   1610:                                break;
                   1611:                }
1.180   ! joris    1612:
1.117     niallo   1613:                if (ret == 0) {
                   1614:                        rfp->rf_flags |= PARSED_DELTAS;
                   1615:                        break;
                   1616:                }
                   1617:                else if (ret == -1)
                   1618:                        fatal("error parsing deltas");
                   1619:        }
                   1620: }
                   1621:
                   1622: /* rcs_parse_deltatexts()
                   1623:  *
                   1624:  * Parse deltatexts. If <rev> is not NULL, parse only as far as that
                   1625:  * revision. If <rev> is NULL, parse everything.
                   1626:  */
1.146     xsa      1627: static void
1.117     niallo   1628: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
                   1629: {
                   1630:        int ret;
                   1631:        struct rcs_delta *rdp;
1.146     xsa      1632:
1.168     deraadt  1633:        if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
                   1634:            (rfp->rf_flags & RCS_CREATE))
1.146     xsa      1635:                return;
                   1636:
1.117     niallo   1637:        if (!(rfp->rf_flags & PARSED_DESC))
                   1638:                rcs_parse_desc(rfp, rev);
                   1639:        for (;;) {
                   1640:                if (rev != NULL) {
                   1641:                        rdp = rcs_findrev(rfp, rev);
                   1642:                        if (rdp->rd_text != NULL)
                   1643:                                break;
                   1644:                        else
                   1645:                                ret = rcs_parse_deltatext(rfp);
                   1646:                } else
                   1647:                        ret = rcs_parse_deltatext(rfp);
                   1648:                if (ret == 0) {
                   1649:                        rfp->rf_flags |= PARSED_DELTATEXTS;
                   1650:                        break;
                   1651:                }
1.146     xsa      1652:                else if (ret == -1)
1.117     niallo   1653:                        fatal("problem parsing deltatexts");
                   1654:        }
                   1655: }
                   1656:
                   1657: /* rcs_parse_desc()
                   1658:  *
                   1659:  * Parse RCS description.
                   1660:  */
1.148     xsa      1661: static void
1.117     niallo   1662: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
                   1663: {
                   1664:        int ret = 0;
1.148     xsa      1665:
                   1666:        if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
                   1667:                return;
1.122     reyk     1668:        if (!(rfp->rf_flags & PARSED_DELTAS))
1.117     niallo   1669:                rcs_parse_deltas(rfp, rev);
                   1670:        /* do parsing */
                   1671:        ret = rcs_gettok(rfp);
1.148     xsa      1672:        if (ret != RCS_TOK_DESC)
                   1673:                fatal("token `%s' found where RCS desc expected",
1.117     niallo   1674:                    RCS_TOKSTR(rfp));
                   1675:
                   1676:        ret = rcs_gettok(rfp);
1.148     xsa      1677:        if (ret != RCS_TOK_STRING)
                   1678:                fatal("token `%s' found where RCS desc expected",
1.117     niallo   1679:                    RCS_TOKSTR(rfp));
                   1680:
                   1681:        rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
                   1682:        rfp->rf_flags |= PARSED_DESC;
                   1683: }
                   1684:
1.1       jfb      1685: /*
1.117     niallo   1686:  * rcs_parse_init()
1.1       jfb      1687:  *
1.117     niallo   1688:  * Initial parsing of file <path>, which are in the RCS format.
1.167     ray      1689:  * Just does admin section.
1.1       jfb      1690:  */
1.167     ray      1691: static void
1.117     niallo   1692: rcs_parse_init(RCSFILE *rfp)
1.1       jfb      1693: {
                   1694:        struct rcs_pdata *pdp;
                   1695:
1.26      jfb      1696:        if (rfp->rf_flags & RCS_PARSED)
1.167     ray      1697:                return;
1.1       jfb      1698:
1.161     ray      1699:        pdp = xcalloc(1, sizeof(*pdp));
1.1       jfb      1700:
1.18      jfb      1701:        pdp->rp_lines = 0;
1.1       jfb      1702:        pdp->rp_pttype = RCS_TOK_ERR;
                   1703:
1.172     joris    1704:        if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
                   1705:                fatal("fopen: `%s'", rfp->rf_path);
1.1       jfb      1706:
1.161     ray      1707:        pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1.1       jfb      1708:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1709:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1710:
                   1711:        /* ditch the strict lock */
1.26      jfb      1712:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1713:        rfp->rf_pdata = pdp;
                   1714:
1.167     ray      1715:        if (rcs_parse_admin(rfp) < 0) {
1.1       jfb      1716:                rcs_freepdata(pdp);
1.117     niallo   1717:                fatal("could not parse admin data");
1.1       jfb      1718:        }
                   1719:
1.117     niallo   1720:        if (rfp->rf_flags & RCS_PARSE_FULLY)
                   1721:                rcs_parse_deltatexts(rfp, NULL);
1.1       jfb      1722:
1.117     niallo   1723:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb      1724: }
                   1725:
                   1726: /*
                   1727:  * rcs_parse_admin()
                   1728:  *
                   1729:  * Parse the administrative portion of an RCS file.
1.31      jfb      1730:  * Returns the type of the first token found after the admin section on
                   1731:  * success, or -1 on failure.
1.1       jfb      1732:  */
                   1733: static int
                   1734: rcs_parse_admin(RCSFILE *rfp)
                   1735: {
                   1736:        u_int i;
                   1737:        int tok, ntok, hmask;
                   1738:        struct rcs_key *rk;
                   1739:
1.180   ! joris    1740:        rfp->rf_head = NULL;
        !          1741:        rfp->rf_branch = NULL;
        !          1742:
1.1       jfb      1743:        /* hmask is a mask of the headers already encountered */
                   1744:        hmask = 0;
                   1745:        for (;;) {
                   1746:                tok = rcs_gettok(rfp);
                   1747:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1748:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1749:                        cvs_log(LP_ERR, "parse error in RCS admin section");
1.147     ray      1750:                        goto fail;
1.168     deraadt  1751:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.31      jfb      1752:                        /*
                   1753:                         * Assume this is the start of the first delta or
                   1754:                         * that we are dealing with an empty RCS file and
                   1755:                         * we just found the description.
                   1756:                         */
1.1       jfb      1757:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31      jfb      1758:                        return (tok);
1.1       jfb      1759:                }
                   1760:
                   1761:                rk = NULL;
1.18      jfb      1762:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1763:                        if (rcs_keys[i].rk_id == tok)
                   1764:                                rk = &(rcs_keys[i]);
                   1765:
                   1766:                if (hmask & (1 << tok)) {
1.50      jfb      1767:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1768:                        cvs_log(LP_ERR, "duplicate RCS key");
1.147     ray      1769:                        goto fail;
1.1       jfb      1770:                }
                   1771:                hmask |= (1 << tok);
                   1772:
                   1773:                switch (tok) {
                   1774:                case RCS_TOK_HEAD:
                   1775:                case RCS_TOK_BRANCH:
                   1776:                case RCS_TOK_COMMENT:
                   1777:                case RCS_TOK_EXPAND:
                   1778:                        ntok = rcs_gettok(rfp);
                   1779:                        if (ntok == RCS_TOK_SCOLON)
                   1780:                                break;
                   1781:                        if (ntok != rk->rk_val) {
1.50      jfb      1782:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1783:                                cvs_log(LP_ERR,
                   1784:                                    "invalid value type for RCS key `%s'",
                   1785:                                    rk->rk_str);
                   1786:                        }
                   1787:
                   1788:                        if (tok == RCS_TOK_HEAD) {
1.111     joris    1789:                                if (rfp->rf_head == NULL)
1.28      jfb      1790:                                        rfp->rf_head = rcsnum_alloc();
1.1       jfb      1791:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1792:                                    rfp->rf_head);
1.14      deraadt  1793:                        } else if (tok == RCS_TOK_BRANCH) {
1.111     joris    1794:                                if (rfp->rf_branch == NULL)
1.35      jfb      1795:                                        rfp->rf_branch = rcsnum_alloc();
                   1796:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1797:                                    rfp->rf_branch) < 0)
1.147     ray      1798:                                        goto fail;
1.14      deraadt  1799:                        } else if (tok == RCS_TOK_COMMENT) {
1.110     joris    1800:                                rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14      deraadt  1801:                        } else if (tok == RCS_TOK_EXPAND) {
1.110     joris    1802:                                rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1803:                        }
                   1804:
                   1805:                        /* now get the expected semi-colon */
                   1806:                        ntok = rcs_gettok(rfp);
                   1807:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1808:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1809:                                cvs_log(LP_ERR,
                   1810:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1811:                                    rk->rk_str);
1.147     ray      1812:                                goto fail;
1.1       jfb      1813:                        }
                   1814:                        break;
                   1815:                case RCS_TOK_ACCESS:
1.29      jfb      1816:                        if (rcs_parse_access(rfp) < 0)
1.147     ray      1817:                                goto fail;
1.1       jfb      1818:                        break;
                   1819:                case RCS_TOK_SYMBOLS:
1.29      jfb      1820:                        if (rcs_parse_symbols(rfp) < 0)
1.147     ray      1821:                                goto fail;
1.1       jfb      1822:                        break;
                   1823:                case RCS_TOK_LOCKS:
1.29      jfb      1824:                        if (rcs_parse_locks(rfp) < 0)
1.147     ray      1825:                                goto fail;
1.1       jfb      1826:                        break;
                   1827:                default:
1.50      jfb      1828:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1829:                        cvs_log(LP_ERR,
                   1830:                            "unexpected token `%s' in RCS admin section",
                   1831:                            RCS_TOKSTR(rfp));
1.147     ray      1832:                        goto fail;
1.1       jfb      1833:                }
                   1834:        }
                   1835:
1.147     ray      1836: fail:
                   1837:        return (-1);
1.1       jfb      1838: }
                   1839:
                   1840: /*
                   1841:  * rcs_parse_delta()
                   1842:  *
                   1843:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1844:  * information in the <rfp> delta list.
                   1845:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1846:  * -1 on error.
                   1847:  */
                   1848: static int
                   1849: rcs_parse_delta(RCSFILE *rfp)
                   1850: {
                   1851:        int ret, tok, ntok, hmask;
                   1852:        u_int i;
                   1853:        char *tokstr;
1.3       vincent  1854:        RCSNUM *datenum;
1.1       jfb      1855:        struct rcs_delta *rdp;
                   1856:        struct rcs_key *rk;
                   1857:
1.161     ray      1858:        rdp = xcalloc(1, sizeof(*rdp));
1.1       jfb      1859:
                   1860:        rdp->rd_num = rcsnum_alloc();
                   1861:        rdp->rd_next = rcsnum_alloc();
                   1862:
                   1863:        TAILQ_INIT(&(rdp->rd_branches));
                   1864:
                   1865:        tok = rcs_gettok(rfp);
1.154     ray      1866:        if (tok == RCS_TOK_DESC) {
                   1867:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1868:                return (0);
                   1869:        } else if (tok != RCS_TOK_NUM) {
1.52      jfb      1870:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1871:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1872:                    RCS_TOKSTR(rfp));
                   1873:                rcs_freedelta(rdp);
                   1874:                return (-1);
                   1875:        }
                   1876:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1877:
                   1878:        hmask = 0;
                   1879:        ret = 0;
                   1880:        tokstr = NULL;
                   1881:
                   1882:        for (;;) {
                   1883:                tok = rcs_gettok(rfp);
                   1884:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1885:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1886:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1887:                        rcs_freedelta(rdp);
                   1888:                        return (-1);
1.14      deraadt  1889:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1890:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1891:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1892:                        break;
                   1893:                }
                   1894:
                   1895:                rk = NULL;
1.18      jfb      1896:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1897:                        if (rcs_keys[i].rk_id == tok)
                   1898:                                rk = &(rcs_keys[i]);
                   1899:
                   1900:                if (hmask & (1 << tok)) {
1.50      jfb      1901:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1902:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1903:                        rcs_freedelta(rdp);
                   1904:                        return (-1);
                   1905:                }
                   1906:                hmask |= (1 << tok);
                   1907:
                   1908:                switch (tok) {
                   1909:                case RCS_TOK_DATE:
                   1910:                case RCS_TOK_AUTHOR:
                   1911:                case RCS_TOK_STATE:
                   1912:                case RCS_TOK_NEXT:
                   1913:                        ntok = rcs_gettok(rfp);
                   1914:                        if (ntok == RCS_TOK_SCOLON) {
                   1915:                                if (rk->rk_flags & RCS_VOPT)
                   1916:                                        break;
                   1917:                                else {
1.50      jfb      1918:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1919:                                        cvs_log(LP_ERR, "missing mandatory "
                   1920:                                            "value to RCS key `%s'",
                   1921:                                            rk->rk_str);
                   1922:                                        rcs_freedelta(rdp);
                   1923:                                        return (-1);
                   1924:                                }
                   1925:                        }
                   1926:
                   1927:                        if (ntok != rk->rk_val) {
1.50      jfb      1928:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1929:                                cvs_log(LP_ERR,
                   1930:                                    "invalid value type for RCS key `%s'",
                   1931:                                    rk->rk_str);
                   1932:                                rcs_freedelta(rdp);
                   1933:                                return (-1);
                   1934:                        }
                   1935:
                   1936:                        if (tokstr != NULL)
1.110     joris    1937:                                xfree(tokstr);
                   1938:                        tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1939:                        /* now get the expected semi-colon */
                   1940:                        ntok = rcs_gettok(rfp);
                   1941:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1942:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1943:                                cvs_log(LP_ERR,
                   1944:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1945:                                    rk->rk_str);
1.110     joris    1946:                                xfree(tokstr);
1.1       jfb      1947:                                rcs_freedelta(rdp);
                   1948:                                return (-1);
                   1949:                        }
                   1950:
                   1951:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1952:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110     joris    1953:                                        xfree(tokstr);
1.11      joris    1954:                                        rcs_freedelta(rdp);
                   1955:                                        return (-1);
                   1956:                                }
1.3       vincent  1957:                                if (datenum->rn_len != 6) {
1.50      jfb      1958:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1959:                                        cvs_log(LP_ERR,
                   1960:                                            "RCS date specification has %s "
                   1961:                                            "fields",
1.3       vincent  1962:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1963:                                            "missing");
1.110     joris    1964:                                        xfree(tokstr);
1.1       jfb      1965:                                        rcs_freedelta(rdp);
1.37      tedu     1966:                                        rcsnum_free(datenum);
                   1967:                                        return (-1);
1.1       jfb      1968:                                }
1.3       vincent  1969:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1970:                                if (rdp->rd_date.tm_year >= 1900)
                   1971:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1972:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1973:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1974:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1975:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1976:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1977:                                rcsnum_free(datenum);
1.14      deraadt  1978:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1979:                                rdp->rd_author = tokstr;
                   1980:                                tokstr = NULL;
1.14      deraadt  1981:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1982:                                rdp->rd_state = tokstr;
                   1983:                                tokstr = NULL;
1.14      deraadt  1984:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      1985:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1986:                        }
                   1987:                        break;
                   1988:                case RCS_TOK_BRANCHES:
1.46      jfb      1989:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   1990:                                rcs_freedelta(rdp);
                   1991:                                return (-1);
                   1992:                        }
1.1       jfb      1993:                        break;
                   1994:                default:
1.50      jfb      1995:                        rcs_errno = RCS_ERR_PARSE;
1.172     joris    1996:                        cvs_log(LP_ERR, "unexpected token `%s' in RCS delta",
1.1       jfb      1997:                            RCS_TOKSTR(rfp));
                   1998:                        rcs_freedelta(rdp);
                   1999:                        return (-1);
                   2000:                }
                   2001:        }
                   2002:
1.13      jfb      2003:        if (tokstr != NULL)
1.110     joris    2004:                xfree(tokstr);
1.13      jfb      2005:
1.1       jfb      2006:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      2007:        rfp->rf_ndelta++;
1.1       jfb      2008:
                   2009:        return (ret);
                   2010: }
                   2011:
                   2012: /*
                   2013:  * rcs_parse_deltatext()
                   2014:  *
                   2015:  * Parse an RCS delta text section and fill in the log and text field of the
                   2016:  * appropriate delta section.
                   2017:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   2018:  * -1 on error.
                   2019:  */
                   2020: static int
                   2021: rcs_parse_deltatext(RCSFILE *rfp)
                   2022: {
                   2023:        int tok;
                   2024:        RCSNUM *tnum;
                   2025:        struct rcs_delta *rdp;
                   2026:
                   2027:        tok = rcs_gettok(rfp);
                   2028:        if (tok == RCS_TOK_EOF)
                   2029:                return (0);
                   2030:
                   2031:        if (tok != RCS_TOK_NUM) {
1.50      jfb      2032:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2033:                cvs_log(LP_ERR,
                   2034:                    "unexpected token `%s' at start of RCS delta text",
                   2035:                    RCS_TOKSTR(rfp));
                   2036:                return (-1);
                   2037:        }
1.13      jfb      2038:
                   2039:        tnum = rcsnum_alloc();
1.1       jfb      2040:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   2041:
                   2042:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2043:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   2044:                        break;
                   2045:        }
1.13      jfb      2046:        rcsnum_free(tnum);
                   2047:
1.1       jfb      2048:        if (rdp == NULL) {
                   2049:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   2050:                    RCS_TOKSTR(rfp));
                   2051:                return (-1);
                   2052:        }
                   2053:
                   2054:        tok = rcs_gettok(rfp);
                   2055:        if (tok != RCS_TOK_LOG) {
1.50      jfb      2056:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2057:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2058:                    RCS_TOKSTR(rfp));
                   2059:                return (-1);
                   2060:        }
                   2061:
                   2062:        tok = rcs_gettok(rfp);
                   2063:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2064:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2065:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2066:                    RCS_TOKSTR(rfp));
                   2067:                return (-1);
                   2068:        }
1.110     joris    2069:        rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2070:        tok = rcs_gettok(rfp);
                   2071:        if (tok != RCS_TOK_TEXT) {
1.50      jfb      2072:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2073:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2074:                    RCS_TOKSTR(rfp));
                   2075:                return (-1);
                   2076:        }
                   2077:
                   2078:        tok = rcs_gettok(rfp);
                   2079:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2080:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2081:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2082:                    RCS_TOKSTR(rfp));
                   2083:                return (-1);
                   2084:        }
                   2085:
1.161     ray      2086:        rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.172     joris    2087:        if (strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1)) >=
                   2088:            RCS_TOKLEN(rfp) + 1)
                   2089:                fatal("rcs_parse_deltatext: strlcpy");
1.42      jfb      2090:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2091:
                   2092:        return (1);
                   2093: }
                   2094:
                   2095: /*
                   2096:  * rcs_parse_access()
                   2097:  *
                   2098:  * Parse the access list given as value to the `access' keyword.
                   2099:  * Returns 0 on success, or -1 on failure.
                   2100:  */
                   2101: static int
                   2102: rcs_parse_access(RCSFILE *rfp)
                   2103: {
                   2104:        int type;
                   2105:
                   2106:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2107:                if (type != RCS_TOK_ID) {
1.50      jfb      2108:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2109:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2110:                            RCS_TOKSTR(rfp));
                   2111:                        return (-1);
                   2112:                }
1.29      jfb      2113:
                   2114:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2115:                        return (-1);
1.1       jfb      2116:        }
                   2117:
                   2118:        return (0);
                   2119: }
                   2120:
                   2121: /*
                   2122:  * rcs_parse_symbols()
                   2123:  *
                   2124:  * Parse the symbol list given as value to the `symbols' keyword.
                   2125:  * Returns 0 on success, or -1 on failure.
                   2126:  */
                   2127: static int
                   2128: rcs_parse_symbols(RCSFILE *rfp)
                   2129: {
                   2130:        int type;
                   2131:        struct rcs_sym *symp;
                   2132:
                   2133:        for (;;) {
                   2134:                type = rcs_gettok(rfp);
                   2135:                if (type == RCS_TOK_SCOLON)
                   2136:                        break;
                   2137:
1.41      jfb      2138:                if (type != RCS_TOK_ID) {
1.50      jfb      2139:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2140:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2141:                            RCS_TOKSTR(rfp));
                   2142:                        return (-1);
                   2143:                }
                   2144:
1.161     ray      2145:                symp = xmalloc(sizeof(*symp));
1.110     joris    2146:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2147:                symp->rs_num = rcsnum_alloc();
                   2148:
                   2149:                type = rcs_gettok(rfp);
                   2150:                if (type != RCS_TOK_COLON) {
1.50      jfb      2151:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2152:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2153:                            RCS_TOKSTR(rfp));
1.11      joris    2154:                        rcsnum_free(symp->rs_num);
1.110     joris    2155:                        xfree(symp->rs_name);
                   2156:                        xfree(symp);
1.1       jfb      2157:                        return (-1);
                   2158:                }
                   2159:
                   2160:                type = rcs_gettok(rfp);
                   2161:                if (type != RCS_TOK_NUM) {
1.50      jfb      2162:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2163:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2164:                            RCS_TOKSTR(rfp));
1.11      joris    2165:                        rcsnum_free(symp->rs_num);
1.110     joris    2166:                        xfree(symp->rs_name);
                   2167:                        xfree(symp);
1.1       jfb      2168:                        return (-1);
                   2169:                }
                   2170:
                   2171:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2172:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2173:                            RCS_TOKSTR(rfp));
1.11      joris    2174:                        rcsnum_free(symp->rs_num);
1.110     joris    2175:                        xfree(symp->rs_name);
                   2176:                        xfree(symp);
1.1       jfb      2177:                        return (-1);
                   2178:                }
                   2179:
1.43      jfb      2180:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2181:        }
                   2182:
                   2183:        return (0);
                   2184: }
                   2185:
                   2186: /*
                   2187:  * rcs_parse_locks()
                   2188:  *
                   2189:  * Parse the lock list given as value to the `locks' keyword.
                   2190:  * Returns 0 on success, or -1 on failure.
                   2191:  */
                   2192: static int
                   2193: rcs_parse_locks(RCSFILE *rfp)
                   2194: {
                   2195:        int type;
                   2196:        struct rcs_lock *lkp;
                   2197:
                   2198:        for (;;) {
                   2199:                type = rcs_gettok(rfp);
                   2200:                if (type == RCS_TOK_SCOLON)
                   2201:                        break;
                   2202:
                   2203:                if (type != RCS_TOK_ID) {
1.50      jfb      2204:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2205:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2206:                            RCS_TOKSTR(rfp));
                   2207:                        return (-1);
                   2208:                }
                   2209:
1.161     ray      2210:                lkp = xmalloc(sizeof(*lkp));
1.110     joris    2211:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2212:                lkp->rl_num = rcsnum_alloc();
                   2213:
                   2214:                type = rcs_gettok(rfp);
                   2215:                if (type != RCS_TOK_COLON) {
1.50      jfb      2216:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2217:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2218:                            RCS_TOKSTR(rfp));
1.37      tedu     2219:                        rcsnum_free(lkp->rl_num);
1.110     joris    2220:                        xfree(lkp->rl_name);
                   2221:                        xfree(lkp);
1.1       jfb      2222:                        return (-1);
                   2223:                }
                   2224:
                   2225:                type = rcs_gettok(rfp);
                   2226:                if (type != RCS_TOK_NUM) {
1.50      jfb      2227:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2228:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2229:                            RCS_TOKSTR(rfp));
1.37      tedu     2230:                        rcsnum_free(lkp->rl_num);
1.110     joris    2231:                        xfree(lkp->rl_name);
                   2232:                        xfree(lkp);
1.1       jfb      2233:                        return (-1);
                   2234:                }
                   2235:
                   2236:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2237:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2238:                            RCS_TOKSTR(rfp));
1.37      tedu     2239:                        rcsnum_free(lkp->rl_num);
1.110     joris    2240:                        xfree(lkp->rl_name);
                   2241:                        xfree(lkp);
1.1       jfb      2242:                        return (-1);
                   2243:                }
                   2244:
                   2245:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2246:        }
                   2247:
                   2248:        /* check if we have a `strict' */
                   2249:        type = rcs_gettok(rfp);
                   2250:        if (type != RCS_TOK_STRICT) {
                   2251:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2252:        } else {
1.26      jfb      2253:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2254:
                   2255:                type = rcs_gettok(rfp);
                   2256:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2257:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2258:                        cvs_log(LP_ERR,
                   2259:                            "missing semi-colon after `strict' keyword");
                   2260:                        return (-1);
                   2261:                }
                   2262:        }
                   2263:
                   2264:        return (0);
                   2265: }
                   2266:
                   2267: /*
                   2268:  * rcs_parse_branches()
                   2269:  *
                   2270:  * Parse the list of branches following a `branches' keyword in a delta.
                   2271:  * Returns 0 on success, or -1 on failure.
                   2272:  */
                   2273: static int
                   2274: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2275: {
                   2276:        int type;
                   2277:        struct rcs_branch *brp;
                   2278:
                   2279:        for (;;) {
                   2280:                type = rcs_gettok(rfp);
                   2281:                if (type == RCS_TOK_SCOLON)
                   2282:                        break;
                   2283:
                   2284:                if (type != RCS_TOK_NUM) {
1.50      jfb      2285:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2286:                        cvs_log(LP_ERR,
                   2287:                            "unexpected token `%s' in list of branches",
                   2288:                            RCS_TOKSTR(rfp));
                   2289:                        return (-1);
                   2290:                }
                   2291:
1.161     ray      2292:                brp = xmalloc(sizeof(*brp));
1.46      jfb      2293:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2294:                if (brp->rb_num == NULL) {
1.110     joris    2295:                        xfree(brp);
1.11      joris    2296:                        return (-1);
                   2297:                }
1.1       jfb      2298:
                   2299:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2300:        }
                   2301:
                   2302:        return (0);
                   2303: }
                   2304:
                   2305: /*
                   2306:  * rcs_freedelta()
                   2307:  *
                   2308:  * Free the contents of a delta structure.
                   2309:  */
1.18      jfb      2310: static void
1.1       jfb      2311: rcs_freedelta(struct rcs_delta *rdp)
                   2312: {
1.12      jfb      2313:        struct rcs_branch *rb;
1.1       jfb      2314:
1.12      jfb      2315:        if (rdp->rd_num != NULL)
                   2316:                rcsnum_free(rdp->rd_num);
                   2317:        if (rdp->rd_next != NULL)
                   2318:                rcsnum_free(rdp->rd_next);
                   2319:
1.1       jfb      2320:        if (rdp->rd_author != NULL)
1.110     joris    2321:                xfree(rdp->rd_author);
1.109     joris    2322:        if (rdp->rd_locker != NULL)
1.110     joris    2323:                xfree(rdp->rd_locker);
1.1       jfb      2324:        if (rdp->rd_state != NULL)
1.110     joris    2325:                xfree(rdp->rd_state);
1.1       jfb      2326:        if (rdp->rd_log != NULL)
1.110     joris    2327:                xfree(rdp->rd_log);
1.1       jfb      2328:        if (rdp->rd_text != NULL)
1.110     joris    2329:                xfree(rdp->rd_text);
1.12      jfb      2330:
                   2331:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2332:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2333:                rcsnum_free(rb->rb_num);
1.110     joris    2334:                xfree(rb);
1.1       jfb      2335:        }
                   2336:
1.110     joris    2337:        xfree(rdp);
1.1       jfb      2338: }
                   2339:
                   2340: /*
                   2341:  * rcs_freepdata()
                   2342:  *
                   2343:  * Free the contents of the parser data structure.
                   2344:  */
                   2345: static void
                   2346: rcs_freepdata(struct rcs_pdata *pd)
                   2347: {
                   2348:        if (pd->rp_file != NULL)
                   2349:                (void)fclose(pd->rp_file);
                   2350:        if (pd->rp_buf != NULL)
1.110     joris    2351:                xfree(pd->rp_buf);
                   2352:        xfree(pd);
1.1       jfb      2353: }
                   2354:
                   2355: /*
                   2356:  * rcs_gettok()
                   2357:  *
                   2358:  * Get the next RCS token from the string <str>.
                   2359:  */
                   2360: static int
                   2361: rcs_gettok(RCSFILE *rfp)
                   2362: {
                   2363:        u_int i;
                   2364:        int ch, last, type;
1.18      jfb      2365:        size_t len;
                   2366:        char *bp;
1.1       jfb      2367:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2368:
                   2369:        type = RCS_TOK_ERR;
                   2370:        bp = pdp->rp_buf;
1.42      jfb      2371:        pdp->rp_tlen = 0;
1.1       jfb      2372:        *bp = '\0';
                   2373:
                   2374:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2375:                type = pdp->rp_pttype;
1.172     joris    2376:                if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
                   2377:                    pdp->rp_blen)
                   2378:                        fatal("rcs_gettok: strlcpy");
1.1       jfb      2379:                pdp->rp_pttype = RCS_TOK_ERR;
                   2380:                return (type);
                   2381:        }
                   2382:
                   2383:        /* skip leading whitespace */
                   2384:        /* XXX we must skip backspace too for compatibility, should we? */
                   2385:        do {
                   2386:                ch = getc(pdp->rp_file);
                   2387:                if (ch == '\n')
1.18      jfb      2388:                        pdp->rp_lines++;
1.1       jfb      2389:        } while (isspace(ch));
                   2390:
                   2391:        if (ch == EOF) {
                   2392:                type = RCS_TOK_EOF;
1.14      deraadt  2393:        } else if (ch == ';') {
1.1       jfb      2394:                type = RCS_TOK_SCOLON;
1.14      deraadt  2395:        } else if (ch == ':') {
1.1       jfb      2396:                type = RCS_TOK_COLON;
1.14      deraadt  2397:        } else if (isalpha(ch)) {
1.31      jfb      2398:                type = RCS_TOK_ID;
1.1       jfb      2399:                *(bp++) = ch;
1.18      jfb      2400:                for (;;) {
1.1       jfb      2401:                        ch = getc(pdp->rp_file);
1.160     joris    2402:                        if (!isalnum(ch) && ch != '_' && ch != '-' &&
                   2403:                            ch != '/') {
1.1       jfb      2404:                                ungetc(ch, pdp->rp_file);
                   2405:                                break;
                   2406:                        }
                   2407:                        *(bp++) = ch;
1.42      jfb      2408:                        pdp->rp_tlen++;
1.18      jfb      2409:                        if (bp == pdp->rp_bufend - 1) {
                   2410:                                len = bp - pdp->rp_buf;
1.151     xsa      2411:                                rcs_growbuf(rfp);
1.18      jfb      2412:                                bp = pdp->rp_buf + len;
                   2413:                        }
1.1       jfb      2414:                }
                   2415:                *bp = '\0';
                   2416:
1.18      jfb      2417:                if (type != RCS_TOK_ERR) {
                   2418:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2419:                                if (strcmp(rcs_keys[i].rk_str,
                   2420:                                    pdp->rp_buf) == 0) {
                   2421:                                        type = rcs_keys[i].rk_id;
                   2422:                                        break;
                   2423:                                }
1.1       jfb      2424:                        }
                   2425:                }
1.14      deraadt  2426:        } else if (ch == '@') {
1.1       jfb      2427:                /* we have a string */
1.18      jfb      2428:                type = RCS_TOK_STRING;
1.1       jfb      2429:                for (;;) {
                   2430:                        ch = getc(pdp->rp_file);
                   2431:                        if (ch == '@') {
                   2432:                                ch = getc(pdp->rp_file);
                   2433:                                if (ch != '@') {
                   2434:                                        ungetc(ch, pdp->rp_file);
                   2435:                                        break;
                   2436:                                }
1.14      deraadt  2437:                        } else if (ch == '\n')
1.18      jfb      2438:                                pdp->rp_lines++;
1.1       jfb      2439:
                   2440:                        *(bp++) = ch;
1.42      jfb      2441:                        pdp->rp_tlen++;
1.18      jfb      2442:                        if (bp == pdp->rp_bufend - 1) {
                   2443:                                len = bp - pdp->rp_buf;
1.151     xsa      2444:                                rcs_growbuf(rfp);
1.18      jfb      2445:                                bp = pdp->rp_buf + len;
                   2446:                        }
1.1       jfb      2447:                }
                   2448:
                   2449:                *bp = '\0';
1.14      deraadt  2450:        } else if (isdigit(ch)) {
1.1       jfb      2451:                *(bp++) = ch;
                   2452:                last = ch;
                   2453:                type = RCS_TOK_NUM;
                   2454:
                   2455:                for (;;) {
                   2456:                        ch = getc(pdp->rp_file);
1.18      jfb      2457:                        if (bp == pdp->rp_bufend)
1.1       jfb      2458:                                break;
                   2459:                        if (!isdigit(ch) && ch != '.') {
                   2460:                                ungetc(ch, pdp->rp_file);
                   2461:                                break;
                   2462:                        }
                   2463:
                   2464:                        if (last == '.' && ch == '.') {
                   2465:                                type = RCS_TOK_ERR;
                   2466:                                break;
                   2467:                        }
                   2468:                        last = ch;
                   2469:                        *(bp++) = ch;
1.42      jfb      2470:                        pdp->rp_tlen++;
1.1       jfb      2471:                }
1.18      jfb      2472:                *bp = '\0';
1.1       jfb      2473:        }
                   2474:
                   2475:        return (type);
                   2476: }
                   2477:
                   2478: /*
                   2479:  * rcs_pushtok()
                   2480:  *
                   2481:  * Push a token back in the parser's token buffer.
                   2482:  */
                   2483: static int
                   2484: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2485: {
                   2486:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2487:
                   2488:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2489:                return (-1);
                   2490:
                   2491:        pdp->rp_pttype = type;
1.172     joris    2492:        if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
                   2493:            sizeof(pdp->rp_ptok))
                   2494:                fatal("rcs_pushtok: strlcpy");
1.1       jfb      2495:        return (0);
                   2496: }
                   2497:
1.18      jfb      2498:
                   2499: /*
                   2500:  * rcs_growbuf()
                   2501:  *
                   2502:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2503:  * RCS_BUFEXTSIZE.
                   2504:  * In case of failure, the original buffer is left unmodified.
                   2505:  */
1.151     xsa      2506: static void
1.18      jfb      2507: rcs_growbuf(RCSFILE *rf)
                   2508: {
                   2509:        void *tmp;
                   2510:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2511:
1.153     ray      2512:        tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
1.161     ray      2513:        pdp->rp_buf = tmp;
1.18      jfb      2514:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2515:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2516: }
                   2517:
                   2518: /*
                   2519:  * rcs_strprint()
                   2520:  *
                   2521:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2522:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2523:  * binary data.
                   2524:  */
1.155     ray      2525: static void
1.42      jfb      2526: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2527: {
                   2528:        const u_char *ap, *ep, *sp;
1.52      jfb      2529:
                   2530:        if (slen == 0)
1.155     ray      2531:                return;
1.42      jfb      2532:
                   2533:        ep = str + slen - 1;
                   2534:
                   2535:        for (sp = str; sp <= ep;)  {
                   2536:                ap = memchr(sp, '@', ep - sp);
                   2537:                if (ap == NULL)
                   2538:                        ap = ep;
1.155     ray      2539:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42      jfb      2540:
                   2541:                if (*ap == '@')
                   2542:                        putc('@', stream);
                   2543:                sp = ap + 1;
1.63      joris    2544:        }
                   2545: }
                   2546:
                   2547: /*
                   2548:  * rcs_expand_keywords()
                   2549:  *
1.125     niallo   2550:  * Return expansion any RCS keywords in <data>
                   2551:  *
                   2552:  * On error, return NULL.
1.63      joris    2553:  */
1.125     niallo   2554: static char *
                   2555: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
1.63      joris    2556:     size_t len, int mode)
                   2557: {
1.155     ray      2558:        ptrdiff_t c_offset, sizdiff, start_offset;
1.147     ray      2559:        size_t i;
1.155     ray      2560:        int kwtype;
                   2561:        u_int j, found;
1.140     xsa      2562:        char *c, *kwstr, *start, *end, *tbuf;
1.125     niallo   2563:        char expbuf[256], buf[256];
1.134     joris    2564:        char *fmt;
1.63      joris    2565:
1.65      niallo   2566:        kwtype = 0;
                   2567:        kwstr = NULL;
1.63      joris    2568:        i = 0;
                   2569:
                   2570:        /*
                   2571:         * Keyword formats:
                   2572:         * $Keyword$
                   2573:         * $Keyword: value$
                   2574:         */
1.144     deraadt  2575:        for (c = data; *c != '\0' && i < len; c++) {
1.63      joris    2576:                if (*c == '$') {
                   2577:                        /* remember start of this possible keyword */
                   2578:                        start = c;
1.125     niallo   2579:                        start_offset = start - data;
1.63      joris    2580:
                   2581:                        /* first following character has to be alphanumeric */
1.144     deraadt  2582:                        c++;
1.63      joris    2583:                        if (!isalpha(*c)) {
                   2584:                                c = start;
                   2585:                                continue;
                   2586:                        }
                   2587:
                   2588:                        /* look for any matching keywords */
                   2589:                        found = 0;
                   2590:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2591:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2592:                                    strlen(rcs_expkw[j].kw_str))) {
                   2593:                                        found = 1;
                   2594:                                        kwstr = rcs_expkw[j].kw_str;
                   2595:                                        kwtype = rcs_expkw[j].kw_type;
                   2596:                                        break;
                   2597:                                }
1.174     joris    2598:                        }
                   2599:
                   2600:                        if (cvs_tagname != NULL &&
                   2601:                            !strncmp(c, cvs_tagname, strlen(cvs_tagname)) &&
                   2602:                            found != 1) {
                   2603:                                found = 1;
                   2604:                                kwstr = cvs_tagname;
                   2605:                                kwtype = RCS_KW_ID;
1.63      joris    2606:                        }
                   2607:
                   2608:                        /* unknown keyword, continue looking */
                   2609:                        if (found == 0) {
                   2610:                                c = start;
                   2611:                                continue;
                   2612:                        }
                   2613:
                   2614:                        /* next character has to be ':' or '$' */
                   2615:                        c += strlen(kwstr);
                   2616:                        if (*c != ':' && *c != '$') {
                   2617:                                c = start;
                   2618:                                continue;
                   2619:                        }
                   2620:
                   2621:                        /*
                   2622:                         * if the next character was ':' we need to look for
                   2623:                         * an '$' before the end of the line to be sure it is
                   2624:                         * in fact a keyword.
                   2625:                         */
                   2626:                        if (*c == ':') {
                   2627:                                while (*c++) {
                   2628:                                        if (*c == '$' || *c == '\n')
                   2629:                                                break;
                   2630:                                }
                   2631:
                   2632:                                if (*c != '$') {
                   2633:                                        c = start;
                   2634:                                        continue;
                   2635:                                }
                   2636:                        }
1.125     niallo   2637:                        c_offset = c - data;
                   2638:                        end = c + 1;
1.63      joris    2639:
                   2640:                        /* start constructing the expansion */
                   2641:                        expbuf[0] = '\0';
                   2642:
                   2643:                        if (mode & RCS_KWEXP_NAME) {
1.172     joris    2644:                                if (strlcat(expbuf, "$", sizeof(expbuf))
                   2645:                                    >= sizeof(expbuf) ||
                   2646:                                    strlcat(expbuf, kwstr, sizeof(expbuf))
                   2647:                                    >= sizeof(expbuf))
                   2648:                                        fatal("rcs_expand_keywords: truncated");
                   2649:                                if ((mode & RCS_KWEXP_VAL) &&
                   2650:                                    strlcat(expbuf, ": ", sizeof(expbuf))
                   2651:                                    >= sizeof(expbuf))
                   2652:                                        fatal("rcs_expand_keywords: truncated");
1.63      joris    2653:                        }
                   2654:
                   2655:                        /*
1.80      reyk     2656:                         * order matters because of RCS_KW_ID and
                   2657:                         * RCS_KW_HEADER here
1.63      joris    2658:                         */
                   2659:                        if (mode & RCS_KWEXP_VAL) {
                   2660:                                if (kwtype & RCS_KW_RCSFILE) {
                   2661:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.172     joris    2662:                                                (void)strlcat(expbuf,
1.80      reyk     2663:                                                    basename(rcsfile),
1.63      joris    2664:                                                    sizeof(expbuf));
                   2665:                                        else
1.172     joris    2666:                                                (void)strlcat(expbuf,
                   2667:                                                    rcsfile, sizeof(expbuf));
                   2668:                                        if (strlcat(expbuf, " ",
                   2669:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2670:                                                fatal("rcs_expand_keywords: "
                   2671:                                                    "truncated");
1.63      joris    2672:                                }
                   2673:
                   2674:                                if (kwtype & RCS_KW_REVISION) {
1.80      reyk     2675:                                        rcsnum_tostr(rdp->rd_num, buf,
                   2676:                                            sizeof(buf));
1.172     joris    2677:                                        if (strlcat(buf, " ", sizeof(buf))
                   2678:                                            >= sizeof(buf) ||
                   2679:                                            strlcat(expbuf, buf,
                   2680:                                            sizeof(expbuf)) >= sizeof(buf))
                   2681:                                                fatal("rcs_expand_keywords: "
                   2682:                                                    "truncated");
1.63      joris    2683:                                }
                   2684:
                   2685:                                if (kwtype & RCS_KW_DATE) {
1.172     joris    2686:                                        fmt = "%Y/%m/%d %H:%M:%S ";
1.134     joris    2687:
1.173     joris    2688:                                        strftime(buf, sizeof(buf), fmt,
                   2689:                                            &rdp->rd_date);
1.172     joris    2690:                                        if (strlcat(expbuf, buf,
                   2691:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2692:                                                fatal("rcs_expand_keywords: "
                   2693:                                                    "string truncated");
1.63      joris    2694:                                }
                   2695:
                   2696:                                if (kwtype & RCS_KW_AUTHOR) {
1.172     joris    2697:                                        if (strlcat(expbuf, rdp->rd_author,
                   2698:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2699:                                            strlcat(expbuf, " ",
                   2700:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2701:                                                fatal("rcs_expand_keywords: "
                   2702:                                                    "string truncated");
1.63      joris    2703:                                }
                   2704:
                   2705:                                if (kwtype & RCS_KW_STATE) {
1.172     joris    2706:                                        if (strlcat(expbuf, rdp->rd_state,
                   2707:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2708:                                            strlcat(expbuf, " ",
                   2709:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2710:                                                fatal("rcs_expand_keywords: "
                   2711:                                                    "string truncated");
1.63      joris    2712:                                }
                   2713:
                   2714:                                /* order does not matter anymore below */
                   2715:                                if (kwtype & RCS_KW_LOG)
1.172     joris    2716:                                        if (strlcat(expbuf, " ",
                   2717:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2718:                                                fatal("rcs_expand_keywords: "
                   2719:                                                    "string truncated");
1.63      joris    2720:
                   2721:                                if (kwtype & RCS_KW_SOURCE) {
1.172     joris    2722:                                        if (strlcat(expbuf, rcsfile,
                   2723:                                            sizeof(expbuf)) >= sizeof(expbuf) ||
                   2724:                                            strlcat(expbuf, " ",
                   2725:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2726:                                                fatal("rcs_expand_keywords: "
                   2727:                                                    "string truncated");
1.63      joris    2728:                                }
                   2729:
                   2730:                                if (kwtype & RCS_KW_NAME)
1.172     joris    2731:                                        if (strlcat(expbuf, " ",
                   2732:                                            sizeof(expbuf)) >= sizeof(expbuf))
                   2733:                                                fatal("rcs_expand_keywords: "
                   2734:                                                    "string truncated");
1.63      joris    2735:                        }
                   2736:
                   2737:                        /* end the expansion */
                   2738:                        if (mode & RCS_KWEXP_NAME)
1.172     joris    2739:                                if (strlcat(expbuf, "$",
                   2740:                                    sizeof(expbuf)) >= sizeof(expbuf))
                   2741:                                        fatal("rcs_expand_keywords: truncated");
1.138     deraadt  2742:
1.125     niallo   2743:                        sizdiff = strlen(expbuf) - (end - start);
1.161     ray      2744:                        tbuf = xstrdup(end);
1.172     joris    2745:
1.125     niallo   2746:                        /* only realloc if we have to */
                   2747:                        if (sizdiff > 0) {
1.161     ray      2748:                                char *newdata;
                   2749:
1.125     niallo   2750:                                len += sizdiff;
1.161     ray      2751:                                newdata = xrealloc(data, 1, len);
                   2752:                                data = newdata;
1.172     joris    2753:
1.125     niallo   2754:                                /*
                   2755:                                 * ensure string pointers are not invalidated
                   2756:                                 * after realloc()
                   2757:                                 */
                   2758:                                start = data + start_offset;
                   2759:                                c = data + c_offset;
                   2760:                        }
1.172     joris    2761:                        if (strlcpy(start, expbuf, len) >= len ||
                   2762:                            strlcat(data, tbuf, len) >= len)
                   2763:                                fatal("rcs_expand_keywords: string truncated");
1.125     niallo   2764:                        xfree(tbuf);
1.63      joris    2765:                        i += strlen(expbuf);
                   2766:                }
1.42      jfb      2767:        }
1.81      niallo   2768:
1.125     niallo   2769:        return (data);
1.81      niallo   2770: }
                   2771:
                   2772: /*
                   2773:  * rcs_deltatext_set()
                   2774:  *
                   2775:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      2776:  * Returns -1 on error, 0 on success.
1.81      niallo   2777:  */
                   2778: int
                   2779: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2780: {
                   2781:        size_t len;
                   2782:        struct rcs_delta *rdp;
1.117     niallo   2783:
                   2784:        /* Write operations require full parsing */
                   2785:        rcs_parse_deltatexts(rfp, NULL);
1.81      niallo   2786:
                   2787:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2788:                return (-1);
                   2789:
                   2790:        if (rdp->rd_text != NULL)
1.110     joris    2791:                xfree(rdp->rd_text);
1.81      niallo   2792:
                   2793:        len = strlen(dtext);
1.103     joris    2794:        if (len != 0) {
1.161     ray      2795:                /* XXX - use xstrdup() if rd_text changes to char *. */
                   2796:                rdp->rd_text = xmalloc(len + 1);
1.155     ray      2797:                rdp->rd_tlen = len;
                   2798:                (void)memcpy(rdp->rd_text, dtext, len + 1);
1.103     joris    2799:        } else {
                   2800:                rdp->rd_text = NULL;
                   2801:                rdp->rd_tlen = 0;
                   2802:        }
1.18      jfb      2803:
1.86      joris    2804:        return (0);
                   2805: }
                   2806:
                   2807: /*
                   2808:  * rcs_rev_setlog()
                   2809:  *
                   2810:  * Sets the log message of revision <rev> to <logtext>
                   2811:  */
                   2812: int
                   2813: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2814: {
                   2815:        struct rcs_delta *rdp;
                   2816:        char buf[16];
                   2817:
                   2818:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2819:
                   2820:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2821:                return (-1);
                   2822:
                   2823:        if (rdp->rd_log != NULL)
1.110     joris    2824:                xfree(rdp->rd_log);
1.86      joris    2825:
1.110     joris    2826:        rdp->rd_log = xstrdup(logtext);
1.86      joris    2827:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   2828:        return (0);
                   2829: }
1.97      niallo   2830: /*
                   2831:  * rcs_rev_getdate()
                   2832:  *
                   2833:  * Get the date corresponding to a given revision.
                   2834:  * Returns the date on success, -1 on failure.
                   2835:  */
                   2836: time_t
                   2837: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2838: {
                   2839:        struct rcs_delta *rdp;
                   2840:
                   2841:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2842:                return (-1);
                   2843:
                   2844:        return (mktime(&rdp->rd_date));
                   2845: }
1.95      niallo   2846:
                   2847: /*
                   2848:  * rcs_state_set()
                   2849:  *
                   2850:  * Sets the state of revision <rev> to <state>
                   2851:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2852:  *
                   2853:  * Returns -1 on failure, 0 on success.
                   2854:  */
                   2855: int
                   2856: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2857: {
                   2858:        struct rcs_delta *rdp;
                   2859:
                   2860:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2861:                return (-1);
                   2862:
                   2863:        if (rdp->rd_state != NULL)
1.110     joris    2864:                xfree(rdp->rd_state);
1.95      niallo   2865:
1.110     joris    2866:        rdp->rd_state = xstrdup(state);
1.95      niallo   2867:
                   2868:        rfp->rf_flags &= ~RCS_SYNCED;
                   2869:
                   2870:        return (0);
                   2871: }
                   2872:
                   2873: /*
                   2874:  * rcs_state_check()
                   2875:  *
                   2876:  * Check if string <state> is valid.
                   2877:  *
1.96      xsa      2878:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   2879:  */
                   2880: int
                   2881: rcs_state_check(const char *state)
                   2882: {
                   2883:        if (strchr(state, ' ') != NULL)
                   2884:                return (-1);
                   2885:
1.18      jfb      2886:        return (0);
1.1       jfb      2887: }
1.97      niallo   2888:
                   2889: /*
                   2890:  * rcs_state_get()
                   2891:  *
                   2892:  * Get the state for a given revision of a specified RCSFILE.
                   2893:  *
                   2894:  * Returns NULL on failure.
                   2895:  */
                   2896: const char *
                   2897: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2898: {
                   2899:        struct rcs_delta *rdp;
                   2900:
                   2901:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2902:                return (NULL);
                   2903:
                   2904:        return (rdp->rd_state);
                   2905: }
                   2906:
1.131     niallo   2907: /*
                   2908:  * rcs_kwexp_buf()
                   2909:  *
                   2910:  * Do keyword expansion on a buffer if necessary
                   2911:  *
                   2912:  */
                   2913: BUF *
                   2914: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2915: {
                   2916:        struct rcs_delta *rdp;
                   2917:        char *expanded, *tbuf;
                   2918:        int expmode;
                   2919:        size_t len;
                   2920:
                   2921:        /*
                   2922:         * Do keyword expansion if required.
                   2923:         */
                   2924:        if (rf->rf_expand != NULL)
                   2925:                expmode = rcs_kwexp_get(rf);
                   2926:        else
                   2927:                expmode = RCS_KWEXP_DEFAULT;
                   2928:
                   2929:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2930:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.172     joris    2931:                        fatal("could not fetch revision");
1.131     niallo   2932:                cvs_buf_putc(bp, '\0');
                   2933:                len = cvs_buf_len(bp);
                   2934:                tbuf = cvs_buf_release(bp);
                   2935:                expanded = rcs_expand_keywords(rf->rf_path, rdp,
                   2936:                    tbuf, len, expmode);
                   2937:                bp = cvs_buf_alloc(len, BUF_AUTOEXT);
1.138     deraadt  2938:                cvs_buf_set(bp, expanded, strlen(expanded), 0);
1.131     niallo   2939:                xfree(expanded);
                   2940:        }
                   2941:        return (bp);
1.176     joris    2942: }
                   2943:
                   2944: RCSNUM *
                   2945: rcs_translate_tag(const char *revstr, RCSFILE *rfp)
                   2946: {
1.178     joris    2947:        size_t i;
1.180   ! joris    2948:        int nextroot;
1.178     joris    2949:        RCSNUM *rev, *brev;
                   2950:        struct rcs_branch *brp;
                   2951:        struct rcs_delta *rdp, *brdp;
1.180   ! joris    2952:        char revision[16];
1.176     joris    2953:
                   2954:        rev = rcs_sym_getrev(rfp, revstr);
                   2955:        if (rev == NULL) {
                   2956:                if ((rev = rcsnum_parse(revstr)) == NULL)
1.180   ! joris    2957:                        fatal("tag %s does not exist (0)", revstr);
1.178     joris    2958:        }
                   2959:
                   2960:        if (RCSNUM_ISBRANCH(rev)) {
                   2961:                brev = rcsnum_alloc();
1.180   ! joris    2962:                rcsnum_cpy(rev, brev, rev->rn_len - 1);
        !          2963:        } else {
        !          2964:                brev = rev;
        !          2965:        }
1.178     joris    2966:
1.180   ! joris    2967:        if ((rdp = rcs_findrev(rfp, brev)) == NULL)
        !          2968:                fatal("tag %s does not exist (1)", revstr);
1.178     joris    2969:
1.180   ! joris    2970:        if (RCSNUM_ISBRANCH(rev)) {
1.178     joris    2971:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                   2972:                        for (i = 0; i < rev->rn_len; i++) {
1.180   ! joris    2973:                                if (brp->rb_num->rn_id[i] != rev->rn_id[i])
        !          2974:                                        break;
1.178     joris    2975:                        }
                   2976:
1.180   ! joris    2977:                        if (i != rev->rn_len)
        !          2978:                                continue;
        !          2979:
1.178     joris    2980:                        break;
                   2981:                }
                   2982:
1.180   ! joris    2983:                if (brp == NULL)
        !          2984:                        return (NULL);
1.178     joris    2985:
1.180   ! joris    2986:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
        !          2987:                        fatal("tag %s does not exist (3)", revstr);
1.178     joris    2988:
1.180   ! joris    2989:                while (rdp->rd_next->rn_len != 0) {
        !          2990:                        if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
        !          2991:                                fatal("tag %s does not exist (4)", revstr);
1.178     joris    2992:                }
                   2993:
1.180   ! joris    2994:                rcsnum_cpy(rdp->rd_num, rev, 0);
1.176     joris    2995:        }
                   2996:
                   2997:        return (rev);
1.131     niallo   2998: }