How to access environment variables ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
44 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

kaveman
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

kaveman
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
In reply to this post by kaveman
Krishna said the following on 02/10/2009 09:41 AM:

> On Tue, Feb 10, 2009 at 8:03 PM, Paolo Bonzini <[hidden email]> wrote:
>> Krishna wrote:
>>> On Tue, Feb 10, 2009 at 7:41 PM, Paolo Bonzini <[hidden email]> wrote:
>>>>> There are extra shell variables showing up like PATH, BASH, TERM, etc.
>>>>> which do not show up in a CGI environment using any other language that
>>>>> I've used to write a similar CGI program (Perl, PHP, Lisp, Scheme, C,
>>>>> Tcl).  All of them print out the same variables as the output of the
>>>>> Perl script.  What we seem to  be doing in Smalltalk is forking/execing
>>>>> a shell and and then getting the environment variables so that we end up
>>>>> with variables from a hybrid environment of CGI and shell.
>>> why not call getenv(3) using the C interface?
>> He wants *all* the variables, not just one.
>>
> ha! ok. is it possible to build a custom gst with int main(int argc,
> char** argv, char** envp) and slurp the environment from there?
>
> another thing: can't you get away with a known set of environment
> variables in a CGI script?
>


If I am debugging a CGI script, I may not always know what I am looking for or
need to look at.  The easiest, time saving method would be to be able to have
something that just shows me the entire set of CGI environment variables and
their values.  Plus, at least to me, it seems a logical thing to be able to do
i.e. the ability to snarf all the environment variables in an optimal fashion.

I personally want to try and use Smalltalk, Lisp, Scheme as my scripting
languages for CGI.  If I can start using them on a sort of regular basis, maybe
I can convince other people to do the same.  Initial thought was a very simple
web based accounting package, but I might look at other things more closely
related to my background in networks.

All of that is a bit off into the future, right now I am just trying to learn
Smalltalk and Lisp and Scheme.


cheers,

     mehul


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
In reply to this post by kaveman
Krishna said the following on 02/10/2009 09:50 AM:

> On Tue, Feb 10, 2009 at 8:11 PM, Krishna <[hidden email]> wrote:
>> On Tue, Feb 10, 2009 at 8:03 PM, Paolo Bonzini <[hidden email]> wrote:
>>> Krishna wrote:
>>>> On Tue, Feb 10, 2009 at 7:41 PM, Paolo Bonzini <[hidden email]> wrote:
>>>>>> There are extra shell variables showing up like PATH, BASH, TERM, etc.
>>>>>> which do not show up in a CGI environment using any other language that
>>>>>> I've used to write a similar CGI program (Perl, PHP, Lisp, Scheme, C,
>>>>>> Tcl).  All of them print out the same variables as the output of the
>>>>>> Perl script.  What we seem to  be doing in Smalltalk is forking/execing
>>>>>> a shell and and then getting the environment variables so that we end up
>>>>>> with variables from a hybrid environment of CGI and shell.
>>>> why not call getenv(3) using the C interface?
>>> He wants *all* the variables, not just one.
>>>
>> ha! ok. is it possible to build a custom gst with int main(int argc,
>> char** argv, char** envp) and slurp the environment from there?
>>
>
> unistd.h defines char** environ. exporting that to smalltalk should
> solve the problem right?
>
>


That would work with GNU C library since it is defined as follows:

/* NULL-terminated array of "NAME=VALUE" environment variables.  */
extern char **__environ;
#ifdef __USE_GNU
extern char **environ;
#endif

I'm not sure how it is defined on other Unix systems.  On Solaris 9 it doesn't
exist in /usr/include/unistd.h:

Solaris-9% grep environ /usr/include/unistd.h
/* large file compilation environment setup */
/* In the LP64 compilation environment, the APIs are already large file */


So although I could probably make use of it, I'd break portability of GST along
with what ever I am working on.

This is my quick and dirty assessment of it.  I have not looked at GST code
ever yet so far.  This is based on just looking at the include files on Linux
and Solaris 9.  I could be interpreting things incorrectly.


cheers,

     mehul



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2

