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

1.67    ! niallo      1: /*     $OpenBSD: rcs.c,v 1.66 2005/09/29 15:29:20 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*
1.66      joris    1217: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1       jfb      1218: {
1.63      joris    1219:        int expmode, res;
1.1       jfb      1220:        size_t len;
                   1221:        void *bp;
1.66      joris    1222:        RCSNUM *crev, *rev;
1.65      niallo   1223:        BUF *rbuf, *dbuf = NULL;
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);
1.66      joris    1231:
                   1232:        if (frev == RCS_HEAD_REV)
                   1233:                rev = rfp->rf_head;
                   1234:        else
                   1235:                rev = frev;
1.28      jfb      1236:
1.1       jfb      1237:        res = rcsnum_cmp(rfp->rf_head, rev, 0);
                   1238:        if (res == 1) {
1.32      jfb      1239:                rcs_errno = RCS_ERR_NOENT;
1.1       jfb      1240:                return (NULL);
1.26      jfb      1241:        }
                   1242:
                   1243:        rdp = rcs_findrev(rfp, rfp->rf_head);
                   1244:        if (rdp == NULL) {
                   1245:                cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                   1246:                return (NULL);
                   1247:        }
                   1248:
1.45      jfb      1249:        len = rdp->rd_tlen;
1.26      jfb      1250:        if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
                   1251:                return (NULL);
                   1252:
                   1253:        cvs_buf_append(rbuf, rdp->rd_text, len);
                   1254:
                   1255:        if (res != 0) {
                   1256:                /* Apply patches backwards to get the right version.
                   1257:                 * This will need some rework to support sub branches.
                   1258:                 */
                   1259:                if ((crev = rcsnum_alloc()) == NULL) {
                   1260:                        cvs_buf_free(rbuf);
1.1       jfb      1261:                        return (NULL);
                   1262:                }
1.26      jfb      1263:                rcsnum_cpy(rfp->rf_head, crev, 0);
                   1264:                do {
                   1265:                        crev->rn_id[crev->rn_len - 1]--;
                   1266:                        rdp = rcs_findrev(rfp, crev);
                   1267:                        if (rdp == NULL) {
                   1268:                                rcsnum_free(crev);
                   1269:                                cvs_buf_free(rbuf);
                   1270:                                return (NULL);
                   1271:                        }
1.1       jfb      1272:
1.26      jfb      1273:                        if (cvs_buf_putc(rbuf, '\0') < 0) {
                   1274:                                rcsnum_free(crev);
1.17      jfb      1275:                                cvs_buf_free(rbuf);
1.11      joris    1276:                                return (NULL);
1.17      jfb      1277:                        }
1.26      jfb      1278:                        bp = cvs_buf_release(rbuf);
1.45      jfb      1279:                        rbuf = rcs_patch((char *)bp, (char *)rdp->rd_text);
1.62      joris    1280:                        free(bp);
1.26      jfb      1281:                        if (rbuf == NULL)
                   1282:                                break;
                   1283:                } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1       jfb      1284:
1.26      jfb      1285:                rcsnum_free(crev);
1.1       jfb      1286:        }
                   1287:
1.63      joris    1288:        /*
                   1289:         * Do keyword expansion if required.
                   1290:         */
                   1291:        if (rfp->rf_expand != NULL)
                   1292:                expmode = rcs_kwexp_get(rfp);
                   1293:        else
                   1294:                expmode = RCS_KWEXP_DEFAULT;
                   1295:
                   1296:        if ((rbuf != NULL) && !(expmode & RCS_KWEXP_NONE)) {
                   1297:                if ((dbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
                   1298:                        return (rbuf);
                   1299:                if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1300:                        return (rbuf);
                   1301:
                   1302:                if (cvs_buf_putc(rbuf, '\0') < 0) {
                   1303:                        cvs_buf_free(dbuf);
                   1304:                        return (rbuf);
                   1305:                }
                   1306:
                   1307:                bp = cvs_buf_release(rbuf);
                   1308:                if ((lines = rcs_splitlines((char *)bp)) != NULL) {
                   1309:                        res = 0;
                   1310:                        TAILQ_FOREACH(lp, &lines->rl_lines, rl_list) {
                   1311:                                if (res++ == 0)
                   1312:                                        continue;
                   1313:                                rcs_expand_keywords(rfp->rf_path, rdp,
                   1314:                                    lp->rl_line, out, sizeof(out), expmode);
                   1315:                                cvs_buf_fappend(dbuf, "%s\n", out);
                   1316:                        }
                   1317:                        rcs_freefoo(lines);
                   1318:                }
                   1319:                free(bp);
                   1320:        }
                   1321:
                   1322:        return (dbuf);
1.1       jfb      1323: }
                   1324:
                   1325: /*
1.52      jfb      1326:  * rcs_rev_add()
                   1327:  *
1.53      jfb      1328:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1329:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1330:  * new revision will have a number equal to the previous head revision plus
                   1331:  * one).  The <msg> argument specifies the log message for that revision, and
                   1332:  * <date> specifies the revision's date (a value of -1 is
                   1333:  * equivalent to using the current time).
1.52      jfb      1334:  * Returns 0 on success, or -1 on failure.
                   1335:  */
                   1336: int
1.53      jfb      1337: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date)
1.52      jfb      1338: {
                   1339:        time_t now;
                   1340:        struct passwd *pw;
                   1341:        struct rcs_delta *rdp;
1.67    ! niallo   1342:        RCSNUM *tmprev = NULL;
1.52      jfb      1343:
                   1344:        if (rev == RCS_HEAD_REV) {
1.67    ! niallo   1345:                const RCSNUM *head_rev;
        !          1346:                char version_str[10];
        !          1347:
        !          1348:                head_rev = rcs_head_get(rf);
        !          1349:                if ((tmprev = rcsnum_alloc()) == NULL) {
        !          1350:                        cvs_log(LP_ERR, "could not allocate rcsnum");
        !          1351:                        return (-1);
        !          1352:                }
        !          1353:                if (rcsnum_cpy(head_rev, tmprev, sizeof(version_str)) != 0) {
        !          1354:                        cvs_log(LP_ERR, "could not perform rcsnum_cpy");
        !          1355:                        rcsnum_free(tmprev);
        !          1356:                        return (-1);
        !          1357:                }
        !          1358:                rev = rcsnum_inc(tmprev);
1.52      jfb      1359:        } else if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1360:                rcs_errno = RCS_ERR_DUPENT;
                   1361:                return (-1);
                   1362:        }
                   1363:
                   1364:        if ((pw = getpwuid(getuid())) == NULL) {
                   1365:                rcs_errno = RCS_ERR_ERRNO;
                   1366:                return (-1);
                   1367:        }
                   1368:
                   1369:        if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
                   1370:                rcs_errno = RCS_ERR_ERRNO;
                   1371:                return (-1);
                   1372:        }
                   1373:        memset(rdp, 0, sizeof(*rdp));
                   1374:
                   1375:        TAILQ_INIT(&(rdp->rd_branches));
                   1376:        TAILQ_INIT(&(rdp->rd_snodes));
                   1377:
                   1378:        if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
                   1379:                rcs_freedelta(rdp);
                   1380:                return (-1);
                   1381:        }
                   1382:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.67    ! niallo   1383:        if (tmprev != NULL)
        !          1384:                rcsnum_free(tmprev);
1.52      jfb      1385:
                   1386:        if ((rdp->rd_author = cvs_strdup(pw->pw_name)) == NULL) {
                   1387:                rcs_freedelta(rdp);
                   1388:                return (-1);
                   1389:        }
                   1390:
                   1391:        if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
                   1392:                rcs_freedelta(rdp);
                   1393:                return (-1);
                   1394:        }
                   1395:
                   1396:        if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
                   1397:                rcs_errno = RCS_ERR_ERRNO;
                   1398:                rcs_freedelta(rdp);
                   1399:                return (-1);
                   1400:        }
                   1401:
1.53      jfb      1402:        if (date != (time_t)(-1))
                   1403:                now = date;
                   1404:        else
                   1405:                time(&now);
1.52      jfb      1406:        gmtime_r(&now, &(rdp->rd_date));
                   1407:
                   1408:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1409:        rf->rf_ndelta++;
1.64      niallo   1410:        /* not synced anymore */
                   1411:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1412:
                   1413:        return (0);
                   1414: }
                   1415:
                   1416: /*
                   1417:  * rcs_rev_remove()
                   1418:  *
                   1419:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1420:  */
                   1421: int
                   1422: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1423: {
                   1424:        int ret;
                   1425:        struct rcs_delta *rdp;
                   1426:
                   1427:        ret = 0;
                   1428:        if (rev == RCS_HEAD_REV)
                   1429:                rev = rf->rf_head;
                   1430:
                   1431:        /* do we actually have that revision? */
                   1432:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1433:                rcs_errno = RCS_ERR_NOENT;
                   1434:                ret = -1;
                   1435:        } else {
                   1436:                /* XXX assumes it's not a sub node */
                   1437:                TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1438:                rf->rf_ndelta--;
                   1439:                rf->rf_flags &= ~RCS_SYNCED;
                   1440:        }
                   1441:
                   1442:        return (ret);
                   1443:
                   1444: }
                   1445:
                   1446: /*
1.1       jfb      1447:  * rcs_findrev()
                   1448:  *
                   1449:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1450:  * The revision number is given in <rev>.
                   1451:  * Returns a pointer to the delta on success, or NULL on failure.
                   1452:  */
                   1453: static struct rcs_delta*
1.43      jfb      1454: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1       jfb      1455: {
                   1456:        u_int cmplen;
                   1457:        struct rcs_delta *rdp;
                   1458:        struct rcs_dlist *hp;
1.6       vincent  1459:        int found;
1.26      jfb      1460:
1.1       jfb      1461:        cmplen = 2;
                   1462:        hp = &(rfp->rf_delta);
                   1463:
1.6       vincent  1464:        do {
                   1465:                found = 0;
                   1466:                TAILQ_FOREACH(rdp, hp, rd_list) {
                   1467:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                   1468:                                if (cmplen == rev->rn_len)
                   1469:                                        return (rdp);
1.1       jfb      1470:
1.6       vincent  1471:                                hp = &(rdp->rd_snodes);
                   1472:                                cmplen += 2;
                   1473:                                found = 1;
                   1474:                                break;
                   1475:                        }
1.1       jfb      1476:                }
1.6       vincent  1477:        } while (found && cmplen < rev->rn_len);
1.1       jfb      1478:
                   1479:        return (NULL);
1.20      jfb      1480: }
                   1481:
                   1482: /*
1.26      jfb      1483:  * rcs_kwexp_set()
                   1484:  *
                   1485:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1486:  * Returns 0 on success, or -1 on failure.
                   1487:  */
                   1488: int
                   1489: rcs_kwexp_set(RCSFILE *file, int mode)
                   1490: {
                   1491:        int i;
                   1492:        char *tmp, buf[8] = "";
                   1493:
                   1494:        if (RCS_KWEXP_INVAL(mode))
                   1495:                return (-1);
                   1496:
                   1497:        i = 0;
                   1498:        if (mode == RCS_KWEXP_NONE)
                   1499:                buf[0] = 'b';
                   1500:        else if (mode == RCS_KWEXP_OLD)
                   1501:                buf[0] = 'o';
                   1502:        else {
                   1503:                if (mode & RCS_KWEXP_NAME)
                   1504:                        buf[i++] = 'k';
                   1505:                if (mode & RCS_KWEXP_VAL)
                   1506:                        buf[i++] = 'v';
                   1507:                if (mode & RCS_KWEXP_LKR)
                   1508:                        buf[i++] = 'l';
                   1509:        }
                   1510:
1.39      joris    1511:        if ((tmp = cvs_strdup(buf)) == NULL) {
1.26      jfb      1512:                cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
                   1513:                    file->rf_path);
                   1514:                return (-1);
                   1515:        }
                   1516:
1.27      jfb      1517:        if (file->rf_expand != NULL)
1.39      joris    1518:                cvs_strfree(file->rf_expand);
1.26      jfb      1519:        file->rf_expand = tmp;
1.64      niallo   1520:        /* not synced anymore */
                   1521:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1522:
                   1523:        return (0);
                   1524: }
                   1525:
                   1526: /*
                   1527:  * rcs_kwexp_get()
                   1528:  *
                   1529:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1530:  */
                   1531: int
                   1532: rcs_kwexp_get(RCSFILE *file)
                   1533: {
                   1534:        return rcs_kflag_get(file->rf_expand);
                   1535: }
                   1536:
                   1537: /*
1.20      jfb      1538:  * rcs_kflag_get()
                   1539:  *
                   1540:  * Get the keyword expansion mode from a set of character flags given in
                   1541:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1542:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1543:  */
                   1544: int
                   1545: rcs_kflag_get(const char *flags)
                   1546: {
                   1547:        int fl;
                   1548:        size_t len;
                   1549:        const char *fp;
                   1550:
                   1551:        fl = 0;
                   1552:        len = strlen(flags);
                   1553:
                   1554:        for (fp = flags; *fp != '\0'; fp++) {
                   1555:                if (*fp == 'k')
                   1556:                        fl |= RCS_KWEXP_NAME;
                   1557:                else if (*fp == 'v')
                   1558:                        fl |= RCS_KWEXP_VAL;
                   1559:                else if (*fp == 'l')
                   1560:                        fl |= RCS_KWEXP_LKR;
                   1561:                else if (*fp == 'o') {
                   1562:                        if (len != 1)
                   1563:                                fl |= RCS_KWEXP_ERR;
                   1564:                        fl |= RCS_KWEXP_OLD;
                   1565:                } else if (*fp == 'b') {
                   1566:                        if (len != 1)
                   1567:                                fl |= RCS_KWEXP_ERR;
                   1568:                } else  /* unknown letter */
                   1569:                        fl |= RCS_KWEXP_ERR;
                   1570:        }
                   1571:
                   1572:        return (fl);
1.32      jfb      1573: }
                   1574:
                   1575: /*
                   1576:  * rcs_errstr()
                   1577:  *
                   1578:  * Get the error string matching the RCS error code <code>.
                   1579:  */
1.57      xsa      1580: const char *
1.32      jfb      1581: rcs_errstr(int code)
                   1582: {
1.50      jfb      1583:        const char *esp;
                   1584:
                   1585:        if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
                   1586:                esp = NULL;
                   1587:        else if (code == RCS_ERR_ERRNO)
                   1588:                esp = strerror(errno);
                   1589:        else
                   1590:                esp = rcs_errstrs[code];
                   1591:        return (esp);
1.1       jfb      1592: }
                   1593:
1.21      jfb      1594: void
                   1595: rcs_kflag_usage(void)
                   1596: {
                   1597:        fprintf(stderr, "Valid expansion modes include:\n"
1.22      jfb      1598:            "\t-kkv\tGenerate keywords using the default form.\n"
                   1599:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                   1600:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                   1601:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                   1602:            "\t-ko\tGenerate old keyword string "
1.21      jfb      1603:            "(no changes from checked in file).\n"
1.22      jfb      1604:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21      jfb      1605: }
1.1       jfb      1606:
                   1607: /*
                   1608:  * rcs_parse()
                   1609:  *
                   1610:  * Parse the contents of file <path>, which are in the RCS format.
                   1611:  * Returns 0 on success, or -1 on failure.
                   1612:  */
1.26      jfb      1613: static int
1.1       jfb      1614: rcs_parse(RCSFILE *rfp)
                   1615: {
                   1616:        int ret;
                   1617:        struct rcs_pdata *pdp;
                   1618:
1.26      jfb      1619:        if (rfp->rf_flags & RCS_PARSED)
1.1       jfb      1620:                return (0);
                   1621:
1.26      jfb      1622:        if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50      jfb      1623:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      1624:                cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
                   1625:                return (-1);
                   1626:        }
                   1627:        memset(pdp, 0, sizeof(*pdp));
                   1628:
1.18      jfb      1629:        pdp->rp_lines = 0;
1.1       jfb      1630:        pdp->rp_pttype = RCS_TOK_ERR;
                   1631:
                   1632:        pdp->rp_file = fopen(rfp->rf_path, "r");
                   1633:        if (pdp->rp_file == NULL) {
1.50      jfb      1634:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      1635:                cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
                   1636:                rcs_freepdata(pdp);
                   1637:                return (-1);
                   1638:        }
                   1639:
1.59      xsa      1640:        pdp->rp_buf = (char *)malloc((size_t)RCS_BUFSIZE);
1.1       jfb      1641:        if (pdp->rp_buf == NULL) {
1.50      jfb      1642:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      1643:                cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
                   1644:                rcs_freepdata(pdp);
                   1645:                return (-1);
                   1646:        }
                   1647:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1648:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1649:
                   1650:        /* ditch the strict lock */
1.26      jfb      1651:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1652:        rfp->rf_pdata = pdp;
                   1653:
1.31      jfb      1654:        if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1       jfb      1655:                rcs_freepdata(pdp);
                   1656:                return (-1);
1.31      jfb      1657:        } else if (ret == RCS_TOK_NUM) {
                   1658:                for (;;) {
                   1659:                        ret = rcs_parse_delta(rfp);
                   1660:                        if (ret == 0)
                   1661:                                break;
                   1662:                        else if (ret == -1) {
                   1663:                                rcs_freepdata(pdp);
                   1664:                                return (-1);
                   1665:                        }
1.1       jfb      1666:                }
                   1667:        }
                   1668:
                   1669:        ret = rcs_gettok(rfp);
                   1670:        if (ret != RCS_TOK_DESC) {
1.50      jfb      1671:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1672:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1673:                    RCS_TOKSTR(rfp));
                   1674:                rcs_freepdata(pdp);
                   1675:                return (-1);
                   1676:        }
                   1677:
                   1678:        ret = rcs_gettok(rfp);
                   1679:        if (ret != RCS_TOK_STRING) {
1.50      jfb      1680:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1681:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1682:                    RCS_TOKSTR(rfp));
                   1683:                rcs_freepdata(pdp);
                   1684:                return (-1);
                   1685:        }
                   1686:
1.39      joris    1687:        rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10      joris    1688:        if (rfp->rf_desc == NULL) {
                   1689:                cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1690:                rcs_freepdata(pdp);
                   1691:                return (-1);
                   1692:        }
1.1       jfb      1693:
                   1694:        for (;;) {
                   1695:                ret = rcs_parse_deltatext(rfp);
                   1696:                if (ret == 0)
                   1697:                        break;
                   1698:                else if (ret == -1) {
                   1699:                        rcs_freepdata(pdp);
                   1700:                        return (-1);
                   1701:                }
                   1702:        }
                   1703:
                   1704:        rcs_freepdata(pdp);
                   1705:
                   1706:        rfp->rf_pdata = NULL;
1.26      jfb      1707:        rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1       jfb      1708:
                   1709:        return (0);
                   1710: }
                   1711:
                   1712: /*
                   1713:  * rcs_parse_admin()
                   1714:  *
                   1715:  * Parse the administrative portion of an RCS file.
1.31      jfb      1716:  * Returns the type of the first token found after the admin section on
                   1717:  * success, or -1 on failure.
1.1       jfb      1718:  */
                   1719: static int
                   1720: rcs_parse_admin(RCSFILE *rfp)
                   1721: {
                   1722:        u_int i;
                   1723:        int tok, ntok, hmask;
                   1724:        struct rcs_key *rk;
                   1725:
                   1726:        /* hmask is a mask of the headers already encountered */
                   1727:        hmask = 0;
                   1728:        for (;;) {
                   1729:                tok = rcs_gettok(rfp);
                   1730:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1731:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1732:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                   1733:                        return (-1);
1.31      jfb      1734:                } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
                   1735:                        /*
                   1736:                         * Assume this is the start of the first delta or
                   1737:                         * that we are dealing with an empty RCS file and
                   1738:                         * we just found the description.
                   1739:                         */
1.1       jfb      1740:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31      jfb      1741:                        return (tok);
1.1       jfb      1742:                }
                   1743:
                   1744:                rk = NULL;
1.18      jfb      1745:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1746:                        if (rcs_keys[i].rk_id == tok)
                   1747:                                rk = &(rcs_keys[i]);
                   1748:
                   1749:                if (hmask & (1 << tok)) {
1.50      jfb      1750:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1751:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1752:                        return (-1);
                   1753:                }
                   1754:                hmask |= (1 << tok);
                   1755:
                   1756:                switch (tok) {
                   1757:                case RCS_TOK_HEAD:
                   1758:                case RCS_TOK_BRANCH:
                   1759:                case RCS_TOK_COMMENT:
                   1760:                case RCS_TOK_EXPAND:
                   1761:                        ntok = rcs_gettok(rfp);
                   1762:                        if (ntok == RCS_TOK_SCOLON)
                   1763:                                break;
                   1764:                        if (ntok != rk->rk_val) {
1.50      jfb      1765:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1766:                                cvs_log(LP_ERR,
                   1767:                                    "invalid value type for RCS key `%s'",
                   1768:                                    rk->rk_str);
                   1769:                        }
                   1770:
                   1771:                        if (tok == RCS_TOK_HEAD) {
1.28      jfb      1772:                                if (rfp->rf_head == NULL) {
                   1773:                                        rfp->rf_head = rcsnum_alloc();
                   1774:                                        if (rfp->rf_head == NULL)
                   1775:                                                return (-1);
                   1776:                                }
1.1       jfb      1777:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1778:                                    rfp->rf_head);
1.14      deraadt  1779:                        } else if (tok == RCS_TOK_BRANCH) {
1.35      jfb      1780:                                if (rfp->rf_branch == NULL) {
                   1781:                                        rfp->rf_branch = rcsnum_alloc();
                   1782:                                        if (rfp->rf_branch == NULL)
                   1783:                                                return (-1);
                   1784:                                }
                   1785:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1786:                                    rfp->rf_branch) < 0)
                   1787:                                        return (-1);
1.14      deraadt  1788:                        } else if (tok == RCS_TOK_COMMENT) {
1.39      joris    1789:                                rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10      joris    1790:                                if (rfp->rf_comment == NULL) {
                   1791:                                        cvs_log(LP_ERRNO,
                   1792:                                            "failed to duplicate rcs token");
                   1793:                                        return (-1);
                   1794:                                }
1.14      deraadt  1795:                        } else if (tok == RCS_TOK_EXPAND) {
1.39      joris    1796:                                rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10      joris    1797:                                if (rfp->rf_expand == NULL) {
                   1798:                                        cvs_log(LP_ERRNO,
                   1799:                                            "failed to duplicate rcs token");
                   1800:                                        return (-1);
                   1801:                                }
1.1       jfb      1802:                        }
                   1803:
                   1804:                        /* now get the expected semi-colon */
                   1805:                        ntok = rcs_gettok(rfp);
                   1806:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1807:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1808:                                cvs_log(LP_ERR,
                   1809:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1810:                                    rk->rk_str);
