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

1.112   ! xsa         1: /*     $OpenBSD: rcs.c,v 1.111 2005/12/12 17:47:03 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:
1.112   ! xsa      1364:        if ((pw = getpwuid(getuid())) == NULL)
        !          1365:                fatal("getpwuid failed");
1.52      jfb      1366:
1.110     joris    1367:        rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.52      jfb      1368:        memset(rdp, 0, sizeof(*rdp));
                   1369:
                   1370:        TAILQ_INIT(&(rdp->rd_branches));
                   1371:        TAILQ_INIT(&(rdp->rd_snodes));
                   1372:
1.111     joris    1373:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1374:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1375:
1.111     joris    1376:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1377:
1.101     niallo   1378:        if (!(rf->rf_flags & RCS_CREATE))
                   1379:                rcsnum_cpy(old, rdp->rd_next, 0);
                   1380:
1.83      joris    1381:        rcsnum_free(old);
1.52      jfb      1382:
1.90      niallo   1383:        if (username == NULL)
                   1384:                username = pw->pw_name;
                   1385:
1.110     joris    1386:        rdp->rd_author = xstrdup(username);
                   1387:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1388:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1389:
1.53      jfb      1390:        if (date != (time_t)(-1))
                   1391:                now = date;
                   1392:        else
                   1393:                time(&now);
1.52      jfb      1394:        gmtime_r(&now, &(rdp->rd_date));
                   1395:
                   1396:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1397:        rf->rf_ndelta++;
1.81      niallo   1398:
1.64      niallo   1399:        /* not synced anymore */
                   1400:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1401:
                   1402:        return (0);
                   1403: }
                   1404:
                   1405: /*
                   1406:  * rcs_rev_remove()
                   1407:  *
                   1408:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1409:  */
                   1410: int
                   1411: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1412: {
                   1413:        int ret;
                   1414:        struct rcs_delta *rdp;
                   1415:
                   1416:        ret = 0;
                   1417:        if (rev == RCS_HEAD_REV)
                   1418:                rev = rf->rf_head;
                   1419:
                   1420:        /* do we actually have that revision? */
                   1421:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1422:                rcs_errno = RCS_ERR_NOENT;
                   1423:                ret = -1;
                   1424:        } else {
                   1425:                /* XXX assumes it's not a sub node */
                   1426:                TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1427:                rf->rf_ndelta--;
                   1428:                rf->rf_flags &= ~RCS_SYNCED;
                   1429:        }
                   1430:
                   1431:        return (ret);
                   1432:
                   1433: }
                   1434:
                   1435: /*
1.1       jfb      1436:  * rcs_findrev()
                   1437:  *
                   1438:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1439:  * The revision number is given in <rev>.
                   1440:  * Returns a pointer to the delta on success, or NULL on failure.
                   1441:  */
1.102     xsa      1442: struct rcs_delta *
1.43      jfb      1443: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1       jfb      1444: {
                   1445:        u_int cmplen;
                   1446:        struct rcs_delta *rdp;
                   1447:        struct rcs_dlist *hp;
1.6       vincent  1448:        int found;
1.26      jfb      1449:
1.1       jfb      1450:        cmplen = 2;
                   1451:        hp = &(rfp->rf_delta);
                   1452:
1.6       vincent  1453:        do {
                   1454:                found = 0;
                   1455:                TAILQ_FOREACH(rdp, hp, rd_list) {
                   1456:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                   1457:                                if (cmplen == rev->rn_len)
                   1458:                                        return (rdp);
1.1       jfb      1459:
1.6       vincent  1460:                                hp = &(rdp->rd_snodes);
                   1461:                                cmplen += 2;
                   1462:                                found = 1;
                   1463:                                break;
                   1464:                        }
1.1       jfb      1465:                }
1.6       vincent  1466:        } while (found && cmplen < rev->rn_len);
1.1       jfb      1467:
                   1468:        return (NULL);
1.20      jfb      1469: }
                   1470:
                   1471: /*
1.26      jfb      1472:  * rcs_kwexp_set()
                   1473:  *
                   1474:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1475:  * Returns 0 on success, or -1 on failure.
                   1476:  */
                   1477: int
                   1478: rcs_kwexp_set(RCSFILE *file, int mode)
                   1479: {
                   1480:        int i;
                   1481:        char *tmp, buf[8] = "";
                   1482:
                   1483:        if (RCS_KWEXP_INVAL(mode))
                   1484:                return (-1);
                   1485:
                   1486:        i = 0;
                   1487:        if (mode == RCS_KWEXP_NONE)
                   1488:                buf[0] = 'b';
                   1489:        else if (mode == RCS_KWEXP_OLD)
                   1490:                buf[0] = 'o';
                   1491:        else {
                   1492:                if (mode & RCS_KWEXP_NAME)
                   1493:                        buf[i++] = 'k';
                   1494:                if (mode & RCS_KWEXP_VAL)
                   1495:                        buf[i++] = 'v';
                   1496:                if (mode & RCS_KWEXP_LKR)
                   1497:                        buf[i++] = 'l';
                   1498:        }
                   1499:
1.110     joris    1500:        tmp = xstrdup(buf);
1.27      jfb      1501:        if (file->rf_expand != NULL)
1.99      niallo   1502:                free(file->rf_expand);
1.26      jfb      1503:        file->rf_expand = tmp;
1.64      niallo   1504:        /* not synced anymore */
                   1505:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1506:
                   1507:        return (0);
                   1508: }
                   1509:
                   1510: /*
                   1511:  * rcs_kwexp_get()
                   1512:  *
                   1513:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1514:  */
                   1515: int
                   1516: rcs_kwexp_get(RCSFILE *file)
                   1517: {
                   1518:        return rcs_kflag_get(file->rf_expand);
                   1519: }
                   1520:
                   1521: /*
1.20      jfb      1522:  * rcs_kflag_get()
                   1523:  *
                   1524:  * Get the keyword expansion mode from a set of character flags given in
                   1525:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1526:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1527:  */
                   1528: int
                   1529: rcs_kflag_get(const char *flags)
                   1530: {
                   1531:        int fl;
                   1532:        size_t len;
                   1533:        const char *fp;
                   1534:
                   1535:        fl = 0;
                   1536:        len = strlen(flags);
                   1537:
                   1538:        for (fp = flags; *fp != '\0'; fp++) {
                   1539:                if (*fp == 'k')
                   1540:                        fl |= RCS_KWEXP_NAME;
                   1541:                else if (*fp == 'v')
                   1542:                        fl |= RCS_KWEXP_VAL;
                   1543:                else if (*fp == 'l')
                   1544:                        fl |= RCS_KWEXP_LKR;
                   1545:                else if (*fp == 'o') {
                   1546:                        if (len != 1)
                   1547:                                fl |= RCS_KWEXP_ERR;
                   1548:                        fl |= RCS_KWEXP_OLD;
                   1549:                } else if (*fp == 'b') {
                   1550:                        if (len != 1)
                   1551:                                fl |= RCS_KWEXP_ERR;
                   1552:                } else  /* unknown letter */
                   1553:                        fl |= RCS_KWEXP_ERR;
                   1554:        }
                   1555:
                   1556:        return (fl);
1.32      jfb      1557: }
                   1558:
                   1559: /*
                   1560:  * rcs_errstr()
                   1561:  *
                   1562:  * Get the error string matching the RCS error code <code>.
                   1563:  */
1.57      xsa      1564: const char *
1.32      jfb      1565: rcs_errstr(int code)
                   1566: {
1.50      jfb      1567:        const char *esp;
                   1568:
                   1569:        if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
                   1570:                esp = NULL;
                   1571:        else if (code == RCS_ERR_ERRNO)
                   1572:                esp = strerror(errno);
                   1573:        else
                   1574:                esp = rcs_errstrs[code];
                   1575:        return (esp);
1.1       jfb      1576: }
                   1577:
1.21      jfb      1578: void
                   1579: rcs_kflag_usage(void)
                   1580: {
                   1581:        fprintf(stderr, "Valid expansion modes include:\n"
1.22      jfb      1582:            "\t-kkv\tGenerate keywords using the default form.\n"
                   1583:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                   1584:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                   1585:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                   1586:            "\t-ko\tGenerate old keyword string "
1.21      jfb      1587:            "(no changes from checked in file).\n"
1.22      jfb      1588:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21      jfb      1589: }
1.1       jfb      1590:
                   1591: /*
                   1592:  * rcs_parse()
                   1593:  *
                   1594:  * Parse the contents of file <path>, which are in the RCS format.
                   1595:  * Returns 0 on success, or -1 on failure.
                   1596:  */
1.26      jfb      1597: static int
1.1       jfb      1598: rcs_parse(RCSFILE *rfp)
                   1599: {
                   1600:        int ret;
                   1601:        struct rcs_pdata *pdp;
                   1602:
1.26      jfb      1603:        if (rfp->rf_flags & RCS_PARSED)
1.1       jfb      1604:                return (0);
                   1605:
1.110     joris    1606:        pdp = (struct rcs_pdata *)xmalloc(sizeof(*pdp));
1.1       jfb      1607:        memset(pdp, 0, sizeof(*pdp));
                   1608:
1.18      jfb      1609:        pdp->rp_lines = 0;
1.1       jfb      1610:        pdp->rp_pttype = RCS_TOK_ERR;
                   1611:
                   1612:        pdp->rp_file = fopen(rfp->rf_path, "r");
                   1613:        if (pdp->rp_file == NULL) {
1.50      jfb      1614:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb      1615:                cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
                   1616:                rcs_freepdata(pdp);
                   1617:                return (-1);
                   1618:        }
                   1619:
1.110     joris    1620:        pdp->rp_buf = (char *)xmalloc((size_t)RCS_BUFSIZE);
1.1       jfb      1621:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1622:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1623:
                   1624:        /* ditch the strict lock */
1.26      jfb      1625:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1626:        rfp->rf_pdata = pdp;
                   1627:
1.31      jfb      1628:        if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1       jfb      1629:                rcs_freepdata(pdp);
                   1630:                return (-1);
1.31      jfb      1631:        } else if (ret == RCS_TOK_NUM) {
                   1632:                for (;;) {
                   1633:                        ret = rcs_parse_delta(rfp);
                   1634:                        if (ret == 0)
                   1635:                                break;
                   1636:                        else if (ret == -1) {
                   1637:                                rcs_freepdata(pdp);
                   1638:                                return (-1);
                   1639:                        }
1.1       jfb      1640:                }
                   1641:        }
                   1642:
                   1643:        ret = rcs_gettok(rfp);
                   1644:        if (ret != RCS_TOK_DESC) {
1.50      jfb      1645:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1646:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1647:                    RCS_TOKSTR(rfp));
                   1648:                rcs_freepdata(pdp);
                   1649:                return (-1);
                   1650:        }
                   1651:
                   1652:        ret = rcs_gettok(rfp);
                   1653:        if (ret != RCS_TOK_STRING) {
1.50      jfb      1654:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1655:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1656:                    RCS_TOKSTR(rfp));
                   1657:                rcs_freepdata(pdp);
                   1658:                return (-1);
                   1659:        }
                   1660:
1.110     joris    1661:        rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1662:        for (;;) {
                   1663:                ret = rcs_parse_deltatext(rfp);
                   1664:                if (ret == 0)
                   1665:                        break;
                   1666:                else if (ret == -1) {
                   1667:                        rcs_freepdata(pdp);
                   1668:                        return (-1);
                   1669:                }
                   1670:        }
                   1671:
                   1672:        rcs_freepdata(pdp);
                   1673:
                   1674:        rfp->rf_pdata = NULL;
1.26      jfb      1675:        rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1       jfb      1676:
                   1677:        return (0);
                   1678: }
                   1679:
                   1680: /*
                   1681:  * rcs_parse_admin()
                   1682:  *
                   1683:  * Parse the administrative portion of an RCS file.
1.31      jfb      1684:  * Returns the type of the first token found after the admin section on
                   1685:  * success, or -1 on failure.
1.1       jfb      1686:  */
                   1687: static int
                   1688: rcs_parse_admin(RCSFILE *rfp)
                   1689: {
                   1690:        u_int i;
                   1691:        int tok, ntok, hmask;
                   1692:        struct rcs_key *rk;
                   1693:
                   1694:        /* hmask is a mask of the headers already encountered */
                   1695:        hmask = 0;
                   1696:        for (;;) {
                   1697:                tok = rcs_gettok(rfp);
                   1698:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1699:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1700:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                   1701:                        return (-1);
1.31      jfb      1702:                } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
                   1703:                        /*
                   1704:                         * Assume this is the start of the first delta or
                   1705:                         * that we are dealing with an empty RCS file and
                   1706:                         * we just found the description.
                   1707:                         */
1.1       jfb      1708:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31      jfb      1709:                        return (tok);
1.1       jfb      1710:                }
                   1711:
                   1712:                rk = NULL;
1.18      jfb      1713:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1714:                        if (rcs_keys[i].rk_id == tok)
                   1715:                                rk = &(rcs_keys[i]);
                   1716:
                   1717:                if (hmask & (1 << tok)) {
1.50      jfb      1718:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1719:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1720:                        return (-1);
                   1721:                }
                   1722:                hmask |= (1 << tok);
                   1723:
                   1724:                switch (tok) {
                   1725:                case RCS_TOK_HEAD:
                   1726:                case RCS_TOK_BRANCH:
                   1727:                case RCS_TOK_COMMENT:
                   1728:                case RCS_TOK_EXPAND:
                   1729:                        ntok = rcs_gettok(rfp);
                   1730:                        if (ntok == RCS_TOK_SCOLON)
                   1731:                                break;
                   1732:                        if (ntok != rk->rk_val) {
1.50      jfb      1733:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1734:                                cvs_log(LP_ERR,
                   1735:                                    "invalid value type for RCS key `%s'",
                   1736:                                    rk->rk_str);
                   1737:                        }
                   1738:
                   1739:                        if (tok == RCS_TOK_HEAD) {
1.111     joris    1740:                                if (rfp->rf_head == NULL)
1.28      jfb      1741:                                        rfp->rf_head = rcsnum_alloc();
1.1       jfb      1742:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1743:                                    rfp->rf_head);
1.14      deraadt  1744:                        } else if (tok == RCS_TOK_BRANCH) {
1.111     joris    1745:                                if (rfp->rf_branch == NULL)
1.35      jfb      1746:                                        rfp->rf_branch = rcsnum_alloc();
                   1747:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1748:                                    rfp->rf_branch) < 0)
                   1749:                                        return (-1);
1.14      deraadt  1750:                        } else if (tok == RCS_TOK_COMMENT) {
1.110     joris    1751:                                rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14      deraadt  1752:                        } else if (tok == RCS_TOK_EXPAND) {
1.110     joris    1753:                                rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1754:                        }
                   1755:
                   1756:                        /* now get the expected semi-colon */
                   1757:                        ntok = rcs_gettok(rfp);
                   1758:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1759:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1760:                                cvs_log(LP_ERR,
                   1761:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1762:                                    rk->rk_str);
1.1       jfb      1763:                                return (-1);
                   1764:                        }
                   1765:                        break;
                   1766:                case RCS_TOK_ACCESS:
1.29      jfb      1767:                        if (rcs_parse_access(rfp) < 0)
                   1768:                                return (-1);
1.1       jfb      1769:                        break;
                   1770:                case RCS_TOK_SYMBOLS:
1.29      jfb      1771:                        if (rcs_parse_symbols(rfp) < 0)
                   1772:                                return (-1);
1.1       jfb      1773:                        break;
                   1774:                case RCS_TOK_LOCKS:
1.29      jfb      1775:                        if (rcs_parse_locks(rfp) < 0)
                   1776:                                return (-1);
