| 1/* $NetBSD: history.c,v 1.47 2014/05/11 01:05:17 christos Exp $ */ 2
|
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE.
| 3/*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Christos Zoulas of Cornell University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE.
|
31 * 32 * $NetBSD: history.c,v 1.34 2009/09/07 21:24:33 christos Exp $
| |
33 */ 34
| 33 */ 34
|
| 35#include "config.h"
|
35#if !defined(lint) && !defined(SCCSID)
| 36#if !defined(lint) && !defined(SCCSID)
|
| 37#if 0
|
36static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
| 38static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
|
| 39#else 40__RCSID("$NetBSD: history.c,v 1.47 2014/05/11 01:05:17 christos Exp $"); 41#endif
|
37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h>
| 42#endif /* not lint && not SCCSID */ 43#include <sys/cdefs.h>
|
39__FBSDID("$FreeBSD: head/lib/libedit/history.c 265863 2014-05-11 01:44:11Z eadler $");
| 44__FBSDID("$FreeBSD: head/lib/libedit/history.c 276881 2015-01-09 07:40:56Z bapt $");
|
40 41/*
| 45 46/*
|
42 * hist.c: History access functions
| 47 * hist.c: TYPE(History) access functions
|
43 */
| 48 */
|
44#include "sys.h" 45
| |
46#include <string.h> 47#include <stdlib.h> 48#include <stdarg.h> 49#include <vis.h> 50#include <sys/stat.h> 51 52static const char hist_cookie[] = "_HiStOrY_V2_\n"; 53 54#include "histedit.h"
| 49#include <string.h> 50#include <stdlib.h> 51#include <stdarg.h> 52#include <vis.h> 53#include <sys/stat.h> 54 55static const char hist_cookie[] = "_HiStOrY_V2_\n"; 56 57#include "histedit.h"
|
| 58#include "chartype.h"
|
55
| 59
|
56typedef int (*history_gfun_t)(ptr_t, HistEvent *); 57typedef int (*history_efun_t)(ptr_t, HistEvent *, const char *); 58typedef void (*history_vfun_t)(ptr_t, HistEvent *); 59typedef int (*history_sfun_t)(ptr_t, HistEvent *, const int);
| 60typedef int (*history_gfun_t)(void *, TYPE(HistEvent) *); 61typedef int (*history_efun_t)(void *, TYPE(HistEvent) *, const Char *); 62typedef void (*history_vfun_t)(void *, TYPE(HistEvent) *); 63typedef int (*history_sfun_t)(void *, TYPE(HistEvent) *, const int);
|
60
| 64
|
61struct history { 62 ptr_t h_ref; /* Argument for history fcns */
| 65struct TYPE(history) { 66 void *h_ref; /* Argument for history fcns */
|
63 int h_ent; /* Last entry point for history */ 64 history_gfun_t h_first; /* Get the first element */ 65 history_gfun_t h_next; /* Get the next element */ 66 history_gfun_t h_last; /* Get the last element */ 67 history_gfun_t h_prev; /* Get the previous element */ 68 history_gfun_t h_curr; /* Get the current element */ 69 history_sfun_t h_set; /* Set the current element */ 70 history_sfun_t h_del; /* Set the given element */ 71 history_vfun_t h_clear; /* Clear the history list */ 72 history_efun_t h_enter; /* Add an element */ 73 history_efun_t h_add; /* Append to an element */ 74}; 75 76#define HNEXT(h, ev) (*(h)->h_next)((h)->h_ref, ev) 77#define HFIRST(h, ev) (*(h)->h_first)((h)->h_ref, ev) 78#define HPREV(h, ev) (*(h)->h_prev)((h)->h_ref, ev) 79#define HLAST(h, ev) (*(h)->h_last)((h)->h_ref, ev) 80#define HCURR(h, ev) (*(h)->h_curr)((h)->h_ref, ev) 81#define HSET(h, ev, n) (*(h)->h_set)((h)->h_ref, ev, n) 82#define HCLEAR(h, ev) (*(h)->h_clear)((h)->h_ref, ev) 83#define HENTER(h, ev, str) (*(h)->h_enter)((h)->h_ref, ev, str) 84#define HADD(h, ev, str) (*(h)->h_add)((h)->h_ref, ev, str) 85#define HDEL(h, ev, n) (*(h)->h_del)((h)->h_ref, ev, n) 86
| 67 int h_ent; /* Last entry point for history */ 68 history_gfun_t h_first; /* Get the first element */ 69 history_gfun_t h_next; /* Get the next element */ 70 history_gfun_t h_last; /* Get the last element */ 71 history_gfun_t h_prev; /* Get the previous element */ 72 history_gfun_t h_curr; /* Get the current element */ 73 history_sfun_t h_set; /* Set the current element */ 74 history_sfun_t h_del; /* Set the given element */ 75 history_vfun_t h_clear; /* Clear the history list */ 76 history_efun_t h_enter; /* Add an element */ 77 history_efun_t h_add; /* Append to an element */ 78}; 79 80#define HNEXT(h, ev) (*(h)->h_next)((h)->h_ref, ev) 81#define HFIRST(h, ev) (*(h)->h_first)((h)->h_ref, ev) 82#define HPREV(h, ev) (*(h)->h_prev)((h)->h_ref, ev) 83#define HLAST(h, ev) (*(h)->h_last)((h)->h_ref, ev) 84#define HCURR(h, ev) (*(h)->h_curr)((h)->h_ref, ev) 85#define HSET(h, ev, n) (*(h)->h_set)((h)->h_ref, ev, n) 86#define HCLEAR(h, ev) (*(h)->h_clear)((h)->h_ref, ev) 87#define HENTER(h, ev, str) (*(h)->h_enter)((h)->h_ref, ev, str) 88#define HADD(h, ev, str) (*(h)->h_add)((h)->h_ref, ev, str) 89#define HDEL(h, ev, n) (*(h)->h_del)((h)->h_ref, ev, n) 90
|
87#define h_strdup(a) strdup(a)
| 91#define h_strdup(a) Strdup(a)
|
88#define h_malloc(a) malloc(a) 89#define h_realloc(a, b) realloc((a), (b)) 90#define h_free(a) free(a) 91 92typedef struct { 93 int num;
| 92#define h_malloc(a) malloc(a) 93#define h_realloc(a, b) realloc((a), (b)) 94#define h_free(a) free(a) 95 96typedef struct { 97 int num;
|
94 char *str;
| 98 Char *str;
|
95} HistEventPrivate; 96 97 98
| 99} HistEventPrivate; 100 101 102
|
99private int history_setsize(History *, HistEvent *, int); 100private int history_getsize(History *, HistEvent *); 101private int history_setunique(History *, HistEvent *, int); 102private int history_getunique(History *, HistEvent *); 103private int history_set_fun(History *, History *); 104private int history_load(History *, const char *); 105private int history_save(History *, const char *); 106private int history_save_fp(History *, FILE*); 107private int history_prev_event(History *, HistEvent *, int); 108private int history_next_event(History *, HistEvent *, int); 109private int history_next_string(History *, HistEvent *, const char *); 110private int history_prev_string(History *, HistEvent *, const char *);
| 103private int history_setsize(TYPE(History) *, TYPE(HistEvent) *, int); 104private int history_getsize(TYPE(History) *, TYPE(HistEvent) *); 105private int history_setunique(TYPE(History) *, TYPE(HistEvent) *, int); 106private int history_getunique(TYPE(History) *, TYPE(HistEvent) *); 107private int history_set_fun(TYPE(History) *, TYPE(History) *); 108private int history_load(TYPE(History) *, const char *); 109private int history_save(TYPE(History) *, const char *); 110private int history_save_fp(TYPE(History) *, FILE *); 111private int history_prev_event(TYPE(History) *, TYPE(HistEvent) *, int); 112private int history_next_event(TYPE(History) *, TYPE(HistEvent) *, int); 113private int history_next_string(TYPE(History) *, TYPE(HistEvent) *, const Char *); 114private int history_prev_string(TYPE(History) *, TYPE(HistEvent) *, const Char *);
|
111 112 113/***********************************************************************/ 114 115/* 116 * Builtin- history implementation 117 */ 118typedef struct hentry_t {
| 115 116 117/***********************************************************************/ 118 119/* 120 * Builtin- history implementation 121 */ 122typedef struct hentry_t {
|
119 HistEvent ev; /* What we return */
| 123 TYPE(HistEvent) ev; /* What we return */
|
120 void *data; /* data */ 121 struct hentry_t *next; /* Next entry */ 122 struct hentry_t *prev; /* Previous entry */ 123} hentry_t; 124 125typedef struct history_t { 126 hentry_t list; /* Fake list header element */ 127 hentry_t *cursor; /* Current element in the list */ 128 int max; /* Maximum number of events */ 129 int cur; /* Current number of events */ 130 int eventid; /* For generation of unique event id */
| 124 void *data; /* data */ 125 struct hentry_t *next; /* Next entry */ 126 struct hentry_t *prev; /* Previous entry */ 127} hentry_t; 128 129typedef struct history_t { 130 hentry_t list; /* Fake list header element */ 131 hentry_t *cursor; /* Current element in the list */ 132 int max; /* Maximum number of events */ 133 int cur; /* Current number of events */ 134 int eventid; /* For generation of unique event id */
|
131 int flags; /* History flags */
| 135 int flags; /* TYPE(History) flags */
|
132#define H_UNIQUE 1 /* Store only unique elements */ 133} history_t; 134
| 136#define H_UNIQUE 1 /* Store only unique elements */ 137} history_t; 138
|
135private int history_def_next(ptr_t, HistEvent *); 136private int history_def_first(ptr_t, HistEvent *); 137private int history_def_prev(ptr_t, HistEvent *); 138private int history_def_last(ptr_t, HistEvent *); 139private int history_def_curr(ptr_t, HistEvent *); 140private int history_def_set(ptr_t, HistEvent *, const int); 141private void history_def_clear(ptr_t, HistEvent *); 142private int history_def_enter(ptr_t, HistEvent *, const char *); 143private int history_def_add(ptr_t, HistEvent *, const char *); 144private int history_def_del(ptr_t, HistEvent *, const int);
| 139private int history_def_next(void *, TYPE(HistEvent) *); 140private int history_def_first(void *, TYPE(HistEvent) *); 141private int history_def_prev(void *, TYPE(HistEvent) *); 142private int history_def_last(void *, TYPE(HistEvent) *); 143private int history_def_curr(void *, TYPE(HistEvent) *); 144private int history_def_set(void *, TYPE(HistEvent) *, const int); 145private void history_def_clear(void *, TYPE(HistEvent) *); 146private int history_def_enter(void *, TYPE(HistEvent) *, const Char *); 147private int history_def_add(void *, TYPE(HistEvent) *, const Char *); 148private int history_def_del(void *, TYPE(HistEvent) *, const int);
|
145
| 149
|
146private int history_def_init(ptr_t *, HistEvent *, int); 147private int history_def_insert(history_t *, HistEvent *, const char *); 148private void history_def_delete(history_t *, HistEvent *, hentry_t *);
| 150private int history_def_init(void **, TYPE(HistEvent) *, int); 151private int history_def_insert(history_t *, TYPE(HistEvent) *, const Char *); 152private void history_def_delete(history_t *, TYPE(HistEvent) *, hentry_t *);
|
149
| 153
|
150private int history_deldata_nth(history_t *, HistEvent *, int, void **); 151private int history_set_nth(ptr_t, HistEvent *, int);
| 154private int history_deldata_nth(history_t *, TYPE(HistEvent) *, int, void **); 155private int history_set_nth(void *, TYPE(HistEvent) *, int);
|
152 153#define history_def_setsize(p, num)(void) (((history_t *)p)->max = (num)) 154#define history_def_getsize(p) (((history_t *)p)->cur) 155#define history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0) 156#define history_def_setunique(p, uni) \ 157 if (uni) \ 158 (((history_t *)p)->flags) |= H_UNIQUE; \ 159 else \ 160 (((history_t *)p)->flags) &= ~H_UNIQUE 161 162#define he_strerror(code) he_errlist[code] 163#define he_seterrev(evp, code) {\ 164 evp->num = code;\ 165 evp->str = he_strerror(code);\ 166 } 167 168/* error messages */
| 156 157#define history_def_setsize(p, num)(void) (((history_t *)p)->max = (num)) 158#define history_def_getsize(p) (((history_t *)p)->cur) 159#define history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0) 160#define history_def_setunique(p, uni) \ 161 if (uni) \ 162 (((history_t *)p)->flags) |= H_UNIQUE; \ 163 else \ 164 (((history_t *)p)->flags) &= ~H_UNIQUE 165 166#define he_strerror(code) he_errlist[code] 167#define he_seterrev(evp, code) {\ 168 evp->num = code;\ 169 evp->str = he_strerror(code);\ 170 } 171 172/* error messages */
|
169static const char *const he_errlist[] = { 170 "OK", 171 "unknown error", 172 "malloc() failed", 173 "first event not found", 174 "last event not found", 175 "empty list", 176 "no next event", 177 "no previous event", 178 "current event is invalid", 179 "event not found", 180 "can't read history from file", 181 "can't write history", 182 "required parameter(s) not supplied", 183 "history size negative", 184 "function not allowed with other history-functions-set the default", 185 "bad parameters"
| 173static const Char *const he_errlist[] = { 174 STR("OK"), 175 STR("unknown error"), 176 STR("malloc() failed"), 177 STR("first event not found"), 178 STR("last event not found"), 179 STR("empty list"), 180 STR("no next event"), 181 STR("no previous event"), 182 STR("current event is invalid"), 183 STR("event not found"), 184 STR("can't read history from file"), 185 STR("can't write history"), 186 STR("required parameter(s) not supplied"), 187 STR("history size negative"), 188 STR("function not allowed with other history-functions-set the default"), 189 STR("bad parameters")
|
186}; 187/* error codes */ 188#define _HE_OK 0 189#define _HE_UNKNOWN 1 190#define _HE_MALLOC_FAILED 2 191#define _HE_FIRST_NOTFOUND 3 192#define _HE_LAST_NOTFOUND 4 193#define _HE_EMPTY_LIST 5 194#define _HE_END_REACHED 6 195#define _HE_START_REACHED 7 196#define _HE_CURR_INVALID 8 197#define _HE_NOT_FOUND 9 198#define _HE_HIST_READ 10 199#define _HE_HIST_WRITE 11 200#define _HE_PARAM_MISSING 12 201#define _HE_SIZE_NEGATIVE 13 202#define _HE_NOT_ALLOWED 14 203#define _HE_BAD_PARAM 15 204 205/* history_def_first(): 206 * Default function to return the first event in the history. 207 */ 208private int
| 190}; 191/* error codes */ 192#define _HE_OK 0 193#define _HE_UNKNOWN 1 194#define _HE_MALLOC_FAILED 2 195#define _HE_FIRST_NOTFOUND 3 196#define _HE_LAST_NOTFOUND 4 197#define _HE_EMPTY_LIST 5 198#define _HE_END_REACHED 6 199#define _HE_START_REACHED 7 200#define _HE_CURR_INVALID 8 201#define _HE_NOT_FOUND 9 202#define _HE_HIST_READ 10 203#define _HE_HIST_WRITE 11 204#define _HE_PARAM_MISSING 12 205#define _HE_SIZE_NEGATIVE 13 206#define _HE_NOT_ALLOWED 14 207#define _HE_BAD_PARAM 15 208 209/* history_def_first(): 210 * Default function to return the first event in the history. 211 */ 212private int
|
209history_def_first(ptr_t p, HistEvent *ev)
| 213history_def_first(void *p, TYPE(HistEvent) *ev)
|
210{ 211 history_t *h = (history_t *) p; 212 213 h->cursor = h->list.next; 214 if (h->cursor != &h->list) 215 *ev = h->cursor->ev; 216 else { 217 he_seterrev(ev, _HE_FIRST_NOTFOUND);
| 214{ 215 history_t *h = (history_t *) p; 216 217 h->cursor = h->list.next; 218 if (h->cursor != &h->list) 219 *ev = h->cursor->ev; 220 else { 221 he_seterrev(ev, _HE_FIRST_NOTFOUND);
|
218 return (-1);
| 222 return -1;
|
219 } 220
| 223 } 224
|
221 return (0);
| 225 return 0;
|
222} 223 224 225/* history_def_last(): 226 * Default function to return the last event in the history. 227 */ 228private int
| 226} 227 228 229/* history_def_last(): 230 * Default function to return the last event in the history. 231 */ 232private int
|
229history_def_last(ptr_t p, HistEvent *ev)
| 233history_def_last(void *p, TYPE(HistEvent) *ev)
|
230{ 231 history_t *h = (history_t *) p; 232 233 h->cursor = h->list.prev; 234 if (h->cursor != &h->list) 235 *ev = h->cursor->ev; 236 else { 237 he_seterrev(ev, _HE_LAST_NOTFOUND);
| 234{ 235 history_t *h = (history_t *) p; 236 237 h->cursor = h->list.prev; 238 if (h->cursor != &h->list) 239 *ev = h->cursor->ev; 240 else { 241 he_seterrev(ev, _HE_LAST_NOTFOUND);
|
238 return (-1);
| 242 return -1;
|
239 } 240
| 243 } 244
|
241 return (0);
| 245 return 0;
|
242} 243 244 245/* history_def_next(): 246 * Default function to return the next event in the history. 247 */ 248private int
| 246} 247 248 249/* history_def_next(): 250 * Default function to return the next event in the history. 251 */ 252private int
|
249history_def_next(ptr_t p, HistEvent *ev)
| 253history_def_next(void *p, TYPE(HistEvent) *ev)
|
250{ 251 history_t *h = (history_t *) p; 252 253 if (h->cursor == &h->list) { 254 he_seterrev(ev, _HE_EMPTY_LIST);
| 254{ 255 history_t *h = (history_t *) p; 256 257 if (h->cursor == &h->list) { 258 he_seterrev(ev, _HE_EMPTY_LIST);
|
255 return (-1);
| 259 return -1;
|
256 } 257 258 if (h->cursor->next == &h->list) { 259 he_seterrev(ev, _HE_END_REACHED);
| 260 } 261 262 if (h->cursor->next == &h->list) { 263 he_seterrev(ev, _HE_END_REACHED);
|
260 return (-1);
| 264 return -1;
|
261 } 262 263 h->cursor = h->cursor->next; 264 *ev = h->cursor->ev; 265
| 265 } 266 267 h->cursor = h->cursor->next; 268 *ev = h->cursor->ev; 269
|
266 return (0);
| 270 return 0;
|
267} 268 269 270/* history_def_prev(): 271 * Default function to return the previous event in the history. 272 */ 273private int
| 271} 272 273 274/* history_def_prev(): 275 * Default function to return the previous event in the history. 276 */ 277private int
|
274history_def_prev(ptr_t p, HistEvent *ev)
| 278history_def_prev(void *p, TYPE(HistEvent) *ev)
|
275{ 276 history_t *h = (history_t *) p; 277 278 if (h->cursor == &h->list) { 279 he_seterrev(ev, 280 (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
| 279{ 280 history_t *h = (history_t *) p; 281 282 if (h->cursor == &h->list) { 283 he_seterrev(ev, 284 (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
|
281 return (-1);
| 285 return -1;
|
282 } 283 284 if (h->cursor->prev == &h->list) { 285 he_seterrev(ev, _HE_START_REACHED);
| 286 } 287 288 if (h->cursor->prev == &h->list) { 289 he_seterrev(ev, _HE_START_REACHED);
|
286 return (-1);
| 290 return -1;
|
287 } 288 289 h->cursor = h->cursor->prev; 290 *ev = h->cursor->ev; 291
| 291 } 292 293 h->cursor = h->cursor->prev; 294 *ev = h->cursor->ev; 295
|
292 return (0);
| 296 return 0;
|
293} 294 295 296/* history_def_curr(): 297 * Default function to return the current event in the history. 298 */ 299private int
| 297} 298 299 300/* history_def_curr(): 301 * Default function to return the current event in the history. 302 */ 303private int
|
300history_def_curr(ptr_t p, HistEvent *ev)
| 304history_def_curr(void *p, TYPE(HistEvent) *ev)
|
301{ 302 history_t *h = (history_t *) p; 303 304 if (h->cursor != &h->list) 305 *ev = h->cursor->ev; 306 else { 307 he_seterrev(ev, 308 (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
| 305{ 306 history_t *h = (history_t *) p; 307 308 if (h->cursor != &h->list) 309 *ev = h->cursor->ev; 310 else { 311 he_seterrev(ev, 312 (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
|
309 return (-1);
| 313 return -1;
|
310 } 311
| 314 } 315
|
312 return (0);
| 316 return 0;
|
313} 314 315 316/* history_def_set(): 317 * Default function to set the current event in the history to the 318 * given one. 319 */ 320private int
| 317} 318 319 320/* history_def_set(): 321 * Default function to set the current event in the history to the 322 * given one. 323 */ 324private int
|
321history_def_set(ptr_t p, HistEvent *ev, const int n)
| 325history_def_set(void *p, TYPE(HistEvent) *ev, const int n)
|
322{ 323 history_t *h = (history_t *) p; 324 325 if (h->cur == 0) { 326 he_seterrev(ev, _HE_EMPTY_LIST);
| 326{ 327 history_t *h = (history_t *) p; 328 329 if (h->cur == 0) { 330 he_seterrev(ev, _HE_EMPTY_LIST);
|
327 return (-1);
| 331 return -1;
|
328 } 329 if (h->cursor == &h->list || h->cursor->ev.num != n) { 330 for (h->cursor = h->list.next; h->cursor != &h->list; 331 h->cursor = h->cursor->next) 332 if (h->cursor->ev.num == n) 333 break; 334 } 335 if (h->cursor == &h->list) { 336 he_seterrev(ev, _HE_NOT_FOUND);
| 332 } 333 if (h->cursor == &h->list || h->cursor->ev.num != n) { 334 for (h->cursor = h->list.next; h->cursor != &h->list; 335 h->cursor = h->cursor->next) 336 if (h->cursor->ev.num == n) 337 break; 338 } 339 if (h->cursor == &h->list) { 340 he_seterrev(ev, _HE_NOT_FOUND);
|
337 return (-1);
| 341 return -1;
|
338 }
| 342 }
|
339 return (0);
| 343 return 0;
|
340} 341 342 343/* history_set_nth(): 344 * Default function to set the current event in the history to the 345 * n-th one. 346 */ 347private int
| 344} 345 346 347/* history_set_nth(): 348 * Default function to set the current event in the history to the 349 * n-th one. 350 */ 351private int
|
348history_set_nth(ptr_t p, HistEvent *ev, int n)
| 352history_set_nth(void *p, TYPE(HistEvent) *ev, int n)
|
349{ 350 history_t *h = (history_t *) p; 351 352 if (h->cur == 0) { 353 he_seterrev(ev, _HE_EMPTY_LIST);
| 353{ 354 history_t *h = (history_t *) p; 355 356 if (h->cur == 0) { 357 he_seterrev(ev, _HE_EMPTY_LIST);
|
354 return (-1);
| 358 return -1;
|
355 } 356 for (h->cursor = h->list.prev; h->cursor != &h->list; 357 h->cursor = h->cursor->prev) 358 if (n-- <= 0) 359 break; 360 if (h->cursor == &h->list) { 361 he_seterrev(ev, _HE_NOT_FOUND);
| 359 } 360 for (h->cursor = h->list.prev; h->cursor != &h->list; 361 h->cursor = h->cursor->prev) 362 if (n-- <= 0) 363 break; 364 if (h->cursor == &h->list) { 365 he_seterrev(ev, _HE_NOT_FOUND);
|
362 return (-1);
| 366 return -1;
|
363 }
| 367 }
|
364 return (0);
| 368 return 0;
|
365} 366 367 368/* history_def_add(): 369 * Append string to element 370 */ 371private int
| 369} 370 371 372/* history_def_add(): 373 * Append string to element 374 */ 375private int
|
372history_def_add(ptr_t p, HistEvent *ev, const char *str)
| 376history_def_add(void *p, TYPE(HistEvent) *ev, const Char *str)
|
373{ 374 history_t *h = (history_t *) p; 375 size_t len;
| 377{ 378 history_t *h = (history_t *) p; 379 size_t len;
|
376 char *s;
| 380 Char *s;
|
377 HistEventPrivate *evp = (void *)&h->cursor->ev; 378 379 if (h->cursor == &h->list)
| 381 HistEventPrivate *evp = (void *)&h->cursor->ev; 382 383 if (h->cursor == &h->list)
|
380 return (history_def_enter(p, ev, str)); 381 len = strlen(evp->str) + strlen(str) + 1; 382 s = (char *) h_malloc(len);
| 384 return history_def_enter(p, ev, str); 385 len = Strlen(evp->str) + Strlen(str) + 1; 386 s = h_malloc(len * sizeof(*s));
|
383 if (s == NULL) { 384 he_seterrev(ev, _HE_MALLOC_FAILED);
| 387 if (s == NULL) { 388 he_seterrev(ev, _HE_MALLOC_FAILED);
|
385 return (-1);
| 389 return -1;
|
386 }
| 390 }
|
387 (void) strlcpy(s, h->cursor->ev.str, len); 388 (void) strlcat(s, str, len); 389 h_free((ptr_t)evp->str);
| 391 (void) Strncpy(s, h->cursor->ev.str, len); 392 s[len - 1] = '\0'; 393 (void) Strncat(s, str, len - Strlen(s) - 1); 394 h_free(evp->str);
|
390 evp->str = s; 391 *ev = h->cursor->ev;
| 395 evp->str = s; 396 *ev = h->cursor->ev;
|
392 return (0);
| 397 return 0;
|
393} 394 395 396private int
| 398} 399 400 401private int
|
397history_deldata_nth(history_t *h, HistEvent *ev,
| 402history_deldata_nth(history_t *h, TYPE(HistEvent) *ev,
|
398 int num, void **data) 399{ 400 if (history_set_nth(h, ev, num) != 0)
| 403 int num, void **data) 404{ 405 if (history_set_nth(h, ev, num) != 0)
|
401 return (-1);
| 406 return -1;
|
402 /* magic value to skip delete (just set to n-th history) */ 403 if (data == (void **)-1)
| 407 /* magic value to skip delete (just set to n-th history) */ 408 if (data == (void **)-1)
|
404 return (0); 405 ev->str = strdup(h->cursor->ev.str);
| 409 return 0; 410 ev->str = Strdup(h->cursor->ev.str);
|
406 ev->num = h->cursor->ev.num; 407 if (data) 408 *data = h->cursor->data; 409 history_def_delete(h, ev, h->cursor);
| 411 ev->num = h->cursor->ev.num; 412 if (data) 413 *data = h->cursor->data; 414 history_def_delete(h, ev, h->cursor);
|
410 return (0);
| 415 return 0;
|
411} 412 413 414/* history_def_del(): 415 * Delete element hp of the h list 416 */ 417/* ARGSUSED */ 418private int
| 416} 417 418 419/* history_def_del(): 420 * Delete element hp of the h list 421 */ 422/* ARGSUSED */ 423private int
|
419history_def_del(ptr_t p, HistEvent *ev __unused,
| 424history_def_del(void *p, TYPE(HistEvent) *ev __attribute__((__unused__)),
|
420 const int num) 421{ 422 history_t *h = (history_t *) p; 423 if (history_def_set(h, ev, num) != 0)
| 425 const int num) 426{ 427 history_t *h = (history_t *) p; 428 if (history_def_set(h, ev, num) != 0)
|
424 return (-1); 425 ev->str = strdup(h->cursor->ev.str);
| 429 return -1; 430 ev->str = Strdup(h->cursor->ev.str);
|
426 ev->num = h->cursor->ev.num; 427 history_def_delete(h, ev, h->cursor);
| 431 ev->num = h->cursor->ev.num; 432 history_def_delete(h, ev, h->cursor);
|
428 return (0);
| 433 return 0;
|
429} 430 431 432/* history_def_delete(): 433 * Delete element hp of the h list 434 */ 435/* ARGSUSED */ 436private void 437history_def_delete(history_t *h,
| 434} 435 436 437/* history_def_delete(): 438 * Delete element hp of the h list 439 */ 440/* ARGSUSED */ 441private void 442history_def_delete(history_t *h,
|
438 HistEvent *ev __unused, hentry_t *hp)
| 443 TYPE(HistEvent) *ev __attribute__((__unused__)), hentry_t *hp)
|
439{ 440 HistEventPrivate *evp = (void *)&hp->ev; 441 if (hp == &h->list) 442 abort(); 443 if (h->cursor == hp) { 444 h->cursor = hp->prev; 445 if (h->cursor == &h->list) 446 h->cursor = hp->next; 447 } 448 hp->prev->next = hp->next; 449 hp->next->prev = hp->prev;
| 444{ 445 HistEventPrivate *evp = (void *)&hp->ev; 446 if (hp == &h->list) 447 abort(); 448 if (h->cursor == hp) { 449 h->cursor = hp->prev; 450 if (h->cursor == &h->list) 451 h->cursor = hp->next; 452 } 453 hp->prev->next = hp->next; 454 hp->next->prev = hp->prev;
|
450 h_free((ptr_t) evp->str);
| 455 h_free(evp->str);
|
451 h_free(hp); 452 h->cur--; 453} 454 455 456/* history_def_insert(): 457 * Insert element with string str in the h list 458 */ 459private int
| 456 h_free(hp); 457 h->cur--; 458} 459 460 461/* history_def_insert(): 462 * Insert element with string str in the h list 463 */ 464private int
|
460history_def_insert(history_t *h, HistEvent *ev, const char *str)
| 465history_def_insert(history_t *h, TYPE(HistEvent) *ev, const Char *str)
|
461{
| 466{
|
| 467 hentry_t *c;
|
462
| 468
|
463 h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t)); 464 if (h->cursor == NULL)
| 469 c = h_malloc(sizeof(*c)); 470 if (c == NULL)
|
465 goto oomem;
| 471 goto oomem;
|
466 if ((h->cursor->ev.str = h_strdup(str)) == NULL) { 467 h_free((ptr_t)h->cursor);
| 472 if ((c->ev.str = h_strdup(str)) == NULL) { 473 h_free(c);
|
468 goto oomem; 469 }
| 474 goto oomem; 475 }
|
470 h->cursor->data = NULL; 471 h->cursor->ev.num = ++h->eventid; 472 h->cursor->next = h->list.next; 473 h->cursor->prev = &h->list; 474 h->list.next->prev = h->cursor; 475 h->list.next = h->cursor;
| 476 c->data = NULL; 477 c->ev.num = ++h->eventid; 478 c->next = h->list.next; 479 c->prev = &h->list; 480 h->list.next->prev = c; 481 h->list.next = c;
|
476 h->cur++;
| 482 h->cur++;
|
| 483 h->cursor = c;
|
477
| 484
|
478 *ev = h->cursor->ev; 479 return (0);
| 485 *ev = c->ev; 486 return 0;
|
480oomem: 481 he_seterrev(ev, _HE_MALLOC_FAILED);
| 487oomem: 488 he_seterrev(ev, _HE_MALLOC_FAILED);
|
482 return (-1);
| 489 return -1;
|
483} 484 485 486/* history_def_enter(): 487 * Default function to enter an item in the history 488 */ 489private int
| 490} 491 492 493/* history_def_enter(): 494 * Default function to enter an item in the history 495 */ 496private int
|
490history_def_enter(ptr_t p, HistEvent *ev, const char *str)
| 497history_def_enter(void *p, TYPE(HistEvent) *ev, const Char *str)
|
491{ 492 history_t *h = (history_t *) p; 493 494 if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
| 498{ 499 history_t *h = (history_t *) p; 500 501 if ((h->flags & H_UNIQUE) != 0 && h->list.next != &h->list &&
|
495 strcmp(h->list.next->ev.str, str) == 0) 496 return (0);
| 502 Strcmp(h->list.next->ev.str, str) == 0) 503 return 0;
|
497 498 if (history_def_insert(h, ev, str) == -1)
| 504 505 if (history_def_insert(h, ev, str) == -1)
|
499 return (-1); /* error, keep error message */
| 506 return -1; /* error, keep error message */
|
500 501 /* 502 * Always keep at least one entry. 503 * This way we don't have to check for the empty list. 504 */ 505 while (h->cur > h->max && h->cur > 0) 506 history_def_delete(h, ev, h->list.prev); 507
| 507 508 /* 509 * Always keep at least one entry. 510 * This way we don't have to check for the empty list. 511 */ 512 while (h->cur > h->max && h->cur > 0) 513 history_def_delete(h, ev, h->list.prev); 514
|
508 return (1);
| 515 return 1;
|
509} 510 511 512/* history_def_init(): 513 * Default history initialization function 514 */ 515/* ARGSUSED */ 516private int
| 516} 517 518 519/* history_def_init(): 520 * Default history initialization function 521 */ 522/* ARGSUSED */ 523private int
|
517history_def_init(ptr_t *p, HistEvent *ev __unused, int n)
| 524history_def_init(void **p, TYPE(HistEvent) *ev __attribute__((__unused__)), int n)
|
518{
| 525{
|
519 history_t *h = (history_t *) h_malloc(sizeof(history_t));
| 526 history_t *h = (history_t *) h_malloc(sizeof(*h));
|
520 if (h == NULL) 521 return -1; 522 523 if (n <= 0) 524 n = 0; 525 h->eventid = 0; 526 h->cur = 0; 527 h->max = n; 528 h->list.next = h->list.prev = &h->list; 529 h->list.ev.str = NULL; 530 h->list.ev.num = 0; 531 h->cursor = &h->list; 532 h->flags = 0;
| 527 if (h == NULL) 528 return -1; 529 530 if (n <= 0) 531 n = 0; 532 h->eventid = 0; 533 h->cur = 0; 534 h->max = n; 535 h->list.next = h->list.prev = &h->list; 536 h->list.ev.str = NULL; 537 h->list.ev.num = 0; 538 h->cursor = &h->list; 539 h->flags = 0;
|
533 *p = (ptr_t) h;
| 540 *p = h;
|
534 return 0; 535} 536 537 538/* history_def_clear(): 539 * Default history cleanup function 540 */ 541private void
| 541 return 0; 542} 543 544 545/* history_def_clear(): 546 * Default history cleanup function 547 */ 548private void
|
542history_def_clear(ptr_t p, HistEvent *ev)
| 549history_def_clear(void *p, TYPE(HistEvent) *ev)
|
543{ 544 history_t *h = (history_t *) p; 545 546 while (h->list.prev != &h->list) 547 history_def_delete(h, ev, h->list.prev);
| 550{ 551 history_t *h = (history_t *) p; 552 553 while (h->list.prev != &h->list) 554 history_def_delete(h, ev, h->list.prev);
|
| 555 h->cursor = &h->list;
|
548 h->eventid = 0; 549 h->cur = 0; 550} 551 552 553 554 555/************************************************************************/ 556 557/* history_init(): 558 * Initialization function. 559 */
| 556 h->eventid = 0; 557 h->cur = 0; 558} 559 560 561 562 563/************************************************************************/ 564 565/* history_init(): 566 * Initialization function. 567 */
|
560public History * 561history_init(void)
| 568public TYPE(History) * 569FUN(history,init)(void)
|
562{
| 570{
|
563 HistEvent ev; 564 History *h = (History *) h_malloc(sizeof(History));
| 571 TYPE(HistEvent) ev; 572 TYPE(History) *h = (TYPE(History) *) h_malloc(sizeof(*h));
|
565 if (h == NULL) 566 return NULL; 567 568 if (history_def_init(&h->h_ref, &ev, 0) == -1) {
| 573 if (h == NULL) 574 return NULL; 575 576 if (history_def_init(&h->h_ref, &ev, 0) == -1) {
|
569 h_free((ptr_t)h);
| 577 h_free(h);
|
570 return NULL; 571 } 572 h->h_ent = -1; 573 h->h_next = history_def_next; 574 h->h_first = history_def_first; 575 h->h_last = history_def_last; 576 h->h_prev = history_def_prev; 577 h->h_curr = history_def_curr; 578 h->h_set = history_def_set; 579 h->h_clear = history_def_clear; 580 h->h_enter = history_def_enter; 581 h->h_add = history_def_add; 582 h->h_del = history_def_del; 583
| 578 return NULL; 579 } 580 h->h_ent = -1; 581 h->h_next = history_def_next; 582 h->h_first = history_def_first; 583 h->h_last = history_def_last; 584 h->h_prev = history_def_prev; 585 h->h_curr = history_def_curr; 586 h->h_set = history_def_set; 587 h->h_clear = history_def_clear; 588 h->h_enter = history_def_enter; 589 h->h_add = history_def_add; 590 h->h_del = history_def_del; 591
|
584 return (h);
| 592 return h;
|
585} 586 587 588/* history_end(): 589 * clean up history; 590 */ 591public void
| 593} 594 595 596/* history_end(): 597 * clean up history; 598 */ 599public void
|
592history_end(History *h)
| 600FUN(history,end)(TYPE(History) *h)
|
593{
| 601{
|
594 HistEvent ev;
| 602 TYPE(HistEvent) ev;
|
595 596 if (h->h_next == history_def_next) 597 history_def_clear(h->h_ref, &ev); 598 h_free(h->h_ref); 599 h_free(h); 600} 601 602 603 604/* history_setsize(): 605 * Set history number of events 606 */ 607private int
| 603 604 if (h->h_next == history_def_next) 605 history_def_clear(h->h_ref, &ev); 606 h_free(h->h_ref); 607 h_free(h); 608} 609 610 611 612/* history_setsize(): 613 * Set history number of events 614 */ 615private int
|
608history_setsize(History *h, HistEvent *ev, int num)
| 616history_setsize(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
609{ 610 611 if (h->h_next != history_def_next) { 612 he_seterrev(ev, _HE_NOT_ALLOWED);
| 617{ 618 619 if (h->h_next != history_def_next) { 620 he_seterrev(ev, _HE_NOT_ALLOWED);
|
613 return (-1);
| 621 return -1;
|
614 } 615 if (num < 0) { 616 he_seterrev(ev, _HE_BAD_PARAM);
| 622 } 623 if (num < 0) { 624 he_seterrev(ev, _HE_BAD_PARAM);
|
617 return (-1);
| 625 return -1;
|
618 } 619 history_def_setsize(h->h_ref, num);
| 626 } 627 history_def_setsize(h->h_ref, num);
|
620 return (0);
| 628 return 0;
|
621} 622 623 624/* history_getsize(): 625 * Get number of events currently in history 626 */ 627private int
| 629} 630 631 632/* history_getsize(): 633 * Get number of events currently in history 634 */ 635private int
|
628history_getsize(History *h, HistEvent *ev)
| 636history_getsize(TYPE(History) *h, TYPE(HistEvent) *ev)
|
629{ 630 if (h->h_next != history_def_next) { 631 he_seterrev(ev, _HE_NOT_ALLOWED);
| 637{ 638 if (h->h_next != history_def_next) { 639 he_seterrev(ev, _HE_NOT_ALLOWED);
|
632 return (-1);
| 640 return -1;
|
633 } 634 ev->num = history_def_getsize(h->h_ref); 635 if (ev->num < -1) { 636 he_seterrev(ev, _HE_SIZE_NEGATIVE);
| 641 } 642 ev->num = history_def_getsize(h->h_ref); 643 if (ev->num < -1) { 644 he_seterrev(ev, _HE_SIZE_NEGATIVE);
|
637 return (-1);
| 645 return -1;
|
638 }
| 646 }
|
639 return (0);
| 647 return 0;
|
640} 641 642 643/* history_setunique(): 644 * Set if adjacent equal events should not be entered in history. 645 */ 646private int
| 648} 649 650 651/* history_setunique(): 652 * Set if adjacent equal events should not be entered in history. 653 */ 654private int
|
647history_setunique(History *h, HistEvent *ev, int uni)
| 655history_setunique(TYPE(History) *h, TYPE(HistEvent) *ev, int uni)
|
648{ 649 650 if (h->h_next != history_def_next) { 651 he_seterrev(ev, _HE_NOT_ALLOWED);
| 656{ 657 658 if (h->h_next != history_def_next) { 659 he_seterrev(ev, _HE_NOT_ALLOWED);
|
652 return (-1);
| 660 return -1;
|
653 } 654 history_def_setunique(h->h_ref, uni);
| 661 } 662 history_def_setunique(h->h_ref, uni);
|
655 return (0);
| 663 return 0;
|
656} 657 658 659/* history_getunique(): 660 * Get if adjacent equal events should not be entered in history. 661 */ 662private int
| 664} 665 666 667/* history_getunique(): 668 * Get if adjacent equal events should not be entered in history. 669 */ 670private int
|
663history_getunique(History *h, HistEvent *ev)
| 671history_getunique(TYPE(History) *h, TYPE(HistEvent) *ev)
|
664{ 665 if (h->h_next != history_def_next) { 666 he_seterrev(ev, _HE_NOT_ALLOWED);
| 672{ 673 if (h->h_next != history_def_next) { 674 he_seterrev(ev, _HE_NOT_ALLOWED);
|
667 return (-1);
| 675 return -1;
|
668 } 669 ev->num = history_def_getunique(h->h_ref);
| 676 } 677 ev->num = history_def_getunique(h->h_ref);
|
670 return (0);
| 678 return 0;
|
671} 672 673 674/* history_set_fun(): 675 * Set history functions 676 */ 677private int
| 679} 680 681 682/* history_set_fun(): 683 * Set history functions 684 */ 685private int
|
678history_set_fun(History *h, History *nh)
| 686history_set_fun(TYPE(History) *h, TYPE(History) *nh)
|
679{
| 687{
|
680 HistEvent ev;
| 688 TYPE(HistEvent) ev;
|
681 682 if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL || 683 nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL || 684 nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL || 685 nh->h_del == NULL || nh->h_ref == NULL) { 686 if (h->h_next != history_def_next) {
| 689 690 if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL || 691 nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL || 692 nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL || 693 nh->h_del == NULL || nh->h_ref == NULL) { 694 if (h->h_next != history_def_next) {
|
687 history_def_init(&h->h_ref, &ev, 0);
| 695 if (history_def_init(&h->h_ref, &ev, 0) == -1) 696 return -1;
|
688 h->h_first = history_def_first; 689 h->h_next = history_def_next; 690 h->h_last = history_def_last; 691 h->h_prev = history_def_prev; 692 h->h_curr = history_def_curr; 693 h->h_set = history_def_set; 694 h->h_clear = history_def_clear; 695 h->h_enter = history_def_enter; 696 h->h_add = history_def_add; 697 h->h_del = history_def_del; 698 }
| 697 h->h_first = history_def_first; 698 h->h_next = history_def_next; 699 h->h_last = history_def_last; 700 h->h_prev = history_def_prev; 701 h->h_curr = history_def_curr; 702 h->h_set = history_def_set; 703 h->h_clear = history_def_clear; 704 h->h_enter = history_def_enter; 705 h->h_add = history_def_add; 706 h->h_del = history_def_del; 707 }
|
699 return (-1);
| 708 return -1;
|
700 } 701 if (h->h_next == history_def_next) 702 history_def_clear(h->h_ref, &ev); 703 704 h->h_ent = -1; 705 h->h_first = nh->h_first; 706 h->h_next = nh->h_next; 707 h->h_last = nh->h_last; 708 h->h_prev = nh->h_prev; 709 h->h_curr = nh->h_curr; 710 h->h_set = nh->h_set; 711 h->h_clear = nh->h_clear; 712 h->h_enter = nh->h_enter; 713 h->h_add = nh->h_add; 714 h->h_del = nh->h_del; 715
| 709 } 710 if (h->h_next == history_def_next) 711 history_def_clear(h->h_ref, &ev); 712 713 h->h_ent = -1; 714 h->h_first = nh->h_first; 715 h->h_next = nh->h_next; 716 h->h_last = nh->h_last; 717 h->h_prev = nh->h_prev; 718 h->h_curr = nh->h_curr; 719 h->h_set = nh->h_set; 720 h->h_clear = nh->h_clear; 721 h->h_enter = nh->h_enter; 722 h->h_add = nh->h_add; 723 h->h_del = nh->h_del; 724
|
716 return (0);
| 725 return 0;
|
717} 718 719 720/* history_load():
| 726} 727 728 729/* history_load():
|
721 * History load function
| 730 * TYPE(History) load function
|
722 */ 723private int
| 731 */ 732private int
|
724history_load(History *h, const char *fname)
| 733history_load(TYPE(History) *h, const char *fname)
|
725{ 726 FILE *fp; 727 char *line; 728 size_t sz, max_size; 729 char *ptr; 730 int i = -1;
| 734{ 735 FILE *fp; 736 char *line; 737 size_t sz, max_size; 738 char *ptr; 739 int i = -1;
|
731 HistEvent ev;
| 740 TYPE(HistEvent) ev; 741#ifdef WIDECHAR 742 static ct_buffer_t conv; 743#endif
|
732 733 if ((fp = fopen(fname, "r")) == NULL)
| 744 745 if ((fp = fopen(fname, "r")) == NULL)
|
734 return (i);
| 746 return i;
|
735 736 if ((line = fgetln(fp, &sz)) == NULL) 737 goto done; 738 739 if (strncmp(line, hist_cookie, sz) != 0) 740 goto done; 741
| 747 748 if ((line = fgetln(fp, &sz)) == NULL) 749 goto done; 750 751 if (strncmp(line, hist_cookie, sz) != 0) 752 goto done; 753
|
742 ptr = h_malloc(max_size = 1024);
| 754 ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
|
743 if (ptr == NULL) 744 goto done; 745 for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) { 746 char c = line[sz]; 747 748 if (sz != 0 && line[sz - 1] == '\n') 749 line[--sz] = '\0'; 750 else 751 line[sz] = '\0'; 752 753 if (max_size < sz) { 754 char *nptr;
| 755 if (ptr == NULL) 756 goto done; 757 for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) { 758 char c = line[sz]; 759 760 if (sz != 0 && line[sz - 1] == '\n') 761 line[--sz] = '\0'; 762 else 763 line[sz] = '\0'; 764 765 if (max_size < sz) { 766 char *nptr;
|
755 max_size = (sz + 1024) & ~1023; 756 nptr = h_realloc(ptr, max_size);
| 767 max_size = (sz + 1024) & (size_t)~1023; 768 nptr = h_realloc(ptr, max_size * sizeof(*ptr));
|
757 if (nptr == NULL) { 758 i = -1; 759 goto oomem; 760 } 761 ptr = nptr; 762 } 763 (void) strunvis(ptr, line); 764 line[sz] = c;
| 769 if (nptr == NULL) { 770 i = -1; 771 goto oomem; 772 } 773 ptr = nptr; 774 } 775 (void) strunvis(ptr, line); 776 line[sz] = c;
|
765 if (HENTER(h, &ev, ptr) == -1) {
| 777 if (HENTER(h, &ev, ct_decode_string(ptr, &conv)) == -1) {
|
766 i = -1; 767 goto oomem; 768 } 769 } 770oomem:
| 778 i = -1; 779 goto oomem; 780 } 781 } 782oomem:
|
771 h_free((ptr_t)ptr);
| 783 h_free(ptr);
|
772done: 773 (void) fclose(fp);
| 784done: 785 (void) fclose(fp);
|
774 return (i);
| 786 return i;
|
775} 776
| 787} 788
|
| 789
|
777/* history_save_fp():
| 790/* history_save_fp():
|
778 * History save with open FILE*
| 791 * TYPE(History) save function
|
779 */
| 792 */
|
780private int history_save_fp(History *h, FILE* fp)
| 793private int 794history_save_fp(TYPE(History) *h, FILE *fp)
|
781{
| 795{
|
782 HistEvent ev;
| 796 TYPE(HistEvent) ev;
|
783 int i = -1, retval; 784 size_t len, max_size; 785 char *ptr;
| 797 int i = -1, retval; 798 size_t len, max_size; 799 char *ptr;
|
| 800 const char *str; 801#ifdef WIDECHAR 802 static ct_buffer_t conv; 803#endif
|
786 787 if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1) 788 goto done; 789 if (fputs(hist_cookie, fp) == EOF) 790 goto done;
| 804 805 if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1) 806 goto done; 807 if (fputs(hist_cookie, fp) == EOF) 808 goto done;
|
791 ptr = h_malloc(max_size = 1024);
| 809 ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
|
792 if (ptr == NULL) 793 goto done; 794 for (i = 0, retval = HLAST(h, &ev); 795 retval != -1; 796 retval = HPREV(h, &ev), i++) {
| 810 if (ptr == NULL) 811 goto done; 812 for (i = 0, retval = HLAST(h, &ev); 813 retval != -1; 814 retval = HPREV(h, &ev), i++) {
|
797 len = strlen(ev.str) * 4;
| 815 str = ct_encode_string(ev.str, &conv); 816 len = strlen(str) * 4;
|
798 if (len >= max_size) { 799 char *nptr;
| 817 if (len >= max_size) { 818 char *nptr;
|
800 max_size = (len + 1024) & ~1023; 801 nptr = h_realloc(ptr, max_size);
| 819 max_size = (len + 1024) & (size_t)~1023; 820 nptr = h_realloc(ptr, max_size * sizeof(*ptr));
|
802 if (nptr == NULL) { 803 i = -1; 804 goto oomem; 805 } 806 ptr = nptr; 807 }
| 821 if (nptr == NULL) { 822 i = -1; 823 goto oomem; 824 } 825 ptr = nptr; 826 }
|
808 (void) strvis(ptr, ev.str, VIS_WHITE);
| 827 (void) strvis(ptr, str, VIS_WHITE);
|
809 (void) fprintf(fp, "%s\n", ptr); 810 } 811oomem:
| 828 (void) fprintf(fp, "%s\n", ptr); 829 } 830oomem:
|
812 h_free((ptr_t)ptr);
| 831 h_free(ptr);
|
813done:
| 832done:
|
814 return (i); 815
| 833 return i;
|
816} 817 818 819/* history_save():
| 834} 835 836 837/* history_save():
|
820 * History save function
| 838 * History save function
|
821 */ 822private int
| 839 */ 840private int
|
823history_save(History *h, const char *fname)
| 841history_save(TYPE(History) *h, const char *fname)
|
824{
| 842{
|
825 FILE *fp; 826 int i;
| 843 FILE *fp; 844 int i;
|
827
| 845
|
828 if ((fp = fopen(fname, "w")) == NULL) 829 return (-1);
| 846 if ((fp = fopen(fname, "w")) == NULL) 847 return -1;
|
830
| 848
|
831 i = history_save_fp(h, fp);
| 849 i = history_save_fp(h, fp);
|
832
| 850
|
833done: 834 (void) fclose(fp); 835 return (i);
| 851 (void) fclose(fp); 852 return i;
|
836} 837 838 839/* history_prev_event(): 840 * Find the previous event, with number given 841 */ 842private int
| 853} 854 855 856/* history_prev_event(): 857 * Find the previous event, with number given 858 */ 859private int
|
843history_prev_event(History *h, HistEvent *ev, int num)
| 860history_prev_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
844{ 845 int retval; 846 847 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev)) 848 if (ev->num == num)
| 861{ 862 int retval; 863 864 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev)) 865 if (ev->num == num)
|
849 return (0);
| 866 return 0;
|
850 851 he_seterrev(ev, _HE_NOT_FOUND);
| 867 868 he_seterrev(ev, _HE_NOT_FOUND);
|
852 return (-1);
| 869 return -1;
|
853} 854 855 856private int
| 870} 871 872 873private int
|
857history_next_evdata(History *h, HistEvent *ev, int num, void **d)
| 874history_next_evdata(TYPE(History) *h, TYPE(HistEvent) *ev, int num, void **d)
|
858{ 859 int retval; 860 861 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
| 875{ 876 int retval; 877 878 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
|
862 if (num-- <= 0) {
| 879 if (ev->num == num) {
|
863 if (d) 864 *d = ((history_t *)h->h_ref)->cursor->data;
| 880 if (d) 881 *d = ((history_t *)h->h_ref)->cursor->data;
|
865 return (0);
| 882 return 0;
|
866 } 867 868 he_seterrev(ev, _HE_NOT_FOUND);
| 883 } 884 885 he_seterrev(ev, _HE_NOT_FOUND);
|
869 return (-1);
| 886 return -1;
|
870} 871 872 873/* history_next_event(): 874 * Find the next event, with number given 875 */ 876private int
| 887} 888 889 890/* history_next_event(): 891 * Find the next event, with number given 892 */ 893private int
|
877history_next_event(History *h, HistEvent *ev, int num)
| 894history_next_event(TYPE(History) *h, TYPE(HistEvent) *ev, int num)
|
878{ 879 int retval; 880 881 for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev)) 882 if (ev->num == num)
| 895{ 896 int retval; 897 898 for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev)) 899 if (ev->num == num)
|
883 return (0);
| 900 return 0;
|
884 885 he_seterrev(ev, _HE_NOT_FOUND);
| 901 902 he_seterrev(ev, _HE_NOT_FOUND);
|
886 return (-1);
| 903 return -1;
|
887} 888 889 890/* history_prev_string(): 891 * Find the previous event beginning with string 892 */ 893private int
| 904} 905 906 907/* history_prev_string(): 908 * Find the previous event beginning with string 909 */ 910private int
|
894history_prev_string(History *h, HistEvent *ev, const char *str)
| 911history_prev_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
|
895{
| 912{
|
896 size_t len = strlen(str);
| 913 size_t len = Strlen(str);
|
897 int retval; 898 899 for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
| 914 int retval; 915 916 for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
|
900 if (strncmp(str, ev->str, len) == 0) 901 return (0);
| 917 if (Strncmp(str, ev->str, len) == 0) 918 return 0;
|
902 903 he_seterrev(ev, _HE_NOT_FOUND);
| 919 920 he_seterrev(ev, _HE_NOT_FOUND);
|
904 return (-1);
| 921 return -1;
|
905} 906 907 908/* history_next_string(): 909 * Find the next event beginning with string 910 */ 911private int
| 922} 923 924 925/* history_next_string(): 926 * Find the next event beginning with string 927 */ 928private int
|
912history_next_string(History *h, HistEvent *ev, const char *str)
| 929history_next_string(TYPE(History) *h, TYPE(HistEvent) *ev, const Char *str)
|
913{
| 930{
|
914 size_t len = strlen(str);
| 931 size_t len = Strlen(str);
|
915 int retval; 916 917 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
| 932 int retval; 933 934 for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
|
918 if (strncmp(str, ev->str, len) == 0) 919 return (0);
| 935 if (Strncmp(str, ev->str, len) == 0) 936 return 0;
|
920 921 he_seterrev(ev, _HE_NOT_FOUND);
| 937 938 he_seterrev(ev, _HE_NOT_FOUND);
|
922 return (-1);
| 939 return -1;
|
923} 924 925 926/* history(): 927 * User interface to history functions. 928 */ 929int
| 940} 941 942 943/* history(): 944 * User interface to history functions. 945 */ 946int
|
930history(History *h, HistEvent *ev, int fun, ...)
| 947FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
|
931{ 932 va_list va;
| 948{ 949 va_list va;
|
933 const char *str;
| 950 const Char *str;
|
934 int retval; 935 936 va_start(va, fun); 937 938 he_seterrev(ev, _HE_OK); 939 940 switch (fun) { 941 case H_GETSIZE: 942 retval = history_getsize(h, ev); 943 break; 944 945 case H_SETSIZE: 946 retval = history_setsize(h, ev, va_arg(va, int)); 947 break; 948 949 case H_GETUNIQUE: 950 retval = history_getunique(h, ev); 951 break; 952 953 case H_SETUNIQUE: 954 retval = history_setunique(h, ev, va_arg(va, int)); 955 break; 956 957 case H_ADD:
| 951 int retval; 952 953 va_start(va, fun); 954 955 he_seterrev(ev, _HE_OK); 956 957 switch (fun) { 958 case H_GETSIZE: 959 retval = history_getsize(h, ev); 960 break; 961 962 case H_SETSIZE: 963 retval = history_setsize(h, ev, va_arg(va, int)); 964 break; 965 966 case H_GETUNIQUE: 967 retval = history_getunique(h, ev); 968 break; 969 970 case H_SETUNIQUE: 971 retval = history_setunique(h, ev, va_arg(va, int)); 972 break; 973 974 case H_ADD:
|
958 str = va_arg(va, const char *);
| 975 str = va_arg(va, const Char *);
|
959 retval = HADD(h, ev, str); 960 break; 961 962 case H_DEL: 963 retval = HDEL(h, ev, va_arg(va, const int)); 964 break; 965 966 case H_ENTER:
| 976 retval = HADD(h, ev, str); 977 break; 978 979 case H_DEL: 980 retval = HDEL(h, ev, va_arg(va, const int)); 981 break; 982 983 case H_ENTER:
|
967 str = va_arg(va, const char *);
| 984 str = va_arg(va, const Char *);
|
968 if ((retval = HENTER(h, ev, str)) != -1) 969 h->h_ent = ev->num; 970 break; 971 972 case H_APPEND:
| 985 if ((retval = HENTER(h, ev, str)) != -1) 986 h->h_ent = ev->num; 987 break; 988 989 case H_APPEND:
|
973 str = va_arg(va, const char *);
| 990 str = va_arg(va, const Char *);
|
974 if ((retval = HSET(h, ev, h->h_ent)) != -1) 975 retval = HADD(h, ev, str); 976 break; 977 978 case H_FIRST: 979 retval = HFIRST(h, ev); 980 break; 981 982 case H_NEXT: 983 retval = HNEXT(h, ev); 984 break; 985 986 case H_LAST: 987 retval = HLAST(h, ev); 988 break; 989 990 case H_PREV: 991 retval = HPREV(h, ev); 992 break; 993 994 case H_CURR: 995 retval = HCURR(h, ev); 996 break; 997 998 case H_SET: 999 retval = HSET(h, ev, va_arg(va, const int)); 1000 break; 1001 1002 case H_CLEAR: 1003 HCLEAR(h, ev); 1004 retval = 0; 1005 break; 1006 1007 case H_LOAD: 1008 retval = history_load(h, va_arg(va, const char *)); 1009 if (retval == -1) 1010 he_seterrev(ev, _HE_HIST_READ); 1011 break; 1012 1013 case H_SAVE: 1014 retval = history_save(h, va_arg(va, const char *)); 1015 if (retval == -1) 1016 he_seterrev(ev, _HE_HIST_WRITE); 1017 break; 1018 1019 case H_SAVE_FP:
| 991 if ((retval = HSET(h, ev, h->h_ent)) != -1) 992 retval = HADD(h, ev, str); 993 break; 994 995 case H_FIRST: 996 retval = HFIRST(h, ev); 997 break; 998 999 case H_NEXT: 1000 retval = HNEXT(h, ev); 1001 break; 1002 1003 case H_LAST: 1004 retval = HLAST(h, ev); 1005 break; 1006 1007 case H_PREV: 1008 retval = HPREV(h, ev); 1009 break; 1010 1011 case H_CURR: 1012 retval = HCURR(h, ev); 1013 break; 1014 1015 case H_SET: 1016 retval = HSET(h, ev, va_arg(va, const int)); 1017 break; 1018 1019 case H_CLEAR: 1020 HCLEAR(h, ev); 1021 retval = 0; 1022 break; 1023 1024 case H_LOAD: 1025 retval = history_load(h, va_arg(va, const char *)); 1026 if (retval == -1) 1027 he_seterrev(ev, _HE_HIST_READ); 1028 break; 1029 1030 case H_SAVE: 1031 retval = history_save(h, va_arg(va, const char *)); 1032 if (retval == -1) 1033 he_seterrev(ev, _HE_HIST_WRITE); 1034 break; 1035 1036 case H_SAVE_FP:
|
1020 retval = history_save_fp(h, va_arg(va, FILE*));
| 1037 retval = history_save_fp(h, va_arg(va, FILE *));
|
1021 if (retval == -1)
| 1038 if (retval == -1)
|
1022 he_seterrev(ev, _HE_HIST_WRITE);
| 1039 he_seterrev(ev, _HE_HIST_WRITE);
|
1023 break; 1024 1025 case H_PREV_EVENT: 1026 retval = history_prev_event(h, ev, va_arg(va, int)); 1027 break; 1028 1029 case H_NEXT_EVENT: 1030 retval = history_next_event(h, ev, va_arg(va, int)); 1031 break; 1032 1033 case H_PREV_STR:
| 1040 break; 1041 1042 case H_PREV_EVENT: 1043 retval = history_prev_event(h, ev, va_arg(va, int)); 1044 break; 1045 1046 case H_NEXT_EVENT: 1047 retval = history_next_event(h, ev, va_arg(va, int)); 1048 break; 1049 1050 case H_PREV_STR:
|
1034 retval = history_prev_string(h, ev, va_arg(va, const char *));
| 1051 retval = history_prev_string(h, ev, va_arg(va, const Char *));
|
1035 break; 1036 1037 case H_NEXT_STR:
| 1052 break; 1053 1054 case H_NEXT_STR:
|
1038 retval = history_next_string(h, ev, va_arg(va, const char *));
| 1055 retval = history_next_string(h, ev, va_arg(va, const Char *));
|
1039 break; 1040 1041 case H_FUNC: 1042 {
| 1056 break; 1057 1058 case H_FUNC: 1059 {
|
1043 History hf;
| 1060 TYPE(History) hf;
|
1044
| 1061
|
1045 hf.h_ref = va_arg(va, ptr_t);
| 1062 hf.h_ref = va_arg(va, void *);
|
1046 h->h_ent = -1; 1047 hf.h_first = va_arg(va, history_gfun_t); 1048 hf.h_next = va_arg(va, history_gfun_t); 1049 hf.h_last = va_arg(va, history_gfun_t); 1050 hf.h_prev = va_arg(va, history_gfun_t); 1051 hf.h_curr = va_arg(va, history_gfun_t); 1052 hf.h_set = va_arg(va, history_sfun_t); 1053 hf.h_clear = va_arg(va, history_vfun_t); 1054 hf.h_enter = va_arg(va, history_efun_t); 1055 hf.h_add = va_arg(va, history_efun_t); 1056 hf.h_del = va_arg(va, history_sfun_t); 1057 1058 if ((retval = history_set_fun(h, &hf)) == -1) 1059 he_seterrev(ev, _HE_PARAM_MISSING); 1060 break; 1061 } 1062 1063 case H_END:
| 1063 h->h_ent = -1; 1064 hf.h_first = va_arg(va, history_gfun_t); 1065 hf.h_next = va_arg(va, history_gfun_t); 1066 hf.h_last = va_arg(va, history_gfun_t); 1067 hf.h_prev = va_arg(va, history_gfun_t); 1068 hf.h_curr = va_arg(va, history_gfun_t); 1069 hf.h_set = va_arg(va, history_sfun_t); 1070 hf.h_clear = va_arg(va, history_vfun_t); 1071 hf.h_enter = va_arg(va, history_efun_t); 1072 hf.h_add = va_arg(va, history_efun_t); 1073 hf.h_del = va_arg(va, history_sfun_t); 1074 1075 if ((retval = history_set_fun(h, &hf)) == -1) 1076 he_seterrev(ev, _HE_PARAM_MISSING); 1077 break; 1078 } 1079 1080 case H_END:
|
1064 history_end(h);
| 1081 FUN(history,end)(h);
|
1065 retval = 0; 1066 break; 1067 1068 case H_NEXT_EVDATA: 1069 { 1070 int num = va_arg(va, int); 1071 void **d = va_arg(va, void **); 1072 retval = history_next_evdata(h, ev, num, d); 1073 break; 1074 } 1075 1076 case H_DELDATA: 1077 { 1078 int num = va_arg(va, int); 1079 void **d = va_arg(va, void **); 1080 retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d); 1081 break; 1082 } 1083 1084 case H_REPLACE: /* only use after H_NEXT_EVDATA */ 1085 {
| 1082 retval = 0; 1083 break; 1084 1085 case H_NEXT_EVDATA: 1086 { 1087 int num = va_arg(va, int); 1088 void **d = va_arg(va, void **); 1089 retval = history_next_evdata(h, ev, num, d); 1090 break; 1091 } 1092 1093 case H_DELDATA: 1094 { 1095 int num = va_arg(va, int); 1096 void **d = va_arg(va, void **); 1097 retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d); 1098 break; 1099 } 1100 1101 case H_REPLACE: /* only use after H_NEXT_EVDATA */ 1102 {
|
1086 const char *line = va_arg(va, const char *);
| 1103 const Char *line = va_arg(va, const Char *);
|
1087 void *d = va_arg(va, void *);
| 1104 void *d = va_arg(va, void *);
|
1088 const char *s; 1089 if(!line || !(s = strdup(line))) {
| 1105 const Char *s; 1106 if(!line || !(s = Strdup(line))) {
|
1090 retval = -1; 1091 break; 1092 } 1093 ((history_t *)h->h_ref)->cursor->ev.str = s; 1094 ((history_t *)h->h_ref)->cursor->data = d; 1095 retval = 0; 1096 break; 1097 } 1098 1099 default: 1100 retval = -1; 1101 he_seterrev(ev, _HE_UNKNOWN); 1102 break; 1103 } 1104 va_end(va); 1105 return retval; 1106}
| 1107 retval = -1; 1108 break; 1109 } 1110 ((history_t *)h->h_ref)->cursor->ev.str = s; 1111 ((history_t *)h->h_ref)->cursor->data = d; 1112 retval = 0; 1113 break; 1114 } 1115 1116 default: 1117 retval = -1; 1118 he_seterrev(ev, _HE_UNKNOWN); 1119 break; 1120 } 1121 va_end(va); 1122 return retval; 1123}
|