home *** CD-ROM | disk | FTP | other *** search
- /* @(#)main.c (c) copyright 10/18/86 (Dan Heller) */
-
- #include "mush.h"
-
- static char *usage_str =
- #ifdef SUNTOOL
- "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-t] [-s subject] [users]\n";
- #else
- #ifdef CURSES
- "usage: %s [-C] [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
- #else
- "usage: %s [-i] [-f [folder] ] [-v] [-S] [-s subject] [user list]\n";
- #endif /* CURSES */
- #endif /* SUNTOOL */
-
- #if defined(sun) && defined(M_DEBUG)
- cpu()
- {
- print("CPU time limit exceeded!\n");
- }
- #endif /* sun && DEBUG */
-
- /*ARGSUSED*/ /* we ignore envp */
- main(argc, argv)
- char **argv;
- {
- u_long flg = NO_FLG;
- int n, source_rc = TRUE;
- char f_flags[10], buf[256], *Cc = NULL, *Subj = NULL;
- register char *p;
- char **args;
-
- if (prog_name = rindex(*argv, '/'))
- prog_name++;
- else
- prog_name = *argv;
-
- (void) signal(SIGBUS, bus_n_seg);
- (void) signal(SIGSEGV, bus_n_seg);
-
- f_flags[0] = 0;
- mailfile = "";
-
- #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);
- f_flags[0] = '\0';
-
- n = 0; /* don't ignore no such file or directory */
- p = getpath(COMMAND_HELP, &n);
-
- if (n) {
- fprintf(stderr, "Warning: can't read %s: %s\n", COMMAND_HELP, p);
- cmd_help = "cmd_help";
- } else
- strdup(cmd_help, p);
-
- init(); /* must be done before checking mail since "login" is set here */
- strdup(spoolfile, sprintf(buf, "%s/%s", MAILDIR, login));
-
- n = FALSE;
- #ifdef SUNTOOL
- if (n = istool = strlen(prog_name) > 3 &&
- !strcmp(prog_name+strlen(prog_name)-4, "tool"))
- turnon(glob_flags, DO_SHELL);
- #endif /* SUNTOOL */
-
- /*
- * preparse the command line to determine whether or not we're going
- * to bail out after checking that the user has no mail. Also, check
- * to see if we're going to run a tool because it must be built first.
- */
- if (!istool && argc > 1) {
- for (args = argv+1; *args && args[0][0] == '-'; args++)
- switch (args[0][1]) {
- #ifdef SUNTOOL
- case 'T' :
- if (args[1])
- args++;
- case 't' :
- istool = 1;
- n = TRUE;
- turnon(glob_flags, DO_SHELL);
- break;
- #endif /* SUNTOOL */
- case 'S' : turnon(glob_flags, DO_SHELL);
- case 'f' :
- case 'u' :
- if (args[1])
- args++;
- n = TRUE;
- break;
- case 'c' :
- case 's' :
- case '1' :
- case '2' :
- if (args[1])
- args++;
- default : ;
- }
- if (*args) { /* unused args indicates sending mail to someone */
- n = TRUE;
- if (!istool)
- turnon(glob_flags, IS_SENDING);
- }
- }
-
- #ifdef SUNTOOL
- /* even if not running tool mode parse all potential suntools args out */
- args = DUBL_NULL;
- tool_parse_all(&argc, argv, &args, prog_name);
- #endif /* SUNTOOL */
-
- /* 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);
- }
- }
-
- for (++argv; *argv && **argv == '-'; argv++)
- switch (argv[0][1]) {
- case 'e':
- /*
- * don't set tty modes -- e.g. echo and cbreak modes aren't
- * changed.
- */
- turnon(glob_flags, ECHO_FLAG);
- #ifdef CURSES
- when 'C':
- /* don't init curses -- don't even set iscurses. */
- if (istool) {
- puts("-C: You are already running in tool mode");
- turnoff(glob_flags, PRE_CURSES);
- } else if (hdrs_only)
- puts("headers only: ignoring -C flag");
- else
- turnon(glob_flags, PRE_CURSES);
- #endif /* CURSES */
- when 'N':
- (void) strcat(f_flags, "-N ");
- when 'r':
- (void) strcat(f_flags, "-r "); /* folder() argument */
- when 'H':
- if (istool) {
- puts("running in tool-mode; -H option ignored.");
- break;
- }
- turnoff(glob_flags, PRE_CURSES);
- if (*(hdrs_only = (*argv)+2) != ':')
- hdrs_only = ":a";
- (void) strcat(f_flags, "-N -r "); /* read only cuz no updates */
- when 'i':
- /* force interactive even if !isatty(0) */
- turnoff(glob_flags, REDIRECT);
- when 'u': /* specify a user's mailbox */
- if (*mailfile)
- puts("You can't specify more than one mailbox"), exit(1);
- strdup(mailfile, sprintf(buf, "%s/%s",
- MAILDIR, (argv[1])? argv[1] : "root"));
- if (argv[1])
- ++argv;
- when 'f':
- if (*mailfile)
- puts("You can't specify more than one mailbox"), exit(1);
- if (argv[1])
- strdup(mailfile, *++argv);
- else
- strdup(mailfile, "&");
- when '1':
- if (argv[1])
- strdup(cmd_help, *++argv);
- else
- puts("-1 \"filename\""), exit(1);
- #ifdef SUNTOOL
- when '2':
- if (argv[1])
- strdup(tool_help, *++argv);
- else
- puts("-2 \"filename\""), exit(1);
- #endif /* SUNTOOL */
- when 's':
- if (istool)
- puts("bad option when run as a tool"), exit(1);
- else if (argv[1])
- Subj = *++argv;
- else
- puts("-s \"subject\""), exit(1);
- when 'c':
- if (istool)
- puts("bad option when run as a tool"), exit(1);
- else if (argv[1])
- Cc = *++argv;
- else
- puts("-c \"cc list\""), exit(1);
- break;
- #ifdef VERBOSE_ARG
- case 'v':
- if (istool)
- puts("bad option when run as a tool"), exit(1);
- turnon(flg, VERBOSE);
- break;
- #endif /* VERBOSE_ARG */
- #ifdef SUNTOOL
- case 'T':
- if ((time_out = atoi(*argv)) <= 29)
- time_out = 30;
- /* -T implies -t */
- case 't': istool = 1;
- #endif /* SUNTOOL */
- case 'S': turnon(glob_flags, DO_SHELL);
- when 'n': source_rc = FALSE;
- when 'd': debug = 1;
- otherwise:
- print("%s: unknown option: `%c'\n", prog_name,
- argv[0][1]? argv[0][1] : '-');
- print(usage_str, prog_name);
- }
-
- if (source_rc) {
- (void) cmd_line(sprintf(buf, "source %s", DEFAULT_RC), msg_list);
- (void) source(0, DUBL_NULL);
- }
-
- 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);
- if (istool)
- turnon(glob_flags, ECHO_FLAG);
- savetty();
- #ifdef TIOCGLTC
- if (isatty(0) && ioctl(0, TIOCGLTC, <chars))
- error("TIOCGLTC");
- #endif /* TIOCGLTC */
- #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 (Cc && *Cc)
- fix_up_addr(Cc);
- /* 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(flg, NEW_SUBJECT);
- if (do_set(set_options, "autosign"))
- turnon(flg, SIGN);
- if (do_set(set_options, "autoedit"))
- turnon(flg, EDIT);
- if (do_set(set_options, "verbose"))
- turnon(flg, VERBOSE);
- if (do_set(set_options, "fortune"))
- turnon(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, Subj, Cc, 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, "home")) || !*p)
- alted:
- p = ALTERNATE_HOME;
- flg = getpid();
- while (!Access(sprintf(tempfile, "%s/.%s%d", p, prog_name, flg++), F_OK))
- ;
- /* just create the file, make sure it's empty. It'll close later and
- * be reopened for reading only.
- */
- {
- int omask = umask(077);
- tmpf = fopen(tempfile, "w");
- (void) umask(omask);
- if (!tmpf) {
- if (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 && !do_set(set_options, "quiet"))
- printf("%s: Type '?' for help.\n", VERSION);
-
- (void) sprintf(buf, "folder %s %s", 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);
- 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);
-
- #ifdef SUNTOOL
- if (istool) {
- n = 0;
- 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()
- {
- print("%s\n", VERSION);
- 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
- (void) cmd_line(sprintf(buf, "set cwd=\"%s\"", cwd), msg_list);
- }
-