commit c93a476220887577076cf269cd31060f020e3458
parent af2e76ca6b873d399aa9e0606e7cfd4dd8f5b9a1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 3 Dec 2015 21:11:28 +0100
Split getnum in 2 functions
There were 2 different things in getnum(), read a decimal number
and read an ed address. Read a decimal number is something we
are going to use in other parts, so it is better to have
a different function for it.
Diffstat:
M | ed.c | | | 43 | ++++++++++++++++++++++++------------------- |
1 file changed, 24 insertions(+), 19 deletions(-)
diff --git a/ed.c b/ed.c
@@ -291,16 +291,26 @@ skipblank(void)
}
static int
-getnum(int *line)
+getnum(void)
{
- int c, ln, sign = 1;
+ int ln;
+
+ for (ln = 0; isdigit(*cmdp); ln += *cmdp++ - '0') {
+ if (ln > INT_MAX/10)
+ error("invalid address");
+ ln *= 10;
+ }
+ return ln;
+}
+
+static int
+linenum(int *line)
+{
+ int ln, c, sign = 1;
skipblank();
switch (*cmdp) {
- case '-':
- case '+':
- --cmdp;
case '.':
ln = curln;
break;
@@ -323,18 +333,13 @@ getnum(int *line)
ln = match(c);
break;
default:
- if (isdigit(*cmdp)) {
- for (ln = 0; isdigit(*cmdp); ln += *cmdp++ - '0') {
- if (ln > INT_MAX/10)
- error("invalid address");
- ln *= 10;
- }
- --cmdp;
- break;
- }
- return 0;
+ if (!isdigit(*cmdp))
+ return 0;
+ *line = getnum();
+ case '-':
+ case '+':
+ return 1;
}
-
++cmdp;
*line = ln;
return 1;
@@ -354,7 +359,7 @@ address(int *line)
* /\// into ///, which will produce an
* error.
*/
- if (!getnum(&ln))
+ if (!linenum(&ln))
return 0;
for (;;) {
@@ -363,7 +368,7 @@ address(int *line)
if (c != '+' && c != '-')
break;
sign = c == '+';
- if (!getnum(&num))
+ if (!linenum(&num))
num = 1;
/* FIXME: detect integer overflow here */
ln += sign * num;
@@ -811,7 +816,7 @@ docmd()
if (nlines > 1)
goto bad_address;
if (isdigit(*cmdp))
- getnum(&num);
+ num = getnum();
else
num = 24;
chkprint(1);