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

1.141   ! ray         1: /*     $OpenBSD: rcs.c,v 1.140 2006/03/09 10:56:33 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.124     xsa       241: static int     rcs_parse_deltas(RCSFILE *, RCSNUM *);
1.57      xsa       242: static int     rcs_parse_deltatext(RCSFILE *);
1.124     xsa       243: static int     rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
                    244: static int     rcs_parse_desc(RCSFILE *, RCSNUM *);
1.57      xsa       245:
                    246: static int     rcs_parse_access(RCSFILE *);
                    247: static int     rcs_parse_symbols(RCSFILE *);
                    248: static int     rcs_parse_locks(RCSFILE *);
                    249: static int     rcs_parse_branches(RCSFILE *, struct rcs_delta *);
                    250: static void    rcs_freedelta(struct rcs_delta *);
                    251: static void    rcs_freepdata(struct rcs_pdata *);
                    252: static int     rcs_gettok(RCSFILE *);
                    253: static int     rcs_pushtok(RCSFILE *, const char *, int);
                    254: static int     rcs_growbuf(RCSFILE *);
                    255: static int     rcs_strprint(const u_char *, size_t, FILE *);
                    256:
1.131     niallo    257: static char*   rcs_expand_keywords(char *, struct rcs_delta *, char *,
1.125     niallo    258:                     size_t, int);
1.26      jfb       259:
1.1       jfb       260: /*
                    261:  * rcs_open()
                    262:  *
                    263:  * Open a file containing RCS-formatted information.  The file's path is
1.26      jfb       264:  * given in <path>, and the opening flags are given in <flags>, which is either
                    265:  * RCS_READ, RCS_WRITE, or RCS_RDWR.  If the open requests write access and
                    266:  * the file does not exist, the RCS_CREATE flag must also be given, in which
                    267:  * case it will be created with the mode specified in a third argument of
                    268:  * type mode_t.  If the file exists and RCS_CREATE is passed, the open will
                    269:  * fail.
1.1       jfb       270:  * Returns a handle to the opened file on success, or NULL on failure.
                    271:  */
1.60      xsa       272: RCSFILE *
1.26      jfb       273: rcs_open(const char *path, int flags, ...)
1.1       jfb       274: {
1.26      jfb       275:        int ret;
                    276:        mode_t fmode;
1.1       jfb       277:        RCSFILE *rfp;
                    278:        struct stat st;
1.26      jfb       279:        va_list vap;
1.109     joris     280:        struct rcs_delta *rdp;
                    281:        struct rcs_lock *lkr;
1.26      jfb       282:
                    283:        fmode = 0;
                    284:        flags &= 0xffff;        /* ditch any internal flags */
1.1       jfb       285:
1.26      jfb       286:        if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
                    287:                if (flags & RCS_CREATE) {
                    288:                        va_start(vap, flags);
                    289:                        fmode = va_arg(vap, mode_t);
                    290:                        va_end(vap);
                    291:                } else {
1.105     joris     292:                        /* XXX, make this command dependant? */
                    293: #if 0
1.26      jfb       294:                        cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
1.105     joris     295: #endif
1.127     xsa       296:                        rcs_errno = RCS_ERR_NOENT;
1.26      jfb       297:                        return (NULL);
                    298:                }
                    299:        } else if ((ret == 0) && (flags & RCS_CREATE)) {
                    300:                cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1       jfb       301:                return (NULL);
                    302:        }
                    303:
1.110     joris     304:        rfp = (RCSFILE *)xmalloc(sizeof(*rfp));
1.1       jfb       305:        memset(rfp, 0, sizeof(*rfp));
                    306:
1.110     joris     307:        rfp->rf_path = xstrdup(path);
1.26      jfb       308:        rfp->rf_flags = flags | RCS_SLOCK;
                    309:        rfp->rf_mode = fmode;
1.1       jfb       310:
                    311:        TAILQ_INIT(&(rfp->rf_delta));
1.29      jfb       312:        TAILQ_INIT(&(rfp->rf_access));
1.1       jfb       313:        TAILQ_INIT(&(rfp->rf_symbols));
                    314:        TAILQ_INIT(&(rfp->rf_locks));
                    315:
1.122     reyk      316:        if (!(rfp->rf_flags & RCS_CREATE)
1.120     niallo    317:            && (rcs_parse_init(rfp) < 0)) {
1.1       jfb       318:                rcs_close(rfp);
                    319:                return (NULL);
                    320:        }
                    321:
1.109     joris     322:        /* fill in rd_locker */
                    323:        TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
                    324:                if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
                    325:                        rcs_close(rfp);
                    326:                        return (NULL);
                    327:                }
                    328:
1.110     joris     329:                rdp->rd_locker = xstrdup(lkr->rl_name);
1.109     joris     330:        }
                    331:
1.1       jfb       332:        return (rfp);
                    333: }
                    334:
                    335: /*
                    336:  * rcs_close()
                    337:  *
                    338:  * Close an RCS file handle.
                    339:  */
                    340: void
                    341: rcs_close(RCSFILE *rfp)
                    342: {
                    343:        struct rcs_delta *rdp;
1.71      moritz    344:        struct rcs_access *rap;
1.13      jfb       345:        struct rcs_lock *rlp;
                    346:        struct rcs_sym *rsp;
1.1       jfb       347:
1.26      jfb       348:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    349:                rcs_write(rfp);
                    350:
1.1       jfb       351:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    352:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    353:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    354:                rcs_freedelta(rdp);
1.71      moritz    355:        }
                    356:
                    357:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    358:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    359:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
1.110     joris     360:                xfree(rap->ra_name);
                    361:                xfree(rap);
1.1       jfb       362:        }
                    363:
1.13      jfb       364:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    365:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    366:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    367:                rcsnum_free(rsp->rs_num);
1.110     joris     368:                xfree(rsp->rs_name);
                    369:                xfree(rsp);
1.13      jfb       370:        }
                    371:
                    372:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    373:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    374:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    375:                rcsnum_free(rlp->rl_num);
1.110     joris     376:                xfree(rlp->rl_name);
                    377:                xfree(rlp);
1.13      jfb       378:        }
                    379:
1.1       jfb       380:        if (rfp->rf_head != NULL)
                    381:                rcsnum_free(rfp->rf_head);
1.11      joris     382:        if (rfp->rf_branch != NULL)
                    383:                rcsnum_free(rfp->rf_branch);
1.1       jfb       384:
                    385:        if (rfp->rf_path != NULL)
1.110     joris     386:                xfree(rfp->rf_path);
1.1       jfb       387:        if (rfp->rf_comment != NULL)
1.110     joris     388:                xfree(rfp->rf_comment);
1.1       jfb       389:        if (rfp->rf_expand != NULL)
1.110     joris     390:                xfree(rfp->rf_expand);
1.1       jfb       391:        if (rfp->rf_desc != NULL)
1.110     joris     392:                xfree(rfp->rf_desc);
1.118     joris     393:        if (rfp->rf_pdata != NULL)
                    394:                rcs_freepdata(rfp->rf_pdata);
1.110     joris     395:        xfree(rfp);
1.1       jfb       396: }
                    397:
                    398: /*
                    399:  * rcs_write()
                    400:  *
                    401:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    402:  * path is in <rf_path>.
                    403:  * Returns 0 on success, or -1 on failure.
                    404:  */
1.27      jfb       405: static int
1.1       jfb       406: rcs_write(RCSFILE *rfp)
                    407: {
                    408:        FILE *fp;
1.72      niallo    409:        char buf[1024], numbuf[64], fn[19] = "";
                    410:        void *bp;
1.29      jfb       411:        struct rcs_access *ap;
1.1       jfb       412:        struct rcs_sym *symp;
1.46      jfb       413:        struct rcs_branch *brp;
1.1       jfb       414:        struct rcs_delta *rdp;
1.75      joris     415:        struct rcs_lock *lkp;
1.72      niallo    416:        ssize_t nread;
1.100     xsa       417:        size_t len;
1.72      niallo    418:        int fd, from_fd, to_fd;
1.75      joris     419:
1.72      niallo    420:        from_fd = to_fd = fd = -1;
1.1       jfb       421:
1.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;
                    900:        else {
                    901:                cvs_log(LP_ERRNO, "invalid lock mode %d", mode);
                    902:                return (-1);
                    903:        }
                    904:
1.70      moritz    905:        file->rf_flags &= ~RCS_SYNCED;
1.30      jfb       906:        return (pmode);
1.27      jfb       907: }
                    908:
                    909: /*
1.40      jfb       910:  * rcs_lock_add()
                    911:  *
                    912:  * Add an RCS lock for the user <user> on revision <rev>.
                    913:  * Returns 0 on success, or -1 on failure.
                    914:  */
                    915: int
                    916: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
                    917: {
                    918:        struct rcs_lock *lkp;
                    919:
                    920:        /* first look for duplication */
                    921:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.109     joris     922:                if ((strcmp(lkp->rl_name, user) == 0) &&
                    923:                    (rcsnum_cmp(rev, lkp->rl_num, 0) == 0)) {
1.40      jfb       924:                        rcs_errno = RCS_ERR_DUPENT;
                    925:                        return (-1);
                    926:                }
                    927:        }
                    928:
1.110     joris     929:        lkp = (struct rcs_lock *)xmalloc(sizeof(*lkp));
                    930:        lkp->rl_name = xstrdup(user);
1.111     joris     931:        lkp->rl_num = rcsnum_alloc();
1.68      moritz    932:        rcsnum_cpy(rev, lkp->rl_num, 0);
1.40      jfb       933:
                    934:        TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
                    935:
                    936:        /* not synced anymore */
                    937:        file->rf_flags &= ~RCS_SYNCED;
                    938:        return (0);
                    939:
                    940:
                    941: }
                    942:
                    943:
                    944: /*
                    945:  * rcs_lock_remove()
                    946:  *
                    947:  * Remove the RCS lock on revision <rev>.
                    948:  * Returns 0 on success, or -1 on failure.
                    949:  */
                    950: int
1.109     joris     951: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
1.40      jfb       952: {
                    953:        struct rcs_lock *lkp;
                    954:
1.109     joris     955:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    956:                if ((strcmp(lkp->rl_name, user) == 0) &&
                    957:                    (rcsnum_cmp(lkp->rl_num, rev, 0) == 0))
1.40      jfb       958:                        break;
1.109     joris     959:        }
1.40      jfb       960:
                    961:        if (lkp == NULL) {
                    962:                rcs_errno = RCS_ERR_NOENT;
                    963:                return (-1);
                    964:        }
                    965:
                    966:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
                    967:        rcsnum_free(lkp->rl_num);
1.110     joris     968:        xfree(lkp->rl_name);
                    969:        xfree(lkp);
1.40      jfb       970:
                    971:        /* not synced anymore */
                    972:        file->rf_flags &= ~RCS_SYNCED;
                    973:        return (0);
                    974: }
                    975:
                    976: /*
1.27      jfb       977:  * rcs_desc_get()
                    978:  *
                    979:  * Retrieve the description for the RCS file <file>.
                    980:  */
1.57      xsa       981: const char *
1.27      jfb       982: rcs_desc_get(RCSFILE *file)
                    983: {
                    984:        return (file->rf_desc);
                    985: }
                    986:
                    987: /*
                    988:  * rcs_desc_set()
                    989:  *
                    990:  * Set the description for the RCS file <file>.
                    991:  * Returns 0 on success, or -1 on failure.
                    992:  */
                    993: int
                    994: rcs_desc_set(RCSFILE *file, const char *desc)
                    995: {
                    996:        char *tmp;
                    997:
1.110     joris     998:        tmp = xstrdup(desc);
1.27      jfb       999:        if (file->rf_desc != NULL)
1.110     joris    1000:                xfree(file->rf_desc);
1.27      jfb      1001:        file->rf_desc = tmp;
                   1002:        file->rf_flags &= ~RCS_SYNCED;
                   1003:
                   1004:        return (0);
1.51      jfb      1005: }
                   1006:
                   1007: /*
                   1008:  * rcs_comment_lookup()
                   1009:  *
                   1010:  * Lookup the assumed comment leader based on a file's suffix.
                   1011:  * Returns a pointer to the string on success, or NULL on failure.
                   1012:  */
