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

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