1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| static int cread_line(const char *const prompt, char *buf, unsigned int *len) { unsigned long num = 0; unsigned long eol_num = 0; unsigned long wlen; unsigned char ichar; int insert = 1; int esc_len = 0; char esc_save[8]; int init_len = (int)strlen(buf); int cur_num = 0;
if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
while (1) { ichar = getcmd_getch();
if ((ichar == '\n') || (ichar == '\r')) { getcmd_putch('\n'); break; }
/* * handle Windows arrow key, etc. * note: arrow keys have two char, the first is 0xe0 */ if (ichar == 0xe0) { ichar = getcmd_getch(); switch(ichar) { case 'H': /* up arrow */ ichar = CTL_CH('p'); break; case 'P': /* down arrow */ ichar = CTL_CH('n'); break; case 'K': /* left arrow */ ichar = CTL_CH('b'); break; case 'M': /* right arrow */ ichar = CTL_CH('f'); break; case 'G': /* Home */ ichar = CTL_CH('a'); break; case 'O': /* End */ ichar = CTL_CH('e'); break; case 'S': /* Delete */ ichar = CTL_CH('d'); break; default: break; } }
/* * handle standard linux xterm esc sequences for arrow key, etc. */ if (esc_len != 0) { if (esc_len == 1) { if (ichar == '[') { esc_save[esc_len] = ichar; esc_len = 2; } else { cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; } continue; }
switch (ichar) { case 'D': /* <- key */ ichar = CTL_CH('b'); esc_len = 0; break; case 'C': /* -> key */ ichar = CTL_CH('f'); esc_len = 0; break; /* pass off to ^F handler */ case 'H': /* Home key */ ichar = CTL_CH('a'); esc_len = 0; break; /* pass off to ^A handler */ case 'A': /* up arrow */ ichar = CTL_CH('p'); esc_len = 0; break; /* pass off to ^P handler */ case 'B': /* down arrow */ ichar = CTL_CH('n'); esc_len = 0; break; /* pass off to ^N handler */ default: esc_save[esc_len++] = ichar; cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; continue; } } /* End of linux arrow key */
switch (ichar) { /* linux esc sequences */ case 0x1b: if (esc_len == 0) { esc_save[esc_len] = ichar; esc_len = 1; } else { getcmd_puts("impossible condition #876\n"); esc_len = 0; } break;
case CTL_CH('a'): BEGINNING_OF_LINE(); break; case CTL_CH('c'): /* ^C - break */ *buf = '\0'; /* discard input */ return (-1); case CTL_CH('f'): if (num < eol_num) { getcmd_putch(buf[num]); num++; } break; case CTL_CH('b'): if (num) { getcmd_putch(CTL_BACKSPACE); num--; } break; case CTL_CH('d'): if (num < eol_num) { wlen = eol_num - num - 1; if (wlen) { memmove(&buf[num], &buf[num+1], wlen); putnstr(buf + num, wlen); }
getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('k'): ERASE_TO_EOL(); break; case CTL_CH('e'): REFRESH_TO_EOL(); break; case CTL_CH('o'): insert = !insert; break; case CTL_CH('x'): case CTL_CH('u'): BEGINNING_OF_LINE(); ERASE_TO_EOL(); break; case DEL: case DEL7: case BACKSPACE: if (num) { wlen = eol_num - num; num--; memmove(&buf[num], &buf[num+1], wlen); getcmd_putch(CTL_BACKSPACE); putnstr(buf + num, wlen); getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('p'): case CTL_CH('n'): { char * hline;
esc_len = 0;
if (ichar == CTL_CH('p')) hline = hist_prev(); else hline = hist_next();
if (!hline) { getcmd_cbeep(); continue; }
/* nuke the current line */ /* first, go home */ BEGINNING_OF_LINE();
/* erase to end of line */ ERASE_TO_EOL();
/* copy new line into place and display */ strcpy(buf, hline); eol_num = (unsigned long)strlen(buf); REFRESH_TO_EOL(); continue; }
default: cread_add_char(ichar, insert, &num, &eol_num, buf, *len); break; } }
*len = eol_num; buf[eol_num] = '\0'; /* lose the newline */
if (buf[0] && buf[0] != CREAD_HIST_CHAR) cread_add_to_hist(buf); hist_cur = hist_add_idx;
return 0; }
|