1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 * Strictly 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 << 0,
33 UNESCAPE_ACCEPT_NUL = 1 << 1,
34 } UnescapeFlags;
35
36 typedef enum ShellEscapeFlags {
37 /* The default is to add shell quotes ("") so the shell will consider this a single argument.
38 * Tabs and newlines are escaped. */
39
40 SHELL_ESCAPE_POSIX = 1 << 1, /* Use POSIX shell escape syntax (a string enclosed in $'') instead of plain quotes. */
41 SHELL_ESCAPE_EMPTY = 1 << 2, /* Format empty arguments as "". */
42 } ShellEscapeFlags;
43
44 char* cescape(const char *s);
45 char* cescape_length(const char *s, size_t n);
46 int cescape_char(char c, char *buf);
47
48 int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul);
49
50 ssize_t cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
cunescape_length(const char * s,size_t length,UnescapeFlags flags,char ** ret)51 static inline ssize_t cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
52 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
53 }
cunescape(const char * s,UnescapeFlags flags,char ** ret)54 static inline ssize_t cunescape(const char *s, UnescapeFlags flags, char **ret) {
55 return cunescape_length(s, strlen(s), flags, ret);
56 }
57
58 typedef enum XEscapeFlags {
59 XESCAPE_8_BIT = 1 << 0,
60 XESCAPE_FORCE_ELLIPSIS = 1 << 1,
61 } XEscapeFlags;
62
63 char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags);
xescape(const char * s,const char * bad)64 static inline char* xescape(const char *s, const char *bad) {
65 return xescape_full(s, bad, SIZE_MAX, 0);
66 }
67 char* octescape(const char *s, size_t len);
68 char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags);
69
70 char* shell_escape(const char *s, const char *bad);
71 char* shell_maybe_quote(const char *s, ShellEscapeFlags flags);
72 char* quote_command_line(char **argv, ShellEscapeFlags flags);
73