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