1.57      xsa      1013: const char *
1.51      jfb      1014: rcs_comment_lookup(const char *filename)
                   1015: {
                   1016:        int i;
                   1017:        const char *sp;
                   1018:
                   1019:        if ((sp = strrchr(filename, '.')) == NULL) {
                   1020:                rcs_errno = RCS_ERR_NOENT;
                   1021:                return (NULL);
                   1022:        }
                   1023:        sp++;
                   1024:
                   1025:        for (i = 0; i < (int)NB_COMTYPES; i++)
                   1026:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                   1027:                        return (rcs_comments[i].rc_cstr);
                   1028:        return (NULL);
1.27      jfb      1029: }
                   1030:
1.33      jfb      1031: /*
                   1032:  * rcs_comment_get()
                   1033:  *
                   1034:  * Retrieve the comment leader for the RCS file <file>.
                   1035:  */
1.57      xsa      1036: const char *
1.33      jfb      1037: rcs_comment_get(RCSFILE *file)
                   1038: {
                   1039:        return (file->rf_comment);
                   1040: }
                   1041:
                   1042: /*
                   1043:  * rcs_comment_set()
                   1044:  *
                   1045:  * Set the comment leader for the RCS file <file>.
                   1046:  * Returns 0 on success, or -1 on failure.
                   1047:  */
                   1048: int
                   1049: rcs_comment_set(RCSFILE *file, const char *comment)
                   1050: {
                   1051:        char *tmp;
                   1052:
1.110     joris    1053:        tmp = xstrdup(comment);
1.33      jfb      1054:        if (file->rf_comment != NULL)
1.110     joris    1055:                xfree(file->rf_comment);
1.33      jfb      1056:        file->rf_comment = tmp;
                   1057:        file->rf_flags &= ~RCS_SYNCED;
                   1058:
                   1059:        return (0);
                   1060: }
1.40      jfb      1061:
                   1062: /*
                   1063:  * rcs_tag_resolve()
                   1064:  *
                   1065:  * Retrieve the revision number corresponding to the tag <tag> for the RCS
                   1066:  * file <file>.
                   1067:  */
1.60      xsa      1068: RCSNUM *
1.40      jfb      1069: rcs_tag_resolve(RCSFILE *file, const char *tag)
                   1070: {
                   1071:        RCSNUM *num;
                   1072:
                   1073:        if ((num = rcsnum_parse(tag)) == NULL) {
                   1074:                num = rcs_sym_getrev(file, tag);
                   1075:        }
                   1076:
                   1077:        return (num);
                   1078: }
                   1079:
1.94      joris    1080: int
                   1081: rcs_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
1.5       vincent  1082: {
                   1083:        char op, *ep;
1.94      joris    1084:        struct cvs_line *lp, *dlp, *ndlp;
1.5       vincent  1085:        int i, lineno, nbln;
1.1       jfb      1086:
1.94      joris    1087:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                   1088:        lp = TAILQ_FIRST(&(plines->l_lines));
1.1       jfb      1089:
                   1090:        /* skip first bogus line */
1.94      joris    1091:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1092:            lp = TAILQ_NEXT(lp, l_list)) {
                   1093:                op = *(lp->l_line);
                   1094:                lineno = (int)strtol((lp->l_line + 1), &ep, 10);
                   1095:                if ((lineno > dlines->l_nblines) || (lineno < 0) ||
1.132     niallo   1096:                    (*ep != ' '))
                   1097:                        fatal("invalid line specification in RCS patch");
1.1       jfb      1098:                ep++;
                   1099:                nbln = (int)strtol(ep, &ep, 10);
1.132     niallo   1100:                if ((nbln < 0) || (*ep != '\0'))
                   1101:                        fatal("invalid line number specification in RCS patch");
1.1       jfb      1102:
                   1103:                /* find the appropriate line */
                   1104:                for (;;) {
                   1105:                        if (dlp == NULL)
                   1106:                                break;
1.94      joris    1107:                        if (dlp->l_lineno == lineno)
1.1       jfb      1108:                                break;
1.94      joris    1109:                        if (dlp->l_lineno > lineno) {
                   1110:                                dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
                   1111:                        } else if (dlp->l_lineno < lineno) {
1.133     niallo   1112:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.138     deraadt  1113:                                    (ndlp->l_lineno > lineno))
1.1       jfb      1114:                                        break;
                   1115:                                dlp = ndlp;
                   1116:                        }
                   1117:                }
1.132     niallo   1118:                if (dlp == NULL)
                   1119:                        fatal("can't find referenced line in RCS patch");
1.1       jfb      1120:
                   1121:                if (op == 'd') {
                   1122:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94      joris    1123:                                ndlp = TAILQ_NEXT(dlp, l_list);
                   1124:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.1       jfb      1125:                                dlp = ndlp;
1.133     niallo   1126:                                /* last line is gone - reset dlp */
                   1127:                                if (dlp == NULL) {
                   1128:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
                   1129:                                            cvs_tqh);
                   1130:                                        dlp = ndlp;
                   1131:                                }
1.1       jfb      1132:                        }
1.14      deraadt  1133:                } else if (op == 'a') {
1.1       jfb      1134:                        for (i = 0; i < nbln; i++) {
                   1135:                                ndlp = lp;
1.94      joris    1136:                                lp = TAILQ_NEXT(lp, l_list);
1.132     niallo   1137:                                if (lp == NULL)
                   1138:                                        fatal("truncated RCS patch");
1.94      joris    1139:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
                   1140:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1141:                                    lp, l_list);
1.1       jfb      1142:                                dlp = lp;
                   1143:
                   1144:                                /* we don't want lookup to block on those */
1.94      joris    1145:                                lp->l_lineno = lineno;
1.1       jfb      1146:
                   1147:                                lp = ndlp;
                   1148:                        }
1.132     niallo   1149:                } else
                   1150:                        fatal("unknown RCS patch operation `%c'", op);
1.1       jfb      1151:
                   1152:                /* last line of the patch, done */
1.94      joris    1153:                if (lp->l_lineno == plines->l_nblines)
1.1       jfb      1154:                        break;
                   1155:        }
                   1156:
                   1157:        /* once we're done patching, rebuild the line numbers */
1.2       vincent  1158:        lineno = 0;
1.94      joris    1159:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1160:                lp->l_lineno = lineno++;
                   1161:        dlines->l_nblines = lineno - 1;
1.1       jfb      1162:
1.5       vincent  1163:        return (0);
1.1       jfb      1164: }
                   1165:
                   1166: /*
                   1167:  * rcs_getrev()
                   1168:  *
                   1169:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent  1170:  * returned buffer is dynamically allocated and should be released using
                   1171:  * cvs_buf_free() once the caller is done using it.
1.1       jfb      1172:  */
                   1173: BUF*
1.66      joris    1174: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1.1       jfb      1175: {
1.131     niallo   1176:        int res;
1.1       jfb      1177:        size_t len;
                   1178:        void *bp;
1.66      joris    1179:        RCSNUM *crev, *rev;
1.125     niallo   1180:        BUF *rbuf;
1.1       jfb      1181:        struct rcs_delta *rdp = NULL;
                   1182:
1.28      jfb      1183:        if (rfp->rf_head == NULL)
                   1184:                return (NULL);
1.66      joris    1185:
                   1186:        if (frev == RCS_HEAD_REV)
                   1187:                rev = rfp->rf_head;
                   1188:        else
                   1189:                rev = frev;
1.28      jfb      1190:
1.1       jfb      1191:        res = rcsnum_cmp(rfp->rf_head, rev, 0);
                   1192:        if (res == 1) {
1.32      jfb      1193:                rcs_errno = RCS_ERR_NOENT;
1.1       jfb      1194:                return (NULL);
1.26      jfb      1195:        }
                   1196:
1.117     niallo   1197:        /* No matter what, we're going to need up the the description parsed */
                   1198:        rcs_parse_desc(rfp, NULL);
                   1199:
1.26      jfb      1200:        rdp = rcs_findrev(rfp, rfp->rf_head);
                   1201:        if (rdp == NULL) {
                   1202:                cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                   1203:                return (NULL);
                   1204:        }
1.117     niallo   1205:        if (rdp->rd_tlen == 0)
                   1206:                rcs_parse_deltatexts(rfp, rfp->rf_head);
1.45      jfb      1207:        len = rdp->rd_tlen;
1.115     niallo   1208:        if (len == 0) {
                   1209:                rbuf = cvs_buf_alloc(1, 0);
                   1210:                cvs_buf_empty(rbuf);
                   1211:                return (rbuf);
                   1212:        }
1.26      jfb      1213:
1.116     joris    1214:        rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
1.26      jfb      1215:        cvs_buf_append(rbuf, rdp->rd_text, len);
                   1216:
                   1217:        if (res != 0) {
                   1218:                /* Apply patches backwards to get the right version.
                   1219:                 * This will need some rework to support sub branches.
                   1220:                 */
                   1221:                do {
1.87      joris    1222:                        crev = rdp->rd_next;
1.26      jfb      1223:                        rdp = rcs_findrev(rfp, crev);
                   1224:                        if (rdp == NULL) {
                   1225:                                cvs_buf_free(rbuf);
                   1226:                                return (NULL);
                   1227:                        }
1.114     xsa      1228:                        cvs_buf_putc(rbuf, '\0');
1.117     niallo   1229:                        /* check if we have parsed this rev's deltatext */
                   1230:                        if (rdp->rd_tlen == 0)
                   1231:                                rcs_parse_deltatexts(rfp, rdp->rd_num);
1.114     xsa      1232:
1.26      jfb      1233:                        bp = cvs_buf_release(rbuf);
1.94      joris    1234:                        rbuf = cvs_patchfile((char *)bp, (char *)rdp->rd_text,
                   1235:                            rcs_patch_lines);
1.110     joris    1236:                        xfree(bp);
1.26      jfb      1237:                        if (rbuf == NULL)
                   1238:                                break;
                   1239:                } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1       jfb      1240:        }
1.125     niallo   1241:        if (cvs_buf_getc(rbuf, cvs_buf_len(rbuf)-1) != '\n'
                   1242:            && rbuf != NULL)
                   1243:                cvs_buf_putc(rbuf, '\n');
                   1244:        return (rbuf);
1.1       jfb      1245: }
                   1246:
                   1247: /*
1.52      jfb      1248:  * rcs_rev_add()
                   1249:  *
1.53      jfb      1250:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1251:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1252:  * new revision will have a number equal to the previous head revision plus
                   1253:  * one).  The <msg> argument specifies the log message for that revision, and
                   1254:  * <date> specifies the revision's date (a value of -1 is
                   1255:  * equivalent to using the current time).
1.96      xsa      1256:  * If <username> is NULL, set the author for this revision to the current user.
1.90      niallo   1257:  * Otherwise, set it to <username>.
1.52      jfb      1258:  * Returns 0 on success, or -1 on failure.
                   1259:  */
                   1260: int
1.90      niallo   1261: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
                   1262:     const char *username)
