aboutsummaryrefslogtreecommitdiffhomepage
path: root/history.c
blob: cb595d7e22c1885a977b2e813a3f11085246d667 (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
/** \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 "fallback.h"
#include "util.h"

#include "wutil.h"
#include "history.h"
#include "common.h"
#include "reader.h"
#include "env.h"
#include "sanity.h"
#include "signal.h"
#include "halloc.h"
#include "halloc_util.h"

/*
  The history is implemented using a linked list. Searches are done
  using linear search. 
*/


/**
   A struct describing the state of the interactive history
   list. Multiple states can be created. Use \c history_set_mode() to
   change between history contexts.
*/
typedef struct
{
	/**
	   Number of entries
	*/
	int count;

	/**
	   Last history item
	*/
	ll_node_t *last;

	/**
	   The last item loaded from file
	*/
	ll_node_t *last_loaded;

	/**
	   Set to one if the file containing the saved history has been loaded
	*/
	int is_loaded;
}
	history_data_t;


static ll_node_t /** Last item in history */ *history_last=0, /** Current search position*/ *history_current =0;

/**
   We only try to save the part of the history that was written in
   this session. This pointer keeps track of where the cutoff is.

   The reason for wanting to do this is so that when two concurrent
   fish sessions have exited, the latter one to exit won't push the
   added history items of the prior session to the top of the history.
*/
static ll_node_t *last_loaded=0;

/**
  past_end is a kludge. It is set when the current history item is
  unset, i.e. a new entry is being typed.
*/
static int past_end =1;

/**
   Items in history
*/
static int history_count=0;

/**
   The name of the current history list. The name is used to switch
   between history lists for different commands as sell as for
   deciding the name of the file to save the history in.
*/
static wchar_t *mode_name;

/**
   Hash table for storing all the history lists.
*/
static hash_table_t history_table;

/**
   Flag, set to 1 once the history file has been loaded
*/
static int is_loaded=0;

static ll_node_t *unused = 0;

/**
   Create a new ll_node_t struct. The struct is allocated using
   halloc, and will be automatically free'd on shutdown, but not
   before.
*/
static ll_node_t *history_create_node()
{
	ll_node_t *res;
	
	if( !unused )
	{
		return halloc( global_context, sizeof( ll_node_t ) );
	}

	res = unused;
	unused = unused->next;
	return res;
	
}

/**
   Return a ll_node_t struct to the pool of unused such structs, so
   that it can later be reused by a call to history_create_node().
*/
static void history_free_node( ll_node_t *n )
{
	n->next = unused;
	unused = n;
}

/**
   Add backslashes to all newlines, so that the returning string is
   suitable for writing to the history file. The memory for the return
   value will be reused by subsequent calls to this function.
*/
static wchar_t *history_escape_newlines( wchar_t *in )
{
	static string_buffer_t *out = 0;
	if( !out )
	{
		out = sb_halloc( global_context );
		if( !out )
		{
			DIE_MEM();
		}
	}
	else
	{
		sb_clear( out );
	}
	for( ; *in; in++ )
	{
		if( *in == L'\\' )
		{
			sb_append_char( out, *in );
			if( *(in+1) )
			{
				in++;
				sb_append_char( out, *in );
			}
			else
			{
				/*
				  This is a weird special case. When we are trying to
				  save a string that ends with a backslash, we need to
				  handle it specially, otherwise this command would be
				  combined with the one following it. We hack around
				  this by adding an additional newline.
				*/
				sb_append_char( out, L'\n' );
			}
			
		}
		else if( *in == L'\n' )
		{
			sb_append_char( out, L'\\' );
			sb_append_char( out, *in );
		}
		else
		{
			sb_append_char( out, *in );
		}
		
	}
	return (wchar_t *)out->buff;
}

/**
   Remove backslashes from all newlines. This makes a string from the
   history file better formated for on screen display. The memory for
   the return value will be reused by subsequent calls to this
   function.
*/
static wchar_t *history_unescape_newlines( wchar_t *in )
{
	static string_buffer_t *out = 0;
	if( !out )
	{
		out = sb_halloc( global_context );
		if( !out )
		{
			DIE_MEM();
		}
	}
	else
	{
		sb_clear( out );
	}
	for( ; *in; in++ )
	{
		if( *in == L'\\' )
		{
			if( *(in+1)!= L'\n')
			{
				sb_append_char( out, *in );
			}
		}
		else
		{
			sb_append_char( out, *in );
		}
		
	}
	return (wchar_t *)out->buff;
}

/**
   Read a complete histor entry from the specified FILE.
*/
static wchar_t *history_load_entry( FILE *in )
{
	int was_backslash = 0;	
	static string_buffer_t *out = 0;
	int first_char = 1;	
	int ignore = 0;
	
	if( !out )
	{
		out = sb_halloc( global_context );
		if( !out )
		{
			DIE_MEM();
		}
	}
	else
	{
		sb_clear( out );
	}
	
	while( 1 )
	{
		wint_t c;
		
		c = getwc( in );
	
	
		if( errno == EILSEQ )
		{
			getc( in );
			continue;
		}

		if( c == WEOF )
			break;


		if( c == L'\n' )
		{
			if( ignore )
			{
				ignore = 0;
				continue;
			}
			if( !was_backslash )
				break;
		}
		
		if( first_char )
		{
			if( c == L'#' ) 
				ignore = 1;
		}
		
		first_char = 0;
		
		if( !ignore )
			sb_append_char( out, c );
	
		was_backslash = ( (c == L'\\') && !was_backslash);
				
	}

	return (wchar_t *)out->buff;
}


/**
   Load history from file
*/
static int history_load()
{
	wchar_t *fn;
	wchar_t *buff=0;
	FILE *in_stream;
	hash_table_t used;
	int res = 0;
	
	if( !mode_name )
	{
		return -1;
	}	

	is_loaded = 1;
		
	signal_block();
	hash_init2( &used, 
				&hash_wcs_func,
				&hash_wcs_cmp,
				4096 );

	fn = wcsdupcat2( env_get(L"HOME"), L"/.", mode_name, L"_history", (void *)0 );
	
	in_stream = wfopen( fn, "r" );
	
	if( in_stream != 0 )
	{
		while( !feof( in_stream ) )
		{
			buff = history_unescape_newlines(history_load_entry( in_stream ));
			/*
			  We do not call history_add here, since that would make
			  history_load() take quadratic time, and may be
			  unacceptably on slow systems with a long history file.

			  Use a hashtable to check for duplicates instead.
			*/
			if( wcslen(buff) && !hash_get( &used,
										   buff ) )
			{
				history_count++;		
				
				history_current = history_create_node();
				
				history_current->data=halloc_wcsdup( global_context, buff );

				hash_put( &used,
						  history_current->data, 
						  L"" );
				
				history_current->next=0;
				history_current->prev = history_last;
				if( history_last != 0 )
				{
					history_last->next = history_current;
				}
				history_last = history_current;
			}
		}
		
		fclose( in_stream );
	}
	else
	{
		if( errno != ENOENT )
		{
			debug( 1, _(L"The following non-fatal error occurred while reading command history from \'%ls\':"), mode_name );
			wperror( L"fopen" );
			res = -1;
			
		}
		
	}	

	
	hash_destroy( &used );
	
	free( fn );
	last_loaded = history_last;
	signal_unblock();
	return res;
	
}

void history_init()
{
	hash_init( &history_table,
			   &hash_wcs_func,
			   &hash_wcs_cmp );
}

/**
   Make sure the current history list is in the history_table.
*/
static void history_to_hash()
{
	history_data_t *d;
	
	if( !mode_name )
		return;
	

	d = (history_data_t *)hash_get( &history_table, 
								  mode_name );
	
	if( !d )
		d = malloc( sizeof(history_data_t));
	d->last=history_last;
	d->last_loaded=last_loaded;
	d->count=history_count;
	d->is_loaded = is_loaded;
	
	hash_put( &history_table, 
			  mode_name,
			  d );	
	
}


void history_set_mode( wchar_t *name )
{
	history_data_t *curr;
	
	if( mode_name )
	{		
		/*
		  Move the current history to the hashtable
		*/
		history_to_hash();		
	}
	
	/*
	  See if the new history already exists
	*/
	curr = (history_data_t *)hash_get( &history_table,
									 name );
	if( curr )
	{
		/*
		  Yes. Restore it.
		*/
		mode_name = (wchar_t *)hash_get_key( &history_table,
											 name );
		history_current = history_last = curr->last;
		last_loaded = curr->last_loaded;
		history_count = curr->count;
		is_loaded = curr->is_loaded;
	}
	else
	{
		/*
		  Nope. Create a new history list.
		*/
		history_count=0;
		history_last = history_current = last_loaded=0;
		mode_name = wcsdup( name );
		is_loaded = 0;
	}
	
	past_end=1;
	
}

/**
   Print history node 
*/
static void history_save_node( ll_node_t *n, FILE *out )
{
	wchar_t *escaped;
	if( n==0 )
		return;
	history_save_node( n->prev, out );

	escaped = history_escape_newlines( (wchar_t *)(n->data) );
	fwprintf(out, L"#\n%ls\n", escaped );
}

/**
   Merge the process history with the history on file, write to
   disc. The merging operation is done so that two concurrently
   running shells wont erase each others history.
*/
static void history_save()
{
	wchar_t *fn;
	FILE *out_stream;
	/* First we save this sessions history in local variables */
	ll_node_t *real_pos = history_last, *real_first = last_loaded;

	if( !is_loaded )
		return;
	
	if( !real_first )
	{
		real_first = history_current;
		while( real_first->prev )
			real_first = real_first->prev;
	}

	if( real_pos == real_first )
		return;
	
	/* Then we load the history from file into the global pointers */
	history_last=history_current=last_loaded=0;
	history_count=0;
	past_end=1;

	if( !history_load() )
	{
		
		if( real_pos != 0 )
		{
			/* 
			   Rewind the session history to the first item which was
			   added in this session
			*/
			while( (real_pos->prev != 0) && (real_pos->prev != real_first) )
			{
				real_pos = real_pos->prev;
			}
			
			/* Free old history entries */
			ll_node_t *kill_node_t = real_pos->prev;
			while( kill_node_t != 0 )
			{
				ll_node_t *tmp = kill_node_t;
				kill_node_t = kill_node_t->prev;
				history_free_node( tmp );		
			}	
			
			/* 
			   Add all the history entries from this session to the global
			   history, free the old version
			*/
			while( real_pos != 0 )
			{
				ll_node_t *next = real_pos->next;
				history_add( (wchar_t *)real_pos->data );
				
				history_free_node( real_pos );
				real_pos = next;
			}
		}
		
		/* Save the global history */
		{
			fn = wcsdupcat2( env_get(L"HOME"), L"/.", mode_name, L"_history", (void *)0 );
			
			out_stream = wfopen( fn, "w" );
			if( out_stream )
			{
				history_save_node( history_last, out_stream );
				if( fclose( out_stream ) )
				{
					debug( 1, L"The following non-fatal error occurred while saving command history to \'%ls\':", fn );
					wperror( L"fopen" );
				}			
			}
			else
			{
				debug( 1, L"The following non-fatal error occurred while saving command history to \'%ls\':", fn );
				wperror( L"fopen" );
			}
			free( fn );	
		}
	}
	
	
}

/**
   Save the specified mode to file
*/

static void history_destroy_mode( void *name, void *link )
{
	mode_name = (wchar_t *)name;
	history_data_t *d = (history_data_t *)link;
	history_last = history_current = d->last;
	last_loaded = d->last_loaded;
	history_count = d->count;
	past_end=1;
	
//	fwprintf( stderr, L"Destroy history mode \'%ls\'\n", mode_name );
	
	if( history_last )
	{
		history_save();	
		history_current = history_last;
		while( history_current != 0 )
		{
			ll_node_t *tmp = history_current;
			history_current = history_current->prev;
			history_free_node( tmp );		
		}	
	}
	free( d );
	free( mode_name );
}

void history_destroy()
{
	
	/**
	   Make sure current mode is in table
	*/
	history_to_hash();		
		
	/**
	   Save all modes in table
	*/
	hash_foreach( &history_table, 
				  &history_destroy_mode );

	hash_destroy( &history_table );
}



/**
   Internal search function
*/
static ll_node_t *history_find( ll_node_t *n, const wchar_t *s )
{
	if( !is_loaded )
	{
		if( history_load() )
		{
			return 0;
		}
	}
	

	if( n == 0 )
		return 0;
	
	if( wcscmp( s, (wchar_t *)n->data ) == 0 )
	{
		return n;		
	}
	else
		return history_find( n->prev, s );
}


void history_add( const wchar_t *str )
{
	ll_node_t *old_node;

	if( !is_loaded )
	{
		if( history_load() )
		{
			return;
		}
	}
	

	if( wcslen( str ) == 0 )
		return;
	
	past_end=1;

	old_node = history_find( history_last, str );

	if( old_node == 0 )
	{
		history_count++;		
		history_current = history_create_node();
		history_current->data=halloc_wcsdup( global_context, str );
	}
	else
	{
		if( old_node == last_loaded )
		{
			last_loaded = last_loaded->prev;
		}
	
		history_current = old_node;
		if( old_node == history_last )
		{
			return;
		}

		if( old_node->prev != 0 )
			old_node->prev->next = old_node->next;
		if( old_node->next != 0 )
			old_node->next->prev = old_node->prev;
	}
	history_current->next=0;
	history_current->prev = history_last;
	if( history_last != 0 )
	{
		history_last->next = history_current;
	}
	history_last = history_current;
}

/**
   This function tests if the search string is a match for the given string
*/
static int history_test( const wchar_t *needle, const wchar_t *haystack )
{

/*
	return wcsncmp( haystack, needle, wcslen(needle) )==0;
*/
	return !!wcsstr( haystack, needle );
}

const wchar_t *history_prev_match( const wchar_t *str )
{
	if( !is_loaded )
	{
		if( history_load() )
		{
			return 0;
		}
	}
	

	if( history_current == 0 )
		return str;
	
	if( history_current->prev == 0 )
	{
		return str;
	}
	if( past_end )
		past_end = 0;
	else
		history_current = history_current->prev;

	if( history_test( str, history_current->data) )
		return (wchar_t *)history_current->data;
	else
		return history_prev_match( str );
}


const wchar_t *history_next_match( const wchar_t *str)
{
	if( !is_loaded )
	{
		if( history_load() )
		{
			return 0;
		}
	}
	

	if( history_current == 0 )
		return str;

	if( history_current->next == 0 )
	{
		past_end = 1;
		return str;
	}
	history_current = history_current->next;
	
	if( history_test( str, history_current->data ) )
		return (wchar_t *)history_current->data;
	else
		return history_next_match( str );	
}

void history_reset()
{
	history_current = history_last;
}

/**
   Move to first history item
*/
void history_first()
{
	if( is_loaded )
		while( history_current->prev )
			history_current = history_current->prev;
	
}

wchar_t *history_get( int idx )
{
	ll_node_t *n;
	int i;
	
	if( !is_loaded )
	{
		if( history_load() )
		{
			return 0;
		}
	}
	
	n = history_last;
	
	if( idx<0)
	{
		debug( 1, L"Tried to access negative history index %d", idx );
		return 0;
	}
	
	for( i=0; i<idx; i++ )
	{
		if( !n )
			break;
		n=n->prev;
	}
	return n?n->data:0;
}


void history_sanity_check()
{	
	return;
	
	if( history_current != 0 )
	{
		int i;
		int history_ok = 1;
		int found_current = 0;
		
		validate_pointer( history_last, L"History root", 1);
		
		ll_node_t *tmp = history_last;
		for( i=0; i<history_count; i++ )
		{
			found_current |= tmp == history_current;

			if( tmp == 0 )
			{
				history_ok = 0;
				debug( 1, L"History items missing" );
				break;
			}

			if( (tmp->prev != 0) && (tmp->prev->next != tmp ) )
			{
				history_ok = 0;
				debug( 1, L"History items order is inconsistent" );
				break;
			}
			
			validate_pointer( tmp->data, L"History data", 1);
			if( tmp->data == 0 )
			{
				history_ok = 0;
				debug( 1, L"Empty history item" );
				break;
			}
			validate_pointer( tmp->prev, L"History node", 1);
			tmp = tmp->prev;
		}
		if( tmp != 0 )
		{
			history_ok = 0;
			debug( 1, L"History list too long" );
		}

		if( (i!= history_count )|| (!found_current))
		{
			debug( 1, L"No history item selected" );
			history_ok=0;
		}
		
		if( !history_ok )
		{
			sanity_lose();			
		}
	}	
}