aboutsummaryrefslogtreecommitdiffhomepage
path: root/history.cpp
blob: fd7b328f1ad7a9ae28ab899c0b0175a7e0669895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
/** \file history.c
	History functions, part of the user interface.
*/
#include "config.h"

#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>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <assert.h>

#include "fallback.h"
#include "util.h"
#include "sanity.h"
#include "tokenizer.h"

#include "wutil.h"
#include "history.h"
#include "common.h"
#include "intern.h"
#include "path.h"
#include "signal.h"
#include "autoload.h"
#include "iothread.h"
#include <map>
#include <algorithm>

/*

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
    
  Newlines are replaced by \n. Backslashes are replaced by \\.
*/



/** When we rewrite the history, the number of items we keep */
#define HISTORY_SAVE_MAX (1024 * 256)

/** Interval in seconds between automatic history save */
#define SAVE_INTERVAL (5*60)

/** Number of new history entries to add before automatic history save */
#define SAVE_COUNT 5

/** Whether we print timing information */
#define LOG_TIMES 0

class time_profiler_t {
    const char *what;
    double start;
    public:
    
    time_profiler_t(const char *w) {
        if (LOG_TIMES) {
            what = w;
            start = timef();
        }
    }
    
    ~time_profiler_t() {
        if (LOG_TIMES) {
            double end = timef();
            printf("(LOG_TIMES %s: %02f msec)\n", what, (end - start) * 1000);
        }
    }
};

/* Our LRU cache is used for restricting the amount of history we have, and limiting how long we order it. */
class history_lru_node_t : public lru_node_t {
    public:
    time_t timestamp;
    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)
    {}
    
    bool write_yaml_to_file(FILE *f) const;
};

class history_lru_cache_t : public lru_cache_t<history_lru_node_t> {
    protected:
    
    /* Override to delete evicted nodes */
    virtual void node_was_evicted(history_lru_node_t *node) {
        delete node;
    }

    public:
    history_lru_cache_t(size_t max) : lru_cache_t<history_lru_node_t>(max) { }
    
    /* Function to add a history item */
    void add_item(const history_item_t &item) {
        /* Skip empty items */
        if (item.empty())
            return;
            
        /* 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());
        if (node != NULL) {
            node->timestamp = std::max(node->timestamp, item.timestamp());
            /* What to do about paths here? Let's just ignore them */
        } else {
            node = new history_lru_node_t(item);
            this->add_node(node);
        }
    }
};

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);

/** Replaces newlines with a literal backslash followed by an n, and replaces backslashes with two backslashes. */
static void escape_yaml(std::string &str);

/** Undoes escape_yaml */
static void unescape_yaml(std::string &str);

/* We can merge two items if they are the same command. We use the more recent timestamp and the longer list of required paths. */
bool history_item_t::merge(const history_item_t &item)
{
    bool result = false;
    if (this->contents == item.contents) {
        this->creation_timestamp = std::max(this->creation_timestamp, item.creation_timestamp);
        if (this->required_paths.size() < item.required_paths.size()) {
            this->required_paths = item.required_paths;
        }
        result = true;
    }
    return result;
}

history_item_t::history_item_t(const wcstring &str) : contents(str), creation_timestamp(time(NULL))
{
}

history_item_t::history_item_t(const wcstring &str, time_t when, const path_list_t &paths) : contents(str), creation_timestamp(when), required_paths(paths)
{
}

bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type) const {
    switch (type) {
    
        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;
            
        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);
            
        default:
            sanity_lose();
            return false;
    }
}