1.52      jfb      1263: {
                   1264:        time_t now;
                   1265:        struct passwd *pw;
1.83      joris    1266:        struct rcs_delta *ordp, *rdp;
                   1267:        RCSNUM *old;
                   1268:
1.111     joris    1269:        old = rcsnum_alloc();
1.52      jfb      1270:        if (rev == RCS_HEAD_REV) {
1.101     niallo   1271:                if (rf->rf_flags & RCS_CREATE) {
                   1272:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1273:                                return (-1);
1.111     joris    1274:                        rf->rf_head = rcsnum_alloc();
1.101     niallo   1275:                        rcsnum_cpy(rev, rf->rf_head, 0);
                   1276:                } else {
                   1277:                        rcsnum_cpy(rf->rf_head, old, 0);
                   1278:                        rev = rcsnum_inc(rf->rf_head);
                   1279:                }
1.83      joris    1280:        } else {
                   1281:                if ((rdp = rcs_findrev(rf, rev)) != NULL) {
                   1282:                        rcs_errno = RCS_ERR_DUPENT;
1.92      joris    1283:                        rcsnum_free(old);
1.83      joris    1284:                        return (-1);
                   1285:                }
                   1286:
1.93      joris    1287:                if (!(rf->rf_flags & RCS_CREATE)) {
                   1288:                        ordp = NULL;
                   1289:                        rcsnum_cpy(rev, old, 0);
                   1290:                        while (ordp == NULL) {
                   1291:                                old = rcsnum_dec(old);
                   1292:                                ordp = rcs_findrev(rf, old);
                   1293:                        }
1.83      joris    1294:                }
1.52      jfb      1295:        }
                   1296:
1.112     xsa      1297:        if ((pw = getpwuid(getuid())) == NULL)
                   1298:                fatal("getpwuid failed");
1.52      jfb      1299:
1.110     joris    1300:        rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.52      jfb      1301:        memset(rdp, 0, sizeof(*rdp));
                   1302:
                   1303:        TAILQ_INIT(&(rdp->rd_branches));
                   1304:        TAILQ_INIT(&(rdp->rd_snodes));
                   1305:
1.111     joris    1306:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1307:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1308:
1.111     joris    1309:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1310:
1.101     niallo   1311:        if (!(rf->rf_flags & RCS_CREATE))
                   1312:                rcsnum_cpy(old, rdp->rd_next, 0);
                   1313:
1.83      joris    1314:        rcsnum_free(old);
1.52      jfb      1315:
1.90      niallo   1316:        if (username == NULL)
                   1317:                username = pw->pw_name;
                   1318:
1.110     joris    1319:        rdp->rd_author = xstrdup(username);
                   1320:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1321:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1322:
1.53      jfb      1323:        if (date != (time_t)(-1))
                   1324:                now = date;
                   1325:        else
                   1326:                time(&now);
1.52      jfb      1327:        gmtime_r(&now, &(rdp->rd_date));
                   1328:
                   1329:        TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
                   1330:        rf->rf_ndelta++;
1.81      niallo   1331:
1.64      niallo   1332:        /* not synced anymore */
                   1333:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1334:
                   1335:        return (0);
                   1336: }
                   1337:
                   1338: /*
                   1339:  * rcs_rev_remove()
                   1340:  *
                   1341:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1342:  */
                   1343: int
                   1344: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1345: {
                   1346:        int ret;
                   1347:        struct rcs_delta *rdp;
                   1348:
                   1349:        ret = 0;
                   1350:        if (rev == RCS_HEAD_REV)
                   1351:                rev = rf->rf_head;
                   1352:
                   1353:        /* do we actually have that revision? */
                   1354:        if ((rdp = rcs_findrev(rf, rev)) == NULL) {
                   1355:                rcs_errno = RCS_ERR_NOENT;
                   1356:                ret = -1;
                   1357:        } else {
                   1358:                /* XXX assumes it's not a sub node */
                   1359:                TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1360:                rf->rf_ndelta--;
                   1361:                rf->rf_flags &= ~RCS_SYNCED;
                   1362:        }
                   1363:
                   1364:        return (ret);
                   1365:
                   1366: }
                   1367:
                   1368: /*
1.1       jfb      1369:  * rcs_findrev()
                   1370:  *
                   1371:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1372:  * The revision number is given in <rev>.
                   1373:  * Returns a pointer to the delta on success, or NULL on failure.
                   1374:  */
1.102     xsa      1375: struct rcs_delta *
1.117     niallo   1376: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1       jfb      1377: {
                   1378:        u_int cmplen;
1.117     niallo   1379:        struct rcs_delta *rdp, *enddelta;
1.1       jfb      1380:        struct rcs_dlist *hp;
1.6       vincent  1381:        int found;
1.26      jfb      1382:
1.117     niallo   1383:        /*
                   1384:         * We need to do more parsing if the last revision in the linked list
                   1385:         * is greater than the requested revision.
                   1386:         */
                   1387:        enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
                   1388:        if ((enddelta == NULL)
                   1389:            || (rcsnum_cmp(enddelta->rd_num, rev, -1) == -1)) {
                   1390:                rcs_parse_deltas(rfp, rev);
                   1391:        }
                   1392:
1.1       jfb      1393:        cmplen = 2;
                   1394:        hp = &(rfp->rf_delta);
                   1395:
1.6       vincent  1396:        do {
                   1397:                found = 0;
                   1398:                TAILQ_FOREACH(rdp, hp, rd_list) {
                   1399:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                   1400:                                if (cmplen == rev->rn_len)
                   1401:                                        return (rdp);
1.1       jfb      1402:
1.6       vincent  1403:                                hp = &(rdp->rd_snodes);
                   1404:                                cmplen += 2;
                   1405:                                found = 1;
                   1406:                                break;
                   1407:                        }
1.1       jfb      1408:                }
1.6       vincent  1409:        } while (found && cmplen < rev->rn_len);
1.1       jfb      1410:
                   1411:        return (NULL);
1.20      jfb      1412: }
                   1413:
                   1414: /*
1.26      jfb      1415:  * rcs_kwexp_set()
                   1416:  *
                   1417:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1418:  * Returns 0 on success, or -1 on failure.
                   1419:  */
                   1420: int
                   1421: rcs_kwexp_set(RCSFILE *file, int mode)
                   1422: {
                   1423:        int i;
                   1424:        char *tmp, buf[8] = "";
                   1425:
                   1426:        if (RCS_KWEXP_INVAL(mode))
                   1427:                return (-1);
                   1428:
                   1429:        i = 0;
                   1430:        if (mode == RCS_KWEXP_NONE)
                   1431:                buf[0] = 'b';
                   1432:        else if (mode == RCS_KWEXP_OLD)
                   1433:                buf[0] = 'o';
                   1434:        else {
                   1435:                if (mode & RCS_KWEXP_NAME)
                   1436:                        buf[i++] = 'k';
                   1437:                if (mode & RCS_KWEXP_VAL)
                   1438:                        buf[i++] = 'v';
                   1439:                if (mode & RCS_KWEXP_LKR)
                   1440:                        buf[i++] = 'l';
                   1441:        }
                   1442:
1.110     joris    1443:        tmp = xstrdup(buf);
1.27      jfb      1444:        if (file->rf_expand != NULL)
1.99      niallo   1445:                free(file->rf_expand);
1.26      jfb      1446:        file->rf_expand = tmp;
1.64      niallo   1447:        /* not synced anymore */
                   1448:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1449:
                   1450:        return (0);
                   1451: }
                   1452:
                   1453: /*
                   1454:  * rcs_kwexp_get()
                   1455:  *
                   1456:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1457:  */
                   1458: int
                   1459: rcs_kwexp_get(RCSFILE *file)
                   1460: {
                   1461:        return rcs_kflag_get(file->rf_expand);
                   1462: }
                   1463:
                   1464: /*
1.20      jfb      1465:  * rcs_kflag_get()
                   1466:  *
                   1467:  * Get the keyword expansion mode from a set of character flags given in
                   1468:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1469:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1470:  */
                   1471: int
                   1472: rcs_kflag_get(const char *flags)
                   1473: {
                   1474:        int fl;
                   1475:        size_t len;
                   1476:        const char *fp;
                   1477:
                   1478:        fl = 0;
                   1479:        len = strlen(flags);
                   1480:
                   1481:        for (fp = flags; *fp != '\0'; fp++) {
                   1482:                if (*fp == 'k')
                   1483:                        fl |= RCS_KWEXP_NAME;
                   1484:                else if (*fp == 'v')
                   1485:                        fl |= RCS_KWEXP_VAL;
                   1486:                else if (*fp == 'l')
                   1487:                        fl |= RCS_KWEXP_LKR;
                   1488:                else if (*fp == 'o') {
                   1489:                        if (len != 1)
                   1490:                                fl |= RCS_KWEXP_ERR;
                   1491:                        fl |= RCS_KWEXP_OLD;
                   1492:                } else if (*fp == 'b') {
                   1493:                        if (len != 1)
                   1494:                                fl |= RCS_KWEXP_ERR;
                   1495:                } else  /* unknown letter */
                   1496:                        fl |= RCS_KWEXP_ERR;
                   1497:        }
                   1498:
                   1499:        return (fl);
1.32      jfb      1500: }
                   1501:
                   1502: /*
                   1503:  * rcs_errstr()
                   1504:  *
                   1505:  * Get the error string matching the RCS error code <code>.
                   1506:  */
1.57      xsa      1507: const char *
1.32      jfb      1508: rcs_errstr(int code)
                   1509: {
1.50      jfb      1510:        const char *esp;
                   1511:
                   1512:        if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
                   1513:                esp = NULL;
                   1514:        else if (code == RCS_ERR_ERRNO)
                   1515:                esp = strerror(errno);
                   1516:        else
                   1517:                esp = rcs_errstrs[code];
                   1518:        return (esp);
1.1       jfb      1519: }
                   1520:
1.21      jfb      1521: void
                   1522: rcs_kflag_usage(void)
                   1523: {
                   1524:        fprintf(stderr, "Valid expansion modes include:\n"
1.22      jfb      1525:            "\t-kkv\tGenerate keywords using the default form.\n"
                   1526:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                   1527:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                   1528:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                   1529:            "\t-ko\tGenerate old keyword string "
1.21      jfb      1530:            "(no changes from checked in file).\n"
1.22      jfb      1531:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21      jfb      1532: }
1.1       jfb      1533:
1.117     niallo   1534: /* rcs_parse_deltas()
                   1535:  *
                   1536:  * Parse deltas. If <rev> is not NULL, parse only as far as that
                   1537:  * revision. If <rev> is NULL, parse all deltas.
                   1538:  *
                   1539:  * Returns 0 on success, -1 on failure.
                   1540:  */
                   1541: static int
                   1542: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
                   1543: {
                   1544:        int ret;
                   1545:        struct rcs_delta *enddelta;
                   1546:        if ((rfp->rf_flags & PARSED_DELTAS)
                   1547:            || (rfp->rf_flags & RCS_CREATE))
                   1548:                return (0);
                   1549:        for (;;) {
                   1550:                ret = rcs_parse_delta(rfp);
                   1551:                if (rev != NULL) {
                   1552:                        enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
                   1553:                        if (rcsnum_cmp(enddelta->rd_num, rev, -1) == 0)
                   1554:                                break;
                   1555:                }
                   1556:                if (ret == 0) {
                   1557:                        rfp->rf_flags |= PARSED_DELTAS;
                   1558:                        break;
                   1559:                }
                   1560:                else if (ret == -1)
                   1561:                        fatal("error parsing deltas");
                   1562:        }
                   1563:        return (0);
                   1564: }
                   1565:
                   1566: /* rcs_parse_deltatexts()
                   1567:  *
                   1568:  * Parse deltatexts. If <rev> is not NULL, parse only as far as that
                   1569:  * revision. If <rev> is NULL, parse everything.
                   1570:  *
                   1571:  * Returns 0 on success, -1 on failure.
                   1572:  */
                   1573: static int
                   1574: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
                   1575: {
                   1576:        int ret;
                   1577:        struct rcs_delta *rdp;
                   1578:        if ((rfp->rf_flags & PARSED_DELTATEXTS)
                   1579:            || (rfp->rf_flags & RCS_CREATE))
                   1580:                return (0);
                   1581:        if (!(rfp->rf_flags & PARSED_DESC))
                   1582:                rcs_parse_desc(rfp, rev);
                   1583:        for (;;) {
                   1584:                if (rev != NULL) {
                   1585:                        rdp = rcs_findrev(rfp, rev);
                   1586:                        if (rdp->rd_text != NULL)
                   1587:                                break;
                   1588:                        else
                   1589:                                ret = rcs_parse_deltatext(rfp);
                   1590:                } else
                   1591:                        ret = rcs_parse_deltatext(rfp);
                   1592:                if (ret == 0) {
                   1593:                        rfp->rf_flags |= PARSED_DELTATEXTS;
                   1594:                        break;
                   1595:                }
                   1596:                else if (ret == -1) {
                   1597:                        fatal("problem parsing deltatexts");
                   1598:                }
                   1599:        }
                   1600:
                   1601:        return (0);
                   1602: }
                   1603:
                   1604: /* rcs_parse_desc()
                   1605:  *
                   1606:  * Parse RCS description.
                   1607:  *
                   1608:  * Returns 0 on success, -1 on failure.
                   1609:  */
                   1610: static int
                   1611: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
                   1612: {
                   1613:        int ret = 0;
                   1614:        if ((rfp->rf_flags & PARSED_DESC)
                   1615:            || (rfp->rf_flags & RCS_CREATE))
                   1616:                return (0);
1.122     reyk     1617:        if (!(rfp->rf_flags & PARSED_DELTAS))
1.117     niallo   1618:                rcs_parse_deltas(rfp, rev);
                   1619:        /* do parsing */
                   1620:        ret = rcs_gettok(rfp);
                   1621:        if (ret != RCS_TOK_DESC) {
                   1622:                rcs_errno = RCS_ERR_PARSE;
                   1623:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1624:                    RCS_TOKSTR(rfp));
                   1625:                fatal("problem parsing RCS desc");
                   1626:                return (-1);
                   1627:        }
                   1628:
                   1629:        ret = rcs_gettok(rfp);
                   1630:        if (ret != RCS_TOK_STRING) {
                   1631:                rcs_errno = RCS_ERR_PARSE;
                   1632:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1633:                    RCS_TOKSTR(rfp));
                   1634:                fatal("problem parsing RCS desc");
                   1635:        }
                   1636:
                   1637:        rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
                   1638:        rfp->rf_flags |= PARSED_DESC;
                   1639:        return (0);
                   1640: }
                   1641:
1.1       jfb      1642: /*
1.117     niallo   1643:  * rcs_parse_init()
1.1       jfb      1644:  *
1.117     niallo   1645:  * Initial parsing of file <path>, which are in the RCS format.
                   1646:  * Just does admin section
1.1       jfb      1647:  * Returns 0 on success, or -1 on failure.
                   1648:  */
1.26      jfb      1649: static int
1.117     niallo   1650: rcs_parse_init(RCSFILE *rfp)
1.1       jfb      1651: {
1.117     niallo   1652:        int ret, count;
1.1       jfb      1653:        struct rcs_pdata *pdp;
                   1654:
1.117     niallo   1655:        count = 0;
1.26      jfb      1656:        if (rfp->rf_flags & RCS_PARSED)
1.1       jfb      1657:                return (0);
                   1658:
1.110     joris    1659:        pdp = (struct rcs_pdata *)xmalloc(sizeof(*pdp));
1.1       jfb      1660:        memset(pdp, 0, sizeof(*pdp));
                   1661:
1.18      jfb      1662:        pdp->rp_lines = 0;
1.1       jfb      1663:        pdp->rp_pttype = RCS_TOK_ERR;
                   1664:
1.113     xsa      1665:        if ((pdp->rp_file = fopen(rfp->rf_path, "r")) == NULL)
                   1666:                fatal("fopen: `%s': %s", rfp->rf_path, strerror(errno));
1.1       jfb      1667:
1.110     joris    1668:        pdp->rp_buf = (char *)xmalloc((size_t)RCS_BUFSIZE);
1.1       jfb      1669:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb      1670:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb      1671:
                   1672:        /* ditch the strict lock */
1.26      jfb      1673:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb      1674:        rfp->rf_pdata = pdp;
                   1675:
1.31      jfb      1676:        if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1       jfb      1677:                rcs_freepdata(pdp);
1.117     niallo   1678:                fatal("could not parse admin data");
1.1       jfb      1679:        }
                   1680:
1.117     niallo   1681:        if (rfp->rf_flags & RCS_PARSE_FULLY)
                   1682:                rcs_parse_deltatexts(rfp, NULL);
1.1       jfb      1683:
1.117     niallo   1684:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb      1685:        return (0);
                   1686: }
                   1687:
                   1688: /*
                   1689:  * rcs_parse_admin()
                   1690:  *
                   1691:  * Parse the administrative portion of an RCS file.
1.31      jfb      1692:  * Returns the type of the first token found after the admin section on
                   1693:  * success, or -1 on failure.
1.1       jfb      1694:  */
                   1695: static int
                   1696: rcs_parse_admin(RCSFILE *rfp)
                   1697: {
                   1698:        u_int i;
                   1699:        int tok, ntok, hmask;
                   1700:        struct rcs_key *rk;
                   1701:
                   1702:        /* hmask is a mask of the headers already encountered */
                   1703:        hmask = 0;
                   1704:        for (;;) {
                   1705:                tok = rcs_gettok(rfp);
                   1706:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1707:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1708:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                   1709:                        return (-1);
1.31      jfb      1710:                } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
                   1711:                        /*
                   1712:                         * Assume this is the start of the first delta or
                   1713:                         * that we are dealing with an empty RCS file and
                   1714:                         * we just found the description.
                   1715:                         */
1.1       jfb      1716:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31      jfb      1717:                        return (tok);
1.1       jfb      1718:                }
                   1719:
                   1720:                rk = NULL;
1.18      jfb      1721:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1722:                        if (rcs_keys[i].rk_id == tok)
                   1723:                                rk = &(rcs_keys[i]);
                   1724:
                   1725:                if (hmask & (1 << tok)) {
1.50      jfb      1726:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1727:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1728:                        return (-1);
                   1729:                }
                   1730:                hmask |= (1 << tok);
                   1731:
                   1732:                switch (tok) {
                   1733:                case RCS_TOK_HEAD:
                   1734:                case RCS_TOK_BRANCH:
                   1735:                case RCS_TOK_COMMENT:
                   1736:                case RCS_TOK_EXPAND:
                   1737:                        ntok = rcs_gettok(rfp);
                   1738:                        if (ntok == RCS_TOK_SCOLON)
                   1739:                                break;
                   1740:                        if (ntok != rk->rk_val) {
1.50      jfb      1741:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1742:                                cvs_log(LP_ERR,
                   1743:                                    "invalid value type for RCS key `%s'",
                   1744:                                    rk->rk_str);
                   1745:                        }
                   1746:
                   1747:                        if (tok == RCS_TOK_HEAD) {
1.111     joris    1748:                                if (rfp->rf_head == NULL)
1.28      jfb      1749:                                        rfp->rf_head = rcsnum_alloc();
1.1       jfb      1750:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1751:                                    rfp->rf_head);
1.14      deraadt  1752:                        } else if (tok == RCS_TOK_BRANCH) {
1.111     joris    1753:                                if (rfp->rf_branch == NULL)
1.35      jfb      1754:                                        rfp->rf_branch = rcsnum_alloc();
                   1755:                                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1756:                                    rfp->rf_branch) < 0)
                   1757:                                        return (-1);
1.14      deraadt  1758:                        } else if (tok == RCS_TOK_COMMENT) {
1.110     joris    1759:                                rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1.14      deraadt  1760:                        } else if (tok == RCS_TOK_EXPAND) {
1.110     joris    1761:                                rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1762:                        }
                   1763:
                   1764:                        /* now get the expected semi-colon */
                   1765:                        ntok = rcs_gettok(rfp);
                   1766:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1767:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1768:                                cvs_log(LP_ERR,
                   1769:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1770:                                    rk->rk_str);
1.1       jfb      1771:                                return (-1);
                   1772:                        }
                   1773:                        break;
                   1774:                case RCS_TOK_ACCESS:
1.29      jfb      1775:                        if (rcs_parse_access(rfp) < 0)
                   1776:                                return (-1);
1.1       jfb      1777:                        break;
                   1778:                case RCS_TOK_SYMBOLS:
1.29      jfb      1779:                        if (rcs_parse_symbols(rfp) < 0)
                   1780:                                return (-1);
1.1       jfb      1781:                        break;
                   1782:                case RCS_TOK_LOCKS:
1.29      jfb      1783:                        if (rcs_parse_locks(rfp) < 0)
                   1784:                                return (-1);
1.1       jfb      1785:                        break;
                   1786:                default:
1.50      jfb      1787:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1788:                        cvs_log(LP_ERR,
                   1789:                            "unexpected token `%s' in RCS admin section",
                   1790:                            RCS_TOKSTR(rfp));
                   1791:                        return (-1);
                   1792:                }
                   1793:        }
                   1794:
                   1795:        return (0);
                   1796: }
                   1797:
                   1798: /*
                   1799:  * rcs_parse_delta()
                   1800:  *
                   1801:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1802:  * information in the <rfp> delta list.
                   1803:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1804:  * -1 on error.
                   1805:  */
                   1806: static int
                   1807: rcs_parse_delta(RCSFILE *rfp)
                   1808: {
                   1809:        int ret, tok, ntok, hmask;
                   1810:        u_int i;
                   1811:        char *tokstr;
1.3       vincent  1812:        RCSNUM *datenum;
1.1       jfb      1813:        struct rcs_delta *rdp;
                   1814:        struct rcs_key *rk;
                   1815:
1.110     joris    1816:        rdp = (struct rcs_delta *)xmalloc(sizeof(*rdp));
1.1       jfb      1817:        memset(rdp, 0, sizeof(*rdp));
                   1818:
                   1819:        rdp->rd_num = rcsnum_alloc();
                   1820:        rdp->rd_next = rcsnum_alloc();
                   1821:
                   1822:        TAILQ_INIT(&(rdp->rd_branches));
1.52      jfb      1823:        TAILQ_INIT(&(rdp->rd_snodes));
1.1       jfb      1824:
                   1825:        tok = rcs_gettok(rfp);
                   1826:        if (tok != RCS_TOK_NUM) {
1.52      jfb      1827:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1828:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1829:                    RCS_TOKSTR(rfp));
                   1830:                rcs_freedelta(rdp);
                   1831:                return (-1);
                   1832:        }
                   1833:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1834:
                   1835:        hmask = 0;
                   1836:        ret = 0;
                   1837:        tokstr = NULL;
                   1838:
                   1839:        for (;;) {
                   1840:                tok = rcs_gettok(rfp);
                   1841:                if (tok == RCS_TOK_ERR) {
1.50      jfb      1842:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1843:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1844:                        rcs_freedelta(rdp);
                   1845:                        return (-1);
1.14      deraadt  1846:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1847:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1848:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1849:                        break;
                   1850:                }
                   1851:
                   1852:                rk = NULL;
1.18      jfb      1853:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1854:                        if (rcs_keys[i].rk_id == tok)
                   1855:                                rk = &(rcs_keys[i]);
                   1856:
                   1857:                if (hmask & (1 << tok)) {
1.50      jfb      1858:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1859:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1860:                        rcs_freedelta(rdp);
                   1861:                        return (-1);
                   1862:                }
                   1863:                hmask |= (1 << tok);
                   1864:
                   1865:                switch (tok) {
                   1866:                case RCS_TOK_DATE:
                   1867:                case RCS_TOK_AUTHOR:
                   1868:                case RCS_TOK_STATE:
                   1869:                case RCS_TOK_NEXT:
                   1870:                        ntok = rcs_gettok(rfp);
                   1871:                        if (ntok == RCS_TOK_SCOLON) {
                   1872:                                if (rk->rk_flags & RCS_VOPT)
                   1873:                                        break;
                   1874:                                else {
1.50      jfb      1875:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1876:                                        cvs_log(LP_ERR, "missing mandatory "
                   1877:                                            "value to RCS key `%s'",
                   1878:                                            rk->rk_str);
                   1879:                                        rcs_freedelta(rdp);
                   1880:                                        return (-1);
                   1881:                                }
                   1882:                        }
                   1883:
                   1884:                        if (ntok != rk->rk_val) {
1.50      jfb      1885:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1886:                                cvs_log(LP_ERR,
                   1887:                                    "invalid value type for RCS key `%s'",
                   1888:                                    rk->rk_str);
                   1889:                                rcs_freedelta(rdp);
                   1890:                                return (-1);
                   1891:                        }
                   1892:
                   1893:                        if (tokstr != NULL)
1.110     joris    1894:                                xfree(tokstr);
                   1895:                        tokstr = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      1896:                        /* now get the expected semi-colon */
                   1897:                        ntok = rcs_gettok(rfp);
                   1898:                        if (ntok != RCS_TOK_SCOLON) {
1.50      jfb      1899:                                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1900:                                cvs_log(LP_ERR,
                   1901:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1902:                                    rk->rk_str);
1.110     joris    1903:                                xfree(tokstr);
1.1       jfb      1904:                                rcs_freedelta(rdp);
                   1905:                                return (-1);
                   1906:                        }
                   1907:
                   1908:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1909:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.110     joris    1910:                                        xfree(tokstr);
1.11      joris    1911:                                        rcs_freedelta(rdp);
                   1912:                                        return (-1);
                   1913:                                }
1.3       vincent  1914:                                if (datenum->rn_len != 6) {
1.50      jfb      1915:                                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1916:                                        cvs_log(LP_ERR,
                   1917:                                            "RCS date specification has %s "
                   1918:                                            "fields",
1.3       vincent  1919:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1920:                                            "missing");
1.110     joris    1921:                                        xfree(tokstr);
1.1       jfb      1922:                                        rcs_freedelta(rdp);
1.37      tedu     1923:                                        rcsnum_free(datenum);
                   1924:                                        return (-1);
1.1       jfb      1925:                                }
1.3       vincent  1926:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1927:                                if (rdp->rd_date.tm_year >= 1900)
                   1928:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1929:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1930:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1931:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1932:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1933:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1934:                                rcsnum_free(datenum);
1.14      deraadt  1935:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1936:                                rdp->rd_author = tokstr;
                   1937:                                tokstr = NULL;
1.14      deraadt  1938:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1939:                                rdp->rd_state = tokstr;
                   1940:                                tokstr = NULL;
1.14      deraadt  1941:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      1942:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1943:                        }
                   1944:                        break;
                   1945:                case RCS_TOK_BRANCHES:
1.46      jfb      1946:                        if (rcs_parse_branches(rfp, rdp) < 0) {
                   1947:                                rcs_freedelta(rdp);
                   1948:                                return (-1);
                   1949:                        }
1.1       jfb      1950:                        break;
                   1951:                default:
1.50      jfb      1952:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1953:                        cvs_log(LP_ERR,
                   1954:                            "unexpected token `%s' in RCS delta",
                   1955:                            RCS_TOKSTR(rfp));
                   1956:                        rcs_freedelta(rdp);
                   1957:                        return (-1);
                   1958:                }
                   1959:        }
                   1960:
1.13      jfb      1961:        if (tokstr != NULL)
1.110     joris    1962:                xfree(tokstr);
1.13      jfb      1963:
1.1       jfb      1964:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      1965:        rfp->rf_ndelta++;
1.1       jfb      1966:
                   1967:        return (ret);
                   1968: }
                   1969:
                   1970: /*
                   1971:  * rcs_parse_deltatext()
                   1972:  *
                   1973:  * Parse an RCS delta text section and fill in the log and text field of the
                   1974:  * appropriate delta section.
                   1975:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1976:  * -1 on error.
                   1977:  */
                   1978: static int
                   1979: rcs_parse_deltatext(RCSFILE *rfp)
                   1980: {
                   1981:        int tok;
                   1982:        RCSNUM *tnum;
                   1983:        struct rcs_delta *rdp;
                   1984:
                   1985:        tok = rcs_gettok(rfp);
                   1986:        if (tok == RCS_TOK_EOF)
                   1987:                return (0);
                   1988:
                   1989:        if (tok != RCS_TOK_NUM) {
1.50      jfb      1990:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      1991:                cvs_log(LP_ERR,
                   1992:                    "unexpected token `%s' at start of RCS delta text",
                   1993:                    RCS_TOKSTR(rfp));
                   1994:                return (-1);
                   1995:        }
1.13      jfb      1996:
                   1997:        tnum = rcsnum_alloc();
1.1       jfb      1998:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   1999:
                   2000:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   2001:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   2002:                        break;
                   2003:        }
1.13      jfb      2004:        rcsnum_free(tnum);
                   2005:
1.1       jfb      2006:        if (rdp == NULL) {
                   2007:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   2008:                    RCS_TOKSTR(rfp));
                   2009:                return (-1);
                   2010:        }
                   2011:
                   2012:        tok = rcs_gettok(rfp);
                   2013:        if (tok != RCS_TOK_LOG) {
1.50      jfb      2014:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2015:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2016:                    RCS_TOKSTR(rfp));
                   2017:                return (-1);
                   2018:        }
                   2019:
                   2020:        tok = rcs_gettok(rfp);
                   2021:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2022:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2023:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   2024:                    RCS_TOKSTR(rfp));
                   2025:                return (-1);
                   2026:        }
1.110     joris    2027:        rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2028:        tok = rcs_gettok(rfp);
                   2029:        if (tok != RCS_TOK_TEXT) {
1.50      jfb      2030:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2031:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2032:                    RCS_TOKSTR(rfp));
                   2033:                return (-1);
                   2034:        }
                   2035:
                   2036:        tok = rcs_gettok(rfp);
                   2037:        if (tok != RCS_TOK_STRING) {
1.50      jfb      2038:                rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2039:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   2040:                    RCS_TOKSTR(rfp));
                   2041:                return (-1);
                   2042:        }
                   2043:
1.110     joris    2044:        rdp->rd_text = (u_char *)xmalloc(RCS_TOKLEN(rfp) + 1);
1.74      joris    2045:        strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1));
1.42      jfb      2046:        rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1       jfb      2047:
                   2048:        return (1);
                   2049: }
                   2050:
                   2051: /*
                   2052:  * rcs_parse_access()
                   2053:  *
                   2054:  * Parse the access list given as value to the `access' keyword.
                   2055:  * Returns 0 on success, or -1 on failure.
                   2056:  */
                   2057: static int
                   2058: rcs_parse_access(RCSFILE *rfp)
                   2059: {
                   2060:        int type;
                   2061:
                   2062:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   2063:                if (type != RCS_TOK_ID) {
1.50      jfb      2064:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2065:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   2066:                            RCS_TOKSTR(rfp));
                   2067:                        return (-1);
                   2068:                }
1.29      jfb      2069:
                   2070:                if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
                   2071:                        return (-1);
1.1       jfb      2072:        }
                   2073:
                   2074:        return (0);
                   2075: }
                   2076:
                   2077: /*
                   2078:  * rcs_parse_symbols()
                   2079:  *
                   2080:  * Parse the symbol list given as value to the `symbols' keyword.
                   2081:  * Returns 0 on success, or -1 on failure.
                   2082:  */
                   2083: static int
                   2084: rcs_parse_symbols(RCSFILE *rfp)
                   2085: {
                   2086:        int type;
                   2087:        struct rcs_sym *symp;
                   2088:
                   2089:        for (;;) {
                   2090:                type = rcs_gettok(rfp);
                   2091:                if (type == RCS_TOK_SCOLON)
                   2092:                        break;
                   2093:
1.41      jfb      2094:                if (type != RCS_TOK_ID) {
1.50      jfb      2095:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2096:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2097:                            RCS_TOKSTR(rfp));
                   2098:                        return (-1);
                   2099:                }
                   2100:
1.110     joris    2101:                symp = (struct rcs_sym *)xmalloc(sizeof(*symp));
                   2102:                symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2103:                symp->rs_num = rcsnum_alloc();
                   2104:
                   2105:                type = rcs_gettok(rfp);
                   2106:                if (type != RCS_TOK_COLON) {
1.50      jfb      2107:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2108:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2109:                            RCS_TOKSTR(rfp));
1.11      joris    2110:                        rcsnum_free(symp->rs_num);
1.110     joris    2111:                        xfree(symp->rs_name);
                   2112:                        xfree(symp);
1.1       jfb      2113:                        return (-1);
                   2114:                }
                   2115:
                   2116:                type = rcs_gettok(rfp);
                   2117:                if (type != RCS_TOK_NUM) {
1.50      jfb      2118:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2119:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2120:                            RCS_TOKSTR(rfp));
1.11      joris    2121:                        rcsnum_free(symp->rs_num);
1.110     joris    2122:                        xfree(symp->rs_name);
                   2123:                        xfree(symp);
1.1       jfb      2124:                        return (-1);
                   2125:                }
                   2126:
                   2127:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   2128:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2129:                            RCS_TOKSTR(rfp));
1.11      joris    2130:                        rcsnum_free(symp->rs_num);
1.110     joris    2131:                        xfree(symp->rs_name);
                   2132:                        xfree(symp);
1.1       jfb      2133:                        return (-1);
                   2134:                }
                   2135:
1.43      jfb      2136:                TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1       jfb      2137:        }
                   2138:
                   2139:        return (0);
                   2140: }
                   2141:
                   2142: /*
                   2143:  * rcs_parse_locks()
                   2144:  *
                   2145:  * Parse the lock list given as value to the `locks' keyword.
                   2146:  * Returns 0 on success, or -1 on failure.
                   2147:  */
                   2148: static int
                   2149: rcs_parse_locks(RCSFILE *rfp)
                   2150: {
                   2151:        int type;
                   2152:        struct rcs_lock *lkp;
                   2153:
                   2154:        for (;;) {
                   2155:                type = rcs_gettok(rfp);
                   2156:                if (type == RCS_TOK_SCOLON)
                   2157:                        break;
                   2158:
                   2159:                if (type != RCS_TOK_ID) {
1.50      jfb      2160:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2161:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   2162:                            RCS_TOKSTR(rfp));
                   2163:                        return (-1);
                   2164:                }
                   2165:
1.110     joris    2166:                lkp = (struct rcs_lock *)xmalloc(sizeof(*lkp));
                   2167:                lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
1.1       jfb      2168:                lkp->rl_num = rcsnum_alloc();
                   2169:
                   2170:                type = rcs_gettok(rfp);
                   2171:                if (type != RCS_TOK_COLON) {
1.50      jfb      2172:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2173:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2174:                            RCS_TOKSTR(rfp));
1.37      tedu     2175:                        rcsnum_free(lkp->rl_num);
1.110     joris    2176:                        xfree(lkp->rl_name);
                   2177:                        xfree(lkp);
1.1       jfb      2178:                        return (-1);
                   2179:                }
                   2180:
                   2181:                type = rcs_gettok(rfp);
                   2182:                if (type != RCS_TOK_NUM) {
1.50      jfb      2183:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2184:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   2185:                            RCS_TOKSTR(rfp));
1.37      tedu     2186:                        rcsnum_free(lkp->rl_num);
1.110     joris    2187:                        xfree(lkp->rl_name);
                   2188:                        xfree(lkp);
1.1       jfb      2189:                        return (-1);
                   2190:                }
                   2191:
                   2192:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   2193:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   2194:                            RCS_TOKSTR(rfp));
1.37      tedu     2195:                        rcsnum_free(lkp->rl_num);
1.110     joris    2196:                        xfree(lkp->rl_name);
                   2197:                        xfree(lkp);
