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

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