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

1.167   ! ray         1: /*     $OpenBSD: rcs.c,v 1.166 2006/04/13 19:55:41 joris Exp $ */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.15      tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.15      tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.15      tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.15      tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.15      tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
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.167   ! ray       237: static void    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.155     ray       254: static void    rcs_strprint(const u_char *, size_t, FILE *);
1.57      xsa       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.159     joris     274:        int ret, mode;
1.26      jfb       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);
1.159     joris     288:                        mode = va_arg(vap, int);
1.26      jfb       289:                        va_end(vap);
1.159     joris     290:                        fmode = (mode_t)mode;
1.26      jfb       291:                } else {
1.105     joris     292:                        /* XXX, make this command dependant? */
                    293: #if 0
1.26      jfb       294:                        cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
1.105     joris     295: #endif
1.127     xsa       296:                        rcs_errno = RCS_ERR_NOENT;
1.26      jfb       297:                        return (NULL);
                    298:                }
                    299:        } else if ((ret == 0) && (flags & RCS_CREATE)) {
                    300:                cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1       jfb       301:                return (NULL);
                    302:        }
                    303:
1.161     ray       304:        rfp = xcalloc(1, sizeof(*rfp));
1.1       jfb       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.167   ! ray       315:        if (!(rfp->rf_flags & RCS_CREATE))
        !           316:                rcs_parse_init(rfp);
1.1       jfb       317:
1.109     joris     318:        /* fill in rd_locker */
                    319:        TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
                    320:                if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
                    321:                        rcs_close(rfp);
                    322:                        return (NULL);
                    323:                }
                    324:
1.110     joris     325:                rdp->rd_locker = xstrdup(lkr->rl_name);
1.109     joris     326:        }
                    327:
1.1       jfb       328:        return (rfp);
                    329: }
                    330:
                    331: /*
                    332:  * rcs_close()
                    333:  *
                    334:  * Close an RCS file handle.
                    335:  */
                    336: void
                    337: rcs_close(RCSFILE *rfp)
                    338: {
                    339:        struct rcs_delta *rdp;
1.71      moritz    340:        struct rcs_access *rap;
1.13      jfb       341:        struct rcs_lock *rlp;
                    342:        struct rcs_sym *rsp;
1.1       jfb       343:
1.26      jfb       344:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    345:                rcs_write(rfp);
                    346:
1.1       jfb       347:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    348:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    349:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    350:                rcs_freedelta(rdp);
1.71      moritz    351:        }
                    352:
                    353:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    354:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    355:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
1.110     joris     356:                xfree(rap->ra_name);
                    357:                xfree(rap);
1.1       jfb       358:        }
                    359:
1.13      jfb       360:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    361:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    362:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    363:                rcsnum_free(rsp->rs_num);
1.110     joris     364:                xfree(rsp->rs_name);
                    365:                xfree(rsp);
1.13      jfb       366:        }
                    367:
                    368:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    369:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    370:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    371:                rcsnum_free(rlp->rl_num);
1.110     joris     372:                xfree(rlp->rl_name);
                    373:                xfree(rlp);
1.13      jfb       374:        }
                    375:
1.1       jfb       376:        if (rfp->rf_head != NULL)
                    377:                rcsnum_free(rfp->rf_head);
1.11      joris     378:        if (rfp->rf_branch != NULL)
                    379:                rcsnum_free(rfp->rf_branch);
1.1       jfb       380:
                    381:        if (rfp->rf_path != NULL)
1.110     joris     382:                xfree(rfp->rf_path);
1.1       jfb       383:        if (rfp->rf_comment != NULL)
1.110     joris     384:                xfree(rfp->rf_comment);
1.1       jfb       385:        if (rfp->rf_expand != NULL)
1.110     joris     386:                xfree(rfp->rf_expand);
1.1       jfb       387:        if (rfp->rf_desc != NULL)
1.110     joris     388:                xfree(rfp->rf_desc);
1.118     joris     389:        if (rfp->rf_pdata != NULL)
                    390:                rcs_freepdata(rfp->rf_pdata);
1.110     joris     391:        xfree(rfp);
1.1       jfb       392: }
                    393:
                    394: /*
                    395:  * rcs_write()
                    396:  *
                    397:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    398:  * path is in <rf_path>.
                    399:  * Returns 0 on success, or -1 on failure.
                    400:  */
1.152     niallo    401: int
1.1       jfb       402: rcs_write(RCSFILE *rfp)
                    403: {
                    404:        FILE *fp;
1.72      niallo    405:        char buf[1024], numbuf[64], fn[19] = "";
                    406:        void *bp;
1.29      jfb       407:        struct rcs_access *ap;
1.1       jfb       408:        struct rcs_sym *symp;
1.46      jfb       409:        struct rcs_branch *brp;
1.1       jfb       410:        struct rcs_delta *rdp;
1.75      joris     411:        struct rcs_lock *lkp;
1.155     ray       412:        ssize_t nread, nwritten;
1.100     xsa       413:        size_t len;
1.72      niallo    414:        int fd, from_fd, to_fd;
1.75      joris     415:
1.72      niallo    416:        from_fd = to_fd = fd = -1;
1.1       jfb       417:
1.137     joris     418:        if (rfp->rf_flags & RCS_SYNCED)
                    419:                return (0);
                    420:
1.117     niallo    421:        /* Write operations need the whole file parsed */
                    422:        rcs_parse_deltatexts(rfp, NULL);
1.1       jfb       423:
1.72      niallo    424:        strlcpy(fn, "/tmp/rcs.XXXXXXXXXX", sizeof(fn));
1.113     xsa       425:        if ((fd = mkstemp(fn)) == -1)
                    426:                fatal("mkstemp: `%s': %s", fn, strerror(errno));
                    427:
                    428:        if ((fp = fdopen(fd, "w+")) == NULL) {
                    429:                fd = errno;
                    430:                unlink(fn);
                    431:                fatal("fdopen: %s", strerror(fd));
1.1       jfb       432:        }
                    433:
1.28      jfb       434:        if (rfp->rf_head != NULL)
                    435:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    436:        else
                    437:                numbuf[0] = '\0';
                    438:
1.1       jfb       439:        fprintf(fp, "head\t%s;\n", numbuf);
1.35      jfb       440:
                    441:        if (rfp->rf_branch != NULL) {
                    442:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    443:                fprintf(fp, "branch\t%s;\n", numbuf);
                    444:        }
                    445:
1.29      jfb       446:        fputs("access", fp);
                    447:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    448:                fprintf(fp, "\n\t%s", ap->ra_name);
                    449:        }
                    450:        fputs(";\n", fp);
1.1       jfb       451:
1.85      joris     452:        fprintf(fp, "symbols");
1.1       jfb       453:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    454:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.126     xsa       455:                strlcpy(buf, symp->rs_name, sizeof(buf));
                    456:                strlcat(buf, ":", sizeof(buf));
                    457:                strlcat(buf, numbuf, sizeof(buf));
1.85      joris     458:                fprintf(fp, "\n\t%s", buf);
1.1       jfb       459:        }
                    460:        fprintf(fp, ";\n");
                    461:
1.75      joris     462:        fprintf(fp, "locks");
                    463:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    464:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    465:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    466:        }
                    467:
                    468:        fprintf(fp, ";");
1.1       jfb       469:
1.26      jfb       470:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       471:                fprintf(fp, " strict;");
                    472:        fputc('\n', fp);
                    473:
1.129     xsa       474:        fputs("comment\t@", fp);
1.42      jfb       475:        if (rfp->rf_comment != NULL) {
1.58      xsa       476:                rcs_strprint((const u_char *)rfp->rf_comment,
                    477:                    strlen(rfp->rf_comment), fp);
1.42      jfb       478:                fputs("@;\n", fp);
1.129     xsa       479:        } else
                    480:                fputs("# @;\n", fp);
1.1       jfb       481:
1.42      jfb       482:        if (rfp->rf_expand != NULL) {
                    483:                fputs("expand @", fp);
1.58      xsa       484:                rcs_strprint((const u_char *)rfp->rf_expand,
                    485:                    strlen(rfp->rf_expand), fp);
1.42      jfb       486:                fputs("@;\n", fp);
                    487:        }
1.1       jfb       488:
1.46      jfb       489:        fputs("\n\n", fp);
1.1       jfb       490:
                    491:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    492:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    493:                    sizeof(numbuf)));
                    494:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44      jfb       495:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1       jfb       496:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    497:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    498:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    499:                    rdp->rd_author, rdp->rd_state);
1.46      jfb       500:                fputs("branches", fp);
                    501:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                    502:                        fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
                    503:                            sizeof(numbuf)));
                    504:                }
                    505:                fputs(";\n", fp);
1.1       jfb       506:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    507:                    numbuf, sizeof(numbuf)));
                    508:        }
                    509:
1.42      jfb       510:        fputs("\ndesc\n@", fp);
1.141     ray       511:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
1.100     xsa       512:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    513:                if (rfp->rf_desc[len-1] != '\n')
                    514:                        fputc('\n', fp);
                    515:        }
                    516:        fputs("@\n", fp);
1.1       jfb       517:
                    518:        /* deltatexts */
                    519:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.100     xsa       520:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
1.1       jfb       521:                    sizeof(numbuf)));
1.42      jfb       522:                fputs("log\n@", fp);
1.100     xsa       523:                if (rdp->rd_log != NULL) {
                    524:                        len = strlen(rdp->rd_log);
                    525:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
                    526:                        if (rdp->rd_log[len-1] != '\n')
                    527:                                fputc('\n', fp);
                    528:                }
1.42      jfb       529:                fputs("@\ntext\n@", fp);
1.100     xsa       530:                if (rdp->rd_text != NULL) {
                    531:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
1.103     joris     532:
                    533:                        if (rdp->rd_tlen != 0) {
                    534:                                if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
                    535:                                        fputc('\n', fp);
                    536:                        }
1.100     xsa       537:                }
                    538:                fputs("@\n", fp);
1.1       jfb       539:        }
                    540:        fclose(fp);
1.73      joris     541:
1.72      niallo    542:        /*
                    543:         * We try to use rename() to atomically put the new file in place.
                    544:         * If that fails, we try a copy.
                    545:         */
                    546:        if (rename(fn, rfp->rf_path) == -1) {
                    547:                if (errno == EXDEV) {
                    548:                        /* rename() not supported so we have to copy. */
                    549:                        if ((chmod(rfp->rf_path, S_IWUSR) == -1)
                    550:                            && !(rfp->rf_flags & RCS_CREATE)) {
1.113     xsa       551:                                fatal("chmod(%s, 0%o) failed",
                    552:                                    rfp->rf_path, S_IWUSR);
1.72      niallo    553:                        }
1.73      joris     554:
1.72      niallo    555:                        if ((from_fd = open(fn, O_RDONLY)) == -1) {
                    556:                                cvs_log(LP_ERRNO, "failed to open `%s'",
                    557:                                    rfp->rf_path);
                    558:                                return (-1);
                    559:                        }
1.73      joris     560:
1.80      reyk      561:                        if ((to_fd = open(rfp->rf_path,
                    562:                            O_WRONLY|O_TRUNC|O_CREAT)) == -1) {
1.73      joris     563:                                cvs_log(LP_ERRNO, "failed to open `%s'", fn);
1.72      niallo    564:                                close(from_fd);
                    565:                                return (-1);
                    566:                        }
1.73      joris     567:
1.110     joris     568:                        bp = xmalloc(MAXBSIZE);
1.155     ray       569:                        for (;;) {
                    570:                                if ((nread = read(from_fd, bp, MAXBSIZE)) == 0)
                    571:                                        break;
                    572:                                if (nread == -1)
                    573:                                        goto err;
                    574:                                nwritten = write(to_fd, bp, (size_t)nread);
                    575:                                if (nwritten == -1 || nwritten != nread)
1.72      niallo    576:                                        goto err;
                    577:                        }
1.73      joris     578:
1.72      niallo    579:                        if (nread < 0) {
                    580: err:                           if (unlink(rfp->rf_path) == -1)
                    581:                                        cvs_log(LP_ERRNO,
                    582:                                            "failed to unlink `%s'",
                    583:                                            rfp->rf_path);
                    584:                                close(from_fd);
                    585:                                close(to_fd);
1.110     joris     586:                                xfree(bp);
1.72      niallo    587:                                return (-1);
                    588:                        }
1.73      joris     589:
1.72      niallo    590:                        close(from_fd);
                    591:                        close(to_fd);
1.110     joris     592:                        xfree(bp);
1.73      joris     593:
1.72      niallo    594:                        if (unlink(fn) == -1) {
                    595:                                cvs_log(LP_ERRNO,
1.73      joris     596:                                    "failed to unlink `%s'", fn);
1.72      niallo    597:                                return (-1);
                    598:                        }
                    599:                } else {
                    600:                        cvs_log(LP_ERRNO,
                    601:                            "failed to access temp RCS output file");
                    602:                        return (-1);
                    603:                }
                    604:        }
