Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goawk's printf family of functions under Windows #5

Closed
jftuga opened this issue Aug 20, 2018 · 7 comments
Closed

goawk's printf family of functions under Windows #5

jftuga opened this issue Aug 20, 2018 · 7 comments

Comments

@jftuga
Copy link

jftuga commented Aug 20, 2018

Please let single-quotes be used in the printf family of functions. Example:

C:\bin>dir c:\windows\system32 /s/a-d/-c | mawk "{sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}"
Total size: 4559643636 bytes,  4.25 (GB) for 24857 files.

Under goawk:

C:\bin>dir c:\windows\system32 /s/a-d/-c | goawk "{sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}"
------------------------------------
{sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}
                      ^
------------------------------------
parse error at 1:23: unexpected '\''

Under Windows, the double-quote has to be used to combine space-containing arguments into one single, command-line argument. Single quotes can not be used. Therefore, single-quotes must be allowed in the goawk implementation of the printf family of functions. Can you please make it so that the printf family can use either single or double quotes?

@benhoyt
Copy link
Owner

benhoyt commented Aug 20, 2018

Hmm, that's strange. Does the AWK (or mawk) grammar actually change on Windows to support single quoted strings? When I try to use single-quoted strings in mawk on Unix (OS X), I get:

$ mawk "BEGIN { printf('foo') }"
mawk: 1: unexpected character '''

So it surprises me that this would work on Windows. Does your first example work for awk and gawk too? From what I've seen by Googling, folks use double quotes but escape them according to Windows shell rules.

@jftuga
Copy link
Author

jftuga commented Aug 20, 2018

It does not work with gawk 4.2.1.

c:\>dir c:\windows\system32 /s/a-d/-c | .\gawk.exe "{sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}"
gawk: cmd. line:1: {sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}
gawk: cmd. line:1:                       ^ invalid char ''' in expression
gawk: cmd. line:1: {sum+=$4} END {printf('Total size: %.0f bytes,  %3.2f (GB) for %1d files.\n', sum, (sum/1073741824.0), NR)}
gawk: cmd. line:1:                       ^ syntax error

The ^ symbol is the escape character for Windows cmd-prompt, but escaping the double-quote still results in a syntax error...

gawk: cmd. line:1: {sum+=$4} END {printf(^Total
gawk: cmd. line:1:                       ^ syntax error

Even though single-quotes are not 'official', they still would be really nice to have under Windows.

@jftuga
Copy link
Author

jftuga commented Aug 20, 2018

A source for a working version of mawk for Windows is located here:

http://gnuwin32.sourceforge.net/packages/mawk.htm

Binaries |   Zip |   96988 |   29 September 2005 |   0c499dacf47c513a8d9af4ec2abe94a7

@benhoyt
Copy link
Owner

benhoyt commented Aug 21, 2018

I don't think I'm going to make this change, sorry. It's not per spec and I'd rather folks work around it on Windows using standard Windows escapes than change the syntax.

@benhoyt benhoyt closed this as completed Aug 21, 2018
@jftuga
Copy link
Author

jftuga commented Aug 21, 2018

I made a patch for this:

https://gist.github.com/jftuga/840cc95139cd009621313ca30037e532

I copied the case statement for the double quotes in lexer.go and then changed the two instances of:

from '"' to '\'':

benhoyt added a commit that referenced this issue Aug 21, 2018
It's trivial, and makes command lines much simpler on Windows where
the quote character is " (double quote). Thanks @jftuga for the
suggestion. Fixes #5.
@benhoyt
Copy link
Owner

benhoyt commented Aug 21, 2018

Hey @jftuga. I actually looked at the lexer and changed my mind on this. It's trivial (a two-line change) and doesn't break any syntax or really hurt anything. So I added the fix and lexer and interp tests for it in 4ac40bb. Hope that helps!

@jftuga
Copy link
Author

jftuga commented Aug 21, 2018

Thank you for making this change. I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants