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

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