refactor: remove os_errmsg and os_msg functions

Instead replace them with fprintf and printf.
This commit is contained in:
dundargoc 2023-12-21 15:57:55 +01:00 committed by dundargoc
parent 7121241e5c
commit eae6727325
7 changed files with 79 additions and 135 deletions

View File

@ -1486,7 +1486,7 @@ void print_line(linenr_T lnum, int use_number, int list)
msg_start(); msg_start();
silent_mode = false; silent_mode = false;
info_message = true; // use os_msg(), not os_errmsg() info_message = true; // use stdout, not stderr
print_line_no_prefix(lnum, use_number, list); print_line_no_prefix(lnum, use_number, list);
if (save_silent) { if (save_silent) {
msg_putchar('\n'); msg_putchar('\n');

View File

@ -252,8 +252,7 @@ static int nlua_luv_thread_common_cfpcall(lua_State *lstate, int nargs, int nres
if (status == LUA_ERRMEM && !(flags & LUVF_CALLBACK_NOEXIT)) { if (status == LUA_ERRMEM && !(flags & LUVF_CALLBACK_NOEXIT)) {
// Terminate this thread, as the main thread may be able to continue // Terminate this thread, as the main thread may be able to continue
// execution. // execution.
os_errmsg(e_outofmem); fprintf(stderr, "%s\n", e_outofmem);
os_errmsg("\n");
lua_close(lstate); lua_close(lstate);
#ifdef MSWIN #ifdef MSWIN
ExitThread(0); ExitThread(0);
@ -640,8 +639,7 @@ static bool nlua_init_packages(lua_State *lstate, bool is_standalone)
lua_getglobal(lstate, "require"); lua_getglobal(lstate, "require");
lua_pushstring(lstate, "vim._init_packages"); lua_pushstring(lstate, "vim._init_packages");
if (nlua_pcall(lstate, 1, 0)) { if (nlua_pcall(lstate, 1, 0)) {
os_errmsg(lua_tostring(lstate, -1)); fprintf(stderr, "%s\n", lua_tostring(lstate, -1));
os_errmsg("\n");
return false; return false;
} }
@ -815,12 +813,12 @@ void nlua_init(char **argv, int argc, int lua_arg0)
lua_State *lstate = luaL_newstate(); lua_State *lstate = luaL_newstate();
if (lstate == NULL) { if (lstate == NULL) {
os_errmsg(_("E970: Failed to initialize lua interpreter\n")); fprintf(stderr, _("E970: Failed to initialize lua interpreter\n"));
os_exit(1); os_exit(1);
} }
luaL_openlibs(lstate); luaL_openlibs(lstate);
if (!nlua_state_init(lstate)) { if (!nlua_state_init(lstate)) {
os_errmsg(_("E970: Failed to initialize builtin lua modules\n")); fprintf(stderr, _("E970: Failed to initialize builtin lua modules\n"));
#ifdef EXITFREE #ifdef EXITFREE
nlua_common_free_all_mem(lstate); nlua_common_free_all_mem(lstate);
#endif #endif
@ -2307,8 +2305,7 @@ void nlua_init_defaults(void)
lua_getglobal(L, "require"); lua_getglobal(L, "require");
lua_pushstring(L, "vim._defaults"); lua_pushstring(L, "vim._defaults");
if (nlua_pcall(L, 1, 0)) { if (nlua_pcall(L, 1, 0)) {
os_errmsg(lua_tostring(L, -1)); fprintf(stderr, "%s\n", lua_tostring(L, -1));
os_errmsg("\n");
} }
} }

View File

@ -247,7 +247,7 @@ int main(int argc, char **argv)
argv0 = argv[0]; argv0 = argv[0];
if (!appname_is_valid()) { if (!appname_is_valid()) {
os_errmsg("$NVIM_APPNAME must be a name or relative path.\n"); fprintf(stderr, "$NVIM_APPNAME must be a name or relative path.\n");
exit(1); exit(1);
} }
@ -336,7 +336,7 @@ int main(int argc, char **argv)
ui_client_forward_stdin = !stdin_isatty; ui_client_forward_stdin = !stdin_isatty;
uint64_t rv = ui_client_start_server(params.argc, params.argv); uint64_t rv = ui_client_start_server(params.argc, params.argv);
if (!rv) { if (!rv) {
os_errmsg("Failed to start Nvim server!\n"); fprintf(stderr, "Failed to start Nvim server!\n");
os_exit(1); os_exit(1);
} }
ui_client_channel_id = rv; ui_client_channel_id = rv;
@ -823,8 +823,7 @@ void preserve_exit(const char *errmsg)
ui_client_stop(); ui_client_stop();
} }
if (errmsg != NULL) { if (errmsg != NULL) {
os_errmsg(errmsg); fprintf(stderr, "%s\n", errmsg);
os_errmsg("\n");
} }
if (ui_client_channel_id) { if (ui_client_channel_id) {
os_exit(1); os_exit(1);
@ -835,7 +834,7 @@ void preserve_exit(const char *errmsg)
FOR_ALL_BUFFERS(buf) { FOR_ALL_BUFFERS(buf) {
if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) { if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) {
if (errmsg != NULL) { if (errmsg != NULL) {
os_errmsg("Vim: preserving files...\r\n"); fprintf(stderr, "Vim: preserving files...\r\n");
} }
ml_sync_all(false, false, true); // preserve all swap files ml_sync_all(false, false, true); // preserve all swap files
break; break;
@ -845,7 +844,7 @@ void preserve_exit(const char *errmsg)
ml_close_all(false); // close all memfiles, without deleting ml_close_all(false); // close all memfiles, without deleting
if (errmsg != NULL) { if (errmsg != NULL) {
os_errmsg("Vim: Finished.\r\n"); fprintf(stderr, "Vim: Finished.\r\n");
} }
getout(1); getout(1);
@ -910,14 +909,11 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
if (is_ui) { if (is_ui) {
if (!chan) { if (!chan) {
os_errmsg("Remote ui failed to start: "); fprintf(stderr, "Remote ui failed to start: %s\n", connect_error);
os_errmsg(connect_error);
os_errmsg("\n");
os_exit(1); os_exit(1);
} else if (strequal(server_addr, os_getenv("NVIM"))) { } else if (strequal(server_addr, os_getenv("NVIM"))) {
os_errmsg("Cannot attach UI of :terminal child to its parent. "); fprintf(stderr, "%s", "Cannot attach UI of :terminal child to its parent. ");
os_errmsg("(Unset $NVIM to skip this check)"); fprintf(stderr, "%s\n", "(Unset $NVIM to skip this check)");
os_errmsg("\n");
os_exit(1); os_exit(1);
} }
@ -941,15 +937,14 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
Object o = nlua_exec(s, a, &err); Object o = nlua_exec(s, a, &err);
api_free_array(a); api_free_array(a);
if (ERROR_SET(&err)) { if (ERROR_SET(&err)) {
os_errmsg(err.msg); fprintf(stderr, "%s\n", err.msg);
os_errmsg("\n");
os_exit(2); os_exit(2);
} }
if (o.type == kObjectTypeDictionary) { if (o.type == kObjectTypeDictionary) {
rvobj.data.dictionary = o.data.dictionary; rvobj.data.dictionary = o.data.dictionary;
} else { } else {
os_errmsg("vim._cs_remote returned unexpected value\n"); fprintf(stderr, "vim._cs_remote returned unexpected value\n");
os_exit(2); os_exit(2);
} }
@ -959,34 +954,33 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
for (size_t i = 0; i < rvobj.data.dictionary.size; i++) { for (size_t i = 0; i < rvobj.data.dictionary.size; i++) {
if (strequal(rvobj.data.dictionary.items[i].key.data, "errmsg")) { if (strequal(rvobj.data.dictionary.items[i].key.data, "errmsg")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) {
os_errmsg("vim._cs_remote returned an unexpected type for 'errmsg'\n"); fprintf(stderr, "vim._cs_remote returned an unexpected type for 'errmsg'\n");
os_exit(2); os_exit(2);
} }
os_errmsg(rvobj.data.dictionary.items[i].value.data.string.data); fprintf(stderr, "%s\n", rvobj.data.dictionary.items[i].value.data.string.data);
os_errmsg("\n");
os_exit(2); os_exit(2);
} else if (strequal(rvobj.data.dictionary.items[i].key.data, "result")) { } else if (strequal(rvobj.data.dictionary.items[i].key.data, "result")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) {
os_errmsg("vim._cs_remote returned an unexpected type for 'result'\n"); fprintf(stderr, "vim._cs_remote returned an unexpected type for 'result'\n");
os_exit(2); os_exit(2);
} }
os_msg(rvobj.data.dictionary.items[i].value.data.string.data); printf("%s", rvobj.data.dictionary.items[i].value.data.string.data);
} else if (strequal(rvobj.data.dictionary.items[i].key.data, "tabbed")) { } else if (strequal(rvobj.data.dictionary.items[i].key.data, "tabbed")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) {
os_errmsg("vim._cs_remote returned an unexpected type for 'tabbed'\n"); fprintf(stderr, "vim._cs_remote returned an unexpected type for 'tabbed'\n");
os_exit(2); os_exit(2);
} }
tabbed = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse; tabbed = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse;
} else if (strequal(rvobj.data.dictionary.items[i].key.data, "should_exit")) { } else if (strequal(rvobj.data.dictionary.items[i].key.data, "should_exit")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) {
os_errmsg("vim._cs_remote returned an unexpected type for 'should_exit'\n"); fprintf(stderr, "vim._cs_remote returned an unexpected type for 'should_exit'\n");
os_exit(2); os_exit(2);
} }
should_exit = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse; should_exit = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse;
} }
} }
if (should_exit == kNone || tabbed == kNone) { if (should_exit == kNone || tabbed == kNone) {
os_errmsg("vim._cs_remote didn't return a value for should_exit or tabbed, bailing\n"); fprintf(stderr, "vim._cs_remote didn't return a value for should_exit or tabbed, bailing\n");
os_exit(2); os_exit(2);
} }
api_free_object(o); api_free_object(o);
@ -1377,7 +1371,7 @@ scripterror:
vim_snprintf(IObuff, IOSIZE, vim_snprintf(IObuff, IOSIZE,
_("Attempt to open script file again: \"%s %s\"\n"), _("Attempt to open script file again: \"%s %s\"\n"),
argv[-1], argv[0]); argv[-1], argv[0]);
os_errmsg(IObuff); fprintf(stderr, "%s", IObuff);
os_exit(2); os_exit(2);
} }
parmp->scriptin = argv[0]; parmp->scriptin = argv[0];
@ -1614,7 +1608,7 @@ static void open_script_files(mparm_T *parmp)
vim_snprintf(IObuff, IOSIZE, vim_snprintf(IObuff, IOSIZE,
_("Cannot open for reading: \"%s\": %s\n"), _("Cannot open for reading: \"%s\": %s\n"),
parmp->scriptin, os_strerror(error)); parmp->scriptin, os_strerror(error));
os_errmsg(IObuff); fprintf(stderr, "%s", IObuff);
os_exit(2); os_exit(2);
} }
} }
@ -1624,9 +1618,8 @@ static void open_script_files(mparm_T *parmp)
if (parmp->scriptout) { if (parmp->scriptout) {
scriptout = os_fopen(parmp->scriptout, parmp->scriptout_append ? APPENDBIN : WRITEBIN); scriptout = os_fopen(parmp->scriptout, parmp->scriptout_append ? APPENDBIN : WRITEBIN);
if (scriptout == NULL) { if (scriptout == NULL) {
os_errmsg(_("Cannot open for script output: \"")); fprintf(stderr, _("Cannot open for script output: \""));
os_errmsg(parmp->scriptout); fprintf(stderr, "%s\"\n", parmp->scriptout);
os_errmsg("\"\n");
os_exit(2); os_exit(2);
} }
} }
@ -2158,17 +2151,12 @@ static void print_mainerr(const char *errstr, const char *str)
signal_stop(); // kill us with CTRL-C here, if you like signal_stop(); // kill us with CTRL-C here, if you like
os_errmsg(prgname); fprintf(stderr, "%s: %s", prgname, _(errstr));
os_errmsg(": ");
os_errmsg(_(errstr));
if (str != NULL) { if (str != NULL) {
os_errmsg(": \""); fprintf(stderr, ": \"%s\"", str);
os_errmsg(str);
os_errmsg("\"");
} }
os_errmsg(_("\nMore info with \"")); fprintf(stderr, _("\nMore info with \""));
os_errmsg(prgname); fprintf(stderr, "%s -h\"\n", prgname);
os_errmsg(" -h\"\n");
} }
/// Prints version information for "nvim -v" or "nvim --version". /// Prints version information for "nvim -v" or "nvim --version".
@ -2176,7 +2164,7 @@ static void version(void)
{ {
// TODO(bfred): not like this? // TODO(bfred): not like this?
nlua_init(NULL, 0, -1); nlua_init(NULL, 0, -1);
info_message = true; // use os_msg(), not os_errmsg() info_message = true; // use stdout, not stderr
list_version(); list_version();
msg_putchar('\n'); msg_putchar('\n');
msg_didout = false; msg_didout = false;
@ -2187,38 +2175,38 @@ static void usage(void)
{ {
signal_stop(); // kill us with CTRL-C here, if you like signal_stop(); // kill us with CTRL-C here, if you like
os_msg(_("Usage:\n")); printf(_("Usage:\n"));
os_msg(_(" nvim [options] [file ...]\n")); printf(_(" nvim [options] [file ...]\n"));
os_msg(_("\nOptions:\n")); printf(_("\nOptions:\n"));
os_msg(_(" --cmd <cmd> Execute <cmd> before any config\n")); printf(_(" --cmd <cmd> Execute <cmd> before any config\n"));
os_msg(_(" +<cmd>, -c <cmd> Execute <cmd> after config and first file\n")); printf(_(" +<cmd>, -c <cmd> Execute <cmd> after config and first file\n"));
os_msg(_(" -l <script> [args...] Execute Lua <script> (with optional args)\n")); printf(_(" -l <script> [args...] Execute Lua <script> (with optional args)\n"));
os_msg(_(" -S <session> Source <session> after loading the first file\n")); printf(_(" -S <session> Source <session> after loading the first file\n"));
os_msg(_(" -s <scriptin> Read Normal mode commands from <scriptin>\n")); printf(_(" -s <scriptin> Read Normal mode commands from <scriptin>\n"));
os_msg(_(" -u <config> Use this config file\n")); printf(_(" -u <config> Use this config file\n"));
os_msg("\n"); printf("\n");
os_msg(_(" -d Diff mode\n")); printf(_(" -d Diff mode\n"));
os_msg(_(" -es, -Es Silent (batch) mode\n")); printf(_(" -es, -Es Silent (batch) mode\n"));
os_msg(_(" -h, --help Print this help message\n")); printf(_(" -h, --help Print this help message\n"));
os_msg(_(" -i <shada> Use this shada file\n")); printf(_(" -i <shada> Use this shada file\n"));
os_msg(_(" -n No swap file, use memory only\n")); printf(_(" -n No swap file, use memory only\n"));
os_msg(_(" -o[N] Open N windows (default: one per file)\n")); printf(_(" -o[N] Open N windows (default: one per file)\n"));
os_msg(_(" -O[N] Open N vertical windows (default: one per file)\n")); printf(_(" -O[N] Open N vertical windows (default: one per file)\n"));
os_msg(_(" -p[N] Open N tab pages (default: one per file)\n")); printf(_(" -p[N] Open N tab pages (default: one per file)\n"));
os_msg(_(" -R Read-only (view) mode\n")); printf(_(" -R Read-only (view) mode\n"));
os_msg(_(" -v, --version Print version information\n")); printf(_(" -v, --version Print version information\n"));
os_msg(_(" -V[N][file] Verbose [level][file]\n")); printf(_(" -V[N][file] Verbose [level][file]\n"));
os_msg("\n"); printf("\n");
os_msg(_(" -- Only file names after this\n")); printf(_(" -- Only file names after this\n"));
os_msg(_(" --api-info Write msgpack-encoded API metadata to stdout\n")); printf(_(" --api-info Write msgpack-encoded API metadata to stdout\n"));
os_msg(_(" --clean \"Factory defaults\" (skip user config and plugins, shada)\n")); printf(_(" --clean \"Factory defaults\" (skip user config and plugins, shada)\n"));
os_msg(_(" --embed Use stdin/stdout as a msgpack-rpc channel\n")); printf(_(" --embed Use stdin/stdout as a msgpack-rpc channel\n"));
os_msg(_(" --headless Don't start a user interface\n")); printf(_(" --headless Don't start a user interface\n"));
os_msg(_(" --listen <address> Serve RPC API from this address\n")); printf(_(" --listen <address> Serve RPC API from this address\n"));
os_msg(_(" --remote[-subcommand] Execute commands remotely on a server\n")); printf(_(" --remote[-subcommand] Execute commands remotely on a server\n"));
os_msg(_(" --server <address> Specify RPC server to send commands to\n")); printf(_(" --server <address> Specify RPC server to send commands to\n"));
os_msg(_(" --startuptime <file> Write startup timing messages to <file>\n")); printf(_(" --startuptime <file> Write startup timing messages to <file>\n"));
os_msg(_("\nSee \":help startup-options\" for all options.\n")); printf(_("\nSee \":help startup-options\" for all options.\n"));
} }
// Check the result of the ATTENTION dialog: // Check the result of the ATTENTION dialog:

View File

@ -225,7 +225,7 @@ int verb_msg(const char *s)
} }
/// Displays the string 's' on the status line /// Displays the string 's' on the status line
/// When terminal not initialized (yet) os_errmsg(..) is used. /// When terminal not initialized (yet) printf("%s", ..) is used.
/// ///
/// @return true if wait_return() not called /// @return true if wait_return() not called
int msg(const char *s, const int attr) int msg(const char *s, const int attr)
@ -748,7 +748,7 @@ bool emsg_multiline(const char *s, bool multiline)
/// emsg() - display an error message /// emsg() - display an error message
/// ///
/// Rings the bell, if appropriate, and calls message() to do the real work /// Rings the bell, if appropriate, and calls message() to do the real work
/// When terminal not initialized (yet) os_errmsg(..) is used. /// When terminal not initialized (yet) fprintf(stderr, "%s", ..) is used.
/// ///
/// @return true if wait_return() not called /// @return true if wait_return() not called
bool emsg(const char *s) bool emsg(const char *s)
@ -2635,9 +2635,9 @@ static void msg_puts_printf(const char *str, const ptrdiff_t maxlen)
memcpy(p, s, (size_t)len); memcpy(p, s, (size_t)len);
*(p + len) = '\0'; *(p + len) = '\0';
if (info_message) { if (info_message) {
os_msg(buf); printf("%s", buf);
} else { } else {
os_errmsg(buf); fprintf(stderr, "%s", buf);
} }
} }
@ -2899,40 +2899,6 @@ static bool do_more_prompt(int typed_char)
return retval; return retval;
} }
#if defined(MSWIN)
/// Headless (no UI) error message handler.
static void do_msg(const char *str, bool errmsg)
{
static bool did_err = false;
assert(str != NULL);
wchar_t *utf16str;
int r = utf8_to_utf16(str, -1, &utf16str);
if (r != 0 && !did_err) {
did_err = true;
fprintf(stderr, "utf8_to_utf16 failed: %d", r);
ELOG("utf8_to_utf16 failed: %d", r);
} else if (r == 0) {
if (errmsg) {
fwprintf(stderr, L"%ls", utf16str);
} else {
wprintf(L"%ls", utf16str);
}
xfree(utf16str);
}
}
void os_errmsg(const char *str)
{
do_msg(str, true);
}
/// Headless (no UI) message handler.
void os_msg(const char *str)
{
do_msg(str, false);
}
#endif // MSWIN
void msg_moremsg(int full) void msg_moremsg(int full)
{ {
int attr = hl_combine_attr(HL_ATTR(HLF_MSG), HL_ATTR(HLF_M)); int attr = hl_combine_attr(HL_ATTR(HLF_MSG), HL_ATTR(HLF_M));

View File

@ -65,10 +65,3 @@ EXTERN int msg_listdo_overwrite INIT( = 0);
// Prefer using semsg(), because perror() may send the output to the wrong // Prefer using semsg(), because perror() may send the output to the wrong
// destination and mess up the screen. // destination and mess up the screen.
#define PERROR(msg) (void)semsg("%s: %s", (msg), strerror(errno)) #define PERROR(msg) (void)semsg("%s: %s", (msg), strerror(errno))
#ifndef MSWIN
/// Headless (no UI) error message handler.
# define os_errmsg(str) fprintf(stderr, "%s", (str))
/// Headless (no UI) message handler.
# define os_msg(str) printf("%s", (str))
#endif

View File

@ -1486,10 +1486,10 @@ int do_set(char *arg, int opt_flags)
if (silent_mode && did_show) { if (silent_mode && did_show) {
// After displaying option values in silent mode. // After displaying option values in silent mode.
silent_mode = false; silent_mode = false;
info_message = true; // use os_msg(), not os_errmsg() info_message = true; // use stdout, not stderr
msg_putchar('\n'); msg_putchar('\n');
silent_mode = true; silent_mode = true;
info_message = false; // use os_msg(), not os_errmsg() info_message = false; // use stdout, not stderr
} }
return OK; return OK;
@ -3926,7 +3926,7 @@ static void showoneopt(vimoption_T *opt, int opt_flags)
int save_silent = silent_mode; int save_silent = silent_mode;
silent_mode = false; silent_mode = false;
info_message = true; // use os_msg(), not os_errmsg() info_message = true; // use stdout, not stderr
OptIndex opt_idx = get_opt_idx(opt); OptIndex opt_idx = get_opt_idx(opt);
void *varp = get_varp_scope(opt, opt_flags); void *varp = get_varp_scope(opt, opt_flags);

View File

@ -5865,8 +5865,8 @@ static bool regmatch(uint8_t *scan, const proftime_T *tm, int *timed_out)
#ifdef REGEXP_DEBUG #ifdef REGEXP_DEBUG
if (scan != NULL && regnarrate) { if (scan != NULL && regnarrate) {
os_errmsg((char *)regprop(scan)); fprintf(stderr, "%s", (char *)regprop(scan));
os_errmsg("(\n"); fprintf(stderr, "%s", "(\n");
} }
#endif #endif
@ -5892,18 +5892,18 @@ static bool regmatch(uint8_t *scan, const proftime_T *tm, int *timed_out)
#ifdef REGEXP_DEBUG #ifdef REGEXP_DEBUG
if (regnarrate) { if (regnarrate) {
os_errmsg((char *)regprop(scan)); fprintf(stderr, "%s", (char *)regprop(scan));
os_errmsg("...\n"); fprintf(stderr, "%s", "...\n");
if (re_extmatch_in != NULL) { if (re_extmatch_in != NULL) {
int i; int i;
os_errmsg(_("External submatches:\n")); fprintf(stderr, _("External submatches:\n"));
for (i = 0; i < NSUBEXP; i++) { for (i = 0; i < NSUBEXP; i++) {
os_errmsg(" \""); fprintf(stderr, "%s", " \"");
if (re_extmatch_in->matches[i] != NULL) { if (re_extmatch_in->matches[i] != NULL) {
os_errmsg((char *)re_extmatch_in->matches[i]); fprintf(stderr, "%s", (char *)re_extmatch_in->matches[i]);
} }
os_errmsg("\"\n"); fprintf(stderr, "%s", "\"\n");
} }
} }
} }