Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

C is quirky, flawed, and an enormous success -- Dennis M. Ritchie


programming / comp.lang.tcl / Why do I need to "join" it?

SubjectAuthor
* Why do I need to "join" it?clt.to.davebr
`- Re: Why do I need to "join" it?saitology9

1
Subject: Why do I need to "join" it?
From: clt.to.d...@dfgh.net
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 15 May 2022 06:00 UTC
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: clt.to.d...@dfgh.net
Newsgroups: comp.lang.tcl
Subject: Why do I need to "join" it?
Date: Sun, 15 May 22 06:00:58 GMT
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <0411652594458@dlp>
Injection-Info: reader02.eternal-september.org; posting-host="2edafd1cd8110f175c66996768bdd6f6";
logging-data="4917"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/TX/Gt7QlDjDcZmOcPcXbb"
Cancel-Lock: sha1:8tXboSVEKHv2B/jJXcOYRjZ1L34=
View all headers
From: gazelle@shell.xmission.com (Kenny McCormack)
Date: Sat May 14 11:27:23 GMT 2022
Subject: Why do I need to "join" it?


is a filename (which must exist).  The program starts with:

   #!/usr/bin/expect --
   if {$argc != 1 || ![file exists $argv]} { puts "Error";exit 1 }

This works fine as long as the filename is "normal" - i.e., doesn't contain
any spaces (and/or perhaps other weird characters).  However, if it has a
space, then the "file exists" check fails, because the actual value of
$argv is something like:

   {/path/to/file name with spaces}


I'm confused, when I remove argc!=1 test in the if statement, the file exists part seems to work for file names with or without spaces, as long as spaces are separated by non-space characters. If two or more spaces are next to each other, it will fail (or use the wrong file if there is one where all runs of multiple spaces in the name are replaced by single spaces).

As noted in other replies above it is tacky, but it does work (for files with no runs of multiple spaces in their names)

If you want to handle really weird file names, enclose any odd file name in quotes on the command line:

   expect-script "/path/to/file name with   spaces"

This ends up wrapping the file name in braces if it includes any spaces when $argv is used as the file name. Because expect (TCL) knows the "file exists" command wants a string, it converts the list in argv to a string by separating each list entry with a single space, and encloses any entries with spaces in braces {}. This is done so TCL can regenerate the original list from the string (not something we need or want here).

However saving the first (and only) list entry in argv back to argv in the script (as below) solves that problem, because argv now contains only the first entry in the list (which is a string, not a list so TCL can pass it directly to the file exists command).

    #!/usr/bin/expect --
    set argv [lindex $argv 0]
    if {$argc != 1 || ![file exists $argv]} { puts "Error";exit 1 }


Dave B






Subject: Re: Why do I need to "join" it?
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Organization: Aioe.org NNTP Server
Date: Sun, 15 May 2022 17:34 UTC
References: 1
Path: i2pn2.org!i2pn.org!aioe.org!a5rWVvs5S5ZXUwkNcVnRMw.user.46.165.242.91.POSTED!not-for-mail
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Subject: Re: Why do I need to "join" it?
Date: Sun, 15 May 2022 13:34:56 -0400
Organization: Aioe.org NNTP Server
Message-ID: <t5rdk9$18rr$1@gioia.aioe.org>
References: <0411652594458@dlp>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="41851"; posting-host="a5rWVvs5S5ZXUwkNcVnRMw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
View all headers
On 5/15/22 2:00 AM, clt.to.davebr@dfgh.net wrote:

I'm confused, when I remove argc!=1 test in the if statement, the file exists part seems to work for file names with or without spaces, as long as spaces are separated by non-space characters. If two or more spaces are next to each other, it will fail (or use the wrong file if there is one where all runs of multiple spaces in the name are replaced by single spaces).


Hello,

The single vs multiple space thing is just the standard way the command line processors work. They ignore spaces between arguments and pass them to the target program/script separated with single spaces.  By the way, this is also how Tcl's own [list] command works.  For example, "[list a b]" is identical to "[list a          b]".  The intermediate spaces have no meaning.  If you want to preserve multiple spaces in an argument, you will need to take special care by using whatever the escape mechanism is for the command line processor: single quotes, double quotes, escape characters, etc.

The reason the original code seems to work for simple file names is Tcl's stringification of single and simple element lists. For example, the string version of scalar "a" and list "[list a]" are identical. However, if the scalar value has spaces or special characters in them, or when the list contains multiple elements, the stringification gives different results in order to reflect the original form.


As noted in other replies above it is tacky, but it does work (for files with no runs of multiple spaces in their names)

If you want to handle really weird file names, enclose any odd file name in quotes on the command line:

    expect-script "/path/to/file name with   spaces"


This is the proper way to (always) distinguish arguments in a command line. Some shells will do this for you automatically through auto-completion when the need arises, but otherwise, most people just ignore the convention because their command invocations usually contain simple scalar values only.




1
rocksolid light 0.7.2
clearneti2ptor