1.73      joris     605:
1.152     niallo    606:        if ((chmod(rfp->rf_path, rfp->rf_mode) == -1)) {
1.72      niallo    607:                cvs_log(LP_ERRNO, "failed to chmod `%s'",
                    608:                    rfp->rf_path);
                    609:                return (-1);
                    610:        }
1.73      joris     611:
1.26      jfb       612:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb       613:
                    614:        return (0);
                    615: }
                    616:
                    617: /*
1.43      jfb       618:  * rcs_head_get()
                    619:  *
                    620:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    621:  */
1.60      xsa       622: const RCSNUM *
1.43      jfb       623: rcs_head_get(RCSFILE *file)
                    624: {
                    625:        return (file->rf_head);
                    626: }
                    627:
                    628: /*
                    629:  * rcs_head_set()
                    630:  *
                    631:  * Set the revision number of the head revision for the RCS file <file> to
                    632:  * <rev>, which must reference a valid revision within the file.
                    633:  */
                    634: int
1.117     niallo    635: rcs_head_set(RCSFILE *file, RCSNUM *rev)
1.43      jfb       636: {
1.167   ! ray       637:        if (rcs_findrev(file, rev) == NULL)
1.43      jfb       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.161     ray       698:        ap = xmalloc(sizeof(*ap));
1.110     joris     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.161     ray       762:        symp = xmalloc(sizeof(*symp));
1.110     joris     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.161     ray       926:        lkp = xmalloc(sizeof(*lkp));
1.110     joris     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:  * rcs_lock_remove()
                    941:  *
                    942:  * Remove the RCS lock on revision <rev>.
                    943:  * Returns 0 on success, or -1 on failure.
                    944:  */
                    945: int
1.109     joris     946: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
1.40      jfb       947: {
                    948:        struct rcs_lock *lkp;
                    949:
1.109     joris     950:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    951:                if ((strcmp(lkp->rl_name, user) == 0) &&
                    952:                    (rcsnum_cmp(lkp->rl_num, rev, 0) == 0))
1.40      jfb       953:                        break;
1.109     joris     954:        }
1.40      jfb       955:
                    956:        if (lkp == NULL) {
                    957:                rcs_errno = RCS_ERR_NOENT;
                    958:                return (-1);
                    959:        }
                    960:
                    961:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
                    962:        rcsnum_free(lkp->rl_num);
1.110     joris     963:        xfree(lkp->rl_name);
                    964:        xfree(lkp);
1.40      jfb       965:
                    966:        /* not synced anymore */
                    967:        file->rf_flags &= ~RCS_SYNCED;
                    968:        return (0);
                    969: }
                    970:
                    971: /*
1.27      jfb       972:  * rcs_desc_get()
                    973:  *
                    974:  * Retrieve the description for the RCS file <file>.
                    975:  */
1.57      xsa       976: const char *
1.27      jfb       977: rcs_desc_get(RCSFILE *file)
                    978: {
                    979:        return (file->rf_desc);
                    980: }
                    981:
                    982: /*
                    983:  * rcs_desc_set()
                    984:  *
                    985:  * Set the description for the RCS file <file>.
                    986:  */
1.149     xsa       987: void
1.27      jfb       988: rcs_desc_set(RCSFILE *file, const char *desc)
                    989: {
                    990:        char *tmp;
                    991:
1.110     joris     992:        tmp = xstrdup(desc);
1.27      jfb       993:        if (file->rf_desc != NULL)
1.110     joris     994:                xfree(file->rf_desc);
1.27      jfb       995:        file->rf_desc = tmp;
                    996:        file->rf_flags &= ~RCS_SYNCED;
1.51      jfb       997: }
                    998:
                    999: /*
                   1000:  * rcs_comment_lookup()
                   1001:  *
                   1002:  * Lookup the assumed comment leader based on a file's suffix.
                   1003:  * Returns a pointer to the string on success, or NULL on failure.
                   1004:  */
1.57      xsa      1005: const char *
1.51      jfb      1006: rcs_comment_lookup(const char *filename)
                   1007: {
                   1008:        int i;
                   1009:        const char *sp;
                   1010:
                   1011:        if ((sp = strrchr(filename, '.')) == NULL) {
                   1012:                rcs_errno = RCS_ERR_NOENT;
                   1013:                return (NULL);
                   1014:        }
                   1015:        sp++;
                   1016:
                   1017:        for (i = 0; i < (int)NB_COMTYPES; i++)
                   1018:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                   1019:                        return (rcs_comments[i].rc_cstr);
                   1020:        return (NULL);
1.27      jfb      1021: }
                   1022:
1.33      jfb      1023: /*
                   1024:  * rcs_comment_get()
                   1025:  *
                   1026:  * Retrieve the comment leader for the RCS file <file>.
                   1027:  */
1.57      xsa      1028: const char *
1.33      jfb      1029: rcs_comment_get(RCSFILE *file)
                   1030: {
                   1031:        return (file->rf_comment);
                   1032: }
                   1033:
                   1034: /*
                   1035:  * rcs_comment_set()
                   1036:  *
                   1037:  * Set the comment leader for the RCS file <file>.
                   1038:  */
1.150     xsa      1039: void
1.33      jfb      1040: rcs_comment_set(RCSFILE *file, const char *comment)
                   1041: {
                   1042:        char *tmp;
                   1043:
1.110     joris    1044:        tmp = xstrdup(comment);
1.33      jfb      1045:        if (file->rf_comment != NULL)
1.110     joris    1046:                xfree(file->rf_comment);
1.33      jfb      1047:        file->rf_comment = tmp;
                   1048:        file->rf_flags &= ~RCS_SYNCED;
                   1049: }
1.40      jfb      1050:
                   1051: /*
                   1052:  * rcs_tag_resolve()
                   1053:  *
                   1054:  * Retrieve the revision number corresponding to the tag <tag> for the RCS
                   1055:  * file <file>.
                   1056:  */
1.60      xsa      1057: RCSNUM *
1.40      jfb      1058: rcs_tag_resolve(RCSFILE *file, const char *tag)
                   1059: {
                   1060:        RCSNUM *num;
                   1061:
                   1062:        if ((num = rcsnum_parse(tag)) == NULL) {
                   1063:                num = rcs_sym_getrev(file, tag);
                   1064:        }
                   1065:
                   1066:        return (num);
                   1067: }
                   1068:
1.94      joris    1069: int
                   1070: rcs_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
1.5       vincent  1071: {
                   1072:        char op, *ep;
1.94      joris    1073:        struct cvs_line *lp, *dlp, *ndlp;
1.5       vincent  1074:        int i, lineno, nbln;
1.1       jfb      1075:
1.94      joris    1076:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1077:        lp = TAILQ_FIRST(&(plines->l_lines));
1.1       jfb      1078:
                   1079:        /* skip first bogus line */
1.94      joris    1080:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1081:            lp = TAILQ_NEXT(lp, l_list)) {
                   1082:                op = *(lp->l_line);
                   1083:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
                   1084:                if ((lineno > dlines->l_nblines) || (lineno < 0) ||
1.132     niallo   1085:                    (*ep != ' '))
                   1086:                        fatal("invalid line specification in RCS patch");
1.1       jfb      1087:                ep++;
                   1088:                nbln = (int)strtol(ep, &ep, 10);
1.132     niallo   1089:                if ((nbln < 0) || (*ep != '\0'))
                   1090:                        fatal("invalid line number specification in RCS patch");
1.1       jfb      1091:
                   1092:                /* find the appropriate line */
                   1093:                for (;;) {
                   1094:                        if (dlp == NULL)
                   1095:                                break;
1.94      joris    1096:                        if (dlp->l_lineno == lineno)
1.1       jfb      1097:                                break;
1.94      joris    1098:                        if (dlp->l_lineno > lineno) {
                   1099:                                dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
                   1100:                        } else if (dlp->l_lineno < lineno) {
1.133     niallo   1101:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.138     deraadt  1102:                                    (ndlp->l_lineno > lineno))
1.1       jfb      1103:                                        break;
                   1104:                                dlp = ndlp;
                   1105:                        }
                   1106:                }
1.132     niallo   1107:                if (dlp == NULL)
                   1108:                        fatal("can't find referenced line in RCS patch");
1.1       jfb      1109:
                   1110:                if (op == 'd') {
                   1111:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94      joris    1112:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1113:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.1       jfb      1114:                                dlp = ndlp;
1.133     niallo   1115:                                /* last line is gone - reset dlp */
                   1116:                                if (dlp == NULL) {
                   1117:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1118:                                            cvs_tqh);
                   1119:                                        dlp = ndlp;
                   1120:                                }
1.1       jfb      1121:                        }
1.14      deraadt  1122:                } else if (op == 'a') {
1.1       jfb      1123:                        for (i = 0; i < nbln; i++) {
                   1124:                                ndlp = lp;
1.94      joris    1125:                                lp = TAILQ_NEXT(lp, l_list);
1.132     niallo   1126:                                if (lp == NULL)
                   1127:                                        fatal("truncated RCS patch");
1.94      joris    1128:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1129:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1130:                                    lp, l_list);
1.1       jfb      1131:                                dlp = lp;
                   1132:
                   1133:                                /* we don't want lookup to block on those */
1.94      joris    1134:                                lp->l_lineno = lineno;
1.1       jfb      1135:
                   1136:                                lp = ndlp;
                   1137:                        }
1.132     niallo   1138:                } else
                   1139:                        fatal("unknown RCS patch operation `%c'", op);
1.1       jfb      1140:
                   1141:                /* last line of the patch, done */
1.94      joris    1142:                if (lp->l_lineno == plines->l_nblines)
1.1       jfb      1143:                        break;
                   1144:        }
                   1145:
                   1146:        /* once we're done patching, rebuild the line numbers */
1.2       vincent  1147:        lineno = 0;
1.94      joris    1148:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1149:                lp->l_lineno = lineno++;
                   1150:        dlines->l_nblines = lineno - 1;
1.1       jfb      1151:
1.5       vincent  1152:        return (0);
1.1       jfb      1153: }
                   1154:
                   1155: /*
                   1156:  * rcs_getrev()
                   1157:  *
                   1158:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent  1159:  * returned buffer is dynamically allocated and should be released using
                   1160:  * cvs_buf_free() once the caller is done using it.
1.1       jfb      1161:  */
                   1162: BUF*
