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

1.318   ! millert     1: /*     $OpenBSD: rcs.c,v 1.317 2017/05/31 16:31:55 joris 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.210     otto       27: #include <sys/stat.h>
                     28:
                     29: #include <ctype.h>
                     30: #include <errno.h>
                     31: #include <libgen.h>
                     32: #include <pwd.h>
                     33: #include <stdlib.h>
                     34: #include <string.h>
                     35: #include <unistd.h>
1.1       jfb        36:
1.219     tobias     37: #include "atomicio.h"
1.94      joris      38: #include "cvs.h"
1.172     joris      39: #include "diff.h"
1.1       jfb        40: #include "rcs.h"
1.303     tobias     41: #include "rcsparse.h"
1.1       jfb        42:
1.312     deraadt    43: #define MINIMUM(a, b)  (((a) < (b)) ? (a) : (b))
                     44:
1.117     niallo     45: #define RCS_KWEXP_SIZE  1024
1.1       jfb        46:
1.218     tobias     47: #define ANNOTATE_NEVER 0
                     48: #define ANNOTATE_NOW   1
                     49: #define ANNOTATE_LATER 2
                     50:
1.47      jfb        51: /* invalid characters in RCS symbol names */
1.49      jfb        52: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
1.47      jfb        53:
1.51      jfb        54: /* comment leaders, depending on the file's suffix */
                     55: static const struct rcs_comment {
1.57      xsa        56:        const char      *rc_suffix;
                     57:        const char      *rc_cstr;
1.51      jfb        58: } rcs_comments[] = {
                     59:        { "1",    ".\\\" " },
                     60:        { "2",    ".\\\" " },
                     61:        { "3",    ".\\\" " },
                     62:        { "4",    ".\\\" " },
                     63:        { "5",    ".\\\" " },
                     64:        { "6",    ".\\\" " },
                     65:        { "7",    ".\\\" " },
                     66:        { "8",    ".\\\" " },
                     67:        { "9",    ".\\\" " },
                     68:        { "a",    "-- "    },   /* Ada           */
                     69:        { "ada",  "-- "    },
                     70:        { "adb",  "-- "    },
                     71:        { "asm",  ";; "    },   /* assembler (MS-DOS) */
                     72:        { "ads",  "-- "    },   /* Ada */
                     73:        { "bat",  ":: "    },   /* batch (MS-DOS) */
                     74:        { "body", "-- "    },   /* Ada */
                     75:        { "c",    " * "    },   /* C */
                     76:        { "c++",  "// "    },   /* C++ */
                     77:        { "cc",   "// "    },
                     78:        { "cpp",  "// "    },
                     79:        { "cxx",  "// "    },
                     80:        { "m",    "// "    },   /* Objective-C */
                     81:        { "cl",   ";;; "   },   /* Common Lisp   */
                     82:        { "cmd",  ":: "    },   /* command (OS/2) */
                     83:        { "cmf",  "c "     },   /* CM Fortran    */
                     84:        { "csh",  "# "     },   /* shell         */
                     85:        { "e",    "# "     },   /* efl           */
                     86:        { "epsf", "% "     },   /* encapsulated postscript */
                     87:        { "epsi", "% "     },   /* encapsulated postscript */
                     88:        { "el",   "; "     },   /* Emacs Lisp    */
                     89:        { "f",    "c "     },   /* Fortran       */
                     90:        { "for",  "c "     },
                     91:        { "h",    " * "    },   /* C-header      */
                     92:        { "hh",   "// "    },   /* C++ header    */
                     93:        { "hpp",  "// "    },
                     94:        { "hxx",  "// "    },
                     95:        { "in",   "# "     },   /* for Makefile.in */
                     96:        { "l",    " * "    },   /* lex */
                     97:        { "mac",  ";; "    },   /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
                     98:        { "mak",  "# "     },   /* makefile, e.g. Visual C++ */
                     99:        { "me",   ".\\\" " },   /* me-macros    t/nroff  */
                    100:        { "ml",   "; "     },   /* mocklisp      */
                    101:        { "mm",   ".\\\" " },   /* mm-macros    t/nroff  */
                    102:        { "ms",   ".\\\" " },   /* ms-macros    t/nroff  */
                    103:        { "man",  ".\\\" " },   /* man-macros   t/nroff  */
                    104:        { "p",    " * "    },   /* pascal        */
                    105:        { "pas",  " * "    },
1.52      jfb       106:        { "pl",   "# "     },   /* Perl (conflict with Prolog) */
                    107:        { "pm",   "# "     },   /* Perl module */
1.51      jfb       108:        { "ps",   "% "     },   /* postscript */
                    109:        { "psw",  "% "     },   /* postscript wrap */
                    110:        { "pswm", "% "     },   /* postscript wrap */
                    111:        { "r",    "# "     },   /* ratfor        */
                    112:        { "rc",   " * "    },   /* Microsoft Windows resource file */
                    113:        { "red",  "% "     },   /* psl/rlisp     */
                    114:        { "sh",   "# "     },   /* shell         */
                    115:        { "sl",   "% "     },   /* psl           */
                    116:        { "spec", "-- "    },   /* Ada           */
                    117:        { "tex",  "% "     },   /* tex           */
                    118:        { "y",    " * "    },   /* yacc          */
                    119:        { "ye",   " * "    },   /* yacc-efl      */
                    120:        { "yr",   " * "    },   /* yacc-ratfor   */
                    121: };
                    122:
1.128     niallo    123: struct rcs_kw rcs_expkw[] =  {
                    124:        { "Author",     RCS_KW_AUTHOR   },
                    125:        { "Date",       RCS_KW_DATE     },
                    126:        { "Header",     RCS_KW_HEADER   },
                    127:        { "Id",         RCS_KW_ID       },
1.229     tobias    128:        { "Locker",     RCS_KW_LOCKER   },
1.128     niallo    129:        { "Log",        RCS_KW_LOG      },
                    130:        { "Name",       RCS_KW_NAME     },
                    131:        { "RCSfile",    RCS_KW_RCSFILE  },
                    132:        { "Revision",   RCS_KW_REVISION },
                    133:        { "Source",     RCS_KW_SOURCE   },
                    134:        { "State",      RCS_KW_STATE    },
1.211     niallo    135:        { "Mdocdate",   RCS_KW_MDOCDATE },
1.128     niallo    136: };
                    137:
1.57      xsa       138: #define NB_COMTYPES    (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
1.51      jfb       139:
1.246     tobias    140: static RCSNUM  *rcs_get_revision(const char *, RCSFILE *);
1.297     ray       141: int            rcs_patch_lines(struct rcs_lines *, struct rcs_lines *,
                    142:                    struct rcs_line **, struct rcs_delta *);
1.57      xsa       143: static void    rcs_freedelta(struct rcs_delta *);
1.155     ray       144: static void    rcs_strprint(const u_char *, size_t, FILE *);
1.57      xsa       145:
1.297     ray       146: static void    rcs_kwexp_line(char *, struct rcs_delta *, struct rcs_lines *,
                    147:                    struct rcs_line *, int mode);
1.26      jfb       148:
1.307     nicm      149: /*
                    150:  * Prepare RCSFILE for parsing. The given file descriptor (if any) must be
                    151:  * read-only and is closed on rcs_close().
                    152:  */
1.60      xsa       153: RCSFILE *
1.172     joris     154: rcs_open(const char *path, int fd, int flags, ...)
1.1       jfb       155: {
1.172     joris     156:        int mode;
1.259     tobias    157:        mode_t fmode;
1.1       jfb       158:        RCSFILE *rfp;
1.26      jfb       159:        va_list vap;
1.269     joris     160:        struct stat st;
1.109     joris     161:        struct rcs_delta *rdp;
                    162:        struct rcs_lock *lkr;
1.26      jfb       163:
1.152     niallo    164:        fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.26      jfb       165:        flags &= 0xffff;        /* ditch any internal flags */
1.1       jfb       166:
1.172     joris     167:        if (flags & RCS_CREATE) {
                    168:                va_start(vap, flags);
                    169:                mode = va_arg(vap, int);
                    170:                va_end(vap);
                    171:                fmode = (mode_t)mode;
1.269     joris     172:        } else {
                    173:                if (fstat(fd, &st) == -1)
                    174:                        fatal("rcs_open: %s: fstat: %s", path, strerror(errno));
                    175:                fmode = st.st_mode;
1.1       jfb       176:        }
1.228     tobias    177:
1.259     tobias    178:        fmode &= ~cvs_umask;
1.1       jfb       179:
1.161     ray       180:        rfp = xcalloc(1, sizeof(*rfp));
1.1       jfb       181:
1.110     joris     182:        rfp->rf_path = xstrdup(path);
1.142     niallo    183:        rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
1.26      jfb       184:        rfp->rf_mode = fmode;
1.305     tobias    185:        if (fd == -1)
                    186:                rfp->rf_file = NULL;
                    187:        else if ((rfp->rf_file = fdopen(fd, "r")) == NULL)
                    188:                fatal("rcs_open: %s: fdopen: %s", path, strerror(errno));
1.175     joris     189:        rfp->rf_dead = 0;
1.1       jfb       190:
                    191:        TAILQ_INIT(&(rfp->rf_delta));
1.29      jfb       192:        TAILQ_INIT(&(rfp->rf_access));
1.1       jfb       193:        TAILQ_INIT(&(rfp->rf_symbols));
                    194:        TAILQ_INIT(&(rfp->rf_locks));
                    195:
1.303     tobias    196:        if (!(rfp->rf_flags & RCS_CREATE)) {
                    197:                if (rcsparse_init(rfp))
                    198:                        fatal("could not parse admin data");
                    199:        }
1.1       jfb       200:
1.109     joris     201:        /* fill in rd_locker */
                    202:        TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
                    203:                if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
                    204:                        rcs_close(rfp);
                    205:                        return (NULL);
                    206:                }
                    207:
1.110     joris     208:                rdp->rd_locker = xstrdup(lkr->rl_name);
1.109     joris     209:        }
                    210:
1.1       jfb       211:        return (rfp);
                    212: }
                    213:
                    214: /*
                    215:  * rcs_close()
                    216:  *
                    217:  * Close an RCS file handle.
                    218:  */
                    219: void
                    220: rcs_close(RCSFILE *rfp)
                    221: {
                    222:        struct rcs_delta *rdp;
1.71      moritz    223:        struct rcs_access *rap;
1.13      jfb       224:        struct rcs_lock *rlp;
                    225:        struct rcs_sym *rsp;
1.1       jfb       226:
1.26      jfb       227:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    228:                rcs_write(rfp);
                    229:
1.1       jfb       230:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    231:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    232:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    233:                rcs_freedelta(rdp);
1.71      moritz    234:        }
                    235:
                    236:        while (!TAILQ_EMPTY(&(rfp->rf_access))) {
                    237:                rap = TAILQ_FIRST(&(rfp->rf_access));
                    238:                TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
1.313     nicm      239:                free(rap->ra_name);
                    240:                free(rap);
1.1       jfb       241:        }
                    242:
1.13      jfb       243:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    244:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    245:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
1.315     fcambus   246:                free(rsp->rs_num);
1.313     nicm      247:                free(rsp->rs_name);
                    248:                free(rsp);
1.13      jfb       249:        }
                    250:
                    251:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    252:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    253:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
1.315     fcambus   254:                free(rlp->rl_num);
1.313     nicm      255:                free(rlp->rl_name);
                    256:                free(rlp);
1.13      jfb       257:        }
                    258:
1.315     fcambus   259:        free(rfp->rf_head);
                    260:        free(rfp->rf_branch);
1.1       jfb       261:
1.305     tobias    262:        if (rfp->rf_file != NULL)
                    263:                fclose(rfp->rf_file);
1.313     nicm      264:        free(rfp->rf_path);
                    265:        free(rfp->rf_comment);
                    266:        free(rfp->rf_expand);
                    267:        free(rfp->rf_desc);
1.118     joris     268:        if (rfp->rf_pdata != NULL)
1.303     tobias    269:                rcsparse_free(rfp);
1.313     nicm      270:        free(rfp);
1.1       jfb       271: }
                    272:
                    273: /*
                    274:  * rcs_write()
                    275:  *
                    276:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    277:  * path is in <rf_path>.
                    278:  */
1.172     joris     279: void
1.1       jfb       280: rcs_write(RCSFILE *rfp)
                    281: {
                    282:        FILE *fp;
1.312     deraadt   283:        char   numbuf[CVS_REV_BUFSZ], *fn, tmpdir[PATH_MAX];
1.29      jfb       284:        struct rcs_access *ap;
1.1       jfb       285:        struct rcs_sym *symp;
1.46      jfb       286:        struct rcs_branch *brp;
1.1       jfb       287:        struct rcs_delta *rdp;
1.75      joris     288:        struct rcs_lock *lkp;
1.100     xsa       289:        size_t len;
1.207     otto      290:        int fd, saved_errno;
1.75      joris     291:
1.207     otto      292:        fd = -1;
1.1       jfb       293:
1.137     joris     294:        if (rfp->rf_flags & RCS_SYNCED)
1.181     joris     295:                return;
                    296:
                    297:        if (cvs_noexec == 1)
1.172     joris     298:                return;
1.137     joris     299:
1.117     niallo    300:        /* Write operations need the whole file parsed */
1.303     tobias    301:        if (rcsparse_deltatexts(rfp, NULL))
                    302:                fatal("rcs_write: rcsparse_deltatexts");
1.1       jfb       303:
1.207     otto      304:        if (strlcpy(tmpdir, rfp->rf_path, sizeof(tmpdir)) >= sizeof(tmpdir))
                    305:                fatal("rcs_write: truncation");
                    306:        (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", dirname(tmpdir));
1.172     joris     307:
1.113     xsa       308:        if ((fd = mkstemp(fn)) == -1)
1.172     joris     309:                fatal("%s", fn);
1.113     xsa       310:
1.207     otto      311:        if ((fp = fdopen(fd, "w")) == NULL) {
1.172     joris     312:                saved_errno = errno;
                    313:                (void)unlink(fn);
1.207     otto      314:                fatal("fdopen %s: %s", fn, strerror(saved_errno));
1.1       jfb       315:        }
1.284     joris     316:
1.296     ray       317:        worklist_add(fn, &temp_files);
1.1       jfb       318:
1.28      jfb       319:        if (rfp->rf_head != NULL)
                    320:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
                    321:        else
                    322:                numbuf[0] = '\0';
                    323:
1.1       jfb       324:        fprintf(fp, "head\t%s;\n", numbuf);
1.35      jfb       325:
                    326:        if (rfp->rf_branch != NULL) {
                    327:                rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
                    328:                fprintf(fp, "branch\t%s;\n", numbuf);
                    329:        }
                    330:
1.29      jfb       331:        fputs("access", fp);
                    332:        TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
                    333:                fprintf(fp, "\n\t%s", ap->ra_name);
                    334:        }
                    335:        fputs(";\n", fp);
1.1       jfb       336:
1.85      joris     337:        fprintf(fp, "symbols");
1.1       jfb       338:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
1.243     tobias    339:                if (RCSNUM_ISBRANCH(symp->rs_num))
                    340:                        rcsnum_addmagic(symp->rs_num);
1.1       jfb       341:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.292     zinovik   342:                fprintf(fp, "\n\t%s:%s", symp->rs_name, numbuf);
1.1       jfb       343:        }
                    344:        fprintf(fp, ";\n");
                    345:
