Data Structures | Macros | Functions
shell.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include "nvim/ascii.h"
#include "nvim/charset.h"
#include "nvim/eval.h"
#include "nvim/event/libuv_process.h"
#include "nvim/event/loop.h"
#include "nvim/event/rstream.h"
#include "nvim/ex_cmds.h"
#include "nvim/fileio.h"
#include "nvim/lib/kvec.h"
#include "nvim/log.h"
#include "nvim/main.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/option_defs.h"
#include "nvim/os/shell.h"
#include "nvim/os/signal.h"
#include "nvim/path.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/types.h"
#include "nvim/ui.h"
#include "nvim/vim.h"

Data Structures

struct  DynamicBuffer


#define DYNAMIC_BUFFER_INIT   { NULL, 0, 0 }
#define NS_1_SECOND   1000000000U
#define OUT_DATA_THRESHOLD   1024 * 10U
#define SHELL_SPECIAL   (char_u *)"\t \"&'$;<>()\\|"
#define STYLE_ECHO   0
#define STYLE_GLOB   1
#define STYLE_VIMGLOB   2
#define STYLE_PRINT   3
#define STYLE_BT   4


int os_expand_wildcards (int num_pat, char_u **pat, int *num_file, char_u ***file, int flags) FUNC_ATTR_NONNULL_ARG(4)
char ** shell_build_argv (const char *cmd, const char *extra_args) FUNC_ATTR_NONNULL_RET
void shell_free_argv (char **argv)
char * shell_argv_to_str (char **const argv) FUNC_ATTR_NONNULL_ALL
int os_call_shell (char_u *cmd, ShellOpts opts, char_u *extra_args)
int call_shell (char_u *cmd, ShellOpts opts, char_u *extra_shell_arg)
char_uget_cmd_output (char_u *cmd, char_u *infile, ShellOpts flags, size_t *ret_len)
int os_system (char **argv, const char *input, size_t len, char **output, size_t *nread) FUNC_ATTR_NONNULL_ARG(1)

Macro Definition Documentation


#define DYNAMIC_BUFFER_INIT   { NULL, 0, 0 }




#define NS_1_SECOND   1000000000U


#define OUT_DATA_THRESHOLD   1024 * 10U


#define SHELL_SPECIAL   (char_u *)"\t \"&'$;<>()\\|"


#define STYLE_BT   4


#define STYLE_ECHO   0


#define STYLE_GLOB   1


#define STYLE_PRINT   3


#define STYLE_VIMGLOB   2

Function Documentation

◆ call_shell()

int call_shell ( char_u cmd,
ShellOpts  opts,
char_u extra_shell_arg 

os_call_shell() wrapper. Handles 'verbose', :profile, and v:shell_error. Invalidates cached tags.

shell command exit code

◆ get_cmd_output()

char_u* get_cmd_output ( char_u cmd,
char_u infile,
ShellOpts  flags,
size_t *  ret_len 

Get the stdout of an external command. If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not NULL store the length there.

cmdcommand to execute
infileoptional input file name
flagscan be kShellOptSilent or 0
ret_lenlength of the stdout
an allocated string, or NULL for error.

◆ os_call_shell()

int os_call_shell ( char_u cmd,
ShellOpts  opts,
char_u extra_args 

Calls the user-configured 'shell' (p_sh) for running a command or wildcard expansion.

cmdThe command to execute, or NULL to run an interactive shell.
optsOptions that control how the shell will work.
extra_argsExtra arguments to the shell, or NULL.
shell command exit code

◆ os_expand_wildcards()

int os_expand_wildcards ( int  num_pat,
char_u **  pat,
int *  num_file,
char_u ***  file,
int  flags 

Performs wildcard pattern matching using the shell.

num_patis the number of input patterns.
patis an array of pointers to input patterns.
[out]num_fileis pointer to number of matched file names. Set to the number of pointers in *file.
[out]fileis pointer to array of pointers to matched file names. Memory pointed to by the initial value of *file will not be freed. Set to NULL if FAIL is returned. Otherwise points to allocated memory.
flagsis a combination of EW_* flags used in expand_wildcards(). If matching fails but EW_NOTFOUND is set in flags or there are no wildcards, the patterns from pat are copied into *file.
OK for success or FAIL for error.

◆ os_system()

int os_system ( char **  argv,
const char *  input,
size_t  len,
char **  output,
size_t *  nread 

os_system - synchronously execute a command in the shell

example: char *output = NULL; size_t nread = 0; char *argv[] = {"ls", "-la", NULL}; int exitcode = os_sytem(argv, NULL, 0, &output, &nread);

argvThe commandline arguments to be passed to the shell. argv will be consumed.
inputThe input to the shell (NULL for no input), passed to the stdin of the resulting process.
lenThe length of the input buffer (not used if input == NULL)
[out]outputPointer to a location where the output will be allocated and stored. Will point to NULL if the shell command did not output anything. If NULL is passed, the shell output will be ignored.
[out]nreadthe number of bytes in the returned buffer (if the returned buffer is not NULL)
the return code of the process, -1 if the process couldn't be started properly

◆ shell_argv_to_str()

char* shell_argv_to_str ( char **const  argv)

Joins shell arguments from argv into a new string. If the result is too long it is truncated with ellipsis ("...").

[allocated] argv joined to a string.

◆ shell_build_argv()

char** shell_build_argv ( const char *  cmd,
const char *  extra_args 

Builds the argument vector for running the user-configured 'shell' (p_sh) with an optional command prefixed by 'shellcmdflag' (p_shcf). E.g.:

["shell", "-extra_args", "-shellcmdflag", "command with spaces"]

cmdCommand string, or NULL to run an interactive shell.
extra_argsExtra arguments to the shell, or NULL.
Newly allocated argument vector. Must be freed with shell_free_argv.

◆ shell_free_argv()

void shell_free_argv ( char **  argv)

Releases the memory allocated by shell_build_argv.

argvThe argument vector.