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
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "config.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <errno.h>
|
2012-01-05 21:58:48 +00:00
|
|
|
#include <pthread.h>
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <stdarg.h> // IWYU pragma: keep
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-05 21:58:48 +00:00
|
|
|
#include <string.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <sys/types.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <termios.h>
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <wchar.h>
|
2016-06-24 00:24:19 +00:00
|
|
|
#include <memory>
|
2016-04-27 23:10:14 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-27 23:10:14 +00:00
|
|
|
#include "signal.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
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
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Common string type.
|
2011-12-27 03:18:46 +00:00
|
|
|
typedef std::wstring wcstring;
|
2011-12-27 08:06:07 +00:00
|
|
|
typedef std::vector<wcstring> wcstring_list_t;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Maximum number of bytes used by a single utf-8 character.
|
2005-09-20 13:26:39 +00:00
|
|
|
#define MAX_UTF8_BYTES 6
|
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal ASCII value.
|
2006-05-26 16:46:38 +00:00
|
|
|
#define ASCII_MAX 127u
|
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal 16-bit Unicode value.
|
|
|
|
#define UCS2_MAX 0xFFFFu
|
2006-05-26 16:46:38 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Highest legal byte value.
|
|
|
|
#define BYTE_MAX 0xFFu
|
2006-05-26 16:46:38 +00:00
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Unicode BOM value.
|
2014-11-01 23:25:28 +00:00
|
|
|
#define UTF8_BOM_WCHAR 0xFEFFu
|
|
|
|
|
2016-01-22 03:56:39 +00:00
|
|
|
// Unicode replacement character.
|
|
|
|
#define REPLACEMENT_WCHAR 0xFFFDu
|
|
|
|
|
|
|
|
// Use Unicode "noncharacters" for internal characters as much as we can. This
|
|
|
|
// 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.
|
|
|
|
#define RESERVED_CHAR_BASE 0xFDD0u
|
2016-04-27 23:10:14 +00:00
|
|
|
#define RESERVED_CHAR_END 0xFDF0u
|
2016-01-22 03:56:39 +00:00
|
|
|
// Split the available noncharacter values into two ranges to ensure there are
|
|
|
|
// no conflicts among the places we use these special characters.
|
|
|
|
#define EXPAND_RESERVED_BASE RESERVED_CHAR_BASE
|
2016-04-27 23:10:14 +00:00
|
|
|
#define EXPAND_RESERVED_END (EXPAND_RESERVED_BASE + 16)
|
2016-01-22 03:56:39 +00:00
|
|
|
#define WILDCARD_RESERVED_BASE EXPAND_RESERVED_END
|
2016-04-27 23:10:14 +00:00
|
|
|
#define WILDCARD_RESERVED_END (WILDCARD_RESERVED_BASE + 16)
|
2016-01-22 03:56:39 +00:00
|
|
|
// Make sure the ranges defined above don't exceed the range for noncharacters.
|
|
|
|
// This is to make sure we didn't do something stupid in subdividing the
|
|
|
|
// Unicode range for our needs.
|
|
|
|
#if WILDCARD_RESERVED_END > RESERVED_CHAR_END
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
#define ENCODE_DIRECT_BASE 0xF600u
|
2016-04-27 23:10:14 +00:00
|
|
|
#define ENCODE_DIRECT_END (ENCODE_DIRECT_BASE + 256)
|
|
|
|
#define INPUT_COMMON_BASE 0xF700u
|
|
|
|
#define INPUT_COMMON_END (INPUT_COMMON_BASE + 64)
|
|
|
|
|
|
|
|
// Flags for unescape_string functions.
|
|
|
|
enum {
|
|
|
|
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
|
2013-11-25 06:57:49 +00:00
|
|
|
};
|
|
|
|
typedef unsigned int unescape_flags_t;
|
2007-01-18 16:02:46 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Flags for the escape() and escape_string() functions.
|
|
|
|
enum {
|
|
|
|
/// Escape all characters, including magic characters like the semicolon.
|
2012-11-19 00:30:30 +00:00
|
|
|
ESCAPE_ALL = 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,
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Do not escape tildes.
|
2012-07-06 21:34:53 +00:00
|
|
|
ESCAPE_NO_TILDE = 1 << 2
|
|
|
|
};
|
|
|
|
typedef unsigned int escape_flags_t;
|
2007-10-06 10:51:31 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Directions.
|
|
|
|
enum selection_direction_t {
|
|
|
|
// Visual directions.
|
2014-01-17 20:04:03 +00:00
|
|
|
direction_north,
|
|
|
|
direction_east,
|
|
|
|
direction_south,
|
2014-01-18 20:42:53 +00:00
|
|
|
direction_west,
|
2015-03-02 21:08:29 +00:00
|
|
|
direction_page_north,
|
|
|
|
direction_page_south,
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Logical directions.
|
2014-01-18 20:42:53 +00:00
|
|
|
direction_next,
|
2014-01-24 23:59:18 +00:00
|
|
|
direction_prev,
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Special value that means deselect.
|
2014-01-24 23:59:18 +00:00
|
|
|
direction_deselect
|
2014-01-17 20:04:03 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
inline bool selection_direction_is_cardinal(selection_direction_t dir) {
|
|
|
|
switch (dir) {
|
2014-01-18 20:42:53 +00:00
|
|
|
case direction_north:
|
|
|
|
case direction_east:
|
|
|
|
case direction_south:
|
2016-07-30 18:48:39 +00:00
|
|
|
case direction_west:
|
|
|
|
case direction_page_north:
|
|
|
|
case direction_page_south: {
|
2014-01-18 20:42:53 +00:00
|
|
|
return true;
|
2016-04-27 23:10:14 +00:00
|
|
|
}
|
2016-07-30 18:48:39 +00:00
|
|
|
case direction_next:
|
|
|
|
case direction_prev:
|
|
|
|
case direction_deselect: {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default: { abort(); }
|
2014-01-18 20:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Helper macro for errors.
|
|
|
|
#define VOMIT_ON_FAILURE(a) \
|
|
|
|
do { \
|
|
|
|
if (0 != (a)) { \
|
|
|
|
VOMIT_ABORT(errno, #a); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define VOMIT_ON_FAILURE_NO_ERRNO(a) \
|
|
|
|
do { \
|
|
|
|
int err = (a); \
|
|
|
|
if (0 != err) { \
|
|
|
|
VOMIT_ABORT(err, #a); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define VOMIT_ABORT(err, str) \
|
|
|
|
do { \
|
|
|
|
int code = (err); \
|
|
|
|
fprintf(stderr, "%s failed on line %d in file %s: %d (%s)\n", str, __LINE__, __FILE__, \
|
|
|
|
code, strerror(code)); \
|
|
|
|
abort(); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/// Exits without invoking destructors (via _exit), useful for code after fork.
|
2012-11-19 00:30:30 +00:00
|
|
|
void exit_without_destructors(int code) __attribute__((noreturn));
|
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.
|
2012-11-05 08:05:42 +00:00
|
|
|
extern wchar_t ellipsis_char;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Character representing an omitted newline at the end of text.
|
2012-12-01 23:44:09 +00:00
|
|
|
extern wchar_t omitted_newline_char;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// The verbosity level of fish. If a call to debug has a severity level higher than \c debug_level,
|
|
|
|
/// it will not be printed.
|
2005-09-24 19:31:17 +00:00
|
|
|
extern int debug_level;
|
|
|
|
|
2016-05-16 02:45:02 +00:00
|
|
|
/// How many stack frames to show when a debug() call is made.
|
|
|
|
extern int debug_stack_frames;
|
|
|
|
|
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.
|
2011-12-27 03:18:46 +00:00
|
|
|
extern const wchar_t *program_name;
|
2005-10-25 09:39:45 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Variants of read() and write() that ignores return values, defeating a warning.
|
2014-04-28 01:27:34 +00:00
|
|
|
void read_ignore(int fd, void *buff, size_t count);
|
|
|
|
void write_ignore(int fd, const void *buff, size_t count);
|
|
|
|
|
2016-06-17 20:08:25 +00:00
|
|
|
/// Set to false at run-time if it's been determined we can't trust the last modified timestamp on
|
|
|
|
/// the tty.
|
|
|
|
extern bool has_working_tty_timestamps;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// This macro is used to check that an input argument is not null. It is a bit lika a non-fatal
|
|
|
|
/// form of assert. Instead of exit-ing on failure, the current function is ended at once. The
|
|
|
|
/// second parameter is the return value of the current function on failure.
|
|
|
|
#define CHECK(arg, retval) \
|
|
|
|
if (!(arg)) { \
|
|
|
|
debug(0, "function %s called with null value for argument %s. ", __func__, #arg); \
|
|
|
|
bugreport(); \
|
2016-05-16 02:45:02 +00:00
|
|
|
show_stackframe(L'E'); \
|
2016-04-27 23:10:14 +00:00
|
|
|
return retval; \
|
|
|
|
}
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Pause for input, then exit the program. If supported, print a backtrace first.
|
|
|
|
#define FATAL_EXIT() \
|
|
|
|
{ \
|
|
|
|
char exit_read_buff; \
|
2016-05-16 02:45:02 +00:00
|
|
|
show_stackframe(L'E'); \
|
2016-04-27 23:10:14 +00:00
|
|
|
read_ignore(0, &exit_read_buff, 1); \
|
|
|
|
exit_without_destructors(1); \
|
|
|
|
}
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
/// Exit program at once after emitting an error message.
|
|
|
|
#define DIE(msg) \
|
|
|
|
{ \
|
|
|
|
fprintf(stderr, "fish: %s on line %ld of file %s, shutting down fish\n", msg, \
|
|
|
|
(long)__LINE__, __FILE__); \
|
|
|
|
FATAL_EXIT(); \
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Exit program at once, leaving an error message about running out of memory.
|
|
|
|
#define DIE_MEM() \
|
|
|
|
{ \
|
|
|
|
fwprintf(stderr, L"fish: Out of memory on line %ld of file %s, shutting down fish\n", \
|
|
|
|
(long)__LINE__, __FILE__); \
|
|
|
|
FATAL_EXIT(); \
|
|
|
|
}
|
2007-04-16 21:40:41 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Check if signals are blocked. If so, print an error message and return from the function
|
|
|
|
/// performing this check.
|
|
|
|
#define CHECK_BLOCK(retval) \
|
|
|
|
if (signal_is_blocked()) { \
|
|
|
|
debug(0, "function %s called while blocking signals. ", __func__); \
|
|
|
|
bugreport(); \
|
2016-05-16 02:45:02 +00:00
|
|
|
show_stackframe(L'E'); \
|
2016-04-27 23:10:14 +00:00
|
|
|
return retval; \
|
|
|
|
}
|
2007-01-20 02:36:49 +00:00
|
|
|
|
2016-06-02 05:03:27 +00:00
|
|
|
/// Shorthand for wgettext call in situations where a C-style string is needed (e.g., fwprintf()).
|
|
|
|
#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
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Check if the specified string element is a part of the specified string list.
|
|
|
|
#define contains(str, ...) contains_internal(str, 0, __VA_ARGS__, NULL)
|
2012-07-16 19:05:36 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Print a stack trace to stderr.
|
2016-05-16 02:45:02 +00:00
|
|
|
void show_stackframe(const wchar_t msg_level, int frame_count = -1, int skip_levels = 0);
|
2006-06-17 13:07:08 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Read a line from the stream f into the string. Returns the number of bytes read or -1 on
|
|
|
|
/// failure.
|
|
|
|
///
|
|
|
|
/// If the carriage return character is encountered, it is ignored. fgetws() considers the line to
|
|
|
|
/// end if reading the file results in either a newline (L'\n') character, the null (L'\\0')
|
|
|
|
/// character or the end of file (WEOF) character.
|
|
|
|
int fgetws2(wcstring *s, FILE *f);
|
2012-11-19 00:30:30 +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 char *in);
|
2012-12-19 21:31:06 +00:00
|
|
|
wcstring str2wcstring(const char *in, size_t len);
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring str2wcstring(const std::string &in);
|
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.
|
2012-11-19 00:30:30 +00:00
|
|
|
char *wcs2str(const wchar_t *in);
|
2013-01-12 22:18:34 +00:00
|
|
|
char *wcs2str(const wcstring &in);
|
2011-12-27 03:18:46 +00:00
|
|
|
std::string wcs2string(const wcstring &input);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if a string prefixes another. Returns true if a is a prefix of b.
|
2012-02-01 00:50:03 +00:00
|
|
|
bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &value);
|
2012-05-09 09:33:42 +00:00
|
|
|
bool string_prefixes_string(const wchar_t *proposed_prefix, const wcstring &value);
|
2012-02-01 00:50:03 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if a string is a suffix of another.
|
2012-05-14 03:49:14 +00:00
|
|
|
bool string_suffixes_string(const wcstring &proposed_suffix, const wcstring &value);
|
|
|
|
bool string_suffixes_string(const wchar_t *proposed_suffix, const wcstring &value);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if a string prefixes another without regard to case. Returns true if a is a prefix of b.
|
|
|
|
bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix,
|
|
|
|
const wcstring &value);
|
2012-03-02 01:31:45 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
enum fuzzy_match_type_t {
|
|
|
|
// We match the string exactly: FOOBAR matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_exact = 0,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// We match a prefix of the string: FO matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_prefix,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// We match the string exactly, but in a case insensitive way: foobar matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_case_insensitive,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// We match a prefix of the string, in a case insensitive way: foo matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_prefix_case_insensitive,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// We match a substring of the string: OOBA matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_substring,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// A subsequence match with insertions only: FBR matches FOOBAR.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_subsequence_insertions_only,
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// We don't match the string.
|
2013-05-25 22:41:18 +00:00
|
|
|
fuzzy_match_none
|
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Indicates where a match type requires replacing the entire token.
|
|
|
|
static inline bool match_type_requires_full_replacement(fuzzy_match_type_t t) {
|
|
|
|
switch (t) {
|
2013-05-25 22:41:18 +00:00
|
|
|
case fuzzy_match_exact:
|
2016-04-27 23:10:14 +00:00
|
|
|
case fuzzy_match_prefix: {
|
2013-05-25 22:41:18 +00:00
|
|
|
return false;
|
2016-04-27 23:10:14 +00:00
|
|
|
}
|
|
|
|
default: { return true; }
|
2013-05-25 22:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Indicates where a match shares a prefix with the string it matches.
|
|
|
|
static inline bool match_type_shares_prefix(fuzzy_match_type_t t) {
|
|
|
|
switch (t) {
|
2013-05-25 22:41:18 +00:00
|
|
|
case fuzzy_match_exact:
|
|
|
|
case fuzzy_match_prefix:
|
|
|
|
case fuzzy_match_case_insensitive:
|
2016-04-27 23:10:14 +00:00
|
|
|
case fuzzy_match_prefix_case_insensitive: {
|
2013-05-25 22:41:18 +00:00
|
|
|
return true;
|
2016-04-27 23:10:14 +00:00
|
|
|
}
|
|
|
|
default: { return false; }
|
2013-05-25 22:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if string is a fuzzy match to another.
|
|
|
|
struct string_fuzzy_match_t {
|
2013-05-25 22:41:18 +00:00
|
|
|
enum fuzzy_match_type_t type;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Strength of the match. The value depends on the type. Lower is stronger.
|
2013-05-25 22:41:18 +00:00
|
|
|
size_t match_distance_first;
|
|
|
|
size_t match_distance_second;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Constructor.
|
|
|
|
explicit string_fuzzy_match_t(enum fuzzy_match_type_t t, size_t distance_first = 0,
|
|
|
|
size_t distance_second = 0);
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Return -1, 0, 1 if this match is (respectively) better than, equal to, or worse than rhs.
|
2013-05-25 22:41:18 +00:00
|
|
|
int compare(const string_fuzzy_match_t &rhs) const;
|
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Compute a fuzzy match for a string. If maximum_match is not fuzzy_match_none, limit the type to
|
|
|
|
/// matches at or below that type.
|
|
|
|
string_fuzzy_match_t string_fuzzy_match_string(const wcstring &string,
|
|
|
|
const wcstring &match_against,
|
|
|
|
fuzzy_match_type_t limit_type = fuzzy_match_none);
|
2013-05-25 22:41:18 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if a list contains a string using a linear search.
|
2012-02-27 04:11:34 +00:00
|
|
|
bool list_contains_string(const wcstring_list_t &list, const wcstring &str);
|
|
|
|
|
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.
|
2012-04-22 03:08:08 +00:00
|
|
|
void assert_is_locked(void *mutex, const char *who, const char *caller);
|
|
|
|
#define ASSERT_IS_LOCKED(x) assert_is_locked((void *)(&x), #x, __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
|
|
|
/// Our crappier versions of debug which is guaranteed to not allocate any memory, or do anything
|
|
|
|
/// other than call write(). This is useful after a call to fork() with threads.
|
|
|
|
void debug_safe(int level, const char *msg, const char *param1 = NULL, const char *param2 = NULL,
|
|
|
|
const char *param3 = NULL, const char *param4 = NULL, const char *param5 = NULL,
|
|
|
|
const char *param6 = NULL, const char *param7 = NULL, const char *param8 = NULL,
|
|
|
|
const char *param9 = NULL, const char *param10 = NULL, const char *param11 = NULL,
|
|
|
|
const char *param12 = NULL);
|
2012-03-05 18:18:42 +00:00
|
|
|
|
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);
|
2012-03-05 18:18:42 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// "Narrows" a wide character string. This just grabs any ASCII characters and trunactes.
|
2016-02-28 09:38:28 +00:00
|
|
|
void narrow_string_safe(char buff[64], const wchar_t *s);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <typename T>
|
|
|
|
T from_string(const wcstring &x) {
|
2011-12-27 03:18:46 +00:00
|
|
|
T result;
|
|
|
|
std::wstringstream stream(x);
|
|
|
|
stream >> result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <typename T>
|
|
|
|
T from_string(const std::string &x) {
|
2012-03-05 18:44:08 +00:00
|
|
|
T result = T();
|
|
|
|
std::stringstream stream(x);
|
|
|
|
stream >> result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <typename T>
|
|
|
|
wcstring to_string(const T &x) {
|
2012-02-02 00:27:14 +00:00
|
|
|
std::wstringstream stream;
|
|
|
|
stream << x;
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// wstringstream is a huge memory pig. Let's provide some specializations where we can.
|
|
|
|
template <>
|
|
|
|
inline wcstring to_string(const long &x) {
|
2012-03-05 18:18:42 +00:00
|
|
|
wchar_t buff[128];
|
|
|
|
format_long_safe(buff, x);
|
|
|
|
return wcstring(buff);
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <>
|
|
|
|
inline bool from_string(const std::string &x) {
|
|
|
|
return !x.empty() && strchr("YTyt1", x.at(0));
|
2012-03-05 18:44:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <>
|
|
|
|
inline bool from_string(const wcstring &x) {
|
|
|
|
return !x.empty() && wcschr(L"YTyt1", x.at(0));
|
2012-03-05 18:44:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
template <>
|
|
|
|
inline wcstring to_string(const int &x) {
|
2012-03-05 18:18:42 +00:00
|
|
|
return to_string(static_cast<long>(x));
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// A hackish thing to simulate rvalue references in C++98. The idea is that you can define a
|
|
|
|
// constructor to take a moved_ref<T> and then swap() out of it.
|
|
|
|
template <typename T>
|
|
|
|
struct moved_ref {
|
2016-02-28 08:33:11 +00:00
|
|
|
T &val;
|
2016-04-27 23:10:14 +00:00
|
|
|
|
|
|
|
explicit moved_ref(T &v) : val(v) {}
|
2016-02-28 08:33:11 +00:00
|
|
|
};
|
|
|
|
|
2013-02-23 00:22:56 +00:00
|
|
|
wchar_t **make_null_terminated_array(const wcstring_list_t &lst);
|
|
|
|
char **make_null_terminated_array(const std::vector<std::string> &lst);
|
2012-03-05 18:18:42 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Helper class for managing a null-terminated array of null-terminated strings (of some char type).
|
2012-02-28 23:11:46 +00:00
|
|
|
template <typename CharType_t>
|
2016-04-27 23:10:14 +00:00
|
|
|
class null_terminated_array_t {
|
2012-02-28 23:11:46 +00:00
|
|
|
CharType_t **array;
|
2013-02-27 20:03:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// No assignment or copying.
|
2013-02-23 00:22:56 +00:00
|
|
|
void operator=(null_terminated_array_t rhs);
|
|
|
|
null_terminated_array_t(const null_terminated_array_t &);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2013-02-23 00:22:56 +00:00
|
|
|
typedef std::vector<std::basic_string<CharType_t> > string_list_t;
|
2012-02-28 23:11:46 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
size_t size() const {
|
2013-02-23 00:22:56 +00:00
|
|
|
size_t len = 0;
|
2016-04-27 23:10:14 +00:00
|
|
|
if (array != NULL) {
|
|
|
|
while (array[len] != NULL) {
|
2013-02-23 00:22:56 +00:00
|
|
|
len++;
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-23 00:22:56 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
void free(void) {
|
2013-02-23 00:22:56 +00:00
|
|
|
::free((void *)array);
|
|
|
|
array = NULL;
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
public:
|
|
|
|
null_terminated_array_t() : array(NULL) {}
|
|
|
|
explicit null_terminated_array_t(const string_list_t &argv)
|
|
|
|
: array(make_null_terminated_array(argv)) {}
|
2013-02-27 20:03:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
~null_terminated_array_t() { this->free(); }
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
void set(const string_list_t &argv) {
|
2012-02-28 23:11:46 +00:00
|
|
|
this->free();
|
2013-02-23 00:22:56 +00:00
|
|
|
this->array = make_null_terminated_array(argv);
|
2012-02-28 23:11:46 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
const CharType_t *const *get() const { return array; }
|
2013-02-27 20:03:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
void clear() { this->free(); }
|
2012-02-28 23:11:46 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Helper function to convert from a null_terminated_array_t<wchar_t> to a
|
|
|
|
// null_terminated_array_t<char_t>.
|
|
|
|
void convert_wide_array_to_narrow(const null_terminated_array_t<wchar_t> &arr,
|
|
|
|
null_terminated_array_t<char> *output);
|
2012-02-29 19:27:14 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
class mutex_lock_t {
|
|
|
|
public:
|
2014-05-04 22:06:40 +00:00
|
|
|
pthread_mutex_t mutex;
|
2016-04-27 23:10:14 +00:00
|
|
|
mutex_lock_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_mutex_init(&mutex, NULL)); }
|
|
|
|
|
|
|
|
~mutex_lock_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_mutex_destroy(&mutex)); }
|
2014-05-04 22:06:40 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Basic scoped lock class.
|
|
|
|
class scoped_lock {
|
2012-01-28 22:56:13 +00:00
|
|
|
pthread_mutex_t *lock_obj;
|
|
|
|
bool locked;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// No copying.
|
2012-08-05 20:24:33 +00:00
|
|
|
scoped_lock &operator=(const scoped_lock &);
|
|
|
|
scoped_lock(const scoped_lock &);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
public:
|
2012-02-28 02:43:24 +00:00
|
|
|
void lock(void);
|
|
|
|
void unlock(void);
|
2016-02-28 03:38:15 +00:00
|
|
|
explicit scoped_lock(pthread_mutex_t &mutex);
|
|
|
|
explicit scoped_lock(mutex_lock_t &lock);
|
2012-02-28 02:43:24 +00:00
|
|
|
~scoped_lock();
|
2011-12-27 03:18:46 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
class rwlock_t {
|
|
|
|
public:
|
2014-08-24 07:59:03 +00:00
|
|
|
pthread_rwlock_t rwlock;
|
2016-04-27 23:10:14 +00:00
|
|
|
rwlock_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_rwlock_init(&rwlock, NULL)); }
|
2014-08-24 07:59:03 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
~rwlock_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_rwlock_destroy(&rwlock)); }
|
2014-08-24 07:59:03 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// Scoped lock class for rwlocks.
|
|
|
|
class scoped_rwlock {
|
2014-08-24 07:59:03 +00:00
|
|
|
pthread_rwlock_t *rwlock_obj;
|
|
|
|
bool locked;
|
|
|
|
bool locked_shared;
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// No copying.
|
2014-08-24 07:59:03 +00:00
|
|
|
scoped_rwlock &operator=(const scoped_lock &);
|
2016-02-28 03:38:15 +00:00
|
|
|
explicit scoped_rwlock(const scoped_lock &);
|
2014-08-24 07:59:03 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
public:
|
2014-08-24 07:59:03 +00:00
|
|
|
void lock(void);
|
|
|
|
void unlock(void);
|
|
|
|
void lock_shared(void);
|
|
|
|
void unlock_shared(void);
|
2016-04-27 23:10:14 +00:00
|
|
|
// Upgrade shared lock to exclusive. Equivalent to `lock.unlock_shared(); lock.lock();`.
|
2014-08-24 07:59:03 +00:00
|
|
|
void upgrade(void);
|
2016-02-28 03:38:15 +00:00
|
|
|
explicit scoped_rwlock(pthread_rwlock_t &rwlock, bool shared = false);
|
|
|
|
explicit scoped_rwlock(rwlock_t &rwlock, bool shared = false);
|
2014-08-24 07:59:03 +00:00
|
|
|
~scoped_rwlock();
|
|
|
|
};
|
2013-02-22 14:34:30 +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
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
scoped_push(T *r, const T &new_value) : ref(r), saved_value(*r), restored(false) {
|
2013-02-27 20:03:30 +00:00
|
|
|
*r = 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) {
|
2013-02-27 20:03:30 +00:00
|
|
|
std::swap(*ref, saved_value);
|
|
|
|
restored = true;
|
|
|
|
}
|
2013-02-22 14:34:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Wrapper around wcstok.
|
|
|
|
class wcstokenizer {
|
2011-12-27 03:18:46 +00:00
|
|
|
wchar_t *buffer, *str, *state;
|
|
|
|
const wcstring sep;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
// No copying.
|
2012-08-05 20:24:33 +00:00
|
|
|
wcstokenizer &operator=(const wcstokenizer &);
|
|
|
|
wcstokenizer(const wcstokenizer &);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
public:
|
2012-07-20 21:33:08 +00:00
|
|
|
wcstokenizer(const wcstring &s, const wcstring &separator);
|
|
|
|
bool next(wcstring &result);
|
|
|
|
~wcstokenizer();
|
2011-12-27 03:18:46 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Appends a path component, with a / if necessary.
|
2011-12-27 03:18:46 +00:00
|
|
|
void append_path_component(wcstring &path, const wcstring &component);
|
|
|
|
|
|
|
|
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, ...);
|
2013-03-24 22:24:29 +00:00
|
|
|
void append_formatv(wcstring &str, const wchar_t *format, va_list ap);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if the given string is a valid variable name.
|
|
|
|
///
|
|
|
|
/// \return null if this is a valid name, and a pointer to the first invalid character otherwise.
|
2014-06-09 19:57:44 +00:00
|
|
|
const wchar_t *wcsvarname(const wchar_t *str);
|
2015-05-17 21:17:01 +00:00
|
|
|
const wchar_t *wcsvarname(const wcstring &str);
|
2006-10-19 15:39:50 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if the given string is a valid function name.
|
|
|
|
///
|
|
|
|
/// \return null if this is a valid name, and a pointer to the first invalid character otherwise.
|
2015-05-17 21:17:01 +00:00
|
|
|
const wchar_t *wcsfuncname(const wcstring &str);
|
2006-10-19 15:47:47 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Test if the given string is valid in a variable name.
|
|
|
|
///
|
|
|
|
/// \return true if this is a valid name, false otherwise.
|
2013-08-08 22:06:46 +00:00
|
|
|
bool wcsvarchr(wchar_t chr);
|
2006-06-01 23:04:38 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Convenience variants on fish_wcwswidth().
|
|
|
|
///
|
|
|
|
/// See fallback.h for the normal definitions.
|
2014-09-26 01:04:11 +00:00
|
|
|
int fish_wcswidth(const wchar_t *str);
|
2016-04-27 23:10:14 +00:00
|
|
|
int fish_wcswidth(const wcstring &str);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// This functions returns the end of the quoted substring beginning at \c in. The type of quoting
|
|
|
|
/// character is detemrined by examining \c in. Returns 0 on error.
|
|
|
|
///
|
|
|
|
/// \param in the position of the opening quote.
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t *quote_end(const wchar_t *in);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// A call to this function will reset the error counter. Some functions print out non-critical
|
|
|
|
/// error messages. These should check the error_count before, and skip printing the message if
|
|
|
|
/// MAX_ERROR_COUNT messages have been printed. The error_reset() should be called after each
|
|
|
|
/// interactive command executes, to allow new messages to be printed.
|
2005-09-20 13:26:39 +00:00
|
|
|
void error_reset();
|
|
|
|
|
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
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Checks if \c needle is included in the list of strings specified. A warning is printed if needle
|
|
|
|
/// is zero.
|
|
|
|
///
|
|
|
|
/// \param needle the string to search for in the list.
|
|
|
|
///
|
|
|
|
/// \return zero if needle is not found, of if needle is null, non-zero otherwise.
|
2014-03-10 01:43:40 +00:00
|
|
|
__sentinel bool contains_internal(const wchar_t *needle, int vararg_handle, ...);
|
|
|
|
__sentinel bool contains_internal(const wcstring &needle, int vararg_handle, ...);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Call read while blocking the SIGCHLD signal. Should only be called if you _know_ there is data
|
|
|
|
/// available for reading, or the program will hang until there is data.
|
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
|
|
|
/// Issue a debug message with printf-style string formating and automatic line breaking. The string
|
|
|
|
/// will begin with the string \c program_name, followed by a colon and a whitespace.
|
|
|
|
///
|
|
|
|
/// Because debug is often called to tell the user about an error, before using wperror to give a
|
|
|
|
/// specific error message, debug will never ever modify the value of errno.
|
|
|
|
///
|
|
|
|
/// \param level the priority of the message. Lower number means higher priority. Messages with a
|
|
|
|
/// priority_number higher than \c debug_level will be ignored..
|
|
|
|
/// \param msg the message format string.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// <code>debug( 1, L"Pi = %.3f", M_PI );</code>
|
|
|
|
///
|
|
|
|
/// will print the string 'fish: Pi = 3.141', given that debug_level is 1 or higher, and that
|
|
|
|
/// program_name is 'fish'.
|
2016-05-16 02:45:02 +00:00
|
|
|
void __attribute__((noinline)) debug(int level, const char *msg, ...);
|
|
|
|
void __attribute__((noinline)) debug(int level, const wchar_t *msg, ...);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
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
|
2014-09-26 01:20:03 +00:00
|
|
|
wcstring escape(const wchar_t *in, escape_flags_t flags);
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring escape_string(const wcstring &in, escape_flags_t flags);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
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,
|
|
|
|
/// appending to result. Return the number of characters consumed, or 0 on error.
|
|
|
|
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);
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Unescapes a string, returning the unescaped value by reference. On failure, the output is set to
|
|
|
|
/// an empty string.
|
2013-11-25 06:57:49 +00:00
|
|
|
bool unescape_string(const wchar_t *input, wcstring *output, unescape_flags_t escape_special);
|
|
|
|
bool unescape_string(const wcstring &input, wcstring *output, unescape_flags_t escape_special);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Returns the width of the terminal window, so that not all functions that use these values
|
|
|
|
/// continually have to keep track of it separately.
|
|
|
|
///
|
|
|
|
/// Only works if common_handle_winch is registered to handle winch signals.
|
2005-10-14 11:40:33 +00:00
|
|
|
int common_get_width();
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Returns the height of the terminal window, so that not all functions that use these values
|
|
|
|
/// continually have to keep track of it separatly.
|
|
|
|
///
|
|
|
|
/// Only works if common_handle_winch is registered to handle winch signals.
|
2005-10-14 11:40:33 +00:00
|
|
|
int common_get_height();
|
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Handle a window change event by looking up the new window size and saving it in an internal
|
|
|
|
/// variable used by common_get_wisth and common_get_height().
|
2012-11-19 00:30:30 +00:00
|
|
|
void common_handle_winch(int signal);
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Write the given paragraph of output, redoing linebreaks to fit the current screen.
|
2015-09-21 18:24:49 +00:00
|
|
|
wcstring reformat_for_screen(const wcstring &msg);
|
2006-01-15 11:58:05 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Tokenize the specified string into the specified wcstring_list_t.
|
|
|
|
///
|
|
|
|
/// \param val the input string. The contents of this string is not changed.
|
|
|
|
/// \param out the list in which to place the elements.
|
2012-11-19 00:30:30 +00:00
|
|
|
void tokenize_variable_array(const wcstring &val, wcstring_list_t &out);
|
2006-05-29 11:13:42 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Make sure the specified direcotry exists. If needed, try to create it and any currently not
|
|
|
|
/// existing parent directories.
|
|
|
|
///
|
|
|
|
/// \return 0 if, at the time of function return the directory exists, -1 otherwise.
|
2012-11-19 00:30:30 +00:00
|
|
|
int create_directory(const wcstring &d);
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Print a short message about how to file a bug report to stderr.
|
2006-11-17 14:58:25 +00:00
|
|
|
void bugreport();
|
|
|
|
|
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.
|
2009-02-02 22:46:45 +00:00
|
|
|
double timef();
|
|
|
|
|
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.
|
2012-11-18 10:16:14 +00:00
|
|
|
void save_term_foreground_process_group(void);
|
|
|
|
void restore_term_foreground_process_group(void);
|
|
|
|
|
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
|
|
|
|
2016-04-27 23:10:14 +00:00
|
|
|
/// Macro to help suppress potentially unused variable warnings.
|
2014-05-01 23:44:37 +00:00
|
|
|
#define USE(var) (void)(var)
|
|
|
|
|
2012-03-01 01:55:50 +00:00
|
|
|
extern "C" {
|
2016-04-27 23:10:14 +00:00
|
|
|
__attribute__((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);
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|