1.75      joris     346:        fprintf(fp, "locks");
                    347:        TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
                    348:                rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
                    349:                fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
                    350:        }
                    351:
                    352:        fprintf(fp, ";");
1.1       jfb       353:
1.26      jfb       354:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       355:                fprintf(fp, " strict;");
                    356:        fputc('\n', fp);
                    357:
1.129     xsa       358:        fputs("comment\t@", fp);
1.42      jfb       359:        if (rfp->rf_comment != NULL) {
1.58      xsa       360:                rcs_strprint((const u_char *)rfp->rf_comment,
                    361:                    strlen(rfp->rf_comment), fp);
1.42      jfb       362:                fputs("@;\n", fp);
1.129     xsa       363:        } else
                    364:                fputs("# @;\n", fp);
1.1       jfb       365:
1.42      jfb       366:        if (rfp->rf_expand != NULL) {
                    367:                fputs("expand @", fp);
1.58      xsa       368:                rcs_strprint((const u_char *)rfp->rf_expand,
                    369:                    strlen(rfp->rf_expand), fp);
1.42      jfb       370:                fputs("@;\n", fp);
                    371:        }
1.1       jfb       372:
1.46      jfb       373:        fputs("\n\n", fp);
1.1       jfb       374:
                    375:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    376:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    377:                    sizeof(numbuf)));
                    378:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44      jfb       379:                    rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1       jfb       380:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    381:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    382:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    383:                    rdp->rd_author, rdp->rd_state);
1.46      jfb       384:                fputs("branches", fp);
                    385:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.232     tobias    386:                        fprintf(fp, "\n\t%s", rcsnum_tostr(brp->rb_num, numbuf,
1.46      jfb       387:                            sizeof(numbuf)));
                    388:                }
                    389:                fputs(";\n", fp);
1.1       jfb       390:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    391:                    numbuf, sizeof(numbuf)));
                    392:        }
                    393:
1.42      jfb       394:        fputs("\ndesc\n@", fp);
1.141     ray       395:        if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
1.100     xsa       396:                rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
                    397:                if (rfp->rf_desc[len-1] != '\n')
                    398:                        fputc('\n', fp);
                    399:        }
                    400:        fputs("@\n", fp);
1.1       jfb       401:
                    402:        /* deltatexts */
                    403:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.100     xsa       404:                fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
1.1       jfb       405:                    sizeof(numbuf)));
1.42      jfb       406:                fputs("log\n@", fp);
1.100     xsa       407:                if (rdp->rd_log != NULL) {
                    408:                        len = strlen(rdp->rd_log);
                    409:                        rcs_strprint((const u_char *)rdp->rd_log, len, fp);
1.294     nicm      410:                        if (len == 0 || rdp->rd_log[len-1] != '\n')
1.100     xsa       411:                                fputc('\n', fp);
                    412:                }
1.42      jfb       413:                fputs("@\ntext\n@", fp);
1.296     ray       414:                if (rdp->rd_text != NULL)
1.100     xsa       415:                        rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
                    416:                fputs("@\n", fp);
1.1       jfb       417:        }
1.269     joris     418:
1.207     otto      419:        if (fchmod(fd, rfp->rf_mode) == -1) {
                    420:                saved_errno = errno;
                    421:                (void)unlink(fn);
                    422:                fatal("fchmod %s: %s", fn, strerror(saved_errno));
                    423:        }
                    424:
1.172     joris     425:        (void)fclose(fp);
1.73      joris     426:
1.207     otto      427:        if (rename(fn, rfp->rf_path) == -1) {
                    428:                saved_errno = errno;
1.172     joris     429:                (void)unlink(fn);
1.207     otto      430:                fatal("rename(%s, %s): %s", fn, rfp->rf_path,
                    431:                    strerror(saved_errno));
1.172     joris     432:        }
1.73      joris     433:
1.172     joris     434:        rfp->rf_flags |= RCS_SYNCED;
1.313     nicm      435:        free(fn);
1.1       jfb       436: }
                    437:
                    438: /*
1.43      jfb       439:  * rcs_head_get()
                    440:  *
                    441:  * Retrieve the revision number of the head revision for the RCS file <file>.
                    442:  */
1.180     joris     443: RCSNUM *
1.43      jfb       444: rcs_head_get(RCSFILE *file)
                    445: {
1.213     niallo    446:        struct rcs_branch *brp;
                    447:        struct rcs_delta *rdp;
                    448:        RCSNUM *rev, *rootrev;
1.180     joris     449:
1.234     tobias    450:        if (file->rf_head == NULL)
                    451:                return NULL;
                    452:
1.213     niallo    453:        rev = rcsnum_alloc();
1.180     joris     454:        if (file->rf_branch != NULL) {
1.213     niallo    455:                /* we have a default branch, use that to calculate the
                    456:                 * real HEAD*/
                    457:                rootrev = rcsnum_alloc();
1.304     tobias    458:                rcsnum_cpy(file->rf_branch, rootrev,
                    459:                    file->rf_branch->rn_len - 1);
1.213     niallo    460:                if ((rdp = rcs_findrev(file, rootrev)) == NULL)
                    461:                        fatal("rcs_head_get: could not find root revision");
                    462:
                    463:                /* HEAD should be the last revision on the default branch */
                    464:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.304     tobias    465:                        if (rcsnum_cmp(brp->rb_num, file->rf_branch,
1.310     chl       466:                            file->rf_branch->rn_len) == 0)
1.213     niallo    467:                                break;
                    468:                }
1.315     fcambus   469:                free(rootrev);
1.304     tobias    470:
                    471:                if (brp == NULL)
                    472:                        fatal("rcs_head_get: could not find first default "
                    473:                            "branch revision");
1.226     tobias    474:
                    475:                if ((rdp = rcs_findrev(file, brp->rb_num)) == NULL)
                    476:                        fatal("rcs_head_get: could not find branch revision");
                    477:                while (rdp->rd_next->rn_len != 0)
                    478:                        if ((rdp = rcs_findrev(file, rdp->rd_next)) == NULL)
                    479:                                fatal("rcs_head_get: could not find "
                    480:                                    "next branch revision");
                    481:
                    482:                rcsnum_cpy(rdp->rd_num, rev, 0);
1.195     joris     483:        } else {
                    484:                rcsnum_cpy(file->rf_head, rev, 0);
1.180     joris     485:        }
                    486:
1.195     joris     487:        return (rev);
1.43      jfb       488: }
                    489:
                    490: /*
                    491:  * rcs_head_set()
                    492:  *
                    493:  * Set the revision number of the head revision for the RCS file <file> to
                    494:  * <rev>, which must reference a valid revision within the file.
                    495:  */
                    496: int
1.117     niallo    497: rcs_head_set(RCSFILE *file, RCSNUM *rev)
1.43      jfb       498: {
1.167     ray       499:        if (rcs_findrev(file, rev) == NULL)
1.43      jfb       500:                return (-1);
                    501:
1.111     joris     502:        if (file->rf_head == NULL)
                    503:                file->rf_head = rcsnum_alloc();
1.43      jfb       504:
1.111     joris     505:        rcsnum_cpy(rev, file->rf_head, 0);
1.70      moritz    506:        file->rf_flags &= ~RCS_SYNCED;
1.43      jfb       507:        return (0);
                    508: }
                    509:
1.276     tobias    510: /*
                    511:  * rcs_branch_new()
                    512:  *
                    513:  * Create a new branch out of supplied revision for the RCS file <file>.
                    514:  */
                    515: RCSNUM *
                    516: rcs_branch_new(RCSFILE *file, RCSNUM *rev)
                    517: {
                    518:        RCSNUM *brev;
                    519:        struct rcs_sym *sym;
                    520:
                    521:        if ((brev = rcsnum_new_branch(rev)) == NULL)
                    522:                return (NULL);
                    523:
                    524:        for (;;) {
                    525:                TAILQ_FOREACH(sym, &(file->rf_symbols), rs_list)
                    526:                        if (!rcsnum_cmp(sym->rs_num, brev, 0))
                    527:                                break;
                    528:
1.287     tobias    529:                if (sym == NULL)
1.276     tobias    530:                        break;
1.287     tobias    531:
                    532:                if (rcsnum_inc(brev) == NULL ||
                    533:                    rcsnum_inc(brev) == NULL) {
1.315     fcambus   534:                        free(brev);
1.287     tobias    535:                        return (NULL);
                    536:                }
1.276     tobias    537:        }
                    538:
                    539:        return (brev);
                    540: }
1.43      jfb       541:
                    542: /*
1.35      jfb       543:  * rcs_branch_get()
                    544:  *
                    545:  * Retrieve the default branch number for the RCS file <file>.
                    546:  * Returns the number on success.  If NULL is returned, then there is no
                    547:  * default branch for this file.
                    548:  */
1.60      xsa       549: const RCSNUM *
1.35      jfb       550: rcs_branch_get(RCSFILE *file)
                    551: {
                    552:        return (file->rf_branch);
                    553: }
                    554:
                    555: /*
                    556:  * rcs_branch_set()
                    557:  *
                    558:  * Set the default branch for the RCS file <file> to <bnum>.
                    559:  * Returns 0 on success, -1 on failure.
                    560:  */
                    561: int
                    562: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
                    563: {
1.111     joris     564:        if (file->rf_branch == NULL)
                    565:                file->rf_branch = rcsnum_alloc();
1.35      jfb       566:
1.111     joris     567:        rcsnum_cpy(bnum, file->rf_branch, 0);
1.70      moritz    568:        file->rf_flags &= ~RCS_SYNCED;
1.35      jfb       569:        return (0);
                    570: }
                    571:
                    572: /*
1.29      jfb       573:  * rcs_access_add()
                    574:  *
                    575:  * Add the login name <login> to the access list for the RCS file <file>.
                    576:  * Returns 0 on success, or -1 on failure.
                    577:  */
                    578: int
                    579: rcs_access_add(RCSFILE *file, const char *login)
                    580: {
                    581:        struct rcs_access *ap;
                    582:
                    583:        /* first look for duplication */
                    584:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
1.254     joris     585:                if (strcmp(ap->ra_name, login) == 0)
1.29      jfb       586:                        return (-1);
                    587:        }
                    588:
1.161     ray       589:        ap = xmalloc(sizeof(*ap));
1.110     joris     590:        ap->ra_name = xstrdup(login);
1.29      jfb       591:        TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
                    592:
                    593:        /* not synced anymore */
                    594:        file->rf_flags &= ~RCS_SYNCED;
                    595:        return (0);
                    596: }
                    597:
                    598: /*
                    599:  * rcs_access_remove()
                    600:  *
                    601:  * Remove an entry with login name <login> from the access list of the RCS
                    602:  * file <file>.
                    603:  * Returns 0 on success, or -1 on failure.
                    604:  */
                    605: int
                    606: rcs_access_remove(RCSFILE *file, const char *login)
                    607: {
                    608:        struct rcs_access *ap;
                    609:
                    610:        TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
                    611:                if (strcmp(ap->ra_name, login) == 0)
                    612:                        break;
                    613:
1.254     joris     614:        if (ap == NULL)
1.29      jfb       615:                return (-1);
                    616:
                    617:        TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.313     nicm      618:        free(ap->ra_name);
                    619:        free(ap);
1.29      jfb       620:
                    621:        /* not synced anymore */
                    622:        file->rf_flags &= ~RCS_SYNCED;
                    623:        return (0);
                    624: }
                    625:
                    626: /*
1.26      jfb       627:  * rcs_sym_add()
1.1       jfb       628:  *
                    629:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    630:  * is named <sym> and is bound to the RCS revision <snum>.
                    631:  */
                    632: int
1.26      jfb       633: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1       jfb       634: {
                    635:        struct rcs_sym *symp;
                    636:
1.254     joris     637:        if (!rcs_sym_check(sym))
1.69      moritz    638:                return (-1);
1.47      jfb       639:
1.1       jfb       640:        /* first look for duplication */
                    641:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
1.254     joris     642:                if (strcmp(symp->rs_name, sym) == 0)
                    643:                        return (1);
1.1       jfb       644:        }
                    645:
1.161     ray       646:        symp = xmalloc(sizeof(*symp));
1.110     joris     647:        symp->rs_name = xstrdup(sym);
1.111     joris     648:        symp->rs_num = rcsnum_alloc();
1.1       jfb       649:        rcsnum_cpy(snum, symp->rs_num, 0);
                    650:
                    651:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    652:
                    653:        /* not synced anymore */
1.26      jfb       654:        rfp->rf_flags &= ~RCS_SYNCED;
1.1       jfb       655:        return (0);
                    656: }
                    657:
                    658: /*
1.27      jfb       659:  * rcs_sym_remove()
                    660:  *
                    661:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    662:  * <file>.  If no such symbol is found, the call fails and returns with an
                    663:  * error.
                    664:  * Returns 0 on success, or -1 on failure.
                    665:  */
                    666: int
                    667: rcs_sym_remove(RCSFILE *file, const char *sym)
                    668: {
                    669:        struct rcs_sym *symp;
                    670:
1.254     joris     671:        if (!rcs_sym_check(sym))
1.69      moritz    672:                return (-1);
1.47      jfb       673:
1.27      jfb       674:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    675:                if (strcmp(symp->rs_name, sym) == 0)
                    676:                        break;
                    677:
1.254     joris     678:        if (symp == NULL)
1.27      jfb       679:                return (-1);
                    680:
                    681:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.313     nicm      682:        free(symp->rs_name);
1.315     fcambus   683:        free(symp->rs_num);
1.313     nicm      684:        free(symp);
1.27      jfb       685:
                    686:        /* not synced anymore */
                    687:        file->rf_flags &= ~RCS_SYNCED;
                    688:        return (0);
1.184     xsa       689: }
                    690:
                    691: /*
                    692:  * rcs_sym_get()
                    693:  *
                    694:  * Find a specific symbol <sym> entry in the tree of the RCS file <file>.
                    695:  *
                    696:  * Returns a pointer to the symbol on success, or NULL on failure.
                    697:  */
                    698: struct rcs_sym *
                    699: rcs_sym_get(RCSFILE *file, const char *sym)
                    700: {
                    701:        struct rcs_sym *symp;
                    702:
                    703:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    704:                if (strcmp(symp->rs_name, sym) == 0)
                    705:                        return (symp);
                    706:
                    707:        return (NULL);
1.27      jfb       708: }
                    709:
                    710: /*
                    711:  * rcs_sym_getrev()
                    712:  *
                    713:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    714:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    715:  * should be freed by the caller once they are done with it.
                    716:  * Returns the RCSNUM on success, or NULL on failure.
                    717:  */
