Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F18824750
PhutilShellLexer.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
2 KB
Subscribers
None
PhutilShellLexer.php
View Options
<?php
/**
* Lexer for shell-like argument strings. Somewhat similar to Python's shlex.
*/
final
class
PhutilShellLexer
extends
PhutilLexer
{
/**
* Convert a shell command string into an argument vector. For example, this
* converts a string like:
*
* a 'b c' "d"'"'"e" f\ g "\""
*
* ...into this argument vector:
*
* array(
* "a",
* "b c",
* "d\"e",
* "f g",
* "\"",
* );
*
* @param string Shell command string.
* @return array Parsed argument vector.
*/
public
function
splitArguments
(
$string
)
{
$tokens
=
$this
->
getTokens
(
$string
);
if
(
count
(
$this
->
getLexerState
())
>
1
)
{
throw
new
UnexpectedValueException
(
pht
(
'Unterminated string in argument list!'
));
}
foreach
(
$tokens
as
$key
=>
$token
)
{
switch
(
$token
[
0
])
{
case
"'"
:
case
'"'
:
unset
(
$tokens
[
$key
]);
break
;
case
'esc'
:
$tokens
[
$key
][
0
]
=
'arg'
;
$tokens
[
$key
][
1
]
=
substr
(
$token
[
1
],
1
);
break
;
default
:
break
;
}
}
$tokens
=
$this
->
mergeTokens
(
array_values
(
$tokens
));
$argv
=
array
();
foreach
(
$tokens
as
$token
)
{
if
(
$token
[
0
]
==
'arg'
)
{
$argv
[]
=
$token
[
1
];
}
}
return
$argv
;
}
protected
function
getRawRules
()
{
return
array
(
'start'
=>
array
(
array
(
'
\s
+'
,
' '
),
array
(
"'"
,
"'"
,
'string1'
),
array
(
'"'
,
'"'
,
'string2'
),
array
(
'
\\\\
.'
,
'esc'
),
array
(
'[^
\\
s
\'
"
\\\\
]+'
,
'arg'
),
),
'string1'
=>
array
(
// NOTE: In a single-quoted string, backslash is not an escape.
array
(
'[^
\'
]+'
,
'arg'
),
array
(
"'"
,
"'"
,
'!pop'
),
),
'string2'
=>
array
(
// NOTE: In a double-quoted string, backslash IS an escape, but only
// for some characters: ", $, `, \ and newline.
array
(
'[^"
\\\\
]+'
,
'arg'
),
array
(
'"'
,
'"'
,
'!pop'
),
array
(
'
\\\\
["$`
\\\\\\
n]'
,
'esc'
),
array
(
'
\\\\
.'
,
'arg'
),
),
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Dec 23, 1:17 PM (53 m, 53 s)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
dc/0a/f7ade0a966f35e14ae871f92cb64
Attached To
rPHUTIL libphutil
Event Timeline
Log In to Comment