Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_Getline.C

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