> That would work with GNU C library since it is defined as follows:
>
> /* NULL-terminated array of "NAME=VALUE" environment variables.  */
> extern char **__environ;
> #ifdef __USE_GNU
> extern char **environ;
> #endif
>
> I'm not sure how it is defined on other Unix systems.  On Solaris 9 it
> doesn't exist in /usr/include/unistd.h:
>
> Solaris-9% grep environ /usr/include/unistd.h
> /* large file compilation environment setup */
> /* In the LP64 compilation environment, the APIs are already large file */

environ is found more or less everywhere:

0) in most systems it is in unistd.h

1) in some systems it is in stdlib.h

2) of all others, Apple systems do not have it but you can use

   #include <crt_externs.h>
   #define environ (*_NSGetEnviron ())

3) otherwise you can define it as

   extern char **environ;

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
In reply to this post by Paolo Bonzini-2
On Tue, Feb 10, 2009 at 09:11, Paolo Bonzini <[hidden email]> wrote:

>
>> There are extra shell variables showing up like PATH, BASH, TERM, etc.
>> which do not show up in a CGI environment using any other language that
>> I've used to write a similar CGI program (Perl, PHP, Lisp, Scheme, C,
>> Tcl).  All of them print out the same variables as the output of the
>> Perl script.  What we seem to  be doing in Smalltalk is forking/execing
>> a shell and and then getting the environment variables so that we end up
>> with variables from a hybrid environment of CGI and shell.
>
> Yes, you're right that's suboptimal.
>
> What is really needed is a package covering POSIX stuff, from
> environment to process creation, in a nice object-oriented way.  Ideally
> you could do something like
>
>   redirections := SpawnParameters new
>       stdin: (FileStream open: '/dev/null' mode: FileStream write);
>       stdout: (FileStream newPipe: FileStream write);
>       stderr: FileStream stderr;
>       workingDirectory: '/'.
>   process := redirections spawn: 'ls -lR'.
>   s := [ process stdout readSide contents ] fork.
>   process wait
>
> Paolo
>


I was wondering if this sort of POSIX support has been added to GST now ?

This was from a couple of years ago when I was trying to get at all the
environment variables in a CGI environment.


cheers,

       mehul

--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2
> I was wondering if this sort of POSIX support has been added to
> GST now ?

Nope, not yet.

http://smalltalk.gnu.org/news/gnu-smalltalk-3-2 is a rough list of the
new functionality that went in since that message.

After 3.2, we went through a relatively long stabilization period with
limited new features, most of them are listed here:
http://smalltalk.gnu.org/news/gnu-smalltalk-3-2-3.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Mon, Mar 21, 2011 at 03:40, Paolo Bonzini <[hidden email]> wrote:

>> I was wondering if this sort of POSIX support has been added to
>> GST now ?
>
> Nope, not yet.
>
> http://smalltalk.gnu.org/news/gnu-smalltalk-3-2 is a rough list of the
> new functionality that went in since that message.
>
> After 3.2, we went through a relatively long stabilization period with
> limited new features, most of them are listed here:
> http://smalltalk.gnu.org/news/gnu-smalltalk-3-2-3.
>
> Paolo
>


OK so once I  modify kernel/CFuncs.st and I believe I need to also
modify libgst/cint.c what else do I need to do in order to get this to work ?


In kernel/CFuncs.st

    environ: aString [
        <category: 'c call-outs'>
        <cCall: 'environ' returning: #(#ptr #string) args: #()>
       
    ]



and in libgst/cint.c

  _gst_define_cfunc ("getenv", getenv);
  _gst_define_cfunc ("environ", environ);



cheers,

      mehul


--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Fri, Mar 25, 2011 at 21:12, Mehul Sanghvi <[hidden email]> wrote:

> On Mon, Mar 21, 2011 at 03:40, Paolo Bonzini <[hidden email]> wrote:
>>> I was wondering if this sort of POSIX support has been added to
>>> GST now ?
>>
>> Nope, not yet.
>>
>> http://smalltalk.gnu.org/news/gnu-smalltalk-3-2 is a rough list of the
>> new functionality that went in since that message.
>>
>> After 3.2, we went through a relatively long stabilization period with
>> limited new features, most of them are listed here:
>> http://smalltalk.gnu.org/news/gnu-smalltalk-3-2-3.
>>
>> Paolo
>>
>
>
> OK so once I  modify kernel/CFuncs.st and I believe I need to also
> modify libgst/cint.c what else do I need to do in order to get this to work ?
>
>
> In kernel/CFuncs.st
>
>    environ: aString [
>        <category: 'c call-outs'>
>        <cCall: 'environ' returning: #(#ptr #string) args: #()>
>
>    ]
>
>
>
> and in libgst/cint.c
>
>  _gst_define_cfunc ("getenv", getenv);
>  _gst_define_cfunc ("environ", environ);
>
>
>



When I do:

    env := Smalltalk environ .

I get the following:

Object: SystemDictionary new: 512 "<0x40290818>" error: did not
understand #environ
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
SystemDictionary(Object)>>doesNotUnderstand: #environ (SysExcept.st:1407)
SystemDictionary(BindingDictionary)>>doesNotUnderstand: #environ
(BindingDict.st:181)
UndefinedObject>>executeStatements (test-cgi.st:13)


I'm sure there is something wrong with my Smalltalk code
as well as probably missing something in what I did to add environ.

One thing is that environ is a variable, not a function, maybe they
are handled differently ?




cheers,

      mehul

--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2
In reply to this post by Mehul Sanghvi-2
On 03/26/2011 02:12 AM, Mehul Sanghvi wrote:

> OK so once I  modify kernel/CFuncs.st and I believe I need to also
> modify libgst/cint.c what else do I need to do in order to get this to work ?
>
>
> In kernel/CFuncs.st
>
>      environ: aString [
> <category: 'c call-outs'>
> <cCall: 'environ' returning: #(#ptr #string) args: #()>
>
>      ]

Where did you add this?

> and in libgst/cint.c
>
>    _gst_define_cfunc ("getenv", getenv);
>    _gst_define_cfunc ("environ", environ);

You should do

static char **get_environ(void)
{
   return environ;
}

...

   _gst_define_cfunc ("get_environ", get_environ);

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Sat, Mar 26, 2011 at 03:10, Paolo Bonzini <[hidden email]> wrote:

> On 03/26/2011 02:12 AM, Mehul Sanghvi wrote:
>>
>> OK so once I  modify kernel/CFuncs.st and I believe I need to also
>> modify libgst/cint.c what else do I need to do in order to get this to
>> work ?
>>
>>
>> In kernel/CFuncs.st
>>
>>     environ: aString [
>>        <category: 'c call-outs'>
>>        <cCall: 'environ' returning: #(#ptr #string) args: #()>
>>
>>     ]
>
> Where did you add this?
>

I did that right below where there was a similar
bit of code for getenv.  I just copied that piece of
code and adjusted it based on the information
in this mailing list thread.


>> and in libgst/cint.c
>>
>>   _gst_define_cfunc ("getenv", getenv);
>>   _gst_define_cfunc ("environ", environ);
>
> You should do
>
> static char **get_environ(void)
> {
>  return environ;
> }
>
> ...
>
>  _gst_define_cfunc ("get_environ", get_environ);
>
> Paolo
>



--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Sat, Mar 26, 2011 at 10:04, Mehul Sanghvi <[hidden email]> wrote:

> On Sat, Mar 26, 2011 at 03:10, Paolo Bonzini <[hidden email]> wrote:
>> On 03/26/2011 02:12 AM, Mehul Sanghvi wrote:
>>>
>>> OK so once I  modify kernel/CFuncs.st and I believe I need to also
>>> modify libgst/cint.c what else do I need to do in order to get this to
>>> work ?
>>>
>>>
>>> In kernel/CFuncs.st
>>>
>>>     environ: aString [
>>>        <category: 'c call-outs'>
>>>        <cCall: 'environ' returning: #(#ptr #string) args: #()>
>>>
>>>     ]
>>
>> Where did you add this?
>>
>
> I did that right below where there was a similar
> bit of code for getenv.  I just copied that piece of
> code and adjusted it based on the information
> in this mailing list thread.
>
>
>>> and in libgst/cint.c
>>>
>>>   _gst_define_cfunc ("getenv", getenv);
>>>   _gst_define_cfunc ("environ", environ);
>>
>> You should do
>>
>> static char **get_environ(void)
>> {
>>  return environ;
>> }
>>
>> ...
>>
>>  _gst_define_cfunc ("get_environ", get_environ);
>>
>> Paolo
>>


