Annotation of OpenXM_contrib2/asir2000/parse/history.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/parse/history.c,v 1.1.1.1 1999/11/10 08:12:34 noro Exp $ */
2: #include <stdio.h>
3: #include <string.h>
4: #include <malloc.h>
5:
6: char *prev_hist(void), *next_hist(void), *search_hist(char *p);
7: void write_hist(char *fname), read_hist(char *fname);
8: void add_hist(char *p), init_hist(int max);
9: void reset_traverse(void);
10:
11: static char **hist_table;
12: static int hist_full, hist_current, hist_length;
13: static int hist_traverse, hist_traverse_index;
14: #define CURRENT (hist_table[hist_current])
15:
16: void init_hist(int max)
17: {
18: hist_length = max;
19: hist_table = (char **)calloc(hist_length,sizeof(char *));
20: hist_full = 0;
21: hist_current = -1;
22: }
23:
24: void reset_traverse(void)
25: {
26: hist_traverse = 0;
27: }
28:
29: void add_hist(char *p)
30: {
31: if ( hist_current >= 0 && CURRENT && !strcmp(p,CURRENT) )
32: return;
33: hist_current++;
34: if ( hist_current == hist_length ) {
35: hist_current = 0;
36: hist_full = 1;
37: }
38: CURRENT = (char *)realloc(CURRENT,strlen(p)+1);
39: strcpy(CURRENT,p);
40: }
41:
42: char *search_hist(char *p)
43: {
44: int len,slen,index,i;
45:
46: if ( *p == '!' ) {
47: if ( hist_current < 0 )
48: return 0;
49: else
50: return CURRENT;
51: }
52: slen = strlen(p);
53: len = hist_full ? hist_length : hist_current+1;
54: index = hist_current;
55: for ( i = len; i; i--, index-- ) {
56: if ( index < 0 )
57: index += hist_length;
58: if ( !strncmp(p,hist_table[index],slen) )
59: return hist_table[index];
60: }
61: return 0;
62: }
63:
64: void read_hist(char *fname)
65: {
66: int l;
67: char buf[BUFSIZ];
68: FILE *fp;
69:
70: if ( !(fp = fopen(fname,"rb")) )
71: return;
72: while ( fgets(buf,BUFSIZ-1,fp) ) {
73: l = strlen(buf);
74: if ( buf[l-1] == '\n' )
75: buf[l-1] = 0;
76: add_hist(buf);
77: }
78: fclose(fp);
79: }
80:
81: void write_hist(char *fname)
82: {
83: int len,index,i;
84: FILE *fp;
85:
86: if ( !(fp = fopen(fname,"wb")) )
87: return;
88: len = hist_full ? hist_length : hist_current+1;
89: index = hist_full ? hist_current+1 : 0;
90: for ( i = 0; i < len; i++, index++ ) {
91: if ( index >= hist_length )
92: index -= hist_length;
93: fprintf(fp,"%s\n",hist_table[index]);
94: }
95: fclose(fp);
96: }
97:
98: char *next_hist(void) {
99: if ( !hist_traverse || hist_traverse_index == hist_current )
100: return 0;
101: else {
102: hist_traverse_index = (hist_traverse_index+1) % hist_length;
103: return hist_table[hist_traverse_index];
104: }
105: }
106:
107: char *prev_hist(void) {
108: if ( !hist_traverse ) {
109: if ( hist_current >= 0 ) {
110: hist_traverse = 1;
111: hist_traverse_index = hist_current;
112: return hist_table[hist_traverse_index];
113: } else
114: return 0;
115: } else {
116: hist_traverse_index--;
117: if ( hist_traverse_index < 0 )
118: if ( hist_full )
119: hist_traverse_index += hist_length;
120: else
121: hist_traverse_index = 0;
122: return hist_table[hist_traverse_index];
123: }
124: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>