/* Output our YAML to a file */
bool history_lru_node_t::write_yaml_to_file(FILE *f) const {
    std::string cmd = wcs2string(key);
    escape_yaml(cmd);
    if (fprintf(f, "- cmd: %s\n", cmd.c_str()) < 0)
        return false;
        
    if (fprintf(f, "   when: %ld\n", (long)timestamp) < 0)
        return false;
    
    if (! required_paths.empty()) {
        if (fputs("   paths:\n", f) < 0)
            return false;
            
        for (path_list_t::const_iterator iter = required_paths.begin(); iter != required_paths.end(); ++iter) {
            std::string path = wcs2string(*iter);
            escape_yaml(path);
            
            if (fprintf(f, "    - %s\n", path.c_str()) < 0)
                return false;
        }
    }
    return true;
}


// 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
static bool parse_timestamp(const char *str, time_t *out_when) {
    const char *cursor = str;
    /* Advance past spaces */
    while (*cursor == ' ')
        cursor++;
        
    /* Look for "when:" */
    size_t when_len = 5;
    if (strncmp(cursor, "when:", when_len) != 0)
        return false;
    cursor += when_len;
    
    /* Advance past spaces */
    while (*cursor == ' ')
        cursor++;
    
    /* Try to parse a timestamp. */
    long timestamp = 0;
    if (isdigit(*cursor) && (timestamp = strtol(cursor, NULL, 0)) > 0) {
        *out_when = (time_t)timestamp;
        return true;
    }
    return false;
}

// 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
static const char *next_line(const char *start, size_t length) {
    /* Handle the hopeless case */
    if (length < 1)
        return NULL;
    
    /* Get a pointer to the end, that we must not pass */
    const char * const end = start + length;
    
    /* Skip past the next newline */
    const char *nextline = (const char *)memchr(start, '\n', length);
    if (! nextline || nextline >= end) {
        return NULL;
    }
    /* Skip past the newline character itself */
    if (++nextline >= end) {
        return NULL;
    }
    
    /* 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);
    if (! next_newline) {
        return NULL;
    }
    
    /* Done */
    return nextline;
}

// 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
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)
{
    size_t cursor = *inout_cursor;
    size_t result = (size_t)(-1);
    while (cursor < mmap_length) {
        const char * const line_start = begin + cursor;
        
        /* Advance the cursor to the next line */
        const char *newline = (const char *)memchr(line_start, '\n', mmap_length - cursor);
        if (newline == NULL)
            break;
        
        /* 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;
        
        /* Skip very short lines to make one of the checks below easier */
        if (line_len < 3)
            continue;
        
        /* Try to be a little YAML compatible. Skip lines with leading %, ---, or ... */
        if (! memcmp(line_start, "%", 1) ||
            ! memcmp(line_start, "---", 3) ||
            ! memcmp(line_start, "...", 3))
            continue;

        /* 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. */
        if (cutoff_timestamp != 0) {
            /* 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.
             */
            const char * const end = begin + mmap_length;
            
            /* 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;
            time_t timestamp;
            const char *interior_line;
            for (interior_line = next_line(line_start, end - line_start);
                 interior_line != NULL && ! has_timestamp;
                 interior_line = next_line(interior_line, end - interior_line)) {
                 
                /* If the first character is not a space, it's not an interior line, so we're done */
                if (interior_line[0] != ' ')
                    break;
                    
                /* 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;
                
                /* Try parsing a timestamp from this line. If we succeed, the loop will break. */
                has_timestamp = parse_timestamp(interior_line, &timestamp);
            }
                 
            /* Skip this item if the timestamp is at or after our cutoff. */
            if (has_timestamp && timestamp >= cutoff_timestamp) {
                continue;
            }
        }

        /* We made it through the gauntlet. */
        result = line_start - begin;
        break;
    }
    *inout_cursor = cursor;
    return result;
}


// 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
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) {
    if (mmap_length == 0 || *inout_cursor >= mmap_length)
        return (size_t)(-1);
    
	const char *end = begin + mmap_length;    
	const char *pos;

	bool ignore_newline = false;
	bool do_push = true;
    bool all_done = false;

    size_t result = *inout_cursor;
	for( pos = begin + *inout_cursor; pos < end && ! all_done; pos++ )
	{

		if( do_push )
		{
			ignore_newline = (*pos == '#');
			do_push = false;
		}

		switch( *pos )
		{
			case '\\':
			{
				pos++;
				break;
			}

			case '\n':
			{
				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;
			}
		}
	}
    *inout_cursor = (pos - begin);
    return result;
}

