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

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