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

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