1.60      xsa       718: RCSNUM *
1.27      jfb       719: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    720: {
                    721:        RCSNUM *num;
                    722:        struct rcs_sym *symp;
                    723:
1.275     tobias    724:        if (!rcs_sym_check(sym) || file->rf_head == NULL)
1.47      jfb       725:                return (NULL);
                    726:
1.176     joris     727:        if (!strcmp(sym, RCS_HEAD_BRANCH)) {
                    728:                num = rcsnum_alloc();
                    729:                rcsnum_cpy(file->rf_head, num, 0);
                    730:                return (num);
                    731:        }
                    732:
1.27      jfb       733:        num = NULL;
                    734:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    735:                if (strcmp(symp->rs_name, sym) == 0)
                    736:                        break;
                    737:
1.254     joris     738:        if (symp != NULL) {
1.111     joris     739:                num = rcsnum_alloc();
                    740:                rcsnum_cpy(symp->rs_num, num, 0);
1.27      jfb       741:        }
                    742:
                    743:        return (num);
1.47      jfb       744: }
                    745:
                    746: /*
                    747:  * rcs_sym_check()
                    748:  *
                    749:  * Check the RCS symbol name <sym> for any unsupported characters.
                    750:  * Returns 1 if the tag is correct, 0 if it isn't valid.
                    751:  */
                    752: int
                    753: rcs_sym_check(const char *sym)
                    754: {
                    755:        int ret;
1.311     okan      756:        const unsigned char *cp;
1.47      jfb       757:
                    758:        ret = 1;
                    759:        cp = sym;
                    760:        if (!isalpha(*cp++))
                    761:                return (0);
                    762:
                    763:        for (; *cp != '\0'; cp++)
                    764:                if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
                    765:                        ret = 0;
                    766:                        break;
                    767:                }
                    768:
                    769:        return (ret);
1.30      jfb       770: }
                    771:
                    772: /*
                    773:  * rcs_lock_getmode()
                    774:  *
                    775:  * Retrieve the locking mode of the RCS file <file>.
                    776:  */
                    777: int
                    778: rcs_lock_getmode(RCSFILE *file)
                    779: {
                    780:        return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
                    781: }
                    782:
                    783: /*
                    784:  * rcs_lock_setmode()
                    785:  *
                    786:  * Set the locking mode of the RCS file <file> to <mode>, which must either
                    787:  * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
                    788:  * Returns the previous mode on success, or -1 on failure.
                    789:  */
                    790: int
                    791: rcs_lock_setmode(RCSFILE *file, int mode)
                    792: {
                    793:        int pmode;
                    794:        pmode = rcs_lock_getmode(file);
                    795:
                    796:        if (mode == RCS_LOCK_STRICT)
                    797:                file->rf_flags |= RCS_SLOCK;
                    798:        else if (mode == RCS_LOCK_LOOSE)
                    799:                file->rf_flags &= ~RCS_SLOCK;
1.145     xsa       800:        else
                    801:                fatal("rcs_lock_setmode: invalid mode `%d'", mode);
1.30      jfb       802:
1.70      moritz    803:        file->rf_flags &= ~RCS_SYNCED;
1.30      jfb       804:        return (pmode);
1.27      jfb       805: }
                    806:
                    807: /*
1.40      jfb       808:  * rcs_lock_add()
                    809:  *
                    810:  * Add an RCS lock for the user <user> on revision <rev>.
                    811:  * Returns 0 on success, or -1 on failure.
                    812:  */
                    813: int
                    814: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
                    815: {
                    816:        struct rcs_lock *lkp;
                    817:
                    818:        /* first look for duplication */
                    819:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168     deraadt   820:                if (strcmp(lkp->rl_name, user) == 0 &&
1.254     joris     821:                    rcsnum_cmp(rev, lkp->rl_num, 0) == 0)
1.40      jfb       822:                        return (-1);
                    823:        }
                    824:
1.161     ray       825:        lkp = xmalloc(sizeof(*lkp));
1.110     joris     826:        lkp->rl_name = xstrdup(user);
1.111     joris     827:        lkp->rl_num = rcsnum_alloc();
1.68      moritz    828:        rcsnum_cpy(rev, lkp->rl_num, 0);
1.40      jfb       829:
                    830:        TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
                    831:
                    832:        /* not synced anymore */
                    833:        file->rf_flags &= ~RCS_SYNCED;
                    834:        return (0);
                    835: }
                    836:
                    837:
                    838: /*
                    839:  * rcs_lock_remove()
                    840:  *
                    841:  * Remove the RCS lock on revision <rev>.
                    842:  * Returns 0 on success, or -1 on failure.
                    843:  */
                    844: int
1.109     joris     845: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
1.40      jfb       846: {
                    847:        struct rcs_lock *lkp;
                    848:
1.109     joris     849:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
1.168     deraadt   850:                if (strcmp(lkp->rl_name, user) == 0 &&
                    851:                    rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
1.40      jfb       852:                        break;
1.109     joris     853:        }
1.40      jfb       854:
1.254     joris     855:        if (lkp == NULL)
1.40      jfb       856:                return (-1);
                    857:
                    858:        TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
1.315     fcambus   859:        free(lkp->rl_num);
1.313     nicm      860:        free(lkp->rl_name);
                    861:        free(lkp);
1.40      jfb       862:
                    863:        /* not synced anymore */
                    864:        file->rf_flags &= ~RCS_SYNCED;
                    865:        return (0);
                    866: }
                    867:
                    868: /*
1.27      jfb       869:  * rcs_desc_get()
                    870:  *
                    871:  * Retrieve the description for the RCS file <file>.
                    872:  */
1.57      xsa       873: const char *
1.27      jfb       874: rcs_desc_get(RCSFILE *file)
                    875: {
                    876:        return (file->rf_desc);
                    877: }
                    878:
                    879: /*
                    880:  * rcs_desc_set()
                    881:  *
                    882:  * Set the description for the RCS file <file>.
                    883:  */
1.149     xsa       884: void
1.27      jfb       885: rcs_desc_set(RCSFILE *file, const char *desc)
                    886: {
                    887:        char *tmp;
                    888:
1.110     joris     889:        tmp = xstrdup(desc);
1.313     nicm      890:        free(file->rf_desc);
1.27      jfb       891:        file->rf_desc = tmp;
                    892:        file->rf_flags &= ~RCS_SYNCED;
1.51      jfb       893: }
                    894:
                    895: /*
                    896:  * rcs_comment_lookup()
                    897:  *
                    898:  * Lookup the assumed comment leader based on a file's suffix.
                    899:  * Returns a pointer to the string on success, or NULL on failure.
                    900:  */
1.57      xsa       901: const char *
1.51      jfb       902: rcs_comment_lookup(const char *filename)
                    903: {
                    904:        int i;
                    905:        const char *sp;
                    906:
1.254     joris     907:        if ((sp = strrchr(filename, '.')) == NULL)
1.51      jfb       908:                return (NULL);
                    909:        sp++;
                    910:
                    911:        for (i = 0; i < (int)NB_COMTYPES; i++)
                    912:                if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
                    913:                        return (rcs_comments[i].rc_cstr);
                    914:        return (NULL);
1.27      jfb       915: }
                    916:
1.33      jfb       917: /*
                    918:  * rcs_comment_get()
                    919:  *
                    920:  * Retrieve the comment leader for the RCS file <file>.
                    921:  */
1.57      xsa       922: const char *
1.33      jfb       923: rcs_comment_get(RCSFILE *file)
                    924: {
                    925:        return (file->rf_comment);
                    926: }
                    927:
                    928: /*
                    929:  * rcs_comment_set()
                    930:  *
                    931:  * Set the comment leader for the RCS file <file>.
                    932:  */
1.150     xsa       933: void
1.33      jfb       934: rcs_comment_set(RCSFILE *file, const char *comment)
                    935: {
                    936:        char *tmp;
                    937:
1.110     joris     938:        tmp = xstrdup(comment);
1.313     nicm      939:        free(file->rf_comment);
1.33      jfb       940:        file->rf_comment = tmp;
                    941:        file->rf_flags &= ~RCS_SYNCED;
1.40      jfb       942: }
                    943:
1.94      joris     944: int
1.297     ray       945: rcs_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines,
                    946:     struct rcs_line **alines, struct rcs_delta *rdp)
1.5       vincent   947: {
1.209     otto      948:        u_char op;
                    949:        char *ep;
1.297     ray       950:        struct rcs_line *lp, *dlp, *ndlp;
1.5       vincent   951:        int i, lineno, nbln;
1.193     niallo    952:        u_char tmp;
1.1       jfb       953:
1.94      joris     954:        dlp = TAILQ_FIRST(&(dlines->l_lines));
                    955:        lp = TAILQ_FIRST(&(plines->l_lines));
1.1       jfb       956:
                    957:        /* skip first bogus line */
1.94      joris     958:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                    959:            lp = TAILQ_NEXT(lp, l_list)) {
1.193     niallo    960:                if (lp->l_len < 2)
                    961:                        fatal("line too short, RCS patch seems broken");
1.94      joris     962:                op = *(lp->l_line);
1.193     niallo    963:                /* NUL-terminate line buffer for strtol() safety. */
                    964:                tmp = lp->l_line[lp->l_len - 1];
                    965:                lp->l_line[lp->l_len - 1] = '\0';
1.209     otto      966:                lineno = (int)strtol((char*)(lp->l_line + 1), &ep, 10);
1.193     niallo    967:                if (lineno - 1 > dlines->l_nblines || lineno < 0) {
                    968:                        fatal("invalid line specification in RCS patch");
                    969:                }
1.1       jfb       970:                ep++;
                    971:                nbln = (int)strtol(ep, &ep, 10);
1.193     niallo    972:                /* Restore the last byte of the buffer */
                    973:                lp->l_line[lp->l_len - 1] = tmp;
                    974:                if (nbln < 0)
1.132     niallo    975:                        fatal("invalid line number specification in RCS patch");
1.1       jfb       976:
                    977:                /* find the appropriate line */
                    978:                for (;;) {
                    979:                        if (dlp == NULL)
                    980:                                break;
1.94      joris     981:                        if (dlp->l_lineno == lineno)
1.1       jfb       982:                                break;
1.94      joris     983:                        if (dlp->l_lineno > lineno) {
1.296     ray       984:                                dlp = TAILQ_PREV(dlp, tqh, l_list);
1.94      joris     985:                        } else if (dlp->l_lineno < lineno) {
1.133     niallo    986:                                if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1.168     deraadt   987:                                    ndlp->l_lineno > lineno)
1.1       jfb       988:                                        break;
                    989:                                dlp = ndlp;
                    990:                        }
                    991:                }
1.132     niallo    992:                if (dlp == NULL)
                    993:                        fatal("can't find referenced line in RCS patch");
1.1       jfb       994:
                    995:                if (op == 'd') {
                    996:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1.94      joris     997:                                ndlp = TAILQ_NEXT(dlp, l_list);
                    998:                                TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1.218     tobias    999:                                if (alines != NULL && dlp->l_line != NULL) {
                   1000:                                        dlp->l_delta = rdp;
                   1001:                                        alines[dlp->l_lineno_orig - 1] =
                   1002:                                                dlp;
                   1003:                                } else
1.313     nicm     1004:                                        free(dlp);
1.1       jfb      1005:                                dlp = ndlp;
1.133     niallo   1006:                                /* last line is gone - reset dlp */
                   1007:                                if (dlp == NULL) {
                   1008:                                        ndlp = TAILQ_LAST(&(dlines->l_lines),
1.296     ray      1009:                                            tqh);
1.133     niallo   1010:                                        dlp = ndlp;
                   1011:                                }
1.1       jfb      1012:                        }
1.14      deraadt  1013:                } else if (op == 'a') {
1.1       jfb      1014:                        for (i = 0; i < nbln; i++) {
                   1015:                                ndlp = lp;
1.94      joris    1016:                                lp = TAILQ_NEXT(lp, l_list);
1.132     niallo   1017:                                if (lp == NULL)
                   1018:                                        fatal("truncated RCS patch");
1.94      joris    1019:                                TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
1.218     tobias   1020:                                if (alines != NULL) {
                   1021:                                        if (lp->l_needsfree == 1)
1.313     nicm     1022:                                                free(lp->l_line);
1.218     tobias   1023:                                        lp->l_line = NULL;
                   1024:                                        lp->l_needsfree = 0;
                   1025:                                }
1.225     tobias   1026:                                lp->l_delta = rdp;
1.94      joris    1027:                                TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
                   1028:                                    lp, l_list);
1.1       jfb      1029:                                dlp = lp;
                   1030:
                   1031:                                /* we don't want lookup to block on those */
1.94      joris    1032:                                lp->l_lineno = lineno;
1.1       jfb      1033:
                   1034:                                lp = ndlp;
                   1035:                        }
1.132     niallo   1036:                } else
                   1037:                        fatal("unknown RCS patch operation `%c'", op);
1.1       jfb      1038:
                   1039:                /* last line of the patch, done */
1.94      joris    1040:                if (lp->l_lineno == plines->l_nblines)
1.1       jfb      1041:                        break;
                   1042:        }
                   1043:
                   1044:        /* once we're done patching, rebuild the line numbers */
1.2       vincent  1045:        lineno = 0;
1.94      joris    1046:        TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
                   1047:                lp->l_lineno = lineno++;
                   1048:        dlines->l_nblines = lineno - 1;
