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

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