home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / edit.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  6KB  |  203 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: edit.c,v 4.1.1.2 90/10/07 21:02:42 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.2 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    edit.c,v $
  17.  * Revision 4.1.1.2  90/10/07  21:02:42  syd
  18.  * Fix EB29 using alternate editor all the time
  19.  * From: Michael Clay
  20.  * 
  21.  * Revision 4.1.1.1  90/07/12  22:43:05  syd
  22.  * Make it aware of the fact that we loose the cursor position on
  23.  * some system calls, so set it far enough off an absolute move will
  24.  * be done on the next cursor address, and then place it where we want it.
  25.  * From: Syd, reported by Douglas Lamb
  26.  * 
  27.  * Revision 4.1  90/04/28  22:42:46  syd
  28.  * checkin of Elm 2.3 as of Release PL0
  29.  * 
  30.  *
  31.  ******************************************************************************/
  32.  
  33. /** This routine is for allowing the user to edit their current folder
  34.     as they wish. 
  35.  
  36. **/
  37.  
  38. #include "headers.h"
  39. #include <errno.h>
  40.  
  41. extern int errno;
  42.  
  43. char   *error_name(), *error_description(); /* *strcpy(); */
  44. long   bytes();
  45. unsigned long sleep();
  46.  
  47. #ifdef ALLOW_MAILBOX_EDITING
  48.  
  49. edit_mailbox()
  50. {
  51.     /** Allow the user to edit their folder, always resynchronizing
  52.         afterwards.   Due to intense laziness on the part of the
  53.         programmer, this routine will invoke $EDITOR on the entire
  54.         file.  The mailer will ALWAYS resync on the folder
  55.         even if nothing has changed since, not unreasonably, it's
  56.         hard to figure out what occurred in the edit session...
  57.     
  58.         Also note that if the user wants to edit their incoming
  59.         mailbox they'll actually be editing the tempfile that is
  60.         an exact copy.  More on how we resync in that case later
  61.         in this code.
  62.     **/
  63.  
  64.     FILE     *real_folder, *temp_folder;
  65.     char     edited_file[SLEN], buffer[SLEN];
  66.  
  67.     if(folder_type == SPOOL) {
  68.       if(save_file_stats(cur_folder) != 0) {
  69.         error1("Problems saving permissions of folder %s!", cur_folder);
  70.         Raw(ON);
  71.         sleep(2);
  72.         return(0);
  73.       }
  74.     }
  75.       
  76.     PutLine0(LINES-1,0,"Invoking editor...");
  77.  
  78.     strcpy(edited_file, (folder_type == NON_SPOOL ? cur_folder : cur_tempfolder));
  79.     if (strcmp(editor, "builtin") == 0 || strcmp(editor, "none") == 0)
  80.       sprintf(buffer, "%s %s", alternative_editor, edited_file);
  81.     else
  82.       sprintf(buffer, "%s %s", editor, edited_file);
  83.  
  84.     Raw(OFF);
  85.  
  86.     if (system_call(buffer, SH, TRUE, FALSE) != 0) {
  87.       error1("Problems invoking editor %s!", alternative_editor);
  88.       Raw(ON);
  89.       sleep(2);
  90.       return(0);
  91.     }
  92.  
  93.     Raw(ON);
  94.     SetXYLocation(0, 40);    /* a location not near the next request, so an absolute is used */
  95.  
  96.     if (folder_type == SPOOL) {    /* uh oh... now the toughie...  */
  97.  
  98.       if (bytes(cur_folder) != mailfile_size) {
  99.  
  100.          /* SIGH.  We've received mail since we invoked the editor
  101.         on the folder.  We'll have to do some strange stuff to
  102.             remedy the problem... */
  103.  
  104.          PutLine0(LINES, 0, "Warning: new mail received...");
  105.          CleartoEOLN();
  106.  
  107.          if ((temp_folder = fopen(edited_file, "a")) == NULL) {
  108.            dprint(1, (debugfile, 
  109.             "Attempt to open \"%s\" to append failed in %s\n", 
  110.             edited_file, "edit_mailbox"));
  111.            set_error("Couldn't reopen tempfile. Edit LOST!");
  112.            return(1);
  113.          }
  114.          /** Now let's lock the folder up and stream the new stuff 
  115.          into the temp file... **/
  116.  
  117.          lock(OUTGOING);    
  118.          if ((real_folder = fopen(cur_folder, "r")) == NULL) {
  119.            dprint(1, (debugfile, 
  120.                "Attempt to open \"%s\" for reading new mail failed in %s\n",
  121.             cur_folder, "edit_mailbox"));
  122.            sprintf(buffer, "Couldn't open %s for reading!  Edit LOST!", 
  123.                cur_folder);
  124.            set_error(buffer);
  125.            unlock();
  126.            return(1);
  127.          }
  128.          if (fseek(real_folder, mailfile_size, 0) == -1) {
  129.            dprint(1, (debugfile,
  130.             "Couldn't seek to end of cur_folder (offset %ld) (%s)\n",
  131.             mailfile_size, "edit_mailbox"));
  132.            set_error("Couldn't seek to end of folder.  Edit LOST!");
  133.            unlock();
  134.            return(1);
  135.          }
  136.     
  137.          /** Now we can finally stream the new mail into the tempfile **/
  138.  
  139.          while (fgets(buffer, SLEN, real_folder) != NULL)
  140.            fprintf(temp_folder, "%s", buffer);
  141.  
  142.          fclose(real_folder);
  143.          fclose(temp_folder);
  144.  
  145.         } else lock(OUTGOING);
  146.  
  147.        /* remove real mail_file and then
  148.         * link or copy the edited mailfile to real mail_file */
  149.  
  150.        (void)unlink(cur_folder);
  151.  
  152.        if (link(edited_file, cur_folder) != 0)  {
  153.          if (errno == EXDEV || errno == EEXIST) {
  154.            /* attempt to link across file systems */
  155.               if (copy(edited_file, cur_folder) != 0) {
  156.          Write_to_screen(
  157.             "\n\rCouldn't copy %s to mailfile %s!\n\r",
  158.             2, edited_file, cur_folder);
  159.          Write_to_screen(
  160.             "\n\rYou'll need to check out %s for your mail.\n\r",
  161.             1, edited_file);
  162.          Write_to_screen("** %s - %s. **\n\r", 2,
  163.             error_name(errno), error_description(errno));
  164.          unlock();                    /* ciao!*/
  165.          emergency_exit();
  166.            }
  167.          } else {
  168.         Write_to_screen("\n\rCouldn't link %s to mailfile %s!\n\r",2,
  169.           edited_file, cur_folder);
  170.             Write_to_screen(
  171.           "\n\rYou'll need to check out %s for your mail.\n\r",
  172.           1, edited_file);
  173.         Write_to_screen("** %s - %s. **\n\r", 2,
  174.           error_name(errno), error_description(errno));
  175.             unlock();                    /* ciao!*/
  176.             emergency_exit();
  177.          }
  178.        }
  179.  
  180.        /* restore file permissions before removing lock */
  181.  
  182.        if(restore_file_stats(cur_folder) != 1) {
  183.          error1("Problems restoring permissions of folder %s!", cur_folder);
  184.          Raw(ON);
  185.          sleep(2);
  186.        }
  187.           
  188.        unlock();
  189.        unlink(edited_file);    /* remove the edited mailfile */
  190.        error("Changes incorporated into new mail...");
  191.  
  192.     } else 
  193.       error("Resynchronizing with new version of folder...");
  194.  
  195.     sleep(2);
  196.     ClearScreen();
  197.     newmbox(cur_folder, FALSE);
  198.     showscreen();
  199.     return(1);
  200. }
  201.  
  202. #endif
  203.