[BACK]Return to ci.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/ci.c, Revision 1.95

1.94      niallo      1: /*     $OpenBSD: ci.c,v 1.93 2006/02/14 13:26:43 xsa Exp $     */
1.1       niallo      2: /*
                      3:  * Copyright (c) 2005 Niall O'Higgins <niallo@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following cinditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source cide must retain the above cipyright
                     11:  *    notice, this list of cinditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     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
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.91      xsa        27: #include "includes.h"
1.1       niallo     28:
1.92      xsa        29: #include "rcsprog.h"
1.4       niallo     30: #include "diff.h"
1.9       niallo     31:
1.95    ! niallo     32: #define CI_OPTSTRING    "d::f::i::j::k::l::m:M::N:n:qr::s:Tt:u::Vw:x:"
1.25      niallo     33: #define DATE_NOW        -1
                     34: #define DATE_MTIME      -2
                     35:
1.58      niallo     36: #define LOG_INIT        "Initial revision"
1.63      niallo     37: #define LOG_PROMPT      "enter log message, terminated with a single '.' "    \
1.58      niallo     38:                         "or end of file:\n>> "
1.63      niallo     39: #define DESC_PROMPT     "enter description, terminated with single '.' "      \
                     40:                        "or end of file:\nNOTE: This is NOT the log message!" \
                     41:                         "\n>> "
1.58      niallo     42:
                     43: struct checkin_params {
                     44:        int flags, openflags;
                     45:        mode_t fmode;
                     46:        time_t date;
                     47:        RCSFILE *file;
                     48:        RCSNUM *frev, *newrev;
                     49:        char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.82      joris      50:        char *author;
1.80      niallo     51:        const char *symbol, *state, *description;
1.58      niallo     52: };
                     53:
1.94      niallo     54: static int      checkin_attach_symbol(struct checkin_params *);
                     55: static int      checkin_checklock(struct checkin_params *);
1.68      xsa        56: static char    *checkin_choose_rcsfile(const char *);
                     57: static char    *checkin_diff_file(struct checkin_params *);
                     58: static char    *checkin_getdesc(void);
                     59: static char    *checkin_getinput(const char *);
                     60: static char    *checkin_getlogmsg(RCSNUM *, RCSNUM *);
                     61: static int      checkin_init(struct checkin_params *);
1.94      niallo     62: static int      checkin_mtimedate(struct checkin_params *);
                     63: static int      checkin_update(struct checkin_params *);
                     64: static void     checkin_revert(struct checkin_params *);
1.4       niallo     65:
1.1       niallo     66: void
                     67: checkin_usage(void)
                     68: {
                     69:        fprintf(stderr,
1.67      xsa        70:            "usage: ci [-MNqTV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.95    ! niallo     71:            "          [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
1.85      xsa        72:            "          [-nsymbol] [-r[rev]] [-sstate] [-tfile|str] [-u[rev]]\n"
1.81      niallo     73:            "          [-wusername] [-xsuffixes] file ...\n");
1.58      niallo     74: }
                     75:
1.55      niallo     76:
1.1       niallo     77:
                     78: /*
                     79:  * checkin_main()
                     80:  *
                     81:  * Handler for the `ci' program.
                     82:  * Returns 0 on success, or >0 on error.
                     83:  */
                     84: int
                     85: checkin_main(int argc, char **argv)
                     86: {
1.58      niallo     87:        int i, ch, status;
                     88:        struct checkin_params pb;
1.1       niallo     89:
1.58      niallo     90:        pb.date = DATE_NOW;
                     91:        pb.file = NULL;
1.82      joris      92:        pb.rcs_msg = pb.username = pb.author = NULL;
1.80      niallo     93:        pb.state = pb.symbol = pb.description = NULL;
1.58      niallo     94:        pb.newrev =  NULL;
                     95:        pb.fmode = pb.flags = status = 0;
1.1       niallo     96:
1.58      niallo     97:        pb.flags = INTERACTIVE;
1.90      niallo     98:        pb.openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.9       niallo     99:
1.58      niallo    100:        while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1       niallo    101:                switch (ch) {
1.20      niallo    102:                case 'd':
1.25      niallo    103:                        if (rcs_optarg == NULL)
1.58      niallo    104:                                pb.date = DATE_MTIME;
1.87      xsa       105:                        else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0)
                    106:                                fatal("invalid date");
1.20      niallo    107:                        break;
1.29      niallo    108:                case 'f':
1.58      niallo    109:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    110:                        pb.flags |= FORCE;
1.29      niallo    111:                        break;
1.1       niallo    112:                case 'h':
                    113:                        (usage)();
                    114:                        exit(0);
1.58      niallo    115:                case 'i':
                    116:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    117:                        pb.openflags |= RCS_CREATE;
1.66      niallo    118:                        pb.flags |= CI_INIT;
1.58      niallo    119:                        break;
                    120:                case 'j':
                    121:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    122:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    123:                        pb.flags &= ~CI_INIT;
1.58      niallo    124:                        break;
1.30      niallo    125:                case 'l':
1.58      niallo    126:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    127:                        pb.flags |= CO_LOCK;
1.54      niallo    128:                        break;
                    129:                case 'M':
1.58      niallo    130:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    131:                        pb.flags |= CO_REVDATE;
1.30      niallo    132:                        break;
1.1       niallo    133:                case 'm':
1.58      niallo    134:                        pb.rcs_msg = rcs_optarg;
1.87      xsa       135:                        if (pb.rcs_msg == NULL)
                    136:                                fatal("missing message for -m option");
1.58      niallo    137:                        pb.flags &= ~INTERACTIVE;
1.3       joris     138:                        break;
1.42      niallo    139:                case 'N':
1.84      joris     140:                        pb.symbol = xstrdup(rcs_optarg);
1.87      xsa       141:                        if (rcs_sym_check(pb.symbol) != 1)
                    142:                                fatal("invalid symbol `%s'", pb.symbol);
1.58      niallo    143:                        pb.flags |= CI_SYMFORCE;
1.42      niallo    144:                        break;
1.38      niallo    145:                case 'n':
1.84      joris     146:                        pb.symbol = xstrdup(rcs_optarg);
1.87      xsa       147:                        if (rcs_sym_check(pb.symbol) != 1)
                    148:                                fatal("invalid symbol `%s'", pb.symbol);
1.38      niallo    149:                        break;
1.3       joris     150:                case 'q':
                    151:                        verbose = 0;
1.1       niallo    152:                        break;
1.30      niallo    153:                case 'r':
1.58      niallo    154:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    155:                        pb.flags |= CI_DEFAULT;
1.9       niallo    156:                        break;
1.51      niallo    157:                case 's':
1.58      niallo    158:                        pb.state = rcs_optarg;
1.87      xsa       159:                        if (rcs_state_check(pb.state) < 0)
                    160:                                fatal("invalid state `%s'", pb.state);
1.67      xsa       161:                        break;
                    162:                case 'T':
                    163:                        pb.flags |= PRESERVETIME;
1.51      niallo    164:                        break;
1.80      niallo    165:                case 't':
1.84      joris     166:                        pb.description = xstrdup(rcs_optarg);
1.80      niallo    167:                        break;
1.9       niallo    168:                case 'u':
1.58      niallo    169:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    170:                        pb.flags |= CO_UNLOCK;
1.9       niallo    171:                        break;
1.30      niallo    172:                case 'V':
                    173:                        printf("%s\n", rcs_version);
                    174:                        exit(0);
1.31      niallo    175:                case 'w':
1.84      joris     176:                        pb.author = xstrdup(rcs_optarg);
1.64      xsa       177:                        break;
                    178:                case 'x':
                    179:                        rcs_suffixes = rcs_optarg;
1.31      niallo    180:                        break;
1.1       niallo    181:                default:
                    182:                        (usage)();
                    183:                        exit(1);
                    184:                }
                    185:        }
                    186:
1.24      joris     187:        argc -= rcs_optind;
                    188:        argv += rcs_optind;
                    189:
1.1       niallo    190:        if (argc == 0) {
                    191:                cvs_log(LP_ERR, "no input file");
                    192:                (usage)();
                    193:                exit(1);
                    194:        }
                    195:
1.86      xsa       196:        if ((pb.username = getlogin()) == NULL)
                    197:                fatal("getlogin failed");
1.31      niallo    198:
                    199:
1.1       niallo    200:        for (i = 0; i < argc; i++) {
1.58      niallo    201:                pb.filename = argv[i];
1.1       niallo    202:
1.58      niallo    203:                /*
                    204:                 * Test for existence of ,v file. If we are expected to
                    205:                 * create one, set NEWFILE flag.
                    206:                 */
1.71      niallo    207:                if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
                    208:                        if (pb.openflags & RCS_CREATE)
                    209:                                pb.flags |= NEWFILE;
                    210:                        else {
                    211:                                cvs_log(LP_ERR, "No existing RCS file");
                    212:                                status = 1;
                    213:                                continue;
                    214:                        }
1.66      niallo    215:                } else {
                    216:                        if (pb.flags & CI_INIT) {
                    217:                                cvs_log(LP_ERR, "%s already exists", pb.fpath);
                    218:                                status = 1;
                    219:                                continue;
                    220:                        }
1.58      niallo    221:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    222:                }
1.63      niallo    223:                /*
                    224:                 * If we are to create a new ,v file, we must decide where it
                    225:                 * should go.
                    226:                 */
                    227:                if (pb.flags & NEWFILE) {
                    228:                        char *fpath = checkin_choose_rcsfile(pb.filename);
                    229:                        if (fpath == NULL) {
                    230:                                status = 1;
                    231:                                continue;
                    232:                        }
                    233:                        strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84      joris     234:                        xfree(fpath);
1.63      niallo    235:                }
                    236:
1.58      niallo    237:                pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
                    238:
1.87      xsa       239:                if (pb.file == NULL)
                    240:                        fatal("failed to open rcsfile '%s'", pb.fpath);
1.29      niallo    241:
1.58      niallo    242:                if (verbose == 1)
                    243:                        printf("%s  <--  %s\n", pb.fpath, pb.filename);
1.61      niallo    244:
1.63      niallo    245:                if (pb.flags & NEWFILE)
                    246:                        status = checkin_init(&pb);
1.15      niallo    247:                else
1.63      niallo    248:                        status = checkin_update(&pb);
1.4       niallo    249:        }
                    250:
1.93      xsa       251:        if (verbose == 1)
                    252:                printf("done\n");
                    253:
1.16      niallo    254:        return (status);
1.4       niallo    255: }
                    256:
1.59      niallo    257: /*
                    258:  * checkin_diff_file()
                    259:  *
1.65      xsa       260:  * Generate the diff between the working file and a revision.
1.59      niallo    261:  * Returns pointer to a char array on success, NULL on failure.
                    262:  */
1.4       niallo    263: static char *
1.58      niallo    264: checkin_diff_file(struct checkin_params *pb)
1.4       niallo    265: {
                    266:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    267:        BUF *b1, *b2, *b3;
                    268:        char rbuf[64], *deltatext;
                    269:
1.58      niallo    270:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4       niallo    271:
1.58      niallo    272:        if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    273:                cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4       niallo    274:                return (NULL);
1.1       niallo    275:        }
                    276:
1.58      niallo    277:        if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4       niallo    278:                cvs_log(LP_ERR, "failed to load revision");
                    279:                cvs_buf_free(b1);
                    280:                return (NULL);
                    281:        }
                    282:
1.57      xsa       283:        if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4       niallo    284:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    285:                cvs_buf_free(b1);
                    286:                cvs_buf_free(b2);
                    287:                return (NULL);
                    288:        }
                    289:
1.50      xsa       290:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    291:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    292:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    293:                cvs_log(LP_ERRNO, "could not write temporary file");
                    294:                cvs_buf_free(b1);
                    295:                cvs_buf_free(b2);
                    296:                return (NULL);
                    297:        }
                    298:        cvs_buf_free(b1);
                    299:
1.50      xsa       300:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    301:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    302:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    303:                cvs_buf_free(b2);
                    304:                (void)unlink(path1);
                    305:                return (NULL);
                    306:        }
                    307:        cvs_buf_free(b2);
                    308:
1.5       niallo    309:        diff_format = D_RCSDIFF;
1.4       niallo    310:        cvs_diffreg(path1, path2, b3);
                    311:        (void)unlink(path1);
                    312:        (void)unlink(path2);
                    313:
                    314:        cvs_buf_putc(b3, '\0');
                    315:        deltatext = (char *)cvs_buf_release(b3);
                    316:
                    317:        return (deltatext);
1.6       niallo    318: }
                    319:
                    320: /*
1.65      xsa       321:  * checkin_getlogmsg()
1.59      niallo    322:  *
1.6       niallo    323:  * Get log message from user interactively.
1.59      niallo    324:  * Returns pointer to a char array on success, NULL on failure.
1.6       niallo    325:  */
                    326: static char *
1.34      niallo    327: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6       niallo    328: {
1.58      niallo    329:        char   *rcs_msg, nrev[16], prev[16];
1.6       niallo    330:        RCSNUM *tmprev;
                    331:
1.7       niallo    332:        rcs_msg = NULL;
1.6       niallo    333:        tmprev = rcsnum_alloc();
                    334:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    335:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    336:        if (rev2 == NULL)
                    337:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    338:        else
                    339:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    340:        rcsnum_free(tmprev);
                    341:
1.47      niallo    342:        if (verbose == 1)
                    343:                printf("new revision: %s; previous revision: %s\n", nrev,
                    344:                    prev);
1.32      joris     345:
1.58      niallo    346:        rcs_msg = checkin_getinput(LOG_PROMPT);
                    347:        return (rcs_msg);
                    348: }
                    349:
                    350:
                    351: /*
                    352:  * checkin_getdesc()
                    353:  *
                    354:  * Get file description interactively.
1.59      niallo    355:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    356:  */
                    357: static char *
                    358: checkin_getdesc()
                    359: {
                    360:        char *description;
                    361:
                    362:        description = checkin_getinput(DESC_PROMPT);
                    363:        return (description);
                    364: }
                    365:
                    366: /*
                    367:  * checkin_getinput()
                    368:  *
1.59      niallo    369:  * Get some input from the user, in RCS style, prompting with message <prompt>.
                    370:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    371:  */
                    372: static char *
                    373: checkin_getinput(const char *prompt)
                    374: {
                    375:        BUF *inputbuf;
                    376:        char *input, buf[128];
                    377:
                    378:        if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
                    379:                cvs_log(LP_ERR, "failed to allocate input buffer");
                    380:                return (NULL);
                    381:        }
                    382:
                    383:        printf(prompt);
