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

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