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

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