home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lynx2.8.1dev.10.tar.gz / lynx2.8.1dev.10.tar / lynx2-8 / src / LYEdit.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  4KB  |  192 lines

  1. #include <HTUtils.h>
  2. #include <tcp.h>
  3. #include <HTParse.h>
  4. #include <HTAlert.h>
  5. #include <LYCurses.h>
  6. #include <LYSignal.h>
  7. #include <LYUtils.h>
  8. #include <LYClean.h>
  9. #include <LYGlobalDefs.h>
  10. #include <LYEdit.h>
  11. #include <LYStrings.h>
  12. #include <LYSystem.h>
  13. #ifdef VMS
  14. #include <unixio.h>
  15. #include <HTVMSUtils.h>
  16. #endif /* VMS */
  17. #ifdef DOSPATH
  18. #include <HTDOS.h>
  19. #endif
  20.  
  21. #include <LYLeaks.h>
  22.  
  23. #define FREE(x) if (x) {free(x); x = NULL;}
  24.  
  25. /*
  26.  *  In edit mode invoke either emacs, vi, pico, jove, jed sedt or the
  27.  *  default editor to display and edit the current file.
  28.  *  For emacs, vi, pico, jove and jed, Lynx will open the file to the
  29.  *  same line that the screen cursor is on when editing is invoked.
  30.  *  Returns FALSE if file is uneditable.
  31.  */
  32. PUBLIC int edit_current_file ARGS3(
  33.     char *,        newfile,
  34.     int,        cur,
  35.     int,        lineno)
  36. {
  37.     char command[512];
  38.     char *filename = NULL;
  39.     char *colon, *number_sign;
  40.     FILE *fp;
  41.  
  42.     /*
  43.      *  If its a remote file then we can't edit it.
  44.      */
  45.     if (!LYisLocalFile(newfile)) {
  46.     _statusline(CANNOT_EDIT_REMOTE_FILES);
  47.     sleep(MessageSecs);
  48.     return FALSE;
  49.     }
  50.  
  51.     /*
  52.      *  If there's a fragment, trim it. - FM
  53.      */
  54.     number_sign = strchr(newfile, '#');
  55.     if (number_sign)
  56.     *number_sign = '\0';
  57.  
  58.     /*
  59.      *  On Unix, first try to open it as a completely referenced file,
  60.      *  then via the path alone.
  61.      *
  62.      * On VMS, only try the path.
  63.      */
  64. #if !defined (VMS) && !defined (DOSPATH) && !defined (__EMX__)
  65.     colon = strchr(newfile, ':');
  66.     StrAllocCopy(filename, (colon + 1));
  67.     HTUnEscape(filename);
  68.     if ((fp = fopen(filename, "r")) == NULL) {
  69.     FREE(filename);
  70. #endif /* !VMS */
  71.     filename = HTParse(newfile, "", PARSE_PATH+PARSE_PUNCTUATION);
  72.     HTUnEscape(filename);
  73. #if defined (DOSPATH) || defined (__EMX__)
  74.     if (strlen(filename)>1) filename++;
  75. #endif
  76. #ifdef DOSPATH
  77.     if ((fp = fopen(HTDOS_name(filename),"r")) == NULL) {
  78. #else
  79. #ifdef VMS
  80.     if ((fp = fopen(HTVMS_name("", filename), "r")) == NULL) {
  81. #else
  82.     if ((fp = fopen(filename, "r")) == NULL) {
  83. #endif /* VMS */
  84. #endif /* DOSPATH */
  85.         HTAlert(COULD_NOT_ACCESS_FILE);
  86.         FREE(filename);
  87.         goto failure;
  88.     }
  89. #if !defined (VMS) && !defined (DOSPATH) && !defined (__EMX__)
  90.     }
  91. #endif /* !VMS */
  92.     fclose(fp);
  93.  
  94. #if defined(VMS) || defined(CANT_EDIT_UNWRITABLE_FILES)
  95.     /*
  96.      *  Don't allow editing if user lacks append access.
  97.      */
  98. #ifdef DOSPATH
  99.     if ((fp = fopen(HTDOS_name("", filename), "a")) == NULL) {
  100. #else
  101. #ifdef VMS
  102.     if ((fp = fopen(HTVMS_name("", filename), "a")) == NULL) {
  103. #else
  104.     if ((fp = fopen(filename, "a")) == NULL) {
  105. #endif /* VMS */
  106. #endif /* DOSPATH */
  107.     _statusline(NOAUTH_TO_EDIT_FILE);
  108.     sleep(MessageSecs);
  109.     goto failure;
  110.     }
  111.     fclose(fp);
  112. #endif /* VMS || CANT_EDIT_UNWRITABLE_FILES */
  113.  
  114.     /*
  115.      *  Make sure cur is at least zero. - FM
  116.      */
  117.     if (cur < 0) {
  118.     cur = 0;
  119.     }
  120.  
  121.     /*
  122.      *  Set up the command for the editor. - FM
  123.      */
  124. #ifdef VMS
  125.     if ((strstr(editor, "sedt") || strstr(editor, "SEDT")) &&
  126.     ((lineno - 1) + (nlinks ? links[cur].ly : 0)) > 0) {
  127.     sprintf(command, "%s %s -%d",
  128.              editor,
  129.              HTVMS_name("", filename),
  130.              ((lineno - 1) + (nlinks ? links[cur].ly : 0)));
  131.     } else {
  132.     sprintf(command, "%s %s", editor, HTVMS_name("", filename));
  133.     }
  134. #else
  135.     if (strstr(editor, "emacs") || strstr(editor, "vi") ||
  136.     strstr(editor, "pico") || strstr(editor, "jove") ||
  137.     strstr(editor, "jed"))
  138.     sprintf(command, "%s +%d \"%s\"",
  139.              editor,
  140.              (lineno + (nlinks ? links[cur].ly : 0)),
  141. #ifdef DOSPATH
  142.              HTDOS_name(filename));
  143. #else
  144.              filename);
  145. #endif /* DOSPATH */
  146.     else
  147. #ifdef __DJGPP__
  148.     sprintf(command, "%s %s", editor, HTDOS_name(filename));
  149. #else
  150.     sprintf(command, "%s \"%s\"", editor,
  151. #ifdef DOSPATH
  152.                  HTDOS_name(filename));
  153. #else
  154.                  filename);
  155. #endif /* DOSPATH */
  156. #endif /* __DJGPP__ */
  157. #endif /* VMS */
  158.     if (TRACE) {
  159.     fprintf(tfp, "LYEdit: %s\n", command);
  160.     sleep(MessageSecs);
  161.     }
  162. #ifndef __EMX__
  163.     FREE(filename);
  164. #endif
  165.  
  166.     /*
  167.      *  Invoke the editor. - FM
  168.      */
  169.     fflush(stderr);
  170.     fflush(stdout);
  171.     stop_curses();
  172.     system(command);
  173.     fflush(stdout);
  174.     fflush(stderr);
  175.     start_curses();
  176.  
  177.     /*
  178.      *  Restore the fragment if there was one. - FM
  179.      */
  180.     if (number_sign)
  181.     *number_sign = '#';
  182.     return TRUE;
  183.  
  184. failure:
  185.     /*
  186.      *  Restore the fragment if there was one. - FM
  187.      */
  188.     if (number_sign)
  189.     *number_sign = '#';
  190.     return FALSE;
  191. }
  192.