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