Debian Codegraph @

packages/seacas/libraries/aprepro_lib/apr_getline.cc

raw
1
2 /*
3 * Copyright (C) 1991, 1992, 1993, 2021, 2022, 2023, 2024 by Chris Thewalt ([email protected])
4 *
5 * Permission to use, copy, modify, and distribute this software
6 * for any purpose and without fee is hereby granted, provided
7 * that the above copyright notices appear in all copies and that both the
8 * copyright notice and this permission notice appear in supporting
9 * documentation. This software is provided "as is" without express or
10 * implied warranty.
11 *
12 * Thanks to the following people who have provided enhancements and fixes:
13 * Ron Ueberschaer, Christoph Keller, Scott Schwartz, Steven List,
14 * DaviD W. Sanderson, Goran Bostrom, Michael Gleason, Glenn Kasten,
15 * Edin Hodzic, Eric J Bivona, Kai Uwe Rommel, Danny Quah, Ulrich Betzler
16 */
17
18 /*
19 * Note: This version has been updated by Mike Gleason <[email protected]>
20 */
21 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
22 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
23
24 #define __windows__ 1
25 #include <conio.h>
26 #include <io.h>
27 #define NOMINMAX
28 #include <windows.h>
29 #define sleep(a) Sleep(a * 1000)
30 #ifndef write
31 #define write _write
32 #define read _read
33 #endif
34 #else
35
36 #ifndef __unix__
37 #define __unix__ 1
38 #endif
39 #endif
40
41 /********************* C library headers ********************************/
42 #include <array>
43 #include <cctype>
44 #include <cerrno>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
48 #ifndef _MSC_VER
49 #include <unistd.h>
50 #endif
51
52 #include "apr_getline.h"
53
54 namespace {
55 const int GL_BUF_SIZE{1024};
56
57 /******************** external interface *********************************/
58
59 bool gl_ellipses_during_completion = true;
60 char gl_buf[GL_BUF_SIZE]; /* input buffer */
61
62 int gl_init_done = -1; /* terminal mode flag */
63 int gl_termw = 80; /* actual terminal width */
64 int gl_scroll = 27; /* width of EOL scrolling region */
65 int gl_width = 0; /* net size available for input */
66 int gl_extent = 0; /* how far to redraw, 0 means all */
67 int gl_overwrite = 0; /* overwrite mode */
68 int gl_pos, gl_cnt = 0; /* position and size of input */
69 char gl_killbuf[GL_BUF_SIZE] = ""; /* killed text */
70 const char *gl_prompt; /* to save the prompt string */
71 int gl_search_mode = 0; /* search mode flag */
72
73 void gl_init(); /* prepare to edit a line */
74 void gl_cleanup(); /* to undo gl_init */
75 void gl_char_init(); /* get ready for no echo input */
76 void gl_char_cleanup(); /* undo gl_char_init */
77 /* returns printable prompt width */
78
79 void gl_addchar(int c); /* install specified char */
80 void gl_del(int loc, int); /* del, either left (-1) or cur (0) */
81
82 [[noreturn]] void gl_error(const char *const buf); /* write error msg and die */
83
84 void gl_fixup(const char *prompt, int change, int cursor); /* fixup state variables and screen */
85 int gl_getc(); /* read one char from terminal */
86 void gl_kill(int pos); /* delete to EOL */
87 void gl_newline(); /* handle \n or \r */
88 void gl_putc(int c); /* write one char to terminal */
89 void gl_puts(const char *const buf); /* write a line to terminal */
90 void gl_redraw(); /* issue \n and redraw all */
91 void gl_transpose(); /* transpose two chars */
92 void gl_yank(); /* yank killed text */
93
94 void hist_init(); /* initializes hist pointers */
95 char *hist_next(); /* return ptr to next item */
96 char *hist_prev(); /* return ptr to prev item */
97 char *hist_save(const char *p); /* makes copy of a string, without NL */
98
99 void search_addchar(int c); /* increment search string */
100 void search_term(); /* reset with current contents */
101 void search_back(int new_search); /* look back for current string */
102 void search_forw(int new_search); /* look forw for current string */
103 void gl_beep(); /* try to play a system beep sound */
104
105 char *copy_string(char *dest, char const *source, long int elements)
106 {
107 char *d;
108 for (d = dest; d + 1 < dest + elements && *source; d++, source++) {
109 *d = *source;
110 }
111 *d = '\0';
112 return d;
113 }
114 } // namespace
115 /************************ nonportable part *********************************/
116
117 #ifdef MSDOS
118 #include <bios.h>
119 #endif
120
121 namespace {
122 #ifdef __unix__
123 #include <termios.h>
124 struct termios io_new_termios, io_old_termios;
125 #endif
126
127 void gl_char_init() /* turn off input echo */
128 {
129 #ifdef __unix__
130 tcgetattr(0, &io_old_termios);
131 io_new_termios = io_old_termios;
132 io_new_termios.c_iflag &= ~(BRKINT | ISTRIP | IXON | IXOFF);
133 io_new_termios.c_iflag |= (IGNBRK | IGNPAR);
134 io_new_termios.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
135 io_new_termios.c_cc[VMIN] = 1;
136 io_new_termios.c_cc[VTIME] = 0;
137 tcsetattr(0, TCSANOW, &io_new_termios);
138 #endif /* __unix__ */
139 }
140
141 void gl_char_cleanup() /* undo effects of gl_char_init */
142 {
143 #ifdef __unix__
144 tcsetattr(0, TCSANOW, &io_old_termios);
145 #endif /* __unix__ */
146 }
147 } // namespace
148 #if defined(MSDOS) || defined(__windows__)
149
150 #define K_UP 0x48
151 #define K_DOWN 0x50
152 #define K_LEFT 0x4B
153 #define K_RIGHT 0x4D
154 #define K_DELETE 0x53
155 #define K_INSERT 0x52
156 #define K_HOME 0x47
157 #define K_END 0x4F
158 #define K_PGUP 0x49
159 #define K_PGDN 0x51
160
161 int pc_keymap(int c)
162 {
163 switch (c) {
164 case K_UP:
165 case K_PGUP:
166 c = 16; /* up -> ^P */
167 break;
168 case K_DOWN:
169 case K_PGDN:
170 c = 14; /* down -> ^N */
171 break;
172 case K_LEFT:
173 c = 2; /* left -> ^B */
174 break;
175 case K_RIGHT:
176 c = 6; /* right -> ^F */
177 break;
178 case K_END:
179 c = 5; /* end -> ^E */
180 break;
181 case K_HOME:
182 c = 1; /* home -> ^A */
183 break;
184 case K_INSERT:
185 c = 15; /* insert -> ^O */
186 break;
187 case K_DELETE:
188 c = 4; /* del -> ^D */
189 break;
190 default: c = 0; /* make it garbage */
191 }
192 return c;
193 }
194 #endif /* defined(MSDOS) || defined(__windows__) */
195
196 namespace {
197 int gl_getc()
198 /* get a character without echoing it to screen */
199 {
200 #ifdef __unix__
201 char ch;
202 while (read(0, &ch, 1) == -1) {
203 if (errno != EINTR) {
204 break;
205 }
206 }
207 int c = (ch <= 0) ? -1 : ch;
208 #endif /* __unix__ */
209 #ifdef MSDOS
210 int c = _bios_keybrd(_NKEYBRD_READ);
211 if ((c & 0377) == 224) {
212 c = pc_keymap((c >> 8) & 0377);
213 }
214 else {
215 c &= 0377;
216 }
217 #endif /* MSDOS */
218 #ifdef __windows__
219 int c = (int)_getch();
220 if ((c == 0) || (c == 0xE0)) {
221 /* Read key code */
222 c = (int)_getch();
223 c = pc_keymap(c);
224 }
225 else if (c == '\r') {
226 /* Note: we only get \r from the console,
227 * and not a matching \n.
228 */
229 c = '\n';
230 }
231 #endif
232 return c;
233 }
234
235 void gl_putc(int c)
236 {
237 char ch = (char)(unsigned char)c;
238
239 write(1, &ch, 1);
240 if (ch == '\n') {
241 ch = '\r';
242 write(1, &ch, 1); /* RAW mode needs '\r', does not hurt */
243 }
244 }
245
246 /******************** fairly portable part *********************************/
247
248 void gl_puts(const char *const buf)
249 {
250 if (buf) {
251 auto len = strlen(buf);
252 write(1, buf, len);
253 }
254 }
255
256 [[noreturn]] void gl_error(const char *const buf)
257 {
258 auto len = strlen(buf);
259
260 gl_cleanup();
261 write(2, buf, len);
262 exit(1);
263 }
264
265 void gl_init()
266 /* set up variables and terminal */
267 {
268 if (gl_init_done < 0) { /* -1 only on startup */
269 const char *cp = (const char *)getenv("COLUMNS");
270 if (cp != nullptr) {
271 auto w = strtoul(cp, nullptr, 10);
272 if (w > 20)
273 SEAMS::gl_setwidth(w);
274 }
275 hist_init();
276 }
277 if (isatty(0) == 0 || isatty(1) == 0)
278 gl_error("\n*** Error: getline(): not interactive, use stdio.\n");
279 gl_char_init();
280 gl_init_done = 1;
281 }
282
283 void gl_cleanup()
284 /* undo effects of gl_init, as necessary */
285 {
286 if (gl_init_done > 0) {
287 gl_char_cleanup();
288 }
289 gl_init_done = 0;
290 #ifdef __windows__
291 Sleep(40);
292 FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
293 #endif
294 }
295 } // namespace
296
297 namespace SEAMS {
298 void gl_setwidth(size_t w)
299 {
300 if (w > 250) {
301 w = 250;
302 }
303 if (w > 20) {
304 gl_termw = w;
305 gl_scroll = w / 3;
306 }
307 else {
308 gl_error("\n*** Error: minimum screen width is 21\n");
309 }
310 }
311
312 char *getline_int(const char *prompt)
313 {
314 gl_init();
315 gl_prompt = (prompt) ? prompt : "";
316 gl_buf[0] = '\0';
317 gl_fixup(gl_prompt, -2, GL_BUF_SIZE);
318
319 #ifdef __windows__
320 FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
321 #endif
322
323 int c;
324 while ((c = gl_getc()) >= 0) {
325 gl_extent = 0; /* reset to full extent */
326 if (isprint(c)) {
327 if (gl_search_mode) {
328 search_addchar(c);
329 }
330 else {
331 gl_addchar(c);
332 }
333 }
334 else {
335 if (gl_search_mode) {
336 if (c == '\033' || c == '\016' || c == '\020') {
337 search_term();
338 c = 0; /* ignore the character */
339 }
340 else if (c == '\010' || c == '\177') {
341 search_addchar(-1); /* unwind search string */
342 c = 0;
343 }
344 else if (c != '\022' && c != '\023') {
345 search_term(); /* terminate and handle char */
346 }
347 }
348 switch (c) {
349 case '\n':
350 case '\r': /* newline */
351 gl_newline();
352 gl_cleanup();
353 return gl_buf;
354 case '\001':
355 gl_fixup(gl_prompt, -1, 0); /* ^A */
356 break;
357 case '\002':
358 gl_fixup(gl_prompt, -1, gl_pos - 1); /* ^B */
359 break;
360 case '\004': /* ^D */
361 if (gl_cnt == 0) {
362 gl_buf[0] = '\0';
363 gl_cleanup();
364 gl_putc('\n');
365 return gl_buf;
366 }
367 else {
368 gl_del(0, 1);
369 }
370 break;
371 case '\005':
372 gl_fixup(gl_prompt, -1, gl_cnt); /* ^E */
373 break;
374 case '\006':
375 gl_fixup(gl_prompt, -1, gl_pos + 1); /* ^F */
376 break;
377 case '\010':
378 case '\177':
379 gl_del(-1, 0); /* ^H and DEL */
380 break;
381 case '\t': /* TAB */ break;
382 case '\013':
383 gl_kill(gl_pos); /* ^K */
384 break;
385 case '\014':
386 gl_redraw(); /* ^L */
387 break;
388 case '\016': /* ^N */
389 copy_string(gl_buf, hist_next(), GL_BUF_SIZE);
390 gl_fixup(gl_prompt, 0, GL_BUF_SIZE);
391 break;
392 case '\017':
393 gl_overwrite = !gl_overwrite; /* ^O */
394 break;
395 case '\020': /* ^P */
396 copy_string(gl_buf, hist_prev(), GL_BUF_SIZE);
397 gl_fixup(gl_prompt, 0, GL_BUF_SIZE);
398 break;
399 case '\022':
400 search_back(1); /* ^R */
401 break;
402 case '\023':
403 search_forw(1); /* ^S */
404 break;
405 case '\024':
406 gl_transpose(); /* ^T */
407 break;
408 case '\025':
409 gl_kill(0); /* ^U */
410 break;
411 case '\031':
412 gl_yank(); /* ^Y */
413 break;
414 default:
415 if (c > 0)
416 gl_beep();
417 break;
418 }
419 }
420 }
421 gl_cleanup();
422 gl_buf[0] = '\0';
423 return gl_buf;
424 }
425 } // namespace SEAMS
426 namespace {
427 void gl_addchar(int c)
428
429 /* adds the character c to the input buffer at current location */
430 {
431
432 if (gl_overwrite == 0 || gl_pos == gl_cnt) {
433 if (gl_cnt > GL_BUF_SIZE - 2) {
434 gl_error("\n*** Error: getline(): input buffer overflow\n");
435 }
436 for (int i = gl_cnt; i >= gl_pos; i--) {
437 gl_buf[i + 1] = gl_buf[i];
438 }
439 gl_buf[gl_pos] = (char)c;
440 gl_fixup(gl_prompt, gl_pos, gl_pos + 1);
441 }
442 else {
443 if (gl_pos > GL_BUF_SIZE - 1) {
444 gl_error("\n*** Error: getline(): input buffer overflow\n");
445 }
446 gl_buf[gl_pos] = (char)c;
447 gl_extent = 1;
448 gl_fixup(gl_prompt, gl_pos, gl_pos + 1);
449 }
450 }
451
452 void gl_yank()
453 /* adds the kill buffer to the input buffer at current location */
454 {
455 int len = strlen(gl_killbuf);
456 if (len > 0) {
457 if (gl_overwrite == 0) {
458 if (gl_cnt + len >= GL_BUF_SIZE) {
459 gl_error("\n*** Error: getline(): input buffer overflow\n");
460 }
461 for (int i = gl_cnt; i >= gl_pos; i--) {
462 gl_buf[i + len] = gl_buf[i];
463 }
464 for (int i = 0; i < len; i++) {
465 gl_buf[gl_pos + i] = gl_killbuf[i];
466 }
467 gl_fixup(gl_prompt, gl_pos, gl_pos + len);
468 }
469 else {
470 if (gl_pos + len >= GL_BUF_SIZE) {
471 gl_error("\n*** Error: getline(): input buffer overflow\n");
472 }
473 if (gl_pos + len > gl_cnt) {
474 gl_buf[gl_pos + len] = '\0';
475 }
476 for (int i = 0; i < len; i++) {
477 gl_buf[gl_pos + i] = gl_killbuf[i];
478 }
479 gl_extent = len;
480 gl_fixup(gl_prompt, gl_pos, gl_pos + len);
481 }
482 }
483 else
484 gl_beep();
485 }
486
487 void gl_transpose()
488 /* switch character under cursor and to left of cursor */
489 {
490 if (gl_pos > 0 && gl_cnt > gl_pos && gl_pos < GL_BUF_SIZE) {
491 int c = gl_buf[gl_pos - 1];
492 gl_buf[gl_pos - 1] = gl_buf[gl_pos];
493 gl_buf[gl_pos] = (char)c;
494 gl_extent = 2;
495 gl_fixup(gl_prompt, gl_pos - 1, gl_pos);
496 }
497 else {
498 gl_beep();
499 }
500 }
501
502 void gl_newline()
503 /*
504 * Cleans up entire line before returning to caller. A \n is appended.
505 * If line longer than screen, we redraw starting at beginning
506 */
507 {
508 int change = gl_cnt;
509 int len = gl_cnt;
510 int loc = gl_width - 5; /* shifts line back to start position */
511
512 if (gl_cnt >= GL_BUF_SIZE - 1) {
513 gl_error("\n*** Error: getline(): input buffer overflow\n");
514 }
515 if (loc > len) {
516 loc = len;
517 }
518 gl_fixup(gl_prompt, change, loc); /* must do this before appending \n */
519 gl_buf[len] = '\n';
520 gl_buf[len + 1] = '\0';
521 gl_putc('\n');
522 }
523
524 void gl_del(int loc, int killsave)
525
526 /*
527 * Delete a character. The loc variable can be:
528 * -1 : delete character to left of cursor
529 * 0 : delete character under cursor
530 */
531 {
532 if ((loc == -1 && gl_pos > 0) || (loc == 0 && gl_pos < gl_cnt)) {
533 int j = 0;
534 for (int i = gl_pos + loc; i < gl_cnt; i++) {
535 if (i < GL_BUF_SIZE - 1) {
536 if ((j == 0) && (killsave != 0)) {
537 gl_killbuf[0] = gl_buf[i];
538 gl_killbuf[1] = '\0';
539 j = 1;
540 }
541 gl_buf[i] = gl_buf[i + 1];
542 }
543 else {
544 gl_error("\n*** Error: getline(): logic error in gl_del().\n");
545 }
546 }
547 gl_fixup(gl_prompt, gl_pos + loc, gl_pos + loc);
548 }
549 else {
550 gl_beep();
551 }
552 }
553
554 void gl_kill(int pos)
555
556 /* delete from pos to the end of line */
557 {
558 if (pos < gl_cnt && pos < GL_BUF_SIZE) {
559 copy_string(gl_killbuf, gl_buf + pos, GL_BUF_SIZE);
560 gl_buf[pos] = '\0';
561 gl_fixup(gl_prompt, pos, pos);
562 }
563 else {
564 gl_beep();
565 }
566 }
567
568 void gl_redraw()
569 /* emit a newline, reset and redraw prompt and current input line */
570 {
571 if (gl_init_done > 0) {
572 gl_putc('\n');
573 gl_fixup(gl_prompt, -2, gl_pos);
574 }
575 }
576
577 void gl_fixup(const char *prompt, int change, int cursor)
578
579 /*
580 * This function is used both for redrawing when input changes or for
581 * moving within the input line. The parameters are:
582 * prompt: compared to last_prompt[] for changes;
583 * change : the index of the start of changes in the input buffer,
584 * with -1 indicating no changes, -2 indicating we're on
585 * a new line, redraw everything.
586 * cursor : the desired location of the cursor after the call.
587 * A value of GL_BUF_SIZE can be used to indicate the cursor should
588 * move just past the end of the input line.
589 */
590 {
591 static int gl_shift; /* index of first on screen character */
592 static int off_right; /* true if more text right of screen */
593 static int off_left; /* true if more text left of screen */
594 static char last_prompt[80] = "";
595 int left = 0, right = -1; /* bounds for redraw */
596 int new_right = -1; /* alternate right bound, using gl_extent */
597
598 if (change == -2) { /* reset */
599 gl_pos = gl_cnt = gl_shift = off_right = off_left = 0;
600 gl_putc('\r');
601 gl_puts(prompt);
602 copy_string(last_prompt, prompt, 80);
603 change = 0;
604 gl_width = gl_termw - strlen(prompt);
605 }
606 else if (strcmp(prompt, last_prompt) != 0) {
607 auto l1 = strlen(last_prompt);
608 auto l2 = strlen(prompt);
609 gl_cnt = gl_cnt + l1 - l2;
610 copy_string(last_prompt, prompt, 80);
611 gl_putc('\r');
612 gl_puts(prompt);
613 gl_pos = gl_shift;
614 gl_width = gl_termw - l2;
615 change = 0;
616 }
617 /* how much to erase at end of line */
618 int pad = (off_right) ? gl_width - 1 : gl_cnt - gl_shift; /* old length */
619 int backup = gl_pos - gl_shift; /* how far to backup before fixing */
620 if (change >= 0) {
621 gl_cnt = strlen(gl_buf);
622 if (change > gl_cnt) {
623 change = gl_cnt;
624 }
625 }
626 if (cursor > gl_cnt) {
627 if (cursor != GL_BUF_SIZE) { /* GL_BUF_SIZE means end of line */
628 if (gl_ellipses_during_completion) {
629 gl_beep();
630 }
631 }
632 cursor = gl_cnt;
633 }
634 if (cursor < 0) {
635 gl_beep();
636 cursor = 0;
637 }
638 int extra = 0; /* adjusts when shift (scroll) happens */
639 if (off_right || (off_left && cursor < gl_shift + gl_width - gl_scroll / 2)) {
640 extra = 2; /* shift the scrolling boundary */
641 }
642 int new_shift = cursor + extra + gl_scroll - gl_width;
643 if (new_shift > 0) {
644 new_shift /= gl_scroll;
645 new_shift *= gl_scroll;
646 }
647 else {
648 new_shift = 0;
649 }
650 if (new_shift != gl_shift) { /* scroll occurs */
651 gl_shift = new_shift;
652 off_left = (gl_shift) ? 1 : 0;
653 off_right = (gl_cnt > gl_shift + gl_width - 1) ? 1 : 0;
654 left = gl_shift;
655 new_right = right = (off_right) ? gl_shift + gl_width - 2 : gl_cnt;
656 }
657 else if (change >= 0) { /* no scroll, but text changed */
658 if (change < gl_shift + off_left) {
659 left = gl_shift;
660 }
661 else {
662 left = change;
663 backup = gl_pos - change;
664 }
665 off_right = (gl_cnt > gl_shift + gl_width - 1) ? 1 : 0;
666 right = (off_right) ? gl_shift + gl_width - 2 : gl_cnt;
667 new_right = (gl_extent && (right > left + gl_extent)) ? left + gl_extent : right;
668 }
669 pad -= (off_right) ? gl_width - 1 : gl_cnt - gl_shift;
670 pad = (pad < 0) ? 0 : pad;
671 if (left <= right) { /* clean up screen */
672 for (int i = 0; i < backup; i++) {
673 gl_putc('\b');
674 }
675 if (left == gl_shift && off_left) {
676 gl_putc('$');
677 left++;
678 }
679 for (int i = left; i < new_right; i++) {
680 gl_putc(gl_buf[i]);
681 }
682 gl_pos = new_right;
683 if (off_right && new_right == right) {
684 gl_putc('$');
685 gl_pos++;
686 }
687 else {
688 for (int i = 0; i < pad; i++) { /* erase remains of prev line */
689 gl_putc(' ');
690 }
691 gl_pos += pad;
692 }
693 }
694 int i = gl_pos - cursor; /* move to final cursor location */
695 if (i > 0) {
696 while (i--) {
697 gl_putc('\b');
698 }
699 }
700 else {
701 if (cursor < GL_BUF_SIZE) {
702 for (int ii = gl_pos; ii < cursor; ii++) {
703 gl_putc(gl_buf[ii]);
704 }
705 }
706 else {
707 gl_error("\n*** Error: getline(): logic error in gl_fixup().\n");
708 }
709 }
710 gl_pos = cursor;
711 }
712
713 /******************* History stuff **************************************/
714
715 #ifndef HIST_SIZE
716 #define HIST_SIZE 100
717 #endif
718
719 int hist_pos = 0, hist_last = 0;
720 std::array<char *, HIST_SIZE> hist_buf;
721 char hist_empty_elem[2] = "";
722
723 void hist_init()
724 {
725 hist_buf[0] = hist_empty_elem;
726 for (int i = 1; i < HIST_SIZE; i++) {
727 hist_buf[i] = nullptr;
728 }
729 }
730 } // namespace
731
732 namespace SEAMS {
733 void gl_histadd(const char *buf)
734 {
735 static char *prev = nullptr;
736
737 /* in case we call gl_histadd() before we call getline() */
738 if (gl_init_done < 0) { /* -1 only on startup */
739 hist_init();
740 gl_init_done = 0;
741 }
742 const char *p = buf;
743 while (*p == ' ' || *p == '\t' || *p == '\n') {
744 p++;
745 }
746 if (*p) {
747 auto len = strlen(buf);
748 if (strchr(p, '\n')) { /* previously line already has NL stripped */
749 len--;
750 }
751 if ((prev == nullptr) || (strlen(prev) != len) || strncmp(prev, buf, len) != 0) {
752 hist_buf[hist_last] = hist_save(buf);
753 prev = hist_buf[hist_last];
754 hist_last = (hist_last + 1) % HIST_SIZE;
755 if (hist_buf[hist_last] && *hist_buf[hist_last]) {
756 free(hist_buf[hist_last]);
757 }
758 hist_buf[hist_last] = hist_empty_elem;
759 }
760 }
761 hist_pos = hist_last;
762 }
763 } // namespace SEAMS
764 namespace {
765 char *hist_prev()
766 /* loads previous hist entry into input buffer, sticks on first */
767 {
768 char *p = nullptr;
769 int next = (hist_pos - 1 + HIST_SIZE) % HIST_SIZE;
770
771 if (hist_buf[hist_pos] != nullptr && next != hist_last) {
772 hist_pos = next;
773 p = hist_buf[hist_pos];
774 }
775 if (p == nullptr) {
776 p = hist_empty_elem;
777 gl_beep();
778 }
779 return p;
780 }
781
782 char *hist_next()
783 /* loads next hist entry into input buffer, clears on last */
784 {
785 char *p = nullptr;
786
787 if (hist_pos != hist_last) {
788 hist_pos = (hist_pos + 1) % HIST_SIZE;
789 p = hist_buf[hist_pos];
790 }
791 if (p == nullptr) {
792 p = hist_empty_elem;
793 gl_beep();
794 }
795 return p;
796 }
797
798 char *hist_save(const char *p)
799
800 /* makes a copy of the string */
801 {
802 char *s = nullptr;
803 auto len = strlen(p);
804 const char *nl = strpbrk(p, "\n\r");
805
806 if (nl) {
807 if ((s = (char *)malloc(len)) != nullptr) {
808 copy_string(s, p, len);
809 s[len - 1] = '\0';
810 }
811 }
812 else {
813 if ((s = (char *)malloc(len + 1)) != nullptr) {
814 copy_string(s, p, len + 1);
815 }
816 }
817 if (s == nullptr) {
818 gl_error("\n*** Error: hist_save() failed on malloc\n");
819 }
820 return s;
821 }
822
823 /******************* Search stuff **************************************/
824
825 char search_prompt[101]; /* prompt includes search string */
826 char search_string[100];
827 int search_pos = 0; /* current location in search_string */
828 int search_forw_flg = 0; /* search direction flag */
829 int search_last = 0; /* last match found */
830
831 void search_update(int c)
832 {
833 if (c == 0) {
834 search_pos = 0;
835 search_string[0] = '\0';
836
837 search_prompt[0] = '?';
838 search_prompt[1] = ' ';
839 search_prompt[2] = '\0';
840 }
841 else if (c > 0) {
842 search_string[search_pos] = (char)c;
843 search_string[search_pos + 1] = (char)0;
844
845 search_prompt[search_pos] = (char)c;
846 search_prompt[search_pos + 1] = (char)'?';
847 search_prompt[search_pos + 2] = (char)' ';
848 search_prompt[search_pos + 3] = (char)0;
849 search_pos++;
850 }
851 else {
852 if (search_pos > 0) {
853 search_pos--;
854 search_string[search_pos] = (char)0;
855
856 search_prompt[search_pos] = (char)'?';
857 search_prompt[search_pos + 1] = (char)' ';
858 search_prompt[search_pos + 2] = (char)0;
859 }
860 else {
861 gl_beep();
862 hist_pos = hist_last;
863 }
864 }
865 }
866
867 void search_addchar(int c)
868 {
869 search_update(c);
870 if (c < 0) {
871 if (search_pos > 0) {
872 hist_pos = search_last;
873 }
874 else {
875 gl_buf[0] = '\0';
876 hist_pos = hist_last;
877 }
878 copy_string(gl_buf, hist_buf[hist_pos], GL_BUF_SIZE);
879 }
880
881 char *loc = nullptr;
882 if ((loc = strstr(gl_buf, search_string)) != nullptr) {
883 gl_fixup(search_prompt, 0, loc - gl_buf);
884 }
885 else if (search_pos > 0) {
886 if (search_forw_flg) {
887 search_forw(0);
888 }
889 else {
890 search_back(0);
891 }
892 }
893 else {
894 gl_fixup(search_prompt, 0, 0);
895 }
896 }
897
898 void search_term()
899 {
900 gl_search_mode = 0;
901 if (gl_buf[0] == 0) { /* not found, reset hist list */
902 hist_pos = hist_last;
903 }
904 gl_fixup(gl_prompt, 0, gl_pos);
905 }
906
907 void search_back(int new_search)
908 {
909 search_forw_flg = 0;
910 if (gl_search_mode == 0) {
911 search_last = hist_pos = hist_last;
912 search_update(0);
913 gl_search_mode = 1;
914 gl_buf[0] = '\0';
915 gl_fixup(search_prompt, 0, 0);
916 }
917 else if (search_pos > 0) {
918 bool found = false;
919 while (!found) {
920 char *loc;
921 char *p = hist_prev();
922 if (*p == 0) { /* not found, done looking */
923 gl_buf[0] = '\0';
924 gl_fixup(search_prompt, 0, 0);
925 found = true;
926 }
927 else if ((loc = strstr(p, search_string)) != nullptr) {
928 copy_string(gl_buf, p, GL_BUF_SIZE);
929 gl_fixup(search_prompt, 0, loc - p);
930 if (new_search)
931 search_last = hist_pos;
932 found = true;
933 }
934 }
935 }
936 else {
937 gl_beep();
938 }
939 }
940
941 void search_forw(int new_search)
942 {
943 char *loc;
944
945 search_forw_flg = 1;
946 if (gl_search_mode == 0) {
947 search_last = hist_pos = hist_last;
948 search_update(0);
949 gl_search_mode = 1;
950 gl_buf[0] = '\0';
951 gl_fixup(search_prompt, 0, 0);
952 }
953 else if (search_pos > 0) {
954 bool found = false;
955 while (!found) {
956 char *p = hist_next();
957 if (*p == 0) { /* not found, done looking */
958 gl_buf[0] = '\0';
959 gl_fixup(search_prompt, 0, 0);
960 found = true;
961 }
962 else if ((loc = strstr(p, search_string)) != nullptr) {
963 copy_string(gl_buf, p, GL_BUF_SIZE);
964 gl_fixup(search_prompt, 0, loc - p);
965 if (new_search)
966 search_last = hist_pos;
967 found = true;
968 }
969 }
970 }
971 else {
972 gl_beep();
973 }
974 }
975
976 void gl_beep()
977 {
978 #ifdef __windows__
979 MessageBeep(MB_OK);
980 #else
981 gl_putc('\007');
982 #endif
983 } /* gl_beep */
984 } // namespace