Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : #pragma once 3 : 4 : #include <inttypes.h> 5 : #include <stddef.h> 6 : #include <stdint.h> 7 : #include <sys/types.h> 8 : #include <uchar.h> 9 : 10 : #include "string-util.h" 11 : #include "missing_type.h" 12 : 13 : /* What characters are special in the shell? */ 14 : /* must be escaped outside and inside double-quotes */ 15 : #define SHELL_NEED_ESCAPE "\"\\`$" 16 : 17 : /* Those that can be escaped or double-quoted. 18 : * 19 : * Stricly speaking, ! does not need to be escaped, except in interactive 20 : * mode, but let's be extra nice to the user and quote ! in case this 21 : * output is ever used in interactive mode. */ 22 : #define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;!" 23 : 24 : /* Note that we assume control characters would need to be escaped too in 25 : * addition to the "special" characters listed here, if they appear in the 26 : * string. Current users disallow control characters. Also '"' shall not 27 : * be escaped. 28 : */ 29 : #define SHELL_NEED_ESCAPE_POSIX "\\\'" 30 : 31 : typedef enum UnescapeFlags { 32 : UNESCAPE_RELAX = 1, 33 : } UnescapeFlags; 34 : 35 : typedef enum EscapeStyle { 36 : ESCAPE_BACKSLASH = 1, 37 : ESCAPE_POSIX = 2, 38 : } EscapeStyle; 39 : 40 : char *cescape(const char *s); 41 : char *cescape_length(const char *s, size_t n); 42 : int cescape_char(char c, char *buf); 43 : 44 : int cunescape(const char *s, UnescapeFlags flags, char **ret); 45 : int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret); 46 : int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret); 47 : int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit); 48 : 49 : char *xescape_full(const char *s, const char *bad, size_t console_width, bool eight_bits); 50 1 : static inline char *xescape(const char *s, const char *bad) { 51 1 : return xescape_full(s, bad, SIZE_MAX, false); 52 : } 53 : char *octescape(const char *s, size_t len); 54 : char *escape_non_printable_full(const char *str, size_t console_width, bool eight_bit); 55 : 56 : char *shell_escape(const char *s, const char *bad); 57 : char* shell_maybe_quote(const char *s, EscapeStyle style);