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

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