diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-07-13 13:30:16 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-07-13 16:01:00 -0400 |
commit | 2c396fce53100cad4e472f29851f07030a80ee50 (patch) | |
tree | 4bbc7b2d313335600474b7370298a23b460ee356 /src/bfstd.h | |
parent | e79f0d038d3ce916e744fd111b70d687f699c0bd (diff) | |
download | bfs-2c396fce53100cad4e472f29851f07030a80ee50.tar.xz |
bfstd: Support wordesc() without allocating
Diffstat (limited to 'src/bfstd.h')
-rw-r--r-- | src/bfstd.h | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/bfstd.h b/src/bfstd.h index 832db66..fb77399 100644 --- a/src/bfstd.h +++ b/src/bfstd.h @@ -320,14 +320,53 @@ size_t xstrwidth(const char *str); // #include <wordexp.h> /** + * Flags for wordesc(). + */ +enum wesc_flags { + /** + * Escape special characters so that the shell will treat the escaped + * string as a single word. + */ + WESC_SHELL = 1 << 0, + /** + * Escape special characters so that the escaped string is safe to print + * to a TTY. + */ + WESC_TTY = 1 << 1, +}; + +/** * Escape a string as a single shell word. * - * @param str + * @param dest + * The destination string to fill. + * @param end + * The end of the destination buffer. + * @param src + * The string to escape. + * @param flags + * Controls which characters to escape. + * @return + * The new NUL terminator of the destination, or `end` on truncation. + */ +char *wordesc(char *dest, char *end, const char *str, enum wesc_flags flags); + +/** + * Escape a string as a single shell word. + * + * @param dest + * The destination string to fill. + * @param end + * The end of the destination buffer. + * @param src * The string to escape. + * @param n + * The maximum length of the string. + * @param flags + * Controls which characters to escape. * @return - * A string that a shell would evaluate to str, dynamically allocated, - * or NULL on failure. + * The new NUL terminator of the destination, or `end` on truncation. */ -char *wordesc(const char *str); +char *wordnesc(char *dest, char *end, const char *str, size_t n, enum wesc_flags flags); #endif // BFS_BFSTD_H |