Anonymous avatar Anonymous committed e51a440

revert to last clean before utf8 attempts, that's in a branch now

Comments (0)

Files changed (1)

 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <locale.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
-#include <wchar.h>
 
 #include <sys/ioctl.h>
 #include <sys/select.h>
 #define cursor_fwd(n) printf("\033[%dC", MAX(1, (n)))
 #define cursor_org()  printf("\033[1;1H")
 
-//char  buf[4096], usrbuf[4096];
-char  buf[4096];
-char usrbuf[4096];
+char  buf[4096], usrbuf[4096];
 char  scrl_prompt_left[]  = "<";
 char  scrl_prompt_right[] = ">";
 char  nrml_prompt_default[] = ">";
 char  word_separators[] = " \t~!@#$%^&*-+=\\|/?.,><";
 
 int start = 0, cur = 0, len = 0;
-int wcur = 0, wlen = 0;
 int mfd;
 pid_t child;
 
 	int cols, left_prompt_len, right_prompt_len = 0;
 	struct winsize ws;
 
-	/*
 	if (ioctl(STDOUT, TIOCGWINSZ, &ws) < 0)
 		err(1, "failed ioctl");
 
 	left_prompt_len = strlen(nrml_prompt);
 	cols -= left_prompt_len;
 
-	start = start_pos(start, wcur, wlen, cols);
+	start = start_pos(start, cur, len, cols);
 
-	if (start > 0 || wlen > cols) {
+	if (start > 0 || len > cols) {
 		left_prompt_len = strlen(scrl_prompt_left);
 		my_write(STDOUT, scrl_prompt_left, left_prompt_len);
 		cols = ws.ws_col - 1 - left_prompt_len;
-		start = start_pos(start, wcur, wlen, cols);
+		start = start_pos(start, cur, len, cols);
 	} else {
 		my_write(STDOUT, nrml_prompt, left_prompt_len);
 	}
 
-	if (start + cols < wlen) {
+	if (start + cols < len) {
 		right_prompt_len = strlen(scrl_prompt_right);
 		cols -= right_prompt_len;
-		start = start_pos(start, wcur, wlen, cols);
+		start = start_pos(start, cur, len, cols);
 	}
 
-	//my_write(STDOUT, usrbuf + start, MIN(cols, len));
-	my_write(STDOUT, usrbuf + start, len);
+	my_write(STDOUT, usrbuf + start, MIN(cols, len));
 
 	if (start + cols < len)
 		my_write(STDOUT, scrl_prompt_right, right_prompt_len);
 
-		*/
-
-	erase_usr();
-	left_prompt_len = strlen(nrml_prompt);
-	my_write(STDOUT, nrml_prompt, left_prompt_len);
-	my_write(STDOUT, usrbuf, len);
 	cursor_beg();