1.6       niallo    384:        for (;;) {
                    385:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    386:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    387:                        break;
1.58      niallo    388:                cvs_buf_append(inputbuf, buf, strlen(buf));
1.46      joris     389:                printf(">> ");
1.6       niallo    390:        }
1.32      joris     391:
1.58      niallo    392:        cvs_buf_putc(inputbuf, '\0');
                    393:        input = (char *)cvs_buf_release(inputbuf);
                    394:
                    395:        return (input);
                    396: }
                    397:
                    398: /*
1.63      niallo    399:  * checkin_update()
                    400:  *
                    401:  * Do a checkin to an existing RCS file.
                    402:  *
                    403:  * On success, return 0. On error return -1.
                    404:  */
                    405: static int
                    406: checkin_update(struct checkin_params *pb)
                    407: {
1.77      xsa       408:        char  *filec, numb1[64], numb2[64];
1.63      niallo    409:        BUF *bp;
                    410:
1.82      joris     411:        /*
                    412:         * XXX this is wrong, we need to get the revision the user
1.85      xsa       413:         * has the lock for. So we can decide if we want to create a
1.82      joris     414:         * branch or not. (if it's not current HEAD we need to branch).
                    415:         */
1.63      niallo    416:        pb->frev = pb->file->rf_head;
                    417:
1.82      joris     418:        if (checkin_checklock(pb) < 0)
                    419:                return (-1);
                    420:
                    421:        /* If revision passed on command line is less than HEAD, bail.
                    422:         * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
                    423:         * there is no lock set for the user.
                    424:         */
1.63      niallo    425:        if ((pb->newrev != NULL)
                    426:            && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77      xsa       427:                cvs_log(LP_ERR,
                    428:                    "%s: revision %s too low; must be higher than %s",
                    429:                    pb->file->rf_path,
                    430:                    rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
                    431:                    rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63      niallo    432:                rcs_close(pb->file);
                    433:                return (-1);
                    434:        }
                    435:
1.73      xsa       436:        /* Load file contents */
1.63      niallo    437:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    438:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
                    439:                return (-1);
                    440:        }
                    441:
                    442:        if (cvs_buf_putc(bp, '\0') < 0)
                    443:                return (-1);
                    444:
                    445:        filec = (char *)cvs_buf_release(bp);
                    446:
1.73      xsa       447:        /* Get RCS patch */
1.63      niallo    448:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
                    449:                cvs_log(LP_ERR, "failed to get diff");
                    450:                return (-1);
                    451:        }
                    452:
                    453:        /*
                    454:         * If -f is not specified and there are no differences, tell
                    455:         * the user and revert to latest version.
                    456:         */
                    457:        if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
                    458:                checkin_revert(pb);
                    459:                return (0);
                    460:        }
                    461:
1.73      xsa       462:        /* If no log message specified, get it interactively. */
1.63      niallo    463:        if (pb->flags & INTERACTIVE)
                    464:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
                    465:
1.82      joris     466:        if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63      niallo    467:                if (rcs_errno != RCS_ERR_NOENT)
1.82      joris     468:                        cvs_log(LP_WARN, "failed to remove lock");
                    469:                else if (!(pb->flags & CO_LOCK))
                    470:                        cvs_log(LP_WARN, "previous revision was not locked; "
                    471:                            "ignoring -l option");
1.63      niallo    472:        }
                    473:
1.73      xsa       474:        /* Current head revision gets the RCS patch as rd_text */
1.87      xsa       475:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
                    476:                fatal("failed to set new rd_text for head rev");
1.63      niallo    477:
                    478:        /*
                    479:         * Set the date of the revision to be the last modification
                    480:         * time of the working file if -d has no argument.
                    481:         */
                    482:        if (pb->date == DATE_MTIME
                    483:            && (checkin_mtimedate(pb) < 0))
                    484:                return (-1);
                    485:
1.73      xsa       486:        /* Now add our new revision */
1.63      niallo    487:        if (rcs_rev_add(pb->file,
                    488:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82      joris     489:            pb->rcs_msg, pb->date, pb->author) != 0) {
1.63      niallo    490:                cvs_log(LP_ERR, "failed to add new revision");
                    491:                return (-1);
                    492:        }
                    493:
                    494:        /*
                    495:         * If we are checking in to a non-default (ie user-specified)
                    496:         * revision, set head to this revision.
                    497:         */
                    498:        if (pb->newrev != NULL)
                    499:                rcs_head_set(pb->file, pb->newrev);
                    500:        else
                    501:                pb->newrev = pb->file->rf_head;
                    502:
1.73      xsa       503:        /* New head revision has to contain entire file; */
1.87      xsa       504:         if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
                    505:                fatal("failed to set new head revision");
1.63      niallo    506:
1.73      xsa       507:        /* Attach a symbolic name to this revision if specified. */
1.63      niallo    508:        if (pb->symbol != NULL
                    509:            && (checkin_attach_symbol(pb) < 0))
                    510:                return (-1);
                    511:
1.73      xsa       512:        /* Set the state of this revision if specified. */
1.63      niallo    513:        if (pb->state != NULL)
                    514:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    515:
1.84      joris     516:        xfree(pb->deltatext);
                    517:        xfree(filec);
1.63      niallo    518:        (void)unlink(pb->filename);
                    519:
1.73      xsa       520:        /* Do checkout if -u or -l are specified. */
1.63      niallo    521:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    522:            && !(pb->flags & CI_DEFAULT))
                    523:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89      joris     524:                    pb->username, pb->author, NULL, NULL);
1.63      niallo    525:
                    526:        /* File will NOW be synced */
                    527:        rcs_close(pb->file);
                    528:
                    529:        if (pb->flags & INTERACTIVE) {
1.84      joris     530:                xfree(pb->rcs_msg);
1.63      niallo    531:                pb->rcs_msg = NULL;
                    532:        }
                    533:        return (0);
                    534: }
                    535:
                    536: /*
1.58      niallo    537:  * checkin_init()
1.65      xsa       538:  *
1.58      niallo    539:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    540:  * On success, return 0. On error return -1.
1.58      niallo    541:  */
1.63      niallo    542: static int
1.58      niallo    543: checkin_init(struct checkin_params *pb)
                    544: {
1.80      niallo    545:        BUF *bp, *dp;
1.93      xsa       546:        char *filec, numb[64];
1.80      niallo    547:        const char *rcs_desc;
1.58      niallo    548:
1.73      xsa       549:        /* Load file contents */
1.58      niallo    550:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    551:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63      niallo    552:                return (-1);
1.58      niallo    553:        }
                    554:
                    555:        if (cvs_buf_putc(bp, '\0') < 0)
1.63      niallo    556:                return (-1);
1.58      niallo    557:
                    558:        filec = (char *)cvs_buf_release(bp);
                    559:
1.73      xsa       560:        /* Get description from user */
1.80      niallo    561:        if (pb->description == NULL)
                    562:                rcs_desc = (const char *)checkin_getdesc();
                    563:        else {
                    564:                if (*pb->description == '-') {
                    565:                        pb->description++;
1.81      niallo    566:                        rcs_desc = (const char *)pb->description;
                    567:                } else {
1.80      niallo    568:                        dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
                    569:                        if (dp == NULL) {
                    570:                                cvs_log(LP_ERR,
                    571:                                    "failed to load description file '%s'",
                    572:                                    pb->description);
1.84      joris     573:                                xfree(filec);
1.80      niallo    574:                                return (-1);
                    575:                        }
                    576:                        if (cvs_buf_putc(dp, '\0') < 0) {
1.84      joris     577:                                xfree(filec);
1.80      niallo    578:                                return (-1);
                    579:                        }
                    580:                        rcs_desc = (const char *)cvs_buf_release(dp);
                    581:                }
                    582:        }
1.58      niallo    583:        rcs_desc_set(pb->file, rcs_desc);
                    584:
1.73      xsa       585:        /* Now add our new revision */
1.82      joris     586:        if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->author) != 0) {
1.58      niallo    587:                cvs_log(LP_ERR, "failed to add new revision");
1.63      niallo    588:                return (-1);
                    589:        }
                    590:        /*
                    591:         * If we are checking in to a non-default (ie user-specified)
                    592:         * revision, set head to this revision.
                    593:         */
                    594:        if (pb->newrev != NULL)
                    595:                rcs_head_set(pb->file, pb->newrev);
                    596:        else
                    597:                pb->newrev = pb->file->rf_head;
                    598:
1.73      xsa       599:        /* New head revision has to contain entire file; */
1.66      niallo    600:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63      niallo    601:                cvs_log(LP_ERR, "failed to set new head revision");
                    602:                return (-1);
1.58      niallo    603:        }
1.73      xsa       604:        /* Attach a symbolic name to this revision if specified. */
1.66      niallo    605:        if (pb->symbol != NULL
                    606:            && (checkin_attach_symbol(pb) < 0))
                    607:                return (-1);
                    608:
1.73      xsa       609:        /* Set the state of this revision if specified. */
1.66      niallo    610:        if (pb->state != NULL)
                    611:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    612:
1.84      joris     613:        xfree(filec);
1.66      niallo    614:        (void)unlink(pb->filename);
                    615:
1.73      xsa       616:        /* Do checkout if -u or -l are specified. */
1.66      niallo    617:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    618:            && !(pb->flags & CI_DEFAULT))
                    619:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89      joris     620:                    pb->username, pb->author, NULL, NULL);
1.63      niallo    621:
1.93      xsa       622:        printf("initial revision: %s\n",
                    623:            rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
                    624:
1.66      niallo    625:        /* File will NOW be synced */
                    626:        rcs_close(pb->file);
1.93      xsa       627:
1.63      niallo    628:        return (0);
1.58      niallo    629: }
                    630:
1.59      niallo    631: /*
                    632:  * checkin_attach_symbol()
                    633:  *
                    634:  * Attempt to attach the specified symbol to the revision.
                    635:  * On success, return 0. On error return -1.
                    636:  */
1.58      niallo    637: static int
                    638: checkin_attach_symbol(struct checkin_params *pb)
                    639: {
                    640:        char rbuf[16];
                    641:        int ret;
                    642:        if (verbose == 1)
                    643:                printf("symbol: %s\n", pb->symbol);
                    644:        if (pb->flags & CI_SYMFORCE)
                    645:                rcs_sym_remove(pb->file, pb->symbol);
                    646:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
                    647:            && (rcs_errno == RCS_ERR_DUPENT)) {
                    648:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    649:                    rbuf, sizeof(rbuf));
                    650:                cvs_log(LP_ERR,
                    651:                    "symbolic name %s already bound to %s",
                    652:                    pb->symbol, rbuf);
                    653:                rcs_close(pb->file);
                    654:                return (-1);
                    655:        } else if (ret == -1) {
                    656:                cvs_log(LP_ERR, "problem adding symbol: %s",
                    657:                    pb->symbol);
                    658:                rcs_close(pb->file);
                    659:                return (-1);
                    660:        }
                    661:        return (0);
                    662: }
                    663:
1.59      niallo    664: /*
                    665:  * checkin_revert()
                    666:  *
                    667:  * If there are no differences between the working file and the latest revision
                    668:  * and the -f flag is not specified, simply revert to the latest version and
                    669:  * warn the user.
                    670:  *
                    671:  */
1.58      niallo    672: static void
                    673: checkin_revert(struct checkin_params *pb)
                    674: {
                    675:        char rbuf[16];
                    676:
                    677:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
                    678:        cvs_log(LP_WARN,
                    679:            "file is unchanged; reverting to previous revision %s",
                    680:            rbuf);
                    681:        (void)unlink(pb->filename);
                    682:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    683:                checkout_rev(pb->file, pb->frev, pb->filename,
1.89      joris     684:                    pb->flags, pb->username, pb->author, NULL, NULL);
1.82      joris     685:        rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58      niallo    686:        rcs_close(pb->file);
                    687: }
                    688:
1.59      niallo    689: /*
                    690:  * checkin_checklock()
                    691:  *
                    692:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    693:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    694:  */