1.1       jfb      2198:                        return (-1);
                   2199:                }
                   2200:
                   2201:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   2202:        }
                   2203:
                   2204:        /* check if we have a `strict' */
                   2205:        type = rcs_gettok(rfp);
                   2206:        if (type != RCS_TOK_STRICT) {
                   2207:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  2208:        } else {
1.26      jfb      2209:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      2210:
                   2211:                type = rcs_gettok(rfp);
                   2212:                if (type != RCS_TOK_SCOLON) {
1.50      jfb      2213:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2214:                        cvs_log(LP_ERR,
                   2215:                            "missing semi-colon after `strict' keyword");
                   2216:                        return (-1);
                   2217:                }
                   2218:        }
                   2219:
                   2220:        return (0);
                   2221: }
                   2222:
                   2223: /*
                   2224:  * rcs_parse_branches()
                   2225:  *
                   2226:  * Parse the list of branches following a `branches' keyword in a delta.
                   2227:  * Returns 0 on success, or -1 on failure.
                   2228:  */
                   2229: static int
                   2230: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   2231: {
                   2232:        int type;
                   2233:        struct rcs_branch *brp;
                   2234:
                   2235:        for (;;) {
                   2236:                type = rcs_gettok(rfp);
                   2237:                if (type == RCS_TOK_SCOLON)
                   2238:                        break;
                   2239:
                   2240:                if (type != RCS_TOK_NUM) {
1.50      jfb      2241:                        rcs_errno = RCS_ERR_PARSE;
1.1       jfb      2242:                        cvs_log(LP_ERR,
                   2243:                            "unexpected token `%s' in list of branches",
                   2244:                            RCS_TOKSTR(rfp));
                   2245:                        return (-1);
                   2246:                }
                   2247:
1.110     joris    2248:                brp = (struct rcs_branch *)xmalloc(sizeof(*brp));
1.46      jfb      2249:                brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11      joris    2250:                if (brp->rb_num == NULL) {
1.110     joris    2251:                        xfree(brp);
1.11      joris    2252:                        return (-1);
                   2253:                }
1.1       jfb      2254:
                   2255:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   2256:        }
                   2257:
                   2258:        return (0);
                   2259: }
                   2260:
                   2261: /*
                   2262:  * rcs_freedelta()
                   2263:  *
                   2264:  * Free the contents of a delta structure.
                   2265:  */
1.18      jfb      2266: static void
1.1       jfb      2267: rcs_freedelta(struct rcs_delta *rdp)
                   2268: {
1.12      jfb      2269:        struct rcs_branch *rb;
1.1       jfb      2270:        struct rcs_delta *crdp;
                   2271:
1.12      jfb      2272:        if (rdp->rd_num != NULL)
                   2273:                rcsnum_free(rdp->rd_num);
                   2274:        if (rdp->rd_next != NULL)
                   2275:                rcsnum_free(rdp->rd_next);
                   2276:
1.1       jfb      2277:        if (rdp->rd_author != NULL)
1.110     joris    2278:                xfree(rdp->rd_author);
1.109     joris    2279:        if (rdp->rd_locker != NULL)
1.110     joris    2280:                xfree(rdp->rd_locker);
1.1       jfb      2281:        if (rdp->rd_state != NULL)
1.110     joris    2282:                xfree(rdp->rd_state);
1.1       jfb      2283:        if (rdp->rd_log != NULL)
1.110     joris    2284:                xfree(rdp->rd_log);
1.1       jfb      2285:        if (rdp->rd_text != NULL)
1.110     joris    2286:                xfree(rdp->rd_text);
1.12      jfb      2287:
                   2288:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   2289:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   2290:                rcsnum_free(rb->rb_num);
1.110     joris    2291:                xfree(rb);
1.12      jfb      2292:        }
1.1       jfb      2293:
                   2294:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   2295:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   2296:                rcs_freedelta(crdp);
                   2297:        }
                   2298:
1.110     joris    2299:        xfree(rdp);
1.1       jfb      2300: }
                   2301:
                   2302: /*
                   2303:  * rcs_freepdata()
                   2304:  *
                   2305:  * Free the contents of the parser data structure.
                   2306:  */
                   2307: static void
                   2308: rcs_freepdata(struct rcs_pdata *pd)
                   2309: {
                   2310:        if (pd->rp_file != NULL)
                   2311:                (void)fclose(pd->rp_file);
                   2312:        if (pd->rp_buf != NULL)
1.110     joris    2313:                xfree(pd->rp_buf);
                   2314:        xfree(pd);
1.1       jfb      2315: }
                   2316:
                   2317: /*
                   2318:  * rcs_gettok()
                   2319:  *
                   2320:  * Get the next RCS token from the string <str>.
                   2321:  */
                   2322: static int
                   2323: rcs_gettok(RCSFILE *rfp)
                   2324: {
                   2325:        u_int i;
                   2326:        int ch, last, type;
1.18      jfb      2327:        size_t len;
                   2328:        char *bp;
1.1       jfb      2329:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2330:
                   2331:        type = RCS_TOK_ERR;
                   2332:        bp = pdp->rp_buf;
1.42      jfb      2333:        pdp->rp_tlen = 0;
1.1       jfb      2334:        *bp = '\0';
                   2335:
                   2336:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   2337:                type = pdp->rp_pttype;
                   2338:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   2339:                pdp->rp_pttype = RCS_TOK_ERR;
                   2340:                return (type);
                   2341:        }
                   2342:
                   2343:        /* skip leading whitespace */
                   2344:        /* XXX we must skip backspace too for compatibility, should we? */
                   2345:        do {
                   2346:                ch = getc(pdp->rp_file);
                   2347:                if (ch == '\n')
1.18      jfb      2348:                        pdp->rp_lines++;
1.1       jfb      2349:        } while (isspace(ch));
                   2350:
                   2351:        if (ch == EOF) {
                   2352:                type = RCS_TOK_EOF;
1.14      deraadt  2353:        } else if (ch == ';') {
1.1       jfb      2354:                type = RCS_TOK_SCOLON;
1.14      deraadt  2355:        } else if (ch == ':') {
1.1       jfb      2356:                type = RCS_TOK_COLON;
1.14      deraadt  2357:        } else if (isalpha(ch)) {
1.31      jfb      2358:                type = RCS_TOK_ID;
1.1       jfb      2359:                *(bp++) = ch;
1.18      jfb      2360:                for (;;) {
1.1       jfb      2361:                        ch = getc(pdp->rp_file);
1.11      joris    2362:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      2363:                                ungetc(ch, pdp->rp_file);
                   2364:                                break;
                   2365:                        }
                   2366:                        *(bp++) = ch;
1.42      jfb      2367:                        pdp->rp_tlen++;
1.18      jfb      2368:                        if (bp == pdp->rp_bufend - 1) {
                   2369:                                len = bp - pdp->rp_buf;
                   2370:                                if (rcs_growbuf(rfp) < 0) {
                   2371:                                        type = RCS_TOK_ERR;
                   2372:                                        break;
                   2373:                                }
                   2374:                                bp = pdp->rp_buf + len;
                   2375:                        }
1.1       jfb      2376:                }
                   2377:                *bp = '\0';
                   2378:
1.18      jfb      2379:                if (type != RCS_TOK_ERR) {
                   2380:                        for (i = 0; i < RCS_NKEYS; i++) {
                   2381:                                if (strcmp(rcs_keys[i].rk_str,
                   2382:                                    pdp->rp_buf) == 0) {
                   2383:                                        type = rcs_keys[i].rk_id;
                   2384:                                        break;
                   2385:                                }
1.1       jfb      2386:                        }
                   2387:                }
1.14      deraadt  2388:        } else if (ch == '@') {
1.1       jfb      2389:                /* we have a string */
1.18      jfb      2390:                type = RCS_TOK_STRING;
1.1       jfb      2391:                for (;;) {
                   2392:                        ch = getc(pdp->rp_file);
                   2393:                        if (ch == '@') {
                   2394:                                ch = getc(pdp->rp_file);
                   2395:                                if (ch != '@') {
                   2396:                                        ungetc(ch, pdp->rp_file);
                   2397:                                        break;
                   2398:                                }
1.14      deraadt  2399:                        } else if (ch == '\n')
1.18      jfb      2400:                                pdp->rp_lines++;
1.1       jfb      2401:
                   2402:                        *(bp++) = ch;
1.42      jfb      2403:                        pdp->rp_tlen++;
1.18      jfb      2404:                        if (bp == pdp->rp_bufend - 1) {
                   2405:                                len = bp - pdp->rp_buf;
                   2406:                                if (rcs_growbuf(rfp) < 0) {
                   2407:                                        type = RCS_TOK_ERR;
                   2408:                                        break;
                   2409:                                }
                   2410:                                bp = pdp->rp_buf + len;
                   2411:                        }
1.1       jfb      2412:                }
                   2413:
                   2414:                *bp = '\0';
1.14      deraadt  2415:        } else if (isdigit(ch)) {
1.1       jfb      2416:                *(bp++) = ch;
                   2417:                last = ch;
                   2418:                type = RCS_TOK_NUM;
                   2419:
                   2420:                for (;;) {
                   2421:                        ch = getc(pdp->rp_file);
1.18      jfb      2422:                        if (bp == pdp->rp_bufend)
1.1       jfb      2423:                                break;
                   2424:                        if (!isdigit(ch) && ch != '.') {
                   2425:                                ungetc(ch, pdp->rp_file);
                   2426:                                break;
                   2427:                        }
                   2428:
                   2429:                        if (last == '.' && ch == '.') {
                   2430:                                type = RCS_TOK_ERR;
                   2431:                                break;
                   2432:                        }
                   2433:                        last = ch;
                   2434:                        *(bp++) = ch;
1.42      jfb      2435:                        pdp->rp_tlen++;
1.1       jfb      2436:                }
1.18      jfb      2437:                *bp = '\0';
1.1       jfb      2438:        }
                   2439:
                   2440:        return (type);
                   2441: }
                   2442:
                   2443: /*
                   2444:  * rcs_pushtok()
                   2445:  *
                   2446:  * Push a token back in the parser's token buffer.
                   2447:  */
                   2448: static int
                   2449: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   2450: {
                   2451:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   2452:
                   2453:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   2454:                return (-1);
                   2455:
                   2456:        pdp->rp_pttype = type;
                   2457:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   2458:        return (0);
                   2459: }
                   2460:
1.18      jfb      2461:
                   2462: /*
                   2463:  * rcs_growbuf()
                   2464:  *
                   2465:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   2466:  * RCS_BUFEXTSIZE.
                   2467:  * In case of failure, the original buffer is left unmodified.
                   2468:  * Returns 0 on success, or -1 on failure.
                   2469:  */
                   2470: static int
                   2471: rcs_growbuf(RCSFILE *rf)
                   2472: {
                   2473:        void *tmp;
                   2474:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   2475:
1.119     niallo   2476:        tmp = xrealloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
1.18      jfb      2477:        if (tmp == NULL) {
1.50      jfb      2478:                rcs_errno = RCS_ERR_ERRNO;
1.18      jfb      2479:                cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
                   2480:                return (-1);
                   2481:        }
                   2482:
                   2483:        pdp->rp_buf = (char *)tmp;
                   2484:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   2485:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42      jfb      2486:
                   2487:        return (0);
                   2488: }
                   2489:
                   2490: /*
                   2491:  * rcs_strprint()
                   2492:  *
                   2493:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   2494:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   2495:  * binary data.
                   2496:  */
                   2497: static int
                   2498: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   2499: {
                   2500:        const u_char *ap, *ep, *sp;
                   2501:        size_t ret;
1.52      jfb      2502:
                   2503:        if (slen == 0)
                   2504:                return (0);
1.42      jfb      2505:
                   2506:        ep = str + slen - 1;
                   2507:
                   2508:        for (sp = str; sp <= ep;)  {
                   2509:                ap = memchr(sp, '@', ep - sp);
                   2510:                if (ap == NULL)
                   2511:                        ap = ep;
                   2512:                ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
                   2513:
                   2514:                if (*ap == '@')
                   2515:                        putc('@', stream);
                   2516:                sp = ap + 1;
1.63      joris    2517:        }
                   2518:
                   2519:        return (0);
                   2520: }
                   2521:
                   2522: /*
                   2523:  * rcs_expand_keywords()
                   2524:  *
1.125     niallo   2525:  * Return expansion any RCS keywords in <data>
                   2526:  *
                   2527:  * On error, return NULL.
1.63      joris    2528:  */
1.125     niallo   2529: static char *
                   2530: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
