home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / dmail / commands.c next >
C/C++ Source or Header  |  1990-09-25  |  11KB  |  574 lines

  1.  
  2. /*
  3.  * COMMANDS.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/dmail/RCS/commands.c,v 1.1 90/02/02 12:04:08 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1985-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  Global Routines:    DO_QUIT()
  10.  *            DO_EXIT()
  11.  *            DO_CD()
  12.  *            DO_ECHO()
  13.  *            DO_GO()
  14.  *            DO_SOURCE()
  15.  *            DO_SHELL()
  16.  *            DO_WRITE()
  17.  *            DO_DELNEXT()
  18.  *            DO_NUMBER()
  19.  *            DO_NEXT()
  20.  *            DO_HEADER()
  21.  *            DO_TYPE()
  22.  *            DO_DELETE()
  23.  *            DO_UNDELETE()
  24.  *            DO_MARK()
  25.  *        DO_BREAK()
  26.  *
  27.  *  Static Routines:    None.
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <sys/file.h>
  33. #include "dmail.h"
  34.  
  35. #define LAST_TYPE   0
  36. #define LAST_HEADER 1
  37.  
  38. static int Last_operation;
  39. static int Last_deleted = -1;
  40. static char ScrBuf[1024];
  41.  
  42. void
  43. do_quit(garbage, com)
  44. char *garbage;
  45. {
  46.     int fd, r, back;
  47.     char *str;
  48.  
  49.     push_break();
  50.     if (get_inode (mail_file) == get_inode (output_file)) {
  51.     back = save_file (0, 0, ST_DELETED | ST_STORED);
  52.     } else {
  53.     r = write_file (output_file, O_CREAT, ST_READ, ST_DELETED | ST_STORED);
  54.     if (r < 0) {
  55.         printf ("Unable to write to %s\n", output_file);
  56.         back = save_file (0, 0, ST_DELETED | ST_STORED);
  57.     } else {
  58.         back = save_file (0, 0, ST_READ | ST_DELETED | ST_STORED);
  59.     }
  60.     }
  61.     if (back < 0)
  62.     printf ("Unable to update %s\n", mail_file);
  63.     if (back > 0)
  64.     printf ("%d  kept in %s\n", back, mail_file);
  65.     sleep (1);
  66.  
  67.     /*
  68.      *    update access time (UNIX, so shell does not report new mail)
  69.      */
  70.  
  71.     if ((fd = open (mail_file, O_RDONLY, 0)) >= 0) {
  72.     read (fd, Buf, 1);
  73.     close (fd);
  74.     }
  75.     if (!com)
  76.     done (0);
  77.     free_entry();
  78.     if (av[1] == 0) {
  79.     if (!Silence)
  80.         puts ("NO FROM FILE SPECIFIED");
  81.     av[1] = mail_file;
  82.     av[2] = NULL;
  83.     }
  84.     Did_cd = 0;
  85.     mail_file = realloc (mail_file, strlen(av[1]) + 1);
  86.     strcpy (mail_file, av[1]);
  87.     str = (av[2]) ? av[2] : mail_file;
  88.     output_file = realloc (output_file, strlen(str) + 1);
  89.     strcpy (output_file, str);
  90.     initial_load_mail();
  91.     m_select (Nulav, M_RESET);
  92.     pop_break();
  93.     if (!Silence)
  94.     printf ("\nRF %-20s   WF %-20s\n", mail_file, output_file);
  95. }
  96.  
  97. void
  98. do_exit(garbage, com)
  99. char *garbage;
  100. {
  101.     char *str;
  102.  
  103.     if (!com)
  104.     done (0);
  105.     push_break();
  106.     free_entry();
  107.     if (av[1] == 0) {
  108.     if (!Silence)
  109.         puts ("NO FROM FILE SPECIFIED");
  110.     av[1] = mail_file;
  111.     av[2] = NULL;
  112.     }
  113.     mail_file = realloc (mail_file, strlen(av[1]) + 1);
  114.     strcpy (mail_file, av[1]);
  115.     str = (av[2]) ? av[2] : mail_file;
  116.     output_file = realloc (output_file, strlen(str) + 1);
  117.     strcpy (output_file, str);
  118.     initial_load_mail();
  119.     m_select (Nulav, M_RESET);
  120.     pop_break();
  121.     if (!Silence)
  122.     printf ("\nRF %-20s   WF %-20s\n", mail_file, output_file);
  123. }
  124.  
  125.  
  126. do_cd()
  127. {
  128.     char *dir = (ac < 2) ? home_dir : av[1];
  129.  
  130.     if (chdir(dir) < 0) {
  131.     printf ("Cannot CD to %s\n", dir);
  132.     return (-1);
  133.     }
  134.     ++Did_cd;
  135.     return (1);
  136. }
  137.  
  138.  
  139. do_echo(str)
  140. char *str;
  141. {
  142.     puts (next_word(str));
  143.     fflush(stdout);
  144.     return (1);
  145. }
  146.  
  147.  
  148. do_go()
  149. {
  150.     int i;
  151.  
  152.     if (ac < 2) {
  153.     puts ("go to which article?");
  154.     }
  155.     rewind_range (1);
  156.     i = get_range();
  157.     if (i < 0) {
  158.     if (!Silence)
  159.         printf ("Message #%d does not exist\n", i);
  160.     return (-1);
  161.     }
  162.     if (i == 0) {
  163.     if (!Silence)
  164.         puts ("No Message");
  165.     return (-1);
  166.     }
  167.     Current = indexof(i);
  168.     return (1);
  169. }
  170.  
  171.  
  172. do_source(scratch, overide)
  173. char *scratch;
  174. {
  175.     volatile char *comline = malloc(1024);
  176.     volatile FILE *fi = NULL;
  177.  
  178.     if (ac < 2) {
  179.     puts ("No file argument to source");
  180.     free(comline);
  181.     return (-1);
  182.     }
  183.     if (push_base()) {
  184.     push_break();
  185.     pop_base();
  186.     if (fi != NULL)
  187.         fclose (fi);
  188.     free(comline);
  189.     pop_break();
  190.     return (-1);
  191.     }
  192.     push_break();
  193.     fi = fopen (av[1], "r");
  194.     pop_break();
  195.     if (fi == NULL) {
  196.     if (!overide)
  197.         printf ("Cannot open %s\n", av[1]);
  198.     free(comline);
  199.     return (-1);
  200.     }
  201.     while (fgets (comline, 1024, fi) != NULL) {
  202.     comline[strlen(comline) - 1] = '\0';
  203.     if (comline[0] != '#')
  204.         exec_command (comline);
  205.     }
  206.     push_break();
  207.     fclose (fi);
  208.     fi = NULL;
  209.     free(comline);
  210.     pop_break();
  211.     pop_base();
  212.     return (1);
  213. }
  214.  
  215.  
  216. do_shell(str)
  217. char *str;
  218. {
  219. #ifdef UNIX
  220.     int pid, ret;
  221. #endif
  222.     char *shell, *ptr;
  223.     char *args;
  224.  
  225. #ifdef AMIGA
  226.     shell = "";
  227. #else
  228.     shell = getenv("SHELL");
  229. #endif
  230.     if (shell == NULL)
  231.     shell = "/bin/sh";
  232.     ptr = shell + strlen(shell) - 1;
  233.     while (ptr > shell && *ptr && *ptr != '/')
  234.     --ptr;
  235.     args = (strcmp(ptr, "/sh"))? "-fc" : "-c";
  236.     push_break();
  237.     str = next_word (str);
  238. #ifdef UNIX
  239.     if (strlen (str)) {
  240.     if ((pid = vfork()) == 0) {
  241.         execl (shell, shell, args, str, NULL);
  242.         _exit (1);
  243.     }
  244.     } else {
  245.     if ((pid = vfork()) == 0) {
  246.         execl (shell, shell, NULL);
  247.         _exit (1);
  248.     }
  249.     }
  250.     while ((ret = wait(0)) > 0) {
  251.     if (ret == pid)
  252.         break;
  253.     }
  254. #endif
  255. #ifdef AMIGA
  256.     Execute(str, NULL, NULL);
  257. #endif
  258.     pop_break();
  259.     return (1);
  260. }
  261.  
  262.  
  263. do_write()
  264. {
  265.     char *file;
  266.     int r, count = 0;
  267.     register int i, j;
  268.  
  269.     if (ac < 2) {
  270.     puts ("You must specify at least a file-name");
  271.     return (-1);
  272.     }
  273.     file = av[1];
  274.     rewind_range (2);
  275.     push_break();
  276.     while (i = get_range()) {
  277.     j = indexof (i);
  278.     if (j >= 0  &&  !(Entry[j].status & ST_DELETED)) {
  279.         Entry[j].status |= ST_STORED | ST_SCR;
  280.         ++count;
  281.     }
  282.     }
  283.     r = write_file (file, O_CREAT, ST_SCR, 0);
  284.     rewind_range (2);
  285.     if (r > 0) {
  286.     while (i = get_range()) {
  287.         j = indexof (i);
  288.         if (j >= 0)
  289.         Entry[j].status &= ~ST_SCR;
  290.     }
  291.     if (!Silence)
  292.         printf ("%d Items written\n", count);
  293.     } else {
  294.     while (i = get_range()) {
  295.         j = indexof (i);
  296.         if (j >= 0)
  297.         Entry[j].status &= ~(ST_SCR | ST_STORED);
  298.     }
  299.     printf ("Could not write to file %s\n", file);
  300.     }
  301.     pop_break();
  302.     return (1);
  303. }
  304.  
  305. /*
  306.  * DB, added 3 Oct 1988
  307.  */
  308.  
  309. do_delprev()
  310. {
  311.     do_mark("", ST_DELETED);
  312.     if (Current)
  313.     return(do_next("", -1));
  314.     return(-1);
  315. }
  316.  
  317. do_delnext()
  318. {
  319.     static int warning;
  320.  
  321.     if (!warning  &&  Last_operation == LAST_HEADER) {
  322.     ++warning;
  323.     puts ("Note that the next command is displaying headers only at");
  324.     puts ("this point.  (one-time warning, NOTHING deleted");
  325.     return (-1);
  326.     }
  327.     if (do_mark("", ST_DELETED) > 0)
  328.     return (do_next("", 1));
  329.     return (-1);
  330. }
  331.  
  332.  
  333. do_number(str, com)
  334. char *str;
  335. int com;
  336. {
  337.     int x;
  338.  
  339.     x = indexof (atoi(str));
  340.     if (x < 0) {
  341.     puts ("Non existant message");
  342.     return (-1);
  343.     }
  344.     Current = x;
  345.     switch (Last_operation) {
  346.     case LAST_TYPE:
  347.     return (do_type());
  348.     case LAST_HEADER:
  349.     return (do_header());
  350.     default:
  351.     puts ("Internal Error NEXT");
  352.     return (-1);
  353.     }
  354. }
  355.  
  356.  
  357. do_next(str, com)
  358. char *str;
  359. {
  360.     int ok;
  361.  
  362.     push_break();
  363.     if (com > 0) {
  364.     if (++Current > Entries)
  365.         Current = Entries;
  366.     if (fix() < 0) {
  367.         puts ("End of file");
  368.         pop_break();
  369.         return (-1);
  370.     }
  371.     --com;
  372.     }
  373.     if (com < 0) {
  374.     ++com;
  375.     ok = 0;
  376.     while (--Current >= 0) {
  377.         if (Entry[Current].no  &&  !(Entry[Current].status & ST_DELETED)) {
  378.         ok = 1;
  379.         break;
  380.         }
  381.     }
  382.     if (!ok) {
  383.         puts ("Start of file");
  384.     Current = 0;
  385.         fix();
  386.         pop_break();
  387.         return (-1);
  388.     }
  389.     }
  390.     pop_break();
  391.     if (!com) {
  392.     switch (Last_operation) {
  393.     case LAST_TYPE:
  394.         return (do_type());
  395.     case LAST_HEADER:
  396.         return (do_header());
  397.     }
  398.     }
  399.     return (1);
  400. }
  401.  
  402.  
  403. do_header()
  404. {
  405.     Last_operation = LAST_HEADER;
  406.     if (push_base()) {
  407.     push_break();
  408.     pop_base();
  409.     PAGER (-1);
  410.     rewind(m_fi);
  411.     fflush (stdout);
  412.     pop_break();
  413.     return (-1);
  414.     }
  415.     if (single_position() < 0)
  416.     return (-1);
  417.     if (Current < 0) {
  418.     puts("Software error #commands.0");
  419.     exit(1);
  420.     }
  421.     PAGER (0);
  422.     sprintf (Puf, "MESSAGE HEADER #%d (%d) %s\n",
  423.         Entry[Current].no,
  424.         Current + 1,
  425.         (Entry[Current].status & ST_DELETED) ? "  DELETED" : "");
  426.     PAGER (Puf);
  427.     sprintf (Puf, "From %s\n", Entry[Current].from);
  428.     PAGER (Puf);
  429.     while (fgets (ScrBuf, 1024, m_fi) != NULL) {
  430.     FPAGER (ScrBuf);
  431.     if (*ScrBuf == '\n') {
  432.         PAGER (-1);
  433.         pop_base();
  434.         return (1);
  435.     }
  436.     }
  437.     PAGER ("END OF FILE ENCOUNTERED");
  438.     PAGER (-1);
  439.     pop_base();
  440.     return (-1);
  441. }
  442.  
  443. do_type()
  444. {
  445.     int i;
  446.  
  447.     Last_operation = LAST_TYPE;
  448.     if (push_base()) {
  449.     push_break();
  450.     pop_base();
  451.     PAGER (-1);
  452.     rewind(m_fi);
  453.     fflush (stdout);
  454.     pop_break();
  455.     return (-1);
  456.     }
  457.     if (single_position() < 0)
  458.     return (-1);
  459.     if (Current < 0) {
  460.     puts("Software Error #commands.1");
  461.     exit(1);
  462.     }
  463.     if (skip_to_data (m_fi) < 0) {
  464.     printf ("Cannot find data for message %d\n", Entry[Current].no);
  465.     return (-1);
  466.     }
  467.     PAGER (0);
  468.     sprintf (Puf, "MESSAGE TEXT #%d (%d) %s\n",
  469.         Entry[Current].no,
  470.         Current + 1,
  471.         (Entry[Current].status & ST_DELETED) ? "  DELETED" : "");
  472.     PAGER (Puf);
  473.     for (i = 0; i < Listsize; ++i) {
  474.     if (*Entry[Current].fields[header[i]]) {
  475.         sprintf (Puf, "%-10s %s",
  476.             Find[header[i]].search,
  477.             Entry[Current].fields[header[i]]);
  478.         PAGER (Puf);
  479.     }
  480.     }
  481.     PAGER ("");
  482.     while ((fgets (ScrBuf, 1024, m_fi) != NULL)  &&  strncmp (ScrBuf, "From ", 5))
  483.     FPAGER (ScrBuf);
  484.     Entry[Current].status |= ST_READ;
  485.     PAGER (-1);
  486.     pop_base();
  487.     return (1);
  488. }
  489.  
  490.  
  491. do_mark(garbage, mask)
  492. char *garbage;
  493. {
  494.     int count = 0;
  495.     register int i, j;
  496.  
  497.     rewind_range (1);
  498.     push_break();
  499.     while (i = get_range()) {
  500.     j = indexof (i);
  501.     if (j >= 0) {
  502.         if (mask & ST_DELETED)
  503.         Last_deleted = j;
  504.         if ((Entry[j].status & mask) != mask) {
  505.         Entry[j].status |= mask;
  506.         if (Entry[j].status & ST_DELETED)
  507.             Entry[j].status &= ~(ST_STORED | ST_READ | ST_TAG);
  508.         ++count;
  509.         }
  510.     }
  511.     }
  512.     if (!Silence)
  513.     printf ("%d  Items\n", count);
  514.     pop_break();
  515.     return (1);
  516. }
  517.  
  518.  
  519. do_unmark(garbage, mask)
  520. char *garbage;
  521. {
  522.     int count = 0;
  523.     register int i, j;
  524.     register struct ENTRY *en;
  525.  
  526.     push_break();
  527.     if (ac == 1 && (mask & ST_DELETED) && Last_deleted != -1)  {
  528.     en = &Entry[Last_deleted];
  529.     if (en->no) {
  530.         en->status &= ~mask;
  531.         printf ("Undeleted last deleted message (# %d)\n", en->no);
  532.         Current = Last_deleted;
  533.         Last_deleted = -1;
  534.     } else {
  535.         puts ("Last deleted message not within current select bounds");
  536.         pop_break();
  537.         return (-1);
  538.     }
  539.     pop_break();
  540.     return (1);
  541.     }
  542.     rewind_range (1);
  543.     while (i = get_range()) {
  544.     j = indexof (i);
  545.     if (j >= 0) {
  546.         if (Entry[j].status & mask) {
  547.         Entry[j].status &= ~mask;
  548.         ++count;
  549.         }
  550.     }
  551.     }
  552.     if (!Silence)
  553.     printf ("%d  Items\n", count);
  554.     pop_break();
  555.     return (1);
  556. }
  557.  
  558. do_break(garbage, mask)
  559. char *garbage;
  560. {
  561.     if (mask)
  562.     pop_break();
  563.     else
  564.     push_break();
  565.     return (1);
  566. }
  567.  
  568. void
  569. do_ver()
  570. {
  571.     puts (DVERSION);
  572. }
  573.  
  574.