summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/cvs.c
blob: ac20d6d6782dd4c019f091a4877c725b1d5d0225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#define DEBUG
/*	$OpenBSD: cvs.c,v 1.4 2004/07/26 15:56:42 jfb Exp $	*/
/*
 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 *
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include <sys/types.h>
#include <sys/wait.h>

#include <err.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sysexits.h>

#include "cvs.h"
#include "log.h"


extern char *__progname;


/* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
int verbosity = 2;



/* compression level used with zlib, 0 meaning no compression taking place */
int   cvs_compress = 0;
int   cvs_trace = 0;
int   cvs_nolog = 0;
int   cvs_readonly = 0;

/* name of the command we are running */
char *cvs_command;
char *cvs_rootstr;
char *cvs_rsh = CVS_RSH_DEFAULT;
char *cvs_editor = CVS_EDITOR_DEFAULT;

struct cvsroot *cvs_root = NULL;


/*
 * Command dispatch table
 * ----------------------
 *
 * The synopsis field should only contain the list of arguments that the
 * command supports, without the actual command's name.
 *
 * Command handlers are expected to return 0 if no error occured, or one of
 * the values known in sysexits.h in case of an error.  In case the error
 * returned is EX_USAGE, the command's usage string is printed to standard
 * error before returning.
 */

static struct cvs_cmd {
	char    cmd_name[CVS_CMD_MAXNAMELEN];
	char    cmd_alias[CVS_CMD_MAXALIAS][CVS_CMD_MAXNAMELEN];
	int   (*cmd_hdlr)(int, char **);
	char   *cmd_synopsis;
	char    cmd_descr[CVS_CMD_MAXDESCRLEN];
} cvs_cdt[] = {
	{
		"add",      { "ad",  "new" }, cvs_add,
		"[-m msg] file ...",
		"Add a new file/directory to the repository",
	},
	{
		"admin",    { "adm", "rcs" }, NULL,
		"",
		"Administration front end for rcs",
	},
	{
		"annotate", { "ann"        }, NULL,
		"",
		"Show last revision where each line was modified",
	},
	{
		"checkout", { "co",  "get" }, cvs_checkout,
		"",
		"Checkout sources for editing",
	},
	{
		"commit",   { "ci",  "com" }, cvs_commit,
		"[-flR] [-F logfile | -m msg] [-r rev] ...",
		"Check files into the repository",
	},
	{
		"diff",     { "di",  "dif" }, cvs_diff,
		"[-cilu] [-D date] [-r rev] ...",
		"Show differences between revisions",
	},
	{
		"edit",     {              }, NULL,
		"",
		"Get ready to edit a watched file",
	},
	{
		"editors",  {              }, NULL,
		"",
		"See who is editing a watched file",
	},
	{
		"export",   { "ex",  "exp" }, NULL,
		"",
		"Export sources from CVS, similar to checkout",
	},
	{
		"history",  { "hi",  "his" }, cvs_history,
		"",
		"Show repository access history",
	},
	{
		"import",   { "im",  "imp" }, NULL,
		"",
		"Import sources into CVS, using vendor branches",
	},
	{
		"init",     {              }, cvs_init,
		"",
		"Create a CVS repository if it doesn't exist",
	},
#if defined(HAVE_KERBEROS)
	{
		"kserver",  {}, NULL
		"",
		"Start a Kerberos authentication CVS server",
	},
#endif
	{
		"log",      { "lo"         }, cvs_getlog,
		"",
		"Print out history information for files",
	},
	{
		"login",    {}, NULL,
		"",
		"Prompt for password for authenticating server",
	},
	{
		"logout",   {}, NULL,
		"",
		"Removes entry in .cvspass for remote repository",
	},
	{
		"rdiff",    {}, NULL,
		"",
		"Create 'patch' format diffs between releases",
	},
	{
		"release",  {}, NULL,
		"",
		"Indicate that a Module is no longer in use",
	},
	{
		"remove",   {}, NULL,
		"",
		"Remove an entry from the repository",
	},
	{
		"rlog",     {}, NULL,
		"",
		"Print out history information for a module",
	},
	{
		"rtag",     {}, NULL,
		"",
		"Add a symbolic tag to a module",
	},
	{
		"server",   {}, cvs_server,
		"",
		"Server mode",
	},
	{
		"status",   {}, NULL,
		"",
		"Display status information on checked out files",
	},
	{
		"tag",      { "ta", }, NULL,
		"",
		"Add a symbolic tag to checked out version of files",
	},
	{
		"unedit",   {}, NULL,
		"",
		"Undo an edit command",
	},
	{
		"update",   {}, cvs_update,
		"",
		"Bring work tree in sync with repository",
	},
	{
		"version",  {}, cvs_version,
		"",
		"Show current CVS version(s)",
	},
	{
		"watch",    {}, NULL,
		"",
		"Set watches",
	},
	{
		"watchers", {}, NULL,
		"",
		"See who is watching a file",
	},
};

#define CVS_NBCMD  (sizeof(cvs_cdt)/sizeof(cvs_cdt[0]))



void             usage        (void);
void             sigchld_hdlr (int);
void             cvs_readrc   (void);
struct cvs_cmd*  cvs_findcmd  (const char *); 



/*
 * sigchld_hdlr()
 *
 * Handler for the SIGCHLD signal, which can be received in case we are
 * running a remote server and it dies.
 */

void
sigchld_hdlr(int signo)
{
	int status;
	pid_t pid;

	if ((pid = wait(&status)) == -1) {
	}
}