// Returns the offset of the next item based on the given history type, or -1
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) {
    size_t result;
    switch (mmap_type) {
        case history_type_fish_2_0:
            result = offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp);
            break;
        
        case history_type_fish_1_x:
            result = offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp);
            break;
    
        default:
        case history_type_unknown:
            // Oh well
            result = (size_t)(-1);
            break;
    }
    return result;
}

history_t & history_t::history_with_name(const wcstring &name) {
    /* 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),
    unsaved_item_count(0),
    mmap_start(NULL),
    mmap_length(0),
    birth_timestamp(time(NULL)),
    save_timestamp(0),
    loaded_old(false)
{
    pthread_mutex_init(&lock, NULL);
}

history_t::~history_t()
{
    pthread_mutex_destroy(&lock);
}

void history_t::add(const history_item_t &item)
{
    scoped_lock locker(lock);
    
    /* Try merging with the last item */
    if (! new_items.empty() && new_items.back().merge(item)) {
        /* We merged, so we don't have to add anything */
    }
    else
    {
        /* We have to add a new item */
        new_items.push_back(item);
        unsaved_item_count++;
    }
    
    /* Prevent the first write from always triggering a save */
    time_t now = time(NULL);
    if (! save_timestamp)
        save_timestamp = now;
    
    /* This might be a good candidate for moving to a background thread */
    if((now > save_timestamp + SAVE_INTERVAL) || (unsaved_item_count >= SAVE_COUNT)) {
        time_profiler_t profiler("save_internal");
		this->save_internal();
    }

}

void history_t::add(const wcstring &str, const path_list_t &valid_paths)
{
    this->add(history_item_t(str, time(NULL), valid_paths));
}

void history_t::remove(const wcstring &str)
{
    /* Add to our list of deleted items */
    deleted_items.insert(str);
    
    /* Remove from our list of new items */
    for (std::vector<history_item_t>::iterator iter = new_items.begin(); iter != new_items.end();)
    {
        if (iter->str() == str) {
            iter = new_items.erase(iter);
        } else {
            iter++;
        }
    }
}

void history_t::get_string_representation(wcstring &result, const wcstring &separator)
{
    scoped_lock locker(lock);
    
    bool first = true;
    
    /* Append new items */
    for (std::vector<history_item_t>::const_reverse_iterator iter=new_items.rbegin(); iter < new_items.rend(); ++iter) {
        if (! first)
            result.append(separator);
        result.append(iter->str());
        first = false;
    }
    
    /* Append old items */
    load_old_if_needed();
    for (std::vector<size_t>::const_reverse_iterator iter = old_item_offsets.rbegin(); iter != old_item_offsets.rend(); ++iter) {        
        size_t offset = *iter;
        const history_item_t item = history_t::decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
        if (! first)
            result.append(separator);
        result.append(item.str());
        first = false;
    }
}

history_item_t history_t::item_at_index(size_t idx) {
    scoped_lock locker(lock);
    
    /* 0 is considered an invalid index */
    assert(idx > 0);
    idx--;
    
    /* idx=0 corresponds to last item in new_items */
    size_t new_item_count = new_items.size();
    if (idx < new_item_count) {
        return new_items.at(new_item_count - idx - 1);
    }
    
    /* Now look in our old items */
    idx -= new_item_count;
    load_old_if_needed();
    size_t old_item_count = old_item_offsets.size();
    if (idx < old_item_count) {
        /* idx=0 corresponds to last item in old_item_offsets */
        size_t offset = old_item_offsets.at(old_item_count - idx - 1);
        return history_t::decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
    }
    
    /* Index past the valid range, so return an empty history item */
    return history_item_t(wcstring(), 0);
}

