history.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /* $NetBSD: history.c,v 1.19 2002/03/18 16:00:54 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #if !defined(lint) && !defined(SCCSID)
  39. #if 0
  40. static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
  41. #else
  42. __RCSID("$NetBSD: history.c,v 1.19 2002/03/18 16:00:54 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * hist.c: History access functions
  47. */
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <stdarg.h>
  51. #ifdef HAVE_VIS_H
  52. #include <vis.h>
  53. #else
  54. #include "np/vis.h"
  55. #endif
  56. #include <sys/stat.h>
  57. static const char hist_cookie[] = "_HiStOrY_V2_\n";
  58. #include "histedit.h"
  59. typedef int (*history_gfun_t)(ptr_t, HistEvent *);
  60. typedef int (*history_efun_t)(ptr_t, HistEvent *, const char *);
  61. typedef void (*history_vfun_t)(ptr_t, HistEvent *);
  62. typedef int (*history_sfun_t)(ptr_t, HistEvent *, const int);
  63. struct history {
  64. ptr_t h_ref; /* Argument for history fcns */
  65. int h_ent; /* Last entry point for history */
  66. history_gfun_t h_first; /* Get the first element */
  67. history_gfun_t h_next; /* Get the next element */
  68. history_gfun_t h_last; /* Get the last element */
  69. history_gfun_t h_prev; /* Get the previous element */
  70. history_gfun_t h_curr; /* Get the current element */
  71. history_sfun_t h_set; /* Set the current element */
  72. history_vfun_t h_clear; /* Clear the history list */
  73. history_efun_t h_enter; /* Add an element */
  74. history_efun_t h_add; /* Append to an element */
  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 h_malloc(a) malloc(a)
  86. #define h_realloc(a, b) realloc((a), (b))
  87. #define h_free(a) free(a)
  88. typedef struct {
  89. int num;
  90. char *str;
  91. } HistEventPrivate;
  92. private int history_setsize(History *, HistEvent *, int);
  93. private int history_getsize(History *, HistEvent *);
  94. private int history_set_fun(History *, History *);
  95. private int history_load(History *, const char *);
  96. private int history_save(History *, const char *);
  97. private int history_prev_event(History *, HistEvent *, int);
  98. private int history_next_event(History *, HistEvent *, int);
  99. private int history_next_string(History *, HistEvent *, const char *);
  100. private int history_prev_string(History *, HistEvent *, const char *);
  101. /***********************************************************************/
  102. /*
  103. * Builtin- history implementation
  104. */
  105. typedef struct hentry_t {
  106. HistEvent ev; /* What we return */
  107. struct hentry_t *next; /* Next entry */
  108. struct hentry_t *prev; /* Previous entry */
  109. } hentry_t;
  110. typedef struct history_t {
  111. hentry_t list; /* Fake list header element */
  112. hentry_t *cursor; /* Current element in the list */
  113. int max; /* Maximum number of events */
  114. int cur; /* Current number of events */
  115. int eventid; /* For generation of unique event id */
  116. } history_t;
  117. private int history_def_first(ptr_t, HistEvent *);
  118. private int history_def_last(ptr_t, HistEvent *);
  119. private int history_def_next(ptr_t, HistEvent *);
  120. private int history_def_prev(ptr_t, HistEvent *);
  121. private int history_def_curr(ptr_t, HistEvent *);
  122. private int history_def_set(ptr_t, HistEvent *, const int n);
  123. private int history_def_enter(ptr_t, HistEvent *, const char *);
  124. private int history_def_add(ptr_t, HistEvent *, const char *);
  125. private void history_def_init(ptr_t *, HistEvent *, int);
  126. private void history_def_clear(ptr_t, HistEvent *);
  127. private int history_def_insert(history_t *, HistEvent *, const char *);
  128. private void history_def_delete(history_t *, HistEvent *, hentry_t *);
  129. #define history_def_setsize(p, num)(void) (((history_t *) p)->max = (num))
  130. #define history_def_getsize(p) (((history_t *) p)->cur)
  131. #define he_strerror(code) he_errlist[code]
  132. #define he_seterrev(evp, code) {\
  133. evp->num = code;\
  134. evp->str = he_strerror(code);\
  135. }
  136. /* error messages */
  137. static const char *const he_errlist[] = {
  138. "OK",
  139. "unknown error",
  140. "malloc() failed",
  141. "first event not found",
  142. "last event not found",
  143. "empty list",
  144. "no next event",
  145. "no previous event",
  146. "current event is invalid",
  147. "event not found",
  148. "can't read history from file",
  149. "can't write history",
  150. "required parameter(s) not supplied",
  151. "history size negative",
  152. "function not allowed with other history-functions-set the default",
  153. "bad parameters"
  154. };
  155. /* error codes */
  156. #define _HE_OK 0
  157. #define _HE_UNKNOWN 1
  158. #define _HE_MALLOC_FAILED 2
  159. #define _HE_FIRST_NOTFOUND 3
  160. #define _HE_LAST_NOTFOUND 4
  161. #define _HE_EMPTY_LIST 5
  162. #define _HE_END_REACHED 6
  163. #define _HE_START_REACHED 7
  164. #define _HE_CURR_INVALID 8
  165. #define _HE_NOT_FOUND 9
  166. #define _HE_HIST_READ 10
  167. #define _HE_HIST_WRITE 11
  168. #define _HE_PARAM_MISSING 12
  169. #define _HE_SIZE_NEGATIVE 13
  170. #define _HE_NOT_ALLOWED 14
  171. #define _HE_BAD_PARAM 15
  172. /* history_def_first():
  173. * Default function to return the first event in the history.
  174. */
  175. private int
  176. history_def_first(ptr_t p, HistEvent *ev)
  177. {
  178. history_t *h = (history_t *) p;
  179. h->cursor = h->list.next;
  180. if (h->cursor != &h->list)
  181. *ev = h->cursor->ev;
  182. else {
  183. he_seterrev(ev, _HE_FIRST_NOTFOUND);
  184. return (-1);
  185. }
  186. return (0);
  187. }
  188. /* history_def_last():
  189. * Default function to return the last event in the history.
  190. */
  191. private int
  192. history_def_last(ptr_t p, HistEvent *ev)
  193. {
  194. history_t *h = (history_t *) p;
  195. h->cursor = h->list.prev;
  196. if (h->cursor != &h->list)
  197. *ev = h->cursor->ev;
  198. else {
  199. he_seterrev(ev, _HE_LAST_NOTFOUND);
  200. return (-1);
  201. }
  202. return (0);
  203. }
  204. /* history_def_next():
  205. * Default function to return the next event in the history.
  206. */
  207. private int
  208. history_def_next(ptr_t p, HistEvent *ev)
  209. {
  210. history_t *h = (history_t *) p;
  211. if (h->cursor != &h->list)
  212. h->cursor = h->cursor->next;
  213. else {
  214. he_seterrev(ev, _HE_EMPTY_LIST);
  215. return (-1);
  216. }
  217. if (h->cursor != &h->list)
  218. *ev = h->cursor->ev;
  219. else {
  220. he_seterrev(ev, _HE_END_REACHED);
  221. return (-1);
  222. }
  223. return (0);
  224. }
  225. /* history_def_prev():
  226. * Default function to return the previous event in the history.
  227. */
  228. private int
  229. history_def_prev(ptr_t p, HistEvent *ev)
  230. {
  231. history_t *h = (history_t *) p;
  232. if (h->cursor != &h->list)
  233. h->cursor = h->cursor->prev;
  234. else {
  235. he_seterrev(ev,
  236. (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);
  237. return (-1);
  238. }
  239. if (h->cursor != &h->list)
  240. *ev = h->cursor->ev;
  241. else {
  242. he_seterrev(ev, _HE_START_REACHED);
  243. return (-1);
  244. }
  245. return (0);
  246. }
  247. /* history_def_curr():
  248. * Default function to return the current event in the history.
  249. */
  250. private int
  251. history_def_curr(ptr_t p, HistEvent *ev)
  252. {
  253. history_t *h = (history_t *) p;
  254. if (h->cursor != &h->list)
  255. *ev = h->cursor->ev;
  256. else {
  257. he_seterrev(ev,
  258. (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);
  259. return (-1);
  260. }
  261. return (0);
  262. }
  263. /* history_def_set():
  264. * Default function to set the current event in the history to the
  265. * given one.
  266. */
  267. private int
  268. history_def_set(ptr_t p, HistEvent *ev, const int n)
  269. {
  270. history_t *h = (history_t *) p;
  271. if (h->cur == 0) {
  272. he_seterrev(ev, _HE_EMPTY_LIST);
  273. return (-1);
  274. }
  275. if (h->cursor == &h->list || h->cursor->ev.num != n) {
  276. for (h->cursor = h->list.next; h->cursor != &h->list;
  277. h->cursor = h->cursor->next)
  278. if (h->cursor->ev.num == n)
  279. break;
  280. }
  281. if (h->cursor == &h->list) {
  282. he_seterrev(ev, _HE_NOT_FOUND);
  283. return (-1);
  284. }
  285. return (0);
  286. }
  287. /* history_def_add():
  288. * Append string to element
  289. */
  290. private int
  291. history_def_add(ptr_t p, HistEvent *ev, const char *str)
  292. {
  293. history_t *h = (history_t *) p;
  294. size_t len;
  295. char *s;
  296. HistEventPrivate *evp = (void *)&h->cursor->ev;
  297. if (h->cursor == &h->list)
  298. return (history_def_enter(p, ev, str));
  299. len = strlen(evp->str) + strlen(str) + 1;
  300. s = (char *) h_malloc(len);
  301. if (!s) {
  302. he_seterrev(ev, _HE_MALLOC_FAILED);
  303. return (-1);
  304. }
  305. (void) strlcpy(s, h->cursor->ev.str, len);
  306. (void) strlcat(s, str, len);
  307. h_free(evp->str);
  308. evp->str = s;
  309. *ev = h->cursor->ev;
  310. return (0);
  311. }
  312. /* history_def_delete():
  313. * Delete element hp of the h list
  314. */
  315. /* ARGSUSED */
  316. private void
  317. history_def_delete(history_t *h, HistEvent *ev, hentry_t *hp)
  318. {
  319. HistEventPrivate *evp = (void *)&hp->ev;
  320. if (hp == &h->list)
  321. abort();
  322. hp->prev->next = hp->next;
  323. hp->next->prev = hp->prev;
  324. h_free((ptr_t) evp->str);
  325. h_free(hp);
  326. h->cur--;
  327. }
  328. /* history_def_insert():
  329. * Insert element with string str in the h list
  330. */
  331. private int
  332. history_def_insert(history_t *h, HistEvent *ev, const char *str)
  333. {
  334. h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
  335. if (h->cursor)
  336. h->cursor->ev.str = strdup(str);
  337. if (!h->cursor || !h->cursor->ev.str) {
  338. he_seterrev(ev, _HE_MALLOC_FAILED);
  339. return (-1);
  340. }
  341. h->cursor->ev.num = ++h->eventid;
  342. h->cursor->next = h->list.next;
  343. h->cursor->prev = &h->list;
  344. h->list.next->prev = h->cursor;
  345. h->list.next = h->cursor;
  346. h->cur++;
  347. *ev = h->cursor->ev;
  348. return (0);
  349. }
  350. /* history_def_enter():
  351. * Default function to enter an item in the history
  352. */
  353. private int
  354. history_def_enter(ptr_t p, HistEvent *ev, const char *str)
  355. {
  356. history_t *h = (history_t *) p;
  357. if (history_def_insert(h, ev, str) == -1)
  358. return (-1); /* error, keep error message */
  359. /*
  360. * Always keep at least one entry.
  361. * This way we don't have to check for the empty list.
  362. */
  363. while (h->cur > h->max && h->cur > 0)
  364. history_def_delete(h, ev, h->list.prev);
  365. return (0);
  366. }
  367. /* history_def_init():
  368. * Default history initialization function
  369. */
  370. /* ARGSUSED */
  371. private void
  372. history_def_init(ptr_t *p, HistEvent *ev, int n)
  373. {
  374. history_t *h = (history_t *) h_malloc(sizeof(history_t));
  375. if (n <= 0)
  376. n = 0;
  377. h->eventid = 0;
  378. h->cur = 0;
  379. h->max = n;
  380. h->list.next = h->list.prev = &h->list;
  381. h->list.ev.str = NULL;
  382. h->list.ev.num = 0;
  383. h->cursor = &h->list;
  384. *p = (ptr_t) h;
  385. }
  386. /* history_def_clear():
  387. * Default history cleanup function
  388. */
  389. private void
  390. history_def_clear(ptr_t p, HistEvent *ev)
  391. {
  392. history_t *h = (history_t *) p;
  393. while (h->list.prev != &h->list)
  394. history_def_delete(h, ev, h->list.prev);
  395. h->eventid = 0;
  396. h->cur = 0;
  397. }
  398. /************************************************************************/
  399. /* history_init():
  400. * Initialization function.
  401. */
  402. public History *
  403. history_init(void)
  404. {
  405. History *h = (History *) h_malloc(sizeof(History));
  406. HistEvent ev;
  407. history_def_init(&h->h_ref, &ev, 0);
  408. h->h_ent = -1;
  409. h->h_next = history_def_next;
  410. h->h_first = history_def_first;
  411. h->h_last = history_def_last;
  412. h->h_prev = history_def_prev;
  413. h->h_curr = history_def_curr;
  414. h->h_set = history_def_set;
  415. h->h_clear = history_def_clear;
  416. h->h_enter = history_def_enter;
  417. h->h_add = history_def_add;
  418. return (h);
  419. }
  420. /* history_end():
  421. * clean up history;
  422. */
  423. public void
  424. history_end(History *h)
  425. {
  426. HistEvent ev;
  427. if (h->h_next == history_def_next)
  428. history_def_clear(h->h_ref, &ev);
  429. }
  430. /* history_setsize():
  431. * Set history number of events
  432. */
  433. private int
  434. history_setsize(History *h, HistEvent *ev, int num)
  435. {
  436. if (h->h_next != history_def_next) {
  437. he_seterrev(ev, _HE_NOT_ALLOWED);
  438. return (-1);
  439. }
  440. if (num < 0) {
  441. he_seterrev(ev, _HE_BAD_PARAM);
  442. return (-1);
  443. }
  444. history_def_setsize(h->h_ref, num);
  445. return (0);
  446. }
  447. /* history_getsize():
  448. * Get number of events currently in history
  449. */
  450. private int
  451. history_getsize(History *h, HistEvent *ev)
  452. {
  453. int retval = 0;
  454. if (h->h_next != history_def_next) {
  455. he_seterrev(ev, _HE_NOT_ALLOWED);
  456. return (-1);
  457. }
  458. retval = history_def_getsize(h->h_ref);
  459. if (retval < -1) {
  460. he_seterrev(ev, _HE_SIZE_NEGATIVE);
  461. return (-1);
  462. }
  463. ev->num = retval;
  464. return (0);
  465. }
  466. /* history_set_fun():
  467. * Set history functions
  468. */
  469. private int
  470. history_set_fun(History *h, History *nh)
  471. {
  472. HistEvent ev;
  473. if (nh->h_first == NULL || nh->h_next == NULL || nh->h_last == NULL ||
  474. nh->h_prev == NULL || nh->h_curr == NULL || nh->h_set == NULL ||
  475. nh->h_enter == NULL || nh->h_add == NULL || nh->h_clear == NULL ||
  476. nh->h_ref == NULL) {
  477. if (h->h_next != history_def_next) {
  478. history_def_init(&h->h_ref, &ev, 0);
  479. h->h_first = history_def_first;
  480. h->h_next = history_def_next;
  481. h->h_last = history_def_last;
  482. h->h_prev = history_def_prev;
  483. h->h_curr = history_def_curr;
  484. h->h_set = history_def_set;
  485. h->h_clear = history_def_clear;
  486. h->h_enter = history_def_enter;
  487. h->h_add = history_def_add;
  488. }
  489. return (-1);
  490. }
  491. if (h->h_next == history_def_next)
  492. history_def_clear(h->h_ref, &ev);
  493. h->h_ent = -1;
  494. h->h_first = nh->h_first;
  495. h->h_next = nh->h_next;
  496. h->h_last = nh->h_last;
  497. h->h_prev = nh->h_prev;
  498. h->h_curr = nh->h_curr;
  499. h->h_set = nh->h_set;
  500. h->h_clear = nh->h_clear;
  501. h->h_enter = nh->h_enter;
  502. h->h_add = nh->h_add;
  503. return (0);
  504. }
  505. /* history_load():
  506. * History load function
  507. */
  508. private int
  509. history_load(History *h, const char *fname)
  510. {
  511. FILE *fp;
  512. char *line;
  513. size_t sz, max_size;
  514. char *ptr;
  515. int i = -1;
  516. HistEvent ev;
  517. if ((fp = fopen(fname, "r")) == NULL)
  518. return (i);
  519. if ((line = fgetln(fp, &sz)) == NULL)
  520. goto done;
  521. if (strncmp(line, hist_cookie, sz) != 0)
  522. goto done;
  523. ptr = h_malloc(max_size = 1024);
  524. for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
  525. char c = line[sz];
  526. if (sz != 0 && line[sz - 1] == '\n')
  527. line[--sz] = '\0';
  528. else
  529. line[sz] = '\0';
  530. if (max_size < sz) {
  531. max_size = (sz + 1023) & ~1023;
  532. ptr = h_realloc(ptr, max_size);
  533. }
  534. (void) strunvis(ptr, line);
  535. line[sz] = c;
  536. HENTER(h, &ev, ptr);
  537. }
  538. h_free(ptr);
  539. done:
  540. (void) fclose(fp);
  541. return (i);
  542. }
  543. /* history_save():
  544. * History save function
  545. */
  546. private int
  547. history_save(History *h, const char *fname)
  548. {
  549. FILE *fp;
  550. HistEvent ev;
  551. int i = 0, retval;
  552. size_t len, max_size;
  553. char *ptr;
  554. if ((fp = fopen(fname, "w")) == NULL)
  555. return (-1);
  556. (void) fchmod(fileno(fp), S_IRUSR|S_IWUSR);
  557. (void) fputs(hist_cookie, fp);
  558. ptr = h_malloc(max_size = 1024);
  559. for (retval = HLAST(h, &ev);
  560. retval != -1;
  561. retval = HPREV(h, &ev), i++) {
  562. len = strlen(ev.str) * 4;
  563. if (len >= max_size) {
  564. max_size = (len + 1023) & 1023;
  565. ptr = h_realloc(ptr, max_size);
  566. }
  567. (void) strvis(ptr, ev.str, VIS_WHITE);
  568. (void) fprintf(fp, "%s\n", ev.str);
  569. }
  570. h_free(ptr);
  571. (void) fclose(fp);
  572. return (i);
  573. }
  574. /* history_prev_event():
  575. * Find the previous event, with number given
  576. */
  577. private int
  578. history_prev_event(History *h, HistEvent *ev, int num)
  579. {
  580. int retval;
  581. for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
  582. if (ev->num == num)
  583. return (0);
  584. he_seterrev(ev, _HE_NOT_FOUND);
  585. return (-1);
  586. }
  587. /* history_next_event():
  588. * Find the next event, with number given
  589. */
  590. private int
  591. history_next_event(History *h, HistEvent *ev, int num)
  592. {
  593. int retval;
  594. for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
  595. if (ev->num == num)
  596. return (0);
  597. he_seterrev(ev, _HE_NOT_FOUND);
  598. return (-1);
  599. }
  600. /* history_prev_string():
  601. * Find the previous event beginning with string
  602. */
  603. private int
  604. history_prev_string(History *h, HistEvent *ev, const char *str)
  605. {
  606. size_t len = strlen(str);
  607. int retval;
  608. for (retval = HCURR(h, ev); retval != -1; retval = HNEXT(h, ev))
  609. if (strncmp(str, ev->str, len) == 0)
  610. return (0);
  611. he_seterrev(ev, _HE_NOT_FOUND);
  612. return (-1);
  613. }
  614. /* history_next_string():
  615. * Find the next event beginning with string
  616. */
  617. private int
  618. history_next_string(History *h, HistEvent *ev, const char *str)
  619. {
  620. size_t len = strlen(str);
  621. int retval;
  622. for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
  623. if (strncmp(str, ev->str, len) == 0)
  624. return (0);
  625. he_seterrev(ev, _HE_NOT_FOUND);
  626. return (-1);
  627. }
  628. /* history():
  629. * User interface to history functions.
  630. */
  631. int
  632. history(History *h, HistEvent *ev, int fun, ...)
  633. {
  634. va_list va;
  635. const char *str;
  636. int retval;
  637. va_start(va, fun);
  638. he_seterrev(ev, _HE_OK);
  639. switch (fun) {
  640. case H_GETSIZE:
  641. retval = history_getsize(h, ev);
  642. break;
  643. case H_SETSIZE:
  644. retval = history_setsize(h, ev, va_arg(va, int));
  645. break;
  646. case H_ADD:
  647. str = va_arg(va, const char *);
  648. retval = HADD(h, ev, str);
  649. break;
  650. case H_ENTER:
  651. str = va_arg(va, const char *);
  652. if ((retval = HENTER(h, ev, str)) != -1)
  653. h->h_ent = ev->num;
  654. break;
  655. case H_APPEND:
  656. str = va_arg(va, const char *);
  657. if ((retval = HSET(h, ev, h->h_ent)) != -1)
  658. retval = HADD(h, ev, str);
  659. break;
  660. case H_FIRST:
  661. retval = HFIRST(h, ev);
  662. break;
  663. case H_NEXT:
  664. retval = HNEXT(h, ev);
  665. break;
  666. case H_LAST:
  667. retval = HLAST(h, ev);
  668. break;
  669. case H_PREV:
  670. retval = HPREV(h, ev);
  671. break;
  672. case H_CURR:
  673. retval = HCURR(h, ev);
  674. break;
  675. case H_SET:
  676. retval = HSET(h, ev, va_arg(va, const int));
  677. break;
  678. case H_CLEAR:
  679. HCLEAR(h, ev);
  680. retval = 0;
  681. break;
  682. case H_LOAD:
  683. retval = history_load(h, va_arg(va, const char *));
  684. if (retval == -1)
  685. he_seterrev(ev, _HE_HIST_READ);
  686. break;
  687. case H_SAVE:
  688. retval = history_save(h, va_arg(va, const char *));
  689. if (retval == -1)
  690. he_seterrev(ev, _HE_HIST_WRITE);
  691. break;
  692. case H_PREV_EVENT:
  693. retval = history_prev_event(h, ev, va_arg(va, int));
  694. break;
  695. case H_NEXT_EVENT:
  696. retval = history_next_event(h, ev, va_arg(va, int));
  697. break;
  698. case H_PREV_STR:
  699. retval = history_prev_string(h, ev, va_arg(va, const char *));
  700. break;
  701. case H_NEXT_STR:
  702. retval = history_next_string(h, ev, va_arg(va, const char *));
  703. break;
  704. case H_FUNC:
  705. {
  706. History hf;
  707. hf.h_ref = va_arg(va, ptr_t);
  708. h->h_ent = -1;
  709. hf.h_first = va_arg(va, history_gfun_t);
  710. hf.h_next = va_arg(va, history_gfun_t);
  711. hf.h_last = va_arg(va, history_gfun_t);
  712. hf.h_prev = va_arg(va, history_gfun_t);
  713. hf.h_curr = va_arg(va, history_gfun_t);
  714. hf.h_set = va_arg(va, history_sfun_t);
  715. hf.h_clear = va_arg(va, history_vfun_t);
  716. hf.h_enter = va_arg(va, history_efun_t);
  717. hf.h_add = va_arg(va, history_efun_t);
  718. if ((retval = history_set_fun(h, &hf)) == -1)
  719. he_seterrev(ev, _HE_PARAM_MISSING);
  720. break;
  721. }
  722. case H_END:
  723. history_end(h);
  724. retval = 0;
  725. break;
  726. default:
  727. retval = -1;
  728. he_seterrev(ev, _HE_UNKNOWN);
  729. break;
  730. }
  731. va_end(va);
  732. return (retval);
  733. }