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

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