/*
 * usage()
 *
 * Display usage information.
 */

void
usage(void)
{
	fprintf(stderr,
	    "Usage: %s [-lQqtv] [-d root] [-e editor] [-z level] "
	    "command [options] ...\n",
	    __progname);
}


int
main(int argc, char **argv)
{
	char *envstr, *ep;
	int ret;
	u_int i, readrc;
	struct cvs_cmd *cmdp;

	readrc = 1;

	if (cvs_log_init(LD_STD, 0) < 0)
		err(1, "failed to initialize logging");

	/* by default, be very verbose */
	(void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);

#ifdef DEBUG
	(void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
#endif

	/* check environment so command-line options override it */
	if ((envstr = getenv("CVS_RSH")) != NULL)
		cvs_rsh = envstr;

	if (((envstr = getenv("CVSEDITOR")) != NULL) ||
	    ((envstr = getenv("VISUAL")) != NULL) ||
	    ((envstr = getenv("EDITOR")) != NULL))
		cvs_editor = envstr;

	while ((ret = getopt(argc, argv, "d:e:fHlnQqrtvz:")) != -1) {
		switch (ret) {
		case 'd':
			cvs_rootstr = optarg;
			break;
		case 'e':
			cvs_editor = optarg;
			break;
		case 'f':
			readrc = 0;
			break;
		case 'l':
			cvs_nolog = 1;
			break;
		case 'n':
			break;
		case 'Q':
			verbosity = 0;
			break;
		case 'q':
			/* don't override -Q */
			if (verbosity > 1)
				verbosity = 1;
			break;
		case 'r':
			cvs_readonly = 1;
			break;
		case 't':
			cvs_trace = 1;
			break;
		case 'v':
			printf("%s\n", CVS_VERSION);
			exit(0);
			/* NOTREACHED */
			break;
		case 'z':
			cvs_compress = (int)strtol(optarg, &ep, 10); 
			if (*ep != '\0')
				errx(1, "error parsing compression level");
			if (cvs_compress < 0 || cvs_compress > 9)
				errx(1, "gzip compression level must be "
				    "between 0 and 9");
			break;
		default:
			usage();
			exit(EX_USAGE);
		}
	}

	argc -= optind;
	argv += optind;

	/* reset getopt() for use by commands */
	optind = 1;
	optreset = 1;

	if (argc == 0) {
		usage();
		exit(EX_USAGE);
	}

	/* setup signal handlers */
	signal(SIGCHLD, sigchld_hdlr);

	cvs_file_init();

	if (readrc)
		cvs_readrc();

	cvs_command = argv[0];
	ret = -1;

	cmdp = cvs_findcmd(cvs_command);
	if (cmdp == NULL) {
		fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
		fprintf(stderr, "CVS commands are:\n");
		for (i = 0; i < CVS_NBCMD; i++)
			fprintf(stderr, "\t%-16s%s\n",
			    cvs_cdt[i].cmd_name, cvs_cdt[i].cmd_descr);
		exit(EX_USAGE);
	}

	if (cmdp->cmd_hdlr == NULL) {
		cvs_log(LP_ERR, "command `%s' not implemented", cvs_command);
		exit(1);
	}

	ret = (*cmdp->cmd_hdlr)(argc, argv);
	if (ret == EX_USAGE) {
		fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
		    cmdp->cmd_synopsis);
	}

	return (ret);
}


/*
 * cvs_findcmd()
 *
 * Find the entry in the command dispatch table whose name or one of its
 * aliases matches <cmd>.
 * Returns a pointer to the command entry on success, NULL on failure.
 */

struct cvs_cmd*
cvs_findcmd(const char *cmd)
{
	u_int i, j;
	struct cvs_cmd *cmdp;

	cmdp = NULL;

	for (i = 0; (i < CVS_NBCMD) && (cmdp == NULL); i++) {
		if (strcmp(cmd, cvs_cdt[i].cmd_name) == 0)
			cmdp = &cvs_cdt[i];
		else {
			for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
				if (strcmp(cmd, cvs_cdt[i].cmd_alias[j]) == 0) {
					cmdp = &cvs_cdt[i];
					break;
				}
			}
		}
	}

	return (cmdp);
}


/*
 * cvs_readrc()
 *
 * Read the CVS `.cvsrc' file in the user's home directory.  If the file
 * exists, it should contain a list of arguments that should always be given
 * implicitly to the specified commands.
 */

void
cvs_readrc(void)
{
	char rcpath[MAXPATHLEN], linebuf[128], *lp;
	struct cvs_cmd *cmdp;
	struct passwd *pw;
	FILE *fp;

	pw = getpwuid(getuid());
	if (pw == NULL) {
		cvs_log(LP_NOTICE, "failed to get user's password entry");
		return;
	}

	snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);

	fp = fopen(rcpath, "r");
	if (fp == NULL) {
		if (errno != ENOENT)
			cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
			    strerror(errno));
		return;
	}

	while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
		lp = strchr(linebuf, ' ');

		/* ignore lines with no arguments */
		if (lp == NULL)
			continue;

		*(lp++) = '\0';
		if (strcmp(linebuf, "cvs") == 0) {
			/* global options */
		}
		else {
			cmdp = cvs_findcmd(linebuf);
			if (cmdp == NULL) {
				cvs_log(LP_NOTICE,
				    "unknown command `%s' in cvsrc",
				    linebuf);
				continue;
			}
		}
	}
	if (ferror(fp)) {
		cvs_log(LP_NOTICE, "failed to read line from cvsrc");
	}

	(void)fclose(fp);
}