G:/ScriptBasic/source/commands/environ.c

Go to the documentation of this file.
00001 /* environ.c
00002 
00003 --GNU LGPL
00004 This library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Lesser General Public
00006 License as published by the Free Software Foundation; either
00007 version 2.1 of the License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00024 #include "../command.h"
00025 
00026 #ifdef __DARWIN__
00027 #define _environ environ
00028 #endif
00029 
00030 extern char **_environ;
00031 
00069 COMMAND(ENVIRON)
00070 #if NOTIMP_ENVIRON
00071 NOTIMPLEMENTED;
00072 #else
00073 
00074   NODE nItem;
00075   VARIABLE Op1;
00076 #define MAX_ENVIRV_LEN 256
00077   char *s,buffer[MAX_ENVIRV_LEN];
00078   size_t sLen;
00079   char **EnvironmentPointer;
00080   long index;
00081   char *(*EnvirFunc)(void *, char *, long );
00082   /* this is an operator and not a command, therefore we do not have our own mortal list */
00083   USE_CALLER_MORTALS;
00084 
00085   /* evaluate the parameter */
00086   nItem = PARAMETERLIST;
00087   Op1 = _EVALUATEEXPRESSION(CAR(nItem));
00088   ASSERTOKE;
00089 
00090   if( memory_IsUndef(Op1) ){
00091     RESULT = NULL;
00092     RETURN;
00093     }
00094 
00095   EnvirFunc = pEo->fpEnvirFunction;
00096   if( TYPE(Op1) == VTYPE_LONG ){
00097     index = LONGVALUE(Op1);
00098     if( EnvirFunc ){
00099       s = EnvirFunc(pEo->pEmbedder,NULL,index);
00100       }else{
00101       EnvironmentPointer = _environ;
00102       if( index < 0 ){
00103         RESULT = NULL;
00104         RETURN;
00105         }
00106       /* we have to walk one by one, because index may be too large and _environ is terminated with NULL and
00107          we do not have the length */
00108       while( index && *EnvironmentPointer ){
00109         index--;
00110         EnvironmentPointer++;
00111         }
00112       s = *EnvironmentPointer;
00113       }
00114     }else{
00115 
00116     Op1 = CONVERT2STRING(Op1);
00117     if( (sLen=STRLEN(Op1)) > MAX_ENVIRV_LEN -1 )sLen = MAX_ENVIRV_LEN-1;
00118     memcpy(buffer,STRINGVALUE(Op1),sLen);
00119     buffer[sLen] = (char)0;
00120     if( EnvirFunc )
00121       s = EnvirFunc(pEo->pEmbedder,buffer,0);
00122     else
00123       s = getenv( buffer );
00124     }
00125 
00126   if( s == NULL ){
00127     RESULT = NULL;
00128     RETURN;
00129     }
00130   sLen=strlen(s);
00131   RESULT = NEWMORTALSTRING(sLen);
00132   ASSERTNULL(RESULT)
00133   memcpy(STRINGVALUE(RESULT),s,sLen);
00134 
00135 #endif
00136 END
00137 
00151 COMMAND(COMMANDF)
00152 #if NOTIMP_COMMANDF
00153 NOTIMPLEMENTED;
00154 #else
00155 
00156   int q;
00157 
00158   /* this is an operator and not a command, therefore we do not have our own mortal list */
00159   USE_CALLER_MORTALS;
00160 
00161   if( pEo->CmdLineArgument == NULL ){
00162     RESULT = NULL;
00163     RETURN;
00164     }
00165 
00166   RESULT = NEWMORTALSTRING(q=strlen(pEo->CmdLineArgument));
00167   ASSERTNULL(RESULT)
00168   memcpy(STRINGVALUE(RESULT),pEo->CmdLineArgument,q);
00169 
00170 #endif
00171 END

Generated on Sun Mar 12 23:56:26 2006 for ScriptBasic by  doxygen 1.4.6-NO