home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / mush6.0 / part04 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-12  |  10.7 KB  |  411 lines

  1. /* @(#)misc.c    (c) copyright 10/18/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4.  
  5. /* check to see if a string describes a message that is within the range of
  6.  * all messages; if invalid, return 0 and print error. else return msg number
  7.  */
  8. chk_msg(s)
  9. register char *s;
  10. {
  11.     register int n;
  12.  
  13.     if ((n = atoi(s)) > 0 && n <= msg_cnt)
  14.     return n;
  15.     else if (*s == '^' && msg_cnt)
  16.     return 1;
  17.     else if (*s == '$' && msg_cnt)
  18.     return msg_cnt;
  19.     else if (*s == '.' && msg_cnt)
  20.     return current_msg+1;
  21.     print("Invalid message number: %s\n", s);
  22.     return 0;
  23. }
  24.  
  25. /*
  26.  * loop thru all msgs starting with current_msg and find next undeleted
  27.  * message.  If the variable "wrap" is set, wrap to the beginning of the
  28.  * message list if we hit the end.  otherwise, stop at the end of the list.
  29.  */
  30. next_msg()
  31. {
  32.     register int n = current_msg;
  33.     register int wrap = !!do_set(set_options, "wrap");
  34.  
  35.     if (!msg_cnt)
  36.     return current_msg = 0;
  37.     for (n++; n != current_msg; n++)
  38.     if (n == msg_cnt)     /* hit the end, start back at the beginning */
  39.         if (!wrap)
  40.         return current_msg = msg_cnt - 1;
  41.         else
  42.         n = -1; /* increments to 0 in  loop  */
  43.     else if (isoff(msg[n].m_flags, DELETE))
  44.         return current_msg = n;
  45.     return current_msg = 0;
  46. }
  47.  
  48. /* since print_help just prints help, always return help() */
  49. print_help(argc, argv)
  50. register char **argv;
  51. {
  52. #ifdef SUNTOOL
  53.     if (istool)
  54.     return help(tool->tl_windowfd, "general", tool_help);
  55. #endif /* SUNTOOL */
  56.     if (!argc || !*++argv)
  57.     return help(0, "general", cmd_help);
  58.     return help(0, *argv, cmd_help);
  59. }
  60.  
  61. /* since this function does not affect messages, return -1 */
  62. help(fd, str, file)
  63. #ifdef SUNTOOL
  64. caddr_t *str;
  65. #else
  66. char *str;
  67. #endif /* SUNTOOL */
  68. char *file;
  69. {
  70. #ifdef SUNTOOL
  71.     if (istool > 1) {
  72.     int oldmask;
  73.     if (!fd)
  74.         fd = print_sw->ts_windowfd;
  75.     oldmask = sigblock(1 << ((SIGALRM) - 1));
  76.     lock_cursors();
  77.     if (display_help(fd, str, file, fonts[LARGE]) && file)
  78.         error("can't read %s", file);
  79.     unlock_cursors();
  80.     (void) sigsetmask(oldmask);
  81.     } else
  82. #endif /* SUNTOOL */
  83.     if (find_help(str, file) && file)
  84.     error("can't read %s", file);
  85.     return -1; /* doesn't affect any messages */
  86. }
  87.  
  88. #ifdef SUNTOOL
  89. void
  90. unlock_cursors()
  91. {
  92.     if (istool < 2)
  93.     return;
  94.     win_setcursor(print_sw->ts_windowfd, &main_cursor);
  95.     win_setcursor(panel_sw->ts_windowfd, &main_cursor);
  96.     if (getting_opts)
  97.     win_setcursor(msg_sw->ts_windowfd, &checkmark);
  98.     else if (ison(glob_flags, IS_GETTING))
  99.     win_setcursor(msg_sw->ts_windowfd, &write_cursor);
  100.     else
  101.     win_setcursor(msg_sw->ts_windowfd, &read_cursor);
  102.     win_setcursor(hdr_panel_sw->ts_windowfd, &main_cursor);
  103.     win_setcursor(hdr_sw->ts_windowfd, &l_cursor);
  104. }
  105.  
  106. void
  107. lock_cursors()
  108. {
  109.     if (istool < 2)
  110.     return;
  111.     win_setcursor(hdr_sw->ts_windowfd, &coffee);
  112.     win_setcursor(print_sw->ts_windowfd, &coffee);
  113.     win_setcursor(panel_sw->ts_windowfd, &coffee);
  114.     win_setcursor(msg_sw->ts_windowfd, &coffee);
  115.     win_setcursor(hdr_panel_sw->ts_windowfd, &coffee);
  116. }
  117.  
  118. #include <suntool/fullscreen.h>
  119. /* return the event-id that confirmed */
  120. confirm(fd)
  121. {
  122.     struct fullscreen *fs;
  123.  
  124.     struct inputmask im;
  125.     struct inputevent event;
  126.  
  127.     fs = fullscreen_init(fd);
  128.     input_imnull(&im);
  129.     im.im_flags |= IM_ASCII;
  130.     win_setinputcodebit(&im, MS_LEFT);
  131.     win_setinputcodebit(&im, MS_MIDDLE);
  132.     win_setinputcodebit(&im, MS_RIGHT);
  133.     win_setinputmask(fd, &im, &im, WIN_NULLLINK);
  134.     win_setcursor(fd, &l_cursor);
  135.     if (input_readevent(fd, &event) == -1)
  136.     error("confim failed");
  137.     fullscreen_destroy(fs);
  138.     return ID;
  139. }
  140. #endif /* SUNTOOL */
  141.  
  142. /* return -1 on error or number of arguments in argv that were parsed */
  143. get_msg_list(argv, list)
  144. register char **argv;
  145. char list[];
  146. {
  147.     register char *p2, *p;
  148.     char buf[256];
  149.     register int n;
  150.  
  151.     if (!msg_cnt) {
  152.     print("No messages.\n");
  153.     return -1;
  154.     }
  155.     if (!argv || !*argv) {
  156.     if (isoff(glob_flags, IS_PIPE))
  157.         set_msg_bit(list, current_msg);
  158.     return 0;
  159.     }
  160.     /* first, stuff argv's args into a single char array buffer */
  161.     (void) argv_to_string(buf, argv);
  162.     p = buf;
  163.     Debug("get_msg_list: parsing: (%s): ", p);
  164.     /*
  165.      * if do_range returns NULL, an invalid message was specified
  166.      */
  167.     if (!(p2 = do_range(p, list)))
  168.     return -1;
  169.     /*
  170.      * if p2 == p (and p isn't $ or ^ or .), then no message list was
  171.      * specified.  set the current message in such cases if we're not piping
  172.      */
  173.     if (p2 == p) {
  174.     if (*p == '$')
  175.         set_msg_bit(list, msg_cnt-1);
  176.     else if (*p == '^')
  177.         set_msg_bit(list, 0);
  178.     else if (*p == '.' || isoff(glob_flags, IS_PIPE))
  179.         set_msg_bit(list, current_msg);
  180.     }
  181.     for (n = 0; p2 > p && *argv; n++)
  182.     p2 -= (strlen(*argv++)+1);
  183.     Debug("parsed %d args\n", n);
  184.     return n;
  185. }
  186.  
  187. #ifdef NOT_NEEDED_NOW
  188. /* return whether or not this process is in the foreground or background */
  189. foreground()
  190. {
  191. #ifdef TIOCGPGRP
  192.     int tpgrp;    /* short in 4.1, int in 4.2 */
  193.  
  194.     if (ioctl(0, TIOCGPGRP, (char *)&tpgrp))
  195.     return 0;
  196.     return tpgrp == getpgrp(0);
  197. #else
  198.     return 1;
  199. #endif /* TIOCGPGRP */
  200. }
  201. #endif /* NOT_NEEDED_NOW */
  202.  
  203. /*
  204.  * execute a command from a string.  f'rinstance: "pick -f foobar"
  205.  * The string is made into an argv and then run.  Errors are printed
  206.  * if the command failed to make.
  207.  *   NOTES:
  208.  *     NEVER pass stright text: e.g. "pick -f foobar", ALWAYS strcpy(buf, "...")
  209.  *     no history is expanded (ignore_bang).
  210.  */
  211. cmd_line(buf, list)
  212. char buf[], list[];
  213. {
  214.     register char **argv;
  215.     int argc, ret_val = -1;
  216.     u_long save_do_pipe = ison(glob_flags, DO_PIPE);
  217.     u_long save_is_pipe = ison(glob_flags, IS_PIPE);
  218.  
  219.     turnoff(glob_flags, DO_PIPE);
  220.     turnoff(glob_flags, IS_PIPE);
  221.     if (argv = make_command(buf, TRPL_NULL, &argc))
  222.     ret_val = do_command(argc, argv, list);
  223.     if (save_do_pipe)
  224.     turnon(glob_flags, DO_PIPE);
  225.     else
  226.     turnoff(glob_flags, DO_PIPE);
  227.     if (save_is_pipe)
  228.     turnon(glob_flags, IS_PIPE);
  229.     else
  230.     turnoff(glob_flags, IS_PIPE);
  231.     return ret_val;
  232. }
  233.  
  234. glob_test(s)
  235. char *s;
  236. {
  237.     print("%s: glob_flags =", s);
  238.     if (ison(glob_flags, DO_UPDATE))
  239.     print_more(" DO_UPDATE");
  240.     if (ison(glob_flags, REV_VIDEO))
  241.     print_more(" REV_VIDEO");
  242.     if (ison(glob_flags, CONT_PRNT  ))
  243.     print_more(" CONT_PRNT");
  244.     if (ison(glob_flags, DO_SHELL    ))
  245.     print_more(" DO_SHELL");
  246.     if (ison(glob_flags, DO_PIPE))
  247.     print_more(" DO_PIPE");
  248.     if (ison(glob_flags, IS_PIPE))
  249.     print_more(" IS_PIPE");
  250.     if (ison(glob_flags, IGN_SIGS))
  251.     print_more(" IGN_SIGS");
  252.     if (ison(glob_flags, IGN_BANG))
  253.     print_more(" IGN_BANG");
  254.     if (ison(glob_flags, ECHO_FLAG))
  255.     print_more(" ECHO_FLAG");
  256.     if (ison(glob_flags, IS_GETTING))
  257.     print_more(" IS_GETTING");
  258.     if (ison(glob_flags, PRE_CURSES))
  259.     print_more(" PRE_CURSES");
  260.     if (ison(glob_flags, READ_ONLY  ))
  261.     print_more(" READ_ONLY");
  262.     if (ison(glob_flags, REDIRECT))
  263.     print_more(" REDIRECT");
  264.     if (ison(glob_flags, WAS_INTR ))
  265.     print_more(" WAS_INTR");
  266.     if (ison(glob_flags, WARNING   ))
  267.     print_more(" WARNING");
  268.     print_more("\n");
  269. }
  270.  
  271. msg_flags(c, v, list)
  272. register char **v, *list;
  273. {
  274.     register int    i;
  275.     register u_long    newflag = 0;
  276.  
  277.     if (c && *++v && !strcmp(*v, "-?"))
  278.     return help(0, "msg_flags", cmd_help);
  279.     if (c && (c = get_msg_list(v, list)) == -1)
  280.     return -1;
  281.     if (v && *(v += (c-1))) {
  282.     turnon(glob_flags, DO_UPDATE);
  283.     while (*++v)
  284.         for (c = 0; v[0][c]; c++)
  285.         switch (lower(v[0][c])) {
  286.             case 'n' : turnon(newflag, UNREAD), turnoff(newflag, OLD);
  287.             when 'd' : turnon(newflag, DELETE);
  288.             when 'p' : turnon(newflag, PRESERVE);
  289.             when 'u' : turnon(newflag, UNREAD); /* fall thru! */
  290.             case 'o' : turnon(newflag, OLD);
  291.             when 'r' :
  292.             if (v[0][c] == 'R')
  293.                 turnoff(newflag, UNREAD), turnon(newflag, OLD);
  294.             else
  295.                 turnon(newflag, REPLIED);
  296.             otherwise: return help(0, "msg_flags", cmd_help);
  297.         }
  298.     }
  299.  
  300.     for (i = 0; i < msg_cnt; i++) {
  301.     if (!msg_bit(list, i))
  302.         continue;
  303.     else if (!newflag) {
  304.         wprint("msg %d: offset: %d, lines: %d, bytes: %d, flags:", i+1,
  305.         msg[i].m_offset, msg[i].m_lines, msg[i].m_size);
  306.         if (ison(msg[i].m_flags, UNREAD))
  307.         wprint(" UNREAD");
  308.         if (ison(msg[i].m_flags, OLD))
  309.         wprint(" OLD");
  310.         if (ison(msg[i].m_flags, DELETE))
  311.         wprint(" DELETE");
  312.         if (ison(msg[i].m_flags, PRESERVE))
  313.         wprint(" PRESERVE");
  314.         if (ison(msg[i].m_flags, REPLIED))
  315.         wprint(" REPLIED");
  316.         if (ison(msg[i].m_flags, UPDATE_STATUS))
  317.         wprint(" UPDATE_STATUS");
  318.         wprint("\n");
  319.     } else
  320.         msg[i].m_flags = newflag;
  321.     }
  322.     return 0;
  323. }
  324.  
  325. /*
  326.  * Internal pager.  Start the internal pager by passing the name of
  327.  * the pager in buf and passing TRUE as start_pager. If the internal
  328.  * pager is desired, pass NULL as buf.  Continue paging by passing
  329.  * FALSE as start_page and the buf is the stuff to pass thru to the
  330.  * pager.  End paging by passing NULL as buf and FALSE as start_flag.
  331.  * If the pager can't be used, or is null, we're paging ourselves.
  332.  * The -e command line flag (tty echoing done by terminal driver) makes
  333.  * this internal paging ugly, but hey -- such users get what they pay for.
  334.  * Windows does nothing but echo buf to the msg window.
  335.  * The "buff" passed to the pager should be a line at a time so as to
  336.  * count \n's.  It could be modified to count \n's, but that'd be slow.
  337.  * Return EOF if pager died, user exited pager, or if user types 'q'
  338.  * at the --more-- prompt for the internal pager.
  339.  */
  340. do_pager(buf, start_pager)
  341. register char *buf;
  342. {
  343.     static FILE *pp;
  344.     static int cnt;
  345.  
  346. #ifdef SUNTOOL
  347.     if (istool) {
  348.     if (buf && !start_pager)
  349.         Addstr(buf);
  350.     return 0;
  351.     }
  352. #endif /* SUNTOOL */
  353.     if (start_pager) {
  354.     turnon(glob_flags, IGN_SIGS);
  355.     if (!buf)
  356.         pp = stdout;
  357.     else if (!(pp = popen(buf, "w")))
  358.         error(buf);
  359.     else
  360.         echo_on();
  361.     cnt = 0;
  362.     } else if (!buf) {
  363.     if (pp && pp != stdout)
  364.         pclose(pp);
  365.     pp = NULL_FILE;
  366.     echo_off();
  367.     turnoff(glob_flags, IGN_SIGS);
  368.     } else if (pp != stdout)
  369.     return fputs(buf, pp); /* returns EOF if user exited pager */
  370.     else {
  371.     register char c = 0, *cr = index(buf, '\n');
  372.     if (cr && (c = *++cr) != '\0')
  373.         *cr = 0; /* send one line to stdout and prompt for more */
  374.     (void) fputs(buf, pp);
  375.     if (cr && !(++cnt % (crt-1))) {
  376.         int n = c_more(NULL);
  377.         if (n == '\n' || n == '\r')
  378.         cnt--; /* go line by line */
  379.         else if (n == CTRL(D) || n < 0) {
  380.         clearerr(stdin);
  381.         cnt += (cnt/2);
  382.         } else if (lower(n) == 'q')
  383.         return EOF;
  384.     }
  385.     if (c) {
  386.         *cr = c;
  387.         return do_pager(cr, FALSE);
  388.     }
  389.     }
  390.     return 0;
  391. }
  392.  
  393. /* curses based "more" like option */
  394. c_more(p)
  395. register char *p;
  396. {
  397.     register int c;
  398.  
  399.     if (!p)
  400.     p = "--more--";
  401.     print_more(p);
  402.     
  403.     while ((c = getchar()) >= 0 && c != CTRL(D) && !isspace(c) &&
  404.        c != '\n' && c != '\r' && lower(c) != 'q')
  405.     bell();
  406.     if (ison(glob_flags, ECHO_FLAG) && c != '\n' && c != '\r')
  407.     while (getchar() != '\n');
  408.     printf("\r%*c\r", strlen(p), ' '), fflush(stdout); /* remove the prompt */
  409.     return c;
  410. }
  411.