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

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