Below is the code I am running, the result I get, and the diff
of what I've changed.  I'm pretty sure my code in the
smalltalk script is wrong


------ BEGIN CODE -------
#!/usr/local/bin/gst -f

Transcript showCr: 'Content-type: text/html'; nl .
'' displayNl .
'<HTML>' displayNl .
'<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>' displayNl .
'<BODY>' displayNl .
'<H2>Yo! Smalltalk,  Whats up ? </H2>' displayNl .

Transcript nl ; showCr: '==== All Env Vars below ====== <br>'; nl.
"pipe := FileStream popen: ('set', logFile) dir: FileStream read .
Transcript showCr: pipe contents ."
env := Smalltalk environ .

"Transcript nl; showCr:  env ; nl ."

'</BODY>' displayNl .
'</HTML>' displayNl .
--------- END CODE -------

--------- BEGIN OUTPUT ----
% gst test-cgi.st
Content-type: text/html


<HTML>
<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>
<BODY>
<H2>Yo! Smalltalk,  Whats up ? </H2>

==== All Env Vars below ====== <br>

Object: SystemDictionary new: 512 "<0x40290818>" error: did not
understand #environ
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
SystemDictionary(Object)>>doesNotUnderstand: #environ (SysExcept.st:1407)
SystemDictionary(BindingDictionary)>>doesNotUnderstand: #environ
(BindingDict.st:181)
UndefinedObject>>executeStatements (test-cgi.st:13)
</BODY>
</HTML>


-------- END OUTPUT --------

------- BEGIN DIFF -------
diff --git a/kernel/CFuncs.st b/kernel/CFuncs.st
index 72b7ca2..ff2db72 100644
--- a/kernel/CFuncs.st
+++ b/kernel/CFuncs.st
@@ -127,6 +127,12 @@ SystemDictionary extend [

     ]

+    environ: aString [
+       <category: 'c call-outs'>
+       <cCall: 'environ' returning: #(#ptr #string) args: #()>
+
+    ]
+
     putenv: aString [
        <category: 'c call-outs'>
        <cCall: 'putenv' returning: #int args: #(#string)>


diff --git a/libgst/cint.c b/libgst/cint.c
index 061a829..44c5369 100644
--- a/libgst/cint.c
+++ b/libgst/cint.c
@@ -195,6 +195,7 @@ static int my_lstat (const char *name,
                     OOP out);
 #endif
 static int my_putenv (const char *str);
+static char **get_environ (void);
 static int my_chdir (const char *str);
 static int my_chown (const char *file, const char *owner, const char *group);
 static int my_symlink (const char* oldpath, const char* newpath);
@@ -392,6 +393,12 @@ my_putenv (const char *str)
   return (putenv (clone));
 }

+static char **
+get_environ (void)
+{
+  return environ;
+}
+

 int
 my_chdir (const char *dir)
@@ -592,6 +599,7 @@ _gst_init_cfuncs (void)
   /* Access to C library */
   _gst_define_cfunc ("system", system);
   _gst_define_cfunc ("getenv", getenv);
+  _gst_define_cfunc ("environ", get_environ);
   _gst_define_cfunc ("putenv", my_putenv);
   _gst_define_cfunc ("printf", printf);

------------- END DIFF ----------------
--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Sat, Mar 26, 2011 at 18:41, Mehul Sanghvi <[hidden email]> wrote:

> On Sat, Mar 26, 2011 at 10:04, Mehul Sanghvi <[hidden email]> wrote:
>> On Sat, Mar 26, 2011 at 03:10, Paolo Bonzini <[hidden email]> wrote:
>>> On 03/26/2011 02:12 AM, Mehul Sanghvi wrote:
>>>>
>>>> OK so once I  modify kernel/CFuncs.st and I believe I need to also
>>>> modify libgst/cint.c what else do I need to do in order to get this to
>>>> work ?
>>>>
>>>>
>>>> In kernel/CFuncs.st
>>>>
>>>>     environ: aString [
>>>>        <category: 'c call-outs'>
>>>>        <cCall: 'environ' returning: #(#ptr #string) args: #()>
>>>>
>>>>     ]
>>>
>>> Where did you add this?
>>>
>>
>> I did that right below where there was a similar
>> bit of code for getenv.  I just copied that piece of
>> code and adjusted it based on the information
>> in this mailing list thread.
>>
>>
>>>> and in libgst/cint.c
>>>>
>>>>   _gst_define_cfunc ("getenv", getenv);
>>>>   _gst_define_cfunc ("environ", environ);
>>>
>>> You should do
>>>
>>> static char **get_environ(void)
>>> {
>>>  return environ;
>>> }
>>>
>>> ...
>>>
>>>  _gst_define_cfunc ("get_environ", get_environ);
>>>
>>> Paolo
>>>
>
>
> Below is the code I am running, the result I get, and the diff
> of what I've changed.  I'm pretty sure my code in the
> smalltalk script is wrong
>
>
> ------ BEGIN CODE -------
> #!/usr/local/bin/gst -f
>
> Transcript showCr: 'Content-type: text/html'; nl .
> '' displayNl .
> '<HTML>' displayNl .
> '<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>' displayNl .
> '<BODY>' displayNl .
> '<H2>Yo! Smalltalk,  Whats up ? </H2>' displayNl .
>
> Transcript nl ; showCr: '==== All Env Vars below ====== <br>'; nl.
> "pipe := FileStream popen: ('set', logFile) dir: FileStream read .
> Transcript showCr: pipe contents ."
> env := Smalltalk environ .
>
> "Transcript nl; showCr:  env ; nl ."
>
> '</BODY>' displayNl .
> '</HTML>' displayNl .
> --------- END CODE -------
>
> --------- BEGIN OUTPUT ----
> % gst test-cgi.st
> Content-type: text/html
>
>
> <HTML>
> <HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>
> <BODY>
> <H2>Yo! Smalltalk,  Whats up ? </H2>
>
> ==== All Env Vars below ====== <br>
>
> Object: SystemDictionary new: 512 "<0x40290818>" error: did not
> understand #environ
> MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
> SystemDictionary(Object)>>doesNotUnderstand: #environ (SysExcept.st:1407)
> SystemDictionary(BindingDictionary)>>doesNotUnderstand: #environ
> (BindingDict.st:181)
> UndefinedObject>>executeStatements (test-cgi.st:13)
> </BODY>
> </HTML>
>
>
> -------- END OUTPUT --------
>
> ------- BEGIN DIFF -------
> diff --git a/kernel/CFuncs.st b/kernel/CFuncs.st
> index 72b7ca2..ff2db72 100644
> --- a/kernel/CFuncs.st
> +++ b/kernel/CFuncs.st
> @@ -127,6 +127,12 @@ SystemDictionary extend [
>
>     ]
>
> +    environ: aString [
> +       <category: 'c call-outs'>
> +       <cCall: 'environ' returning: #(#ptr #string) args: #()>
> +
> +    ]
> +
>     putenv: aString [
>        <category: 'c call-outs'>
>        <cCall: 'putenv' returning: #int args: #(#string)>
>
>
> diff --git a/libgst/cint.c b/libgst/cint.c
> index 061a829..44c5369 100644
> --- a/libgst/cint.c
> +++ b/libgst/cint.c
> @@ -195,6 +195,7 @@ static int my_lstat (const char *name,
>                     OOP out);
>  #endif
>  static int my_putenv (const char *str);
> +static char **get_environ (void);
>  static int my_chdir (const char *str);
>  static int my_chown (const char *file, const char *owner, const char *group);
>  static int my_symlink (const char* oldpath, const char* newpath);
> @@ -392,6 +393,12 @@ my_putenv (const char *str)
>   return (putenv (clone));
>  }
>
> +static char **
> +get_environ (void)
> +{
> +  return environ;
> +}
> +
>
>  int
>  my_chdir (const char *dir)
> @@ -592,6 +599,7 @@ _gst_init_cfuncs (void)
>   /* Access to C library */
>   _gst_define_cfunc ("system", system);
>   _gst_define_cfunc ("getenv", getenv);
> +  _gst_define_cfunc ("environ", get_environ);
>   _gst_define_cfunc ("putenv", my_putenv);
>   _gst_define_cfunc ("printf", printf);
>
> ------------- END DIFF ----------------
> --
> Mehul N. Sanghvi
> email: [hidden email]
>




