home *** CD-ROM | disk | FTP | other *** search
- /* @(#)main.c (c) copyright 10/18/86 (Dan Heller) */
-
- #include "mush.h"
- #include "options.h"
-
- #define PATCHDATE "3/12/89" /* Here because EVERYTHING depends on mush.h */
-
- #if defined(sun) && defined(M_DEBUG)
- cpu()
- {
- print("CPU time limit exceeded!\n");
- }
- #endif /* sun && DEBUG */
-
- #ifdef LCKDFLDIR
- extern char *lckdfldir;
- #endif /* LCKDFLDIR */
-
- #ifdef DOT_LOCK
- int sgid;
- #ifdef BSD
- int rgid;
- #endif /* BSD */
- #endif /* DOT_LOCK */
-
- /*ARGSUSED*/ /* we ignore envp */
- main(argc, argv)
- char **argv;
- {
- int n;
- char buf[256];
- register char *p;
- char **args;
- struct mush_flags Flags;
-
- #ifdef LCKDFLDIR
- lckdfldir = LCKDFLDIR;
- #endif /* LCKDFLDIR */
- if (prog_name = rindex(*argv, '/'))
- prog_name++;
- else
- prog_name = *argv;
-
- (void) signal(SIGBUS, bus_n_seg);
- (void) signal(SIGSEGV, bus_n_seg);
- (void) signal(SIGPIPE, SIG_IGN); /* if pager is terminated before end */
-
- #if defined(sun) && defined(M_DEBUG)
- (void) signal(SIGXCPU, cpu);
-
- if (p = getenv("MALLOC_DEBUG"))
- malloc_debug(atoi(p));
- else
- malloc_debug(0);
- #endif /* sun && debug */
-
- if (!isatty(0))
- turnon(glob_flags, REDIRECT);
-
- n = 0; /* don't ignore no such file or directory */
- p = getpath(COMMAND_HELP, &n);
-
- if (n)
- cmd_help = "cmd_help";
- else
- strdup(cmd_help, p);
-
- init(); /* must be done before checking mail since "login" is set here */
- #ifdef HOMEMAIL
- {
- char *home = do_set(set_options, "home");
- if (!home)
- home = "";
- strdup(spoolfile, sprintf(buf, "%s/%s", home, MAILFILE));
- }
- #else /* HOMEMAIL */
- strdup(spoolfile, sprintf(buf, "%s/%s", MAILDIR, login));
- #endif /* HOMEMAIL */
-
- args = DUBL_NULL;
- n = preparse_opts(&argc,argv,&args);
-
- /* check for any mail at all and exit if we're not continuing */
- if (!n) {
- struct stat statb;
- if (stat(spoolfile, &statb) || statb.st_size == 0) {
- printf("No mail for %s.\n", login);
- exit(0);
- }
- }
-
- #ifdef DOT_LOCK
- sgid = getegid();
- #ifdef BSD
- rgid = getgid();
- setregid(sgid, rgid);
- #else
- setgid(getgid());
- #endif /* BSD */
- #endif /* DOT_LOCK */
-
- parse_options(&argv, &Flags);
-
- if (Flags.source_rc) {
- /* use cmd_line() in case DEFAULT_RC has expandable chars */
- (void) cmd_line(sprintf(buf, "source %s", DEFAULT_RC), msg_list);
- (void) source(0, DUBL_NULL);
- }
- if (*spoolfile != '/') {
- n = 1;
- p = getpath(spoolfile, &n);
- if (n == -1)
- fputs(p, stderr), exit(1);
- else if (n)
- fprintf(stderr, "\"%s\" is a directory.\n", p), exit(1);
- else
- strdup(spoolfile, p);
- }
-
- set_cwd(); /* call _after_ sourcing files */
-
- #ifdef SUNTOOL
- if (istool)
- if (ison(glob_flags, REDIRECT))
- puts("You can't redirect input to a tool."), exit(1);
- else
- make_tool(args), turnon(glob_flags, DO_SHELL);
- #endif /* SUNTOOL */
-
- /* now we're ready for I/O */
- if (isoff(glob_flags, REDIRECT)) {
- /* make sure we can always recover from no echo mode */
- (void) signal(SIGINT, catch);
- (void) signal(SIGQUIT, catch);
- (void) signal(SIGHUP, catch);
- if (istool)
- turnon(glob_flags, ECHO_FLAG);
- tty_settings();
- #ifdef SIGCONT
- (void) signal(SIGTSTP, stop_start); /* this will take care of SIGCONT */
- #endif /* SIGCONT */
- /* echo_off() checks to see if echo_flg is set, so don't worry */
- echo_off();
- }
-
- if (!istool && *argv) { /* we could check IS_SENDING */
- char recipients[BUFSIZ];
- (void) argv_to_string(recipients, argv);
- fix_up_addr(recipients);
- if (Flags.Cc && *(Flags.Cc))
- fix_up_addr(Flags.Cc);
- if (Flags.Bcc && *(Flags.Bcc))
- fix_up_addr(Flags.Bcc);
- /* prompt for subject and Cc list, but not "To: "
- * mail_someone() already takes care of redirection.
- * if -s or -c options are given, they will be passed.
- */
- if (do_set(set_options, "ask"))
- turnon(Flags.flg, NEW_SUBJECT);
- if (do_set(set_options, "autosign"))
- turnon(Flags.flg, SIGN);
- if (do_set(set_options, "autoedit"))
- turnon(Flags.flg, EDIT);
- if (do_set(set_options, "verbose"))
- turnon(Flags.flg, VERBOSE);
- if (do_set(set_options, "fortune"))
- turnon(Flags.flg, DO_FORTUNE);
- /* set now in case user is not running shell, but is running debug */
- (void) signal(SIGCHLD, sigchldcatcher);
- if (!setjmp(jmpbuf))
- (void) mail_someone(recipients,
- Flags.Subj,
- Flags.Cc,
- Flags.Bcc,
- Flags.flg,
- NULL);
- /* do shell set from above: "mush -S user" perhaps */
- if (isoff(glob_flags, DO_SHELL) && !*mailfile) {
- if (isoff(glob_flags, REDIRECT))
- echo_on();
- exit(0);
- }
- }
- turnoff(glob_flags, IS_SENDING); /* no longer sending mail; running shell */
-
- if (ison(glob_flags, REDIRECT)) {
- puts("You can't redirect input unless you're sending mail.");
- puts("If you want to run a shell with redirection, use \"-i\"");
- cleanup(0);
- }
- if (!*mailfile) {
- strdup(mailfile, spoolfile);
- if (!mail_size() && isoff(glob_flags, DO_SHELL)) {
- /* we know it's not the spool file here */
- printf("No mail in %s.\n", mailfile);
- echo_on(), exit(0);
- }
- }
-
- if (!hdrs_only) {
- /* catch will test DO_SHELL and try to longjmp if set. this is a
- * transition state from no-shell to do-shell to ignore sigs to
- * avoid a longjmp botch. Note setjmp isn't called until do_loop().
- */
- turnon(glob_flags, IGN_SIGS);
- #ifdef CURSES
- if (ison(glob_flags, PRE_CURSES))
- (void) curses_init(0, DUBL_NULL);
- turnoff(glob_flags, PRE_CURSES);
- #endif /* CURSES */
- }
-
- /* find a free tmpfile */
- if (!(p = do_set(set_options, "tmpdir")) &&
- !(p = do_set(set_options, "home")))
- alted:
- p = ALTERNATE_HOME;
- {
- int pid = getpid();
- while (!Access(sprintf(tempfile, "%s/.%s%d", p, prog_name, pid++), F_OK))
- ;
- }
- /* just create the file, make sure it's empty. It'll close later and
- * be reopened for reading only.
- */
- if (!(tmpf = mask_fopen(tempfile, "w"))) {
- if (strcmp(p, ALTERNATE_HOME))
- goto alted;
- error("Can't create tempfile %s", tempfile);
- cleanup(0);
- }
-
- /* do pseudo-intelligent stuff with certain signals */
- (void) signal(SIGINT, catch);
- (void) signal(SIGQUIT, catch);
- (void) signal(SIGHUP, catch);
-
- if (!hdrs_only && !istool && (!Flags.src_file || !Flags.src_n_exit) &&
- !do_set(set_options, "quiet"))
- printf("%s: Type '?' for help.\n", VERSION);
-
- (void) sprintf(buf, "folder %s %s", Flags.f_flags, mailfile);
- if (argv = make_command(buf, TRPL_NULL, &argc)) {
- if (folder(argc, argv, NULL) == -1 && isoff(glob_flags, DO_SHELL))
- turnoff(glob_flags, IGN_SIGS), cleanup(0);
- #ifdef CURSES
- if (iscurses)
- (void) curses_help_msg(TRUE);
- #endif /* CURSES */
- free_vec(argv);
- }
-
- if (hdrs_only) {
- (void) sprintf(buf, "headers %s", hdrs_only);
- if (argv = make_command(buf, TRPL_NULL, &argc))
- (void) do_hdrs(argc, argv, NULL);
- cleanup(0);
- }
-
- turnon(glob_flags, DO_SHELL);
- if (istool && msg_cnt)
- set_isread(current_msg);
-
- /* finally, if the user wanted to source a file to execute, do it now */
- if (Flags.src_file) {
- char *s_argv[2];
- s_argv[1] = Flags.src_file;
- (void) source(2, s_argv);
- if (!istool && Flags.src_n_exit)
- cleanup(0);
- }
-
- #ifdef SUNTOOL
- if (istool) {
- n = 0;
- if (!tool_help) {
- p = getpath(TOOL_HELP, &n);
- if (n) {
- fprintf(stderr, "Warning: can't read %s: %s\n", TOOL_HELP, p);
- tool_help = "tool_help";
- } else
- strdup(tool_help, p);
- }
- if (time_out < 30)
- time_out = 60;
- turnoff(glob_flags, IGN_SIGS);
- (void) do_hdrs(0, DUBL_NULL, NULL);
- timerclear(&(mail_timer.it_interval));
- timerclear(&(mail_timer.it_value));
- mail_timer.it_value.tv_sec = time_out;
- setitimer(ITIMER_REAL, &mail_timer, NULL);
- (void) signal(SIGALRM, check_new_mail);
- unlock_cursors();
- while (!(tool->tl_flags & TOOL_DONE))
- tool_select(tool, 1);
- cleanup(0);
- }
- #endif /* SUNTOOL */
- do_loop();
- }
-
- do_version()
- {
- #ifdef PATCHDATE
- print("%s [%s]\n", VERSION, PATCHDATE);
- #else /* !PATCHDATE */
- print("%s\n", VERSION);
- #endif /* PATCHDATE */
- return -1;
- }
-
- /* set the current working directory */
- set_cwd()
- {
- char buf[MAXPATHLEN], cwd[MAXPATHLEN];
- #ifndef SYSV
- extern char *getwd();
- #else /* SYSV */
- extern char *getcwd();
- #endif /* SYSV */
-
- #ifndef SYSV
- if (getwd(cwd) == NULL)
- #else
- if (getcwd(cwd, MAXPATHLEN) == NULL)
- #endif /* SYSV */
- {
- error("getcwd: %s", cwd);
- (void) un_set(&set_options, "cwd");
- } else {
- char *argv[4];
- argv[0] = "cwd";
- argv[1] = "=";
- argv[2] = cwd;
- argv[3] = NULL;
- (void) add_option(&set_options, argv);
- }
- }
-