FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/helpers/command_parser.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
13
(13)
Pull requests
8
(8)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
helpers
/
command_parser.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
179 lines (150 loc) · 6.48 KB
Breadcrumbs
cecli
/
cecli
/
helpers
/
command_parser.py
Copy path
File metadata and controls
179 lines (150 loc) · 6.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from
typing
import
List
def
split_shell_commands
(
commands_str
:
str
)
->
List
[
str
]:
"""
Split shell commands on semicolons, respecting quoted strings and heredoc boundaries.
Args:
commands_str: String containing shell commands separated by semicolons
Returns:
List of command strings split at semicolons outside quotes/heredocs
"""
if
not
commands_str
:
return
[]
commands
:
List
[
str
]
=
[]
current_command
=
[]
# State tracking
in_single_quote
=
False
in_double_quote
=
False
in_heredoc
=
False
heredoc_delimiter
=
""
heredoc_ignore_tabs
=
False
# True for <<-EOF (dash before delimiter)
heredoc_seen_newline
=
False
# Have we seen a newline since entering heredoc?
escaped
=
False
i
=
0
n
=
len
(
commands_str
)
while
i
<
n
:
char
=
commands_str
[
i
]
if
escaped
:
# Current character is escaped, treat it as literal
current_command
.
append
(
char
)
escaped
=
False
i
+=
1
continue
if
char
==
"
\\
"
:
# Escape character
escaped
=
True
current_command
.
append
(
char
)
i
+=
1
continue
if
not
in_heredoc
:
# Not in heredoc, handle quotes and semicolons
if
char
==
"'"
and
not
in_double_quote
:
in_single_quote
=
not
in_single_quote
current_command
.
append
(
char
)
elif
char
==
'"'
and
not
in_single_quote
:
in_double_quote
=
not
in_double_quote
current_command
.
append
(
char
)
elif
char
==
";"
and
not
in_single_quote
and
not
in_double_quote
:
# Split at semicolon outside quotes
command
=
""
.
join
(
current_command
).
strip
()
if
command
:
commands
.
append
(
command
)
current_command
=
[]
else
:
# Check for heredoc start (<<)
if
char
==
"<"
and
i
+
1
<
n
and
commands_str
[
i
+
1
]
==
"<"
:
# Remember position for capturing the heredoc start
heredoc_start_pos
=
i
i
+=
2
# Skip "<<"
# Check for dash (<<-)
if
i
<
n
and
commands_str
[
i
]
==
"-"
:
heredoc_ignore_tabs
=
True
i
+=
1
# Skip whitespace before delimiter
while
i
<
n
and
commands_str
[
i
]
in
(
" "
,
"
\t
"
):
i
+=
1
# Parse delimiter (can be quoted or unquoted)
delimiter_start
=
i
# Check for quotes
if
i
<
n
and
commands_str
[
i
]
in
(
"'"
,
'"'
):
quote_char
=
commands_str
[
i
]
i
+=
1
delimiter_start
=
i
# Find closing quote
while
i
<
n
and
commands_str
[
i
]
!=
quote_char
:
i
+=
1
heredoc_delimiter
=
commands_str
[
delimiter_start
:
i
]
if
i
<
n
:
i
+=
1
# Skip closing quote
else
:
# Unquoted delimiter - alphanumeric and underscore only
while
i
<
n
and
(
commands_str
[
i
].
isalnum
()
or
commands_str
[
i
]
==
"_"
):
i
+=
1
heredoc_delimiter
=
commands_str
[
delimiter_start
:
i
]
if
heredoc_delimiter
:
# Valid heredoc found - capture the entire heredoc start
heredoc_start_text
=
commands_str
[
heredoc_start_pos
:
i
]
current_command
.
append
(
heredoc_start_text
)
# Enter heredoc mode
in_heredoc
=
True
heredoc_seen_newline
=
False
continue
else
:
# Not a valid heredoc, treat as normal "<<"
current_command
.
append
(
"<<"
)
continue
else
:
current_command
.
append
(
char
)
else
:
# Inside heredoc
current_command
.
append
(
char
)
# Track if we've seen a newline (heredoc content starts after newline)
if
char
==
"
\n
"
:
heredoc_seen_newline
=
True
# Look ahead to see if next line starts with delimiter
j
=
i
+
1
# Skip leading whitespace (tabs if heredoc_ignore_tabs)
while
j
<
n
and
commands_str
[
j
]
in
(
" "
,
"
\t
"
):
if
heredoc_ignore_tabs
and
commands_str
[
j
]
==
"
\t
"
:
j
+=
1
elif
not
heredoc_ignore_tabs
:
j
+=
1
else
:
break
# Check if we have the delimiter at this position
if
(
j
+
len
(
heredoc_delimiter
)
<=
n
and
commands_str
[
j
:
j
+
len
(
heredoc_delimiter
)]
==
heredoc_delimiter
):
# Check what comes after delimiter
k
=
j
+
len
(
heredoc_delimiter
)
if
k
>=
n
or
commands_str
[
k
]
in
(
" "
,
"
\t
"
,
"
\n
"
,
"
\r
"
,
";"
):
# Found heredoc end
in_heredoc
=
False
heredoc_delimiter
=
""
heredoc_ignore_tabs
=
False
heredoc_seen_newline
=
False
elif
char
==
";"
and
not
heredoc_seen_newline
:
# Semicolon before seeing a newline in heredoc mode
# This means heredoc start was immediately followed by semicolon
# Not a real heredoc - split at this semicolon
in_heredoc
=
False
heredoc_delimiter
=
""
heredoc_ignore_tabs
=
False
heredoc_seen_newline
=
False
# Remove the semicolon from current_command
current_command
.
pop
()
# Split at this semicolon
command
=
""
.
join
(
current_command
).
strip
()
if
command
:
commands
.
append
(
command
)
current_command
=
[]
continue
i
+=
1
# Add the last command if any
if
escaped
:
# Handle trailing escape character
current_command
.
append
(
"
\\
"
)
command
=
""
.
join
(
current_command
).
strip
()
if
command
:
commands
.
append
(
command
)
return
commands
Back
|
FazBrowse Home
|
New Git URL