1.1       jfb      1777:                        break;
                   1778:                default:
1.50      jfb      1779:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1780:                        cvs_log(LP_ERR,
                   1781:                            "unexpected token `%s' in RCS admin section",
                   1782:                            RCS_TOKSTR(rfp));
                   1783:                        return (-1);
                   1784:                }
                   1785:        }
                   1786:
                   1787:        return (0);
                   1788: }
                   1789:
                   1790: /*
                   1791:  * rcs_parse_delta()
                   1792:  *
                   1793:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1794:  * information in the <rfp> delta list.
                   1795:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1796:  * -1 on error.
                   1797:  */
                   1798: static int
                   1799: rcs_parse_delta(RCSFILE *rfp)
                   1800: {
                   1801:        int ret, tok, ntok, hmask;
                   1802:        u_int i;
                   1803:        char *tokstr;
1.3       vincent  1804:        RCSNUM *datenum;
1.1       jfb      1805:        struct rcs_delta *rdp;
                   1806:        struct rcs_key *rk;
                   1807:
1.110     joris    1808:        rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.1       jfb      1809:        memset(rdp, 0, sizeof(*rdp));
                   1810:
                   1811:        rdp->rd_num = rcsnum_alloc();
                   1812:        rdp->rd_next = rcsnum_alloc();
                   1813:
                   1814:        TAILQ_INIT(&(rdp->rd_branches));
1.52      jfb      1815:        TAILQ_INIT(&(rdp->rd_snodes));
1.1       jfb      1816:
                   1817:        tok = rcs_gettok(rfp);
                   1818:        if (tok != RCS_TOK_NUM) {
1.52      jfb      1819:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1820:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1821:                    RCS_TOKSTR(rfp));
                   1822:                rcs_freedelta(rdp);
                   1823:                return (-1);
                   1824:        }
                   1825:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1826:
                   1827:        hmask = 0;
                   1828:        ret = 0;
                   1829:        tokstr = NULL;
                   1830:
                   1831:        for (;;) {
                   1832:                tok = rcs_gettok(rfp);
                   1833:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1834:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1835:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1836:                        rcs_freedelta(rdp);
                   1837:                        return (-1);
1.14      deraadt  1838:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1839:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1840:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1841:                        break;
                   1842:                }
                   1843:
                   1844:                rk = NULL;
1.18      jfb      1845:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1846:                        if (rcs_keys[i].rk_id == tok)
                   1847:                                rk = &(rcs_keys[i]);
                   1848:
                   1849:                if (hmask & (1 << tok)) {
1.50      jfb      1850:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1851:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1852:                        rcs_freedelta(rdp);
                   1853:                        return (-1);
                   1854:                }
                   1855:                hmask |= (1 << tok);
                   1856:
                   1857:                switch (tok) {
                   1858:                case RCS_TOK_DATE:
                   1859:                case RCS_TOK_AUTHOR:
                   1860:                case RCS_TOK_STATE:
                   1861:                case RCS_TOK_NEXT:
                   1862:                        ntok = rcs_gettok(rfp);
                   1863:                        if (ntok == RCS_TOK_SCOLON) {
                   1864:                                if (rk->rk_flags & RCS_VOPT)
                   1865:                                        break;
                   1866:                                else {
1.50      jfb      1867:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1868:                                        cvs_log(LP_ERR, "missing mandatory "
                   1869:                                            "value to RCS key `%s'",
                   1870:                                            rk->rk_str);
                   1871:                                        rcs_freedelta(rdp);
                   1872:                                        return (-1);
                   1873:                                }
                   1874:                        }
                   1875:
                   1876:                        if (ntok != rk->rk_val) {
1.50      jfb      1877:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1878:                                cvs_log(LP_ERR,
                   1879:                                    "invalid value type for RCS key `%s'",
                   1880:                                    rk->rk_str);
                   1881:                                rcs_freedelta(rdp);
                   1882:                                return (-1);
                   1883:                        }
                   1884:
                   1885:                        if (tokstr != NULL)
1.110     joris    1886:                                xfree(tokstr);
                   1887:                        tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1888:                        /* now get the expected semi-colon */
                   1889:                        ntok = rcs_gettok(rfp);
                   1890:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1891:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1892:                                cvs_log(LP_ERR,
                   1893:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1894:                                    rk->rk_str);
1.110     joris    1895:                                xfree(tokstr);
1.1       jfb      1896:                                rcs_freedelta(rdp);
                   1897:                                return (-1);
                   1898:                        }
                   1899:
                   1900:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1901:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110     joris    1902:                                        xfree(tokstr);
1.11      joris    1903:                                        rcs_freedelta(rdp);
                   1904:                                        return (-1);
                   1905:                                }
1.3       vincent  1906:                                if (datenum->rn_len != 6) {
1.50      jfb      1907:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1908:                                        cvs_log(LP_ERR,
                   1909:                                            "RCS date specification has %s "
                   1910:                                            "fields",
1.3       vincent  1911:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1912:                                            "missing");
1.110     joris    1913:                                        xfree(tokstr);
1.1       jfb      1914:                                        rcs_freedelta(rdp);
1.37      tedu     1915:                                        rcsnum_free(datenum);
                   1916:                                        return (-1);
1.1       jfb      1917:                                }
1.3       vincent  1918:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1919:                                if (rdp->rd_date.tm_year >= 1900)
                   1920:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1921:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1922:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1923:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1924:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1925:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1926:                                rcsnum_free(datenum);
1.14      deraadt  1927:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1928:                                rdp->rd_author = tokstr;
                   1929:                                tokstr = NULL;
1.14      deraadt  1930:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1931:                                rdp->rd_state = tokstr;
                   1932:                                tokstr = NULL;
1.14      deraadt  1933:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      1934:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1935:                        }
                   1936:                        break;
                   1937:                case RCS_TOK_BRANCHES:
1.46      jfb      1938:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   1939:                                rcs_freedelta(rdp);
                   1940:                                return (-1);
                   1941:                        }
1.1       jfb      1942:                        break;
                   1943:                default:
1.50      jfb      1944:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1945:                        cvs_log(LP_ERR,
                   1946:                            "unexpected token `%s' in RCS delta",
                   1947:                            RCS_TOKSTR(rfp));
                   1948:                        rcs_freedelta(rdp);
                   1949:                        return (-1);
                   1950:                }
                   1951:        }
                   1952:
1.13      jfb      1953:        if (tokstr != NULL)
1.110     joris    1954:                xfree(tokstr);
1.13      jfb      1955:
1.1       jfb      1956:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      1957:        rfp->rf_ndelta++;
1.1       jfb      1958:
                   1959:        return (ret);
                   1960: }
                   1961:
                   1962: /*
                   1963:  * rcs_parse_deltatext()
                   1964:  *
                   1965:  * Parse an RCS delta text section and fill in the log and text field of the
                   1966:  * appropriate delta section.
                   1967:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1968:  * -1 on error.
                   1969:  */
                   1970: static int
                   1971: rcs_parse_deltatext(RCSFILE *rfp)
                   1972: {
                   1973:        int tok;
                   1974:        RCSNUM *tnum;
                   1975:        struct rcs_delta *rdp;
                   1976:
                   1977:        tok = rcs_gettok(rfp);
                   1978:        if (tok == RCS_TOK_EOF)
                   1979:                return (0);
                   1980:
                   1981:        if (tok != RCS_TOK_NUM) {
1.50      jfb      1982:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1983:                cvs_log(LP_ERR,
                   1984:                    "unexpected token `%s' at start of RCS delta text",
                   1985:                    RCS_TOKSTR(rfp));
                   1986:                return (-1);
                   1987:        }
1.13      jfb      1988:
                   1989:        tnum = rcsnum_alloc();
1.1       jfb      1990:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   1991:
                   1992:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1993:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   1994:                        break;
                   1995:        }
1.13      jfb      1996:        rcsnum_free(tnum);
                   1997:
1.1       jfb      1998:        if (rdp == NULL) {
                   1999:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   2000:                    RCS_TOKSTR(rfp));
                   2001:                return (-1);
                   2002:        }
                   2003:
                   2004:        tok = rcs_gettok(rfp);
                   2005:        if (tok != RCS_TOK_LOG) {
1.50      jfb      2006:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2007:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2008:                    RCS_TOKSTR(rfp));
                   2009:                return (-1);
                   2010:        }
                   2011:
                   2012:        tok = rcs_gettok(rfp);
                   2013:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2014:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2015:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2016:                    RCS_TOKSTR(rfp));
                   2017:                return (-1);
                   2018:        }
1.110     joris    2019:        rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2020:        tok = rcs_gettok(rfp);
                   2021:        if (tok != RCS_TOK_TEXT) {
1.50      jfb      2022:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2023:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2024:                    RCS_TOKSTR(rfp));
                   2025:                return (-1);
                   2026:        }
                   2027:
                   2028:        tok = rcs_gettok(rfp);
                   2029:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2030:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2031:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2032:                    RCS_TOKSTR(rfp));
                   2033:                return (-1);
                   2034:        }
                   2035:
1.110     joris    2036:        rdp->rd_text = (u_char *)xmalloc(RCS_TOKLEN(rfp) + 1);
1.74      joris    2037:        strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42      jfb      2038:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2039:
                   2040:        return (1);
                   2041: }
                   2042:
                   2043: /*
                   2044:  * rcs_parse_access()
                   2045:  *
                   2046:  * Parse the access list given as value to the `access' keyword.
                   2047:  * Returns 0 on success, or -1 on failure.
                   2048:  */
                   2049: static int
                   2050: rcs_parse_access(RCSFILE *rfp)
                   2051: {
                   2052:        int type;
                   2053:
                   2054:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2055:                if (type != RCS_TOK_ID) {
1.50      jfb      2056:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2057:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2058:                            RCS_TOKSTR(rfp));
                   2059:                        return (-1);
                   2060:                }
1.29      jfb      2061:
                   2062:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2063:                        return (-1);
1.1       jfb      2064:        }
                   2065:
                   2066:        return (0);
                   2067: }
                   2068:
                   2069: /*
                   2070:  * rcs_parse_symbols()
                   2071:  *
                   2072:  * Parse the symbol list given as value to the `symbols' keyword.
                   2073:  * Returns 0 on success, or -1 on failure.
                   2074:  */
                   2075: static int
                   2076: rcs_parse_symbols(RCSFILE *rfp)
                   2077: {
                   2078:        int type;
                   2079:        struct rcs_sym *symp;
                   2080:
                   2081:        for (;;) {
                   2082:                type = rcs_gettok(rfp);
                   2083:                if (type == RCS_TOK_SCOLON)
                   2084:                        break;
                   2085:
1.41      jfb      2086:                if (type != RCS_TOK_ID) {
1.50      jfb      2087:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2088:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2089:                            RCS_TOKSTR(rfp));
                   2090:                        return (-1);
                   2091:                }
                   2092:
1.110     joris    2093:                symp = (struct rcs_sym *)xmalloc(sizeof(*symp));
                   2094:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2095:                symp->rs_num = rcsnum_alloc();
                   2096:
                   2097:                type = rcs_gettok(rfp);
                   2098:                if (type != RCS_TOK_COLON) {
1.50      jfb      2099:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2100:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2101:                            RCS_TOKSTR(rfp));
1.11      joris    2102:                        rcsnum_free(symp->rs_num);
1.110     joris    2103:                        xfree(symp->rs_name);
                   2104:                        xfree(symp);
1.1       jfb      2105:                        return (-1);
                   2106:                }
                   2107:
                   2108:                type = rcs_gettok(rfp);
                   2109:                if (type != RCS_TOK_NUM) {
1.50      jfb      2110:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2111:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2112:                            RCS_TOKSTR(rfp));
1.11      joris    2113:                        rcsnum_free(symp->rs_num);
1.110     joris    2114:                        xfree(symp->rs_name);
                   2115:                        xfree(symp);
1.1       jfb      2116:                        return (-1);
                   2117:                }
                   2118:
                   2119:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2120:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2121:                            RCS_TOKSTR(rfp));
1.11      joris    2122:                        rcsnum_free(symp->rs_num);
1.110     joris    2123:                        xfree(symp->rs_name);
                   2124:                        xfree(symp);
1.1       jfb      2125:                        return (-1);
                   2126:                }
                   2127:
1.43      jfb      2128:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2129:        }
                   2130:
                   2131:        return (0);
                   2132: }
                   2133:
                   2134: /*
                   2135:  * rcs_parse_locks()
                   2136:  *
                   2137:  * Parse the lock list given as value to the `locks' keyword.
                   2138:  * Returns 0 on success, or -1 on failure.
                   2139:  */
                   2140: static int
                   2141: rcs_parse_locks(RCSFILE *rfp)
                   2142: {
                   2143:        int type;
                   2144:        struct rcs_lock *lkp;
                   2145:
                   2146:        for (;;) {
                   2147:                type = rcs_gettok(rfp);
                   2148:                if (type == RCS_TOK_SCOLON)
                   2149:                        break;
                   2150:
                   2151:                if (type != RCS_TOK_ID) {
1.50      jfb      2152:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2153:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2154:                            RCS_TOKSTR(rfp));
                   2155:                        return (-1);
                   2156:                }
                   2157:
1.110     joris    2158:                lkp = (struct rcs_lock *)xmalloc(sizeof(*lkp));
                   2159:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2160:                lkp->rl_num = rcsnum_alloc();
                   2161:
                   2162:                type = rcs_gettok(rfp);
                   2163:                if (type != RCS_TOK_COLON) {
1.50      jfb      2164:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2165:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2166:                            RCS_TOKSTR(rfp));
1.37      tedu     2167:                        rcsnum_free(lkp->rl_num);
1.110     joris    2168:                        xfree(lkp->rl_name);
                   2169:                        xfree(lkp);
1.1       jfb      2170:                        return (-1);
                   2171:                }
                   2172:
                   2173:                type = rcs_gettok(rfp);
                   2174:                if (type != RCS_TOK_NUM) {
1.50      jfb      2175:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2176:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2177:                            RCS_TOKSTR(rfp));
1.37      tedu     2178:                        rcsnum_free(lkp->rl_num);
1.110     joris    2179:                        xfree(lkp->rl_name);
                   2180:                        xfree(lkp);
1.1       jfb      2181:                        return (-1);
                   2182:                }
                   2183:
                   2184:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2185:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2186:                            RCS_TOKSTR(rfp));
1.37      tedu     2187:                        rcsnum_free(lkp->rl_num);
1.110     joris    2188:                        xfree(lkp->rl_name);
                   2189:                        xfree(lkp);
1.1       jfb      2190:                        return (-1);
                   2191:                }
                   2192:
                   2193:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2194:        }
                   2195:
                   2196:        /* check if we have a `strict' */
                   2197:        type = rcs_gettok(rfp);
                   2198:        if (type != RCS_TOK_STRICT) {
                   2199:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2200:        } else {
1.26      jfb      2201:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2202:
                   2203:                type = rcs_gettok(rfp);
                   2204:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2205:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2206:                        cvs_log(LP_ERR,
                   2207:                            "missing semi-colon after `strict' keyword");
                   2208:                        return (-1);
                   2209:                }
                   2210:        }
                   2211:
                   2212:        return (0);
                   2213: }
                   2214:
                   2215: /*
                   2216:  * rcs_parse_branches()
                   2217:  *
                   2218:  * Parse the list of branches following a `branches' keyword in a delta.
                   2219:  * Returns 0 on success, or -1 on failure.
                   2220:  */
                   2221: static int
                   2222: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2223: {
                   2224:        int type;
                   2225:        struct rcs_branch *brp;
                   2226:
                   2227:        for (;;) {
                   2228:                type = rcs_gettok(rfp);
                   2229:                if (type == RCS_TOK_SCOLON)
                   2230:                        break;
                   2231:
                   2232:                if (type != RCS_TOK_NUM) {
1.50      jfb      2233:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2234:                        cvs_log(LP_ERR,
                   2235:                            "unexpected token `%s' in list of branches",
                   2236:                            RCS_TOKSTR(rfp));
                   2237:                        return (-1);
                   2238:                }
                   2239:
1.110     joris    2240:                brp = (struct rcs_branch *)xmalloc(sizeof(*brp));
1.46      jfb      2241:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2242:                if (brp->rb_num == NULL) {
1.110     joris    2243:                        xfree(brp);
1.11      joris    2244:                        return (-1);
                   2245:                }
1.1       jfb      2246:
                   2247:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2248:        }
                   2249:
                   2250:        return (0);
                   2251: }
                   2252:
                   2253: /*
                   2254:  * rcs_freedelta()
                   2255:  *
                   2256:  * Free the contents of a delta structure.
                   2257:  */
1.18      jfb      2258: static void
1.1       jfb      2259: rcs_freedelta(struct rcs_delta *rdp)
                   2260: {
1.12      jfb      2261:        struct rcs_branch *rb;
1.1       jfb      2262:        struct rcs_delta *crdp;
                   2263:
1.12      jfb      2264:        if (rdp->rd_num != NULL)
                   2265:                rcsnum_free(rdp->rd_num);
                   2266:        if (rdp->rd_next != NULL)
                   2267:                rcsnum_free(rdp->rd_next);
                   2268:
1.1       jfb      2269:        if (rdp->rd_author != NULL)
1.110     joris    2270:                xfree(rdp->rd_author);
1.109     joris    2271:        if (rdp->rd_locker != NULL)
1.110     joris    2272:                xfree(rdp->rd_locker);
1.1       jfb      2273:        if (rdp->rd_state != NULL)
1.110     joris    2274:                xfree(rdp->rd_state);
1.1       jfb      2275:        if (rdp->rd_log != NULL)
1.110     joris    2276:                xfree(rdp->rd_log);
1.1       jfb      2277:        if (rdp->rd_text != NULL)
1.110     joris    2278:                xfree(rdp->rd_text);
1.12      jfb      2279:
                   2280:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2281:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2282:                rcsnum_free(rb->rb_num);
1.110     joris    2283:                xfree(rb);
1.12      jfb      2284:        }
1.1       jfb      2285:
                   2286:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   2287:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   2288:                rcs_freedelta(crdp);
                   2289:        }
                   2290:
1.110     joris    2291:        xfree(rdp);
1.1       jfb      2292: }
                   2293:
                   2294: /*
                   2295:  * rcs_freepdata()
                   2296:  *
                   2297:  * Free the contents of the parser data structure.
                   2298:  */
                   2299: static void
                   2300: rcs_freepdata(struct rcs_pdata *pd)
                   2301: {
                   2302:        if (pd->rp_file != NULL)
                   2303:                (void)fclose(pd->rp_file);
                   2304:        if (pd->rp_buf != NULL)
1.110     joris    2305:                xfree(pd->rp_buf);
                   2306:        xfree(pd);
1.1       jfb      2307: }
                   2308:
                   2309: /*
                   2310:  * rcs_gettok()
                   2311:  *
                   2312:  * Get the next RCS token from the string <str>.
                   2313:  */
                   2314: static int
                   2315: rcs_gettok(RCSFILE *rfp)
                   2316: {
                   2317:        u_int i;
                   2318:        int ch, last, type;
1.18      jfb      2319:        size_t len;
                   2320:        char *bp;
1.1       jfb      2321:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2322:
                   2323:        type = RCS_TOK_ERR;
                   2324:        bp = pdp->rp_buf;
1.42      jfb      2325:        pdp->rp_tlen = 0;
1.1       jfb      2326:        *bp = '\0';
                   2327:
                   2328:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2329:                type = pdp->rp_pttype;
                   2330:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   2331:                pdp->rp_pttype = RCS_TOK_ERR;
                   2332:                return (type);
                   2333:        }
                   2334:
                   2335:        /* skip leading whitespace */
                   2336:        /* XXX we must skip backspace too for compatibility, should we? */
                   2337:        do {
                   2338:                ch = getc(pdp->rp_file);
                   2339:                if (ch == '\n')
1.18      jfb      2340:                        pdp->rp_lines++;
1.1       jfb      2341:        } while (isspace(ch));
                   2342:
                   2343:        if (ch == EOF) {
                   2344:                type = RCS_TOK_EOF;
1.14      deraadt  2345:        } else if (ch == ';') {
1.1       jfb      2346:                type = RCS_TOK_SCOLON;
1.14      deraadt  2347:        } else if (ch == ':') {
1.1       jfb      2348:                type = RCS_TOK_COLON;
1.14      deraadt  2349:        } else if (isalpha(ch)) {
1.31      jfb      2350:                type = RCS_TOK_ID;
1.1       jfb      2351:                *(bp++) = ch;
1.18      jfb      2352:                for (;;) {
1.1       jfb      2353:                        ch = getc(pdp->rp_file);
1.11      joris    2354:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      2355:                                ungetc(ch, pdp->rp_file);
                   2356:                                break;
                   2357:                        }
                   2358:                        *(bp++) = ch;
1.42      jfb      2359:                        pdp->rp_tlen++;
1.18      jfb      2360:                        if (bp == pdp->rp_bufend - 1) {
                   2361:                                len = bp - pdp->rp_buf;
                   2362:                                if (rcs_growbuf(rfp) < 0) {
                   2363:                                        type = RCS_TOK_ERR;
                   2364:                                        break;
                   2365:                                }
                   2366:                                bp = pdp->rp_buf + len;
                   2367:                        }
1.1       jfb      2368:                }
                   2369:                *bp = '\0';
                   2370:
1.18      jfb      2371:                if (type != RCS_TOK_ERR) {
                   2372:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2373:                                if (strcmp(rcs_keys[i].rk_str,
                   2374:                                    pdp->rp_buf) == 0) {
                   2375:                                        type = rcs_keys[i].rk_id;
                   2376:                                        break;
                   2377:                                }
1.1       jfb      2378:                        }
                   2379:                }
1.14      deraadt  2380:        } else if (ch == '@') {
1.1       jfb      2381:                /* we have a string */
1.18      jfb      2382:                type = RCS_TOK_STRING;
1.1       jfb      2383:                for (;;) {
                   2384:                        ch = getc(pdp->rp_file);
                   2385:                        if (ch == '@') {
                   2386:                                ch = getc(pdp->rp_file);
                   2387:                                if (ch != '@') {
                   2388:                                        ungetc(ch, pdp->rp_file);
                   2389:                                        break;
                   2390:                                }
1.14      deraadt  2391:                        } else if (ch == '\n')
1.18      jfb      2392:                                pdp->rp_lines++;
1.1       jfb      2393:
                   2394:                        *(bp++) = ch;
1.42      jfb      2395:                        pdp->rp_tlen++;
1.18      jfb      2396:                        if (bp == pdp->rp_bufend - 1) {
                   2397:                                len = bp - pdp->rp_buf;
                   2398:                                if (rcs_growbuf(rfp) < 0) {
                   2399:                                        type = RCS_TOK_ERR;
                   2400:                                        break;
                   2401:                                }
                   2402:                                bp = pdp->rp_buf + len;
                   2403:                        }
1.1       jfb      2404:                }
                   2405:
                   2406:                *bp = '\0';
1.14      deraadt  2407:        } else if (isdigit(ch)) {
1.1       jfb      2408:                *(bp++) = ch;
                   2409:                last = ch;
                   2410:                type = RCS_TOK_NUM;
                   2411:
                   2412:                for (;;) {
                   2413:                        ch = getc(pdp->rp_file);
1.18      jfb      2414:                        if (bp == pdp->rp_bufend)
1.1       jfb      2415:                                break;
                   2416:                        if (!isdigit(ch) && ch != '.') {
                   2417:                                ungetc(ch, pdp->rp_file);
                   2418:                                break;
                   2419:                        }
                   2420:
                   2421:                        if (last == '.' && ch == '.') {
                   2422:                                type = RCS_TOK_ERR;
                   2423:                                break;
                   2424:                        }
                   2425:                        last = ch;
                   2426:                        *(bp++) = ch;
1.42      jfb      2427:                        pdp->rp_tlen++;
1.1       jfb      2428:                }
1.18      jfb      2429:                *bp = '\0';
1.1       jfb      2430:        }
                   2431:
                   2432:        return (type);
                   2433: }
                   2434:
                   2435: /*
                   2436:  * rcs_pushtok()
                   2437:  *
                   2438:  * Push a token back in the parser's token buffer.
                   2439:  */
                   2440: static int
                   2441: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2442: {
                   2443:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2444:
                   2445:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2446:                return (-1);
                   2447:
                   2448:        pdp->rp_pttype = type;
                   2449:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   2450:        return (0);
                   2451: }
                   2452:
1.18      jfb      2453:
                   2454: /*
                   2455:  * rcs_growbuf()
                   2456:  *
                   2457:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2458:  * RCS_BUFEXTSIZE.
                   2459:  * In case of failure, the original buffer is left unmodified.
                   2460:  * Returns 0 on success, or -1 on failure.
                   2461:  */
                   2462: static int
                   2463: rcs_growbuf(RCSFILE *rf)
                   2464: {
                   2465:        void *tmp;
                   2466:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2467:
                   2468:        tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
                   2469:        if (tmp == NULL) {
1.50      jfb      2470:                rcs_errno = RCS_ERR_ERRNO;
1.18      jfb      2471:                cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
                   2472:                return (-1);
                   2473:        }
                   2474:
                   2475:        pdp->rp_buf = (char *)tmp;
                   2476:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2477:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2478:
                   2479:        return (0);
                   2480: }
                   2481:
                   2482: /*
                   2483:  * rcs_strprint()
                   2484:  *
                   2485:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2486:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2487:  * binary data.
                   2488:  */
                   2489: static int
                   2490: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2491: {
                   2492:        const u_char *ap, *ep, *sp;
                   2493:        size_t ret;
1.52      jfb      2494:
                   2495:        if (slen == 0)
                   2496:                return (0);
1.42      jfb      2497:
                   2498:        ep = str + slen - 1;
                   2499:
                   2500:        for (sp = str; sp <= ep;)  {
                   2501:                ap = memchr(sp, '@', ep - sp);
                   2502:                if (ap == NULL)
                   2503:                        ap = ep;
                   2504:                ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
                   2505:
                   2506:                if (*ap == '@')
                   2507:                        putc('@', stream);
                   2508:                sp = ap + 1;
1.63      joris    2509:        }
                   2510:
                   2511:        return (0);
                   2512: }
                   2513:
                   2514: /*
                   2515:  * rcs_expand_keywords()
                   2516:  *
                   2517:  * Expand any RCS keywords in <line> into <out>
                   2518:  */
                   2519: static int
                   2520: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *line, char *out,
                   2521:     size_t len, int mode)
                   2522: {
                   2523:        int kwtype;
                   2524:        u_int i, j, found;
                   2525:        char *c, *kwstr, *start;
                   2526:        char expbuf[128], buf[128];
                   2527:
1.65      niallo   2528:        kwtype = 0;
                   2529:        kwstr = NULL;
1.63      joris    2530:        i = 0;
                   2531:
                   2532:        /*
                   2533:         * Keyword formats:
                   2534:         * $Keyword$
                   2535:         * $Keyword: value$
                   2536:         */
                   2537:        memset(out, '\0', len);
                   2538:        for (c = line; *c != '\0' && i < len; *c++) {
                   2539:                out[i++] = *c;
                   2540:                if (*c == '$') {
                   2541:                        /* remember start of this possible keyword */
                   2542:                        start = c;
                   2543:
                   2544:                        /* first following character has to be alphanumeric */
                   2545:                        *c++;
                   2546:                        if (!isalpha(*c)) {
                   2547:                                c = start;
                   2548:                                continue;
                   2549:                        }
                   2550:
                   2551:                        /* look for any matching keywords */
                   2552:                        found = 0;
                   2553:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2554:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2555:                                    strlen(rcs_expkw[j].kw_str))) {
                   2556:                                        found = 1;
                   2557:                                        kwstr = rcs_expkw[j].kw_str;
                   2558:                                        kwtype = rcs_expkw[j].kw_type;
                   2559:                                        break;
                   2560:                                }
                   2561:                        }
                   2562:
                   2563:                        /* unknown keyword, continue looking */
                   2564:                        if (found == 0) {
                   2565:                                c = start;
                   2566:                                continue;
                   2567:                        }
                   2568:
                   2569:                        /* next character has to be ':' or '$' */
                   2570:                        c += strlen(kwstr);
                   2571:                        if (*c != ':' && *c != '$') {
                   2572:                                c = start;
                   2573:                                continue;
                   2574:                        }
                   2575:
                   2576:                        /*
                   2577:                         * if the next character was ':' we need to look for
                   2578:                         * an '$' before the end of the line to be sure it is
                   2579:                         * in fact a keyword.
                   2580:                         */
                   2581:                        if (*c == ':') {
                   2582:                                while (*c++) {
                   2583:                                        if (*c == '$' || *c == '\n')
                   2584:                                                break;
                   2585:                                }
                   2586:
                   2587:                                if (*c != '$') {
                   2588:                                        c = start;
                   2589:                                        continue;
                   2590:                                }
                   2591:                        }
                   2592:
                   2593:                        /* start constructing the expansion */
                   2594:                        expbuf[0] = '\0';
                   2595:
                   2596:                        if (mode & RCS_KWEXP_NAME) {
                   2597:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2598:                                strlcat(expbuf, kwstr, sizeof(expbuf));
                   2599:                                if (mode & RCS_KWEXP_VAL)
                   2600:                                        strlcat(expbuf, ": ", sizeof(expbuf));
                   2601:                        }
                   2602:
                   2603:                        /*
1.80      reyk     2604:                         * order matters because of RCS_KW_ID and
                   2605:                         * RCS_KW_HEADER here
1.63      joris    2606:                         */
                   2607:                        if (mode & RCS_KWEXP_VAL) {
                   2608:                                if (kwtype & RCS_KW_RCSFILE) {
                   2609:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.80      reyk     2610:                                                strlcat(expbuf,
                   2611:                                                    basename(rcsfile),
1.63      joris    2612:                                                    sizeof(expbuf));
                   2613:                                        else
                   2614:                                                strlcat(expbuf, rcsfile,
                   2615:                                                    sizeof(expbuf));
                   2616:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2617:                                }
                   2618:
                   2619:                                if (kwtype & RCS_KW_REVISION) {
1.80      reyk     2620:                                        rcsnum_tostr(rdp->rd_num, buf,
                   2621:                                            sizeof(buf));
1.63      joris    2622:                                        strlcat(buf, " ", sizeof(buf));
                   2623:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2624:                                }
                   2625:
                   2626:                                if (kwtype & RCS_KW_DATE) {
                   2627:                                        strftime(buf, sizeof(buf),
1.80      reyk     2628:                                            "%Y/%m/%d %H:%M:%S ",
                   2629:                                            &rdp->rd_date);
1.63      joris    2630:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2631:                                }
                   2632:
                   2633:                                if (kwtype & RCS_KW_AUTHOR) {
                   2634:                                        strlcat(expbuf, rdp->rd_author,
                   2635:                                            sizeof(expbuf));
                   2636:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2637:                                }
                   2638:
                   2639:                                if (kwtype & RCS_KW_STATE) {
                   2640:                                        strlcat(expbuf, rdp->rd_state,
                   2641:                                            sizeof(expbuf));
                   2642:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2643:                                }
                   2644:
                   2645:                                /* order does not matter anymore below */
                   2646:                                if (kwtype & RCS_KW_LOG)
                   2647:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2648:
                   2649:                                if (kwtype & RCS_KW_SOURCE) {
1.80      reyk     2650:                                        strlcat(expbuf, rcsfile,
                   2651:                                            sizeof(expbuf));
1.63      joris    2652:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2653:                                }
                   2654:
                   2655:                                if (kwtype & RCS_KW_NAME)
                   2656:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2657:                        }
                   2658:
                   2659:                        /* end the expansion */
                   2660:                        if (mode & RCS_KWEXP_NAME)
                   2661:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2662:
                   2663:                        out[--i] = '\0';
                   2664:                        strlcat(out, expbuf, len);
                   2665:                        i += strlen(expbuf);
                   2666:                }
1.42      jfb      2667:        }
1.81      niallo   2668:
                   2669:        return (0);
                   2670: }
                   2671:
                   2672: /*
                   2673:  * rcs_deltatext_set()
                   2674:  *
                   2675:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      2676:  * Returns -1 on error, 0 on success.
1.81      niallo   2677:  */
                   2678: int
                   2679: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2680: {
                   2681:        size_t len;
                   2682:        struct rcs_delta *rdp;
                   2683:
                   2684:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2685:                return (-1);
                   2686:
                   2687:        if (rdp->rd_text != NULL)
1.110     joris    2688:                xfree(rdp->rd_text);
1.81      niallo   2689:
                   2690:        len = strlen(dtext);
1.103     joris    2691:        if (len != 0) {
1.110     joris    2692:                rdp->rd_text = (u_char *)xmalloc(len);
1.103     joris    2693:                rdp->rd_tlen = len - 1;
                   2694:                strlcpy(rdp->rd_text, dtext, len);
                   2695:        } else {
                   2696:                rdp->rd_text = NULL;
                   2697:                rdp->rd_tlen = 0;
                   2698:        }
1.18      jfb      2699:
1.86      joris    2700:        return (0);
                   2701: }
                   2702:
                   2703: /*
                   2704:  * rcs_rev_setlog()
                   2705:  *
                   2706:  * Sets the log message of revision <rev> to <logtext>
                   2707:  */
                   2708: int
                   2709: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2710: {
                   2711:        struct rcs_delta *rdp;
                   2712:        char buf[16];
                   2713:
                   2714:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2715:
                   2716:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2717:                return (-1);
                   2718:
                   2719:        if (rdp->rd_log != NULL)
1.110     joris    2720:                xfree(rdp->rd_log);
1.86      joris    2721:
1.110     joris    2722:        rdp->rd_log = xstrdup(logtext);
1.86      joris    2723:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   2724:        return (0);
                   2725: }
1.97      niallo   2726: /*
                   2727:  * rcs_rev_getdate()
                   2728:  *
                   2729:  * Get the date corresponding to a given revision.
                   2730:  * Returns the date on success, -1 on failure.
                   2731:  */
                   2732: time_t
                   2733: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2734: {
                   2735:        struct rcs_delta *rdp;
                   2736:
                   2737:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2738:                return (-1);
                   2739:
                   2740:        return (mktime(&rdp->rd_date));
                   2741: }
1.95      niallo   2742:
                   2743: /*
                   2744:  * rcs_state_set()
                   2745:  *
                   2746:  * Sets the state of revision <rev> to <state>
                   2747:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2748:  *
                   2749:  * Returns -1 on failure, 0 on success.
                   2750:  */
                   2751: int
                   2752: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2753: {
                   2754:        struct rcs_delta *rdp;
                   2755:
                   2756:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2757:                return (-1);
                   2758:
                   2759:        if (rdp->rd_state != NULL)
1.110     joris    2760:                xfree(rdp->rd_state);
1.95      niallo   2761:
1.110     joris    2762:        rdp->rd_state = xstrdup(state);
1.95      niallo   2763:
                   2764:        rfp->rf_flags &= ~RCS_SYNCED;
                   2765:
                   2766:        return (0);
                   2767: }
                   2768:
                   2769: /*
                   2770:  * rcs_state_check()
                   2771:  *
                   2772:  * Check if string <state> is valid.
                   2773:  *
1.96      xsa      2774:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   2775:  */
                   2776: int
                   2777: rcs_state_check(const char *state)
                   2778: {
                   2779:        if (strchr(state, ' ') != NULL)
                   2780:                return (-1);
                   2781:
1.18      jfb      2782:        return (0);
1.1       jfb      2783: }
1.97      niallo   2784:
                   2785: /*
                   2786:  * rcs_state_get()
                   2787:  *
                   2788:  * Get the state for a given revision of a specified RCSFILE.
                   2789:  *
                   2790:  * Returns NULL on failure.
                   2791:  */
                   2792: const char *
                   2793: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2794: {
                   2795:        struct rcs_delta *rdp;
                   2796:
                   2797:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2798:                return (NULL);
                   2799:
                   2800:        return (rdp->rd_state);
                   2801: }
                   2802:
1.105     joris    2803:
1.106     joris    2804: #if !defined(RCSPROG)
                   2805:
1.105     joris    2806: static char *month_tab[] = {
                   2807:        "Jan",
                   2808:        "Feb",
                   2809:        "Mar",
                   2810:        "Apr",
                   2811:        "May",
                   2812:        "Jun",
                   2813:        "Jul",
                   2814:        "Aug",
                   2815:        "Sep",
                   2816:        "Oct",
                   2817:        "Nov",
                   2818:        "Dec"
                   2819: };
                   2820:
                   2821: /*
                   2822:  * Checkout a certain revision <rev> of RCS file <rf> to either standard
                   2823:  * output when running in server mode, or to <fpath> when running in local mode.
                   2824:  *
                   2825:  * If type is CHECKOUT_REV_MERGED we have an extra argument, which
                   2826:  * is the buffer containing the merged file.
                   2827:  *
                   2828:  * If type is CHECKOUT_REV_REMOVED, the file has been removed and we
                   2829:  * need to do the same thing.
                   2830:  */
                   2831: int
                   2832: cvs_checkout_rev(RCSFILE *rf, RCSNUM *rev, CVSFILE *cf, char *fpath,
                   2833:     int local, int type, ...)
                   2834: {
                   2835:        BUF *bp;
                   2836:        int l, ret, fsize;
                   2837:        char timebuf[32], entry[MAXPATHLEN], copyfile[MAXPATHLEN];
                   2838:        char *content, *repo, buf[MAXPATHLEN], modestr[16];
                   2839:        struct cvsroot *root;
                   2840:        struct cvs_ent *ent;
                   2841:        va_list ap;
                   2842:        time_t rcstime;
                   2843:        struct timeval tv[2];
                   2844:        struct tm *tp;
                   2845:        RCSNUM *oldrev;
                   2846:
                   2847:        bp = NULL;
                   2848:        ret = -1;
                   2849:        content = NULL;
                   2850:        oldrev = NULL;
                   2851:
                   2852:        if ((type != CHECKOUT_REV_MERGED) && (type != CHECKOUT_REV_REMOVED)) {
                   2853:                /* fetch the contents of the revision */
                   2854:                if ((bp = rcs_getrev(rf, rev)) == NULL) {
                   2855:                        cvs_log(LP_ERR, "revision '%s' not found in file '%s'",
                   2856:                            rcsnum_tostr(rev, buf, sizeof(buf)), fpath);
                   2857:                        goto out;
                   2858:                }
                   2859:        } else if (type != CHECKOUT_REV_REMOVED) {
                   2860:                va_start(ap, type);
                   2861:                bp = va_arg(ap, BUF *);
                   2862:                va_end(ap);
                   2863:        }
                   2864:
                   2865:        if (type == CHECKOUT_REV_CREATED)
                   2866:                rcstime = rcs_rev_getdate(rf, rev);
1.108     joris    2867:        else if (type == CHECKOUT_REV_MERGED ||
                   2868:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    2869:                time(&rcstime);
1.108     joris    2870:                if ((rcstime = cvs_hack_time(rcstime, 1)) < 0)
                   2871:                        goto out;
                   2872:        }
1.105     joris    2873:
1.108     joris    2874:        if (type == CHECKOUT_REV_CREATED ||
                   2875:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    2876:                ctime_r(&rcstime, timebuf);
                   2877:                l = strlen(timebuf);
                   2878:                if ((l > 0) && (timebuf[l - 1] == '\n'))
                   2879:                        timebuf[--l] = '\0';
                   2880:
                   2881:                l = snprintf(entry, sizeof(entry), "/%s/%s/%s/%s/", cf->cf_name,
                   2882:                    rcsnum_tostr(rev, buf, sizeof(buf)),
                   2883:                    (local == 1) ? timebuf : "",
                   2884:                    (type == CHECKOUT_REV_MERGED) ? "+=" : "");
                   2885:                if (l == -1 || l >= (int)sizeof(buf))
                   2886:                        goto out;
                   2887:        }
                   2888:
                   2889:        if (type == CHECKOUT_REV_MERGED) {
1.111     joris    2890:                oldrev = rcsnum_alloc();
                   2891:                rcsnum_cpy(rev, oldrev, 0);
1.105     joris    2892:
                   2893:                if (rcsnum_dec(oldrev) == NULL)
                   2894:                        goto out;
                   2895:
                   2896:                l = snprintf(copyfile, sizeof(copyfile), ".#%s.%s",
                   2897:                    cf->cf_name, rcsnum_tostr(oldrev, buf, sizeof(buf)));
                   2898:                if (l == -1 || l >= (int)sizeof(copyfile))
                   2899:                        goto out;
                   2900:        }
                   2901:
                   2902:        root = CVS_DIR_ROOT(cf);
                   2903:        repo = CVS_DIR_REPO(cf);
                   2904:
                   2905:        /*
                   2906:         * In local mode, just copy the entire contents to fpath.
                   2907:         * In server mode, we need to send it to the client together with
                   2908:         * some responses.
                   2909:         */
                   2910:        if (local) {
                   2911:                l = 0;
                   2912:                if (cf->cf_entry == NULL) {
                   2913:                        l = 1;
                   2914:                        cf->cf_entry = cvs_ent_open(cf->cf_dir, O_RDWR);
                   2915:                        if (cf->cf_entry == NULL) {
                   2916:                                cvs_log(LP_ERR,
                   2917:                                    "failed to open Entry file '%s'", cf->cf_dir);
                   2918:                                goto out;
                   2919:                        }
                   2920:                }
                   2921:
                   2922:                cvs_ent_remove(cf->cf_entry, cf->cf_name, 1);
                   2923:                if (type != CHECKOUT_REV_REMOVED) {
                   2924:                        cvs_ent_addln(cf->cf_entry, entry);
                   2925:                        ent = cvs_ent_get(cf->cf_entry, cf->cf_name);
                   2926:                        ent->processed = 1;
                   2927:                }
                   2928:
                   2929:                if (l == 1)
                   2930:                        cvs_ent_close(cf->cf_entry);
                   2931:
                   2932:                switch (type) {
                   2933:                case CHECKOUT_REV_REMOVED:
                   2934:                        if (cvs_unlink(fpath) < 0)
                   2935:                                goto out;
                   2936:                        break;
                   2937:                case CHECKOUT_REV_MERGED:
                   2938:                        /* XXX move the old file when merging */
1.108     joris    2939:                case CHECKOUT_REV_UPDATED:
1.105     joris    2940:                case CHECKOUT_REV_CREATED:
                   2941:                        if (cvs_buf_write(bp, fpath, cf->cf_mode) < 0) {
                   2942:                                cvs_log(LP_ERR, "failed to update file '%s'",
                   2943:                                    fpath);
                   2944:                                goto out;
                   2945:                        }
                   2946:
1.107     joris    2947:                        /*
                   2948:                         * correct the time first
                   2949:                         */
                   2950:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   2951:                                goto out;
                   2952:
1.105     joris    2953:                        tv[0].tv_sec = rcstime;
                   2954:                        tv[0].tv_usec = 0;
                   2955:                        tv[1] = tv[0];
                   2956:                        if (utimes(fpath, tv) == -1)
                   2957:                                cvs_log(LP_ERRNO, "failed to set timestamps");
                   2958:                        break;
                   2959:                }
                   2960:        } else {
                   2961:                /* sanity */
                   2962:                if (cf->cf_type != DT_REG) {
                   2963:                        cvs_log(LP_ERR, "cvs_checkout_rev: none DT_REG file");
                   2964:                        goto out;
                   2965:                }
                   2966:
                   2967:                /*
                   2968:                 * if we are removing a file, we don't need this stuff.
                   2969:                 */
                   2970:                if (type != CHECKOUT_REV_REMOVED) {
1.107     joris    2971:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   2972:                                goto out;
                   2973:
1.105     joris    2974:                        tp = gmtime(&rcstime);
                   2975:                        l = snprintf(timebuf, sizeof(timebuf),
                   2976:                            "%02d %s %d %02d:%02d:%02d -0000",
                   2977:                            tp->tm_mday, month_tab[tp->tm_mon],
                   2978:                            tp->tm_year + 1900, tp->tm_hour,
                   2979:                            tp->tm_min, tp->tm_sec);
                   2980:                        if (l == -1 || l >= (int)sizeof(timebuf))
                   2981:                                goto out;
                   2982:
                   2983:                        fsize = cvs_buf_len(bp);
                   2984:                        cvs_modetostr(cf->cf_mode, modestr, sizeof(modestr));
                   2985:                        if (cvs_buf_putc(bp, '\0') < 0)
                   2986:                                goto out;
                   2987:                        content = cvs_buf_release(bp);
                   2988:                        bp = NULL;
                   2989:                }
                   2990:
                   2991:                if (type == CHECKOUT_REV_MERGED) {
                   2992:                        printf("Copy-file %s/\n", (cf->cf_dir != NULL) ?
                   2993:                            cf->cf_dir : ".");
                   2994:                        printf("%s/%s/%s\n", root->cr_dir, repo, cf->cf_name);
                   2995:                        printf("%s\n", copyfile);
                   2996:                }
                   2997:
                   2998:                switch (type) {
                   2999:                case CHECKOUT_REV_MERGED:
                   3000:                        printf("Merged");
                   3001:                        break;
                   3002:                case CHECKOUT_REV_REMOVED:
                   3003:                        printf("Removed");
                   3004:                        break;
                   3005:                case CHECKOUT_REV_CREATED:
                   3006:                        printf("Mod-time %s\n", timebuf);
                   3007:                        printf("Created");
                   3008:                        break;
                   3009:                default:
                   3010:                        cvs_log(LP_ERR, "cvs_checkout_rev: bad type %d",
                   3011:                            type);
                   3012:                        goto out;
                   3013:                }
                   3014:
                   3015:                printf(" %s/\n", (cf->cf_dir != NULL) ? cf->cf_dir : ".");
                   3016:                printf("%s/%s\n", repo, cf->cf_name);
                   3017:
                   3018:                if (type != CHECKOUT_REV_REMOVED) {
                   3019:                        printf("%s\n", entry);
                   3020:                        printf("%s\n%d\n%s", modestr, fsize, content);
                   3021:                }
                   3022:        }
                   3023:
                   3024:        ret = 0;
                   3025:
                   3026: out:
                   3027:        if (oldrev != NULL)
                   3028:                rcsnum_free(oldrev);
                   3029:        if (bp != NULL)
                   3030:                cvs_buf_free(bp);
                   3031:        if (content != NULL)
1.110     joris    3032:                xfree(content);
1.105     joris    3033:
                   3034:        return (ret);
                   3035: }
1.106     joris    3036:
                   3037: #endif