OK so I changed the code, accidently discovering my mistake in the
previous code I had sent:

------ BEGIN CODE -----
#!/usr/local/bin/gst -f

Transcript showCr: 'Content-type: text/html'; nl .
'' displayNl .
'<HTML>' displayNl .
'<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>' displayNl .
'<BODY>' displayNl .
'<H2>Yo! Smalltalk,  Whats up ? </H2>' displayNl .

Transcript nl ; showCr: '==== All Env Vars below ====== <br>'; nl.
Transcript nl;  print: Smalltalk environ:  ; nl .

'</BODY>' displayNl .
'</HTML>' displayNl .

---- -END CODE -----


but now I get the following

------- BEGIN OUTPUT -----------

% gst test-cgi.st
Content-type: text/html


<HTML>
<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>
<BODY>
<H2>Yo! Smalltalk,  Whats up ? </H2>

==== All Env Vars below ====== <br>

test-cgi.st:11: expected object

------- END OUTPUT -------

I'm pretty sure I am messing up on something
pretty simple and basic.


cheers,

      mehul

--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Sat, Mar 26, 2011 at 19:41, Mehul Sanghvi <[hidden email]> wrote:

> On Sat, Mar 26, 2011 at 18:41, Mehul Sanghvi <[hidden email]> wrote:
>> On Sat, Mar 26, 2011 at 10:04, Mehul Sanghvi <[hidden email]> wrote:
>>> On Sat, Mar 26, 2011 at 03:10, Paolo Bonzini <[hidden email]> wrote:
>>>> On 03/26/2011 02:12 AM, Mehul Sanghvi wrote:
>>>>>
>>>>> OK so once I  modify kernel/CFuncs.st and I believe I need to also
>>>>> modify libgst/cint.c what else do I need to do in order to get this to
>>>>> work ?
>>>>>
>>>>>
>>>>> In kernel/CFuncs.st
>>>>>
>>>>>     environ: aString [
>>>>>        <category: 'c call-outs'>
>>>>>        <cCall: 'environ' returning: #(#ptr #string) args: #()>
>>>>>
>>>>>     ]
>>>>
>>>> Where did you add this?
>>>>
>>>
>>> I did that right below where there was a similar
>>> bit of code for getenv.  I just copied that piece of
>>> code and adjusted it based on the information
>>> in this mailing list thread.
>>>
>>>
>>>>> and in libgst/cint.c
>>>>>
>>>>>   _gst_define_cfunc ("getenv", getenv);
>>>>>   _gst_define_cfunc ("environ", environ);
>>>>
>>>> You should do
>>>>
>>>> static char **get_environ(void)
>>>> {
>>>>  return environ;
>>>> }
>>>>
>>>> ...
>>>>
>>>>  _gst_define_cfunc ("get_environ", get_environ);
>>>>
>>>> Paolo
>>>>
>>
>>
>> Below is the code I am running, the result I get, and the diff
>> of what I've changed.  I'm pretty sure my code in the
>> smalltalk script is wrong
>>
>>
>> ------ BEGIN CODE -------
>> #!/usr/local/bin/gst -f
>>
>> Transcript showCr: 'Content-type: text/html'; nl .
>> '' displayNl .
>> '<HTML>' displayNl .
>> '<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>' displayNl .
>> '<BODY>' displayNl .
>> '<H2>Yo! Smalltalk,  Whats up ? </H2>' displayNl .
>>
>> Transcript nl ; showCr: '==== All Env Vars below ====== <br>'; nl.
>> "pipe := FileStream popen: ('set', logFile) dir: FileStream read .
>> Transcript showCr: pipe contents ."
>> env := Smalltalk environ .
>>
>> "Transcript nl; showCr:  env ; nl ."
>>
>> '</BODY>' displayNl .
>> '</HTML>' displayNl .
>> --------- END CODE -------
>>
>> --------- BEGIN OUTPUT ----
>> % gst test-cgi.st
>> Content-type: text/html
>>
>>
>> <HTML>
>> <HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>
>> <BODY>
>> <H2>Yo! Smalltalk,  Whats up ? </H2>
>>
>> ==== All Env Vars below ====== <br>
>>
>> Object: SystemDictionary new: 512 "<0x40290818>" error: did not
>> understand #environ
>> MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
>> SystemDictionary(Object)>>doesNotUnderstand: #environ (SysExcept.st:1407)
>> SystemDictionary(BindingDictionary)>>doesNotUnderstand: #environ
>> (BindingDict.st:181)
>> UndefinedObject>>executeStatements (test-cgi.st:13)
>> </BODY>
>> </HTML>
>>
>>
>> -------- END OUTPUT --------
>>
>> ------- BEGIN DIFF -------
>> diff --git a/kernel/CFuncs.st b/kernel/CFuncs.st
>> index 72b7ca2..ff2db72 100644
>> --- a/kernel/CFuncs.st
>> +++ b/kernel/CFuncs.st
>> @@ -127,6 +127,12 @@ SystemDictionary extend [
>>
>>     ]
>>
>> +    environ: aString [
>> +       <category: 'c call-outs'>
>> +       <cCall: 'environ' returning: #(#ptr #string) args: #()>
>> +
>> +    ]
>> +
>>     putenv: aString [
>>        <category: 'c call-outs'>
>>        <cCall: 'putenv' returning: #int args: #(#string)>
>>
>>
>> diff --git a/libgst/cint.c b/libgst/cint.c
>> index 061a829..44c5369 100644
>> --- a/libgst/cint.c
>> +++ b/libgst/cint.c
>> @@ -195,6 +195,7 @@ static int my_lstat (const char *name,
>>                     OOP out);
>>  #endif
>>  static int my_putenv (const char *str);
>> +static char **get_environ (void);
>>  static int my_chdir (const char *str);
>>  static int my_chown (const char *file, const char *owner, const char *group);
>>  static int my_symlink (const char* oldpath, const char* newpath);
>> @@ -392,6 +393,12 @@ my_putenv (const char *str)
>>   return (putenv (clone));
>>  }
>>
>> +static char **
>> +get_environ (void)
>> +{
>> +  return environ;
>> +}
>> +
>>
>>  int
>>  my_chdir (const char *dir)
>> @@ -592,6 +599,7 @@ _gst_init_cfuncs (void)
>>   /* Access to C library */
>>   _gst_define_cfunc ("system", system);
>>   _gst_define_cfunc ("getenv", getenv);
>> +  _gst_define_cfunc ("environ", get_environ);
>>   _gst_define_cfunc ("putenv", my_putenv);
>>   _gst_define_cfunc ("printf", printf);
>>
>> ------------- END DIFF ----------------
>> --
>> Mehul N. Sanghvi
>> email: [hidden email]
>>
>
>
>
>
> OK so I changed the code, accidently discovering my mistake in the
> previous code I had sent:
>
> ------ BEGIN CODE -----
> #!/usr/local/bin/gst -f
>
> Transcript showCr: 'Content-type: text/html'; nl .
> '' displayNl .
> '<HTML>' displayNl .
> '<HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>' displayNl .
> '<BODY>' displayNl .
> '<H2>Yo! Smalltalk,  Whats up ? </H2>' displayNl .
>
> Transcript nl ; showCr: '==== All Env Vars below ====== <br>'; nl.
> Transcript nl;  print: Smalltalk environ:  ; nl .
>
> '</BODY>' displayNl .
> '</HTML>' displayNl .
>
> ---- -END CODE -----
>
>
> but now I get the following
>
> ------- BEGIN OUTPUT -----------
>
> % gst test-cgi.st
> Content-type: text/html
>
>
> <HTML>
> <HEAD><TITLE>Testing CGI/Smalltalk</TITLE></HEAD>
> <BODY>
> <H2>Yo! Smalltalk,  Whats up ? </H2>
>
> ==== All Env Vars below ====== <br>
>
> test-cgi.st:11: expected object
>
> ------- END OUTPUT -------
>
> I'm pretty sure I am messing up on something
> pretty simple and basic.
>
>
> cheers,
>
>      mehul
>
> --
> Mehul N. Sanghvi
> email: [hidden email]
>

