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

Data Structures

struct  DynamicBuffer
 

Macros

#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
 
#define MAX_CHUNK_SIZE   (OUT_DATA_THRESHOLD / 2)
 

Functions

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 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 MAX_CHUNK_SIZE   (OUT_DATA_THRESHOLD / 2)
#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

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.

Parameters
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.
Returns
shell command exit code
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.

Parameters
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.
Returns
OK for success or FAIL for error.
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);

Parameters
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)
Returns
the return code of the process, -1 if the process couldn't be started properly
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 ("...").

Returns
[allocated] argv joined to a string.
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"]

Parameters
cmdCommand string, or NULL to run an interactive shell.
extra_argsExtra arguments to the shell, or NULL.
Returns
Newly allocated argument vector. Must be freed with shell_free_argv.
void shell_free_argv ( char **  argv)

Releases the memory allocated by shell_build_argv.

Parameters
argvThe argument vector.