/* 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. */
static size_t read_line(const char *base, size_t cursor, size_t len, std::string &result) {
    /* Locate the newline */
    assert(cursor <= len);
    const char *start = base + cursor;
    const char *newline = (char *)memchr(start, '\n', len - cursor);
    if (newline != NULL) {
        /* We found a newline. */
        result.assign(start, newline - start);
        
        /* Return the amount to advance the cursor; skip over the newline */
        return newline - start + 1;
    } else {
        /* We ran off the end */
        result.clear();
        return len - cursor;
    }
}

/* Trims leading spaces in the given string, returning how many there were */
static size_t trim_leading_spaces(std::string &str) {
    size_t i = 0, max = str.size();
    while (i < max && str[i] == ' ')
        i++;
    str.erase(0, i);
    return i;
}

static bool extract_prefix(std::string &key, std::string &value, const std::string &line) {
    size_t where = line.find(":");
    if (where != std::string::npos) {
        key = line.substr(0, where);
        
        // skip a space after the : if necessary
        size_t val_start = where + 1;
        if (val_start < line.size() && line.at(val_start) == ' ')
            val_start++;
        value = line.substr(val_start);
        
        unescape_yaml(key);
        unescape_yaml(value);
    }
    return where != std::string::npos;
}

/* Decode an item via the fish 2.0 format */
history_item_t history_t::decode_item_fish_2_0(const char *base, size_t len) {
    wcstring cmd;
    time_t when = 0;
    path_list_t paths;
    
    size_t indent = 0, cursor = 0;
    std::string key, value, line;
    
    /* Read the "- cmd:" line */
    size_t advance = read_line(base, cursor, len, line);
    trim_leading_spaces(line);
    if (! extract_prefix(key, value, line) || key != "- cmd")
        goto done;
    
    cursor += advance;
    cmd = str2wcstring(value);

    /* Read the remaining lines */
    for (;;) {
        /* Read a line */
        size_t advance = read_line(base, cursor, len, line);
        
        /* Count and trim leading spaces */
        size_t this_indent = trim_leading_spaces(line);
        if (indent == 0)
            indent = this_indent;
        
        if (this_indent == 0 || indent != this_indent)
            break;
        
        if (! extract_prefix(key, value, line))
            break;
            
        /* We are definitely going to consume this line */
        unescape_yaml(value);
        cursor += advance;
        
        if (key == "when") {
            /* Parse an int from the timestamp */
            long tmp = 0;
            if (sscanf(value.c_str(), "%ld", &tmp) > 0) {
                when = tmp;
            }
        } else if (key == "paths") {
            /* Read lines starting with " - " until we can't read any more */
            for (;;) {
                size_t advance = read_line(base, cursor, len, line);
                if (trim_leading_spaces(line) <= indent)
                    break;
                    
                if (strncmp(line.c_str(), "- ", 2))
                    break;
                    
                /* We're going to consume this line */
                cursor += advance;
                
                /* Skip the leading dash-space and then store this path it */
                line.erase(0, 2);
                unescape_yaml(line);
                paths.push_back(str2wcstring(line));
            }
        }
    }
done:
    return history_item_t(cmd, when, paths);
}

history_item_t history_t::decode_item(const char *base, size_t len, history_file_type_t type) {
    switch (type) {
        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"");
    }
}

/**
   Remove backslashes from all newlines. This makes a string from the
   history file better formated for on screen display.
*/
static wcstring history_unescape_newlines_fish_1_x( const wcstring &in_str )
{
    wcstring out;
	for (const wchar_t *in = in_str.c_str(); *in; in++)
	{
		if( *in == L'\\' )
		{
			if( *(in+1)!= L'\n')
			{
				out.push_back(*in);
			}
		}
		else
		{
			out.push_back(*in);
		}
	}
	return out;
}


/* Decode an item via the fish 1.x format. Adapted from fish 1.x's item_get(). */
history_item_t history_t::decode_item_fish_1_x(const char *begin, size_t length) {

    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;
    
    while( 1 )
    {
        wchar_t c;
        mbstate_t state;
        size_t res;

        memset( &state, 0, sizeof(state) );

        res = mbrtowc( &c, pos, end-pos, &state );

        if( res == (size_t)-1 )
        {
            pos++;
            continue;
        }
        else if( res == (size_t)-2 )
        {
            break;
        }
        else if( res == (size_t)0 )
        {
            pos++;
            continue;
        }
        pos += res;

        if( c == L'\n' )
        {
            if( timestamp_mode )
            {
                const wchar_t *time_string = out.c_str();
                while( *time_string && !iswdigit(*time_string))
                    time_string++;
                errno=0;

                if( *time_string )
                {
                    time_t tm;
                    wchar_t *end;

                    errno = 0;
                    tm = (time_t)wcstol( time_string, &end, 10 );

                    if( tm && !errno && !*end )
                    {
                        timestamp = tm;
                    }

                }

                out.clear();
                timestamp_mode = false;
                continue;
            }
            if( !was_backslash )
                break;
        }

        if( first_char )
        {
            if( c == L'#' )
                timestamp_mode = true;
        }

        first_char = false;

        out.push_back(c);

        was_backslash = ( (c == L'\\') && !was_backslash);

    }
    
    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 */
static history_file_type_t infer_file_type(const char *data, size_t len) {
    history_file_type_t result = history_type_unknown;
    if (len > 0) {
        /* Old fish started with a # */
        if (data[0] == '#') {
            result = history_type_fish_1_x;
        } else {
            /* Assume new fish */
            result = history_type_fish_2_0;
        }
    }
    return result;
}

void history_t::populate_from_mmap(void)
{
    mmap_type = infer_file_type(mmap_start, mmap_length);
    size_t cursor = 0;
    for (;;) {
        size_t offset = offset_of_next_item(mmap_start, mmap_length, mmap_type, &cursor, birth_timestamp);
        // If we get back -1, we're done
        if (offset == (size_t)(-1))
            break;

        // Remember this item
        old_item_offsets.push_back(offset);        
    }
}

// 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.
static bool map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len)
{
    bool result = false;
    wcstring filename = history_filename(name, L"");
    if (! filename.empty())
    {
        int fd;
		if((fd = wopen_cloexec(filename, O_RDONLY)) > 0)
		{
			off_t len = lseek( fd, 0, SEEK_END );
			if(len != (off_t)-1)
			{
				size_t mmap_length = (size_t)len;
				if(lseek(fd, 0, SEEK_SET) == 0)
				{
                    char *mmap_start;
					if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd, 0)) != MAP_FAILED)
					{
						result = true;
                        *out_map_start = mmap_start;
                        *out_map_len = mmap_length;
					}
				}
			}
			close( fd );
		}
    }
    return result;
}

bool history_t::load_old_if_needed(void)
{
    if (loaded_old) return true;
    loaded_old = true;
    
    
    // PCA not sure why signals were blocked here
	//signal_block();
    
	bool ok = false;    
    if (map_file(name, &mmap_start, &mmap_length)) {
        // Here we've mapped the file
        ok = true;
        time_profiler_t profiler("populate_from_mmap");
        this->populate_from_mmap();
    } 
    
	//signal_unblock();
    return ok;
}

void history_search_t::skip_matches(const wcstring_list_t &skips) {
    external_skips = skips;
    std::sort(external_skips.begin(), external_skips.end());
}

bool history_search_t::should_skip_match(const wcstring &str) const {
    return std::binary_search(external_skips.begin(), external_skips.end(), str);
}

bool history_search_t::go_forwards() {
    /* Pop the top index (if more than one) and return if we have any left */
    if (prev_matches.size() > 1) {
        prev_matches.pop_back();
        return true;
    }
    return false;
}

