summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-06-04 12:59:12 +0000
committertjr <tjr@FreeBSD.org>2002-06-04 12:59:12 +0000
commit33d27dd210c9b2a95f1ab802306f257894298a4f (patch)
tree20257442bc2cc5cae672c11d72306aa5eae7e200
parentf5f8d2529997c1813e19eef4f358b49317c1698b (diff)
downloadFreeBSD-src-33d27dd210c9b2a95f1ab802306f257894298a4f.zip
FreeBSD-src-33d27dd210c9b2a95f1ab802306f257894298a4f.tar.gz
Quote alias values in the output of the alias(1) builtin so they are
suitable for re-input to the shell (SUSv3)
-rw-r--r--bin/sh/alias.c14
-rw-r--r--bin/sh/output.c33
-rw-r--r--bin/sh/sh.12
3 files changed, 44 insertions, 5 deletions
diff --git a/bin/sh/alias.c b/bin/sh/alias.c
index b899edb..f6b462b 100644
--- a/bin/sh/alias.c
+++ b/bin/sh/alias.c
@@ -207,8 +207,11 @@ aliascmd(int argc, char **argv)
for (i = 0; i < ATABSIZE; i++)
for (ap = atab[i]; ap; ap = ap->next) {
- if (*ap->name != '\0')
- out1fmt("alias %s=%s\n", ap->name, ap->val);
+ if (*ap->name != '\0') {
+ out1fmt("alias %s=", ap->name);
+ out1qstr(ap->val);
+ out1c('\n');
+ }
}
return (0);
}
@@ -217,8 +220,11 @@ aliascmd(int argc, char **argv)
if ((ap = lookupalias(n, 0)) == NULL) {
outfmt(out2, "alias: %s not found\n", n);
ret = 1;
- } else
- out1fmt("alias %s=%s\n", n, ap->val);
+ } else {
+ out1fmt("alias %s=", n);
+ out1qstr(ap->val);
+ out1c('\n');
+ }
else {
*v++ = '\0';
setalias(n, v);
diff --git a/bin/sh/output.c b/bin/sh/output.c
index 56a437c..9b8fa5c 100644
--- a/bin/sh/output.c
+++ b/bin/sh/output.c
@@ -107,6 +107,11 @@ out1str(const char *p)
outstr(p, out1);
}
+void
+out1qstr(const char *p)
+{
+ outqstr(p, out1);
+}
void
out2str(const char *p)
@@ -114,6 +119,11 @@ out2str(const char *p)
outstr(p, out2);
}
+void
+out2qstr(const char *p)
+{
+ outqstr(p, out2);
+}
void
outstr(const char *p, struct output *file)
@@ -124,10 +134,31 @@ outstr(const char *p, struct output *file)
flushout(file);
}
+/* Like outstr(), but quote for re-input into the shell. */
+void
+outqstr(const char *p, struct output *file)
+{
+ char ch;
+
+ out1c('\'');
+ while ((ch = *p++) != '\0') {
+ switch (ch) {
+ case '\'':
+ /*
+ * Can't quote single quotes inside single quotes;
+ * close them, write escaped single quote, open again.
+ */
+ outstr("'\\''", file);
+ break;
+ default:
+ outc(ch, file);
+ }
+ }
+ out1c('\'');
+}
char out_junk[16];
-
void
emptyoutbuf(struct output *dest)
{
diff --git a/bin/sh/sh.1 b/bin/sh/sh.1
index b1bd6ff..57a21ee 100644
--- a/bin/sh/sh.1
+++ b/bin/sh/sh.1
@@ -1268,6 +1268,8 @@ With no arguments, the
builtin command prints the names and values of all defined aliases
(see
.Ic unalias ) .
+Alias values are written with appropriate quoting so that they are
+suitable for reinput to the shell.
.It Ic bg Op Ar job ...
Continue the specified jobs
(or the current job if no jobs are given)
OpenPOWER on IntegriCloud