summaryrefslogtreecommitdiff
path: root/parse
blob: b903a57c0b3c56b2725ad9a49c5b359221403409 (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
#!/bin/sh

#   This file is part of AGNOSTIC - Unix Emulation in Javascript.
#   Copyright (C) 2014-2015 by Assaf Gordon <assafgordon@gmail.com>
#   Released under GPLv3 or later

if [ -z "$1" ] ; then
echo "AGNOSTIC - Unix Emulation in JavaScript
Shell-Syntax-Parse tool

Usage:
  parse [shell command]

This debugging tool will parse the shell command,
then print the JSON-syntax structure syntax of the command.

NOTE: this script tests only the syntax parsing step.
      unknown shell commands can be successfully parsed
      (e.g. \"seq 1 10 | no_such_program >/dev/sbd1\" is valid syntax,
       but will fail when actually executed).

Example:
    $ ./parse 'seq 1 10 | wc -l && echo ok'
    {
      \"and_or_list\": [
        {
          \"pipeline\": [
            {
              \"SimpleCommand\": {
                \"command\": [
                  { \"literal\": \"seq\" },
                  { \"delimiter\": null },
                  { \"literal\": \"1\" },
                  { \"delimiter\": null },
                  { \"literal\": \"10\" }
                ]
              }
            },
            {
              \"SimpleCommand\": {
                \"command\": [
                  { \"literal\": \"wc\" },
                  { \"delimiter\": null },
                  { \"literal\": \"-l\" }
                ]
              }
            }
          ]
        },
        \"&&\",
        {
          \"SimpleCommand\": {
            \"command\": [
              { \"literal\": \"echo\" },
              { \"delimiter\": null },
              { \"literal\": \"ok\" }
            ]
          }
        }
      ]
    }
"
exit 1
fi

if ! which jq 2>/dev/null >/dev/null ; then
	echo "Error: this script requires the 'jq' program (which was not found)" >&2
	exit 1
fi

nodejs ./src/tools/shell_parse.js "$@" | jq .