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

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