1.1       jfb      1049:
1.5       vincent  1050:        return (0);
1.241     joris    1051: }
                   1052:
                   1053: void
                   1054: rcs_delta_stats(struct rcs_delta *rdp, int *ladded, int *lremoved)
                   1055: {
1.297     ray      1056:        struct rcs_lines *plines;
                   1057:        struct rcs_line *lp;
1.308     nicm     1058:        int added, i, nbln, removed;
1.241     joris    1059:        char op, *ep;
                   1060:        u_char tmp;
                   1061:
                   1062:        added = removed = 0;
                   1063:
                   1064:        plines = cvs_splitlines(rdp->rd_text, rdp->rd_tlen);
                   1065:        lp = TAILQ_FIRST(&(plines->l_lines));
                   1066:
                   1067:        /* skip first bogus line */
                   1068:        for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
                   1069:            lp = TAILQ_NEXT(lp, l_list)) {
                   1070:                if (lp->l_len < 2)
                   1071:                        fatal("line too short, RCS patch seems broken");
                   1072:                op = *(lp->l_line);
                   1073:                /* NUL-terminate line buffer for strtol() safety. */
                   1074:                tmp = lp->l_line[lp->l_len - 1];
                   1075:                lp->l_line[lp->l_len - 1] = '\0';
1.308     nicm     1076:                (void)strtol((lp->l_line + 1), &ep, 10);
1.241     joris    1077:                ep++;
                   1078:                nbln = (int)strtol(ep, &ep, 10);
                   1079:                /* Restore the last byte of the buffer */
                   1080:                lp->l_line[lp->l_len - 1] = tmp;
                   1081:                if (nbln < 0)
                   1082:                        fatal("invalid line number specification in RCS patch");
                   1083:
                   1084:                if (op == 'a') {
                   1085:                        added += nbln;
                   1086:                        for (i = 0; i < nbln; i++) {
                   1087:                                lp = TAILQ_NEXT(lp, l_list);
                   1088:                                if (lp == NULL)
                   1089:                                        fatal("truncated RCS patch");
                   1090:                        }
                   1091:                }
                   1092:                else if (op == 'd')
                   1093:                        removed += nbln;
                   1094:                else
                   1095:                        fatal("unknown RCS patch operation '%c'", op);
                   1096:        }
1.263     tobias   1097:
                   1098:        cvs_freelines(plines);
1.241     joris    1099:
                   1100:        *ladded = added;
                   1101:        *lremoved = removed;
1.1       jfb      1102: }
                   1103:
                   1104: /*
1.52      jfb      1105:  * rcs_rev_add()
                   1106:  *
1.53      jfb      1107:  * Add a revision to the RCS file <rf>.  The new revision's number can be
                   1108:  * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
                   1109:  * new revision will have a number equal to the previous head revision plus
                   1110:  * one).  The <msg> argument specifies the log message for that revision, and
                   1111:  * <date> specifies the revision's date (a value of -1 is
                   1112:  * equivalent to using the current time).
1.295     ray      1113:  * If <author> is NULL, set the author for this revision to the current user.
1.52      jfb      1114:  * Returns 0 on success, or -1 on failure.
                   1115:  */
                   1116: int
1.90      niallo   1117: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1.295     ray      1118:     const char *author)
1.52      jfb      1119: {
                   1120:        time_t now;
1.264     tobias   1121:        RCSNUM *root = NULL;
1.52      jfb      1122:        struct passwd *pw;
1.264     tobias   1123:        struct rcs_branch *brp, *obrp;
1.83      joris    1124:        struct rcs_delta *ordp, *rdp;
                   1125:
1.52      jfb      1126:        if (rev == RCS_HEAD_REV) {
1.101     niallo   1127:                if (rf->rf_flags & RCS_CREATE) {
                   1128:                        if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
                   1129:                                return (-1);
1.313     nicm     1130:                        free(rf->rf_head);
1.299     tobias   1131:                        rf->rf_head = rev;
1.275     tobias   1132:                } else if (rf->rf_head == NULL) {
                   1133:                        return (-1);
1.101     niallo   1134:                } else {
                   1135:                        rev = rcsnum_inc(rf->rf_head);
                   1136:                }
1.83      joris    1137:        } else {
1.254     joris    1138:                if ((rdp = rcs_findrev(rf, rev)) != NULL)
1.83      joris    1139:                        return (-1);
1.52      jfb      1140:        }
                   1141:
1.161     ray      1142:        rdp = xcalloc(1, sizeof(*rdp));
1.52      jfb      1143:
                   1144:        TAILQ_INIT(&(rdp->rd_branches));
                   1145:
1.111     joris    1146:        rdp->rd_num = rcsnum_alloc();
1.52      jfb      1147:        rcsnum_cpy(rev, rdp->rd_num, 0);
1.83      joris    1148:
1.111     joris    1149:        rdp->rd_next = rcsnum_alloc();
1.92      joris    1150:
1.295     ray      1151:        if (!author && !(author = getlogin())) {
                   1152:                if (!(pw = getpwuid(getuid())))
                   1153:                        fatal("getpwuid failed");
                   1154:                author = pw->pw_name;
                   1155:        }
                   1156:        rdp->rd_author = xstrdup(author);
1.110     joris    1157:        rdp->rd_state = xstrdup(RCS_STATE_EXP);
                   1158:        rdp->rd_log = xstrdup(msg);
1.52      jfb      1159:
1.53      jfb      1160:        if (date != (time_t)(-1))
                   1161:                now = date;
                   1162:        else
                   1163:                time(&now);
1.52      jfb      1164:        gmtime_r(&now, &(rdp->rd_date));
                   1165:
1.180     joris    1166:        if (RCSNUM_ISBRANCHREV(rev))
                   1167:                TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
                   1168:        else
                   1169:                TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1.52      jfb      1170:        rf->rf_ndelta++;
1.81      niallo   1171:
1.180     joris    1172:        if (!(rf->rf_flags & RCS_CREATE)) {
                   1173:                if (RCSNUM_ISBRANCHREV(rev)) {
1.264     tobias   1174:                        if (rev->rn_id[rev->rn_len - 1] == 1) {
                   1175:                                /* a new branch */
                   1176:                                root = rcsnum_branch_root(rev);
                   1177:                                brp = xmalloc(sizeof(*brp));
                   1178:                                brp->rb_num = rcsnum_alloc();
                   1179:                                rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
                   1180:
                   1181:                                if ((ordp = rcs_findrev(rf, root)) == NULL)
                   1182:                                        fatal("root node not found");
                   1183:
                   1184:                                TAILQ_FOREACH(obrp, &(ordp->rd_branches),
                   1185:                                    rb_list) {
                   1186:                                        if (!rcsnum_cmp(obrp->rb_num,
                   1187:                                            brp->rb_num,
                   1188:                                            brp->rb_num->rn_len - 1))
                   1189:                                                break;
                   1190:                                }
1.222     joris    1191:
1.264     tobias   1192:                                if (obrp == NULL) {
                   1193:                                        TAILQ_INSERT_TAIL(&(ordp->rd_branches),
                   1194:                                            brp, rb_list);
                   1195:                                }
                   1196:                        } else {
                   1197:                                root = rcsnum_alloc();
                   1198:                                rcsnum_cpy(rev, root, 0);
                   1199:                                rcsnum_dec(root);
                   1200:                                if ((ordp = rcs_findrev(rf, root)) == NULL)
                   1201:                                        fatal("previous revision not found");
1.222     joris    1202:                                rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
1.264     tobias   1203:                        }
1.180     joris    1204:                } else {
                   1205:                        ordp = TAILQ_NEXT(rdp, rd_list);
                   1206:                        rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
                   1207:                }
                   1208:        }
                   1209:
1.315     fcambus  1210:        free(root);
1.264     tobias   1211:
1.64      niallo   1212:        /* not synced anymore */
                   1213:        rf->rf_flags &= ~RCS_SYNCED;
1.52      jfb      1214:
                   1215:        return (0);
                   1216: }
                   1217:
                   1218: /*
                   1219:  * rcs_rev_remove()
                   1220:  *
                   1221:  * Remove the revision whose number is <rev> from the RCS file <rf>.
                   1222:  */
                   1223: int
                   1224: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
                   1225: {
1.251     joris    1226:        int fd1, fd2;
1.194     joris    1227:        char *path_tmp1, *path_tmp2;
1.166     joris    1228:        struct rcs_delta *rdp, *prevrdp, *nextrdp;
1.209     otto     1229:        BUF *prevbuf, *newdiff, *newdeltatext;
1.166     joris    1230:
1.52      jfb      1231:        if (rev == RCS_HEAD_REV)
                   1232:                rev = rf->rf_head;
                   1233:
1.275     tobias   1234:        if (rev == NULL)
                   1235:                return (-1);
                   1236:
1.52      jfb      1237:        /* do we actually have that revision? */
1.254     joris    1238:        if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.166     joris    1239:                return (-1);
                   1240:
                   1241:        /*
                   1242:         * This is confusing, the previous delta is next in the TAILQ list.
                   1243:         * the next delta is the previous one in the TAILQ list.
                   1244:         *
                   1245:         * When the HEAD revision got specified, nextrdp will be NULL.
                   1246:         * When the first revision got specified, prevrdp will be NULL.
                   1247:         */
                   1248:        prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
1.296     ray      1249:        nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, tqh, rd_list);
1.166     joris    1250:
                   1251:        newdeltatext = NULL;
1.209     otto     1252:        prevbuf = NULL;
1.262     joris    1253:        path_tmp1 = path_tmp2 = NULL;
1.166     joris    1254:
1.168     deraadt  1255:        if (prevrdp != NULL && nextrdp != NULL) {
1.297     ray      1256:                newdiff = buf_alloc(64);
1.166     joris    1257:
                   1258:                /* calculate new diff */
1.172     joris    1259:                (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.251     joris    1260:                fd1 = rcs_rev_write_stmp(rf, nextrdp->rd_num, path_tmp1, 0);
1.166     joris    1261:
1.172     joris    1262:                (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.251     joris    1263:                fd2 = rcs_rev_write_stmp(rf, prevrdp->rd_num, path_tmp2, 0);
1.166     joris    1264:
                   1265:                diff_format = D_RCSDIFF;
1.291     ray      1266:                if (diffreg(path_tmp1, path_tmp2,
1.290     ray      1267:                    fd1, fd2, newdiff, D_FORCEASCII) == D_ERROR)
1.172     joris    1268:                        fatal("rcs_diffreg failed");
1.166     joris    1269:
1.251     joris    1270:                close(fd1);
                   1271:                close(fd2);
                   1272:
1.194     joris    1273:                newdeltatext = newdiff;
1.168     deraadt  1274:        } else if (nextrdp == NULL && prevrdp != NULL) {
1.194     joris    1275:                newdeltatext = prevbuf;
1.166     joris    1276:        }
                   1277:
                   1278:        if (newdeltatext != NULL) {
                   1279:                if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
                   1280:                        fatal("error setting new deltatext");
                   1281:        }
                   1282:
                   1283:        TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
                   1284:
                   1285:        /* update pointers */
1.168     deraadt  1286:        if (prevrdp != NULL && nextrdp != NULL) {
1.166     joris    1287:                rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
                   1288:        } else if (prevrdp != NULL) {
1.170     xsa      1289:                if (rcs_head_set(rf, prevrdp->rd_num) < 0)
                   1290:                        fatal("rcs_head_set failed");
1.166     joris    1291:        } else if (nextrdp != NULL) {
1.315     fcambus  1292:                free(nextrdp->rd_next);
1.166     joris    1293:                nextrdp->rd_next = rcsnum_alloc();
1.52      jfb      1294:        } else {
1.315     fcambus  1295:                free(rf->rf_head);
1.166     joris    1296:                rf->rf_head = NULL;
1.52      jfb      1297:        }
                   1298:
1.166     joris    1299:        rf->rf_ndelta--;
                   1300:        rf->rf_flags &= ~RCS_SYNCED;
                   1301:
                   1302:        rcs_freedelta(rdp);
1.313     nicm     1303:        free(newdeltatext);
                   1304:        free(path_tmp1);
                   1305:        free(path_tmp2);
1.172     joris    1306:
1.166     joris    1307:        return (0);
1.52      jfb      1308: }
                   1309:
                   1310: /*
1.1       jfb      1311:  * rcs_findrev()
                   1312:  *
                   1313:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                   1314:  * The revision number is given in <rev>.
1.156     joris    1315:  *
1.1       jfb      1316:  * Returns a pointer to the delta on success, or NULL on failure.
                   1317:  */
1.102     xsa      1318: struct rcs_delta *
1.117     niallo   1319: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1.1       jfb      1320: {
1.178     joris    1321:        int isbrev;
1.156     joris    1322:        struct rcs_delta *rdp;
1.178     joris    1323:
1.261     tobias   1324:        if (rev == NULL)
                   1325:                return NULL;
                   1326:
1.178     joris    1327:        isbrev = RCSNUM_ISBRANCHREV(rev);
1.26      jfb      1328:
1.117     niallo   1329:        /*
                   1330:         * We need to do more parsing if the last revision in the linked list
                   1331:         * is greater than the requested revision.
                   1332:         */
1.156     joris    1333:        rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1.168     deraadt  1334:        if (rdp == NULL ||
1.178     joris    1335:            (!isbrev && rcsnum_cmp(rdp->rd_num, rev, 0) == -1) ||
                   1336:            ((isbrev && rdp->rd_num->rn_len < 4) ||
                   1337:            (isbrev && rcsnum_differ(rev, rdp->rd_num)))) {
1.303     tobias   1338:                if (rcsparse_deltas(rfp, rev))
                   1339:                        fatal("error parsing deltas");
1.117     niallo   1340:        }
                   1341:
1.156     joris    1342:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1.178     joris    1343:                if (rcsnum_differ(rdp->rd_num, rev))
                   1344:                        continue;
                   1345:                else
1.156     joris    1346:                        return (rdp);
                   1347:        }
1.1       jfb      1348:
                   1349:        return (NULL);
1.20      jfb      1350: }
                   1351:
                   1352: /*
1.26      jfb      1353:  * rcs_kwexp_set()
                   1354:  *
                   1355:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                   1356:  */
