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

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