blob: 278cc330c0ecee7f7cc62ae3435b1d115c62fe31 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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/*
11 * misc2.c: Various functions.
12 */
13#include "vim.h"
14
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000015static char_u *username = NULL; /* cached result of mch_get_user_name() */
16
17static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
21/*
22 * Return TRUE if in the current mode we need to use virtual.
23 */
24 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010025virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026{
27 /* While an operator is being executed we return "virtual_op", because
28 * VIsual_active has already been reset, thus we can't check for "block"
29 * being used. */
30 if (virtual_op != MAYBE)
31 return virtual_op;
32 return (ve_flags == VE_ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 || ((ve_flags & VE_INSERT) && (State & INSERT)));
35}
36
37/*
38 * Get the screen position of the cursor.
39 */
40 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010041getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042{
43 colnr_T x;
44
45 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
46 return (int)x;
47}
48
49/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000050 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 * cursor in that column.
52 * The caller must have saved the cursor line for undo!
53 */
54 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010055coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
58
59 if (wcol == MAXCOL)
60 curwin->w_valid &= ~VALID_VIRTCOL;
61 else
62 {
63 /* Virtcol is valid */
64 curwin->w_valid |= VALID_VIRTCOL;
65 curwin->w_virtcol = wcol;
66 }
67 return rc;
68}
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010071 * Get the screen position of character col with a coladd in the cursor line.
72 */
73 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010074getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010075{
76 colnr_T x;
77 pos_T pos;
78
79 pos.lnum = curwin->w_cursor.lnum;
80 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010081 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 getvvcol(curwin, &pos, &x, NULL, NULL);
83 return (int)x;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Try to advance the Cursor to the specified screen column.
88 * If virtual editing: fine tune the cursor position.
89 * Note that all virtual positions off the end of a line should share
90 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
91 * beginning at coladd 0.
92 *
93 * return OK if desired column is reached, FAIL if not
94 */
95 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010096coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 int rc = getvpos(&curwin->w_cursor, wcol);
99
100 if (wcol == MAXCOL || rc == FAIL)
101 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000102 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000104 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 curwin->w_valid |= VALID_VIRTCOL;
106 curwin->w_virtcol = wcol;
107 }
108 return rc;
109}
110
111/*
112 * Return in "pos" the position of the cursor advanced to screen column "wcol".
113 * return OK if desired column is reached, FAIL if not
114 */
115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100116getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 return coladvance2(pos, FALSE, virtual_active(), wcol);
119}
120
121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100122coladvance2(
123 pos_T *pos,
124 int addspaces, /* change the text to achieve our goal? */
125 int finetune, /* change char offset for the exact column */
126 colnr_T wcol) /* column to move to */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int idx;
129 char_u *ptr;
130 char_u *line;
131 colnr_T col = 0;
132 int csize = 0;
133 int one_more;
134#ifdef FEAT_LINEBREAK
135 int head = 0;
136#endif
137
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000138 one_more = (State & INSERT)
139 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000140 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100141 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) ;
Bram Moolenaara1381de2009-11-03 15:44:21 +0000142 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144 if (wcol >= MAXCOL)
145 {
146 idx = (int)STRLEN(line) - 1 + one_more;
147 col = wcol;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if ((addspaces || finetune) && !VIsual_active)
150 {
151 curwin->w_curswant = linetabsize(line) + one_more;
152 if (curwin->w_curswant > 0)
153 --curwin->w_curswant;
154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 }
156 else
157 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200158 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaarebefac62005-12-28 22:39:57 +0000160 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && wcol >= (colnr_T)width)
164 {
165 csize = linetabsize(line);
166 if (csize > 0)
167 csize--;
168
Bram Moolenaarebefac62005-12-28 22:39:57 +0000169 if (wcol / width > (colnr_T)csize / width
170 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 {
172 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000173 * right screen edge. In Insert mode allow going just beyond
174 * the last character (like what happens when typing and
175 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 wcol = (csize / width + 1) * width - 1;
177 }
178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 ptr = line;
181 while (col <= wcol && *ptr != NUL)
182 {
183 /* Count a tab for what it's worth (if list mode not on) */
184#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200185 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100186 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200188 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190 col += csize;
191 }
192 idx = (int)(ptr - line);
193 /*
194 * Handle all the special cases. The virtual_active() check
195 * is needed to ensure that a virtual position off the end of
196 * a line has the correct indexing. The one_more comparison
197 * replaces an explicit add of one_more later on.
198 */
199 if (col > wcol || (!virtual_active() && one_more == 0))
200 {
201 idx -= 1;
202# ifdef FEAT_LINEBREAK
203 /* Don't count the chars from 'showbreak'. */
204 csize -= head;
205# endif
206 col -= csize;
207 }
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (virtual_active()
210 && addspaces
211 && ((col != wcol && col != wcol + 1) || csize > 1))
212 {
213 /* 'virtualedit' is set: The difference between wcol and col is
214 * filled with spaces. */
215
216 if (line[idx] == NUL)
217 {
218 /* Append spaces */
219 int correct = wcol - col;
220 char_u *newline = alloc(idx + correct + 1);
221 int t;
222
223 if (newline == NULL)
224 return FAIL;
225
226 for (t = 0; t < idx; ++t)
227 newline[t] = line[t];
228
229 for (t = 0; t < correct; ++t)
230 newline[t + idx] = ' ';
231
232 newline[idx + correct] = NUL;
233
234 ml_replace(pos->lnum, newline, FALSE);
235 changed_bytes(pos->lnum, (colnr_T)idx);
236 idx += correct;
237 col = wcol;
238 }
239 else
240 {
241 /* Break a tab */
242 int linelen = (int)STRLEN(line);
243 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000244 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 int t, s = 0;
246 int v;
247
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000248 if (-correct > csize)
249 return FAIL;
250
251 newline = alloc(linelen + csize);
252 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 return FAIL;
254
255 for (t = 0; t < linelen; t++)
256 {
257 if (t != idx)
258 newline[s++] = line[t];
259 else
260 for (v = 0; v < csize; v++)
261 newline[s++] = ' ';
262 }
263
264 newline[linelen + csize - 1] = NUL;
265
266 ml_replace(pos->lnum, newline, FALSE);
267 changed_bytes(pos->lnum, idx);
268 idx += (csize - 1 + correct);
269 col += correct;
270 }
271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 if (idx < 0)
275 pos->col = 0;
276 else
277 pos->col = idx;
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 pos->coladd = 0;
280
281 if (finetune)
282 {
283 if (wcol == MAXCOL)
284 {
285 /* The width of the last character is used to set coladd. */
286 if (!one_more)
287 {
288 colnr_T scol, ecol;
289
290 getvcol(curwin, pos, &scol, NULL, &ecol);
291 pos->coladd = ecol - scol;
292 }
293 }
294 else
295 {
296 int b = (int)wcol - (int)col;
297
298 /* The difference between wcol and col is used to set coladd. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200299 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 pos->coladd = b;
301
302 col += b;
303 }
304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Bram Moolenaara1381de2009-11-03 15:44:21 +0000306 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200308 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310 if (col < wcol)
311 return FAIL;
312 return OK;
313}
314
315/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000316 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 */
318 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100319inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 return inc(&curwin->w_cursor);
322}
323
Bram Moolenaar446cb832008-06-24 21:56:24 +0000324/*
325 * Increment the line pointer "lp" crossing line boundaries as necessary.
326 * Return 1 when going to the next line.
327 * Return 2 when moving forward onto a NUL at the end of the line).
328 * Return -1 when at the end of file.
329 * Return 0 otherwise.
330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100332inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100334 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100336 /* when searching position may be set to end of a line */
337 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100339 p = ml_get_pos(lp);
340 if (*p != NUL) /* still within line, move to next char (may be NUL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100342 if (has_mbyte)
343 {
344 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100346 lp->col += l;
347 return ((p[l] != NUL) ? 0 : 2);
348 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100349 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100350 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 return ((p[1] != NUL) ? 0 : 2);
352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
354 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
355 {
356 lp->col = 0;
357 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return 1;
360 }
361 return -1;
362}
363
364/*
365 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
366 */
367 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100368incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
370 int r;
371
372 if ((r = inc(lp)) >= 1 && lp->col)
373 r = inc(lp);
374 return r;
375}
376
377/*
378 * dec(p)
379 *
380 * Decrement the line pointer 'p' crossing line boundaries as necessary.
381 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
382 */
383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100384dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100386 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100390dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 char_u *p;
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100395 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100397 /* past end of line */
398 p = ml_get(lp->lnum);
399 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100400 if (has_mbyte)
401 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 return 0;
403 }
404
405 if (lp->col > 0)
406 {
407 /* still within line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (has_mbyte)
410 {
411 p = ml_get(lp->lnum);
412 lp->col -= (*mb_head_off)(p, p + lp->col);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 return 0;
415 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100416
417 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100419 /* there is a prior line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 lp->lnum--;
421 p = ml_get(lp->lnum);
422 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (has_mbyte)
424 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 return 1;
426 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100427
428 /* at start of file */
429 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
432/*
433 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
434 */
435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int r;
439
440 if ((r = dec(lp)) == 1 && lp->col)
441 r = dec(lp);
442 return r;
443}
444
445/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200446 * Get the line number relative to the current cursor position, i.e. the
447 * difference between line number and cursor position. Only look for lines that
448 * can be visible, folded lines don't count.
449 */
450 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451get_cursor_rel_lnum(
452 win_T *wp,
453 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200454{
455 linenr_T cursor = wp->w_cursor.lnum;
456 linenr_T retval = 0;
457
458#ifdef FEAT_FOLDING
459 if (hasAnyFolding(wp))
460 {
461 if (lnum > cursor)
462 {
463 while (lnum > cursor)
464 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100465 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200466 /* if lnum and cursor are in the same fold,
467 * now lnum <= cursor */
468 if (lnum > cursor)
469 retval++;
470 lnum--;
471 }
472 }
473 else if (lnum < cursor)
474 {
475 while (lnum < cursor)
476 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100477 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200478 /* if lnum and cursor are in the same fold,
479 * now lnum >= cursor */
480 if (lnum < cursor)
481 retval--;
482 lnum++;
483 }
484 }
485 /* else if (lnum == cursor)
486 * retval = 0;
487 */
488 }
489 else
490#endif
491 retval = lnum - cursor;
492
493 return retval;
494}
495
496/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200497 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
498 * This allows for the col to be on the NUL byte.
499 */
500 void
501check_pos(buf_T *buf, pos_T *pos)
502{
503 char_u *line;
504 colnr_T len;
505
506 if (pos->lnum > buf->b_ml.ml_line_count)
507 pos->lnum = buf->b_ml.ml_line_count;
508
509 if (pos->col > 0)
510 {
511 line = ml_get_buf(buf, pos->lnum, FALSE);
512 len = (colnr_T)STRLEN(line);
513 if (pos->col > len)
514 pos->col = len;
515 }
516}
517
518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 * Make sure curwin->w_cursor.lnum is valid.
520 */
521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100522check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
525 {
526#ifdef FEAT_FOLDING
527 /* If there is a closed fold at the end of the file, put the cursor in
528 * its first line. Otherwise in the last line. */
529 if (!hasFolding(curbuf->b_ml.ml_line_count,
530 &curwin->w_cursor.lnum, NULL))
531#endif
532 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
533 }
534 if (curwin->w_cursor.lnum <= 0)
535 curwin->w_cursor.lnum = 1;
536}
537
538/*
539 * Make sure curwin->w_cursor.col is valid.
540 */
541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200544 check_cursor_col_win(curwin);
545}
546
547/*
548 * Make sure win->w_cursor.col is valid.
549 */
550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200552{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 colnr_T len;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200554 colnr_T oldcol = win->w_cursor.col;
555 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200557 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 win->w_cursor.col = 0;
560 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000562 /* Allow cursor past end-of-line when:
563 * - in Insert mode or restarting Insert mode
564 * - in Visual mode and 'selection' isn't "old"
565 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000568 || (ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200570 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000572 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200573 win->w_cursor.col = len - 1;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000575 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200576 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200579 else if (win->w_cursor.col < 0)
580 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 /* If virtual editing is on, we can leave the cursor on the old position,
583 * only we must set it to virtual. But don't do it when at the end of the
584 * line. */
585 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200586 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000588 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200589 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200590 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200592
593 /* Make sure that coladd is not more than the char width.
594 * Not for the last character, coladd is then used when the cursor
595 * is actually after the last character. */
596 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200597 {
598 int cs, ce;
599
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200600 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
601 if (win->w_cursor.coladd > ce - cs)
602 win->w_cursor.coladd = ce - cs;
603 }
604 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000605 else
606 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609}
610
611/*
612 * make sure curwin->w_cursor in on a valid character
613 */
614 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100615check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
617 check_cursor_lnum();
618 check_cursor_col();
619}
620
621#if defined(FEAT_TEXTOBJ) || defined(PROTO)
622/*
623 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
624 * Allow it when in Visual mode and 'selection' is not "old".
625 */
626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100627adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 && gchar_cursor() == NUL)
632 --curwin->w_cursor.col;
633}
634#endif
635
636/*
637 * When curwin->w_leftcol has changed, adjust the cursor position.
638 * Return TRUE if the cursor was moved.
639 */
640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100641leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 long lastcol;
644 colnr_T s, e;
645 int retval = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100646 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200649 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 validate_virtcol();
651
652 /*
653 * If the cursor is right or left of the screen, move it to last or first
654 * character.
655 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100656 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100659 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100661 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100664 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666
667 /*
668 * If the start of the character under the cursor is not on the screen,
669 * advance the cursor one more char. If this fails (last char of the
670 * line) adjust the scrolling.
671 */
672 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
673 if (e > (colnr_T)lastcol)
674 {
675 retval = TRUE;
676 coladvance(s - 1);
677 }
678 else if (s < curwin->w_leftcol)
679 {
680 retval = TRUE;
681 if (coladvance(e + 1) == FAIL) /* there isn't another character */
682 {
683 curwin->w_leftcol = s; /* adjust w_leftcol instead */
684 changed_cline_bef_curs();
685 }
686 }
687
688 if (retval)
689 curwin->w_set_curswant = TRUE;
690 redraw_later(NOT_VALID);
691 return retval;
692}
693
694/**********************************************************************
695 * Various routines dealing with allocation and deallocation of memory.
696 */
697
698#if defined(MEM_PROFILE) || defined(PROTO)
699
700# define MEM_SIZES 8200
701static long_u mem_allocs[MEM_SIZES];
702static long_u mem_frees[MEM_SIZES];
703static long_u mem_allocated;
704static long_u mem_freed;
705static long_u mem_peak;
706static long_u num_alloc;
707static long_u num_freed;
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100710mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 *sizep += sizeof(size_t);
713}
714
715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100716mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
718 *sizep += sizeof(size_t);
719}
720
721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100722mem_post_alloc(
723 void **pp,
724 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 if (*pp == NULL)
727 return;
728 size -= sizeof(size_t);
729 *(long_u *)*pp = size;
730 if (size <= MEM_SIZES-1)
731 mem_allocs[size-1]++;
732 else
733 mem_allocs[MEM_SIZES-1]++;
734 mem_allocated += size;
735 if (mem_allocated - mem_freed > mem_peak)
736 mem_peak = mem_allocated - mem_freed;
737 num_alloc++;
738 *pp = (void *)((char *)*pp + sizeof(size_t));
739}
740
741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 long_u size;
745
746 *pp = (void *)((char *)*pp - sizeof(size_t));
747 size = *(size_t *)*pp;
748 if (size <= MEM_SIZES-1)
749 mem_frees[size-1]++;
750 else
751 mem_frees[MEM_SIZES-1]++;
752 mem_freed += size;
753 num_freed++;
754}
755
756/*
757 * called on exit via atexit()
758 */
759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100760vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762 int i, j;
763
764 printf("\r\n");
765 j = 0;
766 for (i = 0; i < MEM_SIZES - 1; i++)
767 {
768 if (mem_allocs[i] || mem_frees[i])
769 {
770 if (mem_frees[i] > mem_allocs[i])
771 printf("\r\n%s", _("ERROR: "));
772 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
773 j++;
774 if (j > 3)
775 {
776 j = 0;
777 printf("\r\n");
778 }
779 }
780 }
781
782 i = MEM_SIZES - 1;
783 if (mem_allocs[i])
784 {
785 printf("\r\n");
786 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200787 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
789 }
790
791 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
792 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
793 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
794 num_alloc, num_freed);
795}
796
797#endif /* MEM_PROFILE */
798
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100799#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100800 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100801alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100802{
803 if (alloc_fail_countdown == 0)
804 {
805 if (--alloc_fail_repeat <= 0)
806 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100807 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100808 return TRUE;
809 }
810 --alloc_fail_countdown;
811 return FALSE;
812}
813#endif
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Some memory is reserved for error messages and for being able to
817 * call mf_release_all(), which needs some memory for mf_trans_add().
818 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100819#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200820#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000823 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * Use lalloc for larger blocks.
825 */
826 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100827alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
829 return (lalloc((long_u)size, TRUE));
830}
831
832/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100833 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100834 */
835 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100837{
838#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100839 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100840 return NULL;
841#endif
842 return (lalloc((long_u)size, TRUE));
843}
844
845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * Allocate memory and set all bytes to zero.
847 */
848 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100849alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 char_u *p;
852
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200853 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (p != NULL)
855 (void)vim_memset(p, 0, (size_t)size);
856 return p;
857}
858
859/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100860 * Same as alloc_clear() but with allocation id for testing
861 */
862 char_u *
863alloc_clear_id(unsigned size, alloc_id_T id UNUSED)
864{
865#ifdef FEAT_EVAL
866 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
867 return NULL;
868#endif
869 return alloc_clear(size);
870}
871
872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * alloc() with check for maximum line length
874 */
875 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100876alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200878#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (sizeof(int) == 2 && size > 0x7fff)
880 {
881 /* Don't hide this message */
882 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100883 emsg(_("E340: Line is becoming too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 return NULL;
885 }
886#endif
887 return (lalloc((long_u)size, TRUE));
888}
889
890/*
891 * Allocate memory like lalloc() and set all bytes to zero.
892 */
893 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 char_u *p;
897
898 p = (lalloc(size, message));
899 if (p != NULL)
900 (void)vim_memset(p, 0, (size_t)size);
901 return p;
902}
903
904/*
905 * Low level memory allocation function.
906 * This is used often, KEEP IT FAST!
907 */
908 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 char_u *p; /* pointer to new storage space */
912 static int releasing = FALSE; /* don't do mf_release_all() recursive */
913 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100914#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 static long_u allocated = 0; /* allocated since last avail check */
916#endif
917
918 /* Safety check for allocating zero bytes */
919 if (size == 0)
920 {
921 /* Don't hide this message */
922 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 siemsg(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 return NULL;
925 }
926
927#ifdef MEM_PROFILE
928 mem_pre_alloc_l(&size);
929#endif
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 /*
932 * Loop when out of memory: Try to release some memfile blocks and
933 * if some blocks are released call malloc again.
934 */
935 for (;;)
936 {
937 /*
938 * Handle three kind of systems:
939 * 1. No check for available memory: Just return.
940 * 2. Slow check for available memory: call mch_avail_mem() after
941 * allocating KEEP_ROOM amount of memory.
942 * 3. Strict check for available memory: call mch_avail_mem()
943 */
944 if ((p = (char_u *)malloc((size_t)size)) != NULL)
945 {
946#ifndef HAVE_AVAIL_MEM
947 /* 1. No check for available memory: Just return. */
948 goto theend;
949#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 /* 2. Slow check for available memory: call mch_avail_mem() after
951 * allocating (KEEP_ROOM / 2) amount of memory. */
952 allocated += size;
953 if (allocated < KEEP_ROOM / 2)
954 goto theend;
955 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200958 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000960 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 p = NULL;
962 }
963 else
964 goto theend;
965#endif
966 }
967 /*
968 * Remember that mf_release_all() is being called to avoid an endless
969 * loop, because mf_release_all() may call alloc() recursively.
970 */
971 if (releasing)
972 break;
973 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000974
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100975 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000976 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 releasing = FALSE;
979 if (!try_again)
980 break;
981 }
982
983 if (message && p == NULL)
984 do_outofmem_msg(size);
985
986theend:
987#ifdef MEM_PROFILE
988 mem_post_alloc((void **)&p, (size_t)size);
989#endif
990 return p;
991}
992
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100993/*
994 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100995 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100996#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100997 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100998lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100999{
1000#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001001 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001002 return NULL;
1003#endif
1004 return (lalloc((long_u)size, message));
1005}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001006#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#if defined(MEM_PROFILE) || defined(PROTO)
1009/*
1010 * realloc() with memory profiling.
1011 */
1012 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001013mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 void *p;
1016
1017 mem_pre_free(&ptr);
1018 mem_pre_alloc_s(&size);
1019
1020 p = realloc(ptr, size);
1021
1022 mem_post_alloc(&p, size);
1023
1024 return p;
1025}
1026#endif
1027
1028/*
1029* Avoid repeating the error message many times (they take 1 second each).
1030* Did_outofmem_msg is reset when a character is read.
1031*/
1032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001033do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 if (!did_outofmem_msg)
1036 {
1037 /* Don't hide this message */
1038 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001039
1040 /* Must come first to avoid coming back here when printing the error
1041 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001043
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001044 semsg(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046}
1047
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001048#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001049
1050# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001051static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001052# endif
1053
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001054/*
1055 * Free everything that we allocated.
1056 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001057 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1058 * surprised if Vim crashes...
1059 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001060 */
1061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001062free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001063{
1064 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001065
1066 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1067 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001068 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001069 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001070 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001071
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001072 /* Don't want to trigger autocommands from here on. */
1073 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001074
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001075 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1076 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001077 if (first_tabpage->tp_next != NULL)
1078 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001079 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001080 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001081
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001082# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001083 /* Free all spell info. */
1084 spell_free_all();
1085# endif
1086
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001087#if defined(FEAT_INS_EXPAND) && defined(FEAT_BEVAL_TERM)
1088 ui_remove_balloon();
1089# endif
1090
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001091# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001092 /* Clear user commands (before deleting buffers). */
1093 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001094# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001095
1096# ifdef FEAT_MENU
1097 /* Clear menus. */
1098 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001099# ifdef FEAT_MULTI_LANG
1100 do_cmdline_cmd((char_u *)"menutranslate clear");
1101# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001102# endif
1103
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001104 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001105 do_cmdline_cmd((char_u *)"lmapclear");
1106 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001107 do_cmdline_cmd((char_u *)"mapclear");
1108 do_cmdline_cmd((char_u *)"mapclear!");
1109 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001110# if defined(FEAT_EVAL)
1111 do_cmdline_cmd((char_u *)"breakdel *");
1112# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001113# if defined(FEAT_PROFILE)
1114 do_cmdline_cmd((char_u *)"profdel *");
1115# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001116# if defined(FEAT_KEYMAP)
1117 do_cmdline_cmd((char_u *)"set keymap=");
1118#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001119
1120# ifdef FEAT_TITLE
1121 free_titles();
1122# endif
1123# if defined(FEAT_SEARCHPATH)
1124 free_findfile();
1125# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001126
1127 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001128 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001129 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001130 free_all_marks();
1131 alist_clear(&global_alist);
1132 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001133# if defined(FEAT_CMDL_COMPL)
1134 free_users();
1135# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001136 free_search_patterns();
1137 free_old_sub();
1138 free_last_insert();
1139 free_prev_shellcmd();
1140 free_regexp_stuff();
1141 free_tag_stuff();
1142 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001143# ifdef FEAT_SIGNS
1144 free_signs();
1145# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001146# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001147 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001148# endif
1149# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001150 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001151# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001152 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001153
1154 /* Free some global vars. */
1155 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001156# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001157 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001158# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001159 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001160# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001161 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001162# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001163 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001164 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001165
1166 /* Clear cmdline history. */
1167 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001168# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001169 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001170# endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001171#ifdef FEAT_TEXT_PROP
1172 clear_global_prop_types();
1173#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001174
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001175#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001176 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001177 win_T *win;
1178 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001179
1180 qf_free_all(NULL);
1181 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001182 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001183 qf_free_all(win);
1184 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001185#endif
1186
1187 /* Close all script inputs. */
1188 close_all_scripts();
1189
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001190 /* Destroy all windows. Must come before freeing buffers. */
1191 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001192
Bram Moolenaar4f198282017-10-23 21:53:30 +02001193 /* Free all option values. Must come after closing windows. */
1194 free_all_options();
1195
Bram Moolenaar2a329742008-04-01 12:53:43 +00001196 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1197 * were freed already. */
1198#ifdef FEAT_AUTOCHDIR
1199 p_acd = FALSE;
1200#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001201 for (buf = firstbuf; buf != NULL; )
1202 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001203 bufref_T bufref;
1204
1205 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001206 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001207 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001208 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001209 buf = nextbuf; /* didn't work, try next one */
1210 else
1211 buf = firstbuf;
1212 }
1213
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001214# ifdef FEAT_ARABIC
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001215 free_cmdline_buf();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001216# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001217
1218 /* Clear registers. */
1219 clear_registers();
1220 ResetRedobuff();
1221 ResetRedobuff();
1222
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001223# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001224 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001225# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001226
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001227 /* highlight info */
1228 free_highlight();
1229
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001230 reset_last_sourcing();
1231
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001232 free_tabpage(first_tabpage);
1233 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001234
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001235# ifdef UNIX
1236 /* Machine-specific free. */
1237 mch_free_mem();
1238# endif
1239
1240 /* message history */
1241 for (;;)
1242 if (delete_first_msg() == FAIL)
1243 break;
1244
Bram Moolenaar655da312016-05-28 22:22:34 +02001245# ifdef FEAT_JOB_CHANNEL
1246 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001247# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001248# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001249 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001250# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001251# ifdef FEAT_EVAL
1252 /* must be after channel_free_all() with unrefs partials */
1253 eval_clear();
1254# endif
1255# ifdef FEAT_JOB_CHANNEL
1256 /* must be after eval_clear() with unrefs jobs */
1257 job_free_all();
1258# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001259
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001260 free_termoptions();
1261
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001262 /* screenlines (can't display anything now!) */
1263 free_screenlines();
1264
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001265# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001266 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001267# endif
1268# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001269 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001270# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001271 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001272
1273 vim_free(IObuff);
1274 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001275# ifdef FEAT_QUICKFIX
1276 check_quickfix_busy();
1277# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001278}
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001282 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 */
1284 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001285vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 char_u *p;
1288 unsigned len;
1289
1290 len = (unsigned)STRLEN(string) + 1;
1291 p = alloc(len);
1292 if (p != NULL)
1293 mch_memmove(p, string, (size_t)len);
1294 return p;
1295}
1296
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001297/*
1298 * Copy up to "len" bytes of "string" into newly allocated memory and
1299 * terminate with a NUL.
1300 * The allocated memory always has size "len + 1", also when "string" is
1301 * shorter.
1302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001304vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 char_u *p;
1307
1308 p = alloc((unsigned)(len + 1));
1309 if (p != NULL)
1310 {
1311 STRNCPY(p, string, len);
1312 p[len] = NUL;
1313 }
1314 return p;
1315}
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001318 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1319 * Returns NULL when out of memory.
1320 */
1321 char_u *
1322vim_memsave(char_u *p, int len)
1323{
1324 char_u *ret = alloc((unsigned)len);
1325
1326 if (ret != NULL)
1327 mch_memmove(ret, p, (size_t)len);
1328 return ret;
1329}
1330
1331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1333 * by a backslash.
1334 */
1335 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001338 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339}
1340
1341/*
1342 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1343 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001344 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 */
1346 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001347vim_strsave_escaped_ext(
1348 char_u *string,
1349 char_u *esc_chars,
1350 int cc,
1351 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 char_u *p;
1354 char_u *p2;
1355 char_u *escaped_string;
1356 unsigned length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 /*
1360 * First count the number of backslashes required.
1361 * Then allocate the memory and insert them.
1362 */
1363 length = 1; /* count the trailing NUL */
1364 for (p = string; *p; p++)
1365 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001366 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
1368 length += l; /* count a multibyte char */
1369 p += l - 1;
1370 continue;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1373 ++length; /* count a backslash */
1374 ++length; /* count an ordinary char */
1375 }
1376 escaped_string = alloc(length);
1377 if (escaped_string != NULL)
1378 {
1379 p2 = escaped_string;
1380 for (p = string; *p; p++)
1381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001382 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
1384 mch_memmove(p2, p, (size_t)l);
1385 p2 += l;
1386 p += l - 1; /* skip multibyte char */
1387 continue;
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001390 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 *p2++ = *p;
1392 }
1393 *p2 = NUL;
1394 }
1395 return escaped_string;
1396}
1397
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001398/*
1399 * Return TRUE when 'shell' has "csh" in the tail.
1400 */
1401 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001402csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001403{
1404 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1405}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001406
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001407/*
1408 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001409 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001410 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001411 * Escape a newline, depending on the 'shell' option.
1412 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1413 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001414 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001415 * Returns the result in allocated memory, NULL if we have run out.
1416 */
1417 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001419{
1420 unsigned length;
1421 char_u *p;
1422 char_u *d;
1423 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001424 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001425 int csh_like;
1426
1427 /* Only csh and similar shells expand '!' within single quotes. For sh and
1428 * the like we must not put a backslash before it, it will be taken
1429 * literally. If do_special is set the '!' will be escaped twice.
1430 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001431 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001432
1433 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001434 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001435 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001436 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001437# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001438 if (!p_ssl)
1439 {
1440 if (*p == '"')
1441 ++length; /* " -> "" */
1442 }
1443 else
1444# endif
1445 if (*p == '\'')
1446 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001447 if ((*p == '\n' && (csh_like || do_newline))
1448 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001449 {
1450 ++length; /* insert backslash */
1451 if (csh_like && do_special)
1452 ++length; /* insert backslash */
1453 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001454 if (do_special && find_cmdline_var(p, &l) >= 0)
1455 {
1456 ++length; /* insert backslash */
1457 p += l - 1;
1458 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001459 }
1460
1461 /* Allocate memory for the result and fill it. */
1462 escaped_string = alloc(length);
1463 if (escaped_string != NULL)
1464 {
1465 d = escaped_string;
1466
1467 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001468# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001469 if (!p_ssl)
1470 *d++ = '"';
1471 else
1472# endif
1473 *d++ = '\'';
1474
1475 for (p = string; *p != NUL; )
1476 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001477# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001478 if (!p_ssl)
1479 {
1480 if (*p == '"')
1481 {
1482 *d++ = '"';
1483 *d++ = '"';
1484 ++p;
1485 continue;
1486 }
1487 }
1488 else
1489# endif
1490 if (*p == '\'')
1491 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001492 *d++ = '\'';
1493 *d++ = '\\';
1494 *d++ = '\'';
1495 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001496 ++p;
1497 continue;
1498 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001499 if ((*p == '\n' && (csh_like || do_newline))
1500 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001501 {
1502 *d++ = '\\';
1503 if (csh_like && do_special)
1504 *d++ = '\\';
1505 *d++ = *p++;
1506 continue;
1507 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001508 if (do_special && find_cmdline_var(p, &l) >= 0)
1509 {
1510 *d++ = '\\'; /* insert backslash */
1511 while (--l >= 0) /* copy the var */
1512 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001513 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001514 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001515
1516 MB_COPY_CHAR(p, d);
1517 }
1518
1519 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001520# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001521 if (!p_ssl)
1522 *d++ = '"';
1523 else
1524# endif
1525 *d++ = '\'';
1526 *d = NUL;
1527 }
1528
1529 return escaped_string;
1530}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532/*
1533 * Like vim_strsave(), but make all characters uppercase.
1534 * This uses ASCII lower-to-upper case translation, language independent.
1535 */
1536 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 char_u *p1;
1540
1541 p1 = vim_strsave(string);
1542 vim_strup(p1);
1543 return p1;
1544}
1545
1546/*
1547 * Like vim_strnsave(), but make all characters uppercase.
1548 * This uses ASCII lower-to-upper case translation, language independent.
1549 */
1550 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001551vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 char_u *p1;
1554
1555 p1 = vim_strnsave(string, len);
1556 vim_strup(p1);
1557 return p1;
1558}
1559
1560/*
1561 * ASCII lower-to-upper case translation, language independent.
1562 */
1563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001564vim_strup(
1565 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
1567 char_u *p2;
1568 int c;
1569
1570 if (p != NULL)
1571 {
1572 p2 = p;
1573 while ((c = *p2) != NUL)
1574#ifdef EBCDIC
1575 *p2++ = isalpha(c) ? toupper(c) : c;
1576#else
1577 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1578#endif
1579 }
1580}
1581
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001582#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001583/*
1584 * Make string "s" all upper-case and return it in allocated memory.
1585 * Handles multi-byte characters as well as possible.
1586 * Returns NULL when out of memory.
1587 */
1588 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001589strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001590{
1591 char_u *p;
1592 char_u *res;
1593
1594 res = p = vim_strsave(orig);
1595
1596 if (res != NULL)
1597 while (*p != NUL)
1598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001599 int l;
1600
1601 if (enc_utf8)
1602 {
1603 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001604 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001605 char_u *s;
1606
1607 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001608 l = utf_ptr2len(p);
1609 if (c == 0)
1610 {
1611 /* overlong sequence, use only the first byte */
1612 c = *p;
1613 l = 1;
1614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001615 uc = utf_toupper(c);
1616
1617 /* Reallocate string when byte count changes. This is rare,
1618 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001619 newl = utf_char2len(uc);
1620 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001621 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001622 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001623 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001624 {
1625 vim_free(res);
1626 return NULL;
1627 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001628 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001629 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001630 p = s + (p - res);
1631 vim_free(res);
1632 res = s;
1633 }
1634
1635 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001636 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001637 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001638 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001639 p += l; /* skip multi-byte character */
1640 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001641 {
1642 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1643 p++;
1644 }
1645 }
1646
1647 return res;
1648}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001649
1650/*
1651 * Make string "s" all lower-case and return it in allocated memory.
1652 * Handles multi-byte characters as well as possible.
1653 * Returns NULL when out of memory.
1654 */
1655 char_u *
1656strlow_save(char_u *orig)
1657{
1658 char_u *p;
1659 char_u *res;
1660
1661 res = p = vim_strsave(orig);
1662
1663 if (res != NULL)
1664 while (*p != NUL)
1665 {
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001666 int l;
1667
1668 if (enc_utf8)
1669 {
1670 int c, lc;
1671 int newl;
1672 char_u *s;
1673
1674 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001675 l = utf_ptr2len(p);
1676 if (c == 0)
1677 {
1678 /* overlong sequence, use only the first byte */
1679 c = *p;
1680 l = 1;
1681 }
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001682 lc = utf_tolower(c);
1683
1684 /* Reallocate string when byte count changes. This is rare,
1685 * thus it's OK to do another malloc()/free(). */
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001686 newl = utf_char2len(lc);
1687 if (newl != l)
1688 {
1689 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1690 if (s == NULL)
1691 {
1692 vim_free(res);
1693 return NULL;
1694 }
1695 mch_memmove(s, res, p - res);
1696 STRCPY(s + (p - res) + newl, p + l);
1697 p = s + (p - res);
1698 vim_free(res);
1699 res = s;
1700 }
1701
1702 utf_char2bytes(lc, p);
1703 p += newl;
1704 }
1705 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1706 p += l; /* skip multi-byte character */
1707 else
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001708 {
1709 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1710 p++;
1711 }
1712 }
1713
1714 return res;
1715}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001716#endif
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 * delete spaces at the end of a string
1720 */
1721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001722del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 char_u *q;
1725
1726 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001727 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 *q = NUL;
1729}
1730
1731/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001733 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001736vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001738 STRNCPY(to, from, len);
1739 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740}
1741
1742/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001743 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001744 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001745 */
1746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001747vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001748{
1749 size_t tolen = STRLEN(to);
1750 size_t fromlen = STRLEN(from);
1751
1752 if (tolen + fromlen + 1 > tosize)
1753 {
1754 mch_memmove(to + tolen, from, tosize - tolen - 1);
1755 to[tosize - 1] = NUL;
1756 }
1757 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001758 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001759}
1760
1761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 * Isolate one part of a string option where parts are separated with
1763 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001764 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * "*option" is advanced to the next part.
1766 * The length is returned.
1767 */
1768 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001769copy_option_part(
1770 char_u **option,
1771 char_u *buf,
1772 int maxlen,
1773 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int len = 0;
1776 char_u *p = *option;
1777
1778 /* skip '.' at start of option part, for 'suffixes' */
1779 if (*p == '.')
1780 buf[len++] = *p++;
1781 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1782 {
1783 /*
1784 * Skip backslash before a separator character and space.
1785 */
1786 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1787 ++p;
1788 if (len < maxlen - 1)
1789 buf[len++] = *p;
1790 ++p;
1791 }
1792 buf[len] = NUL;
1793
1794 if (*p != NUL && *p != ',') /* skip non-standard separator */
1795 ++p;
1796 p = skip_to_option_part(p); /* p points to next file name */
1797
1798 *option = p;
1799 return len;
1800}
1801
1802/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001803 * Replacement for free() that ignores NULL pointers.
1804 * Also skip free() when exiting for sure, this helps when we caught a deadly
1805 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001806 * If you want to set NULL after calling this function, you should use
1807 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 */
1809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001812 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
1814#ifdef MEM_PROFILE
1815 mem_pre_free(&x);
1816#endif
1817 free(x);
1818 }
1819}
1820
1821#ifndef HAVE_MEMSET
1822 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001823vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 char *p = ptr;
1826
1827 while (size-- > 0)
1828 *p++ = c;
1829 return ptr;
1830}
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1834/*
1835 * Compare two strings, ignoring case, using current locale.
1836 * Doesn't work for multi-byte characters.
1837 * return 0 for match, < 0 for smaller, > 0 for bigger
1838 */
1839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001840vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 int i;
1843
1844 for (;;)
1845 {
1846 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1847 if (i != 0)
1848 return i; /* this character different */
1849 if (*s1 == NUL)
1850 break; /* strings match until NUL */
1851 ++s1;
1852 ++s2;
1853 }
1854 return 0; /* strings match */
1855}
1856#endif
1857
1858#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1859/*
1860 * Compare two strings, for length "len", ignoring case, using current locale.
1861 * Doesn't work for multi-byte characters.
1862 * return 0 for match, < 0 for smaller, > 0 for bigger
1863 */
1864 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001865vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 int i;
1868
1869 while (len > 0)
1870 {
1871 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1872 if (i != 0)
1873 return i; /* this character different */
1874 if (*s1 == NUL)
1875 break; /* strings match until NUL */
1876 ++s1;
1877 ++s2;
1878 --len;
1879 }
1880 return 0; /* strings match */
1881}
1882#endif
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
1885 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001886 * with characters from 128 to 255 correctly. It also doesn't return a
1887 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 char_u *p;
1893 int b;
1894
1895 p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (enc_utf8 && c >= 0x80)
1897 {
1898 while (*p != NUL)
1899 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001900 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001901
1902 /* Avoid matching an illegal byte here. */
1903 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001905 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 return NULL;
1908 }
1909 if (enc_dbcs != 0 && c > 255)
1910 {
1911 int n2 = c & 0xff;
1912
1913 c = ((unsigned)c >> 8) & 0xff;
1914 while ((b = *p) != NUL)
1915 {
1916 if (b == c && p[1] == n2)
1917 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001918 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 return NULL;
1921 }
1922 if (has_mbyte)
1923 {
1924 while ((b = *p) != NUL)
1925 {
1926 if (b == c)
1927 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001928 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 return NULL;
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 while ((b = *p) != NUL)
1933 {
1934 if (b == c)
1935 return p;
1936 ++p;
1937 }
1938 return NULL;
1939}
1940
1941/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001942 * Version of strchr() that only works for bytes and handles unsigned char
1943 * strings with characters above 128 correctly. It also doesn't return a
1944 * pointer to the NUL at the end of the string.
1945 */
1946 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001948{
1949 char_u *p = string;
1950
1951 while (*p != NUL)
1952 {
1953 if (*p == c)
1954 return p;
1955 ++p;
1956 }
1957 return NULL;
1958}
1959
1960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001962 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001963 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
1965 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001966vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
1968 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001969 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Bram Moolenaar05159a02005-02-26 23:04:13 +00001971 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001973 if (*p == c)
1974 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001975 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 return retval;
1978}
1979
1980/*
1981 * Vim's version of strpbrk(), in case it's missing.
1982 * Don't generate a prototype for this, causes problems when it's not used.
1983 */
1984#ifndef PROTO
1985# ifndef HAVE_STRPBRK
1986# ifdef vim_strpbrk
1987# undef vim_strpbrk
1988# endif
1989 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001990vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992 while (*s)
1993 {
1994 if (vim_strchr(charset, *s) != NULL)
1995 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001996 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998 return NULL;
1999}
2000# endif
2001#endif
2002
2003/*
2004 * Vim has its own isspace() function, because on some machines isspace()
2005 * can't handle characters above 128.
2006 */
2007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 return ((x >= 9 && x <= 13) || x == ' ');
2011}
2012
2013/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002014 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 */
2016
2017/*
2018 * Clear an allocated growing array.
2019 */
2020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 vim_free(gap->ga_data);
2024 ga_init(gap);
2025}
2026
2027/*
2028 * Clear a growing array that contains a list of strings.
2029 */
2030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 int i;
2034
2035 for (i = 0; i < gap->ga_len; ++i)
2036 vim_free(((char_u **)(gap->ga_data))[i]);
2037 ga_clear(gap);
2038}
2039
2040/*
2041 * Initialize a growing array. Don't forget to set ga_itemsize and
2042 * ga_growsize! Or use ga_init2().
2043 */
2044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002048 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 gap->ga_len = 0;
2050}
2051
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 ga_init(gap);
2056 gap->ga_itemsize = itemsize;
2057 gap->ga_growsize = growsize;
2058}
2059
2060/*
2061 * Make room in growing array "gap" for at least "n" items.
2062 * Return FAIL for failure, OK otherwise.
2063 */
2064 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002065ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002067 size_t old_len;
2068 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 char_u *pp;
2070
Bram Moolenaar86b68352004-12-27 21:59:20 +00002071 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 if (n < gap->ga_growsize)
2074 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002075 new_len = gap->ga_itemsize * (gap->ga_len + n);
2076 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002077 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (pp == NULL)
2079 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002080 old_len = gap->ga_itemsize * gap->ga_maxlen;
2081 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002082 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 gap->ga_data = pp;
2084 }
2085 return OK;
2086}
2087
Bram Moolenaar113e1072019-01-20 15:30:40 +01002088#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002090 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002091 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002092 * Returns NULL when out of memory.
2093 */
2094 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002095ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002096{
2097 int i;
2098 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002099 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002100 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002101 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002102
2103 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002104 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002105
2106 s = alloc(len + 1);
2107 if (s != NULL)
2108 {
2109 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002110 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002111 for (i = 0; i < gap->ga_len; ++i)
2112 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002113 if (p != s)
2114 {
2115 STRCPY(p, sep);
2116 p += sep_len;
2117 }
2118 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2119 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002120 }
2121 }
2122 return s;
2123}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002124#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002125
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002126#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002127/*
2128 * Make a copy of string "p" and add it to "gap".
2129 * When out of memory nothing changes.
2130 */
2131 void
2132ga_add_string(garray_T *gap, char_u *p)
2133{
2134 char_u *cp = vim_strsave(p);
2135
2136 if (cp != NULL)
2137 {
2138 if (ga_grow(gap, 1) == OK)
2139 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2140 else
2141 vim_free(cp);
2142 }
2143}
2144#endif
2145
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002146/*
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002147 * Concatenate a string to a growarray which contains bytes.
Bram Moolenaar43345542015-11-29 17:35:35 +01002148 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * Note: Does NOT copy the NUL at the end!
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
Bram Moolenaar43345542015-11-29 17:35:35 +01002154 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaar478af672017-04-10 22:22:42 +02002156 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002157 return;
2158 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (ga_grow(gap, len) == OK)
2160 {
2161 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2162 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164}
2165
2166/*
2167 * Append one byte to a growarray which contains bytes.
2168 */
2169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002170ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 if (ga_grow(gap, 1) == OK)
2173 {
2174 *((char *)gap->ga_data + gap->ga_len) = c;
2175 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177}
2178
Bram Moolenaar597a4222014-06-25 14:39:50 +02002179#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2180 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002181/*
2182 * Append the text in "gap" below the cursor line and clear "gap".
2183 */
2184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002185append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002186{
2187 /* Remove trailing CR. */
2188 if (gap->ga_len > 0
2189 && !curbuf->b_p_bin
2190 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2191 --gap->ga_len;
2192 ga_append(gap, NUL);
2193 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2194 gap->ga_len = 0;
2195}
2196#endif
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198/************************************************************************
2199 * functions that use lookup tables for various things, generally to do with
2200 * special key codes.
2201 */
2202
2203/*
2204 * Some useful tables.
2205 */
2206
2207static struct modmasktable
2208{
2209 short mod_mask; /* Bit-mask for particular key modifier */
2210 short mod_flag; /* Bit(s) for particular key modifier */
2211 char_u name; /* Single letter name of modifier */
2212} mod_mask_table[] =
2213{
2214 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002215 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2217 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2218 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2219 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2220 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02002221#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2223#endif
2224 /* 'A' must be the last one */
2225 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2226 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002227 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228};
2229
2230/*
2231 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002232 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 */
2234#define MOD_KEYS_ENTRY_SIZE 5
2235
2236static char_u modifier_keys_table[] =
2237{
2238/* mod mask with modifier without modifier */
2239 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2240 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2241 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2242 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2243 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2244 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2245 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2246 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2247 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2248 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2249 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2250 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2251 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2252 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2253 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2254 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2255 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2256 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2257 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2258 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2259 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2260 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2261 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2262 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2263 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2264 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2265 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2266 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2267 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2268 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2269 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2272
2273 /* vt100 F1 */
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2278
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2289
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2300
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2308 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2310 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2311
2312 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2316 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2317 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2318 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2319
2320 /* TAB pseudo code*/
2321 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2322
2323 NUL
2324};
2325
2326static struct key_name_entry
2327{
2328 int key; /* Special key code or ascii value */
2329 char_u *name; /* Name of key */
2330} key_names_table[] =
2331{
2332 {' ', (char_u *)"Space"},
2333 {TAB, (char_u *)"Tab"},
2334 {K_TAB, (char_u *)"Tab"},
2335 {NL, (char_u *)"NL"},
2336 {NL, (char_u *)"NewLine"}, /* Alternative name */
2337 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2338 {NL, (char_u *)"LF"}, /* Alternative name */
2339 {CAR, (char_u *)"CR"},
2340 {CAR, (char_u *)"Return"}, /* Alternative name */
2341 {CAR, (char_u *)"Enter"}, /* Alternative name */
2342 {K_BS, (char_u *)"BS"},
2343 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2344 {ESC, (char_u *)"Esc"},
2345 {CSI, (char_u *)"CSI"},
2346 {K_CSI, (char_u *)"xCSI"},
2347 {'|', (char_u *)"Bar"},
2348 {'\\', (char_u *)"Bslash"},
2349 {K_DEL, (char_u *)"Del"},
2350 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2351 {K_KDEL, (char_u *)"kDel"},
2352 {K_UP, (char_u *)"Up"},
2353 {K_DOWN, (char_u *)"Down"},
2354 {K_LEFT, (char_u *)"Left"},
2355 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002356 {K_XUP, (char_u *)"xUp"},
2357 {K_XDOWN, (char_u *)"xDown"},
2358 {K_XLEFT, (char_u *)"xLeft"},
2359 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002360 {K_PS, (char_u *)"PasteStart"},
2361 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 {K_F1, (char_u *)"F1"},
2364 {K_F2, (char_u *)"F2"},
2365 {K_F3, (char_u *)"F3"},
2366 {K_F4, (char_u *)"F4"},
2367 {K_F5, (char_u *)"F5"},
2368 {K_F6, (char_u *)"F6"},
2369 {K_F7, (char_u *)"F7"},
2370 {K_F8, (char_u *)"F8"},
2371 {K_F9, (char_u *)"F9"},
2372 {K_F10, (char_u *)"F10"},
2373
2374 {K_F11, (char_u *)"F11"},
2375 {K_F12, (char_u *)"F12"},
2376 {K_F13, (char_u *)"F13"},
2377 {K_F14, (char_u *)"F14"},
2378 {K_F15, (char_u *)"F15"},
2379 {K_F16, (char_u *)"F16"},
2380 {K_F17, (char_u *)"F17"},
2381 {K_F18, (char_u *)"F18"},
2382 {K_F19, (char_u *)"F19"},
2383 {K_F20, (char_u *)"F20"},
2384
2385 {K_F21, (char_u *)"F21"},
2386 {K_F22, (char_u *)"F22"},
2387 {K_F23, (char_u *)"F23"},
2388 {K_F24, (char_u *)"F24"},
2389 {K_F25, (char_u *)"F25"},
2390 {K_F26, (char_u *)"F26"},
2391 {K_F27, (char_u *)"F27"},
2392 {K_F28, (char_u *)"F28"},
2393 {K_F29, (char_u *)"F29"},
2394 {K_F30, (char_u *)"F30"},
2395
2396 {K_F31, (char_u *)"F31"},
2397 {K_F32, (char_u *)"F32"},
2398 {K_F33, (char_u *)"F33"},
2399 {K_F34, (char_u *)"F34"},
2400 {K_F35, (char_u *)"F35"},
2401 {K_F36, (char_u *)"F36"},
2402 {K_F37, (char_u *)"F37"},
2403
2404 {K_XF1, (char_u *)"xF1"},
2405 {K_XF2, (char_u *)"xF2"},
2406 {K_XF3, (char_u *)"xF3"},
2407 {K_XF4, (char_u *)"xF4"},
2408
2409 {K_HELP, (char_u *)"Help"},
2410 {K_UNDO, (char_u *)"Undo"},
2411 {K_INS, (char_u *)"Insert"},
2412 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2413 {K_KINS, (char_u *)"kInsert"},
2414 {K_HOME, (char_u *)"Home"},
2415 {K_KHOME, (char_u *)"kHome"},
2416 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002417 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {K_END, (char_u *)"End"},
2419 {K_KEND, (char_u *)"kEnd"},
2420 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002421 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {K_PAGEUP, (char_u *)"PageUp"},
2423 {K_PAGEDOWN, (char_u *)"PageDown"},
2424 {K_KPAGEUP, (char_u *)"kPageUp"},
2425 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2426
2427 {K_KPLUS, (char_u *)"kPlus"},
2428 {K_KMINUS, (char_u *)"kMinus"},
2429 {K_KDIVIDE, (char_u *)"kDivide"},
2430 {K_KMULTIPLY, (char_u *)"kMultiply"},
2431 {K_KENTER, (char_u *)"kEnter"},
2432 {K_KPOINT, (char_u *)"kPoint"},
2433
2434 {K_K0, (char_u *)"k0"},
2435 {K_K1, (char_u *)"k1"},
2436 {K_K2, (char_u *)"k2"},
2437 {K_K3, (char_u *)"k3"},
2438 {K_K4, (char_u *)"k4"},
2439 {K_K5, (char_u *)"k5"},
2440 {K_K6, (char_u *)"k6"},
2441 {K_K7, (char_u *)"k7"},
2442 {K_K8, (char_u *)"k8"},
2443 {K_K9, (char_u *)"k9"},
2444
2445 {'<', (char_u *)"lt"},
2446
2447 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002448#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002450#endif
2451#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002453#endif
2454#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002456#endif
2457#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002459#endif
2460#ifdef FEAT_MOUSE_URXVT
2461 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2462#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002463#ifdef FEAT_MOUSE_SGR
2464 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002465 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2468 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2469 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2470 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2471 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002472 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2474 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2475 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2476 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2477 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2478 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002479 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2480 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2481 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2482 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2483 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2484 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {K_X1MOUSE, (char_u *)"X1Mouse"},
2486 {K_X1DRAG, (char_u *)"X1Drag"},
2487 {K_X1RELEASE, (char_u *)"X1Release"},
2488 {K_X2MOUSE, (char_u *)"X2Mouse"},
2489 {K_X2DRAG, (char_u *)"X2Drag"},
2490 {K_X2RELEASE, (char_u *)"X2Release"},
2491 {K_DROP, (char_u *)"Drop"},
2492 {K_ZERO, (char_u *)"Nul"},
2493#ifdef FEAT_EVAL
2494 {K_SNR, (char_u *)"SNR"},
2495#endif
2496 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002497 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002499 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500};
2501
2502#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2503
2504#ifdef FEAT_MOUSE
2505static struct mousetable
2506{
2507 int pseudo_code; /* Code for pseudo mouse event */
2508 int button; /* Which mouse button is it? */
2509 int is_click; /* Is it a mouse button click event? */
2510 int is_drag; /* Is it a mouse drag event? */
2511} mouse_table[] =
2512{
2513 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2514#ifdef FEAT_GUI
2515 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2516#endif
2517 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2518 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2519#ifdef FEAT_GUI
2520 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2521#endif
2522 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2523 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2524 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2525 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2526 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2527 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2528 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2529 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2530 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2531 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2532 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2533 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2534 /* DRAG without CLICK */
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002535 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* RELEASE without CLICK */
2537 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2538 {0, 0, 0, 0},
2539};
2540#endif /* FEAT_MOUSE */
2541
2542/*
2543 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2544 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2545 */
2546 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002547name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549 int i;
2550
2551 c = TOUPPER_ASC(c);
2552 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2553 if (c == mod_mask_table[i].name)
2554 return mod_mask_table[i].mod_flag;
2555 return 0;
2556}
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558/*
2559 * Check if if there is a special key code for "key" that includes the
2560 * modifiers specified.
2561 */
2562 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002563simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 int i;
2566 int key0;
2567 int key1;
2568
2569 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2570 {
2571 /* TAB is a special case */
2572 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2573 {
2574 *modifiers &= ~MOD_MASK_SHIFT;
2575 return K_S_TAB;
2576 }
2577 key0 = KEY2TERMCAP0(key);
2578 key1 = KEY2TERMCAP1(key);
2579 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2580 if (key0 == modifier_keys_table[i + 3]
2581 && key1 == modifier_keys_table[i + 4]
2582 && (*modifiers & modifier_keys_table[i]))
2583 {
2584 *modifiers &= ~modifier_keys_table[i];
2585 return TERMCAP2KEY(modifier_keys_table[i + 1],
2586 modifier_keys_table[i + 2]);
2587 }
2588 }
2589 return key;
2590}
2591
2592/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002593 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002594 */
2595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002596handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002597{
2598 switch (key)
2599 {
2600 case K_XUP: return K_UP;
2601 case K_XDOWN: return K_DOWN;
2602 case K_XLEFT: return K_LEFT;
2603 case K_XRIGHT: return K_RIGHT;
2604 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002605 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002606 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002607 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002608 case K_XF1: return K_F1;
2609 case K_XF2: return K_F2;
2610 case K_XF3: return K_F3;
2611 case K_XF4: return K_F4;
2612 case K_S_XF1: return K_S_F1;
2613 case K_S_XF2: return K_S_F2;
2614 case K_S_XF3: return K_S_F3;
2615 case K_S_XF4: return K_S_F4;
2616 }
2617 return key;
2618}
2619
2620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * Return a string which contains the name of the given key when the given
2622 * modifiers are down.
2623 */
2624 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002625get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 static char_u string[MAX_KEY_NAME_LEN + 1];
2628
2629 int i, idx;
2630 int table_idx;
2631 char_u *s;
2632
2633 string[0] = '<';
2634 idx = 1;
2635
2636 /* Key that stands for a normal character. */
2637 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2638 c = KEY2TERMCAP1(c);
2639
2640 /*
2641 * Translate shifted special keys into unshifted keys and set modifier.
2642 * Same for CTRL and ALT modifiers.
2643 */
2644 if (IS_SPECIAL(c))
2645 {
2646 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2647 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2648 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2649 {
2650 modifiers |= modifier_keys_table[i];
2651 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2652 modifier_keys_table[i + 4]);
2653 break;
2654 }
2655 }
2656
2657 /* try to find the key in the special key table */
2658 table_idx = find_special_key_in_table(c);
2659
2660 /*
2661 * When not a known special key, and not a printable character, try to
2662 * extract modifiers.
2663 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002664 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
2666 if (table_idx < 0
2667 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2668 && (c & 0x80))
2669 {
2670 c &= 0x7f;
2671 modifiers |= MOD_MASK_ALT;
2672 /* try again, to find the un-alted key in the special key table */
2673 table_idx = find_special_key_in_table(c);
2674 }
2675 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2676 {
2677#ifdef EBCDIC
2678 c = CtrlChar(c);
2679#else
2680 c += '@';
2681#endif
2682 modifiers |= MOD_MASK_CTRL;
2683 }
2684 }
2685
2686 /* translate the modifier into a string */
2687 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2688 if ((modifiers & mod_mask_table[i].mod_mask)
2689 == mod_mask_table[i].mod_flag)
2690 {
2691 string[idx++] = mod_mask_table[i].name;
2692 string[idx++] = (char_u)'-';
2693 }
2694
2695 if (table_idx < 0) /* unknown special key, may output t_xx */
2696 {
2697 if (IS_SPECIAL(c))
2698 {
2699 string[idx++] = 't';
2700 string[idx++] = '_';
2701 string[idx++] = KEY2TERMCAP0(c);
2702 string[idx++] = KEY2TERMCAP1(c);
2703 }
2704 /* Not a special key, only modifiers, output directly */
2705 else
2706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (has_mbyte && (*mb_char2len)(c) > 1)
2708 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002709 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 string[idx++] = c;
2711 else
2712 {
2713 s = transchar(c);
2714 while (*s)
2715 string[idx++] = *s++;
2716 }
2717 }
2718 }
2719 else /* use name of special key */
2720 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002721 size_t len = STRLEN(key_names_table[table_idx].name);
2722
2723 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2724 {
2725 STRCPY(string + idx, key_names_table[table_idx].name);
2726 idx += (int)len;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
2729 string[idx++] = '>';
2730 string[idx] = NUL;
2731 return string;
2732}
2733
2734/*
2735 * Try translating a <> name at (*srcp)[] to dst[].
2736 * Return the number of characters added to dst[], zero for no match.
2737 * If there is a match, srcp is advanced to after the <> name.
2738 * dst[] must be big enough to hold the result (up to six characters)!
2739 */
2740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002741trans_special(
2742 char_u **srcp,
2743 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002744 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2745 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 int modifiers = 0;
2748 int key;
2749 int dlen = 0;
2750
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002751 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (key == 0)
2753 return 0;
2754
2755 /* Put the appropriate modifier in a string */
2756 if (modifiers != 0)
2757 {
2758 dst[dlen++] = K_SPECIAL;
2759 dst[dlen++] = KS_MODIFIER;
2760 dst[dlen++] = modifiers;
2761 }
2762
2763 if (IS_SPECIAL(key))
2764 {
2765 dst[dlen++] = K_SPECIAL;
2766 dst[dlen++] = KEY2TERMCAP0(key);
2767 dst[dlen++] = KEY2TERMCAP1(key);
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else if (has_mbyte && !keycode)
2770 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else if (keycode)
2772 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2773 else
2774 dst[dlen++] = key;
2775
2776 return dlen;
2777}
2778
2779/*
2780 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2781 * srcp is advanced to after the <> name.
2782 * returns 0 if there is no match.
2783 */
2784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002785find_special_key(
2786 char_u **srcp,
2787 int *modp,
2788 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002789 int keep_x_key, /* don't translate xHome to Home key */
2790 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 char_u *last_dash;
2793 char_u *end_of_name;
2794 char_u *src;
2795 char_u *bp;
2796 int modifiers;
2797 int bit;
2798 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002799 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002800 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 src = *srcp;
2803 if (src[0] != '<')
2804 return 0;
2805
2806 /* Find end of modifier list */
2807 last_dash = src;
2808 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2809 {
2810 if (*bp == '-')
2811 {
2812 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002813 if (bp[1] != NUL)
2814 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002815 if (has_mbyte)
2816 l = mb_ptr2len(bp + 1);
2817 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002818 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002819 /* Anything accepted, like <C-?>.
2820 * <C-"> or <M-"> are not special in strings as " is
2821 * the string delimiter. With a backslash it works: <M-\"> */
2822 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002823 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002824 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2825 && bp[3] == '>')
2826 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2830 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002831 else if (STRNICMP(bp, "char-", 5) == 0)
2832 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002833 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002834 bp += l + 5;
2835 break;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838
2839 if (*bp == '>') /* found matching '>' */
2840 {
2841 end_of_name = bp + 1;
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /* Which modifiers are given? */
2844 modifiers = 0x0;
2845 for (bp = src + 1; bp < last_dash; bp++)
2846 {
2847 if (*bp != '-')
2848 {
2849 bit = name_to_mod_mask(*bp);
2850 if (bit == 0x0)
2851 break; /* Illegal modifier name */
2852 modifiers |= bit;
2853 }
2854 }
2855
2856 /*
2857 * Legal modifier name.
2858 */
2859 if (bp >= last_dash)
2860 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002861 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2862 && VIM_ISDIGIT(last_dash[6]))
2863 {
2864 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002865 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002866 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002869 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002870 int off = 1;
2871
2872 /* Modifier with single letter, or special key name. */
2873 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2874 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002875 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002876 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002877 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002878 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002879 if (modifiers != 0 && last_dash[l + off] == '>')
2880 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002881 else
2882 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002883 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002884 if (!keep_x_key)
2885 key = handle_x_keys(key);
2886 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889 /*
2890 * get_special_key_code() may return NUL for invalid
2891 * special key name.
2892 */
2893 if (key != NUL)
2894 {
2895 /*
2896 * Only use a modifier when there is no special key code that
2897 * includes the modifier.
2898 */
2899 key = simplify_key(key, &modifiers);
2900
2901 if (!keycode)
2902 {
2903 /* don't want keycode, use single byte code */
2904 if (key == K_BS)
2905 key = BS;
2906 else if (key == K_DEL || key == K_KDEL)
2907 key = DEL;
2908 }
2909
2910 /*
2911 * Normal Key with modifier: Try to make a single byte code.
2912 */
2913 if (!IS_SPECIAL(key))
2914 key = extract_modifiers(key, &modifiers);
2915
2916 *modp = modifiers;
2917 *srcp = end_of_name;
2918 return key;
2919 }
2920 }
2921 }
2922 return 0;
2923}
2924
2925/*
2926 * Try to include modifiers in the key.
2927 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2928 */
2929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002930extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 int modifiers = *modp;
2933
Bram Moolenaard0573012017-10-28 21:11:06 +02002934#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002935 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (!(modifiers & MOD_MASK_CMD))
2937#endif
2938 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2939 {
2940 key = TOUPPER_ASC(key);
2941 modifiers &= ~MOD_MASK_SHIFT;
2942 }
2943 if ((modifiers & MOD_MASK_CTRL)
2944#ifdef EBCDIC
2945 /* * TODO: EBCDIC Better use:
2946 * && (Ctrl_chr(key) || key == '?')
2947 * ??? */
2948 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2949 != NULL
2950#else
2951 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2952#endif
2953 )
2954 {
2955 key = Ctrl_chr(key);
2956 modifiers &= ~MOD_MASK_CTRL;
2957 /* <C-@> is <Nul> */
2958 if (key == 0)
2959 key = K_ZERO;
2960 }
Bram Moolenaard0573012017-10-28 21:11:06 +02002961#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002962 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (!(modifiers & MOD_MASK_CMD))
2964#endif
2965 if ((modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002966 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 key |= 0x80;
2969 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2970 }
2971
2972 *modp = modifiers;
2973 return key;
2974}
2975
2976/*
2977 * Try to find key "c" in the special key table.
2978 * Return the index when found, -1 when not found.
2979 */
2980 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002981find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int i;
2984
2985 for (i = 0; key_names_table[i].name != NULL; i++)
2986 if (c == key_names_table[i].key)
2987 break;
2988 if (key_names_table[i].name == NULL)
2989 i = -1;
2990 return i;
2991}
2992
2993/*
2994 * Find the special key with the given name (the given string does not have to
2995 * end with NUL, the name is assumed to end before the first non-idchar).
2996 * If the name starts with "t_" the next two characters are interpreted as a
2997 * termcap name.
2998 * Return the key code, or 0 if not found.
2999 */
3000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003001get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 char_u *table_name;
3004 char_u string[3];
3005 int i, j;
3006
3007 /*
3008 * If it's <t_xx> we get the code for xx from the termcap
3009 */
3010 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3011 {
3012 string[0] = name[2];
3013 string[1] = name[3];
3014 string[2] = NUL;
3015 if (add_termcap_entry(string, FALSE) == OK)
3016 return TERMCAP2KEY(name[2], name[3]);
3017 }
3018 else
3019 for (i = 0; key_names_table[i].name != NULL; i++)
3020 {
3021 table_name = key_names_table[i].name;
3022 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3023 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3024 break;
3025 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3026 return key_names_table[i].key;
3027 }
3028 return 0;
3029}
3030
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003031#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003033get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003035 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 return NULL;
3037 return key_names_table[i].name;
3038}
3039#endif
3040
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003041#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042/*
3043 * Look up the given mouse code to return the relevant information in the other
3044 * arguments. Return which button is down or was released.
3045 */
3046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003047get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 int i;
3050
3051 for (i = 0; mouse_table[i].pseudo_code; i++)
3052 if (code == mouse_table[i].pseudo_code)
3053 {
3054 *is_click = mouse_table[i].is_click;
3055 *is_drag = mouse_table[i].is_drag;
3056 return mouse_table[i].button;
3057 }
3058 return 0; /* Shouldn't get here */
3059}
3060
3061/*
3062 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3063 * the given information about which mouse button is down, and whether the
3064 * mouse was clicked, dragged or released.
3065 */
3066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067get_pseudo_mouse_code(
3068 int button, /* eg MOUSE_LEFT */
3069 int is_click,
3070 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 int i;
3073
3074 for (i = 0; mouse_table[i].pseudo_code; i++)
3075 if (button == mouse_table[i].button
3076 && is_click == mouse_table[i].is_click
3077 && is_drag == mouse_table[i].is_drag)
3078 {
3079#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003080 /* Trick: a non mappable left click and release has mouse_col -1
3081 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3082 * gui_mouse_moved() */
3083 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003085 if (mouse_col < 0)
3086 mouse_col = 0;
3087 else
3088 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3090 return (int)KE_LEFTMOUSE_NM;
3091 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3092 return (int)KE_LEFTRELEASE_NM;
3093 }
3094#endif
3095 return mouse_table[i].pseudo_code;
3096 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003097 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099#endif /* FEAT_MOUSE */
3100
3101/*
3102 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3103 */
3104 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003105get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 int c = *buf->b_p_ff;
3108
3109 if (buf->b_p_bin || c == 'u')
3110 return EOL_UNIX;
3111 if (c == 'm')
3112 return EOL_MAC;
3113 return EOL_DOS;
3114}
3115
3116/*
3117 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3118 * argument.
3119 */
3120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121get_fileformat_force(
3122 buf_T *buf,
3123 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 int c;
3126
3127 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003128 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 else
3130 {
3131 if ((eap != NULL && eap->force_bin != 0)
3132 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3133 return EOL_UNIX;
3134 c = *buf->b_p_ff;
3135 }
3136 if (c == 'u')
3137 return EOL_UNIX;
3138 if (c == 'm')
3139 return EOL_MAC;
3140 return EOL_DOS;
3141}
3142
3143/*
3144 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3145 * Sets both 'textmode' and 'fileformat'.
3146 * Note: Does _not_ set global value of 'textmode'!
3147 */
3148 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149set_fileformat(
3150 int t,
3151 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 char *p = NULL;
3154
3155 switch (t)
3156 {
3157 case EOL_DOS:
3158 p = FF_DOS;
3159 curbuf->b_p_tx = TRUE;
3160 break;
3161 case EOL_UNIX:
3162 p = FF_UNIX;
3163 curbuf->b_p_tx = FALSE;
3164 break;
3165 case EOL_MAC:
3166 p = FF_MAC;
3167 curbuf->b_p_tx = FALSE;
3168 break;
3169 }
3170 if (p != NULL)
3171 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003172 OPT_FREE | opt_flags, 0);
3173
Bram Moolenaarf740b292006-02-16 22:11:02 +00003174 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003176 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#ifdef FEAT_TITLE
3178 need_maketitle = TRUE; /* set window title later */
3179#endif
3180}
3181
3182/*
3183 * Return the default fileformat from 'fileformats'.
3184 */
3185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003186default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 switch (*p_ffs)
3189 {
3190 case 'm': return EOL_MAC;
3191 case 'd': return EOL_DOS;
3192 }
3193 return EOL_UNIX;
3194}
3195
3196/*
3197 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3198 */
3199 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003200call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 char_u *ncmd;
3203 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003204#ifdef FEAT_PROFILE
3205 proftime_T wait_time;
3206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208 if (p_verbose > 3)
3209 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003210 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003211 smsg(_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 cmd == NULL ? p_sh : cmd);
3213 out_char('\n');
3214 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003215 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217
Bram Moolenaar05159a02005-02-26 23:04:13 +00003218#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003219 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003220 prof_child_enter(&wait_time);
3221#endif
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (*p_sh == NUL)
3224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003225 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 retval = -1;
3227 }
3228 else
3229 {
3230#ifdef FEAT_GUI_MSWIN
3231 /* Don't hide the pointer while executing a shell command. */
3232 gui_mch_mousehide(FALSE);
3233#endif
3234#ifdef FEAT_GUI
3235 ++hold_gui_events;
3236#endif
3237 /* The external command may update a tags file, clear cached tags. */
3238 tag_freematch();
3239
3240 if (cmd == NULL || *p_sxq == NUL)
3241 retval = mch_call_shell(cmd, opt);
3242 else
3243 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003244 char_u *ecmd = cmd;
3245
3246 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3247 {
3248 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3249 if (ecmd == NULL)
3250 ecmd = cmd;
3251 }
3252 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (ncmd != NULL)
3254 {
3255 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003256 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003257 /* When 'shellxquote' is ( append ).
3258 * When 'shellxquote' is "( append )". */
3259 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3260 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3261 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 retval = mch_call_shell(ncmd, opt);
3263 vim_free(ncmd);
3264 }
3265 else
3266 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003267 if (ecmd != cmd)
3268 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270#ifdef FEAT_GUI
3271 --hold_gui_events;
3272#endif
3273 /*
3274 * Check the window size, in case it changed while executing the
3275 * external command.
3276 */
3277 shell_resized_check();
3278 }
3279
3280#ifdef FEAT_EVAL
3281 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003282# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003283 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003284 prof_child_exit(&wait_time);
3285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
3287
3288 return retval;
3289}
3290
3291/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003292 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3293 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 */
3295 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003296get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (State & NORMAL)
3299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003301 {
3302 if (VIsual_select)
3303 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003305 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003306 else if (finish_op)
3307 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 return State;
3310}
3311
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003312/*
3313 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003314 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003315 * "b" must point to the start of the file name
3316 */
3317 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003318after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003319{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003320 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003321 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3322}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003323
3324/*
3325 * Return TRUE if file names "f1" and "f2" are in the same directory.
3326 * "f1" may be a short name, "f2" must be a full path.
3327 */
3328 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003329same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003330{
3331 char_u ffname[MAXPATHL];
3332 char_u *t1;
3333 char_u *t2;
3334
3335 /* safety check */
3336 if (f1 == NULL || f2 == NULL)
3337 return FALSE;
3338
3339 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3340 t1 = gettail_sep(ffname);
3341 t2 = gettail_sep(f2);
3342 return (t1 - ffname == t2 - f2
3343 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3344}
3345
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02003346#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
3347 || defined(MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01003348 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 || defined(PROTO)
3350/*
3351 * Change to a file's directory.
3352 * Caller must call shorten_fnames()!
3353 * Return OK or FAIL.
3354 */
3355 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003356vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003358 char_u old_dir[MAXPATHL];
3359 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003360 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003362 if (mch_dirname(old_dir, MAXPATHL) != OK)
3363 *old_dir = NUL;
3364
3365 vim_strncpy(new_dir, fname, MAXPATHL - 1);
3366 *gettail_sep(new_dir) = NUL;
3367
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01003368 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003369 // nothing to do
3370 res = OK;
3371 else
3372 {
3373 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
3374
3375 if (res == OK && trigger_autocmd != NULL)
3376 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
3377 new_dir, FALSE, curbuf);
3378 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003379 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381#endif
3382
3383#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3384/*
3385 * Check if "name" ends in a slash and is not a directory.
3386 * Used for systems where stat() ignores a trailing slash on a file name.
3387 * The Vim code assumes a trailing slash is only ignored for a directory.
3388 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003389 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003390illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 if (name[0] == NUL)
3393 return FALSE; /* no file name is not illegal */
3394 if (name[strlen(name) - 1] != '/')
3395 return FALSE; /* no trailing slash */
3396 if (mch_isdir((char_u *)name))
3397 return FALSE; /* trailing slash for a directory */
3398 return TRUE;
3399}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003400
3401/*
3402 * Special implementation of mch_stat() for Solaris.
3403 */
3404 int
3405vim_stat(const char *name, stat_T *stp)
3406{
3407 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3408 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003409 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003410}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#endif
3412
3413#if defined(CURSOR_SHAPE) || defined(PROTO)
3414
3415/*
3416 * Handling of cursor and mouse pointer shapes in various modes.
3417 */
3418
3419cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3420{
3421 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3422 * defaults when Vim starts.
3423 * Adjust the SHAPE_IDX_ defines when making changes! */
3424 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3425 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3426 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3427 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3428 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3429 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3430 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3431 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3432 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3433 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3434 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3435 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3436 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3437 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3438 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3439 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3440 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3441};
3442
3443#ifdef FEAT_MOUSESHAPE
3444/*
3445 * Table with names for mouse shapes. Keep in sync with all the tables for
3446 * mch_set_mouse_shape()!.
3447 */
3448static char * mshape_names[] =
3449{
3450 "arrow", /* default, must be the first one */
3451 "blank", /* hidden */
3452 "beam",
3453 "updown",
3454 "udsizing",
3455 "leftright",
3456 "lrsizing",
3457 "busy",
3458 "no",
3459 "crosshair",
3460 "hand1",
3461 "hand2",
3462 "pencil",
3463 "question",
3464 "rightup-arrow",
3465 "up-arrow",
3466 NULL
3467};
3468#endif
3469
3470/*
3471 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3472 * ("what" is SHAPE_MOUSE).
3473 * Returns error message for an illegal option, NULL otherwise.
3474 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003476parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 char_u *modep;
3479 char_u *colonp;
3480 char_u *commap;
3481 char_u *slashp;
3482 char_u *p, *endp;
3483 int idx = 0; /* init for GCC */
3484 int all_idx;
3485 int len;
3486 int i;
3487 long n;
3488 int found_ve = FALSE; /* found "ve" flag */
3489 int round;
3490
3491 /*
3492 * First round: check for errors; second round: do it for real.
3493 */
3494 for (round = 1; round <= 2; ++round)
3495 {
3496 /*
3497 * Repeat for all comma separated parts.
3498 */
3499#ifdef FEAT_MOUSESHAPE
3500 if (what == SHAPE_MOUSE)
3501 modep = p_mouseshape;
3502 else
3503#endif
3504 modep = p_guicursor;
3505 while (*modep != NUL)
3506 {
3507 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003508 commap = vim_strchr(modep, ',');
3509
3510 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003511 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /*
3516 * Repeat for all mode's before the colon.
3517 * For the 'a' mode, we loop to handle all the modes.
3518 */
3519 all_idx = -1;
3520 while (modep < colonp || all_idx >= 0)
3521 {
3522 if (all_idx < 0)
3523 {
3524 /* Find the mode. */
3525 if (modep[1] == '-' || modep[1] == ':')
3526 len = 1;
3527 else
3528 len = 2;
3529 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3530 all_idx = SHAPE_IDX_COUNT - 1;
3531 else
3532 {
3533 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3534 if (STRNICMP(modep, shape_table[idx].name, len)
3535 == 0)
3536 break;
3537 if (idx == SHAPE_IDX_COUNT
3538 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003539 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3541 found_ve = TRUE;
3542 }
3543 modep += len + 1;
3544 }
3545
3546 if (all_idx >= 0)
3547 idx = all_idx--;
3548 else if (round == 2)
3549 {
3550#ifdef FEAT_MOUSESHAPE
3551 if (what == SHAPE_MOUSE)
3552 {
3553 /* Set the default, for the missing parts */
3554 shape_table[idx].mshape = 0;
3555 }
3556 else
3557#endif
3558 {
3559 /* Set the defaults, for the missing parts */
3560 shape_table[idx].shape = SHAPE_BLOCK;
3561 shape_table[idx].blinkwait = 700L;
3562 shape_table[idx].blinkon = 400L;
3563 shape_table[idx].blinkoff = 250L;
3564 }
3565 }
3566
3567 /* Parse the part after the colon */
3568 for (p = colonp + 1; *p && *p != ','; )
3569 {
3570#ifdef FEAT_MOUSESHAPE
3571 if (what == SHAPE_MOUSE)
3572 {
3573 for (i = 0; ; ++i)
3574 {
3575 if (mshape_names[i] == NULL)
3576 {
3577 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003578 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (round == 2)
3580 shape_table[idx].mshape =
3581 getdigits(&p) + MSHAPE_NUMBERED;
3582 else
3583 (void)getdigits(&p);
3584 break;
3585 }
3586 len = (int)STRLEN(mshape_names[i]);
3587 if (STRNICMP(p, mshape_names[i], len) == 0)
3588 {
3589 if (round == 2)
3590 shape_table[idx].mshape = i;
3591 p += len;
3592 break;
3593 }
3594 }
3595 }
3596 else /* if (what == SHAPE_MOUSE) */
3597#endif
3598 {
3599 /*
3600 * First handle the ones with a number argument.
3601 */
3602 i = *p;
3603 len = 0;
3604 if (STRNICMP(p, "ver", 3) == 0)
3605 len = 3;
3606 else if (STRNICMP(p, "hor", 3) == 0)
3607 len = 3;
3608 else if (STRNICMP(p, "blinkwait", 9) == 0)
3609 len = 9;
3610 else if (STRNICMP(p, "blinkon", 7) == 0)
3611 len = 7;
3612 else if (STRNICMP(p, "blinkoff", 8) == 0)
3613 len = 8;
3614 if (len != 0)
3615 {
3616 p += len;
3617 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003618 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 n = getdigits(&p);
3620 if (len == 3) /* "ver" or "hor" */
3621 {
3622 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003623 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (round == 2)
3625 {
3626 if (TOLOWER_ASC(i) == 'v')
3627 shape_table[idx].shape = SHAPE_VER;
3628 else
3629 shape_table[idx].shape = SHAPE_HOR;
3630 shape_table[idx].percentage = n;
3631 }
3632 }
3633 else if (round == 2)
3634 {
3635 if (len == 9)
3636 shape_table[idx].blinkwait = n;
3637 else if (len == 7)
3638 shape_table[idx].blinkon = n;
3639 else
3640 shape_table[idx].blinkoff = n;
3641 }
3642 }
3643 else if (STRNICMP(p, "block", 5) == 0)
3644 {
3645 if (round == 2)
3646 shape_table[idx].shape = SHAPE_BLOCK;
3647 p += 5;
3648 }
3649 else /* must be a highlight group name then */
3650 {
3651 endp = vim_strchr(p, '-');
3652 if (commap == NULL) /* last part */
3653 {
3654 if (endp == NULL)
3655 endp = p + STRLEN(p); /* find end of part */
3656 }
3657 else if (endp > commap || endp == NULL)
3658 endp = commap;
3659 slashp = vim_strchr(p, '/');
3660 if (slashp != NULL && slashp < endp)
3661 {
3662 /* "group/langmap_group" */
3663 i = syn_check_group(p, (int)(slashp - p));
3664 p = slashp + 1;
3665 }
3666 if (round == 2)
3667 {
3668 shape_table[idx].id = syn_check_group(p,
3669 (int)(endp - p));
3670 shape_table[idx].id_lm = shape_table[idx].id;
3671 if (slashp != NULL && slashp < endp)
3672 shape_table[idx].id = i;
3673 }
3674 p = endp;
3675 }
3676 } /* if (what != SHAPE_MOUSE) */
3677
3678 if (*p == '-')
3679 ++p;
3680 }
3681 }
3682 modep = p;
3683 if (*modep == ',')
3684 ++modep;
3685 }
3686 }
3687
3688 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3689 if (!found_ve)
3690 {
3691#ifdef FEAT_MOUSESHAPE
3692 if (what == SHAPE_MOUSE)
3693 {
3694 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3695 }
3696 else
3697#endif
3698 {
3699 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3700 shape_table[SHAPE_IDX_VE].percentage =
3701 shape_table[SHAPE_IDX_V].percentage;
3702 shape_table[SHAPE_IDX_VE].blinkwait =
3703 shape_table[SHAPE_IDX_V].blinkwait;
3704 shape_table[SHAPE_IDX_VE].blinkon =
3705 shape_table[SHAPE_IDX_V].blinkon;
3706 shape_table[SHAPE_IDX_VE].blinkoff =
3707 shape_table[SHAPE_IDX_V].blinkoff;
3708 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3709 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3710 }
3711 }
3712
3713 return NULL;
3714}
3715
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003716# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3717 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * Return the index into shape_table[] for the current mode.
3720 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3721 */
3722 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003723get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725#ifdef FEAT_MOUSESHAPE
3726 if (mouse && (State == HITRETURN || State == ASKMORE))
3727 {
3728# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003729 int x, y;
3730 gui_mch_getmouse(&x, &y);
3731 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 return SHAPE_IDX_MOREL;
3733# endif
3734 return SHAPE_IDX_MORE;
3735 }
3736 if (mouse && drag_status_line)
3737 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (mouse && drag_sep_line)
3739 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#endif
3741 if (!mouse && State == SHOWMATCH)
3742 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (State & VREPLACE_FLAG)
3744 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (State & REPLACE_FLAG)
3746 return SHAPE_IDX_R;
3747 if (State & INSERT)
3748 return SHAPE_IDX_I;
3749 if (State & CMDLINE)
3750 {
3751 if (cmdline_at_end())
3752 return SHAPE_IDX_C;
3753 if (cmdline_overstrike())
3754 return SHAPE_IDX_CR;
3755 return SHAPE_IDX_CI;
3756 }
3757 if (finish_op)
3758 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 if (VIsual_active)
3760 {
3761 if (*p_sel == 'e')
3762 return SHAPE_IDX_VE;
3763 else
3764 return SHAPE_IDX_V;
3765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 return SHAPE_IDX_N;
3767}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3771static int old_mouse_shape = 0;
3772
3773/*
3774 * Set the mouse shape:
3775 * If "shape" is -1, use shape depending on the current mode,
3776 * depending on the current state.
3777 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3778 * when the mouse moves off the status or command line).
3779 */
3780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003781update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 int new_mouse_shape;
3784
3785 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003786 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return;
3788
3789 /* Postpone the updating when more is to come. Speeds up executing of
3790 * mappings. */
3791 if (shape_idx == -1 && char_avail())
3792 {
3793 postponed_mouseshape = TRUE;
3794 return;
3795 }
3796
Bram Moolenaar14716812006-05-04 21:54:08 +00003797 /* When ignoring the mouse don't change shape on the statusline. */
3798 if (*p_mouse == NUL
3799 && (shape_idx == SHAPE_IDX_CLINE
3800 || shape_idx == SHAPE_IDX_STATUS
3801 || shape_idx == SHAPE_IDX_VSEP))
3802 shape_idx = -2;
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (shape_idx == -2
3805 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3806 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3807 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3808 return;
3809 if (shape_idx < 0)
3810 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3811 else
3812 new_mouse_shape = shape_table[shape_idx].mshape;
3813 if (new_mouse_shape != old_mouse_shape)
3814 {
3815 mch_set_mouse_shape(new_mouse_shape);
3816 old_mouse_shape = new_mouse_shape;
3817 }
3818 postponed_mouseshape = FALSE;
3819}
3820# endif
3821
3822#endif /* CURSOR_SHAPE */
3823
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825/* TODO: make some #ifdef for this */
3826/*--------[ file searching ]-------------------------------------------------*/
3827/*
3828 * File searching functions for 'path', 'tags' and 'cdpath' options.
3829 * External visible functions:
3830 * vim_findfile_init() creates/initialises the search context
3831 * vim_findfile_free_visited() free list of visited files/dirs of search
3832 * context
3833 * vim_findfile() find a file in the search context
3834 * vim_findfile_cleanup() cleanup/free search context created by
3835 * vim_findfile_init()
3836 *
3837 * All static functions and variables start with 'ff_'
3838 *
3839 * In general it works like this:
3840 * First you create yourself a search context by calling vim_findfile_init().
3841 * It is possible to give a search context from a previous call to
3842 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3843 * until you are satisfied with the result or it returns NULL. On every call it
3844 * returns the next file which matches the conditions given to
3845 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3846 *
3847 * It is possible to call vim_findfile_init() again to reinitialise your search
3848 * with some new parameters. Don't forget to pass your old search context to
3849 * it, so it can reuse it and especially reuse the list of already visited
3850 * directories. If you want to delete the list of already visited directories
3851 * simply call vim_findfile_free_visited().
3852 *
3853 * When you are done call vim_findfile_cleanup() to free the search context.
3854 *
3855 * The function vim_findfile_init() has a long comment, which describes the
3856 * needed parameters.
3857 *
3858 *
3859 *
3860 * ATTENTION:
3861 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003862 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 * thread-safe!!!!!
3864 *
3865 * To minimize parameter passing (or because I'm to lazy), only the
3866 * external visible functions get a search context as a parameter. This is
3867 * then assigned to a static global, which is used throughout the local
3868 * functions.
3869 */
3870
3871/*
3872 * type for the directory search stack
3873 */
3874typedef struct ff_stack
3875{
3876 struct ff_stack *ffs_prev;
3877
3878 /* the fix part (no wildcards) and the part containing the wildcards
3879 * of the search path
3880 */
3881 char_u *ffs_fix_path;
3882#ifdef FEAT_PATH_EXTRA
3883 char_u *ffs_wc_path;
3884#endif
3885
3886 /* files/dirs found in the above directory, matched by the first wildcard
3887 * of wc_part
3888 */
3889 char_u **ffs_filearray;
3890 int ffs_filearray_size;
3891 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3892
3893 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003894 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 int ffs_stage;
3898
3899 /* How deep are we in the directory tree?
3900 * Counts backward from value of level parameter to vim_findfile_init
3901 */
3902 int ffs_level;
3903
3904 /* Did we already expand '**' to an empty string? */
3905 int ffs_star_star_empty;
3906} ff_stack_T;
3907
3908/*
3909 * type for already visited directories or files.
3910 */
3911typedef struct ff_visited
3912{
3913 struct ff_visited *ffv_next;
3914
3915#ifdef FEAT_PATH_EXTRA
3916 /* Visited directories are different if the wildcard string are
3917 * different. So we have to save it.
3918 */
3919 char_u *ffv_wc_path;
3920#endif
3921 /* for unix use inode etc for comparison (needed because of links), else
3922 * use filename.
3923 */
3924#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003925 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3926 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 ino_t ffv_ino; /* inode number */
3928#endif
3929 /* The memory for this struct is allocated according to the length of
3930 * ffv_fname.
3931 */
3932 char_u ffv_fname[1]; /* actually longer */
3933} ff_visited_T;
3934
3935/*
3936 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003937 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3939 * So we have to do 3 searches:
3940 * 1) search from the current files directory downward for the file "tags"
3941 * 2) search from the current files directory downward for the file "TAGS"
3942 * 3) search from Vims current directory downwards for the file "tags"
3943 * As you can see, the first and the third search are for the same file, so for
3944 * the third search we can use the visited list of the first search. For the
3945 * second search we must start from a empty visited list.
3946 * The struct ff_visited_list_hdr is used to manage a linked list of already
3947 * visited lists.
3948 */
3949typedef struct ff_visited_list_hdr
3950{
3951 struct ff_visited_list_hdr *ffvl_next;
3952
3953 /* the filename the attached visited list is for */
3954 char_u *ffvl_filename;
3955
3956 ff_visited_T *ffvl_visited_list;
3957
3958} ff_visited_list_hdr_T;
3959
3960
3961/*
3962 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003963 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 */
3965#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967/*
3968 * The search context:
3969 * ffsc_stack_ptr: the stack for the dirs to search
3970 * ffsc_visited_list: the currently active visited list
3971 * ffsc_dir_visited_list: the currently active visited list for search dirs
3972 * ffsc_visited_lists_list: the list of all visited lists
3973 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3974 * ffsc_file_to_search: the file to search for
3975 * ffsc_start_dir: the starting directory, if search path was relative
3976 * ffsc_fix_path: the fix part of the given path (without wildcards)
3977 * Needed for upward search.
3978 * ffsc_wc_path: the part of the given path containing wildcards
3979 * ffsc_level: how many levels of dirs to search downwards
3980 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003981 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003982 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984typedef struct ff_search_ctx_T
3985{
3986 ff_stack_T *ffsc_stack_ptr;
3987 ff_visited_list_hdr_T *ffsc_visited_list;
3988 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3989 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3990 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3991 char_u *ffsc_file_to_search;
3992 char_u *ffsc_start_dir;
3993 char_u *ffsc_fix_path;
3994#ifdef FEAT_PATH_EXTRA
3995 char_u *ffsc_wc_path;
3996 int ffsc_level;
3997 char_u **ffsc_stopdirs_v;
3998#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003999 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004000 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004001} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003/* locally needed functions */
4004#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004005static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004007static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004009static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4010static void ff_free_visited_list(ff_visited_T *vl);
4011static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004013static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4014static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4015static void ff_clear(ff_search_ctx_T *search_ctx);
4016static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004018static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004020static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#endif
4022#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004023static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#endif
4025
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004026static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#if 0
4029/*
4030 * if someone likes findfirst/findnext, here are the functions
4031 * NOT TESTED!!
4032 */
4033
4034static void *ff_fn_search_context = NULL;
4035
4036 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004037vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 ff_fn_search_context =
4040 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4041 ff_fn_search_context, rel_fname);
4042 if (NULL == ff_fn_search_context)
4043 return NULL;
4044 else
4045 return vim_findnext()
4046}
4047
4048 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004049vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
4051 char_u *ret = vim_findfile(ff_fn_search_context);
4052
4053 if (NULL == ret)
4054 {
4055 vim_findfile_cleanup(ff_fn_search_context);
4056 ff_fn_search_context = NULL;
4057 }
4058 return ret;
4059}
4060#endif
4061
4062/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004063 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004065 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 *
4067 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4068 * with the search context.
4069 *
4070 * Find the file 'filename' in the directory 'path'.
4071 * The parameter 'path' may contain wildcards. If so only search 'level'
4072 * directories deep. The parameter 'level' is the absolute maximum and is
4073 * not related to restricts given to the '**' wildcard. If 'level' is 100
4074 * and you use '**200' vim_findfile() will stop after 100 levels.
4075 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004076 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4077 * escape special characters.
4078 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4080 * restarted on the next higher directory level. This is repeated until the
4081 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4082 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4083 *
4084 * If the 'path' is relative, the starting dir for the search is either VIM's
4085 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004086 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * the first wildcard.
4088 *
4089 * Upward search is only done on the starting dir.
4090 *
4091 * If 'free_visited' is TRUE the list of already visited files/directories is
4092 * cleared. Set this to FALSE if you just want to search from another
4093 * directory, but want to be sure that no directory from a previous search is
4094 * searched again. This is useful if you search for a file at different places.
4095 * The list of visited files/dirs can also be cleared with the function
4096 * vim_findfile_free_visited().
4097 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004098 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4099 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 *
4101 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004102 * passed in the parameter "search_ctx_arg". This context is reused and
4103 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004105 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4106 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004108 * If you don't have a search context from a previous call "search_ctx_arg"
4109 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 *
4111 * This function silently ignores a few errors, vim_findfile() will have
4112 * limited functionality then.
4113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004115vim_findfile_init(
4116 char_u *path,
4117 char_u *filename,
4118 char_u *stopdirs UNUSED,
4119 int level,
4120 int free_visited,
4121 int find_what,
4122 void *search_ctx_arg,
4123 int tagfile, /* expanding names of tags files */
4124 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004127 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004129 ff_stack_T *sptr;
4130 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 /* If a search context is given by the caller, reuse it, else allocate a
4133 * new one.
4134 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004135 if (search_ctx_arg != NULL)
4136 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
4138 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004139 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4140 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004142 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004144 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004145 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004148 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 /* clear visited list if wanted */
4151 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004152 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 else
4154 {
4155 /* Reuse old visited lists. Get the visited list for the given
4156 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004157 * one. */
4158 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4159 &search_ctx->ffsc_visited_lists_list);
4160 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004162 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4163 &search_ctx->ffsc_dir_visited_lists_list);
4164 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 goto error_return;
4166 }
4167
4168 if (ff_expand_buffer == NULL)
4169 {
4170 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4171 if (ff_expand_buffer == NULL)
4172 goto error_return;
4173 }
4174
4175 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004176 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (path[0] == '.'
4178 && (vim_ispathsep(path[1]) || path[1] == NUL)
4179 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4180 && rel_fname != NULL)
4181 {
4182 int len = (int)(gettail(rel_fname) - rel_fname);
4183
4184 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4185 {
4186 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004187 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004188 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004191 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4192 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 goto error_return;
4194 if (*++path != NUL)
4195 ++path;
4196 }
4197 else if (*path == NUL || !vim_isAbsName(path))
4198 {
4199#ifdef BACKSLASH_IN_FILENAME
4200 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4201 if (*path != NUL && path[1] == ':')
4202 {
4203 char_u drive[3];
4204
4205 drive[0] = path[0];
4206 drive[1] = ':';
4207 drive[2] = NUL;
4208 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4209 goto error_return;
4210 path += 2;
4211 }
4212 else
4213#endif
4214 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4215 goto error_return;
4216
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004217 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4218 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 goto error_return;
4220
4221#ifdef BACKSLASH_IN_FILENAME
4222 /* A path that starts with "/dir" is relative to the drive, not to the
4223 * directory (but not for "//machine/dir"). Only use the drive name. */
4224 if ((*path == '/' || *path == '\\')
4225 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004226 && search_ctx->ffsc_start_dir[1] == ':')
4227 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#endif
4229 }
4230
4231#ifdef FEAT_PATH_EXTRA
4232 /*
4233 * If stopdirs are given, split them into an array of pointers.
4234 * If this fails (mem allocation), there is no upward search at all or a
4235 * stop directory is not recognized -> continue silently.
4236 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004237 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 * is handled as unlimited upward search. See function
4239 * ff_path_in_stoplist() for details.
4240 */
4241 if (stopdirs != NULL)
4242 {
4243 char_u *walker = stopdirs;
4244 int dircount;
4245
4246 while (*walker == ';')
4247 walker++;
4248
4249 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004250 search_ctx->ffsc_stopdirs_v =
4251 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004253 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 do
4256 {
4257 char_u *helper;
4258 void *ptr;
4259
4260 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004261 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 (dircount + 1) * sizeof(char_u *));
4263 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004264 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
4266 /* ignore, keep what we have and continue */
4267 break;
4268 walker = vim_strchr(walker, ';');
4269 if (walker)
4270 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004271 search_ctx->ffsc_stopdirs_v[dircount-1] =
4272 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 walker++;
4274 }
4275 else
4276 /* this might be "", which means ascent till top
4277 * of directory tree.
4278 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004279 search_ctx->ffsc_stopdirs_v[dircount-1] =
4280 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 dircount++;
4283
4284 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004285 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288#endif
4289
4290#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004291 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 /* split into:
4294 * -fix path
4295 * -wildcard_stuff (might be NULL)
4296 */
4297 wc_part = vim_strchr(path, '*');
4298 if (wc_part != NULL)
4299 {
4300 int llevel;
4301 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004302 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004305 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004309 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4311 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4312 * For EBCDIC you get different character values.
4313 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004314 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 * string.
4316 */
4317 len = 0;
4318 while (*wc_part != NUL)
4319 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004320 if (len + 5 >= MAXPATHL)
4321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004323 break;
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (STRNCMP(wc_part, "**", 2) == 0)
4326 {
4327 ff_expand_buffer[len++] = *wc_part++;
4328 ff_expand_buffer[len++] = *wc_part++;
4329
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004330 llevel = strtol((char *)wc_part, &errpt, 10);
4331 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004333 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* restrict is 0 -> remove already added '**' */
4335 len -= 2;
4336 else
4337 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004338 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004339 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004341 semsg(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 goto error_return;
4343 }
4344 }
4345 else
4346 ff_expand_buffer[len++] = *wc_part++;
4347 }
4348 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004349 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004351 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 goto error_return;
4353 }
4354 else
4355#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004356 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004358 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
4360 /* store the fix part as startdir.
4361 * This is needed if the parameter path is fully qualified.
4362 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004363 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004364 if (search_ctx->ffsc_start_dir == NULL)
4365 goto error_return;
4366 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368
4369 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004370 if (STRLEN(search_ctx->ffsc_start_dir)
4371 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004374 goto error_return;
4375 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004376 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004378 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004379 int eb_len = (int)STRLEN(ff_expand_buffer);
4380 char_u *buf = alloc(eb_len
4381 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004382
4383 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004384 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004385 if (mch_isdir(buf))
4386 {
4387 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4388 add_pathsep(ff_expand_buffer);
4389 }
4390#ifdef FEAT_PATH_EXTRA
4391 else
4392 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004393 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004394 char_u *wc_path = NULL;
4395 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004396 int len = 0;
4397
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004398 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004399 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004400 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004401 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4402 add_pathsep(ff_expand_buffer);
4403 }
4404 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004405 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004406
4407 if (search_ctx->ffsc_wc_path != NULL)
4408 {
4409 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004410 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004411 + STRLEN(search_ctx->ffsc_fix_path + len)
4412 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004413 if (temp == NULL || wc_path == NULL)
4414 {
4415 vim_free(buf);
4416 vim_free(temp);
4417 vim_free(wc_path);
4418 goto error_return;
4419 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004420
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004421 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4422 STRCAT(temp, search_ctx->ffsc_wc_path);
4423 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004424 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004425 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004426 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004427 }
4428#endif
4429 vim_free(buf);
4430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 sptr = ff_create_stack_element(ff_expand_buffer,
4433#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004434 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#endif
4436 level, 0);
4437
4438 if (sptr == NULL)
4439 goto error_return;
4440
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004441 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004443 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4444 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 goto error_return;
4446
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004447 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449error_return:
4450 /*
4451 * We clear the search context now!
4452 * Even when the caller gave us a (perhaps valid) context we free it here,
4453 * as we might have already destroyed it.
4454 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004455 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return NULL;
4457}
4458
4459#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4460/*
4461 * Get the stopdir string. Check that ';' is not escaped.
4462 */
4463 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004464vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465{
4466 char_u *r_ptr = buf;
4467
4468 while (*r_ptr != NUL && *r_ptr != ';')
4469 {
4470 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4471 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004472 /* Overwrite the escape char,
4473 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004474 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 r_ptr++;
4476 }
4477 r_ptr++;
4478 }
4479 if (*r_ptr == ';')
4480 {
4481 *r_ptr = 0;
4482 r_ptr++;
4483 }
4484 else if (*r_ptr == NUL)
4485 r_ptr = NULL;
4486 return r_ptr;
4487}
4488#endif
4489
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004490/*
4491 * Clean up the given search context. Can handle a NULL pointer.
4492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004494vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004496 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 return;
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004500 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502}
4503
4504/*
4505 * Find a file in a search context.
4506 * The search context was created with vim_findfile_init() above.
4507 * Return a pointer to an allocated file name or NULL if nothing found.
4508 * To get all matching files call this function until you get NULL.
4509 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004510 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 *
4512 * The search algorithm is depth first. To change this replace the
4513 * stack with a list (don't forget to leave partly searched directories on the
4514 * top of the list).
4515 */
4516 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004517vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
4519 char_u *file_path;
4520#ifdef FEAT_PATH_EXTRA
4521 char_u *rest_of_wildcards;
4522 char_u *path_end = NULL;
4523#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004524 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4526 int len;
4527#endif
4528 int i;
4529 char_u *p;
4530#ifdef FEAT_SEARCHPATH
4531 char_u *suf;
4532#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004533 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004535 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 return NULL;
4537
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004538 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 /*
4541 * filepath is used as buffer for various actions and as the storage to
4542 * return a found filename.
4543 */
4544 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4545 return NULL;
4546
4547#ifdef FEAT_PATH_EXTRA
4548 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004549 if (search_ctx->ffsc_start_dir != NULL)
4550 path_end = &search_ctx->ffsc_start_dir[
4551 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#endif
4553
4554#ifdef FEAT_PATH_EXTRA
4555 /* upward search loop */
4556 for (;;)
4557 {
4558#endif
4559 /* downward search loop */
4560 for (;;)
4561 {
4562 /* check if user user wants to stop the search*/
4563 ui_breakcheck();
4564 if (got_int)
4565 break;
4566
4567 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004568 stackp = ff_pop(search_ctx);
4569 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571
4572 /*
4573 * TODO: decide if we leave this test in
4574 *
4575 * GOOD: don't search a directory(-tree) twice.
4576 * BAD: - check linked list for every new directory entered.
4577 * - check for double files also done below
4578 *
4579 * Here we check if we already searched this directory.
4580 * We already searched a directory if:
4581 * 1) The directory is the same.
4582 * 2) We would use the same wildcard string.
4583 *
4584 * Good if you have links on same directory via several ways
4585 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4586 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4587 *
4588 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004589 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004591 if (stackp->ffs_filearray == NULL
4592 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004594 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004596 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597#endif
4598 ) == FAIL)
4599 {
4600#ifdef FF_VERBOSE
4601 if (p_verbose >= 5)
4602 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004603 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004604 smsg("Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004605 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004607 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004608 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004611 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 continue;
4613 }
4614#ifdef FF_VERBOSE
4615 else if (p_verbose >= 5)
4616 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004617 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004618 smsg("Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004619 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004621 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004622 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624#endif
4625
4626 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004627 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004629 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 continue;
4631 }
4632
4633 file_path[0] = NUL;
4634
4635 /*
4636 * If no filearray till now expand wildcards
4637 * The function expand_wildcards() can handle an array of paths
4638 * and all possible expands are returned in one array. We use this
4639 * to handle the expansion of '**' into an empty string.
4640 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004641 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 char_u *dirptrs[2];
4644
4645 /* we use filepath to build the path expand_wildcards() should
4646 * expand.
4647 */
4648 dirptrs[0] = file_path;
4649 dirptrs[1] = NULL;
4650
4651 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004652 if (!vim_isAbsName(stackp->ffs_fix_path)
4653 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004655 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4656 {
4657 STRCPY(file_path, search_ctx->ffsc_start_dir);
4658 add_pathsep(file_path);
4659 }
4660 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004661 {
4662 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004663 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666
4667 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004668 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4669 {
4670 STRCAT(file_path, stackp->ffs_fix_path);
4671 add_pathsep(file_path);
4672 }
4673 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004674 {
4675 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004676 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004680 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (*rest_of_wildcards != NUL)
4682 {
4683 len = (int)STRLEN(file_path);
4684 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4685 {
4686 /* pointer to the restrict byte
4687 * The restrict byte is not a character!
4688 */
4689 p = rest_of_wildcards + 2;
4690
4691 if (*p > 0)
4692 {
4693 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004694 if (len + 1 < MAXPATHL)
4695 file_path[len++] = '*';
4696 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004697 {
4698 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004699 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702
4703 if (*p == 0)
4704 {
4705 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004706 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 else
4709 rest_of_wildcards += 3;
4710
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004711 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004714 stackp->ffs_star_star_empty = 1;
4715 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 }
4718
4719 /*
4720 * Here we copy until the next path separator or the end of
4721 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004722 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * pushing every directory returned from expand_wildcards()
4724 * on the stack again for further search.
4725 */
4726 while (*rest_of_wildcards
4727 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004728 if (len + 1 < MAXPATHL)
4729 file_path[len++] = *rest_of_wildcards++;
4730 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004731 {
4732 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004733 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736 file_path[len] = NUL;
4737 if (vim_ispathsep(*rest_of_wildcards))
4738 rest_of_wildcards++;
4739 }
4740#endif
4741
4742 /*
4743 * Expand wildcards like "*" and "$VAR".
4744 * If the path is a URL don't try this.
4745 */
4746 if (path_with_url(dirptrs[0]))
4747 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004748 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004750 if (stackp->ffs_filearray != NULL
4751 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004753 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004755 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004758 /* Add EW_NOTWILD because the expanded path may contain
4759 * wildcard characters that are to be taken literally.
4760 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004762 &stackp->ffs_filearray_size,
4763 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004764 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004766 stackp->ffs_filearray_cur = 0;
4767 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769#ifdef FEAT_PATH_EXTRA
4770 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004771 rest_of_wildcards = &stackp->ffs_wc_path[
4772 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#endif
4774
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004775 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 /* this is the first time we work on this directory */
4778#ifdef FEAT_PATH_EXTRA
4779 if (*rest_of_wildcards == NUL)
4780#endif
4781 {
4782 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004783 * We don't have further wildcards to expand, so we have to
4784 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004786 for (i = stackp->ffs_filearray_cur;
4787 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004789 if (!path_with_url(stackp->ffs_filearray[i])
4790 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 continue; /* not a directory */
4792
Bram Moolenaar21160b92009-11-11 15:56:10 +00004793 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004795 if (STRLEN(stackp->ffs_filearray[i]) + 1
4796 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4797 {
4798 STRCPY(file_path, stackp->ffs_filearray[i]);
4799 add_pathsep(file_path);
4800 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4801 }
4802 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004803 {
4804 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004805 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 /*
4809 * Try without extra suffix and then with suffixes
4810 * from 'suffixesadd'.
4811 */
4812#ifdef FEAT_SEARCHPATH
4813 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004814 if (search_ctx->ffsc_tagfile)
4815 suf = (char_u *)"";
4816 else
4817 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 for (;;)
4819#endif
4820 {
4821 /* if file exists and we didn't already find it */
4822 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004823 || (mch_getperm(file_path) >= 0
4824 && (search_ctx->ffsc_find_what
4825 == FINDFILE_BOTH
4826 || ((search_ctx->ffsc_find_what
4827 == FINDFILE_DIR)
4828 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829#ifndef FF_VERBOSE
4830 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004831 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 file_path
4833#ifdef FEAT_PATH_EXTRA
4834 , (char_u *)""
4835#endif
4836 ) == OK)
4837#endif
4838 )
4839 {
4840#ifdef FF_VERBOSE
4841 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004842 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 file_path
4844#ifdef FEAT_PATH_EXTRA
4845 , (char_u *)""
4846#endif
4847 ) == FAIL)
4848 {
4849 if (p_verbose >= 5)
4850 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004851 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 smsg("Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 file_path);
4854 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004855 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004856 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 continue;
4859 }
4860#endif
4861
4862 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004863 stackp->ffs_filearray_cur = i + 1;
4864 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004866 if (!path_with_url(file_path))
4867 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4869 == OK)
4870 {
4871 p = shorten_fname(file_path,
4872 ff_expand_buffer);
4873 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004874 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876#ifdef FF_VERBOSE
4877 if (p_verbose >= 5)
4878 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004879 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 smsg("HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004882 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004883 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885#endif
4886 return file_path;
4887 }
4888
4889#ifdef FEAT_SEARCHPATH
4890 /* Not found or found already, try next suffix. */
4891 if (*suf == NUL)
4892 break;
4893 copy_option_part(&suf, file_path + len,
4894 MAXPATHL - len, ",");
4895#endif
4896 }
4897 }
4898 }
4899#ifdef FEAT_PATH_EXTRA
4900 else
4901 {
4902 /*
4903 * still wildcards left, push the directories for further
4904 * search
4905 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004906 for (i = stackp->ffs_filearray_cur;
4907 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004909 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 continue; /* not a directory */
4911
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004912 ff_push(search_ctx,
4913 ff_create_stack_element(
4914 stackp->ffs_filearray[i],
4915 rest_of_wildcards,
4916 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
4919#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004920 stackp->ffs_filearray_cur = 0;
4921 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923
4924#ifdef FEAT_PATH_EXTRA
4925 /*
4926 * if wildcards contains '**' we have to descent till we reach the
4927 * leaves of the directory tree.
4928 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004929 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004931 for (i = stackp->ffs_filearray_cur;
4932 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004934 if (fnamecmp(stackp->ffs_filearray[i],
4935 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004937 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004939 ff_push(search_ctx,
4940 ff_create_stack_element(stackp->ffs_filearray[i],
4941 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 }
4944#endif
4945
4946 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004947 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 }
4950
4951#ifdef FEAT_PATH_EXTRA
4952 /* If we reached this, we didn't find anything downwards.
4953 * Let's check if we should do an upward search.
4954 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004955 if (search_ctx->ffsc_start_dir
4956 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 ff_stack_T *sptr;
4959
4960 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004961 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4962 (int)(path_end - search_ctx->ffsc_start_dir),
4963 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 break;
4965
4966 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004967 while (path_end > search_ctx->ffsc_start_dir
4968 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004970 while (path_end > search_ctx->ffsc_start_dir
4971 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 path_end--;
4973 *path_end = 0;
4974 path_end--;
4975
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004976 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004979 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4980 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4981 {
4982 STRCPY(file_path, search_ctx->ffsc_start_dir);
4983 add_pathsep(file_path);
4984 STRCAT(file_path, search_ctx->ffsc_fix_path);
4985 }
4986 else
4987 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 /* create a new stack entry */
4990 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004991 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (sptr == NULL)
4993 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004994 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 else
4997 break;
4998 }
4999#endif
5000
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005001fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 vim_free(file_path);
5003 return NULL;
5004}
5005
5006/*
5007 * Free the list of lists of visited files and directories
5008 * Can handle it if the passed search_context is NULL;
5009 */
5010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005013 ff_search_ctx_T *search_ctx;
5014
5015 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 return;
5017
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005018 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
5019 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5020 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021}
5022
5023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005024vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026 ff_visited_list_hdr_T *vp;
5027
5028 while (*list_headp != NULL)
5029 {
5030 vp = (*list_headp)->ffvl_next;
5031 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5032
5033 vim_free((*list_headp)->ffvl_filename);
5034 vim_free(*list_headp);
5035 *list_headp = vp;
5036 }
5037 *list_headp = NULL;
5038}
5039
5040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005041ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 ff_visited_T *vp;
5044
5045 while (vl != NULL)
5046 {
5047 vp = vl->ffv_next;
5048#ifdef FEAT_PATH_EXTRA
5049 vim_free(vl->ffv_wc_path);
5050#endif
5051 vim_free(vl);
5052 vl = vp;
5053 }
5054 vl = NULL;
5055}
5056
5057/*
5058 * Returns the already visited list for the given filename. If none is found it
5059 * allocates a new one.
5060 */
5061 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005062ff_get_visited_list(
5063 char_u *filename,
5064 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 ff_visited_list_hdr_T *retptr = NULL;
5067
5068 /* check if a visited list for the given filename exists */
5069 if (*list_headp != NULL)
5070 {
5071 retptr = *list_headp;
5072 while (retptr != NULL)
5073 {
5074 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5075 {
5076#ifdef FF_VERBOSE
5077 if (p_verbose >= 5)
5078 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005079 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005080 smsg("ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 filename);
5082 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005083 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005084 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086#endif
5087 return retptr;
5088 }
5089 retptr = retptr->ffvl_next;
5090 }
5091 }
5092
5093#ifdef FF_VERBOSE
5094 if (p_verbose >= 5)
5095 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005096 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005097 smsg("ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005099 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005100 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102#endif
5103
5104 /*
5105 * if we reach this we didn't find a list and we have to allocate new list
5106 */
5107 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5108 if (retptr == NULL)
5109 return NULL;
5110
5111 retptr->ffvl_visited_list = NULL;
5112 retptr->ffvl_filename = vim_strsave(filename);
5113 if (retptr->ffvl_filename == NULL)
5114 {
5115 vim_free(retptr);
5116 return NULL;
5117 }
5118 retptr->ffvl_next = *list_headp;
5119 *list_headp = retptr;
5120
5121 return retptr;
5122}
5123
5124#ifdef FEAT_PATH_EXTRA
5125/*
5126 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5127 * They are equal if:
5128 * - both paths are NULL
5129 * - they have the same length
5130 * - char by char comparison is OK
5131 * - the only differences are in the counters behind a '**', so
5132 * '**\20' is equal to '**\24'
5133 */
5134 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005135ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005137 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005138 int c1 = NUL;
5139 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005140 int prev1 = NUL;
5141 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 if (s1 == s2)
5144 return TRUE;
5145
5146 if (s1 == NULL || s2 == NULL)
5147 return FALSE;
5148
Bram Moolenaard43f0952015-08-27 22:30:47 +02005149 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005151 c1 = PTR2CHAR(s1 + i);
5152 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005153
5154 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5155 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005156 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005157 prev2 = prev1;
5158 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005159
Bram Moolenaar15675582018-02-09 12:29:56 +01005160 i += MB_PTR2LEN(s1 + i);
5161 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005163 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164}
5165#endif
5166
5167/*
5168 * maintains the list of already visited files and dirs
5169 * returns FAIL if the given file/dir is already in the list
5170 * returns OK if it is newly added
5171 *
5172 * TODO: What to do on memory allocation problems?
5173 * -> return TRUE - Better the file is found several times instead of
5174 * never.
5175 */
5176 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005177ff_check_visited(
5178 ff_visited_T **visited_list,
5179 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 ff_visited_T *vp;
5186#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005187 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 int url = FALSE;
5189#endif
5190
5191 /* For an URL we only compare the name, otherwise we compare the
5192 * device/inode (unix) or the full path name (not Unix). */
5193 if (path_with_url(fname))
5194 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005195 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#ifdef UNIX
5197 url = TRUE;
5198#endif
5199 }
5200 else
5201 {
5202 ff_expand_buffer[0] = NUL;
5203#ifdef UNIX
5204 if (mch_stat((char *)fname, &st) < 0)
5205#else
5206 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5207#endif
5208 return FAIL;
5209 }
5210
5211 /* check against list of already visited files */
5212 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5213 {
5214 if (
5215#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005216 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5217 && vp->ffv_ino == st.st_ino)
5218 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#endif
5220 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5221 )
5222 {
5223#ifdef FEAT_PATH_EXTRA
5224 /* are the wildcard parts equal */
5225 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5226#endif
5227 /* already visited */
5228 return FAIL;
5229 }
5230 }
5231
5232 /*
5233 * New file/dir. Add it to the list of visited files/dirs.
5234 */
5235 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5236 + STRLEN(ff_expand_buffer)));
5237
5238 if (vp != NULL)
5239 {
5240#ifdef UNIX
5241 if (!url)
5242 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005243 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 vp->ffv_ino = st.st_ino;
5245 vp->ffv_dev = st.st_dev;
5246 vp->ffv_fname[0] = NUL;
5247 }
5248 else
5249 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005250 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
5252 STRCPY(vp->ffv_fname, ff_expand_buffer);
5253#ifdef UNIX
5254 }
5255#endif
5256#ifdef FEAT_PATH_EXTRA
5257 if (wc_path != NULL)
5258 vp->ffv_wc_path = vim_strsave(wc_path);
5259 else
5260 vp->ffv_wc_path = NULL;
5261#endif
5262
5263 vp->ffv_next = *visited_list;
5264 *visited_list = vp;
5265 }
5266
5267 return OK;
5268}
5269
5270/*
5271 * create stack element from given path pieces
5272 */
5273 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274ff_create_stack_element(
5275 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005277 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279 int level,
5280 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 ff_stack_T *new;
5283
5284 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5285 if (new == NULL)
5286 return NULL;
5287
5288 new->ffs_prev = NULL;
5289 new->ffs_filearray = NULL;
5290 new->ffs_filearray_size = 0;
5291 new->ffs_filearray_cur = 0;
5292 new->ffs_stage = 0;
5293 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005294 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 /* the following saves NULL pointer checks in vim_findfile */
5297 if (fix_part == NULL)
5298 fix_part = (char_u *)"";
5299 new->ffs_fix_path = vim_strsave(fix_part);
5300
5301#ifdef FEAT_PATH_EXTRA
5302 if (wc_part == NULL)
5303 wc_part = (char_u *)"";
5304 new->ffs_wc_path = vim_strsave(wc_part);
5305#endif
5306
5307 if (new->ffs_fix_path == NULL
5308#ifdef FEAT_PATH_EXTRA
5309 || new->ffs_wc_path == NULL
5310#endif
5311 )
5312 {
5313 ff_free_stack_element(new);
5314 new = NULL;
5315 }
5316
5317 return new;
5318}
5319
5320/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005321 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
5323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005324ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005327 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005328 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005330 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5331 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333}
5334
5335/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005336 * Pop a dir from the directory stack.
5337 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 */
5339 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 ff_stack_T *sptr;
5343
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005344 sptr = search_ctx->ffsc_stack_ptr;
5345 if (search_ctx->ffsc_stack_ptr != NULL)
5346 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 return sptr;
5349}
5350
5351/*
5352 * free the given stack element
5353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005358 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005360 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#endif
5362
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005363 if (stack_ptr->ffs_filearray != NULL)
5364 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005366 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367}
5368
5369/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005370 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 */
5372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005373ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 ff_stack_T *sptr;
5376
5377 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005378 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 ff_free_stack_element(sptr);
5380
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005381 vim_free(search_ctx->ffsc_file_to_search);
5382 vim_free(search_ctx->ffsc_start_dir);
5383 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005385 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#endif
5387
5388#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005389 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 int i = 0;
5392
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005393 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005395 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 i++;
5397 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005398 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005400 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401#endif
5402
5403 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005404 search_ctx->ffsc_file_to_search = NULL;
5405 search_ctx->ffsc_start_dir = NULL;
5406 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005408 search_ctx->ffsc_wc_path = NULL;
5409 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411}
5412
5413#ifdef FEAT_PATH_EXTRA
5414/*
5415 * check if the given path is in the stopdirs
5416 * returns TRUE if yes else FALSE
5417 */
5418 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 int i = 0;
5422
5423 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005424 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 path_len--;
5426
5427 /* if no path consider it as match */
5428 if (path_len == 0)
5429 return TRUE;
5430
5431 for (i = 0; stopdirs_v[i] != NULL; i++)
5432 {
5433 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5434 {
5435 /* match for parent directory. So '/home' also matches
5436 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5437 * '/home/r' would also match '/home/rks'
5438 */
5439 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005440 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return TRUE;
5442 }
5443 else
5444 {
5445 if (fnamecmp(stopdirs_v[i], path) == 0)
5446 return TRUE;
5447 }
5448 }
5449 return FALSE;
5450}
5451#endif
5452
5453#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5454/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005455 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 *
5457 * On the first call set the parameter 'first' to TRUE to initialize
5458 * the search. For repeating calls to FALSE.
5459 *
5460 * Repeating calls will return other files called 'ptr[len]' from the path.
5461 *
5462 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5463 * don't need valid values.
5464 *
5465 * If nothing found on the first call the option FNAME_MESS will issue the
5466 * message:
5467 * 'Can't find file "<file>" in path'
5468 * On repeating calls:
5469 * 'No more file "<file>" found in path'
5470 *
5471 * options:
5472 * FNAME_MESS give error message when not found
5473 *
5474 * Uses NameBuff[]!
5475 *
5476 * Returns an allocated string for the file name. NULL for error.
5477 *
5478 */
5479 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005480find_file_in_path(
5481 char_u *ptr, /* file name */
5482 int len, /* length of file name */
5483 int options,
5484 int first, /* use count'th matching file name */
5485 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 return find_file_in_path_option(ptr, len, options, first,
5488 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005489 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490}
5491
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005492static char_u *ff_file_to_find = NULL;
5493static void *fdip_search_ctx = NULL;
5494
5495#if defined(EXITFREE)
5496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005498{
5499 vim_free(ff_file_to_find);
5500 vim_findfile_cleanup(fdip_search_ctx);
5501}
5502#endif
5503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504/*
5505 * Find the directory name "ptr[len]" in the path.
5506 *
5507 * options:
5508 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005509 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 *
5511 * Uses NameBuff[]!
5512 *
5513 * Returns an allocated string for the file name. NULL for error.
5514 */
5515 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516find_directory_in_path(
5517 char_u *ptr, /* file name */
5518 int len, /* length of file name */
5519 int options,
5520 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005523 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524}
5525
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005526 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527find_file_in_path_option(
5528 char_u *ptr, /* file name */
5529 int len, /* length of file name */
5530 int options,
5531 int first, /* use count'th matching file name */
5532 char_u *path_option, /* p_path or p_cdpath */
5533 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5534 char_u *rel_fname, /* file name we are looking relative to. */
5535 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 static int did_findfile_init = FALSE;
5539 char_u save_char;
5540 char_u *file_name = NULL;
5541 char_u *buf = NULL;
5542 int rel_to_curdir;
5543#ifdef AMIGA
5544 struct Process *proc = (struct Process *)FindTask(0L);
5545 APTR save_winptr = proc->pr_WindowPtr;
5546
5547 /* Avoid a requester here for a volume that doesn't exist. */
5548 proc->pr_WindowPtr = (APTR)-1L;
5549#endif
5550
5551 if (first == TRUE)
5552 {
5553 /* copy file name into NameBuff, expanding environment variables */
5554 save_char = ptr[len];
5555 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005556 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 ptr[len] = save_char;
5558
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005559 vim_free(ff_file_to_find);
5560 ff_file_to_find = vim_strsave(NameBuff);
5561 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
5563 file_name = NULL;
5564 goto theend;
5565 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005566 if (options & FNAME_UNESC)
5567 {
5568 /* Change all "\ " to " ". */
5569 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5570 if (ptr[0] == '\\' && ptr[1] == ' ')
5571 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005575 rel_to_curdir = (ff_file_to_find[0] == '.'
5576 && (ff_file_to_find[1] == NUL
5577 || vim_ispathsep(ff_file_to_find[1])
5578 || (ff_file_to_find[1] == '.'
5579 && (ff_file_to_find[2] == NUL
5580 || vim_ispathsep(ff_file_to_find[2])))));
5581 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 /* "..", "../path", "." and "./path": don't use the path_option */
5583 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005584#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005586 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005587 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#endif
5590#ifdef AMIGA
5591 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005592 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
5594 )
5595 {
5596 /*
5597 * Absolute path, no need to use "path_option".
5598 * If this is not a first call, return NULL. We already returned a
5599 * filename on the first call.
5600 */
5601 if (first == TRUE)
5602 {
5603 int l;
5604 int run;
5605
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005606 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005608 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 goto theend;
5610 }
5611
5612 /* When FNAME_REL flag given first use the directory of the file.
5613 * Otherwise or when this fails use the current directory. */
5614 for (run = 1; run <= 2; ++run)
5615 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005616 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (run == 1
5618 && rel_to_curdir
5619 && (options & FNAME_REL)
5620 && rel_fname != NULL
5621 && STRLEN(rel_fname) + l < MAXPATHL)
5622 {
5623 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005624 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 l = (int)STRLEN(NameBuff);
5626 }
5627 else
5628 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005629 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 run = 2;
5631 }
5632
5633 /* When the file doesn't exist, try adding parts of
5634 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005635 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 for (;;)
5637 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005638 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005639 && (find_what == FINDFILE_BOTH
5640 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005641 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 file_name = vim_strsave(NameBuff);
5644 goto theend;
5645 }
5646 if (*buf == NUL)
5647 break;
5648 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5649 }
5650 }
5651 }
5652 }
5653 else
5654 {
5655 /*
5656 * Loop over all paths in the 'path' or 'cdpath' option.
5657 * When "first" is set, first setup to the start of the option.
5658 * Otherwise continue to find the next match.
5659 */
5660 if (first == TRUE)
5661 {
5662 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005663 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 dir = path_option;
5665 did_findfile_init = FALSE;
5666 }
5667
5668 for (;;)
5669 {
5670 if (did_findfile_init)
5671 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005672 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (file_name != NULL)
5674 break;
5675
5676 did_findfile_init = FALSE;
5677 }
5678 else
5679 {
5680 char_u *r_ptr;
5681
5682 if (dir == NULL || *dir == NUL)
5683 {
5684 /* We searched all paths of the option, now we can
5685 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005686 vim_findfile_cleanup(fdip_search_ctx);
5687 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 break;
5689 }
5690
5691 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5692 break;
5693
5694 /* copy next path */
5695 buf[0] = 0;
5696 copy_option_part(&dir, buf, MAXPATHL, " ,");
5697
5698#ifdef FEAT_PATH_EXTRA
5699 /* get the stopdir string */
5700 r_ptr = vim_findfile_stopdir(buf);
5701#else
5702 r_ptr = NULL;
5703#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005704 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005705 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005706 fdip_search_ctx, FALSE, rel_fname);
5707 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 did_findfile_init = TRUE;
5709 vim_free(buf);
5710 }
5711 }
5712 }
5713 if (file_name == NULL && (options & FNAME_MESS))
5714 {
5715 if (first == TRUE)
5716 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005717 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005718 semsg(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005719 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005721 semsg(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005722 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else
5725 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005726 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 semsg(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005728 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005730 semsg(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005731 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 }
5734
5735theend:
5736#ifdef AMIGA
5737 proc->pr_WindowPtr = save_winptr;
5738#endif
5739 return file_name;
5740}
5741
5742#endif /* FEAT_SEARCHPATH */
5743
5744/*
5745 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5746 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5747 */
5748 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005749vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751#ifndef FEAT_SEARCHPATH
5752 return mch_chdir((char *)new_dir);
5753#else
5754 char_u *dir_name;
5755 int r;
5756
5757 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5758 FNAME_MESS, curbuf->b_ffname);
5759 if (dir_name == NULL)
5760 return -1;
5761 r = mch_chdir((char *)dir_name);
5762 vim_free(dir_name);
5763 return r;
5764#endif
5765}
5766
5767/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005768 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005770 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5771 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 * Returns OK or FAIL.
5773 */
5774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005775get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005777 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 if (mch_get_user_name(buf, len) == FAIL)
5780 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005781 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005784 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 return OK;
5786}
5787
5788#ifndef HAVE_QSORT
5789/*
5790 * Our own qsort(), for systems that don't have it.
5791 * It's simple and slow. From the K&R C book.
5792 */
5793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005794qsort(
5795 void *base,
5796 size_t elm_count,
5797 size_t elm_size,
5798 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 char_u *buf;
5801 char_u *p1;
5802 char_u *p2;
5803 int i, j;
5804 int gap;
5805
5806 buf = alloc((unsigned)elm_size);
5807 if (buf == NULL)
5808 return;
5809
5810 for (gap = elm_count / 2; gap > 0; gap /= 2)
5811 for (i = gap; i < elm_count; ++i)
5812 for (j = i - gap; j >= 0; j -= gap)
5813 {
5814 /* Compare the elements. */
5815 p1 = (char_u *)base + j * elm_size;
5816 p2 = (char_u *)base + (j + gap) * elm_size;
5817 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5818 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005819 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 mch_memmove(buf, p1, elm_size);
5821 mch_memmove(p1, p2, elm_size);
5822 mch_memmove(p2, buf, elm_size);
5823 }
5824
5825 vim_free(buf);
5826}
5827#endif
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829/*
5830 * Sort an array of strings.
5831 */
5832static int
5833#ifdef __BORLANDC__
5834_RTLENTRYF
5835#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005836sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 static int
5839#ifdef __BORLANDC__
5840_RTLENTRYF
5841#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005842sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 return STRCMP(*(char **)s1, *(char **)s2);
5845}
5846
5847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005848sort_strings(
5849 char_u **files,
5850 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
5855#if !defined(NO_EXPANDPATH) || defined(PROTO)
5856/*
5857 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005858 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 * Return value like strcmp(p, q), but consider path separators.
5860 */
5861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005862pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005864 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005865 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005866 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005868 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005870 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005871 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005874 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005876 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 return 0;
5878 s = q;
Bram Moolenaar15675582018-02-09 12:29:56 +01005879 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 break;
5881 }
5882
5883 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005884 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
5886 s = p;
5887 break;
5888 }
5889
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005890 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891#ifdef BACKSLASH_IN_FILENAME
5892 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005893 && !((c1 == '/' && c2 == '\\')
5894 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896 )
5897 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005898 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005900 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005902 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5903 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005905
5906 i += MB_PTR2LEN((char_u *)p + i);
5907 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005909 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005910 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005912 c1 = PTR2CHAR((char_u *)s + i);
5913 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005915 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005916 && i > 0
5917 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005919 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005921 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 return 0; /* match with trailing slash */
5925 if (s == q)
5926 return -1; /* no match */
5927 return 1;
5928}
5929#endif
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931/*
5932 * The putenv() implementation below comes from the "screen" program.
5933 * Included with permission from Juergen Weigert.
5934 * See pty.c for the copyright notice.
5935 */
5936
5937/*
5938 * putenv -- put value into environment
5939 *
5940 * Usage: i = putenv (string)
5941 * int i;
5942 * char *string;
5943 *
5944 * where string is of the form <name>=<value>.
5945 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5946 *
5947 * Putenv may need to add a new name into the environment, or to
5948 * associate a value longer than the current value with a particular
5949 * name. So, to make life simpler, putenv() copies your entire
5950 * environment into the heap (i.e. malloc()) from the stack
5951 * (i.e. where it resides when your process is initiated) the first
5952 * time you call it.
5953 *
5954 * (history removed, not very interesting. See the "screen" sources.)
5955 */
5956
5957#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5958
5959#define EXTRASIZE 5 /* increment to add to env. size */
5960
5961static int envsize = -1; /* current size of environment */
Bram Moolenaard0573012017-10-28 21:11:06 +02005962extern char **environ; /* the global which is your env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005964static int findenv(char *name); /* look for a name in the env. */
5965static int newenv(void); /* copy env. from stack to heap */
5966static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
5968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 int i;
5972 char *p;
5973
5974 if (envsize < 0)
5975 { /* first time putenv called */
5976 if (newenv() < 0) /* copy env. to heap */
5977 return -1;
5978 }
5979
5980 i = findenv((char *)string); /* look for name in environment */
5981
5982 if (i < 0)
5983 { /* name must be added */
5984 for (i = 0; environ[i]; i++);
5985 if (i >= (envsize - 1))
5986 { /* need new slot */
5987 if (moreenv() < 0)
5988 return -1;
5989 }
5990 p = (char *)alloc((unsigned)(strlen(string) + 1));
5991 if (p == NULL) /* not enough core */
5992 return -1;
5993 environ[i + 1] = 0; /* new end of env. */
5994 }
5995 else
5996 { /* name already in env. */
5997 p = vim_realloc(environ[i], strlen(string) + 1);
5998 if (p == NULL)
5999 return -1;
6000 }
6001 sprintf(p, "%s", string); /* copy into env. */
6002 environ[i] = p;
6003
6004 return 0;
6005}
6006
6007 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006008findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 char *namechar, *envchar;
6011 int i, found;
6012
6013 found = 0;
6014 for (i = 0; environ[i] && !found; i++)
6015 {
6016 envchar = environ[i];
6017 namechar = name;
6018 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6019 {
6020 namechar++;
6021 envchar++;
6022 }
6023 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6024 }
6025 return found ? i - 1 : -1;
6026}
6027
6028 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006029newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 char **env, *elem;
6032 int i, esize;
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 for (i = 0; environ[i]; i++)
6035 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02006036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 esize = i + EXTRASIZE + 1;
6038 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6039 if (env == NULL)
6040 return -1;
6041
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 for (i = 0; environ[i]; i++)
6043 {
6044 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6045 if (elem == NULL)
6046 return -1;
6047 env[i] = elem;
6048 strcpy(elem, environ[i]);
6049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051 env[i] = 0;
6052 environ = env;
6053 envsize = esize;
6054 return 0;
6055}
6056
6057 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006058moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 int esize;
6061 char **env;
6062
6063 esize = envsize + EXTRASIZE;
6064 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6065 if (env == 0)
6066 return -1;
6067 environ = env;
6068 envsize = esize;
6069 return 0;
6070}
6071
6072# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006073/*
6074 * Used for mch_getenv() for Mac.
6075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006077vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 int i;
6080 char_u *p;
6081
6082 if (envsize < 0)
6083 return NULL;
6084
6085 i = findenv((char *)string);
6086
6087 if (i < 0)
6088 return NULL;
6089
6090 p = vim_strchr((char_u *)environ[i], '=');
6091 return (p + 1);
6092}
6093# endif
6094
6095#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006096
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006097#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006098/*
6099 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6100 * rights to write into.
6101 */
6102 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006103filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006104{
6105 int retval = 0;
6106#if defined(UNIX) || defined(VMS)
6107 int perm = 0;
6108#endif
6109
6110#if defined(UNIX) || defined(VMS)
6111 perm = mch_getperm(fname);
6112#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006113 if (
6114# ifdef WIN3264
6115 mch_writable(fname) &&
6116# else
6117# if defined(UNIX) || defined(VMS)
6118 (perm & 0222) &&
6119# endif
6120# endif
6121 mch_access((char *)fname, W_OK) == 0
6122 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006123 {
6124 ++retval;
6125 if (mch_isdir(fname))
6126 ++retval;
6127 }
6128 return retval;
6129}
6130#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006131
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006132#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6133/*
6134 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006135 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006136 */
6137 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006138get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006139{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006140 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006141
6142 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006143 if (n == EOF) return -1;
6144 c = getc(fd);
6145 if (c == EOF) return -1;
6146 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006147}
6148
6149/*
6150 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006151 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006152 */
6153 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006154get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006155{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006156 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006157
6158 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006159 if (n == EOF) return -1;
6160 c = getc(fd);
6161 if (c == EOF) return -1;
6162 n = (n << 8) + c;
6163 c = getc(fd);
6164 if (c == EOF) return -1;
6165 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006166}
6167
6168/*
6169 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006170 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006171 */
6172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006173get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006174{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006175 int c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006176 /* Use unsigned rather than int otherwise result is undefined
6177 * when left-shift sets the MSB. */
6178 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006179
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006180 c = getc(fd);
6181 if (c == EOF) return -1;
6182 n = (unsigned)c;
6183 c = getc(fd);
6184 if (c == EOF) return -1;
6185 n = (n << 8) + (unsigned)c;
6186 c = getc(fd);
6187 if (c == EOF) return -1;
6188 n = (n << 8) + (unsigned)c;
6189 c = getc(fd);
6190 if (c == EOF) return -1;
6191 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006192 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006193}
6194
6195/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006196 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006197 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006198 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006199 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006200get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006201{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006202 int c;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006203 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006204 int i;
6205
6206 for (i = 0; i < 8; ++i)
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006207 {
6208 c = getc(fd);
6209 if (c == EOF) return -1;
6210 n = (n << 8) + c;
6211 }
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006212 return n;
6213}
6214
6215/*
6216 * Read a string of length "cnt" from "fd" into allocated memory.
6217 * Returns NULL when out of memory or unable to read that many bytes.
6218 */
6219 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006220read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006221{
6222 char_u *str;
6223 int i;
6224 int c;
6225
6226 /* allocate memory */
6227 str = alloc((unsigned)cnt + 1);
6228 if (str != NULL)
6229 {
6230 /* Read the string. Quit when running into the EOF. */
6231 for (i = 0; i < cnt; ++i)
6232 {
6233 c = getc(fd);
6234 if (c == EOF)
6235 {
6236 vim_free(str);
6237 return NULL;
6238 }
6239 str[i] = c;
6240 }
6241 str[i] = NUL;
6242 }
6243 return str;
6244}
6245
6246/*
6247 * Write a number to file "fd", MSB first, in "len" bytes.
6248 */
6249 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006250put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006251{
6252 int i;
6253
6254 for (i = len - 1; i >= 0; --i)
6255 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6256 return FAIL;
6257 return OK;
6258}
6259
6260#ifdef _MSC_VER
6261# if (_MSC_VER <= 1200)
6262/* This line is required for VC6 without the service pack. Also see the
6263 * matching #pragma below. */
6264 # pragma optimize("", off)
6265# endif
6266#endif
6267
6268/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006269 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006270 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006271 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006272 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006273put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006274{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006275 char_u buf[8];
6276
6277 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006278 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006279}
6280
6281/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006282 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006283 */
6284 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006285time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006286{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006287 int c;
6288 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006289 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006290 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006291
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006292 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006293 * can't use put_bytes() here.
6294 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006295 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006296 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006297 * it's safe to assume that long_u is 4 bytes or more and when using 8
6298 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006299 for (i = 7; i >= 0; --i)
6300 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006301 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006302 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006303 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006304 else
6305 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006306#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006307 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006308#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006309 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006310#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006311 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006312 }
6313 }
6314}
6315
6316#ifdef _MSC_VER
6317# if (_MSC_VER <= 1200)
6318 # pragma optimize("", on)
6319# endif
6320#endif
6321
6322#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006323
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006324#if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006325/*
6326 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6327 * When "s" is NULL FALSE is returned.
6328 */
6329 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006330has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006331{
6332 char_u *p;
6333
6334 if (s != NULL)
6335 for (p = s; *p != NUL; ++p)
6336 if (*p >= 128)
6337 return TRUE;
6338 return FALSE;
6339}
6340#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006341
6342#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006343# define MAX_REPEAT_PARSE 8
6344
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006345/*
6346 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006347 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006348 * These functions can call arbitrary vimscript and should only be called when
6349 * it is safe to do so.
6350 */
6351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006352parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006353{
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006354 win_T *old_curwin = curwin;
6355 int i;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006356
Bram Moolenaar94f01952018-09-01 15:30:03 +02006357 // Do not handle messages while redrawing, because it may cause buffers to
6358 // change or be wiped while they are being redrawn.
6359 if (updating_screen)
6360 return;
6361
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006362 // Loop when a job ended, but don't keep looping forever.
6363 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
6364 {
6365 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006366# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006367 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006368# endif
6369
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006370# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006371 // Process the queued netbeans messages.
6372 netbeans_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006373# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006374# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006375 // Write any buffer lines still to be written.
6376 channel_write_any_lines();
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006377
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006378 // Process the messages queued on channels.
6379 channel_parse_messages();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006380# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006381# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006382 // Process the queued clientserver messages.
6383 server_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006384# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006385# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006386 // Check if any jobs have ended. If so, repeat the above to handle
6387 // changes, e.g. stdin may have been closed.
6388 if (job_check_ended())
6389 continue;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006390# endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01006391# ifdef FEAT_TERMINAL
6392 free_unused_terminals();
6393# endif
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006394 break;
6395 }
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006396
Bram Moolenaar94f01952018-09-01 15:30:03 +02006397 // If the current window changed we need to bail out of the waiting loop.
6398 // E.g. when a job exit callback closes the terminal window.
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006399 if (curwin != old_curwin)
6400 ins_char_typebuf(K_IGNORE);
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006401}
6402#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006403
Bram Moolenaar51628222016-12-01 23:03:28 +01006404#ifndef PROTO /* proto is defined in vim.h */
6405# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006406/*
6407 * Return time in msec since "start_tv".
6408 */
6409 long
6410elapsed(struct timeval *start_tv)
6411{
6412 struct timeval now_tv;
6413
6414 gettimeofday(&now_tv, NULL);
6415 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6416 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6417}
Bram Moolenaar51628222016-12-01 23:03:28 +01006418# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006419
Bram Moolenaar51628222016-12-01 23:03:28 +01006420# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006421/*
6422 * Return time in msec since "start_tick".
6423 */
6424 long
6425elapsed(DWORD start_tick)
6426{
6427 DWORD now = GetTickCount();
6428
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006429 return (long)now - (long)start_tick;
6430}
Bram Moolenaar51628222016-12-01 23:03:28 +01006431# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006432#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006433
6434#if defined(FEAT_JOB_CHANNEL) \
6435 || (defined(UNIX) && (!defined(USE_SYSTEM) \
6436 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
6437 || defined(PROTO)
6438/*
6439 * Parse "cmd" and put the white-separated parts in "argv".
6440 * "argv" is an allocated array with "argc" entries and room for 4 more.
6441 * Returns FAIL when out of memory.
6442 */
6443 int
6444mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
6445{
6446 int i;
6447 char_u *p, *d;
6448 int inquote;
6449
6450 /*
6451 * Do this loop twice:
6452 * 1: find number of arguments
6453 * 2: separate them and build argv[]
6454 */
6455 for (i = 0; i < 2; ++i)
6456 {
6457 p = skipwhite(cmd);
6458 inquote = FALSE;
6459 *argc = 0;
6460 for (;;)
6461 {
6462 if (i == 1)
6463 (*argv)[*argc] = (char *)p;
6464 ++*argc;
6465 d = p;
6466 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
6467 {
6468 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006469 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02006470 inquote = !inquote;
6471 else
6472 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006473 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02006474 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006475 // First pass: skip over "\ " and "\"".
6476 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02006477 ++p;
6478 }
6479 if (i == 1)
6480 *d++ = *p;
6481 }
6482 ++p;
6483 }
6484 if (*p == NUL)
6485 {
6486 if (i == 1)
6487 *d++ = NUL;
6488 break;
6489 }
6490 if (i == 1)
6491 *d++ = NUL;
6492 p = skipwhite(p + 1);
6493 }
6494 if (*argv == NULL)
6495 {
6496 if (use_shcf)
6497 {
6498 /* Account for possible multiple args in p_shcf. */
6499 p = p_shcf;
6500 for (;;)
6501 {
6502 p = skiptowhite(p);
6503 if (*p == NUL)
6504 break;
6505 ++*argc;
6506 p = skipwhite(p);
6507 }
6508 }
6509
6510 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
6511 if (*argv == NULL) /* out of memory */
6512 return FAIL;
6513 }
6514 }
6515 return OK;
6516}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006517
6518# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6519/*
6520 * Build "argv[argc]" from the string "cmd".
6521 * "argv[argc]" is set to NULL;
6522 * Return FAIL when out of memory.
6523 */
6524 int
6525build_argv_from_string(char_u *cmd, char ***argv, int *argc)
6526{
6527 char_u *cmd_copy;
6528 int i;
6529
6530 /* Make a copy, parsing will modify "cmd". */
6531 cmd_copy = vim_strsave(cmd);
6532 if (cmd_copy == NULL
6533 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
6534 {
6535 vim_free(cmd_copy);
6536 return FAIL;
6537 }
6538 for (i = 0; i < *argc; i++)
6539 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
6540 (*argv)[*argc] = NULL;
6541 vim_free(cmd_copy);
6542 return OK;
6543}
6544
6545/*
6546 * Build "argv[argc]" from the list "l".
6547 * "argv[argc]" is set to NULL;
6548 * Return FAIL when out of memory.
6549 */
6550 int
6551build_argv_from_list(list_T *l, char ***argv, int *argc)
6552{
6553 listitem_T *li;
6554 char_u *s;
6555
6556 /* Pass argv[] to mch_call_shell(). */
6557 *argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
6558 if (*argv == NULL)
6559 return FAIL;
6560 *argc = 0;
6561 for (li = l->lv_first; li != NULL; li = li->li_next)
6562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006563 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006564 if (s == NULL)
6565 {
6566 int i;
6567
6568 for (i = 0; i < *argc; ++i)
6569 vim_free((*argv)[i]);
6570 return FAIL;
6571 }
6572 (*argv)[*argc] = (char *)vim_strsave(s);
6573 *argc += 1;
6574 }
6575 (*argv)[*argc] = NULL;
6576 return OK;
6577}
6578# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006579#endif