1.66      joris    1163: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1       jfb      1164: {
1.156     joris    1165:        u_int i, numlen;
                   1166:        int isbranch, lookonbranch;
1.1       jfb      1167:        size_t len;
                   1168:        void *bp;
1.156     joris    1169:        RCSNUM *crev, *rev, *brev;
1.125     niallo   1170:        BUF *rbuf;
1.1       jfb      1171:        struct rcs_delta *rdp = NULL;
1.156     joris    1172:        struct rcs_branch *rb;
1.1       jfb      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.156     joris    1182:        /* XXX rcsnum_cmp() */
                   1183:        for (i = 0; i < rfp->rf_head->rn_len; i++) {
                   1184:                if (rfp->rf_head->rn_id[i] < rev->rn_id[i]) {
                   1185:                        rcs_errno = RCS_ERR_NOENT;
                   1186:                        return (NULL);
                   1187:                }
1.26      jfb      1188:        }
                   1189:
1.117     niallo   1190:        /* No matter what, we're going to need up the the description parsed */
                   1191:        rcs_parse_desc(rfp, NULL);
                   1192:
1.26      jfb      1193:        rdp = rcs_findrev(rfp, rfp->rf_head);
                   1194:        if (rdp == NULL) {
                   1195:                cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                   1196:                return (NULL);
                   1197:        }
1.156     joris    1198:
1.117     niallo   1199:        if (rdp->rd_tlen == 0)
                   1200:                rcs_parse_deltatexts(rfp, rfp->rf_head);
1.156     joris    1201:
1.45      jfb      1202:        len = rdp->rd_tlen;
1.115     niallo   1203:        if (len == 0) {
                   1204:                rbuf = cvs_buf_alloc(1, 0);
                   1205:                cvs_buf_empty(rbuf);
                   1206:                return (rbuf);
                   1207:        }
1.26      jfb      1208:
1.116     joris    1209:        rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
1.26      jfb      1210:        cvs_buf_append(rbuf, rdp->rd_text, len);
                   1211:
1.156     joris    1212:        isbranch = 0;
                   1213:        brev = NULL;
                   1214:
                   1215:        /*
                   1216:         * If a branch was passed, get the latest revision on it.
                   1217:         */
                   1218:        if (RCSNUM_ISBRANCH(rev)) {
                   1219:                brev = rev;
                   1220:                rdp = rcs_findrev(rfp, rev);
                   1221:                if (rdp == NULL)
1.158     joris    1222:                        return (NULL);
1.156     joris    1223:
                   1224:                rev = rdp->rd_num;
                   1225:        } else {
                   1226:                if (RCSNUM_ISBRANCHREV(rev)) {
                   1227:                        brev = rcsnum_revtobr(rev);
                   1228:                        isbranch = 1;
                   1229:                }
                   1230:        }
                   1231:
                   1232:        lookonbranch = 0;
                   1233:        crev = NULL;
                   1234:
                   1235:        /* Apply patches backwards to get the right version.
                   1236:         */
                   1237:        do {
                   1238:                if (rcsnum_cmp(rfp->rf_head, rev, 0) == 0)
                   1239:                        break;
                   1240:
                   1241:                if ((isbranch == 1) && (rdp->rd_num->rn_len < rev->rn_len) &&
                   1242:                    (!TAILQ_EMPTY(&(rdp->rd_branches))))
                   1243:                        lookonbranch = 1;
                   1244:
                   1245:                if (isbranch && lookonbranch == 1) {
                   1246:                        lookonbranch = 0;
                   1247:                        TAILQ_FOREACH(rb, &(rdp->rd_branches), rb_list) {
                   1248:                                /* XXX rcsnum_cmp() is totally broken for
                   1249:                                 * this purpose.
                   1250:                                 */
                   1251:                                numlen = MIN(brev->rn_len, rb->rb_num->rn_len);
                   1252:                                for (i = 0; i < numlen; i++) {
                   1253:                                        if (rb->rb_num->rn_id[i] !=
                   1254:                                            brev->rn_id[i])
                   1255:                                                break;
                   1256:                                }
                   1257:
                   1258:                                if (i == numlen) {
                   1259:                                        crev = rb->rb_num;
                   1260:                                        break;
                   1261:                                }
                   1262:                        }
                   1263:                } else {
1.87      joris    1264:                        crev = rdp->rd_next;
1.156     joris    1265:                }
                   1266:
                   1267:                rdp = rcs_findrev(rfp, crev);
                   1268:                if (rdp == NULL) {
                   1269:                        cvs_buf_free(rbuf);
                   1270:                        return (NULL);
                   1271:                }
                   1272:
                   1273:                cvs_buf_putc(rbuf, '\0');
                   1274:
                   1275:                /* check if we have parsed this rev's deltatext */
                   1276:                if (rdp->rd_tlen == 0)
                   1277:                        rcs_parse_deltatexts(rfp, rdp->rd_num);
                   1278:
                   1279:                bp = cvs_buf_release(rbuf);
                   1280:                rbuf = cvs_patchfile((char *)bp, (char *)rdp->rd_text,
                   1281:                    rcs_patch_lines);
                   1282:                xfree(bp);
                   1283:
                   1284:                if (rbuf == NULL)
                   1285:                        break;
                   1286:        } while (rcsnum_cmp(crev, rev, 0) != 0);
                   1287:
1.125     niallo   1288:        if (cvs_buf_getc(rbuf, cvs_buf_len(rbuf)-1) != '\n'
                   1289:            && rbuf != NULL)
                   1290:                cvs_buf_putc(rbuf, '\n');
1.156     joris    1291:
1.125     niallo   1292:        return (rbuf);
1.1       jfb      1293: }
                   1294:
                   1295: /*
1.52      jfb      1296:  * rcs_rev_add()
                   1297:  *
1.53      jfb      1298:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1299:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1300:  * new revision will have a number equal to the previous head revision plus
                   1301:  * one).  The <msg> argument specifies the log message for that revision, and
                   1302:  * <date> specifies the revision's date (a value of -1 is
                   1303:  * equivalent to using the current time).
1.96      xsa      1304:  * If <username> is NULL, set the author for this revision to the current user.
1.90      niallo   1305:  * Otherwise, set it to <username>.
1.52      jfb      1306:  * Returns 0 on success, or -1 on failure.
                   1307:  */
                   1308: int
1.90      niallo   1309: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1310:     const char *username)
1.52      jfb      1311: {
                   1312:        time_t now;
                   1313:        struct passwd *pw;
1.83      joris    1314:        struct rcs_delta *ordp, *rdp;
                   1315:
1.52      jfb      1316:        if (rev == RCS_HEAD_REV) {
1.101     niallo   1317:                if (rf->rf_flags & RCS_CREATE) {
                   1318:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1319:                                return (-1);
1.111     joris    1320:                        rf->rf_head = rcsnum_alloc();
1.101     niallo   1321:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1322:                } else {
                   1323:                        rev = rcsnum_inc(rf->rf_head);
                   1324:                }
1.83      joris    1325:        } else {
                   1326:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1327:                        rcs_errno = RCS_ERR_DUPENT;
                   1328:                        return (-1);
                   1329:                }
1.52      jfb      1330:        }
                   1331:
1.112     xsa      1332:        if ((pw = getpwuid(getuid())) == NULL)
                   1333:                fatal("getpwuid failed");
1.52      jfb      1334:
1.161     ray      1335:        rdp = xcalloc(1, sizeof(*rdp));
1.52      jfb      1336:
                   1337:        TAILQ_INIT(&(rdp->rd_branches));
                   1338:
1.111     joris    1339:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1340:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1341:
1.111     joris    1342:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1343:
1.143     niallo   1344:        if (!(rf->rf_flags & RCS_CREATE)) {
                   1345:                /* next should point to the previous HEAD */
                   1346:                ordp = TAILQ_FIRST(&(rf->rf_delta));
                   1347:                rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
                   1348:        }
1.101     niallo   1349:
1.52      jfb      1350:
1.90      niallo   1351:        if (username == NULL)
                   1352:                username = pw->pw_name;
                   1353:
1.110     joris    1354:        rdp->rd_author = xstrdup(username);
                   1355:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1356:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1357:
1.53      jfb      1358:        if (date != (time_t)(-1))
                   1359:                now = date;
                   1360:        else
                   1361:                time(&now);
1.52      jfb      1362:        gmtime_r(&now, &(rdp->rd_date));
                   1363:
                   1364:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1365:        rf->rf_ndelta++;
1.81      niallo   1366:
1.64      niallo   1367:        /* not synced anymore */
                   1368:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1369:
                   1370:        return (0);
                   1371: }
                   1372:
                   1373: /*
                   1374:  * rcs_rev_remove()
                   1375:  *
                   1376:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1377:  */
                   1378: int
                   1379: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1380: {
1.166     joris    1381:        size_t len;
                   1382:        char *tmpdir;
                   1383:        char *newdeltatext, path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
                   1384:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
                   1385:        BUF *nextbuf, *prevbuf, *newdiff;
                   1386:
                   1387: #if defined(RCSPROG)
                   1388:        tmpdir = rcs_tmpdir;
                   1389: #else
                   1390:        tmpdir = cvs_tmpdir;
                   1391: #endif
1.52      jfb      1392:
                   1393:        if (rev == RCS_HEAD_REV)
                   1394:                rev = rf->rf_head;
                   1395:
                   1396:        /* do we actually have that revision? */
                   1397:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1398:                rcs_errno = RCS_ERR_NOENT;
1.166     joris    1399:                return (-1);
                   1400:        }
                   1401:
                   1402:        /*
                   1403:         * This is confusing, the previous delta is next in the TAILQ list.
                   1404:         * the next delta is the previous one in the TAILQ list.
                   1405:         *
                   1406:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1407:         * When the first revision got specified, prevrdp will be NULL.
                   1408:         */
                   1409:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
                   1410:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
                   1411:
                   1412:        newdeltatext = NULL;
                   1413:        prevbuf = nextbuf = NULL;
                   1414:
                   1415:        if (prevrdp != NULL) {
                   1416:                if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
                   1417:                        fatal("error getting revision");
                   1418:        }
                   1419:
                   1420:        if ((prevrdp != NULL) && (nextrdp != NULL)) {
                   1421:                if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
                   1422:                        fatal("error getting revision");
                   1423:
                   1424:                newdiff = cvs_buf_alloc(64, BUF_AUTOEXT);
                   1425:
                   1426:                /* calculate new diff */
                   1427:                len = strlcpy(path_tmp1, tmpdir, sizeof(path_tmp1));
                   1428:                if (len >= sizeof(path_tmp1))
                   1429:                        fatal("path truncation in rcs_rev_remove");
                   1430:
                   1431:                len = strlcat(path_tmp1, "/diff1.XXXXXXXXXX",
                   1432:                    sizeof(path_tmp1));
                   1433:                if (len >= sizeof(path_tmp1))
                   1434:                        fatal("path truncation in rcs_rev_remove");
                   1435:
                   1436:                cvs_buf_write_stmp(nextbuf, path_tmp1, 0600);
                   1437:                cvs_buf_free(nextbuf);
                   1438:
                   1439:                len = strlcpy(path_tmp2, tmpdir, sizeof(path_tmp2));
                   1440:                if (len >= sizeof(path_tmp2))
                   1441:                        fatal("path truncation in rcs_rev_remove");
                   1442:
                   1443:                len = strlcat(path_tmp2, "/diff2.XXXXXXXXXX",
                   1444:                    sizeof(path_tmp2));
                   1445:                if (len >= sizeof(path_tmp2))
                   1446:                        fatal("path truncation in rcs_rev_remove");
                   1447:
                   1448:                cvs_buf_write_stmp(prevbuf, path_tmp2, 0600);
                   1449:                cvs_buf_free(prevbuf);
                   1450:
                   1451:                diff_format = D_RCSDIFF;
                   1452:                cvs_diffreg(path_tmp1, path_tmp2, newdiff);
                   1453:
                   1454:                newdeltatext = cvs_buf_release(newdiff);
                   1455:        } else if ((nextrdp == NULL) && (prevrdp != NULL)) {
                   1456:                newdeltatext = cvs_buf_release(prevbuf);
                   1457:        }
                   1458:
                   1459:        if (newdeltatext != NULL) {
                   1460:                if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
                   1461:                        fatal("error setting new deltatext");
                   1462:        }
                   1463:
                   1464:        TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1465:
                   1466:        /* update pointers */
                   1467:        if ((prevrdp != NULL) && (nextrdp != NULL)) {
                   1468:                rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
                   1469:        } else if (prevrdp != NULL) {
                   1470:                rcs_head_set(rf, prevrdp->rd_num);
                   1471:        } else if (nextrdp != NULL) {
                   1472:                rcsnum_free(nextrdp->rd_next);
                   1473:                nextrdp->rd_next = rcsnum_alloc();
1.52      jfb      1474:        } else {
1.166     joris    1475:                rcsnum_free(rf->rf_head);
                   1476:                rf->rf_head = NULL;
1.52      jfb      1477:        }
                   1478:
1.166     joris    1479:        rf->rf_ndelta--;
                   1480:        rf->rf_flags &= ~RCS_SYNCED;
                   1481:
                   1482:        rcs_freedelta(rdp);
                   1483:
                   1484:        if (newdeltatext != NULL)
                   1485:                xfree(newdeltatext);
1.52      jfb      1486:
1.166     joris    1487:        return (0);
1.52      jfb      1488: }
                   1489:
                   1490: /*
1.1       jfb      1491:  * rcs_findrev()
                   1492:  *
                   1493:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1494:  * The revision number is given in <rev>.
1.156     joris    1495:  *
                   1496:  * If the given revision is a branch number, we translate it into the latest
                   1497:  * revision on the branch.
                   1498:  *
1.1       jfb      1499:  * Returns a pointer to the delta on success, or NULL on failure.
                   1500:  */
1.102     xsa      1501: struct rcs_delta *
1.117     niallo   1502: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1       jfb      1503: {
                   1504:        u_int cmplen;
1.156     joris    1505:        struct rcs_delta *rdp;
                   1506:        RCSNUM *brev, *frev;
1.26      jfb      1507:
1.117     niallo   1508:        /*
                   1509:         * We need to do more parsing if the last revision in the linked list
                   1510:         * is greater than the requested revision.
                   1511:         */
1.156     joris    1512:        rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
                   1513:        if ((rdp == NULL)
                   1514:            || (rcsnum_cmp(rdp->rd_num, rev, 0) == -1)) {
1.117     niallo   1515:                rcs_parse_deltas(rfp, rev);
                   1516:        }
                   1517:
1.156     joris    1518:        /*
                   1519:         * Translate a branch into the latest revision on the branch itself.
                   1520:         */
                   1521:        if (RCSNUM_ISBRANCH(rev)) {
                   1522:                brev = rcsnum_brtorev(rev);
                   1523:                frev = brev;
                   1524:                for (;;) {
                   1525:                        rdp = rcs_findrev(rfp, frev);
                   1526:                        if (rdp == NULL)
1.158     joris    1527:                                return (NULL);
1.1       jfb      1528:
1.156     joris    1529:                        if (rdp->rd_next->rn_len == 0)
1.6       vincent  1530:                                break;
1.156     joris    1531:
                   1532:                        frev = rdp->rd_next;
1.1       jfb      1533:                }
1.156     joris    1534:
                   1535:                rcsnum_free(brev);
                   1536:                return (rdp);
                   1537:        }
                   1538:
                   1539:        cmplen = rev->rn_len;
                   1540:
                   1541:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1542:                if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0)
                   1543:                        return (rdp);
                   1544:        }
