From 53774af5e94f437135e4664949d65ccf3be030e0 Mon Sep 17 00:00:00 2001 From: Michael Reed Date: Sat, 30 May 2015 01:06:19 -0400 Subject: main.c: Simplify error string management This removes the need for preprocessor defines as array indices, and brings error handling more in line with other files, which for the most most part to use constant strings (also, see `globals.h`). Helped-By: Nicolas Hillegeer --- src/nvim/main.c | 54 ++++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index ddc39caa6f..ef9390aaf8 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -123,18 +123,12 @@ typedef struct { #endif // Error messages -static const char *main_errors[] = { - N_("Unknown option argument"), -#define ME_UNKNOWN_OPTION 0 - N_("Too many edit arguments"), -#define ME_TOO_MANY_ARGS 1 - N_("Argument missing after"), -#define ME_ARG_MISSING 2 - N_("Garbage after option argument"), -#define ME_GARBAGE 3 - N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments") -#define ME_EXTRA_CMD 4 -}; +static const char *err_arg_missing = N_("Argument missing after"); +static const char *err_opt_garbage = N_("Garbage after option argument"); +static const char *err_opt_unknown = N_("Unknown option argument"); +static const char *err_too_many_args = N_("Too many edit arguments"); +static const char *err_extra_cmd = + N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"); /// Performs early initialization. @@ -865,7 +859,7 @@ static void command_line_scan(mparm_T *parmp) */ if (argv[0][0] == '+' && !had_minmin) { if (parmp->n_commands >= MAX_ARG_CMDS) - mainerr(ME_EXTRA_CMD, NULL); + mainerr(err_extra_cmd, NULL); argv_idx = -1; /* skip to next argument */ if (argv[0][1] == NUL) parmp->commands[parmp->n_commands++] = "$"; @@ -885,7 +879,7 @@ static void command_line_scan(mparm_T *parmp) silent_mode = TRUE; } else { if (parmp->edit_type != EDIT_NONE) { - mainerr(ME_TOO_MANY_ARGS, argv[0]); + mainerr(err_too_many_args, argv[0]); } parmp->edit_type = EDIT_STDIN; } @@ -934,7 +928,7 @@ static void command_line_scan(mparm_T *parmp) argv_idx += 11; } else { if (argv[0][argv_idx]) - mainerr(ME_UNKNOWN_OPTION, argv[0]); + mainerr(err_opt_unknown, argv[0]); had_minmin = TRUE; } if (!want_argument) @@ -1033,7 +1027,7 @@ static void command_line_scan(mparm_T *parmp) case 'q': /* "-q" QuickFix mode */ if (parmp->edit_type != EDIT_NONE) - mainerr(ME_TOO_MANY_ARGS, argv[0]); + mainerr(err_too_many_args, argv[0]); parmp->edit_type = EDIT_QF; if (argv[0][argv_idx]) { /* "-q{errorfile}" */ parmp->use_ef = (char_u *)argv[0] + argv_idx; @@ -1062,7 +1056,7 @@ static void command_line_scan(mparm_T *parmp) case 't': /* "-t {tag}" or "-t{tag}" jump to tag */ if (parmp->edit_type != EDIT_NONE) - mainerr(ME_TOO_MANY_ARGS, argv[0]); + mainerr(err_too_many_args, argv[0]); parmp->edit_type = EDIT_TAG; if (argv[0][argv_idx]) { /* "-t{tag}" */ parmp->tagname = (char_u *)argv[0] + argv_idx; @@ -1111,7 +1105,7 @@ static void command_line_scan(mparm_T *parmp) command */ if (argv[0][argv_idx] != NUL) { if (parmp->n_commands >= MAX_ARG_CMDS) - mainerr(ME_EXTRA_CMD, NULL); + mainerr(err_extra_cmd, NULL); parmp->commands[parmp->n_commands++] = argv[0] + argv_idx; argv_idx = -1; @@ -1127,7 +1121,7 @@ static void command_line_scan(mparm_T *parmp) break; default: - mainerr(ME_UNKNOWN_OPTION, argv[0]); + mainerr(err_opt_unknown, argv[0]); } /* @@ -1138,11 +1132,11 @@ static void command_line_scan(mparm_T *parmp) * Check for garbage immediately after the option letter. */ if (argv[0][argv_idx] != NUL) - mainerr(ME_GARBAGE, argv[0]); + mainerr(err_opt_garbage, argv[0]); --argc; if (argc < 1 && c != 'S') /* -S has an optional argument */ - mainerr(ME_ARG_MISSING, argv[0]); + mainerr(err_arg_missing, argv[0]); ++argv; argv_idx = -1; @@ -1150,7 +1144,7 @@ static void command_line_scan(mparm_T *parmp) case 'c': /* "-c {command}" execute command */ case 'S': /* "-S {file}" execute Vim script */ if (parmp->n_commands >= MAX_ARG_CMDS) - mainerr(ME_EXTRA_CMD, NULL); + mainerr(err_extra_cmd, NULL); if (c == 'S') { char *a; @@ -1180,7 +1174,7 @@ static void command_line_scan(mparm_T *parmp) if (argv[-1][2] == 'c') { /* "--cmd {command}" execute command */ if (parmp->n_pre_commands >= MAX_ARG_CMDS) - mainerr(ME_EXTRA_CMD, NULL); + mainerr(err_extra_cmd, NULL); parmp->pre_commands[parmp->n_pre_commands++] = argv[0]; } /* "--startuptime " already handled */ @@ -1257,7 +1251,7 @@ scripterror: /* Check for only one type of editing. */ if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE) - mainerr(ME_TOO_MANY_ARGS, argv[0]); + mainerr(err_too_many_args, argv[0]); parmp->edit_type = EDIT_FILE; /* Add the file to the global argument list. */ @@ -1926,16 +1920,16 @@ static bool file_owned(const char *fname) #endif /// Prints the following then exits: -/// - An error message main_errors[n] -/// - A string str if not null +/// - An error message `errstr` +/// - A string `str` if not null /// -/// @param n error number represented by an ME_* macro -/// @param str string to append to the primary error message, or NULL -static void mainerr(int n, const char *str) +/// @param errstr string containing an error message +/// @param str string to append to the primary error message, or NULL +static void mainerr(const char *errstr, const char *str) { signal_stop(); // kill us with CTRL-C here, if you like - mch_errmsg(_(main_errors[n])); + mch_errmsg(_(errstr)); if (str != NULL) { mch_errmsg(": \""); mch_errmsg(str); -- cgit From 7c2afbd9a69c5aa2d148251f1a7e5773fb0f2ebb Mon Sep 17 00:00:00 2001 From: Michael Reed Date: Sat, 30 May 2015 01:06:19 -0400 Subject: main.c:mainerr(): Namespace messages Error messages in general should be namespaced, especially in the context of a shell. Given the possibility of a backgrounded job printing messages to standard output/error, namespacing these messages should avoid any confusion as to where the message came from. Helped-by: Scott Prager Helped-by: oni-link --- src/nvim/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index ef9390aaf8..d917d4a9b8 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -122,6 +122,8 @@ typedef struct { # include "main.c.generated.h" #endif +static char *argv0; + // Error messages static const char *err_arg_missing = N_("Argument missing after"); static const char *err_opt_garbage = N_("Garbage after option argument"); @@ -180,6 +182,8 @@ int nvim_main(int argc, char **argv) int main(int argc, char **argv) #endif { + argv0 = (char *)path_tail((char_u *)argv[0]); + char_u *fname = NULL; /* file name from command line */ mparm_T params; /* various parameters passed between * main() and other functions. */ @@ -1929,13 +1933,17 @@ static void mainerr(const char *errstr, const char *str) { signal_stop(); // kill us with CTRL-C here, if you like + mch_errmsg(argv0); + mch_errmsg(": "); mch_errmsg(_(errstr)); if (str != NULL) { mch_errmsg(": \""); mch_errmsg(str); mch_errmsg("\""); } - mch_errmsg(_("\nMore info with \"nvim -h\"\n")); + mch_errmsg(_("\nMore info with \"")); + mch_errmsg(argv0); + mch_errmsg(" -h\"\n"); mch_exit(1); } -- cgit From b113754de8815fd59e1b42f8eae239b0a31c43ee Mon Sep 17 00:00:00 2001 From: Michael Reed Date: Sat, 30 May 2015 01:06:19 -0400 Subject: Passing by: Remove X_LOCALE refs We don't link to any X11 libs, so this is most likely unneeded. --- src/nvim/ex_cmds2.c | 2 +- src/nvim/main.c | 4 ++-- src/nvim/mbyte.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 5c1f487d5e..e7029b8762 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -2987,7 +2987,7 @@ void ex_checktime(exarg_T *eap) no_check_timestamps = save_no_check_timestamps; } -#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +#if defined(HAVE_LOCALE_H) # define HAVE_GET_LOCALE_VAL static char *get_locale_val(int what) diff --git a/src/nvim/main.c b/src/nvim/main.c index d917d4a9b8..e1bb2d0b66 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -146,7 +146,7 @@ void early_init(void) // Init the table of Normal mode commands. init_normal_cmds(); -#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +#if defined(HAVE_LOCALE_H) // Setup to use the current locale (for ctype() and many other things). // NOTE: Translated messages with encodings other than latin1 will not // work until set_init_1() has been called! @@ -809,7 +809,7 @@ static int get_number_arg(const char *p, int *idx, int def) return def; } -#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +#if defined(HAVE_LOCALE_H) /* * Setup to use the current locale (for ctype() and many other things). */ diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 368ae8e773..c8ebcefe0e 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -3406,7 +3406,7 @@ char_u * enc_locale(void) if (!(s = nl_langinfo(CODESET)) || *s == NUL) # endif { -# if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +# if defined(HAVE_LOCALE_H) if (!(s = setlocale(LC_CTYPE, NULL)) || *s == NUL) # endif { -- cgit