xref: /vim-8.2.3635/src/main.c (revision 00a927d6)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
11 # include "vimio.h"		/* for close() and dup() */
12 #endif
13 
14 #define EXTERN
15 #include "vim.h"
16 
17 #ifdef SPAWNO
18 # include <spawno.h>		/* special MS-DOS swapping library */
19 #endif
20 
21 #ifdef __CYGWIN__
22 # ifndef WIN32
23 #  include <cygwin/version.h>
24 #  include <sys/cygwin.h>	/* for cygwin_conv_to_posix_path() and/or
25 				 * cygwin_conv_path() */
26 # endif
27 # include <limits.h>
28 #endif
29 
30 /* Maximum number of commands from + or -c arguments. */
31 #define MAX_ARG_CMDS 10
32 
33 /* values for "window_layout" */
34 #define WIN_HOR	    1	    /* "-o" horizontally split windows */
35 #define	WIN_VER	    2	    /* "-O" vertically split windows */
36 #define	WIN_TABS    3	    /* "-p" windows on tab pages */
37 
38 /* Struct for various parameters passed between main() and other functions. */
39 typedef struct
40 {
41     int		argc;
42     char	**argv;
43 
44     int		evim_mode;		/* started as "evim" */
45     char_u	*use_vimrc;		/* vimrc from -u argument */
46 
47     int		n_commands;		     /* no. of commands from + or -c */
48     char_u	*commands[MAX_ARG_CMDS];     /* commands from + or -c arg. */
49     char_u	cmds_tofree[MAX_ARG_CMDS];   /* commands that need free() */
50     int		n_pre_commands;		     /* no. of commands from --cmd */
51     char_u	*pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
52 
53     int		edit_type;		/* type of editing to do */
54     char_u	*tagname;		/* tag from -t argument */
55 #ifdef FEAT_QUICKFIX
56     char_u	*use_ef;		/* 'errorfile' from -q argument */
57 #endif
58 
59     int		want_full_screen;
60     int		stdout_isatty;		/* is stdout a terminal? */
61     char_u	*term;			/* specified terminal name */
62 #ifdef FEAT_CRYPT
63     int		ask_for_key;		/* -x argument */
64 #endif
65     int		no_swap_file;		/* "-n" argument used */
66 #ifdef FEAT_EVAL
67     int		use_debug_break_level;
68 #endif
69 #ifdef FEAT_WINDOWS
70     int		window_count;		/* number of windows to use */
71     int		window_layout;		/* 0, WIN_HOR, WIN_VER or WIN_TABS */
72 #endif
73 
74 #ifdef FEAT_CLIENTSERVER
75     int		serverArg;		/* TRUE when argument for a server */
76     char_u	*serverName_arg;	/* cmdline arg for server name */
77     char_u	*serverStr;		/* remote server command */
78     char_u	*serverStrEnc;		/* encoding of serverStr */
79     char_u	*servername;		/* allocated name for our server */
80 #endif
81 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
82     int		literal;		/* don't expand file names */
83 #endif
84 #ifdef MSWIN
85     int		full_path;		/* file name argument was full path */
86 #endif
87 #ifdef FEAT_DIFF
88     int		diff_mode;		/* start with 'diff' set */
89 #endif
90 } mparm_T;
91 
92 /* Values for edit_type. */
93 #define EDIT_NONE   0	    /* no edit type yet */
94 #define EDIT_FILE   1	    /* file name argument[s] given, use argument list */
95 #define EDIT_STDIN  2	    /* read file from stdin */
96 #define EDIT_TAG    3	    /* tag name argument given, use tagname */
97 #define EDIT_QF	    4	    /* start in quickfix mode */
98 
99 #if defined(UNIX) || defined(VMS)
100 static int file_owned __ARGS((char *fname));
101 #endif
102 static void mainerr __ARGS((int, char_u *));
103 static void main_msg __ARGS((char *s));
104 static void usage __ARGS((void));
105 static int get_number_arg __ARGS((char_u *p, int *idx, int def));
106 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
107 static void init_locale __ARGS((void));
108 #endif
109 static void parse_command_name __ARGS((mparm_T *parmp));
110 static void early_arg_scan __ARGS((mparm_T *parmp));
111 static void command_line_scan __ARGS((mparm_T *parmp));
112 static void check_tty __ARGS((mparm_T *parmp));
113 static void read_stdin __ARGS((void));
114 static void create_windows __ARGS((mparm_T *parmp));
115 #ifdef FEAT_WINDOWS
116 static void edit_buffers __ARGS((mparm_T *parmp));
117 #endif
118 static void exe_pre_commands __ARGS((mparm_T *parmp));
119 static void exe_commands __ARGS((mparm_T *parmp));
120 static void source_startup_scripts __ARGS((mparm_T *parmp));
121 static void main_start_gui __ARGS((void));
122 #if defined(HAS_SWAP_EXISTS_ACTION)
123 static void check_swap_exists_action __ARGS((void));
124 #endif
125 #ifdef FEAT_CLIENTSERVER
126 static void exec_on_server __ARGS((mparm_T *parmp));
127 static void prepare_server __ARGS((mparm_T *parmp));
128 static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
129 static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
130 #endif
131 
132 
133 /*
134  * Different types of error messages.
135  */
136 static char *(main_errors[]) =
137 {
138     N_("Unknown option argument"),
139 #define ME_UNKNOWN_OPTION	0
140     N_("Too many edit arguments"),
141 #define ME_TOO_MANY_ARGS	1
142     N_("Argument missing after"),
143 #define ME_ARG_MISSING		2
144     N_("Garbage after option argument"),
145 #define ME_GARBAGE		3
146     N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
147 #define ME_EXTRA_CMD		4
148     N_("Invalid argument for"),
149 #define ME_INVALID_ARG		5
150 };
151 
152 #ifndef PROTO	    /* don't want a prototype for main() */
153     int
154 # ifdef VIMDLL
155 _export
156 # endif
157 # ifdef FEAT_GUI_MSWIN
158 #  ifdef __BORLANDC__
159 _cdecl
160 #  endif
161 VimMain
162 # else
163 main
164 # endif
165 (argc, argv)
166     int		argc;
167     char	**argv;
168 {
169     char_u	*fname = NULL;		/* file name from command line */
170     mparm_T	params;			/* various parameters passed between
171 					 * main() and other functions. */
172 #ifdef STARTUPTIME
173     int		i;
174 #endif
175 
176     /*
177      * Do any system-specific initialisations.  These can NOT use IObuff or
178      * NameBuff.  Thus emsg2() cannot be called!
179      */
180     mch_early_init();
181 
182     /* Many variables are in "params" so that we can pass them to invoked
183      * functions without a lot of arguments.  "argc" and "argv" are also
184      * copied, so that they can be changed. */
185     vim_memset(&params, 0, sizeof(params));
186     params.argc = argc;
187     params.argv = argv;
188     params.want_full_screen = TRUE;
189 #ifdef FEAT_EVAL
190     params.use_debug_break_level = -1;
191 #endif
192 #ifdef FEAT_WINDOWS
193     params.window_count = -1;
194 #endif
195 
196 #ifdef FEAT_TCL
197     vim_tcl_init(params.argv[0]);
198 #endif
199 
200 #ifdef MEM_PROFILE
201     atexit(vim_mem_profile_dump);
202 #endif
203 
204 #ifdef STARTUPTIME
205     for (i = 1; i < argc; ++i)
206     {
207 	if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
208 	{
209 	    time_fd = mch_fopen(argv[i + 1], "a");
210 	    TIME_MSG("--- VIM STARTING ---");
211 	    break;
212 	}
213     }
214 #endif
215     starttime = time(NULL);
216 
217 #ifdef __EMX__
218     _wildcard(&params.argc, &params.argv);
219 #endif
220 
221 #ifdef FEAT_MBYTE
222     (void)mb_init();	/* init mb_bytelen_tab[] to ones */
223 #endif
224 #ifdef FEAT_EVAL
225     eval_init();	/* init global variables */
226 #endif
227 
228 #ifdef __QNXNTO__
229     qnx_init();		/* PhAttach() for clipboard, (and gui) */
230 #endif
231 
232 #ifdef MAC_OS_CLASSIC
233     /* Prepare for possibly starting GUI sometime */
234     /* Macintosh needs this before any memory is allocated. */
235     gui_prepare(&params.argc, params.argv);
236     TIME_MSG("GUI prepared");
237 #endif
238 
239     /* Init the table of Normal mode commands. */
240     init_normal_cmds();
241 
242 #if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
243     make_version();	/* Construct the long version string. */
244 #endif
245 
246     /*
247      * Allocate space for the generic buffers (needed for set_init_1() and
248      * EMSG2()).
249      */
250     if ((IObuff = alloc(IOSIZE)) == NULL
251 	    || (NameBuff = alloc(MAXPATHL)) == NULL)
252 	mch_exit(0);
253     TIME_MSG("Allocated generic buffers");
254 
255 #ifdef NBDEBUG
256     /* Wait a moment for debugging NetBeans.  Must be after allocating
257      * NameBuff. */
258     nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
259     nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
260     TIME_MSG("NetBeans debug wait");
261 #endif
262 
263 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
264     /*
265      * Setup to use the current locale (for ctype() and many other things).
266      * NOTE: Translated messages with encodings other than latin1 will not
267      * work until set_init_1() has been called!
268      */
269     init_locale();
270     TIME_MSG("locale set");
271 #endif
272 
273 #ifdef FEAT_GUI
274     gui.dofork = TRUE;		    /* default is to use fork() */
275 #endif
276 
277     /*
278      * Do a first scan of the arguments in "argv[]":
279      *   -display or --display
280      *   --server...
281      *   --socketid
282      *   --windowid
283      */
284     early_arg_scan(&params);
285 
286 #ifdef FEAT_SUN_WORKSHOP
287     findYourself(params.argv[0]);
288 #endif
289 #if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
290     /* Prepare for possibly starting GUI sometime */
291     gui_prepare(&params.argc, params.argv);
292     TIME_MSG("GUI prepared");
293 #endif
294 
295 #ifdef FEAT_CLIPBOARD
296     clip_init(FALSE);		/* Initialise clipboard stuff */
297     TIME_MSG("clipboard setup");
298 #endif
299 
300     /*
301      * Check if we have an interactive window.
302      * On the Amiga: If there is no window, we open one with a newcli command
303      * (needed for :! to * work). mch_check_win() will also handle the -d or
304      * -dev argument.
305      */
306     params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
307     TIME_MSG("window checked");
308 
309     /*
310      * Allocate the first window and buffer.
311      * Can't do anything without it, exit when it fails.
312      */
313     if (win_alloc_first() == FAIL)
314 	mch_exit(0);
315 
316     init_yank();		/* init yank buffers */
317 
318     alist_init(&global_alist);	/* Init the argument list to empty. */
319 
320     /*
321      * Set the default values for the options.
322      * NOTE: Non-latin1 translated messages are working only after this,
323      * because this is where "has_mbyte" will be set, which is used by
324      * msg_outtrans_len_attr().
325      * First find out the home directory, needed to expand "~" in options.
326      */
327     init_homedir();		/* find real value of $HOME */
328     set_init_1();
329     TIME_MSG("inits 1");
330 
331 #ifdef FEAT_EVAL
332     set_lang_var();		/* set v:lang and v:ctype */
333 #endif
334 
335 #ifdef FEAT_CLIENTSERVER
336     /*
337      * Do the client-server stuff, unless "--servername ''" was used.
338      * This may exit Vim if the command was sent to the server.
339      */
340     exec_on_server(&params);
341 #endif
342 
343     /*
344      * Figure out the way to work from the command name argv[0].
345      * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
346      */
347     parse_command_name(&params);
348 
349     /*
350      * Process the command line arguments.  File names are put in the global
351      * argument list "global_alist".
352      */
353     command_line_scan(&params);
354     TIME_MSG("parsing arguments");
355 
356     /*
357      * On some systems, when we compile with the GUI, we always use it.  On Mac
358      * there is no terminal version, and on Windows we can't fork one off with
359      * :gui.
360      */
361 #ifdef ALWAYS_USE_GUI
362     gui.starting = TRUE;
363 #else
364 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
365     /*
366      * Check if the GUI can be started.  Reset gui.starting if not.
367      * Don't know about other systems, stay on the safe side and don't check.
368      */
369     if (gui.starting)
370     {
371 	if (gui_init_check() == FAIL)
372 	{
373 	    gui.starting = FALSE;
374 
375 	    /* When running "evim" or "gvim -y" we need the menus, exit if we
376 	     * don't have them. */
377 	    if (params.evim_mode)
378 		mch_exit(1);
379 	}
380 #  if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
381 	/* Re-initialize locale, it may have been altered by gui_init_check() */
382 	init_locale();
383 #  endif
384     }
385 # endif
386 #endif
387 
388     if (GARGCOUNT > 0)
389     {
390 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
391 	/*
392 	 * Expand wildcards in file names.
393 	 */
394 	if (!params.literal)
395 	{
396 	    /* Temporarily add '(' and ')' to 'isfname'.  These are valid
397 	     * filename characters but are excluded from 'isfname' to make
398 	     * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
399 	    do_cmdline_cmd((char_u *)":set isf+=(,)");
400 	    alist_expand(NULL, 0);
401 	    do_cmdline_cmd((char_u *)":set isf&");
402 	}
403 #endif
404 	fname = alist_name(&GARGLIST[0]);
405     }
406 
407 #if defined(WIN32) && defined(FEAT_MBYTE)
408     {
409 	extern void set_alist_count(void);
410 
411 	/* Remember the number of entries in the argument list.  If it changes
412 	 * we don't react on setting 'encoding'. */
413 	set_alist_count();
414     }
415 #endif
416 
417 #ifdef MSWIN
418     if (GARGCOUNT == 1 && params.full_path)
419     {
420 	/*
421 	 * If there is one filename, fully qualified, we have very probably
422 	 * been invoked from explorer, so change to the file's directory.
423 	 * Hint: to avoid this when typing a command use a forward slash.
424 	 * If the cd fails, it doesn't matter.
425 	 */
426 	(void)vim_chdirfile(fname);
427     }
428 #endif
429     TIME_MSG("expanding arguments");
430 
431 #ifdef FEAT_DIFF
432     if (params.diff_mode && params.window_count == -1)
433 	params.window_count = 0;	/* open up to 3 windows */
434 #endif
435 
436     /* Don't redraw until much later. */
437     ++RedrawingDisabled;
438 
439     /*
440      * When listing swap file names, don't do cursor positioning et. al.
441      */
442     if (recoverymode && fname == NULL)
443 	params.want_full_screen = FALSE;
444 
445     /*
446      * When certain to start the GUI, don't check capabilities of terminal.
447      * For GTK we can't be sure, but when started from the desktop it doesn't
448      * make sense to try using a terminal.
449      */
450 #if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
451     if (gui.starting
452 # ifdef FEAT_GUI_GTK
453 	    && !isatty(2)
454 # endif
455 	    )
456 	params.want_full_screen = FALSE;
457 #endif
458 
459 #if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
460     /* When the GUI is started from Finder, need to display messages in a
461      * message box.  isatty(2) returns TRUE anyway, thus we need to check the
462      * name to know we're not started from a terminal. */
463     if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
464     {
465 	params.want_full_screen = FALSE;
466 
467 	/* Avoid always using "/" as the current directory.  Note that when
468 	 * started from Finder the arglist will be filled later in
469 	 * HandleODocAE() and "fname" will be NULL. */
470 	if (getcwd((char *)NameBuff, MAXPATHL) != NULL
471 						&& STRCMP(NameBuff, "/") == 0)
472 	{
473 	    if (fname != NULL)
474 		(void)vim_chdirfile(fname);
475 	    else
476 	    {
477 		expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
478 		vim_chdir(NameBuff);
479 	    }
480 	}
481     }
482 #endif
483 
484     /*
485      * mch_init() sets up the terminal (window) for use.  This must be
486      * done after resetting full_screen, otherwise it may move the cursor
487      * (MSDOS).
488      * Note that we may use mch_exit() before mch_init()!
489      */
490     mch_init();
491     TIME_MSG("shell init");
492 
493 #ifdef USE_XSMP
494     /*
495      * For want of anywhere else to do it, try to connect to xsmp here.
496      * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
497      * Hijacking -X 'no X connection' to also disable XSMP connection as that
498      * has a similar delay upon failure.
499      * Only try if SESSION_MANAGER is set to something non-null.
500      */
501     if (!x_no_connect)
502     {
503 	char *p = getenv("SESSION_MANAGER");
504 
505 	if (p != NULL && *p != NUL)
506 	{
507 	    xsmp_init();
508 	    TIME_MSG("xsmp init");
509 	}
510     }
511 #endif
512 
513     /*
514      * Print a warning if stdout is not a terminal.
515      */
516     check_tty(&params);
517 
518     /* This message comes before term inits, but after setting "silent_mode"
519      * when the input is not a tty. */
520     if (GARGCOUNT > 1 && !silent_mode)
521 	printf(_("%d files to edit\n"), GARGCOUNT);
522 
523     if (params.want_full_screen && !silent_mode)
524     {
525 	termcapinit(params.term);	/* set terminal name and get terminal
526 				   capabilities (will set full_screen) */
527 	screen_start();		/* don't know where cursor is now */
528 	TIME_MSG("Termcap init");
529     }
530 
531     /*
532      * Set the default values for the options that use Rows and Columns.
533      */
534     ui_get_shellsize();		/* inits Rows and Columns */
535 #ifdef FEAT_NETBEANS_INTG
536     if (usingNetbeans)
537 	Columns += 2;		/* leave room for glyph gutter */
538 #endif
539     win_init_size();
540 #ifdef FEAT_DIFF
541     /* Set the 'diff' option now, so that it can be checked for in a .vimrc
542      * file.  There is no buffer yet though. */
543     if (params.diff_mode)
544 	diff_win_options(firstwin, FALSE);
545 #endif
546 
547     cmdline_row = Rows - p_ch;
548     msg_row = cmdline_row;
549     screenalloc(FALSE);		/* allocate screen buffers */
550     set_init_2();
551     TIME_MSG("inits 2");
552 
553     msg_scroll = TRUE;
554     no_wait_return = TRUE;
555 
556     init_mappings();		/* set up initial mappings */
557 
558     init_highlight(TRUE, FALSE); /* set the default highlight groups */
559     TIME_MSG("init highlight");
560 
561 #ifdef FEAT_EVAL
562     /* Set the break level after the terminal is initialized. */
563     debug_break_level = params.use_debug_break_level;
564 #endif
565 
566     /* Execute --cmd arguments. */
567     exe_pre_commands(&params);
568 
569     /* Source startup scripts. */
570     source_startup_scripts(&params);
571 
572 #ifdef FEAT_EVAL
573     /*
574      * Read all the plugin files.
575      * Only when compiled with +eval, since most plugins need it.
576      */
577     if (p_lpl)
578     {
579 # ifdef VMS	/* Somehow VMS doesn't handle the "**". */
580 	source_runtime((char_u *)"plugin/*.vim", TRUE);
581 # else
582 	source_runtime((char_u *)"plugin/**/*.vim", TRUE);
583 # endif
584 	TIME_MSG("loading plugins");
585     }
586 #endif
587 
588 #ifdef FEAT_DIFF
589     /* Decide about window layout for diff mode after reading vimrc. */
590     if (params.diff_mode && params.window_layout == 0)
591     {
592 	if (diffopt_horizontal())
593 	    params.window_layout = WIN_HOR;	/* use horizontal split */
594 	else
595 	    params.window_layout = WIN_VER;	/* use vertical split */
596     }
597 #endif
598 
599     /*
600      * Recovery mode without a file name: List swap files.
601      * This uses the 'dir' option, therefore it must be after the
602      * initializations.
603      */
604     if (recoverymode && fname == NULL)
605     {
606 	recover_names(NULL, TRUE, 0);
607 	mch_exit(0);
608     }
609 
610     /*
611      * Set a few option defaults after reading .vimrc files:
612      * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
613      */
614     set_init_3();
615     TIME_MSG("inits 3");
616 
617     /*
618      * "-n" argument: Disable swap file by setting 'updatecount' to 0.
619      * Note that this overrides anything from a vimrc file.
620      */
621     if (params.no_swap_file)
622 	p_uc = 0;
623 
624 #ifdef FEAT_FKMAP
625     if (curwin->w_p_rl && p_altkeymap)
626     {
627 	p_hkmap = FALSE;	/* Reset the Hebrew keymap mode */
628 # ifdef FEAT_ARABIC
629 	curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
630 # endif
631 	p_fkmap = TRUE;		/* Set the Farsi keymap mode */
632     }
633 #endif
634 
635 #ifdef FEAT_GUI
636     if (gui.starting)
637     {
638 #if defined(UNIX) || defined(VMS)
639 	/* When something caused a message from a vimrc script, need to output
640 	 * an extra newline before the shell prompt. */
641 	if (did_emsg || msg_didout)
642 	    putchar('\n');
643 #endif
644 
645 	gui_start();		/* will set full_screen to TRUE */
646 	TIME_MSG("starting GUI");
647 
648 	/* When running "evim" or "gvim -y" we need the menus, exit if we
649 	 * don't have them. */
650 	if (!gui.in_use && params.evim_mode)
651 	    mch_exit(1);
652     }
653 #endif
654 
655 #ifdef SPAWNO		/* special MSDOS swapping library */
656     init_SPAWNO("", SWAP_ANY);
657 #endif
658 
659 #ifdef FEAT_VIMINFO
660     /*
661      * Read in registers, history etc, but not marks, from the viminfo file.
662      * This is where v:oldfiles gets filled.
663      */
664     if (*p_viminfo != NUL)
665     {
666 	read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
667 	TIME_MSG("reading viminfo");
668     }
669 #endif
670 
671 #ifdef FEAT_QUICKFIX
672     /*
673      * "-q errorfile": Load the error file now.
674      * If the error file can't be read, exit before doing anything else.
675      */
676     if (params.edit_type == EDIT_QF)
677     {
678 	if (params.use_ef != NULL)
679 	    set_string_option_direct((char_u *)"ef", -1,
680 					   params.use_ef, OPT_FREE, SID_CARG);
681 	if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
682 	{
683 	    out_char('\n');
684 	    mch_exit(3);
685 	}
686 	TIME_MSG("reading errorfile");
687     }
688 #endif
689 
690     /*
691      * Start putting things on the screen.
692      * Scroll screen down before drawing over it
693      * Clear screen now, so file message will not be cleared.
694      */
695     starting = NO_BUFFERS;
696     no_wait_return = FALSE;
697     if (!exmode_active)
698 	msg_scroll = FALSE;
699 
700 #ifdef FEAT_GUI
701     /*
702      * This seems to be required to make callbacks to be called now, instead
703      * of after things have been put on the screen, which then may be deleted
704      * when getting a resize callback.
705      * For the Mac this handles putting files dropped on the Vim icon to
706      * global_alist.
707      */
708     if (gui.in_use)
709     {
710 # ifdef FEAT_SUN_WORKSHOP
711 	if (!usingSunWorkShop)
712 # endif
713 	    gui_wait_for_chars(50L);
714 	TIME_MSG("GUI delay");
715     }
716 #endif
717 
718 #if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
719     qnx_clip_init();
720 #endif
721 
722 #ifdef FEAT_XCLIPBOARD
723     /* Start using the X clipboard, unless the GUI was started. */
724 # ifdef FEAT_GUI
725     if (!gui.in_use)
726 # endif
727     {
728 	setup_term_clip();
729 	TIME_MSG("setup clipboard");
730     }
731 #endif
732 
733 #ifdef FEAT_CLIENTSERVER
734     /* Prepare for being a Vim server. */
735     prepare_server(&params);
736 #endif
737 
738     /*
739      * If "-" argument given: Read file from stdin.
740      * Do this before starting Raw mode, because it may change things that the
741      * writing end of the pipe doesn't like, e.g., in case stdin and stderr
742      * are the same terminal: "cat | vim -".
743      * Using autocommands here may cause trouble...
744      */
745     if (params.edit_type == EDIT_STDIN && !recoverymode)
746 	read_stdin();
747 
748 #if defined(UNIX) || defined(VMS)
749     /* When switching screens and something caused a message from a vimrc
750      * script, need to output an extra newline on exit. */
751     if ((did_emsg || msg_didout) && *T_TI != NUL)
752 	newline_on_exit = TRUE;
753 #endif
754 
755     /*
756      * When done something that is not allowed or error message call
757      * wait_return.  This must be done before starttermcap(), because it may
758      * switch to another screen. It must be done after settmode(TMODE_RAW),
759      * because we want to react on a single key stroke.
760      * Call settmode and starttermcap here, so the T_KS and T_TI may be
761      * defined by termcapinit and redefined in .exrc.
762      */
763     settmode(TMODE_RAW);
764     TIME_MSG("setting raw mode");
765 
766     if (need_wait_return || msg_didany)
767     {
768 	wait_return(TRUE);
769 	TIME_MSG("waiting for return");
770     }
771 
772     starttermcap();	    /* start termcap if not done by wait_return() */
773     TIME_MSG("start termcap");
774 
775 #ifdef FEAT_MOUSE
776     setmouse();				/* may start using the mouse */
777 #endif
778     if (scroll_region)
779 	scroll_region_reset();		/* In case Rows changed */
780     scroll_start();	/* may scroll the screen to the right position */
781 
782     /*
783      * Don't clear the screen when starting in Ex mode, unless using the GUI.
784      */
785     if (exmode_active
786 #ifdef FEAT_GUI
787 			&& !gui.in_use
788 #endif
789 					)
790 	must_redraw = CLEAR;
791     else
792     {
793 	screenclear();			/* clear screen */
794 	TIME_MSG("clearing screen");
795     }
796 
797 #ifdef FEAT_CRYPT
798     if (params.ask_for_key)
799     {
800 	(void)get_crypt_key(TRUE, TRUE);
801 	TIME_MSG("getting crypt key");
802     }
803 #endif
804 
805     no_wait_return = TRUE;
806 
807     /*
808      * Create the requested number of windows and edit buffers in them.
809      * Also does recovery if "recoverymode" set.
810      */
811     create_windows(&params);
812     TIME_MSG("opening buffers");
813 
814 #ifdef FEAT_EVAL
815     /* clear v:swapcommand */
816     set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
817 #endif
818 
819     /* Ex starts at last line of the file */
820     if (exmode_active)
821 	curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
822 
823 #ifdef FEAT_AUTOCMD
824     apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
825     TIME_MSG("BufEnter autocommands");
826 #endif
827     setpcmark();
828 
829 #ifdef FEAT_QUICKFIX
830     /*
831      * When started with "-q errorfile" jump to first error now.
832      */
833     if (params.edit_type == EDIT_QF)
834     {
835 	qf_jump(NULL, 0, 0, FALSE);
836 	TIME_MSG("jump to first error");
837     }
838 #endif
839 
840 #ifdef FEAT_WINDOWS
841     /*
842      * If opened more than one window, start editing files in the other
843      * windows.
844      */
845     edit_buffers(&params);
846 #endif
847 
848 #ifdef FEAT_DIFF
849     if (params.diff_mode)
850     {
851 	win_T	*wp;
852 
853 	/* set options in each window for "vimdiff". */
854 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
855 	    diff_win_options(wp, TRUE);
856     }
857 #endif
858 
859     /*
860      * Shorten any of the filenames, but only when absolute.
861      */
862     shorten_fnames(FALSE);
863 
864     /*
865      * Need to jump to the tag before executing the '-c command'.
866      * Makes "vim -c '/return' -t main" work.
867      */
868     if (params.tagname != NULL)
869     {
870 #if defined(HAS_SWAP_EXISTS_ACTION)
871 	swap_exists_did_quit = FALSE;
872 #endif
873 
874 	vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
875 	do_cmdline_cmd(IObuff);
876 	TIME_MSG("jumping to tag");
877 
878 #if defined(HAS_SWAP_EXISTS_ACTION)
879 	/* If the user doesn't want to edit the file then we quit here. */
880 	if (swap_exists_did_quit)
881 	    getout(1);
882 #endif
883     }
884 
885     /* Execute any "+", "-c" and "-S" arguments. */
886     if (params.n_commands > 0)
887 	exe_commands(&params);
888 
889     RedrawingDisabled = 0;
890     redraw_all_later(NOT_VALID);
891     no_wait_return = FALSE;
892     starting = 0;
893 
894 #ifdef FEAT_TERMRESPONSE
895     /* Requesting the termresponse is postponed until here, so that a "-c q"
896      * argument doesn't make it appear in the shell Vim was started from. */
897     may_req_termresponse();
898 #endif
899 
900     /* start in insert mode */
901     if (p_im)
902 	need_start_insertmode = TRUE;
903 
904 #ifdef FEAT_AUTOCMD
905     apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
906     TIME_MSG("VimEnter autocommands");
907 #endif
908 
909 #if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
910     /* When a startup script or session file setup for diff'ing and
911      * scrollbind, sync the scrollbind now. */
912     if (curwin->w_p_diff && curwin->w_p_scb)
913     {
914 	update_topline();
915 	check_scrollbind((linenr_T)0, 0L);
916 	TIME_MSG("diff scrollbinding");
917     }
918 #endif
919 
920 #if defined(WIN3264) && !defined(FEAT_GUI_W32)
921     mch_set_winsize_now();	    /* Allow winsize changes from now on */
922 #endif
923 
924 #if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
925     /* When tab pages were created, may need to update the tab pages line and
926      * scrollbars.  This is skipped while creating them. */
927     if (first_tabpage->tp_next != NULL)
928     {
929 	out_flush();
930 	gui_init_which_components(NULL);
931 	gui_update_scrollbars(TRUE);
932     }
933     need_mouse_correct = TRUE;
934 #endif
935 
936     /* If ":startinsert" command used, stuff a dummy command to be able to
937      * call normal_cmd(), which will then start Insert mode. */
938     if (restart_edit != 0)
939 	stuffcharReadbuff(K_NOP);
940 
941 #ifdef FEAT_NETBEANS_INTG
942     if (usingNetbeans)
943 	/* Tell the client that it can start sending commands. */
944 	netbeans_startup_done();
945 #endif
946 
947     TIME_MSG("before starting main loop");
948 
949     /*
950      * Call the main command loop.  This never returns.
951      * For embedded MzScheme the main_loop will be called by Scheme
952      * for proper stack tracking
953      */
954 #ifndef FEAT_MZSCHEME
955     main_loop(FALSE, FALSE);
956 #else
957     mzscheme_main();
958 #endif
959 
960     return 0;
961 }
962 #endif /* PROTO */
963 
964 /*
965  * Main loop: Execute Normal mode commands until exiting Vim.
966  * Also used to handle commands in the command-line window, until the window
967  * is closed.
968  * Also used to handle ":visual" command after ":global": execute Normal mode
969  * commands, return when entering Ex mode.  "noexmode" is TRUE then.
970  */
971     void
972 main_loop(cmdwin, noexmode)
973     int		cmdwin;	    /* TRUE when working in the command-line window */
974     int		noexmode;   /* TRUE when return on entering Ex mode */
975 {
976     oparg_T	oa;				/* operator arguments */
977     int		previous_got_int = FALSE;	/* "got_int" was TRUE */
978 
979 #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
980     /* Setup to catch a terminating error from the X server.  Just ignore
981      * it, restore the state and continue.  This might not always work
982      * properly, but at least we don't exit unexpectedly when the X server
983      * exists while Vim is running in a console. */
984     if (!cmdwin && !noexmode && SETJMP(x_jump_env))
985     {
986 	State = NORMAL;
987 # ifdef FEAT_VISUAL
988 	VIsual_active = FALSE;
989 # endif
990 	got_int = TRUE;
991 	need_wait_return = FALSE;
992 	global_busy = FALSE;
993 	exmode_active = 0;
994 	skip_redraw = FALSE;
995 	RedrawingDisabled = 0;
996 	no_wait_return = 0;
997 # ifdef FEAT_EVAL
998 	emsg_skip = 0;
999 # endif
1000 	emsg_off = 0;
1001 # ifdef FEAT_MOUSE
1002 	setmouse();
1003 # endif
1004 	settmode(TMODE_RAW);
1005 	starttermcap();
1006 	scroll_start();
1007 	redraw_later_clear();
1008     }
1009 #endif
1010 
1011     clear_oparg(&oa);
1012     while (!cmdwin
1013 #ifdef FEAT_CMDWIN
1014 	    || cmdwin_result == 0
1015 #endif
1016 	    )
1017     {
1018 	if (stuff_empty())
1019 	{
1020 	    did_check_timestamps = FALSE;
1021 	    if (need_check_timestamps)
1022 		check_timestamps(FALSE);
1023 	    if (need_wait_return)	/* if wait_return still needed ... */
1024 		wait_return(FALSE);	/* ... call it now */
1025 	    if (need_start_insertmode && goto_im()
1026 #ifdef FEAT_VISUAL
1027 		    && !VIsual_active
1028 #endif
1029 		    )
1030 	    {
1031 		need_start_insertmode = FALSE;
1032 		stuffReadbuff((char_u *)"i");	/* start insert mode next */
1033 		/* skip the fileinfo message now, because it would be shown
1034 		 * after insert mode finishes! */
1035 		need_fileinfo = FALSE;
1036 	    }
1037 	}
1038 
1039 	/* Reset "got_int" now that we got back to the main loop.  Except when
1040 	 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1041 	 * the ":g" command.
1042 	 * For ":g/pat/vi" we reset "got_int" when used once.  When used
1043 	 * a second time we go back to Ex mode and abort the ":g" command. */
1044 	if (got_int)
1045 	{
1046 	    if (noexmode && global_busy && !exmode_active && previous_got_int)
1047 	    {
1048 		/* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1049 		 * used and keep "got_int" set, so that it aborts ":g". */
1050 		exmode_active = EXMODE_NORMAL;
1051 		State = NORMAL;
1052 	    }
1053 	    else if (!global_busy || !exmode_active)
1054 	    {
1055 		if (!quit_more)
1056 		    (void)vgetc();		/* flush all buffers */
1057 		got_int = FALSE;
1058 	    }
1059 	    previous_got_int = TRUE;
1060 	}
1061 	else
1062 	    previous_got_int = FALSE;
1063 
1064 	if (!exmode_active)
1065 	    msg_scroll = FALSE;
1066 	quit_more = FALSE;
1067 
1068 	/*
1069 	 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1070 	 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1071 	 * update cursor and redraw.
1072 	 */
1073 	if (skip_redraw || exmode_active)
1074 	    skip_redraw = FALSE;
1075 	else if (do_redraw || stuff_empty())
1076 	{
1077 #ifdef FEAT_AUTOCMD
1078 	    /* Trigger CursorMoved if the cursor moved. */
1079 	    if (!finish_op && has_cursormoved()
1080 			     && !equalpos(last_cursormoved, curwin->w_cursor))
1081 	    {
1082 		apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1083 		last_cursormoved = curwin->w_cursor;
1084 	    }
1085 #endif
1086 
1087 #if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1088 	    /* Scroll-binding for diff mode may have been postponed until
1089 	     * here.  Avoids doing it for every change. */
1090 	    if (diff_need_scrollbind)
1091 	    {
1092 		check_scrollbind((linenr_T)0, 0L);
1093 		diff_need_scrollbind = FALSE;
1094 	    }
1095 #endif
1096 #if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1097 	    /* Include a closed fold completely in the Visual area. */
1098 	    foldAdjustVisual();
1099 #endif
1100 #ifdef FEAT_FOLDING
1101 	    /*
1102 	     * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1103 	     * contain the cursor.
1104 	     * When 'foldopen' is "all", open the fold(s) under the cursor.
1105 	     * This may mark the window for redrawing.
1106 	     */
1107 	    if (hasAnyFolding(curwin) && !char_avail())
1108 	    {
1109 		foldCheckClose();
1110 		if (fdo_flags & FDO_ALL)
1111 		    foldOpenCursor();
1112 	    }
1113 #endif
1114 
1115 	    /*
1116 	     * Before redrawing, make sure w_topline is correct, and w_leftcol
1117 	     * if lines don't wrap, and w_skipcol if lines wrap.
1118 	     */
1119 	    update_topline();
1120 	    validate_cursor();
1121 
1122 #ifdef FEAT_VISUAL
1123 	    if (VIsual_active)
1124 		update_curbuf(INVERTED);/* update inverted part */
1125 	    else
1126 #endif
1127 		if (must_redraw)
1128 		update_screen(0);
1129 	    else if (redraw_cmdline || clear_cmdline)
1130 		showmode();
1131 #ifdef FEAT_WINDOWS
1132 	    redraw_statuslines();
1133 #endif
1134 #ifdef FEAT_TITLE
1135 	    if (need_maketitle)
1136 		maketitle();
1137 #endif
1138 	    /* display message after redraw */
1139 	    if (keep_msg != NULL)
1140 	    {
1141 		char_u *p;
1142 
1143 		/* msg_attr_keep() will set keep_msg to NULL, must free the
1144 		 * string here. */
1145 		p = keep_msg;
1146 		keep_msg = NULL;
1147 		msg_attr(p, keep_msg_attr);
1148 		vim_free(p);
1149 	    }
1150 	    if (need_fileinfo)		/* show file info after redraw */
1151 	    {
1152 		fileinfo(FALSE, TRUE, FALSE);
1153 		need_fileinfo = FALSE;
1154 	    }
1155 
1156 	    emsg_on_display = FALSE;	/* can delete error message now */
1157 	    did_emsg = FALSE;
1158 	    msg_didany = FALSE;		/* reset lines_left in msg_start() */
1159 	    may_clear_sb_text();	/* clear scroll-back text on next msg */
1160 	    showruler(FALSE);
1161 
1162 	    setcursor();
1163 	    cursor_on();
1164 
1165 	    do_redraw = FALSE;
1166 
1167 #ifdef STARTUPTIME
1168 	    /* Now that we have drawn the first screen all the startup stuff
1169 	     * has been done, close any file for startup messages. */
1170 	    if (time_fd != NULL)
1171 	    {
1172 		TIME_MSG("first screen update");
1173 		TIME_MSG("--- VIM STARTED ---");
1174 		fclose(time_fd);
1175 		time_fd = NULL;
1176 	    }
1177 #endif
1178 	}
1179 #ifdef FEAT_GUI
1180 	if (need_mouse_correct)
1181 	    gui_mouse_correct();
1182 #endif
1183 
1184 	/*
1185 	 * Update w_curswant if w_set_curswant has been set.
1186 	 * Postponed until here to avoid computing w_virtcol too often.
1187 	 */
1188 	update_curswant();
1189 
1190 #ifdef FEAT_EVAL
1191 	/*
1192 	 * May perform garbage collection when waiting for a character, but
1193 	 * only at the very toplevel.  Otherwise we may be using a List or
1194 	 * Dict internally somewhere.
1195 	 * "may_garbage_collect" is reset in vgetc() which is invoked through
1196 	 * do_exmode() and normal_cmd().
1197 	 */
1198 	may_garbage_collect = (!cmdwin && !noexmode);
1199 #endif
1200 	/*
1201 	 * If we're invoked as ex, do a round of ex commands.
1202 	 * Otherwise, get and execute a normal mode command.
1203 	 */
1204 	if (exmode_active)
1205 	{
1206 	    if (noexmode)   /* End of ":global/path/visual" commands */
1207 		return;
1208 	    do_exmode(exmode_active == EXMODE_VIM);
1209 	}
1210 	else
1211 	    normal_cmd(&oa, TRUE);
1212     }
1213 }
1214 
1215 
1216 #if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1217 /*
1218  * Exit, but leave behind swap files for modified buffers.
1219  */
1220     void
1221 getout_preserve_modified(exitval)
1222     int		exitval;
1223 {
1224 # if defined(SIGHUP) && defined(SIG_IGN)
1225     /* Ignore SIGHUP, because a dropped connection causes a read error, which
1226      * makes Vim exit and then handling SIGHUP causes various reentrance
1227      * problems. */
1228     signal(SIGHUP, SIG_IGN);
1229 # endif
1230 
1231     ml_close_notmod();		    /* close all not-modified buffers */
1232     ml_sync_all(FALSE, FALSE);	    /* preserve all swap files */
1233     ml_close_all(FALSE);	    /* close all memfiles, without deleting */
1234     getout(exitval);		    /* exit Vim properly */
1235 }
1236 #endif
1237 
1238 
1239 /* Exit properly */
1240     void
1241 getout(exitval)
1242     int		exitval;
1243 {
1244 #ifdef FEAT_AUTOCMD
1245     buf_T	*buf;
1246     win_T	*wp;
1247     tabpage_T	*tp, *next_tp;
1248 #endif
1249 
1250     exiting = TRUE;
1251 
1252     /* When running in Ex mode an error causes us to exit with a non-zero exit
1253      * code.  POSIX requires this, although it's not 100% clear from the
1254      * standard. */
1255     if (exmode_active)
1256 	exitval += ex_exitval;
1257 
1258     /* Position the cursor on the last screen line, below all the text */
1259 #ifdef FEAT_GUI
1260     if (!gui.in_use)
1261 #endif
1262 	windgoto((int)Rows - 1, 0);
1263 
1264 #if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1265     /* Optionally print hashtable efficiency. */
1266     hash_debug_results();
1267 #endif
1268 
1269 #ifdef FEAT_GUI
1270     msg_didany = FALSE;
1271 #endif
1272 
1273 #ifdef FEAT_AUTOCMD
1274     /* Trigger BufWinLeave for all windows, but only once per buffer. */
1275 # if defined FEAT_WINDOWS
1276     for (tp = first_tabpage; tp != NULL; tp = next_tp)
1277     {
1278 	next_tp = tp->tp_next;
1279 	for (wp = (tp == curtab)
1280 		    ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1281 	{
1282 	    buf = wp->w_buffer;
1283 	    if (buf->b_changedtick != -1)
1284 	    {
1285 		apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
1286 								  FALSE, buf);
1287 		buf->b_changedtick = -1;    /* note that we did it already */
1288 		/* start all over, autocommands may mess up the lists */
1289 		next_tp = first_tabpage;
1290 		break;
1291 	    }
1292 	}
1293     }
1294 # else
1295     apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1296 # endif
1297 
1298     /* Trigger BufUnload for buffers that are loaded */
1299     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1300 	if (buf->b_ml.ml_mfp != NULL)
1301 	{
1302 	    apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1303 								  FALSE, buf);
1304 	    if (!buf_valid(buf))	/* autocmd may delete the buffer */
1305 		break;
1306 	}
1307     apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1308 #endif
1309 
1310 #ifdef FEAT_VIMINFO
1311     if (*p_viminfo != NUL)
1312 	/* Write out the registers, history, marks etc, to the viminfo file */
1313 	write_viminfo(NULL, FALSE);
1314 #endif
1315 
1316 #ifdef FEAT_AUTOCMD
1317     apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1318 #endif
1319 
1320 #ifdef FEAT_PROFILE
1321     profile_dump();
1322 #endif
1323 
1324     if (did_emsg
1325 #ifdef FEAT_GUI
1326 	    || (gui.in_use && msg_didany && p_verbose > 0)
1327 #endif
1328 	    )
1329     {
1330 	/* give the user a chance to read the (error) message */
1331 	no_wait_return = FALSE;
1332 	wait_return(FALSE);
1333     }
1334 
1335 #ifdef FEAT_AUTOCMD
1336     /* Position the cursor again, the autocommands may have moved it */
1337 # ifdef FEAT_GUI
1338     if (!gui.in_use)
1339 # endif
1340 	windgoto((int)Rows - 1, 0);
1341 #endif
1342 
1343 #ifdef FEAT_MZSCHEME
1344     mzscheme_end();
1345 #endif
1346 #ifdef FEAT_TCL
1347     tcl_end();
1348 #endif
1349 #ifdef FEAT_RUBY
1350     ruby_end();
1351 #endif
1352 #ifdef FEAT_PYTHON
1353     python_end();
1354 #endif
1355 #ifdef FEAT_PERL
1356     perl_end();
1357 #endif
1358 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1359     iconv_end();
1360 #endif
1361 #ifdef FEAT_NETBEANS_INTG
1362     netbeans_end();
1363 #endif
1364 #ifdef FEAT_CSCOPE
1365     cs_end();
1366 #endif
1367 #ifdef FEAT_EVAL
1368     if (garbage_collect_at_exit)
1369 	garbage_collect();
1370 #endif
1371 
1372     mch_exit(exitval);
1373 }
1374 
1375 /*
1376  * Get a (optional) count for a Vim argument.
1377  */
1378     static int
1379 get_number_arg(p, idx, def)
1380     char_u	*p;	    /* pointer to argument */
1381     int		*idx;	    /* index in argument, is incremented */
1382     int		def;	    /* default value */
1383 {
1384     if (vim_isdigit(p[*idx]))
1385     {
1386 	def = atoi((char *)&(p[*idx]));
1387 	while (vim_isdigit(p[*idx]))
1388 	    *idx = *idx + 1;
1389     }
1390     return def;
1391 }
1392 
1393 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1394 /*
1395  * Setup to use the current locale (for ctype() and many other things).
1396  */
1397     static void
1398 init_locale()
1399 {
1400     setlocale(LC_ALL, "");
1401 
1402 # if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1403     /* Make sure strtod() uses a decimal point, not a comma. */
1404     setlocale(LC_NUMERIC, "C");
1405 # endif
1406 
1407 # ifdef WIN32
1408     /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1409      * text while it's expecting text in the current locale.  This call avoids
1410      * that. */
1411     setlocale(LC_CTYPE, "C");
1412 # endif
1413 
1414 # ifdef FEAT_GETTEXT
1415     {
1416 	int	mustfree = FALSE;
1417 	char_u	*p;
1418 
1419 #  ifdef DYNAMIC_GETTEXT
1420 	/* Initialize the gettext library */
1421 	dyn_libintl_init(NULL);
1422 #  endif
1423 	/* expand_env() doesn't work yet, because chartab[] is not initialized
1424 	 * yet, call vim_getenv() directly */
1425 	p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1426 	if (p != NULL && *p != NUL)
1427 	{
1428 	    vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
1429 	    bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1430 	}
1431 	if (mustfree)
1432 	    vim_free(p);
1433 	textdomain(VIMPACKAGE);
1434     }
1435 # endif
1436 }
1437 #endif
1438 
1439 /*
1440  * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1441  * If the executable name starts with "r" we disable shell commands.
1442  * If the next character is "e" we run in Easy mode.
1443  * If the next character is "g" we run the GUI version.
1444  * If the next characters are "view" we start in readonly mode.
1445  * If the next characters are "diff" or "vimdiff" we start in diff mode.
1446  * If the next characters are "ex" we start in Ex mode.  If it's followed
1447  * by "im" use improved Ex mode.
1448  */
1449     static void
1450 parse_command_name(parmp)
1451     mparm_T	*parmp;
1452 {
1453     char_u	*initstr;
1454 
1455     initstr = gettail((char_u *)parmp->argv[0]);
1456 
1457 #ifdef MACOS_X_UNIX
1458     /* An issue has been seen when launching Vim in such a way that
1459      * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1460      * executable or a symbolic link of it. Until this issue is resolved
1461      * we prohibit the GUI from being used.
1462      */
1463     if (STRCMP(initstr, parmp->argv[0]) == 0)
1464 	disallow_gui = TRUE;
1465 
1466     /* TODO: On MacOS X default to gui if argv[0] ends in:
1467      *       /Vim.app/Contents/MacOS/Vim */
1468 #endif
1469 
1470 #ifdef FEAT_EVAL
1471     set_vim_var_string(VV_PROGNAME, initstr, -1);
1472 #endif
1473 
1474     if (TOLOWER_ASC(initstr[0]) == 'r')
1475     {
1476 	restricted = TRUE;
1477 	++initstr;
1478     }
1479 
1480     /* Use evim mode for "evim" and "egvim", not for "editor". */
1481     if (TOLOWER_ASC(initstr[0]) == 'e'
1482 	    && (TOLOWER_ASC(initstr[1]) == 'v'
1483 		|| TOLOWER_ASC(initstr[1]) == 'g'))
1484     {
1485 #ifdef FEAT_GUI
1486 	gui.starting = TRUE;
1487 #endif
1488 	parmp->evim_mode = TRUE;
1489 	++initstr;
1490     }
1491 
1492     /* "gvim" starts the GUI.  Also accept "Gvim" for MS-Windows. */
1493     if (TOLOWER_ASC(initstr[0]) == 'g')
1494     {
1495 	main_start_gui();
1496 #ifdef FEAT_GUI
1497 	++initstr;
1498 #endif
1499     }
1500 
1501     if (STRNICMP(initstr, "view", 4) == 0)
1502     {
1503 	readonlymode = TRUE;
1504 	curbuf->b_p_ro = TRUE;
1505 	p_uc = 10000;			/* don't update very often */
1506 	initstr += 4;
1507     }
1508     else if (STRNICMP(initstr, "vim", 3) == 0)
1509 	initstr += 3;
1510 
1511     /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1512     if (STRICMP(initstr, "diff") == 0)
1513     {
1514 #ifdef FEAT_DIFF
1515 	parmp->diff_mode = TRUE;
1516 #else
1517 	mch_errmsg(_("This Vim was not compiled with the diff feature."));
1518 	mch_errmsg("\n");
1519 	mch_exit(2);
1520 #endif
1521     }
1522 
1523     if (STRNICMP(initstr, "ex", 2) == 0)
1524     {
1525 	if (STRNICMP(initstr + 2, "im", 2) == 0)
1526 	    exmode_active = EXMODE_VIM;
1527 	else
1528 	    exmode_active = EXMODE_NORMAL;
1529 	change_compatible(TRUE);	/* set 'compatible' */
1530     }
1531 }
1532 
1533 /*
1534  * Get the name of the display, before gui_prepare() removes it from
1535  * argv[].  Used for the xterm-clipboard display.
1536  *
1537  * Also find the --server... arguments and --socketid and --windowid
1538  */
1539     static void
1540 early_arg_scan(parmp)
1541     mparm_T	*parmp UNUSED;
1542 {
1543 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1544 	|| !defined(FEAT_NETBEANS_INTG)
1545     int		argc = parmp->argc;
1546     char	**argv = parmp->argv;
1547     int		i;
1548 
1549     for (i = 1; i < argc; i++)
1550     {
1551 	if (STRCMP(argv[i], "--") == 0)
1552 	    break;
1553 # ifdef FEAT_XCLIPBOARD
1554 	else if (STRICMP(argv[i], "-display") == 0
1555 #  if defined(FEAT_GUI_GTK)
1556 		|| STRICMP(argv[i], "--display") == 0
1557 #  endif
1558 		)
1559 	{
1560 	    if (i == argc - 1)
1561 		mainerr_arg_missing((char_u *)argv[i]);
1562 	    xterm_display = argv[++i];
1563 	}
1564 # endif
1565 # ifdef FEAT_CLIENTSERVER
1566 	else if (STRICMP(argv[i], "--servername") == 0)
1567 	{
1568 	    if (i == argc - 1)
1569 		mainerr_arg_missing((char_u *)argv[i]);
1570 	    parmp->serverName_arg = (char_u *)argv[++i];
1571 	}
1572 	else if (STRICMP(argv[i], "--serverlist") == 0)
1573 	    parmp->serverArg = TRUE;
1574 	else if (STRNICMP(argv[i], "--remote", 8) == 0)
1575 	{
1576 	    parmp->serverArg = TRUE;
1577 #  ifdef FEAT_GUI
1578 	    if (strstr(argv[i], "-wait") != 0)
1579 		/* don't fork() when starting the GUI to edit files ourself */
1580 		gui.dofork = FALSE;
1581 #  endif
1582 	}
1583 # endif
1584 
1585 # if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1586 #  ifdef FEAT_GUI_W32
1587 	else if (STRICMP(argv[i], "--windowid") == 0)
1588 #  else
1589 	else if (STRICMP(argv[i], "--socketid") == 0)
1590 #  endif
1591 	{
1592 	    long_u	id;
1593 	    int		count;
1594 
1595 	    if (i == argc - 1)
1596 		mainerr_arg_missing((char_u *)argv[i]);
1597 	    if (STRNICMP(argv[i+1], "0x", 2) == 0)
1598 		count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
1599 	    else
1600 		count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
1601 	    if (count != 1)
1602 		mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1603 	    else
1604 #  ifdef FEAT_GUI_W32
1605 		win_socket_id = id;
1606 #  else
1607 		gtk_socket_id = id;
1608 #  endif
1609 	    i++;
1610 	}
1611 # endif
1612 # ifdef FEAT_GUI_GTK
1613 	else if (STRICMP(argv[i], "--echo-wid") == 0)
1614 	    echo_wid_arg = TRUE;
1615 # endif
1616 # ifndef FEAT_NETBEANS_INTG
1617 	else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
1618         {
1619             mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1620             mch_exit(2);
1621         }
1622 # endif
1623 
1624     }
1625 #endif
1626 }
1627 
1628 /*
1629  * Scan the command line arguments.
1630  */
1631     static void
1632 command_line_scan(parmp)
1633     mparm_T	*parmp;
1634 {
1635     int		argc = parmp->argc;
1636     char	**argv = parmp->argv;
1637     int		argv_idx;		/* index in argv[n][] */
1638     int		had_minmin = FALSE;	/* found "--" argument */
1639     int		want_argument;		/* option argument with argument */
1640     int		c;
1641     char_u	*p = NULL;
1642     long	n;
1643 
1644     --argc;
1645     ++argv;
1646     argv_idx = 1;	    /* active option letter is argv[0][argv_idx] */
1647     while (argc > 0)
1648     {
1649 	/*
1650 	 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1651 	 */
1652 	if (argv[0][0] == '+' && !had_minmin)
1653 	{
1654 	    if (parmp->n_commands >= MAX_ARG_CMDS)
1655 		mainerr(ME_EXTRA_CMD, NULL);
1656 	    argv_idx = -1;	    /* skip to next argument */
1657 	    if (argv[0][1] == NUL)
1658 		parmp->commands[parmp->n_commands++] = (char_u *)"$";
1659 	    else
1660 		parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1661 	}
1662 
1663 	/*
1664 	 * Optional argument.
1665 	 */
1666 	else if (argv[0][0] == '-' && !had_minmin)
1667 	{
1668 	    want_argument = FALSE;
1669 	    c = argv[0][argv_idx++];
1670 #ifdef VMS
1671 	    /*
1672 	     * VMS only uses upper case command lines.  Interpret "-X" as "-x"
1673 	     * and "-/X" as "-X".
1674 	     */
1675 	    if (c == '/')
1676 	    {
1677 		c = argv[0][argv_idx++];
1678 		c = TOUPPER_ASC(c);
1679 	    }
1680 	    else
1681 		c = TOLOWER_ASC(c);
1682 #endif
1683 	    switch (c)
1684 	    {
1685 	    case NUL:		/* "vim -"  read from stdin */
1686 				/* "ex -" silent mode */
1687 		if (exmode_active)
1688 		    silent_mode = TRUE;
1689 		else
1690 		{
1691 		    if (parmp->edit_type != EDIT_NONE)
1692 			mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1693 		    parmp->edit_type = EDIT_STDIN;
1694 		    read_cmd_fd = 2;	/* read from stderr instead of stdin */
1695 		}
1696 		argv_idx = -1;		/* skip to next argument */
1697 		break;
1698 
1699 	    case '-':		/* "--" don't take any more option arguments */
1700 				/* "--help" give help message */
1701 				/* "--version" give version message */
1702 				/* "--literal" take files literally */
1703 				/* "--nofork" don't fork */
1704 				/* "--noplugin[s]" skip plugins */
1705 				/* "--cmd <cmd>" execute cmd before vimrc */
1706 		if (STRICMP(argv[0] + argv_idx, "help") == 0)
1707 		    usage();
1708 		else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1709 		{
1710 		    Columns = 80;	/* need to init Columns */
1711 		    info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1712 		    list_version();
1713 		    msg_putchar('\n');
1714 		    msg_didout = FALSE;
1715 		    mch_exit(0);
1716 		}
1717 		else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1718 		{
1719 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1720 		    parmp->literal = TRUE;
1721 #endif
1722 		}
1723 		else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1724 		{
1725 #ifdef FEAT_GUI
1726 		    gui.dofork = FALSE;	/* don't fork() when starting GUI */
1727 #endif
1728 		}
1729 		else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1730 		    p_lpl = FALSE;
1731 		else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1732 		{
1733 		    want_argument = TRUE;
1734 		    argv_idx += 3;
1735 		}
1736 		else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1737 		{
1738 		    want_argument = TRUE;
1739 		    argv_idx += 11;
1740 		}
1741 #ifdef FEAT_CLIENTSERVER
1742 		else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1743 		    ; /* already processed -- no arg */
1744 		else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1745 		       || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1746 		{
1747 		    /* already processed -- snatch the following arg */
1748 		    if (argc > 1)
1749 		    {
1750 			--argc;
1751 			++argv;
1752 		    }
1753 		}
1754 #endif
1755 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1756 # ifdef FEAT_GUI_GTK
1757 		else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1758 # else
1759 		else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1760 # endif
1761 		{
1762 		    /* already processed -- snatch the following arg */
1763 		    if (argc > 1)
1764 		    {
1765 			--argc;
1766 			++argv;
1767 		    }
1768 		}
1769 #endif
1770 #ifdef FEAT_GUI_GTK
1771 		else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1772 		{
1773 		    /* already processed, skip */
1774 		}
1775 #endif
1776 		else
1777 		{
1778 		    if (argv[0][argv_idx])
1779 			mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1780 		    had_minmin = TRUE;
1781 		}
1782 		if (!want_argument)
1783 		    argv_idx = -1;	/* skip to next argument */
1784 		break;
1785 
1786 	    case 'A':		/* "-A" start in Arabic mode */
1787 #ifdef FEAT_ARABIC
1788 		set_option_value((char_u *)"arabic", 1L, NULL, 0);
1789 #else
1790 		mch_errmsg(_(e_noarabic));
1791 		mch_exit(2);
1792 #endif
1793 		break;
1794 
1795 	    case 'b':		/* "-b" binary mode */
1796 		/* Needs to be effective before expanding file names, because
1797 		 * for Win32 this makes us edit a shortcut file itself,
1798 		 * instead of the file it links to. */
1799 		set_options_bin(curbuf->b_p_bin, 1, 0);
1800 		curbuf->b_p_bin = 1;	    /* binary file I/O */
1801 		break;
1802 
1803 	    case 'C':		/* "-C"  Compatible */
1804 		change_compatible(TRUE);
1805 		break;
1806 
1807 	    case 'e':		/* "-e" Ex mode */
1808 		exmode_active = EXMODE_NORMAL;
1809 		break;
1810 
1811 	    case 'E':		/* "-E" Improved Ex mode */
1812 		exmode_active = EXMODE_VIM;
1813 		break;
1814 
1815 	    case 'f':		/* "-f"  GUI: run in foreground.  Amiga: open
1816 				window directly, not with newcli */
1817 #ifdef FEAT_GUI
1818 		gui.dofork = FALSE;	/* don't fork() when starting GUI */
1819 #endif
1820 		break;
1821 
1822 	    case 'g':		/* "-g" start GUI */
1823 		main_start_gui();
1824 		break;
1825 
1826 	    case 'F':		/* "-F" start in Farsi mode: rl + fkmap set */
1827 #ifdef FEAT_FKMAP
1828 		p_fkmap = TRUE;
1829 		set_option_value((char_u *)"rl", 1L, NULL, 0);
1830 #else
1831 		mch_errmsg(_(e_nofarsi));
1832 		mch_exit(2);
1833 #endif
1834 		break;
1835 
1836 	    case 'h':		/* "-h" give help message */
1837 #ifdef FEAT_GUI_GNOME
1838 		/* Tell usage() to exit for "gvim". */
1839 		gui.starting = FALSE;
1840 #endif
1841 		usage();
1842 		break;
1843 
1844 	    case 'H':		/* "-H" start in Hebrew mode: rl + hkmap set */
1845 #ifdef FEAT_RIGHTLEFT
1846 		p_hkmap = TRUE;
1847 		set_option_value((char_u *)"rl", 1L, NULL, 0);
1848 #else
1849 		mch_errmsg(_(e_nohebrew));
1850 		mch_exit(2);
1851 #endif
1852 		break;
1853 
1854 	    case 'l':		/* "-l" lisp mode, 'lisp' and 'showmatch' on */
1855 #ifdef FEAT_LISP
1856 		set_option_value((char_u *)"lisp", 1L, NULL, 0);
1857 		p_sm = TRUE;
1858 #endif
1859 		break;
1860 
1861 	    case 'M':		/* "-M"  no changes or writing of files */
1862 		reset_modifiable();
1863 		/* FALLTHROUGH */
1864 
1865 	    case 'm':		/* "-m"  no writing of files */
1866 		p_write = FALSE;
1867 		break;
1868 
1869 	    case 'y':		/* "-y"  easy mode */
1870 #ifdef FEAT_GUI
1871 		gui.starting = TRUE;	/* start GUI a bit later */
1872 #endif
1873 		parmp->evim_mode = TRUE;
1874 		break;
1875 
1876 	    case 'N':		/* "-N"  Nocompatible */
1877 		change_compatible(FALSE);
1878 		break;
1879 
1880 	    case 'n':		/* "-n" no swap file */
1881 		parmp->no_swap_file = TRUE;
1882 		break;
1883 
1884 	    case 'p':		/* "-p[N]" open N tab pages */
1885 #ifdef TARGET_API_MAC_OSX
1886 		/* For some reason on MacOS X, an argument like:
1887 		   -psn_0_10223617 is passed in when invoke from Finder
1888 		   or with the 'open' command */
1889 		if (argv[0][argv_idx] == 's')
1890 		{
1891 		    argv_idx = -1; /* bypass full -psn */
1892 		    main_start_gui();
1893 		    break;
1894 		}
1895 #endif
1896 #ifdef FEAT_WINDOWS
1897 		/* default is 0: open window for each file */
1898 		parmp->window_count = get_number_arg((char_u *)argv[0],
1899 								&argv_idx, 0);
1900 		parmp->window_layout = WIN_TABS;
1901 #endif
1902 		break;
1903 
1904 	    case 'o':		/* "-o[N]" open N horizontal split windows */
1905 #ifdef FEAT_WINDOWS
1906 		/* default is 0: open window for each file */
1907 		parmp->window_count = get_number_arg((char_u *)argv[0],
1908 								&argv_idx, 0);
1909 		parmp->window_layout = WIN_HOR;
1910 #endif
1911 		break;
1912 
1913 		case 'O':	/* "-O[N]" open N vertical split windows */
1914 #if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1915 		/* default is 0: open window for each file */
1916 		parmp->window_count = get_number_arg((char_u *)argv[0],
1917 								&argv_idx, 0);
1918 		parmp->window_layout = WIN_VER;
1919 #endif
1920 		break;
1921 
1922 #ifdef FEAT_QUICKFIX
1923 	    case 'q':		/* "-q" QuickFix mode */
1924 		if (parmp->edit_type != EDIT_NONE)
1925 		    mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1926 		parmp->edit_type = EDIT_QF;
1927 		if (argv[0][argv_idx])		/* "-q{errorfile}" */
1928 		{
1929 		    parmp->use_ef = (char_u *)argv[0] + argv_idx;
1930 		    argv_idx = -1;
1931 		}
1932 		else if (argc > 1)		/* "-q {errorfile}" */
1933 		    want_argument = TRUE;
1934 		break;
1935 #endif
1936 
1937 	    case 'R':		/* "-R" readonly mode */
1938 		readonlymode = TRUE;
1939 		curbuf->b_p_ro = TRUE;
1940 		p_uc = 10000;			/* don't update very often */
1941 		break;
1942 
1943 	    case 'r':		/* "-r" recovery mode */
1944 	    case 'L':		/* "-L" recovery mode */
1945 		recoverymode = 1;
1946 		break;
1947 
1948 	    case 's':
1949 		if (exmode_active)	/* "-s" silent (batch) mode */
1950 		    silent_mode = TRUE;
1951 		else		/* "-s {scriptin}" read from script file */
1952 		    want_argument = TRUE;
1953 		break;
1954 
1955 	    case 't':		/* "-t {tag}" or "-t{tag}" jump to tag */
1956 		if (parmp->edit_type != EDIT_NONE)
1957 		    mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1958 		parmp->edit_type = EDIT_TAG;
1959 		if (argv[0][argv_idx])		/* "-t{tag}" */
1960 		{
1961 		    parmp->tagname = (char_u *)argv[0] + argv_idx;
1962 		    argv_idx = -1;
1963 		}
1964 		else				/* "-t {tag}" */
1965 		    want_argument = TRUE;
1966 		break;
1967 
1968 #ifdef FEAT_EVAL
1969 	    case 'D':		/* "-D"		Debugging */
1970 		parmp->use_debug_break_level = 9999;
1971 		break;
1972 #endif
1973 #ifdef FEAT_DIFF
1974 	    case 'd':		/* "-d"		'diff' */
1975 # ifdef AMIGA
1976 		/* check for "-dev {device}" */
1977 		if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1978 		    want_argument = TRUE;
1979 		else
1980 # endif
1981 		    parmp->diff_mode = TRUE;
1982 		break;
1983 #endif
1984 	    case 'V':		/* "-V{N}"	Verbose level */
1985 		/* default is 10: a little bit verbose */
1986 		p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1987 		if (argv[0][argv_idx] != NUL)
1988 		{
1989 		    set_option_value((char_u *)"verbosefile", 0L,
1990 					     (char_u *)argv[0] + argv_idx, 0);
1991 		    argv_idx = (int)STRLEN(argv[0]);
1992 		}
1993 		break;
1994 
1995 	    case 'v':		/* "-v"  Vi-mode (as if called "vi") */
1996 		exmode_active = 0;
1997 #ifdef FEAT_GUI
1998 		gui.starting = FALSE;	/* don't start GUI */
1999 #endif
2000 		break;
2001 
2002 	    case 'w':		/* "-w{number}"	set window height */
2003 				/* "-w {scriptout}"	write to script */
2004 		if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2005 		{
2006 		    n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2007 		    set_option_value((char_u *)"window", n, NULL, 0);
2008 		    break;
2009 		}
2010 		want_argument = TRUE;
2011 		break;
2012 
2013 #ifdef FEAT_CRYPT
2014 	    case 'x':		/* "-x"  encrypted reading/writing of files */
2015 		parmp->ask_for_key = TRUE;
2016 		break;
2017 #endif
2018 
2019 	    case 'X':		/* "-X"  don't connect to X server */
2020 #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2021 		x_no_connect = TRUE;
2022 #endif
2023 		break;
2024 
2025 	    case 'Z':		/* "-Z"  restricted mode */
2026 		restricted = TRUE;
2027 		break;
2028 
2029 	    case 'c':		/* "-c{command}" or "-c {command}" execute
2030 				   command */
2031 		if (argv[0][argv_idx] != NUL)
2032 		{
2033 		    if (parmp->n_commands >= MAX_ARG_CMDS)
2034 			mainerr(ME_EXTRA_CMD, NULL);
2035 		    parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2036 								   + argv_idx;
2037 		    argv_idx = -1;
2038 		    break;
2039 		}
2040 		/*FALLTHROUGH*/
2041 	    case 'S':		/* "-S {file}" execute Vim script */
2042 	    case 'i':		/* "-i {viminfo}" use for viminfo */
2043 #ifndef FEAT_DIFF
2044 	    case 'd':		/* "-d {device}" device (for Amiga) */
2045 #endif
2046 	    case 'T':		/* "-T {terminal}" terminal name */
2047 	    case 'u':		/* "-u {vimrc}" vim inits file */
2048 	    case 'U':		/* "-U {gvimrc}" gvim inits file */
2049 	    case 'W':		/* "-W {scriptout}" overwrite */
2050 #ifdef FEAT_GUI_W32
2051 	    case 'P':		/* "-P {parent title}" MDI parent */
2052 #endif
2053 		want_argument = TRUE;
2054 		break;
2055 
2056 	    default:
2057 		mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2058 	    }
2059 
2060 	    /*
2061 	     * Handle option arguments with argument.
2062 	     */
2063 	    if (want_argument)
2064 	    {
2065 		/*
2066 		 * Check for garbage immediately after the option letter.
2067 		 */
2068 		if (argv[0][argv_idx] != NUL)
2069 		    mainerr(ME_GARBAGE, (char_u *)argv[0]);
2070 
2071 		--argc;
2072 		if (argc < 1 && c != 'S')  /* -S has an optional argument */
2073 		    mainerr_arg_missing((char_u *)argv[0]);
2074 		++argv;
2075 		argv_idx = -1;
2076 
2077 		switch (c)
2078 		{
2079 		case 'c':	/* "-c {command}" execute command */
2080 		case 'S':	/* "-S {file}" execute Vim script */
2081 		    if (parmp->n_commands >= MAX_ARG_CMDS)
2082 			mainerr(ME_EXTRA_CMD, NULL);
2083 		    if (c == 'S')
2084 		    {
2085 			char	*a;
2086 
2087 			if (argc < 1)
2088 			    /* "-S" without argument: use default session file
2089 			     * name. */
2090 			    a = SESSION_FILE;
2091 			else if (argv[0][0] == '-')
2092 			{
2093 			    /* "-S" followed by another option: use default
2094 			     * session file name. */
2095 			    a = SESSION_FILE;
2096 			    ++argc;
2097 			    --argv;
2098 			}
2099 			else
2100 			    a = argv[0];
2101 			p = alloc((unsigned)(STRLEN(a) + 4));
2102 			if (p == NULL)
2103 			    mch_exit(2);
2104 			sprintf((char *)p, "so %s", a);
2105 			parmp->cmds_tofree[parmp->n_commands] = TRUE;
2106 			parmp->commands[parmp->n_commands++] = p;
2107 		    }
2108 		    else
2109 			parmp->commands[parmp->n_commands++] =
2110 							    (char_u *)argv[0];
2111 		    break;
2112 
2113 		case '-':
2114 		    if (argv[-1][2] == 'c')
2115 		    {
2116 			/* "--cmd {command}" execute command */
2117 			if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2118 			    mainerr(ME_EXTRA_CMD, NULL);
2119 			parmp->pre_commands[parmp->n_pre_commands++] =
2120 							    (char_u *)argv[0];
2121 		    }
2122 		    /* "--startuptime <file>" already handled */
2123 		    break;
2124 
2125 	    /*	case 'd':   -d {device} is handled in mch_check_win() for the
2126 	     *		    Amiga */
2127 
2128 #ifdef FEAT_QUICKFIX
2129 		case 'q':	/* "-q {errorfile}" QuickFix mode */
2130 		    parmp->use_ef = (char_u *)argv[0];
2131 		    break;
2132 #endif
2133 
2134 		case 'i':	/* "-i {viminfo}" use for viminfo */
2135 		    use_viminfo = (char_u *)argv[0];
2136 		    break;
2137 
2138 		case 's':	/* "-s {scriptin}" read from script file */
2139 		    if (scriptin[0] != NULL)
2140 		    {
2141 scripterror:
2142 			mch_errmsg(_("Attempt to open script file again: \""));
2143 			mch_errmsg(argv[-1]);
2144 			mch_errmsg(" ");
2145 			mch_errmsg(argv[0]);
2146 			mch_errmsg("\"\n");
2147 			mch_exit(2);
2148 		    }
2149 		    if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2150 		    {
2151 			mch_errmsg(_("Cannot open for reading: \""));
2152 			mch_errmsg(argv[0]);
2153 			mch_errmsg("\"\n");
2154 			mch_exit(2);
2155 		    }
2156 		    if (save_typebuf() == FAIL)
2157 			mch_exit(2);	/* out of memory */
2158 		    break;
2159 
2160 		case 't':	/* "-t {tag}" */
2161 		    parmp->tagname = (char_u *)argv[0];
2162 		    break;
2163 
2164 		case 'T':	/* "-T {terminal}" terminal name */
2165 		    /*
2166 		     * The -T term argument is always available and when
2167 		     * HAVE_TERMLIB is supported it overrides the environment
2168 		     * variable TERM.
2169 		     */
2170 #ifdef FEAT_GUI
2171 		    if (term_is_gui((char_u *)argv[0]))
2172 			gui.starting = TRUE;	/* start GUI a bit later */
2173 		    else
2174 #endif
2175 			parmp->term = (char_u *)argv[0];
2176 		    break;
2177 
2178 		case 'u':	/* "-u {vimrc}" vim inits file */
2179 		    parmp->use_vimrc = (char_u *)argv[0];
2180 		    break;
2181 
2182 		case 'U':	/* "-U {gvimrc}" gvim inits file */
2183 #ifdef FEAT_GUI
2184 		    use_gvimrc = (char_u *)argv[0];
2185 #endif
2186 		    break;
2187 
2188 		case 'w':	/* "-w {nr}" 'window' value */
2189 				/* "-w {scriptout}" append to script file */
2190 		    if (vim_isdigit(*((char_u *)argv[0])))
2191 		    {
2192 			argv_idx = 0;
2193 			n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2194 			set_option_value((char_u *)"window", n, NULL, 0);
2195 			argv_idx = -1;
2196 			break;
2197 		    }
2198 		    /*FALLTHROUGH*/
2199 		case 'W':	/* "-W {scriptout}" overwrite script file */
2200 		    if (scriptout != NULL)
2201 			goto scripterror;
2202 		    if ((scriptout = mch_fopen(argv[0],
2203 				    c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2204 		    {
2205 			mch_errmsg(_("Cannot open for script output: \""));
2206 			mch_errmsg(argv[0]);
2207 			mch_errmsg("\"\n");
2208 			mch_exit(2);
2209 		    }
2210 		    break;
2211 
2212 #ifdef FEAT_GUI_W32
2213 		case 'P':		/* "-P {parent title}" MDI parent */
2214 		    gui_mch_set_parent(argv[0]);
2215 		    break;
2216 #endif
2217 		}
2218 	    }
2219 	}
2220 
2221 	/*
2222 	 * File name argument.
2223 	 */
2224 	else
2225 	{
2226 	    argv_idx = -1;	    /* skip to next argument */
2227 
2228 	    /* Check for only one type of editing. */
2229 	    if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2230 		mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2231 	    parmp->edit_type = EDIT_FILE;
2232 
2233 #ifdef MSWIN
2234 	    /* Remember if the argument was a full path before changing
2235 	     * slashes to backslashes. */
2236 	    if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2237 		parmp->full_path = TRUE;
2238 #endif
2239 
2240 	    /* Add the file to the global argument list. */
2241 	    if (ga_grow(&global_alist.al_ga, 1) == FAIL
2242 		    || (p = vim_strsave((char_u *)argv[0])) == NULL)
2243 		mch_exit(2);
2244 #ifdef FEAT_DIFF
2245 	    if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2246 				      && !mch_isdir(alist_name(&GARGLIST[0])))
2247 	    {
2248 		char_u	    *r;
2249 
2250 		r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2251 		if (r != NULL)
2252 		{
2253 		    vim_free(p);
2254 		    p = r;
2255 		}
2256 	    }
2257 #endif
2258 #if defined(__CYGWIN32__) && !defined(WIN32)
2259 	    /*
2260 	     * If vim is invoked by non-Cygwin tools, convert away any
2261 	     * DOS paths, so things like .swp files are created correctly.
2262 	     * Look for evidence of non-Cygwin paths before we bother.
2263 	     * This is only for when using the Unix files.
2264 	     */
2265 	    if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
2266 	    {
2267 		char posix_path[PATH_MAX];
2268 
2269 # if CYGWIN_VERSION_DLL_MAJOR >= 1007
2270 		cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2271 # else
2272 		cygwin_conv_to_posix_path(p, posix_path);
2273 # endif
2274 		vim_free(p);
2275 		p = vim_strsave(posix_path);
2276 		if (p == NULL)
2277 		    mch_exit(2);
2278 	    }
2279 #endif
2280 
2281 #ifdef USE_FNAME_CASE
2282 	    /* Make the case of the file name match the actual file. */
2283 	    fname_case(p, 0);
2284 #endif
2285 
2286 	    alist_add(&global_alist, p,
2287 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2288 		    parmp->literal ? 2 : 0	/* add buffer nr after exp. */
2289 #else
2290 		    2		/* add buffer number now and use curbuf */
2291 #endif
2292 		    );
2293 
2294 #if defined(FEAT_MBYTE) && defined(WIN32)
2295 	    {
2296 		/* Remember this argument has been added to the argument list.
2297 		 * Needed when 'encoding' is changed. */
2298 		used_file_arg(argv[0], parmp->literal, parmp->full_path,
2299 # ifdef FEAT_DIFF
2300 							    parmp->diff_mode
2301 # else
2302 							    FALSE
2303 # endif
2304 							    );
2305 	    }
2306 #endif
2307 	}
2308 
2309 	/*
2310 	 * If there are no more letters after the current "-", go to next
2311 	 * argument.  argv_idx is set to -1 when the current argument is to be
2312 	 * skipped.
2313 	 */
2314 	if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2315 	{
2316 	    --argc;
2317 	    ++argv;
2318 	    argv_idx = 1;
2319 	}
2320     }
2321 
2322 #ifdef FEAT_EVAL
2323     /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2324      * one. */
2325     if (parmp->n_commands > 0)
2326     {
2327 	p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2328 	if (p != NULL)
2329 	{
2330 	    sprintf((char *)p, ":%s\r", parmp->commands[0]);
2331 	    set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2332 	    vim_free(p);
2333 	}
2334     }
2335 #endif
2336 }
2337 
2338 /*
2339  * Print a warning if stdout is not a terminal.
2340  * When starting in Ex mode and commands come from a file, set Silent mode.
2341  */
2342     static void
2343 check_tty(parmp)
2344     mparm_T	*parmp;
2345 {
2346     int		input_isatty;		/* is active input a terminal? */
2347 
2348     input_isatty = mch_input_isatty();
2349     if (exmode_active)
2350     {
2351 	if (!input_isatty)
2352 	    silent_mode = TRUE;
2353     }
2354     else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2355 #ifdef FEAT_GUI
2356 	    /* don't want the delay when started from the desktop */
2357 	    && !gui.starting
2358 #endif
2359 	    )
2360     {
2361 #ifdef NBDEBUG
2362 	/*
2363 	 * This shouldn't be necessary. But if I run netbeans with the log
2364 	 * output coming to the console and XOpenDisplay fails, I get vim
2365 	 * trying to start with input/output to my console tty.  This fills my
2366 	 * input buffer so fast I can't even kill the process in under 2
2367 	 * minutes (and it beeps continuously the whole time :-)
2368 	 */
2369 	if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2370 	{
2371 	    mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2372 	    exit(1);
2373 	}
2374 #endif
2375 	if (!parmp->stdout_isatty)
2376 	    mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2377 	if (!input_isatty)
2378 	    mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2379 	out_flush();
2380 	if (scriptin[0] == NULL)
2381 	    ui_delay(2000L, TRUE);
2382 	TIME_MSG("Warning delay");
2383     }
2384 }
2385 
2386 /*
2387  * Read text from stdin.
2388  */
2389     static void
2390 read_stdin()
2391 {
2392     int	    i;
2393 
2394 #if defined(HAS_SWAP_EXISTS_ACTION)
2395     /* When getting the ATTENTION prompt here, use a dialog */
2396     swap_exists_action = SEA_DIALOG;
2397 #endif
2398     no_wait_return = TRUE;
2399     i = msg_didany;
2400     set_buflisted(TRUE);
2401     (void)open_buffer(TRUE, NULL);	/* create memfile and read file */
2402     no_wait_return = FALSE;
2403     msg_didany = i;
2404     TIME_MSG("reading stdin");
2405 #if defined(HAS_SWAP_EXISTS_ACTION)
2406     check_swap_exists_action();
2407 #endif
2408 #if !(defined(AMIGA) || defined(MACOS))
2409     /*
2410      * Close stdin and dup it from stderr.  Required for GPM to work
2411      * properly, and for running external commands.
2412      * Is there any other system that cannot do this?
2413      */
2414     close(0);
2415     ignored = dup(2);
2416 #endif
2417 }
2418 
2419 /*
2420  * Create the requested number of windows and edit buffers in them.
2421  * Also does recovery if "recoverymode" set.
2422  */
2423     static void
2424 create_windows(parmp)
2425     mparm_T	*parmp UNUSED;
2426 {
2427 #ifdef FEAT_WINDOWS
2428     int		dorewind;
2429     int		done = 0;
2430 
2431     /*
2432      * Create the number of windows that was requested.
2433      */
2434     if (parmp->window_count == -1)	/* was not set */
2435 	parmp->window_count = 1;
2436     if (parmp->window_count == 0)
2437 	parmp->window_count = GARGCOUNT;
2438     if (parmp->window_count > 1)
2439     {
2440 	/* Don't change the windows if there was a command in .vimrc that
2441 	 * already split some windows */
2442 	if (parmp->window_layout == 0)
2443 	    parmp->window_layout = WIN_HOR;
2444 	if (parmp->window_layout == WIN_TABS)
2445 	{
2446 	    parmp->window_count = make_tabpages(parmp->window_count);
2447 	    TIME_MSG("making tab pages");
2448 	}
2449 	else if (firstwin->w_next == NULL)
2450 	{
2451 	    parmp->window_count = make_windows(parmp->window_count,
2452 					     parmp->window_layout == WIN_VER);
2453 	    TIME_MSG("making windows");
2454 	}
2455 	else
2456 	    parmp->window_count = win_count();
2457     }
2458     else
2459 	parmp->window_count = 1;
2460 #endif
2461 
2462     if (recoverymode)			/* do recover */
2463     {
2464 	msg_scroll = TRUE;		/* scroll message up */
2465 	ml_recover();
2466 	if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2467 	    getout(1);
2468 	do_modelines(0);		/* do modelines */
2469     }
2470     else
2471     {
2472 	/*
2473 	 * Open a buffer for windows that don't have one yet.
2474 	 * Commands in the .vimrc might have loaded a file or split the window.
2475 	 * Watch out for autocommands that delete a window.
2476 	 */
2477 #ifdef FEAT_AUTOCMD
2478 	/*
2479 	 * Don't execute Win/Buf Enter/Leave autocommands here
2480 	 */
2481 	++autocmd_no_enter;
2482 	++autocmd_no_leave;
2483 #endif
2484 #ifdef FEAT_WINDOWS
2485 	dorewind = TRUE;
2486 	while (done++ < 1000)
2487 	{
2488 	    if (dorewind)
2489 	    {
2490 		if (parmp->window_layout == WIN_TABS)
2491 		    goto_tabpage(1);
2492 		else
2493 		    curwin = firstwin;
2494 	    }
2495 	    else if (parmp->window_layout == WIN_TABS)
2496 	    {
2497 		if (curtab->tp_next == NULL)
2498 		    break;
2499 		goto_tabpage(0);
2500 	    }
2501 	    else
2502 	    {
2503 		if (curwin->w_next == NULL)
2504 		    break;
2505 		curwin = curwin->w_next;
2506 	    }
2507 	    dorewind = FALSE;
2508 #endif
2509 	    curbuf = curwin->w_buffer;
2510 	    if (curbuf->b_ml.ml_mfp == NULL)
2511 	    {
2512 #ifdef FEAT_FOLDING
2513 		/* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2514 		if (p_fdls >= 0)
2515 		    curwin->w_p_fdl = p_fdls;
2516 #endif
2517 #if defined(HAS_SWAP_EXISTS_ACTION)
2518 		/* When getting the ATTENTION prompt here, use a dialog */
2519 		swap_exists_action = SEA_DIALOG;
2520 #endif
2521 		set_buflisted(TRUE);
2522 		(void)open_buffer(FALSE, NULL); /* create memfile, read file */
2523 
2524 #if defined(HAS_SWAP_EXISTS_ACTION)
2525 		if (swap_exists_action == SEA_QUIT)
2526 		{
2527 		    if (got_int || only_one_window())
2528 		    {
2529 			/* abort selected or quit and only one window */
2530 			did_emsg = FALSE;   /* avoid hit-enter prompt */
2531 			getout(1);
2532 		    }
2533 		    /* We can't close the window, it would disturb what
2534 		     * happens next.  Clear the file name and set the arg
2535 		     * index to -1 to delete it later. */
2536 		    setfname(curbuf, NULL, NULL, FALSE);
2537 		    curwin->w_arg_idx = -1;
2538 		    swap_exists_action = SEA_NONE;
2539 		}
2540 		else
2541 		    handle_swap_exists(NULL);
2542 #endif
2543 #ifdef FEAT_AUTOCMD
2544 		dorewind = TRUE;		/* start again */
2545 #endif
2546 	    }
2547 #ifdef FEAT_WINDOWS
2548 	    ui_breakcheck();
2549 	    if (got_int)
2550 	    {
2551 		(void)vgetc();	/* only break the file loading, not the rest */
2552 		break;
2553 	    }
2554 	}
2555 #endif
2556 #ifdef FEAT_WINDOWS
2557 	if (parmp->window_layout == WIN_TABS)
2558 	    goto_tabpage(1);
2559 	else
2560 	    curwin = firstwin;
2561 	curbuf = curwin->w_buffer;
2562 #endif
2563 #ifdef FEAT_AUTOCMD
2564 	--autocmd_no_enter;
2565 	--autocmd_no_leave;
2566 #endif
2567     }
2568 }
2569 
2570 #ifdef FEAT_WINDOWS
2571     /*
2572      * If opened more than one window, start editing files in the other
2573      * windows.  make_windows() has already opened the windows.
2574      */
2575     static void
2576 edit_buffers(parmp)
2577     mparm_T	*parmp;
2578 {
2579     int		arg_idx;		/* index in argument list */
2580     int		i;
2581     int		advance = TRUE;
2582 
2583 # ifdef FEAT_AUTOCMD
2584     /*
2585      * Don't execute Win/Buf Enter/Leave autocommands here
2586      */
2587     ++autocmd_no_enter;
2588     ++autocmd_no_leave;
2589 # endif
2590 
2591     /* When w_arg_idx is -1 remove the window (see create_windows()). */
2592     if (curwin->w_arg_idx == -1)
2593     {
2594 	win_close(curwin, TRUE);
2595 	advance = FALSE;
2596     }
2597 
2598     arg_idx = 1;
2599     for (i = 1; i < parmp->window_count; ++i)
2600     {
2601 	/* When w_arg_idx is -1 remove the window (see create_windows()). */
2602 	if (curwin->w_arg_idx == -1)
2603 	{
2604 	    ++arg_idx;
2605 	    win_close(curwin, TRUE);
2606 	    advance = FALSE;
2607 	    continue;
2608 	}
2609 
2610 	if (advance)
2611 	{
2612 	    if (parmp->window_layout == WIN_TABS)
2613 	    {
2614 		if (curtab->tp_next == NULL)	/* just checking */
2615 		    break;
2616 		goto_tabpage(0);
2617 	    }
2618 	    else
2619 	    {
2620 		if (curwin->w_next == NULL)	/* just checking */
2621 		    break;
2622 		win_enter(curwin->w_next, FALSE);
2623 	    }
2624 	}
2625 	advance = TRUE;
2626 
2627 	/* Only open the file if there is no file in this window yet (that can
2628 	 * happen when .vimrc contains ":sall"). */
2629 	if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2630 	{
2631 	    curwin->w_arg_idx = arg_idx;
2632 	    /* Edit file from arg list, if there is one.  When "Quit" selected
2633 	     * at the ATTENTION prompt close the window. */
2634 # ifdef HAS_SWAP_EXISTS_ACTION
2635 	    swap_exists_did_quit = FALSE;
2636 # endif
2637 	    (void)do_ecmd(0, arg_idx < GARGCOUNT
2638 			  ? alist_name(&GARGLIST[arg_idx]) : NULL,
2639 			  NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
2640 # ifdef HAS_SWAP_EXISTS_ACTION
2641 	    if (swap_exists_did_quit)
2642 	    {
2643 		/* abort or quit selected */
2644 		if (got_int || only_one_window())
2645 		{
2646 		    /* abort selected and only one window */
2647 		    did_emsg = FALSE;   /* avoid hit-enter prompt */
2648 		    getout(1);
2649 		}
2650 		win_close(curwin, TRUE);
2651 		advance = FALSE;
2652 	    }
2653 # endif
2654 	    if (arg_idx == GARGCOUNT - 1)
2655 		arg_had_last = TRUE;
2656 	    ++arg_idx;
2657 	}
2658 	ui_breakcheck();
2659 	if (got_int)
2660 	{
2661 	    (void)vgetc();	/* only break the file loading, not the rest */
2662 	    break;
2663 	}
2664     }
2665 
2666     if (parmp->window_layout == WIN_TABS)
2667 	goto_tabpage(1);
2668 # ifdef FEAT_AUTOCMD
2669     --autocmd_no_enter;
2670 # endif
2671     win_enter(firstwin, FALSE);		/* back to first window */
2672 # ifdef FEAT_AUTOCMD
2673     --autocmd_no_leave;
2674 # endif
2675     TIME_MSG("editing files in windows");
2676     if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
2677 	win_equal(curwin, FALSE, 'b');	/* adjust heights */
2678 }
2679 #endif /* FEAT_WINDOWS */
2680 
2681 /*
2682  * Execute the commands from --cmd arguments "cmds[cnt]".
2683  */
2684     static void
2685 exe_pre_commands(parmp)
2686     mparm_T	*parmp;
2687 {
2688     char_u	**cmds = parmp->pre_commands;
2689     int		cnt = parmp->n_pre_commands;
2690     int		i;
2691 
2692     if (cnt > 0)
2693     {
2694 	curwin->w_cursor.lnum = 0; /* just in case.. */
2695 	sourcing_name = (char_u *)_("pre-vimrc command line");
2696 # ifdef FEAT_EVAL
2697 	current_SID = SID_CMDARG;
2698 # endif
2699 	for (i = 0; i < cnt; ++i)
2700 	    do_cmdline_cmd(cmds[i]);
2701 	sourcing_name = NULL;
2702 # ifdef FEAT_EVAL
2703 	current_SID = 0;
2704 # endif
2705 	TIME_MSG("--cmd commands");
2706     }
2707 }
2708 
2709 /*
2710  * Execute "+", "-c" and "-S" arguments.
2711  */
2712     static void
2713 exe_commands(parmp)
2714     mparm_T	*parmp;
2715 {
2716     int		i;
2717 
2718     /*
2719      * We start commands on line 0, make "vim +/pat file" match a
2720      * pattern on line 1.  But don't move the cursor when an autocommand
2721      * with g`" was used.
2722      */
2723     msg_scroll = TRUE;
2724     if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2725 	curwin->w_cursor.lnum = 0;
2726     sourcing_name = (char_u *)"command line";
2727 #ifdef FEAT_EVAL
2728     current_SID = SID_CARG;
2729 #endif
2730     for (i = 0; i < parmp->n_commands; ++i)
2731     {
2732 	do_cmdline_cmd(parmp->commands[i]);
2733 	if (parmp->cmds_tofree[i])
2734 	    vim_free(parmp->commands[i]);
2735     }
2736     sourcing_name = NULL;
2737 #ifdef FEAT_EVAL
2738     current_SID = 0;
2739 #endif
2740     if (curwin->w_cursor.lnum == 0)
2741 	curwin->w_cursor.lnum = 1;
2742 
2743     if (!exmode_active)
2744 	msg_scroll = FALSE;
2745 
2746 #ifdef FEAT_QUICKFIX
2747     /* When started with "-q errorfile" jump to first error again. */
2748     if (parmp->edit_type == EDIT_QF)
2749 	qf_jump(NULL, 0, 0, FALSE);
2750 #endif
2751     TIME_MSG("executing command arguments");
2752 }
2753 
2754 /*
2755  * Source startup scripts.
2756  */
2757     static void
2758 source_startup_scripts(parmp)
2759     mparm_T	*parmp;
2760 {
2761     int		i;
2762 
2763     /*
2764      * For "evim" source evim.vim first of all, so that the user can overrule
2765      * any things he doesn't like.
2766      */
2767     if (parmp->evim_mode)
2768     {
2769 	(void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
2770 	TIME_MSG("source evim file");
2771     }
2772 
2773     /*
2774      * If -u argument given, use only the initializations from that file and
2775      * nothing else.
2776      */
2777     if (parmp->use_vimrc != NULL)
2778     {
2779 	if (STRCMP(parmp->use_vimrc, "NONE") == 0
2780 				     || STRCMP(parmp->use_vimrc, "NORC") == 0)
2781 	{
2782 #ifdef FEAT_GUI
2783 	    if (use_gvimrc == NULL)	    /* don't load gvimrc either */
2784 		use_gvimrc = parmp->use_vimrc;
2785 #endif
2786 	    if (parmp->use_vimrc[2] == 'N')
2787 		p_lpl = FALSE;		    /* don't load plugins either */
2788 	}
2789 	else
2790 	{
2791 	    if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
2792 		EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2793 	}
2794     }
2795     else if (!silent_mode)
2796     {
2797 #ifdef AMIGA
2798 	struct Process	*proc = (struct Process *)FindTask(0L);
2799 	APTR		save_winptr = proc->pr_WindowPtr;
2800 
2801 	/* Avoid a requester here for a volume that doesn't exist. */
2802 	proc->pr_WindowPtr = (APTR)-1L;
2803 #endif
2804 
2805 	/*
2806 	 * Get system wide defaults, if the file name is defined.
2807 	 */
2808 #ifdef SYS_VIMRC_FILE
2809 	(void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
2810 #endif
2811 #ifdef MACOS_X
2812 	(void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
2813 #endif
2814 
2815 	/*
2816 	 * Try to read initialization commands from the following places:
2817 	 * - environment variable VIMINIT
2818 	 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2819 	 * - second user vimrc file ($VIM/.vimrc for Dos)
2820 	 * - environment variable EXINIT
2821 	 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2822 	 * - second user exrc file ($VIM/.exrc for Dos)
2823 	 * The first that exists is used, the rest is ignored.
2824 	 */
2825 	if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2826 	{
2827 	    if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
2828 #ifdef USR_VIMRC_FILE2
2829 		&& do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2830 							   DOSO_VIMRC) == FAIL
2831 #endif
2832 #ifdef USR_VIMRC_FILE3
2833 		&& do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2834 							   DOSO_VIMRC) == FAIL
2835 #endif
2836 		&& process_env((char_u *)"EXINIT", FALSE) == FAIL
2837 		&& do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
2838 	    {
2839 #ifdef USR_EXRC_FILE2
2840 		(void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
2841 #endif
2842 	    }
2843 	}
2844 
2845 	/*
2846 	 * Read initialization commands from ".vimrc" or ".exrc" in current
2847 	 * directory.  This is only done if the 'exrc' option is set.
2848 	 * Because of security reasons we disallow shell and write commands
2849 	 * now, except for unix if the file is owned by the user or 'secure'
2850 	 * option has been reset in environment of global ".exrc" or ".vimrc".
2851 	 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2852 	 * SYS_VIMRC_FILE.
2853 	 */
2854 	if (p_exrc)
2855 	{
2856 #if defined(UNIX) || defined(VMS)
2857 	    /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2858 	    if (!file_owned(VIMRC_FILE))
2859 #endif
2860 		secure = p_secure;
2861 
2862 	    i = FAIL;
2863 	    if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2864 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2865 #ifdef USR_VIMRC_FILE2
2866 		    && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2867 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2868 #endif
2869 #ifdef USR_VIMRC_FILE3
2870 		    && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2871 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2872 #endif
2873 #ifdef SYS_VIMRC_FILE
2874 		    && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2875 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2876 #endif
2877 				)
2878 		i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
2879 
2880 	    if (i == FAIL)
2881 	    {
2882 #if defined(UNIX) || defined(VMS)
2883 		/* if ".exrc" is not owned by user set 'secure' mode */
2884 		if (!file_owned(EXRC_FILE))
2885 		    secure = p_secure;
2886 		else
2887 		    secure = 0;
2888 #endif
2889 		if (	   fullpathcmp((char_u *)USR_EXRC_FILE,
2890 				      (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2891 #ifdef USR_EXRC_FILE2
2892 			&& fullpathcmp((char_u *)USR_EXRC_FILE2,
2893 				      (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2894 #endif
2895 				)
2896 		    (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
2897 	    }
2898 	}
2899 	if (secure == 2)
2900 	    need_wait_return = TRUE;
2901 	secure = 0;
2902 #ifdef AMIGA
2903 	proc->pr_WindowPtr = save_winptr;
2904 #endif
2905     }
2906     TIME_MSG("sourcing vimrc file(s)");
2907 }
2908 
2909 /*
2910  * Setup to start using the GUI.  Exit with an error when not available.
2911  */
2912     static void
2913 main_start_gui()
2914 {
2915 #ifdef FEAT_GUI
2916     gui.starting = TRUE;	/* start GUI a bit later */
2917 #else
2918     mch_errmsg(_(e_nogvim));
2919     mch_errmsg("\n");
2920     mch_exit(2);
2921 #endif
2922 }
2923 
2924 /*
2925  * Get an environment variable, and execute it as Ex commands.
2926  * Returns FAIL if the environment variable was not executed, OK otherwise.
2927  */
2928     int
2929 process_env(env, is_viminit)
2930     char_u	*env;
2931     int		is_viminit; /* when TRUE, called for VIMINIT */
2932 {
2933     char_u	*initstr;
2934     char_u	*save_sourcing_name;
2935     linenr_T	save_sourcing_lnum;
2936 #ifdef FEAT_EVAL
2937     scid_T	save_sid;
2938 #endif
2939 
2940     if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2941     {
2942 	if (is_viminit)
2943 	    vimrc_found(NULL, NULL);
2944 	save_sourcing_name = sourcing_name;
2945 	save_sourcing_lnum = sourcing_lnum;
2946 	sourcing_name = env;
2947 	sourcing_lnum = 0;
2948 #ifdef FEAT_EVAL
2949 	save_sid = current_SID;
2950 	current_SID = SID_ENV;
2951 #endif
2952 	do_cmdline_cmd(initstr);
2953 	sourcing_name = save_sourcing_name;
2954 	sourcing_lnum = save_sourcing_lnum;
2955 #ifdef FEAT_EVAL
2956 	current_SID = save_sid;;
2957 #endif
2958 	return OK;
2959     }
2960     return FAIL;
2961 }
2962 
2963 #if defined(UNIX) || defined(VMS)
2964 /*
2965  * Return TRUE if we are certain the user owns the file "fname".
2966  * Used for ".vimrc" and ".exrc".
2967  * Use both stat() and lstat() for extra security.
2968  */
2969     static int
2970 file_owned(fname)
2971     char	*fname;
2972 {
2973     struct stat s;
2974 # ifdef UNIX
2975     uid_t	uid = getuid();
2976 # else	 /* VMS */
2977     uid_t	uid = ((getgid() << 16) | getuid());
2978 # endif
2979 
2980     return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2981 # ifdef HAVE_LSTAT
2982 	    || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2983 # endif
2984 	    );
2985 }
2986 #endif
2987 
2988 /*
2989  * Give an error message main_errors["n"] and exit.
2990  */
2991     static void
2992 mainerr(n, str)
2993     int		n;	/* one of the ME_ defines */
2994     char_u	*str;	/* extra argument or NULL */
2995 {
2996 #if defined(UNIX) || defined(__EMX__) || defined(VMS)
2997     reset_signals();		/* kill us with CTRL-C here, if you like */
2998 #endif
2999 
3000     mch_errmsg(longVersion);
3001     mch_errmsg("\n");
3002     mch_errmsg(_(main_errors[n]));
3003     if (str != NULL)
3004     {
3005 	mch_errmsg(": \"");
3006 	mch_errmsg((char *)str);
3007 	mch_errmsg("\"");
3008     }
3009     mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
3010 
3011     mch_exit(1);
3012 }
3013 
3014     void
3015 mainerr_arg_missing(str)
3016     char_u	*str;
3017 {
3018     mainerr(ME_ARG_MISSING, str);
3019 }
3020 
3021 /*
3022  * print a message with three spaces prepended and '\n' appended.
3023  */
3024     static void
3025 main_msg(s)
3026     char *s;
3027 {
3028     mch_msg("   ");
3029     mch_msg(s);
3030     mch_msg("\n");
3031 }
3032 
3033 /*
3034  * Print messages for "vim -h" or "vim --help" and exit.
3035  */
3036     static void
3037 usage()
3038 {
3039     int		i;
3040     static char	*(use[]) =
3041     {
3042 	N_("[file ..]       edit specified file(s)"),
3043 	N_("-               read text from stdin"),
3044 	N_("-t tag          edit file where tag is defined"),
3045 #ifdef FEAT_QUICKFIX
3046 	N_("-q [errorfile]  edit file with first error")
3047 #endif
3048     };
3049 
3050 #if defined(UNIX) || defined(__EMX__) || defined(VMS)
3051     reset_signals();		/* kill us with CTRL-C here, if you like */
3052 #endif
3053 
3054     mch_msg(longVersion);
3055     mch_msg(_("\n\nusage:"));
3056     for (i = 0; ; ++i)
3057     {
3058 	mch_msg(_(" vim [arguments] "));
3059 	mch_msg(_(use[i]));
3060 	if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3061 	    break;
3062 	mch_msg(_("\n   or:"));
3063     }
3064 #ifdef VMS
3065     mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
3066 #endif
3067 
3068     mch_msg(_("\n\nArguments:\n"));
3069     main_msg(_("--\t\t\tOnly file names after this"));
3070 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3071     main_msg(_("--literal\t\tDon't expand wildcards"));
3072 #endif
3073 #ifdef FEAT_OLE
3074     main_msg(_("-register\t\tRegister this gvim for OLE"));
3075     main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3076 #endif
3077 #ifdef FEAT_GUI
3078     main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3079     main_msg(_("-f  or  --nofork\tForeground: Don't fork when starting GUI"));
3080 #endif
3081     main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3082     main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3083     main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3084 #ifdef FEAT_DIFF
3085     main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3086 #endif
3087     main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3088     main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3089     main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3090     main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3091     main_msg(_("-M\t\t\tModifications in text not allowed"));
3092     main_msg(_("-b\t\t\tBinary mode"));
3093 #ifdef FEAT_LISP
3094     main_msg(_("-l\t\t\tLisp mode"));
3095 #endif
3096     main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3097     main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
3098     main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3099 #ifdef FEAT_EVAL
3100     main_msg(_("-D\t\t\tDebugging mode"));
3101 #endif
3102     main_msg(_("-n\t\t\tNo swap file, use memory only"));
3103     main_msg(_("-r\t\t\tList swap files and exit"));
3104     main_msg(_("-r (with file name)\tRecover crashed session"));
3105     main_msg(_("-L\t\t\tSame as -r"));
3106 #ifdef AMIGA
3107     main_msg(_("-f\t\t\tDon't use newcli to open window"));
3108     main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3109 #endif
3110 #ifdef FEAT_ARABIC
3111     main_msg(_("-A\t\t\tstart in Arabic mode"));
3112 #endif
3113 #ifdef FEAT_RIGHTLEFT
3114     main_msg(_("-H\t\t\tStart in Hebrew mode"));
3115 #endif
3116 #ifdef FEAT_FKMAP
3117     main_msg(_("-F\t\t\tStart in Farsi mode"));
3118 #endif
3119     main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3120     main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3121 #ifdef FEAT_GUI
3122     main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3123 #endif
3124     main_msg(_("--noplugin\t\tDon't load plugin scripts"));
3125 #ifdef FEAT_WINDOWS
3126     main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
3127     main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3128     main_msg(_("-O[N]\t\tLike -o but split vertically"));
3129 #endif
3130     main_msg(_("+\t\t\tStart at end of file"));
3131     main_msg(_("+<lnum>\t\tStart at line <lnum>"));
3132     main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
3133     main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3134     main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3135     main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3136     main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3137     main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3138 #ifdef FEAT_CRYPT
3139     main_msg(_("-x\t\t\tEdit encrypted files"));
3140 #endif
3141 #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3142 # if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3143     main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3144 # endif
3145     main_msg(_("-X\t\t\tDo not connect to X server"));
3146 #endif
3147 #ifdef FEAT_CLIENTSERVER
3148     main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3149     main_msg(_("--remote-silent <files>  Same, don't complain if there is no server"));
3150     main_msg(_("--remote-wait <files>  As --remote but wait for files to have been edited"));
3151     main_msg(_("--remote-wait-silent <files>  Same, don't complain if there is no server"));
3152 # ifdef FEAT_WINDOWS
3153     main_msg(_("--remote-tab[-wait][-silent] <files>  As --remote but use tab page per file"));
3154 # endif
3155     main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3156     main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3157     main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3158     main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3159 #endif
3160 #ifdef STARTUPTIME
3161     main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
3162 #endif
3163 #ifdef FEAT_VIMINFO
3164     main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3165 #endif
3166     main_msg(_("-h  or  --help\tPrint Help (this message) and exit"));
3167     main_msg(_("--version\t\tPrint version information and exit"));
3168 
3169 #ifdef FEAT_GUI_X11
3170 # ifdef FEAT_GUI_MOTIF
3171     mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3172 # else
3173 #  ifdef FEAT_GUI_ATHENA
3174 #   ifdef FEAT_GUI_NEXTAW
3175     mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3176 #   else
3177     mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3178 #   endif
3179 #  endif
3180 # endif
3181     main_msg(_("-display <display>\tRun vim on <display>"));
3182     main_msg(_("-iconic\t\tStart vim iconified"));
3183 # if 0
3184     main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
3185     mch_msg(_("\t\t\t  (Unimplemented)\n"));
3186 # endif
3187     main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3188     main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3189     main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3190     main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3191     main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3192     main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3193     main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3194     main_msg(_("-scrollbarwidth <width>  Use a scrollbar width of <width> (also: -sw)"));
3195 # ifdef FEAT_GUI_ATHENA
3196     main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3197 # endif
3198     main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3199     main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3200     main_msg(_("-xrm <resource>\tSet the specified resource"));
3201 #endif /* FEAT_GUI_X11 */
3202 #if defined(FEAT_GUI) && defined(RISCOS)
3203     mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3204     main_msg(_("--columns <number>\tInitial width of window in columns"));
3205     main_msg(_("--rows <number>\tInitial height of window in rows"));
3206 #endif
3207 #ifdef FEAT_GUI_GTK
3208     mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3209     main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3210     main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3211     main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3212     main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
3213 # ifdef HAVE_GTK2
3214     main_msg(_("--role <role>\tSet a unique role to identify the main window"));
3215 # endif
3216     main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3217 #endif
3218 #ifdef FEAT_GUI_W32
3219     main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3220     main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3221 #endif
3222 
3223 #ifdef FEAT_GUI_GNOME
3224     /* Gnome gives extra messages for --help if we continue, but not for -h. */
3225     if (gui.starting)
3226 	mch_msg("\n");
3227     else
3228 #endif
3229 	mch_exit(0);
3230 }
3231 
3232 #if defined(HAS_SWAP_EXISTS_ACTION)
3233 /*
3234  * Check the result of the ATTENTION dialog:
3235  * When "Quit" selected, exit Vim.
3236  * When "Recover" selected, recover the file.
3237  */
3238     static void
3239 check_swap_exists_action()
3240 {
3241     if (swap_exists_action == SEA_QUIT)
3242 	getout(1);
3243     handle_swap_exists(NULL);
3244 }
3245 #endif
3246 
3247 #if defined(STARTUPTIME) || defined(PROTO)
3248 static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3249 
3250 static struct timeval	prev_timeval;
3251 
3252 # ifdef WIN3264
3253 /*
3254  * Windows doesn't have gettimeofday(), although it does have struct timeval.
3255  */
3256     static int
3257 gettimeofday(struct timeval *tv, char *dummy)
3258 {
3259     long t = clock();
3260     tv->tv_sec = t / CLOCKS_PER_SEC;
3261     tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3262     return 0;
3263 }
3264 # endif
3265 
3266 /*
3267  * Save the previous time before doing something that could nest.
3268  * set "*tv_rel" to the time elapsed so far.
3269  */
3270     void
3271 time_push(tv_rel, tv_start)
3272     void	*tv_rel, *tv_start;
3273 {
3274     *((struct timeval *)tv_rel) = prev_timeval;
3275     gettimeofday(&prev_timeval, NULL);
3276     ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3277 					- ((struct timeval *)tv_rel)->tv_usec;
3278     ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3279 					 - ((struct timeval *)tv_rel)->tv_sec;
3280     if (((struct timeval *)tv_rel)->tv_usec < 0)
3281     {
3282 	((struct timeval *)tv_rel)->tv_usec += 1000000;
3283 	--((struct timeval *)tv_rel)->tv_sec;
3284     }
3285     *(struct timeval *)tv_start = prev_timeval;
3286 }
3287 
3288 /*
3289  * Compute the previous time after doing something that could nest.
3290  * Subtract "*tp" from prev_timeval;
3291  * Note: The arguments are (void *) to avoid trouble with systems that don't
3292  * have struct timeval.
3293  */
3294     void
3295 time_pop(tp)
3296     void	*tp;	/* actually (struct timeval *) */
3297 {
3298     prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3299     prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3300     if (prev_timeval.tv_usec < 0)
3301     {
3302 	prev_timeval.tv_usec += 1000000;
3303 	--prev_timeval.tv_sec;
3304     }
3305 }
3306 
3307     static void
3308 time_diff(then, now)
3309     struct timeval	*then;
3310     struct timeval	*now;
3311 {
3312     long	usec;
3313     long	msec;
3314 
3315     usec = now->tv_usec - then->tv_usec;
3316     msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3317     usec = usec % 1000L;
3318     fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3319 }
3320 
3321     void
3322 time_msg(msg, tv_start)
3323     char	*msg;
3324     void	*tv_start;  /* only for do_source: start time; actually
3325 			       (struct timeval *) */
3326 {
3327     static struct timeval	start;
3328     struct timeval		now;
3329 
3330     if (time_fd != NULL)
3331     {
3332 	if (strstr(msg, "STARTING") != NULL)
3333 	{
3334 	    gettimeofday(&start, NULL);
3335 	    prev_timeval = start;
3336 	    fprintf(time_fd, "\n\ntimes in msec\n");
3337 	    fprintf(time_fd, " clock   self+sourced   self:  sourced script\n");
3338 	    fprintf(time_fd, " clock   elapsed:              other lines\n\n");
3339 	}
3340 	gettimeofday(&now, NULL);
3341 	time_diff(&start, &now);
3342 	if (((struct timeval *)tv_start) != NULL)
3343 	{
3344 	    fprintf(time_fd, "  ");
3345 	    time_diff(((struct timeval *)tv_start), &now);
3346 	}
3347 	fprintf(time_fd, "  ");
3348 	time_diff(&prev_timeval, &now);
3349 	prev_timeval = now;
3350 	fprintf(time_fd, ": %s\n", msg);
3351     }
3352 }
3353 
3354 #endif
3355 
3356 #if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3357 
3358 /*
3359  * Common code for the X command server and the Win32 command server.
3360  */
3361 
3362 static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
3363 
3364 /*
3365  * Do the client-server stuff, unless "--servername ''" was used.
3366  */
3367     static void
3368 exec_on_server(parmp)
3369     mparm_T	*parmp;
3370 {
3371     if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3372     {
3373 # ifdef WIN32
3374 	/* Initialise the client/server messaging infrastructure. */
3375 	serverInitMessaging();
3376 # endif
3377 
3378 	/*
3379 	 * When a command server argument was found, execute it.  This may
3380 	 * exit Vim when it was successful.  Otherwise it's executed further
3381 	 * on.  Remember the encoding used here in "serverStrEnc".
3382 	 */
3383 	if (parmp->serverArg)
3384 	{
3385 	    cmdsrv_main(&parmp->argc, parmp->argv,
3386 				    parmp->serverName_arg, &parmp->serverStr);
3387 # ifdef FEAT_MBYTE
3388 	    parmp->serverStrEnc = vim_strsave(p_enc);
3389 # endif
3390 	}
3391 
3392 	/* If we're still running, get the name to register ourselves.
3393 	 * On Win32 can register right now, for X11 need to setup the
3394 	 * clipboard first, it's further down. */
3395 	parmp->servername = serverMakeName(parmp->serverName_arg,
3396 							      parmp->argv[0]);
3397 # ifdef WIN32
3398 	if (parmp->servername != NULL)
3399 	{
3400 	    serverSetName(parmp->servername);
3401 	    vim_free(parmp->servername);
3402 	}
3403 # endif
3404     }
3405 }
3406 
3407 /*
3408  * Prepare for running as a Vim server.
3409  */
3410     static void
3411 prepare_server(parmp)
3412     mparm_T	*parmp;
3413 {
3414 # if defined(FEAT_X11)
3415     /*
3416      * Register for remote command execution with :serversend and --remote
3417      * unless there was a -X or a --servername '' on the command line.
3418      * Only register nongui-vim's with an explicit --servername argument.
3419      * When running as root --servername is also required.
3420      */
3421     if (X_DISPLAY != NULL && parmp->servername != NULL && (
3422 #  ifdef FEAT_GUI
3423 		(gui.in_use
3424 #   ifdef UNIX
3425 		 && getuid() != ROOT_UID
3426 #   endif
3427 		) ||
3428 #  endif
3429 		parmp->serverName_arg != NULL))
3430     {
3431 	(void)serverRegisterName(X_DISPLAY, parmp->servername);
3432 	vim_free(parmp->servername);
3433 	TIME_MSG("register server name");
3434     }
3435     else
3436 	serverDelayedStartName = parmp->servername;
3437 # endif
3438 
3439     /*
3440      * Execute command ourselves if we're here because the send failed (or
3441      * else we would have exited above).
3442      */
3443     if (parmp->serverStr != NULL)
3444     {
3445 	char_u *p;
3446 
3447 	server_to_input_buf(serverConvert(parmp->serverStrEnc,
3448 						       parmp->serverStr, &p));
3449 	vim_free(p);
3450     }
3451 }
3452 
3453     static void
3454 cmdsrv_main(argc, argv, serverName_arg, serverStr)
3455     int		*argc;
3456     char	**argv;
3457     char_u	*serverName_arg;
3458     char_u	**serverStr;
3459 {
3460     char_u	*res;
3461     int		i;
3462     char_u	*sname;
3463     int		ret;
3464     int		didone = FALSE;
3465     int		exiterr = 0;
3466     char	**newArgV = argv + 1;
3467     int		newArgC = 1,
3468 		Argc = *argc;
3469     int		argtype;
3470 #define ARGTYPE_OTHER		0
3471 #define ARGTYPE_EDIT		1
3472 #define ARGTYPE_EDIT_WAIT	2
3473 #define ARGTYPE_SEND		3
3474     int		silent = FALSE;
3475     int		tabs = FALSE;
3476 # ifndef FEAT_X11
3477     HWND	srv;
3478 # else
3479     Window	srv;
3480 
3481     setup_term_clip();
3482 # endif
3483 
3484     sname = serverMakeName(serverName_arg, argv[0]);
3485     if (sname == NULL)
3486 	return;
3487 
3488     /*
3489      * Execute the command server related arguments and remove them
3490      * from the argc/argv array; We may have to return into main()
3491      */
3492     for (i = 1; i < Argc; i++)
3493     {
3494 	res = NULL;
3495 	if (STRCMP(argv[i], "--") == 0)	/* end of option arguments */
3496 	{
3497 	    for (; i < *argc; i++)
3498 	    {
3499 		*newArgV++ = argv[i];
3500 		newArgC++;
3501 	    }
3502 	    break;
3503 	}
3504 
3505 	if (STRICMP(argv[i], "--remote-send") == 0)
3506 	    argtype = ARGTYPE_SEND;
3507 	else if (STRNICMP(argv[i], "--remote", 8) == 0)
3508 	{
3509 	    char	*p = argv[i] + 8;
3510 
3511 	    argtype = ARGTYPE_EDIT;
3512 	    while (*p != NUL)
3513 	    {
3514 		if (STRNICMP(p, "-wait", 5) == 0)
3515 		{
3516 		    argtype = ARGTYPE_EDIT_WAIT;
3517 		    p += 5;
3518 		}
3519 		else if (STRNICMP(p, "-silent", 7) == 0)
3520 		{
3521 		    silent = TRUE;
3522 		    p += 7;
3523 		}
3524 		else if (STRNICMP(p, "-tab", 4) == 0)
3525 		{
3526 		    tabs = TRUE;
3527 		    p += 4;
3528 		}
3529 		else
3530 		{
3531 		    argtype = ARGTYPE_OTHER;
3532 		    break;
3533 		}
3534 	    }
3535 	}
3536 	else
3537 	    argtype = ARGTYPE_OTHER;
3538 
3539 	if (argtype != ARGTYPE_OTHER)
3540 	{
3541 	    if (i == *argc - 1)
3542 		mainerr_arg_missing((char_u *)argv[i]);
3543 	    if (argtype == ARGTYPE_SEND)
3544 	    {
3545 		*serverStr = (char_u *)argv[i + 1];
3546 		i++;
3547 	    }
3548 	    else
3549 	    {
3550 		*serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3551 					  tabs, argtype == ARGTYPE_EDIT_WAIT);
3552 		if (*serverStr == NULL)
3553 		{
3554 		    /* Probably out of memory, exit. */
3555 		    didone = TRUE;
3556 		    exiterr = 1;
3557 		    break;
3558 		}
3559 		Argc = i;
3560 	    }
3561 # ifdef FEAT_X11
3562 	    if (xterm_dpy == NULL)
3563 	    {
3564 		mch_errmsg(_("No display"));
3565 		ret = -1;
3566 	    }
3567 	    else
3568 		ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3569 						    NULL, &srv, 0, 0, silent);
3570 # else
3571 	    /* Win32 always works? */
3572 	    ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3573 # endif
3574 	    if (ret < 0)
3575 	    {
3576 		if (argtype == ARGTYPE_SEND)
3577 		{
3578 		    /* Failed to send, abort. */
3579 		    mch_errmsg(_(": Send failed.\n"));
3580 		    didone = TRUE;
3581 		    exiterr = 1;
3582 		}
3583 		else if (!silent)
3584 		    /* Let vim start normally.  */
3585 		    mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3586 		break;
3587 	    }
3588 
3589 # ifdef FEAT_GUI_W32
3590 	    /* Guess that when the server name starts with "g" it's a GUI
3591 	     * server, which we can bring to the foreground here.
3592 	     * Foreground() in the server doesn't work very well. */
3593 	    if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3594 		SetForegroundWindow(srv);
3595 # endif
3596 
3597 	    /*
3598 	     * For --remote-wait: Wait until the server did edit each
3599 	     * file.  Also detect that the server no longer runs.
3600 	     */
3601 	    if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3602 	    {
3603 		int	numFiles = *argc - i - 1;
3604 		int	j;
3605 		char_u  *done = alloc(numFiles);
3606 		char_u  *p;
3607 # ifdef FEAT_GUI_W32
3608 		NOTIFYICONDATA ni;
3609 		int	count = 0;
3610 		extern HWND message_window;
3611 # endif
3612 
3613 		if (numFiles > 0 && argv[i + 1][0] == '+')
3614 		    /* Skip "+cmd" argument, don't wait for it to be edited. */
3615 		    --numFiles;
3616 
3617 # ifdef FEAT_GUI_W32
3618 		ni.cbSize = sizeof(ni);
3619 		ni.hWnd = message_window;
3620 		ni.uID = 0;
3621 		ni.uFlags = NIF_ICON|NIF_TIP;
3622 		ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3623 		sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3624 		Shell_NotifyIcon(NIM_ADD, &ni);
3625 # endif
3626 
3627 		/* Wait for all files to unload in remote */
3628 		memset(done, 0, numFiles);
3629 		while (memchr(done, 0, numFiles) != NULL)
3630 		{
3631 # ifdef WIN32
3632 		    p = serverGetReply(srv, NULL, TRUE, TRUE);
3633 		    if (p == NULL)
3634 			break;
3635 # else
3636 		    if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3637 			break;
3638 # endif
3639 		    j = atoi((char *)p);
3640 		    if (j >= 0 && j < numFiles)
3641 		    {
3642 # ifdef FEAT_GUI_W32
3643 			++count;
3644 			sprintf(ni.szTip, _("%d of %d edited"),
3645 							     count, numFiles);
3646 			Shell_NotifyIcon(NIM_MODIFY, &ni);
3647 # endif
3648 			done[j] = 1;
3649 		    }
3650 		}
3651 # ifdef FEAT_GUI_W32
3652 		Shell_NotifyIcon(NIM_DELETE, &ni);
3653 # endif
3654 	    }
3655 	}
3656 	else if (STRICMP(argv[i], "--remote-expr") == 0)
3657 	{
3658 	    if (i == *argc - 1)
3659 		mainerr_arg_missing((char_u *)argv[i]);
3660 # ifdef WIN32
3661 	    /* Win32 always works? */
3662 	    if (serverSendToVim(sname, (char_u *)argv[i + 1],
3663 						    &res, NULL, 1, FALSE) < 0)
3664 # else
3665 	    if (xterm_dpy == NULL)
3666 		mch_errmsg(_("No display: Send expression failed.\n"));
3667 	    else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3668 						 &res, NULL, 1, 1, FALSE) < 0)
3669 # endif
3670 	    {
3671 		if (res != NULL && *res != NUL)
3672 		{
3673 		    /* Output error from remote */
3674 		    mch_errmsg((char *)res);
3675 		    vim_free(res);
3676 		    res = NULL;
3677 		}
3678 		mch_errmsg(_(": Send expression failed.\n"));
3679 	    }
3680 	}
3681 	else if (STRICMP(argv[i], "--serverlist") == 0)
3682 	{
3683 # ifdef WIN32
3684 	    /* Win32 always works? */
3685 	    res = serverGetVimNames();
3686 # else
3687 	    if (xterm_dpy != NULL)
3688 		res = serverGetVimNames(xterm_dpy);
3689 # endif
3690 	    if (called_emsg)
3691 		mch_errmsg("\n");
3692 	}
3693 	else if (STRICMP(argv[i], "--servername") == 0)
3694 	{
3695 	    /* Already processed. Take it out of the command line */
3696 	    i++;
3697 	    continue;
3698 	}
3699 	else
3700 	{
3701 	    *newArgV++ = argv[i];
3702 	    newArgC++;
3703 	    continue;
3704 	}
3705 	didone = TRUE;
3706 	if (res != NULL && *res != NUL)
3707 	{
3708 	    mch_msg((char *)res);
3709 	    if (res[STRLEN(res) - 1] != '\n')
3710 		mch_msg("\n");
3711 	}
3712 	vim_free(res);
3713     }
3714 
3715     if (didone)
3716     {
3717 	display_errors();	/* display any collected messages */
3718 	exit(exiterr);	/* Mission accomplished - get out */
3719     }
3720 
3721     /* Return back into main() */
3722     *argc = newArgC;
3723     vim_free(sname);
3724 }
3725 
3726 /*
3727  * Build a ":drop" command to send to a Vim server.
3728  */
3729     static char_u *
3730 build_drop_cmd(filec, filev, tabs, sendReply)
3731     int		filec;
3732     char	**filev;
3733     int		tabs;		/* Use ":tab drop" instead of ":drop". */
3734     int		sendReply;
3735 {
3736     garray_T	ga;
3737     int		i;
3738     char_u	*inicmd = NULL;
3739     char_u	*p;
3740     char_u	cwd[MAXPATHL];
3741 
3742     if (filec > 0 && filev[0][0] == '+')
3743     {
3744 	inicmd = (char_u *)filev[0] + 1;
3745 	filev++;
3746 	filec--;
3747     }
3748     /* Check if we have at least one argument. */
3749     if (filec <= 0)
3750 	mainerr_arg_missing((char_u *)filev[-1]);
3751     if (mch_dirname(cwd, MAXPATHL) != OK)
3752 	return NULL;
3753     if ((p = vim_strsave_escaped_ext(cwd,
3754 #ifdef BACKSLASH_IN_FILENAME
3755 		    "",  /* rem_backslash() will tell what chars to escape */
3756 #else
3757 		    PATH_ESC_CHARS,
3758 #endif
3759 		    '\\', TRUE)) == NULL)
3760 	return NULL;
3761     ga_init2(&ga, 1, 100);
3762     ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3763     ga_concat(&ga, p);
3764     vim_free(p);
3765 
3766     /* Call inputsave() so that a prompt for an encryption key works. */
3767     ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3768     if (tabs)
3769 	ga_concat(&ga, (char_u *)"tab ");
3770     ga_concat(&ga, (char_u *)"drop");
3771     for (i = 0; i < filec; i++)
3772     {
3773 	/* On Unix the shell has already expanded the wildcards, don't want to
3774 	 * do it again in the Vim server.  On MS-Windows only escape
3775 	 * non-wildcard characters. */
3776 	p = vim_strsave_escaped((char_u *)filev[i],
3777 #ifdef UNIX
3778 		PATH_ESC_CHARS
3779 #else
3780 		(char_u *)" \t%#"
3781 #endif
3782 		);
3783 	if (p == NULL)
3784 	{
3785 	    vim_free(ga.ga_data);
3786 	    return NULL;
3787 	}
3788 	ga_concat(&ga, (char_u *)" ");
3789 	ga_concat(&ga, p);
3790 	vim_free(p);
3791     }
3792     /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3793      * CTRL-\ CTRL-N again. */
3794     ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3795     ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3796     if (sendReply)
3797 	ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3798     ga_concat(&ga, (char_u *)"<CR>:");
3799     if (inicmd != NULL)
3800     {
3801 	/* Can't use <CR> after "inicmd", because an "startinsert" would cause
3802 	 * the following commands to be inserted as text.  Use a "|",
3803 	 * hopefully "inicmd" does allow this... */
3804 	ga_concat(&ga, inicmd);
3805 	ga_concat(&ga, (char_u *)"|");
3806     }
3807     /* Bring the window to the foreground, goto Insert mode when 'im' set and
3808      * clear command line. */
3809     ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
3810     ga_append(&ga, NUL);
3811     return ga.ga_data;
3812 }
3813 
3814 /*
3815  * Replace termcodes such as <CR> and insert as key presses if there is room.
3816  */
3817     void
3818 server_to_input_buf(str)
3819     char_u	*str;
3820 {
3821     char_u      *ptr = NULL;
3822     char_u      *cpo_save = p_cpo;
3823 
3824     /* Set 'cpoptions' the way we want it.
3825      *    B set - backslashes are *not* treated specially
3826      *    k set - keycodes are *not* reverse-engineered
3827      *    < unset - <Key> sequences *are* interpreted
3828      *  The last but one parameter of replace_termcodes() is TRUE so that the
3829      *  <lt> sequence is recognised - needed for a real backslash.
3830      */
3831     p_cpo = (char_u *)"Bk";
3832     str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
3833     p_cpo = cpo_save;
3834 
3835     if (*ptr != NUL)	/* trailing CTRL-V results in nothing */
3836     {
3837 	/*
3838 	 * Add the string to the input stream.
3839 	 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3840 	 *
3841 	 * First clear typed characters from the typeahead buffer, there could
3842 	 * be half a mapping there.  Then append to the existing string, so
3843 	 * that multiple commands from a client are concatenated.
3844 	 */
3845 	if (typebuf.tb_maplen < typebuf.tb_len)
3846 	    del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3847 	(void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3848 
3849 	/* Let input_available() know we inserted text in the typeahead
3850 	 * buffer. */
3851 	typebuf_was_filled = TRUE;
3852     }
3853     vim_free((char_u *)ptr);
3854 }
3855 
3856 /*
3857  * Evaluate an expression that the client sent to a string.
3858  * Handles disabling error messages and disables debugging, otherwise Vim
3859  * hangs, waiting for "cont" to be typed.
3860  */
3861     char_u *
3862 eval_client_expr_to_string(expr)
3863     char_u *expr;
3864 {
3865     char_u	*res;
3866     int		save_dbl = debug_break_level;
3867     int		save_ro = redir_off;
3868 
3869     debug_break_level = -1;
3870     redir_off = 0;
3871     ++emsg_skip;
3872 
3873     res = eval_to_string(expr, NULL, TRUE);
3874 
3875     debug_break_level = save_dbl;
3876     redir_off = save_ro;
3877     --emsg_skip;
3878 
3879     /* A client can tell us to redraw, but not to display the cursor, so do
3880      * that here. */
3881     setcursor();
3882     out_flush();
3883 #ifdef FEAT_GUI
3884     if (gui.in_use)
3885 	gui_update_cursor(FALSE, FALSE);
3886 #endif
3887 
3888     return res;
3889 }
3890 
3891 /*
3892  * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3893  * return an allocated string.  Otherwise return "data".
3894  * "*tofree" is set to the result when it needs to be freed later.
3895  */
3896     char_u *
3897 serverConvert(client_enc, data, tofree)
3898     char_u *client_enc UNUSED;
3899     char_u *data;
3900     char_u **tofree;
3901 {
3902     char_u	*res = data;
3903 
3904     *tofree = NULL;
3905 # ifdef FEAT_MBYTE
3906     if (client_enc != NULL && p_enc != NULL)
3907     {
3908 	vimconv_T	vimconv;
3909 
3910 	vimconv.vc_type = CONV_NONE;
3911 	if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3912 					      && vimconv.vc_type != CONV_NONE)
3913 	{
3914 	    res = string_convert(&vimconv, data, NULL);
3915 	    if (res == NULL)
3916 		res = data;
3917 	    else
3918 		*tofree = res;
3919 	}
3920 	convert_setup(&vimconv, NULL, NULL);
3921     }
3922 # endif
3923     return res;
3924 }
3925 
3926 
3927 /*
3928  * Make our basic server name: use the specified "arg" if given, otherwise use
3929  * the tail of the command "cmd" we were started with.
3930  * Return the name in allocated memory.  This doesn't include a serial number.
3931  */
3932     static char_u *
3933 serverMakeName(arg, cmd)
3934     char_u	*arg;
3935     char	*cmd;
3936 {
3937     char_u *p;
3938 
3939     if (arg != NULL && *arg != NUL)
3940 	p = vim_strsave_up(arg);
3941     else
3942     {
3943 	p = vim_strsave_up(gettail((char_u *)cmd));
3944 	/* Remove .exe or .bat from the name. */
3945 	if (p != NULL && vim_strchr(p, '.') != NULL)
3946 	    *vim_strchr(p, '.') = NUL;
3947     }
3948     return p;
3949 }
3950 #endif /* FEAT_CLIENTSERVER */
3951 
3952 /*
3953  * When FEAT_FKMAP is defined, also compile the Farsi source code.
3954  */
3955 #if defined(FEAT_FKMAP) || defined(PROTO)
3956 # include "farsi.c"
3957 #endif
3958 
3959 /*
3960  * When FEAT_ARABIC is defined, also compile the Arabic source code.
3961  */
3962 #if defined(FEAT_ARABIC) || defined(PROTO)
3963 # include "arabic.c"
3964 #endif
3965