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

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