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

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