chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / escape.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <inttypes.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 #include <uchar.h>
13
14 #include "string-util.h"
15 #include "missing.h"
16
17 /* What characters are special in the shell? */
18 /* must be escaped outside and inside double-quotes */
19 #define SHELL_NEED_ESCAPE "\"\\`$"
20
21 /* Those that can be escaped or double-quoted.
22  *
23  * Stricly speaking, ! does not need to be escaped, except in interactive
24  * mode, but let's be extra nice to the user and quote ! in case this
25  * output is ever used in interactive mode. */
26 #define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;!"
27
28 /* Note that we assume control characters would need to be escaped too in
29  * addition to the "special" characters listed here, if they appear in the
30  * string. Current users disallow control characters. Also '"' shall not
31  * be escaped.
32  */
33 #define SHELL_NEED_ESCAPE_POSIX "\\\'"
34
35 typedef enum UnescapeFlags {
36         UNESCAPE_RELAX = 1,
37 } UnescapeFlags;
38
39 typedef enum EscapeStyle {
40         ESCAPE_BACKSLASH = 1,
41         ESCAPE_POSIX = 2,
42 } EscapeStyle;
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(const char *s, UnescapeFlags flags, char **ret);
49 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret);
50 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
51 int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit);
52
53 char *xescape(const char *s, const char *bad);
54 #if 0 /// UNNEEDED by elogind
55 char *octescape(const char *s, size_t len);
56
57 char *shell_escape(const char *s, const char *bad);
58 #endif // 0
59 char* shell_maybe_quote(const char *s, EscapeStyle style);