| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
/* What characters are special in the shell? */ |
| 7 |
/* must be escaped outside and inside double-quotes */ |
| 8 |
#define SHELL_NEED_ESCAPE "\"\\`$" |
| 9 |
|
| 10 |
/* Those that can be escaped or double-quoted. |
| 11 |
* |
| 12 |
* Strictly speaking, ! does not need to be escaped, except in interactive |
| 13 |
* mode, but let's be extra nice to the user and quote ! in case this |
| 14 |
* output is ever used in interactive mode. */ |
| 15 |
#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;!" |
| 16 |
|
| 17 |
/* Note that we assume control characters would need to be escaped too in |
| 18 |
* addition to the "special" characters listed here, if they appear in the |
| 19 |
* string. Current users disallow control characters. Also '"' shall not |
| 20 |
* be escaped. |
| 21 |
*/ |
| 22 |
#define SHELL_NEED_ESCAPE_POSIX "\\\'" |
| 23 |
|
| 24 |
typedef enum UnescapeFlags { |
| 25 |
UNESCAPE_RELAX = 1 << 0, |
| 26 |
UNESCAPE_ACCEPT_NUL = 1 << 1, |
| 27 |
} UnescapeFlags; |
| 28 |
|
| 29 |
typedef enum ShellEscapeFlags { |
| 30 |
/* The default is to add shell quotes ("") so the shell will consider this a single argument. |
| 31 |
* Tabs and newlines are escaped. */ |
| 32 |
|
| 33 |
SHELL_ESCAPE_POSIX = 1 << 1, /* Use POSIX shell escape syntax (a string enclosed in $'') instead of plain quotes. */ |
| 34 |
SHELL_ESCAPE_EMPTY = 1 << 2, /* Format empty arguments as "". */ |
| 35 |
} ShellEscapeFlags; |
| 36 |
|
| 37 |
int cescape_char(char c, char *buf); |
| 38 |
char* cescape_length(const char *s, size_t n) _nonnull_if_nonzero_(1, 2); |
| 39 |
static inline char* cescape(const char *s) { |
| 40 |
return cescape_length(s, SIZE_MAX); |
| 41 |
} |
| 42 |
|
| 43 |
int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul); |
| 44 |
|
| 45 |
ssize_t cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret); |
| 46 |
static inline ssize_t cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) { |
| 47 |
return cunescape_length_with_prefix(s, length, NULL, flags, ret); |
| 48 |
} |
| 49 |
static inline ssize_t cunescape(const char *s, UnescapeFlags flags, char **ret) { |
| 50 |
return cunescape_length(s, SIZE_MAX, flags, ret); |
| 51 |
} |
| 52 |
|
| 53 |
typedef enum XEscapeFlags { |
| 54 |
XESCAPE_8_BIT = 1 << 0, |
| 55 |
XESCAPE_FORCE_ELLIPSIS = 1 << 1, |
| 56 |
} XEscapeFlags; |
| 57 |
|
| 58 |
char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags); |
| 59 |
static inline char* xescape(const char *s, const char *bad) { |
| 60 |
return xescape_full(s, bad, SIZE_MAX, 0); |
| 61 |
} |
| 62 |
char* octescape_full(const char *s, size_t len, const char *bad); |
| 63 |
static inline char* octescape(const char *s, size_t len) { |
| 64 |
return octescape_full(s, len, NULL); |
| 65 |
} |
| 66 |
char* decescape(const char *s, size_t len, const char *bad) _nonnull_if_nonzero_(1, 2); |
| 67 |
char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags); |
| 68 |
|
| 69 |
char* shell_escape(const char *s, const char *bad); |
| 70 |
char* shell_maybe_quote(const char *s, ShellEscapeFlags flags); |
| 71 |
char* quote_command_line(char * const *argv, ShellEscapeFlags flags); |