So running gst from the command line:


% gst
GNU Smalltalk ready

st> Smalltalk getenv: 'HOME'
'/home/msanghvi'
st> Smalltalk environ:
st>


Seems like Smalltalk environ:  does not return anything.
Did I miss something in what I did ?


cheers,

      mehul

--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2
In reply to this post by Mehul Sanghvi-2
> +    environ: aString [

This should be just "environ [".

The method wasn't found because you were defining it with the wrong name.

Paolo

> +       <category: 'c call-outs'>
> +       <cCall: 'environ' returning: #(#ptr #string) args: #()>
> +
> +    ]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Sun, Mar 27, 2011 at 05:03, Paolo Bonzini <[hidden email]> wrote:

>> +    environ: aString [
>
> This should be just "environ [".
>
> The method wasn't found because you were defining it with the wrong name.
>
> Paolo
>
>> +       <category: 'c call-outs'>
>> +       <cCall: 'environ' returning: #(#ptr #string) args: #()>
>> +
>> +    ]
>


I made that change, recompiled, and I get the following:

% gst
GNU Smalltalk ready

st> Smalltalk getenv: 'SHELL'
'/bin/bash'
st> Smalltalk environ
CPtr(16rBFFFF6BC)
st> Smalltalk environ:
st>


I am guessing some sort of pointer issue either in cint.c or CFuncs.st ?