1.1       jfb      1811:                                return (-1);
                   1812:                        }
                   1813:                        break;
                   1814:                case RCS_TOK_ACCESS:
1.29      jfb      1815:                        if (rcs_parse_access(rfp) < 0)
                   1816:                                return (-1);
1.1       jfb      1817:                        break;
                   1818:                case RCS_TOK_SYMBOLS:
1.29      jfb      1819:                        if (rcs_parse_symbols(rfp) < 0)
                   1820:                                return (-1);
1.1       jfb      1821:                        break;
                   1822:                case RCS_TOK_LOCKS:
1.29      jfb      1823:                        if (rcs_parse_locks(rfp) < 0)
                   1824:                                return (-1);
1.1       jfb      1825:                        break;
                   1826:                default:
1.50      jfb      1827:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1828:                        cvs_log(LP_ERR,
                   1829:                            "unexpected token `%s' in RCS admin section",
                   1830:                            RCS_TOKSTR(rfp));
                   1831:                        return (-1);
                   1832:                }
                   1833:        }
                   1834:
                   1835:        return (0);
                   1836: }
                   1837:
                   1838: /*
                   1839:  * rcs_parse_delta()
                   1840:  *
                   1841:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1842:  * information in the <rfp> delta list.
                   1843:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1844:  * -1 on error.
                   1845:  */
                   1846: static int
                   1847: rcs_parse_delta(RCSFILE *rfp)
                   1848: {
                   1849:        int ret, tok, ntok, hmask;
                   1850:        u_int i;
                   1851:        char *tokstr;
1.3       vincent  1852:        RCSNUM *datenum;
1.1       jfb      1853:        struct rcs_delta *rdp;
                   1854:        struct rcs_key *rk;
                   1855:
                   1856:        rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
                   1857:        if (rdp == NULL) {
1.50      jfb      1858:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      1859:                cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
                   1860:                return (-1);
                   1861:        }
                   1862:        memset(rdp, 0, sizeof(*rdp));
                   1863:
                   1864:        rdp->rd_num = rcsnum_alloc();
1.11      joris    1865:        if (rdp->rd_num == NULL) {
                   1866:                rcs_freedelta(rdp);
                   1867:                return (-1);
                   1868:        }
1.1       jfb      1869:        rdp->rd_next = rcsnum_alloc();
1.11      joris    1870:        if (rdp->rd_next == NULL) {
                   1871:                rcs_freedelta(rdp);
                   1872:                return (-1);
                   1873:        }
1.1       jfb      1874:
                   1875:        TAILQ_INIT(&(rdp->rd_branches));
1.52      jfb      1876:        TAILQ_INIT(&(rdp->rd_snodes));
1.1       jfb      1877:
                   1878:        tok = rcs_gettok(rfp);
                   1879:        if (tok != RCS_TOK_NUM) {
1.52      jfb      1880:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1881:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1882:                    RCS_TOKSTR(rfp));
                   1883:                rcs_freedelta(rdp);
                   1884:                return (-1);
                   1885:        }
                   1886:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1887:
                   1888:        hmask = 0;
                   1889:        ret = 0;
                   1890:        tokstr = NULL;
                   1891:
                   1892:        for (;;) {
                   1893:                tok = rcs_gettok(rfp);
                   1894:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1895:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1896:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1897:                        rcs_freedelta(rdp);
                   1898:                        return (-1);
1.14      deraadt  1899:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1900:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1901:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1902:                        break;
                   1903:                }
                   1904:
                   1905:                rk = NULL;
1.18      jfb      1906:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1907:                        if (rcs_keys[i].rk_id == tok)
                   1908:                                rk = &(rcs_keys[i]);
                   1909:
                   1910:                if (hmask & (1 << tok)) {
1.50      jfb      1911:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1912:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1913:                        rcs_freedelta(rdp);
                   1914:                        return (-1);
                   1915:                }
                   1916:                hmask |= (1 << tok);
                   1917:
                   1918:                switch (tok) {
                   1919:                case RCS_TOK_DATE:
                   1920:                case RCS_TOK_AUTHOR:
                   1921:                case RCS_TOK_STATE:
                   1922:                case RCS_TOK_NEXT:
                   1923:                        ntok = rcs_gettok(rfp);
                   1924:                        if (ntok == RCS_TOK_SCOLON) {
                   1925:                                if (rk->rk_flags & RCS_VOPT)
                   1926:                                        break;
                   1927:                                else {
1.50      jfb      1928:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1929:                                        cvs_log(LP_ERR, "missing mandatory "
                   1930:                                            "value to RCS key `%s'",
                   1931:                                            rk->rk_str);
                   1932:                                        rcs_freedelta(rdp);
                   1933:                                        return (-1);
                   1934:                                }
                   1935:                        }
                   1936:
                   1937:                        if (ntok != rk->rk_val) {
1.50      jfb      1938:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1939:                                cvs_log(LP_ERR,
                   1940:                                    "invalid value type for RCS key `%s'",
                   1941:                                    rk->rk_str);
                   1942:                                rcs_freedelta(rdp);
                   1943:                                return (-1);
                   1944:                        }
                   1945:
                   1946:                        if (tokstr != NULL)
1.41      jfb      1947:                                cvs_strfree(tokstr);
1.39      joris    1948:                        tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10      joris    1949:                        if (tokstr == NULL) {
1.15      tedu     1950:                                cvs_log(LP_ERRNO,
1.10      joris    1951:                                    "failed to duplicate rcs token");
                   1952:                                rcs_freedelta(rdp);
                   1953:                                return (-1);
                   1954:                        }
1.1       jfb      1955:
                   1956:                        /* now get the expected semi-colon */
                   1957:                        ntok = rcs_gettok(rfp);
                   1958:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1959:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1960:                                cvs_log(LP_ERR,
                   1961:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1962:                                    rk->rk_str);
1.41      jfb      1963:                                cvs_strfree(tokstr);
1.1       jfb      1964:                                rcs_freedelta(rdp);
                   1965:                                return (-1);
                   1966:                        }
                   1967:
                   1968:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1969:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41      jfb      1970:                                        cvs_strfree(tokstr);
1.11      joris    1971:                                        rcs_freedelta(rdp);
                   1972:                                        return (-1);
                   1973:                                }
1.3       vincent  1974:                                if (datenum->rn_len != 6) {
1.50      jfb      1975:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1976:                                        cvs_log(LP_ERR,
                   1977:                                            "RCS date specification has %s "
                   1978:                                            "fields",
1.3       vincent  1979:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1980:                                            "missing");
1.41      jfb      1981:                                        cvs_strfree(tokstr);
1.1       jfb      1982:                                        rcs_freedelta(rdp);
1.37      tedu     1983:                                        rcsnum_free(datenum);
                   1984:                                        return (-1);
1.1       jfb      1985:                                }
1.3       vincent  1986:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1987:                                if (rdp->rd_date.tm_year >= 1900)
                   1988:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1989:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1990:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1991:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1992:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1993:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1994:                                rcsnum_free(datenum);
1.14      deraadt  1995:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1996:                                rdp->rd_author = tokstr;
                   1997:                                tokstr = NULL;
1.14      deraadt  1998:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1999:                                rdp->rd_state = tokstr;
                   2000:                                tokstr = NULL;
1.14      deraadt  2001:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      2002:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   2003:                        }
                   2004:                        break;
                   2005:                case RCS_TOK_BRANCHES:
1.46      jfb      2006:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   2007:                                rcs_freedelta(rdp);
                   2008:                                return (-1);
                   2009:                        }
1.1       jfb      2010:                        break;
                   2011:                default:
1.50      jfb      2012:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2013:                        cvs_log(LP_ERR,
                   2014:                            "unexpected token `%s' in RCS delta",
                   2015:                            RCS_TOKSTR(rfp));
                   2016:                        rcs_freedelta(rdp);
                   2017:                        return (-1);
                   2018:                }
                   2019:        }
                   2020:
1.13      jfb      2021:        if (tokstr != NULL)
1.39      joris    2022:                cvs_strfree(tokstr);
1.13      jfb      2023:
1.1       jfb      2024:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      2025:        rfp->rf_ndelta++;
1.1       jfb      2026:
                   2027:        return (ret);
                   2028: }
                   2029:
                   2030: /*
                   2031:  * rcs_parse_deltatext()
                   2032:  *
                   2033:  * Parse an RCS delta text section and fill in the log and text field of the
                   2034:  * appropriate delta section.
                   2035:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   2036:  * -1 on error.
                   2037:  */
                   2038: static int
                   2039: rcs_parse_deltatext(RCSFILE *rfp)
                   2040: {
                   2041:        int tok;
                   2042:        RCSNUM *tnum;
                   2043:        struct rcs_delta *rdp;
                   2044:
                   2045:        tok = rcs_gettok(rfp);
                   2046:        if (tok == RCS_TOK_EOF)
                   2047:                return (0);
                   2048:
                   2049:        if (tok != RCS_TOK_NUM) {
1.50      jfb      2050:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2051:                cvs_log(LP_ERR,
                   2052:                    "unexpected token `%s' at start of RCS delta text",
                   2053:                    RCS_TOKSTR(rfp));
                   2054:                return (-1);
                   2055:        }
1.13      jfb      2056:
                   2057:        tnum = rcsnum_alloc();
                   2058:        if (tnum == NULL)
                   2059:                return (-1);
1.1       jfb      2060:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   2061:
                   2062:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2063:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   2064:                        break;
                   2065:        }
1.13      jfb      2066:        rcsnum_free(tnum);
                   2067:
1.1       jfb      2068:        if (rdp == NULL) {
                   2069:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   2070:                    RCS_TOKSTR(rfp));
                   2071:                return (-1);
                   2072:        }
                   2073:
                   2074:        tok = rcs_gettok(rfp);
                   2075:        if (tok != RCS_TOK_LOG) {
1.50      jfb      2076:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2077:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2078:                    RCS_TOKSTR(rfp));
                   2079:                return (-1);
                   2080:        }
                   2081:
                   2082:        tok = rcs_gettok(rfp);
                   2083:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2084:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2085:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2086:                    RCS_TOKSTR(rfp));
                   2087:                return (-1);
                   2088:        }
1.39      joris    2089:        rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1       jfb      2090:        if (rdp->rd_log == NULL) {
                   2091:                cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
                   2092:                return (-1);
                   2093:        }
                   2094:
                   2095:        tok = rcs_gettok(rfp);
                   2096:        if (tok != RCS_TOK_TEXT) {
1.50      jfb      2097:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2098:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2099:                    RCS_TOKSTR(rfp));
                   2100:                return (-1);
                   2101:        }
                   2102:
                   2103:        tok = rcs_gettok(rfp);
                   2104:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2105:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2106:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2107:                    RCS_TOKSTR(rfp));
                   2108:                return (-1);
                   2109:        }
                   2110:
1.45      jfb      2111:        rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp));
1.1       jfb      2112:        if (rdp->rd_text == NULL) {
                   2113:                cvs_log(LP_ERRNO, "failed to copy RCS delta text");
                   2114:                return (-1);
                   2115:        }
1.45      jfb      2116:        memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1.42      jfb      2117:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2118:
                   2119:        return (1);
                   2120: }
                   2121:
                   2122: /*
                   2123:  * rcs_parse_access()
                   2124:  *
                   2125:  * Parse the access list given as value to the `access' keyword.
                   2126:  * Returns 0 on success, or -1 on failure.
                   2127:  */
                   2128: static int
                   2129: rcs_parse_access(RCSFILE *rfp)
                   2130: {
                   2131:        int type;
                   2132:
                   2133:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2134:                if (type != RCS_TOK_ID) {
1.50      jfb      2135:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2136:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2137:                            RCS_TOKSTR(rfp));
                   2138:                        return (-1);
                   2139:                }
1.29      jfb      2140:
                   2141:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2142:                        return (-1);
1.1       jfb      2143:        }
                   2144:
                   2145:        return (0);
                   2146: }
                   2147:
                   2148: /*
                   2149:  * rcs_parse_symbols()
                   2150:  *
                   2151:  * Parse the symbol list given as value to the `symbols' keyword.
                   2152:  * Returns 0 on success, or -1 on failure.
                   2153:  */
                   2154: static int
                   2155: rcs_parse_symbols(RCSFILE *rfp)
                   2156: {
                   2157:        int type;
                   2158:        struct rcs_sym *symp;
                   2159:
                   2160:        for (;;) {
                   2161:                type = rcs_gettok(rfp);
                   2162:                if (type == RCS_TOK_SCOLON)
                   2163:                        break;
                   2164:
1.41      jfb      2165:                if (type != RCS_TOK_ID) {
1.50      jfb      2166:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2167:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2168:                            RCS_TOKSTR(rfp));
                   2169:                        return (-1);
                   2170:                }
                   2171:
                   2172:                symp = (struct rcs_sym *)malloc(sizeof(*symp));
                   2173:                if (symp == NULL) {
1.50      jfb      2174:                        rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      2175:                        cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                   2176:                        return (-1);
                   2177:                }
1.39      joris    2178:                symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10      joris    2179:                if (symp->rs_name == NULL) {
                   2180:                        cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   2181:                        free(symp);
                   2182:                        return (-1);
                   2183:                }
                   2184:
1.1       jfb      2185:                symp->rs_num = rcsnum_alloc();
1.11      joris    2186:                if (symp->rs_num == NULL) {
                   2187:                        cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39      joris    2188:                        cvs_strfree(symp->rs_name);
1.11      joris    2189:                        free(symp);
                   2190:                        return (-1);
                   2191:                }
1.1       jfb      2192:
                   2193:                type = rcs_gettok(rfp);
                   2194:                if (type != RCS_TOK_COLON) {
1.50      jfb      2195:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2196:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2197:                            RCS_TOKSTR(rfp));
1.11      joris    2198:                        rcsnum_free(symp->rs_num);
1.39      joris    2199:                        cvs_strfree(symp->rs_name);
1.1       jfb      2200:                        free(symp);
                   2201:                        return (-1);
                   2202:                }
                   2203:
                   2204:                type = rcs_gettok(rfp);
                   2205:                if (type != RCS_TOK_NUM) {
1.50      jfb      2206:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2207:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2208:                            RCS_TOKSTR(rfp));
1.11      joris    2209:                        rcsnum_free(symp->rs_num);
1.39      joris    2210:                        cvs_strfree(symp->rs_name);
1.1       jfb      2211:                        free(symp);
                   2212:                        return (-1);
                   2213:                }
                   2214:
                   2215:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2216:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2217:                            RCS_TOKSTR(rfp));
1.11      joris    2218:                        rcsnum_free(symp->rs_num);
1.39      joris    2219:                        cvs_strfree(symp->rs_name);
1.1       jfb      2220:                        free(symp);
                   2221:                        return (-1);
                   2222:                }
                   2223:
