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