1.162     xsa      1357: void
1.26      jfb      1358: rcs_kwexp_set(RCSFILE *file, int mode)
                   1359: {
                   1360:        int i;
                   1361:        char *tmp, buf[8] = "";
                   1362:
                   1363:        if (RCS_KWEXP_INVAL(mode))
1.162     xsa      1364:                return;
1.26      jfb      1365:
                   1366:        i = 0;
                   1367:        if (mode == RCS_KWEXP_NONE)
                   1368:                buf[0] = 'b';
                   1369:        else if (mode == RCS_KWEXP_OLD)
                   1370:                buf[0] = 'o';
                   1371:        else {
                   1372:                if (mode & RCS_KWEXP_NAME)
                   1373:                        buf[i++] = 'k';
                   1374:                if (mode & RCS_KWEXP_VAL)
                   1375:                        buf[i++] = 'v';
                   1376:                if (mode & RCS_KWEXP_LKR)
                   1377:                        buf[i++] = 'l';
                   1378:        }
                   1379:
1.110     joris    1380:        tmp = xstrdup(buf);
1.313     nicm     1381:        free(file->rf_expand);
1.26      jfb      1382:        file->rf_expand = tmp;
1.64      niallo   1383:        /* not synced anymore */
                   1384:        file->rf_flags &= ~RCS_SYNCED;
1.26      jfb      1385: }
                   1386:
                   1387: /*
                   1388:  * rcs_kwexp_get()
                   1389:  *
                   1390:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                   1391:  */
                   1392: int
                   1393: rcs_kwexp_get(RCSFILE *file)
                   1394: {
1.306     tobias   1395:        if (file->rf_expand == NULL)
                   1396:                return (RCS_KWEXP_DEFAULT);
                   1397:
                   1398:        return (rcs_kflag_get(file->rf_expand));
1.26      jfb      1399: }
                   1400:
                   1401: /*
1.20      jfb      1402:  * rcs_kflag_get()
                   1403:  *
                   1404:  * Get the keyword expansion mode from a set of character flags given in
                   1405:  * <flags> and return the appropriate flag mask.  In case of an error, the
                   1406:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                   1407:  */
                   1408: int
                   1409: rcs_kflag_get(const char *flags)
                   1410: {
                   1411:        int fl;
                   1412:        size_t len;
                   1413:        const char *fp;
1.253     tobias   1414:
1.306     tobias   1415:        if (flags == NULL || !(len = strlen(flags)))
                   1416:                return (RCS_KWEXP_ERR);
1.20      jfb      1417:
                   1418:        fl = 0;
                   1419:        for (fp = flags; *fp != '\0'; fp++) {
                   1420:                if (*fp == 'k')
                   1421:                        fl |= RCS_KWEXP_NAME;
                   1422:                else if (*fp == 'v')
                   1423:                        fl |= RCS_KWEXP_VAL;
                   1424:                else if (*fp == 'l')
                   1425:                        fl |= RCS_KWEXP_LKR;
                   1426:                else if (*fp == 'o') {
                   1427:                        if (len != 1)
                   1428:                                fl |= RCS_KWEXP_ERR;
                   1429:                        fl |= RCS_KWEXP_OLD;
                   1430:                } else if (*fp == 'b') {
                   1431:                        if (len != 1)
                   1432:                                fl |= RCS_KWEXP_ERR;
1.205     xsa      1433:                        fl |= RCS_KWEXP_NONE;
1.20      jfb      1434:                } else  /* unknown letter */
                   1435:                        fl |= RCS_KWEXP_ERR;
                   1436:        }
                   1437:
                   1438:        return (fl);
1.1       jfb      1439: }
                   1440:
                   1441: /*
                   1442:  * rcs_freedelta()
                   1443:  *
                   1444:  * Free the contents of a delta structure.
                   1445:  */
1.18      jfb      1446: static void
1.1       jfb      1447: rcs_freedelta(struct rcs_delta *rdp)
                   1448: {
1.12      jfb      1449:        struct rcs_branch *rb;
1.1       jfb      1450:
1.315     fcambus  1451:        free(rdp->rd_num);
                   1452:        free(rdp->rd_next);
1.313     nicm     1453:        free(rdp->rd_author);
                   1454:        free(rdp->rd_locker);
                   1455:        free(rdp->rd_state);
                   1456:        free(rdp->rd_log);
                   1457:        free(rdp->rd_text);
1.12      jfb      1458:
                   1459:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   1460:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
1.315     fcambus  1461:                free(rb->rb_num);
1.313     nicm     1462:                free(rb);
1.1       jfb      1463:        }
                   1464:
1.313     nicm     1465:        free(rdp);
1.1       jfb      1466: }
                   1467:
                   1468: /*
1.42      jfb      1469:  * rcs_strprint()
                   1470:  *
                   1471:  * Output an RCS string <str> of size <slen> to the stream <stream>.  Any
                   1472:  * '@' characters are escaped.  Otherwise, the string can contain arbitrary
                   1473:  * binary data.
                   1474:  */
1.155     ray      1475: static void
1.42      jfb      1476: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
                   1477: {
                   1478:        const u_char *ap, *ep, *sp;
1.52      jfb      1479:
                   1480:        if (slen == 0)
1.155     ray      1481:                return;
1.42      jfb      1482:
                   1483:        ep = str + slen - 1;
                   1484:
                   1485:        for (sp = str; sp <= ep;)  {
                   1486:                ap = memchr(sp, '@', ep - sp);
                   1487:                if (ap == NULL)
                   1488:                        ap = ep;
1.155     ray      1489:                (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
1.42      jfb      1490:
                   1491:                if (*ap == '@')
                   1492:                        putc('@', stream);
                   1493:                sp = ap + 1;
1.63      joris    1494:        }
                   1495: }
                   1496:
                   1497: /*
1.81      niallo   1498:  * rcs_deltatext_set()
                   1499:  *
                   1500:  * Set deltatext for <rev> in RCS file <rfp> to <dtext>
1.96      xsa      1501:  * Returns -1 on error, 0 on success.
1.81      niallo   1502:  */
                   1503: int
1.194     joris    1504: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
1.81      niallo   1505: {
                   1506:        size_t len;
1.194     joris    1507:        u_char *dtext;
1.81      niallo   1508:        struct rcs_delta *rdp;
1.117     niallo   1509:
                   1510:        /* Write operations require full parsing */
1.303     tobias   1511:        if (rcsparse_deltatexts(rfp, NULL))
                   1512:                return (-1);
1.81      niallo   1513:
                   1514:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1515:                return (-1);
                   1516:
1.313     nicm     1517:        free(rdp->rd_text);
1.81      niallo   1518:
1.297     ray      1519:        len = buf_len(bp);
                   1520:        dtext = buf_release(bp);
1.194     joris    1521:        bp = NULL;
                   1522:
1.103     joris    1523:        if (len != 0) {
1.194     joris    1524:                rdp->rd_text = xmalloc(len);
1.155     ray      1525:                rdp->rd_tlen = len;
1.194     joris    1526:                (void)memcpy(rdp->rd_text, dtext, len);
1.103     joris    1527:        } else {
                   1528:                rdp->rd_text = NULL;
                   1529:                rdp->rd_tlen = 0;
                   1530:        }
1.194     joris    1531:
1.313     nicm     1532:        free(dtext);
1.86      joris    1533:        return (0);
                   1534: }
                   1535:
                   1536: /*
                   1537:  * rcs_rev_setlog()
                   1538:  *
1.282     tobias   1539:  * Sets the log message of revision <rev> to <logtext>.
1.86      joris    1540:  */
                   1541: int
                   1542: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
                   1543: {
                   1544:        struct rcs_delta *rdp;
                   1545:
                   1546:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1547:                return (-1);
                   1548:
1.313     nicm     1549:        free(rdp->rd_log);
1.86      joris    1550:
1.110     joris    1551:        rdp->rd_log = xstrdup(logtext);
1.86      joris    1552:        rfp->rf_flags &= ~RCS_SYNCED;
1.95      niallo   1553:        return (0);
                   1554: }
1.97      niallo   1555: /*
                   1556:  * rcs_rev_getdate()
                   1557:  *
                   1558:  * Get the date corresponding to a given revision.
                   1559:  * Returns the date on success, -1 on failure.
                   1560:  */
                   1561: time_t
                   1562: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
                   1563: {
                   1564:        struct rcs_delta *rdp;
                   1565:
                   1566:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1567:                return (-1);
                   1568:
1.204     joris    1569:        return (timegm(&rdp->rd_date));
1.97      niallo   1570: }
1.95      niallo   1571:
                   1572: /*
                   1573:  * rcs_state_set()
                   1574:  *
                   1575:  * Sets the state of revision <rev> to <state>
                   1576:  * NOTE: default state is 'Exp'. States may not contain spaces.
                   1577:  *
                   1578:  * Returns -1 on failure, 0 on success.
                   1579:  */
                   1580: int
                   1581: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
                   1582: {
                   1583:        struct rcs_delta *rdp;
                   1584:
                   1585:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1586:                return (-1);
                   1587:
1.313     nicm     1588:        free(rdp->rd_state);
1.95      niallo   1589:
1.110     joris    1590:        rdp->rd_state = xstrdup(state);
1.95      niallo   1591:
                   1592:        rfp->rf_flags &= ~RCS_SYNCED;
                   1593:
                   1594:        return (0);
                   1595: }
                   1596:
                   1597: /*
                   1598:  * rcs_state_check()
                   1599:  *
                   1600:  * Check if string <state> is valid.
                   1601:  *
1.96      xsa      1602:  * Returns 0 if the string is valid, -1 otherwise.
1.95      niallo   1603:  */
                   1604: int
                   1605: rcs_state_check(const char *state)
                   1606: {
1.279     tobias   1607:        if (strcmp(state, RCS_STATE_DEAD) && strcmp(state, RCS_STATE_EXP))
1.95      niallo   1608:                return (-1);
                   1609:
1.18      jfb      1610:        return (0);
1.1       jfb      1611: }
1.97      niallo   1612:
                   1613: /*
                   1614:  * rcs_state_get()
                   1615:  *
                   1616:  * Get the state for a given revision of a specified RCSFILE.
                   1617:  *
                   1618:  * Returns NULL on failure.
                   1619:  */
                   1620: const char *
                   1621: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
                   1622: {
                   1623:        struct rcs_delta *rdp;
                   1624:
                   1625:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   1626:                return (NULL);
                   1627:
                   1628:        return (rdp->rd_state);
                   1629: }
                   1630:
1.246     tobias   1631: /* rcs_get_revision() */
                   1632: static RCSNUM *
                   1633: rcs_get_revision(const char *revstr, RCSFILE *rfp)
1.176     joris    1634: {
1.213     niallo   1635:        RCSNUM *rev, *brev, *frev;
1.178     joris    1636:        struct rcs_branch *brp;
1.183     joris    1637:        struct rcs_delta *rdp;
1.213     niallo   1638:        size_t i;
1.183     joris    1639:
                   1640:        rdp = NULL;
1.215     joris    1641:
                   1642:        if (!strcmp(revstr, RCS_HEAD_BRANCH)) {
1.261     tobias   1643:                if (rfp->rf_head == NULL)
1.280     tobias   1644:                        return (NULL);
1.261     tobias   1645:
1.215     joris    1646:                frev = rcsnum_alloc();
                   1647:                rcsnum_cpy(rfp->rf_head, frev, 0);
                   1648:                return (frev);
                   1649:        }
1.176     joris    1650:
1.213     niallo   1651:        /* Possibly we could be passed a version number */
1.223     tobias   1652:        if ((rev = rcsnum_parse(revstr)) != NULL) {
                   1653:                /* Do not return if it is not in RCS file */
1.280     tobias   1654:                if ((rdp = rcs_findrev(rfp, rev)) != NULL)
                   1655:                        return (rev);
1.223     tobias   1656:        } else {
                   1657:                /* More likely we will be passed a symbol */
                   1658:                rev = rcs_sym_getrev(rfp, revstr);
                   1659:        }
1.222     joris    1660:
1.213     niallo   1661:        if (rev == NULL)
1.245     tobias   1662:                return (NULL);
1.183     joris    1663:
1.220     joris    1664:        /*
                   1665:         * If it was not a branch, thats ok the symbolic
                   1666:         * name refered to a revision, so return the resolved
1.265     joris    1667:         * revision for the given name. */
1.224     tobias   1668:        if (!RCSNUM_ISBRANCH(rev)) {
1.265     joris    1669:                /* Sanity check: The first two elements of any
                   1670:                 * revision (be it on a branch or on trunk) cannot
                   1671:                 * be greater than HEAD.
                   1672:                 *
                   1673:                 * XXX: To avoid comparing to uninitialized memory,
                   1674:                 * the minimum of both revision lengths is taken
                   1675:                 * instead of just 2.
                   1676:                 */
1.280     tobias   1677:                if (rfp->rf_head == NULL || rcsnum_cmp(rev, rfp->rf_head,
1.312     deraadt  1678:                    MINIMUM(rfp->rf_head->rn_len, rev->rn_len)) < 0) {
1.315     fcambus  1679:                        free(rev);
1.280     tobias   1680:                        return (NULL);
1.224     tobias   1681:                }
1.220     joris    1682:                return (rev);
1.224     tobias   1683:        }
1.183     joris    1684:
1.213     niallo   1685:        brev = rcsnum_alloc();
                   1686:        rcsnum_cpy(rev, brev, rev->rn_len - 1);
1.178     joris    1687:
1.180     joris    1688:        if ((rdp = rcs_findrev(rfp, brev)) == NULL)
1.246     tobias   1689:                fatal("rcs_get_revision: tag `%s' does not exist", revstr);
1.315     fcambus  1690:        free(brev);
1.178     joris    1691:
1.213     niallo   1692:        TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                   1693:                for (i = 0; i < rev->rn_len; i++)
                   1694:                        if (brp->rb_num->rn_id[i] != rev->rn_id[i])
                   1695:                                break;
                   1696:                if (i != rev->rn_len)
                   1697:                        continue;
                   1698:                break;
                   1699:        }
1.178     joris    1700:
1.315     fcambus  1701:        free(rev);
1.213     niallo   1702:        frev = rcsnum_alloc();
                   1703:        if (brp == NULL) {
                   1704:                rcsnum_cpy(rdp->rd_num, frev, 0);
                   1705:                return (frev);
                   1706:        } else {
                   1707:                /* Fetch the delta with the correct branch num */
1.180     joris    1708:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.246     tobias   1709:                        fatal("rcs_get_revision: could not fetch branch "
                   1710:                            "delta");
1.213     niallo   1711:                rcsnum_cpy(rdp->rd_num, frev, 0);
                   1712:                return (frev);
1.176     joris    1713:        }
1.196     niallo   1714: }
                   1715:
                   1716: /*
                   1717:  * rcs_rev_getlines()
                   1718:  *
1.238     tobias   1719:  * Get the entire contents of revision <frev> from the RCSFILE <rfp> and
1.297     ray      1720:  * return it as a pointer to a struct rcs_lines.
1.196     niallo   1721:  */
1.297     ray      1722: struct rcs_lines *
                   1723: rcs_rev_getlines(RCSFILE *rfp, RCSNUM *frev, struct rcs_line ***alines)