1.43      jfb      2224:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2225:        }
                   2226:
                   2227:        return (0);
                   2228: }
                   2229:
                   2230: /*
                   2231:  * rcs_parse_locks()
                   2232:  *
                   2233:  * Parse the lock list given as value to the `locks' keyword.
                   2234:  * Returns 0 on success, or -1 on failure.
                   2235:  */
                   2236: static int
                   2237: rcs_parse_locks(RCSFILE *rfp)
                   2238: {
                   2239:        int type;
                   2240:        struct rcs_lock *lkp;
                   2241:
                   2242:        for (;;) {
                   2243:                type = rcs_gettok(rfp);
                   2244:                if (type == RCS_TOK_SCOLON)
                   2245:                        break;
                   2246:
                   2247:                if (type != RCS_TOK_ID) {
1.50      jfb      2248:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2249:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2250:                            RCS_TOKSTR(rfp));
                   2251:                        return (-1);
                   2252:                }
                   2253:
                   2254:                lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
                   2255:                if (lkp == NULL) {
                   2256:                        cvs_log(LP_ERRNO, "failed to allocate RCS lock");
                   2257:                        return (-1);
                   2258:                }
                   2259:                lkp->rl_num = rcsnum_alloc();
1.11      joris    2260:                if (lkp->rl_num == NULL) {
                   2261:                        free(lkp);
                   2262:                        return (-1);
                   2263:                }
1.1       jfb      2264:
                   2265:                type = rcs_gettok(rfp);
                   2266:                if (type != RCS_TOK_COLON) {
1.50      jfb      2267:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2268:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2269:                            RCS_TOKSTR(rfp));
1.37      tedu     2270:                        rcsnum_free(lkp->rl_num);
1.1       jfb      2271:                        free(lkp);
                   2272:                        return (-1);
                   2273:                }
                   2274:
                   2275:                type = rcs_gettok(rfp);
                   2276:                if (type != RCS_TOK_NUM) {
1.50      jfb      2277:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2278:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2279:                            RCS_TOKSTR(rfp));
1.37      tedu     2280:                        rcsnum_free(lkp->rl_num);
1.1       jfb      2281:                        free(lkp);
                   2282:                        return (-1);
                   2283:                }
                   2284:
                   2285:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2286:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2287:                            RCS_TOKSTR(rfp));
1.37      tedu     2288:                        rcsnum_free(lkp->rl_num);
1.1       jfb      2289:                        free(lkp);
                   2290:                        return (-1);
                   2291:                }
                   2292:
                   2293:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2294:        }
                   2295:
                   2296:        /* check if we have a `strict' */
                   2297:        type = rcs_gettok(rfp);
                   2298:        if (type != RCS_TOK_STRICT) {
                   2299:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2300:        } else {
1.26      jfb      2301:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2302:
                   2303:                type = rcs_gettok(rfp);
                   2304:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2305:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2306:                        cvs_log(LP_ERR,
                   2307:                            "missing semi-colon after `strict' keyword");
                   2308:                        return (-1);
                   2309:                }
                   2310:        }
                   2311:
                   2312:        return (0);
                   2313: }
                   2314:
                   2315: /*
                   2316:  * rcs_parse_branches()
                   2317:  *
                   2318:  * Parse the list of branches following a `branches' keyword in a delta.
                   2319:  * Returns 0 on success, or -1 on failure.
                   2320:  */
                   2321: static int
                   2322: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2323: {
                   2324:        int type;
                   2325:        struct rcs_branch *brp;
                   2326:
                   2327:        for (;;) {
                   2328:                type = rcs_gettok(rfp);
                   2329:                if (type == RCS_TOK_SCOLON)
                   2330:                        break;
                   2331:
                   2332:                if (type != RCS_TOK_NUM) {
1.50      jfb      2333:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2334:                        cvs_log(LP_ERR,
                   2335:                            "unexpected token `%s' in list of branches",
                   2336:                            RCS_TOKSTR(rfp));
                   2337:                        return (-1);
                   2338:                }
                   2339:
                   2340:                brp = (struct rcs_branch *)malloc(sizeof(*brp));
                   2341:                if (brp == NULL) {
1.50      jfb      2342:                        rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      2343:                        cvs_log(LP_ERRNO, "failed to allocate RCS branch");
                   2344:                        return (-1);
                   2345:                }
1.46      jfb      2346:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2347:                if (brp->rb_num == NULL) {
                   2348:                        free(brp);
                   2349:                        return (-1);
                   2350:                }
1.1       jfb      2351:
                   2352:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2353:        }
                   2354:
                   2355:        return (0);
                   2356: }
                   2357:
                   2358: /*
                   2359:  * rcs_freedelta()
                   2360:  *
                   2361:  * Free the contents of a delta structure.
                   2362:  */
1.18      jfb      2363: static void
1.1       jfb      2364: rcs_freedelta(struct rcs_delta *rdp)
                   2365: {
1.12      jfb      2366:        struct rcs_branch *rb;
1.1       jfb      2367:        struct rcs_delta *crdp;
                   2368:
1.12      jfb      2369:        if (rdp->rd_num != NULL)
                   2370:                rcsnum_free(rdp->rd_num);
                   2371:        if (rdp->rd_next != NULL)
                   2372:                rcsnum_free(rdp->rd_next);
                   2373:
1.1       jfb      2374:        if (rdp->rd_author != NULL)
1.39      joris    2375:                cvs_strfree(rdp->rd_author);
1.1       jfb      2376:        if (rdp->rd_state != NULL)
1.39      joris    2377:                cvs_strfree(rdp->rd_state);
1.1       jfb      2378:        if (rdp->rd_log != NULL)
1.39      joris    2379:                cvs_strfree(rdp->rd_log);
1.1       jfb      2380:        if (rdp->rd_text != NULL)
1.45      jfb      2381:                free(rdp->rd_text);
1.12      jfb      2382:
                   2383:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2384:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2385:                rcsnum_free(rb->rb_num);
                   2386:                free(rb);
                   2387:        }
1.1       jfb      2388:
                   2389:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   2390:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   2391:                rcs_freedelta(crdp);
                   2392:        }
                   2393:
                   2394:        free(rdp);
                   2395: }
                   2396:
                   2397: /*
                   2398:  * rcs_freepdata()
                   2399:  *
                   2400:  * Free the contents of the parser data structure.
                   2401:  */
                   2402: static void
                   2403: rcs_freepdata(struct rcs_pdata *pd)
                   2404: {
                   2405:        if (pd->rp_file != NULL)
                   2406:                (void)fclose(pd->rp_file);
                   2407:        if (pd->rp_buf != NULL)
                   2408:                free(pd->rp_buf);
                   2409:        free(pd);
                   2410: }
                   2411:
                   2412: /*
                   2413:  * rcs_gettok()
                   2414:  *
                   2415:  * Get the next RCS token from the string <str>.
                   2416:  */
                   2417: static int
                   2418: rcs_gettok(RCSFILE *rfp)
                   2419: {
                   2420:        u_int i;
                   2421:        int ch, last, type;
1.18      jfb      2422:        size_t len;
                   2423:        char *bp;
1.1       jfb      2424:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2425:
                   2426:        type = RCS_TOK_ERR;
                   2427:        bp = pdp->rp_buf;
1.42      jfb      2428:        pdp->rp_tlen = 0;
1.1       jfb      2429:        *bp = '\0';
                   2430:
                   2431:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2432:                type = pdp->rp_pttype;
                   2433:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   2434:                pdp->rp_pttype = RCS_TOK_ERR;
                   2435:                return (type);
                   2436:        }
                   2437:
                   2438:        /* skip leading whitespace */
                   2439:        /* XXX we must skip backspace too for compatibility, should we? */
                   2440:        do {
                   2441:                ch = getc(pdp->rp_file);
                   2442:                if (ch == '\n')
1.18      jfb      2443:                        pdp->rp_lines++;
1.1       jfb      2444:        } while (isspace(ch));
                   2445:
                   2446:        if (ch == EOF) {
                   2447:                type = RCS_TOK_EOF;
1.14      deraadt  2448:        } else if (ch == ';') {
1.1       jfb      2449:                type = RCS_TOK_SCOLON;
1.14      deraadt  2450:        } else if (ch == ':') {
1.1       jfb      2451:                type = RCS_TOK_COLON;
1.14      deraadt  2452:        } else if (isalpha(ch)) {
1.31      jfb      2453:                type = RCS_TOK_ID;
1.1       jfb      2454:                *(bp++) = ch;
1.18      jfb      2455:                for (;;) {
1.1       jfb      2456:                        ch = getc(pdp->rp_file);
1.11      joris    2457:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      2458:                                ungetc(ch, pdp->rp_file);
                   2459:                                break;
                   2460:                        }
                   2461:                        *(bp++) = ch;
1.42      jfb      2462:                        pdp->rp_tlen++;
1.18      jfb      2463:                        if (bp == pdp->rp_bufend - 1) {
                   2464:                                len = bp - pdp->rp_buf;
                   2465:                                if (rcs_growbuf(rfp) < 0) {
                   2466:                                        type = RCS_TOK_ERR;
                   2467:                                        break;
                   2468:                                }
                   2469:                                bp = pdp->rp_buf + len;
                   2470:                        }
1.1       jfb      2471:                }
                   2472:                *bp = '\0';
                   2473:
1.18      jfb      2474:                if (type != RCS_TOK_ERR) {
                   2475:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2476:                                if (strcmp(rcs_keys[i].rk_str,
                   2477:                                    pdp->rp_buf) == 0) {
                   2478:                                        type = rcs_keys[i].rk_id;
                   2479:                                        break;
                   2480:                                }
1.1       jfb      2481:                        }
                   2482:                }
1.14      deraadt  2483:        } else if (ch == '@') {
1.1       jfb      2484:                /* we have a string */
1.18      jfb      2485:                type = RCS_TOK_STRING;
1.1       jfb      2486:                for (;;) {
                   2487:                        ch = getc(pdp->rp_file);
                   2488:                        if (ch == '@') {
                   2489:                                ch = getc(pdp->rp_file);
                   2490:                                if (ch != '@') {
                   2491:                                        ungetc(ch, pdp->rp_file);
                   2492:                                        break;
                   2493:                                }
1.14      deraadt  2494:                        } else if (ch == '\n')
1.18      jfb      2495:                                pdp->rp_lines++;
1.1       jfb      2496:
                   2497:                        *(bp++) = ch;
1.42      jfb      2498:                        pdp->rp_tlen++;
1.18      jfb      2499:                        if (bp == pdp->rp_bufend - 1) {
                   2500:                                len = bp - pdp->rp_buf;
                   2501:                                if (rcs_growbuf(rfp) < 0) {
                   2502:                                        type = RCS_TOK_ERR;
                   2503:                                        break;
                   2504:                                }
                   2505:                                bp = pdp->rp_buf + len;
                   2506:                        }
1.1       jfb      2507:                }
                   2508:
                   2509:                *bp = '\0';
1.14      deraadt  2510:        } else if (isdigit(ch)) {
1.1       jfb      2511:                *(bp++) = ch;
                   2512:                last = ch;
                   2513:                type = RCS_TOK_NUM;
                   2514:
                   2515:                for (;;) {
                   2516:                        ch = getc(pdp->rp_file);
1.18      jfb      2517:                        if (bp == pdp->rp_bufend)
1.1       jfb      2518:                                break;
                   2519:                        if (!isdigit(ch) && ch != '.') {
                   2520:                                ungetc(ch, pdp->rp_file);
                   2521:                                break;
                   2522:                        }
                   2523:
                   2524:                        if (last == '.' && ch == '.') {
                   2525:                                type = RCS_TOK_ERR;
                   2526:                                break;
                   2527:                        }
                   2528:                        last = ch;
                   2529:                        *(bp++) = ch;
1.42      jfb      2530:                        pdp->rp_tlen++;
1.1       jfb      2531:                }
1.18      jfb      2532:                *bp = '\0';
1.1       jfb      2533:        }
                   2534:
                   2535:        return (type);
                   2536: }
                   2537:
                   2538: /*
                   2539:  * rcs_pushtok()
                   2540:  *
                   2541:  * Push a token back in the parser's token buffer.
                   2542:  */
                   2543: static int
                   2544: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2545: {
                   2546:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2547:
                   2548:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2549:                return (-1);
                   2550:
                   2551:        pdp->rp_pttype = type;
                   2552:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   2553:        return (0);
                   2554: }
                   2555:
                   2556:
                   2557: /*
                   2558:  * rcs_splitlines()
                   2559:  *
                   2560:  * Split the contents of a file into a list of lines.
                   2561:  */
                   2562: static struct rcs_foo*
                   2563: rcs_splitlines(const char *fcont)
                   2564: {
                   2565:        char *dcp;
                   2566:        struct rcs_foo *foo;
                   2567:        struct rcs_line *lp;
                   2568:
                   2569:        foo = (struct rcs_foo *)malloc(sizeof(*foo));
                   2570:        if (foo == NULL) {
                   2571:                cvs_log(LP_ERR, "failed to allocate line structure");
                   2572:                return (NULL);
                   2573:        }
                   2574:        TAILQ_INIT(&(foo->rl_lines));
                   2575:        foo->rl_nblines = 0;
                   2576:        foo->rl_data = strdup(fcont);
                   2577:        if (foo->rl_data == NULL) {
                   2578:                cvs_log(LP_ERRNO, "failed to copy file contents");
                   2579:                free(foo);
                   2580:                return (NULL);
                   2581:        }
                   2582:
                   2583:        /*
                   2584:         * Add a first bogus line with line number 0.  This is used so we
                   2585:         * can position the line pointer before 1 when changing the first line
                   2586:         * in rcs_patch().
                   2587:         */
                   2588:        lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38      joris    2589:        if (lp == NULL) {
                   2590:                rcs_freefoo(foo);
1.1       jfb      2591:                return (NULL);
1.38      joris    2592:        }
1.5       vincent  2593:
1.1       jfb      2594:        lp->rl_line = NULL;
                   2595:        lp->rl_lineno = 0;
                   2596:        TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   2597:
                   2598:
                   2599:        for (dcp = foo->rl_data; *dcp != '\0';) {
                   2600:                lp = (struct rcs_line *)malloc(sizeof(*lp));
                   2601:                if (lp == NULL) {
1.38      joris    2602:                        rcs_freefoo(foo);
1.1       jfb      2603:                        cvs_log(LP_ERR, "failed to allocate line entry");
                   2604:                        return (NULL);
                   2605:                }
                   2606:
                   2607:                lp->rl_line = dcp;
                   2608:                lp->rl_lineno = ++(foo->rl_nblines);
                   2609:                TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   2610:
                   2611:                dcp = strchr(dcp, '\n');
                   2612:                if (dcp == NULL) {
                   2613:                        break;
                   2614:                }
                   2615:                *(dcp++) = '\0';
                   2616:        }
                   2617:
                   2618:        return (foo);
1.5       vincent  2619: }
                   2620:
                   2621: static void
                   2622: rcs_freefoo(struct rcs_foo *fp)
                   2623: {
                   2624:        struct rcs_line *lp;
                   2625:
                   2626:        while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
                   2627:                TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
                   2628:                free(lp);
                   2629:        }
                   2630:        free(fp->rl_data);
                   2631:        free(fp);
