Annotation of src/usr.bin/rcs/ci.c, Revision 1.106
1.106 ! niallo 1: /* $OpenBSD: ci.c,v 1.105 2006/03/05 17:20:35 niallo Exp $ */
1.1 niallo 2: /*
1.98 niallo 3: * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1 niallo 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.98 niallo 36: #define KW_ID "Id"
37: #define KW_AUTHOR "Author"
38: #define KW_DATE "Date"
39: #define KW_STATE "State"
40: #define KW_REVISION "Revision"
41: #define KW_TYPE_ID 1
42: #define KW_TYPE_AUTHOR 2
43: #define KW_TYPE_DATE 3
44: #define KW_TYPE_STATE 4
45: #define KW_TYPE_REVISION 5
46: #define KW_NUMTOKS_ID 10
47: #define KW_NUMTOKS_AUTHOR 3
48: #define KW_NUMTOKS_DATE 4
49: #define KW_NUMTOKS_STATE 3
50: #define KW_NUMTOKS_REVISION 3
51:
1.58 niallo 52: #define LOG_INIT "Initial revision"
1.63 niallo 53: #define LOG_PROMPT "enter log message, terminated with a single '.' " \
1.58 niallo 54: "or end of file:\n>> "
1.63 niallo 55: #define DESC_PROMPT "enter description, terminated with single '.' " \
56: "or end of file:\nNOTE: This is NOT the log message!" \
57: "\n>> "
1.58 niallo 58:
1.98 niallo 59: extern struct rcs_kw rcs_expkw[];
60:
1.58 niallo 61: struct checkin_params {
62: int flags, openflags;
63: mode_t fmode;
64: time_t date;
65: RCSFILE *file;
66: RCSNUM *frev, *newrev;
67: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.98 niallo 68: char *author, *state;
69: const char *symbol, *description;
1.58 niallo 70: };
71:
1.94 niallo 72: static int checkin_attach_symbol(struct checkin_params *);
73: static int checkin_checklock(struct checkin_params *);
1.68 xsa 74: static char *checkin_choose_rcsfile(const char *);
75: static char *checkin_diff_file(struct checkin_params *);
76: static char *checkin_getdesc(void);
77: static char *checkin_getinput(const char *);
78: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *);
79: static int checkin_init(struct checkin_params *);
1.98 niallo 80: static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **,
81: char **);
82: static int checkin_keywordtype(char *);
1.94 niallo 83: static int checkin_mtimedate(struct checkin_params *);
1.98 niallo 84: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
85: char **);
1.94 niallo 86: static int checkin_update(struct checkin_params *);
87: static void checkin_revert(struct checkin_params *);
1.4 niallo 88:
1.1 niallo 89: void
90: checkin_usage(void)
91: {
92: fprintf(stderr,
1.67 xsa 93: "usage: ci [-MNqTV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.95 niallo 94: " [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
1.85 xsa 95: " [-nsymbol] [-r[rev]] [-sstate] [-tfile|str] [-u[rev]]\n"
1.81 niallo 96: " [-wusername] [-xsuffixes] file ...\n");
1.58 niallo 97: }
98:
1.55 niallo 99:
1.1 niallo 100:
101: /*
102: * checkin_main()
103: *
104: * Handler for the `ci' program.
105: * Returns 0 on success, or >0 on error.
106: */
107: int
108: checkin_main(int argc, char **argv)
109: {
1.58 niallo 110: int i, ch, status;
111: struct checkin_params pb;
1.1 niallo 112:
1.58 niallo 113: pb.date = DATE_NOW;
114: pb.file = NULL;
1.98 niallo 115: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
116: pb.symbol = pb.description = NULL;
1.58 niallo 117: pb.newrev = NULL;
118: pb.fmode = pb.flags = status = 0;
1.1 niallo 119:
1.58 niallo 120: pb.flags = INTERACTIVE;
1.90 niallo 121: pb.openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.9 niallo 122:
1.58 niallo 123: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 124: switch (ch) {
1.20 niallo 125: case 'd':
1.25 niallo 126: if (rcs_optarg == NULL)
1.58 niallo 127: pb.date = DATE_MTIME;
1.87 xsa 128: else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0)
129: fatal("invalid date");
1.20 niallo 130: break;
1.29 niallo 131: case 'f':
1.58 niallo 132: rcs_set_rev(rcs_optarg, &pb.newrev);
133: pb.flags |= FORCE;
1.29 niallo 134: break;
1.1 niallo 135: case 'h':
136: (usage)();
137: exit(0);
1.58 niallo 138: case 'i':
139: rcs_set_rev(rcs_optarg, &pb.newrev);
140: pb.openflags |= RCS_CREATE;
1.66 niallo 141: pb.flags |= CI_INIT;
1.58 niallo 142: break;
143: case 'j':
144: rcs_set_rev(rcs_optarg, &pb.newrev);
145: pb.openflags &= ~RCS_CREATE;
1.66 niallo 146: pb.flags &= ~CI_INIT;
1.58 niallo 147: break;
1.98 niallo 148: case 'k':
149: rcs_set_rev(rcs_optarg, &pb.newrev);
150: pb.flags |= CI_KEYWORDSCAN;
151: break;
1.30 niallo 152: case 'l':
1.58 niallo 153: rcs_set_rev(rcs_optarg, &pb.newrev);
154: pb.flags |= CO_LOCK;
1.54 niallo 155: break;
156: case 'M':
1.58 niallo 157: rcs_set_rev(rcs_optarg, &pb.newrev);
158: pb.flags |= CO_REVDATE;
1.30 niallo 159: break;
1.1 niallo 160: case 'm':
1.58 niallo 161: pb.rcs_msg = rcs_optarg;
1.87 xsa 162: if (pb.rcs_msg == NULL)
163: fatal("missing message for -m option");
1.58 niallo 164: pb.flags &= ~INTERACTIVE;
1.3 joris 165: break;
1.42 niallo 166: case 'N':
1.84 joris 167: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 168: if (rcs_sym_check(pb.symbol) != 1)
169: fatal("invalid symbol `%s'", pb.symbol);
1.58 niallo 170: pb.flags |= CI_SYMFORCE;
1.42 niallo 171: break;
1.38 niallo 172: case 'n':
1.84 joris 173: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 174: if (rcs_sym_check(pb.symbol) != 1)
175: fatal("invalid symbol `%s'", pb.symbol);
1.38 niallo 176: break;
1.3 joris 177: case 'q':
178: verbose = 0;
1.1 niallo 179: break;
1.30 niallo 180: case 'r':
1.58 niallo 181: rcs_set_rev(rcs_optarg, &pb.newrev);
182: pb.flags |= CI_DEFAULT;
1.9 niallo 183: break;
1.51 niallo 184: case 's':
1.58 niallo 185: pb.state = rcs_optarg;
1.87 xsa 186: if (rcs_state_check(pb.state) < 0)
187: fatal("invalid state `%s'", pb.state);
1.67 xsa 188: break;
189: case 'T':
190: pb.flags |= PRESERVETIME;
1.51 niallo 191: break;
1.80 niallo 192: case 't':
1.84 joris 193: pb.description = xstrdup(rcs_optarg);
1.80 niallo 194: break;
1.9 niallo 195: case 'u':
1.58 niallo 196: rcs_set_rev(rcs_optarg, &pb.newrev);
197: pb.flags |= CO_UNLOCK;
1.9 niallo 198: break;
1.30 niallo 199: case 'V':
200: printf("%s\n", rcs_version);
201: exit(0);
1.31 niallo 202: case 'w':
1.84 joris 203: pb.author = xstrdup(rcs_optarg);
1.64 xsa 204: break;
205: case 'x':
206: rcs_suffixes = rcs_optarg;
1.31 niallo 207: break;
1.1 niallo 208: default:
209: (usage)();
210: exit(1);
211: }
212: }
213:
1.24 joris 214: argc -= rcs_optind;
215: argv += rcs_optind;
216:
1.1 niallo 217: if (argc == 0) {
218: cvs_log(LP_ERR, "no input file");
219: (usage)();
220: exit(1);
221: }
222:
1.86 xsa 223: if ((pb.username = getlogin()) == NULL)
224: fatal("getlogin failed");
1.31 niallo 225:
226:
1.1 niallo 227: for (i = 0; i < argc; i++) {
1.58 niallo 228: pb.filename = argv[i];
1.1 niallo 229:
1.58 niallo 230: /*
231: * Test for existence of ,v file. If we are expected to
232: * create one, set NEWFILE flag.
233: */
1.71 niallo 234: if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
235: if (pb.openflags & RCS_CREATE)
236: pb.flags |= NEWFILE;
237: else {
238: cvs_log(LP_ERR, "No existing RCS file");
239: status = 1;
240: continue;
241: }
1.66 niallo 242: } else {
243: if (pb.flags & CI_INIT) {
244: cvs_log(LP_ERR, "%s already exists", pb.fpath);
245: status = 1;
246: continue;
247: }
1.58 niallo 248: pb.openflags &= ~RCS_CREATE;
1.66 niallo 249: }
1.63 niallo 250: /*
251: * If we are to create a new ,v file, we must decide where it
252: * should go.
253: */
254: if (pb.flags & NEWFILE) {
255: char *fpath = checkin_choose_rcsfile(pb.filename);
256: if (fpath == NULL) {
257: status = 1;
258: continue;
259: }
260: strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84 joris 261: xfree(fpath);
1.63 niallo 262: }
263:
1.58 niallo 264: pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
265:
1.87 xsa 266: if (pb.file == NULL)
267: fatal("failed to open rcsfile '%s'", pb.fpath);
1.29 niallo 268:
1.58 niallo 269: if (verbose == 1)
270: printf("%s <-- %s\n", pb.fpath, pb.filename);
1.61 niallo 271:
1.63 niallo 272: if (pb.flags & NEWFILE)
273: status = checkin_init(&pb);
1.15 niallo 274: else
1.63 niallo 275: status = checkin_update(&pb);
1.4 niallo 276: }
277:
1.93 xsa 278: if (verbose == 1)
279: printf("done\n");
280:
1.16 niallo 281: return (status);
1.4 niallo 282: }
283:
1.59 niallo 284: /*
285: * checkin_diff_file()
286: *
1.65 xsa 287: * Generate the diff between the working file and a revision.
1.59 niallo 288: * Returns pointer to a char array on success, NULL on failure.
289: */
1.4 niallo 290: static char *
1.58 niallo 291: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 292: {
293: char path1[MAXPATHLEN], path2[MAXPATHLEN];
294: BUF *b1, *b2, *b3;
295: char rbuf[64], *deltatext;
296:
1.58 niallo 297: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4 niallo 298:
1.58 niallo 299: if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
300: cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4 niallo 301: return (NULL);
1.1 niallo 302: }
303:
1.58 niallo 304: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4 niallo 305: cvs_log(LP_ERR, "failed to load revision");
306: cvs_buf_free(b1);
307: return (NULL);
308: }
309:
1.57 xsa 310: if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4 niallo 311: cvs_log(LP_ERR, "failed to allocated buffer for diff");
312: cvs_buf_free(b1);
313: cvs_buf_free(b2);
314: return (NULL);
315: }
316:
1.50 xsa 317: strlcpy(path1, rcs_tmpdir, sizeof(path1));
318: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 319: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
320: cvs_log(LP_ERRNO, "could not write temporary file");
321: cvs_buf_free(b1);
322: cvs_buf_free(b2);
323: return (NULL);
324: }
325: cvs_buf_free(b1);
326:
1.50 xsa 327: strlcpy(path2, rcs_tmpdir, sizeof(path2));
328: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 329: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
330: cvs_buf_free(b2);
331: (void)unlink(path1);
332: return (NULL);
333: }
334: cvs_buf_free(b2);
335:
1.5 niallo 336: diff_format = D_RCSDIFF;
1.4 niallo 337: cvs_diffreg(path1, path2, b3);
338: (void)unlink(path1);
339: (void)unlink(path2);
340:
341: cvs_buf_putc(b3, '\0');
342: deltatext = (char *)cvs_buf_release(b3);
343:
344: return (deltatext);
1.6 niallo 345: }
346:
347: /*
1.65 xsa 348: * checkin_getlogmsg()
1.59 niallo 349: *
1.6 niallo 350: * Get log message from user interactively.
1.59 niallo 351: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 352: */
353: static char *
1.34 niallo 354: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 355: {
1.58 niallo 356: char *rcs_msg, nrev[16], prev[16];
1.6 niallo 357: RCSNUM *tmprev;
358:
1.7 niallo 359: rcs_msg = NULL;
1.6 niallo 360: tmprev = rcsnum_alloc();
361: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 362: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 363: if (rev2 == NULL)
364: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
365: else
366: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 367: rcsnum_free(tmprev);
368:
1.47 niallo 369: if (verbose == 1)
370: printf("new revision: %s; previous revision: %s\n", nrev,
371: prev);
1.32 joris 372:
1.58 niallo 373: rcs_msg = checkin_getinput(LOG_PROMPT);
374: return (rcs_msg);
375: }
376:
377:
378: /*
379: * checkin_getdesc()
380: *
381: * Get file description interactively.
1.59 niallo 382: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 383: */
384: static char *
385: checkin_getdesc()
386: {
387: char *description;
388:
389: description = checkin_getinput(DESC_PROMPT);
390: return (description);
391: }
392:
393: /*
394: * checkin_getinput()
395: *
1.59 niallo 396: * Get some input from the user, in RCS style, prompting with message <prompt>.
397: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 398: */
399: static char *
400: checkin_getinput(const char *prompt)
401: {
402: BUF *inputbuf;
403: char *input, buf[128];
404:
405: if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
406: cvs_log(LP_ERR, "failed to allocate input buffer");
407: return (NULL);
408: }
409:
410: printf(prompt);
1.6 niallo 411: for (;;) {
412: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 413: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 414: break;
1.58 niallo 415: cvs_buf_append(inputbuf, buf, strlen(buf));
1.46 joris 416: printf(">> ");
1.6 niallo 417: }
1.32 joris 418:
1.58 niallo 419: cvs_buf_putc(inputbuf, '\0');
420: input = (char *)cvs_buf_release(inputbuf);
421:
422: return (input);
423: }
424:
425: /*
1.63 niallo 426: * checkin_update()
427: *
428: * Do a checkin to an existing RCS file.
429: *
430: * On success, return 0. On error return -1.
431: */
432: static int
433: checkin_update(struct checkin_params *pb)
434: {
1.77 xsa 435: char *filec, numb1[64], numb2[64];
1.63 niallo 436: BUF *bp;
437:
1.82 joris 438: /*
439: * XXX this is wrong, we need to get the revision the user
1.85 xsa 440: * has the lock for. So we can decide if we want to create a
1.82 joris 441: * branch or not. (if it's not current HEAD we need to branch).
442: */
1.63 niallo 443: pb->frev = pb->file->rf_head;
444:
1.82 joris 445: if (checkin_checklock(pb) < 0)
446: return (-1);
447:
448: /* If revision passed on command line is less than HEAD, bail.
449: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
450: * there is no lock set for the user.
451: */
1.63 niallo 452: if ((pb->newrev != NULL)
453: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 454: cvs_log(LP_ERR,
455: "%s: revision %s too low; must be higher than %s",
456: pb->file->rf_path,
457: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
458: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63 niallo 459: rcs_close(pb->file);
460: return (-1);
461: }
462:
1.104 niallo 463: /*
464: * Set the date of the revision to be the last modification
465: * time of the working file if -d has no argument.
466: */
467: if (pb->date == DATE_MTIME
468: && (checkin_mtimedate(pb) < 0))
469: return (-1);
470:
471: /* Date from argv/mtime must be more recent than HEAD */
472: if (pb->date != DATE_NOW) {
473: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
474: if (pb->date <= head_date) {
475: char *head_date_str, *tdate;
476: head_date_str = xstrdup(ctime(&head_date));
477: head_date_str[strlen(head_date_str) - 1] = '\0';
478: tdate = xstrdup(ctime(&pb->date));
479: tdate[strlen(tdate) - 1] = '\0';
480: cvs_log(LP_ERR,
481: "%s: Date %s preceeds %s in revision %s",
482: pb->file->rf_path, tdate, head_date_str,
483: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
484: rcs_close(pb->file);
485: xfree(head_date_str);
486: xfree(tdate);
487: return (-1);
488: }
489: }
490:
1.73 xsa 491: /* Load file contents */
1.63 niallo 492: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
493: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.106 ! niallo 494: rcs_close(pb->file);
1.63 niallo 495: return (-1);
496: }
497:
1.106 ! niallo 498: if (cvs_buf_putc(bp, '\0') < 0) {
! 499: rcs_close(pb->file);
1.63 niallo 500: return (-1);
1.106 ! niallo 501: }
1.63 niallo 502:
503: filec = (char *)cvs_buf_release(bp);
504:
1.73 xsa 505: /* Get RCS patch */
1.63 niallo 506: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
507: cvs_log(LP_ERR, "failed to get diff");
1.106 ! niallo 508: rcs_close(pb->file);
1.63 niallo 509: return (-1);
510: }
511:
512: /*
513: * If -f is not specified and there are no differences, tell
514: * the user and revert to latest version.
515: */
516: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
517: checkin_revert(pb);
518: return (0);
519: }
520:
1.73 xsa 521: /* If no log message specified, get it interactively. */
1.63 niallo 522: if (pb->flags & INTERACTIVE)
523: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
524:
1.82 joris 525: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 526: if (rcs_errno != RCS_ERR_NOENT)
1.82 joris 527: cvs_log(LP_WARN, "failed to remove lock");
528: else if (!(pb->flags & CO_LOCK))
529: cvs_log(LP_WARN, "previous revision was not locked; "
530: "ignoring -l option");
1.63 niallo 531: }
532:
1.73 xsa 533: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 534: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
535: fatal("failed to set new rd_text for head rev");
1.63 niallo 536:
1.73 xsa 537: /* Now add our new revision */
1.63 niallo 538: if (rcs_rev_add(pb->file,
539: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 540: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 541: cvs_log(LP_ERR, "failed to add new revision");
1.106 ! niallo 542: rcs_close(pb->file);
1.63 niallo 543: return (-1);
544: }
545:
546: /*
547: * If we are checking in to a non-default (ie user-specified)
548: * revision, set head to this revision.
549: */
550: if (pb->newrev != NULL)
551: rcs_head_set(pb->file, pb->newrev);
552: else
553: pb->newrev = pb->file->rf_head;
554:
1.73 xsa 555: /* New head revision has to contain entire file; */
1.87 xsa 556: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
557: fatal("failed to set new head revision");
1.63 niallo 558:
1.73 xsa 559: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 560: if (pb->symbol != NULL
561: && (checkin_attach_symbol(pb) < 0))
562: return (-1);
563:
1.73 xsa 564: /* Set the state of this revision if specified. */
1.63 niallo 565: if (pb->state != NULL)
566: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
567:
1.84 joris 568: xfree(pb->deltatext);
569: xfree(filec);
1.63 niallo 570: (void)unlink(pb->filename);
571:
1.73 xsa 572: /* Do checkout if -u or -l are specified. */
1.63 niallo 573: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
574: && !(pb->flags & CI_DEFAULT))
575: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 576: pb->username, pb->author, NULL, NULL);
1.63 niallo 577:
578: /* File will NOW be synced */
579: rcs_close(pb->file);
580:
581: if (pb->flags & INTERACTIVE) {
1.84 joris 582: xfree(pb->rcs_msg);
1.63 niallo 583: pb->rcs_msg = NULL;
584: }
585: return (0);
586: }
587:
588: /*
1.58 niallo 589: * checkin_init()
1.65 xsa 590: *
1.58 niallo 591: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 592: * On success, return 0. On error return -1.
1.58 niallo 593: */
1.63 niallo 594: static int
1.58 niallo 595: checkin_init(struct checkin_params *pb)
596: {
1.80 niallo 597: BUF *bp, *dp;
1.93 xsa 598: char *filec, numb[64];
1.80 niallo 599: const char *rcs_desc;
1.58 niallo 600:
1.73 xsa 601: /* Load file contents */
1.58 niallo 602: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
603: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.106 ! niallo 604: rcs_close(pb->file);
1.63 niallo 605: return (-1);
1.58 niallo 606: }
607:
1.106 ! niallo 608: if (cvs_buf_putc(bp, '\0') < 0) {
! 609: rcs_close(pb->file);
1.63 niallo 610: return (-1);
1.106 ! niallo 611: }
1.58 niallo 612:
613: filec = (char *)cvs_buf_release(bp);
614:
1.98 niallo 615: /* Get default values from working copy if -k specified */
616: if (pb->flags & CI_KEYWORDSCAN)
617: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
618: &pb->author);
619:
1.73 xsa 620: /* Get description from user */
1.80 niallo 621: if (pb->description == NULL)
622: rcs_desc = (const char *)checkin_getdesc();
623: else {
624: if (*pb->description == '-') {
625: pb->description++;
1.81 niallo 626: rcs_desc = (const char *)pb->description;
627: } else {
1.80 niallo 628: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
629: if (dp == NULL) {
630: cvs_log(LP_ERR,
631: "failed to load description file '%s'",
632: pb->description);
1.84 joris 633: xfree(filec);
1.106 ! niallo 634: rcs_close(pb->file);
1.80 niallo 635: return (-1);
636: }
637: if (cvs_buf_putc(dp, '\0') < 0) {
1.84 joris 638: xfree(filec);
1.106 ! niallo 639: rcs_close(pb->file);
1.80 niallo 640: return (-1);
641: }
642: rcs_desc = (const char *)cvs_buf_release(dp);
643: }
644: }
1.58 niallo 645: rcs_desc_set(pb->file, rcs_desc);
646:
1.97 niallo 647: /*
648: * Set the date of the revision to be the last modification
649: * time of the working file if -d has no argument.
650: */
651: if (pb->date == DATE_MTIME
652: && (checkin_mtimedate(pb) < 0))
653: return (-1);
654:
1.73 xsa 655: /* Now add our new revision */
1.96 niallo 656: if (rcs_rev_add(pb->file,
657: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.103 niallo 658: (pb->rcs_msg == NULL ? LOG_INIT : pb->rcs_msg),
659: pb->date, pb->author) != 0) {
1.58 niallo 660: cvs_log(LP_ERR, "failed to add new revision");
1.106 ! niallo 661: rcs_close(pb->file);
1.63 niallo 662: return (-1);
663: }
664: /*
665: * If we are checking in to a non-default (ie user-specified)
666: * revision, set head to this revision.
667: */
668: if (pb->newrev != NULL)
669: rcs_head_set(pb->file, pb->newrev);
670: else
671: pb->newrev = pb->file->rf_head;
672:
1.73 xsa 673: /* New head revision has to contain entire file; */
1.66 niallo 674: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 675: cvs_log(LP_ERR, "failed to set new head revision");
1.106 ! niallo 676: rcs_close(pb->file);
1.63 niallo 677: return (-1);
1.58 niallo 678: }
1.73 xsa 679: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 680: if (pb->symbol != NULL
681: && (checkin_attach_symbol(pb) < 0))
682: return (-1);
683:
1.73 xsa 684: /* Set the state of this revision if specified. */
1.66 niallo 685: if (pb->state != NULL)
686: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
687:
1.84 joris 688: xfree(filec);
1.66 niallo 689: (void)unlink(pb->filename);
690:
1.73 xsa 691: /* Do checkout if -u or -l are specified. */
1.66 niallo 692: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
693: && !(pb->flags & CI_DEFAULT))
694: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 695: pb->username, pb->author, NULL, NULL);
1.63 niallo 696:
1.93 xsa 697: printf("initial revision: %s\n",
698: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
699:
1.66 niallo 700: /* File will NOW be synced */
701: rcs_close(pb->file);
1.93 xsa 702:
1.63 niallo 703: return (0);
1.58 niallo 704: }
705:
1.59 niallo 706: /*
707: * checkin_attach_symbol()
708: *
709: * Attempt to attach the specified symbol to the revision.
710: * On success, return 0. On error return -1.
711: */
1.58 niallo 712: static int
713: checkin_attach_symbol(struct checkin_params *pb)
714: {
715: char rbuf[16];
716: int ret;
717: if (verbose == 1)
718: printf("symbol: %s\n", pb->symbol);
719: if (pb->flags & CI_SYMFORCE)
720: rcs_sym_remove(pb->file, pb->symbol);
721: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
722: && (rcs_errno == RCS_ERR_DUPENT)) {
723: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
724: rbuf, sizeof(rbuf));
725: cvs_log(LP_ERR,
726: "symbolic name %s already bound to %s",
727: pb->symbol, rbuf);
728: rcs_close(pb->file);
729: return (-1);
730: } else if (ret == -1) {
731: cvs_log(LP_ERR, "problem adding symbol: %s",
732: pb->symbol);
733: rcs_close(pb->file);
734: return (-1);
735: }
736: return (0);
737: }
738:
1.59 niallo 739: /*
740: * checkin_revert()
741: *
742: * If there are no differences between the working file and the latest revision
743: * and the -f flag is not specified, simply revert to the latest version and
744: * warn the user.
745: *
746: */
1.58 niallo 747: static void
748: checkin_revert(struct checkin_params *pb)
749: {
750: char rbuf[16];
751:
752: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
753: cvs_log(LP_WARN,
754: "file is unchanged; reverting to previous revision %s",
755: rbuf);
756: (void)unlink(pb->filename);
757: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
758: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 759: pb->flags, pb->username, pb->author, NULL, NULL);
1.82 joris 760: rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58 niallo 761: rcs_close(pb->file);
762: }
763:
1.59 niallo 764: /*
765: * checkin_checklock()
766: *
767: * Check for the existence of a lock on the file. If there are no locks, or it
768: * is not locked by the correct user, return -1. Otherwise, return 0.
769: */
1.58 niallo 770: static int
771: checkin_checklock(struct checkin_params *pb)
772: {
773: struct rcs_lock *lkp;
774:
1.82 joris 775: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
776: if ((!strcmp(lkp->rl_name, pb->username)) &&
777: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
778: return (0);
1.58 niallo 779: }
1.32 joris 780:
1.82 joris 781: cvs_log(LP_ERR,
782: "%s: no lock set by %s", pb->file->rf_path, pb->username);
783: rcs_close(pb->file);
784: return (-1);
1.61 niallo 785: }
786:
787: /*
1.62 niallo 788: * checkin_mtimedate()
1.61 niallo 789: *
790: * Set the date of the revision to be the last modification
1.62 niallo 791: * time of the working file.
1.61 niallo 792: *
793: * On success, return 0. On error return -1.
794: */
795: static int
1.62 niallo 796: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 797: {
798: struct stat sb;
799: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 800: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 801: rcs_close(pb->file);
802: return (-1);
803: }
804: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 805: return (0);
1.63 niallo 806: }
807:
808: /*
1.75 niallo 809: * checkin_choose_rcsfile()
1.63 niallo 810: *
811: * Given a relative filename, decide where the corresponding ,v file
1.65 xsa 812: * should be.
1.63 niallo 813: *
814: * Returns pointer to a char array on success, NULL on failure.
815: */
816: static char *
817: checkin_choose_rcsfile(const char *filename)
818: {
1.76 niallo 819: char name[MAXPATHLEN], *basepath;
820: const char *ptr;
1.63 niallo 821: size_t len;
822: struct stat sb;
823:
1.84 joris 824: basepath = xmalloc(MAXPATHLEN);
1.88 alek 825: basepath[0] = '\0';
1.76 niallo 826: if (strchr(filename, '/') == NULL) {
827: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
828: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
829: /* <path>/RCS/<filename>,v */
830: strlcat(basepath, filename, MAXPATHLEN);
831: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
832: } else {
833: /* <path>/<filename>,v */
834: strlcpy(basepath, filename, MAXPATHLEN);
835: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
836: }
837: } else {
838: ptr = filename;
839: /* Walk backwards till we find the base directory */
840: len = strlen(filename);
841: ptr += len + 1;
842: while (filename[len] != '/') {
843: len--;
844: ptr--;
845: }
1.63 niallo 846: /*
847: * Need two bytes extra for trailing slash and
848: * NUL-termination.
849: */
850: len += 2;
1.76 niallo 851: if (len > MAXPATHLEN) {
1.84 joris 852: xfree(basepath);
1.63 niallo 853: return (NULL);
854: }
1.76 niallo 855: strlcpy(basepath, filename, len);
856: strlcpy(name, ptr, MAXPATHLEN);
1.63 niallo 857: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
858: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
859: /* <path>/RCS/<filename>,v */
1.76 niallo 860: strlcat(basepath, name, MAXPATHLEN);
1.63 niallo 861: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
862: } else {
863: /* <path>/<filename>,v */
1.76 niallo 864: strlcpy(basepath, filename, MAXPATHLEN);
1.63 niallo 865: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
866: }
1.76 niallo 867: }
868: return (basepath);
1.98 niallo 869: }
870:
871: /*
872: * checkin_keywordscan()
873: *
874: * Searches working file for keyword values to determine its revision
875: * number, creation date and author, and uses these values instead of
876: * calculating them locally.
877: *
878: * Params: The data buffer to scan and pointers to pointers of variables in
879: * which to store the outputs.
880: *
881: * On success, return 0. On error return -1.
882: */
883: static int
884: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
885: char **state)
886: {
887: size_t len;
888: u_int i, j, found, end;
889: char *c, *kwstr, *start, buf[128];
890:
891: c = start = kwstr = NULL;
892:
893: i = found = 0;
894:
895: len = strlen(data);
896: for (c = data; *c != '\0' && i < len; *c++) {
897: if (*c == '$') {
898: start = c;
899: *c++;
900: if (!isalpha(*c)) {
901: c = start;
902: continue;
903: }
904: /* look for any matching keywords */
905: found = 0;
906: for (j = 0; j < 10; j++) {
907: if (!strncmp(c, rcs_expkw[j].kw_str,
908: strlen(rcs_expkw[j].kw_str))) {
909: found = 1;
910: kwstr = rcs_expkw[j].kw_str;
911: break;
912: }
913: }
914:
915: /* unknown keyword, continue looking */
916: if (found == 0) {
917: c = start;
918: continue;
919: }
920:
921: c += strlen(kwstr);
922: if (*c != ':' && *c != '$') {
923: c = start;
924: continue;
925: }
926:
927: if (*c == ':') {
928: while (*c++) {
929: if (*c == '$') {
930: end = c - start + 2;
931: if (end >= sizeof(buf))
932: fatal("keyword buffer"
933: " too small!");
934: strlcpy(buf, start, end);
935: checkin_parsekeyword(buf, rev,
936: date, author, state);
937: break;
938: }
939: }
940:
941: if (*c != '$') {
942: c = start;
943: continue;
944: }
945: }
946: }
947: }
948: if (found == 0)
949: return (-1);
950: else
951: return (0);
952: }
953:
954: /*
955: * checkin_keywordtype()
956: *
957: * Given an RCS keyword string, determine what type of string it is.
958: * This enables us to know what data should be in it.
959: *
960: * Returns type on success, or -1 on failure.
961: */
962: static int
963: checkin_keywordtype(char *keystring)
964: {
965: char *p;
966:
967: p = keystring;
968: *p++;
969: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
970: return (KW_TYPE_ID);
971: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
972: return (KW_TYPE_AUTHOR);
973: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
974: return (KW_TYPE_DATE);
975: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
976: return (KW_TYPE_STATE);
977: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
978: return (KW_TYPE_REVISION);
979: else
980: return (-1);
981: }
982:
983: /*
984: * checkin_parsekeyword()
985: *
986: * Do the actual parsing of an RCS keyword string, setting the values passed
987: * to the function to whatever is found.
988: *
989: */
990: static void
991: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
992: char **author, char **state)
993: {
994: char *tokens[10], *p, *datestring;
995: size_t len = 0;
1.99 niallo 996: int i = 0;
1.98 niallo 997: /* Parse data out of the expanded keyword */
998: switch (checkin_keywordtype(keystring)) {
999: case KW_TYPE_ID:
1.101 niallo 1000: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1001: (p = strtok(NULL, " "))) {
1.99 niallo 1002: if (i < KW_NUMTOKS_ID - 1)
1003: tokens[i++] = p;
1.98 niallo 1004: }
1.99 niallo 1005: tokens[i] = NULL;
1.98 niallo 1006: if (*author != NULL)
1007: xfree(*author);
1008: if (*state != NULL)
1009: xfree(*state);
1010: /* only parse revision if one is not already set */
1011: if (*rev == NULL) {
1012: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1013: fatal("could not parse rcsnum");
1014: }
1015: len = strlen(tokens[5]) + 1;
1016: *author = xmalloc(len);
1017: strlcpy(*author, tokens[5], len);
1018: len = strlen(tokens[6]) + 1;
1019: *state = xmalloc(len);
1020: strlcpy(*state, tokens[6], len);
1021: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
1022: datestring = xmalloc(len);
1023: strlcpy(datestring, tokens[3], len);
1024: strlcat(datestring, " ", len);
1025: strlcat(datestring, tokens[4], len);
1026: if ((*date = cvs_date_parse(datestring)) <= 0)
1027: fatal("could not parse date\n");
1028: xfree(datestring);
1029: break;
1030: case KW_TYPE_AUTHOR:
1.101 niallo 1031: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1032: (p = strtok(NULL, " "))) {
1.99 niallo 1033: if (i < KW_NUMTOKS_AUTHOR - 1)
1034: tokens[i++] = p;
1.98 niallo 1035: }
1036: if (*author != NULL)
1037: xfree(*author);
1038: len = strlen(tokens[1]) + 1;
1039: *author = xmalloc(len);
1040: strlcpy(*author, tokens[1], len);
1041: break;
1042: case KW_TYPE_DATE:
1.101 niallo 1043: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1044: (p = strtok(NULL, " "))) {
1.99 niallo 1045: if (i < KW_NUMTOKS_DATE - 1)
1046: tokens[i++] = p;
1.98 niallo 1047: }
1048: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
1049: datestring = xmalloc(len);
1050: strlcpy(datestring, tokens[1], len);
1051: strlcat(datestring, " ", len);
1052: strlcat(datestring, tokens[2], len);
1053: if ((*date = cvs_date_parse(datestring)) <= 0)
1054: fatal("could not parse date\n");
1055: xfree(datestring);
1056: break;
1057: case KW_TYPE_STATE:
1.101 niallo 1058: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1059: (p = strtok(NULL, " "))) {
1.99 niallo 1060: if (i < KW_NUMTOKS_STATE - 1)
1061: tokens[i++] = p;
1.98 niallo 1062: }
1063: if (*state != NULL)
1064: xfree(*state);
1065: len = strlen(tokens[1]) + 1;
1066: *state = xmalloc(len);
1067: strlcpy(*state, tokens[1], len);
1068: break;
1069: case KW_TYPE_REVISION:
1070: /* only parse revision if one is not already set */
1071: if (*rev != NULL)
1072: break;
1.102 niallo 1073: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1074: (p = strtok(NULL, " "))) {
1.99 niallo 1075: if (i < KW_NUMTOKS_REVISION - 1)
1076: tokens[i++] = p;
1.98 niallo 1077: }
1078: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1079: fatal("could not parse rcsnum");
1080: break;
1081: }
1.1 niallo 1082: }