1.196     niallo   1724: {
1.209     otto     1725:        size_t plen;
1.218     tobias   1726:        int annotate, done, i, nextroot;
1.196     niallo   1727:        RCSNUM *tnum, *bnum;
                   1728:        struct rcs_branch *brp;
1.218     tobias   1729:        struct rcs_delta *hrdp, *prdp, *rdp, *trdp;
1.196     niallo   1730:        u_char *patch;
1.297     ray      1731:        struct rcs_line *line, *nline;
                   1732:        struct rcs_lines *dlines, *plines;
1.196     niallo   1733:
1.309     nicm     1734:        hrdp = prdp = rdp = trdp = NULL;
                   1735:
1.275     tobias   1736:        if (rfp->rf_head == NULL ||
                   1737:            (hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
1.197     joris    1738:                fatal("rcs_rev_getlines: no HEAD revision");
1.196     niallo   1739:
                   1740:        tnum = frev;
1.303     tobias   1741:        if (rcsparse_deltatexts(rfp, hrdp->rd_num))
                   1742:                fatal("rcs_rev_getlines: rcsparse_deltatexts");
1.196     niallo   1743:
                   1744:        /* revision on branch, get the branch root */
                   1745:        nextroot = 2;
1.278     joris    1746:        bnum = rcsnum_alloc();
                   1747:        if (RCSNUM_ISBRANCHREV(tnum))
1.196     niallo   1748:                rcsnum_cpy(tnum, bnum, nextroot);
1.278     joris    1749:        else
                   1750:                rcsnum_cpy(tnum, bnum, tnum->rn_len);
1.196     niallo   1751:
1.218     tobias   1752:        if (alines != NULL) {
                   1753:                /* start with annotate first at requested revision */
                   1754:                annotate = ANNOTATE_LATER;
                   1755:                *alines = NULL;
                   1756:        } else
                   1757:                annotate = ANNOTATE_NEVER;
                   1758:
1.196     niallo   1759:        dlines = cvs_splitlines(hrdp->rd_text, hrdp->rd_tlen);
                   1760:
                   1761:        done = 0;
                   1762:
                   1763:        rdp = hrdp;
1.218     tobias   1764:        if (!rcsnum_differ(rdp->rd_num, bnum)) {
                   1765:                if (annotate == ANNOTATE_LATER) {
                   1766:                        /* found requested revision for annotate */
                   1767:                        i = 0;
                   1768:                        TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
                   1769:                                line->l_lineno_orig = line->l_lineno;
                   1770:                                i++;
                   1771:                        }
                   1772:
1.297     ray      1773:                        *alines = xcalloc(i + 1, sizeof(struct rcs_line *));
1.218     tobias   1774:                        (*alines)[i] = NULL;
                   1775:                        annotate = ANNOTATE_NOW;
                   1776:
                   1777:                        /* annotate down to 1.1 from where we are */
1.315     fcambus  1778:                        free(bnum);
1.218     tobias   1779:                        bnum = rcsnum_parse("1.1");
                   1780:                        if (!rcsnum_differ(rdp->rd_num, bnum)) {
                   1781:                                goto next;
                   1782:                        }
                   1783:                } else
                   1784:                        goto next;
                   1785:        }
1.196     niallo   1786:
1.218     tobias   1787:        prdp = hrdp;
1.196     niallo   1788:        if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
                   1789:                goto done;
                   1790:
                   1791: again:
1.314     millert  1792:        while (rdp != NULL) {
1.196     niallo   1793:                if (rdp->rd_next->rn_len != 0) {
                   1794:                        trdp = rcs_findrev(rfp, rdp->rd_next);
                   1795:                        if (trdp == NULL)
                   1796:                                fatal("failed to grab next revision");
                   1797:                }
                   1798:
                   1799:                if (rdp->rd_tlen == 0) {
1.303     tobias   1800:                        if (rcsparse_deltatexts(rfp, rdp->rd_num))
                   1801:                                fatal("rcs_rev_getlines: rcsparse_deltatexts");
1.196     niallo   1802:                        if (rdp->rd_tlen == 0) {
                   1803:                                if (!rcsnum_differ(rdp->rd_num, bnum))
                   1804:                                        break;
                   1805:                                rdp = trdp;
                   1806:                                continue;
                   1807:                        }
                   1808:                }
                   1809:
                   1810:                plen = rdp->rd_tlen;
                   1811:                patch = rdp->rd_text;
                   1812:                plines = cvs_splitlines(patch, plen);
1.218     tobias   1813:                if (annotate == ANNOTATE_NOW)
                   1814:                        rcs_patch_lines(dlines, plines, *alines, prdp);
                   1815:                else
                   1816:                        rcs_patch_lines(dlines, plines, NULL, NULL);
1.196     niallo   1817:                cvs_freelines(plines);
                   1818:
1.218     tobias   1819:                if (!rcsnum_differ(rdp->rd_num, bnum)) {
                   1820:                        if (annotate != ANNOTATE_LATER)
                   1821:                                break;
                   1822:
                   1823:                        /* found requested revision for annotate */
                   1824:                        i = 0;
                   1825:                        TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
                   1826:                                line->l_lineno_orig = line->l_lineno;
                   1827:                                i++;
                   1828:                        }
                   1829:
1.297     ray      1830:                        *alines = xcalloc(i + 1, sizeof(struct rcs_line *));
1.218     tobias   1831:                        (*alines)[i] = NULL;
                   1832:                        annotate = ANNOTATE_NOW;
                   1833:
                   1834:                        /* annotate down to 1.1 from where we are */
1.315     fcambus  1835:                        free(bnum);
1.218     tobias   1836:                        bnum = rcsnum_parse("1.1");
                   1837:
                   1838:                        if (!rcsnum_differ(rdp->rd_num, bnum))
                   1839:                                break;
                   1840:                }
1.196     niallo   1841:
1.218     tobias   1842:                prdp = rdp;
1.196     niallo   1843:                rdp = trdp;
                   1844:        }
                   1845:
                   1846: next:
1.314     millert  1847:        if (rdp == NULL || !rcsnum_differ(rdp->rd_num, frev))
1.196     niallo   1848:                done = 1;
                   1849:
                   1850:        if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
                   1851:                nextroot += 2;
                   1852:                rcsnum_cpy(frev, bnum, nextroot);
                   1853:
                   1854:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
1.213     niallo   1855:                        for (i = 0; i < nextroot - 1; i++)
                   1856:                                if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
1.196     niallo   1857:                                        break;
1.213     niallo   1858:                        if (i == nextroot - 1)
                   1859:                                break;
1.196     niallo   1860:                }
                   1861:
1.218     tobias   1862:                if (brp == NULL) {
                   1863:                        if (annotate != ANNOTATE_NEVER) {
1.313     nicm     1864:                                free(*alines);
1.218     tobias   1865:                                *alines = NULL;
                   1866:                                cvs_freelines(dlines);
1.315     fcambus  1867:                                free(bnum);
1.218     tobias   1868:                                return (NULL);
                   1869:                        }
1.196     niallo   1870:                        fatal("expected branch not found on branch list");
1.218     tobias   1871:                }
1.196     niallo   1872:
                   1873:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
1.197     joris    1874:                        fatal("rcs_rev_getlines: failed to get delta for target rev");
1.196     niallo   1875:
                   1876:                goto again;
                   1877:        }
                   1878: done:
1.225     tobias   1879:        /* put remaining lines into annotate buffer */
1.218     tobias   1880:        if (annotate == ANNOTATE_NOW) {
                   1881:                for (line = TAILQ_FIRST(&(dlines->l_lines));
                   1882:                    line != NULL; line = nline) {
                   1883:                        nline = TAILQ_NEXT(line, l_list);
                   1884:                        TAILQ_REMOVE(&(dlines->l_lines), line, l_list);
                   1885:                        if (line->l_line == NULL) {
1.313     nicm     1886:                                free(line);
1.218     tobias   1887:                                continue;
                   1888:                        }
                   1889:
                   1890:                        line->l_delta = rdp;
                   1891:                        (*alines)[line->l_lineno_orig - 1] = line;
                   1892:                }
                   1893:
                   1894:                cvs_freelines(dlines);
                   1895:                dlines = NULL;
                   1896:        }
                   1897:
1.196     niallo   1898:        if (bnum != tnum)
1.315     fcambus  1899:                free(bnum);
1.196     niallo   1900:
                   1901:        return (dlines);
1.225     tobias   1902: }
                   1903:
                   1904: void
1.297     ray      1905: rcs_annotate_getlines(RCSFILE *rfp, RCSNUM *frev, struct rcs_line ***alines)
1.225     tobias   1906: {
                   1907:        size_t plen;
                   1908:        int i, nextroot;
                   1909:        RCSNUM *bnum;
                   1910:        struct rcs_branch *brp;
                   1911:        struct rcs_delta *rdp, *trdp;
                   1912:        u_char *patch;
1.297     ray      1913:        struct rcs_line *line;
                   1914:        struct rcs_lines *dlines, *plines;
1.225     tobias   1915:
1.309     nicm     1916:        rdp = trdp = NULL;
                   1917:
1.225     tobias   1918:        if (!RCSNUM_ISBRANCHREV(frev))
                   1919:                fatal("rcs_annotate_getlines: branch revision expected");
                   1920:
                   1921:        /* revision on branch, get the branch root */
                   1922:        nextroot = 2;
                   1923:        bnum = rcsnum_alloc();
                   1924:        rcsnum_cpy(frev, bnum, nextroot);
                   1925:
                   1926:        /*
                   1927:         * Going from HEAD to 1.1 enables the use of an array, which is
                   1928:         * much faster. Unfortunately this is not possible with branch
                   1929:         * revisions, so copy over our alines (array) into dlines (tailq).
                   1930:         */
                   1931:        dlines = xcalloc(1, sizeof(*dlines));
                   1932:        TAILQ_INIT(&(dlines->l_lines));
                   1933:        line = xcalloc(1, sizeof(*line));
                   1934:        TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
                   1935:
                   1936:        for (i = 0; (*alines)[i] != NULL; i++) {
                   1937:                line = (*alines)[i];
                   1938:                line->l_lineno = i + 1;
                   1939:                TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
                   1940:        }
                   1941:
                   1942:        rdp = rcs_findrev(rfp, bnum);
                   1943:        if (rdp == NULL)
                   1944:                fatal("failed to grab branch root revision");
                   1945:
                   1946:        do {
                   1947:                nextroot += 2;
                   1948:                rcsnum_cpy(frev, bnum, nextroot);
                   1949:
                   1950:                TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
                   1951:                        for (i = 0; i < nextroot - 1; i++)
                   1952:                                if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
                   1953:                                        break;
                   1954:                        if (i == nextroot - 1)
                   1955:                                break;
                   1956:                }
                   1957:
                   1958:                if (brp == NULL)
                   1959:                        fatal("expected branch not found on branch list");
                   1960:
                   1961:                if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
                   1962:                        fatal("failed to get delta for target rev");
                   1963:
                   1964:                for (;;) {
                   1965:                        if (rdp->rd_next->rn_len != 0) {
                   1966:                                trdp = rcs_findrev(rfp, rdp->rd_next);
                   1967:                                if (trdp == NULL)
                   1968:                                        fatal("failed to grab next revision");
                   1969:                        }
                   1970:
                   1971:                        if (rdp->rd_tlen == 0) {
1.303     tobias   1972:                                if (rcsparse_deltatexts(rfp, rdp->rd_num))
                   1973:                                        fatal("rcs_annotate_getlines: "
                   1974:                                            "rcsparse_deltatexts");
1.225     tobias   1975:                                if (rdp->rd_tlen == 0) {
                   1976:                                        if (!rcsnum_differ(rdp->rd_num, bnum))
                   1977:                                                break;
                   1978:                                        rdp = trdp;
                   1979:                                        continue;
                   1980:                                }
                   1981:                        }
                   1982:
                   1983:                        plen = rdp->rd_tlen;
                   1984:                        patch = rdp->rd_text;
                   1985:                        plines = cvs_splitlines(patch, plen);
                   1986:                        rcs_patch_lines(dlines, plines, NULL, rdp);
                   1987:                        cvs_freelines(plines);
                   1988:
                   1989:                        if (!rcsnum_differ(rdp->rd_num, bnum))
                   1990:                                break;
                   1991:
                   1992:                        rdp = trdp;
                   1993:                }
                   1994:        } while (rcsnum_differ(rdp->rd_num, frev));
                   1995:
                   1996:        if (bnum != frev)
1.315     fcambus  1997:                free(bnum);
1.225     tobias   1998:
                   1999:        /*
                   2000:         * All lines have been parsed, now they must be copied over
                   2001:         * into alines (array) again.
                   2002:         */
1.313     nicm     2003:        free(*alines);
1.225     tobias   2004:
                   2005:        i = 0;
                   2006:        TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
                   2007:                if (line->l_line != NULL)
                   2008:                        i++;
                   2009:        }
1.297     ray      2010:        *alines = xcalloc(i + 1, sizeof(struct rcs_line *));
1.225     tobias   2011:        (*alines)[i] = NULL;
                   2012:
                   2013:        i = 0;
                   2014:        TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
                   2015:                if (line->l_line != NULL)
                   2016:                        (*alines)[i++] = line;
                   2017:        }
1.196     niallo   2018: }
                   2019:
                   2020: /*
                   2021:  * rcs_rev_getbuf()
                   2022:  *
                   2023:  * XXX: This is really really slow and should be avoided if at all possible!
                   2024:  *
                   2025:  * Get the entire contents of revision <rev> from the RCSFILE <rfp> and
                   2026:  * return it as a BUF pointer.
                   2027:  */
                   2028: BUF *
