Browse code

Add detection of "special parameters" for substitution

Detect Special parameters as defined in
http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02

Treat these as parameters that are not set, instead of
producing an error that a modifier is missing.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2018/05/26 00:59:32
Showing 3 changed files
... ...
@@ -174,3 +174,38 @@ A|$9                        |
174 174
 A|${9}                      |
175 175
 A|${9:+bbb}                 |
176 176
 A|${9:-bbb}                 |     bbb
177
+
178
+# Special parameters won't be set in the Dockerfile:
179
+# http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
180
+A|$@                        |
181
+A|${@}                      |
182
+A|${@:+bbb}                 |
183
+A|${@:-bbb}                 |     bbb
184
+A|$*                        |
185
+A|${*}                      |
186
+A|${*:+bbb}                 |
187
+A|${*:-bbb}                 |     bbb
188
+A|$#                        |
189
+A|${#}                      |
190
+A|${#:+bbb}                 |
191
+A|${#:-bbb}                 |     bbb
192
+A|$?                        |
193
+A|${?}                      |
194
+A|${?:+bbb}                 |
195
+A|${?:-bbb}                 |     bbb
196
+A|$-                        |
197
+A|${-}                      |
198
+A|${-:+bbb}                 |
199
+A|${-:-bbb}                 |     bbb
200
+A|$$                        |
201
+A|${$}                      |
202
+A|${$:+bbb}                 |
203
+A|${$:-bbb}                 |     bbb
204
+A|$!                        |
205
+A|${!}                      |
206
+A|${!:+bbb}                 |
207
+A|${!:-bbb}                 |     bbb
208
+A|$0                        |
209
+A|${0}                      |
210
+A|${0:+bbb}                 |
211
+A|${0:-bbb}                 |     bbb
... ...
@@ -318,7 +318,7 @@ func (sw *shellWord) processName() string {
318 318
 
319 319
 	for sw.scanner.Peek() != scanner.EOF {
320 320
 		ch := sw.scanner.Peek()
321
-		if name.Len() == 0 && unicode.IsDigit(ch) {
321
+		if name.Len() == 0 && (unicode.IsDigit(ch) || isSpecialParam(ch)) {
322 322
 			ch = sw.scanner.Next()
323 323
 			return string(ch)
324 324
 		}
... ...
@@ -332,6 +332,18 @@ func (sw *shellWord) processName() string {
332 332
 	return name.String()
333 333
 }
334 334
 
335
+// isSpecialParam checks if the provided character is a special parameters,
336
+// as defined in http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
337
+func isSpecialParam(char rune) bool {
338
+	switch char {
339
+	case '@', '*', '#', '?', '-', '$', '!', '0':
340
+		// Special parameters
341
+		// http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_02
342
+		return true
343
+	}
344
+	return false
345
+}
346
+
335 347
 func (sw *shellWord) getEnv(name string) string {
336 348
 	for _, env := range sw.envs {
337 349
 		i := strings.Index(env, "=")
... ...
@@ -26,13 +26,11 @@ func TestShellParser4EnvVars(t *testing.T) {
26 26
 		line := scanner.Text()
27 27
 		lineCount++
28 28
 
29
-		// Trim comments and blank lines
30
-		i := strings.Index(line, "#")
31
-		if i >= 0 {
32
-			line = line[:i]
29
+		// Skip comments and blank lines
30
+		if strings.HasPrefix(line, "#") {
31
+			continue
33 32
 		}
34 33
 		line = strings.TrimSpace(line)
35
-
36 34
 		if line == "" {
37 35
 			continue
38 36
 		}