| 1 | #ifndef QUOTE_H |
| 2 | #define QUOTE_H |
| 3 | |
| 4 | struct strbuf; |
| 5 | struct strvec; |
| 6 | |
| 7 | extern int quote_path_fully; |
| 8 | |
| 9 | /* Help to copy the thing properly quoted for the shell safety. |
| 10 | * any single quote is replaced with '\'', any exclamation point |
| 11 | * is replaced with '\!', and the whole thing is enclosed in a |
| 12 | * single quote pair. |
| 13 | * |
| 14 | * For example, if you are passing the result to system() as an |
| 15 | * argument: |
| 16 | * |
| 17 | * sprintf(cmd, "foobar %s %s", sq_quote(arg0), sq_quote(arg1)) |
| 18 | * |
| 19 | * would be appropriate. If the system() is going to call ssh to |
| 20 | * run the command on the other side: |
| 21 | * |
| 22 | * sprintf(cmd, "git-diff-tree %s %s", sq_quote(arg0), sq_quote(arg1)); |
| 23 | * sprintf(rcmd, "ssh %s %s", sq_quote(host), sq_quote(cmd)); |
| 24 | * |
| 25 | * Note that the above examples leak memory! Remember to free result from |
| 26 | * sq_quote() in a real application. |
| 27 | * |
| 28 | * sq_quote_buf() writes to an existing buffer of specified size; it |
| 29 | * will return the number of characters that would have been written |
| 30 | * excluding the final null regardless of the buffer size. |
| 31 | * |
| 32 | * sq_quotef() quotes the entire formatted string as a single result. |
| 33 | */ |
| 34 | |
| 35 | void sq_quote_buf(struct strbuf *, const char *src); |
| 36 | void sq_quote_argv(struct strbuf *, const char **argv); |
| 37 | __attribute__((format (printf, 2, 3))) |
| 38 | void sq_quotef(struct strbuf *, const char *fmt, ...); |
| 39 | |
| 40 | /* |
| 41 | * These match their non-pretty variants, except that they avoid |
| 42 | * quoting when there are no exotic characters. These should only be used for |
| 43 | * human-readable output, as sq_dequote() is not smart enough to dequote it. |
| 44 | */ |
| 45 | void sq_quote_buf_pretty(struct strbuf *, const char *src); |
| 46 | void sq_quote_argv_pretty(struct strbuf *, const char **argv); |
| 47 | void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv); |
| 48 | |
| 49 | /* |
| 50 | * This unwraps what sq_quote() produces in place, but returns |
| 51 | * NULL if the input does not look like what sq_quote would have |
| 52 | * produced (the full string must be a single quoted item). |
| 53 | */ |
| 54 | char *sq_dequote(char *); |
| 55 | |
| 56 | /* |
| 57 | * Like sq_dequote(), but dequote a single item, and leave "next" pointing to |
| 58 | * the next character. E.g., in the string: |
| 59 | * |
| 60 | * 'one' 'two' 'three' |
| 61 | * |
| 62 | * after the first call, the return value would be the unquoted string "one", |
| 63 | * with "next" pointing to the space between "one" and "two"). The caller is |
| 64 | * responsible for advancing the pointer to the start of the next item before |
| 65 | * calling sq_dequote_step() again. |
| 66 | */ |
| 67 | char *sq_dequote_step(char *src, char **next); |
| 68 | |
| 69 | /* |
| 70 | * Same as the above, but can be used to unwrap many arguments in the |
| 71 | * same string separated by space. The strvec will duplicate and take |
| 72 | * ownership of the strings, but note that "arg" is still modified in-place |
| 73 | * during parsing. |
| 74 | */ |
| 75 | int sq_dequote_to_strvec(char *arg, struct strvec *); |
| 76 | |
| 77 | int unquote_c_style(struct strbuf *, const char *quoted, const char **endp); |
| 78 | |
| 79 | /* Bits in the flags parameter to quote_c_style() */ |
| 80 | #define CQUOTE_NODQ 01 |
| 81 | size_t quote_c_style(const char *name, struct strbuf *, FILE *, unsigned); |
| 82 | void quote_two_c_style(struct strbuf *, const char *, const char *, unsigned); |
| 83 | |
| 84 | void write_name_quoted(const char *name, FILE *, int terminator); |
| 85 | void write_name_quoted_relative(const char *name, const char *prefix, |
| 86 | FILE *fp, int terminator); |
| 87 | |
| 88 | /* quote path as relative to the given prefix */ |
| 89 | char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags); |
| 90 | #define QUOTE_PATH_QUOTE_SP 01 |
| 91 | |
| 92 | /* quoting as a string literal for other languages */ |
| 93 | void perl_quote_buf(struct strbuf *sb, const char *src); |
| 94 | void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len); |
| 95 | void python_quote_buf(struct strbuf *sb, const char *src); |
| 96 | void tcl_quote_buf(struct strbuf *sb, const char *src); |
| 97 | void basic_regex_quote_buf(struct strbuf *sb, const char *src); |
| 98 | |
| 99 | #endif |