shithub: neatroff

Download patch

ref: 1e65f096bbff9993fedc691c440837d20737a8a3
parent: 002f248ade44e83258b36bd0bd1bc2e3605dcc7a
author: Ali Gholami Rudi <[email protected]>
date: Fri Mar 2 07:12:55 EST 2018

cp: support \$^

Suggested and tested by aksr <[email protected]>.

--- a/cp.c
+++ b/cp.c
@@ -85,6 +85,27 @@
 	in_push(num_getfmt(regid()), NULL);
 }
 
+/* interpolate \$*, \$@, and \$^ */
+static void cp_args(int quote, int escape)
+{
+	struct sbuf sb;
+	char *s;
+	int i;
+	sbuf_init(&sb);
+	for (i = 1; i <= in_nargs(); i++) {
+		sbuf_append(&sb, i > 1 ? " " : "");
+		sbuf_append(&sb, quote ? "\"" : "");
+		s = in_arg(i);
+		while (*s) {
+			sbuf_append(&sb, escape && *s == '"' ? "\"" : "");
+			sbuf_add(&sb, (unsigned char) *s++);
+		}
+		sbuf_append(&sb, quote ? "\"" : "");
+	}
+	in_push(sbuf_buf(&sb), NULL);
+	sbuf_done(&sb);
+}
+
 /* interpolate \$1 */
 static void cp_arg(void)
 {
@@ -92,6 +113,18 @@
 	char *arg = NULL;
 	int argnum;
 	cparg(argname, sizeof(argname));
+	if (!strcmp("@", argname)) {
+		cp_args(1, 0);
+		return;
+	}
+	if (!strcmp("*", argname)) {
+		cp_args(0, 0);
+		return;
+	}
+	if (!strcmp("^", argname)) {
+		cp_args(1, 1);
+		return;
+	}
 	argnum = atoi(argname);
 	if (argnum > 0 && argnum < NARGS + 1)
 		arg = in_arg(argnum);