[hackers] [PATCH 03/17] Move cmdline to String type

From: quinq <quinq_AT_fifth.space>
Date: Tue, 6 Mar 2018 12:57:55 +0100

From: "Roberto E. Vargas Caballero" <k0ga_AT_shike2.com>

---
 ed.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/ed.c b/ed.c
index 97fa5e8..ca55c61 100644
--- a/ed.c
+++ b/ed.c
_AT_@ -61,9 +61,8 @@ static size_t sizetxt, memtxt;
 static int scratch;
 static int pflag, modflag, uflag, gflag;
 static size_t csize;
-static char *cmdline;
+static String cmdline;
 static char *ocmdline;
-static size_t cmdsiz, cmdcap;
 static int repidx;
 static char *rhs;
 static char *lastmatch;
_AT_@ -76,12 +75,15 @@ discard(void)
 {
 	int c;
 
-	/* discard until end of line */
-	if (repidx < 0  &&
-	    ((cmdsiz > 0 && cmdline[cmdsiz-1] != '\n') || cmdsiz == 0)) {
-		while ((c = getchar()) != '\n' && c != EOF)
-			/* nothing */;
-	}
+	if (repidx >= 0)
+		return;
+
+	/* discard until the end of the line */
+	if (cmdline.siz > 0 && cmdline.str[cmdline.siz-1] == '\n')
+		return;
+
+	while ((c = getchar()) != '\n' && c != EOF)
+		;
 }
 
 static void undo(void);
_AT_@ -158,7 +160,7 @@ input(void)
 		return ocmdline[repidx++];
 
 	if ((c = getchar()) != EOF)
-		cmdline = addchar(c, cmdline, &cmdcap, &cmdsiz);
+		addchar_(c, &cmdline);
 	return c;
 }
 
_AT_@ -170,7 +172,7 @@ back(int c)
 	} else {
 		ungetc(c, stdin);
 		if (c != EOF)
-			--cmdsiz;
+			--cmdline.siz;
 	}
 	return c;
 }
_AT_@ -1085,7 +1087,7 @@ repeat:
 		execsh();
 		break;
 	case EOF:
-		if (cmdsiz == 0)
+		if (cmdline.siz == 0)
 			quit();
 	case '\n':
 		if (gflag && uflag)
_AT_@ -1281,8 +1283,8 @@ save_last_cmd:
 	if (rep)
 		return;
 	free(ocmdline);
-	cmdline = addchar('\0', cmdline, &cmdcap, &cmdsiz);
-	if ((ocmdline = strdup(cmdline)) == NULL)
+	addchar_('\0', &cmdline);
+	if ((ocmdline = strdup(cmdline.str)) == NULL)
 		error("out of memory");
 }
 
_AT_@ -1332,7 +1334,7 @@ doglobal(void)
 	int i, k;
 
 	skipblank();
-	cmdsiz = 0;
+	cmdline.siz = 0;
 	gflag = 1;
 	if (uflag)
 		chkprint(0);
_AT_@ -1394,7 +1396,7 @@ edit(void)
 	for (;;) {
 		newcmd = 1;
 		ocurln = curln;
-		cmdsiz = 0;
+		cmdline.siz = 0;
 		repidx = -1;
 		if (optprompt) {
 			fputs(prompt, stdout);
-- 
2.14.2
Received on Tue Mar 06 2018 - 12:57:55 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 06 2018 - 13:01:31 CET