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