diff --git a/src/diff.c b/src/diff.c index af40b1295a..67d941c35a 100644 --- a/src/diff.c +++ b/src/diff.c @@ -25,6 +25,7 @@ #include "undo.h" #include "window.h" #include "os/os.h" +#include "os/shell.h" static int diff_busy = FALSE; // ex_diffgetput() is busy @@ -840,7 +841,11 @@ static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff) tmp_orig, tmp_new); append_redir(cmd, (int)len, p_srr, tmp_diff); block_autocmds(); /* Avoid ShellCmdPost stuff */ - (void)call_shell(cmd, SHELL_FILTER | SHELL_SILENT | SHELL_DOOUT, NULL); + (void)call_shell( + cmd, + kShellOptFilter | kShellOptSilent | kShellOptDoOut, + NULL + ); unblock_autocmds(); vim_free(cmd); } @@ -943,7 +948,7 @@ void ex_diffpatch(exarg_T *eap) #endif // ifdef UNIX // Avoid ShellCmdPost stuff block_autocmds(); - (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED, NULL); + (void)call_shell(buf, kShellOptFilter | kShellOptCooked, NULL); unblock_autocmds(); } diff --git a/src/eval.c b/src/eval.c index 847082aebf..f3644cceb0 100644 --- a/src/eval.c +++ b/src/eval.c @@ -61,6 +61,7 @@ #include "version.h" #include "window.h" #include "os/os.h" +#include "os/shell.h" #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) # include @@ -14543,7 +14544,7 @@ static void f_system(typval_T *argvars, typval_T *rettv) } res = get_cmd_output(get_tv_string(&argvars[0]), infile, - SHELL_SILENT | SHELL_COOKED); + kShellOptSilent | kShellOptCooked); #ifdef USE_CR /* translate into */ diff --git a/src/ex_cmds.c b/src/ex_cmds.c index 335425b919..85b91741aa 100644 --- a/src/ex_cmds.c +++ b/src/ex_cmds.c @@ -57,6 +57,7 @@ #include "undo.h" #include "window.h" #include "os/os.h" +#include "os/shell.h" static int linelen(int *has_tab); static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, @@ -1023,21 +1024,21 @@ do_filter ( */ if (do_out) - shell_flags |= SHELL_DOOUT; + shell_flags |= kShellOptDoOut; if (!do_in && do_out && !p_stmp) { // Use a pipe to fetch stdout of the command, do not use a temp file. - shell_flags |= SHELL_READ; + shell_flags |= kShellOptRead; curwin->w_cursor.lnum = line2; } else if (do_in && !do_out && !p_stmp) { // Use a pipe to write stdin of the command, do not use a temp file. - shell_flags |= SHELL_WRITE; + shell_flags |= kShellOptWrite; curbuf->b_op_start.lnum = line1; curbuf->b_op_end.lnum = line2; } else if (do_in && do_out && !p_stmp) { // Use a pipe to write stdin and fetch stdout of the command, do not // use a temp file. - shell_flags |= SHELL_READ|SHELL_WRITE; + shell_flags |= kShellOptRead | kShellOptWrite; curbuf->b_op_start.lnum = line1; curbuf->b_op_end.lnum = line2; curwin->w_cursor.lnum = line2; @@ -1100,9 +1101,13 @@ do_filter ( * 'u' to fix the text * Switch to cooked mode when not redirecting stdin, avoids that something * like ":r !cat" hangs. - * Pass on the SHELL_DOOUT flag when the output is being redirected. + * Pass on the kShellDoOut flag when the output is being redirected. */ - if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags, NULL)) { + if (call_shell( + cmd_buf, + kShellOptFilter | kShellOptCooked | shell_flags, + NULL + )) { redraw_later_clear(); wait_return(FALSE); } @@ -1133,7 +1138,7 @@ do_filter ( read_linecount = curbuf->b_ml.ml_line_count - read_linecount; - if (shell_flags & SHELL_READ) { + if (shell_flags & kShellOptRead) { curbuf->b_op_start.lnum = line2 + 1; curbuf->b_op_end.lnum = curwin->w_cursor.lnum; appended_lines_mark(line2, read_linecount); @@ -1256,7 +1261,7 @@ do_shell ( if (!swapping_screen()) windgoto(msg_row, msg_col); cursor_on(); - (void)call_shell(cmd, SHELL_COOKED | flags, NULL); + (void)call_shell(cmd, kShellOptCooked | flags, NULL); did_check_timestamps = FALSE; need_check_timestamps = TRUE; diff --git a/src/ex_cmds2.c b/src/ex_cmds2.c index ffa91626f3..8ea817f6ec 100644 --- a/src/ex_cmds2.c +++ b/src/ex_cmds2.c @@ -43,6 +43,7 @@ #include "undo.h" #include "window.h" #include "os/os.h" +#include "os/shell.h" static void cmd_source(char_u *fname, exarg_T *eap); @@ -3518,7 +3519,7 @@ static char_u **find_locales(void) /* Find all available locales by running command "locale -a". If this * doesn't work we won't have completion. */ char_u *locale_a = get_cmd_output((char_u *)"locale -a", - NULL, SHELL_SILENT); + NULL, kShellOptSilent); if (locale_a == NULL) return NULL; ga_init2(&locales_ga, sizeof(char_u *), 20); diff --git a/src/misc1.c b/src/misc1.c index 8b96bb2235..1fbc14885d 100644 --- a/src/misc1.c +++ b/src/misc1.c @@ -49,6 +49,7 @@ #include "undo.h" #include "window.h" #include "os/os.h" +#include "os/shell.h" static char_u *vim_version_dir(char_u *vimdir); static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); static void init_users(void); @@ -3670,7 +3671,7 @@ get_cmd_output ( * Don't check timestamps here. */ ++no_check_timestamps; - call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags, NULL); + call_shell(command, kShellOptDoOut | kShellOptExpand | flags, NULL); --no_check_timestamps; vim_free(command); diff --git a/src/os/shell.h b/src/os/shell.h index c9dbc4ce9e..8cee4b8a90 100644 --- a/src/os/shell.h +++ b/src/os/shell.h @@ -5,6 +5,17 @@ #include "types.h" +// Flags for mch_call_shell() second argument +typedef enum { + kShellOptFilter = 1, // filtering text + kShellOptExpand = 2, // expanding wildcards + kShellOptCooked = 4, // set term to cooked mode + kShellOptDoOut = 8, // redirecting output + kShellOptSilent = 16, // don't print error returned by command + kShellOptRead = 32, // read lines and insert into buffer + kShellOptWrite = 64 // write lines from buffer +} ShellOpts; + char ** shell_build_argv(char_u *cmd, char_u *extra_shell_arg); void shell_free_argv(char **argv); diff --git a/src/os_unix.c b/src/os_unix.c index 3efa1a26c2..0a6a8a2794 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -1681,7 +1681,7 @@ waitstatus *status; return wait_pid; } -int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) +int mch_call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg) { int tmode = cur_tmode; @@ -1708,7 +1708,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) int did_settmode = FALSE; /* settmode(TMODE_RAW) called */ out_flush(); - if (options & SHELL_COOKED) + if (opts & kShellOptCooked) settmode(TMODE_COOK); /* set to normal mode */ argv = shell_build_argv(cmd, extra_shell_arg); @@ -1722,8 +1722,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) * input from the buffer: Try using a pseudo-tty to get the stdin/stdout * of the executed command into the Vim window. Or use a pipe. */ - if ((options & (SHELL_READ|SHELL_WRITE)) - ) { + if (opts & (kShellOptRead|kShellOptWrite)) { { pipe_error = (pipe(fd_toshell) < 0); if (!pipe_error) { /* pipe create OK */ @@ -1744,8 +1743,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) if ((pid = fork()) == -1) { /* maybe we should use vfork() */ MSG_PUTS(_("\nCannot fork\n")); - if ((options & (SHELL_READ|SHELL_WRITE)) - ) { + if (opts & (kShellOptRead | kShellOptWrite)) { { close(fd_toshell[0]); close(fd_toshell[1]); @@ -1756,7 +1754,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) } else if (pid == 0) { /* child */ reset_signals(); /* handle signals normally */ - if (!show_shell_mess || (options & SHELL_EXPAND)) { + if (!show_shell_mess || (opts & kShellOptExpand)) { int fd; /* @@ -1789,8 +1787,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) /* Don't need this now that we've duplicated it */ close(fd); } - } else if ((options & (SHELL_READ|SHELL_WRITE)) - ) { + } else if (opts & (kShellOptRead|kShellOptWrite)) { # ifdef HAVE_SETSID /* Create our own process group, so that the child and all its @@ -1860,8 +1857,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) * This is also used to pipe stdin/stdout to/from the external * command. */ - if ((options & (SHELL_READ|SHELL_WRITE)) - ) { + if (opts & (kShellOptRead|kShellOptWrite)) { # define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */ char_u buffer[BUFLEN + 1]; int buffer_off = 0; /* valid bytes in buffer[] */ @@ -1907,7 +1903,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) old_State = State; State = EXTERNCMD; /* don't redraw at window resize */ - if ((options & SHELL_WRITE) && toshell_fd >= 0) { + if ((opts & kShellOptWrite) && toshell_fd >= 0) { /* Fork a process that will write the lines to the * external program. */ if ((wpid = fork()) == -1) { @@ -1963,7 +1959,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) } } - if (options & SHELL_READ) + if (opts & kShellOptRead) ga_init2(&ga, 1, BUFLEN); noread_cnt = 0; @@ -1986,10 +1982,10 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) * typeahead. */ len = 0; - if (!(options & SHELL_EXPAND) - && ((options & - (SHELL_READ|SHELL_WRITE|SHELL_COOKED)) - != (SHELL_READ|SHELL_WRITE|SHELL_COOKED) + if (!(opts & kShellOptExpand) + && ((opts & + (kShellOptRead|kShellOptWrite|kShellOptCooked)) + != (kShellOptRead|kShellOptWrite|kShellOptCooked) ) && wait_pid == 0 && (ta_len > 0 || noread_cnt > 4)) { @@ -2081,7 +2077,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) * When writing buffer lines, drop the typed * characters (only check for CTRL-C). */ - if (options & SHELL_WRITE) + if (opts & kShellOptWrite) ta_len = 0; else if (toshell_fd >= 0) { len = write(toshell_fd, (char *)ta_buf, (size_t)1); @@ -2124,7 +2120,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) goto finished; noread_cnt = 0; - if (options & SHELL_READ) { + if (opts & kShellOptRead) { /* Do NUL -> NL translation, append NL separated * lines to the current buffer. */ for (i = 0; i < len; ++i) { @@ -2227,7 +2223,7 @@ int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg) } finished: p_more = p_more_save; - if (options & SHELL_READ) { + if (opts & kShellOptRead) { if (ga.ga_len > 0) { append_ga_line(&ga); /* remember that the NL was missing */ @@ -2283,7 +2279,7 @@ finished: MSG_PUTS(_("\nCannot execute shell ")); msg_outtrans(p_sh); msg_putchar('\n'); - } else if (!(options & SHELL_SILENT)) { + } else if (!(opts & kShellOptSilent)) { MSG_PUTS(_("\nshell returned ")); msg_outnum((long)retval); msg_putchar('\n'); @@ -2663,7 +2659,11 @@ int flags; /* EW_* flags */ /* * execute the shell command */ - i = call_shell(command, SHELL_EXPAND | SHELL_SILENT, extra_shell_arg); + i = call_shell( + command, + kShellOptExpand | kShellOptSilent, + extra_shell_arg + ); /* When running in the background, give it some time to create the temp * file, but don't wait for it to finish. */ diff --git a/src/os_unix.h b/src/os_unix.h index 48ce4f8e87..6b35d6822b 100644 --- a/src/os_unix.h +++ b/src/os_unix.h @@ -1,5 +1,8 @@ #ifndef NEOVIM_OS_UNIX_H #define NEOVIM_OS_UNIX_H + +#include "os/shell.h" + /* os_unix.c */ void mch_write(char_u *s, int len); void mch_startjmp(void); @@ -42,7 +45,7 @@ int mch_screenmode(char_u *arg); int mch_get_shellsize(void); void mch_set_shellsize(void); void mch_new_shellsize(void); -int mch_call_shell(char_u *cmd, int options, char_u *extra_shell_arg); +int mch_call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg); int mch_expandpath(garray_T *gap, char_u *path, int flags); int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, diff --git a/src/path.c b/src/path.c index f643c8ec82..eceb5ca64d 100644 --- a/src/path.c +++ b/src/path.c @@ -9,6 +9,7 @@ #include "misc1.h" #include "misc2.h" #include "os/os.h" +#include "os/shell.h" #include "os_unix.h" #include "regexp.h" #include "tag.h" @@ -1212,7 +1213,7 @@ expand_backtick ( buffer = eval_to_string(cmd + 1, &p, TRUE); else buffer = get_cmd_output(cmd, NULL, - (flags & EW_SILENT) ? SHELL_SILENT : 0); + (flags & EW_SILENT) ? kShellOptSilent : 0); vim_free(cmd); if (buffer == NULL) return 0; diff --git a/src/quickfix.c b/src/quickfix.c index 90a60ce485..a0ec091ec7 100644 --- a/src/quickfix.c +++ b/src/quickfix.c @@ -2546,7 +2546,7 @@ void ex_make(exarg_T *eap) msg_outtrans(cmd); /* show what we are doing */ /* let the shell know if we are redirecting output or not */ - do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0); + do_shell(cmd, *p_sp != NUL ? kShellOptDoOut : 0); res = qf_init(wp, fname, (eap->cmdidx != CMD_make diff --git a/src/vim.h b/src/vim.h index d45a61388b..cb33a3e17f 100644 --- a/src/vim.h +++ b/src/vim.h @@ -489,15 +489,6 @@ enum { #define REMAP_SCRIPT -2 /* remap script-local mappings only */ #define REMAP_SKIP -3 /* no remapping for first char */ -/* Values for mch_call_shell() second argument */ -#define SHELL_FILTER 1 /* filtering text */ -#define SHELL_EXPAND 2 /* expanding wildcards */ -#define SHELL_COOKED 4 /* set term to cooked mode */ -#define SHELL_DOOUT 8 /* redirecting output */ -#define SHELL_SILENT 16 /* don't print error returned by command */ -#define SHELL_READ 32 /* read lines and insert into buffer */ -#define SHELL_WRITE 64 /* write lines from buffer */ - /* Values returned by mch_nodetype() */ #define NODE_NORMAL 0 /* file or directory, check with os_isdir()*/ #define NODE_WRITABLE 1 /* something we can write to (character