2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
10 * You may distribute under the terms of the GNU General Public License
11 * as specified in the README file that comes with the CVS source distribution.
13 * This is the main C driver for the CVS system.
15 * Credit to Dick Grune, Vrije Universiteit, Amsterdam, for writing
16 * the shell-script CVS system that this is based on.
25 #include "xgethostname.h"
28 uint32_t arc4random(void);
31 const char *program_name;
32 const char *program_path;
33 const char *cvs_cmd_name;
35 const char *global_session_id; /* Random session ID */
38 /* FIXME: Perhaps this should be renamed original_hostname or the like? */
39 char *server_hostname;
43 int cvswrite = !CVSREAD_DFLT;
55 *** CVSROOT/config options
58 struct config *config;
62 mode_t cvsumask = UMASK_DFLT;
67 * Defaults, for the environment variables that are not set
69 char *Editor = EDITOR_DFLT;
75 /* Temp dir, if set by the user. */
76 static char *tmpdir_cmdline;
80 /* Returns in order of precedence:
82 * 1. Temp dir as set via the command line.
83 * 2. Temp dir as set in CVSROOT/config.
84 * 3. Temp dir as set in $TMPDIR env var.
85 * 4. Contents of TMPDIR_DFLT preprocessor macro.
88 * It is a fatal error if this function would otherwise return NULL or an
92 get_cvs_tmp_dir (void)
95 if (tmpdir_cmdline) retval = tmpdir_cmdline;
96 else if (config && config->TmpDir) retval = config->TmpDir;
97 else retval = get_system_temp_dir ();
98 if (!retval) retval = TMPDIR_DFLT;
100 if (!retval || !*retval) error (1, 0, "No temp dir specified.");
107 /* When our working directory contains subdirectories with different
108 values in CVS/Root files, we maintain a list of them. */
109 List *root_directories = NULL;
111 static const struct cmd
113 const char *fullname; /* Full name of the function (e.g. "commit") */
115 /* Synonyms for the command, nick1 and nick2. We supply them
116 mostly for two reasons: (1) CVS has always supported them, and
117 we need to maintain compatibility, (2) if there is a need for a
118 version which is shorter than the fullname, for ease in typing.
119 Synonyms have the disadvantage that people will see "new" and
120 then have to think about it, or look it up, to realize that is
121 the operation they know as "add". Also, this means that one
122 cannot create a command "cvs new" with a different meaning. So
123 new synonyms are probably best used sparingly, and where used
124 should be abbreviations of the fullname (preferably consisting
125 of the first 2 or 3 or so letters).
127 One thing that some systems do is to recognize any unique
128 abbreviation, for example "annotat" "annota", etc., for
129 "annotate". The problem with this is that scripts and user
130 habits will expect a certain abbreviation to be unique, and in
131 a future release of CVS it may not be. So it is better to
132 accept only an explicit list of abbreviations and plan on
133 supporting them in the future as well as now. */
138 int (*func) (int, char **); /* Function takes (argc, argv) arguments. */
139 unsigned long attr; /* Attributes. */
143 { "add", "ad", "new", add, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
144 { "admin", "adm", "rcs", admin, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
145 { "annotate", "ann", NULL, annotate, CVS_CMD_USES_WORK_DIR },
146 { "checkout", "co", "get", checkout, 0 },
147 { "commit", "ci", "com", commit, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
148 { "diff", "di", "dif", diff, CVS_CMD_USES_WORK_DIR },
149 { "edit", NULL, NULL, edit, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
150 { "editors", NULL, NULL, editors, CVS_CMD_USES_WORK_DIR },
151 { "export", "exp", "ex", checkout, CVS_CMD_USES_WORK_DIR },
152 { "history", "hi", "his", history, CVS_CMD_USES_WORK_DIR },
153 { "import", "im", "imp", import, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR | CVS_CMD_IGNORE_ADMROOT},
154 { "init", NULL, NULL, init, CVS_CMD_MODIFIES_REPOSITORY },
155 #if defined (HAVE_KERBEROS) && defined (SERVER_SUPPORT)
156 { "kserver", NULL, NULL, server, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR }, /* placeholder */
158 { "log", "lo", NULL, cvslog, CVS_CMD_USES_WORK_DIR },
159 #ifdef AUTH_CLIENT_SUPPORT
160 { "login", "logon", "lgn", login, 0 },
161 { "logout", NULL, NULL, logout, 0 },
162 #endif /* AUTH_CLIENT_SUPPORT */
163 { "ls", "dir", "list", ls, 0 },
164 #if (defined(AUTH_SERVER_SUPPORT) || defined (HAVE_GSSAPI)) && defined(SERVER_SUPPORT)
165 { "pserver", NULL, NULL, server, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR }, /* placeholder */
167 { "rannotate","rann", "ra", annotate, 0 },
168 { "rdiff", "patch", "pa", patch, 0 },
169 { "release", "re", "rel", release, CVS_CMD_MODIFIES_REPOSITORY },
170 { "remove", "rm", "delete", cvsremove, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
171 { "rlog", "rl", NULL, cvslog, 0 },
172 { "rls", "rdir", "rlist", ls, 0 },
173 { "rtag", "rt", "rfreeze", cvstag, CVS_CMD_MODIFIES_REPOSITORY },
174 #ifdef SERVER_SUPPORT
175 { "server", NULL, NULL, server, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
177 { "suck", NULL, NULL, suck, 0 },
178 { "status", "st", "stat", cvsstatus, CVS_CMD_USES_WORK_DIR },
179 { "tag", "ta", "freeze", cvstag, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
180 { "unedit", NULL, NULL, unedit, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
181 { "update", "up", "upd", update, CVS_CMD_USES_WORK_DIR },
182 { "version", "ve", "ver", version, 0 },
183 { "watch", NULL, NULL, watch, CVS_CMD_MODIFIES_REPOSITORY | CVS_CMD_USES_WORK_DIR },
184 { "watchers", NULL, NULL, watchers, CVS_CMD_USES_WORK_DIR },
185 { NULL, NULL, NULL, NULL, 0 },
188 static const char *const usg[] =
190 /* CVS usage messages never have followed the GNU convention of
191 putting metavariables in uppercase. I don't know whether that
192 is a good convention or not, but if it changes it would have to
193 change in all the usage messages. For now, they consistently
194 use lowercase, as far as I know. Punctuation is pretty funky,
195 though. Sometimes they use none, as here. Sometimes they use
196 single quotes (not the TeX-ish `' stuff), as in --help-options.
197 Sometimes they use double quotes, as in cvs -H add.
199 Most (not all) of the usage messages seem to have periods at
200 the end of each line. I haven't tried to duplicate this style
201 in --help as it is a rather different format from the rest. */
203 "Usage: %s [cvs-options] command [command-options-and-arguments]\n",
204 " where cvs-options are -q, -n, etc.\n",
205 " (specify --help-options for a list of options)\n",
206 " where command is add, admin, etc.\n",
207 " (specify --help-commands for a list of commands\n",
208 " or --help-synonyms for a list of command synonyms)\n",
209 " where command-options-and-arguments depend on the specific command\n",
210 " (specify -H followed by a command name for command-specific help)\n",
211 " Specify --help to receive this message\n",
214 /* Some people think that a bug-reporting address should go here. IMHO,
215 the web sites are better because anything else is very likely to go
216 obsolete in the years between a release and when someone might be
217 reading this help. Besides, we could never adequately discuss
218 bug reporting in a concise enough way to put in a help message. */
220 /* I was going to put this at the top, but usage() wants the %s to
221 be in the first line. */
222 "The Concurrent Versions System (CVS) is a tool for version control.\n",
223 /* I really don't think I want to try to define "version control"
224 in one line. I'm not sure one can get more concise than the
225 paragraph in ../cvs.spec without assuming the reader knows what
226 version control means. */
228 "For CVS updates and additional information, see\n",
229 " the CVS home page at http://www.nongnu.org/cvs/ or\n",
230 " the CVSNT home page at http://www.cvsnt.org/\n",
234 static const char *const cmd_usage[] =
236 "CVS commands are:\n",
237 " add Add a new file/directory to the repository\n",
238 " admin Administration front end for rcs\n",
239 " annotate Show last revision where each line was modified\n",
240 " checkout Checkout sources for editing\n",
241 " commit Check files into the repository\n",
242 " diff Show differences between revisions\n",
243 " edit Get ready to edit a watched file\n",
244 " editors See who is editing a watched file\n",
245 " export Export sources from CVS, similar to checkout\n",
246 " history Show repository access history\n",
247 " import Import sources into CVS, using vendor branches\n",
248 " init Create a CVS repository if it doesn't exist\n",
249 #if defined (HAVE_KERBEROS) && defined (SERVER_SUPPORT)
250 " kserver Kerberos server mode\n",
252 " log Print out history information for files\n",
253 #ifdef AUTH_CLIENT_SUPPORT
254 " login Prompt for password for authenticating server\n",
255 " logout Removes entry in .cvspass for remote repository\n",
256 #endif /* AUTH_CLIENT_SUPPORT */
257 " ls List files available from CVS\n",
258 #if (defined(AUTH_SERVER_SUPPORT) || defined (HAVE_GSSAPI)) && defined(SERVER_SUPPORT)
259 " pserver Password server mode\n",
261 " rannotate Show last revision where each line of module was modified\n",
262 " rdiff Create 'patch' format diffs between releases\n",
263 " release Indicate that a Module is no longer in use\n",
264 " remove Remove an entry from the repository\n",
265 " rlog Print out history information for a module\n",
266 " rls List files in a module\n",
267 " rtag Add a symbolic tag to a module\n",
268 #ifdef SERVER_SUPPORT
269 " server Server mode\n",
271 " status Display status information on checked out files\n",
272 " tag Add a symbolic tag to checked out version of files\n",
273 " unedit Undo an edit command\n",
274 " update Bring work tree in sync with repository\n",
275 " version Show current CVS version(s)\n",
276 " watch Set watches\n",
277 " watchers See who is watching a file\n",
278 "(Specify the --help option for a list of other help options)\n",
282 static const char *const opt_usage[] =
284 /* Omit -b because it is just for compatibility. */
285 "CVS global options (specified before the command name) are:\n",
286 " -H Displays usage information for command.\n",
287 " -Q Cause CVS to be really quiet.\n",
288 " -q Cause CVS to be somewhat quiet.\n",
289 " -r Make checked-out files read-only.\n",
290 " -w Make checked-out files read-write (default).\n",
291 " -g Force group-write permissions on checked-out files.\n",
292 " -n Do not execute anything that will change the disk.\n",
293 " -t Show trace of program execution (repeat for more\n",
294 " verbosity) -- try with -n.\n",
295 " -R Assume repository is read-only, such as CDROM\n",
296 " -v CVS version and copyright.\n",
297 " -T tmpdir Use 'tmpdir' for temporary files.\n",
298 " -e editor Use 'editor' for editing log information.\n",
299 " -d CVS_root Overrides $CVSROOT as the root of the CVS tree.\n",
300 " -f Do not use the ~/.cvsrc file.\n",
301 #ifdef CLIENT_SUPPORT
302 " -z # Request compression level '#' for net traffic.\n",
304 " -x Encrypt all net traffic.\n",
306 " -a Authenticate all net traffic.\n",
308 " -s VAR=VAL Set CVS user variable.\n",
309 "(Specify the --help option for a list of other help options)\n",
315 set_root_directory (Node *p, void *ignored)
317 if (current_parsed_root == NULL && p->data != NULL)
319 current_parsed_root = p->data;
320 original_parsed_root = current_parsed_root;
327 static const char * const*
332 const struct cmd *c = &cmds[0];
333 /* Three more for title, "specify --help" line, and NULL. */
336 while (c->fullname != NULL)
342 synonyms = xnmalloc (numcmds, sizeof(char *));
344 *line++ = "CVS command synonyms are:\n";
345 for (c = &cmds[0]; c->fullname != NULL; c++)
347 if (c->nick1 || c->nick2)
349 *line = Xasprintf (" %-12s %s %s\n", c->fullname,
350 c->nick1 ? c->nick1 : "",
351 c->nick2 ? c->nick2 : "");
355 *line++ = "(Specify the --help option for a list of other help options)\n";
358 return (const char * const*) synonyms; /* will never be freed */
364 lookup_command_attribute (const char *cmd_name)
366 const struct cmd *cm;
368 for (cm = cmds; cm->fullname; cm++)
370 if (strcmp (cmd_name, cm->fullname) == 0)
374 error (1, 0, "unknown command: %s", cmd_name);
381 * Exit with an error code and an informative message about the signal
382 * received. This function, by virtue of causing an actual call to exit(),
383 * causes all the atexit() handlers to be called.
386 * sig The signal recieved.
389 * The cleanup routines registered via atexit() and the error function
390 * itself can potentially change the exit status. They shouldn't do this
391 * unless they encounter problems doing their own jobs.
394 * Nothing. This function will always exit. It should exit with an exit
395 * status of 1, but might not, as noted in the ERRORS section above.
397 #ifndef DONT_USE_SIGNALS
398 static RETSIGTYPE main_cleanup (int) __attribute__ ((__noreturn__));
399 #endif /* DONT_USE_SIGNALS */
401 main_cleanup (int sig)
403 #ifndef DONT_USE_SIGNALS
431 name = "broken pipe";
436 name = "termination";
440 /* This case should never be reached, because we list above all
441 the signals for which we actually establish a signal handler. */
442 sprintf (temp, "%d", sig);
447 /* This always exits, which will cause our exit handlers to be called. */
448 error (1, 0, "received %s signal", name);
449 /* but make the exit explicit to silence warnings when gcc processes the
450 * noreturn attribute.
453 #endif /* !DONT_USE_SIGNALS */
460 * When !defined ALLOW_CONFIG_OVERRIDE, this will never have any value but
463 extern char *gConfigPath;
468 enum {RANDOM_BYTES = 8};
469 enum {COMMITID_RAW_SIZE = (sizeof(time_t) + RANDOM_BYTES)};
471 static char const alphabet[62] =
472 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
476 main (int argc, char **argv)
478 cvsroot_t *CVSroot_parsed = NULL;
479 bool cvsroot_update_env = true;
481 const struct cmd *cm;
485 int help = 0; /* Has the user asked for help? This
486 lets us support the `cvs -H cmd'
487 convention to give help for cmd. */
488 static const char short_options[] = "+QqrwgtnRvb:T:e:d:Hfz:s:xal";
489 static struct option long_options[] =
491 {"help", 0, NULL, 'H'},
492 {"version", 0, NULL, 'v'},
493 {"help-commands", 0, NULL, 1},
494 {"help-synonyms", 0, NULL, 2},
495 {"help-options", 0, NULL, 4},
496 #ifdef SERVER_SUPPORT
497 {"allow-root", required_argument, NULL, 3},
498 #endif /* SERVER_SUPPORT */
501 /* `getopt_long' stores the option index here, but right now we
503 int option_index = 0;
505 #ifdef SYSTEM_INITIALIZE
506 /* Hook for OS-specific behavior, for example socket subsystems on
507 NT and OS2 or dealing with windows and arguments on Mac. */
508 SYSTEM_INITIALIZE (&argc, &argv);
511 #ifdef SYSTEM_CLEANUP
512 /* Hook for OS-specific behavior, for example socket subsystems on
513 NT and OS2 or dealing with windows and arguments on Mac. */
514 cleanup_register (SYSTEM_CLEANUP);
518 /* On systems that have tzset (which is almost all the ones I know
519 of), it's a good idea to call it. */
524 * Just save the last component of the path for error messages
526 program_path = xstrdup (argv[0]);
527 #ifdef ARGV0_NOT_PROGRAM_NAME
528 /* On some systems, e.g. VMS, argv[0] is not the name of the command
529 which the user types to invoke the program. */
530 program_name = "cvs";
532 program_name = last_component (argv[0]);
536 * Query the environment variables up-front, so that
537 * they can be overridden by command line arguments
539 if ((cp = getenv (EDITOR1_ENV)) != NULL)
541 else if ((cp = getenv (EDITOR2_ENV)) != NULL)
543 else if ((cp = getenv (EDITOR3_ENV)) != NULL)
545 if (getenv (CVSREAD_ENV) != NULL)
547 if (getenv (CVSREADONLYFS_ENV) != NULL) {
552 /* Set this to 0 to force getopt initialization. getopt() sets
553 this to 1 internally. */
556 /* We have to parse the options twice because else there is no
557 chance to avoid reading the global options from ".cvsrc". Set
558 opterr to 0 for avoiding error messages about invalid options.
562 while ((c = getopt_long
563 (argc, argv, short_options, long_options, &option_index))
570 #ifdef SERVER_SUPPORT
571 /* Don't try and read a .cvsrc file if we are a server. */
574 # if defined (AUTH_SERVER_SUPPORT) || defined (HAVE_GSSAPI)
575 || !strcmp (argv[optind], "pserver")
577 # ifdef HAVE_KERBEROS
578 || !strcmp (argv[optind], "kserver")
579 # endif /* HAVE_KERBEROS */
580 || !strcmp (argv[optind], "server")))
582 /* Avoid any .cvsrc file. */
584 /* Pre-parse the server options to get the config path. */
585 cvs_cmd_name = argv[optind];
586 parseServerOptions (argc - optind, argv + optind);
588 #endif /* SERVER_SUPPORT */
591 * Scan cvsrc file for global options.
594 read_cvsrc (&argc, &argv, "cvs");
599 while ((c = getopt_long
600 (argc, argv, short_options, long_options, &option_index))
606 /* --help-commands */
610 /* --help-synonyms */
611 usage (cmd_synonyms());
617 #ifdef SERVER_SUPPORT
620 root_allow_add (optarg, gConfigPath);
622 #endif /* SERVER_SUPPORT */
637 * Force full write permissions for the group.
638 * See the user's manual for details and dangers.
640 umask(umask(S_IRWXG|S_IRWXO) & S_IRWXO);
654 /* no-op to simply ignore the old -l option */
657 (void) fputs ("\n", stdout);
659 (void) fputs ("\n", stdout);
661 Copyright (C) 2005 Free Software Foundation, Inc.\n\
663 Portions contributed by Thorsten Glaser for the MirOS Project.\n\
664 Senior active maintainers include Larry Jones, Derek R. Price,\n\
665 and Mark D. Baushke. Please see the AUTHORS and README files from the CVS\n\
666 distribution kit for a complete list of contributors and copyrights.\n",
668 (void) fputs ("\n", stdout);
669 (void) fputs ("CVS may be copied only under the terms of the GNU General Public License,\n", stdout);
670 (void) fputs ("a copy of which can be found with the CVS distribution kit.\n", stdout);
671 (void) fputs ("\n", stdout);
673 (void) fputs ("Specify the --help option for further information about CVS\n", stdout);
678 /* This option used to specify the directory for RCS
679 executables. But since we don't run them any more,
680 this is a noop. Silently ignore it so that .cvsrc
681 and scripts and inetd.conf and such can work with
682 either new or old CVS. */
685 if (tmpdir_cmdline) free (tmpdir_cmdline);
686 tmpdir_cmdline = xstrdup (optarg);
689 if (free_Editor) free (Editor);
690 Editor = xstrdup (optarg);
694 if (CVSroot_cmdline != NULL)
695 free (CVSroot_cmdline);
696 CVSroot_cmdline = xstrdup (optarg);
702 use_cvsrc = 0; /* unnecessary, since we've done it above */
705 #ifdef CLIENT_SUPPORT
706 gzip_level = strtol (optarg, &end, 10);
707 if (*end != '\0' || gzip_level < 0 || gzip_level > 9)
709 "gzip compression level must be between 0 and 9");
710 #endif /* CLIENT_SUPPORT */
711 /* If no CLIENT_SUPPORT, we just silently ignore the gzip
712 * level, so that users can have it in their .cvsrc and not
715 * We still parse the argument to -z for correctness since
716 * one user complained of being bitten by a run of
717 * `cvs -z -n up' which read -n as the argument to -z without
721 variable_set (optarg);
724 #ifdef CLIENT_SUPPORT
726 #endif /* CLIENT_SUPPORT */
727 /* If no CLIENT_SUPPORT, ignore -x, so that users can
728 have it in their .cvsrc and not cause any trouble.
729 If no ENCRYPTION, we still accept -x, but issue an
730 error if we are being run as a client. */
733 #ifdef CLIENT_SUPPORT
736 /* If no CLIENT_SUPPORT, ignore -a, so that users can
737 have it in their .cvsrc and not cause any trouble.
738 We will issue an error later if stream
739 authentication is not supported. */
752 /* Calculate the cvs global session ID */
754 global_session_id = Xasprintf("1%010llX%04X%04X",
755 (unsigned long long)time(NULL),
756 (int)(getpid() & 0xFFFF), (int)(arc4random() & 0xFFFF));
758 TRACE (TRACE_FUNCTION, "main: Session ID is %s", global_session_id);
760 /* Look up the command name. */
762 cvs_cmd_name = argv[0];
763 for (cm = cmds; cm->fullname; cm++)
765 if (cm->nick1 && !strcmp (cvs_cmd_name, cm->nick1))
767 if (cm->nick2 && !strcmp (cvs_cmd_name, cm->nick2))
769 if (!strcmp (cvs_cmd_name, cm->fullname))
775 fprintf (stderr, "Unknown command: `%s'\n\n", cvs_cmd_name);
779 cvs_cmd_name = cm->fullname; /* Global pointer for later use */
783 argc = -1; /* some functions only check for this */
784 err = (*(cm->func)) (argc, argv);
788 /* The user didn't ask for help, so go ahead and authenticate,
789 set up CVSROOT, and the rest of it. */
791 short int lock_cleanup_setup = 0;
793 /* The UMASK environment variable isn't handled with the
794 others above, since we don't want to signal errors if the
795 user has asked for help. This won't work if somebody adds
796 a command-line flag to set the umask, since we'll have to
797 parse it before we get here. */
799 if ((cp = getenv (CVSUMASK_ENV)) != NULL)
801 /* FIXME: Should be accepting symbolic as well as numeric mask. */
802 cvsumask = strtol (cp, &end, 8) & 0777;
804 error (1, errno, "invalid umask value in %s (%s)",
808 /* HOSTNAME & SERVER_HOSTNAME need to be set before they are
809 * potentially used in gserver_authenticate_connection() (called from
810 * pserver_authenticate_connection, below).
812 hostname = xgethostname ();
816 "xgethostname () returned NULL, using \"localhost\"");
817 hostname = xstrdup ("localhost");
820 /* Keep track of this separately since the client can change
821 * HOSTNAME on the server.
823 server_hostname = xstrdup (hostname);
825 #ifdef SERVER_SUPPORT
827 # ifdef HAVE_KERBEROS
828 /* If we are invoked with a single argument "kserver", then we are
829 running as Kerberos server as root. Do the authentication as
830 the very first thing, to minimize the amount of time we are
832 if (strcmp (cvs_cmd_name, "kserver") == 0)
834 kserver_authenticate_connection ();
836 /* Pretend we were invoked as a plain server. */
837 cvs_cmd_name = "server";
839 # endif /* HAVE_KERBEROS */
841 # if defined (AUTH_SERVER_SUPPORT) || defined (HAVE_GSSAPI)
842 if (strcmp (cvs_cmd_name, "pserver") == 0)
844 /* The reason that --allow-root is not a command option
845 is mainly that it seems easier to make it a global option. */
847 /* Gets username and password from client, authenticates, then
848 switches to run as that user and sends an ACK back to the
850 pserver_authenticate_connection ();
852 /* Pretend we were invoked as a plain server. */
853 cvs_cmd_name = "server";
855 # endif /* AUTH_SERVER_SUPPORT || HAVE_GSSAPI */
856 #endif /* SERVER_SUPPORT */
858 server_active = strcmp (cvs_cmd_name, "server") == 0;
860 #ifdef SERVER_SUPPORT
863 /* This is only used for writing into the history file. For
864 remote connections, it might be nice to have hostname
865 and/or remote path, on the other hand I'm not sure whether
866 it is worth the trouble. */
867 CurDir = xstrdup ("<remote>");
868 cleanup_register (server_cleanup);
873 cleanup_register (close_stdout);
876 error (1, errno, "cannot get working directory");
881 /* XXX pid < 10^32 */
882 val = Xasprintf ("%ld", (long) getpid ());
883 setenv (CVS_PID_ENV, val, 1);
887 /* make sure we clean up on error */
888 signals_register (main_cleanup);
890 #ifdef KLUDGE_FOR_WNT_TESTSUITE
891 /* Probably the need for this will go away at some point once
892 we call fflush enough places (e.g. fflush (stdout) in
894 (void) setvbuf (stdout, NULL, _IONBF, 0);
895 (void) setvbuf (stderr, NULL, _IONBF, 0);
896 #endif /* KLUDGE_FOR_WNT_TESTSUITE */
899 read_cvsrc (&argc, &argv, cvs_cmd_name);
901 /* Fiddling with CVSROOT doesn't make sense if we're running
902 * in server mode, since the client will send the repository
903 * directory after the connection is made.
907 /* First check if a root was set via the command line. */
910 if (!(CVSroot_parsed = parse_cvsroot (CVSroot_cmdline)))
911 error (1, 0, "Bad CVSROOT: `%s'.", CVSroot_cmdline);
914 /* See if we are able to find a 'better' value for CVSroot
915 * in the CVSADM_ROOT directory.
917 * "cvs import" shouldn't check CVS/Root; in general it
918 * ignores CVS directories and CVS/Root is likely to
919 * specify a different repository than the one we are
920 * importing to, but if this is not import and no root was
921 * specified on the command line, set the root from the
925 && !(cm->attr & CVS_CMD_IGNORE_ADMROOT)
927 CVSroot_parsed = Name_Root (NULL, NULL);
929 /* Now, if there is no root on the command line and we didn't find
930 * one in a file, set it via the $CVSROOT env var.
934 char *tmp = getenv (CVSROOT_ENV);
937 if (!(CVSroot_parsed = parse_cvsroot (tmp)))
938 error (1, 0, "Bad CVSROOT: `%s'.", tmp);
939 cvsroot_update_env = false;
946 if (!(CVSroot_parsed = parse_cvsroot (CVSROOT_DFLT)))
947 error (1, 0, "Bad CVSROOT: `%s'.", CVSROOT_DFLT);
949 #endif /* CVSROOT_DFLT */
951 /* Now we've reconciled CVSROOT from the command line, the
952 CVS/Root file, and the environment variable. Do the
953 last sanity checks on the variable. */
954 if (!CVSroot_parsed && cm->func != version)
957 "No CVSROOT specified! Please use the `-d' option");
959 "or set the %s environment variable.", CVSROOT_ENV);
963 /* Here begins the big loop over unique cvsroot values. We
964 need to call do_recursion once for each unique value found
965 in CVS/Root. Prime the list with the current value. */
967 /* Create the list. */
968 assert (root_directories == NULL);
969 root_directories = getlist ();
976 n->type = NT_UNKNOWN;
977 n->key = xstrdup (CVSroot_parsed->original);
978 n->data = CVSroot_parsed;
980 if (addnode (root_directories, n))
981 error (1, 0, "cannot add initial CVSROOT %s", n->key);
984 assert (current_parsed_root == NULL);
986 /* Handle running 'cvs version' with no CVSROOT. */
988 if (cm->func == version && !CVSroot_parsed)
991 /* If we're running the server, we want to execute this main
992 loop once and only once (we won't be serving multiple roots
993 from this connection, so there's no need to do it more than
994 once). To get out of the loop, we perform a "break" at the
997 while (server_active ||
998 walklist (root_directories, set_root_directory, NULL))
1000 /* Fiddling with CVSROOT doesn't make sense if we're running
1001 in server mode, since the client will send the repository
1002 directory after the connection is made. */
1006 /* Now we're 100% sure that we have a valid CVSROOT
1007 variable. Parse it to see if we're supposed to do
1008 remote accesses or use a special access method. */
1010 TRACE (TRACE_FUNCTION,
1011 "main loop with CVSROOT=%s",
1012 current_parsed_root ? current_parsed_root->directory
1016 * Check to see if the repository exists.
1018 if (!current_parsed_root->isremote)
1023 path = Xasprintf ("%s/%s", current_parsed_root->directory,
1025 if (!isaccessible (path, R_OK | X_OK))
1028 /* If this is "cvs init", the root need not exist yet.
1030 if (strcmp (cvs_cmd_name, "init"))
1031 error (1, save_errno, "%s", path);
1036 /* Update the CVSROOT environment variable. */
1037 if (cvsroot_update_env)
1038 setenv (CVSROOT_ENV, current_parsed_root->original, 1);
1041 /* Parse the CVSROOT/config file, but only for local. For the
1042 server, we parse it after we know $CVSROOT. For the
1043 client, it doesn't get parsed at all, obviously. The
1044 presence of the parse_config call here is not meant to
1045 predetermine whether CVSROOT/config overrides things from
1046 read_cvsrc and other such places or vice versa. That sort
1047 of thing probably needs more thought. */
1048 if (!server_active && !current_parsed_root->isremote)
1050 /* If there was an error parsing the config file, parse_config
1051 already printed an error. We keep going. Why? Because
1052 if we didn't, then there would be no way to check in a new
1053 CVSROOT/config file to fix the broken one! */
1054 if (config) free_config (config);
1055 config = parse_config (current_parsed_root->directory, NULL);
1057 /* Can set TMPDIR in the environment if necessary now, since
1058 * if it was set in config, we now know it.
1060 push_env_temp_dir ();
1063 #ifdef CLIENT_SUPPORT
1064 /* Need to check for current_parsed_root != NULL here since
1065 * we could still be in server mode before the server function
1066 * gets called below and sets the root
1068 if (current_parsed_root != NULL && current_parsed_root->isremote)
1070 /* Create a new list for directory names that we've
1071 sent to the server. */
1072 if (dirs_sent_to_server != NULL)
1073 dellist (&dirs_sent_to_server);
1074 dirs_sent_to_server = getlist ();
1079 #ifdef SERVER_SUPPORT
1080 /* Don't worry about lock_cleanup_setup when the server is
1081 * active since we can only go through this loop once in that
1087 #ifdef CLIENT_SUPPORT
1088 !current_parsed_root->isremote &&
1090 !lock_cleanup_setup))
1092 /* Set up to clean up any locks we might create on exit. */
1093 cleanup_register (Lock_Cleanup);
1094 lock_cleanup_setup = 1;
1097 /* Call our worker function. */
1098 err = (*(cm->func)) (argc, argv);
1100 /* Mark this root directory as done. When the server is
1101 active, our list will be empty -- don't try and
1102 remove it from the list. */
1106 Node *n = findnode (root_directories,
1107 original_parsed_root->original);
1109 assert (n->data != NULL);
1111 current_parsed_root = NULL;
1116 } /* end of loop for cvsroot values */
1118 dellist (&root_directories);
1119 } /* end of stuff that gets done if the user DOESN'T ask for help */
1123 /* This is exit rather than return because apparently that keeps
1124 some tools which check for memory leaks happier. */
1125 exit (err ? EXIT_FAILURE : 0);
1126 /* Keep picky/stupid compilers (e.g. Visual C++ 5.0) happy. */
1133 Make_Date (const char *rawdate)
1137 if (!get_date (&t, rawdate, NULL))
1138 error (1, 0, "Can't parse date/time: `%s'", rawdate);
1140 /* Truncate nanoseconds. */
1141 return date_from_time_t (t.tv_sec);
1146 /* Parse a string of the form TAG[:DATE], where TAG could be the empty string.
1149 * input The string to be parsed.
1152 * tag The tag found, if any. If TAG is the empty string, then leave
1153 * this value unchanged.
1154 * date The date found, if any. If DATE is the empty string or is
1155 * missing, leave this value unchanged.
1158 * If either TAG or DATE is replaced for output, the previous value is freed.
1161 * If either TAG or DATE cannot be parsed, then this function will exit with
1162 * a fatal error message.
1168 parse_tagdate (char **tag, char **date, const char *input)
1172 TRACE (TRACE_FUNCTION, "parse_tagdate (%s, %s, %s)",
1173 *tag ? *tag : "(null)", *date ? *date : "(null)",
1176 if ((p = strchr (input, ':')))
1178 /* Parse the tag. */
1181 /* The tag has > 0 length. */
1182 if (*tag) free (*tag);
1183 *tag = xmalloc (p - input + 1);
1184 strncpy (*tag, input, p - input);
1185 (*tag)[p - input] = '\0';
1188 /* Parse the date. */
1191 if (*date) free (*date);
1192 *date = strcmp (p, "BASE") ? Make_Date (p) : xstrdup (p);
1195 else if (strlen (input))
1197 /* The tag has > 0 length. */
1198 if (*tag) free (*tag);
1199 *tag = xstrdup (input);
1202 TRACE (TRACE_DATA, "parse_tagdate: got tag = `%s', date = `%s'",
1203 *tag ? *tag : "(null)", *date ? *date : "(null)");
1208 /* Convert a time_t to an RCS format date. This is mainly for the
1209 use of "cvs history", because the CVSROOT/history file contains
1210 time_t format dates; most parts of CVS will want to avoid using
1211 time_t's directly, and instead use RCS_datecmp, Make_Date, &c.
1212 Assuming that the time_t is in GMT (as it generally should be),
1213 then the result will be in GMT too.
1215 Returns a newly malloc'd string. */
1218 date_from_time_t (time_t unixtime)
1221 char date[MAXDATELEN];
1224 ftm = gmtime (&unixtime);
1226 /* This is a system, like VMS, where the system clock is in local
1227 time. Hopefully using localtime here matches the "zero timezone"
1228 hack I added to get_date (get_date of course being the relevant
1229 issue for Make_Date, and for history.c too I think). */
1230 ftm = localtime (&unixtime);
1232 (void) sprintf (date, DATEFORM,
1233 (long)ftm->tm_year + (ftm->tm_year < 100 ? 0L : 1900L),
1234 ftm->tm_mon + 1, ftm->tm_mday, ftm->tm_hour,
1235 ftm->tm_min, ftm->tm_sec);
1236 ret = xstrdup (date);
1242 /* Convert a date to RFC822/1123 format. This is used in contexts like
1243 dates to send in the protocol; it should not vary based on locale or
1244 other such conventions for users. We should have another routine which
1245 does that kind of thing.
1247 The SOURCE date is in our internal RCS format. DEST should point to
1248 storage managed by the caller, at least MAXDATELEN characters. */
1250 date_to_internet (char *dest, const char *source)
1254 date_to_tm (&date, source);
1255 tm_to_internet (dest, &date);
1261 date_to_tm (struct tm *dest, const char *source)
1264 if (sscanf (source, SDATEFORM,
1265 &y, &dest->tm_mon, &dest->tm_mday,
1266 &dest->tm_hour, &dest->tm_min, &dest->tm_sec)
1268 /* Is there a better way to handle errors here? I made this
1269 non-fatal in case we are called from the code which can't
1270 deal with fatal errors. */
1271 error (0, 0, "internal error: bad date %s", source);
1273 dest->tm_year = y - ((y > 100) ? 1900 : 0);
1279 /* Convert a date to RFC822/1123 format. This is used in contexts like
1280 dates to send in the protocol; it should not vary based on locale or
1281 other such conventions for users. We should have another routine which
1282 does that kind of thing.
1284 The SOURCE date is a pointer to a struct tm. DEST should point to
1285 storage managed by the caller, at least MAXDATELEN characters. */
1287 tm_to_internet (char *dest, const struct tm *source)
1289 /* Just to reiterate, these strings are from RFC822 and do not vary
1290 according to locale. */
1291 static const char *const month_names[] =
1292 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1293 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1295 sprintf (dest, "%d %s %ld %02d:%02d:%02d -0000", source->tm_mday,
1296 source->tm_mon < 0 || source->tm_mon > 11
1297 ? "???" : month_names[source->tm_mon],
1298 (long)source->tm_year + 1900, source->tm_hour, source->tm_min,
1305 * Format a date for the current locale.
1308 * UNIXTIME The UNIX seconds since the epoch.
1311 * If my_strftime() encounters an error, this function can return NULL.
1313 * Otherwise, returns a date string in ISO8601 format, e.g.:
1315 * 2004-04-29 13:24:22 -0700
1317 * It is the responsibility of the caller to return of this string.
1320 format_time_t (time_t unixtime)
1322 static char buf[sizeof ("yyyy-mm-dd HH:MM:SS -HHMM")];
1323 /* Convert to a time in the local time zone. */
1324 struct tm ltm = *(localtime (&unixtime));
1326 if (!my_strftime (buf, sizeof (buf), "%Y-%m-%d %H:%M:%S %z", <m, 0, 0))
1329 return xstrdup (buf);
1334 /* Like format_time_t(), but return time in UTC.
1337 gmformat_time_t (time_t unixtime)
1339 static char buf[sizeof ("yyyy-mm-dd HH:MM:SS -HHMM")];
1340 /* Convert to a time in the local time zone. */
1341 struct tm ltm = *(gmtime (&unixtime));
1343 if (!my_strftime (buf, sizeof (buf), "%Y-%m-%d %H:%M:%S %z", <m, 0, 0))
1346 return xstrdup (buf);
1351 /* Format a date in the local timezone using format_time_t() given a date from
1352 * an arbitrary timezone in a string.
1355 * DATESTR A string that looks like anything get_date() can parse, e.g.:
1357 * 2004-04-29 20:24:22
1360 * As get_date() & format_time_t(). Prints a warning if either provide
1361 * error return values. See RETURNS.
1364 * A freshly allocated string that is a copy of the input string if either
1365 * get_date() or format_time_t() encounter an error and as format_time_t()
1369 format_date_alloc (char *datestr)
1374 TRACE (TRACE_FUNCTION, "format_date (%s)", datestr);
1376 /* Convert the date string to seconds since the epoch. */
1377 if (!get_date (&t, datestr, NULL))
1379 error (0, 0, "Can't parse date/time: `%s'.", datestr);
1383 /* Get the time into a string, truncating any nanoseconds returned by
1386 if ((buf = format_time_t (t.tv_sec)) == NULL)
1388 error (0, 0, "Unable to reformat date `%s'.", datestr);
1395 return xstrdup (datestr);
1401 usage (register const char *const *cpp)
1403 (void) fprintf (stderr, *cpp++, program_name, cvs_cmd_name);
1405 (void) fprintf (stderr, "%s", *cpp);
1406 exit (EXIT_FAILURE);
1409 /* vim:tabstop=8:shiftwidth=4