static void search_forw (int new_search) { int found = 0; char *p, *loc; search_forw_flg = 1; if (gl_search_mode == 0) { search_last = hist_pos = hist_last; search_update(0); gl_search_mode = 1; gl_buf[0] = 0; gl_fixup(search_prompt, 0, 0); } else if (search_pos > 0) { while (!found) { p = hist_next(); if (*p == 0) { /* not found, done looking */ gl_buf[0] = 0; gl_fixup(search_prompt, 0, 0); found = 1; } else if ((loc = strstr(p, search_string)) != 0) { strcpy(gl_buf, p); gl_fixup(search_prompt, 0, (int)(loc - p)); if (new_search) search_last = hist_pos; found = 1; } } } else { gl_putc('\007'); } }
static void gl_yank (void) /* adds the kill buffer to the input buffer at current location */ { int i, len; len = strlen(gl_killbuf); if (len > 0) { if (gl_overwrite == 0) { if (gl_cnt + len >= BUF_SIZE - 1) gl_error("\n*** Error: getline(): input buffer overflow\n"); for (i=gl_cnt; i >= gl_pos; i--) gl_buf[i+len] = gl_buf[i]; for (i=0; i < len; i++) gl_buf[gl_pos+i] = gl_killbuf[i]; gl_fixup(gl_prompt, gl_pos, gl_pos+len); } else { if (gl_pos + len > gl_cnt) { if (gl_pos + len >= BUF_SIZE - 1) gl_error("\n*** Error: getline(): input buffer overflow\n"); gl_buf[gl_pos + len] = 0; } for (i=0; i < len; i++) gl_buf[gl_pos+i] = gl_killbuf[i]; gl_extent = len; gl_fixup(gl_prompt, gl_pos, gl_pos+len); } } else gl_putc('\007'); }
static void search_update (int c) { if (c == 0) { search_pos = 0; search_string[0] = 0; search_prompt[0] = '?'; search_prompt[1] = ' '; search_prompt[2] = 0; } else if (c > 0) { search_string[search_pos] = c; search_string[search_pos+1] = 0; search_prompt[search_pos] = c; search_prompt[search_pos+1] = '?'; search_prompt[search_pos+2] = ' '; search_prompt[search_pos+3] = 0; search_pos++; } else { if (search_pos > 0) { search_pos--; search_string[search_pos] = 0; search_prompt[search_pos] = '?'; search_prompt[search_pos+1] = ' '; search_prompt[search_pos+2] = 0; } else { gl_putc('\007'); hist_pos = hist_last; } } }
static void gl_addchar(int c) /* adds the character c to the input buffer at current location */ { int i; if (gl_cnt >= BUF_SIZE - 2) { gl_putc('\a'); return; } if(mbcslocale) { mbstate_t mb_st; wchar_t wc; char s[9]; /* only 3 needed */ int res; int clen ; s[0] = c; clen = 1; res = 0; /* This is a DBCS locale, so input is 1 or 2 bytes. This loop should not be necessary. */ if((unsigned int) c >= (unsigned int) 0x80) { while(clen <= MB_CUR_MAX) { mbs_init(&mb_st); res = mbrtowc(&wc, s, clen, &mb_st); if(res >= 0) break; if(res == -1) gl_error("invalid multibyte character in mbcs_get_next"); /* so res == -2 */ c = gl_getc(); if(c == EOF) gl_error("EOF whilst reading MBCS char"); s[clen++] = c; } /* we've tried enough, so must be complete or invalid by now */ } if( res >= 0 ) { if (!(gl_overwrite == 0 || gl_pos == gl_cnt)) gl_del(0); for (i = gl_cnt; i >= gl_pos; i--) gl_buf[i+clen] = gl_buf[i]; for (i = 0; i < clen; i++) gl_buf[gl_pos + i] = s[i]; gl_fixup(gl_prompt, gl_pos, gl_pos+clen); } } else if (gl_overwrite == 0 || gl_pos == gl_cnt) { for (i = gl_cnt; i >= gl_pos; i--) gl_buf[i+1] = gl_buf[i]; gl_buf[gl_pos] = (char) c; gl_fixup(gl_prompt, gl_pos, gl_pos+1); } else { gl_buf[gl_pos] = (char) c; gl_extent = 1; gl_fixup(gl_prompt, gl_pos, gl_pos+1); } }
static void gl_redraw (void) /* emit a newline, reset and redraw prompt and current input line */ { if (gl_init_done > 0) { gl_putc('\n'); gl_fixup(gl_prompt, -2, gl_pos); } }
static void gl_kill (int pos) /* delete from pos to the end of line */ { if (pos < gl_cnt) { strcpy(gl_killbuf, gl_buf + pos); gl_buf[pos] = '\0'; gl_fixup(gl_prompt, pos, pos); } else gl_putc('\007'); }
static void gl_transpose (void) /* switch character under cursor and to left of cursor */ { int c; if (gl_pos > 0 && gl_cnt > gl_pos) { c = gl_buf[gl_pos-1]; gl_buf[gl_pos-1] = gl_buf[gl_pos]; gl_buf[gl_pos] = c; gl_extent = 2; gl_fixup(gl_prompt, gl_pos-1, gl_pos); } else gl_putc('\007'); }
static char * hist_next (void) /* loads next hist entry into input buffer, clears on last */ { char *p = 0; if (hist_pos != hist_last) { hist_pos = (hist_pos+1) % HIST_SIZE; p = hist_buf[hist_pos]; } if (p == 0) { p = ""; gl_putc('\007'); } return p; }
static char * hist_prev (void) /* loads previous hist entry into input buffer, sticks on first */ { char *p = 0; int next = (hist_pos - 1 + HIST_SIZE) % HIST_SIZE; if (hist_buf[hist_pos] != 0 && next != hist_last) { hist_pos = next; p = hist_buf[hist_pos]; } if (p == 0) { p = ""; gl_putc('\007'); } return p; }
static void gl_del (int loc) /* * Delete a character. The loc variable can be: * -1 : delete character to left of cursor * 0 : delete character under cursor */ { int i; if ((loc == -1 && gl_pos > 0) || (loc == 0 && gl_pos < gl_cnt)) { for (i=gl_pos+loc; i < gl_cnt; i++) gl_buf[i] = gl_buf[i+1]; gl_fixup(gl_prompt, gl_pos+loc, gl_pos+loc); } else gl_putc('\007'); }
static void gl_newline (void) /* * Cleans up entire line before returning to caller. A \n is appended. * If line longer than screen, we redraw starting at beginning */ { int change = gl_cnt; int len = gl_cnt; int loc = gl_width - 5; /* shifts line back to start position */ if (gl_cnt >= BUF_SIZE - 1) gl_error("\n*** Error: getline(): input buffer overflow\n"); if (gl_out_hook) { change = gl_out_hook(gl_buf); len = strlen(gl_buf); } if (loc > len) loc = len; gl_fixup(gl_prompt, change, loc); /* must do this before appending \n */ gl_buf[len] = '\n'; gl_buf[len+1] = '\0'; gl_putc('\n'); }
static void gl_fixup (char *prompt, int change, int cursor) /* * This function is used both for redrawing when input changes or for * moving within the input line. The parameters are: * prompt: compared to last_prompt[] for changes; * change : the index of the start of changes in the input buffer, * with -1 indicating no changes, -2 indicating we're on * a new line, redraw everything. * cursor : the desired location of the cursor after the call. * A value of BUF_SIZE can be used to indicate the cursor should * move just past the end of the input line. */ { static int gl_shift; /* index of first on screen character */ static int off_right; /* true if more text right of screen */ static int off_left; /* true if more text left of screen */ static char last_prompt[80] = ""; int left = 0, right = -1; /* bounds for redraw */ int pad; /* how much to erase at end of line */ int backup; /* how far to backup before fixing */ int new_shift; /* value of shift based on cursor */ int extra; /* adjusts when shift (scroll) happens */ int i; int new_right = -1; /* alternate right bound, using gl_extent */ int l1, l2; if (change == -2) { /* reset */ gl_pos = gl_cnt = gl_shift = off_right = off_left = 0; gl_putc('\r'); gl_puts(prompt); strcpy(last_prompt, prompt); change = 0; gl_width = gl_termw - gl_strlen(prompt); } else if (strcmp(prompt, last_prompt) != 0) { l1 = gl_strlen(last_prompt); l2 = gl_strlen(prompt); gl_cnt = gl_cnt + l1 - l2; strcpy(last_prompt, prompt); gl_putc('\r'); gl_puts(prompt); gl_pos = gl_shift; gl_width = gl_termw - l2; change = 0; } pad = (off_right)? gl_width - 1 : gl_cnt - gl_shift; /* old length */ backup = gl_pos - gl_shift; if (change >= 0) { gl_cnt = strlen(gl_buf); if (change > gl_cnt) change = gl_cnt; } if (cursor > gl_cnt) { if (cursor != BUF_SIZE) /* BUF_SIZE means end of line */ gl_putc('\007'); cursor = gl_cnt; } if (cursor < 0) { gl_putc('\007'); cursor = 0; } if (off_right || (off_left && cursor < gl_shift + gl_width - gl_scroll / 2)) extra = 2; /* shift the scrolling boundary */ else extra = 0; new_shift = cursor + extra + gl_scroll - gl_width; if (new_shift > 0) { new_shift /= gl_scroll; new_shift *= gl_scroll; } else new_shift = 0; if (new_shift != gl_shift) { /* scroll occurs */ gl_shift = new_shift; off_left = (gl_shift)? 1 : 0; off_right = (gl_cnt > gl_shift + gl_width - 1)? 1 : 0; left = gl_shift; new_right = right = (off_right)? gl_shift + gl_width - 2 : gl_cnt; } else if (change >= 0) { /* no scroll, but text changed */ if (change < gl_shift + off_left) { left = gl_shift; } else { left = change; backup = gl_pos - change; } off_right = (gl_cnt > gl_shift + gl_width - 1)? 1 : 0; right = (off_right)? gl_shift + gl_width - 2 : gl_cnt; new_right = (gl_extent && (right > left + gl_extent))? left + gl_extent : right; } pad -= (off_right)? gl_width - 1 : gl_cnt - gl_shift; pad = (pad < 0)? 0 : pad; if (left <= right) { /* clean up screen */ for (i=0; i < backup; i++) gl_putc('\b'); if (left == gl_shift && off_left) { gl_putc('$'); left++; } for (i=left; i < new_right; i++) gl_putc(gl_buf[i]); gl_pos = new_right; if (off_right && new_right == right) { gl_putc('$'); gl_pos++; } else { for (i=0; i < pad; i++) /* erase remains of prev line */ gl_putc(' '); gl_pos += pad; } } i = gl_pos - cursor; /* move to final cursor location */ if (i > 0) { while (i--) gl_putc('\b'); } else { for (i=gl_pos; i < cursor; i++) gl_putc(gl_buf[i]); } gl_pos = cursor; }
char *getline_int(char *prompt) { int c, loc, tmp; int sig; gl_init(); gl_prompt = (prompt)? prompt : ""; gl_buf[0] = 0; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, -2, BUF_SIZE); while ((c = gl_getc()) >= 0) { gl_extent = 0; /* reset to full extent */ if (isprint(c)) { if (gl_search_mode) search_addchar(c); else gl_addchar(c); } else { if (gl_search_mode) { if (c == '\033' || c == '\016' || c == '\020') { search_term(); c = 0; /* ignore the character */ } else if (c == '\010' || c == '\177') { search_addchar(-1); /* unwind search string */ c = 0; } else if (c != '\022' && c != '\023') { search_term(); /* terminate and handle char */ } } switch (c) { case '\n': case '\r': /* newline */ gl_newline(); gl_cleanup(); return gl_buf; /*NOTREACHED*/ break; case '\001': gl_fixup(gl_prompt, -1, 0); /* ^A */ break; case '\002': gl_fixup(gl_prompt, -1, gl_pos-1); /* ^B */ break; case '\004': /* ^D */ if (gl_cnt == 0) { gl_buf[0] = 0; gl_cleanup(); gl_putc('\n'); return gl_buf; } else { gl_del(0); } break; case '\005': gl_fixup(gl_prompt, -1, gl_cnt); /* ^E */ break; case '\006': gl_fixup(gl_prompt, -1, gl_pos+1); /* ^F */ break; case '\010': case '\177': gl_del(-1); /* ^H and DEL */ break; case '\t': /* TAB */ if (gl_tab_hook) { tmp = gl_pos; loc = gl_tab_hook(gl_buf, gl_strlen(gl_prompt), &tmp); if (loc >= 0 || tmp != gl_pos) gl_fixup(gl_prompt, loc, tmp); } break; case '\013': gl_kill(gl_pos); /* ^K */ break; case '\014': gl_redraw(); /* ^L */ break; case '\016': /* ^N */ strcpy(gl_buf, hist_next()); if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case '\017': gl_overwrite = !gl_overwrite; /* ^O */ break; case '\020': /* ^P */ strcpy(gl_buf, hist_prev()); if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case '\022': search_back(1); /* ^R */ break; case '\023': search_forw(1); /* ^S */ break; case '\024': gl_transpose(); /* ^T */ break; case '\025': gl_kill(0); /* ^U */ break; case '\031': gl_yank(); /* ^Y */ break; case '\033': /* ansi arrow keys */ c = gl_getc(); if (c == '[') { switch(c = gl_getc()) { case 'A': /* up */ strcpy(gl_buf, hist_prev()); if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case 'B': /* down */ strcpy(gl_buf, hist_next()); if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case 'C': gl_fixup(gl_prompt, -1, gl_pos+1); /* right */ break; case 'D': gl_fixup(gl_prompt, -1, gl_pos-1); /* left */ break; default: gl_putc('\007'); /* who knows */ break; } } else if (c == 'f' || c == 'F') { gl_word(1); } else if (c == 'b' || c == 'B') { gl_word(-1); } else gl_putc('\007'); break; default: /* check for a terminal signal */ if (c > 0) { /* ignore 0 (reset above) */ sig = 0; #ifdef SIGINT if (c == gl_intrc) sig = SIGINT; #endif #ifdef SIGQUIT if (c == gl_quitc) sig = SIGQUIT; #endif #ifdef SIGTSTP if (c == gl_suspc || c == gl_dsuspc) sig = SIGTSTP; #endif if (sig != 0) { gl_cleanup(); kill(0, sig); gl_init(); gl_redraw(); c = 0; } } if (c > 0) gl_putc('\007'); break; } } } gl_cleanup(); gl_buf[0] = 0; return gl_buf; }
int getline(const char *prompt, char *buf, int buflen) { int c, loc, tmp; int mb_len; mbstate_t mb_st; int i; wchar_t wc; BUF_SIZE = buflen; gl_buf = buf; gl_buf[0] = '\0'; if (setjmp(gl_jmp)) { gl_newline(); gl_cleanup(); return 0; } gl_init(); gl_pos = 0; gl_prompt = (prompt)? prompt : ""; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, -2, BUF_SIZE); while ((c = gl_getc()) >= 0) { gl_extent = 0; /* reset to full extent */ if (!iscntrl(c)) { if (gl_search_mode) search_addchar(c); else gl_addchar(c); } else { if (gl_search_mode) { if (c == '\033' || c == '\016' || c == '\020') { search_term(); c = 0; /* ignore the character */ } else if (c == '\010' || c == '\177') { search_addchar(-1); /* unwind search string */ c = 0; } else if (c != '\022' && c != '\023') { search_term(); /* terminate and handle char */ } } switch (c) { case '\n': case '\r': /* newline */ gl_newline(); gl_cleanup(); return 0; /*NOTREACHED*/ break; case '\001': gl_fixup(gl_prompt, -1, 0); /* ^A */ break; case '\002': /* ^B */ if(mbcslocale) { mb_len = 0; mbs_init(&mb_st); for(i = 0; i < gl_pos ;) { mbrtowc(&wc, gl_buf+i, MB_CUR_MAX, &mb_st); mb_len = Ri18n_wcwidth(wc); i += (wc==0) ? 0 : mb_len; } gl_fixup(gl_prompt, -1, gl_pos - mb_len); } else gl_fixup(gl_prompt, -1, gl_pos-1); break; case '\003': /* ^C */ gl_fixup(gl_prompt, -1, gl_cnt); gl_puts("^C\n"); gl_kill(0); gl_fixup(gl_prompt, -2, BUF_SIZE); break; case '\004': /* ^D */ if (gl_cnt == 0) { gl_buf[0] = 0; gl_cleanup(); gl_putc('\n'); return 0; } else { gl_del(0); } break; case '\005': gl_fixup(gl_prompt, -1, gl_cnt); /* ^E */ break; case '\006': /* ^F */ if(mbcslocale) { if(gl_pos >= gl_cnt) break; mb_len = 0; mbs_init(&mb_st); for(i = 0; i<= gl_pos ;){ mbrtowc(&wc, gl_buf+i, MB_CUR_MAX, &mb_st); mb_len = Ri18n_wcwidth(wc); i += (wc==0) ? 0 : mb_len; } gl_fixup(gl_prompt, -1, gl_pos + mb_len); } else gl_fixup(gl_prompt, -1, gl_pos+1); break; case '\010': case '\177': gl_del(-1); /* ^H and DEL */ break; case '\t': /* TAB */ if (gl_tab_hook) { tmp = gl_pos; loc = gl_tab_hook(gl_buf, gl_strlen(gl_prompt), &tmp); if (loc != -1 || tmp != gl_pos) gl_fixup(gl_prompt, loc, tmp); } break; case '\013': gl_kill(gl_pos); /* ^K */ break; case '\014': gl_redraw(); /* ^L */ break; case '\016': /* ^N */ strncpy(gl_buf, gl_hist_next(), BUF_SIZE-2); gl_buf[BUF_SIZE-2] = '\0'; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case '\017': gl_overwrite = !gl_overwrite; /* ^O */ break; case '\020': /* ^P */ strncpy(gl_buf, gl_hist_prev(),BUF_SIZE-2); gl_buf[BUF_SIZE-2] = '\0'; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case '\022': search_back(1); /* ^R */ break; case '\023': search_forw(1); /* ^S */ break; case '\024': gl_transpose(); /* ^T */ break; case '\025': gl_kill(0); /* ^U */ break; case '\027': gl_killword(-1); /* ^W */ break; case '\031': gl_yank(); /* ^Y */ break; case '\032': /* ^Z */ gl_newline(); gl_cleanup(); return 1; /*NOTREACHED*/ break; case '\033': /* ansi arrow keys */ c = gl_getc(); if (c == '[') { switch(c = gl_getc()) { case 'A': /* up */ strncpy(gl_buf, gl_hist_prev(), BUF_SIZE-2); gl_buf[BUF_SIZE-2] = '\0'; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case 'B': /* down */ strncpy(gl_buf, gl_hist_next(), BUF_SIZE-2); gl_buf[BUF_SIZE-2] = '\0'; if (gl_in_hook) gl_in_hook(gl_buf); gl_fixup(gl_prompt, 0, BUF_SIZE); break; case 'C': /* right */ if(mbcslocale) { mb_len = 0; mbs_init(&mb_st); for(i = 0; i <= gl_pos ;) { mbrtowc(&wc, gl_buf+i, MB_CUR_MAX, &mb_st); mb_len = Ri18n_wcwidth(wc); i += (wc==0) ? 0 : mb_len; } gl_fixup(gl_prompt, -1, gl_pos + mb_len); } else gl_fixup(gl_prompt, -1, gl_pos+1); break; case 'D': /* left */ if(mbcslocale) { mb_len = 0; mbs_init(&mb_st); for(i = 0; i <= gl_pos ;) { mbrtowc(&wc, gl_buf+i, MB_CUR_MAX, &mb_st); mb_len = Ri18n_wcwidth(wc); i += (wc==0) ? 0 :mb_len; } gl_fixup(gl_prompt, -1, gl_pos - mb_len); } else gl_fixup(gl_prompt, -1, gl_pos-1); break; default: gl_putc('\007'); /* who knows */ break; } } else if (c == 'f' || c == 'F') { gl_word(1); } else if (c == 'b' || c == 'B') { gl_word(-1); } else gl_putc('\007'); break; default: /* check for a terminal signal */ if (c > 0) gl_putc('\007'); break; } } } gl_newline(); gl_cleanup(); return 0; }