]> git.proxmox.com Git - systemd.git/blame - src/basic/escape.h
New upstream version 249~rc1
[systemd.git] / src / basic / escape.h
CommitLineData
a032b68d 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
db2df898
MP
2#pragma once
3
db2df898 4#include <inttypes.h>
4c89c718
MP
5#include <stddef.h>
6#include <stdint.h>
7#include <sys/types.h>
8#include <uchar.h>
9
10#include "string-util.h"
6e866b33 11#include "missing_type.h"
db2df898
MP
12
13/* What characters are special in the shell? */
14/* must be escaped outside and inside double-quotes */
15#define SHELL_NEED_ESCAPE "\"\\`$"
81c58355
MB
16
17/* Those that can be escaped or double-quoted.
18 *
9e294e28 19 * Strictly speaking, ! does not need to be escaped, except in interactive
81c58355
MB
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 "\\\'"
db2df898
MP
30
31typedef enum UnescapeFlags {
46cdbd49
BR
32 UNESCAPE_RELAX = 1 << 0,
33 UNESCAPE_ACCEPT_NUL = 1 << 1,
db2df898
MP
34} UnescapeFlags;
35
8b3d4ff0
MB
36typedef 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;
81c58355 43
a032b68d
MB
44char* cescape(const char *s);
45char* cescape_length(const char *s, size_t n);
b012e921 46int cescape_char(char c, char *buf);
db2df898 47
db2df898 48int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
46cdbd49
BR
49static inline int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
50 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
51}
52static inline int cunescape(const char *s, UnescapeFlags flags, char **ret) {
53 return cunescape_length(s, strlen(s), flags, ret);
54}
55int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul);
db2df898 56
8b3d4ff0
MB
57typedef enum XEscapeFlags {
58 XESCAPE_8_BIT = 1 << 0,
59 XESCAPE_FORCE_ELLIPSIS = 1 << 1,
60} XEscapeFlags;
61
62char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags);
a032b68d 63static inline char* xescape(const char *s, const char *bad) {
8b3d4ff0 64 return xescape_full(s, bad, SIZE_MAX, 0);
f2dec872 65}
a032b68d 66char* octescape(const char *s, size_t len);
8b3d4ff0 67char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags);
db2df898 68
a032b68d 69char* shell_escape(const char *s, const char *bad);
8b3d4ff0 70char* shell_maybe_quote(const char *s, ShellEscapeFlags flags);