1.58      niallo    695: static int
                    696: checkin_checklock(struct checkin_params *pb)
                    697: {
                    698:        struct rcs_lock *lkp;
                    699:
1.82      joris     700:        TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
                    701:                if ((!strcmp(lkp->rl_name, pb->username)) &&
                    702:                    (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
                    703:                        return (0);
1.58      niallo    704:        }
1.32      joris     705:
1.82      joris     706:        cvs_log(LP_ERR,
                    707:            "%s: no lock set by %s", pb->file->rf_path, pb->username);
                    708:        rcs_close(pb->file);
                    709:        return (-1);
1.61      niallo    710: }
                    711:
                    712: /*
1.62      niallo    713:  * checkin_mtimedate()
1.61      niallo    714:  *
                    715:  * Set the date of the revision to be the last modification
1.62      niallo    716:  * time of the working file.
1.61      niallo    717:  *
                    718:  * On success, return 0. On error return -1.
                    719:  */
                    720: static int
1.62      niallo    721: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    722: {
                    723:        struct stat sb;
                    724:        if (stat(pb->filename, &sb) != 0) {
1.72      xsa       725:                cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61      niallo    726:                rcs_close(pb->file);
                    727:                return (-1);
                    728:        }
                    729:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58      niallo    730:        return (0);
1.63      niallo    731: }
                    732:
                    733: /*
1.75      niallo    734:  * checkin_choose_rcsfile()
1.63      niallo    735:  *
                    736:  * Given a relative filename, decide where the corresponding ,v file
1.65      xsa       737:  * should be.
1.63      niallo    738:  *
                    739:  * Returns pointer to a char array on success, NULL on failure.
                    740:  */
                    741: static char *
                    742: checkin_choose_rcsfile(const char *filename)
                    743: {
1.76      niallo    744:        char name[MAXPATHLEN], *basepath;
                    745:        const char *ptr;
1.63      niallo    746:        size_t len;
                    747:        struct stat sb;
                    748:
1.84      joris     749:        basepath = xmalloc(MAXPATHLEN);
1.88      alek      750:        basepath[0] = '\0';
1.76      niallo    751:        if (strchr(filename, '/') == NULL) {
                    752:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    753:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    754:                        /* <path>/RCS/<filename>,v */
                    755:                        strlcat(basepath, filename, MAXPATHLEN);
                    756:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    757:                } else {
                    758:                        /* <path>/<filename>,v */
                    759:                        strlcpy(basepath, filename, MAXPATHLEN);
                    760:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    761:                }
                    762:        } else {
                    763:                ptr = filename;
                    764:                /* Walk backwards till we find the base directory */
                    765:                len = strlen(filename);
                    766:                ptr += len + 1;
                    767:                while (filename[len] != '/') {
                    768:                        len--;
                    769:                        ptr--;
                    770:                }
1.63      niallo    771:                /*
                    772:                 * Need two bytes extra for trailing slash and
                    773:                 * NUL-termination.
                    774:                 */
                    775:                len += 2;
1.76      niallo    776:                if (len > MAXPATHLEN) {
1.84      joris     777:                        xfree(basepath);
1.63      niallo    778:                        return (NULL);
                    779:                }
1.76      niallo    780:                strlcpy(basepath, filename, len);
                    781:                strlcpy(name, ptr, MAXPATHLEN);
1.63      niallo    782:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    783:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    784:                        /* <path>/RCS/<filename>,v */
1.76      niallo    785:                        strlcat(basepath, name, MAXPATHLEN);
1.63      niallo    786:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    787:                } else {
                    788:                        /* <path>/<filename>,v */
1.76      niallo    789:                        strlcpy(basepath, filename, MAXPATHLEN);
1.63      niallo    790:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    791:                }
1.76      niallo    792:        }
                    793:        return (basepath);
1.1       niallo    794: }