/* (not including trailing NULL) */ int text_gets(TW *tw, char *line, int len) { LPSTR dest = line; LPSTR limit = dest + len; /* don't leave room for '\0' */ int ch; do { if (dest >= limit) break; ch = text_getch(tw); switch(ch) { case 26: /* ^Z == EOF */ return 0; case '\b': /* ^H */ case 0x7f: /* DEL */ if (dest > line) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); #ifndef WINDOWS_NO_UNICODE while ((dest > line) && ((dest[-1] & 0xC0) == 0x80)) { /* It's a UTF-8 continuation char. */ --(dest); } if (dest == line) break; #endif --dest; } break; case 21: /* ^U */ while (dest > line) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); --dest; } break; default: *dest++ = ch; text_putch(tw, ch); break; } } while (ch != '\n'); *dest = '\0'; return (dest-line); }
/* (not including trailing NULL) */ int text_gets(TW *tw, char *line, int len) { LPSTR dest = line; LPSTR limit = dest + len; /* don't leave room for '\0' */ int ch; do { if (dest >= limit) break; ch = text_getch(tw); switch(ch) { case 26: /* ^Z == EOF */ return 0; case '\b': /* ^H */ case 0x7f: /* DEL */ if (dest > line) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); --dest; } break; case 21: /* ^U */ while (dest > line) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); --dest; } break; default: *dest++ = ch; text_putch(tw, ch); break; } } while (ch != '\n'); *dest = '\0'; return (dest-line); }
/* Read line from keyboard using buffered input * Return at most 'len' characters * Does NOT add null terminating character * This is NOT the same as fgets() * Do not mix this with calls to text_getch() */ int text_read_line(TW *tw, char *line, int len) { int ch; if (tw->line_eof) return 0; while (!tw->line_complete) { /* we have not yet collected a full line */ ch = text_getch(tw); switch(ch) { case EOF: case 26: /* ^Z == EOF */ tw->line_eof = TRUE; tw->line_complete = TRUE; break; case '\b': /* ^H */ case 0x7f: /* DEL */ if (tw->line_end) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); #ifndef WINDOWS_NO_UNICODE while ((tw->line_end) && ((tw->line_buf[tw->line_end-1] & 0xC0) == 0x80)) { /* It's a UTF-8 continuation char. */ --(tw->line_end); } if (tw->line_end == 0) break; #endif --(tw->line_end); } break; case 21: /* ^U */ while (tw->line_end) { text_putch(tw, '\b'); text_putch(tw, ' '); text_putch(tw, '\b'); --(tw->line_end); } break; case '\r': case '\n': tw->line_complete = TRUE; /* fall through */ default: tw->line_buf[tw->line_end++] = ch; text_putch(tw, ch); break; } if (tw->line_end >= sizeof(tw->line_buf)) tw->line_complete = TRUE; } if (tw->quitnow) return -1; if (tw->line_complete) { /* We either filled the buffer or got CR, LF or EOF */ int count = min(len, tw->line_end - tw->line_start); memcpy(line, tw->line_buf + tw->line_start, count); tw->line_start += count; if (tw->line_start == tw->line_end) { tw->line_start = tw->line_end = 0; tw->line_complete = FALSE; } return count; } return 0; }