fish-shell/src/output.h
Kurtis Rader 509ee64fc9 implement our own assert() function
I recently upgraded the software on my macOS server and was dismayed to
see that cppcheck reported a huge number of format string errors due to
mismatches between the format string and its arguments from calls to
`assert()`. It turns out they are due to the macOS header using `%lu`
for the line number which is obviously wrong since it is using the C
preprocessor `__LINE__` symbol which evaluates to a signed int.

I also noticed that the macOS implementation writes to stdout, rather
than stderr. It also uses `printf()` which can be a problem on some
platforms if the stream is already in wide mode which is the normal case
for fish.

So implement our own `assert()` implementation. This also eliminates
double-negative warnings that we get from some of our calls to
`assert()` on some platforms by oclint.

Also reimplement the `DIE()` macro in terms of our internal
implementation.

Rewrite `assert(0 && msg)` statements to `DIE(msg)` for clarity and to
eliminate oclint warnings about constant expressions.

Fixes #3276, albeit not in the fashion I originally envisioned.
2017-02-14 18:48:27 -08:00

58 lines
1.6 KiB
C++

// Generic output functions.
//
// Constants for various character classifications. Each character of a command string can be
// classified as one of the following types.
#ifndef FISH_OUTPUT_H
#define FISH_OUTPUT_H
#include <stddef.h>
#include <vector>
#include "color.h"
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
/// Constants for various colors as used by the set_color function.
enum {
FISH_COLOR_BLACK, // 0
FISH_COLOR_RED, // 1
FISH_COLOR_GREEN, // 2
FISH_COLOR_YELLOW, // 3
FISH_COLOR_BLUE, // 4
FISH_COLOR_MAGENTA, // 5
FISH_COLOR_CYAN, // 6
FISH_COLOR_WHITE, // 7
FISH_COLOR_NORMAL, // 8 terminal default
FISH_COLOR_RESET // 9
};
void set_color(rgb_color_t c, rgb_color_t c2);
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line);
#define writembs(mbs) writembs_check((mbs), #mbs, __FILE__, __LINE__)
int writech(wint_t ch);
void writestr(const wchar_t *str);
rgb_color_t parse_color(const wcstring &val, bool is_background);
int writeb(tputs_arg_t b);
void output_set_writer(int (*writer)(char));
int (*output_get_writer())(char);
/// Sets what colors are supported.
enum { color_support_term256 = 1 << 0, color_support_term24bit = 1 << 1 };
typedef unsigned int color_support_t;
color_support_t output_get_color_support();
void output_set_color_support(color_support_t support);
rgb_color_t best_color(const std::vector<rgb_color_t> &colors, color_support_t support);
bool write_color(rgb_color_t color, bool is_fg);
unsigned char index_for_color(rgb_color_t c);
#endif