Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read /? literally from ARGV[1]

61 views
Skip to first unread message

Marc de Bourget

unread,
Nov 19, 2016, 1:32:36 PM11/19/16
to
If the user types the program name followed by "/?", I want
to output a help usage message so the command line call is:

e.g. gawk -f specialtasktest.awk /?

However, I can't catch /? from ARGV[1].
# specialtasktest.awk
BEGIN {
print ARGV[1]
}

gawk -f specialtasktest.awk /?
=> /d

It seems due to filename expansion on the command line
arguments /? gets changed to /d (not shure what /d is).

How to get /? instead of /d as ARGV[1]?

I'm on Windows, behavior is the same for GAWK and TAWK.

Janis Papanagnou

unread,
Nov 19, 2016, 1:47:25 PM11/19/16
to
What shell are you using on Windows? It might help to escape
or quote the globbing patterns, "/?", '/?', or /\?, but that
depends on the shell. It might be work to use Unix convention
and define -? as option switch for the help usage message, or
any syntax that is not using path delimiting characters like
the slashes / or \.

Janis

Marc de Bourget

unread,
Nov 19, 2016, 2:10:13 PM11/19/16
to
I use the default Windows command line (cmd.exe).
Only '/?' and /\? would work. They print the content literally.
However, this is much too ugly, I don't want to force the user
to write something like /\? to get help. /? is quite usual for
Windows command line tools so I would like not to renounce it.
What is your output for /? on Linux? Thank you.

Janis Papanagnou

unread,
Nov 19, 2016, 2:30:40 PM11/19/16
to
On Linux (or Unix systems in general) the ? is [usually] taken
as globbing character, it tries to find a matching file. If no
file match is found it is passed literally:

$ awk 'BEGIN { print ARGV[1] }' /?
/?

(In Unix shell you can also disable file globbing, so that even
in presence of a file named, say d, below / (root) you will get
always a literal /? .)

Janis

Kenny McCormack

unread,
Nov 19, 2016, 2:45:54 PM11/19/16
to
In article <d694b725-6602-4d54...@googlegroups.com>,
My first reaction seeing your post was: Are you using Cygwin?
(Janis will understand...)

But I think Janis has actually missed a subtlety. Even if you are not using
a Unix-like shell, the real problem is that, under Windows, both GAWK and
TAWK do their own filename expansion (to make up for the fact that the
defective shells shipped with Windows don't). The result is that you get
filename expansion even if you don't want it.

Anyway, I just tested with TAWK (Note that I don't have a GAWK for Windows
at the moment). With this as "x.awk":
BEGIN { print ARGV[1] }

I can reproduce the problem, like this:

C> echo foo > \q
C> awkw -f x /?
/q
C> del \q

Note that I have to have a file in the root directory with a single
character name in order to make it happen. Note also that / and \ are
interchangeable here.

I'm curious what single character file you had in your root directory...
(Apparently, something named 'd'...)

P.S. I think the best overall solution is to have your 'help' option be
'/h' instead of '/?'. Yes, I know other programs can get away with '/?',
but that's not really relevant. They don't have the GAWK or TAWK runtimes
to contend with. Finally, do note that the '/' character isn't an issue.
Only the '?' is causing a problem. You don't need to switch to '-' as
Janis suggested.

--
"There are two things that are important in politics.
The first is money and I can't remember what the second one is."
- Mark Hanna -

Marc de Bourget

unread,
Nov 19, 2016, 3:17:49 PM11/19/16
to
Yes, exactly, I have a directory named c:\d
If I change it to c:\e the result is /e instead of /d :-)

In the meantime I have noticed at least for exe file compilation
there is a solution with the TAWK compiler option "-ee":
TAWK manual, page 97:
"-ee (This option is two letters). Disables filename expansion of
filenames in the ARGV array of the compiled program. The filename
patterns are left in the ARGV array, allowing you to do your own
filename expansion, if you so desire. Note that a compiled TAWK
program still processes filname patterns in each element of the
ARGV array when it comes to read and process that filename, so
this option does not actually disable the use of filename patterns
in the compiled program, it just makes the original filename patterns
show up in the ARGV array. As usual, if your program changes any
element of the ARGV array before TAWK processes that element as a
filename, TAWK processes the changed ARGV element rather than the
original."

There is also an "-eo" option which seems useful: Manual, page 98:
"-eo (This option is two letters). Suppresses default command line
processing of options by the compiled TAWK program. Specifically,
the compiled TAWK program will ignore any -F, -v, -w or — options
in its command line. The -eo and -ee options can be combined, for
example: -eoe"

So, at least for TAWK there aren't any problems if I compile
specialtasktest.awk to a native Windows exe file:
# specialtasktest.awk
BEGIN {
  print ARGV[1]
}

>awkcw -xe -ee specialtasktest.awk
or
>awkcw -xe -eo -ee specialtasktest.awk
=> specialtasktest.exe

