Powershell: inconsistent/strange behavior with quote parsing? - parsing

all! I'm trying to compile a program using PowerShell, but the command is being parsed strangely.
This command executes correctly in cmd.exe:
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
But PowerShell executes it as: (blue, navy, purple texts as they appear in PowerShell ISE)
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
This spits the following error:
The string starting:
At line:1 char:147
+ dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d"
"src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d <<<< " -D -O -release
is missing the terminator: ".
At line:1 char:163
So it seems to be interpreting a period as a quote. This is peculiar. Has anyone else had this problem with PowerShell?
Things I've tried:
escaping quotes
making sure all quotes are "straight quotes" and not angled
putting a space before quotes (parses correctly, but the program doesn't understand the arguments.)
Thanks,
Charles.

I believe this should do the trick (newlines added for clarity only, and removal of extra quotes):
dmd '-od"bin"' '-of"bin\convHull.exe"' '-I"src"'
src\concSort.d src\fileParser.d src\main.d src\pointLogic.d src\quickHull.d src\stupidHull.d
-D -O -release
Note that in the case where a quote (") is to be passed as part of the argument itself, I surrounded the entire argument with single quotes ('). From the experimentation below it can be seen that only -of"..." needs the quotes about it.
Happy coding.
I can't find a good reference on this exact production, however note the following parsings:
-x"w." -> error: " expected (last " is special)
-x"w."" -> -x"w and ."" (the . starts a new token and the " in that starts
a quote; however, the quotes are not removed)
'-x"w."' -> -x"w." (extra quote fine, neither special)
-x"w" -> -x"w" (no . and " not special)
-x"w"" -> -x"w"" (no . and " not special)
a".b" -> a.b (didn't start with `-`, quotes removed)
a".b -> error: " expected (" is special)
So it does indeed appear to have something to do with the . and - combination (and it might not be exclusive). From the above I believe that a token starting with - does not include the . character as a valid character in the token so the lexer terminates said token and starts a new token -- easily provable with a good EBNF reference, which I don't have.
The best I can find is Appendix C: The PowerShell Grammar:
The ParameterToken rule is used to match cmdlet parameters such as -foo or -
boolProp: . Note that this rule will also match --foobar, so this rule has
to be checked before the --token rule.
<ParameterToken> = -[:letter:]+[:]{0 |1}
However, this is incomplete at best and does not even include a definition of "letter".

I don't have the executable, but this seems to want to work.
$cmd = #'
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
'#
&$cmd

Related

invalid URL escape "%" when docker push

I set up a registry at docker-registry.elektron.space and when I want to push an image with $ docker push docker-registry.elektron.space/boxbeat-media-server, the upload animation is running in loop for each entity passing from "Pushing" state to "Retrying in X seconds".
After a while I get this error:
failed to parse Location header "https://docker-registry.elektron.space/v2/boxbeat-media-server/blobs/uploads/56244149-c196-439a-85bf-af1121e0b84b%?_state=h1lqY-NljkLbgzTCjd8jxcfdscojPHApblWu-45ISK57Ik5hbWUiOiJib3hiZWF0LW1lZGlhLXNlcnZlciIsIlVVSUQiOiI1NjI0NDE0OS1jMTk2LTQzOWEtODViZi1hZjExMjFlMGI4NGIiLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjAtMDMtMDFUMTU6MzI6NTAuMzcxNjc5NTc5WiJ9": parse https://docker-registry.elektron.space/v2/boxbeat-media-server/blobs/uploads/56244149-c196-439a-85bf-af1121e0b84b%?_state=h1lqY-NljkLbgzTCjd8jxcfdscojPHApblWu-45ISK57Ik5hbWUiOiJib3hiZWF0LW1lZGlhLXNlcnZlciIsIlVVSUQiOiI1NjI0NDE0OS1jMTk2LTQzOWEtODViZi1hZjExMjFlMGI4NGIiLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjAtMDMtMDFUMTU6MzI6NTAuMzcxNjc5NTc5WiJ9: invalid URL escape "%"
In a readable way:
failed to parse Location header
"https://docker-registry.elektron.space/v2/boxbeat-media-server/blobs/uploads/
56244149-c196-439a-85bf-af1121e0b84b%?_state=
h1lqY-NljkLbgzTCjd8jxcfdscojPHApblWu-45ISK57Ik5hbWUiOiJib3hiZWF0LW1lZGlhLXNlcnZlciIsIlVVSUQiOiI1NjI0NDE0OS1jMTk2LTQzOWEtODViZi1hZjExMjFlMGI4NGIiLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjAtMDMtMDFUMTU6MzI6NTAuMzcxNjc5NTc5WiJ9":
parse https://docker-registry.elektron.space/v2/boxbeat-media-server/blobs/uploads/
56244149-c196-439a-85bf-af1121e0b84b%?_state=
h1lqY-NljkLbgzTCjd8jxcfdscojPHApblWu-45ISK57Ik5hbWUiOiJib3hiZWF0LW1lZGlhLXNlcnZlciIsIlVVSUQiOiI1NjI0NDE0OS1jMTk2LTQzOWEtODViZi1hZjExMjFlMGI4NGIiLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjAtMDMtMDFUMTU6MzI6NTAuMzcxNjc5NTc5WiJ9:
invalid URL escape "%"
Where does this "%" come from? I thought this could come from zsh then I tried to run it with bash but same result.
Any idea?
The issue is that a % sign is used to initiate an escape sequence in url encoding. You need to escape the % itself.
So in your case you should replace the % with %25 which is the escaped form if it. That way you don't get the error because the parser doesn't think an escape sequence is about to start when it sees the %
... /uploads/56244149-c196-439a-85bf-af1121e0b84b%25 ...
This article can also help to understand things better. Even though its about javascript, the information is applicable much broader.
You can lookup escape sequences on this page.

Rascal: parsing string with multiple "_"s

I try to parse a string containing multiple "_"s, but I get a CallFailed exception.
I have tried to create a small as possible example of the problem syntax.
layout Layout = WhitespaceAndComment* !>> [\ \t\n\r#];
lexical WhitespaceAndComment = [\ \t\n\r] | #category="Comment" "#" ![\n]* $;
syntax SourceList = sourceList: "$"? "{"? Id sourceFile "}"?;
lexical Id = ([a-zA-Z/.\-][a-zA-Z0-9_/.]* !>> [a-zA-Z0-9_/.]) \ Reserved;
keyword Reserved =
"$" | "{" | "}" ;
I am unable to parse this small example.
rascal>try { parse(#SourceList, "test"); } catch CallFailed(m, e): println("<m> : <e>");
|prompt:///|(25,9,<1,25>,<1,34>) : [type(sort("SourceList"),(sort("SourceList"):choice(sort("SourceList"),{prod(label("sourceList",sort("SourceList")),[opt(lit("$")),layouts("$default$"),opt(lit("{")),layouts("$default$"),label("sourceFile",lex("Id")),layouts("$default$"),opt(lit("}"))],{})}),layouts("$default$"):choice(layouts("$default$"),{prod(layouts("$default$"),[],{})}),empty():choice(empty(),{prod(empty(),[],{})}),lex("Id"):choice(lex("Id"),{prod(lex("Id"),[conditional(seq([\char-class([range(45,47),range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(46,57),range(65,90),range(95,95),range(97,122)])),{\not-follow(\char-class([range(46,57),range(65,90),range(95,95),range(97,122)]))})]),{delete(keywords("Reserved"))})],{})}),keywords("Reserved"):choice(keywords("Reserved"),{prod(keywords("Reserved"),[lit("$")],{}),prod(keywords("Reserved"),[lit("}")],{}),prod(keywords("Reserved"),[lit("{")],{})}))),"${test}"]
ok
A changed sourcefile from "test" to "${test}" gives exactly the same output.
The complete syntax in which SourceList is embedded has many more rules. But then I get the following results.
set(${TARGET_NAME}_DEPS
GenConfiguration_OBJ_TN_Common # accept
${COMMON_BB_PCMDEPS} # reject
COMMON_BB_PCMDEPS # accept
COMMON_BB_PCM_DEPS # reject
)
for which I want to have a solution.
What is wrong with the minimal example? Why is test or ${test} not accepted?
BTW: I am using the latest unstable. Does it make sense to install and try the stable release?
I've tried to reproduce your problem, but it seems to work here:
rascal>parse(#SourceList, "test")
SourceList: (SourceList) `test`
The unstable version is fine at the moment. In fact it's high time to release a stable version. So for now you're better off with the unstable version.
The CallFailed exception is confusing. It means that a function is called which can not be matched OR not be found. So maybe parse is not in scope by not importing ParseTree, or you have a different function called parse which does not have a type[Tree] and str as the expected parameters in scope. As long as the ParseTree module is imported, you're call to parse should be fine.
Please let me know if you have made progress. Perhaps a restart of Eclipse might clear up something as well.

Linux expect newline

I have a custom package I want to install automatically in my docker using expect.
The first thing the package asks me to do is press Enter to continue, then it prints another 2 empty lines then it waits for an input.
My expect script :
#!/usr/bin/expect -f
set timeout -1
spawn ./install
expect "\n"
send -- "\n"
But as you can see in the image, it just runs the installer and exits.
I tried removing the expect "\n" so only send -- "\n" will execute but now even the install message doesn't appear (tried with set timeout 1000 before send and it also didn't work)
Any ideas?
P.S : This is a link to the package if anyone wants to have a go at it:
https://www.bayometric.com/downloads/digital-persona/DP_UareU_Linux223_20140429.2.zip
(the installer is inside DP-UareU-RTE-2.2.3-1.20140429_1533.tar.gz)
expect "\n" match a linefeed exactly, I think this is not what your program is sending.
To wait for a Shell prompt you can use expect "%" or expect "*" to match anything.
If you need to make sure you're dealing with the right prompt you may be able to use something like expect "*Linux Installation*".
Also don't send \n but \r for the enter key :
#!/usr/bin/expect
spawn ./install
expect "*Linux Installation*"
send "\r"
expect eof
Note that the default flag is -gl for glob pattern matching but you can also use the -re flag for regular expression matching.

I want to count detected strings in file with grep, excluding a specific phrase

Hi I'm trying to search a log file for the following words and assign the number of matches to a variable as a number.
errors
error
fail
failure
can't
But I don't want to match the words error or errors if its preceeded by "No "
So ignore error if its "no error" and ignore errors if its "no errors"
Here's what I have so far
ErrorCheck=$(grep -vi "No errors" $LOGFILE | grep -ciE "error|fail|can't" $LOGFILE)
Its not working out for me unfortunatly, any suggestions would be great.
PS I'm using microcore running busybox shell, so I have a slightly lean environment to work in.
All comments and suggestions welcome.
Thanks for your input.
Well, I think one problem is that the "-v" flag for grep will basically omit the entire line containing 'No errors' or any other string you specify. So if a single line contains both "no failures" and "can't" for example, you'd have a problem.
One possible (sort of janky) way to do this could be to store values in three different variables: NUM_COUNTED minus NUM_NOTS = ErrorCheck, which should account for having a "no failure" and an actual failure indicator in the same line.
NUM_COUNTED=grep -ciF 'error
fail
can't' $LOGFILE
NUM_NOTS=grep -ciF 'no error
no fail' $LOGFILE
ErrorCheck=`expr $NUM_COUNTED - $NUM_NOTS`
Alternatively, this seems to be giving (mostly accurate) results:
ErrorCheck=$(grep -vi 'No errors' $LOGFILE | grep -ciF 'error
fail
can't' $LOGFILE)
The -F flag just tells grep to look for string literals (error, fail, and can't) which are newline separated.
Hope this helps.

Enclosing a python Path variable in quotes?

I need help fixing a Python script, but know ABSOLUTELY NOTHING about Python (though I am an experienced programmer so I get the jargon.)
I have this script that is trying to write output to a generated path, but when I run it, it gives me an error saying something about "E:\Program", and when I check, sure enough, it creates a folder named "Program" in the root of my E: drive. I'm CERTAIN it is trying to write to "E:\Program Files" but the space is terminating the command.
I did find where "path" is assigned:
path = tkm.path_archuncomp + bs.path + bs.name + '.ext'
How would I enclose that path in quotes that are assigned to the variable?
Inserting the quotes character appears to be the same as languages like C. Simply use "slash" notion:
path = \" + some_variable + \"
Unfortunately for me, repairing the script is more complicated than that, and doing so only created errors, so I can't confirm that was done properly. :(

Resources