-	cursor_fwd(left_prompt_len + wcur - start);
+	cursor_fwd(left_prompt_len + cur - start);
 	fflush(stdout);
 }
 
 
 void run()
 {
-	static int escape = 0, wstart = 0, in_mb = 0;
-	int rdlen, i, mask;
+	static int escape = 0;
+	int rdlen, i;
 	char c;
-	wchar_t wc;
 	fd_set rd;
 
 	for (;;) {
 					case '[' :
 						continue;
 					case '3' : // Delete
-						escape++;
-						if (cur == len) break;
-						for (i = cur + 1; i < len && (usrbuf[i] & 0xc0) == 0x80; i++)
-							;
-						shift_buf(usrbuf, cur + (i - cur), len, -(i - cur));
-						len -= (i - cur);
-						wlen = MAX(0, wlen - 1);
-						break;
-					case '7' : // Home
-						cur = wcur = 0;
+						if (cur != len)
+							shift_buf(usrbuf, cur + 1, len--, -1);
 						escape++;
 						break;
-					case '8' : // End
-						cur = len;
-						wcur = wlen;
-						escape++;
-						break;
-					case 'C' : // Right arrow
-						for (i = cur + 1; i < len && (usrbuf[i] & 0xc0) == 0x80; i++)
-							;
-						cur = i;
-						wcur = MIN(wcur + 1, wlen);
-						break;
-					case 'D' : // Left Arrow
-						for (i = cur - 1; i > 0 && (usrbuf[i] & 0xc0) == 0x80; i--)
-							;
-						cur = i;
-						wcur = MAX(wcur - 1, 0);
-						break;
+					case '7' : cur = 0;   escape++;     break; // Home
+					case '8' : cur = len; escape++;     break; // End
+					case 'C' : cur = MIN(cur + 1, len); break; // Left  Arrow
+					case 'D' : cur = MAX(cur - 1, 0  ); break; // Right Arrow
 					default  : escape++;
 				}
 				escape--;
 						erase_usr();
 						usrbuf[len++] = '\n';
 						my_write(mfd, usrbuf, len);
-						start = cur = len = wcur = wlen = 0;
+						start = cur = len = 0;
 						break;
 					case BACKSPACE :
 					case DEL :
-						for (i = cur - 1; i >= 0 && (usrbuf[i] & 0xc0) == 0x80; i--)
-							;
 						if (cur != len)
-							shift_buf(usrbuf, cur, len, cur - i);
-						if (mbtowc(&wc, usrbuf + i, cur - i) < 0)
-							warnx("failed mbtowc conversion");
-						len = MAX(0, len - (cur - i));
-						cur = MAX(0, i);
-						if ((i = wcwidth(wc)) < 0)
-							warnx("failed wcwidth");
-						wlen = MAX(0, wlen - i);
-						wcur = MAX(0, wcur - i);
+							shift_buf(usrbuf, cur, len, -1);
+						cur = MAX(0, cur - 1);
+						len = MAX(0, len - 1);
 						break;
-					case CTRL('a') : cur = wcur = 0;          break;
-					case CTRL('e') : cur = len; wcur = wlen;  break;
-					case CTRL('k') : len = cur; wlen = wcur;  break;
-					case CTRL('u') : start = cur = len = 0;
-									 wcur = wlen = 0;         break;
+					case CTRL('a') : cur = 0;                 break;
+					case CTRL('b') : cur = MAX(0, cur - 1);   break;
+					case CTRL('e') : cur = len;               break;
+					case CTRL('f') : cur = MIN(len, cur + 1); break;
+					case CTRL('k') : len = cur;               break;
+					case CTRL('u') : start = cur = len = 0;   break;
 					case CTRL('w') :
-						for (i = cur - 1; i >= 0 && strchr(word_separators, usrbuf[i]); i--) {
-						    wcur--;
-							wlen--;
-						}
-						for (; i >=0 && !strchr(word_separators, usrbuf[i]); i--) {
-						    if ((usrbuf[i] & 0xc0) == 0xc0 || !(usrbuf[i] & 0x80)) {
-								wcur--;
-								wlen--;
-							}
-						}
-						i++;
-						shift_buf(usrbuf, cur, len, -(cur - i));
-						len -= cur - i;
-						cur = i;
-						break;
+						 for (i = cur - 1; i >= 0 && strchr(word_separators, usrbuf[i]); i--)
+							 ;
+						 for (; i >=0 && !strchr(word_separators, usrbuf[i]); i--)
+							 ;
+						 i++;
+						 shift_buf(usrbuf, cur, len, -(cur - i));
+						 len -= cur - i;
+						 cur = i;
+						 break;
 					case CTRL('l') :
-						cursor_org();
-						cursor_clr();
-						fflush(stdout);
-						kill(-child, SIGWINCH);
-						break;
+						 cursor_org();
+						 cursor_clr();
+						 fflush(stdout);
+						 kill(-child, SIGWINCH);
+						 break;
 					default :
-						if (cur != len)
-							shift_buf(usrbuf, cur, len, 1);
-						usrbuf[cur] = c;
-
-						if (c & 0x80) { // not ascii, so...
-							if ((c & 0x40) && !in_mb) { // start of multibyte character
-								for (in_mb = 0, mask = 0x80; mask & c; in_mb++, mask >>= 1)
-									;
-								in_mb--;
-								wstart = cur;
-							} else if ((c & 0xc0) == 0x80) { // continuation of multibyte character
-								if (--in_mb == 0) { // end of multibyte character
-									if (mbtowc(&wc, usrbuf + wstart, cur - wstart + 1) <= 0)
-										warnx("failed multibyte to wide char conversion");
-									if ((i = wcwidth(wc)) < 0)
-										warnx("failed to find width of wide char");
-									wcur += i;
-									wlen += i;
-								}
-							} else { // not the start, not continuing, something is wrong
-								warnx("invalide multibyte sequence");
-							}
-						} else { // ascii
-							wcur++;
-							wlen++;
-						}
-						cur++;
-						len++;
+						 if (cur != len)
+							 shift_buf(usrbuf, cur, len, 1);
+						 usrbuf[cur++] = c;
+						 len++;
+						 break;
 				}
 			}
-			if (!in_mb)
-				print_usr();
+			print_usr();
 		}
 
 		if (FD_ISSET(mfd, &rd)) {
 	if (i >= argc)
 		usage();
 
-	if (!setlocale(LC_CTYPE, ""))
-		err(1, "failed to set locale");
-
 	tcgetattr(0, &set);
 	set.c_lflag &= ~ECHO;
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.