blob: d85ad119b8baabd3dd8e8cbf8c429501c30f95b9 [file] [log] [blame]
Bram Moolenaardefa0672019-07-21 19:25:37 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * viminfo.c: viminfo related functions
12 */
13
14#include "vim.h"
15#include "version.h"
16
17#if defined(FEAT_VIMINFO) || defined(PROTO)
18
19static int viminfo_errcnt;
20
21/*
22 * Get the viminfo file name to use.
23 * If "file" is given and not empty, use it (has already been expanded by
24 * cmdline functions).
25 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
26 * expand environment variables.
27 * Returns an allocated string. NULL when out of memory.
28 */
29 static char_u *
30viminfo_filename(char_u *file)
31{
32 if (file == NULL || *file == NUL)
33 {
34 if (*p_viminfofile != NUL)
35 file = p_viminfofile;
36 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
37 {
38#ifdef VIMINFO_FILE2
39# ifdef VMS
40 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
41# else
42# ifdef MSWIN
43 // Use $VIM only if $HOME is the default "C:/".
44 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
45 && mch_getenv((char_u *)"HOME") == NULL)
46# else
47 if (mch_getenv((char_u *)"HOME") == NULL)
48# endif
49# endif
50 {
51 // don't use $VIM when not available.
52 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
53 if (STRCMP("$VIM", NameBuff) != 0) // $VIM was expanded
54 file = (char_u *)VIMINFO_FILE2;
55 else
56 file = (char_u *)VIMINFO_FILE;
57 }
58 else
59#endif
60 file = (char_u *)VIMINFO_FILE;
61 }
62 expand_env(file, NameBuff, MAXPATHL);
63 file = NameBuff;
64 }
65 return vim_strsave(file);
66}
67
68 static int
69read_viminfo_bufferlist(
70 vir_T *virp,
71 int writing)
72{
73 char_u *tab;
74 linenr_T lnum;
75 colnr_T col;
76 buf_T *buf;
77 char_u *sfname;
78 char_u *xline;
79
80 // Handle long line and escaped characters.
81 xline = viminfo_readstring(virp, 1, FALSE);
82
83 // don't read in if there are files on the command-line or if writing:
84 if (xline != NULL && !writing && ARGCOUNT == 0
85 && find_viminfo_parameter('%') != NULL)
86 {
87 // Format is: <fname> Tab <lnum> Tab <col>.
88 // Watch out for a Tab in the file name, work from the end.
89 lnum = 0;
90 col = 0;
91 tab = vim_strrchr(xline, '\t');
92 if (tab != NULL)
93 {
94 *tab++ = '\0';
95 col = (colnr_T)atoi((char *)tab);
96 tab = vim_strrchr(xline, '\t');
97 if (tab != NULL)
98 {
99 *tab++ = '\0';
100 lnum = atol((char *)tab);
101 }
102 }
103
104 // Expand "~/" in the file name at "line + 1" to a full path.
105 // Then try shortening it by comparing with the current directory
106 expand_env(xline, NameBuff, MAXPATHL);
107 sfname = shorten_fname1(NameBuff);
108
109 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
110 if (buf != NULL) // just in case...
111 {
112 buf->b_last_cursor.lnum = lnum;
113 buf->b_last_cursor.col = col;
114 buflist_setfpos(buf, curwin, lnum, col, FALSE);
115 }
116 }
117 vim_free(xline);
118
119 return viminfo_readline(virp);
120}
121
122 static void
123write_viminfo_bufferlist(FILE *fp)
124{
125 buf_T *buf;
126 win_T *win;
127 tabpage_T *tp;
128 char_u *line;
129 int max_buffers;
130
131 if (find_viminfo_parameter('%') == NULL)
132 return;
133
134 // Without a number -1 is returned: do all buffers.
135 max_buffers = get_viminfo_parameter('%');
136
137 // Allocate room for the file name, lnum and col.
138#define LINE_BUF_LEN (MAXPATHL + 40)
139 line = alloc(LINE_BUF_LEN);
140 if (line == NULL)
141 return;
142
143 FOR_ALL_TAB_WINDOWS(tp, win)
144 set_last_cursor(win);
145
146 fputs(_("\n# Buffer list:\n"), fp);
147 FOR_ALL_BUFFERS(buf)
148 {
149 if (buf->b_fname == NULL
150 || !buf->b_p_bl
151#ifdef FEAT_QUICKFIX
152 || bt_quickfix(buf)
153#endif
154#ifdef FEAT_TERMINAL
155 || bt_terminal(buf)
156#endif
157 || removable(buf->b_ffname))
158 continue;
159
160 if (max_buffers-- == 0)
161 break;
162 putc('%', fp);
163 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
164 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
165 (long)buf->b_last_cursor.lnum,
166 buf->b_last_cursor.col);
167 viminfo_writestring(fp, line);
168 }
169 vim_free(line);
170}
171
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200172#if defined(FEAT_CMDHIST) || defined(PROTO)
173/*
174 * Buffers for history read from a viminfo file. Only valid while reading.
175 */
176static histentry_T *viminfo_history[HIST_COUNT] =
177 {NULL, NULL, NULL, NULL, NULL};
178static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
179static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
180static int viminfo_add_at_front = FALSE;
181
182/*
183 * Translate a history type number to the associated character.
184 */
185 static int
186hist_type2char(
187 int type,
188 int use_question) // use '?' instead of '/'
189{
190 if (type == HIST_CMD)
191 return ':';
192 if (type == HIST_SEARCH)
193 {
194 if (use_question)
195 return '?';
196 else
197 return '/';
198 }
199 if (type == HIST_EXPR)
200 return '=';
201 return '@';
202}
203
204/*
205 * Prepare for reading the history from the viminfo file.
206 * This allocates history arrays to store the read history lines.
207 */
208 static void
209prepare_viminfo_history(int asklen, int writing)
210{
211 int i;
212 int num;
213 int type;
214 int len;
215 int hislen = get_hislen();
216
217 init_history();
218 viminfo_add_at_front = (asklen != 0 && !writing);
219 if (asklen > hislen)
220 asklen = hislen;
221
222 for (type = 0; type < HIST_COUNT; ++type)
223 {
224 histentry_T *histentry = get_histentry(type);
225
226 // Count the number of empty spaces in the history list. Entries read
227 // from viminfo previously are also considered empty. If there are
228 // more spaces available than we request, then fill them up.
229 for (i = 0, num = 0; i < hislen; i++)
230 if (histentry[i].hisstr == NULL || histentry[i].viminfo)
231 num++;
232 len = asklen;
233 if (num > len)
234 len = num;
235 if (len <= 0)
236 viminfo_history[type] = NULL;
237 else
238 viminfo_history[type] = LALLOC_MULT(histentry_T, len);
239 if (viminfo_history[type] == NULL)
240 len = 0;
241 viminfo_hislen[type] = len;
242 viminfo_hisidx[type] = 0;
243 }
244}
245
246/*
247 * Accept a line from the viminfo, store it in the history array when it's
248 * new.
249 */
250 static int
251read_viminfo_history(vir_T *virp, int writing)
252{
253 int type;
254 long_u len;
255 char_u *val;
256 char_u *p;
257
258 type = hist_char2type(virp->vir_line[0]);
259 if (viminfo_hisidx[type] < viminfo_hislen[type])
260 {
261 val = viminfo_readstring(virp, 1, TRUE);
262 if (val != NULL && *val != NUL)
263 {
264 int sep = (*val == ' ' ? NUL : *val);
265
266 if (!in_history(type, val + (type == HIST_SEARCH),
267 viminfo_add_at_front, sep, writing))
268 {
269 // Need to re-allocate to append the separator byte.
270 len = STRLEN(val);
271 p = alloc(len + 2);
272 if (p != NULL)
273 {
274 if (type == HIST_SEARCH)
275 {
276 // Search entry: Move the separator from the first
277 // column to after the NUL.
278 mch_memmove(p, val + 1, (size_t)len);
279 p[len] = sep;
280 }
281 else
282 {
283 // Not a search entry: No separator in the viminfo
284 // file, add a NUL separator.
285 mch_memmove(p, val, (size_t)len + 1);
286 p[len + 1] = NUL;
287 }
288 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
289 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
290 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
291 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
292 viminfo_hisidx[type]++;
293 }
294 }
295 }
296 vim_free(val);
297 }
298 return viminfo_readline(virp);
299}
300
301/*
302 * Accept a new style history line from the viminfo, store it in the history
303 * array when it's new.
304 */
305 static void
306handle_viminfo_history(
307 garray_T *values,
308 int writing)
309{
310 int type;
311 long_u len;
312 char_u *val;
313 char_u *p;
314 bval_T *vp = (bval_T *)values->ga_data;
315
316 // Check the format:
317 // |{bartype},{histtype},{timestamp},{separator},"text"
318 if (values->ga_len < 4
319 || vp[0].bv_type != BVAL_NR
320 || vp[1].bv_type != BVAL_NR
321 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
322 || vp[3].bv_type != BVAL_STRING)
323 return;
324
325 type = vp[0].bv_nr;
326 if (type >= HIST_COUNT)
327 return;
328 if (viminfo_hisidx[type] < viminfo_hislen[type])
329 {
330 val = vp[3].bv_string;
331 if (val != NULL && *val != NUL)
332 {
333 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
334 ? vp[2].bv_nr : NUL;
335 int idx;
336 int overwrite = FALSE;
337
338 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
339 {
340 // If lines were written by an older Vim we need to avoid
341 // getting duplicates. See if the entry already exists.
342 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
343 {
344 p = viminfo_history[type][idx].hisstr;
345 if (STRCMP(val, p) == 0
346 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
347 {
348 overwrite = TRUE;
349 break;
350 }
351 }
352
353 if (!overwrite)
354 {
355 // Need to re-allocate to append the separator byte.
356 len = vp[3].bv_len;
357 p = alloc(len + 2);
358 }
359 else
360 len = 0; // for picky compilers
361 if (p != NULL)
362 {
363 viminfo_history[type][idx].time_set = vp[1].bv_nr;
364 if (!overwrite)
365 {
366 mch_memmove(p, val, (size_t)len + 1);
367 // Put the separator after the NUL.
368 p[len + 1] = sep;
369 viminfo_history[type][idx].hisstr = p;
370 viminfo_history[type][idx].hisnum = 0;
371 viminfo_history[type][idx].viminfo = TRUE;
372 viminfo_hisidx[type]++;
373 }
374 }
375 }
376 }
377 }
378}
379
380/*
381 * Concatenate history lines from viminfo after the lines typed in this Vim.
382 */
383 static void
384concat_history(int type)
385{
386 int idx;
387 int i;
388 int hislen = get_hislen();
389 histentry_T *histentry = get_histentry(type);
390 int *hisidx = get_hisidx(type);
391 int *hisnum = get_hisnum(type);
392
393 idx = *hisidx + viminfo_hisidx[type];
394 if (idx >= hislen)
395 idx -= hislen;
396 else if (idx < 0)
397 idx = hislen - 1;
398 if (viminfo_add_at_front)
399 *hisidx = idx;
400 else
401 {
402 if (*hisidx == -1)
403 *hisidx = hislen - 1;
404 do
405 {
406 if (histentry[idx].hisstr != NULL || histentry[idx].viminfo)
407 break;
408 if (++idx == hislen)
409 idx = 0;
410 } while (idx != *hisidx);
411 if (idx != *hisidx && --idx < 0)
412 idx = hislen - 1;
413 }
414 for (i = 0; i < viminfo_hisidx[type]; i++)
415 {
416 vim_free(histentry[idx].hisstr);
417 histentry[idx].hisstr = viminfo_history[type][i].hisstr;
418 histentry[idx].viminfo = TRUE;
419 histentry[idx].time_set = viminfo_history[type][i].time_set;
420 if (--idx < 0)
421 idx = hislen - 1;
422 }
423 idx += 1;
424 idx %= hislen;
425 for (i = 0; i < viminfo_hisidx[type]; i++)
426 {
427 histentry[idx++].hisnum = ++*hisnum;
428 idx %= hislen;
429 }
430}
431
432 static int
433sort_hist(const void *s1, const void *s2)
434{
435 histentry_T *p1 = *(histentry_T **)s1;
436 histentry_T *p2 = *(histentry_T **)s2;
437
438 if (p1->time_set < p2->time_set) return -1;
439 if (p1->time_set > p2->time_set) return 1;
440 return 0;
441}
442
443/*
444 * Merge history lines from viminfo and lines typed in this Vim based on the
445 * timestamp;
446 */
447 static void
448merge_history(int type)
449{
450 int max_len;
451 histentry_T **tot_hist;
452 histentry_T *new_hist;
453 int i;
454 int len;
455 int hislen = get_hislen();
456 histentry_T *histentry = get_histentry(type);
457 int *hisidx = get_hisidx(type);
458 int *hisnum = get_hisnum(type);
459
460 // Make one long list with all entries.
461 max_len = hislen + viminfo_hisidx[type];
462 tot_hist = ALLOC_MULT(histentry_T *, max_len);
463 new_hist = ALLOC_MULT(histentry_T, hislen );
464 if (tot_hist == NULL || new_hist == NULL)
465 {
466 vim_free(tot_hist);
467 vim_free(new_hist);
468 return;
469 }
470 for (i = 0; i < viminfo_hisidx[type]; i++)
471 tot_hist[i] = &viminfo_history[type][i];
472 len = i;
473 for (i = 0; i < hislen; i++)
474 if (histentry[i].hisstr != NULL)
475 tot_hist[len++] = &histentry[i];
476
477 // Sort the list on timestamp.
478 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
479
480 // Keep the newest ones.
481 for (i = 0; i < hislen; i++)
482 {
483 if (i < len)
484 {
485 new_hist[i] = *tot_hist[i];
486 tot_hist[i]->hisstr = NULL;
487 if (new_hist[i].hisnum == 0)
488 new_hist[i].hisnum = ++*hisnum;
489 }
490 else
491 clear_hist_entry(&new_hist[i]);
492 }
493 *hisidx = (i < len ? i : len) - 1;
494
495 // Free what is not kept.
496 for (i = 0; i < viminfo_hisidx[type]; i++)
497 vim_free(viminfo_history[type][i].hisstr);
498 for (i = 0; i < hislen; i++)
499 vim_free(histentry[i].hisstr);
500 vim_free(histentry);
501 set_histentry(type, new_hist);
502 vim_free(tot_hist);
503}
504
505/*
506 * Finish reading history lines from viminfo. Not used when writing viminfo.
507 */
508 static void
509finish_viminfo_history(vir_T *virp)
510{
511 int type;
512 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
513
514 for (type = 0; type < HIST_COUNT; ++type)
515 {
516 if (get_histentry(type) == NULL)
517 continue;
518
519 if (merge)
520 merge_history(type);
521 else
522 concat_history(type);
523
524 VIM_CLEAR(viminfo_history[type]);
525 viminfo_hisidx[type] = 0;
526 }
527}
528
529/*
530 * Write history to viminfo file in "fp".
531 * When "merge" is TRUE merge history lines with a previously read viminfo
532 * file, data is in viminfo_history[].
533 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
534 */
535 static void
536write_viminfo_history(FILE *fp, int merge)
537{
538 int i;
539 int type;
540 int num_saved;
541 int round;
542 int hislen;
543
544 init_history();
545 hislen = get_hislen();
546 if (hislen == 0)
547 return;
548 for (type = 0; type < HIST_COUNT; ++type)
549 {
550 histentry_T *histentry = get_histentry(type);
551 int *hisidx = get_hisidx(type);
552
553 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
554 if (num_saved == 0)
555 continue;
556 if (num_saved < 0) // Use default
557 num_saved = hislen;
558 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
559 type == HIST_CMD ? _("Command Line") :
560 type == HIST_SEARCH ? _("Search String") :
561 type == HIST_EXPR ? _("Expression") :
562 type == HIST_INPUT ? _("Input Line") :
563 _("Debug Line"));
564 if (num_saved > hislen)
565 num_saved = hislen;
566
567 /*
568 * Merge typed and viminfo history:
569 * round 1: history of typed commands.
570 * round 2: history from recently read viminfo.
571 */
572 for (round = 1; round <= 2; ++round)
573 {
574 if (round == 1)
575 // start at newest entry, somewhere in the list
576 i = *hisidx;
577 else if (viminfo_hisidx[type] > 0)
578 // start at newest entry, first in the list
579 i = 0;
580 else
581 // empty list
582 i = -1;
583 if (i >= 0)
584 while (num_saved > 0
585 && !(round == 2 && i >= viminfo_hisidx[type]))
586 {
587 char_u *p;
588 time_t timestamp;
589 int c = NUL;
590
591 if (round == 1)
592 {
593 p = histentry[i].hisstr;
594 timestamp = histentry[i].time_set;
595 }
596 else
597 {
598 p = viminfo_history[type] == NULL ? NULL
599 : viminfo_history[type][i].hisstr;
600 timestamp = viminfo_history[type] == NULL ? 0
601 : viminfo_history[type][i].time_set;
602 }
603
604 if (p != NULL && (round == 2
605 || !merge
606 || !histentry[i].viminfo))
607 {
608 --num_saved;
609 fputc(hist_type2char(type, TRUE), fp);
610 // For the search history: put the separator in the
611 // second column; use a space if there isn't one.
612 if (type == HIST_SEARCH)
613 {
614 c = p[STRLEN(p) + 1];
615 putc(c == NUL ? ' ' : c, fp);
616 }
617 viminfo_writestring(fp, p);
618
619 {
620 char cbuf[NUMBUFLEN];
621
622 // New style history with a bar line. Format:
623 // |{bartype},{histtype},{timestamp},{separator},"text"
624 if (c == NUL)
625 cbuf[0] = NUL;
626 else
627 sprintf(cbuf, "%d", c);
628 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
629 type, (long)timestamp, cbuf);
630 barline_writestring(fp, p, LSIZE - 20);
631 putc('\n', fp);
632 }
633 }
634 if (round == 1)
635 {
636 // Decrement index, loop around and stop when back at
637 // the start.
638 if (--i < 0)
639 i = hislen - 1;
640 if (i == *hisidx)
641 break;
642 }
643 else
644 {
645 // Increment index. Stop at the end in the while.
646 ++i;
647 }
648 }
649 }
650 for (i = 0; i < viminfo_hisidx[type]; ++i)
651 if (viminfo_history[type] != NULL)
652 vim_free(viminfo_history[type][i].hisstr);
653 VIM_CLEAR(viminfo_history[type]);
654 viminfo_hisidx[type] = 0;
655 }
656}
657#endif // FEAT_VIMINFO
658
Bram Moolenaardefa0672019-07-21 19:25:37 +0200659 static void
660write_viminfo_barlines(vir_T *virp, FILE *fp_out)
661{
662 int i;
663 garray_T *gap = &virp->vir_barlines;
664 int seen_useful = FALSE;
665 char *line;
666
667 if (gap->ga_len > 0)
668 {
669 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
670
671 // Skip over continuation lines until seeing a useful line.
672 for (i = 0; i < gap->ga_len; ++i)
673 {
674 line = ((char **)(gap->ga_data))[i];
675 if (seen_useful || line[1] != '<')
676 {
677 fputs(line, fp_out);
678 seen_useful = TRUE;
679 }
680 }
681 }
682}
683
684/*
685 * Parse a viminfo line starting with '|'.
686 * Add each decoded value to "values".
687 * Returns TRUE if the next line is to be read after using the parsed values.
688 */
689 static int
690barline_parse(vir_T *virp, char_u *text, garray_T *values)
691{
692 char_u *p = text;
693 char_u *nextp = NULL;
694 char_u *buf = NULL;
695 bval_T *value;
696 int i;
697 int allocated = FALSE;
698 int eof;
699 char_u *sconv;
700 int converted;
701
702 while (*p == ',')
703 {
704 ++p;
705 if (ga_grow(values, 1) == FAIL)
706 break;
707 value = (bval_T *)(values->ga_data) + values->ga_len;
708
709 if (*p == '>')
710 {
711 // Need to read a continuation line. Put strings in allocated
712 // memory, because virp->vir_line is overwritten.
713 if (!allocated)
714 {
715 for (i = 0; i < values->ga_len; ++i)
716 {
717 bval_T *vp = (bval_T *)(values->ga_data) + i;
718
719 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
720 {
721 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
722 vp->bv_allocated = TRUE;
723 }
724 }
725 allocated = TRUE;
726 }
727
728 if (vim_isdigit(p[1]))
729 {
730 size_t len;
731 size_t todo;
732 size_t n;
733
734 // String value was split into lines that are each shorter
735 // than LSIZE:
736 // |{bartype},>{length of "{text}{text2}"}
737 // |<"{text1}
738 // |<{text2}",{value}
739 // Length includes the quotes.
740 ++p;
741 len = getdigits(&p);
742 buf = alloc((int)(len + 1));
743 if (buf == NULL)
744 return TRUE;
745 p = buf;
746 for (todo = len; todo > 0; todo -= n)
747 {
748 eof = viminfo_readline(virp);
749 if (eof || virp->vir_line[0] != '|'
750 || virp->vir_line[1] != '<')
751 {
752 // File was truncated or garbled. Read another line if
753 // this one starts with '|'.
754 vim_free(buf);
755 return eof || virp->vir_line[0] == '|';
756 }
757 // Get length of text, excluding |< and NL chars.
758 n = STRLEN(virp->vir_line);
759 while (n > 0 && (virp->vir_line[n - 1] == NL
760 || virp->vir_line[n - 1] == CAR))
761 --n;
762 n -= 2;
763 if (n > todo)
764 {
765 // more values follow after the string
766 nextp = virp->vir_line + 2 + todo;
767 n = todo;
768 }
769 mch_memmove(p, virp->vir_line + 2, n);
770 p += n;
771 }
772 *p = NUL;
773 p = buf;
774 }
775 else
776 {
777 // Line ending in ">" continues in the next line:
778 // |{bartype},{lots of values},>
779 // |<{value},{value}
780 eof = viminfo_readline(virp);
781 if (eof || virp->vir_line[0] != '|'
782 || virp->vir_line[1] != '<')
783 // File was truncated or garbled. Read another line if
784 // this one starts with '|'.
785 return eof || virp->vir_line[0] == '|';
786 p = virp->vir_line + 2;
787 }
788 }
789
790 if (isdigit(*p))
791 {
792 value->bv_type = BVAL_NR;
793 value->bv_nr = getdigits(&p);
794 ++values->ga_len;
795 }
796 else if (*p == '"')
797 {
798 int len = 0;
799 char_u *s = p;
800
801 // Unescape special characters in-place.
802 ++p;
803 while (*p != '"')
804 {
805 if (*p == NL || *p == NUL)
806 return TRUE; // syntax error, drop the value
807 if (*p == '\\')
808 {
809 ++p;
810 if (*p == 'n')
811 s[len++] = '\n';
812 else
813 s[len++] = *p;
814 ++p;
815 }
816 else
817 s[len++] = *p++;
818 }
819 ++p;
820 s[len] = NUL;
821
822 converted = FALSE;
823 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
824 {
825 sconv = string_convert(&virp->vir_conv, s, NULL);
826 if (sconv != NULL)
827 {
828 if (s == buf)
829 vim_free(s);
830 s = sconv;
831 buf = s;
832 converted = TRUE;
833 }
834 }
835
836 // Need to copy in allocated memory if the string wasn't allocated
837 // above and we did allocate before, thus vir_line may change.
838 if (s != buf && allocated)
839 s = vim_strsave(s);
840 value->bv_string = s;
841 value->bv_type = BVAL_STRING;
842 value->bv_len = len;
843 value->bv_allocated = allocated || converted;
844 ++values->ga_len;
845 if (nextp != NULL)
846 {
847 // values following a long string
848 p = nextp;
849 nextp = NULL;
850 }
851 }
852 else if (*p == ',')
853 {
854 value->bv_type = BVAL_EMPTY;
855 ++values->ga_len;
856 }
857 else
858 break;
859 }
860 return TRUE;
861}
862
863 static int
864read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
865{
866 char_u *p = virp->vir_line + 1;
867 int bartype;
868 garray_T values;
869 bval_T *vp;
870 int i;
871 int read_next = TRUE;
872
873 /*
874 * The format is: |{bartype},{value},...
875 * For a very long string:
876 * |{bartype},>{length of "{text}{text2}"}
877 * |<{text1}
878 * |<{text2},{value}
879 * For a long line not using a string
880 * |{bartype},{lots of values},>
881 * |<{value},{value}
882 */
883 if (*p == '<')
884 {
885 // Continuation line of an unrecognized item.
886 if (writing)
887 ga_add_string(&virp->vir_barlines, virp->vir_line);
888 }
889 else
890 {
891 ga_init2(&values, sizeof(bval_T), 20);
892 bartype = getdigits(&p);
893 switch (bartype)
894 {
895 case BARTYPE_VERSION:
896 // Only use the version when it comes before the encoding.
897 // If it comes later it was copied by a Vim version that
898 // doesn't understand the version.
899 if (!got_encoding)
900 {
901 read_next = barline_parse(virp, p, &values);
902 vp = (bval_T *)values.ga_data;
903 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
904 virp->vir_version = vp->bv_nr;
905 }
906 break;
907
908 case BARTYPE_HISTORY:
909 read_next = barline_parse(virp, p, &values);
910 handle_viminfo_history(&values, writing);
911 break;
912
913 case BARTYPE_REGISTER:
914 read_next = barline_parse(virp, p, &values);
915 handle_viminfo_register(&values, force);
916 break;
917
918 case BARTYPE_MARK:
919 read_next = barline_parse(virp, p, &values);
920 handle_viminfo_mark(&values, force);
921 break;
922
923 default:
924 // copy unrecognized line (for future use)
925 if (writing)
926 ga_add_string(&virp->vir_barlines, virp->vir_line);
927 }
928 for (i = 0; i < values.ga_len; ++i)
929 {
930 vp = (bval_T *)values.ga_data + i;
931 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
932 vim_free(vp->bv_string);
933 }
934 ga_clear(&values);
935 }
936
937 if (read_next)
938 return viminfo_readline(virp);
939 return FALSE;
940}
941
942 static void
943write_viminfo_version(FILE *fp_out)
944{
945 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
946 BARTYPE_VERSION, VIMINFO_VERSION);
947}
948
949 static int
950no_viminfo(void)
951{
952 // "vim -i NONE" does not read or write a viminfo file
953 return STRCMP(p_viminfofile, "NONE") == 0;
954}
955
956/*
957 * Report an error for reading a viminfo file.
958 * Count the number of errors. When there are more than 10, return TRUE.
959 */
960 int
961viminfo_error(char *errnum, char *message, char_u *line)
962{
963 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
964 errnum, message);
965 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
966 if (IObuff[STRLEN(IObuff) - 1] == '\n')
967 IObuff[STRLEN(IObuff) - 1] = NUL;
968 emsg((char *)IObuff);
969 if (++viminfo_errcnt >= 10)
970 {
971 emsg(_("E136: viminfo: Too many errors, skipping rest of file"));
972 return TRUE;
973 }
974 return FALSE;
975}
976
977/*
978 * Compare the 'encoding' value in the viminfo file with the current value of
979 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
980 * conversion of text with iconv() in viminfo_readstring().
981 */
982 static int
983viminfo_encoding(vir_T *virp)
984{
985 char_u *p;
986 int i;
987
988 if (get_viminfo_parameter('c') != 0)
989 {
990 p = vim_strchr(virp->vir_line, '=');
991 if (p != NULL)
992 {
993 // remove trailing newline
994 ++p;
995 for (i = 0; vim_isprintc(p[i]); ++i)
996 ;
997 p[i] = NUL;
998
999 convert_setup(&virp->vir_conv, p, p_enc);
1000 }
1001 }
1002 return viminfo_readline(virp);
1003}
1004
1005#if defined(FEAT_EVAL) || defined(PROTO)
1006/*
1007 * Restore global vars that start with a capital from the viminfo file
1008 */
1009 static int
1010read_viminfo_varlist(vir_T *virp, int writing)
1011{
1012 char_u *tab;
1013 int type = VAR_NUMBER;
1014 typval_T tv;
1015 funccal_entry_T funccal_entry;
1016
1017 if (!writing && (find_viminfo_parameter('!') != NULL))
1018 {
1019 tab = vim_strchr(virp->vir_line + 1, '\t');
1020 if (tab != NULL)
1021 {
1022 *tab++ = '\0'; // isolate the variable name
1023 switch (*tab)
1024 {
1025 case 'S': type = VAR_STRING; break;
1026#ifdef FEAT_FLOAT
1027 case 'F': type = VAR_FLOAT; break;
1028#endif
1029 case 'D': type = VAR_DICT; break;
1030 case 'L': type = VAR_LIST; break;
1031 case 'B': type = VAR_BLOB; break;
1032 case 'X': type = VAR_SPECIAL; break;
1033 }
1034
1035 tab = vim_strchr(tab, '\t');
1036 if (tab != NULL)
1037 {
1038 tv.v_type = type;
1039 if (type == VAR_STRING || type == VAR_DICT
1040 || type == VAR_LIST || type == VAR_BLOB)
1041 tv.vval.v_string = viminfo_readstring(virp,
1042 (int)(tab - virp->vir_line + 1), TRUE);
1043#ifdef FEAT_FLOAT
1044 else if (type == VAR_FLOAT)
1045 (void)string2float(tab + 1, &tv.vval.v_float);
1046#endif
1047 else
1048 tv.vval.v_number = atol((char *)tab + 1);
1049 if (type == VAR_DICT || type == VAR_LIST)
1050 {
1051 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
1052
1053 if (etv == NULL)
1054 // Failed to parse back the dict or list, use it as a
1055 // string.
1056 tv.v_type = VAR_STRING;
1057 else
1058 {
1059 vim_free(tv.vval.v_string);
1060 tv = *etv;
1061 vim_free(etv);
1062 }
1063 }
1064 else if (type == VAR_BLOB)
1065 {
1066 blob_T *blob = string2blob(tv.vval.v_string);
1067
1068 if (blob == NULL)
1069 // Failed to parse back the blob, use it as a string.
1070 tv.v_type = VAR_STRING;
1071 else
1072 {
1073 vim_free(tv.vval.v_string);
1074 tv.v_type = VAR_BLOB;
1075 tv.vval.v_blob = blob;
1076 }
1077 }
1078
1079 // when in a function use global variables
1080 save_funccal(&funccal_entry);
1081 set_var(virp->vir_line + 1, &tv, FALSE);
1082 restore_funccal();
1083
1084 if (tv.v_type == VAR_STRING)
1085 vim_free(tv.vval.v_string);
1086 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
1087 tv.v_type == VAR_BLOB)
1088 clear_tv(&tv);
1089 }
1090 }
1091 }
1092
1093 return viminfo_readline(virp);
1094}
1095
1096/*
1097 * Write global vars that start with a capital to the viminfo file
1098 */
1099 static void
1100write_viminfo_varlist(FILE *fp)
1101{
1102 hashitem_T *hi;
1103 dictitem_T *this_var;
1104 int todo;
1105 char *s = "";
1106 char_u *p;
1107 char_u *tofree;
1108 char_u numbuf[NUMBUFLEN];
1109
1110 if (find_viminfo_parameter('!') == NULL)
1111 return;
1112
1113 fputs(_("\n# global variables:\n"), fp);
1114
1115 todo = (int)globvarht.ht_used;
1116 for (hi = globvarht.ht_array; todo > 0; ++hi)
1117 {
1118 if (!HASHITEM_EMPTY(hi))
1119 {
1120 --todo;
1121 this_var = HI2DI(hi);
1122 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
1123 {
1124 switch (this_var->di_tv.v_type)
1125 {
1126 case VAR_STRING: s = "STR"; break;
1127 case VAR_NUMBER: s = "NUM"; break;
1128 case VAR_FLOAT: s = "FLO"; break;
1129 case VAR_DICT: s = "DIC"; break;
1130 case VAR_LIST: s = "LIS"; break;
1131 case VAR_BLOB: s = "BLO"; break;
1132 case VAR_SPECIAL: s = "XPL"; break;
1133
1134 case VAR_UNKNOWN:
1135 case VAR_FUNC:
1136 case VAR_PARTIAL:
1137 case VAR_JOB:
1138 case VAR_CHANNEL:
1139 continue;
1140 }
1141 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
1142 if (this_var->di_tv.v_type == VAR_SPECIAL)
1143 {
1144 sprintf((char *)numbuf, "%ld",
1145 (long)this_var->di_tv.vval.v_number);
1146 p = numbuf;
1147 tofree = NULL;
1148 }
1149 else
1150 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
1151 if (p != NULL)
1152 viminfo_writestring(fp, p);
1153 vim_free(tofree);
1154 }
1155 }
1156 }
1157}
1158#endif // FEAT_EVAL
1159
1160/*
1161 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
1162 * first part of the viminfo file which contains everything but the marks that
1163 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
1164 */
1165 static int
1166read_viminfo_up_to_marks(
1167 vir_T *virp,
1168 int forceit,
1169 int writing)
1170{
1171 int eof;
1172 buf_T *buf;
1173 int got_encoding = FALSE;
1174
1175#ifdef FEAT_CMDHIST
1176 prepare_viminfo_history(forceit ? 9999 : 0, writing);
1177#endif
1178
1179 eof = viminfo_readline(virp);
1180 while (!eof && virp->vir_line[0] != '>')
1181 {
1182 switch (virp->vir_line[0])
1183 {
1184 // Characters reserved for future expansion, ignored now
1185 case '+': // "+40 /path/dir file", for running vim without args
1186 case '^': // to be defined
1187 case '<': // long line - ignored
1188 // A comment or empty line.
1189 case NUL:
1190 case '\r':
1191 case '\n':
1192 case '#':
1193 eof = viminfo_readline(virp);
1194 break;
1195 case '|':
1196 eof = read_viminfo_barline(virp, got_encoding,
1197 forceit, writing);
1198 break;
1199 case '*': // "*encoding=value"
1200 got_encoding = TRUE;
1201 eof = viminfo_encoding(virp);
1202 break;
1203 case '!': // global variable
1204#ifdef FEAT_EVAL
1205 eof = read_viminfo_varlist(virp, writing);
1206#else
1207 eof = viminfo_readline(virp);
1208#endif
1209 break;
1210 case '%': // entry for buffer list
1211 eof = read_viminfo_bufferlist(virp, writing);
1212 break;
1213 case '"':
1214 // When registers are in bar lines skip the old style register
1215 // lines.
1216 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
1217 eof = read_viminfo_register(virp, forceit);
1218 else
1219 do {
1220 eof = viminfo_readline(virp);
1221 } while (!eof && (virp->vir_line[0] == TAB
1222 || virp->vir_line[0] == '<'));
1223 break;
1224 case '/': // Search string
1225 case '&': // Substitute search string
1226 case '~': // Last search string, followed by '/' or '&'
1227 eof = read_viminfo_search_pattern(virp, forceit);
1228 break;
1229 case '$':
1230 eof = read_viminfo_sub_string(virp, forceit);
1231 break;
1232 case ':':
1233 case '?':
1234 case '=':
1235 case '@':
1236#ifdef FEAT_CMDHIST
1237 // When history is in bar lines skip the old style history
1238 // lines.
1239 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
1240 eof = read_viminfo_history(virp, writing);
1241 else
1242#endif
1243 eof = viminfo_readline(virp);
1244 break;
1245 case '-':
1246 case '\'':
1247 // When file marks are in bar lines skip the old style lines.
1248 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
1249 eof = read_viminfo_filemark(virp, forceit);
1250 else
1251 eof = viminfo_readline(virp);
1252 break;
1253 default:
1254 if (viminfo_error("E575: ", _("Illegal starting char"),
1255 virp->vir_line))
1256 eof = TRUE;
1257 else
1258 eof = viminfo_readline(virp);
1259 break;
1260 }
1261 }
1262
1263#ifdef FEAT_CMDHIST
1264 // Finish reading history items.
1265 if (!writing)
1266 finish_viminfo_history(virp);
1267#endif
1268
1269 // Change file names to buffer numbers for fmarks.
1270 FOR_ALL_BUFFERS(buf)
1271 fmarks_check_names(buf);
1272
1273 return eof;
1274}
1275
1276/*
1277 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
1278 */
1279 static void
1280do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
1281{
1282 int eof = FALSE;
1283 vir_T vir;
1284 int merge = FALSE;
1285 int do_copy_marks = FALSE;
1286 garray_T buflist;
1287
1288 if ((vir.vir_line = alloc(LSIZE)) == NULL)
1289 return;
1290 vir.vir_fd = fp_in;
1291 vir.vir_conv.vc_type = CONV_NONE;
1292 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
1293 vir.vir_version = -1;
1294
1295 if (fp_in != NULL)
1296 {
1297 if (flags & VIF_WANT_INFO)
1298 {
1299 if (fp_out != NULL)
1300 {
1301 // Registers and marks are read and kept separate from what
1302 // this Vim is using. They are merged when writing.
1303 prepare_viminfo_registers();
1304 prepare_viminfo_marks();
1305 }
1306
1307 eof = read_viminfo_up_to_marks(&vir,
1308 flags & VIF_FORCEIT, fp_out != NULL);
1309 merge = TRUE;
1310 }
1311 else if (flags != 0)
1312 // Skip info, find start of marks
1313 while (!(eof = viminfo_readline(&vir))
1314 && vir.vir_line[0] != '>')
1315 ;
1316
1317 do_copy_marks = (flags &
1318 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
1319 }
1320
1321 if (fp_out != NULL)
1322 {
1323 // Write the info:
1324 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
1325 VIM_VERSION_MEDIUM);
1326 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
1327 write_viminfo_version(fp_out);
1328 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
1329 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
1330 write_viminfo_search_pattern(fp_out);
1331 write_viminfo_sub_string(fp_out);
1332#ifdef FEAT_CMDHIST
1333 write_viminfo_history(fp_out, merge);
1334#endif
1335 write_viminfo_registers(fp_out);
1336 finish_viminfo_registers();
1337#ifdef FEAT_EVAL
1338 write_viminfo_varlist(fp_out);
1339#endif
1340 write_viminfo_filemarks(fp_out);
1341 finish_viminfo_marks();
1342 write_viminfo_bufferlist(fp_out);
1343 write_viminfo_barlines(&vir, fp_out);
1344
1345 if (do_copy_marks)
1346 ga_init2(&buflist, sizeof(buf_T *), 50);
1347 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
1348 }
1349
1350 if (do_copy_marks)
1351 {
1352 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
1353 if (fp_out != NULL)
1354 ga_clear(&buflist);
1355 }
1356
1357 vim_free(vir.vir_line);
1358 if (vir.vir_conv.vc_type != CONV_NONE)
1359 convert_setup(&vir.vir_conv, NULL, NULL);
1360 ga_clear_strings(&vir.vir_barlines);
1361}
1362
1363/*
1364 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
1365 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
1366 */
1367 int
1368read_viminfo(
1369 char_u *file, // file name or NULL to use default name
1370 int flags) // VIF_WANT_INFO et al.
1371{
1372 FILE *fp;
1373 char_u *fname;
1374
1375 if (no_viminfo())
1376 return FAIL;
1377
1378 fname = viminfo_filename(file); // get file name in allocated buffer
1379 if (fname == NULL)
1380 return FAIL;
1381 fp = mch_fopen((char *)fname, READBIN);
1382
1383 if (p_verbose > 0)
1384 {
1385 verbose_enter();
1386 smsg(_("Reading viminfo file \"%s\"%s%s%s"),
1387 fname,
1388 (flags & VIF_WANT_INFO) ? _(" info") : "",
1389 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1390 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
1391 fp == NULL ? _(" FAILED") : "");
1392 verbose_leave();
1393 }
1394
1395 vim_free(fname);
1396 if (fp == NULL)
1397 return FAIL;
1398
1399 viminfo_errcnt = 0;
1400 do_viminfo(fp, NULL, flags);
1401
1402 fclose(fp);
1403 return OK;
1404}
1405
1406/*
1407 * Write the viminfo file. The old one is read in first so that effectively a
1408 * merge of current info and old info is done. This allows multiple vims to
1409 * run simultaneously, without losing any marks etc.
1410 * If "forceit" is TRUE, then the old file is not read in, and only internal
1411 * info is written to the file.
1412 */
1413 void
1414write_viminfo(char_u *file, int forceit)
1415{
1416 char_u *fname;
1417 FILE *fp_in = NULL; // input viminfo file, if any
1418 FILE *fp_out = NULL; // output viminfo file
1419 char_u *tempname = NULL; // name of temp viminfo file
1420 stat_T st_new; // mch_stat() of potential new file
1421#if defined(UNIX) || defined(VMS)
1422 mode_t umask_save;
1423#endif
1424#ifdef UNIX
1425 int shortname = FALSE; // use 8.3 file name
1426 stat_T st_old; // mch_stat() of existing viminfo file
1427#endif
1428#ifdef MSWIN
1429 int hidden = FALSE;
1430#endif
1431
1432 if (no_viminfo())
1433 return;
1434
1435 fname = viminfo_filename(file); // may set to default if NULL
1436 if (fname == NULL)
1437 return;
1438
1439 fp_in = mch_fopen((char *)fname, READBIN);
1440 if (fp_in == NULL)
1441 {
1442 int fd;
1443
1444 // if it does exist, but we can't read it, don't try writing
1445 if (mch_stat((char *)fname, &st_new) == 0)
1446 goto end;
1447
1448 // Create the new .viminfo non-accessible for others, because it may
1449 // contain text from non-accessible documents. It is up to the user to
1450 // widen access (e.g. to a group). This may also fail if there is a
1451 // race condition, then just give up.
1452 fd = mch_open((char *)fname,
1453 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1454 if (fd < 0)
1455 goto end;
1456 fp_out = fdopen(fd, WRITEBIN);
1457 }
1458 else
1459 {
1460 /*
1461 * There is an existing viminfo file. Create a temporary file to
1462 * write the new viminfo into, in the same directory as the
1463 * existing viminfo file, which will be renamed once all writing is
1464 * successful.
1465 */
1466#ifdef UNIX
1467 /*
1468 * For Unix we check the owner of the file. It's not very nice to
1469 * overwrite a user's viminfo file after a "su root", with a
1470 * viminfo file that the user can't read.
1471 */
1472 st_old.st_dev = (dev_t)0;
1473 st_old.st_ino = 0;
1474 st_old.st_mode = 0600;
1475 if (mch_stat((char *)fname, &st_old) == 0
1476 && getuid() != ROOT_UID
1477 && !(st_old.st_uid == getuid()
1478 ? (st_old.st_mode & 0200)
1479 : (st_old.st_gid == getgid()
1480 ? (st_old.st_mode & 0020)
1481 : (st_old.st_mode & 0002))))
1482 {
1483 int tt = msg_didany;
1484
1485 // avoid a wait_return for this message, it's annoying
1486 semsg(_("E137: Viminfo file is not writable: %s"), fname);
1487 msg_didany = tt;
1488 fclose(fp_in);
1489 goto end;
1490 }
1491#endif
1492#ifdef MSWIN
1493 // Get the file attributes of the existing viminfo file.
1494 hidden = mch_ishidden(fname);
1495#endif
1496
1497 /*
1498 * Make tempname, find one that does not exist yet.
1499 * Beware of a race condition: If someone logs out and all Vim
1500 * instances exit at the same time a temp file might be created between
1501 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
1502 * May try twice: Once normal and once with shortname set, just in
1503 * case somebody puts his viminfo file in an 8.3 filesystem.
1504 */
1505 for (;;)
1506 {
1507 int next_char = 'z';
1508 char_u *wp;
1509
1510 tempname = buf_modname(
1511#ifdef UNIX
1512 shortname,
1513#else
1514 FALSE,
1515#endif
1516 fname,
1517#ifdef VMS
1518 (char_u *)"-tmp",
1519#else
1520 (char_u *)".tmp",
1521#endif
1522 FALSE);
1523 if (tempname == NULL) // out of memory
1524 break;
1525
1526 /*
1527 * Try a series of names. Change one character, just before
1528 * the extension. This should also work for an 8.3
1529 * file name, when after adding the extension it still is
1530 * the same file as the original.
1531 */
1532 wp = tempname + STRLEN(tempname) - 5;
1533 if (wp < gettail(tempname)) // empty file name?
1534 wp = gettail(tempname);
1535 for (;;)
1536 {
1537 /*
1538 * Check if tempfile already exists. Never overwrite an
1539 * existing file!
1540 */
1541 if (mch_stat((char *)tempname, &st_new) == 0)
1542 {
1543#ifdef UNIX
1544 /*
1545 * Check if tempfile is same as original file. May happen
1546 * when modname() gave the same file back. E.g. silly
1547 * link, or file name-length reached. Try again with
1548 * shortname set.
1549 */
1550 if (!shortname && st_new.st_dev == st_old.st_dev
1551 && st_new.st_ino == st_old.st_ino)
1552 {
1553 VIM_CLEAR(tempname);
1554 shortname = TRUE;
1555 break;
1556 }
1557#endif
1558 }
1559 else
1560 {
1561 // Try creating the file exclusively. This may fail if
1562 // another Vim tries to do it at the same time.
1563#ifdef VMS
1564 // fdopen() fails for some reason
1565 umask_save = umask(077);
1566 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1567 (void)umask(umask_save);
1568#else
1569 int fd;
1570
1571 // Use mch_open() to be able to use O_NOFOLLOW and set file
1572 // protection:
1573 // Unix: same as original file, but strip s-bit. Reset
1574 // umask to avoid it getting in the way.
1575 // Others: r&w for user only.
1576# ifdef UNIX
1577 umask_save = umask(0);
1578 fd = mch_open((char *)tempname,
1579 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
1580 (int)((st_old.st_mode & 0777) | 0600));
1581 (void)umask(umask_save);
1582# else
1583 fd = mch_open((char *)tempname,
1584 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1585# endif
1586 if (fd < 0)
1587 {
1588 fp_out = NULL;
1589# ifdef EEXIST
1590 // Avoid trying lots of names while the problem is lack
1591 // of permission, only retry if the file already
1592 // exists.
1593 if (errno != EEXIST)
1594 break;
1595# endif
1596 }
1597 else
1598 fp_out = fdopen(fd, WRITEBIN);
1599#endif // VMS
1600 if (fp_out != NULL)
1601 break;
1602 }
1603
1604 // Assume file exists, try again with another name.
1605 if (next_char == 'a' - 1)
1606 {
1607 // They all exist? Must be something wrong! Don't write
1608 // the viminfo file then.
1609 semsg(_("E929: Too many viminfo temp files, like %s!"),
1610 tempname);
1611 break;
1612 }
1613 *wp = next_char;
1614 --next_char;
1615 }
1616
1617 if (tempname != NULL)
1618 break;
1619 // continue if shortname was set
1620 }
1621
1622#if defined(UNIX) && defined(HAVE_FCHOWN)
1623 if (tempname != NULL && fp_out != NULL)
1624 {
1625 stat_T tmp_st;
1626
1627 /*
1628 * Make sure the original owner can read/write the tempfile and
1629 * otherwise preserve permissions, making sure the group matches.
1630 */
1631 if (mch_stat((char *)tempname, &tmp_st) >= 0)
1632 {
1633 if (st_old.st_uid != tmp_st.st_uid)
1634 // Changing the owner might fail, in which case the
1635 // file will now owned by the current user, oh well.
1636 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
1637 if (st_old.st_gid != tmp_st.st_gid
1638 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
1639 // can't set the group to what it should be, remove
1640 // group permissions
1641 (void)mch_setperm(tempname, 0600);
1642 }
1643 else
1644 // can't stat the file, set conservative permissions
1645 (void)mch_setperm(tempname, 0600);
1646 }
1647#endif
1648 }
1649
1650 /*
1651 * Check if the new viminfo file can be written to.
1652 */
1653 if (fp_out == NULL)
1654 {
1655 semsg(_("E138: Can't write viminfo file %s!"),
1656 (fp_in == NULL || tempname == NULL) ? fname : tempname);
1657 if (fp_in != NULL)
1658 fclose(fp_in);
1659 goto end;
1660 }
1661
1662 if (p_verbose > 0)
1663 {
1664 verbose_enter();
1665 smsg(_("Writing viminfo file \"%s\""), fname);
1666 verbose_leave();
1667 }
1668
1669 viminfo_errcnt = 0;
1670 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
1671
1672 if (fclose(fp_out) == EOF)
1673 ++viminfo_errcnt;
1674
1675 if (fp_in != NULL)
1676 {
1677 fclose(fp_in);
1678
1679 // In case of an error keep the original viminfo file. Otherwise
1680 // rename the newly written file. Give an error if that fails.
1681 if (viminfo_errcnt == 0)
1682 {
1683 if (vim_rename(tempname, fname) == -1)
1684 {
1685 ++viminfo_errcnt;
1686 semsg(_("E886: Can't rename viminfo file to %s!"), fname);
1687 }
1688# ifdef MSWIN
1689 // If the viminfo file was hidden then also hide the new file.
1690 else if (hidden)
1691 mch_hide(fname);
1692# endif
1693 }
1694 if (viminfo_errcnt > 0)
1695 mch_remove(tempname);
1696 }
1697
1698end:
1699 vim_free(fname);
1700 vim_free(tempname);
1701}
1702
1703/*
1704 * Read a line from the viminfo file.
1705 * Returns TRUE for end-of-file;
1706 */
1707 int
1708viminfo_readline(vir_T *virp)
1709{
1710 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1711}
1712
1713/*
1714 * Check string read from viminfo file.
1715 * Remove '\n' at the end of the line.
1716 * - replace CTRL-V CTRL-V with CTRL-V
1717 * - replace CTRL-V 'n' with '\n'
1718 *
1719 * Check for a long line as written by viminfo_writestring().
1720 *
1721 * Return the string in allocated memory (NULL when out of memory).
1722 */
1723 char_u *
1724viminfo_readstring(
1725 vir_T *virp,
1726 int off, // offset for virp->vir_line
1727 int convert UNUSED) // convert the string
1728{
1729 char_u *retval;
1730 char_u *s, *d;
1731 long len;
1732
1733 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
1734 {
1735 len = atol((char *)virp->vir_line + off + 1);
1736 retval = lalloc(len, TRUE);
1737 if (retval == NULL)
1738 {
1739 // Line too long? File messed up? Skip next line.
1740 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
1741 return NULL;
1742 }
1743 (void)vim_fgets(retval, (int)len, virp->vir_fd);
1744 s = retval + 1; // Skip the leading '<'
1745 }
1746 else
1747 {
1748 retval = vim_strsave(virp->vir_line + off);
1749 if (retval == NULL)
1750 return NULL;
1751 s = retval;
1752 }
1753
1754 // Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place.
1755 d = retval;
1756 while (*s != NUL && *s != '\n')
1757 {
1758 if (s[0] == Ctrl_V && s[1] != NUL)
1759 {
1760 if (s[1] == 'n')
1761 *d++ = '\n';
1762 else
1763 *d++ = Ctrl_V;
1764 s += 2;
1765 }
1766 else
1767 *d++ = *s++;
1768 }
1769 *d = NUL;
1770
1771 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
1772 {
1773 d = string_convert(&virp->vir_conv, retval, NULL);
1774 if (d != NULL)
1775 {
1776 vim_free(retval);
1777 retval = d;
1778 }
1779 }
1780
1781 return retval;
1782}
1783
1784/*
1785 * write string to viminfo file
1786 * - replace CTRL-V with CTRL-V CTRL-V
1787 * - replace '\n' with CTRL-V 'n'
1788 * - add a '\n' at the end
1789 *
1790 * For a long line:
1791 * - write " CTRL-V <length> \n " in first line
1792 * - write " < <string> \n " in second line
1793 */
1794 void
1795viminfo_writestring(FILE *fd, char_u *p)
1796{
1797 int c;
1798 char_u *s;
1799 int len = 0;
1800
1801 for (s = p; *s != NUL; ++s)
1802 {
1803 if (*s == Ctrl_V || *s == '\n')
1804 ++len;
1805 ++len;
1806 }
1807
1808 // If the string will be too long, write its length and put it in the next
1809 // line. Take into account that some room is needed for what comes before
1810 // the string (e.g., variable name). Add something to the length for the
1811 // '<', NL and trailing NUL.
1812 if (len > LSIZE / 2)
1813 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
1814
1815 while ((c = *p++) != NUL)
1816 {
1817 if (c == Ctrl_V || c == '\n')
1818 {
1819 putc(Ctrl_V, fd);
1820 if (c == '\n')
1821 c = 'n';
1822 }
1823 putc(c, fd);
1824 }
1825 putc('\n', fd);
1826}
1827
1828/*
1829 * Write a string in quotes that barline_parse() can read back.
1830 * Breaks the line in less than LSIZE pieces when needed.
1831 * Returns remaining characters in the line.
1832 */
1833 int
1834barline_writestring(FILE *fd, char_u *s, int remaining_start)
1835{
1836 char_u *p;
1837 int remaining = remaining_start;
1838 int len = 2;
1839
1840 // Count the number of characters produced, including quotes.
1841 for (p = s; *p != NUL; ++p)
1842 {
1843 if (*p == NL)
1844 len += 2;
1845 else if (*p == '"' || *p == '\\')
1846 len += 2;
1847 else
1848 ++len;
1849 }
1850 if (len > remaining - 2)
1851 {
1852 fprintf(fd, ">%d\n|<", len);
1853 remaining = LSIZE - 20;
1854 }
1855
1856 putc('"', fd);
1857 for (p = s; *p != NUL; ++p)
1858 {
1859 if (*p == NL)
1860 {
1861 putc('\\', fd);
1862 putc('n', fd);
1863 --remaining;
1864 }
1865 else if (*p == '"' || *p == '\\')
1866 {
1867 putc('\\', fd);
1868 putc(*p, fd);
1869 --remaining;
1870 }
1871 else
1872 putc(*p, fd);
1873 --remaining;
1874
1875 if (remaining < 3)
1876 {
1877 putc('\n', fd);
1878 putc('|', fd);
1879 putc('<', fd);
1880 // Leave enough space for another continuation.
1881 remaining = LSIZE - 20;
1882 }
1883 }
1884 putc('"', fd);
1885 return remaining - 2;
1886}
1887
1888/*
1889 * ":rviminfo" and ":wviminfo".
1890 */
1891 void
1892ex_viminfo(
1893 exarg_T *eap)
1894{
1895 char_u *save_viminfo;
1896
1897 save_viminfo = p_viminfo;
1898 if (*p_viminfo == NUL)
1899 p_viminfo = (char_u *)"'100";
1900 if (eap->cmdidx == CMD_rviminfo)
1901 {
1902 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
1903 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
1904 emsg(_("E195: Cannot open viminfo file for reading"));
1905 }
1906 else
1907 write_viminfo(eap->arg, eap->forceit);
1908 p_viminfo = save_viminfo;
1909}
1910
1911#endif // FEAT_VIMINFO