summaryrefslogtreecommitdiff
path: root/shell/ash_remove_unnecessary_code_in_backquote_expansion.patch
blob: 06067dde0b0d44d7591347cfa56c7e2398332788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
From: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Date: Thu, 19 Apr 2018 18:16:12 +0800

> ash originally had support for omitting the fork when expanding a
> builtin in backquotes.  dash has gradually been removing this support,
> most recently in commit 66b614e29038e31745c4a5d296f64f8d64f5c377
> ("[EVAL] Remove unused EV_BACKCMD flag").
>
> Some traces still remain, however.  Remove:
>
> - the buf and nleft elements of the backcmd structure;
> - a misleading comment regarding handling of builtins.
>
> Signed-off-by: Ron Yorston <rmy@xxxxxxxxxxxx>

Unfortunately we may need this at some point in the future due
to changes in POSIX.  So let's keep it around for now until we
get things such as `jobs -p` to work.

*************************************

From: Ron Yorston <rmy@xxxxxxxxxxxx>
Date: Thu, 19 Apr 2018 17:18:47 +0100

>Unfortunately we may need this at some point in the future due
>to changes in POSIX.  So let's keep it around for now until we
>get things such as `jobs -p` to work.

As you wish.

Something even more trivial I noticed later:  the TRACE at the end of
expbackq incorrectly refers to the function as evalbackq.

*************************************

Date: Tue, 10 Apr 2018 13:23:35 +0100
From: Ron Yorston <rmy@pobox.com>
To: busybox@busybox.net
Subject: [PATCH] ash: remove unnecessary code in backquote expansion

Some traces remain of ash's ancient support for omitting the fork when
expanding a builtin command in backquotes.

Remove:

- the buf and nleft elements of the backcmd structure;
- a misleading comment regarding handling of builtins.

I've submitted a similar patch to dash.

Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 shell/ash.c | 37 +++++++++----------------------------
 1 file changed, 9 insertions(+), 28 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 45c747dbc..6f1458722 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -6356,15 +6356,12 @@ exptilde(char *startp, char *p, int flags)
 }
 
 /*
- * Execute a command inside back quotes.  If it's a builtin command, we
- * want to save its output in a block obtained from malloc.  Otherwise
- * we fork off a subprocess and get the output of the command via a pipe.
- * Should be called with interrupts off.
+ * Execute a command inside back quotes.  We fork off a subprocess and
+ * get the output of the command via a pipe.  Should be called with
+ * interrupts off.
  */
 struct backcmd {                /* result of evalbackcmd */
 	int fd;                 /* file descriptor to read from */
-	int nleft;              /* number of chars in buffer */
-	char *buf;              /* buffer */
 	struct job *jp;         /* job structure for command */
 };
 
@@ -6394,8 +6391,6 @@ evalbackcmd(union node *n, struct backcmd *result)
 	struct job *jp;
 
 	result->fd = -1;
-	result->buf = NULL;
-	result->nleft = 0;
 	result->jp = NULL;
 	if (n == NULL) {
 		goto out;
@@ -6432,8 +6427,7 @@ evalbackcmd(union node *n, struct backcmd *result)
 	result->jp = jp;
 
  out:
-	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
-		result->fd, result->buf, result->nleft, result->jp));
+	TRACE(("evalbackcmd done: fd=%d jp=0x%x\n", result->fd, result->jp));
 }
 
 /*
@@ -6445,7 +6439,6 @@ expbackq(union node *cmd, int flag)
 	struct backcmd in;
 	int i;
 	char buf[128];
-	char *p;
 	char *dest;
 	int startloc;
 	int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
@@ -6457,24 +6450,12 @@ expbackq(union node *cmd, int flag)
 	evalbackcmd(cmd, &in);
 	popstackmark(&smark);
 
-	p = in.buf;
-	i = in.nleft;
-	if (i == 0)
-		goto read;
-	for (;;) {
-		memtodest(p, i, syntax, flag & QUOTES_ESC);
- read:
-		if (in.fd < 0)
-			break;
-		i = nonblock_immune_read(in.fd, buf, sizeof(buf));
-		TRACE(("expbackq: read returns %d\n", i));
-		if (i <= 0)
-			break;
-		p = buf;
-	}
-
-	free(in.buf);
 	if (in.fd >= 0) {
+		while ((i = nonblock_immune_read(in.fd, buf, sizeof(buf))) > 0) {
+			TRACE(("expbackq: read returns %d\n", i));
+			memtodest(buf, i, syntax, flag & QUOTES_ESC);
+		}
+
 		close(in.fd);
 		back_exitstatus = waitforjob(in.jp);
 	}