1.1       jfb      1545:
                   1546:        return (NULL);
1.20      jfb      1547: }
                   1548:
                   1549: /*
1.26      jfb      1550:  * rcs_kwexp_set()
                   1551:  *
                   1552:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1553:  */
1.162     xsa      1554: void
1.26      jfb      1555: rcs_kwexp_set(RCSFILE *file, int mode)
                   1556: {
                   1557:        int i;
                   1558:        char *tmp, buf[8] = "";
                   1559:
                   1560:        if (RCS_KWEXP_INVAL(mode))
1.162     xsa      1561:                return;
1.26      jfb      1562:
                   1563:        i = 0;
                   1564:        if (mode == RCS_KWEXP_NONE)
                   1565:                buf[0] = 'b';
                   1566:        else if (mode == RCS_KWEXP_OLD)
                   1567:                buf[0] = 'o';
                   1568:        else {
                   1569:                if (mode & RCS_KWEXP_NAME)
                   1570:                        buf[i++] = 'k';
                   1571:                if (mode & RCS_KWEXP_VAL)
                   1572:                        buf[i++] = 'v';
                   1573:                if (mode & RCS_KWEXP_LKR)
                   1574:                        buf[i++] = 'l';
                   1575:        }
                   1576:
1.110     joris    1577:        tmp = xstrdup(buf);
1.27      jfb      1578:        if (file->rf_expand != NULL)
1.147     ray      1579:                xfree(file->rf_expand);
1.26      jfb      1580:        file->rf_expand = tmp;
1.64      niallo   1581:        /* not synced anymore */
                   1582:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1583: }
                   1584:
                   1585: /*
                   1586:  * rcs_kwexp_get()
                   1587:  *
                   1588:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1589:  */
                   1590: int
                   1591: rcs_kwexp_get(RCSFILE *file)
                   1592: {
                   1593:        return rcs_kflag_get(file->rf_expand);
                   1594: }
                   1595:
                   1596: /*
1.20      jfb      1597:  * rcs_kflag_get()
                   1598:  *
                   1599:  * Get the keyword expansion mode from a set of character flags given in
                   1600:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1601:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1602:  */
                   1603: int
                   1604: rcs_kflag_get(const char *flags)
                   1605: {
                   1606:        int fl;
                   1607:        size_t len;
                   1608:        const char *fp;
                   1609:
                   1610:        fl = 0;
                   1611:        len = strlen(flags);
                   1612:
                   1613:        for (fp = flags; *fp != '\0'; fp++) {
                   1614:                if (*fp == 'k')
                   1615:                        fl |= RCS_KWEXP_NAME;
                   1616:                else if (*fp == 'v')
                   1617:                        fl |= RCS_KWEXP_VAL;
                   1618:                else if (*fp == 'l')
                   1619:                        fl |= RCS_KWEXP_LKR;
                   1620:                else if (*fp == 'o') {
                   1621:                        if (len != 1)
                   1622:                                fl |= RCS_KWEXP_ERR;
                   1623:                        fl |= RCS_KWEXP_OLD;
                   1624:                } else if (*fp == 'b') {
                   1625:                        if (len != 1)
                   1626:                                fl |= RCS_KWEXP_ERR;
                   1627:                } else  /* unknown letter */
                   1628:                        fl |= RCS_KWEXP_ERR;
                   1629:        }
                   1630:
                   1631:        return (fl);
1.32      jfb      1632: }
                   1633:
                   1634: /*
                   1635:  * rcs_errstr()
                   1636:  *
                   1637:  * Get the error string matching the RCS error code <code>.
                   1638:  */
1.57      xsa      1639: const char *
1.32      jfb      1640: rcs_errstr(int code)
                   1641: {
1.50      jfb      1642:        const char *esp;
                   1643:
                   1644:        if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
                   1645:                esp = NULL;
                   1646:        else if (code == RCS_ERR_ERRNO)
                   1647:                esp = strerror(errno);
                   1648:        else
                   1649:                esp = rcs_errstrs[code];
                   1650:        return (esp);
1.1       jfb      1651: }
                   1652:
1.117     niallo   1653: /* rcs_parse_deltas()
                   1654:  *
                   1655:  * Parse deltas. If <rev> is not NULL, parse only as far as that
                   1656:  * revision. If <rev> is NULL, parse all deltas.
                   1657:  */
1.146     xsa      1658: static void
1.117     niallo   1659: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
                   1660: {
                   1661:        int ret;
                   1662:        struct rcs_delta *enddelta;
1.146     xsa      1663:
                   1664:        if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
                   1665:                return;
                   1666:
1.117     niallo   1667:        for (;;) {
                   1668:                ret = rcs_parse_delta(rfp);
                   1669:                if (rev != NULL) {
                   1670:                        enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.155     ray      1671:                        if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1.117     niallo   1672:                                break;
                   1673:                }
                   1674:                if (ret == 0) {
                   1675:                        rfp->rf_flags |= PARSED_DELTAS;
                   1676:                        break;
                   1677:                }
                   1678:                else if (ret == -1)
                   1679:                        fatal("error parsing deltas");
                   1680:        }
                   1681: }
                   1682:
                   1683: /* rcs_parse_deltatexts()
                   1684:  *
                   1685:  * Parse deltatexts. If <rev> is not NULL, parse only as far as that
                   1686:  * revision. If <rev> is NULL, parse everything.
                   1687:  */
1.146     xsa      1688: static void
1.117     niallo   1689: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
                   1690: {
                   1691:        int ret;
                   1692:        struct rcs_delta *rdp;
1.146     xsa      1693:
1.117     niallo   1694:        if ((rfp->rf_flags & PARSED_DELTATEXTS)
                   1695:            || (rfp->rf_flags & RCS_CREATE))
1.146     xsa      1696:                return;
                   1697:
1.117     niallo   1698:        if (!(rfp->rf_flags & PARSED_DESC))
                   1699:                rcs_parse_desc(rfp, rev);
                   1700:        for (;;) {
                   1701:                if (rev != NULL) {
                   1702:                        rdp = rcs_findrev(rfp, rev);
                   1703:                        if (rdp->rd_text != NULL)
                   1704:                                break;
                   1705:                        else
                   1706:                                ret = rcs_parse_deltatext(rfp);
                   1707:                } else
                   1708:                        ret = rcs_parse_deltatext(rfp);
                   1709:                if (ret == 0) {
                   1710:                        rfp->rf_flags |= PARSED_DELTATEXTS;
                   1711:                        break;
                   1712:                }
1.146     xsa      1713:                else if (ret == -1)
1.117     niallo   1714:                        fatal("problem parsing deltatexts");
                   1715:        }
                   1716: }
                   1717:
                   1718: /* rcs_parse_desc()
                   1719:  *
                   1720:  * Parse RCS description.
                   1721:  */
1.148     xsa      1722: static void
1.117     niallo   1723: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
                   1724: {
                   1725:        int ret = 0;
1.148     xsa      1726:
                   1727:        if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
                   1728:                return;
1.122     reyk     1729:        if (!(rfp->rf_flags & PARSED_DELTAS))
1.117     niallo   1730:                rcs_parse_deltas(rfp, rev);
                   1731:        /* do parsing */
                   1732:        ret = rcs_gettok(rfp);
1.148     xsa      1733:        if (ret != RCS_TOK_DESC)
                   1734:                fatal("token `%s' found where RCS desc expected",
1.117     niallo   1735:                    RCS_TOKSTR(rfp));
                   1736:
                   1737:        ret = rcs_gettok(rfp);
1.148     xsa      1738:        if (ret != RCS_TOK_STRING)
                   1739:                fatal("token `%s' found where RCS desc expected",
1.117     niallo   1740:                    RCS_TOKSTR(rfp));
                   1741:
                   1742:        rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
                   1743:        rfp->rf_flags |= PARSED_DESC;
                   1744: }
                   1745:
1.1       jfb      1746: /*
1.117     niallo   1747:  * rcs_parse_init()
1.1       jfb      1748:  *
1.117     niallo   1749:  * Initial parsing of file <path>, which are in the RCS format.
1.167   ! ray      1750:  * Just does admin section.
1.1       jfb      1751:  */
1.167   ! ray      1752: static void
1.117     niallo   1753: rcs_parse_init(RCSFILE *rfp)
1.1       jfb      1754: {
                   1755:        struct rcs_pdata *pdp;
                   1756:
1.26      jfb      1757:        if (rfp->rf_flags & RCS_PARSED)
1.167   ! ray      1758:                return;
1.1       jfb      1759:
1.161     ray      1760:        pdp = xcalloc(1, sizeof(*pdp));
1.1       jfb      1761:
1.18      jfb      1762:        pdp->rp_lines = 0;
1.1       jfb      1763:        pdp->rp_pttype = RCS_TOK_ERR;
                   1764:
1.113     xsa      1765:        if ((pdp->rp_file = fopen(rfp->rf_path, "r")) == NULL)
                   1766:                fatal("fopen: `%s': %s", rfp->rf_path, strerror(errno));
1.1       jfb      1767:
1.161     ray      1768:        pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1.1       jfb      1769:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1770:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1771:
                   1772:        /* ditch the strict lock */
1.26      jfb      1773:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1774:        rfp->rf_pdata = pdp;
                   1775:
1.167   ! ray      1776:        if (rcs_parse_admin(rfp) < 0) {
1.1       jfb      1777:                rcs_freepdata(pdp);
1.117     niallo   1778:                fatal("could not parse admin data");
1.1       jfb      1779:        }
                   1780:
1.117     niallo   1781:        if (rfp->rf_flags & RCS_PARSE_FULLY)
                   1782:                rcs_parse_deltatexts(rfp, NULL);
1.1       jfb      1783:
1.117     niallo   1784:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb      1785: }
                   1786:
                   1787: /*
                   1788:  * rcs_parse_admin()
                   1789:  *
                   1790:  * Parse the administrative portion of an RCS file.
1.31      jfb      1791:  * Returns the type of the first token found after the admin section on
                   1792:  * success, or -1 on failure.
1.1       jfb      1793:  */
                   1794: static int
                   1795: rcs_parse_admin(RCSFILE *rfp)
                   1796: {
                   1797:        u_int i;
                   1798:        int tok, ntok, hmask;
                   1799:        struct rcs_key *rk;
                   1800:
                   1801:        /* hmask is a mask of the headers already encountered */
                   1802:        hmask = 0;
                   1803:        for (;;) {
                   1804:                tok = rcs_gettok(rfp);
                   1805:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1806:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1807:                        cvs_log(LP_ERR, "parse error in RCS admin section");
1.147     ray      1808:                        goto fail;
1.31      jfb      1809:                } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
                   1810:                        /*
                   1811:                         * Assume this is the start of the first delta or
                   1812:                         * that we are dealing with an empty RCS file and
                   1813:                         * we just found the description.
                   1814:                         */
1.1       jfb      1815:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31      jfb      1816:                        return (tok);
1.1       jfb      1817:                }
                   1818:
                   1819:                rk = NULL;
1.18      jfb      1820:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1821:                        if (rcs_keys[i].rk_id == tok)
                   1822:                                rk = &(rcs_keys[i]);
                   1823:
                   1824:                if (hmask & (1 << tok)) {
1.50      jfb      1825:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1826:                        cvs_log(LP_ERR, "duplicate RCS key");
1.147     ray      1827:                        goto fail;
1.1       jfb      1828:                }
                   1829:                hmask |= (1 << tok);
                   1830:
                   1831:                switch (tok) {
                   1832:                case RCS_TOK_HEAD:
                   1833:                case RCS_TOK_BRANCH:
                   1834:                case RCS_TOK_COMMENT:
                   1835:                case RCS_TOK_EXPAND:
                   1836:                        ntok = rcs_gettok(rfp);
                   1837:                        if (ntok == RCS_TOK_SCOLON)
                   1838:                                break;
                   1839:                        if (ntok != rk->rk_val) {
1.50      jfb      1840:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1841:                                cvs_log(LP_ERR,
                   1842:                                    "invalid value type for RCS key `%s'",
                   1843:                                    rk->rk_str);
                   1844:                        }
                   1845:
                   1846:                        if (tok == RCS_TOK_HEAD) {
1.111     joris    1847:                                if (rfp->rf_head == NULL)
1.28      jfb      1848:                                        rfp->rf_head = rcsnum_alloc();
1.1       jfb      1849:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1850:                                    rfp->rf_head);
1.14      deraadt  1851:                        } else if (tok == RCS_TOK_BRANCH) {
1.111     joris    1852:                                if (rfp->rf_branch == NULL)
1.35      jfb      1853:                                        rfp->rf_branch = rcsnum_alloc();
                   1854:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1855:                                    rfp->rf_branch) < 0)
1.147     ray      1856:                                        goto fail;
1.14      deraadt  1857:                        } else if (tok == RCS_TOK_COMMENT) {
1.110     joris    1858:                                rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14      deraadt  1859:                        } else if (tok == RCS_TOK_EXPAND) {
1.110     joris    1860:                                rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1861:                        }
                   1862:
                   1863:                        /* now get the expected semi-colon */
                   1864:                        ntok = rcs_gettok(rfp);
                   1865:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1866:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1867:                                cvs_log(LP_ERR,
                   1868:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1869:                                    rk->rk_str);