bool history_search_t::go_backwards() {
    /* Backwards means increasing our index */
    const size_t max_idx = (size_t)(-1);

    size_t idx = 0;
    if (! prev_matches.empty())
        idx = prev_matches.back().first;
    
    if (idx == max_idx)
        return false;
        
    while (++idx < max_idx) {
        const history_item_t item = history->item_at_index(idx);
        /* We're done if it's empty */
        if (item.empty()) {
            return false;
        }

        /* Look for a term that matches and that we haven't seen before */
        const wcstring &str = item.str();
        if (item.matches_search(term, search_type) && ! match_already_made(str) && ! should_skip_match(str)) {
            prev_matches.push_back(prev_match_t(idx, item));
            return true;
        }
    }
    return false;
}

/** Goes to the end (forwards) */
void history_search_t::go_to_end(void) {
    prev_matches.clear();
}

/** Returns if we are at the end, which is where we start. */
bool history_search_t::is_at_end(void) const {
    return prev_matches.empty();
}


/** Goes to the beginning (backwards) */
void history_search_t::go_to_beginning(void) {
    /* Just go backwards as far as we can */
    while (go_backwards())
        ;
}


history_item_t history_search_t::current_item() const {
    assert(! prev_matches.empty()); 
    return prev_matches.back().second;
}

wcstring history_search_t::current_string() const {
    history_item_t item = this->current_item();
    return item.str();
}

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) {
        if (iter->second.str() == match)
            return true;
    }
    return false;
}

static void replace_all(std::string &str, const char *needle, const char *replacement)
{
    size_t needle_len = strlen(needle), replacement_len = strlen(replacement);
    size_t offset = 0;
    while((offset = str.find(needle, offset)) != std::string::npos)
    {
        str.replace(offset, needle_len, replacement);
        offset += replacement_len;
    }
}

static void escape_yaml(std::string &str) {
    replace_all(str, "\\", "\\\\"); //replace one backslash with two
    replace_all(str, "\n", "\\n"); //replace newline with backslash + literal n
}

static void unescape_yaml(std::string &str) {
    bool prev_escape = false;
    for (size_t idx = 0; idx < str.size(); idx++) {
        char c = str.at(idx);
        if (prev_escape) {
            if (c == '\\') {
                /* Two backslashes in a row. Delete this one */
                str.erase(idx, 1);
                idx--;
            } else if (c == 'n') {
                /* Replace backslash + n with an actual newline */
                str.replace(idx - 1, 2, "\n");
                idx--;
            }
            prev_escape = false;
        } else {
            prev_escape = (c == '\\');
        }
    }
}

static wcstring history_filename(const wcstring &name, const wcstring &suffix)
{
    wcstring path;
    if (! path_get_config(path))
        return L"";
    
    wcstring result = path;
    result.append(L"/");
    result.append(name);
    result.append(L"_history");
    result.append(suffix);
    return result;
}

void history_t::clear_file_state()
{
    /* Erase everything we know about our file */
    if (mmap_start != NULL && mmap_start != MAP_FAILED) {
        munmap((void *)mmap_start, mmap_length);
    }
    mmap_start = NULL;
    mmap_length = 0;
    loaded_old = false;
    old_item_offsets.clear();
    save_timestamp=time(0);
}