1.200     joris    2029: rcs_rev_getbuf(RCSFILE *rfp, RCSNUM *rev, int mode)
1.196     niallo   2030: {
1.200     joris    2031:        int expmode, expand;
                   2032:        struct rcs_delta *rdp;
1.297     ray      2033:        struct rcs_lines *lines;
                   2034:        struct rcs_line *lp, *nlp;
1.196     niallo   2035:        BUF *bp;
                   2036:
1.309     nicm     2037:        rdp = NULL;
                   2038:        expmode = RCS_KWEXP_NONE;
1.200     joris    2039:        expand = 0;
1.218     tobias   2040:        lines = rcs_rev_getlines(rfp, rev, NULL);
1.297     ray      2041:        bp = buf_alloc(1024 * 16);
1.200     joris    2042:
                   2043:        if (!(mode & RCS_KWEXP_NONE)) {
1.306     tobias   2044:                expmode = rcs_kwexp_get(rfp);
1.200     joris    2045:
                   2046:                if (!(expmode & RCS_KWEXP_NONE)) {
1.314     millert  2047:                        if ((rdp = rcs_findrev(rfp, rev)) == NULL) {
                   2048:                                char version[RCSNUM_MAXSTR];
                   2049:
                   2050:                                rcsnum_tostr(rev, version, sizeof(version));
                   2051:                                fatal("could not find desired version %s in %s",
                   2052:                                    version, rfp->rf_path);
                   2053:                        }
                   2054:
1.200     joris    2055:                        expand = 1;
                   2056:                }
                   2057:        }
                   2058:
1.255     deraadt  2059:        for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
1.233     tobias   2060:                nlp = TAILQ_NEXT(lp, l_list);
                   2061:
                   2062:                if (lp->l_line == NULL) {
                   2063:                        lp = nlp;
1.196     niallo   2064:                        continue;
1.233     tobias   2065:                }
1.200     joris    2066:
                   2067:                if (expand)
1.233     tobias   2068:                        rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.200     joris    2069:
1.233     tobias   2070:                do {
1.297     ray      2071:                        buf_append(bp, lp->l_line, lp->l_len);
1.233     tobias   2072:                } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196     niallo   2073:        }
                   2074:
                   2075:        cvs_freelines(lines);
                   2076:
                   2077:        return (bp);
                   2078: }
                   2079:
                   2080: /*
                   2081:  * rcs_rev_write_fd()
                   2082:  *
                   2083:  * Write the entire contents of revision <frev> from the rcsfile <rfp> to
                   2084:  * file descriptor <fd>.
                   2085:  */
                   2086: void
1.273     joris    2087: rcs_rev_write_fd(RCSFILE *rfp, RCSNUM *rev, int _fd, int mode)
1.196     niallo   2088: {
1.273     joris    2089:        int fd;
                   2090:        FILE *fp;
                   2091:        size_t ret;
1.200     joris    2092:        int expmode, expand;
1.196     niallo   2093:        struct rcs_delta *rdp;
1.297     ray      2094:        struct rcs_lines *lines;
                   2095:        struct rcs_line *lp, *nlp;
1.217     joris    2096:        extern int print_stdout;
1.196     niallo   2097:
1.309     nicm     2098:        rdp = NULL;
                   2099:        expmode = RCS_KWEXP_NONE;
1.200     joris    2100:        expand = 0;
1.218     tobias   2101:        lines = rcs_rev_getlines(rfp, rev, NULL);
1.200     joris    2102:
1.196     niallo   2103:        if (!(mode & RCS_KWEXP_NONE)) {
1.306     tobias   2104:                expmode = rcs_kwexp_get(rfp);
1.196     niallo   2105:
                   2106:                if (!(expmode & RCS_KWEXP_NONE)) {
                   2107:                        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
                   2108:                                fatal("could not fetch revision");
1.200     joris    2109:                        expand = 1;
1.196     niallo   2110:                }
                   2111:        }
1.200     joris    2112:
1.273     joris    2113:        fd = dup(_fd);
                   2114:        if (fd == -1)
                   2115:                fatal("rcs_rev_write_fd: dup: %s", strerror(errno));
                   2116:
                   2117:        if ((fp = fdopen(fd, "w")) == NULL)
                   2118:                fatal("rcs_rev_write_fd: fdopen: %s", strerror(errno));
                   2119:
1.255     deraadt  2120:        for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
1.233     tobias   2121:                nlp = TAILQ_NEXT(lp, l_list);
                   2122:
                   2123:                if (lp->l_line == NULL) {
                   2124:                        lp = nlp;
1.196     niallo   2125:                        continue;
1.233     tobias   2126:                }
1.200     joris    2127:
                   2128:                if (expand)
1.233     tobias   2129:                        rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
1.217     joris    2130:
1.233     tobias   2131:                do {
                   2132:                        /*
                   2133:                         * Solely for the checkout and update -p options.
                   2134:                         */
                   2135:                        if (cvs_server_active == 1 &&
                   2136:                            (cvs_cmdop == CVS_OP_CHECKOUT ||
                   2137:                            cvs_cmdop == CVS_OP_UPDATE) && print_stdout == 1) {
1.273     joris    2138:                                ret = fwrite("M ", 1, 2, fp);
                   2139:                                if (ret != 2)
1.233     tobias   2140:                                        fatal("rcs_rev_write_fd: %s",
                   2141:                                            strerror(errno));
                   2142:                        }
                   2143:
1.273     joris    2144:                        ret = fwrite(lp->l_line, 1, lp->l_len, fp);
                   2145:                        if (ret != lp->l_len)
1.217     joris    2146:                                fatal("rcs_rev_write_fd: %s", strerror(errno));
1.233     tobias   2147:                } while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
1.196     niallo   2148:        }
                   2149:
                   2150:        cvs_freelines(lines);
1.273     joris    2151:        (void)fclose(fp);
1.196     niallo   2152: }
                   2153:
                   2154: /*
                   2155:  * rcs_rev_write_stmp()
                   2156:  *
                   2157:  * Write the contents of the rev <rev> to a temporary file whose path is
                   2158:  * specified using <template> (see mkstemp(3)). NB. This function will modify
                   2159:  * <template>, as per mkstemp.
                   2160:  */
1.251     joris    2161: int
1.196     niallo   2162: rcs_rev_write_stmp(RCSFILE *rfp,  RCSNUM *rev, char *template, int mode)
                   2163: {
                   2164:        int fd;
                   2165:
                   2166:        if ((fd = mkstemp(template)) == -1)
                   2167:                fatal("mkstemp: `%s': %s", template, strerror(errno));
                   2168:
1.296     ray      2169:        worklist_add(template, &temp_files);
1.196     niallo   2170:        rcs_rev_write_fd(rfp, rev, fd, mode);
                   2171:
1.257     joris    2172:        if (lseek(fd, 0, SEEK_SET) < 0)
1.251     joris    2173:                fatal("rcs_rev_write_stmp: lseek: %s", strerror(errno));
                   2174:
                   2175:        return (fd);
1.196     niallo   2176: }
                   2177:
                   2178: static void
1.297     ray      2179: rcs_kwexp_line(char *rcsfile, struct rcs_delta *rdp, struct rcs_lines *lines,
                   2180:     struct rcs_line *line, int mode)
1.196     niallo   2181: {
1.231     tobias   2182:        BUF *tmpbuf;
1.196     niallo   2183:        int kwtype;
                   2184:        u_int j, found;
1.209     otto     2185:        const u_char *c, *start, *fin, *end;
                   2186:        char *kwstr;
1.196     niallo   2187:        char expbuf[256], buf[256];
1.231     tobias   2188:        size_t clen, kwlen, len, tlen;
1.196     niallo   2189:
                   2190:        kwtype = 0;
                   2191:        kwstr = NULL;
1.230     tobias   2192:
                   2193:        if (mode & RCS_KWEXP_OLD)
                   2194:                return;
1.196     niallo   2195:
                   2196:        len = line->l_len;
                   2197:        if (len == 0)
                   2198:                return;
                   2199:
                   2200:        c = line->l_line;
                   2201:        found = 0;
                   2202:        /* Final character in buffer. */
                   2203:        fin = c + len - 1;
                   2204:
                   2205:        /*
                   2206:         * Keyword formats:
                   2207:         * $Keyword$
                   2208:         * $Keyword: value$
                   2209:         */
                   2210:        for (; c < fin; c++) {
1.231     tobias   2211:                if (*c != '$')
                   2212:                        continue;
1.196     niallo   2213:
1.231     tobias   2214:                /* remember start of this possible keyword */
                   2215:                start = c;
1.196     niallo   2216:
1.231     tobias   2217:                /* first following character has to be alphanumeric */
                   2218:                c++;
                   2219:                if (!isalpha(*c)) {
                   2220:                        c = start;
                   2221:                        continue;
                   2222:                }
1.196     niallo   2223:
1.231     tobias   2224:                /* Number of characters between c and fin, inclusive. */
                   2225:                clen = fin - c + 1;
1.196     niallo   2226:
1.231     tobias   2227:                /* look for any matching keywords */
                   2228:                found = 0;
                   2229:                for (j = 0; j < RCS_NKWORDS; j++) {
                   2230:                        kwlen = strlen(rcs_expkw[j].kw_str);
1.196     niallo   2231:                        /*
1.231     tobias   2232:                         * kwlen must be less than clen since clen
                   2233:                         * includes either a terminating `$' or a `:'.
1.196     niallo   2234:                         */
1.231     tobias   2235:                        if (kwlen < clen &&
                   2236:                            memcmp(c, rcs_expkw[j].kw_str, kwlen) == 0 &&
                   2237:                            (c[kwlen] == '$' || c[kwlen] == ':')) {
                   2238:                                found = 1;
                   2239:                                kwstr = rcs_expkw[j].kw_str;
                   2240:                                kwtype = rcs_expkw[j].kw_type;
                   2241:                                c += kwlen;
                   2242:                                break;
                   2243:                        }
                   2244:                }
1.196     niallo   2245:
1.231     tobias   2246:                if (found == 0 && cvs_tagname != NULL) {
                   2247:                        kwlen = strlen(cvs_tagname);
                   2248:                        if (kwlen < clen &&
                   2249:                            memcmp(c, cvs_tagname, kwlen) == 0 &&
                   2250:                            (c[kwlen] == '$' || c[kwlen] == ':')) {
                   2251:                                found = 1;
                   2252:                                kwstr = cvs_tagname;
                   2253:                                kwtype = RCS_KW_ID;
                   2254:                                c += kwlen;
1.196     niallo   2255:                        }
1.231     tobias   2256:                }
1.196     niallo   2257:
1.231     tobias   2258:                /* unknown keyword, continue looking */
                   2259:                if (found == 0) {
                   2260:                        c = start;
                   2261:                        continue;
                   2262:                }
1.196     niallo   2263:
1.231     tobias   2264:                /*
                   2265:                 * if the next character was ':' we need to look for
                   2266:                 * an '$' before the end of the line to be sure it is
                   2267:                 * in fact a keyword.
                   2268:                 */
                   2269:                if (*c == ':') {
                   2270:                        for (; c <= fin; ++c) {
                   2271:                                if (*c == '$' || *c == '\n')
                   2272:                                        break;
1.196     niallo   2273:                        }
                   2274:
1.231     tobias   2275:                        if (*c != '$') {
                   2276:                                c = start;
                   2277:                                continue;
                   2278:                        }
                   2279:                }
                   2280:                end = c + 1;
1.196     niallo   2281:
1.231     tobias   2282:                /* start constructing the expansion */
                   2283:                expbuf[0] = '\0';
1.196     niallo   2284:
1.231     tobias   2285:                if (mode & RCS_KWEXP_NAME) {
                   2286:                        if (strlcat(expbuf, "$", sizeof(expbuf)) >=
                   2287:                            sizeof(expbuf) || strlcat(expbuf, kwstr,
                   2288:                            sizeof(expbuf)) >= sizeof(expbuf))
                   2289:                                fatal("rcs_kwexp_line: truncated");
                   2290:                        if ((mode & RCS_KWEXP_VAL) &&
                   2291:                            strlcat(expbuf, ": ", sizeof(expbuf)) >=
                   2292:                            sizeof(expbuf))
                   2293:                                fatal("rcs_kwexp_line: truncated");
                   2294:                }
1.196     niallo   2295:
1.231     tobias   2296:                /*
                   2297:                 * order matters because of RCS_KW_ID and
                   2298:                 * RCS_KW_HEADER here
                   2299:                 */
                   2300:                if (mode & RCS_KWEXP_VAL) {
                   2301:                        if (kwtype & RCS_KW_RCSFILE) {
                   2302:                                if (!(kwtype & RCS_KW_FULLPATH))
                   2303:                                        (void)strlcat(expbuf, basename(rcsfile),
                   2304:                                            sizeof(expbuf));
                   2305:                                else
                   2306:                                        (void)strlcat(expbuf, rcsfile,
                   2307:                                            sizeof(expbuf));
                   2308:                                if (strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2309:                                    sizeof(expbuf))
                   2310:                                        fatal("rcs_kwexp_line: truncated");
                   2311:                        }
1.211     niallo   2312:
1.231     tobias   2313:                        if (kwtype & RCS_KW_REVISION) {
                   2314:                                rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
                   2315:                                if (strlcat(buf, " ", sizeof(buf)) >=
                   2316:                                    sizeof(buf) || strlcat(expbuf, buf,
                   2317:                                    sizeof(expbuf)) >= sizeof(buf))
                   2318:                                        fatal("rcs_kwexp_line: truncated");
                   2319:                        }
1.196     niallo   2320:
1.231     tobias   2321:                        if (kwtype & RCS_KW_DATE) {
1.283     canacar  2322:                                if (strftime(buf, sizeof(buf),
                   2323:                                    "%Y/%m/%d %H:%M:%S ",
1.231     tobias   2324:                                    &rdp->rd_date) == 0)
                   2325:                                        fatal("rcs_kwexp_line: strftime "
                   2326:                                            "failure");
                   2327:                                if (strlcat(expbuf, buf, sizeof(expbuf)) >=
                   2328:                                    sizeof(expbuf))
                   2329:                                        fatal("rcs_kwexp_line: string "
                   2330:                                            "truncated");
                   2331:                        }
1.196     niallo   2332:
1.231     tobias   2333:                        if (kwtype & RCS_KW_MDOCDATE) {
                   2334:                                /*
                   2335:                                 * Do not prepend ' ' for a single
                   2336:                                 * digit, %e would do so and there is
                   2337:                                 * no better format for strftime().
                   2338:                                 */
1.283     canacar  2339:                                if (strftime(buf, sizeof(buf),
                   2340:                                    (rdp->rd_date.tm_mday < 10) ?
1.285     joris    2341:                                        "%B%e %Y " : "%B %e %Y ",
1.231     tobias   2342:                                    &rdp->rd_date) == 0)
                   2343:                                        fatal("rcs_kwexp_line: strftime "
                   2344:                                            "failure");
                   2345:                                if (strlcat(expbuf, buf, sizeof(expbuf)) >=
                   2346:                                    sizeof(expbuf))
                   2347:                                        fatal("rcs_kwexp_line: string "
                   2348:                                            "truncated");
                   2349:                        }
                   2350:
                   2351:                        if (kwtype & RCS_KW_AUTHOR) {
                   2352:                                if (strlcat(expbuf, rdp->rd_author,
                   2353:                                    sizeof(expbuf)) >= sizeof(expbuf) ||
                   2354:                                    strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2355:                                    sizeof(expbuf))
                   2356:                                        fatal("rcs_kwexp_line: string "
                   2357:                                            "truncated");
                   2358:                        }
                   2359:
                   2360:                        if (kwtype & RCS_KW_STATE) {
                   2361:                                if (strlcat(expbuf, rdp->rd_state,
                   2362:                                    sizeof(expbuf)) >= sizeof(expbuf) ||
                   2363:                                    strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2364:                                    sizeof(expbuf))
                   2365:                                        fatal("rcs_kwexp_line: string "
                   2366:                                            "truncated");
                   2367:                        }
                   2368:
                   2369:                        /* order does not matter anymore below */
1.233     tobias   2370:                        if (kwtype & RCS_KW_LOG) {
                   2371:                                char linebuf[256];
1.297     ray      2372:                                struct rcs_line *cur, *lp;
1.233     tobias   2373:                                char *logp, *l_line, *prefix, *q, *sprefix;
                   2374:                                size_t i;
                   2375:
1.236     tobias   2376:                                /* Log line */
1.233     tobias   2377:                                if (!(kwtype & RCS_KW_FULLPATH))
                   2378:                                        (void)strlcat(expbuf,
                   2379:                                            basename(rcsfile), sizeof(expbuf));
                   2380:                                else
                   2381:                                        (void)strlcat(expbuf, rcsfile,
                   2382:                                            sizeof(expbuf));
                   2383:
1.231     tobias   2384:                                if (strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2385:                                    sizeof(expbuf))
                   2386:                                        fatal("rcs_kwexp_line: string "
                   2387:                                            "truncated");
1.233     tobias   2388:
                   2389:                                cur = line;
                   2390:
                   2391:                                /* copy rdp->rd_log for strsep */
                   2392:                                logp = xstrdup(rdp->rd_log);
                   2393:
                   2394:                                /* copy our prefix for later processing */
                   2395:                                prefix = xmalloc(start - line->l_line + 1);
                   2396:                                memcpy(prefix, line->l_line,
                   2397:                                    start - line->l_line);
                   2398:                                prefix[start - line->l_line] = '\0';
                   2399:
                   2400:                                /* copy also prefix without trailing blanks. */
                   2401:                                sprefix = xstrdup(prefix);
                   2402:                                for (i = strlen(sprefix); i > 0 &&
                   2403:                                    sprefix[i - 1] == ' '; i--)
                   2404:                                        sprefix[i - 1] = '\0';
                   2405:
                   2406:                                /* new line: revision + date + author */
                   2407:                                linebuf[0] = '\0';
                   2408:                                if (strlcat(linebuf, "Revision ",
                   2409:                                    sizeof(linebuf)) >= sizeof(linebuf))
                   2410:                                        fatal("rcs_kwexp_line: truncated");
                   2411:                                rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
                   2412:                                if (strlcat(linebuf, buf, sizeof(linebuf))
                   2413:                                    >= sizeof(buf))
                   2414:                                        fatal("rcs_kwexp_line: truncated");
1.283     canacar  2415:                                if (strftime(buf, sizeof(buf),
                   2416:                                    "  %Y/%m/%d %H:%M:%S  ",
1.233     tobias   2417:                                    &rdp->rd_date) == 0)
                   2418:                                        fatal("rcs_kwexp_line: strftime "
                   2419:                                            "failure");
                   2420:                                if (strlcat(linebuf, buf, sizeof(linebuf))
                   2421:                                    >= sizeof(linebuf))
                   2422:                                        fatal("rcs_kwexp_line: string "
                   2423:                                            "truncated");
                   2424:                                if (strlcat(linebuf, rdp->rd_author,
                   2425:                                    sizeof(linebuf)) >= sizeof(linebuf))
                   2426:                                        fatal("rcs_kwexp_line: string "
                   2427:                                            "truncated");
                   2428:
                   2429:                                lp = xcalloc(1, sizeof(*lp));
1.316     millert  2430:                                xasprintf((char **)&(lp->l_line), "%s%s\n",
1.233     tobias   2431:                                    prefix, linebuf);
                   2432:                                lp->l_len = strlen(lp->l_line);
                   2433:                                TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
                   2434:                                    l_list);
                   2435:                                cur = lp;
                   2436:
                   2437:                                /* Log message */
                   2438:                                q = logp;
                   2439:                                while ((l_line = strsep(&q, "\n")) != NULL &&
                   2440:                                    q != NULL) {
                   2441:                                        lp = xcalloc(1, sizeof(*lp));
                   2442:
                   2443:                                        if (l_line[0] == '\0') {
1.316     millert  2444:                                                xasprintf((char **)&(lp->l_line),
                   2445:                                                    "%s\n", sprefix);
1.233     tobias   2446:                                        } else {
1.316     millert  2447:                                                xasprintf((char **)&(lp->l_line),
1.233     tobias   2448:                                                    "%s%s\n", prefix, l_line);
                   2449:                                        }
                   2450:
                   2451:                                        lp->l_len = strlen(lp->l_line);
                   2452:                                        TAILQ_INSERT_AFTER(&(lines->l_lines),
                   2453:                                            cur, lp, l_list);
                   2454:                                        cur = lp;
                   2455:                                }
1.313     nicm     2456:                                free(logp);
1.233     tobias   2457:
                   2458:                                /*
                   2459:                                 * This is just another hairy mess, but it must
1.237     tobias   2460:                                 * be done: All characters behind Log will be
1.233     tobias   2461:                                 * written in a new line next to log messages.
                   2462:                                 * But that's not enough, we have to strip all
                   2463:                                 * trailing whitespaces of our prefix.
                   2464:                                 */
                   2465:                                lp = xcalloc(1, sizeof(*lp));
1.318   ! millert  2466:                                xasprintf((char **)&lp->l_line, "%s%s",
        !          2467:                                    sprefix, end);
1.233     tobias   2468:                                lp->l_len = strlen(lp->l_line);
                   2469:                                TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
                   2470:                                    l_list);
                   2471:                                cur = lp;
                   2472:
                   2473:                                end = line->l_line + line->l_len - 1;
                   2474:
1.313     nicm     2475:                                free(prefix);
                   2476:                                free(sprefix);
1.233     tobias   2477:
                   2478:                        }
1.231     tobias   2479:
                   2480:                        if (kwtype & RCS_KW_SOURCE) {
                   2481:                                if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >=
                   2482:                                    sizeof(expbuf) || strlcat(expbuf, " ",
                   2483:                                    sizeof(expbuf)) >= sizeof(expbuf))
                   2484:                                        fatal("rcs_kwexp_line: string "
                   2485:                                            "truncated");
1.196     niallo   2486:                        }
                   2487:
1.231     tobias   2488:                        if (kwtype & RCS_KW_NAME)
                   2489:                                if (strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2490:                                    sizeof(expbuf))
                   2491:                                        fatal("rcs_kwexp_line: string "
                   2492:                                            "truncated");
1.196     niallo   2493:
1.231     tobias   2494:                        if (kwtype & RCS_KW_LOCKER)
                   2495:                                if (strlcat(expbuf, " ", sizeof(expbuf)) >=
                   2496:                                    sizeof(expbuf))
                   2497:                                        fatal("rcs_kwexp_line: string "
                   2498:                                            "truncated");
1.196     niallo   2499:                }
1.231     tobias   2500:
                   2501:                /* end the expansion */
                   2502:                if (mode & RCS_KWEXP_NAME)
                   2503:                        if (strlcat(expbuf, "$",
                   2504:                            sizeof(expbuf)) >= sizeof(expbuf))
                   2505:                                fatal("rcs_kwexp_line: truncated");
                   2506:
                   2507:                /* Concatenate everything together. */
1.297     ray      2508:                tmpbuf = buf_alloc(len + strlen(expbuf));
1.231     tobias   2509:                /* Append everything before keyword. */
1.297     ray      2510:                buf_append(tmpbuf, line->l_line,
1.231     tobias   2511:                    start - line->l_line);
                   2512:                /* Append keyword. */
1.297     ray      2513:                buf_puts(tmpbuf, expbuf);
1.231     tobias   2514:                /* Point c to end of keyword. */
1.297     ray      2515:                tlen = buf_len(tmpbuf) - 1;
1.231     tobias   2516:                /* Append everything after keyword. */
1.297     ray      2517:                buf_append(tmpbuf, end,
1.231     tobias   2518:                    line->l_line + line->l_len - end);
1.297     ray      2519:                c = buf_get(tmpbuf) + tlen;
1.231     tobias   2520:                /* Point fin to end of data. */
1.297     ray      2521:                fin = buf_get(tmpbuf) + buf_len(tmpbuf) - 1;
1.231     tobias   2522:                /* Recalculate new length. */
1.297     ray      2523:                len = buf_len(tmpbuf);
1.231     tobias   2524:
                   2525:                /* tmpbuf is now ready, convert to string */
                   2526:                if (line->l_needsfree)
1.313     nicm     2527:                        free(line->l_line);
1.231     tobias   2528:                line->l_len = len;
1.297     ray      2529:                line->l_line = buf_release(tmpbuf);
1.231     tobias   2530:                line->l_needsfree = 1;
1.196     niallo   2531:        }
1.131     niallo   2532: }
1.246     tobias   2533:
                   2534: /* rcs_translate_tag() */
                   2535: RCSNUM *
                   2536: rcs_translate_tag(const char *revstr, RCSFILE *rfp)
                   2537: {
                   2538:        int follow;
1.258     joris    2539:        time_t deltatime;
1.250     joris    2540:        char branch[CVS_REV_BUFSZ];
1.288     joris    2541:        RCSNUM *brev, *frev, *rev;
1.246     tobias   2542:        struct rcs_delta *rdp, *trdp;
1.274     tobias   2543:        time_t cdate;
1.246     tobias   2544:
1.288     joris    2545:        brev = frev = NULL;
1.246     tobias   2546:
1.250     joris    2547:        if (revstr == NULL) {
                   2548:                if (rfp->rf_branch != NULL) {
                   2549:                        rcsnum_tostr(rfp->rf_branch, branch, sizeof(branch));
                   2550:                        revstr = branch;
                   2551:                } else {
                   2552:                        revstr = RCS_HEAD_BRANCH;
                   2553:                }
                   2554:        }
1.246     tobias   2555:
                   2556:        if ((rev = rcs_get_revision(revstr, rfp)) == NULL)
1.258     joris    2557:                return (NULL);
                   2558:
                   2559:        if ((rdp = rcs_findrev(rfp, rev)) == NULL)
1.266     tobias   2560:                return (NULL);
1.258     joris    2561:
1.264     tobias   2562:        /* let's see if we must follow a branch */
                   2563:        if (!strcmp(revstr, RCS_HEAD_BRANCH))
                   2564:                follow = 1;
                   2565:        else {
                   2566:                frev = rcs_sym_getrev(rfp, revstr);
                   2567:                if (frev == NULL)
1.288     joris    2568:                        frev = rcsnum_parse(revstr);
1.264     tobias   2569:
                   2570:                brev = rcsnum_alloc();
                   2571:                rcsnum_cpy(rev, brev, rev->rn_len - 1);
                   2572:
                   2573:                if (frev != NULL && RCSNUM_ISBRANCH(frev) &&
                   2574:                    !rcsnum_cmp(frev, brev, 0)) {
                   2575:                        follow = 1;
                   2576:                } else
                   2577:                        follow = 0;
                   2578:
1.315     fcambus  2579:                free(brev);
1.264     tobias   2580:        }
                   2581:
1.274     tobias   2582:        if (cvs_specified_date != -1)
                   2583:                cdate = cvs_specified_date;
                   2584:        else
                   2585:                cdate = cvs_directory_date;
                   2586:
                   2587:        if (cdate == -1) {
1.317     joris    2588:                free(frev);
                   2589:
1.258     joris    2590:                /* XXX */
1.264     tobias   2591:                if (rev->rn_len < 4 || !follow) {
1.258     joris    2592:                        return (rev);
                   2593:                }
                   2594:
                   2595:                /* Find the latest delta on that branch */
1.315     fcambus  2596:                free(rev);
1.258     joris    2597:                for (;;) {
                   2598:                        if (rdp->rd_next->rn_len == 0)
                   2599:                                break;
                   2600:                        if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
1.272     tobias   2601:                                fatal("rcs_translate_tag: could not fetch "
1.258     joris    2602:                                    "branch delta");
                   2603:                }
                   2604:
                   2605:                rev = rcsnum_alloc();
                   2606:                rcsnum_cpy(rdp->rd_num, rev, 0);
                   2607:                return (rev);
1.246     tobias   2608:        }
                   2609:
                   2610:        if (frev != NULL) {
                   2611:                brev = rcsnum_revtobr(frev);
1.260     joris    2612:                brev->rn_len = rev->rn_len - 1;
1.315     fcambus  2613:                free(frev);
1.246     tobias   2614:        }
                   2615:
1.315     fcambus  2616:        free(rev);
1.246     tobias   2617:
                   2618:        do {
1.286     tobias   2619:                deltatime = timegm(&(rdp->rd_date));
1.258     joris    2620:
                   2621:                if (RCSNUM_ISBRANCHREV(rdp->rd_num)) {
1.274     tobias   2622:                        if (deltatime > cdate) {
1.258     joris    2623:                                trdp = TAILQ_PREV(rdp, rcs_dlist, rd_list);
                   2624:                                if (trdp == NULL)
                   2625:                                        trdp = rdp;
1.260     joris    2626:
                   2627:                                if (trdp->rd_num->rn_len != rdp->rd_num->rn_len)
                   2628:                                        return (NULL);
                   2629:
1.258     joris    2630:                                rev = rcsnum_alloc();
                   2631:                                rcsnum_cpy(trdp->rd_num, rev, 0);
1.260     joris    2632:                                return (rev);
                   2633:                        }
                   2634:
                   2635:                        if (rdp->rd_next->rn_len == 0) {
                   2636:                                rev = rcsnum_alloc();
                   2637:                                rcsnum_cpy(rdp->rd_num, rev, 0);
1.258     joris    2638:                                return (rev);
                   2639:                        }
                   2640:                } else {
1.274     tobias   2641:                        if (deltatime < cdate) {
1.258     joris    2642:                                rev = rcsnum_alloc();
                   2643:                                rcsnum_cpy(rdp->rd_num, rev, 0);
                   2644:                                return (rev);
                   2645:                        }
1.246     tobias   2646:                }
                   2647:
                   2648:                if (follow && rdp->rd_next->rn_len != 0) {
                   2649:                        if (brev != NULL && !rcsnum_cmp(brev, rdp->rd_num, 0))
                   2650:                                break;
                   2651:
                   2652:                        trdp = rcs_findrev(rfp, rdp->rd_next);
                   2653:                        if (trdp == NULL)
                   2654:                                fatal("failed to grab next revision");
                   2655:                        rdp = trdp;
                   2656:                } else
                   2657:                        follow = 0;
                   2658:        } while (follow);
                   2659:
1.258     joris    2660:        return (NULL);
1.246     tobias   2661: }