1.147     ray      1870:                                goto fail;
1.1       jfb      1871:                        }
                   1872:                        break;
                   1873:                case RCS_TOK_ACCESS:
1.29      jfb      1874:                        if (rcs_parse_access(rfp) < 0)
1.147     ray      1875:                                goto fail;
1.1       jfb      1876:                        break;
                   1877:                case RCS_TOK_SYMBOLS:
1.29      jfb      1878:                        if (rcs_parse_symbols(rfp) < 0)
1.147     ray      1879:                                goto fail;
1.1       jfb      1880:                        break;
                   1881:                case RCS_TOK_LOCKS:
1.29      jfb      1882:                        if (rcs_parse_locks(rfp) < 0)
1.147     ray      1883:                                goto fail;
1.1       jfb      1884:                        break;
                   1885:                default:
1.50      jfb      1886:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1887:                        cvs_log(LP_ERR,
                   1888:                            "unexpected token `%s' in RCS admin section",
                   1889:                            RCS_TOKSTR(rfp));
1.147     ray      1890:                        goto fail;
1.1       jfb      1891:                }
                   1892:        }
                   1893:
1.147     ray      1894: fail:
                   1895:        return (-1);
1.1       jfb      1896: }
                   1897:
                   1898: /*
                   1899:  * rcs_parse_delta()
                   1900:  *
                   1901:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1902:  * information in the <rfp> delta list.
                   1903:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1904:  * -1 on error.
                   1905:  */
                   1906: static int
                   1907: rcs_parse_delta(RCSFILE *rfp)
                   1908: {
                   1909:        int ret, tok, ntok, hmask;
                   1910:        u_int i;
                   1911:        char *tokstr;
1.3       vincent  1912:        RCSNUM *datenum;
1.1       jfb      1913:        struct rcs_delta *rdp;
                   1914:        struct rcs_key *rk;
                   1915:
1.161     ray      1916:        rdp = xcalloc(1, sizeof(*rdp));
1.1       jfb      1917:
                   1918:        rdp->rd_num = rcsnum_alloc();
                   1919:        rdp->rd_next = rcsnum_alloc();
                   1920:
                   1921:        TAILQ_INIT(&(rdp->rd_branches));
                   1922:
                   1923:        tok = rcs_gettok(rfp);
1.154     ray      1924:        if (tok == RCS_TOK_DESC) {
                   1925:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1926:                return (0);
                   1927:        } else if (tok != RCS_TOK_NUM) {
1.52      jfb      1928:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1929:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1930:                    RCS_TOKSTR(rfp));
                   1931:                rcs_freedelta(rdp);
                   1932:                return (-1);
                   1933:        }
                   1934:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1935:
                   1936:        hmask = 0;
                   1937:        ret = 0;
                   1938:        tokstr = NULL;
                   1939:
                   1940:        for (;;) {
                   1941:                tok = rcs_gettok(rfp);
                   1942:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1943:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1944:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1945:                        rcs_freedelta(rdp);
                   1946:                        return (-1);
1.14      deraadt  1947:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1948:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1949:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1950:                        break;
                   1951:                }
                   1952:
                   1953:                rk = NULL;
1.18      jfb      1954:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1955:                        if (rcs_keys[i].rk_id == tok)
                   1956:                                rk = &(rcs_keys[i]);
                   1957:
                   1958:                if (hmask & (1 << tok)) {
1.50      jfb      1959:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1960:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1961:                        rcs_freedelta(rdp);
                   1962:                        return (-1);
                   1963:                }
                   1964:                hmask |= (1 << tok);
                   1965:
                   1966:                switch (tok) {
                   1967:                case RCS_TOK_DATE:
                   1968:                case RCS_TOK_AUTHOR:
                   1969:                case RCS_TOK_STATE:
                   1970:                case RCS_TOK_NEXT:
                   1971:                        ntok = rcs_gettok(rfp);
                   1972:                        if (ntok == RCS_TOK_SCOLON) {
                   1973:                                if (rk->rk_flags & RCS_VOPT)
                   1974:                                        break;
                   1975:                                else {
1.50      jfb      1976:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1977:                                        cvs_log(LP_ERR, "missing mandatory "
                   1978:                                            "value to RCS key `%s'",
                   1979:                                            rk->rk_str);
                   1980:                                        rcs_freedelta(rdp);
                   1981:                                        return (-1);
                   1982:                                }
                   1983:                        }
                   1984:
                   1985:                        if (ntok != rk->rk_val) {
1.50      jfb      1986:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1987:                                cvs_log(LP_ERR,
                   1988:                                    "invalid value type for RCS key `%s'",
                   1989:                                    rk->rk_str);
                   1990:                                rcs_freedelta(rdp);
                   1991:                                return (-1);
                   1992:                        }
                   1993:
                   1994:                        if (tokstr != NULL)
1.110     joris    1995:                                xfree(tokstr);
                   1996:                        tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1997:                        /* now get the expected semi-colon */
                   1998:                        ntok = rcs_gettok(rfp);
                   1999:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      2000:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2001:                                cvs_log(LP_ERR,
                   2002:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      2003:                                    rk->rk_str);
1.110     joris    2004:                                xfree(tokstr);
1.1       jfb      2005:                                rcs_freedelta(rdp);
                   2006:                                return (-1);
                   2007:                        }
                   2008:
                   2009:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      2010:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110     joris    2011:                                        xfree(tokstr);
1.11      joris    2012:                                        rcs_freedelta(rdp);
                   2013:                                        return (-1);
                   2014:                                }
1.3       vincent  2015:                                if (datenum->rn_len != 6) {
1.50      jfb      2016:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2017:                                        cvs_log(LP_ERR,
                   2018:                                            "RCS date specification has %s "
                   2019:                                            "fields",
1.3       vincent  2020:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      2021:                                            "missing");
1.110     joris    2022:                                        xfree(tokstr);
1.1       jfb      2023:                                        rcs_freedelta(rdp);
1.37      tedu     2024:                                        rcsnum_free(datenum);
                   2025:                                        return (-1);
1.1       jfb      2026:                                }
1.3       vincent  2027:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      2028:                                if (rdp->rd_date.tm_year >= 1900)
                   2029:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  2030:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   2031:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   2032:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   2033:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   2034:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   2035:                                rcsnum_free(datenum);
1.14      deraadt  2036:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      2037:                                rdp->rd_author = tokstr;
                   2038:                                tokstr = NULL;
1.14      deraadt  2039:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      2040:                                rdp->rd_state = tokstr;
                   2041:                                tokstr = NULL;
1.14      deraadt  2042:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      2043:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   2044:                        }
                   2045:                        break;
                   2046:                case RCS_TOK_BRANCHES:
1.46      jfb      2047:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   2048:                                rcs_freedelta(rdp);
                   2049:                                return (-1);
                   2050:                        }
1.1       jfb      2051:                        break;
                   2052:                default:
1.50      jfb      2053:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2054:                        cvs_log(LP_ERR,
                   2055:                            "unexpected token `%s' in RCS delta",
                   2056:                            RCS_TOKSTR(rfp));
                   2057:                        rcs_freedelta(rdp);
                   2058:                        return (-1);
                   2059:                }
                   2060:        }
                   2061:
1.13      jfb      2062:        if (tokstr != NULL)
1.110     joris    2063:                xfree(tokstr);
1.13      jfb      2064:
1.1       jfb      2065:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      2066:        rfp->rf_ndelta++;
1.1       jfb      2067:
                   2068:        return (ret);
                   2069: }
                   2070:
                   2071: /*
                   2072:  * rcs_parse_deltatext()
                   2073:  *
                   2074:  * Parse an RCS delta text section and fill in the log and text field of the
                   2075:  * appropriate delta section.
                   2076:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   2077:  * -1 on error.
                   2078:  */
                   2079: static int
                   2080: rcs_parse_deltatext(RCSFILE *rfp)
                   2081: {
                   2082:        int tok;
                   2083:        RCSNUM *tnum;
                   2084:        struct rcs_delta *rdp;
                   2085:
                   2086:        tok = rcs_gettok(rfp);
                   2087:        if (tok == RCS_TOK_EOF)
                   2088:                return (0);
                   2089:
                   2090:        if (tok != RCS_TOK_NUM) {
1.50      jfb      2091:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2092:                cvs_log(LP_ERR,
                   2093:                    "unexpected token `%s' at start of RCS delta text",
                   2094:                    RCS_TOKSTR(rfp));
                   2095:                return (-1);
                   2096:        }
1.13      jfb      2097:
                   2098:        tnum = rcsnum_alloc();
1.1       jfb      2099:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   2100:
                   2101:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2102:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   2103:                        break;
                   2104:        }
1.13      jfb      2105:        rcsnum_free(tnum);
                   2106:
1.1       jfb      2107:        if (rdp == NULL) {
                   2108:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   2109:                    RCS_TOKSTR(rfp));
                   2110:                return (-1);
                   2111:        }
                   2112:
                   2113:        tok = rcs_gettok(rfp);
                   2114:        if (tok != RCS_TOK_LOG) {
1.50      jfb      2115:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2116:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2117:                    RCS_TOKSTR(rfp));
                   2118:                return (-1);
                   2119:        }
                   2120:
                   2121:        tok = rcs_gettok(rfp);
                   2122:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2123:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2124:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2125:                    RCS_TOKSTR(rfp));
                   2126:                return (-1);
                   2127:        }
1.110     joris    2128:        rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2129:        tok = rcs_gettok(rfp);
                   2130:        if (tok != RCS_TOK_TEXT) {
1.50      jfb      2131:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2132:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2133:                    RCS_TOKSTR(rfp));
                   2134:                return (-1);
                   2135:        }
                   2136:
                   2137:        tok = rcs_gettok(rfp);
                   2138:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2139:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2140:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2141:                    RCS_TOKSTR(rfp));
                   2142:                return (-1);
                   2143:        }
                   2144:
1.161     ray      2145:        rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.74      joris    2146:        strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42      jfb      2147:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2148:
                   2149:        return (1);
                   2150: }
                   2151:
                   2152: /*
                   2153:  * rcs_parse_access()
                   2154:  *
                   2155:  * Parse the access list given as value to the `access' keyword.
                   2156:  * Returns 0 on success, or -1 on failure.
                   2157:  */
                   2158: static int
                   2159: rcs_parse_access(RCSFILE *rfp)
                   2160: {
                   2161:        int type;
                   2162:
                   2163:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2164:                if (type != RCS_TOK_ID) {
1.50      jfb      2165:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2166:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2167:                            RCS_TOKSTR(rfp));
                   2168:                        return (-1);
                   2169:                }
1.29      jfb      2170:
                   2171:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2172:                        return (-1);
1.1       jfb      2173:        }
                   2174:
                   2175:        return (0);
                   2176: }
                   2177:
                   2178: /*
                   2179:  * rcs_parse_symbols()
                   2180:  *
                   2181:  * Parse the symbol list given as value to the `symbols' keyword.
                   2182:  * Returns 0 on success, or -1 on failure.
                   2183:  */
                   2184: static int
                   2185: rcs_parse_symbols(RCSFILE *rfp)
                   2186: {
                   2187:        int type;
                   2188:        struct rcs_sym *symp;
                   2189:
                   2190:        for (;;) {
                   2191:                type = rcs_gettok(rfp);
                   2192:                if (type == RCS_TOK_SCOLON)
                   2193:                        break;
                   2194:
1.41      jfb      2195:                if (type != RCS_TOK_ID) {
1.50      jfb      2196:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2197:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2198:                            RCS_TOKSTR(rfp));
                   2199:                        return (-1);
                   2200:                }
                   2201:
1.161     ray      2202:                symp = xmalloc(sizeof(*symp));
1.110     joris    2203:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2204:                symp->rs_num = rcsnum_alloc();
                   2205:
                   2206:                type = rcs_gettok(rfp);
                   2207:                if (type != RCS_TOK_COLON) {
1.50      jfb      2208:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2209:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2210:                            RCS_TOKSTR(rfp));
1.11      joris    2211:                        rcsnum_free(symp->rs_num);
1.110     joris    2212:                        xfree(symp->rs_name);
                   2213:                        xfree(symp);
1.1       jfb      2214:                        return (-1);
                   2215:                }
                   2216:
                   2217:                type = rcs_gettok(rfp);
                   2218:                if (type != RCS_TOK_NUM) {
1.50      jfb      2219:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2220:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2221:                            RCS_TOKSTR(rfp));
1.11      joris    2222:                        rcsnum_free(symp->rs_num);
1.110     joris    2223:                        xfree(symp->rs_name);
                   2224:                        xfree(symp);
1.1       jfb      2225:                        return (-1);
                   2226:                }
                   2227:
                   2228:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2229:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2230:                            RCS_TOKSTR(rfp));
