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

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