--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2
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

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Mon, Mar 28, 2011 at 04:14, Paolo Bonzini <[hidden email]> wrote:
> On 03/28/2011 12:19 AM, Mehul Sanghvi wrote:
>>

[ explanation deleted ]

That helps a lot in understanding things.  Thanks.


>
>
> 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
>



I used VisualWorks, on and off,  at a previous job a few years ago
which started me on the
Smalltalk wagon.  But even though I was able to figure things out in
VW and debug
our product, I consider myself totally new.  I plan to go through the
Computer Programming
with Gnu Smalltalk book in order to get beyond that newbie distinction
and then follow that
up with butterfly book (Smalltalk, Objects, and Design by Liu).

I am aware that I needed the '.'  is needed to terminate the
statement.  But since
I didn't need when I did things like

     gst> Smalltalk getenv: 'SHELL'
     '/bin/bash'
     gst>

I didn't think I needed it when I was doing the environ statement.
Also the fact that
it was only through this process of going back and forth with you that
I've learnt I can
do something like

     gst>  Smalltalk environ

which is different from

     gst>  Smalltalk environ:

the last one needing a '.' to make it a complete statement or an object.


cheers,

      mehul


--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Paolo Bonzini-2
On 03/28/2011 03:42 PM, Mehul Sanghvi wrote:
>       gst>  Smalltalk getenv: 'SHELL'
>       '/bin/bash'
>       gst>
>
> I didn't think I needed it when I was doing the environ statement.

You didn't need it for "Smalltalk environ" indeed.  But here,

> which is different from
>
>       gst>   Smalltalk environ:
>
> the last one needing a '.' to make it a complete statement or an object.

adding a "." would only make a syntax error, because you need an
argument after "environ:", just like you had one above "getenv:".

Tip: if you think it's complicated, you aren't understanding it. :)

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to access environment variables ?

Mehul Sanghvi-2
On Mon, Mar 28, 2011 at 09:52, Paolo Bonzini <[hidden email]> wrote:

> On 03/28/2011 03:42 PM, Mehul Sanghvi wrote:
>>
>>      gst>  Smalltalk getenv: 'SHELL'
>>      '/bin/bash'
>>      gst>
>>
>> I didn't think I needed it when I was doing the environ statement.
>
> You didn't need it for "Smalltalk environ" indeed.  But here,
>
>> which is different from
>>
>>      gst>   Smalltalk environ:
>>
>> the last one needing a '.' to make it a complete statement or an object.
>
> adding a "." would only make a syntax error, because you need an argument
> after "environ:", just like you had one above "getenv:".
>
> Tip: if you think it's complicated, you aren't understanding it. :)
>
> Paolo
>

More like not paying attention to it.  :)


--
Mehul N. Sanghvi
email: [hidden email]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
123