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

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