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

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