ref: 7c5f650fdfbb739ca71414c6701136b8882cb01b
parent: a236d3e6aa58b9bd5cfdaab5be4a97045ddb28e0
author: Ali Gholami Rudi <ali@rudi.ir>
date: Sun Apr 14 12:08:47 EDT 2013
tr: tabs are trimmed for request arguments
--- a/tr.c
+++ b/tr.c
@@ -66,9 +66,10 @@
static void tr_rm(char **args)
{
- if (!args[1])
- return;
- str_rm(REG(args[1][0], args[1][1]));
+ int i;
+ for (i = 1; i <= NARGS; i++)
+ if (args[i])
+ str_rm(REG(args[i][0], args[i][1]));
}
static void tr_rn(char **args)
@@ -281,8 +282,9 @@
char *e = s + len - 1;
int quoted = 0;
int c;
- while ((c = cp_next()) == ' ')
- ;
+ c = cp_next();
+ while (c == ' ')
+ c = cp_next();
if (c == '"') {
quoted = 1;
c = cp_next();
@@ -322,7 +324,7 @@
return s;
}
-/* read macro arguments */
+/* read macro arguments; trims tabs if rmtabs is nonzero */
static int mkargs(char **args, char *buf, int len)
{
char *s = buf;
@@ -330,17 +332,45 @@
int c;
int n = 0;
while (n < NARGS) {
+ char *r = s;
c = cp_next();
if (c < 0 || c == '\n')
return n;
cp_back(c);
- args[n++] = s;
s = arg_normal(s, e - s);
+ if (*r != '\0')
+ args[n++] = r;
}
jmp_eol();
return n;
}
+/* read request arguments; trims tabs too */
+static int mkargs_req(char **args, char *buf, int len)
+{
+ char *r, *s = buf;
+ char *e = buf + len - 1;
+ int c;
+ int n = 0;
+ c = cp_next();
+ while (n < NARGS && s < e) {
+ r = s;
+ while (c == ' ' || c == '\t')
+ c = cp_next();
+ while (c >= 0 && c != '\n' && c != ' ' && c != '\t' && s < e) {
+ *s++ = c;
+ c = cp_next();
+ }
+ *s++ = '\0';
+ if (*r != '\0')
+ args[n++] = r;
+ if (c < 0 || c == '\n')
+ return n;
+ }
+ jmp_eol();
+ return n;
+}
+
/* read arguments for .ds */
static int mkargs_ds(char **args, char *buf, int len)
{
@@ -364,7 +394,7 @@
char *e = buf + len - 1;
args[0] = s;
s = arg_regname(s, e - s);
- return mkargs(args + 1, s, e - s) + 1;
+ return mkargs_req(args + 1, s, e - s) + 1;
}
/* do not read arguments; for .if, .ie and .el */
@@ -454,7 +484,7 @@
if (req->args)
req->args(args + 1, buf, sizeof(buf));
else
- mkargs(args + 1, buf, sizeof(buf));
+ mkargs_req(args + 1, buf, sizeof(buf));
req->f(args);
} else {
mkargs(args + 1, buf, sizeof(buf));