[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-smalltalk] How to access environment variables ?
From: |
Paolo Bonzini |
Subject: |
Re: [Help-smalltalk] How to access environment variables ? |
Date: |
Mon, 28 Mar 2011 10:14:50 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.7 |
On 03/28/2011 12:19 AM, Mehul Sanghvi wrote:
st> Smalltalk getenv: 'SHELL'
'/bin/bash'
st> Smalltalk environ
CPtr(16rBFFFF6BC)
Actually, I was wrong. The correct definition will use one of these
cCall pragmas:
<cCall: 'environ' returning: #(#ptr #char) args: #()>
<cCall: 'environ' returning: #{CString} args: #()>
This is because a CObject always adds implicitly a "*" to the C type. A
CInt is an int *, a #(#ptr #int) is an int **. Similarly:
- a #(#ptr #char) is a char **. This is correct;
- in C, a String is a char *. So, a CString is also a char **, and a
#(#ptr #string) would have been a char ***. This is why it was wrong.
So you have two ways to express a char **. The two are almost
equivalent, but are used in a slightly different way.
Let's start from #(#ptr #char). Here, "env" is a pointer to the first
string, and "env value" will be a CChar object (i.e. a char *, like
"*env" in C):
st> env
CPtr(16r1142FD0)
st> env value
CChar(16r7FFF2069F44F)
It will pointing to the first character in the string. You can see it
with "env value value", which is like **env in C:
st> env value value
$O
So you need to convert the null-terminated C string manually to a
Smalltalk String object:
st> String fromCData: env value
'ORBIT_SOCKETDIR=/tmp/orbit-pbonzini'
If you want to obtain a Dictionary of environment variables, you have this:
env := Smalltalk environ.
envDict := Dictionary new.
[ (envString := env value) isNil ] whileFalse: [
match := (String fromCData: envString) =~ '(.*?)=(.*)'.
envDict at: (match at: 1) put: (match at: 2).
env incr ]
With #{CString}, instead, GNU Smalltalk will do some of the work for
you. However, to avoid falling into the mistake of thinking it's magic,
you should first understand the above well.
With that change, you'll get
st> s := Smalltalk environ.
CString(16r1142FD0)
This is the same pointer to the first environment variable; note the
address is the same. However, when you do "env value" GNU Smalltalk
will automatically fetch the whole null-terminated string:
st> env value
'ORBIT_SOCKETDIR=/tmp/orbit-pbonzini'
Then, to place the variables in a dictionary you need a small change to
the above code:
env := Smalltalk environ.
envDict := Dictionary new.
[ (envString := env value) isNil ] whileFalse: [
match := envString =~ '(.*?)=(.*)'.
envDict at: (match at: 1) put: (match at: 2).
env incr ]
Regarding the rest of your message,
st> Smalltalk environ:
st>
I am guessing some sort of pointer issue either in cint.c or CFuncs.st ?
No, there's no guessing required. You simply haven't completed your
statement, so gst is waiting for you to type the end.
Honest question, how much do you know Smalltalk? I think you have a
bigger problem than accessing environment variables.
Paolo
- Re: [Help-smalltalk] How to access environment variables ?, (continued)
- Re: [Help-smalltalk] How to access environment variables ?, Paolo Bonzini, 2011/03/21
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/25
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/25
- Re: [Help-smalltalk] How to access environment variables ?, Paolo Bonzini, 2011/03/26
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/26
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/26
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/26
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/26
- Re: [Help-smalltalk] How to access environment variables ?, Paolo Bonzini, 2011/03/27
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/27
- Re: [Help-smalltalk] How to access environment variables ?,
Paolo Bonzini <=
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/28
- Re: [Help-smalltalk] How to access environment variables ?, Paolo Bonzini, 2011/03/28
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/28
- Re: [Help-smalltalk] How to access environment variables ?, Mehul Sanghvi, 2011/03/28
- Re: [Help-smalltalk] How to access environment variables ?, Paolo Bonzini, 2011/03/28