Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
afni_environ.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008
00009
00010
00011
00012
00013
00014
00015 char * AFNI_suck_file( char *fname )
00016 {
00017 int len , fd , ii ;
00018 char *buf ;
00019
00020 if( fname == NULL || fname[0] == '\0' ) return NULL ;
00021
00022 len = THD_filesize( fname ) ;
00023 if( len <= 0 ) return NULL ;
00024
00025 fd = open( fname , O_RDONLY ) ;
00026 if( fd < 0 ) return NULL ;
00027
00028 buf = (char *) malloc( sizeof(char) * (len+4) ) ;
00029 ii = read( fd , buf , len ) ;
00030 close( fd ) ;
00031 if( ii <= 0 ){ free(buf) ; return NULL; }
00032
00033 buf[len] = '\0' ;
00034 return buf ;
00035 }
00036
00037
00038
00039
00040
00041
00042 #define ISTARRED(s) ( (s)[0]=='*' && (s)[1]=='*' && (s)[2]=='*' )
00043
00044 #define EOLSKIP \
00045 do{ for( ; fptr[0] != '\n' && fptr[0] != '\0' ; fptr++ ) ; \
00046 if( fptr[0] == '\0' ) goto done ; \
00047 fptr++ ; } while(0)
00048
00049 #define GETSSS \
00050 do{ int nu=0,qq; \
00051 if( fptr-fbuf >= nbuf || fptr[0] == '\0' ) goto done ; \
00052 str[0]='\0'; qq=sscanf(fptr,"%127s%n",str,&nu); nused+=nu;fptr+=nu; \
00053 if( str[0]=='\0' || qq==0 || nu==0 ) goto done ; \
00054 } while(0)
00055
00056 #define GETSTR \
00057 do{ GETSSS ; \
00058 while(str[0]=='!' || (str[0]=='/' && str[1]=='/') || \
00059 (str[0]=='#' && str[1]=='\0') ){EOLSKIP; GETSSS;} \
00060 } while(0)
00061
00062 #define GETEQN \
00063 do{ GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00064 strcpy(left,str) ; \
00065 GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00066 strcpy(middle,str) ; \
00067 GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00068 strcpy(right,str) ; } while(0)
00069
00070 #define NSBUF 256
00071
00072 static int afni_env_done = 0 ;
00073
00074 void AFNI_mark_environ_done(void){ afni_env_done = 1 ; return ; }
00075
00076 char * my_getenv( char *ename )
00077 {
00078 if( !afni_env_done ){
00079 char * sysenv = getenv("AFNI_SYSTEM_AFNIRC") ;
00080 if( sysenv != NULL ) AFNI_process_environ(sysenv) ;
00081 AFNI_process_environ(NULL) ;
00082 }
00083 return getenv( ename ) ;
00084 }
00085
00086 void AFNI_process_environ( char *fname )
00087 {
00088 int nbuf , nused , ii ;
00089 char *fbuf , *fptr ;
00090 char str[NSBUF] , left[NSBUF] , middle[NSBUF] , right[NSBUF] ;
00091
00092 ENTRY("AFNI_process_environ") ;
00093 if( fname != NULL ){
00094 strcpy(str,fname) ;
00095 } else {
00096 char *home ;
00097 if( afni_env_done ) EXRETURN ;
00098 home = getenv("HOME") ;
00099 if( home != NULL ){ strcpy(str,home) ; strcat(str,"/.afnirc") ; }
00100 else { strcpy(str,".afnirc") ; }
00101 afni_env_done = 1 ;
00102 }
00103
00104 fbuf = AFNI_suck_file( str ) ; if( fbuf == NULL ) EXRETURN ;
00105 nbuf = strlen(fbuf) ; if( nbuf == 0 ) EXRETURN ;
00106
00107 fptr = fbuf ; nused = 0 ;
00108
00109
00110
00111 str[0] = '\0' ;
00112
00113 while( nused < nbuf ){
00114
00115
00116
00117
00118 SkipSection: while( ! ISTARRED(str) ){ GETSTR; }
00119
00120
00121
00122 if( strcmp(str,"***END") == 0 ) break ;
00123
00124 if( strcmp(str,"***ENVIRONMENT") != 0 ){ GETSTR ; goto SkipSection ; }
00125
00126
00127
00128
00129 if( strcmp(str,"***ENVIRONMENT") == 0 ){
00130 char *enveqn ; int nl , nr ;
00131
00132 while(1){
00133 GETEQN ;
00134
00135 if( !THD_filename_pure(left) ) continue ;
00136
00137 nl = strlen(left) ; nr = strlen(right) ;
00138 enveqn = (char *) malloc(nl+nr+4) ;
00139 strcpy(enveqn,left) ; strcat(enveqn,"=") ; strcat(enveqn,right) ;
00140 putenv(enveqn) ;
00141 }
00142
00143 continue ;
00144 }
00145
00146 }
00147
00148 done:
00149 free(fbuf) ; EXRETURN ;
00150 }
00151
00152
00153
00154 int AFNI_yesenv( char *ename )
00155 {
00156 char *ept ;
00157 if( ename == NULL ) return 0 ;
00158 ept = my_getenv(ename) ;
00159 return YESSISH(ept) ;
00160 }
00161
00162
00163
00164 int AFNI_noenv( char *ename )
00165 {
00166 char *ept ;
00167 if( ename == NULL ) return 0 ;
00168 ept = my_getenv(ename) ;
00169 return NOISH(ept) ;
00170 }
00171
00172
00173
00174 double AFNI_numenv( char *ename )
00175 {
00176 char *ept,*ccc ; double val ;
00177 if( ename == NULL ) return 0.0l ;
00178 ept = my_getenv(ename) ;
00179 if( ept == NULL ) return 0.0l ;
00180 val = strtod(ept,&ccc) ;
00181 if( *ccc == 'k' || *ccc == 'K' ) val *= 1024.0l ;
00182 else if( *ccc == 'm' || *ccc == 'M' ) val *= 1024.0l*1024.0l ;
00183 else if( *ccc == 'g' || *ccc == 'G' ) val *= 1024.0l*1024.0l*1024.0l ;
00184 return val ;
00185 }
00186
00187
00188
00189
00190 int AFNI_setenv( char *cmd )
00191 {
00192 char nam[256]="\0" , val[1024]="\0" , eqn[1280] , *eee ;
00193
00194 if( cmd == NULL || strlen(cmd) < 3 ) return(-1) ;
00195
00196 sscanf( cmd , "%255s %1023s" , nam , val ) ;
00197 if( nam[0] == '\0' || val[0] == '\0' && strchr(cmd,'=') != NULL ){
00198 char *ccc = strdup(cmd) ;
00199 eee = strchr(ccc,'=') ; *eee = ' ' ;
00200 sscanf( ccc , "%255s %1023s" , nam , val ) ;
00201 free((void *)ccc) ;
00202 }
00203 if( nam[0] == '\0' || val[0] == '\0' ) return(-1) ;
00204
00205 sprintf(eqn,"%s=%s",nam,val) ;
00206 eee = strdup(eqn) ; putenv(eee) ;
00207 return(0) ;
00208 }