1.63      joris    2531:     size_t len, int mode)
                   2532: {
1.125     niallo   2533:        int kwtype, sizdiff;
                   2534:        u_int i, j, found, start_offset, c_offset;
1.140     xsa      2535:        char *c, *kwstr, *start, *end, *tbuf;
1.125     niallo   2536:        char expbuf[256], buf[256];
1.140     xsa      2537:        struct tm *tb;
1.134     joris    2538:        char *fmt;
1.63      joris    2539:
1.65      niallo   2540:        kwtype = 0;
                   2541:        kwstr = NULL;
1.63      joris    2542:        i = 0;
                   2543:
                   2544:        /*
1.134     joris    2545:         * -z support for RCS
                   2546:         */
                   2547:        tb = &rdp->rd_date;
1.140     xsa      2548:        if (timezone_flag != NULL)
                   2549:                tb = rcs_set_tz(timezone_flag, rdp);
1.134     joris    2550:
                   2551:        /*
1.63      joris    2552:         * Keyword formats:
                   2553:         * $Keyword$
                   2554:         * $Keyword: value$
                   2555:         */
1.125     niallo   2556:        for (c = data; *c != '\0' && i < len; *c++) {
1.63      joris    2557:                if (*c == '$') {
                   2558:                        /* remember start of this possible keyword */
                   2559:                        start = c;
1.125     niallo   2560:                        start_offset = start - data;
1.63      joris    2561:
                   2562:                        /* first following character has to be alphanumeric */
                   2563:                        *c++;
                   2564:                        if (!isalpha(*c)) {
                   2565:                                c = start;
                   2566:                                continue;
                   2567:                        }
                   2568:
                   2569:                        /* look for any matching keywords */
                   2570:                        found = 0;
                   2571:                        for (j = 0; j < RCS_NKWORDS; j++) {
                   2572:                                if (!strncmp(c, rcs_expkw[j].kw_str,
                   2573:                                    strlen(rcs_expkw[j].kw_str))) {
                   2574:                                        found = 1;
                   2575:                                        kwstr = rcs_expkw[j].kw_str;
                   2576:                                        kwtype = rcs_expkw[j].kw_type;
                   2577:                                        break;
                   2578:                                }
                   2579:                        }
                   2580:
                   2581:                        /* unknown keyword, continue looking */
                   2582:                        if (found == 0) {
                   2583:                                c = start;
                   2584:                                continue;
                   2585:                        }
                   2586:
                   2587:                        /* next character has to be ':' or '$' */
                   2588:                        c += strlen(kwstr);
                   2589:                        if (*c != ':' && *c != '$') {
                   2590:                                c = start;
                   2591:                                continue;
                   2592:                        }
                   2593:
                   2594:                        /*
                   2595:                         * if the next character was ':' we need to look for
                   2596:                         * an '$' before the end of the line to be sure it is
                   2597:                         * in fact a keyword.
                   2598:                         */
                   2599:                        if (*c == ':') {
                   2600:                                while (*c++) {
                   2601:                                        if (*c == '$' || *c == '\n')
                   2602:                                                break;
                   2603:                                }
                   2604:
                   2605:                                if (*c != '$') {
                   2606:                                        c = start;
                   2607:                                        continue;
                   2608:                                }
                   2609:                        }
1.125     niallo   2610:                        c_offset = c - data;
                   2611:                        end = c + 1;
1.63      joris    2612:
                   2613:                        /* start constructing the expansion */
                   2614:                        expbuf[0] = '\0';
                   2615:
                   2616:                        if (mode & RCS_KWEXP_NAME) {
                   2617:                                strlcat(expbuf, "$", sizeof(expbuf));
                   2618:                                strlcat(expbuf, kwstr, sizeof(expbuf));
                   2619:                                if (mode & RCS_KWEXP_VAL)
                   2620:                                        strlcat(expbuf, ": ", sizeof(expbuf));
                   2621:                        }
                   2622:
                   2623:                        /*
1.80      reyk     2624:                         * order matters because of RCS_KW_ID and
                   2625:                         * RCS_KW_HEADER here
1.63      joris    2626:                         */
                   2627:                        if (mode & RCS_KWEXP_VAL) {
                   2628:                                if (kwtype & RCS_KW_RCSFILE) {
                   2629:                                        if (!(kwtype & RCS_KW_FULLPATH))
1.80      reyk     2630:                                                strlcat(expbuf,
                   2631:                                                    basename(rcsfile),
1.63      joris    2632:                                                    sizeof(expbuf));
                   2633:                                        else
                   2634:                                                strlcat(expbuf, rcsfile,
                   2635:                                                    sizeof(expbuf));
                   2636:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2637:                                }
                   2638:
                   2639:                                if (kwtype & RCS_KW_REVISION) {
1.80      reyk     2640:                                        rcsnum_tostr(rdp->rd_num, buf,
                   2641:                                            sizeof(buf));
1.63      joris    2642:                                        strlcat(buf, " ", sizeof(buf));
                   2643:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2644:                                }
                   2645:
                   2646:                                if (kwtype & RCS_KW_DATE) {
1.134     joris    2647:                                        if (timezone_flag != NULL)
                   2648:                                                fmt = "%Y/%m/%d %H:%M:%S%z ";
                   2649:                                        else
                   2650:                                                fmt = "%Y/%m/%d %H:%M:%S ";
                   2651:
                   2652:                                        strftime(buf, sizeof(buf), fmt, tb);
1.63      joris    2653:                                        strlcat(expbuf, buf, sizeof(expbuf));
                   2654:                                }
                   2655:
                   2656:                                if (kwtype & RCS_KW_AUTHOR) {
                   2657:                                        strlcat(expbuf, rdp->rd_author,
                   2658:                                            sizeof(expbuf));
                   2659:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2660:                                }
                   2661:
                   2662:                                if (kwtype & RCS_KW_STATE) {
                   2663:                                        strlcat(expbuf, rdp->rd_state,
                   2664:                                            sizeof(expbuf));
                   2665:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2666:                                }
                   2667:
                   2668:                                /* order does not matter anymore below */
                   2669:                                if (kwtype & RCS_KW_LOG)
                   2670:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2671:
                   2672:                                if (kwtype & RCS_KW_SOURCE) {
1.80      reyk     2673:                                        strlcat(expbuf, rcsfile,
                   2674:                                            sizeof(expbuf));
1.63      joris    2675:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2676:                                }
                   2677:
                   2678:                                if (kwtype & RCS_KW_NAME)
                   2679:                                        strlcat(expbuf, " ", sizeof(expbuf));
                   2680:                        }
                   2681:
                   2682:                        /* end the expansion */
                   2683:                        if (mode & RCS_KWEXP_NAME)
                   2684:                                strlcat(expbuf, "$", sizeof(expbuf));
1.138     deraadt  2685:
1.125     niallo   2686:                        sizdiff = strlen(expbuf) - (end - start);
                   2687:                        tbuf = xmalloc(strlen(end) + 1);
                   2688:                        strlcpy(tbuf, end, strlen(end) + 1);
                   2689:                        /* only realloc if we have to */
                   2690:                        if (sizdiff > 0) {
                   2691:                                len += sizdiff;
                   2692:                                data = xrealloc(data, len);
                   2693:                                /*
                   2694:                                 * ensure string pointers are not invalidated
                   2695:                                 * after realloc()
                   2696:                                 */
                   2697:                                start = data + start_offset;
                   2698:                                c = data + c_offset;
                   2699:                        }
                   2700:                        strlcpy(start, expbuf, len);
                   2701:                        strlcat(data, tbuf, len);
                   2702:                        xfree(tbuf);
1.63      joris    2703:                        i += strlen(expbuf);
                   2704:                }
1.42      jfb      2705:        }
1.81      niallo   2706:
1.125     niallo   2707:        return (data);
1.81      niallo   2708: }
                   2709:
                   2710: /*
                   2711:  * rcs_deltatext_set()
                   2712:  *
                   2713:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      2714:  * Returns -1 on error, 0 on success.
1.81      niallo   2715:  */
                   2716: int
                   2717: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
                   2718: {
                   2719:        size_t len;
                   2720:        struct rcs_delta *rdp;
1.117     niallo   2721:
                   2722:        /* Write operations require full parsing */
                   2723:        rcs_parse_deltatexts(rfp, NULL);
1.81      niallo   2724:
                   2725:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2726:                return (-1);
                   2727:
                   2728:        if (rdp->rd_text != NULL)
1.110     joris    2729:                xfree(rdp->rd_text);
1.81      niallo   2730:
                   2731:        len = strlen(dtext);
1.103     joris    2732:        if (len != 0) {
1.110     joris    2733:                rdp->rd_text = (u_char *)xmalloc(len);
1.103     joris    2734:                rdp->rd_tlen = len - 1;
                   2735:                strlcpy(rdp->rd_text, dtext, len);
                   2736:        } else {
                   2737:                rdp->rd_text = NULL;
                   2738:                rdp->rd_tlen = 0;
                   2739:        }
1.18      jfb      2740:
1.86      joris    2741:        return (0);
                   2742: }
                   2743:
                   2744: /*
                   2745:  * rcs_rev_setlog()
                   2746:  *
                   2747:  * Sets the log message of revision <rev> to <logtext>
                   2748:  */
                   2749: int
                   2750: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   2751: {
                   2752:        struct rcs_delta *rdp;
                   2753:        char buf[16];
                   2754:
                   2755:        rcsnum_tostr(rev, buf, sizeof(buf));
                   2756:
                   2757:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2758:                return (-1);
                   2759:
                   2760:        if (rdp->rd_log != NULL)
1.110     joris    2761:                xfree(rdp->rd_log);
1.86      joris    2762:
1.110     joris    2763:        rdp->rd_log = xstrdup(logtext);
1.86      joris    2764:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   2765:        return (0);
                   2766: }
1.97      niallo   2767: /*
                   2768:  * rcs_rev_getdate()
                   2769:  *
                   2770:  * Get the date corresponding to a given revision.
                   2771:  * Returns the date on success, -1 on failure.
                   2772:  */
                   2773: time_t
                   2774: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   2775: {
                   2776:        struct rcs_delta *rdp;
                   2777:
                   2778:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2779:                return (-1);
                   2780:
                   2781:        return (mktime(&rdp->rd_date));
                   2782: }
1.95      niallo   2783:
                   2784: /*
                   2785:  * rcs_state_set()
                   2786:  *
                   2787:  * Sets the state of revision <rev> to <state>
                   2788:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   2789:  *
                   2790:  * Returns -1 on failure, 0 on success.
                   2791:  */
                   2792: int
                   2793: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   2794: {
                   2795:        struct rcs_delta *rdp;
                   2796:
                   2797:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2798:                return (-1);
                   2799:
                   2800:        if (rdp->rd_state != NULL)
1.110     joris    2801:                xfree(rdp->rd_state);
1.95      niallo   2802:
1.110     joris    2803:        rdp->rd_state = xstrdup(state);
1.95      niallo   2804:
                   2805:        rfp->rf_flags &= ~RCS_SYNCED;
                   2806:
                   2807:        return (0);
                   2808: }
                   2809:
                   2810: /*
                   2811:  * rcs_state_check()
                   2812:  *
                   2813:  * Check if string <state> is valid.
                   2814:  *
1.96      xsa      2815:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   2816:  */
                   2817: int
                   2818: rcs_state_check(const char *state)
                   2819: {
                   2820:        if (strchr(state, ' ') != NULL)
                   2821:                return (-1);
                   2822:
1.18      jfb      2823:        return (0);
1.1       jfb      2824: }
1.97      niallo   2825:
                   2826: /*
                   2827:  * rcs_state_get()
                   2828:  *
                   2829:  * Get the state for a given revision of a specified RCSFILE.
                   2830:  *
                   2831:  * Returns NULL on failure.
                   2832:  */
                   2833: const char *
                   2834: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   2835: {
                   2836:        struct rcs_delta *rdp;
                   2837:
                   2838:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2839:                return (NULL);
                   2840:
                   2841:        return (rdp->rd_state);
                   2842: }
                   2843:
1.131     niallo   2844: /*
                   2845:  * rcs_kwexp_buf()
                   2846:  *
                   2847:  * Do keyword expansion on a buffer if necessary
                   2848:  *
                   2849:  */
                   2850: BUF *
                   2851: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
                   2852: {
                   2853:        struct rcs_delta *rdp;
                   2854:        char *expanded, *tbuf;
                   2855:        int expmode;
                   2856:        size_t len;
                   2857:
                   2858:        /*
                   2859:         * Do keyword expansion if required.
                   2860:         */
                   2861:        if (rf->rf_expand != NULL)
                   2862:                expmode = rcs_kwexp_get(rf);
                   2863:        else
                   2864:                expmode = RCS_KWEXP_DEFAULT;
                   2865:
                   2866:        if (!(expmode & RCS_KWEXP_NONE)) {
                   2867:                if ((rdp = rcs_findrev(rf, rev)) == NULL)
                   2868:                    fatal("could not fetch revision");
                   2869:                cvs_buf_putc(bp, '\0');
                   2870:                len = cvs_buf_len(bp);
                   2871:                tbuf = cvs_buf_release(bp);
                   2872:                expanded = rcs_expand_keywords(rf->rf_path, rdp,
                   2873:                    tbuf, len, expmode);
                   2874:                bp = cvs_buf_alloc(len, BUF_AUTOEXT);
1.138     deraadt  2875:                cvs_buf_set(bp, expanded, strlen(expanded), 0);
1.131     niallo   2876:                xfree(expanded);
                   2877:        }
                   2878:        return (bp);
                   2879: }
                   2880:
1.105     joris    2881:
1.106     joris    2882: #if !defined(RCSPROG)
                   2883:
1.105     joris    2884: static char *month_tab[] = {
                   2885:        "Jan",
                   2886:        "Feb",
                   2887:        "Mar",
                   2888:        "Apr",
                   2889:        "May",
                   2890:        "Jun",
                   2891:        "Jul",
                   2892:        "Aug",
                   2893:        "Sep",
                   2894:        "Oct",
                   2895:        "Nov",
                   2896:        "Dec"
                   2897: };
                   2898:
                   2899: /*
                   2900:  * Checkout a certain revision <rev> of RCS file <rf> to either standard
                   2901:  * output when running in server mode, or to <fpath> when running in local mode.
                   2902:  *
                   2903:  * If type is CHECKOUT_REV_MERGED we have an extra argument, which
                   2904:  * is the buffer containing the merged file.
                   2905:  *
                   2906:  * If type is CHECKOUT_REV_REMOVED, the file has been removed and we
                   2907:  * need to do the same thing.
                   2908:  */
                   2909: int
                   2910: cvs_checkout_rev(RCSFILE *rf, RCSNUM *rev, CVSFILE *cf, char *fpath,
                   2911:     int local, int type, ...)
                   2912: {
                   2913:        BUF *bp;
                   2914:        int l, ret, fsize;
                   2915:        char timebuf[32], entry[MAXPATHLEN], copyfile[MAXPATHLEN];
                   2916:        char *content, *repo, buf[MAXPATHLEN], modestr[16];
                   2917:        struct cvsroot *root;
                   2918:        struct cvs_ent *ent;
                   2919:        va_list ap;
                   2920:        time_t rcstime;
                   2921:        struct timeval tv[2];
                   2922:        struct tm *tp;
                   2923:        RCSNUM *oldrev;
                   2924:
                   2925:        bp = NULL;
                   2926:        ret = -1;
                   2927:        content = NULL;
                   2928:        oldrev = NULL;
                   2929:
                   2930:        if ((type != CHECKOUT_REV_MERGED) && (type != CHECKOUT_REV_REMOVED)) {
                   2931:                /* fetch the contents of the revision */
                   2932:                if ((bp = rcs_getrev(rf, rev)) == NULL) {
                   2933:                        cvs_log(LP_ERR, "revision '%s' not found in file '%s'",
                   2934:                            rcsnum_tostr(rev, buf, sizeof(buf)), fpath);
                   2935:                        goto out;
                   2936:                }
1.131     niallo   2937:                bp = rcs_kwexp_buf(bp, rf, rev);
1.105     joris    2938:        } else if (type != CHECKOUT_REV_REMOVED) {
                   2939:                va_start(ap, type);
                   2940:                bp = va_arg(ap, BUF *);
                   2941:                va_end(ap);
                   2942:        }
                   2943:
                   2944:        if (type == CHECKOUT_REV_CREATED)
                   2945:                rcstime = rcs_rev_getdate(rf, rev);
1.108     joris    2946:        else if (type == CHECKOUT_REV_MERGED ||
                   2947:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    2948:                time(&rcstime);
1.108     joris    2949:                if ((rcstime = cvs_hack_time(rcstime, 1)) < 0)
                   2950:                        goto out;
                   2951:        }
1.105     joris    2952:
1.108     joris    2953:        if (type == CHECKOUT_REV_CREATED ||
1.130     niallo   2954:            type == CHECKOUT_REV_MERGED ||
1.108     joris    2955:            type == CHECKOUT_REV_UPDATED) {
1.105     joris    2956:                ctime_r(&rcstime, timebuf);
                   2957:                l = strlen(timebuf);
                   2958:                if ((l > 0) && (timebuf[l - 1] == '\n'))
                   2959:                        timebuf[--l] = '\0';
                   2960:
                   2961:                l = snprintf(entry, sizeof(entry), "/%s/%s/%s/%s/", cf->cf_name,
                   2962:                    rcsnum_tostr(rev, buf, sizeof(buf)),
                   2963:                    (local == 1) ? timebuf : "",
                   2964:                    (type == CHECKOUT_REV_MERGED) ? "+=" : "");
                   2965:                if (l == -1 || l >= (int)sizeof(buf))
                   2966:                        goto out;
                   2967:        }
                   2968:
                   2969:        if (type == CHECKOUT_REV_MERGED) {
1.111     joris    2970:                oldrev = rcsnum_alloc();
                   2971:                rcsnum_cpy(rev, oldrev, 0);
1.105     joris    2972:
                   2973:                if (rcsnum_dec(oldrev) == NULL)
                   2974:                        goto out;
                   2975:
                   2976:                l = snprintf(copyfile, sizeof(copyfile), ".#%s.%s",
                   2977:                    cf->cf_name, rcsnum_tostr(oldrev, buf, sizeof(buf)));
                   2978:                if (l == -1 || l >= (int)sizeof(copyfile))
                   2979:                        goto out;
                   2980:        }
                   2981:
                   2982:        root = CVS_DIR_ROOT(cf);
                   2983:        repo = CVS_DIR_REPO(cf);
                   2984:
                   2985:        /*
                   2986:         * In local mode, just copy the entire contents to fpath.
                   2987:         * In server mode, we need to send it to the client together with
                   2988:         * some responses.
                   2989:         */
                   2990:        if (local) {
                   2991:                l = 0;
                   2992:                if (cf->cf_entry == NULL) {
                   2993:                        l = 1;
                   2994:                        cf->cf_entry = cvs_ent_open(cf->cf_dir, O_RDWR);
                   2995:                        if (cf->cf_entry == NULL) {
1.122     reyk     2996:                                cvs_log(LP_ERR, "failed to open Entry "
                   2997:                                    "file '%s'", cf->cf_dir);
1.105     joris    2998:                                goto out;
                   2999:                        }
                   3000:                }
                   3001:
                   3002:                cvs_ent_remove(cf->cf_entry, cf->cf_name, 1);
                   3003:                if (type != CHECKOUT_REV_REMOVED) {
                   3004:                        cvs_ent_addln(cf->cf_entry, entry);
                   3005:                        ent = cvs_ent_get(cf->cf_entry, cf->cf_name);
                   3006:                        ent->processed = 1;
                   3007:                }
                   3008:
                   3009:                if (l == 1)
                   3010:                        cvs_ent_close(cf->cf_entry);
                   3011:
                   3012:                switch (type) {
                   3013:                case CHECKOUT_REV_REMOVED:
                   3014:                        if (cvs_unlink(fpath) < 0)
                   3015:                                goto out;
                   3016:                        break;
                   3017:                case CHECKOUT_REV_MERGED:
                   3018:                        /* XXX move the old file when merging */
1.108     joris    3019:                case CHECKOUT_REV_UPDATED:
1.105     joris    3020:                case CHECKOUT_REV_CREATED:
1.114     xsa      3021:                        cvs_buf_write(bp, fpath, cf->cf_mode);
1.107     joris    3022:                        /*
                   3023:                         * correct the time first
                   3024:                         */
                   3025:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   3026:                                goto out;
                   3027:
1.105     joris    3028:                        tv[0].tv_sec = rcstime;
                   3029:                        tv[0].tv_usec = 0;
                   3030:                        tv[1] = tv[0];
                   3031:                        if (utimes(fpath, tv) == -1)
                   3032:                                cvs_log(LP_ERRNO, "failed to set timestamps");
                   3033:                        break;
                   3034:                }
                   3035:        } else {
                   3036:                /* sanity */
                   3037:                if (cf->cf_type != DT_REG) {
                   3038:                        cvs_log(LP_ERR, "cvs_checkout_rev: none DT_REG file");
                   3039:                        goto out;
                   3040:                }
                   3041:
                   3042:                /*
                   3043:                 * if we are removing a file, we don't need this stuff.
                   3044:                 */
                   3045:                if (type != CHECKOUT_REV_REMOVED) {
1.107     joris    3046:                        if ((rcstime = cvs_hack_time(rcstime, 0)) == 0)
                   3047:                                goto out;
                   3048:
1.105     joris    3049:                        tp = gmtime(&rcstime);
                   3050:                        l = snprintf(timebuf, sizeof(timebuf),
                   3051:                            "%02d %s %d %02d:%02d:%02d -0000",
                   3052:                            tp->tm_mday, month_tab[tp->tm_mon],
                   3053:                            tp->tm_year + 1900, tp->tm_hour,
                   3054:                            tp->tm_min, tp->tm_sec);
                   3055:                        if (l == -1 || l >= (int)sizeof(timebuf))
                   3056:                                goto out;
                   3057:
                   3058:                        fsize = cvs_buf_len(bp);
                   3059:                        cvs_modetostr(cf->cf_mode, modestr, sizeof(modestr));
1.114     xsa      3060:                        cvs_buf_putc(bp, '\0');
1.105     joris    3061:                        content = cvs_buf_release(bp);
                   3062:                        bp = NULL;
                   3063:                }
                   3064:
                   3065:                if (type == CHECKOUT_REV_MERGED) {
                   3066:                        printf("Copy-file %s/\n", (cf->cf_dir != NULL) ?
                   3067:                            cf->cf_dir : ".");
                   3068:                        printf("%s/%s/%s\n", root->cr_dir, repo, cf->cf_name);
                   3069:                        printf("%s\n", copyfile);
                   3070:                }
                   3071:
                   3072:                switch (type) {
                   3073:                case CHECKOUT_REV_MERGED:
                   3074:                        printf("Merged");
                   3075:                        break;
                   3076:                case CHECKOUT_REV_REMOVED:
                   3077:                        printf("Removed");
                   3078:                        break;
                   3079:                case CHECKOUT_REV_CREATED:
                   3080:                        printf("Mod-time %s\n", timebuf);
                   3081:                        printf("Created");
                   3082:                        break;
                   3083:                default:
                   3084:                        cvs_log(LP_ERR, "cvs_checkout_rev: bad type %d",
                   3085:                            type);
                   3086:                        goto out;
                   3087:                }
                   3088:
                   3089:                printf(" %s/\n", (cf->cf_dir != NULL) ? cf->cf_dir : ".");
                   3090:                printf("%s/%s\n", repo, cf->cf_name);
                   3091:
                   3092:                if (type != CHECKOUT_REV_REMOVED) {
                   3093:                        printf("%s\n", entry);
                   3094:                        printf("%s\n%d\n%s", modestr, fsize, content);
                   3095:                }
                   3096:        }
                   3097:
                   3098:        ret = 0;
                   3099:
                   3100: out:
                   3101:        if (oldrev != NULL)
                   3102:                rcsnum_free(oldrev);
                   3103:        if (bp != NULL)
                   3104:                cvs_buf_free(bp);
                   3105:        if (content != NULL)
1.110     joris    3106:                xfree(content);
1.105     joris    3107:
                   3108:        return (ret);
                   3109: }
1.106     joris    3110:
                   3111: #endif