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

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