home *** CD-ROM | disk | FTP | other *** search
- /* @(#)misc.c (c) copyright 10/18/86 (Dan Heller) */
-
- #include "mush.h"
-
- /* check to see if a string describes a message that is within the range of
- * all messages; if invalid, return 0 and print error. else return msg number
- */
- chk_msg(s)
- register char *s;
- {
- register int n;
-
- if ((n = atoi(s)) > 0 && n <= msg_cnt)
- return n;
- else if (*s == '^' && msg_cnt)
- return 1;
- else if (*s == '$' && msg_cnt)
- return msg_cnt;
- else if (*s == '.' && msg_cnt)
- return current_msg+1;
- print("Invalid message number: %s\n", s);
- return 0;
- }
-
- /*
- * loop thru all msgs starting with current_msg and find next undeleted
- * message. If the variable "wrap" is set, wrap to the beginning of the
- * message list if we hit the end. otherwise, stop at the end of the list.
- */
- next_msg()
- {
- register int n = current_msg;
- register int wrap = !!do_set(set_options, "wrap");
-
- if (!msg_cnt)
- return current_msg = 0;
- for (n++; n != current_msg; n++)
- if (n == msg_cnt) /* hit the end, start back at the beginning */
- if (!wrap)
- return current_msg = msg_cnt - 1;
- else
- n = -1; /* increments to 0 in loop */
- else if (isoff(msg[n].m_flags, DELETE))
- return current_msg = n;
- return current_msg = 0;
- }
-
- /* since print_help just prints help, always return help() */
- print_help(argc, argv)
- register char **argv;
- {
- #ifdef SUNTOOL
- if (istool)
- return help(tool->tl_windowfd, "general", tool_help);
- #endif /* SUNTOOL */
- if (!argc || !*++argv)
- return help(0, "general", cmd_help);
- return help(0, *argv, cmd_help);
- }
-
- /* since this function does not affect messages, return -1 */
- help(fd, str, file)
- #ifdef SUNTOOL
- caddr_t *str;
- #else
- char *str;
- #endif /* SUNTOOL */
- char *file;
- {
- #ifdef SUNTOOL
- if (istool > 1) {
- int oldmask;
- if (!fd)
- fd = print_sw->ts_windowfd;
- oldmask = sigblock(1 << ((SIGALRM) - 1));
- lock_cursors();
- if (display_help(fd, str, file, fonts[LARGE]) && file)
- error("can't read %s", file);
- unlock_cursors();
- (void) sigsetmask(oldmask);
- } else
- #endif /* SUNTOOL */
- if (find_help(str, file) && file)
- error("can't read %s", file);
- return -1; /* doesn't affect any messages */
- }
-
- #ifdef SUNTOOL
- void
- unlock_cursors()
- {
- if (istool < 2)
- return;
- win_setcursor(print_sw->ts_windowfd, &main_cursor);
- win_setcursor(panel_sw->ts_windowfd, &main_cursor);
- if (getting_opts)
- win_setcursor(msg_sw->ts_windowfd, &checkmark);
- else if (ison(glob_flags, IS_GETTING))
- win_setcursor(msg_sw->ts_windowfd, &write_cursor);
- else
- win_setcursor(msg_sw->ts_windowfd, &read_cursor);
- win_setcursor(hdr_panel_sw->ts_windowfd, &main_cursor);
- win_setcursor(hdr_sw->ts_windowfd, &l_cursor);
- }
-
- void
- lock_cursors()
- {
- if (istool < 2)
- return;
- win_setcursor(hdr_sw->ts_windowfd, &coffee);
- win_setcursor(print_sw->ts_windowfd, &coffee);
- win_setcursor(panel_sw->ts_windowfd, &coffee);
- win_setcursor(msg_sw->ts_windowfd, &coffee);
- win_setcursor(hdr_panel_sw->ts_windowfd, &coffee);
- }
-
- #include <suntool/fullscreen.h>
- /* return the event-id that confirmed */
- confirm(fd)
- {
- struct fullscreen *fs;
-
- struct inputmask im;
- struct inputevent event;
-
- fs = fullscreen_init(fd);
- input_imnull(&im);
- im.im_flags |= IM_ASCII;
- win_setinputcodebit(&im, MS_LEFT);
- win_setinputcodebit(&im, MS_MIDDLE);
- win_setinputcodebit(&im, MS_RIGHT);
- win_setinputmask(fd, &im, &im, WIN_NULLLINK);
- win_setcursor(fd, &l_cursor);
- if (input_readevent(fd, &event) == -1)
- error("confim failed");
- fullscreen_destroy(fs);
- return ID;
- }
- #endif /* SUNTOOL */
-
- /* return -1 on error or number of arguments in argv that were parsed */
- get_msg_list(argv, list)
- register char **argv;
- char list[];
- {
- register char *p2, *p;
- char buf[256];
- register int n;
-
- if (!msg_cnt) {
- print("No messages.\n");
- return -1;
- }
- if (!argv || !*argv) {
- if (isoff(glob_flags, IS_PIPE))
- set_msg_bit(list, current_msg);
- return 0;
- }
- /* first, stuff argv's args into a single char array buffer */
- (void) argv_to_string(buf, argv);
- p = buf;
- Debug("get_msg_list: parsing: (%s): ", p);
- /*
- * if do_range returns NULL, an invalid message was specified
- */
- if (!(p2 = do_range(p, list)))
- return -1;
- /*
- * if p2 == p (and p isn't $ or ^ or .), then no message list was
- * specified. set the current message in such cases if we're not piping
- */
- if (p2 == p) {
- if (*p == '$')
- set_msg_bit(list, msg_cnt-1);
- else if (*p == '^')
- set_msg_bit(list, 0);
- else if (*p == '.' || isoff(glob_flags, IS_PIPE))
- set_msg_bit(list, current_msg);
- }
- for (n = 0; p2 > p && *argv; n++)
- p2 -= (strlen(*argv++)+1);
- Debug("parsed %d args\n", n);
- return n;
- }
-
- #ifdef NOT_NEEDED_NOW
- /* return whether or not this process is in the foreground or background */
- foreground()
- {
- #ifdef TIOCGPGRP
- int tpgrp; /* short in 4.1, int in 4.2 */
-
- if (ioctl(0, TIOCGPGRP, (char *)&tpgrp))
- return 0;
- return tpgrp == getpgrp(0);
- #else
- return 1;
- #endif /* TIOCGPGRP */
- }
- #endif /* NOT_NEEDED_NOW */
-
- /*
- * execute a command from a string. f'rinstance: "pick -f foobar"
- * The string is made into an argv and then run. Errors are printed
- * if the command failed to make.
- * NOTES:
- * NEVER pass stright text: e.g. "pick -f foobar", ALWAYS strcpy(buf, "...")
- * no history is expanded (ignore_bang).
- */
- cmd_line(buf, list)
- char buf[], list[];
- {
- register char **argv;
- int argc, ret_val = -1;
- u_long save_do_pipe = ison(glob_flags, DO_PIPE);
- u_long save_is_pipe = ison(glob_flags, IS_PIPE);
-
- turnoff(glob_flags, DO_PIPE);
- turnoff(glob_flags, IS_PIPE);
- if (argv = make_command(buf, TRPL_NULL, &argc))
- ret_val = do_command(argc, argv, list);
- if (save_do_pipe)
- turnon(glob_flags, DO_PIPE);
- else
- turnoff(glob_flags, DO_PIPE);
- if (save_is_pipe)
- turnon(glob_flags, IS_PIPE);
- else
- turnoff(glob_flags, IS_PIPE);
- return ret_val;
- }
-
- glob_test(s)
- char *s;
- {
- print("%s: glob_flags =", s);
- if (ison(glob_flags, DO_UPDATE))
- print_more(" DO_UPDATE");
- if (ison(glob_flags, REV_VIDEO))
- print_more(" REV_VIDEO");
- if (ison(glob_flags, CONT_PRNT ))
- print_more(" CONT_PRNT");
- if (ison(glob_flags, DO_SHELL ))
- print_more(" DO_SHELL");
- if (ison(glob_flags, DO_PIPE))
- print_more(" DO_PIPE");
- if (ison(glob_flags, IS_PIPE))
- print_more(" IS_PIPE");
- if (ison(glob_flags, IGN_SIGS))
- print_more(" IGN_SIGS");
- if (ison(glob_flags, IGN_BANG))
- print_more(" IGN_BANG");
- if (ison(glob_flags, ECHO_FLAG))
- print_more(" ECHO_FLAG");
- if (ison(glob_flags, IS_GETTING))
- print_more(" IS_GETTING");
- if (ison(glob_flags, PRE_CURSES))
- print_more(" PRE_CURSES");
- if (ison(glob_flags, READ_ONLY ))
- print_more(" READ_ONLY");
- if (ison(glob_flags, REDIRECT))
- print_more(" REDIRECT");
- if (ison(glob_flags, WAS_INTR ))
- print_more(" WAS_INTR");
- if (ison(glob_flags, WARNING ))
- print_more(" WARNING");
- print_more("\n");
- }
-
- msg_flags(c, v, list)
- register char **v, *list;
- {
- register int i;
- register u_long newflag = 0;
-
- if (c && *++v && !strcmp(*v, "-?"))
- return help(0, "msg_flags", cmd_help);
- if (c && (c = get_msg_list(v, list)) == -1)
- return -1;
- if (v && *(v += (c-1))) {
- turnon(glob_flags, DO_UPDATE);
- while (*++v)
- for (c = 0; v[0][c]; c++)
- switch (lower(v[0][c])) {
- case 'n' : turnon(newflag, UNREAD), turnoff(newflag, OLD);
- when 'd' : turnon(newflag, DELETE);
- when 'p' : turnon(newflag, PRESERVE);
- when 'u' : turnon(newflag, UNREAD); /* fall thru! */
- case 'o' : turnon(newflag, OLD);
- when 'r' :
- if (v[0][c] == 'R')
- turnoff(newflag, UNREAD), turnon(newflag, OLD);
- else
- turnon(newflag, REPLIED);
- otherwise: return help(0, "msg_flags", cmd_help);
- }
- }
-
- for (i = 0; i < msg_cnt; i++) {
- if (!msg_bit(list, i))
- continue;
- else if (!newflag) {
- wprint("msg %d: offset: %d, lines: %d, bytes: %d, flags:", i+1,
- msg[i].m_offset, msg[i].m_lines, msg[i].m_size);
- if (ison(msg[i].m_flags, UNREAD))
- wprint(" UNREAD");
- if (ison(msg[i].m_flags, OLD))
- wprint(" OLD");
- if (ison(msg[i].m_flags, DELETE))
- wprint(" DELETE");
- if (ison(msg[i].m_flags, PRESERVE))
- wprint(" PRESERVE");
- if (ison(msg[i].m_flags, REPLIED))
- wprint(" REPLIED");
- if (ison(msg[i].m_flags, UPDATE_STATUS))
- wprint(" UPDATE_STATUS");
- wprint("\n");
- } else
- msg[i].m_flags = newflag;
- }
- return 0;
- }
-
- /*
- * Internal pager. Start the internal pager by passing the name of
- * the pager in buf and passing TRUE as start_pager. If the internal
- * pager is desired, pass NULL as buf. Continue paging by passing
- * FALSE as start_page and the buf is the stuff to pass thru to the
- * pager. End paging by passing NULL as buf and FALSE as start_flag.
- * If the pager can't be used, or is null, we're paging ourselves.
- * The -e command line flag (tty echoing done by terminal driver) makes
- * this internal paging ugly, but hey -- such users get what they pay for.
- * Windows does nothing but echo buf to the msg window.
- * The "buff" passed to the pager should be a line at a time so as to
- * count \n's. It could be modified to count \n's, but that'd be slow.
- * Return EOF if pager died, user exited pager, or if user types 'q'
- * at the --more-- prompt for the internal pager.
- */
- do_pager(buf, start_pager)
- register char *buf;
- {
- static FILE *pp;
- static int cnt;
-
- #ifdef SUNTOOL
- if (istool) {
- if (buf && !start_pager)
- Addstr(buf);
- return 0;
- }
- #endif /* SUNTOOL */
- if (start_pager) {
- turnon(glob_flags, IGN_SIGS);
- if (!buf)
- pp = stdout;
- else if (!(pp = popen(buf, "w")))
- error(buf);
- else
- echo_on();
- cnt = 0;
- } else if (!buf) {
- if (pp && pp != stdout)
- pclose(pp);
- pp = NULL_FILE;
- echo_off();
- turnoff(glob_flags, IGN_SIGS);
- } else if (pp != stdout)
- return fputs(buf, pp); /* returns EOF if user exited pager */
- else {
- register char c = 0, *cr = index(buf, '\n');
- if (cr && (c = *++cr) != '\0')
- *cr = 0; /* send one line to stdout and prompt for more */
- (void) fputs(buf, pp);
- if (cr && !(++cnt % (crt-1))) {
- int n = c_more(NULL);
- if (n == '\n' || n == '\r')
- cnt--; /* go line by line */
- else if (n == CTRL(D) || n < 0) {
- clearerr(stdin);
- cnt += (cnt/2);
- } else if (lower(n) == 'q')
- return EOF;
- }
- if (c) {
- *cr = c;
- return do_pager(cr, FALSE);
- }
- }
- return 0;
- }
-
- /* curses based "more" like option */
- c_more(p)
- register char *p;
- {
- register int c;
-
- if (!p)
- p = "--more--";
- print_more(p);
-
- while ((c = getchar()) >= 0 && c != CTRL(D) && !isspace(c) &&
- c != '\n' && c != '\r' && lower(c) != 'q')
- bell();
- if (ison(glob_flags, ECHO_FLAG) && c != '\n' && c != '\r')
- while (getchar() != '\n');
- printf("\r%*c\r", strlen(p), ' '), fflush(stdout); /* remove the prompt */
- return c;
- }
-