>specialtasktest.exe /?
=> /?

Janis Papanagnou

unread,
Nov 19, 2016, 3:49:50 PM11/19/16
to
On 19.11.2016 20:45, Kenny McCormack wrote:
> In article <d694b725-6602-4d54...@googlegroups.com>,
> Marc de Bourget <marcde...@gmail.com> wrote:
>> Le samedi 19 novembre 2016 19:47:25 UTC+1, Janis Papanagnou a écrit :
>>>
>>> What shell are you using on Windows? It might help to escape
>
> But I think Janis has actually missed a subtlety. Even if you are not using
> a Unix-like shell, the real problem is that, under Windows, both GAWK and
> TAWK do their own filename expansion (to make up for the fact that the
> defective shells shipped with Windows don't). The result is that you get
> filename expansion even if you don't want it.

I am aware of that; that's why I asked what shell the OP uses as my very
first question. Of course escapes or quotes won't help in those "DOS shells",
but they do help if using environments with other non-MS shells that have
been developed for Windows (you named one of them).

> Only the '?' is causing a problem. You don't need to switch to '-' as
> Janis suggested.

You don't need to switch it to '-' but it's an option to do so, isn't it?
Since with '/?' the file expansion will generally pop in if - as in the OP's
case - there's a file /d beneath the root directory - not an uncommon case,
I'd say. And would '-?' be expanded on Windows if there's no file match? (I
have no Windows running at the moment, but I'd be surprised.) I'd expect
that /? would match any single letter file under the current root directory,
while -? would only match in the rare case that you have a two-letter file
starting with a '-' in your current working directory. (Or am I mistaken?
Please CMIIW.)

Janis

Marc de Bourget

unread,
Nov 20, 2016, 6:22:12 AM11/20/16
to
With gawk -f specialtasktest.awk -? I have encountered 4 strange cases:
1. A directory named: "-" (of course without double quotes):
=> Output always: -
2. A directory named "--": Output: Nothing
3. A directory named "-e" (with existence of directory "--"):
=> Output: -e
4. A directory named "-e" (without existence of directory "--"):
>gawk -f specialtasktest.awk -?
=> output:
gawk: option requires an argument -- e
Usage: gawk [POSIX or GNU style options] ...
...

This is funny, isn't it?

Ed Morton

unread,
Nov 20, 2016, 9:49:46 AM11/20/16
to
It's your OS that's doing that, not awk, so this question is OT here and you're
more likely to get a good answer in a Windows NG.

Ed.

Manuel Collado

unread,
Nov 20, 2016, 2:30:30 PM11/20/16
to
You can try mawk. It seems to not expand /? nor /*.

Regards.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

Marc de Bourget

unread,
Nov 20, 2016, 3:07:54 PM11/20/16
to
Yes I know. I use MAWK whenever I can but most times its
features are simply too Spartan so I use most often TAWK.
The TAWK compiler option mentioned above solves the issue.

Kenny McCormack

unread,
Nov 20, 2016, 3:43:15 PM11/20/16
to
In article <f67599d1-0c8a-4b6b...@googlegroups.com>,
Marc de Bourget <marcde...@gmail.com> wrote:
...
>Yes I know. I use MAWK whenever I can but most times its
>features are simply too Spartan so I use most often TAWK.
>The TAWK compiler option mentioned above solves the issue.

I think this is the right way to go. As I've said many times, there's no
good reason to run GAWK under Windows if one can use TAWK. I don't
understand why you are even worrying about GAWK. Note that I *could*
understand someone asking this question and wanting a sensible answer *if*
that person did not have TAWK. But we know that you do, so...

(P.S.)
This comment also applies to your next question about fseek/ftell on GAWK.
Incidentally, the answer to that question is "Not likely. I don't see how
that would be considered in the remit of the GAWK developers."

--
Modern Conservative: Someone who can take time out from demanding more
flag burning laws, more abortion laws, more drug laws, more obscenity
laws, and more police authority to make warrantless arrests to remind
us that we need to "get the government off our backs".

Kenny McCormack

unread,
Nov 20, 2016, 3:50:35 PM11/20/16
to
In article <o0sd57$9l6$1...@dont-email.me>,
Ed Morton <morto...@gmail.com> wrote:
...
>it's your OS that's doing that, not awk, so this question is OT here and
>you're more likely to get a good answer in a Windows NG.

Not true.

In fact, this question has nothing to do with Windows (per se) or the
Windows shell. As I indicated in earlier posts, the user's shell is
irrelevant.

Rather, the problem has to do with functionalities of (one or two,
depedning on how you look at it) specific implementation(s) of GAWK for the
Windows platform. But the issue is with those implementations, not with
anything in Windows per se.

In comp.lang.c, they consider implementations off-topic. But we're not
like that here.

--
If it seems like I'm not responding to some moronic criticism that you've
posted in response to one of my posts, be aware that I do not debate with idiots.

It doesn't mean you've won anything...
0 new messages