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

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