1.11      joris    2231:                        rcsnum_free(symp->rs_num);
1.110     joris    2232:                        xfree(symp->rs_name);
                   2233:                        xfree(symp);
1.1       jfb      2234:                        return (-1);
                   2235:                }
                   2236:
1.43      jfb      2237:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2238:        }
                   2239:
                   2240:        return (0);
                   2241: }
                   2242:
                   2243: /*
                   2244:  * rcs_parse_locks()
                   2245:  *
                   2246:  * Parse the lock list given as value to the `locks' keyword.
                   2247:  * Returns 0 on success, or -1 on failure.
                   2248:  */
                   2249: static int
                   2250: rcs_parse_locks(RCSFILE *rfp)
                   2251: {
                   2252:        int type;
                   2253:        struct rcs_lock *lkp;
                   2254:
                   2255:        for (;;) {
                   2256:                type = rcs_gettok(rfp);
                   2257:                if (type == RCS_TOK_SCOLON)
                   2258:                        break;
                   2259:
                   2260:                if (type != RCS_TOK_ID) {
1.50      jfb      2261:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2262:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2263:                            RCS_TOKSTR(rfp));
                   2264:                        return (-1);
                   2265:                }
                   2266:
1.161     ray      2267:                lkp = xmalloc(sizeof(*lkp));
1.110     joris    2268:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2269:                lkp->rl_num = rcsnum_alloc();
                   2270:
                   2271:                type = rcs_gettok(rfp);
                   2272:                if (type != RCS_TOK_COLON) {
1.50      jfb      2273:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2274:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2275:                            RCS_TOKSTR(rfp));
1.37      tedu     2276:                        rcsnum_free(lkp->rl_num);
1.110     joris    2277:                        xfree(lkp->rl_name);
                   2278:                        xfree(lkp);
1.1       jfb      2279:                        return (-1);
                   2280:                }
                   2281:
                   2282:                type = rcs_gettok(rfp);
                   2283:                if (type != RCS_TOK_NUM) {
1.50      jfb      2284:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2285:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2286:                            RCS_TOKSTR(rfp));
1.37      tedu     2287:                        rcsnum_free(lkp->rl_num);
1.110     joris    2288:                        xfree(lkp->rl_name);
                   2289:                        xfree(lkp);
1.1       jfb      2290:                        return (-1);
                   2291:                }
                   2292:
                   2293:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2294:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2295:                            RCS_TOKSTR(rfp));
1.37      tedu     2296:                        rcsnum_free(lkp->rl_num);
1.110     joris    2297:                        xfree(lkp->rl_name);
                   2298:                        xfree(lkp);
1.1       jfb      2299:                        return (-1);
                   2300:                }
                   2301:
                   2302:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2303:        }
                   2304:
                   2305:        /* check if we have a `strict' */
                   2306:        type = rcs_gettok(rfp);
                   2307:        if (type != RCS_TOK_STRICT) {
                   2308:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2309:        } else {
1.26      jfb      2310:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2311:
                   2312:                type = rcs_gettok(rfp);
                   2313:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2314:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2315:                        cvs_log(LP_ERR,
                   2316:                            "missing semi-colon after `strict' keyword");
                   2317:                        return (-1);
                   2318:                }
                   2319:        }
                   2320:
                   2321:        return (0);
                   2322: }
                   2323:
                   2324: /*
                   2325:  * rcs_parse_branches()
                   2326:  *
                   2327:  * Parse the list of branches following a `branches' keyword in a delta.
                   2328:  * Returns 0 on success, or -1 on failure.
                   2329:  */
                   2330: static int
                   2331: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2332: {
                   2333:        int type;
                   2334:        struct rcs_branch *brp;
                   2335:
                   2336:        for (;;) {
                   2337:                type = rcs_gettok(rfp);
                   2338:                if (type == RCS_TOK_SCOLON)
                   2339:                        break;
                   2340:
                   2341:                if (type != RCS_TOK_NUM) {
1.50      jfb      2342:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2343:                        cvs_log(LP_ERR,
                   2344:                            "unexpected token `%s' in list of branches",
                   2345:                            RCS_TOKSTR(rfp));
                   2346:                        return (-1);
                   2347:                }
                   2348:
1.161     ray      2349:                brp = xmalloc(sizeof(*brp));
1.46      jfb      2350:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2351:                if (brp->rb_num == NULL) {
1.110     joris    2352:                        xfree(brp);
1.11      joris    2353:                        return (-1);
                   2354:                }
1.1       jfb      2355:
                   2356:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2357:        }
                   2358:
                   2359:        return (0);
                   2360: }
                   2361:
                   2362: /*
                   2363:  * rcs_freedelta()
                   2364:  *
                   2365:  * Free the contents of a delta structure.
                   2366:  */
1.18      jfb      2367: static void
1.1       jfb      2368: rcs_freedelta(struct rcs_delta *rdp)
                   2369: {
1.12      jfb      2370:        struct rcs_branch *rb;
1.1       jfb      2371:
1.12      jfb      2372:        if (rdp->rd_num != NULL)
                   2373:                rcsnum_free(rdp->rd_num);
                   2374:        if (rdp->rd_next != NULL)
                   2375:                rcsnum_free(rdp->rd_next);
                   2376:
1.1       jfb      2377:        if (rdp->rd_author != NULL)
1.110     joris    2378:                xfree(rdp->rd_author);
1.109     joris    2379:        if (rdp->rd_locker != NULL)
1.110     joris    2380:                xfree(rdp->rd_locker);
1.1       jfb      2381:        if (rdp->rd_state != NULL)
1.110     joris    2382:                xfree(rdp->rd_state);
1.1       jfb      2383:        if (rdp->rd_log != NULL)
1.110     joris    2384:                xfree(rdp->rd_log);
1.1       jfb      2385:        if (rdp->rd_text != NULL)
1.110     joris    2386:                xfree(rdp->rd_text);
1.12      jfb      2387:
                   2388:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2389:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2390:                rcsnum_free(rb->rb_num);
1.110     joris    2391:                xfree(rb);
1.1       jfb      2392:        }
                   2393:
1.110     joris    2394:        xfree(rdp);
1.1       jfb      2395: }
                   2396:
                   2397: /*
                   2398:  * rcs_freepdata()
                   2399:  *
                   2400:  * Free the contents of the parser data structure.
                   2401:  */
                   2402: static void
                   2403: rcs_freepdata(struct rcs_pdata *pd)
                   2404: {
                   2405:        if (pd->rp_file != NULL)
                   2406:                (void)fclose(pd->rp_file);
                   2407:        if (pd->rp_buf != NULL)
1.110     joris    2408:                xfree(pd->rp_buf);
                   2409:        xfree(pd);
1.1       jfb      2410: }
                   2411:
                   2412: /*
                   2413:  * rcs_gettok()
                   2414:  *
                   2415:  * Get the next RCS token from the string <str>.
                   2416:  */
                   2417: static int
                   2418: rcs_gettok(RCSFILE *rfp)
                   2419: {
                   2420:        u_int i;
                   2421:        int ch, last, type;
1.18      jfb      2422:        size_t len;
                   2423:        char *bp;
1.1       jfb      2424:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2425:
                   2426:        type = RCS_TOK_ERR;
                   2427:        bp = pdp->rp_buf;
1.42      jfb      2428:        pdp->rp_tlen = 0;
1.1       jfb      2429:        *bp = '\0';
                   2430:
                   2431:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2432:                type = pdp->rp_pttype;
                   2433:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   2434:                pdp->rp_pttype = RCS_TOK_ERR;
                   2435:                return (type);
                   2436:        }
                   2437:
                   2438:        /* skip leading whitespace */
                   2439:        /* XXX we must skip backspace too for compatibility, should we? */
                   2440:        do {
                   2441:                ch = getc(pdp->rp_file);
                   2442:                if (ch == '\n')
1.18      jfb      2443:                        pdp->rp_lines++;
1.1       jfb      2444:        } while (isspace(ch));
                   2445:
                   2446:        if (ch == EOF) {
                   2447:                type = RCS_TOK_EOF;
1.14      deraadt  2448:        } else if (ch == ';') {
1.1       jfb      2449:                type = RCS_TOK_SCOLON;
1.14      deraadt  2450:        } else if (ch == ':') {
1.1       jfb      2451:                type = RCS_TOK_COLON;
1.14      deraadt  2452:        } else if (isalpha(ch)) {
1.31      jfb      2453:                type = RCS_TOK_ID;
1.1       jfb      2454:                *(bp++) = ch;
1.18      jfb      2455:                for (;;) {
1.1       jfb      2456:                        ch = getc(pdp->rp_file);
1.160     joris    2457:                        if (!isalnum(ch) && ch != '_' && ch != '-' &&
                   2458:                            ch != '/') {
1.1       jfb      2459:                                ungetc(ch, pdp->rp_file);
                   2460:                                break;
                   2461:                        }
                   2462:                        *(bp++) = ch;
1.42      jfb      2463:                        pdp->rp_tlen++;
1.18      jfb      2464:                        if (bp == pdp->rp_bufend - 1) {
                   2465:                                len = bp - pdp->rp_buf;
1.151     xsa      2466:                                rcs_growbuf(rfp);
1.18      jfb      2467:                                bp = pdp->rp_buf + len;
                   2468:                        }
1.1       jfb      2469:                }
                   2470:                *bp = '\0';
                   2471:
1.18      jfb      2472:                if (type != RCS_TOK_ERR) {
                   2473:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2474:                                if (strcmp(rcs_keys[i].rk_str,
                   2475:                                    pdp->rp_buf) == 0) {
                   2476:                                        type = rcs_keys[i].rk_id;
                   2477:                                        break;
                   2478:                                }
1.1       jfb      2479:                        }
                   2480:                }
1.14      deraadt  2481:        } else if (ch == '@') {
1.1       jfb      2482:                /* we have a string */
1.18      jfb      2483:                type = RCS_TOK_STRING;
1.1       jfb      2484:                for (;;) {
                   2485:                        ch = getc(pdp->rp_file);
                   2486:                        if (ch == '@') {
                   2487:                                ch = getc(pdp->rp_file);
                   2488:                                if (ch != '@') {
                   2489:                                        ungetc(ch, pdp->rp_file);
                   2490:                                        break;
                   2491:                                }
1.14      deraadt  2492:                        } else if (ch == '\n')
1.18      jfb      2493:                                pdp->rp_lines++;
1.1       jfb      2494:
                   2495:                        *(bp++) = ch;
1.42      jfb      2496:                        pdp->rp_tlen++;
1.18      jfb      2497:                        if (bp == pdp->rp_bufend - 1) {
                   2498:                                len = bp - pdp->rp_buf;
1.151     xsa      2499:                                rcs_growbuf(rfp);
1.18      jfb      2500:                                bp = pdp->rp_buf + len;
                   2501:                        }
1.1       jfb      2502:                }
                   2503:
                   2504:                *bp = '\0';
1.14      deraadt  2505:        } else if (isdigit(ch)) {
1.1       jfb      2506:                *(bp++) = ch;
                   2507:                last = ch;
                   2508:                type = RCS_TOK_NUM;
                   2509:
                   2510:                for (;;) {
                   2511:                        ch = getc(pdp->rp_file);
1.18      jfb      2512:                        if (bp == pdp->rp_bufend)
1.1       jfb      2513:                                break;
                   2514:                        if (!isdigit(ch) && ch != '.') {
                   2515:                                ungetc(ch, pdp->rp_file);
                   2516:                                break;
                   2517:                        }
                   2518:
                   2519:                        if (last == '.' && ch == '.') {
                   2520:                                type = RCS_TOK_ERR;
                   2521:                                break;
                   2522:                        }
                   2523:                        last = ch;
                   2524:                        *(bp++) = ch;
1.42      jfb      2525:                        pdp->rp_tlen++;
1.1       jfb      2526:                }
1.18      jfb      2527:                *bp = '\0';
1.1       jfb      2528:        }
                   2529:
                   2530:        return (type);
                   2531: }
                   2532:
                   2533: /*
                   2534:  * rcs_pushtok()
                   2535:  *
                   2536:  * Push a token back in the parser's token buffer.
                   2537:  */
                   2538: static int
                   2539: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2540: {
                   2541:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2542:
                   2543:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2544:                return (-1);
                   2545:
                   2546:        pdp->rp_pttype = type;
                   2547:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   2548:        return (0);
                   2549: }
                   2550:
1.18      jfb      2551:
                   2552: /*
                   2553:  * rcs_growbuf()
                   2554:  *
                   2555:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2556:  * RCS_BUFEXTSIZE.
                   2557:  * In case of failure, the original buffer is left unmodified.
                   2558:  */
1.151     xsa      2559: static void
1.18      jfb      2560: rcs_growbuf(RCSFILE *rf)
                   2561: {
                   2562:        void *tmp;
                   2563:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2564:
1.153     ray      2565:        tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
1.161     ray      2566:        pdp->rp_buf = tmp;
1.18      jfb      2567:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2568:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2569: }
                   2570:
                   2571: /*
                   2572:  * rcs_strprint()
                   2573:  *
                   2574:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2575:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2576:  * binary data.
                   2577:  */
