1*60ecde0cSDaniel Fojt /* $NetBSD: hist.c,v 1.34 2019/07/23 10:19:35 christos Exp $ */
232fe07f8SJohn Marino
332fe07f8SJohn Marino /*-
432fe07f8SJohn Marino * Copyright (c) 1992, 1993
532fe07f8SJohn Marino * The Regents of the University of California. All rights reserved.
632fe07f8SJohn Marino *
732fe07f8SJohn Marino * This code is derived from software contributed to Berkeley by
832fe07f8SJohn Marino * Christos Zoulas of Cornell University.
932fe07f8SJohn Marino *
1032fe07f8SJohn Marino * Redistribution and use in source and binary forms, with or without
1132fe07f8SJohn Marino * modification, are permitted provided that the following conditions
1232fe07f8SJohn Marino * are met:
1332fe07f8SJohn Marino * 1. Redistributions of source code must retain the above copyright
1432fe07f8SJohn Marino * notice, this list of conditions and the following disclaimer.
1532fe07f8SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1632fe07f8SJohn Marino * notice, this list of conditions and the following disclaimer in the
1732fe07f8SJohn Marino * documentation and/or other materials provided with the distribution.
1832fe07f8SJohn Marino * 3. Neither the name of the University nor the names of its contributors
1932fe07f8SJohn Marino * may be used to endorse or promote products derived from this software
2032fe07f8SJohn Marino * without specific prior written permission.
2132fe07f8SJohn Marino *
2232fe07f8SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2332fe07f8SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2432fe07f8SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2532fe07f8SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2632fe07f8SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2732fe07f8SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2832fe07f8SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2932fe07f8SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3032fe07f8SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3132fe07f8SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3232fe07f8SJohn Marino * SUCH DAMAGE.
3332fe07f8SJohn Marino */
3432fe07f8SJohn Marino
3532fe07f8SJohn Marino #include "config.h"
3632fe07f8SJohn Marino #if !defined(lint) && !defined(SCCSID)
3732fe07f8SJohn Marino #if 0
3832fe07f8SJohn Marino static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
3932fe07f8SJohn Marino #else
40*60ecde0cSDaniel Fojt __RCSID("$NetBSD: hist.c,v 1.34 2019/07/23 10:19:35 christos Exp $");
4132fe07f8SJohn Marino #endif
4232fe07f8SJohn Marino #endif /* not lint && not SCCSID */
4332fe07f8SJohn Marino
4432fe07f8SJohn Marino /*
4532fe07f8SJohn Marino * hist.c: History access functions
4632fe07f8SJohn Marino */
4732fe07f8SJohn Marino #include <stdlib.h>
4812db70c8Szrj #include <string.h>
4912db70c8Szrj #include <vis.h>
5012db70c8Szrj
5132fe07f8SJohn Marino #include "el.h"
5232fe07f8SJohn Marino
5332fe07f8SJohn Marino /* hist_init():
5432fe07f8SJohn Marino * Initialization function.
5532fe07f8SJohn Marino */
5612db70c8Szrj libedit_private int
hist_init(EditLine * el)5732fe07f8SJohn Marino hist_init(EditLine *el)
5832fe07f8SJohn Marino {
5932fe07f8SJohn Marino
6032fe07f8SJohn Marino el->el_history.fun = NULL;
6132fe07f8SJohn Marino el->el_history.ref = NULL;
62*60ecde0cSDaniel Fojt el->el_history.buf = el_calloc(EL_BUFSIZ, sizeof(*el->el_history.buf));
6332fe07f8SJohn Marino if (el->el_history.buf == NULL)
6432fe07f8SJohn Marino return -1;
65*60ecde0cSDaniel Fojt el->el_history.sz = EL_BUFSIZ;
6632fe07f8SJohn Marino el->el_history.last = el->el_history.buf;
6732fe07f8SJohn Marino return 0;
6832fe07f8SJohn Marino }
6932fe07f8SJohn Marino
7032fe07f8SJohn Marino
7132fe07f8SJohn Marino /* hist_end():
7232fe07f8SJohn Marino * clean up history;
7332fe07f8SJohn Marino */
7412db70c8Szrj libedit_private void
hist_end(EditLine * el)7532fe07f8SJohn Marino hist_end(EditLine *el)
7632fe07f8SJohn Marino {
7732fe07f8SJohn Marino
7832fe07f8SJohn Marino el_free(el->el_history.buf);
7932fe07f8SJohn Marino el->el_history.buf = NULL;
8032fe07f8SJohn Marino }
8132fe07f8SJohn Marino
8232fe07f8SJohn Marino
8332fe07f8SJohn Marino /* hist_set():
8432fe07f8SJohn Marino * Set new history interface
8532fe07f8SJohn Marino */
8612db70c8Szrj libedit_private int
hist_set(EditLine * el,hist_fun_t fun,void * ptr)8732fe07f8SJohn Marino hist_set(EditLine *el, hist_fun_t fun, void *ptr)
8832fe07f8SJohn Marino {
8932fe07f8SJohn Marino
9032fe07f8SJohn Marino el->el_history.ref = ptr;
9132fe07f8SJohn Marino el->el_history.fun = fun;
9232fe07f8SJohn Marino return 0;
9332fe07f8SJohn Marino }
9432fe07f8SJohn Marino
9532fe07f8SJohn Marino
9632fe07f8SJohn Marino /* hist_get():
9732fe07f8SJohn Marino * Get a history line and update it in the buffer.
9832fe07f8SJohn Marino * eventno tells us the event to get.
9932fe07f8SJohn Marino */
10012db70c8Szrj libedit_private el_action_t
hist_get(EditLine * el)10132fe07f8SJohn Marino hist_get(EditLine *el)
10232fe07f8SJohn Marino {
10312db70c8Szrj const wchar_t *hp;
10432fe07f8SJohn Marino int h;
10512db70c8Szrj size_t blen, hlen;
10632fe07f8SJohn Marino
10732fe07f8SJohn Marino if (el->el_history.eventno == 0) { /* if really the current line */
10812db70c8Szrj (void) wcsncpy(el->el_line.buffer, el->el_history.buf,
10932fe07f8SJohn Marino el->el_history.sz);
11032fe07f8SJohn Marino el->el_line.lastchar = el->el_line.buffer +
11132fe07f8SJohn Marino (el->el_history.last - el->el_history.buf);
11232fe07f8SJohn Marino
11332fe07f8SJohn Marino #ifdef KSHVI
11432fe07f8SJohn Marino if (el->el_map.type == MAP_VI)
11532fe07f8SJohn Marino el->el_line.cursor = el->el_line.buffer;
11632fe07f8SJohn Marino else
11732fe07f8SJohn Marino #endif /* KSHVI */
11832fe07f8SJohn Marino el->el_line.cursor = el->el_line.lastchar;
11932fe07f8SJohn Marino
12032fe07f8SJohn Marino return CC_REFRESH;
12132fe07f8SJohn Marino }
12232fe07f8SJohn Marino if (el->el_history.ref == NULL)
12332fe07f8SJohn Marino return CC_ERROR;
12432fe07f8SJohn Marino
12532fe07f8SJohn Marino hp = HIST_FIRST(el);
12632fe07f8SJohn Marino
12732fe07f8SJohn Marino if (hp == NULL)
12832fe07f8SJohn Marino return CC_ERROR;
12932fe07f8SJohn Marino
13032fe07f8SJohn Marino for (h = 1; h < el->el_history.eventno; h++)
13112db70c8Szrj if ((hp = HIST_NEXT(el)) == NULL)
13212db70c8Szrj goto out;
13312db70c8Szrj
13412db70c8Szrj hlen = wcslen(hp) + 1;
13512db70c8Szrj blen = (size_t)(el->el_line.limit - el->el_line.buffer);
13612db70c8Szrj if (hlen > blen && !ch_enlargebufs(el, hlen))
13712db70c8Szrj goto out;
13812db70c8Szrj
13912db70c8Szrj memcpy(el->el_line.buffer, hp, hlen * sizeof(*hp));
14012db70c8Szrj el->el_line.lastchar = el->el_line.buffer + hlen - 1;
14132fe07f8SJohn Marino
14232fe07f8SJohn Marino if (el->el_line.lastchar > el->el_line.buffer
14332fe07f8SJohn Marino && el->el_line.lastchar[-1] == '\n')
14432fe07f8SJohn Marino el->el_line.lastchar--;
14532fe07f8SJohn Marino if (el->el_line.lastchar > el->el_line.buffer
14632fe07f8SJohn Marino && el->el_line.lastchar[-1] == ' ')
14732fe07f8SJohn Marino el->el_line.lastchar--;
14832fe07f8SJohn Marino #ifdef KSHVI
14932fe07f8SJohn Marino if (el->el_map.type == MAP_VI)
15032fe07f8SJohn Marino el->el_line.cursor = el->el_line.buffer;
15132fe07f8SJohn Marino else
15232fe07f8SJohn Marino #endif /* KSHVI */
15332fe07f8SJohn Marino el->el_line.cursor = el->el_line.lastchar;
15432fe07f8SJohn Marino
15532fe07f8SJohn Marino return CC_REFRESH;
15612db70c8Szrj out:
15712db70c8Szrj el->el_history.eventno = h;
15812db70c8Szrj return CC_ERROR;
15912db70c8Szrj
16032fe07f8SJohn Marino }
16132fe07f8SJohn Marino
16232fe07f8SJohn Marino
16332fe07f8SJohn Marino /* hist_command()
16432fe07f8SJohn Marino * process a history command
16532fe07f8SJohn Marino */
16612db70c8Szrj libedit_private int
hist_command(EditLine * el,int argc,const wchar_t ** argv)16712db70c8Szrj hist_command(EditLine *el, int argc, const wchar_t **argv)
16832fe07f8SJohn Marino {
16912db70c8Szrj const wchar_t *str;
17032fe07f8SJohn Marino int num;
17112db70c8Szrj HistEventW ev;
17232fe07f8SJohn Marino
17332fe07f8SJohn Marino if (el->el_history.ref == NULL)
17432fe07f8SJohn Marino return -1;
17532fe07f8SJohn Marino
17612db70c8Szrj if (argc == 1 || wcscmp(argv[1], L"list") == 0) {
17712db70c8Szrj size_t maxlen = 0;
17812db70c8Szrj char *buf = NULL;
17912db70c8Szrj int hno = 1;
18032fe07f8SJohn Marino /* List history entries */
18132fe07f8SJohn Marino
18212db70c8Szrj for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) {
18312db70c8Szrj char *ptr =
18412db70c8Szrj ct_encode_string(str, &el->el_scratch);
18512db70c8Szrj size_t len = strlen(ptr);
18612db70c8Szrj if (len > 0 && ptr[len - 1] == '\n')
18712db70c8Szrj ptr[--len] = '\0';
18812db70c8Szrj len = len * 4 + 1;
18912db70c8Szrj if (len >= maxlen) {
19012db70c8Szrj maxlen = len + 1024;
19112db70c8Szrj char *nbuf = el_realloc(buf, maxlen);
19212db70c8Szrj if (nbuf == NULL) {
19312db70c8Szrj el_free(buf);
19412db70c8Szrj return -1;
19512db70c8Szrj }
19612db70c8Szrj buf = nbuf;
19712db70c8Szrj }
19812db70c8Szrj strvis(buf, ptr, VIS_NL);
19912db70c8Szrj (void) fprintf(el->el_outfile, "%d\t%s\n",
20012db70c8Szrj hno++, buf);
20112db70c8Szrj }
20212db70c8Szrj el_free(buf);
20332fe07f8SJohn Marino return 0;
20432fe07f8SJohn Marino }
20532fe07f8SJohn Marino
20632fe07f8SJohn Marino if (argc != 3)
20732fe07f8SJohn Marino return -1;
20832fe07f8SJohn Marino
20912db70c8Szrj num = (int)wcstol(argv[2], NULL, 0);
21032fe07f8SJohn Marino
21112db70c8Szrj if (wcscmp(argv[1], L"size") == 0)
21212db70c8Szrj return history_w(el->el_history.ref, &ev, H_SETSIZE, num);
21332fe07f8SJohn Marino
21412db70c8Szrj if (wcscmp(argv[1], L"unique") == 0)
21512db70c8Szrj return history_w(el->el_history.ref, &ev, H_SETUNIQUE, num);
21632fe07f8SJohn Marino
21732fe07f8SJohn Marino return -1;
21832fe07f8SJohn Marino }
21932fe07f8SJohn Marino
22032fe07f8SJohn Marino /* hist_enlargebuf()
22132fe07f8SJohn Marino * Enlarge history buffer to specified value. Called from el_enlargebufs().
22232fe07f8SJohn Marino * Return 0 for failure, 1 for success.
22332fe07f8SJohn Marino */
22412db70c8Szrj libedit_private int
22532fe07f8SJohn Marino /*ARGSUSED*/
hist_enlargebuf(EditLine * el,size_t oldsz,size_t newsz)22632fe07f8SJohn Marino hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
22732fe07f8SJohn Marino {
22812db70c8Szrj wchar_t *newbuf;
22932fe07f8SJohn Marino
23032fe07f8SJohn Marino newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
23132fe07f8SJohn Marino if (!newbuf)
23232fe07f8SJohn Marino return 0;
23332fe07f8SJohn Marino
23432fe07f8SJohn Marino (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
23532fe07f8SJohn Marino
23632fe07f8SJohn Marino el->el_history.last = newbuf +
23732fe07f8SJohn Marino (el->el_history.last - el->el_history.buf);
23832fe07f8SJohn Marino el->el_history.buf = newbuf;
23932fe07f8SJohn Marino el->el_history.sz = newsz;
24032fe07f8SJohn Marino
24132fe07f8SJohn Marino return 1;
24232fe07f8SJohn Marino }
24332fe07f8SJohn Marino
24412db70c8Szrj libedit_private wchar_t *
hist_convert(EditLine * el,int fn,void * arg)24532fe07f8SJohn Marino hist_convert(EditLine *el, int fn, void *arg)
24632fe07f8SJohn Marino {
24732fe07f8SJohn Marino HistEventW ev;
24832fe07f8SJohn Marino if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
24932fe07f8SJohn Marino return NULL;
25032fe07f8SJohn Marino return ct_decode_string((const char *)(const void *)ev.str,
25132fe07f8SJohn Marino &el->el_scratch);
25232fe07f8SJohn Marino }
253