From: Chris Allegretta Date: Thu, 21 Mar 2002 05:07:28 +0000 (+0000) Subject: External command code and fix for BUG #70 X-Git-Tag: v1.1.8~22 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=52c5a6ea48eaeb938a62daff51217811bcee676b;p=nano.git External command code and fix for BUG #70 git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1136 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/BUGS b/BUGS index c4fbe18a..731dbab5 100644 --- a/BUGS +++ b/BUGS @@ -131,6 +131,7 @@ Trevor Cordes) (68) [FIXED]. - Home and End control keys (^A, ^E) do not always work in filename prompt (bug found by Ian Turner) (69) [1.0 series only] [FIXED]. +- Trying to insert a file of 0 bytes will hang nano (70) [FIXED]. ** Open BUGS ** $Id$ diff --git a/ChangeLog b/ChangeLog index 7cd7666d..b9d0ac79 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,9 +2,14 @@ CVS code - - General - Type misalignments and mem leaks in renumber_all, do_justify and do_spell (Rocco & Steven Kneizys). + - New "External Command" code, originally by Dwayne Rightler. + New function files.c:open_pipe(), changes to do_insertfile(), + new list extcmd_list, cmd is ^X after ^R by default. - files.c: check_writable_directory() - Stat full_path, not path (Steven Kneizys). + read_file() + - Abort if we read a file of 0 lines (num_lines == 0), fixes BUG #70. - nano.c: help_init() - Capitalize Meta altkeys. diff --git a/files.c b/files.c index 6be09182..5d8f30a5 100644 --- a/files.c +++ b/files.c @@ -241,7 +241,15 @@ int read_file(int fd, char *filename, int quiet) num_lines++; buf[0] = 0; } - /* Did we even GET a file? */ + + /* Did we try to insert a file of 0 bytes? */ + if (num_lines == 0) + { + statusbar(_("Read %d lines"), 0); + return 1; + } + + /* Did we even GET a file if we don't already have one? */ if (totsize == 0 || fileptr == NULL) { new_file(); statusbar(_("Read %d lines"), num_lines); @@ -280,6 +288,45 @@ int read_file(int fd, char *filename, int quiet) return 1; } +#ifndef NANO_SMALL +int open_pipe(char *command) +{ + int forkpid, fd; + char *pipefile, *execute; + + execute = charalloc(strlen(command) + 24); + if ((pipefile = safe_tempnam(0, "nano.")) == NULL) { + statusbar(_("Could not create a temporary filename: %s"), + strerror(errno)); + free(execute); + free(pipefile); + return 1; + } + + sprintf(execute,"%s 2>&1 > %s",command,pipefile); + umask(0); + mkfifo(pipefile,0700); + forkpid = fork(); + if (forkpid == -1) { + statusbar(_("Could not fork")); + free(execute); + free(pipefile); + return 1; + } + else if (forkpid == 0) { + execl("/bin/sh","/bin/sh","-c",execute,0); + exit(0); + } + fd = open(pipefile,O_RDONLY); + read_file(fd,"stdin",0); + unlink(pipefile); + free(execute); + free(pipefile); + set_modified(); + return 0; +} +#endif /* NANO_SMALL */ + /* Open the file (and decide if it exists) */ int open_file(char *filename, int insert, int quiet) { @@ -427,7 +474,24 @@ int do_insertfile(int loading_file) } #endif - i = open_file(realname, 1, loading_file); +#ifndef NANO_SMALL + if (i == NANO_EXTCMD_KEY) { + i = statusq(1, extcmd_list, "", _("Command to execute ")); + if (i == -1) { + statusbar(_("Cancelled")); + UNSET(KEEP_CUTBUFFER); + display_main_list(); + return 0; + } + if (answer != NULL) { + i = open_pipe(answer); + } + } + else +#endif /* NANO_SMALL */ + { + i = open_file(realname, 1, loading_file); + } #ifdef ENABLE_MULTIBUFFER if (loading_file) diff --git a/global.c b/global.c index 9f9f761a..ccf5e6a0 100644 --- a/global.c +++ b/global.c @@ -99,6 +99,7 @@ shortcut *writefile_list = NULL; shortcut *insertfile_list = NULL; shortcut *help_list = NULL; shortcut *spell_list = NULL; +shortcut *extcmd_list = NULL; #ifndef DISABLE_BROWSER shortcut *browser_list = NULL; #endif @@ -268,7 +269,7 @@ void shortcut_init(int unjustify) #ifndef NANO_SMALL char *nano_tofiles_msg = "", *nano_gotodir_msg = "", *nano_case_msg = - "", *nano_reverse_msg = ""; + "", *nano_reverse_msg = "", *nano_execute_msg = ""; char *nano_dos_msg = "", *nano_mac_msg = ""; #ifdef HAVE_REGEX_H @@ -317,6 +318,7 @@ void shortcut_init(int unjustify) nano_case_msg = _("Make the current search or replace case (in)sensitive"); nano_tofiles_msg = _("Go to file browser"); + nano_execute_msg = _("Execute external command"); nano_gotodir_msg = _("Goto Directory"); nano_cancel_msg = _("Cancel the current function"); nano_append_msg = _("Append to the current file"); @@ -638,6 +640,10 @@ void shortcut_init(int unjustify) sc_init_one(&insertfile_list, NANO_TOFILES_KEY, _("To Files"), nano_tofiles_msg, 0, 0, 0, NOVIEW, 0); #endif +#ifndef NANO_SMALL + sc_init_one(&insertfile_list, NANO_EXTCMD_KEY, _("Execute Command"), + nano_execute_msg, 0, 0, 0, NOVIEW, 0); +#endif sc_init_one(&spell_list, NANO_HELP_KEY, _("Get Help"), nano_help_msg, 0, 0, 0, VIEW, do_help); @@ -645,6 +651,13 @@ void shortcut_init(int unjustify) sc_init_one(&spell_list, NANO_CANCEL_KEY, _("Cancel"), nano_cancel_msg, 0, 0, 0, VIEW, 0); +#ifndef NANO_SMALL + sc_init_one(&extcmd_list, NANO_HELP_KEY, + _("Get Help"), nano_help_msg, 0, 0, 0, VIEW, do_help); + + sc_init_one(&extcmd_list, NANO_CANCEL_KEY, _("Cancel"), + nano_cancel_msg, 0, 0, 0, VIEW, 0); +#endif #ifndef DISABLE_BROWSER diff --git a/nano.c b/nano.c index a42b3781..4685129b 100644 --- a/nano.c +++ b/nano.c @@ -2504,6 +2504,14 @@ void help_init(void) "instance of the given misspelled word in the " "current file.\n\n The following other functions are " "available in Spell Check mode:\n\n"); +#ifndef NANO_SMALL + else if (currshortcut == extcmd_list) + ptr = _("External Command Help Text\n\n " + "This menu allows you to insert the output of a command " + "run by the shell into the current buffer (or a new " + "buffer in multibuffer mode).\n\n The following keys are " + "available in this mode:\n\n"); +#endif else /* Default to the main help list */ ptr = help_text_init; diff --git a/nano.h b/nano.h index 697a7039..79527f43 100644 --- a/nano.h +++ b/nano.h @@ -289,6 +289,7 @@ know what you're doing */ #define NANO_OPENPREV_ALTKEY NANO_ALT_COMMA #define NANO_OPENNEXT_ALTKEY NANO_ALT_PERIOD #define NANO_BRACKET_KEY NANO_ALT_BRACKET +#define NANO_EXTCMD_KEY NANO_CONTROL_X #define TOGGLE_CONST_KEY NANO_ALT_C #define TOGGLE_AUTOINDENT_KEY NANO_ALT_I diff --git a/proto.h b/proto.h index 527527bb..1d91998f 100644 --- a/proto.h +++ b/proto.h @@ -72,7 +72,7 @@ extern shortcut *shortcut_list; extern shortcut *main_list, *whereis_list; extern shortcut *replace_list, *goto_list; extern shortcut *writefile_list, *insertfile_list; -extern shortcut *spell_list, *replace_list_2; +extern shortcut *spell_list, *replace_list_2, *extcmd_list; extern shortcut *help_list; #ifndef DISABLE_BROWSER extern shortcut *browser_list, *gotodir_list;