Annotation of src/usr.bin/rcs/ci.c, Revision 1.103
1.103 ! niallo 1: /* $OpenBSD: ci.c,v 1.102 2006/02/20 16:30:18 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.73 xsa 463: /* Load file contents */
1.63 niallo 464: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
465: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
466: return (-1);
467: }
468:
469: if (cvs_buf_putc(bp, '\0') < 0)
470: return (-1);
471:
472: filec = (char *)cvs_buf_release(bp);
473:
1.73 xsa 474: /* Get RCS patch */
1.63 niallo 475: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
476: cvs_log(LP_ERR, "failed to get diff");
477: return (-1);
478: }
479:
480: /*
481: * If -f is not specified and there are no differences, tell
482: * the user and revert to latest version.
483: */
484: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
485: checkin_revert(pb);
486: return (0);
487: }
488:
1.73 xsa 489: /* If no log message specified, get it interactively. */
1.63 niallo 490: if (pb->flags & INTERACTIVE)
491: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
492:
1.82 joris 493: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 494: if (rcs_errno != RCS_ERR_NOENT)
1.82 joris 495: cvs_log(LP_WARN, "failed to remove lock");
496: else if (!(pb->flags & CO_LOCK))
497: cvs_log(LP_WARN, "previous revision was not locked; "
498: "ignoring -l option");
1.63 niallo 499: }
500:
1.73 xsa 501: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 502: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
503: fatal("failed to set new rd_text for head rev");
1.63 niallo 504:
505: /*
506: * Set the date of the revision to be the last modification
507: * time of the working file if -d has no argument.
508: */
509: if (pb->date == DATE_MTIME
510: && (checkin_mtimedate(pb) < 0))
511: return (-1);
512:
1.73 xsa 513: /* Now add our new revision */
1.63 niallo 514: if (rcs_rev_add(pb->file,
515: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 516: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 517: cvs_log(LP_ERR, "failed to add new revision");
518: return (-1);
519: }
520:
521: /*
522: * If we are checking in to a non-default (ie user-specified)
523: * revision, set head to this revision.
524: */
525: if (pb->newrev != NULL)
526: rcs_head_set(pb->file, pb->newrev);
527: else
528: pb->newrev = pb->file->rf_head;
529:
1.73 xsa 530: /* New head revision has to contain entire file; */
1.87 xsa 531: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
532: fatal("failed to set new head revision");
1.63 niallo 533:
1.73 xsa 534: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 535: if (pb->symbol != NULL
536: && (checkin_attach_symbol(pb) < 0))
537: return (-1);
538:
1.73 xsa 539: /* Set the state of this revision if specified. */
1.63 niallo 540: if (pb->state != NULL)
541: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
542:
1.84 joris 543: xfree(pb->deltatext);
544: xfree(filec);
1.63 niallo 545: (void)unlink(pb->filename);
546:
1.73 xsa 547: /* Do checkout if -u or -l are specified. */
1.63 niallo 548: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
549: && !(pb->flags & CI_DEFAULT))
550: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 551: pb->username, pb->author, NULL, NULL);
1.63 niallo 552:
553: /* File will NOW be synced */
554: rcs_close(pb->file);
555:
556: if (pb->flags & INTERACTIVE) {
1.84 joris 557: xfree(pb->rcs_msg);
1.63 niallo 558: pb->rcs_msg = NULL;
559: }
560: return (0);
561: }
562:
563: /*
1.58 niallo 564: * checkin_init()
1.65 xsa 565: *
1.58 niallo 566: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 567: * On success, return 0. On error return -1.
1.58 niallo 568: */
1.63 niallo 569: static int
1.58 niallo 570: checkin_init(struct checkin_params *pb)
571: {
1.80 niallo 572: BUF *bp, *dp;
1.93 xsa 573: char *filec, numb[64];
1.80 niallo 574: const char *rcs_desc;
1.58 niallo 575:
1.73 xsa 576: /* Load file contents */
1.58 niallo 577: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
578: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63 niallo 579: return (-1);
1.58 niallo 580: }
581:
582: if (cvs_buf_putc(bp, '\0') < 0)
1.63 niallo 583: return (-1);
1.58 niallo 584:
585: filec = (char *)cvs_buf_release(bp);
586:
1.98 niallo 587: /* Get default values from working copy if -k specified */
588: if (pb->flags & CI_KEYWORDSCAN)
589: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
590: &pb->author);
591:
1.73 xsa 592: /* Get description from user */
1.80 niallo 593: if (pb->description == NULL)
594: rcs_desc = (const char *)checkin_getdesc();
595: else {
596: if (*pb->description == '-') {
597: pb->description++;
1.81 niallo 598: rcs_desc = (const char *)pb->description;
599: } else {
1.80 niallo 600: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
601: if (dp == NULL) {
602: cvs_log(LP_ERR,
603: "failed to load description file '%s'",
604: pb->description);
1.84 joris 605: xfree(filec);
1.80 niallo 606: return (-1);
607: }
608: if (cvs_buf_putc(dp, '\0') < 0) {
1.84 joris 609: xfree(filec);
1.80 niallo 610: return (-1);
611: }
612: rcs_desc = (const char *)cvs_buf_release(dp);
613: }
614: }
1.58 niallo 615: rcs_desc_set(pb->file, rcs_desc);
616:
1.97 niallo 617: /*
618: * Set the date of the revision to be the last modification
619: * time of the working file if -d has no argument.
620: */
621: if (pb->date == DATE_MTIME
622: && (checkin_mtimedate(pb) < 0))
623: return (-1);
624:
1.73 xsa 625: /* Now add our new revision */
1.96 niallo 626: if (rcs_rev_add(pb->file,
627: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.103 ! niallo 628: (pb->rcs_msg == NULL ? LOG_INIT : pb->rcs_msg),
! 629: pb->date, pb->author) != 0) {
1.58 niallo 630: cvs_log(LP_ERR, "failed to add new revision");
1.63 niallo 631: return (-1);
632: }
633: /*
634: * If we are checking in to a non-default (ie user-specified)
635: * revision, set head to this revision.
636: */
637: if (pb->newrev != NULL)
638: rcs_head_set(pb->file, pb->newrev);
639: else
640: pb->newrev = pb->file->rf_head;
641:
1.73 xsa 642: /* New head revision has to contain entire file; */
1.66 niallo 643: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 644: cvs_log(LP_ERR, "failed to set new head revision");
645: return (-1);
1.58 niallo 646: }
1.73 xsa 647: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 648: if (pb->symbol != NULL
649: && (checkin_attach_symbol(pb) < 0))
650: return (-1);
651:
1.73 xsa 652: /* Set the state of this revision if specified. */
1.66 niallo 653: if (pb->state != NULL)
654: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
655:
1.84 joris 656: xfree(filec);
1.66 niallo 657: (void)unlink(pb->filename);
658:
1.73 xsa 659: /* Do checkout if -u or -l are specified. */
1.66 niallo 660: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
661: && !(pb->flags & CI_DEFAULT))
662: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 663: pb->username, pb->author, NULL, NULL);
1.63 niallo 664:
1.93 xsa 665: printf("initial revision: %s\n",
666: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
667:
1.66 niallo 668: /* File will NOW be synced */
669: rcs_close(pb->file);
1.93 xsa 670:
1.63 niallo 671: return (0);
1.58 niallo 672: }
673:
1.59 niallo 674: /*
675: * checkin_attach_symbol()
676: *
677: * Attempt to attach the specified symbol to the revision.
678: * On success, return 0. On error return -1.
679: */
1.58 niallo 680: static int
681: checkin_attach_symbol(struct checkin_params *pb)
682: {
683: char rbuf[16];
684: int ret;
685: if (verbose == 1)
686: printf("symbol: %s\n", pb->symbol);
687: if (pb->flags & CI_SYMFORCE)
688: rcs_sym_remove(pb->file, pb->symbol);
689: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
690: && (rcs_errno == RCS_ERR_DUPENT)) {
691: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
692: rbuf, sizeof(rbuf));
693: cvs_log(LP_ERR,
694: "symbolic name %s already bound to %s",
695: pb->symbol, rbuf);
696: rcs_close(pb->file);
697: return (-1);
698: } else if (ret == -1) {
699: cvs_log(LP_ERR, "problem adding symbol: %s",
700: pb->symbol);
701: rcs_close(pb->file);
702: return (-1);
703: }
704: return (0);
705: }
706:
1.59 niallo 707: /*
708: * checkin_revert()
709: *
710: * If there are no differences between the working file and the latest revision
711: * and the -f flag is not specified, simply revert to the latest version and
712: * warn the user.
713: *
714: */
1.58 niallo 715: static void
716: checkin_revert(struct checkin_params *pb)
717: {
718: char rbuf[16];
719:
720: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
721: cvs_log(LP_WARN,
722: "file is unchanged; reverting to previous revision %s",
723: rbuf);
724: (void)unlink(pb->filename);
725: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
726: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 727: pb->flags, pb->username, pb->author, NULL, NULL);
1.82 joris 728: rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58 niallo 729: rcs_close(pb->file);
730: }
731:
1.59 niallo 732: /*
733: * checkin_checklock()
734: *
735: * Check for the existence of a lock on the file. If there are no locks, or it
736: * is not locked by the correct user, return -1. Otherwise, return 0.
737: */
1.58 niallo 738: static int
739: checkin_checklock(struct checkin_params *pb)
740: {
741: struct rcs_lock *lkp;
742:
1.82 joris 743: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
744: if ((!strcmp(lkp->rl_name, pb->username)) &&
745: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
746: return (0);
1.58 niallo 747: }
1.32 joris 748:
1.82 joris 749: cvs_log(LP_ERR,
750: "%s: no lock set by %s", pb->file->rf_path, pb->username);
751: rcs_close(pb->file);
752: return (-1);
1.61 niallo 753: }
754:
755: /*
1.62 niallo 756: * checkin_mtimedate()
1.61 niallo 757: *
758: * Set the date of the revision to be the last modification
1.62 niallo 759: * time of the working file.
1.61 niallo 760: *
761: * On success, return 0. On error return -1.
762: */
763: static int
1.62 niallo 764: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 765: {
766: struct stat sb;
767: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 768: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 769: rcs_close(pb->file);
770: return (-1);
771: }
772: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 773: return (0);
1.63 niallo 774: }
775:
776: /*
1.75 niallo 777: * checkin_choose_rcsfile()
1.63 niallo 778: *
779: * Given a relative filename, decide where the corresponding ,v file
1.65 xsa 780: * should be.
1.63 niallo 781: *
782: * Returns pointer to a char array on success, NULL on failure.
783: */
784: static char *
785: checkin_choose_rcsfile(const char *filename)
786: {
1.76 niallo 787: char name[MAXPATHLEN], *basepath;
788: const char *ptr;
1.63 niallo 789: size_t len;
790: struct stat sb;
791:
1.84 joris 792: basepath = xmalloc(MAXPATHLEN);
1.88 alek 793: basepath[0] = '\0';
1.76 niallo 794: if (strchr(filename, '/') == NULL) {
795: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
796: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
797: /* <path>/RCS/<filename>,v */
798: strlcat(basepath, filename, MAXPATHLEN);
799: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
800: } else {
801: /* <path>/<filename>,v */
802: strlcpy(basepath, filename, MAXPATHLEN);
803: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
804: }
805: } else {
806: ptr = filename;
807: /* Walk backwards till we find the base directory */
808: len = strlen(filename);
809: ptr += len + 1;
810: while (filename[len] != '/') {
811: len--;
812: ptr--;
813: }
1.63 niallo 814: /*
815: * Need two bytes extra for trailing slash and
816: * NUL-termination.
817: */
818: len += 2;
1.76 niallo 819: if (len > MAXPATHLEN) {
1.84 joris 820: xfree(basepath);
1.63 niallo 821: return (NULL);
822: }
1.76 niallo 823: strlcpy(basepath, filename, len);
824: strlcpy(name, ptr, MAXPATHLEN);
1.63 niallo 825: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
826: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
827: /* <path>/RCS/<filename>,v */
1.76 niallo 828: strlcat(basepath, name, MAXPATHLEN);
1.63 niallo 829: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
830: } else {
831: /* <path>/<filename>,v */
1.76 niallo 832: strlcpy(basepath, filename, MAXPATHLEN);
1.63 niallo 833: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
834: }
1.76 niallo 835: }
836: return (basepath);
1.98 niallo 837: }
838:
839: /*
840: * checkin_keywordscan()
841: *
842: * Searches working file for keyword values to determine its revision
843: * number, creation date and author, and uses these values instead of
844: * calculating them locally.
845: *
846: * Params: The data buffer to scan and pointers to pointers of variables in
847: * which to store the outputs.
848: *
849: * On success, return 0. On error return -1.
850: */
851: static int
852: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
853: char **state)
854: {
855: size_t len;
856: u_int i, j, found, end;
857: char *c, *kwstr, *start, buf[128];
858:
859: c = start = kwstr = NULL;
860:
861: i = found = 0;
862:
863: len = strlen(data);
864: for (c = data; *c != '\0' && i < len; *c++) {
865: if (*c == '$') {
866: start = c;
867: *c++;
868: if (!isalpha(*c)) {
869: c = start;
870: continue;
871: }
872: /* look for any matching keywords */
873: found = 0;
874: for (j = 0; j < 10; j++) {
875: if (!strncmp(c, rcs_expkw[j].kw_str,
876: strlen(rcs_expkw[j].kw_str))) {
877: found = 1;
878: kwstr = rcs_expkw[j].kw_str;
879: break;
880: }
881: }
882:
883: /* unknown keyword, continue looking */
884: if (found == 0) {
885: c = start;
886: continue;
887: }
888:
889: c += strlen(kwstr);
890: if (*c != ':' && *c != '$') {
891: c = start;
892: continue;
893: }
894:
895: if (*c == ':') {
896: while (*c++) {
897: if (*c == '$') {
898: end = c - start + 2;
899: if (end >= sizeof(buf))
900: fatal("keyword buffer"
901: " too small!");
902: strlcpy(buf, start, end);
903: checkin_parsekeyword(buf, rev,
904: date, author, state);
905: break;
906: }
907: }
908:
909: if (*c != '$') {
910: c = start;
911: continue;
912: }
913: }
914: }
915: }
916: if (found == 0)
917: return (-1);
918: else
919: return (0);
920: }
921:
922: /*
923: * checkin_keywordtype()
924: *
925: * Given an RCS keyword string, determine what type of string it is.
926: * This enables us to know what data should be in it.
927: *
928: * Returns type on success, or -1 on failure.
929: */
930: static int
931: checkin_keywordtype(char *keystring)
932: {
933: char *p;
934:
935: p = keystring;
936: *p++;
937: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
938: return (KW_TYPE_ID);
939: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
940: return (KW_TYPE_AUTHOR);
941: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
942: return (KW_TYPE_DATE);
943: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
944: return (KW_TYPE_STATE);
945: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
946: return (KW_TYPE_REVISION);
947: else
948: return (-1);
949: }
950:
951: /*
952: * checkin_parsekeyword()
953: *
954: * Do the actual parsing of an RCS keyword string, setting the values passed
955: * to the function to whatever is found.
956: *
957: */
958: static void
959: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
960: char **author, char **state)
961: {
962: char *tokens[10], *p, *datestring;
963: size_t len = 0;
1.99 niallo 964: int i = 0;
1.98 niallo 965: /* Parse data out of the expanded keyword */
966: switch (checkin_keywordtype(keystring)) {
967: case KW_TYPE_ID:
1.101 niallo 968: for ((p = strtok(keystring, " ")); p;
1.98 niallo 969: (p = strtok(NULL, " "))) {
1.99 niallo 970: if (i < KW_NUMTOKS_ID - 1)
971: tokens[i++] = p;
1.98 niallo 972: }
1.99 niallo 973: tokens[i] = NULL;
1.98 niallo 974: if (*author != NULL)
975: xfree(*author);
976: if (*state != NULL)
977: xfree(*state);
978: /* only parse revision if one is not already set */
979: if (*rev == NULL) {
980: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
981: fatal("could not parse rcsnum");
982: }
983: len = strlen(tokens[5]) + 1;
984: *author = xmalloc(len);
985: strlcpy(*author, tokens[5], len);
986: len = strlen(tokens[6]) + 1;
987: *state = xmalloc(len);
988: strlcpy(*state, tokens[6], len);
989: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
990: datestring = xmalloc(len);
991: strlcpy(datestring, tokens[3], len);
992: strlcat(datestring, " ", len);
993: strlcat(datestring, tokens[4], len);
994: if ((*date = cvs_date_parse(datestring)) <= 0)
995: fatal("could not parse date\n");
996: xfree(datestring);
997: break;
998: case KW_TYPE_AUTHOR:
1.101 niallo 999: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1000: (p = strtok(NULL, " "))) {
1.99 niallo 1001: if (i < KW_NUMTOKS_AUTHOR - 1)
1002: tokens[i++] = p;
1.98 niallo 1003: }
1004: if (*author != NULL)
1005: xfree(*author);
1006: len = strlen(tokens[1]) + 1;
1007: *author = xmalloc(len);
1008: strlcpy(*author, tokens[1], len);
1009: break;
1010: case KW_TYPE_DATE:
1.101 niallo 1011: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1012: (p = strtok(NULL, " "))) {
1.99 niallo 1013: if (i < KW_NUMTOKS_DATE - 1)
1014: tokens[i++] = p;
1.98 niallo 1015: }
1016: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
1017: datestring = xmalloc(len);
1018: strlcpy(datestring, tokens[1], len);
1019: strlcat(datestring, " ", len);
1020: strlcat(datestring, tokens[2], len);
1021: if ((*date = cvs_date_parse(datestring)) <= 0)
1022: fatal("could not parse date\n");
1023: xfree(datestring);
1024: break;
1025: case KW_TYPE_STATE:
1.101 niallo 1026: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1027: (p = strtok(NULL, " "))) {
1.99 niallo 1028: if (i < KW_NUMTOKS_STATE - 1)
1029: tokens[i++] = p;
1.98 niallo 1030: }
1031: if (*state != NULL)
1032: xfree(*state);
1033: len = strlen(tokens[1]) + 1;
1034: *state = xmalloc(len);
1035: strlcpy(*state, tokens[1], len);
1036: break;
1037: case KW_TYPE_REVISION:
1038: /* only parse revision if one is not already set */
1039: if (*rev != NULL)
1040: break;
1.102 niallo 1041: for ((p = strtok(keystring, " ")); p;
1.98 niallo 1042: (p = strtok(NULL, " "))) {
1.99 niallo 1043: if (i < KW_NUMTOKS_REVISION - 1)
1044: tokens[i++] = p;
1.98 niallo 1045: }
1046: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1047: fatal("could not parse rcsnum");
1048: break;
1049: }
1050:
1051: return;
1.1 niallo 1052: }