Annotation of src/usr.bin/rcs/ci.c, Revision 1.110
1.110 ! joris 1: /* $OpenBSD: ci.c,v 1.109 2006/03/06 15:04:17 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.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"
91: " [-wusername] [-xsuffixes] 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.4 niallo 274: }
275:
1.93 xsa 276: if (verbose == 1)
277: printf("done\n");
278:
1.16 niallo 279: return (status);
1.4 niallo 280: }
281:
1.59 niallo 282: /*
283: * checkin_diff_file()
284: *
1.65 xsa 285: * Generate the diff between the working file and a revision.
1.59 niallo 286: * Returns pointer to a char array on success, NULL on failure.
287: */
1.4 niallo 288: static char *
1.58 niallo 289: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 290: {
291: char path1[MAXPATHLEN], path2[MAXPATHLEN];
292: BUF *b1, *b2, *b3;
293: char rbuf[64], *deltatext;
294:
1.58 niallo 295: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4 niallo 296:
1.58 niallo 297: if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
298: cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4 niallo 299: return (NULL);
1.1 niallo 300: }
301:
1.58 niallo 302: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4 niallo 303: cvs_log(LP_ERR, "failed to load revision");
304: cvs_buf_free(b1);
305: return (NULL);
306: }
307:
1.57 xsa 308: if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4 niallo 309: cvs_log(LP_ERR, "failed to allocated buffer for diff");
310: cvs_buf_free(b1);
311: cvs_buf_free(b2);
312: return (NULL);
313: }
314:
1.50 xsa 315: strlcpy(path1, rcs_tmpdir, sizeof(path1));
316: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 317: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
318: cvs_log(LP_ERRNO, "could not write temporary file");
319: cvs_buf_free(b1);
320: cvs_buf_free(b2);
321: return (NULL);
322: }
323: cvs_buf_free(b1);
324:
1.50 xsa 325: strlcpy(path2, rcs_tmpdir, sizeof(path2));
326: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 327: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
328: cvs_buf_free(b2);
329: (void)unlink(path1);
330: return (NULL);
331: }
332: cvs_buf_free(b2);
333:
1.5 niallo 334: diff_format = D_RCSDIFF;
1.4 niallo 335: cvs_diffreg(path1, path2, b3);
336: (void)unlink(path1);
337: (void)unlink(path2);
338:
339: cvs_buf_putc(b3, '\0');
340: deltatext = (char *)cvs_buf_release(b3);
341:
342: return (deltatext);
1.6 niallo 343: }
344:
345: /*
1.65 xsa 346: * checkin_getlogmsg()
1.59 niallo 347: *
1.6 niallo 348: * Get log message from user interactively.
1.59 niallo 349: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 350: */
351: static char *
1.34 niallo 352: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 353: {
1.58 niallo 354: char *rcs_msg, nrev[16], prev[16];
1.6 niallo 355: RCSNUM *tmprev;
356:
1.7 niallo 357: rcs_msg = NULL;
1.6 niallo 358: tmprev = rcsnum_alloc();
359: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 360: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 361: if (rev2 == NULL)
362: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
363: else
364: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 365: rcsnum_free(tmprev);
366:
1.47 niallo 367: if (verbose == 1)
368: printf("new revision: %s; previous revision: %s\n", nrev,
369: prev);
1.32 joris 370:
1.107 deraadt 371: rcs_msg = checkin_getinput("enter log message, terminated with a "
372: "single '.' or end of file:\n>> ");
1.58 niallo 373: return (rcs_msg);
374: }
375:
376:
377: /*
378: * checkin_getdesc()
379: *
380: * Get file description interactively.
1.59 niallo 381: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 382: */
383: static char *
384: checkin_getdesc()
385: {
386: char *description;
387:
1.107 deraadt 388: description = checkin_getinput("enter description, terminated with "
389: "single '.' or end of file:\n"
390: "NOTE: This is NOT the log message!\n>> ");
1.58 niallo 391: return (description);
392: }
393:
394: /*
395: * checkin_getinput()
396: *
1.59 niallo 397: * Get some input from the user, in RCS style, prompting with message <prompt>.
398: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 399: */
400: static char *
401: checkin_getinput(const char *prompt)
402: {
403: BUF *inputbuf;
404: char *input, buf[128];
405:
406: if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
407: cvs_log(LP_ERR, "failed to allocate input buffer");
408: return (NULL);
409: }
410:
411: printf(prompt);
1.6 niallo 412: for (;;) {
413: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 414: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 415: break;
1.58 niallo 416: cvs_buf_append(inputbuf, buf, strlen(buf));
1.46 joris 417: printf(">> ");
1.6 niallo 418: }
1.32 joris 419:
1.58 niallo 420: cvs_buf_putc(inputbuf, '\0');
421: input = (char *)cvs_buf_release(inputbuf);
422:
423: return (input);
424: }
425:
426: /*
1.63 niallo 427: * checkin_update()
428: *
429: * Do a checkin to an existing RCS file.
430: *
431: * On success, return 0. On error return -1.
432: */
433: static int
434: checkin_update(struct checkin_params *pb)
435: {
1.77 xsa 436: char *filec, numb1[64], numb2[64];
1.63 niallo 437: BUF *bp;
438:
1.82 joris 439: /*
440: * XXX this is wrong, we need to get the revision the user
1.85 xsa 441: * has the lock for. So we can decide if we want to create a
1.82 joris 442: * branch or not. (if it's not current HEAD we need to branch).
443: */
1.63 niallo 444: pb->frev = pb->file->rf_head;
445:
1.82 joris 446: if (checkin_checklock(pb) < 0)
447: return (-1);
448:
449: /* If revision passed on command line is less than HEAD, bail.
450: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
451: * there is no lock set for the user.
452: */
1.63 niallo 453: if ((pb->newrev != NULL)
454: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 455: cvs_log(LP_ERR,
456: "%s: revision %s too low; must be higher than %s",
457: pb->file->rf_path,
458: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
459: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63 niallo 460: rcs_close(pb->file);
461: return (-1);
462: }
463:
1.104 niallo 464: /*
465: * Set the date of the revision to be the last modification
466: * time of the working file if -d has no argument.
467: */
468: if (pb->date == DATE_MTIME
469: && (checkin_mtimedate(pb) < 0))
470: return (-1);
471:
472: /* Date from argv/mtime must be more recent than HEAD */
473: if (pb->date != DATE_NOW) {
474: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
475: if (pb->date <= head_date) {
476: char *head_date_str, *tdate;
477: head_date_str = xstrdup(ctime(&head_date));
478: head_date_str[strlen(head_date_str) - 1] = '\0';
479: tdate = xstrdup(ctime(&pb->date));
480: tdate[strlen(tdate) - 1] = '\0';
481: cvs_log(LP_ERR,
482: "%s: Date %s preceeds %s in revision %s",
483: pb->file->rf_path, tdate, head_date_str,
484: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
485: rcs_close(pb->file);
486: xfree(head_date_str);
487: xfree(tdate);
488: return (-1);
489: }
490: }
491:
1.73 xsa 492: /* Load file contents */
1.63 niallo 493: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
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.107 deraadt 556: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
1.87 xsa 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) {
1.106 niallo 603: rcs_close(pb->file);
1.63 niallo 604: return (-1);
1.58 niallo 605: }
606:
1.106 niallo 607: if (cvs_buf_putc(bp, '\0') < 0) {
608: rcs_close(pb->file);
1.63 niallo 609: return (-1);
1.106 niallo 610: }
1.58 niallo 611:
612: filec = (char *)cvs_buf_release(bp);
613:
1.98 niallo 614: /* Get default values from working copy if -k specified */
615: if (pb->flags & CI_KEYWORDSCAN)
616: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
617: &pb->author);
618:
1.73 xsa 619: /* Get description from user */
1.80 niallo 620: if (pb->description == NULL)
621: rcs_desc = (const char *)checkin_getdesc();
622: else {
623: if (*pb->description == '-') {
624: pb->description++;
1.81 niallo 625: rcs_desc = (const char *)pb->description;
626: } else {
1.80 niallo 627: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
628: if (dp == NULL) {
629: cvs_log(LP_ERR,
630: "failed to load description file '%s'",
631: pb->description);
1.84 joris 632: xfree(filec);
1.106 niallo 633: rcs_close(pb->file);
1.80 niallo 634: return (-1);
635: }
636: if (cvs_buf_putc(dp, '\0') < 0) {
1.84 joris 637: xfree(filec);
1.106 niallo 638: rcs_close(pb->file);
1.80 niallo 639: return (-1);
640: }
641: rcs_desc = (const char *)cvs_buf_release(dp);
642: }
643: }
1.58 niallo 644: rcs_desc_set(pb->file, rcs_desc);
645:
1.97 niallo 646: /*
647: * Set the date of the revision to be the last modification
648: * time of the working file if -d has no argument.
649: */
650: if (pb->date == DATE_MTIME
651: && (checkin_mtimedate(pb) < 0))
652: return (-1);
653:
1.73 xsa 654: /* Now add our new revision */
1.96 niallo 655: if (rcs_rev_add(pb->file,
656: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 657: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 658: pb->date, pb->author) != 0) {
1.58 niallo 659: cvs_log(LP_ERR, "failed to add new revision");
1.106 niallo 660: rcs_close(pb->file);
1.63 niallo 661: return (-1);
662: }
663: /*
664: * If we are checking in to a non-default (ie user-specified)
665: * revision, set head to this revision.
666: */
667: if (pb->newrev != NULL)
668: rcs_head_set(pb->file, pb->newrev);
669: else
670: pb->newrev = pb->file->rf_head;
671:
1.73 xsa 672: /* New head revision has to contain entire file; */
1.66 niallo 673: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 674: cvs_log(LP_ERR, "failed to set new head revision");
1.106 niallo 675: rcs_close(pb->file);
1.63 niallo 676: return (-1);
1.58 niallo 677: }
1.73 xsa 678: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 679: if (pb->symbol != NULL
680: && (checkin_attach_symbol(pb) < 0))
681: return (-1);
682:
1.73 xsa 683: /* Set the state of this revision if specified. */
1.66 niallo 684: if (pb->state != NULL)
685: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
686:
1.84 joris 687: xfree(filec);
1.66 niallo 688: (void)unlink(pb->filename);
689:
1.73 xsa 690: /* Do checkout if -u or -l are specified. */
1.66 niallo 691: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
692: && !(pb->flags & CI_DEFAULT))
693: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 694: pb->username, pb->author, NULL, NULL);
1.63 niallo 695:
1.93 xsa 696: printf("initial revision: %s\n",
697: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
698:
1.66 niallo 699: /* File will NOW be synced */
700: rcs_close(pb->file);
1.93 xsa 701:
1.63 niallo 702: return (0);
1.58 niallo 703: }
704:
1.59 niallo 705: /*
706: * checkin_attach_symbol()
707: *
708: * Attempt to attach the specified symbol to the revision.
709: * On success, return 0. On error return -1.
710: */
1.58 niallo 711: static int
712: checkin_attach_symbol(struct checkin_params *pb)
713: {
714: char rbuf[16];
715: int ret;
716: if (verbose == 1)
717: printf("symbol: %s\n", pb->symbol);
718: if (pb->flags & CI_SYMFORCE)
719: rcs_sym_remove(pb->file, pb->symbol);
720: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
721: && (rcs_errno == RCS_ERR_DUPENT)) {
722: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
723: rbuf, sizeof(rbuf));
724: cvs_log(LP_ERR,
725: "symbolic name %s already bound to %s",
726: pb->symbol, rbuf);
727: rcs_close(pb->file);
728: return (-1);
729: } else if (ret == -1) {
730: cvs_log(LP_ERR, "problem adding symbol: %s",
731: pb->symbol);
732: rcs_close(pb->file);
733: return (-1);
734: }
735: return (0);
736: }
737:
1.59 niallo 738: /*
739: * checkin_revert()
740: *
741: * If there are no differences between the working file and the latest revision
742: * and the -f flag is not specified, simply revert to the latest version and
743: * warn the user.
744: *
745: */
1.58 niallo 746: static void
747: checkin_revert(struct checkin_params *pb)
748: {
749: char rbuf[16];
750:
751: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
752: cvs_log(LP_WARN,
753: "file is unchanged; reverting to previous revision %s",
754: rbuf);
755: (void)unlink(pb->filename);
756: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
757: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 758: pb->flags, pb->username, pb->author, NULL, NULL);
1.82 joris 759: rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58 niallo 760: rcs_close(pb->file);
761: }
762:
1.59 niallo 763: /*
764: * checkin_checklock()
765: *
766: * Check for the existence of a lock on the file. If there are no locks, or it
767: * is not locked by the correct user, return -1. Otherwise, return 0.
768: */
1.58 niallo 769: static int
770: checkin_checklock(struct checkin_params *pb)
771: {
772: struct rcs_lock *lkp;
773:
1.82 joris 774: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
775: if ((!strcmp(lkp->rl_name, pb->username)) &&
776: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
777: return (0);
1.58 niallo 778: }
1.32 joris 779:
1.82 joris 780: cvs_log(LP_ERR,
781: "%s: no lock set by %s", pb->file->rf_path, pb->username);
782: rcs_close(pb->file);
783: return (-1);
1.61 niallo 784: }
785:
786: /*
1.62 niallo 787: * checkin_mtimedate()
1.61 niallo 788: *
789: * Set the date of the revision to be the last modification
1.62 niallo 790: * time of the working file.
1.61 niallo 791: *
792: * On success, return 0. On error return -1.
793: */
794: static int
1.62 niallo 795: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 796: {
797: struct stat sb;
798: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 799: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 800: rcs_close(pb->file);
801: return (-1);
802: }
803: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 804: return (0);
1.63 niallo 805: }
806:
807: /*
1.75 niallo 808: * checkin_choose_rcsfile()
1.63 niallo 809: *
810: * Given a relative filename, decide where the corresponding ,v file
1.65 xsa 811: * should be.
1.63 niallo 812: *
813: * Returns pointer to a char array on success, NULL on failure.
814: */
815: static char *
816: checkin_choose_rcsfile(const char *filename)
817: {
1.76 niallo 818: char name[MAXPATHLEN], *basepath;
819: const char *ptr;
1.63 niallo 820: size_t len;
821: struct stat sb;
822:
1.84 joris 823: basepath = xmalloc(MAXPATHLEN);
1.88 alek 824: basepath[0] = '\0';
1.76 niallo 825: if (strchr(filename, '/') == NULL) {
826: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
827: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
828: /* <path>/RCS/<filename>,v */
829: strlcat(basepath, filename, MAXPATHLEN);
830: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
831: } else {
832: /* <path>/<filename>,v */
833: strlcpy(basepath, filename, MAXPATHLEN);
834: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
835: }
836: } else {
837: ptr = filename;
838: /* Walk backwards till we find the base directory */
839: len = strlen(filename);
840: ptr += len + 1;
841: while (filename[len] != '/') {
842: len--;
843: ptr--;
844: }
1.63 niallo 845: /*
846: * Need two bytes extra for trailing slash and
847: * NUL-termination.
848: */
849: len += 2;
1.76 niallo 850: if (len > MAXPATHLEN) {
1.84 joris 851: xfree(basepath);
1.63 niallo 852: return (NULL);
853: }
1.76 niallo 854: strlcpy(basepath, filename, len);
855: strlcpy(name, ptr, MAXPATHLEN);
1.63 niallo 856: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
857: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
858: /* <path>/RCS/<filename>,v */
1.76 niallo 859: strlcat(basepath, name, MAXPATHLEN);
1.63 niallo 860: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
861: } else {
862: /* <path>/<filename>,v */
1.76 niallo 863: strlcpy(basepath, filename, MAXPATHLEN);
1.63 niallo 864: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
865: }
1.76 niallo 866: }
867: return (basepath);
1.98 niallo 868: }
869:
870: /*
871: * checkin_keywordscan()
872: *
873: * Searches working file for keyword values to determine its revision
874: * number, creation date and author, and uses these values instead of
875: * calculating them locally.
876: *
877: * Params: The data buffer to scan and pointers to pointers of variables in
878: * which to store the outputs.
879: *
880: * On success, return 0. On error return -1.
881: */
882: static int
883: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
884: char **state)
885: {
886: size_t len;
887: u_int i, j, found, end;
888: char *c, *kwstr, *start, buf[128];
889:
890: c = start = kwstr = NULL;
891:
892: i = found = 0;
893:
894: len = strlen(data);
895: for (c = data; *c != '\0' && i < len; *c++) {
896: if (*c == '$') {
897: start = c;
898: *c++;
899: if (!isalpha(*c)) {
900: c = start;
901: continue;
902: }
903: /* look for any matching keywords */
1.107 deraadt 904: found = 0;
905: for (j = 0; j < 10; j++) {
906: if (!strncmp(c, rcs_expkw[j].kw_str,
907: strlen(rcs_expkw[j].kw_str))) {
908: found = 1;
909: kwstr = rcs_expkw[j].kw_str;
910: break;
911: }
912: }
913:
914: /* unknown keyword, continue looking */
915: if (found == 0) {
916: c = start;
917: continue;
918: }
1.98 niallo 919:
920: c += strlen(kwstr);
921: if (*c != ':' && *c != '$') {
922: c = start;
923: continue;
924: }
925:
926: if (*c == ':') {
927: while (*c++) {
928: if (*c == '$') {
929: end = c - start + 2;
930: if (end >= sizeof(buf))
931: fatal("keyword buffer"
932: " too small!");
933: strlcpy(buf, start, end);
934: checkin_parsekeyword(buf, rev,
1.107 deraadt 935: date, author, state);
1.98 niallo 936: break;
937: }
938: }
939:
940: if (*c != '$') {
941: c = start;
942: continue;
943: }
944: }
945: }
946: }
947: if (found == 0)
948: return (-1);
949: else
950: return (0);
951: }
952:
953: /*
954: * checkin_keywordtype()
955: *
956: * Given an RCS keyword string, determine what type of string it is.
957: * This enables us to know what data should be in it.
958: *
959: * Returns type on success, or -1 on failure.
1.107 deraadt 960: */
1.98 niallo 961: static int
962: checkin_keywordtype(char *keystring)
963: {
964: char *p;
1.107 deraadt 965:
1.98 niallo 966: p = keystring;
967: *p++;
968: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
969: return (KW_TYPE_ID);
970: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
971: return (KW_TYPE_AUTHOR);
972: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
973: return (KW_TYPE_DATE);
974: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
975: return (KW_TYPE_STATE);
976: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
977: return (KW_TYPE_REVISION);
978: else
979: return (-1);
980: }
981:
982: /*
983: * checkin_parsekeyword()
984: *
985: * Do the actual parsing of an RCS keyword string, setting the values passed
986: * to the function to whatever is found.
987: *
988: */
989: static void
990: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
991: char **author, char **state)
992: {
993: char *tokens[10], *p, *datestring;
994: size_t len = 0;
1.99 niallo 995: int i = 0;
1.107 deraadt 996:
997: /* Parse data out of the expanded keyword */
1.98 niallo 998: switch (checkin_keywordtype(keystring)) {
999: case KW_TYPE_ID:
1.101 niallo 1000: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1001: (p = strtok(NULL, " "))) {
1.99 niallo 1002: if (i < KW_NUMTOKS_ID - 1)
1003: tokens[i++] = p;
1.107 deraadt 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.107 deraadt 1032: (p = strtok(NULL, " "))) {
1.99 niallo 1033: if (i < KW_NUMTOKS_AUTHOR - 1)
1034: tokens[i++] = p;
1.107 deraadt 1035: }
1.98 niallo 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.107 deraadt 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.107 deraadt 1059: (p = strtok(NULL, " "))) {
1.99 niallo 1060: if (i < KW_NUMTOKS_STATE - 1)
1061: tokens[i++] = p;
1.107 deraadt 1062: }
1.98 niallo 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.107 deraadt 1074: (p = strtok(NULL, " "))) {
1.99 niallo 1075: if (i < KW_NUMTOKS_REVISION - 1)
1076: tokens[i++] = p;
1.107 deraadt 1077: }
1.98 niallo 1078: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1079: fatal("could not parse rcsnum");
1080: break;
1081: }
1.1 niallo 1082: }