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

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