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

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