1.18      jfb      2632: }
                   2633:
                   2634: /*
                   2635:  * rcs_growbuf()
                   2636:  *
                   2637:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2638:  * RCS_BUFEXTSIZE.
                   2639:  * In case of failure, the original buffer is left unmodified.
                   2640:  * Returns 0 on success, or -1 on failure.
                   2641:  */
                   2642: static int
                   2643: rcs_growbuf(RCSFILE *rf)
                   2644: {
                   2645:        void *tmp;
                   2646:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2647:
                   2648:        tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
                   2649:        if (tmp == NULL) {
1.50      jfb      2650:                rcs_errno = RCS_ERR_ERRNO;
1.18      jfb      2651:                cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
                   2652:                return (-1);
                   2653:        }
                   2654:
                   2655:        pdp->rp_buf = (char *)tmp;
                   2656:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2657:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2658:
                   2659:        return (0);
                   2660: }
                   2661:
                   2662: /*
                   2663:  * rcs_strprint()
                   2664:  *
                   2665:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2666:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2667:  * binary data.
                   2668:  */
                   2669: static int
                   2670: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2671: {
                   2672:        const u_char *ap, *ep, *sp;
                   2673:        size_t ret;
1.52      jfb      2674:
                   2675:        if (slen == 0)
                   2676:                return (0);
1.42      jfb      2677:
                   2678:        ep = str + slen - 1;
                   2679:
                   2680:        for (sp = str; sp <= ep;)  {
                   2681:                ap = memchr(sp, '@', ep - sp);
                   2682:                if (ap == NULL)
                   2683:                        ap = ep;
                   2684:                ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
                   2685:
                   2686:                if (*ap == '@')
                   2687:                        putc('@', stream);
                   2688:                sp = ap + 1;
1.63      joris    2689:        }
                   2690:
                   2691:        return (0);
                   2692: }
                   2693:
                   2694: /*
                   2695:  * rcs_expand_keywords()
                   2696:  *
                   2697:  * Expand any RCS keywords in <line> into <out>
                   2698:  */
                   2699: static int
                   2700: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
                   2701:     size_t len, int mode)
                   2702: {
                   2703:        int kwtype;
                   2704:        u_int i, j, found;
                   2705:        char *c, *kwstr, *start;
                   2706:        char expbuf[128], buf[128];
                   2707:
1.65      niallo   2708:        kwtype = 0;
                   2709:        kwstr = NULL;
1.63      joris    2710:        i = 0;
                   2711:
                   2712:        /*
                   2713:         * Keyword formats:
                   2714:         * $Keyword$
                   2715:         * $Keyword: value$
                   2716:         */
                   2717:        memset(out, '\0', len);
                   2718:        for (c = line; *c != '\0' && i < len; *c++) {
                   2719:                out[i++] = *c;
                   2720:                if (*c == '$') {
                   2721:                        /* remember start of this possible keyword */
                   2722:                        start = c;
                   2723:
                   2724:                        /* first following character has to be alphanumeric */
                   2725:                        *c++;
                   2726:                        if (!isalpha(*c)) {
                   2727:                                c = start;
                   2728:                                continue;
                   2729:                        }
                   2730:
                   2731:                        /* look for any matching keywords */
                   2732:                        found = 0;
                   2733:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2734:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2735:                                    strlen(rcs_expkw[j].kw_str))) {
                   2736:                                        found = 1;
                   2737:                                        kwstr = rcs_expkw[j].kw_str;
                   2738:                                        kwtype = rcs_expkw[j].kw_type;
                   2739:                                        break;
                   2740:                                }
                   2741:                        }
                   2742:
                   2743:                        /* unknown keyword, continue looking */
                   2744:                        if (found == 0) {
                   2745:                                c = start;
                   2746:                                continue;
                   2747:                        }
                   2748:
                   2749:                        /* next character has to be ':' or '$' */
                   2750:                        c += strlen(kwstr);
                   2751:                        if (*c != ':' && *c != '$') {
                   2752:                                c = start;
                   2753:                                continue;
                   2754:                        }
                   2755:
                   2756:                        /*
                   2757:                         * if the next character was ':' we need to look for
                   2758:                         * an '$' before the end of the line to be sure it is
                   2759:                         * in fact a keyword.
                   2760:                         */
                   2761:                        if (*c == ':') {
                   2762:                                while (*c++) {
                   2763:                                        if (*c == '$' || *c == '\n')
                   2764:                                                break;
                   2765:                                }
                   2766:
                   2767:                                if (*c != '$') {
                   2768:                                        c = start;
                   2769:                                        continue;
                   2770:                                }
                   2771:                        }
                   2772:
                   2773:                        /* start constructing the expansion */
                   2774:                        expbuf[0] = '\0';
                   2775:
                   2776:                        if (mode & RCS_KWEXP_NAME) {
                   2777:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2778:                                strlcat(expbuf, kwstr, sizeof(expbuf));
                   2779:                                if (mode & RCS_KWEXP_VAL)
                   2780:                                        strlcat(expbuf, ": ", sizeof(expbuf));
                   2781:                        }
                   2782:
                   2783:                        /*
                   2784:                         * order matters because of RCS_KW_ID and RCS_KW_HEADER here
                   2785:                         */
                   2786:                        if (mode & RCS_KWEXP_VAL) {
                   2787:                                if (kwtype & RCS_KW_RCSFILE) {
                   2788:                                        if (!(kwtype & RCS_KW_FULLPATH))
                   2789:                                                strlcat(expbuf, basename(rcsfile),
                   2790:                                                    sizeof(expbuf));
                   2791:                                        else
                   2792:                                                strlcat(expbuf, rcsfile,
                   2793:                                                    sizeof(expbuf));
                   2794:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2795:                                }
                   2796:
                   2797:                                if (kwtype & RCS_KW_REVISION) {
                   2798:                                        rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
                   2799:                                        strlcat(buf, " ", sizeof(buf));
                   2800:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2801:                                }
                   2802:
                   2803:                                if (kwtype & RCS_KW_DATE) {
                   2804:                                        strftime(buf, sizeof(buf),
                   2805:                                            "%Y/%m/%d %H:%M:%S ", &rdp->rd_date);
                   2806:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2807:                                }
                   2808:
                   2809:                                if (kwtype & RCS_KW_AUTHOR) {
                   2810:                                        strlcat(expbuf, rdp->rd_author,
                   2811:                                            sizeof(expbuf));
                   2812:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2813:                                }
                   2814:
                   2815:                                if (kwtype & RCS_KW_STATE) {
                   2816:                                        strlcat(expbuf, rdp->rd_state,
                   2817:                                            sizeof(expbuf));
                   2818:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2819:                                }
                   2820:
                   2821:                                /* order does not matter anymore below */
                   2822:                                if (kwtype & RCS_KW_LOG)
                   2823:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2824:
                   2825:                                if (kwtype & RCS_KW_SOURCE) {
                   2826:                                        strlcat(expbuf, rcsfile, sizeof(expbuf));
                   2827:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2828:                                }
                   2829:
                   2830:                                if (kwtype & RCS_KW_NAME)
                   2831:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2832:                        }
                   2833:
                   2834:                        /* end the expansion */
                   2835:                        if (mode & RCS_KWEXP_NAME)
                   2836:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2837:
                   2838:                        out[--i] = '\0';
                   2839:                        strlcat(out, expbuf, len);
                   2840:                        i += strlen(expbuf);
                   2841:                }
1.42      jfb      2842:        }
1.18      jfb      2843:
                   2844:        return (0);
1.1       jfb      2845: }