* Create help for explaining how encrypted password file support
[alpine.git] / pith / hist.c
blob2ed00af207fa1e2b2fb02b5849a886ed4a1dc391
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: hist.c 807 2007-11-09 01:21:33Z hubert@u.washington.edu $";
3 #endif
5 /*
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"
25 void
26 init_hist(HISTORY_S **history, int histsize)
28 size_t l;
30 if(!history)
31 return;
33 if(!*history){
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;
46 void
47 free_hist(HISTORY_S **history)
49 int i;
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);
62 char *
63 get_prev_hist(HISTORY_S *history, char *savethis, unsigned saveflags, void *cntxt)
65 int nextcurindex;
66 size_t l;
68 if(!(history && history->histsize > 0))
69 return NULL;
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]))
77 return NULL;
79 /* save what user typed */
80 if(history->curindex == history->origindex){
81 if(!savethis)
82 savethis = "";
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';
96 else
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);
111 char *
112 get_next_hist(HISTORY_S *history, char *savethis, unsigned saveflags, void *cntxt)
114 if(!(history && history->histsize > 0))
115 return NULL;
117 /* already at end (most recent) of history */
118 if(history->curindex == history->origindex)
119 return NULL;
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);
128 void
129 save_hist(HISTORY_S *history, char *savethis, unsigned saveflags, void *cntxt)
131 size_t l;
132 int plusone;
134 if(!(history && history->histsize > 0))
135 return;
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';
160 else{
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)
180 int i, cnt = 0;
182 if(history && history->histsize > 0)
183 for(i = 0; i < history->histsize; i++)
184 if(history->hist[i] && history->hist[i]->str)
185 cnt++;
187 return(cnt);
191 static HISTORY_S **histlist[100];
193 void
194 add_to_histlist(HISTORY_S **history)
196 int i;
198 if(history){
199 /* find empty slot */
200 for(i = 0; i < 100; i++)
201 if(!histlist[i])
202 break;
204 if(i < 100)
205 histlist[i] = history;
210 void
211 free_histlist(void)
213 int i;
215 for(i = 0; i < 100; i++)
216 if(histlist[i])
217 free_hist(histlist[i]);