1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: hist.c 807 2007-11-09 01:21:33Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2014 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
20 #include "../pith/headers.h"
21 #include "../pith/conf.h"
22 #include "../pith/hist.h"
26 init_hist(HISTORY_S
**history
, int histsize
)
34 l
= sizeof(**history
) + histsize
* sizeof(ONE_HIST_S
);
35 *history
= (HISTORY_S
*) fs_get(l
);
36 memset(*history
, 0, l
);
37 (*history
)->histsize
= histsize
;
38 (*history
)->origindex
= histsize
- 1;
39 add_to_histlist(history
);
42 (*history
)->curindex
= (*history
)->origindex
;
47 free_hist(HISTORY_S
**history
)
51 if(history
&& *history
){
53 for(i
= 0; i
< (*history
)->histsize
; i
++)
54 if((*history
)->hist
[i
] && (*history
)->hist
[i
]->str
)
55 fs_give((void **) &(*history
)->hist
[i
]->str
);
57 fs_give((void **) history
);
63 get_prev_hist(HISTORY_S
*history
, char *savethis
, unsigned saveflags
, void *cntxt
)
68 if(!(history
&& history
->histsize
> 0))
71 nextcurindex
= (history
->curindex
+ 1) % history
->histsize
;
73 /* already at start of history */
74 if(nextcurindex
== history
->origindex
75 || !(history
->hist
[nextcurindex
] && history
->hist
[nextcurindex
]->str
76 && history
->hist
[nextcurindex
]->str
[0]))
79 /* save what user typed */
80 if(history
->curindex
== history
->origindex
){
84 if(!history
->hist
[history
->origindex
]){
85 history
->hist
[history
->origindex
] = (ONE_HIST_S
*) fs_get(sizeof(ONE_HIST_S
));
86 memset(history
->hist
[history
->origindex
], 0, sizeof(ONE_HIST_S
));
89 if(history
->hist
[history
->origindex
]->str
){
90 if(strlen(history
->hist
[history
->origindex
]->str
) < (l
=strlen(savethis
)))
91 fs_resize((void **) &history
->hist
[history
->origindex
]->str
, l
+1);
93 strncpy(history
->hist
[history
->origindex
]->str
, savethis
, l
+1);
94 history
->hist
[history
->origindex
]->str
[l
] = '\0';
97 history
->hist
[history
->origindex
]->str
= cpystr(savethis
);
99 history
->hist
[history
->origindex
]->flags
= saveflags
;
101 history
->hist
[history
->origindex
]->cntxt
= cntxt
;
104 history
->curindex
= nextcurindex
;
106 return((history
->hist
[history
->curindex
] && history
->hist
[history
->curindex
]->str
)
107 ? history
->hist
[history
->curindex
]->str
: NULL
);
112 get_next_hist(HISTORY_S
*history
, char *savethis
, unsigned saveflags
, void *cntxt
)
114 if(!(history
&& history
->histsize
> 0))
117 /* already at end (most recent) of history */
118 if(history
->curindex
== history
->origindex
)
121 history
->curindex
= (history
->curindex
+ history
->histsize
- 1) % history
->histsize
;
123 return((history
->hist
[history
->curindex
] && history
->hist
[history
->curindex
]->str
)
124 ? history
->hist
[history
->curindex
]->str
: NULL
);
129 save_hist(HISTORY_S
*history
, char *savethis
, unsigned saveflags
, void *cntxt
)
134 if(!(history
&& history
->histsize
> 0))
137 plusone
= (history
->origindex
+ 1) % history
->histsize
;
139 if(!history
->hist
[history
->origindex
]){
140 history
->hist
[history
->origindex
] = (ONE_HIST_S
*) fs_get(sizeof(ONE_HIST_S
));
141 memset(history
->hist
[history
->origindex
], 0, sizeof(ONE_HIST_S
));
144 if(savethis
&& savethis
[0]
145 && (!history
->hist
[history
->origindex
]->str
146 || strcmp(history
->hist
[history
->origindex
]->str
, savethis
)
147 || history
->hist
[history
->origindex
]->flags
!= saveflags
148 || history
->hist
[history
->origindex
]->cntxt
!= cntxt
)
149 && !(history
->hist
[plusone
] && history
->hist
[plusone
]->str
150 && !strcmp(history
->hist
[plusone
]->str
, savethis
)
151 && history
->hist
[history
->origindex
]->flags
== saveflags
152 && history
->hist
[history
->origindex
]->cntxt
== cntxt
)){
153 if(history
->hist
[history
->origindex
]->str
){
154 if(strlen(history
->hist
[history
->origindex
]->str
) < (l
=strlen(savethis
)))
155 fs_resize((void **) &history
->hist
[history
->origindex
]->str
, l
+1);
157 strncpy(history
->hist
[history
->origindex
]->str
, savethis
, l
+1);
158 history
->hist
[history
->origindex
]->str
[l
] = '\0';
161 history
->hist
[history
->origindex
]->str
= cpystr(savethis
);
164 history
->hist
[history
->origindex
]->flags
= saveflags
;
165 history
->hist
[history
->origindex
]->cntxt
= cntxt
;
167 history
->origindex
= (history
->origindex
+ history
->histsize
- 1) % history
->histsize
;
168 if(history
->hist
[history
->origindex
] && history
->hist
[history
->origindex
]->str
)
169 history
->hist
[history
->origindex
]->str
[0] = '\0';
175 * Returns count of items entered into history.
178 items_in_hist(HISTORY_S
*history
)
182 if(history
&& history
->histsize
> 0)
183 for(i
= 0; i
< history
->histsize
; i
++)
184 if(history
->hist
[i
] && history
->hist
[i
]->str
)
191 static HISTORY_S
**histlist
[100];
194 add_to_histlist(HISTORY_S
**history
)
199 /* find empty slot */
200 for(i
= 0; i
< 100; i
++)
205 histlist
[i
] = history
;
215 for(i
= 0; i
< 100; i
++)
217 free_hist(histlist
[i
]);