void history_t::compact_new_items() {
    /* 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();
    while (idx--) {
        const history_item_t &item = new_items[idx];
        if (! seen.insert(item.contents).second) {
            // 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);
        }
    }
}

/** Save the specified mode to file */
void history_t::save_internal()
{
    /* This must be called while locked */
    ASSERT_IS_LOCKED(lock);
    
    /* Nothing to do if there's no new items */
    if (new_items.empty() && deleted_items.empty())
        return;
        
    /* Compact our new items so we don't have duplicates */
    this->compact_new_items();
    
	bool ok = true;
    
    wcstring tmp_name = history_filename(name, L".tmp");    
	if( ! tmp_name.empty() )
	{        
        /* Make an LRU cache to save only the last N elements */
        history_lru_cache_t lru(HISTORY_SAVE_MAX);
        
        /* Insert old items in, from old to new. Merge them with our new items, inserting items with earlier timestamps first. */
        std::vector<history_item_t>::const_iterator new_item_iter = new_items.begin();
        
        /* 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;
        if (map_file(name, &local_mmap_start, &local_mmap_size)) {
            const history_file_type_t local_mmap_type = infer_file_type(local_mmap_start, local_mmap_size);
            size_t cursor = 0;
            for (;;) {
                size_t offset = offset_of_next_item(local_mmap_start, local_mmap_size, local_mmap_type, &cursor, 0);
                /* If we get back -1, we're done */
                if (offset == (size_t)(-1))
                    break;

                /* Try decoding an old item */
                const history_item_t old_item = history_t::decode_item(local_mmap_start + offset, local_mmap_size - offset, local_mmap_type);
                if (old_item.empty() || is_deleted(old_item))
                {
//                    debug(0, L"Item is deleted : %s\n", old_item.str().c_str());
                    continue;
                }
                /* 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. */
                for (; new_item_iter != new_items.end(); ++new_item_iter) {
                    if (new_item_iter->timestamp() < old_item.timestamp()) {
                        /* This "new item" is in fact older. */
                        lru.add_item(*new_item_iter);
                    } else {
                        /* The new item is not older. */
                        break;
                    }
                }
                
                /* Now add this old item */
                lru.add_item(old_item);
            }
            munmap((void *)local_mmap_start, local_mmap_size);
        }
        
        /* Insert any remaining new items */
        for (; new_item_iter != new_items.end(); ++new_item_iter)
        {
            lru.add_item(*new_item_iter);
        }
                
        signal_block();
    
        FILE *out;
		if( (out=wfopen( tmp_name, "w" ) ) )
		{
            /* Write them out */
            for (history_lru_cache_t::iterator iter = lru.begin(); iter != lru.end(); ++iter) {
                const history_lru_node_t *node = *iter;
                if (! node->write_yaml_to_file(out)) {
                    ok = false;
                    break;
                }
            }
            
			if( fclose( out ) || !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);
			}
		}
        
        signal_unblock();
        
        /* Make sure we clear all nodes, since this doesn't happen automatically */
        lru.evict_all_nodes();
        
        /* We've saved everything, so we have no more unsaved items */
        unsaved_item_count = 0;
	}	

	if( ok )
	{
		/* Our history has been written to the file, so clear our state so we can re-reference the file. */
		this->clear_file_state();
	}
}

void history_t::save(void)
{
    scoped_lock locker(lock);
    this->save_internal();
}

void history_t::clear(void)
{
    scoped_lock locker(lock);
    new_items.clear();
    deleted_items.clear();
    unsaved_item_count = 0;
    old_item_offsets.clear();
    wcstring filename = history_filename(name, L"");
    if (! filename.empty())
        wunlink(filename);
    this->clear_file_state();
    
}

bool history_t::is_empty(void)
{
    bool result = false;
    scoped_lock locker(lock);
    if (new_items.empty())
    {
        load_old_if_needed();
        result = old_item_offsets.empty();
    }
    return result;
}

