main.c: Misc. improvements

- Constify parameters and reduce char_u usage
- Modernize function signatures
- Modernize function comment blocks, rewriting if needed
- Factor out mainerr_arg_missing(); all the other ME_* macros lack
  such a wrapper function.

main_errors[]
  - Remove unneeded parentheses
  - Remove an unused error string and its respective macro

mainerr()
  - Don't print version when called. mainerr() only handles errors
    related to command line arguments, so the version isn't much help at
    all.
  - Changed 'vim' instance to 'nvim'

Misc.
  - Remove duplicate include
  - Replace unneeded 'inttypes.h' include with 'stdint.h'
  - Remove stray comments
  - Remove excessive newlines
This commit is contained in:
Michael Reed 2015-01-23 19:42:21 -05:00
parent 1a40013bec
commit 9381004066

View File

@ -9,7 +9,7 @@
#define EXTERN #define EXTERN
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
@ -59,7 +59,6 @@
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/version.h" #include "nvim/version.h"
#include "nvim/window.h" #include "nvim/window.h"
#include "nvim/os/time.h"
#include "nvim/os/input.h" #include "nvim/os/input.h"
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/os/time.h" #include "nvim/os/time.h"
@ -120,11 +119,8 @@ typedef struct {
# include "main.c.generated.h" # include "main.c.generated.h"
#endif #endif
/* // Error messages
* Different types of error messages. static const char *main_errors[] = {
*/
static char *(main_errors[]) =
{
N_("Unknown option argument"), N_("Unknown option argument"),
#define ME_UNKNOWN_OPTION 0 #define ME_UNKNOWN_OPTION 0
N_("Too many edit arguments"), N_("Too many edit arguments"),
@ -133,12 +129,11 @@ static char *(main_errors[]) =
#define ME_ARG_MISSING 2 #define ME_ARG_MISSING 2
N_("Garbage after option argument"), N_("Garbage after option argument"),
#define ME_GARBAGE 3 #define ME_GARBAGE 3
N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"), N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments")
#define ME_EXTRA_CMD 4 #define ME_EXTRA_CMD 4
N_("Invalid argument for"),
#define ME_INVALID_ARG 5
}; };
/// Performs early initialization. /// Performs early initialization.
/// ///
/// Needed for unit tests. Must be called after `time_init()`. /// Needed for unit tests. Must be called after `time_init()`.
@ -219,12 +214,6 @@ int main(int argc, char **argv)
*/ */
command_line_scan(&params); command_line_scan(&params);
/*
* On some systems, when we compile with the GUI, we always use it. On Mac
* there is no terminal version, and on Windows we can't fork one off with
* :gui.
*/
if (GARGCOUNT > 0) if (GARGCOUNT > 0)
fname = get_fname(&params); fname = get_fname(&params);
@ -242,13 +231,6 @@ int main(int argc, char **argv)
if (recoverymode && fname == NULL) if (recoverymode && fname == NULL)
params.want_full_screen = FALSE; params.want_full_screen = FALSE;
/*
* When certain to start the GUI, don't check capabilities of terminal.
* For GTK we can't be sure, but when started from the desktop it doesn't
* make sense to try using a terminal.
*/
// term_init() sets up the terminal (window) for use. This must be // term_init() sets up the terminal (window) for use. This must be
// done after resetting full_screen, otherwise it may move the cursor // done after resetting full_screen, otherwise it may move the cursor
term_init(); term_init();
@ -383,7 +365,6 @@ int main(int argc, char **argv)
if (!exmode_active) if (!exmode_active)
msg_scroll = FALSE; msg_scroll = FALSE;
/* /*
* If "-" argument given: Read file from stdin. * If "-" argument given: Read file from stdin.
* Do this before starting Raw mode, because it may change things that the * Do this before starting Raw mode, because it may change things that the
@ -510,7 +491,6 @@ int main(int argc, char **argv)
apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
TIME_MSG("VimEnter autocommands"); TIME_MSG("VimEnter autocommands");
/* When a startup script or session file setup for diff'ing and /* When a startup script or session file setup for diff'ing and
* scrollbind, sync the scrollbind now. */ * scrollbind, sync the scrollbind now. */
if (curwin->w_p_diff && curwin->w_p_scb) { if (curwin->w_p_diff && curwin->w_p_scb) {
@ -519,14 +499,11 @@ int main(int argc, char **argv)
TIME_MSG("diff scrollbinding"); TIME_MSG("diff scrollbinding");
} }
/* If ":startinsert" command used, stuff a dummy command to be able to /* If ":startinsert" command used, stuff a dummy command to be able to
* call normal_cmd(), which will then start Insert mode. */ * call normal_cmd(), which will then start Insert mode. */
if (restart_edit != 0) if (restart_edit != 0)
stuffcharReadbuff(K_NOP); stuffcharReadbuff(K_NOP);
TIME_MSG("before starting main loop"); TIME_MSG("before starting main loop");
/* /*
@ -751,8 +728,6 @@ main_loop (
} }
/* Exit properly */ /* Exit properly */
void getout(int exitval) void getout(int exitval)
{ {
@ -772,7 +747,6 @@ void getout(int exitval)
/* Optionally print hashtable efficiency. */ /* Optionally print hashtable efficiency. */
hash_debug_results(); hash_debug_results();
if (get_vim_var_nr(VV_DYING) <= 1) { if (get_vim_var_nr(VV_DYING) <= 1) {
/* Trigger BufWinLeave for all windows, but only once per buffer. */ /* Trigger BufWinLeave for all windows, but only once per buffer. */
for (tp = first_tabpage; tp != NULL; tp = next_tp) { for (tp = first_tabpage; tp != NULL; tp = next_tp) {
@ -836,20 +810,25 @@ void getout(int exitval)
mch_exit(exitval); mch_exit(exitval);
} }
/* /// Gets the integer value of a numeric command line argument if given,
* Get a (optional) count for a Vim argument. /// such as '-o10'.
*/ ///
static int /// @param[in] p pointer to argument
get_number_arg ( /// @param[in, out] idx pointer to index in argument, is incremented
char_u *p, /* pointer to argument */ /// @param[in] def default value
int *idx, /* index in argument, is incremented */ ///
int def /* default value */ /// @return def unmodified if:
) /// - argument isn't given
/// - argument is non-numeric
///
/// @return argument's numeric value otherwise
static int get_number_arg(const char *p, int *idx, int def)
{ {
if (vim_isdigit(p[*idx])) { if (vim_isdigit(p[*idx])) {
def = atoi((char *)&(p[*idx])); def = atoi(&(p[*idx]));
while (vim_isdigit(p[*idx])) while (vim_isdigit(p[*idx])) {
*idx = *idx + 1; *idx = *idx + 1;
}
} }
return def; return def;
} }
@ -867,7 +846,6 @@ static void init_locale(void)
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
# endif # endif
{ {
int mustfree = FALSE; int mustfree = FALSE;
char_u *p; char_u *p;
@ -903,7 +881,6 @@ static void parse_command_name(mparm_T *parmp)
initstr = path_tail((char_u *)parmp->argv[0]); initstr = path_tail((char_u *)parmp->argv[0]);
set_vim_var_string(VV_PROGNAME, initstr, -1); set_vim_var_string(VV_PROGNAME, initstr, -1);
set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1); set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
@ -998,7 +975,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, (char_u *)argv[0]); mainerr(ME_TOO_MANY_ARGS, argv[0]);
parmp->edit_type = EDIT_STDIN; parmp->edit_type = EDIT_STDIN;
read_cmd_fd = 2; /* read from stderr instead of stdin */ read_cmd_fd = 2; /* read from stderr instead of stdin */
} }
@ -1049,7 +1026,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, (char_u *)argv[0]); mainerr(ME_UNKNOWN_OPTION, argv[0]);
had_minmin = TRUE; had_minmin = TRUE;
} }
if (!want_argument) if (!want_argument)
@ -1130,28 +1107,25 @@ static void command_line_scan(mparm_T *parmp)
} }
#endif #endif
/* default is 0: open window for each file */ /* default is 0: open window for each file */
parmp->window_count = get_number_arg((char_u *)argv[0], parmp->window_count = get_number_arg(argv[0], &argv_idx, 0);
&argv_idx, 0);
parmp->window_layout = WIN_TABS; parmp->window_layout = WIN_TABS;
break; break;
case 'o': /* "-o[N]" open N horizontal split windows */ case 'o': /* "-o[N]" open N horizontal split windows */
/* default is 0: open window for each file */ /* default is 0: open window for each file */
parmp->window_count = get_number_arg((char_u *)argv[0], parmp->window_count = get_number_arg(argv[0], &argv_idx, 0);
&argv_idx, 0);
parmp->window_layout = WIN_HOR; parmp->window_layout = WIN_HOR;
break; break;
case 'O': /* "-O[N]" open N vertical split windows */ case 'O': /* "-O[N]" open N vertical split windows */
/* default is 0: open window for each file */ /* default is 0: open window for each file */
parmp->window_count = get_number_arg((char_u *)argv[0], parmp->window_count = get_number_arg(argv[0], &argv_idx, 0);
&argv_idx, 0);
parmp->window_layout = WIN_VER; parmp->window_layout = WIN_VER;
break; break;
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, (char_u *)argv[0]); mainerr(ME_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;
@ -1180,7 +1154,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, (char_u *)argv[0]); mainerr(ME_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;
@ -1197,7 +1171,7 @@ static void command_line_scan(mparm_T *parmp)
break; break;
case 'V': /* "-V{N}" Verbose level */ case 'V': /* "-V{N}" Verbose level */
/* default is 10: a little bit verbose */ /* default is 10: a little bit verbose */
p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10); p_verbose = get_number_arg(argv[0], &argv_idx, 10);
if (argv[0][argv_idx] != NUL) { if (argv[0][argv_idx] != NUL) {
set_option_value((char_u *)"verbosefile", 0L, set_option_value((char_u *)"verbosefile", 0L,
(char_u *)argv[0] + argv_idx, 0); (char_u *)argv[0] + argv_idx, 0);
@ -1212,7 +1186,7 @@ static void command_line_scan(mparm_T *parmp)
case 'w': /* "-w{number}" set window height */ case 'w': /* "-w{number}" set window height */
/* "-w {scriptout}" write to script */ /* "-w {scriptout}" write to script */
if (vim_isdigit(((char_u *)argv[0])[argv_idx])) { if (vim_isdigit(((char_u *)argv[0])[argv_idx])) {
n = get_number_arg((char_u *)argv[0], &argv_idx, 10); n = get_number_arg(argv[0], &argv_idx, 10);
set_option_value((char_u *)"window", n, NULL, 0); set_option_value((char_u *)"window", n, NULL, 0);
break; break;
} }
@ -1247,7 +1221,7 @@ static void command_line_scan(mparm_T *parmp)
break; break;
default: default:
mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]); mainerr(ME_UNKNOWN_OPTION, argv[0]);
} }
/* /*
@ -1258,11 +1232,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, (char_u *)argv[0]); mainerr(ME_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_arg_missing((char_u *)argv[0]); mainerr(ME_ARG_MISSING, argv[0]);
++argv; ++argv;
argv_idx = -1; argv_idx = -1;
@ -1357,7 +1331,7 @@ scripterror:
/* "-w {scriptout}" append to script file */ /* "-w {scriptout}" append to script file */
if (vim_isdigit(*((char_u *)argv[0]))) { if (vim_isdigit(*((char_u *)argv[0]))) {
argv_idx = 0; argv_idx = 0;
n = get_number_arg((char_u *)argv[0], &argv_idx, 10); n = get_number_arg(argv[0], &argv_idx, 10);
set_option_value((char_u *)"window", n, NULL, 0); set_option_value((char_u *)"window", n, NULL, 0);
argv_idx = -1; argv_idx = -1;
break; break;
@ -1386,10 +1360,9 @@ 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, (char_u *)argv[0]); mainerr(ME_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. */
ga_grow(&global_alist.al_ga, 1); ga_grow(&global_alist.al_ga, 1);
p = vim_strsave((char_u *)argv[0]); p = vim_strsave((char_u *)argv[0]);
@ -2046,13 +2019,11 @@ process_env (
return FAIL; return FAIL;
} }
#if defined(UNIX) #ifdef UNIX
/* /// Checks if user owns file.
* Return TRUE if we are certain the user owns the file "fname". /// Use both uv_fs_stat() and uv_fs_lstat() through os_fileinfo() and
* Used for ".vimrc" and ".exrc". /// os_fileinfo_link() respectively for extra security.
* Use both stat() and lstat() for extra security. static bool file_owned(const char *fname)
*/
static int file_owned(char *fname)
{ {
uid_t uid = getuid(); uid_t uid = getuid();
FileInfo file_info; FileInfo file_info;
@ -2064,34 +2035,27 @@ static int file_owned(char *fname)
} }
#endif #endif
/* /// Prints the following then exits:
* Give an error message main_errors["n"] and exit. /// - An error message main_errors[n]
*/ /// - A string str if not null
static void ///
mainerr ( /// @param n error number represented by an ME_* macro
int n, /* one of the ME_ defines */ /// @param str string to append to the primary error message, or NULL
char_u *str /* extra argument or NULL */ static void mainerr(int n, 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(longVersion);
mch_errmsg("\n");
mch_errmsg(_(main_errors[n])); mch_errmsg(_(main_errors[n]));
if (str != NULL) { if (str != NULL) {
mch_errmsg(": \""); mch_errmsg(": \"");
mch_errmsg((char *)str); mch_errmsg(str);
mch_errmsg("\""); mch_errmsg("\"");
} }
mch_errmsg(_("\nMore info with: \"vim -h\"\n")); mch_errmsg(_("\nMore info with \"nvim -h\"\n"));
mch_exit(1); mch_exit(1);
} }
void mainerr_arg_missing(char_u *str)
{
mainerr(ME_ARG_MISSING, str);
}
/* /*
* print a message with three spaces prepended and '\n' appended. * print a message with three spaces prepended and '\n' appended.
@ -2177,7 +2141,6 @@ static void usage(void)
main_msg(_("-h or --help\tPrint Help (this message) and exit")); main_msg(_("-h or --help\tPrint Help (this message) and exit"));
main_msg(_("--version\t\tPrint version information and exit")); main_msg(_("--version\t\tPrint version information and exit"));
mch_exit(0); mch_exit(0);
} }
@ -2192,5 +2155,3 @@ static void check_swap_exists_action(void)
getout(1); getout(1);
handle_swap_exists(NULL); handle_swap_exists(NULL);
} }