home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / BMH.C < prev    next >
C/C++ Source or Header  |  1992-08-20  |  8KB  |  293 lines

  1. /*
  2.    bmh.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Derived from bm source. See the file copyright.bm for conditions of use.
  5.  
  6.    920709 : Created.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <process.h>
  12. #include <alloc.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include "misc.h"
  16. #include "help.h"
  17. #include "buffer.h"
  18. #include "current.h"
  19. #include "pc.h"
  20. #include "rc.h"
  21.  
  22. static void bmhhelp(void);
  23. static void callprog(int argc, char *argv[]);
  24.  
  25. int ali_main(int argc, char *argv[]);
  26. int comp_main(int argc, char *argv[]);
  27. int dist_main(int argc, char *argv[]);
  28. int folders_main(int argc, char *argv[]);
  29. int forw_main(int argc, char *argv[]);
  30. int next_main(int argc, char *argv[]);
  31. int pager_main(int argc, char *argv[]);
  32. int repl_main(int argc, char *argv[]);
  33. int rmm_main(int argc, char *argv[]);
  34. int scan_main(int argc, char *argv[]);
  35. int show_main(int argc, char *argv[]);
  36. int smtp_main(int argc, char *argv[]);
  37.  
  38. static long FileSize = -1L;
  39.  
  40. static int
  41. fsize(char *dir, char *file, char *ext)
  42. {
  43.    char filename[256];
  44.    struct stat statbuf;
  45.  
  46.    sprintf(filename, "%s/%s%s", dir, file, ext);
  47.  
  48.    return stat(filename, &statbuf) == -1 ? -1L : statbuf.st_size;
  49. }
  50.  
  51. static void
  52. doprompt(void)
  53. {
  54.    static char owrite[] = "\r                             \r";
  55.    long curfsize = fsize(getrc(maildir), getrc(username), EXT);
  56.  
  57.    if (FileSize != curfsize) {
  58.       printf("%sYou have new mail\n", owrite);
  59.       FileSize = curfsize;
  60.       }
  61.  
  62.    printf("%s%s> ", owrite, newsfile());
  63.    fflush(stdout);
  64. }
  65.  
  66. int
  67. main(int argc, char *argv[])
  68. {
  69.    int haveread1 = 0;
  70.    char *s, line[255];
  71. /*
  72.    if (curmsg == -1)
  73.       return -1;
  74. */
  75.    dohelp(argc, argv, "bmh [many options]");
  76.    callprog(argc, argv);
  77.    scan_main(argc, argv);
  78.  
  79.    FileSize = fsize(getrc(maildir), getrc(username), EXT);
  80.  
  81.    doprompt();
  82.  
  83.    while ( (s = getline(line, sizeof(line)) ) != NULL) {
  84.       char *arg[MAXARGS];
  85.       int ch,
  86.          nargs = parse(rip(s), arg, MAXARGS);
  87.  
  88.       freebuf();
  89.       switch (ch = nargs > 0 ? arg[0][0] : 0) {
  90.          case '?':
  91.             bmhhelp();
  92.             break;
  93.          case '.':               /* (re-)display current message */
  94.             show_main(1, arg);
  95.             haveread1 = 1;
  96.             break;
  97.          case '!':
  98. /*
  99.             fprintf(stderr, "bmh: coreleft is %u bytes\n", coreleft);
  100. */
  101.             arg[nargs] = NULL;
  102.             if (spawnvp(P_WAIT, arg[1], &arg[1]) == -1)
  103.                perror("bmh");
  104.             break;
  105.          case '#':
  106.             printf("'#' is not a bmh command: enter a number, e.g. \n 1\n");
  107.             break;
  108.          case 0:
  109.          case '\n':
  110.             if (haveread1)
  111.                next_main(1, arg);
  112.             else 
  113.                show_main(1, arg);
  114.             haveread1 = 1;
  115.             break;
  116.          case 'a':
  117.             ali_main(nargs, arg);
  118.             putchar('\n');
  119.             break;
  120.          case 'b':
  121.             dist_main(nargs, arg);
  122.             haveread1 = 1;
  123.             break;
  124.          case 'd':
  125.             rmm_main(nargs, arg);
  126.             haveread1 = 1;
  127.             FileSize = fsize(getrc(maildir), getrc(username), EXT);
  128.             break;
  129.          case 'f':
  130.             forw_main(nargs, arg);
  131.             haveread1 = 1;
  132.             break;
  133.          case 'h':
  134.             scan_main(nargs, arg);
  135.             break;
  136.          case 'k':
  137.             smtp_main(nargs, arg);
  138.             break;
  139.          case 'l':
  140.             smtp_main(1, arg);
  141.             break;
  142.          case 'm':
  143.             comp_main(1, arg);
  144.             break;
  145.          case 'n':
  146.             if (nargs == 1) {
  147.                arg[1] = "-short";
  148.                folders_main(2, arg);
  149.                putchar('\n');
  150.                }
  151.             else {
  152.                if (setcurrent(arg[1], 1) == -1)
  153.                   fprintf(stderr, "bmh: can't change to %s\n", arg[1]);
  154.                else 
  155.                   scan_main(1, arg);
  156.                }
  157.             break;
  158.          case 'N':
  159.             folders_main(1, arg);
  160.             break;
  161.          case 'p':
  162.             savemsgs(nargs, arg, "prn", 1);
  163.             break;
  164.          case 'x':
  165.             printf("bmh: warning mail file may already have been updated");
  166.          case 'q':   /* fall thru */
  167.             return 0;
  168.          case 'r':
  169.             repl_main(nargs, arg);
  170.             haveread1 = 1;
  171.             break;
  172.          case 's':
  173.          case 'w':
  174.             if ((nargs == 1) || isdigit(arg[nargs-1][0])) {
  175.                savemsgs(nargs, arg, "mbox", ch == 's');
  176.                printf("bmh: appended msg(s) to mbox\n");
  177.                }
  178.             else 
  179.                savemsgs(nargs-1, arg, arg[nargs-1], ch == 's');
  180.             haveread1 = 1;
  181.             break;
  182.          case '$':
  183.             fprintf(stderr, "bmh: there is no need to sync the mailfile\n");
  184.             break;
  185.          case 'u':
  186.             fprintf(stderr, "bmh: undelete is not supported\n");
  187.             break;
  188.          case 'v':
  189.             pager_main(nargs, arg);
  190.             break;
  191.          default:
  192.             if (isdigit(ch)) {
  193.                arg[nargs] = arg[0];
  194.                show_main(nargs+1, arg);
  195.                haveread1 = 1;
  196.                }
  197.             else
  198.                printf("Invalid command - ? for help\n");
  199.             break;
  200.          }
  201.       doprompt();
  202.       }
  203.    return 0;
  204. }
  205.  
  206. static void
  207. bmhhelp(void)
  208. {
  209.    version();
  210.    printf("\n");
  211.    printf(" d [msglist]         delete message(s)\n");
  212.    printf(" m                   mail a message\n");
  213.    printf(" s [msglist] [file]  save message(s) in file (default mbox)\n");
  214.    printf(" w [msglist] [file]  save message(s) in file (default mbox, no headers)\n");
  215.    printf(" f [msg]             forward a message\n");
  216.    printf(" b [msg]             bounce a message (remail)\n");
  217.    printf(" r [msg]             reply to a message\n");
  218.    printf(" p [msglst]          print message(s) on printer\n");
  219.    printf(" .                   display current message\n");
  220.    printf(" h                   list messages in notesfile\n");
  221.    printf(" l                   list unsent messages\n");
  222.    printf(" k                   kill unsent messages\n");
  223.    printf(" n [file] / N        display / change notesfile, N for verbose listing\n");
  224.    printf(" #                   display message(s) (where # is the number of the message)\n");
  225.    printf(" q                   quit\n");
  226.    printf(" ! cmd               run shell command\n");
  227.    printf(" ?                   print this help screen\n");
  228.    printf(" a [alias]           display mail alias(es)\n");
  229. /*
  230.    printf(" v file              view this file\n");
  231. */
  232. }
  233.  
  234. typedef int (*FN)(int argc, char *argv[]);
  235.  
  236. static struct jumptable {
  237.    char jump[8];
  238.    FN prog;
  239. } table[] = {
  240.     "ali",     ali_main,
  241.     "comp",    comp_main,
  242.     "dist",    dist_main,
  243.     "folders", folders_main,
  244.     "forw",    forw_main,
  245.     "next",    next_main,
  246.     "repl",    repl_main,
  247.     "rmm",     rmm_main,
  248.     "scan",    scan_main,
  249.     "show",    show_main,
  250.     "smtp",    smtp_main
  251. };
  252.  
  253. static void
  254. callprog(int argc, char *argv[])
  255. {
  256.    struct jumptable *result;
  257.    char *start = argv[0] + strlen(argv[0]), *cp, *lookup;
  258.  
  259.    while ( (start>=argv[0]) && (*start != '/') && (*start != '\\')) {
  260.       *start = tolower(*start);
  261.       start--;
  262.       }
  263.    start++;
  264.    if ((cp = strchr(start, '.')) != NULL)
  265.       *cp = 0;
  266. /*
  267.    printf("command base is %s\n", start);
  268. */
  269.    if (strcmp(start, "bmh") != 0)
  270.       lookup = start;
  271.    else if ( (argc == 1) || (argv[1][0] != '-') )
  272.       return ;
  273.    else 
  274.       lookup = &argv[1][1];
  275.  
  276.    result = (struct jumptable *) bsearch(lookup, table,
  277.       sizeof(table) / sizeof(struct jumptable),
  278.       sizeof(struct jumptable), strcmp);
  279.  
  280.    if (result == NULL)
  281.       return ;
  282.    else {
  283.       if (lookup == start)
  284.          exit( (*result->prog) (argc, argv) );
  285.       else {
  286.          argv[1]++; /* skip '-' */
  287.          exit( (*result->prog) (argc-1, &argv[1]) );
  288.          }
  289.       }
  290. }
  291.  
  292.  
  293.