/* 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;
    
    /* Very naive tests! Skip export; probably should skip others. */
    const char * const ignore_prefixes[] = {
        "export ",
        "#"
    };
    
    for (size_t i=0; i < sizeof ignore_prefixes / sizeof *ignore_prefixes; i++) {
        const char *prefix = ignore_prefixes[i];
        if (! line.compare(0, strlen(prefix), prefix)) {
            return false;
        }
    }
    
    /* Skip lines with backticks */
    if (line.find('`') != std::string::npos)
        return false;
    
    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;
    for (;;) {
        line.clear();
        bool success = false, has_newline = false;
        
        /* Loop until we've read a line */
        do {
            char buff[128];
            success = !! fgets(buff, sizeof buff, stream);
            if (success) {
                /* Skip the newline */
                char *newline = strchr(buff, '\n');
                if (newline) *newline = '\0';
                has_newline = (newline != NULL);
                
                /* Append what we've got */
                line.append(buff);
            }
        } while (success && ! has_newline);
        
        /* Maybe add this line */
        if (should_import_bash_history_line(line)) {
            this->add(str2wcstring(line));
        }
        
        if (line.empty())
            break;
    }
}

void history_init()
{
}


void history_destroy()
{
    /* Save all histories */
    for (std::map<wcstring, history_t *>::iterator iter = histories.begin(); iter != histories.end(); ++iter) {
        iter->second->save();
    }
}


void history_sanity_check()
{
	/*
	  No sanity checking implemented yet...
	*/
}

int file_detection_context_t::perform_file_detection(bool test_all) {
    ASSERT_IS_BACKGROUND_THREAD();
    valid_paths.clear();
    int result = 1;
    for (path_list_t::const_iterator iter = potential_paths.begin(); iter != potential_paths.end(); ++iter) {    
        if (path_is_valid(*iter, working_directory)) {
            /* Push the original (possibly relative) path */
            valid_paths.push_back(*iter);
        } else {
            /* Not a valid path */
            result = 0;
            if (! test_all)
                break;
        }
    }
    return result;
}

bool file_detection_context_t::paths_are_valid(const path_list_t &paths) {
    this->potential_paths = paths;
    return perform_file_detection(false) > 0;
}

file_detection_context_t::file_detection_context_t(history_t *hist, const wcstring &cmd) :
    history(hist),
    command(cmd),
    when(time(NULL)),
    working_directory(get_working_directory())
{
}

static int threaded_perform_file_detection(file_detection_context_t *ctx) {
    ASSERT_IS_BACKGROUND_THREAD();
    assert(ctx != NULL);
    return ctx->perform_file_detection(true /* test all */);
}

static void perform_file_detection_done(file_detection_context_t *ctx, int success) {
    /* Now that file detection is done, create the history item */
    ctx->history->add(ctx->command, ctx->valid_paths);
    
    /* Done with the context. */
    delete ctx;
}

static bool string_could_be_path(const wcstring &potential_path) {
    // Assume that things with leading dashes aren't paths
    if (potential_path.empty() || potential_path.at(0) == L'-')
        return false;
    return true;
}

void history_t::add_with_file_detection(const wcstring &str)
{
    ASSERT_IS_MAIN_THREAD();
    path_list_t potential_paths;
    
    tokenizer tokenizer;
    for( tok_init( &tokenizer, str.c_str(), TOK_SQUASH_ERRORS );
        tok_has_next( &tokenizer );
        tok_next( &tokenizer ) )
    {
        int type = tok_last_type( &tokenizer );
        if (type == TOK_STRING) {
            const wchar_t *token_cstr = tok_last(&tokenizer);
            if (token_cstr) {
                wcstring potential_path = token_cstr;
                if (unescape_string(potential_path, false) && string_could_be_path(potential_path)) {
                    potential_paths.push_back(potential_path);
                }
            }
        }
    }
    tok_destroy(&tokenizer);
    
    if (! potential_paths.empty()) {
        /* We have some paths. Make a context. */
        file_detection_context_t *context = new file_detection_context_t(this, str);
                        
        /* Store the potential paths. Reverse them to put them in the same order as in the command. */
        context->potential_paths.swap(potential_paths);
        iothread_perform(threaded_perform_file_detection, perform_file_detection_done, context);
    }
}

bool history_t::is_deleted(const history_item_t &item) const
{
    return deleted_items.count(item.str()) > 0;
}