]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/escape.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / escape.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <inttypes.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <uchar.h>
28
29 #include "string-util.h"
30 #include "missing.h"
31
32 /* What characters are special in the shell? */
33 /* must be escaped outside and inside double-quotes */
34 #define SHELL_NEED_ESCAPE "\"\\`$"
35
36 /* Those that can be escaped or double-quoted.
37 *
38 * Stricly speaking, ! does not need to be escaped, except in interactive
39 * mode, but let's be extra nice to the user and quote ! in case this
40 * output is ever used in interactive mode. */
41 #define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;!"
42
43 /* Note that we assume control characters would need to be escaped too in
44 * addition to the "special" characters listed here, if they appear in the
45 * string. Current users disallow control characters. Also '"' shall not
46 * be escaped.
47 */
48 #define SHELL_NEED_ESCAPE_POSIX "\\\'"
49
50 typedef enum UnescapeFlags {
51 UNESCAPE_RELAX = 1,
52 } UnescapeFlags;
53
54 typedef enum EscapeStyle {
55 ESCAPE_BACKSLASH = 1,
56 ESCAPE_POSIX = 2,
57 } EscapeStyle;
58
59 char *cescape(const char *s);
60 char *cescape_length(const char *s, size_t n);
61 size_t cescape_char(char c, char *buf);
62
63 int cunescape(const char *s, UnescapeFlags flags, char **ret);
64 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret);
65 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
66 int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit);
67
68 char *xescape(const char *s, const char *bad);
69 char *octescape(const char *s, size_t len);
70
71 char *shell_escape(const char *s, const char *bad);
72 char* shell_maybe_quote(const char *s, EscapeStyle style);