2005-09-20 21:26:39 +08:00
/** \file history.c
2012-11-18 18:23:22 +08:00
History functions , part of the user interface .
2005-09-20 21:26:39 +08:00
*/
2006-08-11 09:18:35 +08:00
# include "config.h"
2005-09-20 21:26:39 +08:00
# include <stdlib.h>
# include <stdio.h>
# include <wchar.h>
# include <errno.h>
# include <dirent.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
2006-10-21 06:36:49 +08:00
# include <sys/mman.h>
# include <fcntl.h>
# include <string.h>
# include <time.h>
# include <assert.h>
2005-09-20 21:26:39 +08:00
2006-02-28 21:17:16 +08:00
# include "fallback.h"
2005-09-20 21:26:39 +08:00
# include "util.h"
2012-02-07 02:52:13 +08:00
# include "sanity.h"
2012-02-16 03:33:41 +08:00
# include "tokenizer.h"
2013-04-06 15:28:55 +08:00
# include "reader.h"
2014-03-29 14:22:03 +08:00
# include "parse_tree.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include "wutil.h"
# include "history.h"
# include "common.h"
2006-10-21 06:36:49 +08:00
# include "intern.h"
2006-10-19 19:50:23 +08:00
# include "path.h"
2006-10-21 06:36:49 +08:00
# include "signal.h"
2012-02-06 12:54:41 +08:00
# include "autoload.h"
2012-02-16 03:33:41 +08:00
# include "iothread.h"
2012-02-06 08:42:24 +08:00
# include <map>
2012-02-06 12:54:41 +08:00
# include <algorithm>
2012-02-16 16:24:27 +08:00
/*
Our history format is intended to be valid YAML . Here it is :
- cmd : ssh blah blah blah
when : 2348237
paths :
- / path / to / something
- / path / to / something_else
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
Newlines are replaced by \ n . Backslashes are replaced by \ \ .
*/
2012-02-06 12:54:41 +08:00
/** When we rewrite the history, the number of items we keep */
# define HISTORY_SAVE_MAX (1024 * 256)
2012-03-02 06:56:34 +08:00
/** Whether we print timing information */
2012-12-03 16:14:39 +08:00
# define LOG_TIMES 0
2012-03-02 06:56:34 +08:00
2013-04-28 06:21:14 +08:00
/** Default buffer size for flushing to the history file */
# define HISTORY_OUTPUT_BUFFER_SIZE (4096 * 4)
/* Helper class for certain output. This is basically a string that allows us to ensure we only flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to implement your own streambuf? Total insanity. */
class history_output_buffer_t
{
/* A null-terminated C string */
std : : vector < char > buffer ;
/* Offset is the offset of the null terminator */
size_t offset ;
static size_t safe_strlen ( const char * s )
{
return s ? strlen ( s ) : 0 ;
}
2013-05-05 17:33:17 +08:00
public :
2013-04-28 06:21:14 +08:00
/* Add a bit more to HISTORY_OUTPUT_BUFFER_SIZE because we flush once we've exceeded that size */
history_output_buffer_t ( ) : buffer ( HISTORY_OUTPUT_BUFFER_SIZE + 128 , ' \0 ' ) , offset ( 0 )
{
}
/* Append one or more strings */
void append ( const char * s1 , const char * s2 = NULL , const char * s3 = NULL )
{
const char * ptrs [ 4 ] = { s1 , s2 , s3 , NULL } ;
const size_t lengths [ 4 ] = { safe_strlen ( s1 ) , safe_strlen ( s2 ) , safe_strlen ( s3 ) , 0 } ;
/* Determine the additional size we'll need */
size_t additional_length = 0 ;
for ( size_t i = 0 ; i < sizeof lengths / sizeof * lengths ; i + + )
{
additional_length + = lengths [ i ] ;
}
/* Allocate that much, plus a null terminator */
size_t required_size = offset + additional_length + 1 ;
if ( required_size > buffer . size ( ) )
{
buffer . resize ( required_size , ' \0 ' ) ;
}
/* Copy */
for ( size_t i = 0 ; ptrs [ i ] ! = NULL ; i + + )
{
memmove ( & buffer . at ( offset ) , ptrs [ i ] , lengths [ i ] ) ;
offset + = lengths [ i ] ;
}
/* Null terminator was appended by virtue of the resize() above (or in a previous invocation). */
assert ( buffer . at ( buffer . size ( ) - 1 ) = = ' \0 ' ) ;
}
/* Output to a given fd, resetting our buffer. Returns true on success, false on error */
bool flush_to_fd ( int fd )
{
bool result = write_loop ( fd , & buffer . at ( 0 ) , offset ) > = 0 ;
offset = 0 ;
return result ;
}
/* Return how much data we've accumulated */
size_t output_size ( ) const
{
return offset ;
}
} ;
2012-11-19 08:30:30 +08:00
class time_profiler_t
{
2012-03-02 06:56:34 +08:00
const char * what ;
double start ;
2012-11-19 08:30:30 +08:00
public :
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
time_profiler_t ( const char * w )
{
if ( LOG_TIMES )
{
2012-03-02 06:56:34 +08:00
what = w ;
start = timef ( ) ;
}
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
~ time_profiler_t ( )
{
if ( LOG_TIMES )
{
2012-03-02 06:56:34 +08:00
double end = timef ( ) ;
2012-12-03 16:14:09 +08:00
fprintf ( stderr , " (LOG_TIMES %s: %02f msec) \n " , what , ( end - start ) * 1000 ) ;
2012-03-02 06:56:34 +08:00
}
}
} ;
2012-12-03 08:39:35 +08:00
/* Lock a file via fcntl; returns true on success, false on failure. */
static bool history_file_lock ( int fd , short type )
{
assert ( type = = F_RDLCK | | type = = F_WRLCK ) ;
struct flock flk = { } ;
flk . l_type = type ;
flk . l_whence = SEEK_SET ;
int ret = fcntl ( fd , F_SETLKW , ( void * ) & flk ) ;
return ret ! = - 1 ;
}
2012-02-06 12:54:41 +08:00
/* Our LRU cache is used for restricting the amount of history we have, and limiting how long we order it. */
2012-11-19 08:30:30 +08:00
class history_lru_node_t : public lru_node_t
{
public :
2012-02-06 12:54:41 +08:00
time_t timestamp ;
2012-02-16 16:24:27 +08:00
path_list_t required_paths ;
history_lru_node_t ( const history_item_t & item ) :
lru_node_t ( item . str ( ) ) ,
timestamp ( item . timestamp ( ) ) ,
required_paths ( item . required_paths )
{ }
2012-02-06 12:54:41 +08:00
} ;
2012-11-19 08:30:30 +08:00
class history_lru_cache_t : public lru_cache_t < history_lru_node_t >
{
protected :
2012-11-18 18:23:22 +08:00
2012-02-06 12:54:41 +08:00
/* Override to delete evicted nodes */
2012-11-19 08:30:30 +08:00
virtual void node_was_evicted ( history_lru_node_t * node )
{
2012-02-06 12:54:41 +08:00
delete node ;
}
2012-11-19 08:30:30 +08:00
public :
2012-02-06 12:54:41 +08:00
history_lru_cache_t ( size_t max ) : lru_cache_t < history_lru_node_t > ( max ) { }
2012-11-18 18:23:22 +08:00
2012-02-06 12:54:41 +08:00
/* Function to add a history item */
2012-11-19 08:30:30 +08:00
void add_item ( const history_item_t & item )
{
2012-02-06 12:54:41 +08:00
/* Skip empty items */
if ( item . empty ( ) )
return ;
2012-11-18 18:23:22 +08:00
2012-02-06 12:54:41 +08:00
/* See if it's in the cache. If it is, update the timestamp. If not, we create a new node and add it. Note that calling get_node promotes the node to the front. */
history_lru_node_t * node = this - > get_node ( item . str ( ) ) ;
2012-11-19 08:30:30 +08:00
if ( node ! = NULL )
{
2012-02-06 12:54:41 +08:00
node - > timestamp = std : : max ( node - > timestamp , item . timestamp ( ) ) ;
2012-02-16 16:24:27 +08:00
/* What to do about paths here? Let's just ignore them */
2012-11-19 08:30:30 +08:00
}
else
{
2012-02-06 12:54:41 +08:00
node = new history_lru_node_t ( item ) ;
this - > add_node ( node ) ;
}
}
} ;
2005-09-20 21:26:39 +08:00
2012-02-06 08:42:24 +08:00
static pthread_mutex_t hist_lock = PTHREAD_MUTEX_INITIALIZER ;
static std : : map < wcstring , history_t * > histories ;
static wcstring history_filename ( const wcstring & name , const wcstring & suffix ) ;
2012-02-16 16:24:27 +08:00
/** Replaces newlines with a literal backslash followed by an n, and replaces backslashes with two backslashes. */
static void escape_yaml ( std : : string & str ) ;
2012-02-06 08:42:24 +08:00
2012-02-16 16:24:27 +08:00
/** Undoes escape_yaml */
static void unescape_yaml ( std : : string & str ) ;
2012-02-06 12:54:41 +08:00
2014-03-29 14:22:03 +08:00
/* We can merge two items if they are the same command. We use the more recent timestamp, more recent identifier, and the longer list of required paths. */
2012-03-02 06:56:34 +08:00
bool history_item_t : : merge ( const history_item_t & item )
{
bool result = false ;
2012-11-19 08:30:30 +08:00
if ( this - > contents = = item . contents )
{
2012-03-02 06:56:34 +08:00
this - > creation_timestamp = std : : max ( this - > creation_timestamp , item . creation_timestamp ) ;
2012-11-19 08:30:30 +08:00
if ( this - > required_paths . size ( ) < item . required_paths . size ( ) )
{
2012-03-02 06:56:34 +08:00
this - > required_paths = item . required_paths ;
}
2014-03-29 14:22:03 +08:00
if ( this - > identifier < item . identifier )
{
this - > identifier = item . identifier ;
}
2012-03-02 06:56:34 +08:00
result = true ;
}
return result ;
}
2014-03-29 14:22:03 +08:00
history_item_t : : history_item_t ( const wcstring & str ) : contents ( str ) , creation_timestamp ( time ( NULL ) ) , identifier ( 0 )
2012-02-06 08:42:24 +08:00
{
}
2014-03-29 14:22:03 +08:00
history_item_t : : history_item_t ( const wcstring & str , time_t when , history_identifier_t ident ) : contents ( str ) , creation_timestamp ( when ) , identifier ( ident )
2012-02-06 08:42:24 +08:00
{
}
2012-11-19 08:30:30 +08:00
bool history_item_t : : matches_search ( const wcstring & term , enum history_search_type_t type ) const
{
switch ( type )
{
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case HISTORY_SEARCH_TYPE_CONTAINS :
/* We consider equal strings to NOT match a contains search (so that you don't have to see history equal to what you typed). The length check ensures that. */
return contents . size ( ) > term . size ( ) & & contents . find ( term ) ! = wcstring : : npos ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case HISTORY_SEARCH_TYPE_PREFIX :
/* We consider equal strings to match a prefix search, so that autosuggest will allow suggesting what you've typed */
return string_prefixes_string ( term , contents ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
default :
sanity_lose ( ) ;
return false ;
2012-02-07 02:52:13 +08:00
}
}
2013-04-28 06:21:14 +08:00
/* Append our YAML history format to the provided vector at the given offset, updating the offset */
static void append_yaml_to_buffer ( const wcstring & wcmd , time_t timestamp , const path_list_t & required_paths , history_output_buffer_t * buffer )
2012-11-19 08:30:30 +08:00
{
2012-12-03 08:39:35 +08:00
std : : string cmd = wcs2string ( wcmd ) ;
2012-02-16 16:24:27 +08:00
escape_yaml ( cmd ) ;
2013-04-28 06:21:14 +08:00
buffer - > append ( " - cmd: " , cmd . c_str ( ) , " \n " ) ;
2012-11-18 18:23:22 +08:00
2013-04-28 06:21:14 +08:00
char timestamp_str [ 96 ] ;
2013-11-20 01:36:20 +08:00
snprintf ( timestamp_str , sizeof timestamp_str , " %ld " , ( long ) timestamp ) ;
2013-04-28 06:21:14 +08:00
buffer - > append ( " when: " , timestamp_str , " \n " ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( ! required_paths . empty ( ) )
{
2013-04-28 06:21:14 +08:00
buffer - > append ( " paths: \n " ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
for ( path_list_t : : const_iterator iter = required_paths . begin ( ) ; iter ! = required_paths . end ( ) ; + + iter )
{
2012-02-16 16:24:27 +08:00
std : : string path = wcs2string ( * iter ) ;
escape_yaml ( path ) ;
2013-04-28 06:21:14 +08:00
buffer - > append ( " - " , path . c_str ( ) , " \n " ) ;
2012-02-16 16:24:27 +08:00
}
}
2012-02-06 12:54:41 +08:00
}
2012-02-06 08:42:24 +08:00
2012-04-16 13:49:27 +08:00
// Parse a timestamp line that looks like this: spaces, "when:", spaces, timestamp, newline
// The string is NOT null terminated; however we do know it contains a newline, so stop when we reach it
2012-11-19 08:30:30 +08:00
static bool parse_timestamp ( const char * str , time_t * out_when )
{
2012-04-16 13:49:27 +08:00
const char * cursor = str ;
/* Advance past spaces */
while ( * cursor = = ' ' )
cursor + + ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Look for "when:" */
size_t when_len = 5 ;
if ( strncmp ( cursor , " when: " , when_len ) ! = 0 )
return false ;
cursor + = when_len ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Advance past spaces */
while ( * cursor = = ' ' )
cursor + + ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Try to parse a timestamp. */
long timestamp = 0 ;
2012-11-19 08:30:30 +08:00
if ( isdigit ( * cursor ) & & ( timestamp = strtol ( cursor , NULL , 0 ) ) > 0 )
{
2012-04-16 13:49:27 +08:00
* out_when = ( time_t ) timestamp ;
return true ;
}
return false ;
}
2012-04-17 11:26:50 +08:00
// Returns a pointer to the start of the next line, or NULL
// The next line must itself end with a newline
// Note that the string is not null terminated
2012-11-19 08:30:30 +08:00
static const char * next_line ( const char * start , size_t length )
{
2012-04-17 11:26:50 +08:00
/* Handle the hopeless case */
if ( length < 1 )
return NULL ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Get a pointer to the end, that we must not pass */
const char * const end = start + length ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Skip past the next newline */
const char * nextline = ( const char * ) memchr ( start , ' \n ' , length ) ;
2012-11-19 08:30:30 +08:00
if ( ! nextline | | nextline > = end )
{
2012-04-17 11:26:50 +08:00
return NULL ;
}
/* Skip past the newline character itself */
2012-11-19 08:30:30 +08:00
if ( + + nextline > = end )
{
2012-04-17 11:26:50 +08:00
return NULL ;
}
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Make sure this new line is itself "newline terminated". If it's not, return NULL; */
const char * next_newline = ( const char * ) memchr ( nextline , ' \n ' , end - nextline ) ;
2012-11-19 08:30:30 +08:00
if ( ! next_newline )
{
2012-04-17 11:26:50 +08:00
return NULL ;
}
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Done */
return nextline ;
}
2012-04-16 13:49:27 +08:00
// Support for iteratively locating the offsets of history items
// Pass the address and length of a mapped region.
// Pass a pointer to a cursor size_t, initially 0
// If custoff_timestamp is nonzero, skip items created at or after that timestamp
// Returns (size_t)(-1) when done
2012-06-16 07:22:37 +08:00
static size_t offset_of_next_item_fish_2_0 ( const char * begin , size_t mmap_length , size_t * inout_cursor , time_t cutoff_timestamp )
2012-04-16 13:49:27 +08:00
{
size_t cursor = * inout_cursor ;
size_t result = ( size_t ) ( - 1 ) ;
2012-11-19 08:30:30 +08:00
while ( cursor < mmap_length )
{
2012-04-16 13:49:27 +08:00
const char * const line_start = begin + cursor ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Advance the cursor to the next line */
2012-04-16 13:49:27 +08:00
const char * newline = ( const char * ) memchr ( line_start , ' \n ' , mmap_length - cursor ) ;
if ( newline = = NULL )
break ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Advance the cursor past this line. +1 is for the newline */
size_t line_len = newline - line_start ;
cursor + = line_len + 1 ;
/* Skip lines with a leading space, since these are in the interior of one of our items */
if ( line_start [ 0 ] = = ' ' )
continue ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Skip very short lines to make one of the checks below easier */
if ( line_len < 3 )
continue ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Try to be a little YAML compatible. Skip lines with leading %, ---, or ... */
if ( ! memcmp ( line_start , " % " , 1 ) | |
2012-11-19 08:30:30 +08:00
! memcmp ( line_start , " --- " , 3 ) | |
! memcmp ( line_start , " ... " , 3 ) )
2012-04-16 13:49:27 +08:00
continue ;
2012-04-17 11:26:50 +08:00
/* At this point, we know line_start is at the beginning of an item. But maybe we want to skip this item because of timestamps. A 0 cutoff means we don't care; if we do care, then try parsing out a timestamp. */
2012-11-19 08:30:30 +08:00
if ( cutoff_timestamp ! = 0 )
{
2012-04-16 13:49:27 +08:00
/* Hackish fast way to skip items created after our timestamp. This is the mechanism by which we avoid "seeing" commands from other sessions that started after we started. We try hard to ensure that our items are sorted by their timestamps, so in theory we could just break, but I don't think that works well if (for example) the clock changes. So we'll read all subsequent items.
*/
2012-04-17 11:26:50 +08:00
const char * const end = begin + mmap_length ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Walk over lines that we think are interior. These lines are not null terminated, but are guaranteed to contain a newline. */
bool has_timestamp = false ;
2012-04-16 13:49:27 +08:00
time_t timestamp ;
2012-04-17 11:26:50 +08:00
const char * interior_line ;
2013-11-25 23:07:17 +08:00
/*
* Ensure the loop is processed at least once . Otherwise ,
* timestamp is unitialized .
*/
bool processed_once = false ;
2012-04-17 11:26:50 +08:00
for ( interior_line = next_line ( line_start , end - line_start ) ;
2012-11-19 08:30:30 +08:00
interior_line ! = NULL & & ! has_timestamp ;
interior_line = next_line ( interior_line , end - interior_line ) )
{
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* If the first character is not a space, it's not an interior line, so we're done */
if ( interior_line [ 0 ] ! = ' ' )
break ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Hackish optimization: since we just stepped over some interior line, update the cursor so we don't have to look at these lines next time */
cursor = interior_line - begin ;
2012-11-18 18:23:22 +08:00
2012-04-17 11:26:50 +08:00
/* Try parsing a timestamp from this line. If we succeed, the loop will break. */
has_timestamp = parse_timestamp ( interior_line , & timestamp ) ;
2013-11-25 23:07:17 +08:00
processed_once = true ;
2012-04-17 11:26:50 +08:00
}
2012-11-18 18:23:22 +08:00
2013-11-25 23:07:17 +08:00
assert ( processed_once ) ;
2012-12-03 16:25:08 +08:00
/* Skip this item if the timestamp is past our cutoff. */
if ( has_timestamp & & timestamp > cutoff_timestamp )
2012-11-19 08:30:30 +08:00
{
2012-04-16 13:49:27 +08:00
continue ;
}
2012-04-17 11:26:50 +08:00
}
2012-04-16 13:49:27 +08:00
/* We made it through the gauntlet. */
result = line_start - begin ;
break ;
}
* inout_cursor = cursor ;
return result ;
}
2012-06-16 07:22:37 +08:00
// Same as offset_of_next_item_fish_2_0, but for fish 1.x (pre fishfish)
// Adapted from history_populate_from_mmap in history.c
2012-11-19 08:30:30 +08:00
static size_t offset_of_next_item_fish_1_x ( const char * begin , size_t mmap_length , size_t * inout_cursor , time_t cutoff_timestamp )
{
2012-06-16 07:22:37 +08:00
if ( mmap_length = = 0 | | * inout_cursor > = mmap_length )
return ( size_t ) ( - 1 ) ;
2012-11-19 08:30:30 +08:00
const char * end = begin + mmap_length ;
const char * pos ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
bool ignore_newline = false ;
bool do_push = true ;
2012-06-16 07:22:37 +08:00
bool all_done = false ;
size_t result = * inout_cursor ;
2012-11-19 08:30:30 +08:00
for ( pos = begin + * inout_cursor ; pos < end & & ! all_done ; pos + + )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
if ( do_push )
{
ignore_newline = ( * pos = = ' # ' ) ;
do_push = false ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
switch ( * pos )
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
case ' \\ ' :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
pos + + ;
break ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
case ' \n ' :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( ignore_newline )
{
ignore_newline = false ;
}
else
{
/* Note: pos will be left pointing just after this newline, because of the ++ in the loop */
all_done = true ;
}
break ;
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
}
}
2012-06-16 07:22:37 +08:00
* inout_cursor = ( pos - begin ) ;
return result ;
}
// Returns the offset of the next item based on the given history type, or -1
2012-11-19 08:30:30 +08:00
static size_t offset_of_next_item ( const char * begin , size_t mmap_length , history_file_type_t mmap_type , size_t * inout_cursor , time_t cutoff_timestamp )
{
2012-06-16 07:22:37 +08:00
size_t result ;
2012-11-19 08:30:30 +08:00
switch ( mmap_type )
{
2012-11-19 16:31:03 +08:00
case history_type_fish_2_0 :
result = offset_of_next_item_fish_2_0 ( begin , mmap_length , inout_cursor , cutoff_timestamp ) ;
break ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case history_type_fish_1_x :
result = offset_of_next_item_fish_1_x ( begin , mmap_length , inout_cursor , cutoff_timestamp ) ;
break ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
default :
case history_type_unknown :
// Oh well
result = ( size_t ) ( - 1 ) ;
break ;
2012-06-16 07:22:37 +08:00
}
return result ;
}
2012-11-19 08:30:30 +08:00
history_t & history_t : : history_with_name ( const wcstring & name )
{
2012-02-06 08:42:24 +08:00
/* Note that histories are currently never deleted, so we can return a reference to them without using something like shared_ptr */
scoped_lock locker ( hist_lock ) ;
history_t * & current = histories [ name ] ;
if ( current = = NULL )
current = new history_t ( name ) ;
return * current ;
}
history_t : : history_t ( const wcstring & pname ) :
name ( pname ) ,
2012-12-03 08:39:35 +08:00
first_unwritten_new_item_index ( 0 ) ,
2014-03-29 14:22:03 +08:00
disable_automatic_save_counter ( 0 ) ,
2012-02-06 08:42:24 +08:00
mmap_start ( NULL ) ,
mmap_length ( 0 ) ,
2012-12-12 05:18:40 +08:00
mmap_file_id ( kInvalidFileID ) ,
2012-03-02 06:56:34 +08:00
birth_timestamp ( time ( NULL ) ) ,
2012-12-03 16:21:58 +08:00
countdown_to_vacuum ( - 1 ) ,
2012-12-03 15:38:38 +08:00
loaded_old ( false ) ,
chaos_mode ( false )
2012-02-06 08:42:24 +08:00
{
pthread_mutex_init ( & lock , NULL ) ;
}
history_t : : ~ history_t ( )
{
pthread_mutex_destroy ( & lock ) ;
}
2012-02-16 16:24:27 +08:00
void history_t : : add ( const history_item_t & item )
2012-02-06 08:42:24 +08:00
{
scoped_lock locker ( lock ) ;
2012-11-18 18:23:22 +08:00
2012-03-02 06:56:34 +08:00
/* Try merging with the last item */
2012-11-19 08:30:30 +08:00
if ( ! new_items . empty ( ) & & new_items . back ( ) . merge ( item ) )
{
2012-03-02 06:56:34 +08:00
/* We merged, so we don't have to add anything */
}
else
{
/* We have to add a new item */
new_items . push_back ( item ) ;
2014-03-29 14:22:03 +08:00
save_internal_unless_disabled ( ) ;
2012-03-02 06:56:34 +08:00
}
2014-03-29 14:22:03 +08:00
}
2012-12-03 18:25:08 +08:00
2014-03-29 14:22:03 +08:00
void history_t : : save_internal_unless_disabled ( )
{
/* This must be called while locked */
ASSERT_IS_LOCKED ( lock ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Respect disable_automatic_save_counter */
if ( disable_automatic_save_counter > 0 )
{
return ;
}
2014-04-01 01:01:39 +08:00
2012-12-03 16:14:09 +08:00
/* We may or may not vacuum. We try to vacuum every kVacuumFrequency items, but start the countdown at a random number so that even if the user never runs more than 25 commands, we'll eventually vacuum. If countdown_to_vacuum is -1, it means we haven't yet picked a value for the counter. */
const int kVacuumFrequency = 25 ;
if ( countdown_to_vacuum < 0 )
2012-11-19 08:30:30 +08:00
{
2012-12-03 16:14:09 +08:00
static unsigned int seed = ( unsigned int ) time ( NULL ) ;
/* Generate a number in the range [0, kVacuumFrequency) */
countdown_to_vacuum = rand_r ( & seed ) / ( RAND_MAX / kVacuumFrequency + 1 ) ;
2012-03-02 06:56:34 +08:00
}
2014-04-01 01:01:39 +08:00
2012-12-03 16:14:09 +08:00
/* Determine if we're going to vacuum */
bool vacuum = false ;
if ( countdown_to_vacuum = = 0 )
{
countdown_to_vacuum = kVacuumFrequency ;
vacuum = true ;
}
2014-04-01 01:01:39 +08:00
2012-12-03 16:14:09 +08:00
/* This might be a good candidate for moving to a background thread */
time_profiler_t profiler ( vacuum ? " save_internal vacuum " : " save_internal no vacuum " ) ;
this - > save_internal ( vacuum ) ;
2014-04-01 01:01:39 +08:00
2012-12-03 16:14:09 +08:00
/* Update our countdown */
assert ( countdown_to_vacuum > 0 ) ;
countdown_to_vacuum - - ;
2012-02-16 16:24:27 +08:00
}
2014-03-29 14:22:03 +08:00
void history_t : : add ( const wcstring & str , history_identifier_t ident )
2012-02-16 16:24:27 +08:00
{
2012-12-03 17:53:52 +08:00
time_t when = time ( NULL ) ;
/* Big hack: do not allow timestamps equal to our birthdate. This is because we include items whose timestamps are equal to our birthdate when reading old history, so we can catch "just closed" items. But this means that we may interpret our own items, that we just wrote, as old items, if we wrote them in the same second as our birthdate.
*/
if ( when = = this - > birth_timestamp )
when + + ;
2014-03-29 14:22:03 +08:00
this - > add ( history_item_t ( str , when , ident ) ) ;
2012-02-06 08:42:24 +08:00
}
2012-06-05 12:24:42 +08:00
void history_t : : remove ( const wcstring & str )
{
2012-06-16 07:22:37 +08:00
/* Add to our list of deleted items */
deleted_items . insert ( str ) ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
/* Remove from our list of new items */
2012-12-03 08:39:35 +08:00
size_t idx = new_items . size ( ) ;
while ( idx - - )
2012-06-05 12:24:42 +08:00
{
2014-03-29 14:22:03 +08:00
if ( new_items . at ( idx ) . str ( ) = = str )
2012-11-19 08:30:30 +08:00
{
2012-12-03 08:39:35 +08:00
new_items . erase ( new_items . begin ( ) + idx ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
/* If this index is before our first_unwritten_new_item_index, then subtract one from that index so it stays pointing at the same item. If it is equal to or larger, then we have not yet writen this item, so we don't have to adjust the index. */
if ( idx < first_unwritten_new_item_index )
{
first_unwritten_new_item_index - - ;
}
2012-06-05 12:24:42 +08:00
}
}
2012-12-03 08:39:35 +08:00
assert ( first_unwritten_new_item_index < = new_items . size ( ) ) ;
2012-06-05 12:24:42 +08:00
}
2014-03-29 14:22:03 +08:00
void history_t : : set_valid_file_paths ( const wcstring_list_t & valid_file_paths , history_identifier_t ident )
{
/* 0 identifier is used to mean "not necessary" */
if ( ident = = 0 )
{
return ;
}
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
scoped_lock locker ( lock ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Look for an item with the given identifier. It is likely to be at the end of new_items */
for ( history_item_list_t : : reverse_iterator iter = new_items . rbegin ( ) ; iter ! = new_items . rend ( ) ; iter + + )
{
if ( iter - > identifier = = ident )
{
/* Found it */
iter - > required_paths = valid_file_paths ;
break ;
}
}
}
2012-03-20 02:52:18 +08:00
void history_t : : get_string_representation ( wcstring & result , const wcstring & separator )
{
scoped_lock locker ( lock ) ;
2012-11-18 18:23:22 +08:00
2012-03-20 02:52:18 +08:00
bool first = true ;
2013-10-27 06:27:39 +08:00
2013-09-06 04:49:18 +08:00
std : : set < wcstring > seen ;
2012-11-18 18:23:22 +08:00
2012-12-12 05:18:40 +08:00
/* Append new items. Note that in principle we could use const_reverse_iterator, but we do not because reverse_iterator is not convertible to const_reverse_iterator ( http://github.com/fish-shell/fish-shell/issues/431 ) */
2014-03-29 14:22:03 +08:00
for ( history_item_list_t : : reverse_iterator iter = new_items . rbegin ( ) ; iter < new_items . rend ( ) ; + + iter )
2012-11-19 08:30:30 +08:00
{
2013-09-06 04:49:18 +08:00
/* Skip duplicates */
if ( ! seen . insert ( iter - > str ( ) ) . second )
continue ;
2013-10-27 06:27:39 +08:00
2012-03-20 02:52:18 +08:00
if ( ! first )
result . append ( separator ) ;
2012-06-05 12:24:42 +08:00
result . append ( iter - > str ( ) ) ;
2012-03-20 02:52:18 +08:00
first = false ;
}
2012-11-18 18:23:22 +08:00
2012-03-20 02:52:18 +08:00
/* Append old items */
load_old_if_needed ( ) ;
2014-03-29 14:22:03 +08:00
for ( std : : deque < size_t > : : reverse_iterator iter = old_item_offsets . rbegin ( ) ; iter ! = old_item_offsets . rend ( ) ; + + iter )
2012-11-19 08:30:30 +08:00
{
2012-03-20 02:52:18 +08:00
size_t offset = * iter ;
2012-06-16 07:22:37 +08:00
const history_item_t item = history_t : : decode_item ( mmap_start + offset , mmap_length - offset , mmap_type ) ;
2013-10-27 06:27:39 +08:00
2013-09-06 04:49:18 +08:00
/* Skip duplicates */
if ( ! seen . insert ( item . str ( ) ) . second )
continue ;
2013-10-27 06:27:39 +08:00
2012-03-20 02:52:18 +08:00
if ( ! first )
result . append ( separator ) ;
result . append ( item . str ( ) ) ;
first = false ;
}
}
2012-11-19 08:30:30 +08:00
history_item_t history_t : : item_at_index ( size_t idx )
{
2012-02-06 08:42:24 +08:00
scoped_lock locker ( lock ) ;
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
/* 0 is considered an invalid index */
assert ( idx > 0 ) ;
idx - - ;
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
/* idx=0 corresponds to last item in new_items */
size_t new_item_count = new_items . size ( ) ;
2012-11-19 08:30:30 +08:00
if ( idx < new_item_count )
{
2012-02-06 08:42:24 +08:00
return new_items . at ( new_item_count - idx - 1 ) ;
}
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
/* Now look in our old items */
idx - = new_item_count ;
load_old_if_needed ( ) ;
size_t old_item_count = old_item_offsets . size ( ) ;
2012-11-19 08:30:30 +08:00
if ( idx < old_item_count )
{
2012-02-06 08:42:24 +08:00
/* idx=0 corresponds to last item in old_item_offsets */
size_t offset = old_item_offsets . at ( old_item_count - idx - 1 ) ;
2012-06-16 07:22:37 +08:00
return history_t : : decode_item ( mmap_start + offset , mmap_length - offset , mmap_type ) ;
2012-02-06 08:42:24 +08:00
}
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
/* Index past the valid range, so return an empty history item */
return history_item_t ( wcstring ( ) , 0 ) ;
}
2012-03-02 06:56:34 +08:00
/* Read one line, stripping off any newline, and updating cursor. Note that our input string is NOT null terminated; it's just a memory mapped file. */
2012-11-19 08:30:30 +08:00
static size_t read_line ( const char * base , size_t cursor , size_t len , std : : string & result )
{
2012-02-16 16:24:27 +08:00
/* Locate the newline */
assert ( cursor < = len ) ;
const char * start = base + cursor ;
const char * newline = ( char * ) memchr ( start , ' \n ' , len - cursor ) ;
2012-11-19 08:30:30 +08:00
if ( newline ! = NULL )
{
2012-02-16 16:24:27 +08:00
/* We found a newline. */
result . assign ( start , newline - start ) ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* Return the amount to advance the cursor; skip over the newline */
return newline - start + 1 ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-02-16 16:24:27 +08:00
/* We ran off the end */
result . clear ( ) ;
return len - cursor ;
}
}
/* Trims leading spaces in the given string, returning how many there were */
2012-11-19 08:30:30 +08:00
static size_t trim_leading_spaces ( std : : string & str )
{
2012-02-16 16:24:27 +08:00
size_t i = 0 , max = str . size ( ) ;
while ( i < max & & str [ i ] = = ' ' )
i + + ;
str . erase ( 0 , i ) ;
return i ;
}
2013-04-08 05:59:48 +08:00
static bool extract_prefix_and_unescape_yaml ( std : : string & key , std : : string & value , const std : : string & line )
2012-11-19 08:30:30 +08:00
{
2012-02-16 16:24:27 +08:00
size_t where = line . find ( " : " ) ;
2012-11-19 08:30:30 +08:00
if ( where ! = std : : string : : npos )
{
2013-04-09 01:20:56 +08:00
key . assign ( line , 0 , where ) ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
// skip a space after the : if necessary
size_t val_start = where + 1 ;
if ( val_start < line . size ( ) & & line . at ( val_start ) = = ' ' )
val_start + + ;
2013-04-09 01:20:56 +08:00
value . assign ( line , val_start , line . size ( ) - val_start ) ;
2012-11-18 18:23:22 +08:00
2012-02-24 02:29:00 +08:00
unescape_yaml ( key ) ;
unescape_yaml ( value ) ;
2012-02-16 16:24:27 +08:00
}
return where ! = std : : string : : npos ;
}
2012-06-16 07:22:37 +08:00
/* Decode an item via the fish 2.0 format */
2012-11-19 08:30:30 +08:00
history_item_t history_t : : decode_item_fish_2_0 ( const char * base , size_t len )
{
2012-02-16 16:24:27 +08:00
wcstring cmd ;
time_t when = 0 ;
path_list_t paths ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
size_t indent = 0 , cursor = 0 ;
std : : string key , value , line ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* Read the "- cmd:" line */
size_t advance = read_line ( base , cursor , len , line ) ;
trim_leading_spaces ( line ) ;
2013-04-08 05:59:48 +08:00
if ( ! extract_prefix_and_unescape_yaml ( key , value , line ) | | key ! = " - cmd " )
2012-02-16 16:24:27 +08:00
goto done ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
cursor + = advance ;
2012-02-19 01:58:54 +08:00
cmd = str2wcstring ( value ) ;
2012-02-16 16:24:27 +08:00
/* Read the remaining lines */
2012-11-19 08:30:30 +08:00
for ( ; ; )
{
2012-02-16 16:24:27 +08:00
/* Read a line */
size_t advance = read_line ( base , cursor , len , line ) ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* Count and trim leading spaces */
size_t this_indent = trim_leading_spaces ( line ) ;
if ( indent = = 0 )
indent = this_indent ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
if ( this_indent = = 0 | | indent ! = this_indent )
break ;
2012-11-18 18:23:22 +08:00
2013-04-08 05:59:48 +08:00
if ( ! extract_prefix_and_unescape_yaml ( key , value , line ) )
2012-02-06 08:42:24 +08:00
break ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* We are definitely going to consume this line */
cursor + = advance ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( key = = " when " )
{
2013-04-09 01:20:56 +08:00
/* Parse an int from the timestamp. Should this fail, strtol returns 0; that's acceptable. */
char * end = NULL ;
long tmp = strtol ( value . c_str ( ) , & end , 0 ) ;
when = tmp ;
2012-11-19 08:30:30 +08:00
}
else if ( key = = " paths " )
{
2012-02-16 16:24:27 +08:00
/* Read lines starting with " - " until we can't read any more */
2012-11-19 08:30:30 +08:00
for ( ; ; )
{
2012-02-16 16:24:27 +08:00
size_t advance = read_line ( base , cursor , len , line ) ;
if ( trim_leading_spaces ( line ) < = indent )
break ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
if ( strncmp ( line . c_str ( ) , " - " , 2 ) )
break ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* We're going to consume this line */
cursor + = advance ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
/* Skip the leading dash-space and then store this path it */
line . erase ( 0 , 2 ) ;
unescape_yaml ( line ) ;
2012-07-25 13:31:31 +08:00
paths . push_back ( str2wcstring ( line ) ) ;
2012-02-06 08:42:24 +08:00
}
}
}
2012-02-16 16:24:27 +08:00
done :
2014-03-29 14:22:03 +08:00
history_item_t result ( cmd , when ) ;
result . required_paths . swap ( paths ) ;
return result ;
2012-06-16 07:22:37 +08:00
}
2012-11-19 08:30:30 +08:00
history_item_t history_t : : decode_item ( const char * base , size_t len , history_file_type_t type )
{
switch ( type )
{
2012-11-19 16:31:03 +08:00
case history_type_fish_1_x :
return history_t : : decode_item_fish_1_x ( base , len ) ;
case history_type_fish_2_0 :
return history_t : : decode_item_fish_2_0 ( base , len ) ;
default :
return history_item_t ( L " " ) ;
2012-06-16 07:22:37 +08:00
}
}
/**
Remove backslashes from all newlines . This makes a string from the
history file better formated for on screen display .
*/
2012-11-19 08:30:30 +08:00
static wcstring history_unescape_newlines_fish_1_x ( const wcstring & in_str )
2012-06-16 07:22:37 +08:00
{
wcstring out ;
2012-11-19 08:30:30 +08:00
for ( const wchar_t * in = in_str . c_str ( ) ; * in ; in + + )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
if ( * in = = L ' \\ ' )
{
if ( * ( in + 1 ) ! = L ' \n ' )
{
out . push_back ( * in ) ;
}
}
else
{
out . push_back ( * in ) ;
}
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
return out ;
2012-06-16 07:22:37 +08:00
}
/* Decode an item via the fish 1.x format. Adapted from fish 1.x's item_get(). */
2012-11-19 08:30:30 +08:00
history_item_t history_t : : decode_item_fish_1_x ( const char * begin , size_t length )
{
2012-06-16 07:22:37 +08:00
const char * end = begin + length ;
const char * pos = begin ;
bool was_backslash = 0 ;
wcstring out ;
bool first_char = true ;
bool timestamp_mode = false ;
time_t timestamp = 0 ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
while ( 1 )
2012-06-16 07:22:37 +08:00
{
wchar_t c ;
mbstate_t state ;
size_t res ;
2012-11-19 08:30:30 +08:00
memset ( & state , 0 , sizeof ( state ) ) ;
2012-06-16 07:22:37 +08:00
2012-11-19 08:30:30 +08:00
res = mbrtowc ( & c , pos , end - pos , & state ) ;
2012-06-16 07:22:37 +08:00
2012-11-19 08:30:30 +08:00
if ( res = = ( size_t ) - 1 )
2012-06-16 07:22:37 +08:00
{
pos + + ;
continue ;
}
2012-11-19 08:30:30 +08:00
else if ( res = = ( size_t ) - 2 )
2012-06-16 07:22:37 +08:00
{
break ;
}
2012-11-19 08:30:30 +08:00
else if ( res = = ( size_t ) 0 )
2012-06-16 07:22:37 +08:00
{
pos + + ;
continue ;
}
pos + = res ;
2012-11-19 08:30:30 +08:00
if ( c = = L ' \n ' )
2012-06-16 07:22:37 +08:00
{
2012-11-19 08:30:30 +08:00
if ( timestamp_mode )
2012-06-16 07:22:37 +08:00
{
const wchar_t * time_string = out . c_str ( ) ;
2012-11-19 08:30:30 +08:00
while ( * time_string & & ! iswdigit ( * time_string ) )
2012-06-16 07:22:37 +08:00
time_string + + ;
errno = 0 ;
2012-11-19 08:30:30 +08:00
if ( * time_string )
2012-06-16 07:22:37 +08:00
{
time_t tm ;
wchar_t * end ;
errno = 0 ;
2012-11-19 08:30:30 +08:00
tm = ( time_t ) wcstol ( time_string , & end , 10 ) ;
2012-06-16 07:22:37 +08:00
2012-11-19 08:30:30 +08:00
if ( tm & & ! errno & & ! * end )
2012-06-16 07:22:37 +08:00
{
timestamp = tm ;
}
}
out . clear ( ) ;
timestamp_mode = false ;
continue ;
}
2012-11-19 08:30:30 +08:00
if ( ! was_backslash )
2012-06-16 07:22:37 +08:00
break ;
}
2012-11-19 08:30:30 +08:00
if ( first_char )
2012-06-16 07:22:37 +08:00
{
2012-11-19 08:30:30 +08:00
if ( c = = L ' # ' )
2012-06-16 07:22:37 +08:00
timestamp_mode = true ;
}
first_char = false ;
out . push_back ( c ) ;
2012-11-19 08:30:30 +08:00
was_backslash = ( ( c = = L ' \\ ' ) & & ! was_backslash ) ;
2012-06-16 07:22:37 +08:00
}
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
out = history_unescape_newlines_fish_1_x ( out ) ;
return history_item_t ( out , timestamp ) ;
}
/* Try to infer the history file type based on inspecting the data */
2012-11-19 08:30:30 +08:00
static history_file_type_t infer_file_type ( const char * data , size_t len )
{
2012-06-16 07:22:37 +08:00
history_file_type_t result = history_type_unknown ;
2012-11-19 08:30:30 +08:00
if ( len > 0 )
{
2012-06-16 07:22:37 +08:00
/* Old fish started with a # */
2012-11-19 08:30:30 +08:00
if ( data [ 0 ] = = ' # ' )
{
2012-06-16 07:22:37 +08:00
result = history_type_fish_1_x ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-06-16 07:22:37 +08:00
/* Assume new fish */
result = history_type_fish_2_0 ;
}
}
return result ;
2012-02-06 08:42:24 +08:00
}
void history_t : : populate_from_mmap ( void )
{
2012-06-16 07:22:37 +08:00
mmap_type = infer_file_type ( mmap_start , mmap_length ) ;
2012-02-16 16:24:27 +08:00
size_t cursor = 0 ;
2012-11-19 08:30:30 +08:00
for ( ; ; )
{
2012-06-16 07:22:37 +08:00
size_t offset = offset_of_next_item ( mmap_start , mmap_length , mmap_type , & cursor , birth_timestamp ) ;
2012-04-16 13:49:27 +08:00
// If we get back -1, we're done
if ( offset = = ( size_t ) ( - 1 ) )
2012-02-16 16:24:27 +08:00
break ;
2012-03-02 06:56:34 +08:00
2012-04-16 13:49:27 +08:00
// Remember this item
2012-11-18 18:23:22 +08:00
old_item_offsets . push_back ( offset ) ;
2012-02-16 16:24:27 +08:00
}
2012-02-06 08:42:24 +08:00
}
2012-12-03 08:39:35 +08:00
/* Do a private, read-only map of the entirety of a history file with the given name. Returns true if successful. Returns the mapped memory region by reference. */
2013-04-28 06:21:14 +08:00
bool history_t : : map_file ( const wcstring & name , const char * * out_map_start , size_t * out_map_len , file_id_t * file_id )
2012-02-06 08:42:24 +08:00
{
2012-04-16 13:49:27 +08:00
bool result = false ;
2012-02-06 08:42:24 +08:00
wcstring filename = history_filename ( name , L " " ) ;
2012-04-16 13:49:27 +08:00
if ( ! filename . empty ( ) )
{
2012-12-03 08:39:35 +08:00
int fd = wopen_cloexec ( filename , O_RDONLY ) ;
if ( fd > = 0 )
2012-11-18 18:23:22 +08:00
{
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
/* Get the file ID if requested */
if ( file_id ! = NULL )
2014-04-28 04:34:51 +08:00
* file_id = file_id_for_fd ( fd ) ;
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
/* Take a read lock to guard against someone else appending. This is released when the file is closed (below). We will read the file after releasing the lock, but that's not a problem, because we never modify already written data. In short, the purpose of this lock is to ensure we don't see the file size change mid-update.
We may fail to lock ( e . g . on lockless NFS - see https : / / github . com / fish - shell / fish - shell / issues / 685 ) . In that case , we proceed as if it did not fail . The risk is that we may get an incomplete history item ; this is unlikely because we only treat an item as valid if it has a terminating newline .
Simulate a failing lock in chaos_mode
*/
if ( ! chaos_mode ) history_file_lock ( fd , F_RDLCK ) ;
off_t len = lseek ( fd , 0 , SEEK_END ) ;
if ( len ! = ( off_t ) - 1 )
2012-11-19 08:30:30 +08:00
{
2013-04-28 06:21:14 +08:00
size_t mmap_length = ( size_t ) len ;
if ( lseek ( fd , 0 , SEEK_SET ) = = 0 )
2012-11-19 08:30:30 +08:00
{
2013-04-28 06:21:14 +08:00
char * mmap_start ;
if ( ( mmap_start = ( char * ) mmap ( 0 , mmap_length , PROT_READ , MAP_PRIVATE , fd , 0 ) ) ! = MAP_FAILED )
2012-11-19 08:30:30 +08:00
{
2013-04-28 06:21:14 +08:00
result = true ;
* out_map_start = mmap_start ;
* out_map_len = mmap_length ;
2012-11-19 08:30:30 +08:00
}
}
}
close ( fd ) ;
2012-11-18 18:23:22 +08:00
}
2012-04-16 13:49:27 +08:00
}
return result ;
}
bool history_t : : load_old_if_needed ( void )
{
if ( loaded_old ) return true ;
loaded_old = true ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
// PCA not sure why signals were blocked here
2012-11-19 08:30:30 +08:00
//signal_block();
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
bool ok = false ;
2012-12-03 15:38:38 +08:00
if ( map_file ( name , & mmap_start , & mmap_length , & mmap_file_id ) )
2012-11-19 08:30:30 +08:00
{
2012-04-16 13:49:27 +08:00
// Here we've mapped the file
ok = true ;
time_profiler_t profiler ( " populate_from_mmap " ) ;
this - > populate_from_mmap ( ) ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
//signal_unblock();
2012-03-26 16:21:10 +08:00
return ok ;
2012-02-06 08:42:24 +08:00
}
2012-11-19 08:30:30 +08:00
void history_search_t : : skip_matches ( const wcstring_list_t & skips )
{
2012-02-07 03:52:24 +08:00
external_skips = skips ;
std : : sort ( external_skips . begin ( ) , external_skips . end ( ) ) ;
}
2012-11-19 08:30:30 +08:00
bool history_search_t : : should_skip_match ( const wcstring & str ) const
{
2012-02-07 03:52:24 +08:00
return std : : binary_search ( external_skips . begin ( ) , external_skips . end ( ) , str ) ;
}
2012-11-19 08:30:30 +08:00
bool history_search_t : : go_forwards ( )
{
2012-02-06 14:48:43 +08:00
/* Pop the top index (if more than one) and return if we have any left */
2012-11-19 08:30:30 +08:00
if ( prev_matches . size ( ) > 1 )
{
2012-02-06 14:30:42 +08:00
prev_matches . pop_back ( ) ;
2012-02-06 14:48:43 +08:00
return true ;
}
return false ;
2012-02-06 08:42:24 +08:00
}
2012-11-19 08:30:30 +08:00
bool history_search_t : : go_backwards ( )
{
2012-02-06 08:42:24 +08:00
/* Backwards means increasing our index */
const size_t max_idx = ( size_t ) ( - 1 ) ;
2012-02-06 14:30:42 +08:00
size_t idx = 0 ;
if ( ! prev_matches . empty ( ) )
idx = prev_matches . back ( ) . first ;
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
if ( idx = = max_idx )
return false ;
2012-11-18 18:23:22 +08:00
2013-04-06 15:28:55 +08:00
const bool main_thread = is_main_thread ( ) ;
2013-05-05 17:33:17 +08:00
2012-11-19 08:30:30 +08:00
while ( + + idx < max_idx )
{
2013-04-06 15:28:55 +08:00
if ( main_thread ? reader_interrupted ( ) : reader_thread_job_is_stale ( ) )
{
return false ;
}
2013-05-05 17:33:17 +08:00
2012-02-06 14:30:42 +08:00
const history_item_t item = history - > item_at_index ( idx ) ;
2013-04-06 15:28:55 +08:00
/* We're done if it's empty or we cancelled */
2012-11-19 08:30:30 +08:00
if ( item . empty ( ) )
{
2012-02-06 08:42:24 +08:00
return false ;
}
2012-02-06 14:30:42 +08:00
/* Look for a term that matches and that we haven't seen before */
2012-02-07 03:52:24 +08:00
const wcstring & str = item . str ( ) ;
2012-11-19 08:30:30 +08:00
if ( item . matches_search ( term , search_type ) & & ! match_already_made ( str ) & & ! should_skip_match ( str ) )
{
2012-02-16 16:24:27 +08:00
prev_matches . push_back ( prev_match_t ( idx , item ) ) ;
2012-02-06 08:42:24 +08:00
return true ;
}
}
return false ;
}
/** Goes to the end (forwards) */
2012-11-19 08:30:30 +08:00
void history_search_t : : go_to_end ( void )
{
2012-02-06 14:30:42 +08:00
prev_matches . clear ( ) ;
2012-02-06 08:42:24 +08:00
}
2012-02-06 15:22:18 +08:00
/** Returns if we are at the end, which is where we start. */
2012-11-19 08:30:30 +08:00
bool history_search_t : : is_at_end ( void ) const
{
2012-02-06 15:22:18 +08:00
return prev_matches . empty ( ) ;
}
2012-02-06 08:42:24 +08:00
/** Goes to the beginning (backwards) */
2012-11-19 08:30:30 +08:00
void history_search_t : : go_to_beginning ( void )
{
2012-02-06 14:30:42 +08:00
/* Just go backwards as far as we can */
while ( go_backwards ( ) )
;
2012-02-06 08:42:24 +08:00
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
history_item_t history_search_t : : current_item ( ) const
{
2012-11-18 18:23:22 +08:00
assert ( ! prev_matches . empty ( ) ) ;
2012-02-06 14:30:42 +08:00
return prev_matches . back ( ) . second ;
2006-10-22 09:21:02 +08:00
}
2012-11-19 08:30:30 +08:00
wcstring history_search_t : : current_string ( ) const
{
2012-02-16 16:24:27 +08:00
history_item_t item = this - > current_item ( ) ;
return item . str ( ) ;
}
2012-11-19 08:30:30 +08:00
bool history_search_t : : match_already_made ( const wcstring & match ) const
{
for ( std : : vector < prev_match_t > : : const_iterator iter = prev_matches . begin ( ) ; iter ! = prev_matches . end ( ) ; + + iter )
{
2012-02-16 16:24:27 +08:00
if ( iter - > second . str ( ) = = match )
2012-02-06 14:30:42 +08:00
return true ;
}
return false ;
2006-10-22 09:21:02 +08:00
}
2012-02-16 16:24:27 +08:00
static void replace_all ( std : : string & str , const char * needle , const char * replacement )
2012-02-06 08:42:24 +08:00
{
2012-02-16 16:24:27 +08:00
size_t needle_len = strlen ( needle ) , replacement_len = strlen ( replacement ) ;
2012-02-06 08:42:24 +08:00
size_t offset = 0 ;
2012-11-19 08:30:30 +08:00
while ( ( offset = str . find ( needle , offset ) ) ! = std : : string : : npos )
2012-02-06 08:42:24 +08:00
{
str . replace ( offset , needle_len , replacement ) ;
2012-02-16 16:24:27 +08:00
offset + = replacement_len ;
2012-02-06 08:42:24 +08:00
}
}
2012-11-19 08:30:30 +08:00
static void escape_yaml ( std : : string & str )
{
2012-02-16 16:24:27 +08:00
replace_all ( str , " \\ " , " \\ \\ " ) ; //replace one backslash with two
replace_all ( str , " \n " , " \\ n " ) ; //replace newline with backslash + literal n
}
2013-04-08 05:59:48 +08:00
/* This function is called frequently, so it ought to be fast. */
2012-11-19 08:30:30 +08:00
static void unescape_yaml ( std : : string & str )
{
2013-04-08 05:59:48 +08:00
size_t cursor = 0 , size = str . size ( ) ;
while ( cursor < size )
2012-11-19 08:30:30 +08:00
{
2013-04-08 05:59:48 +08:00
// Operate on a const version of str, to avoid needless COWs that at() does.
const std : : string & const_str = str ;
2013-05-05 17:33:17 +08:00
2013-04-08 05:59:48 +08:00
// Look for a backslash
size_t backslash = const_str . find ( ' \\ ' , cursor ) ;
if ( backslash = = std : : string : : npos | | backslash + 1 > = size )
{
// Either not found, or found as the last character
break ;
}
else
2012-11-19 08:30:30 +08:00
{
2013-04-08 05:59:48 +08:00
// Backslash found. Maybe we'll do something about it. Be sure to invoke the const version of at().
char escaped_char = const_str . at ( backslash + 1 ) ;
if ( escaped_char = = ' \\ ' )
2012-11-19 08:30:30 +08:00
{
2013-04-08 05:59:48 +08:00
// Two backslashes in a row. Delete the second one.
str . erase ( backslash + 1 , 1 ) ;
size - - ;
2012-11-19 08:30:30 +08:00
}
2013-04-08 05:59:48 +08:00
else if ( escaped_char = = ' n ' )
2012-11-19 08:30:30 +08:00
{
2013-04-08 05:59:48 +08:00
// Backslash + n. Replace with a newline.
str . replace ( backslash , 2 , " \n " ) ;
size - - ;
2012-02-16 16:24:27 +08:00
}
2013-04-08 05:59:48 +08:00
// The character at index backslash has now been made whole; start at the next character
cursor = backslash + 1 ;
2012-02-16 16:24:27 +08:00
}
}
2012-02-06 12:54:41 +08:00
}
2012-02-06 08:42:24 +08:00
static wcstring history_filename ( const wcstring & name , const wcstring & suffix )
{
wcstring path ;
if ( ! path_get_config ( path ) )
return L " " ;
2012-11-18 18:23:22 +08:00
2012-02-06 08:42:24 +08:00
wcstring result = path ;
result . append ( L " / " ) ;
result . append ( name ) ;
result . append ( L " _history " ) ;
result . append ( suffix ) ;
return result ;
}
2012-02-16 16:24:27 +08:00
void history_t : : clear_file_state ( )
{
/* Erase everything we know about our file */
2012-11-19 08:30:30 +08:00
if ( mmap_start ! = NULL & & mmap_start ! = MAP_FAILED )
{
2012-02-16 16:24:27 +08:00
munmap ( ( void * ) mmap_start , mmap_length ) ;
}
2012-03-02 06:56:34 +08:00
mmap_start = NULL ;
2012-02-16 16:24:27 +08:00
mmap_length = 0 ;
loaded_old = false ;
old_item_offsets . clear ( ) ;
}
2012-11-19 08:30:30 +08:00
void history_t : : compact_new_items ( )
{
2012-03-02 06:56:34 +08:00
/* Keep only the most recent items with the given contents. This algorithm could be made more efficient, but likely would consume more memory too. */
std : : set < wcstring > seen ;
size_t idx = new_items . size ( ) ;
2012-11-19 08:30:30 +08:00
while ( idx - - )
{
2012-03-02 06:56:34 +08:00
const history_item_t & item = new_items [ idx ] ;
2012-11-19 08:30:30 +08:00
if ( ! seen . insert ( item . contents ) . second )
{
2012-03-02 06:56:34 +08:00
// This item was not inserted because it was already in the set, so delete the item at this index
new_items . erase ( new_items . begin ( ) + idx ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
if ( idx < first_unwritten_new_item_index )
{
/* Decrement first_unwritten_new_item_index if we are deleting a previously written item */
first_unwritten_new_item_index - - ;
}
2012-03-02 06:56:34 +08:00
}
}
}
2012-12-03 08:39:35 +08:00
bool history_t : : save_internal_via_rewrite ( )
2012-02-06 08:42:24 +08:00
{
2012-02-06 12:54:41 +08:00
/* This must be called while locked */
2012-03-02 06:56:34 +08:00
ASSERT_IS_LOCKED ( lock ) ;
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
bool ok = false ;
2012-11-18 18:23:22 +08:00
2012-12-03 17:53:52 +08:00
wcstring tmp_name_template = history_filename ( name , L " .XXXXXX " ) ;
if ( ! tmp_name_template . empty ( ) )
2012-11-19 08:30:30 +08:00
{
2012-02-24 02:49:30 +08:00
/* Make an LRU cache to save only the last N elements */
history_lru_cache_t lru ( HISTORY_SAVE_MAX ) ;
2012-11-18 18:23:22 +08:00
2012-03-02 06:56:34 +08:00
/* Insert old items in, from old to new. Merge them with our new items, inserting items with earlier timestamps first. */
2014-03-29 14:22:03 +08:00
history_item_list_t : : const_iterator new_item_iter = new_items . begin ( ) ;
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Map in existing items (which may have changed out from underneath us, so don't trust our old mmap'd data) */
const char * local_mmap_start = NULL ;
size_t local_mmap_size = 0 ;
2012-12-03 15:38:38 +08:00
if ( map_file ( name , & local_mmap_start , & local_mmap_size , NULL ) )
2012-11-19 08:30:30 +08:00
{
2012-06-16 07:22:37 +08:00
const history_file_type_t local_mmap_type = infer_file_type ( local_mmap_start , local_mmap_size ) ;
2012-04-16 13:49:27 +08:00
size_t cursor = 0 ;
2012-11-19 08:30:30 +08:00
for ( ; ; )
{
2012-06-16 07:22:37 +08:00
size_t offset = offset_of_next_item ( local_mmap_start , local_mmap_size , local_mmap_type , & cursor , 0 ) ;
2012-04-16 13:49:27 +08:00
/* If we get back -1, we're done */
if ( offset = = ( size_t ) ( - 1 ) )
2012-03-02 06:56:34 +08:00
break ;
2012-04-16 13:49:27 +08:00
/* Try decoding an old item */
2012-06-16 07:22:37 +08:00
const history_item_t old_item = history_t : : decode_item ( local_mmap_start + offset , local_mmap_size - offset , local_mmap_type ) ;
2012-12-03 08:39:35 +08:00
if ( old_item . empty ( ) | | deleted_items . count ( old_item . str ( ) ) > 0 )
2012-06-05 12:24:42 +08:00
{
// debug(0, L"Item is deleted : %s\n", old_item.str().c_str());
2012-04-16 13:49:27 +08:00
continue ;
2012-06-05 12:24:42 +08:00
}
2012-04-16 13:49:27 +08:00
/* The old item may actually be more recent than our new item, if it came from another session. Insert all new items at the given index with an earlier timestamp. */
2012-11-19 08:30:30 +08:00
for ( ; new_item_iter ! = new_items . end ( ) ; + + new_item_iter )
{
if ( new_item_iter - > timestamp ( ) < old_item . timestamp ( ) )
{
2012-04-16 13:49:27 +08:00
/* This "new item" is in fact older. */
lru . add_item ( * new_item_iter ) ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-04-16 13:49:27 +08:00
/* The new item is not older. */
break ;
}
2012-03-02 06:56:34 +08:00
}
2012-11-18 18:23:22 +08:00
2012-04-16 13:49:27 +08:00
/* Now add this old item */
lru . add_item ( old_item ) ;
2012-03-02 06:56:34 +08:00
}
2012-04-16 13:49:27 +08:00
munmap ( ( void * ) local_mmap_start , local_mmap_size ) ;
2012-02-24 02:49:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-03-02 06:56:34 +08:00
/* Insert any remaining new items */
2012-06-05 12:24:42 +08:00
for ( ; new_item_iter ! = new_items . end ( ) ; + + new_item_iter )
{
2012-03-02 06:56:34 +08:00
lru . add_item ( * new_item_iter ) ;
2012-02-24 02:49:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-02-24 02:49:30 +08:00
signal_block ( ) ;
2012-11-18 18:23:22 +08:00
2012-12-03 17:53:52 +08:00
/* Try to create a temporary file, up to 10 times. We don't use mkstemps because we want to open it CLO_EXEC. This should almost always succeed on the first try. */
int out_fd = - 1 ;
wcstring tmp_name ;
for ( size_t attempt = 0 ; attempt < 10 & & out_fd = = - 1 ; attempt + + )
{
char * narrow_str = wcs2str ( tmp_name_template . c_str ( ) ) ;
2014-05-04 06:27:58 +08:00
# if HAVE_MKOSTEMP
out_fd = mkostemp ( narrow_str , O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC ) ;
if ( out_fd > = 0 )
{
tmp_name = str2wcstring ( narrow_str ) ;
}
# else
2012-12-03 17:53:52 +08:00
if ( narrow_str & & mktemp ( narrow_str ) )
{
/* It was successfully templated; try opening it atomically */
tmp_name = str2wcstring ( narrow_str ) ;
out_fd = wopen_cloexec ( tmp_name , O_WRONLY | O_CREAT | O_EXCL | O_TRUNC , 0644 ) ;
}
2014-05-04 06:27:58 +08:00
# endif
2012-12-03 17:53:52 +08:00
free ( narrow_str ) ;
}
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
if ( out_fd > = 0 )
2012-11-19 08:30:30 +08:00
{
2013-04-28 06:21:14 +08:00
/* Write them out */
bool errored = false ;
history_output_buffer_t buffer ;
for ( history_lru_cache_t : : iterator iter = lru . begin ( ) ; iter ! = lru . end ( ) ; + + iter )
2012-11-19 08:30:30 +08:00
{
2013-04-28 06:21:14 +08:00
const history_lru_node_t * node = * iter ;
append_yaml_to_buffer ( node - > key , node - > timestamp , node - > required_paths , & buffer ) ;
if ( buffer . output_size ( ) > = HISTORY_OUTPUT_BUFFER_SIZE & & ! buffer . flush_to_fd ( out_fd ) )
2012-12-03 08:39:35 +08:00
{
2013-04-28 06:21:14 +08:00
errored = true ;
break ;
2012-02-06 12:54:41 +08:00
}
2013-04-28 06:21:14 +08:00
}
2012-11-18 18:23:22 +08:00
2013-04-28 06:21:14 +08:00
if ( ! errored & & buffer . flush_to_fd ( out_fd ) )
{
ok = true ;
2012-11-19 08:30:30 +08:00
}
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
if ( ! ok )
{
/*
This message does not have high enough priority to
be shown by default .
*/
debug ( 2 , L " Error when writing history file " ) ;
}
else
{
wcstring new_name = history_filename ( name , wcstring ( ) ) ;
wrename ( tmp_name , new_name ) ;
}
2012-12-03 08:39:35 +08:00
close ( out_fd ) ;
2013-04-28 06:21:14 +08:00
}
2012-11-18 18:23:22 +08:00
2012-02-24 02:49:30 +08:00
signal_unblock ( ) ;
2012-11-18 18:23:22 +08:00
2012-02-24 02:49:30 +08:00
/* Make sure we clear all nodes, since this doesn't happen automatically */
lru . evict_all_nodes ( ) ;
2012-12-03 08:39:35 +08:00
}
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
if ( ok )
{
/* We've saved everything, so we have no more unsaved items */
this - > first_unwritten_new_item_index = new_items . size ( ) ;
2012-12-03 18:25:08 +08:00
/* We deleted our deleted items */
2012-12-03 15:38:38 +08:00
this - > deleted_items . clear ( ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
/* Our history has been written to the file, so clear our state so we can re-reference the file. */
this - > clear_file_state ( ) ;
}
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
return ok ;
}
2012-11-18 18:23:22 +08:00
2012-12-03 08:39:35 +08:00
bool history_t : : save_internal_via_appending ( )
{
/* This must be called while locked */
ASSERT_IS_LOCKED ( lock ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
/* No deleting allowed */
assert ( deleted_items . empty ( ) ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
bool ok = false ;
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
/* If the file is different (someone vacuumed it) then we need to update our mmap */
bool file_changed = false ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
/* Get the path to the real history file */
wcstring history_path = history_filename ( name , wcstring ( ) ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
signal_block ( ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
/* Open the file */
int out_fd = wopen_cloexec ( history_path , O_WRONLY | O_APPEND ) ;
if ( out_fd > = 0 )
{
2012-12-03 15:38:38 +08:00
/* Check to see if the file changed */
2014-04-28 04:34:51 +08:00
if ( file_id_for_fd ( out_fd ) ! = mmap_file_id )
2012-12-03 15:38:38 +08:00
file_changed = true ;
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
/* Exclusive lock on the entire file. This is released when we close the file (below). This may fail on (e.g.) lockless NFS. If so, proceed as if it did not fail; the risk is that we may get interleaved history items, which is considered better than no history, or forcing everything through the slow copy-move mode. We try to minimize this possibility by writing with O_APPEND.
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
Simulate a failing lock in chaos_mode
*/
if ( ! chaos_mode ) history_file_lock ( out_fd , F_WRLCK ) ;
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
/* We (hopefully successfully) took the exclusive lock. Append to the file.
Note that this is sketchy for a few reasons :
- Another shell may have appended its own items with a later timestamp , so our file may no longer be sorted by timestamp .
- Another shell may have appended the same items , so our file may now contain duplicates .
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
We cannot modify any previous parts of our file , because other instances may be reading those portions . We can only append .
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
Originally we always rewrote the file on saving , which avoided both of these problems . However , appending allows us to save history after every command , which is nice !
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
Periodically we " clean up " the file by rewriting it , so that most of the time it doesn ' t have duplicates , although we don ' t yet sort by timestamp ( the timestamp isn ' t really used for much anyways ) .
*/
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
/* So far so good. Write all items at or after first_unwritten_new_item_index */
2013-05-05 17:33:17 +08:00
2013-04-28 06:21:14 +08:00
bool errored = false ;
history_output_buffer_t buffer ;
while ( first_unwritten_new_item_index < new_items . size ( ) )
{
const history_item_t & item = new_items . at ( first_unwritten_new_item_index ) ;
append_yaml_to_buffer ( item . str ( ) , item . timestamp ( ) , item . get_required_paths ( ) , & buffer ) ;
if ( buffer . output_size ( ) > = HISTORY_OUTPUT_BUFFER_SIZE )
{
errored = ! buffer . flush_to_fd ( out_fd ) ;
if ( errored ) break ;
}
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
/* We wrote this item, hooray */
first_unwritten_new_item_index + + ;
}
2012-12-03 18:25:08 +08:00
2013-04-28 06:21:14 +08:00
if ( ! errored & & buffer . flush_to_fd ( out_fd ) )
{
ok = true ;
2012-12-03 08:39:35 +08:00
}
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
close ( out_fd ) ;
2013-04-28 06:21:14 +08:00
}
2012-02-06 08:42:24 +08:00
2012-12-03 08:39:35 +08:00
signal_unblock ( ) ;
2012-12-03 18:25:08 +08:00
2012-12-03 15:38:38 +08:00
/* If someone has replaced the file, forget our file state */
if ( file_changed )
{
this - > clear_file_state ( ) ;
}
2012-12-03 18:25:08 +08:00
2012-12-03 08:39:35 +08:00
return ok ;
}
2012-12-03 15:38:38 +08:00
/** Save the specified mode to file; optionally also vacuums */
void history_t : : save_internal ( bool vacuum )
2012-12-03 08:39:35 +08:00
{
/* This must be called while locked */
ASSERT_IS_LOCKED ( lock ) ;
/* Nothing to do if there's no new items */
2012-12-03 18:25:08 +08:00
if ( first_unwritten_new_item_index > = new_items . size ( ) & & deleted_items . empty ( ) )
2012-12-03 08:39:35 +08:00
return ;
/* Compact our new items so we don't have duplicates */
this - > compact_new_items ( ) ;
/* Try saving. If we have items to delete, we have to rewrite the file. If we do not, we can append to it. */
bool ok = false ;
2012-12-03 15:38:38 +08:00
if ( ! vacuum & & deleted_items . empty ( ) )
{
/* Try doing a fast append */
ok = save_internal_via_appending ( ) ;
}
2012-12-03 08:39:35 +08:00
if ( ! ok )
2012-11-19 08:30:30 +08:00
{
2012-12-03 15:38:38 +08:00
/* We did not or could not append; rewrite the file ("vacuum" it) */
ok = this - > save_internal_via_rewrite ( ) ;
2012-11-19 08:30:30 +08:00
}
2012-02-06 08:42:24 +08:00
}
2012-02-06 12:54:41 +08:00
2012-06-16 07:22:37 +08:00
void history_t : : save ( void )
{
2012-02-06 12:54:41 +08:00
scoped_lock locker ( lock ) ;
2012-12-03 15:38:38 +08:00
this - > save_internal ( false ) ;
}
void history_t : : save_and_vacuum ( void )
{
scoped_lock locker ( lock ) ;
this - > save_internal ( true ) ;
2012-02-06 12:54:41 +08:00
}
2012-02-06 08:42:24 +08:00
2014-03-29 14:22:03 +08:00
void history_t : : disable_automatic_saving ( )
{
scoped_lock locker ( lock ) ;
disable_automatic_save_counter + + ;
assert ( disable_automatic_save_counter ! = 0 ) ; // overflow!
}
void history_t : : enable_automatic_saving ( )
{
scoped_lock locker ( lock ) ;
assert ( disable_automatic_save_counter > 0 ) ; //underflow
disable_automatic_save_counter - - ;
save_internal_unless_disabled ( ) ;
}
2012-06-16 07:22:37 +08:00
void history_t : : clear ( void )
{
2012-02-16 16:24:27 +08:00
scoped_lock locker ( lock ) ;
new_items . clear ( ) ;
2012-06-05 12:24:42 +08:00
deleted_items . clear ( ) ;
2012-12-03 08:39:35 +08:00
first_unwritten_new_item_index = 0 ;
2012-02-16 16:24:27 +08:00
old_item_offsets . clear ( ) ;
wcstring filename = history_filename ( name , L " " ) ;
if ( ! filename . empty ( ) )
2012-02-19 01:11:22 +08:00
wunlink ( filename ) ;
2012-02-16 16:24:27 +08:00
this - > clear_file_state ( ) ;
2012-11-18 18:23:22 +08:00
2012-02-16 16:24:27 +08:00
}
2012-07-10 13:54:08 +08:00
bool history_t : : is_empty ( void )
{
scoped_lock locker ( lock ) ;
2012-12-06 05:33:07 +08:00
2012-12-05 08:00:35 +08:00
/* If we have new items, we're not empty */
if ( ! new_items . empty ( ) )
return false ;
bool empty = false ;
if ( loaded_old )
2012-07-10 13:54:08 +08:00
{
2012-12-05 08:00:35 +08:00
/* If we've loaded old items, see if we have any offsets */
empty = old_item_offsets . empty ( ) ;
2012-07-10 13:54:08 +08:00
}
2012-12-05 08:00:35 +08:00
else
{
/* If we have not loaded old items, don't actually load them (which may be expensive); just stat the file and see if it exists and is nonempty */
const wcstring where = history_filename ( name , L " " ) ;
struct stat buf = { } ;
if ( wstat ( where , & buf ) ! = 0 )
{
/* Access failed, assume missing */
empty = true ;
}
else
{
/* We're empty if the file is empty */
empty = ( buf . st_size = = 0 ) ;
}
}
return empty ;
2012-07-10 13:54:08 +08:00
}
2012-06-16 07:22:37 +08:00
/* Indicate whether we ought to import the bash history file into fish */
static bool should_import_bash_history_line ( const std : : string & line )
{
if ( line . empty ( ) )
return false ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
/* Very naive tests! Skip export; probably should skip others. */
2012-11-19 08:30:30 +08:00
const char * const ignore_prefixes [ ] =
{
2012-06-16 07:22:37 +08:00
" export " ,
" # "
} ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
for ( size_t i = 0 ; i < sizeof ignore_prefixes / sizeof * ignore_prefixes ; i + + )
{
2012-06-16 07:22:37 +08:00
const char * prefix = ignore_prefixes [ i ] ;
2012-11-19 08:30:30 +08:00
if ( ! line . compare ( 0 , strlen ( prefix ) , prefix ) )
{
2012-06-16 07:22:37 +08:00
return false ;
}
}
2012-11-18 18:23:22 +08:00
2012-07-10 13:54:08 +08:00
/* Skip lines with backticks */
if ( line . find ( ' ` ' ) ! = std : : string : : npos )
return false ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
return true ;
}
void history_t : : populate_from_bash ( FILE * stream )
{
/* Bash's format is very simple: just lines with #s for comments.
Ignore a few commands that are bash - specific . This list ought to be expanded .
*/
std : : string line ;
2012-11-19 08:30:30 +08:00
for ( ; ; )
{
2012-06-16 07:22:37 +08:00
line . clear ( ) ;
bool success = false , has_newline = false ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
/* Loop until we've read a line */
2012-11-19 08:30:30 +08:00
do
{
2012-06-16 07:22:37 +08:00
char buff [ 128 ] ;
success = ! ! fgets ( buff , sizeof buff , stream ) ;
2012-11-19 08:30:30 +08:00
if ( success )
{
2012-06-16 07:22:37 +08:00
/* Skip the newline */
char * newline = strchr ( buff , ' \n ' ) ;
if ( newline ) * newline = ' \0 ' ;
has_newline = ( newline ! = NULL ) ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
/* Append what we've got */
line . append ( buff ) ;
}
2012-11-19 08:30:30 +08:00
}
while ( success & & ! has_newline ) ;
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
/* Maybe add this line */
2012-11-19 08:30:30 +08:00
if ( should_import_bash_history_line ( line ) )
{
2012-06-16 07:22:37 +08:00
this - > add ( str2wcstring ( line ) ) ;
}
2012-11-18 18:23:22 +08:00
2012-06-16 07:22:37 +08:00
if ( line . empty ( ) )
break ;
}
}
2006-10-21 06:36:49 +08:00
void history_init ( )
2006-10-21 18:30:35 +08:00
{
2006-10-21 06:36:49 +08:00
}
2005-09-20 21:26:39 +08:00
2006-10-21 06:36:49 +08:00
void history_destroy ( )
{
2012-02-06 12:54:41 +08:00
/* Save all histories */
2012-11-19 08:30:30 +08:00
for ( std : : map < wcstring , history_t * > : : iterator iter = histories . begin ( ) ; iter ! = histories . end ( ) ; + + iter )
{
2012-02-06 12:54:41 +08:00
iter - > second - > save ( ) ;
}
2006-10-21 06:36:49 +08:00
}
2005-09-20 21:26:39 +08:00
2006-10-21 06:36:49 +08:00
void history_sanity_check ( )
{
2012-11-19 08:30:30 +08:00
/*
No sanity checking implemented yet . . .
*/
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
int file_detection_context_t : : perform_file_detection ( bool test_all )
{
2012-02-16 16:24:27 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
valid_paths . clear ( ) ;
int result = 1 ;
2012-11-19 08:30:30 +08:00
for ( path_list_t : : const_iterator iter = potential_paths . begin ( ) ; iter ! = potential_paths . end ( ) ; + + iter )
{
if ( path_is_valid ( * iter , working_directory ) )
{
2012-02-16 16:24:27 +08:00
/* Push the original (possibly relative) path */
2012-07-25 13:31:31 +08:00
valid_paths . push_back ( * iter ) ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-02-16 16:24:27 +08:00
/* Not a valid path */
result = 0 ;
if ( ! test_all )
break ;
2012-02-16 03:33:41 +08:00
}
}
2012-02-16 16:24:27 +08:00
return result ;
}
2012-11-19 08:30:30 +08:00
bool file_detection_context_t : : paths_are_valid ( const path_list_t & paths )
{
2012-02-16 16:24:27 +08:00
this - > potential_paths = paths ;
return perform_file_detection ( false ) > 0 ;
}
2014-03-29 14:22:03 +08:00
file_detection_context_t : : file_detection_context_t ( history_t * hist , history_identifier_t ident ) :
2012-02-16 16:24:27 +08:00
history ( hist ) ,
2014-03-29 14:22:03 +08:00
working_directory ( env_get_pwd_slash ( ) ) ,
history_item_identifier ( ident )
2012-02-19 10:54:36 +08:00
{
2012-02-16 16:24:27 +08:00
}
2012-02-16 03:33:41 +08:00
2012-11-19 08:30:30 +08:00
static int threaded_perform_file_detection ( file_detection_context_t * ctx )
{
2012-02-16 03:33:41 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
assert ( ctx ! = NULL ) ;
2012-02-16 16:24:27 +08:00
return ctx - > perform_file_detection ( true /* test all */ ) ;
2012-02-16 03:33:41 +08:00
}
2012-11-19 08:30:30 +08:00
static void perform_file_detection_done ( file_detection_context_t * ctx , int success )
{
2014-03-29 14:22:03 +08:00
ASSERT_IS_MAIN_THREAD ( ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Now that file detection is done, update the history item with the valid file paths */
ctx - > history - > set_valid_file_paths ( ctx - > valid_paths , ctx - > history_item_identifier ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Allow saving again */
ctx - > history - > enable_automatic_saving ( ) ;
2012-11-18 18:23:22 +08:00
2012-02-16 03:33:41 +08:00
/* Done with the context. */
delete ctx ;
}
2012-11-19 08:30:30 +08:00
static bool string_could_be_path ( const wcstring & potential_path )
{
2012-02-16 03:33:41 +08:00
// Assume that things with leading dashes aren't paths
if ( potential_path . empty ( ) | | potential_path . at ( 0 ) = = L ' - ' )
2014-03-29 14:22:03 +08:00
{
2012-02-16 03:33:41 +08:00
return false ;
2014-03-29 14:22:03 +08:00
}
2012-02-16 03:33:41 +08:00
return true ;
}
void history_t : : add_with_file_detection ( const wcstring & str )
{
ASSERT_IS_MAIN_THREAD ( ) ;
path_list_t potential_paths ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Find all arguments that look like they could be file paths */
2013-02-03 06:50:22 +08:00
bool impending_exit = false ;
2014-03-29 14:22:03 +08:00
parse_node_tree_t tree ;
parse_tree_from_string ( str , parse_flag_none , & tree , NULL ) ;
size_t count = tree . size ( ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
for ( size_t i = 0 ; i < count ; i + + )
2012-02-16 03:33:41 +08:00
{
2014-03-29 14:22:03 +08:00
const parse_node_t & node = tree . at ( i ) ;
if ( ! node . has_source ( ) )
2012-11-19 08:30:30 +08:00
{
2014-03-29 14:22:03 +08:00
continue ;
}
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
if ( node . type = = symbol_argument )
{
wcstring potential_path = node . get_source ( str ) ;
bool unescaped = unescape_string_in_place ( & potential_path , UNESCAPE_DEFAULT ) ;
if ( unescaped & & string_could_be_path ( potential_path ) )
2012-11-19 08:30:30 +08:00
{
2014-03-29 14:22:03 +08:00
potential_paths . push_back ( potential_path ) ;
}
}
else if ( node . type = = symbol_plain_statement )
{
/* Hack hack hack - if the command is likely to trigger an exit, then don't do background file detection, because we won't be able to write it to our history file before we exit. */
if ( tree . decoration_for_plain_statement ( node ) = = parse_statement_decoration_exec )
{
impending_exit = true ;
}
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
wcstring command ;
tree . command_for_plain_statement ( node , str , & command ) ;
unescape_string_in_place ( & command , UNESCAPE_DEFAULT ) ;
if ( contains ( command , L " exit " , L " reboot " ) )
{
impending_exit = true ;
2012-02-16 03:33:41 +08:00
}
}
}
2013-02-04 03:38:22 +08:00
2014-03-29 14:22:03 +08:00
/* If we got a path, we'll perform file detection for autosuggestion hinting */
history_identifier_t identifier = 0 ;
if ( ! potential_paths . empty ( ) & & ! impending_exit )
2013-02-03 06:50:22 +08:00
{
2014-03-29 14:22:03 +08:00
/* Grab the next identifier */
static history_identifier_t sLastIdentifier = 0 ;
identifier = + + sLastIdentifier ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Create a new detection context */
file_detection_context_t * context = new file_detection_context_t ( this , identifier ) ;
2012-02-16 03:33:41 +08:00
context - > potential_paths . swap ( potential_paths ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Prevent saving until we're done, so we have time to get the paths */
this - > disable_automatic_saving ( ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* Kick it off. Even though we haven't added the item yet, it updates the item on the main thread, so we can't race */
2012-02-16 03:33:41 +08:00
iothread_perform ( threaded_perform_file_detection , perform_file_detection_done , context ) ;
}
2014-03-29 14:22:03 +08:00
/* Actually add the item to the history */
this - > add ( str , identifier ) ;
2014-04-01 01:01:39 +08:00
2014-03-29 14:22:03 +08:00
/* If we think we're about to exit, save immediately, regardless of any disabling. This may cause us to lose file hinting for some commands, but it beats losing history items */
if ( impending_exit )
{
this - > save ( ) ;
}
}