Merge pull request #2767 from Pyrohh/mainerr-simplify

[RDY] main.c: Some refactoring / cleanups

Reviewed-by: Scott Prager <splinterofchaos@gmail.com>
Reviewed-by: oni-link <knil.ino@gmail.com>
This commit is contained in:
Michael Reed 2015-05-30 18:14:48 -04:00
commit 8c84c124ed
3 changed files with 37 additions and 35 deletions

View File

@ -2987,7 +2987,7 @@ void ex_checktime(exarg_T *eap)
no_check_timestamps = save_no_check_timestamps; 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 # define HAVE_GET_LOCALE_VAL
static char *get_locale_val(int what) static char *get_locale_val(int what)

View File

@ -122,19 +122,15 @@ typedef struct {
# include "main.c.generated.h" # include "main.c.generated.h"
#endif #endif
static char *argv0;
// Error messages // Error messages
static const char *main_errors[] = { static const char *err_arg_missing = N_("Argument missing after");
N_("Unknown option argument"), static const char *err_opt_garbage = N_("Garbage after option argument");
#define ME_UNKNOWN_OPTION 0 static const char *err_opt_unknown = N_("Unknown option argument");
N_("Too many edit arguments"), static const char *err_too_many_args = N_("Too many edit arguments");
#define ME_TOO_MANY_ARGS 1 static const char *err_extra_cmd =
N_("Argument missing after"), N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments");
#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
};
/// Performs early initialization. /// Performs early initialization.
@ -150,7 +146,7 @@ void early_init(void)
// Init the table of Normal mode commands. // Init the table of Normal mode commands.
init_normal_cmds(); 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). // Setup to use the current locale (for ctype() and many other things).
// NOTE: Translated messages with encodings other than latin1 will not // NOTE: Translated messages with encodings other than latin1 will not
// work until set_init_1() has been called! // work until set_init_1() has been called!
@ -186,6 +182,8 @@ int nvim_main(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
#endif #endif
{ {
argv0 = (char *)path_tail((char_u *)argv[0]);
char_u *fname = NULL; /* file name from command line */ char_u *fname = NULL; /* file name from command line */
mparm_T params; /* various parameters passed between mparm_T params; /* various parameters passed between
* main() and other functions. */ * main() and other functions. */
@ -811,7 +809,7 @@ static int get_number_arg(const char *p, int *idx, int def)
return 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). * Setup to use the current locale (for ctype() and many other things).
*/ */
@ -865,7 +863,7 @@ static void command_line_scan(mparm_T *parmp)
*/ */
if (argv[0][0] == '+' && !had_minmin) { if (argv[0][0] == '+' && !had_minmin) {
if (parmp->n_commands >= MAX_ARG_CMDS) if (parmp->n_commands >= MAX_ARG_CMDS)
mainerr(ME_EXTRA_CMD, NULL); mainerr(err_extra_cmd, NULL);
argv_idx = -1; /* skip to next argument */ argv_idx = -1; /* skip to next argument */
if (argv[0][1] == NUL) if (argv[0][1] == NUL)
parmp->commands[parmp->n_commands++] = "$"; parmp->commands[parmp->n_commands++] = "$";
@ -885,7 +883,7 @@ static void command_line_scan(mparm_T *parmp)
silent_mode = TRUE; silent_mode = TRUE;
} else { } else {
if (parmp->edit_type != EDIT_NONE) { 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; parmp->edit_type = EDIT_STDIN;
} }
@ -934,7 +932,7 @@ static void command_line_scan(mparm_T *parmp)
argv_idx += 11; argv_idx += 11;
} else { } else {
if (argv[0][argv_idx]) if (argv[0][argv_idx])
mainerr(ME_UNKNOWN_OPTION, argv[0]); mainerr(err_opt_unknown, argv[0]);
had_minmin = TRUE; had_minmin = TRUE;
} }
if (!want_argument) if (!want_argument)
@ -1033,7 +1031,7 @@ static void command_line_scan(mparm_T *parmp)
case 'q': /* "-q" QuickFix mode */ case 'q': /* "-q" QuickFix mode */
if (parmp->edit_type != EDIT_NONE) 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; parmp->edit_type = EDIT_QF;
if (argv[0][argv_idx]) { /* "-q{errorfile}" */ if (argv[0][argv_idx]) { /* "-q{errorfile}" */
parmp->use_ef = (char_u *)argv[0] + argv_idx; parmp->use_ef = (char_u *)argv[0] + argv_idx;
@ -1062,7 +1060,7 @@ static void command_line_scan(mparm_T *parmp)
case 't': /* "-t {tag}" or "-t{tag}" jump to tag */ case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
if (parmp->edit_type != EDIT_NONE) 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; parmp->edit_type = EDIT_TAG;
if (argv[0][argv_idx]) { /* "-t{tag}" */ if (argv[0][argv_idx]) { /* "-t{tag}" */
parmp->tagname = (char_u *)argv[0] + argv_idx; parmp->tagname = (char_u *)argv[0] + argv_idx;
@ -1111,7 +1109,7 @@ static void command_line_scan(mparm_T *parmp)
command */ command */
if (argv[0][argv_idx] != NUL) { if (argv[0][argv_idx] != NUL) {
if (parmp->n_commands >= MAX_ARG_CMDS) if (parmp->n_commands >= MAX_ARG_CMDS)
mainerr(ME_EXTRA_CMD, NULL); mainerr(err_extra_cmd, NULL);
parmp->commands[parmp->n_commands++] = argv[0] parmp->commands[parmp->n_commands++] = argv[0]
+ argv_idx; + argv_idx;
argv_idx = -1; argv_idx = -1;
@ -1127,7 +1125,7 @@ static void command_line_scan(mparm_T *parmp)
break; break;
default: default:
mainerr(ME_UNKNOWN_OPTION, argv[0]); mainerr(err_opt_unknown, argv[0]);
} }
/* /*
@ -1138,11 +1136,11 @@ static void command_line_scan(mparm_T *parmp)
* Check for garbage immediately after the option letter. * Check for garbage immediately after the option letter.
*/ */
if (argv[0][argv_idx] != NUL) if (argv[0][argv_idx] != NUL)
mainerr(ME_GARBAGE, argv[0]); mainerr(err_opt_garbage, argv[0]);
--argc; --argc;
if (argc < 1 && c != 'S') /* -S has an optional argument */ if (argc < 1 && c != 'S') /* -S has an optional argument */
mainerr(ME_ARG_MISSING, argv[0]); mainerr(err_arg_missing, argv[0]);
++argv; ++argv;
argv_idx = -1; argv_idx = -1;
@ -1150,7 +1148,7 @@ static void command_line_scan(mparm_T *parmp)
case 'c': /* "-c {command}" execute command */ case 'c': /* "-c {command}" execute command */
case 'S': /* "-S {file}" execute Vim script */ case 'S': /* "-S {file}" execute Vim script */
if (parmp->n_commands >= MAX_ARG_CMDS) if (parmp->n_commands >= MAX_ARG_CMDS)
mainerr(ME_EXTRA_CMD, NULL); mainerr(err_extra_cmd, NULL);
if (c == 'S') { if (c == 'S') {
char *a; char *a;
@ -1180,7 +1178,7 @@ static void command_line_scan(mparm_T *parmp)
if (argv[-1][2] == 'c') { if (argv[-1][2] == 'c') {
/* "--cmd {command}" execute command */ /* "--cmd {command}" execute command */
if (parmp->n_pre_commands >= MAX_ARG_CMDS) 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]; parmp->pre_commands[parmp->n_pre_commands++] = argv[0];
} }
/* "--startuptime <file>" already handled */ /* "--startuptime <file>" already handled */
@ -1257,7 +1255,7 @@ scripterror:
/* Check for only one type of editing. */ /* Check for only one type of editing. */
if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE) 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; parmp->edit_type = EDIT_FILE;
/* Add the file to the global argument list. */ /* Add the file to the global argument list. */
@ -1926,22 +1924,26 @@ static bool file_owned(const char *fname)
#endif #endif
/// Prints the following then exits: /// Prints the following then exits:
/// - An error message main_errors[n] /// - An error message `errstr`
/// - A string str if not null /// - A string `str` if not null
/// ///
/// @param n error number represented by an ME_* macro /// @param errstr string containing an error message
/// @param str string to append to the primary error message, or NULL /// @param str string to append to the primary error message, or NULL
static void mainerr(int n, const char *str) static void 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
mch_errmsg(_(main_errors[n])); mch_errmsg(argv0);
mch_errmsg(": ");
mch_errmsg(_(errstr));
if (str != NULL) { if (str != NULL) {
mch_errmsg(": \""); mch_errmsg(": \"");
mch_errmsg(str); mch_errmsg(str);
mch_errmsg("\""); 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); mch_exit(1);
} }

View File

@ -3406,7 +3406,7 @@ char_u * enc_locale(void)
if (!(s = nl_langinfo(CODESET)) || *s == NUL) if (!(s = nl_langinfo(CODESET)) || *s == NUL)
# endif # endif
{ {
# if defined(HAVE_LOCALE_H) || defined(X_LOCALE) # if defined(HAVE_LOCALE_H)
if (!(s = setlocale(LC_CTYPE, NULL)) || *s == NUL) if (!(s = setlocale(LC_CTYPE, NULL)) || *s == NUL)
# endif # endif
{ {