1.155     ray      2578: static void
1.42      jfb      2579: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2580: {
                   2581:        const u_char *ap, *ep, *sp;
1.52      jfb      2582:
                   2583:        if (slen == 0)
1.155     ray      2584:                return;
1.42      jfb      2585:
                   2586:        ep = str + slen - 1;
                   2587:
                   2588:        for (sp = str; sp <= ep;)  {
                   2589:                ap = memchr(sp, '@', ep - sp);
                   2590:                if (ap == NULL)
                   2591:                        ap = ep;
1.155     ray      2592:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42      jfb      2593:
                   2594:                if (*ap == '@')
                   2595:                        putc('@', stream);
                   2596:                sp = ap + 1;
1.63      joris    2597:        }
                   2598: }
                   2599:
                   2600: /*
                   2601:  * rcs_expand_keywords()
                   2602:  *
1.125     niallo   2603:  * Return expansion any RCS keywords in <data>
                   2604:  *
                   2605:  * On error, return NULL.
1.63      joris    2606:  */
1.125     niallo   2607: static char *
                   2608: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
1.63      joris    2609:     size_t len, int mode)
                   2610: {
1.155     ray      2611:        ptrdiff_t c_offset, sizdiff, start_offset;
1.147     ray      2612:        size_t i;
1.155     ray      2613:        int kwtype;
                   2614:        u_int j, found;
1.140     xsa      2615:        char *c, *kwstr, *start, *end, *tbuf;
1.125     niallo   2616:        char expbuf[256], buf[256];
1.165     joris    2617:        struct tm tb;
1.134     joris    2618:        char *fmt;
1.63      joris    2619:
1.65      niallo   2620:        kwtype = 0;
                   2621:        kwstr = NULL;
1.63      joris    2622:        i = 0;
                   2623:
                   2624:        /*
1.134     joris    2625:         * -z support for RCS
                   2626:         */
1.165     joris    2627:        tb = rdp->rd_date;
1.140     xsa      2628:        if (timezone_flag != NULL)
1.165     joris    2629:                rcs_set_tz(timezone_flag, rdp, &tb);
1.134     joris    2630:
                   2631:        /*
1.63      joris    2632:         * Keyword formats:
                   2633:         * $Keyword$
                   2634:         * $Keyword: value$
                   2635:         */
1.144     deraadt  2636:        for (c = data; *c != '\0' && i < len; c++) {
1.63      joris    2637:                if (*c == '$') {
                   2638:                        /* remember start of this possible keyword */
                   2639:                        start = c;
1.125     niallo   2640:                        start_offset = start - data;
1.63      joris    2641:
                   2642:                        /* first following character has to be alphanumeric */
1.144     deraadt  2643:                        c++;
1.63      joris    2644:                        if (!isalpha(*c)) {
                   2645:                                c = start;
                   2646:                                continue;
                   2647:                        }
                   2648:
                   2649:                        /* look for any matching keywords */
                   2650:                        found = 0;
                   2651:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2652:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2653:                                    strlen(rcs_expkw[j].kw_str))) {
                   2654:                                        found = 1;
                   2655:                                        kwstr = rcs_expkw[j].kw_str;
                   2656:                                        kwtype = rcs_expkw[j].kw_type;
                   2657:                                        break;
                   2658:                                }
                   2659:                        }
                   2660:
                   2661:                        /* unknown keyword, continue looking */
                   2662:                        if (found == 0) {
                   2663:                                c = start;
                   2664:                                continue;
                   2665:                        }
                   2666:
                   2667:                        /* next character has to be ':' or '$' */
                   2668:                        c += strlen(kwstr);
                   2669:                        if (*c != ':' && *c != '$') {
                   2670:                                c = start;
                   2671:                                continue;
                   2672:                        }
                   2673:
                   2674:                        /*
                   2675:                         * if the next character was ':' we need to look for
                   2676:                         * an '$' before the end of the line to be sure it is
                   2677:                         * in fact a keyword.
                   2678:                         */
                   2679:                        if (*c == ':') {
                   2680:                                while (*c++) {
                   2681:                                        if (*c == '$' || *c == '\n')
                   2682:                                                break;
                   2683:                                }
                   2684:
                   2685:                                if (*c != '$') {
                   2686:                                        c = start;
                   2687:                                        continue;
                   2688:                                }
                   2689:                        }
1.125     niallo   2690:                        c_offset = c - data;
                   2691:                        end = c + 1;
1.63      joris    2692:
                   2693:                        /* start constructing the expansion */
                   2694:                        expbuf[0] = '\0';
                   2695:
                   2696:                        if (mode & RCS_KWEXP_NAME) {
                   2697:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2698:                                strlcat(expbuf, kwstr, sizeof(expbuf));
                   2699:                                if (mode & RCS_KWEXP_VAL)
                   2700:                                        strlcat(expbuf, ": ", sizeof(expbuf));
                   2701:                        }
                   2702:
                   2703:                        /*
1.80      reyk     2704:                         * order matters because of RCS_KW_ID and
                   2705:                         * RCS_KW_HEADER here
1.63      joris    2706:                         */
                   2707:                        if (mode & RCS_KWEXP_VAL) {
                   2708:                                if (kwtype & RCS_KW_RCSFILE) {
                   2709:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.80      reyk     2710:                                                strlcat(expbuf,
                   2711:                                                    basename(rcsfile),
1.63      joris    2712:                                                    sizeof(expbuf));
                   2713:                                        else
                   2714:                                                strlcat(expbuf, rcsfile,
                   2715:                                                    sizeof(expbuf));
                   2716:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2717:                                }
                   2718:
                   2719:                                if (kwtype & RCS_KW_REVISION) {
1.80      reyk     2720:                                        rcsnum_tostr(rdp->rd_num, buf,
                   2721:                                            sizeof(buf));
1.63      joris    2722:                                        strlcat(buf, " ", sizeof(buf));
                   2723:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2724:                                }
                   2725:
                   2726:                                if (kwtype & RCS_KW_DATE) {
1.134     joris    2727:                                        if (timezone_flag != NULL)
                   2728:                                                fmt = "%Y/%m/%d %H:%M:%S%z ";
                   2729:                                        else
                   2730:                                                fmt = "%Y/%m/%d %H:%M:%S ";
                   2731:
1.165     joris    2732:                                        strftime(buf, sizeof(buf), fmt, &tb);
1.63      joris    2733:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2734:                                }
                   2735:
                   2736:                                if (kwtype & RCS_KW_AUTHOR) {
                   2737:                                        strlcat(expbuf, rdp->rd_author,
                   2738:                                            sizeof(expbuf));
                   2739:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2740:                                }
                   2741:
                   2742:                                if (kwtype & RCS_KW_STATE) {
                   2743:                                        strlcat(expbuf, rdp->rd_state,
                   2744:                                            sizeof(expbuf));
                   2745:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2746:                                }
                   2747:
                   2748:                                /* order does not matter anymore below */
                   2749:                                if (kwtype & RCS_KW_LOG)
                   2750:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2751:
                   2752:                                if (kwtype & RCS_KW_SOURCE) {
1.80      reyk     2753:                                        strlcat(expbuf, rcsfile,
                   2754:                                            sizeof(expbuf));
1.63      joris    2755:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2756:                                }
                   2757:
                   2758:                                if (kwtype & RCS_KW_NAME)
                   2759:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2760:                        }
                   2761:
                   2762:                        /* end the expansion */
                   2763:                        if (mode & RCS_KWEXP_NAME)
                   2764:                                strlcat(expbuf, "$", sizeof(expbuf));
1.138     deraadt  2765:
1.125     niallo   2766:                        sizdiff = strlen(expbuf) - (end - start);
1.161     ray      2767:                        tbuf = xstrdup(end);
1.125     niallo   2768:                        /* only realloc if we have to */
                   2769:                        if (sizdiff > 0) {
1.161     ray      2770:                                char *newdata;
                   2771:
1.125     niallo   2772:                                len += sizdiff;
1.161     ray      2773:                                newdata = xrealloc(data, 1, len);
                   2774:                                data = newdata;
1.125     niallo   2775:                                /*
                   2776:                                 * ensure string pointers are not invalidated
                   2777:                                 * after realloc()
                   2778:                                 */
                   2779:                                start = data + start_offset;
                   2780:                                c = data + c_offset;
                   2781:                        }
                   2782:                        strlcpy(start, expbuf, len);
                   2783:                        strlcat(data, tbuf, len);
                   2784:                        xfree(tbuf);
1.63      joris    2785:                        i += strlen(expbuf);
                   2786:                }
1.42      jfb      2787:        }
1.81      niallo   2788:
1.125     niallo   2789:        return (data);
1.81      niallo   2790: }
                   2791:
                   2792: /*
                   2793:  * rcs_deltatext_set()
                   2794:  *
                   2795:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      2796:  * Returns -1 on error, 0 on success.
1.81      niallo   2797:  */
                   2798: int
                   2799: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2800: {
                   2801:        size_t len;
                   2802:        struct rcs_delta *rdp;
1.117     niallo   2803:
                   2804:        /* Write operations require full parsing */
                   2805:        rcs_parse_deltatexts(rfp, NULL);
1.81      niallo   2806:
                   2807:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2808:                return (-1);
                   2809:
                   2810:        if (rdp->rd_text != NULL)
1.110     joris    2811:                xfree(rdp->rd_text);
1.81      niallo   2812:
                   2813:        len = strlen(dtext);
1.103     joris    2814:        if (len != 0) {
1.161     ray      2815:                /* XXX - use xstrdup() if rd_text changes to char *. */
                   2816:                rdp->rd_text = xmalloc(len + 1);
1.155     ray      2817:                rdp->rd_tlen = len;
                   2818:                (void)memcpy(rdp->rd_text, dtext, len + 1);
1.103     joris    2819:        } else {
                   2820:                rdp->rd_text = NULL;
                   2821:                rdp->rd_tlen = 0;
                   2822:        }
1.18      jfb      2823:
1.86      joris    2824:        return (0);
                   2825: }
                   2826:
                   2827: /*
                   2828:  * rcs_rev_setlog()
                   2829:  *
                   2830:  * Sets the log message of revision <rev> to <logtext>
                   2831:  */
                   2832: int
                   2833: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2834: {
                   2835:        struct rcs_delta *rdp;
                   2836:        char buf[16];
                   2837:
                   2838:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2839:
                   2840:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2841:                return (-1);
                   2842:
                   2843:        if (rdp->rd_log != NULL)
1.110     joris    2844:                xfree(rdp->rd_log);
1.86      joris    2845:
1.110     joris    2846:        rdp->rd_log = xstrdup(logtext);
1.86      joris    2847:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   2848:        return (0);
                   2849: }
1.97      niallo   2850: /*
                   2851:  * rcs_rev_getdate()
                   2852:  *
                   2853:  * Get the date corresponding to a given revision.
                   2854:  * Returns the date on success, -1 on failure.
                   2855:  */
                   2856: time_t
                   2857: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2858: {
                   2859:        struct rcs_delta *rdp;
                   2860:
                   2861:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2862:                return (-1);
                   2863:
                   2864:        return (mktime(&rdp->rd_date));
                   2865: }
1.95      niallo   2866:
                   2867: /*
                   2868:  * rcs_state_set()
                   2869:  *
                   2870:  * Sets the state of revision <rev> to <state>
                   2871:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2872:  *
                   2873:  * Returns -1 on failure, 0 on success.
                   2874:  */
                   2875: int
                   2876: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2877: {
                   2878:        struct rcs_delta *rdp;
                   2879:
                   2880:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2881:                return (-1);
                   2882:
                   2883:        if (rdp->rd_state != NULL)
1.110     joris    2884:                xfree(rdp->rd_state);
1.95      niallo   2885:
1.110     joris    2886:        rdp->rd_state = xstrdup(state);
1.95      niallo   2887:
                   2888:        rfp->rf_flags &= ~RCS_SYNCED;
                   2889:
                   2890:        return (0);
                   2891: }
                   2892:
                   2893: /*
                   2894:  * rcs_state_check()
                   2895:  *
                   2896:  * Check if string <state> is valid.
                   2897:  *
1.96      xsa      2898:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   2899:  */
                   2900: int
                   2901: rcs_state_check(const char *state)
                   2902: {
                   2903:        if (strchr(state, ' ') != NULL)
                   2904:                return (-1);
                   2905:
1.18      jfb      2906:        return (0);
1.1       jfb      2907: }
1.97      niallo   2908:
                   2909: /*
                   2910:  * rcs_state_get()
                   2911:  *
                   2912:  * Get the state for a given revision of a specified RCSFILE.
                   2913:  *
                   2914:  * Returns NULL on failure.
                   2915:  */
                   2916: const char *
                   2917: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2918: {
                   2919:        struct rcs_delta *rdp;
                   2920:
                   2921:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2922:                return (NULL);
                   2923:
                   2924:        return (rdp->rd_state);
                   2925: }
                   2926:
1.131     niallo   2927: /*
                   2928:  * rcs_kwexp_buf()
                   2929:  *
                   2930:  * Do keyword expansion on a buffer if necessary
                   2931:  *
                   2932:  */
                   2933: BUF *
                   2934: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2935: {
                   2936:        struct rcs_delta *rdp;
                   2937:        char *expanded, *tbuf;
                   2938:        int expmode;
                   2939:        size_t len;
                   2940:
                   2941:        /*
                   2942:         * Do keyword expansion if required.
                   2943:         */
                   2944:        if (rf->rf_expand != NULL)
                   2945:                expmode = rcs_kwexp_get(rf);
                   2946:        else
                   2947:                expmode = RCS_KWEXP_DEFAULT;
                   2948:
                   2949:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2950:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
                   2951:                    fatal("could not fetch revision");
                   2952:                cvs_buf_putc(bp, '\0');
                   2953:                len = cvs_buf_len(bp);
                   2954:                tbuf = cvs_buf_release(bp);
                   2955:                expanded = rcs_expand_keywords(rf->rf_path, rdp,
                   2956:                    tbuf, len, expmode);
                   2957:                bp = cvs_buf_alloc(len, BUF_AUTOEXT);
1.138     deraadt  2958:                cvs_buf_set(bp, expanded, strlen(expanded), 0);
1.131     niallo   2959:                xfree(expanded);
                   2960:        }
                   2961:        return (bp);
                   2962: }
1.105     joris    2963:
1.106     joris    2964: #if !defined(RCSPROG)
                   2965:
1.105     joris    2966: static char *month_tab[] = {
                   2967:        "Jan",
                   2968:        "Feb",
                   2969:        "Mar",
                   2970:        "Apr",
                   2971:        "May",
                   2972:        "Jun",
                   2973:        "Jul",
                   2974:        "Aug",
                   2975:        "Sep",
                   2976:        "Oct",
                   2977:        "Nov",
                   2978:        "Dec"
                   2979: };
                   2980:
1.163     xsa      2981: void
                   2982: rcs_kflag_usage(void)
                   2983: {
                   2984:        (void)fprintf(stderr, "Valid expansion modes include:\n"
                   2985:            "\t-kkv\tGenerate keywords using the default form.\n"
                   2986:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                   2987:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                   2988:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                   2989:            "\t-ko\tGenerate old keyword string "
                   2990:            "(no changes from checked in file).\n"
                   2991:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
                   2992: }
                   2993:
1.105     joris    2994: /*
                   2995:  * Checkout a certain revision <rev> of RCS file <rf> to either standard
                   2996:  * output when running in server mode, or to <fpath> when running in local mode.
                   2997:  *
                   2998:  * If type is CHECKOUT_REV_MERGED we have an extra argument, which
                   2999:  * is the buffer containing the merged file.
                   3000:  *
                   3001:  * If type is CHECKOUT_REV_REMOVED, the file has been removed and we
                   3002:  * need to do the same thing.
                   3003:  */
                   3004: int
                   3005: cvs_checkout_rev(RCSFILE *rf, RCSNUM *rev, CVSFILE *cf, char *fpath,
                   3006:     int local, int type, ...)
                   3007: {
                   3008:        BUF *bp;
                   3009:        int l, ret, fsize;
                   3010:        char timebuf[32], entry[MAXPATHLEN], copyfile[MAXPATHLEN];
                   3011:        char *content, *repo, buf[MAXPATHLEN], modestr[16];
                   3012:        struct cvsroot *root;
                   3013:        struct cvs_ent *ent;
                   3014:        va_list ap;
                   3015:        time_t rcstime;
                   3016:        struct timeval tv[2];
                   3017:        struct tm *tp;
                   3018:        RCSNUM *oldrev;
                   3019:
                   3020:        bp = NULL;
                   3021:        ret = -1;
                   3022:        content = NULL;
                   3023:        oldrev = NULL;
                   3024:
                   3025:        if ((type != CHECKOUT_REV_MERGED) && (type != CHECKOUT_REV_REMOVED)) {
                   3026:                /* fetch the contents of the revision */
                   3027:                if ((bp = rcs_getrev(rf, rev)) == NULL) {
                   3028:                        cvs_log(LP_ERR, "revision '%s' not found in file '%s'",
                   3029:                            rcsnum_tostr(rev, buf, sizeof(buf)), fpath);
                   3030:                        goto out;
                   3031:                }
1.131     niallo   3032:                bp = rcs_kwexp_buf(bp, rf, rev);
1.105     joris    3033:        } else if (type != CHECKOUT_REV_REMOVED) {
                   3034:                va_start(ap, type);
                   3035:                bp = va_arg(ap, BUF *);
                   3036:                va_end(ap);
                   3037:        }
                   3038:
                   3039:        if (type == CHECKOUT_REV_CREATED)
                   3040:                rcstime = rcs_rev_getdate(rf, rev);
1.108     joris    3041:        else if (type == CHECKOUT_REV_MERGED ||
                   3042:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    3043:                time(&rcstime);
1.108     joris    3044:                if ((rcstime = cvs_hack_time(rcstime, 1)) < 0)
                   3045:                        goto out;
                   3046:        }
1.105     joris    3047:
1.108     joris    3048:        if (type == CHECKOUT_REV_CREATED ||
1.130     niallo   3049:            type == CHECKOUT_REV_MERGED ||
1.108     joris    3050:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    3051:                ctime_r(&rcstime, timebuf);
                   3052:                l = strlen(timebuf);
                   3053:                if ((l > 0) && (timebuf[l - 1] == '\n'))
                   3054:                        timebuf[--l] = '\0';
                   3055:
                   3056:                l = snprintf(entry, sizeof(entry), "/%s/%s/%s/%s/", cf->cf_name,
                   3057:                    rcsnum_tostr(rev, buf, sizeof(buf)),
                   3058:                    (local == 1) ? timebuf : "",
                   3059:                    (type == CHECKOUT_REV_MERGED) ? "+=" : "");
                   3060:                if (l == -1 || l >= (int)sizeof(buf))
                   3061:                        goto out;
                   3062:        }
                   3063:
                   3064:        if (type == CHECKOUT_REV_MERGED) {
1.111     joris    3065:                oldrev = rcsnum_alloc();
                   3066:                rcsnum_cpy(rev, oldrev, 0);
1.105     joris    3067:
1.143     niallo   3068:                if (oldrev->rn_id[oldrev->rn_len - 1] <= 0)
1.105     joris    3069:                        goto out;
1.143     niallo   3070:                oldrev = rcsnum_dec(oldrev);
1.105     joris    3071:
                   3072:                l = snprintf(copyfile, sizeof(copyfile), ".#%s.%s",
                   3073:                    cf->cf_name, rcsnum_tostr(oldrev, buf, sizeof(buf)));
                   3074:                if (l == -1 || l >= (int)sizeof(copyfile))
                   3075:                        goto out;
                   3076:        }
                   3077:
                   3078:        root = CVS_DIR_ROOT(cf);
                   3079:        repo = CVS_DIR_REPO(cf);
                   3080:
                   3081:        /*
                   3082:         * In local mode, just copy the entire contents to fpath.
                   3083:         * In server mode, we need to send it to the client together with
                   3084:         * some responses.
                   3085:         */
                   3086:        if (local) {
                   3087:                l = 0;
                   3088:                if (cf->cf_entry == NULL) {
                   3089:                        l = 1;
                   3090:                        cf->cf_entry = cvs_ent_open(cf->cf_dir, O_RDWR);
                   3091:                        if (cf->cf_entry == NULL) {
1.122     reyk     3092:                                cvs_log(LP_ERR, "failed to open Entry "
                   3093:                                    "file '%s'", cf->cf_dir);
1.105     joris    3094:                                goto out;
                   3095:                        }
                   3096:                }
                   3097:
                   3098:                cvs_ent_remove(cf->cf_entry, cf->cf_name, 1);
                   3099:                if (type != CHECKOUT_REV_REMOVED) {
                   3100:                        cvs_ent_addln(cf->cf_entry, entry);
                   3101:                        ent = cvs_ent_get(cf->cf_entry, cf->cf_name);
                   3102:                        ent->processed = 1;
                   3103:                }
                   3104:
                   3105:                if (l == 1)
                   3106:                        cvs_ent_close(cf->cf_entry);
                   3107:
                   3108:                switch (type) {
                   3109:                case CHECKOUT_REV_REMOVED:
                   3110:                        if (cvs_unlink(fpath) < 0)
                   3111:                                goto out;
                   3112:                        break;
                   3113:                case CHECKOUT_REV_MERGED:
                   3114:                        /* XXX move the old file when merging */
1.108     joris    3115:                case CHECKOUT_REV_UPDATED:
1.105     joris    3116:                case CHECKOUT_REV_CREATED:
1.114     xsa      3117:                        cvs_buf_write(bp, fpath, cf->cf_mode);
1.107     joris    3118:                        /*
                   3119:                         * correct the time first
                   3120:                         */
                   3121:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   3122:                                goto out;
                   3123:
1.105     joris    3124:                        tv[0].tv_sec = rcstime;
                   3125:                        tv[0].tv_usec = 0;
                   3126:                        tv[1] = tv[0];
                   3127:                        if (utimes(fpath, tv) == -1)
                   3128:                                cvs_log(LP_ERRNO, "failed to set timestamps");
                   3129:                        break;
                   3130:                }
                   3131:        } else {
                   3132:                /* sanity */
                   3133:                if (cf->cf_type != DT_REG) {
                   3134:                        cvs_log(LP_ERR, "cvs_checkout_rev: none DT_REG file");
                   3135:                        goto out;
                   3136:                }
                   3137:
                   3138:                /*
                   3139:                 * if we are removing a file, we don't need this stuff.
                   3140:                 */
                   3141:                if (type != CHECKOUT_REV_REMOVED) {
1.107     joris    3142:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   3143:                                goto out;
                   3144:
1.105     joris    3145:                        tp = gmtime(&rcstime);
                   3146:                        l = snprintf(timebuf, sizeof(timebuf),
                   3147:                            "%02d %s %d %02d:%02d:%02d -0000",
                   3148:                            tp->tm_mday, month_tab[tp->tm_mon],
                   3149:                            tp->tm_year + 1900, tp->tm_hour,
                   3150:                            tp->tm_min, tp->tm_sec);
                   3151:                        if (l == -1 || l >= (int)sizeof(timebuf))
                   3152:                                goto out;
                   3153:
                   3154:                        fsize = cvs_buf_len(bp);
                   3155:                        cvs_modetostr(cf->cf_mode, modestr, sizeof(modestr));
1.114     xsa      3156:                        cvs_buf_putc(bp, '\0');
1.105     joris    3157:                        content = cvs_buf_release(bp);
                   3158:                        bp = NULL;
                   3159:                }
                   3160:
                   3161:                if (type == CHECKOUT_REV_MERGED) {
                   3162:                        printf("Copy-file %s/\n", (cf->cf_dir != NULL) ?
                   3163:                            cf->cf_dir : ".");
                   3164:                        printf("%s/%s/%s\n", root->cr_dir, repo, cf->cf_name);
                   3165:                        printf("%s\n", copyfile);
                   3166:                }
                   3167:
                   3168:                switch (type) {
                   3169:                case CHECKOUT_REV_MERGED:
                   3170:                        printf("Merged");
                   3171:                        break;
                   3172:                case CHECKOUT_REV_REMOVED:
                   3173:                        printf("Removed");
                   3174:                        break;
                   3175:                case CHECKOUT_REV_CREATED:
                   3176:                        printf("Mod-time %s\n", timebuf);
                   3177:                        printf("Created");
                   3178:                        break;
                   3179:                default:
                   3180:                        cvs_log(LP_ERR, "cvs_checkout_rev: bad type %d",
                   3181:                            type);
                   3182:                        goto out;
                   3183:                }
                   3184:
                   3185:                printf(" %s/\n", (cf->cf_dir != NULL) ? cf->cf_dir : ".");
                   3186:                printf("%s/%s\n", repo, cf->cf_name);
                   3187:
                   3188:                if (type != CHECKOUT_REV_REMOVED) {
                   3189:                        printf("%s\n", entry);
                   3190:                        printf("%s\n%d\n%s", modestr, fsize, content);
                   3191:                }
                   3192:        }
                   3193:
                   3194:        ret = 0;
                   3195:
                   3196: out:
                   3197:        if (oldrev != NULL)
                   3198:                rcsnum_free(oldrev);
                   3199:        if (bp != NULL)
                   3200:                cvs_buf_free(bp);
                   3201:        if (content != NULL)
1.110     joris    3202:                xfree(content);
1.105     joris    3203:
                   3204:        return (ret);
                   3205: }
1.106     joris    3206:
1.163     xsa      3207: #endif /* !RCSPROG */