diff options
Diffstat (limited to 'src/memline.c')
-rw-r--r-- | src/memline.c | 4378 |
1 files changed, 4378 insertions, 0 deletions
diff --git a/src/memline.c b/src/memline.c new file mode 100644 index 0000000000..7c18663292 --- /dev/null +++ b/src/memline.c @@ -0,0 +1,4378 @@ +/* vi:set ts=8 sts=4 sw=4: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +/* for debugging */ +/* #define CHECK(c, s) if (c) EMSG(s) */ +#define CHECK(c, s) + +/* + * memline.c: Contains the functions for appending, deleting and changing the + * text lines. The memfile functions are used to store the information in blocks + * of memory, backed up by a file. The structure of the information is a tree. + * The root of the tree is a pointer block. The leaves of the tree are data + * blocks. In between may be several layers of pointer blocks, forming branches. + * + * Three types of blocks are used: + * - Block nr 0 contains information for recovery + * - Pointer blocks contain list of pointers to other blocks. + * - Data blocks contain the actual text. + * + * Block nr 0 contains the block0 structure (see below). + * + * Block nr 1 is the first pointer block. It is the root of the tree. + * Other pointer blocks are branches. + * + * If a line is too big to fit in a single page, the block containing that + * line is made big enough to hold the line. It may span several pages. + * Otherwise all blocks are one page. + * + * A data block that was filled when starting to edit a file and was not + * changed since then, can have a negative block number. This means that it + * has not yet been assigned a place in the file. When recovering, the lines + * in this data block can be read from the original file. When the block is + * changed (lines appended/deleted/changed) or when it is flushed it gets a + * positive number. Use mf_trans_del() to get the new number, before calling + * mf_get(). + */ + +#if defined(MSDOS) || defined(WIN32) || defined(_WIN64) +# include <io.h> +#endif + +#include "vim.h" + +#ifdef HAVE_FCNTL_H +# include <fcntl.h> +#endif +#ifndef UNIX /* it's in os_unix.h for Unix */ +# include <time.h> +#endif + +#ifdef SASC +# include <proto/dos.h> /* for Open() and Close() */ +#endif + +typedef struct block0 ZERO_BL; /* contents of the first block */ +typedef struct pointer_block PTR_BL; /* contents of a pointer block */ +typedef struct data_block DATA_BL; /* contents of a data block */ +typedef struct pointer_entry PTR_EN; /* block/line-count pair */ + +#define DATA_ID (('d' << 8) + 'a') /* data block id */ +#define PTR_ID (('p' << 8) + 't') /* pointer block id */ +#define BLOCK0_ID0 'b' /* block 0 id 0 */ +#define BLOCK0_ID1 '0' /* block 0 id 1 */ + +/* + * pointer to a block, used in a pointer block + */ +struct pointer_entry +{ + blocknr_T pe_bnum; /* block number */ + linenr_T pe_line_count; /* number of lines in this branch */ + linenr_T pe_old_lnum; /* lnum for this block (for recovery) */ + int pe_page_count; /* number of pages in block pe_bnum */ +}; + +/* + * A pointer block contains a list of branches in the tree. + */ +struct pointer_block +{ + short_u pb_id; /* ID for pointer block: PTR_ID */ + short_u pb_count; /* number of pointer in this block */ + short_u pb_count_max; /* maximum value for pb_count */ + PTR_EN pb_pointer[1]; /* list of pointers to blocks (actually longer) + * followed by empty space until end of page */ +}; + +/* + * A data block is a leaf in the tree. + * + * The text of the lines is at the end of the block. The text of the first line + * in the block is put at the end, the text of the second line in front of it, + * etc. Thus the order of the lines is the opposite of the line number. + */ +struct data_block +{ + short_u db_id; /* ID for data block: DATA_ID */ + unsigned db_free; /* free space available */ + unsigned db_txt_start; /* byte where text starts */ + unsigned db_txt_end; /* byte just after data block */ + linenr_T db_line_count; /* number of lines in this block */ + unsigned db_index[1]; /* index for start of line (actually bigger) + * followed by empty space upto db_txt_start + * followed by the text in the lines until + * end of page */ +}; + +/* + * The low bits of db_index hold the actual index. The topmost bit is + * used for the global command to be able to mark a line. + * This method is not clean, but otherwise there would be at least one extra + * byte used for each line. + * The mark has to be in this place to keep it with the correct line when other + * lines are inserted or deleted. + */ +#define DB_MARKED ((unsigned)1 << ((sizeof(unsigned) * 8) - 1)) +#define DB_INDEX_MASK (~DB_MARKED) + +#define INDEX_SIZE (sizeof(unsigned)) /* size of one db_index entry */ +#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) /* size of data block header */ + +#define B0_FNAME_SIZE 900 +#define B0_UNAME_SIZE 40 +#define B0_HNAME_SIZE 40 +/* + * Restrict the numbers to 32 bits, otherwise most compilers will complain. + * This won't detect a 64 bit machine that only swaps a byte in the top 32 + * bits, but that is crazy anyway. + */ +#define B0_MAGIC_LONG 0x30313233L +#define B0_MAGIC_INT 0x20212223L +#define B0_MAGIC_SHORT 0x10111213L +#define B0_MAGIC_CHAR 0x55 + +/* + * Block zero holds all info about the swap file. + * + * NOTE: DEFINITION OF BLOCK 0 SHOULD NOT CHANGE! It would make all existing + * swap files unusable! + * + * If size of block0 changes anyway, adjust MIN_SWAP_PAGE_SIZE in vim.h!! + * + * This block is built up of single bytes, to make it portable accros + * different machines. b0_magic_* is used to check the byte order and size of + * variables, because the rest of the swap file is not portable. + */ +struct block0 +{ + char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1 */ + char_u b0_version[10]; /* Vim version string */ + char_u b0_page_size[4];/* number of bytes per page */ + char_u b0_mtime[4]; /* last modification time of file */ + char_u b0_ino[4]; /* inode of b0_fname */ + char_u b0_pid[4]; /* process id of creator (or 0) */ + char_u b0_uname[B0_UNAME_SIZE]; /* name of user (uid if no name) */ + char_u b0_hname[B0_HNAME_SIZE]; /* host name (if it has a name) */ + char_u b0_fname[B0_FNAME_SIZE]; /* name of file being edited */ + long b0_magic_long; /* check for byte order of long */ + int b0_magic_int; /* check for byte order of int */ + short b0_magic_short; /* check for byte order of short */ + char_u b0_magic_char; /* check for last char */ +}; +#define b0_dirty b0_fname[B0_FNAME_SIZE-1] + +#define STACK_INCR 5 /* nr of entries added to ml_stack at a time */ + +/* + * The line number where the first mark may be is remembered. + * If it is 0 there are no marks at all. + * (always used for the current buffer only, no buffer change possible while + * executing a global command). + */ +static linenr_T lowest_marked = 0; + +/* + * arguments for ml_find_line() + */ +#define ML_DELETE 0x11 /* delete line */ +#define ML_INSERT 0x12 /* insert line */ +#define ML_FIND 0x13 /* just find the line */ +#define ML_FLUSH 0x02 /* flush locked block */ +#define ML_SIMPLE(x) (x & 0x10) /* DEL, INS or FIND */ + +static void set_b0_fname __ARGS((ZERO_BL *, buf_T *buf)); +static time_t swapfile_info __ARGS((char_u *)); +static int recov_file_names __ARGS((char_u **, char_u *, int prepend_dot)); +static int ml_append_int __ARGS((buf_T *, linenr_T, char_u *, colnr_T, int, int)); +static int ml_delete_int __ARGS((buf_T *, linenr_T, int)); +static char_u *findswapname __ARGS((buf_T *, char_u **, char_u *)); +static void ml_flush_line __ARGS((buf_T *)); +static bhdr_T *ml_new_data __ARGS((memfile_T *, int, int)); +static bhdr_T *ml_new_ptr __ARGS((memfile_T *)); +static bhdr_T *ml_find_line __ARGS((buf_T *, linenr_T, int)); +static int ml_add_stack __ARGS((buf_T *)); +static char_u *makeswapname __ARGS((buf_T *, char_u *)); +static void ml_lineadd __ARGS((buf_T *, int)); +static int b0_magic_wrong __ARGS((ZERO_BL *)); +#ifdef CHECK_INODE +static int fnamecmp_ino __ARGS((char_u *, char_u *, long)); +#endif +static void long_to_char __ARGS((long, char_u *)); +static long char_to_long __ARGS((char_u *)); +#if defined(UNIX) || defined(WIN3264) +static char_u *make_percent_swname __ARGS((char_u *dir, char_u *name)); +#endif +#ifdef FEAT_BYTEOFF +static void ml_updatechunk __ARGS((buf_T *buf, long line, long len, int updtype)); +#endif + +/* + * open a new memline for 'curbuf' + * + * return FAIL for failure, OK otherwise + */ + int +ml_open() +{ + memfile_T *mfp; + bhdr_T *hp = NULL; + ZERO_BL *b0p; + PTR_BL *pp; + DATA_BL *dp; + +/* + * init fields in memline struct + */ + curbuf->b_ml.ml_stack_size = 0; /* no stack yet */ + curbuf->b_ml.ml_stack = NULL; /* no stack yet */ + curbuf->b_ml.ml_stack_top = 0; /* nothing in the stack */ + curbuf->b_ml.ml_locked = NULL; /* no cached block */ + curbuf->b_ml.ml_line_lnum = 0; /* no cached line */ +#ifdef FEAT_BYTEOFF + curbuf->b_ml.ml_chunksize = NULL; +#endif + +/* + * When 'updatecount' is non-zero, flag that a swap file may be opened later. + */ + if (p_uc && curbuf->b_p_swf) + curbuf->b_may_swap = TRUE; + else + curbuf->b_may_swap = FALSE; + +/* + * Open the memfile. No swap file is created yet. + */ + mfp = mf_open(NULL, 0); + if (mfp == NULL) + goto error; + + curbuf->b_ml.ml_mfp = mfp; + curbuf->b_ml.ml_flags = ML_EMPTY; + curbuf->b_ml.ml_line_count = 1; + +#if defined(MSDOS) && !defined(DJGPP) + /* for 16 bit MS-DOS create a swapfile now, because we run out of + * memory very quickly */ + if (p_uc != 0) + ml_open_file(curbuf); +#endif + +/* + * fill block0 struct and write page 0 + */ + if ((hp = mf_new(mfp, FALSE, 1)) == NULL) + goto error; + if (hp->bh_bnum != 0) + { + EMSG(_("E298: Didn't get block nr 0?")); + goto error; + } + b0p = (ZERO_BL *)(hp->bh_data); + + b0p->b0_id[0] = BLOCK0_ID0; + b0p->b0_id[1] = BLOCK0_ID1; + b0p->b0_dirty = curbuf->b_changed ? 0x55 : 0; + b0p->b0_magic_long = (long)B0_MAGIC_LONG; + b0p->b0_magic_int = (int)B0_MAGIC_INT; + b0p->b0_magic_short = (short)B0_MAGIC_SHORT; + b0p->b0_magic_char = B0_MAGIC_CHAR; + + STRNCPY(b0p->b0_version, "VIM ", 4); + STRNCPY(b0p->b0_version + 4, Version, 6); + set_b0_fname(b0p, curbuf); + long_to_char((long)mfp->mf_page_size, b0p->b0_page_size); + (void)get_user_name(b0p->b0_uname, B0_UNAME_SIZE); + b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL; + mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE); + b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL; + long_to_char(mch_get_pid(), b0p->b0_pid); + + /* + * Always sync block number 0 to disk, so we can check the file name in + * the swap file in findswapname(). Don't do this for help files though. + * Only works when there's a swapfile, otherwise it's done when the file + * is created. + */ + mf_put(mfp, hp, TRUE, FALSE); + if (!curbuf->b_help) + (void)mf_sync(mfp, 0); + +/* + * fill in root pointer block and write page 1 + */ + if ((hp = ml_new_ptr(mfp)) == NULL) + goto error; + if (hp->bh_bnum != 1) + { + EMSG(_("E298: Didn't get block nr 1?")); + goto error; + } + pp = (PTR_BL *)(hp->bh_data); + pp->pb_count = 1; + pp->pb_pointer[0].pe_bnum = 2; + pp->pb_pointer[0].pe_page_count = 1; + pp->pb_pointer[0].pe_old_lnum = 1; + pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */ + mf_put(mfp, hp, TRUE, FALSE); + +/* + * allocate first data block and create an empty line 1. + */ + if ((hp = ml_new_data(mfp, FALSE, 1)) == NULL) + goto error; + if (hp->bh_bnum != 2) + { + EMSG(_("E298: Didn't get block nr 2?")); + goto error; + } + + dp = (DATA_BL *)(hp->bh_data); + dp->db_index[0] = --dp->db_txt_start; /* at end of block */ + dp->db_free -= 1 + INDEX_SIZE; + dp->db_line_count = 1; + *((char_u *)dp + dp->db_txt_start) = NUL; /* emtpy line */ + + return OK; + +error: + if (mfp != NULL) + { + if (hp) + mf_put(mfp, hp, FALSE, FALSE); + mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */ + } + curbuf->b_ml.ml_mfp = NULL; + return FAIL; +} + +/* + * ml_setname() is called when the file name of "buf" has been changed. + * It may rename the swap file. + */ + void +ml_setname(buf) + buf_T *buf; +{ + int success = FALSE; + memfile_T *mfp; + char_u *fname; + char_u *dirp; +#if defined(MSDOS) || defined(MSWIN) + char_u *p; +#endif + + mfp = buf->b_ml.ml_mfp; + if (mfp->mf_fd < 0) /* there is no swap file yet */ + { + /* + * When 'updatecount' is 0 and 'noswapfile' there is no swap file. + * For help files we will make a swap file now. + */ + if (p_uc != 0) + ml_open_file(buf); /* create a swap file */ + return; + } + + /* + * Try all directories in the 'directory' option. + */ + dirp = p_dir; + for (;;) + { + if (*dirp == NUL) /* tried all directories, fail */ + break; + fname = findswapname(buf, &dirp, mfp->mf_fname); /* alloc's fname */ + if (fname == NULL) /* no file name found for this dir */ + continue; + +#if defined(MSDOS) || defined(MSWIN) + /* + * Set full pathname for swap file now, because a ":!cd dir" may + * change directory without us knowing it. + */ + p = FullName_save(fname, FALSE); + vim_free(fname); + fname = p; + if (fname == NULL) + continue; +#endif + /* if the file name is the same we don't have to do anything */ + if (fnamecmp(fname, mfp->mf_fname) == 0) + { + vim_free(fname); + success = TRUE; + break; + } + /* need to close the swap file before renaming */ + if (mfp->mf_fd >= 0) + { + close(mfp->mf_fd); + mfp->mf_fd = -1; + } + + /* try to rename the swap file */ + if (vim_rename(mfp->mf_fname, fname) == 0) + { + success = TRUE; + vim_free(mfp->mf_fname); + mfp->mf_fname = fname; + vim_free(mfp->mf_ffname); +#if defined(MSDOS) || defined(MSWIN) + mfp->mf_ffname = NULL; /* mf_fname is full pathname already */ +#else + mf_set_ffname(mfp); +#endif + break; + } + vim_free(fname); /* this fname didn't work, try another */ + } + + if (mfp->mf_fd == -1) /* need to (re)open the swap file */ + { + mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR | O_EXTRA, 0); + if (mfp->mf_fd < 0) + { + /* could not (re)open the swap file, what can we do???? */ + EMSG(_("E301: Oops, lost the swap file!!!")); + return; + } + } + if (!success) + EMSG(_("E302: Could not rename swap file")); +} + +/* + * Open a file for the memfile for all buffers that are not readonly or have + * been modified. + * Used when 'updatecount' changes from zero to non-zero. + */ + void +ml_open_files() +{ + buf_T *buf; + + for (buf = firstbuf; buf != NULL; buf = buf->b_next) + if (!buf->b_p_ro || buf->b_changed) + ml_open_file(buf); +} + +/* + * Open a swap file for an existing memfile, if there is no swap file yet. + * If we are unable to find a file name, mf_fname will be NULL + * and the memfile will be in memory only (no recovery possible). + */ + void +ml_open_file(buf) + buf_T *buf; +{ + memfile_T *mfp; + char_u *fname; + char_u *dirp; + + mfp = buf->b_ml.ml_mfp; + if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf) + return; /* nothing to do */ + + /* + * Try all directories in 'directory' option. + */ + dirp = p_dir; + for (;;) + { + if (*dirp == NUL) + break; + /* There is a small chance that between chosing the swap file name and + * creating it, another Vim creates the file. In that case the + * creation will fail and we will use another directory. */ + fname = findswapname(buf, &dirp, NULL); /* allocates fname */ + if (fname == NULL) + continue; + if (mf_open_file(mfp, fname) == OK) /* consumes fname! */ + { +#if defined(MSDOS) || defined(MSWIN) || defined(RISCOS) + /* + * set full pathname for swap file now, because a ":!cd dir" may + * change directory without us knowing it. + */ + mf_fullname(mfp); +#endif + /* Flush block zero, so others can read it */ + if (mf_sync(mfp, MFS_ZERO) == OK) + break; + /* Writing block 0 failed: close the file and try another dir */ + mf_close_file(buf, FALSE); + } + } + + if (mfp->mf_fname == NULL) /* Failed! */ + { + need_wait_return = TRUE; /* call wait_return later */ + ++no_wait_return; + (void)EMSG2(_("E303: Unable to open swap file for \"%s\", recovery impossible"), + buf_spname(buf) != NULL + ? (char_u *)buf_spname(buf) + : buf->b_fname); + --no_wait_return; + } + + /* don't try to open a swap file again */ + buf->b_may_swap = FALSE; +} + +/* + * If still need to create a swap file, and starting to edit a not-readonly + * file, or reading into an existing buffer, create a swap file now. + */ + void +check_need_swap(newfile) + int newfile; /* reading file into new buffer */ +{ + if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile)) + ml_open_file(curbuf); +} + +/* + * Close memline for buffer 'buf'. + * If 'del_file' is TRUE, delete the swap file + */ + void +ml_close(buf, del_file) + buf_T *buf; + int del_file; +{ + if (buf->b_ml.ml_mfp == NULL) /* not open */ + return; + mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */ + if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) + vim_free(buf->b_ml.ml_line_ptr); + vim_free(buf->b_ml.ml_stack); +#ifdef FEAT_BYTEOFF + vim_free(buf->b_ml.ml_chunksize); + buf->b_ml.ml_chunksize = NULL; +#endif + buf->b_ml.ml_mfp = NULL; + + /* Reset the "recovered" flag, give the ATTENTION prompt the next time + * this buffer is loaded. */ + buf->b_flags &= ~BF_RECOVERED; +} + +/* + * Close all existing memlines and memfiles. + * Only used when exiting. + * When 'del_file' is TRUE, delete the memfiles. + */ + void +ml_close_all(del_file) + int del_file; +{ + buf_T *buf; + + for (buf = firstbuf; buf != NULL; buf = buf->b_next) + ml_close(buf, del_file); +#ifdef TEMPDIRNAMES + vim_deltempdir(); /* delete created temp directory */ +#endif +} + +/* + * Close all memfiles for not modified buffers. + * Only use just before exiting! + */ + void +ml_close_notmod() +{ + buf_T *buf; + + for (buf = firstbuf; buf != NULL; buf = buf->b_next) + if (!bufIsChanged(buf)) + ml_close(buf, TRUE); /* close all not-modified buffers */ +} + +/* + * Update the timestamp in the .swp file. + * Used when the file has been written. + */ + void +ml_timestamp(buf) + buf_T *buf; +{ + memfile_T *mfp; + bhdr_T *hp; + ZERO_BL *b0p; + + mfp = buf->b_ml.ml_mfp; + + if (mfp == NULL || (hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) + return; + b0p = (ZERO_BL *)(hp->bh_data); + if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1) + EMSG(_("E304: ml_timestamp: Didn't get block 0??")); + else + set_b0_fname(b0p, buf); + mf_put(mfp, hp, TRUE, FALSE); +} + +/* + * Write file name and timestamp into block 0 of a swap file. + * Also set buf->b_mtime. + * Don't use NameBuff[]!!! + */ + static void +set_b0_fname(b0p, buf) + ZERO_BL *b0p; + buf_T *buf; +{ + struct stat st; + + if (buf->b_ffname == NULL) + b0p->b0_fname[0] = NUL; + else + { +#if defined(MSDOS) || defined(MSWIN) || defined(AMIGA) || defined(RISCOS) + /* systems that cannot translate "~user" back into a path: copy the + * file name unmodified */ + STRNCPY(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE); +#else + size_t flen, ulen; + char_u uname[B0_UNAME_SIZE]; + + /* + * For a file under the home directory of the current user, we try to + * replace the home directory path with "~user". This helps when + * editing the same file on different machines over a network. + * First replace home dir path with "~/" with home_replace(). + * Then insert the user name to get "~user/". + */ + home_replace(NULL, buf->b_ffname, b0p->b0_fname, B0_FNAME_SIZE, TRUE); + if (b0p->b0_fname[0] == '~') + { + flen = STRLEN(b0p->b0_fname); + /* If there is no user name or it is too long, don't use "~/" */ + if (get_user_name(uname, B0_UNAME_SIZE) == FAIL + || (ulen = STRLEN(uname)) + flen > B0_FNAME_SIZE - 1) + STRNCPY(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE); + else + { + mch_memmove(b0p->b0_fname + ulen + 1, b0p->b0_fname + 1, flen); + mch_memmove(b0p->b0_fname + 1, uname, ulen); + } + } +#endif + if (mch_stat((char *)buf->b_ffname, &st) >= 0) + { + long_to_char((long)st.st_mtime, b0p->b0_mtime); +#ifdef CHECK_INODE + long_to_char((long)st.st_ino, b0p->b0_ino); +#endif + buf_store_time(buf, &st, buf->b_ffname); + buf->b_mtime_read = buf->b_mtime; + } + else + { + long_to_char(0L, b0p->b0_mtime); +#ifdef CHECK_INODE + long_to_char(0L, b0p->b0_ino); +#endif + buf->b_mtime = 0; + buf->b_mtime_read = 0; + buf->b_orig_size = 0; + buf->b_orig_mode = 0; + } + } +} + +/* + * try to recover curbuf from the .swp file + */ + void +ml_recover() +{ + buf_T *buf = NULL; + memfile_T *mfp = NULL; + char_u *fname; + bhdr_T *hp = NULL; + ZERO_BL *b0p; + PTR_BL *pp; + DATA_BL *dp; + infoptr_T *ip; + blocknr_T bnum; + int page_count; + struct stat org_stat, swp_stat; + int len; + int directly; + linenr_T lnum; + char_u *p; + int i; + long error; + int cannot_open; + linenr_T line_count; + int has_error; + int idx; + int top; + int txt_start; + off_t size; + int called_from_main; + int serious_error = TRUE; + long mtime; + int attr; + + recoverymode = TRUE; + called_from_main = (curbuf->b_ml.ml_mfp == NULL); + attr = hl_attr(HLF_E); +/* + * If the file name ends in ".sw?" we use it directly. + * Otherwise a search is done to find the swap file(s). + */ + fname = curbuf->b_fname; + if (fname == NULL) /* When there is no file name */ + fname = (char_u *)""; + len = (int)STRLEN(fname); + if (len >= 4 && +#if defined(VMS) || defined(RISCOS) + STRNICMP(fname + len - 4, "_sw" , 3) +#else + STRNICMP(fname + len - 4, ".sw" , 3) +#endif + == 0) + { + directly = TRUE; + fname = vim_strsave(fname); /* make a copy for mf_open() */ + } + else + { + directly = FALSE; + + /* count the number of matching swap files */ + len = recover_names(&fname, FALSE, 0); + if (len == 0) /* no swap files found */ + { + EMSG2(_("E305: No swap file found for %s"), fname); + goto theend; + } + if (len == 1) /* one swap file found, use it */ + i = 1; + else /* several swap files found, choose */ + { + /* list the names of the swap files */ + (void)recover_names(&fname, TRUE, 0); + msg_putchar('\n'); + MSG_PUTS(_("Enter number of swap file to use (0 to quit): ")); + i = get_number(FALSE); + if (i < 1 || i > len) + goto theend; + } + /* get the swap file name that will be used */ + (void)recover_names(&fname, FALSE, i); + } + if (fname == NULL) + goto theend; /* out of memory */ + + /* When called from main() still need to initialize storage structure */ + if (called_from_main && ml_open() == FAIL) + getout(1); + +/* + * allocate a buffer structure (only the memline in it is really used) + */ + buf = (buf_T *)alloc((unsigned)sizeof(buf_T)); + if (buf == NULL) + { + vim_free(fname); + goto theend; + } + +/* + * init fields in memline struct + */ + buf->b_ml.ml_stack_size = 0; /* no stack yet */ + buf->b_ml.ml_stack = NULL; /* no stack yet */ + buf->b_ml.ml_stack_top = 0; /* nothing in the stack */ + buf->b_ml.ml_line_lnum = 0; /* no cached line */ + buf->b_ml.ml_locked = NULL; /* no locked block */ + buf->b_ml.ml_flags = 0; + +/* + * open the memfile from the old swap file + */ + p = vim_strsave(fname); /* save fname for the message + (mf_open() may free fname) */ + mfp = mf_open(fname, O_RDONLY); /* consumes fname! */ + if (mfp == NULL || mfp->mf_fd < 0) + { + if (p != NULL) + { + EMSG2(_("E306: Cannot open %s"), p); + vim_free(p); + } + goto theend; + } + vim_free(p); + buf->b_ml.ml_mfp = mfp; + + /* + * The page size set in mf_open() might be different from the page size + * used in the swap file, we must get it from block 0. But to read block + * 0 we need a page size. Use the minimal size for block 0 here, it will + * be set to the real value below. + */ + mfp->mf_page_size = MIN_SWAP_PAGE_SIZE; + +/* + * try to read block 0 + */ + if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) + { + msg_start(); + MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST); + msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); + MSG_PUTS_ATTR( + _("\nMaybe no changes were made or Vim did not update the swap file."), + attr | MSG_HIST); + msg_end(); + goto theend; + } + b0p = (ZERO_BL *)(hp->bh_data); + if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0) + { + msg_start(); + msg_outtrans_attr(mfp->mf_fname, MSG_HIST); + MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), + MSG_HIST); + MSG_PUTS_ATTR(_("Use Vim version 3.0.\n"), MSG_HIST); + msg_end(); + goto theend; + } + if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1) + { + EMSG2(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname); + goto theend; + } + if (b0_magic_wrong(b0p)) + { + msg_start(); + msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); +#if defined(MSDOS) || defined(MSWIN) + if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0) + MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), + attr | MSG_HIST); + else +#endif + MSG_PUTS_ATTR(_(" cannot be used on this computer.\n"), + attr | MSG_HIST); + MSG_PUTS_ATTR(_("The file was created on "), attr | MSG_HIST); + /* avoid going past the end of a currupted hostname */ + b0p->b0_fname[0] = NUL; + MSG_PUTS_ATTR(b0p->b0_hname, attr | MSG_HIST); + MSG_PUTS_ATTR(_(",\nor the file has been damaged."), attr | MSG_HIST); + msg_end(); + goto theend; + } + /* + * If we guessed the wrong page size, we have to recalculate the + * highest block number in the file. + */ + if (mfp->mf_page_size != (unsigned)char_to_long(b0p->b0_page_size)) + { + mf_new_page_size(mfp, (unsigned)char_to_long(b0p->b0_page_size)); + if ((size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0) + mfp->mf_blocknr_max = 0; /* no file or empty file */ + else + mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size); + mfp->mf_infile_count = mfp->mf_blocknr_max; + } + +/* + * If .swp file name given directly, use name from swap file for buffer. + */ + if (directly) + { + expand_env(b0p->b0_fname, NameBuff, MAXPATHL); + if (setfname(curbuf, NameBuff, NULL, TRUE) == FAIL) + goto theend; + } + + home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE); + msg_str((char_u *)_("Using swap file \"%s\""), NameBuff); + + if (buf_spname(curbuf) != NULL) + STRCPY(NameBuff, buf_spname(curbuf)); + else + home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE); + msg_str((char_u *)_("Original file \"%s\""), NameBuff); + msg_putchar('\n'); + +/* + * check date of swap file and original file + */ + mtime = char_to_long(b0p->b0_mtime); + if (curbuf->b_ffname != NULL + && mch_stat((char *)curbuf->b_ffname, &org_stat) != -1 + && ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1 + && org_stat.st_mtime > swp_stat.st_mtime) + || org_stat.st_mtime != mtime)) + { + EMSG(_("E308: Warning: Original file may have been changed")); + } + out_flush(); + mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */ + hp = NULL; + + /* + * Now that we are sure that the file is going to be recovered, clear the + * contents of the current buffer. + */ + while (!(curbuf->b_ml.ml_flags & ML_EMPTY)) + ml_delete((linenr_T)1, FALSE); + + /* + * Try reading the original file to obtain the values of 'fileformat', + * 'fileencoding', etc. Ignore errors. The text itself is not used. + */ + if (curbuf->b_ffname != NULL) + { + (void)readfile(curbuf->b_ffname, NULL, (linenr_T)0, + (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW); + while (!(curbuf->b_ml.ml_flags & ML_EMPTY)) + ml_delete((linenr_T)1, FALSE); + } + + bnum = 1; /* start with block 1 */ + page_count = 1; /* which is 1 page */ + lnum = 0; /* append after line 0 in curbuf */ + line_count = 0; + idx = 0; /* start with first index in block 1 */ + error = 0; + buf->b_ml.ml_stack_top = 0; + buf->b_ml.ml_stack = NULL; + buf->b_ml.ml_stack_size = 0; /* no stack yet */ + + if (curbuf->b_ffname == NULL) + cannot_open = TRUE; + else + cannot_open = FALSE; + + serious_error = FALSE; + for ( ; !got_int; line_breakcheck()) + { + if (hp != NULL) + mf_put(mfp, hp, FALSE, FALSE); /* release previous block */ + + /* + * get block + */ + if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL) + { + if (bnum == 1) + { + EMSG2(_("E309: Unable to read block 1 from %s"), mfp->mf_fname); + goto theend; + } + ++error; + ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"), + (colnr_T)0, TRUE); + } + else /* there is a block */ + { + pp = (PTR_BL *)(hp->bh_data); + if (pp->pb_id == PTR_ID) /* it is a pointer block */ + { + /* check line count when using pointer block first time */ + if (idx == 0 && line_count != 0) + { + for (i = 0; i < (int)pp->pb_count; ++i) + line_count -= pp->pb_pointer[i].pe_line_count; + if (line_count != 0) + { + ++error; + ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"), + (colnr_T)0, TRUE); + } + } + + if (pp->pb_count == 0) + { + ml_append(lnum++, (char_u *)_("???EMPTY BLOCK"), + (colnr_T)0, TRUE); + ++error; + } + else if (idx < (int)pp->pb_count) /* go a block deeper */ + { + if (pp->pb_pointer[idx].pe_bnum < 0) + { + /* + * Data block with negative block number. + * Try to read lines from the original file. + * This is slow, but it works. + */ + if (!cannot_open) + { + line_count = pp->pb_pointer[idx].pe_line_count; + if (readfile(curbuf->b_ffname, NULL, lnum, + pp->pb_pointer[idx].pe_old_lnum - 1, + line_count, NULL, 0) == FAIL) + cannot_open = TRUE; + else + lnum += line_count; + } + if (cannot_open) + { + ++error; + ml_append(lnum++, (char_u *)_("???LINES MISSING"), + (colnr_T)0, TRUE); + } + ++idx; /* get same block again for next index */ + continue; + } + + /* + * going one block deeper in the tree + */ + if ((top = ml_add_stack(buf)) < 0) /* new entry in stack */ + { + ++error; + break; /* out of memory */ + } + ip = &(buf->b_ml.ml_stack[top]); + ip->ip_bnum = bnum; + ip->ip_index = idx; + + bnum = pp->pb_pointer[idx].pe_bnum; + line_count = pp->pb_pointer[idx].pe_line_count; + page_count = pp->pb_pointer[idx].pe_page_count; + continue; + } + } + else /* not a pointer block */ + { + dp = (DATA_BL *)(hp->bh_data); + if (dp->db_id != DATA_ID) /* block id wrong */ + { + if (bnum == 1) + { + EMSG2(_("E310: Block 1 ID wrong (%s not a .swp file?)"), + mfp->mf_fname); + goto theend; + } + ++error; + ml_append(lnum++, (char_u *)_("???BLOCK MISSING"), + (colnr_T)0, TRUE); + } + else + { + /* + * it is a data block + * Append all the lines in this block + */ + has_error = FALSE; + /* + * check length of block + * if wrong, use length in pointer block + */ + if (page_count * mfp->mf_page_size != dp->db_txt_end) + { + ml_append(lnum++, (char_u *)_("??? from here until ???END lines may be messed up"), + (colnr_T)0, TRUE); + ++error; + has_error = TRUE; + dp->db_txt_end = page_count * mfp->mf_page_size; + } + + /* make sure there is a NUL at the end of the block */ + *((char_u *)dp + dp->db_txt_end - 1) = NUL; + + /* + * check number of lines in block + * if wrong, use count in data block + */ + if (line_count != dp->db_line_count) + { + ml_append(lnum++, (char_u *)_("??? from here until ???END lines may have been inserted/deleted"), + (colnr_T)0, TRUE); + ++error; + has_error = TRUE; + } + + for (i = 0; i < dp->db_line_count; ++i) + { + txt_start = (dp->db_index[i] & DB_INDEX_MASK); + if (txt_start <= HEADER_SIZE + || txt_start >= (int)dp->db_txt_end) + { + p = (char_u *)"???"; + ++error; + } + else + p = (char_u *)dp + txt_start; + ml_append(lnum++, p, (colnr_T)0, TRUE); + } + if (has_error) + ml_append(lnum++, (char_u *)_("???END"), (colnr_T)0, TRUE); + } + } + } + + if (buf->b_ml.ml_stack_top == 0) /* finished */ + break; + + /* + * go one block up in the tree + */ + ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]); + bnum = ip->ip_bnum; + idx = ip->ip_index + 1; /* go to next index */ + page_count = 1; + } + + /* + * The dummy line from the empty buffer will now be after the last line in + * the buffer. Delete it. + */ + ml_delete(curbuf->b_ml.ml_line_count, FALSE); + curbuf->b_flags |= BF_RECOVERED; + + recoverymode = FALSE; + if (got_int) + EMSG(_("E311: Recovery Interrupted")); + else if (error) + { + ++no_wait_return; + MSG(">>>>>>>>>>>>>"); + EMSG(_("E312: Errors detected while recovering; look for lines starting with ???")); + --no_wait_return; + MSG(_("See \":help E312 |