2016-04-27 23:10:14 +00:00
|
|
|
// Prototypes for various functions, mostly string utilities, that are used by most parts of fish.
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_COMMON_H
|
|
|
|
#define FISH_COMMON_H
|
2017-04-20 06:43:02 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <errno.h>
|
2017-09-26 13:19:33 +00:00
|
|
|
#include <limits.h>
|
2019-02-13 13:08:43 +00:00
|
|
|
// Needed for va_list et al.
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <stdarg.h> // IWYU pragma: keep
|
2017-07-08 21:17:35 +00:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h> // IWYU pragma: keep
|
|
|
|
#endif
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <termios.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2018-08-12 02:55:06 +00:00
|
|
|
#include <algorithm>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cwchar>
|
2018-10-02 22:10:14 +00:00
|
|
|
#include <functional>
|
2022-10-27 00:24:03 +00:00
|
|
|
#include <initializer_list>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <iterator>
|
2016-06-24 00:24:19 +00:00
|
|
|
#include <memory>
|
2017-08-18 19:26:35 +00:00
|
|
|
#include <mutex>
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <string>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <utility>
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <vector>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2018-09-28 02:22:55 +00:00
|
|
|
#include "maybe.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2020-11-20 20:11:03 +00:00
|
|
|
// Create a generic define for all BSD platforms
|
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
|
|
|
#define __BSD__
|
|
|
|
#endif
|
|
|
|
|
2019-02-06 04:36:38 +00:00
|
|
|
// PATH_MAX may not exist.
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
2016-06-17 20:08:25 +00:00
|
|
|
// Define a symbol we can use elsewhere in our code to determine if we're being built on MS Windows
|
|
|
|
// under Cygwin.
|
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(__CYGWIN__) || \
|
|
|
|
defined(__WIN32__)
|
|
|
|
#define OS_IS_CYGWIN
|
|
|
|
#endif
|
|
|
|
|
2021-02-07 06:17:16 +00:00
|
|
|
// Check if Thread Sanitizer is enabled.
|
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(thread_sanitizer)
|
|
|
|
#define FISH_TSAN_WORKAROUNDS 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef __SANITIZE_THREAD__
|
|
|
|
#define FISH_TSAN_WORKAROUNDS 1
|
|
|
|
#endif
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Common string type.
|
2022-09-20 18:58:37 +00:00
|
|
|
typedef std::wstring wcstring;
|
|
|
|
typedef std::vector<wcstring> wcstring_list_t;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2020-06-08 01:47:27 +00:00
|
|
|
struct termsize_t;
|
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal ASCII value.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define ASCII_MAX 127u
|
2006-05-26 16:46:38 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal 16-bit Unicode value.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define UCS2_MAX 0xFFFFu
|
2006-05-26 16:46:38 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal byte value.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define BYTE_MAX 0xFFu
|
2006-05-26 16:46:38 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Unicode BOM value.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define UTF8_BOM_WCHAR 0xFEFFu
|
2014-11-01 23:25:28 +00:00
|
|
|
|
2022-10-29 03:07:36 +00:00
|
|
|
// Use Unicode "non-characters" for internal characters as much as we can. This
|
2016-01-22 03:56:39 +00:00
|
|
|
// gives us 32 "characters" for internal use that we can guarantee should not
|
|
|
|
// appear in our input stream. See http://www.unicode.org/faq/private_use.html.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define RESERVED_CHAR_BASE static_cast<wchar_t>(0xFDD0)
|
|
|
|
#define RESERVED_CHAR_END static_cast<wchar_t>(0xFDF0)
|
2022-10-29 03:07:36 +00:00
|
|
|
// Split the available non-character values into two ranges to ensure there are
|
2016-01-22 03:56:39 +00:00
|
|
|
// no conflicts among the places we use these special characters.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define EXPAND_RESERVED_BASE RESERVED_CHAR_BASE
|
|
|
|
#define EXPAND_RESERVED_END (EXPAND_RESERVED_BASE + 16)
|
|
|
|
#define WILDCARD_RESERVED_BASE EXPAND_RESERVED_END
|
|
|
|
#define WILDCARD_RESERVED_END (WILDCARD_RESERVED_BASE + 16)
|
2022-10-29 03:07:36 +00:00
|
|
|
// Make sure the ranges defined above don't exceed the range for non-characters.
|
2016-01-22 03:56:39 +00:00
|
|
|
// This is to make sure we didn't do something stupid in subdividing the
|
|
|
|
// Unicode range for our needs.
|
2023-02-24 23:54:46 +00:00
|
|
|
// #if WILDCARD_RESERVED_END > RESERVED_CHAR_END
|
|
|
|
// #error
|
|
|
|
// #endif
|
2016-01-22 03:56:39 +00:00
|
|
|
|
|
|
|
// These are in the Unicode private-use range. We really shouldn't use this
|
|
|
|
// range but have little choice in the matter given how our lexer/parser works.
|
|
|
|
// We can't use non-characters for these two ranges because there are only 66 of
|
|
|
|
// them and we need at least 256 + 64.
|
|
|
|
//
|
|
|
|
// If sizeof(wchar_t))==4 we could avoid using private-use chars; however, that
|
|
|
|
// would result in fish having different behavior on machines with 16 versus 32
|
|
|
|
// bit wchar_t. It's better that fish behave the same on both types of systems.
|
|
|
|
//
|
|
|
|
// Note: We don't use the highest 8 bit range (0xF800 - 0xF8FF) because we know
|
|
|
|
// of at least one use of a codepoint in that range: the Apple symbol (0xF8FF)
|
|
|
|
// on Mac OS X. See http://www.unicode.org/faq/private_use.html.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define ENCODE_DIRECT_BASE static_cast<wchar_t>(0xF600)
|
|
|
|
#define ENCODE_DIRECT_END (ENCODE_DIRECT_BASE + 256)
|
2016-04-27 23:10:14 +00:00
|
|
|
|
2017-09-26 13:19:33 +00:00
|
|
|
// NAME_MAX is not defined on Solaris
|
|
|
|
#if !defined(NAME_MAX)
|
|
|
|
#include <sys/param.h>
|
|
|
|
#if defined(MAXNAMELEN)
|
|
|
|
// MAXNAMELEN is defined on Linux, BSD, and Solaris among others
|
|
|
|
#define NAME_MAX MAXNAMELEN
|
|
|
|
#else
|
|
|
|
static_assert(false, "Neither NAME_MAX nor MAXNAMELEN is defined!");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 03:33:20 +00:00
|
|
|
// PATH_MAX may not exist.
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#else
|
|
|
|
/// Fallback length of MAXPATHLEN. Hopefully a sane value.
|
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-11-15 05:30:11 +00:00
|
|
|
enum escape_string_style_t {
|
|
|
|
STRING_STYLE_SCRIPT,
|
|
|
|
STRING_STYLE_URL,
|
|
|
|
STRING_STYLE_VAR,
|
2018-11-17 02:21:05 +00:00
|
|
|
STRING_STYLE_REGEX,
|
2018-11-15 05:30:11 +00:00
|
|
|
};
|
2017-06-21 04:55:16 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Flags for unescape_string functions.
|
|
|
|
enum {
|
2020-03-26 19:45:40 +00:00
|
|
|
UNESCAPE_DEFAULT = 0, // default behavior
|
|
|
|
UNESCAPE_SPECIAL = 1 << 0, // escape special fish syntax characters like the semicolon
|
|
|
|
UNESCAPE_INCOMPLETE = 1 << 1, // allow incomplete escape sequences
|
2020-03-08 13:03:15 +00:00
|
|
|
UNESCAPE_NO_BACKSLASHES = 1 << 2, // don't handle backslash escapes
|
2013-11-25 06:57:49 +00:00
|
|
|
};
|
2022-09-20 18:58:37 +00:00
|
|
|
typedef unsigned int unescape_flags_t;
|
2007-01-18 16:02:46 +00:00
|
|
|
|
2022-07-25 14:25:04 +00:00
|
|
|
// Flags for the escape_string() function. These are only applicable when the escape style is
|
|
|
|
// "script" (i.e., STRING_STYLE_SCRIPT).
|
2016-04-27 23:10:14 +00:00
|
|
|
enum {
|
2022-10-29 03:07:36 +00:00
|
|
|
/// Do not escape special fish syntax characters like the semicolon. Only escape non-printable
|
2022-07-25 14:25:04 +00:00
|
|
|
/// characters and backslashes.
|
|
|
|
ESCAPE_NO_PRINTABLES = 1 << 0,
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Do not try to use 'simplified' quoted escapes, and do not use empty quotes as the empty
|
|
|
|
/// string.
|
2012-07-06 21:34:53 +00:00
|
|
|
ESCAPE_NO_QUOTED = 1 << 1,
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Do not escape tildes.
|
2022-07-25 17:18:53 +00:00
|
|
|
ESCAPE_NO_TILDE = 1 << 2,
|
2022-10-29 03:07:36 +00:00
|
|
|
/// Replace non-printable control characters with Unicode symbols.
|
2022-07-25 17:18:53 +00:00
|
|
|
ESCAPE_SYMBOLIC = 1 << 3
|
2012-07-06 21:34:53 +00:00
|
|
|
};
|
2022-09-20 18:58:37 +00:00
|
|
|
typedef unsigned int escape_flags_t;
|
2007-10-06 10:51:31 +00:00
|
|
|
|
2021-05-20 18:25:32 +00:00
|
|
|
/// A user-visible job ID.
|
|
|
|
using job_id_t = int;
|
|
|
|
|
|
|
|
/// The non user-visible, never-recycled job ID.
|
|
|
|
/// Every job has a unique positive value for this.
|
|
|
|
using internal_job_id_t = uint64_t;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Exits without invoking destructors (via _exit), useful for code after fork.
|
2016-11-29 09:41:03 +00:00
|
|
|
[[noreturn]] void exit_without_destructors(int code);
|
2012-01-05 21:58:48 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Save the shell mode on startup so we can restore them on exit.
|
2012-11-19 00:30:30 +00:00
|
|
|
extern struct termios shell_modes;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// The character to use where the text has been truncated. Is an ellipsis on unicode system and a $
|
|
|
|
/// on other systems.
|
2019-04-28 22:00:36 +00:00
|
|
|
wchar_t get_ellipsis_char();
|
|
|
|
|
2018-09-28 02:28:39 +00:00
|
|
|
/// The character or string to use where text has been truncated (ellipsis if possible, otherwise
|
|
|
|
/// ...)
|
2019-04-28 22:00:36 +00:00
|
|
|
const wchar_t *get_ellipsis_str();
|
2012-11-05 08:05:42 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Character representing an omitted newline at the end of text.
|
2019-04-28 22:00:36 +00:00
|
|
|
const wchar_t *get_omitted_newline_str();
|
|
|
|
int get_omitted_newline_width();
|
2012-12-01 23:44:09 +00:00
|
|
|
|
2017-04-09 18:48:21 +00:00
|
|
|
/// Character used for the silent mode of the read command
|
2019-04-28 22:00:36 +00:00
|
|
|
wchar_t get_obfuscation_read_char();
|
2017-04-09 18:48:21 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Profiling flag. True if commands should be profiled.
|
2014-02-09 22:04:43 +00:00
|
|
|
extern bool g_profiling_active;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Name of the current program. Should be set at startup. Used by the debug function.
|
2018-11-28 14:08:24 +00:00
|
|
|
extern const wchar_t *program_name;
|
2005-10-25 09:39:45 +00:00
|
|
|
|
2018-09-29 04:20:50 +00:00
|
|
|
/// Set to false if it's been determined we can't trust the last modified timestamp on the tty.
|
|
|
|
extern const bool has_working_tty_timestamps;
|
2016-06-17 20:08:25 +00:00
|
|
|
|
2021-02-04 23:18:34 +00:00
|
|
|
/// A global, empty string. This is useful for functions which wish to return a reference to an
|
|
|
|
/// empty string.
|
|
|
|
extern const wcstring g_empty_string;
|
|
|
|
|
2023-02-11 20:31:08 +00:00
|
|
|
/// A global, empty wcstring_list_t. This is useful for functions which wish to return a reference
|
|
|
|
/// to an empty string.
|
|
|
|
extern const wcstring_list_t g_empty_string_list;
|
|
|
|
|
2016-11-29 09:41:03 +00:00
|
|
|
// Pause for input, then exit the program. If supported, print a backtrace first.
|
2017-08-18 19:26:35 +00:00
|
|
|
#define FATAL_EXIT() \
|
2019-11-20 06:17:30 +00:00
|
|
|
do { \
|
2017-08-18 19:26:35 +00:00
|
|
|
char exit_read_buff; \
|
2022-06-16 08:26:43 +00:00
|
|
|
show_stackframe(); \
|
2017-08-12 14:54:26 +00:00
|
|
|
ignore_result(read(0, &exit_read_buff, 1)); \
|
2017-08-18 19:26:35 +00:00
|
|
|
exit_without_destructors(1); \
|
2019-11-20 06:17:30 +00:00
|
|
|
} while (0)
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2017-02-14 04:37:27 +00:00
|
|
|
/// Exit the program at once after emitting an error message and stack trace if possible.
|
|
|
|
/// We use our own private implementation of `assert()` for two reasons. First, some implementations
|
|
|
|
/// are subtly broken. For example, using `printf()` which can cause problems when mixed with wide
|
|
|
|
/// stdio functions and should be writing the message to stderr rather than stdout. Second, if
|
|
|
|
/// possible it is useful to provide additional context such as a stack backtrace.
|
|
|
|
#undef assert
|
2022-08-21 12:52:39 +00:00
|
|
|
#define assert(e) likely(e) ? ((void)0) : __fish_assert(#e, __FILE__, __LINE__, 0)
|
|
|
|
#define assert_with_errno(e) likely(e) ? ((void)0) : __fish_assert(#e, __FILE__, __LINE__, errno)
|
2017-06-17 04:50:08 +00:00
|
|
|
#define DIE(msg) __fish_assert(msg, __FILE__, __LINE__, 0)
|
|
|
|
#define DIE_WITH_ERRNO(msg) __fish_assert(msg, __FILE__, __LINE__, errno)
|
2017-02-15 05:09:15 +00:00
|
|
|
/// This macro is meant to be used with functions that return zero on success otherwise return an
|
|
|
|
/// errno value. Most notably the pthread family of functions which we never expect to fail.
|
2017-06-24 06:19:09 +00:00
|
|
|
#define DIE_ON_FAILURE(e) \
|
|
|
|
do { \
|
|
|
|
int status = e; \
|
2021-11-08 19:36:01 +00:00
|
|
|
if (unlikely(status != 0)) { \
|
2017-06-17 04:50:08 +00:00
|
|
|
__fish_assert(#e, __FILE__, __LINE__, status); \
|
2017-06-24 06:19:09 +00:00
|
|
|
} \
|
2017-02-15 05:09:15 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-06-17 04:50:08 +00:00
|
|
|
[[noreturn]] void __fish_assert(const char *msg, const char *file, size_t line, int error);
|
2007-04-16 21:40:41 +00:00
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
/// Shorthand for wgettext call in situations where a C-style string is needed (e.g.,
|
|
|
|
/// std::fwprintf()).
|
2016-06-02 05:03:27 +00:00
|
|
|
#define _(wstr) wgettext(wstr).c_str()
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-06-15 02:55:30 +00:00
|
|
|
/// Noop, used to tell xgettext that a string should be translated. Use this when a string cannot be
|
|
|
|
/// passed through wgettext() at the point where it is used. For example, when initializing a
|
|
|
|
/// static array or structure. You must pass the string through wgettext() when it is used.
|
|
|
|
/// See https://developer.gnome.org/glib/stable/glib-I18N.html#N-:CAPS
|
2016-04-27 23:10:14 +00:00
|
|
|
#define N_(wstr) wstr
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2021-07-22 17:43:25 +00:00
|
|
|
/// An empty struct which may be embedded (or inherited from) to prevent copying.
|
|
|
|
struct [[gnu::unused]] noncopyable_t {
|
|
|
|
noncopyable_t() = default;
|
|
|
|
noncopyable_t(noncopyable_t &&) = default;
|
|
|
|
noncopyable_t &operator=(noncopyable_t &&) = default;
|
|
|
|
noncopyable_t(const noncopyable_t &) = delete;
|
|
|
|
noncopyable_t &operator=(const noncopyable_t &) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct [[gnu::unused]] nonmovable_t {
|
|
|
|
nonmovable_t() = default;
|
|
|
|
nonmovable_t(nonmovable_t &&) = delete;
|
|
|
|
nonmovable_t &operator=(nonmovable_t &&) = delete;
|
|
|
|
};
|
|
|
|
|
2018-09-29 04:58:44 +00:00
|
|
|
/// Test if a collection contains a value.
|
|
|
|
template <typename Col, typename T2>
|
|
|
|
bool contains(const Col &col, const T2 &val) {
|
|
|
|
return std::find(std::begin(col), std::end(col), val) != std::end(col);
|
2018-08-12 02:55:06 +00:00
|
|
|
}
|
2012-07-16 19:05:36 +00:00
|
|
|
|
2022-09-25 19:04:33 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool contains(std::initializer_list<T1> col, const T2 &val) {
|
|
|
|
return std::find(std::begin(col), std::end(col), val) != std::end(col);
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:32:15 +00:00
|
|
|
/// Append a vector \p donator to the vector \p receiver.
|
|
|
|
template <typename T>
|
|
|
|
void vec_append(std::vector<T> &receiver, std::vector<T> &&donator) {
|
2020-12-01 19:36:38 +00:00
|
|
|
if (receiver.empty()) {
|
|
|
|
receiver = std::move(donator);
|
|
|
|
} else {
|
2021-12-21 10:08:23 +00:00
|
|
|
receiver.insert(receiver.end(), std::make_move_iterator(donator.begin()),
|
2020-12-01 19:36:38 +00:00
|
|
|
std::make_move_iterator(donator.end()));
|
|
|
|
}
|
2018-09-10 01:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:14:47 +00:00
|
|
|
/// A function type to check for cancellation.
|
|
|
|
/// \return true if execution should cancel.
|
|
|
|
using cancel_checker_t = std::function<bool()>;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Print a stack trace to stderr.
|
2022-06-16 08:26:43 +00:00
|
|
|
void show_stackframe(int frame_count = 100, int skip_levels = 0);
|
2006-06-17 13:07:08 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Returns a wide character string equivalent of the specified multibyte character string.
|
|
|
|
///
|
|
|
|
/// This function encodes illegal character sequences in a reversible way using the private use
|
|
|
|
/// area.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring str2wcstring(const std::string &in);
|
2017-12-19 10:05:20 +00:00
|
|
|
wcstring str2wcstring(const std::string &in, size_t len);
|
2023-02-27 03:13:40 +00:00
|
|
|
wcstring str2wcstring(const char *in);
|
|
|
|
wcstring str2wcstring(const char *in, size_t len);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Returns a newly allocated multibyte character string equivalent of the specified wide character
|
|
|
|
/// string.
|
|
|
|
///
|
|
|
|
/// This function decodes illegal character sequences in a reversible way using the private use
|
|
|
|
/// area.
|
2011-12-27 03:18:46 +00:00
|
|
|
std::string wcs2string(const wcstring &input);
|
2021-02-04 21:28:48 +00:00
|
|
|
std::string wcs2string(const wchar_t *in, size_t len);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2021-02-17 23:36:22 +00:00
|
|
|
/// Like wcs2string, but appends to \p receiver instead of returning a new string.
|
|
|
|
void wcs2string_appending(const wchar_t *in, size_t len, std::string *receiver);
|
|
|
|
|
2016-12-03 21:27:50 +00:00
|
|
|
// Check if we are running in the test mode, where we should suppress error output
|
2022-09-20 18:58:37 +00:00
|
|
|
#define TESTS_PROGRAM_NAME L"(ignore)"
|
2016-12-03 21:27:50 +00:00
|
|
|
bool should_suppress_stderr_for_tests();
|
|
|
|
|
2021-09-29 06:31:47 +00:00
|
|
|
/// Branch prediction hints. Idea borrowed from Linux kernel. Just used for asserts.
|
2022-11-29 19:26:32 +00:00
|
|
|
#define likely(x) __builtin_expect(bool(x), 1)
|
|
|
|
#define unlikely(x) __builtin_expect(bool(x), 0)
|
2021-09-29 06:31:47 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
void assert_is_main_thread(const char *who);
|
|
|
|
#define ASSERT_IS_MAIN_THREAD_TRAMPOLINE(x) assert_is_main_thread(x)
|
|
|
|
#define ASSERT_IS_MAIN_THREAD() ASSERT_IS_MAIN_THREAD_TRAMPOLINE(__FUNCTION__)
|
|
|
|
|
|
|
|
void assert_is_background_thread(const char *who);
|
|
|
|
#define ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(x) assert_is_background_thread(x)
|
|
|
|
#define ASSERT_IS_BACKGROUND_THREAD() ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(__FUNCTION__)
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Useful macro for asserting that a lock is locked. This doesn't check whether this thread locked
|
|
|
|
/// it, which it would be nice if it did, but here it is anyways.
|
2021-02-06 21:58:35 +00:00
|
|
|
void assert_is_locked(std::mutex &mutex, const char *who, const char *caller);
|
|
|
|
#define ASSERT_IS_LOCKED(m) assert_is_locked(m, #m, __FUNCTION__)
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer.
|
2012-03-05 18:18:42 +00:00
|
|
|
wcstring format_size(long long sz);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Version of format_size that does not allocate memory.
|
2012-03-05 18:18:42 +00:00
|
|
|
void format_size_safe(char buff[128], unsigned long long sz);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Writes out a long safely.
|
2014-01-12 21:53:59 +00:00
|
|
|
void format_long_safe(char buff[64], long val);
|
|
|
|
void format_long_safe(wchar_t buff[64], long val);
|
2022-08-04 00:32:38 +00:00
|
|
|
void format_llong_safe(wchar_t buff[64], long long val);
|
2019-02-04 00:06:10 +00:00
|
|
|
void format_ullong_safe(wchar_t buff[64], unsigned long long val);
|
2012-03-05 18:18:42 +00:00
|
|
|
|
2022-10-29 03:07:36 +00:00
|
|
|
/// "Narrows" a wide character string. This just grabs any ASCII characters and truncates.
|
2016-02-28 09:38:28 +00:00
|
|
|
void narrow_string_safe(char buff[64], const wchar_t *s);
|
|
|
|
|
2022-08-13 19:15:23 +00:00
|
|
|
/// Stored in blocks to reference the file which created the block.
|
2023-01-14 22:56:24 +00:00
|
|
|
using filename_ref_t = std::shared_ptr<wcstring>;
|
2022-08-13 19:15:23 +00:00
|
|
|
|
2021-02-06 21:32:38 +00:00
|
|
|
using scoped_lock = std::lock_guard<std::mutex>;
|
2013-02-22 14:34:30 +00:00
|
|
|
|
2017-01-29 20:37:04 +00:00
|
|
|
// An object wrapping a scoped lock and a value
|
|
|
|
// This is returned from owning_lock.acquire()
|
|
|
|
// Sample usage:
|
|
|
|
// owning_lock<string> locked_name;
|
|
|
|
// acquired_lock<string> name = name.acquire();
|
|
|
|
// name.value = "derp"
|
|
|
|
//
|
|
|
|
// Or for simple cases:
|
|
|
|
// name.acquire().value = "derp"
|
|
|
|
//
|
2019-05-10 16:10:43 +00:00
|
|
|
template <typename Data>
|
2021-07-22 17:43:25 +00:00
|
|
|
class acquired_lock : noncopyable_t {
|
2017-02-08 05:52:35 +00:00
|
|
|
template <typename T>
|
2017-01-29 20:37:04 +00:00
|
|
|
friend class owning_lock;
|
|
|
|
|
2019-06-15 00:18:43 +00:00
|
|
|
template <typename T>
|
|
|
|
friend class acquired_lock;
|
|
|
|
|
|
|
|
acquired_lock(std::mutex &lk, Data *v) : lock(lk), value(v) {}
|
|
|
|
acquired_lock(std::unique_lock<std::mutex> &&lk, Data *v) : lock(std::move(lk)), value(v) {}
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock;
|
2019-05-10 16:10:43 +00:00
|
|
|
Data *value;
|
2018-09-01 20:11:42 +00:00
|
|
|
|
2017-02-08 05:52:35 +00:00
|
|
|
public:
|
2019-05-10 16:10:43 +00:00
|
|
|
Data *operator->() { return value; }
|
|
|
|
const Data *operator->() const { return value; }
|
|
|
|
Data &operator*() { return *value; }
|
|
|
|
const Data &operator*() const { return *value; }
|
|
|
|
|
2019-06-15 00:18:43 +00:00
|
|
|
/// Implicit conversion to const version.
|
|
|
|
operator acquired_lock<const Data>() {
|
|
|
|
// We're about to give up our lock, don't hold onto the data.
|
|
|
|
const Data *cvalue = value;
|
|
|
|
value = nullptr;
|
|
|
|
return acquired_lock<const Data>(std::move(lock), cvalue);
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:10:43 +00:00
|
|
|
/// Create from a global lock.
|
|
|
|
/// This is used in weird cases where a global lock protects more than one piece of data.
|
|
|
|
static acquired_lock from_global(std::mutex &lk, Data *v) { return acquired_lock{lk, v}; }
|
2019-05-31 16:13:21 +00:00
|
|
|
|
|
|
|
/// \return a reference to the lock, for use with a condition variable.
|
|
|
|
std::unique_lock<std::mutex> &get_lock() { return lock; }
|
2017-01-29 20:37:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A lock that owns a piece of data
|
|
|
|
// Access to the data is only provided by taking the lock
|
2019-04-27 19:04:36 +00:00
|
|
|
template <typename Data>
|
2017-01-29 20:37:04 +00:00
|
|
|
class owning_lock {
|
|
|
|
// No copying
|
|
|
|
owning_lock &operator=(const scoped_lock &) = delete;
|
|
|
|
owning_lock(const scoped_lock &) = delete;
|
|
|
|
owning_lock(owning_lock &&) = default;
|
|
|
|
owning_lock &operator=(owning_lock &&) = default;
|
|
|
|
|
2018-12-31 02:15:49 +00:00
|
|
|
std::mutex lock;
|
2019-04-27 19:04:36 +00:00
|
|
|
Data data;
|
2017-01-29 20:37:04 +00:00
|
|
|
|
2017-02-08 05:52:35 +00:00
|
|
|
public:
|
2019-04-27 19:04:36 +00:00
|
|
|
owning_lock(Data &&d) : data(std::move(d)) {}
|
2019-05-12 19:33:13 +00:00
|
|
|
owning_lock(const Data &d) : data(d) {}
|
2017-01-29 20:37:04 +00:00
|
|
|
owning_lock() : data() {}
|
|
|
|
|
2019-04-27 19:04:36 +00:00
|
|
|
acquired_lock<Data> acquire() { return {lock, &data}; }
|
2017-01-29 20:37:04 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// A scoped manager to save the current value of some variable, and optionally set it to a new
|
|
|
|
/// value. On destruction it restores the variable to its old value.
|
|
|
|
///
|
|
|
|
/// This can be handy when there are multiple code paths to exit a block.
|
2013-02-22 14:34:30 +00:00
|
|
|
template <typename T>
|
2016-04-27 23:10:14 +00:00
|
|
|
class scoped_push {
|
|
|
|
T *const ref;
|
2013-02-22 14:34:30 +00:00
|
|
|
T saved_value;
|
|
|
|
bool restored;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
public:
|
|
|
|
explicit scoped_push(T *r) : ref(r), saved_value(*r), restored(false) {}
|
2013-02-22 14:34:30 +00:00
|
|
|
|
2017-01-27 00:14:50 +00:00
|
|
|
scoped_push(T *r, T new_value) : ref(r), restored(false) {
|
|
|
|
saved_value = std::move(*ref);
|
|
|
|
*ref = std::move(new_value);
|
2013-02-22 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
~scoped_push() { restore(); }
|
2013-02-22 14:34:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
void restore() {
|
|
|
|
if (!restored) {
|
2017-01-27 00:14:50 +00:00
|
|
|
*ref = std::move(saved_value);
|
2013-02-27 20:03:30 +00:00
|
|
|
restored = true;
|
|
|
|
}
|
2013-02-22 14:34:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring format_string(const wchar_t *format, ...);
|
2012-02-10 02:43:36 +00:00
|
|
|
wcstring vformat_string(const wchar_t *format, va_list va_orig);
|
2012-02-22 18:51:06 +00:00
|
|
|
void append_format(wcstring &str, const wchar_t *format, ...);
|
2019-11-19 00:54:36 +00:00
|
|
|
void append_formatv(wcstring &target, const wchar_t *format, va_list va_orig);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2023-01-14 22:56:24 +00:00
|
|
|
#ifndef HAVE_STD__MAKE_UNIQUE
|
2017-01-21 23:02:41 +00:00
|
|
|
/// make_unique implementation
|
2023-01-14 22:56:24 +00:00
|
|
|
namespace std {
|
2017-01-27 04:00:43 +00:00
|
|
|
template <typename T, typename... Args>
|
2020-11-22 13:39:48 +00:00
|
|
|
std::unique_ptr<T> make_unique(Args &&...args) {
|
2017-01-21 23:02:41 +00:00
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
2023-01-14 22:56:24 +00:00
|
|
|
} // namespace std
|
2017-01-22 08:32:08 +00:00
|
|
|
#endif
|
2023-01-14 22:56:24 +00:00
|
|
|
using std::make_unique;
|
2017-01-21 23:02:41 +00:00
|
|
|
|
2021-07-02 21:11:03 +00:00
|
|
|
/// This functions returns the end of the quoted substring beginning at \c pos. Returns 0 on error.
|
2016-04-27 23:10:14 +00:00
|
|
|
///
|
2021-07-02 21:11:03 +00:00
|
|
|
/// \param pos the position of the opening quote.
|
|
|
|
/// \param quote the quote to use, usually pointed to by \c pos.
|
2021-06-26 04:16:03 +00:00
|
|
|
const wchar_t *quote_end(const wchar_t *pos, wchar_t quote);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2022-02-04 20:44:45 +00:00
|
|
|
/// This functions returns the end of the comment substring beginning at \c pos.
|
|
|
|
///
|
|
|
|
/// \param pos the position where the comment starts, including the '#' symbol.
|
|
|
|
const wchar_t *comment_end(const wchar_t *pos);
|
|
|
|
|
2016-06-04 02:05:13 +00:00
|
|
|
/// This function should be called after calling `setlocale()` to perform fish specific locale
|
|
|
|
/// initialization.
|
|
|
|
void fish_setlocale();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-02-06 07:20:43 +00:00
|
|
|
/// Call read, blocking and repeating on EINTR. Exits on EAGAIN.
|
|
|
|
/// \return the number of bytes read, or 0 on EOF. On EAGAIN, returns -1 if nothing was read.
|
2012-08-04 22:11:43 +00:00
|
|
|
long read_blocked(int fd, void *buf, size_t count);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Loop a write request while failure is non-critical. Return -1 and set errno in case of critical
|
|
|
|
/// error.
|
2012-01-14 11:41:50 +00:00
|
|
|
ssize_t write_loop(int fd, const char *buff, size_t count);
|
2009-02-22 20:28:52 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Loop a read request while failure is non-critical. Return -1 and set errno in case of critical
|
|
|
|
/// error.
|
2012-03-01 01:55:50 +00:00
|
|
|
ssize_t read_loop(int fd, void *buff, size_t count);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Replace special characters with backslash escape sequences. Newline is replaced with \n, etc.
|
|
|
|
///
|
|
|
|
/// \param in The string to be escaped
|
|
|
|
/// \param flags Flags to control the escaping
|
|
|
|
/// \return The escaped string
|
2022-07-25 14:25:04 +00:00
|
|
|
wcstring escape_string(const wchar_t *in, escape_flags_t flags = 0,
|
2017-06-24 06:19:09 +00:00
|
|
|
escape_string_style_t style = STRING_STYLE_SCRIPT);
|
2022-07-25 14:25:04 +00:00
|
|
|
wcstring escape_string(const wcstring &in, escape_flags_t flags = 0,
|
2017-06-24 06:19:09 +00:00
|
|
|
escape_string_style_t style = STRING_STYLE_SCRIPT);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2021-07-02 21:11:03 +00:00
|
|
|
/// Escape a string so that it may be inserted into a double-quoted string.
|
|
|
|
/// This permits ownership transfer.
|
|
|
|
wcstring escape_string_for_double_quotes(wcstring in);
|
|
|
|
|
2018-06-23 20:15:32 +00:00
|
|
|
/// \return a string representation suitable for debugging (not for presenting to the user). This
|
|
|
|
/// replaces non-ASCII characters with either tokens like <BRACE_SEP> or <\xfdd7>. No other escapes
|
|
|
|
/// are made (i.e. this is a lossy escape).
|
|
|
|
wcstring debug_escape(const wcstring &in);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Expand backslashed escapes and substitute them with their unescaped counterparts. Also
|
|
|
|
/// optionally change the wildcards, the tilde character and a few more into constants which are
|
|
|
|
/// defined in a private use area of Unicode. This assumes wchar_t is a unicode character set.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Given a null terminated string starting with a backslash, read the escape as if it is unquoted,
|
2018-09-28 02:22:55 +00:00
|
|
|
/// appending to result. Return the number of characters consumed, or none() on error.
|
|
|
|
maybe_t<size_t> read_unquoted_escape(const wchar_t *input, wcstring *result, bool allow_incomplete,
|
|
|
|
bool unescape_special);
|
2015-09-12 19:59:40 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Unescapes a string in-place. A true result indicates the string was unescaped, a false result
|
|
|
|
/// indicates the string was unmodified.
|
2013-11-25 06:57:49 +00:00
|
|
|
bool unescape_string_in_place(wcstring *str, unescape_flags_t escape_special);
|
|
|
|
|
2017-06-23 03:47:54 +00:00
|
|
|
/// Reverse the effects of calling `escape_string`. Returns the unescaped value by reference. On
|
|
|
|
/// failure, the output is set to an empty string.
|
|
|
|
bool unescape_string(const wchar_t *input, wcstring *output, unescape_flags_t escape_special,
|
|
|
|
escape_string_style_t style = STRING_STYLE_SCRIPT);
|
|
|
|
|
2021-11-27 20:46:15 +00:00
|
|
|
bool unescape_string(const wchar_t *input, size_t len, wcstring *output,
|
|
|
|
unescape_flags_t escape_special,
|
|
|
|
escape_string_style_t style = STRING_STYLE_SCRIPT);
|
|
|
|
|
2017-06-23 03:47:54 +00:00
|
|
|
bool unescape_string(const wcstring &input, wcstring *output, unescape_flags_t escape_special,
|
|
|
|
escape_string_style_t style = STRING_STYLE_SCRIPT);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2020-06-08 01:47:27 +00:00
|
|
|
/// Write the given paragraph of output, redoing linebreaks to fit \p termsize.
|
|
|
|
wcstring reformat_for_screen(const wcstring &msg, const termsize_t &termsize);
|
2006-01-15 11:58:05 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Return the number of seconds from the UNIX epoch, with subsecond precision. This function uses
|
2016-06-25 02:25:48 +00:00
|
|
|
/// the gettimeofday function and will have the same precision as that function.
|
2021-08-27 23:20:30 +00:00
|
|
|
using timepoint_t = double;
|
|
|
|
timepoint_t timef();
|
2009-02-02 22:46:45 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Call the following function early in main to set the main thread. This is our replacement for
|
|
|
|
/// pthread_main_np().
|
2012-01-05 21:58:48 +00:00
|
|
|
void set_main_thread();
|
2012-03-19 18:52:18 +00:00
|
|
|
bool is_main_thread();
|
2012-01-05 21:58:48 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Configures thread assertions for testing.
|
2012-05-14 03:19:02 +00:00
|
|
|
void configure_thread_assertions_for_testing();
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Set up a guard to complain if we try to do certain things (like take a lock) after calling fork.
|
2012-02-28 02:43:24 +00:00
|
|
|
void setup_fork_guards(void);
|
2012-01-05 21:58:48 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Save the value of tcgetpgrp so we can restore it on exit.
|
2020-05-31 21:11:39 +00:00
|
|
|
void save_term_foreground_process_group();
|
|
|
|
void restore_term_foreground_process_group_for_exit();
|
2012-11-18 10:16:14 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Return whether we are the child of a fork.
|
2012-02-28 02:43:24 +00:00
|
|
|
bool is_forked_child(void);
|
|
|
|
void assert_is_not_forked_child(const char *who);
|
|
|
|
#define ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(x) assert_is_not_forked_child(x)
|
|
|
|
#define ASSERT_IS_NOT_FORKED_CHILD() ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(__FUNCTION__)
|
2009-02-02 22:46:45 +00:00
|
|
|
|
2019-02-14 23:53:07 +00:00
|
|
|
/// Determines if we are running under Microsoft's Windows Subsystem for Linux to work around
|
|
|
|
/// some known limitations and/or bugs.
|
2018-03-09 20:40:35 +00:00
|
|
|
/// See https://github.com/Microsoft/WSL/issues/423 and Microsoft/WSL#2997
|
2019-02-14 23:53:07 +00:00
|
|
|
bool is_windows_subsystem_for_linux();
|
2017-12-25 00:29:12 +00:00
|
|
|
|
2022-10-29 03:07:36 +00:00
|
|
|
/// Detect if we are running under Cygwin or Cygwin64
|
2019-01-02 06:12:07 +00:00
|
|
|
constexpr bool is_cygwin() {
|
2019-01-22 02:06:16 +00:00
|
|
|
#ifdef __CYGWIN__
|
2019-01-02 06:12:07 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-01 01:55:50 +00:00
|
|
|
extern "C" {
|
2019-02-10 12:20:01 +00:00
|
|
|
[[gnu::noinline]] void debug_thread_error(void);
|
2012-03-01 01:55:50 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 21:30:31 +00:00
|
|
|
/// Converts from wide char to digit in the specified base. If d is not a valid digit in the
|
|
|
|
/// specified base, return -1.
|
|
|
|
long convert_digit(wchar_t d, int base);
|
|
|
|
|
2016-10-09 21:38:26 +00:00
|
|
|
/// This is a macro that can be used to silence "unused parameter" warnings from the compiler for
|
|
|
|
/// functions which need to accept parameters they do not use because they need to be compatible
|
|
|
|
/// with an interface. It's similar to the Python idiom of doing `_ = expr` at the top of a
|
|
|
|
/// function in the same situation.
|
2016-10-11 02:52:22 +00:00
|
|
|
#define UNUSED(expr) \
|
|
|
|
do { \
|
|
|
|
(void)(expr); \
|
|
|
|
} while (0)
|
2016-10-09 21:38:26 +00:00
|
|
|
|
2016-10-17 23:23:29 +00:00
|
|
|
// Return true if the character is in a range reserved for fish's private use.
|
|
|
|
bool fish_reserved_codepoint(wchar_t c);
|
2016-11-08 23:30:52 +00:00
|
|
|
|
2017-01-18 21:54:54 +00:00
|
|
|
void redirect_tty_output();
|
|
|
|
|
2019-02-06 04:36:38 +00:00
|
|
|
std::string get_path_to_tmp_dir();
|
|
|
|
|
2017-04-20 06:43:02 +00:00
|
|
|
bool valid_var_name_char(wchar_t chr);
|
|
|
|
bool valid_var_name(const wcstring &str);
|
2021-02-14 02:47:50 +00:00
|
|
|
bool valid_var_name(const wchar_t *str);
|
2017-04-20 06:43:02 +00:00
|
|
|
bool valid_func_name(const wcstring &str);
|
2017-05-02 07:39:50 +00:00
|
|
|
|
|
|
|
// Return values (`$status` values for fish scripts) for various situations.
|
|
|
|
enum {
|
|
|
|
/// The status code used for normal exit in a command.
|
2017-05-04 07:18:02 +00:00
|
|
|
STATUS_CMD_OK = 0,
|
2017-05-02 07:39:50 +00:00
|
|
|
/// The status code used for failure exit in a command (but not if the args were invalid).
|
2017-05-04 07:18:02 +00:00
|
|
|
STATUS_CMD_ERROR = 1,
|
2018-12-16 05:05:27 +00:00
|
|
|
/// The status code used for invalid arguments given to a command. This is distinct from valid
|
|
|
|
/// arguments that might result in a command failure. An invalid args condition is something
|
|
|
|
/// like an unrecognized flag, missing or too many arguments, an invalid integer, etc. But
|
|
|
|
STATUS_INVALID_ARGS = 2,
|
|
|
|
|
2017-05-02 07:39:50 +00:00
|
|
|
/// The status code used when a command was not found.
|
2017-05-04 07:18:02 +00:00
|
|
|
STATUS_CMD_UNKNOWN = 127,
|
2021-03-28 04:21:29 +00:00
|
|
|
|
2017-05-02 07:39:50 +00:00
|
|
|
/// The status code used when an external command can not be run.
|
|
|
|
STATUS_NOT_EXECUTABLE = 126,
|
|
|
|
|
|
|
|
/// The status code used when a wildcard had no matches.
|
|
|
|
STATUS_UNMATCHED_WILDCARD = 124,
|
|
|
|
/// The status code used when illegal command name is encountered.
|
|
|
|
STATUS_ILLEGAL_CMD = 123,
|
|
|
|
/// The status code used when `read` is asked to consume too much data.
|
|
|
|
STATUS_READ_TOO_MUCH = 122,
|
2020-01-24 01:34:46 +00:00
|
|
|
/// The status code when an expansion fails, for example, "$foo["
|
|
|
|
STATUS_EXPAND_ERROR = 121,
|
2017-05-02 07:39:50 +00:00
|
|
|
};
|
2017-08-12 14:54:26 +00:00
|
|
|
|
|
|
|
/* Normally casting an expression to void discards its value, but GCC
|
|
|
|
versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
|
|
|
|
which may cause unwanted diagnostics in that case. Use __typeof__
|
|
|
|
and __extension__ to work around the problem, if the workaround is
|
|
|
|
known to be needed. */
|
|
|
|
#if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
|
2017-08-18 19:26:35 +00:00
|
|
|
#define ignore_result(x) \
|
|
|
|
(__extension__({ \
|
|
|
|
__typeof__(x) __x = (x); \
|
|
|
|
(void)__x; \
|
|
|
|
}))
|
2017-08-12 14:54:26 +00:00
|
|
|
#else
|
2017-08-18 19:26:35 +00:00
|
|
|
#define ignore_result(x) ((void)(x))
|
2017-08-12 14:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-19 16:55:06 +00:00
|
|
|
// Custom hash function used by unordered_map/unordered_set when key is const
|
|
|
|
#ifndef CONST_WCSTRING_HASH
|
2022-09-20 18:58:37 +00:00
|
|
|
#define CONST_WCSTRING_HASH 1
|
2017-08-19 16:55:06 +00:00
|
|
|
namespace std {
|
2017-08-19 18:47:16 +00:00
|
|
|
template <>
|
|
|
|
struct hash<const wcstring> {
|
2017-08-19 23:27:24 +00:00
|
|
|
std::size_t operator()(const wcstring &w) const {
|
|
|
|
std::hash<wcstring> hasher;
|
2020-05-01 20:30:22 +00:00
|
|
|
return hasher(w);
|
2017-08-19 23:27:24 +00:00
|
|
|
}
|
2017-08-19 18:47:16 +00:00
|
|
|
};
|
2018-09-28 02:28:39 +00:00
|
|
|
} // namespace std
|
2017-08-19 16:55:06 +00:00
|
|
|
#endif
|
2017-08-19 23:27:24 +00:00
|
|
|
|
2018-10-10 03:33:20 +00:00
|
|
|
/// Get the absolute path to the fish executable itself
|
2019-11-19 00:54:36 +00:00
|
|
|
std::string get_executable_path(const char *argv0);
|
2018-10-10 03:33:20 +00:00
|
|
|
|
2018-10-02 22:10:14 +00:00
|
|
|
/// A RAII wrapper for resources that don't recur, so we don't have to create a separate RAII
|
|
|
|
/// wrapper for each function. Avoids needing to call "return cleanup()" or similar / everywhere.
|
|
|
|
struct cleanup_t {
|
2019-05-05 10:09:25 +00:00
|
|
|
private:
|
2018-10-02 22:10:14 +00:00
|
|
|
const std::function<void()> cleanup;
|
2019-05-05 10:09:25 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
cleanup_t(std::function<void()> exit_actions) : cleanup{std::move(exit_actions)} {}
|
|
|
|
~cleanup_t() { cleanup(); }
|
2018-10-02 22:10:14 +00:00
|
|
|
};
|
|
|
|
|
2019-01-30 01:59:41 +00:00
|
|
|
bool is_console_session();
|
|
|
|
|
2021-02-08 19:48:23 +00:00
|
|
|
/// Compile-time agnostic-size strcmp/wcscmp implementation. Unicode-unaware.
|
|
|
|
template <typename T>
|
|
|
|
constexpr ssize_t const_strcmp(const T *lhs, const T *rhs) {
|
|
|
|
return (*lhs == *rhs) ? (*lhs == 0 ? 0 : const_strcmp(lhs + 1, rhs + 1))
|
|
|
|
: (*lhs > *rhs ? 1 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile-time agnostic-size strlen/wcslen implementation. Unicode-unaware.
|
2021-02-17 18:17:06 +00:00
|
|
|
template <typename T, size_t N>
|
2021-07-15 19:13:53 +00:00
|
|
|
constexpr size_t const_strlen(const T (&val)[N], size_t last_checked_idx = N,
|
|
|
|
size_t first_nul_idx = N) {
|
|
|
|
// Assume there's a nul char at the end (index N) but there may be one before that that.
|
|
|
|
return last_checked_idx == 0
|
|
|
|
? first_nul_idx
|
|
|
|
: const_strlen(val, last_checked_idx - 1,
|
|
|
|
val[last_checked_idx - 1] ? first_nul_idx : last_checked_idx - 1);
|
2021-02-08 19:48:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 20:15:24 +00:00
|
|
|
/// \return true if the array \p vals is sorted by its name property.
|
|
|
|
template <typename T, size_t N>
|
|
|
|
constexpr bool is_sorted_by_name(const T (&vals)[N], size_t idx = 1) {
|
|
|
|
return idx >= N ? true
|
|
|
|
: (const_strcmp(vals[idx - 1].name, vals[idx].name) <= 0 &&
|
|
|
|
is_sorted_by_name(vals, idx + 1));
|
|
|
|
}
|
|
|
|
#define ASSERT_SORTED_BY_NAME(x) static_assert(is_sorted_by_name(x), #x " not sorted by name")
|
2021-02-08 19:48:23 +00:00
|
|
|
|
2021-08-26 20:40:02 +00:00
|
|
|
/// \return a pointer to the first entry with the given name, assuming the entries are sorted by
|
|
|
|
/// name. \return nullptr if not found.
|
|
|
|
template <typename T, size_t N>
|
|
|
|
const T *get_by_sorted_name(const wchar_t *name, const T (&vals)[N]) {
|
|
|
|
assert(name && "Null name");
|
|
|
|
auto is_less = [](const T &v, const wchar_t *n) -> bool { return std::wcscmp(v.name, n) < 0; };
|
|
|
|
auto where = std::lower_bound(std::begin(vals), std::end(vals), name, is_less);
|
|
|
|
if (where != std::end(vals) && std::wcscmp(where->name, name) == 0) {
|
|
|
|
return &*where;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, size_t N>
|
|
|
|
const T *get_by_sorted_name(const wcstring &name, const T (&vals)[N]) {
|
|
|
|
return get_by_sorted_name(name.c_str(), vals);
|
|
|
|
}
|
|
|
|
|
2021-10-04 23:40:40 +00:00
|
|
|
/// As established in 1ab81ab90d1a408702e11f081fdaaafa30636c31, iswdigit() is very slow under glibc,
|
|
|
|
/// and does nothing more than establish whether or not the single specified character is in the
|
|
|
|
/// range ('0','9').
|
2021-11-08 19:36:01 +00:00
|
|
|
__attribute__((always_inline)) bool inline iswdigit(const wchar_t c) {
|
2021-10-04 23:40:40 +00:00
|
|
|
return c >= L'0' && c <= L'9';
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
#endif // FISH_COMMON_H
|