This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
/* --------------------------------------------------------------------------- ** This software is in the public domain, furnished "as is", without technical ** support, and with no warranty, express or implied, as to its usefulness for ** any purpose. ** ** envVarConv2Str.c ** read env parameter from pqact.conf and convert it to normal test string ** ** Author: Y.C. Tang ** Modify: Harry Chen ** -------------------------------------------------------------------------*/ #ifdef IDENT_C static const char* const envVarConv2Str_c_Id = "$Id: envVarConv2Str.c,v 1.1 1994/04/29 16:55:55 fxa Exp $"; #endif #include <stdio.h> #include <assert.h> #include <ctype.h> #include <string.h> #include <time.h> #include <signal.h> #include "ldm.h" #include "regexp.h" /* Henry Spencer's package, not library version */ #include "palt.h" #include "action.h" #include "ldmprint.h" #include "atofeedt.h" #include "alloc.h" #include "ulog.h" # define MAXLEN 1024 /* -- AppendStr ---------------------------------------------------------- ** appen each character to the string ** ** return 0 if sucess /* -- implementation ------------------------------------------------------- ** ** ** ** -------------------------------------------------------------------------*/ int AppendStr(char *OriStr, char *AppStr) { if ( AppStr == NULL ) { /* selected environment variable not exist */ udebug("envVarConv2Str Append string is NULL characters"); return 0; } if ( ( strlen(OriStr) + strlen(AppStr) ) >= MAXLEN ) { uerror("envVarConv2Str Append string exceeds maximum length: %d characters", MAXLEN); return 1; } else { strcat(OriStr, AppStr); return 0; } }/*AppendStr*/ /* --envVarConv2Str ------------------------------------------------------- ** Convert enviroment variable to the normal text string ** ** /* -- implementation ------------------------------------------------------- ** pass in the original string no mater if it has env. Var. ** and return the converted string ** ** -------------------------------------------------------------------------*/ char *envVarConv2Str(char *instr) { /* declaration */ int instrlen=0, i=0, iret=0; static char outstr[MAXLEN+1]; char varstr[MAXLEN+1]; char tmpstr[2]; enum cstate { TEXT, VARSTART, PARVAR, NOPARVAR } state; /* check input string length */ if ( ( instrlen=strlen(instr) ) >= MAXLEN ) { uerror("envVarConv2Str input string exceeds maximum length: %d characters", MAXLEN); return(NULL); } /* set initial state */ varstr[0]=0; outstr[0]=0; tmpstr[0]=0; state=TEXT; /* start text expanding loop */ for ( i=0; i< instrlen; i++ ) { /* uinfo("envVarConv2Str processing char : %c", instr[i]); */ switch(state) { case TEXT : /* general text character */ if ( instr[i] != '$' ) { /* general text character */ sprintf(tmpstr,"%c",instr[i]); if ( (iret=AppendStr(outstr, tmpstr)) != 0 ) return NULL; } else { /* environment variable starts */ state=VARSTART; } break; case VARSTART : /* environment variable starts */ if ( instr[i] == '{' ) { /* environment variable starts with { */ state=PARVAR; } else if ( isalpha(instr[i]) || (instr[i] == '_') ) { /* environment variable continue */ state=NOPARVAR; sprintf(tmpstr,"%c",instr[i]); if ( (iret=AppendStr(varstr, tmpstr)) != 0 ) return NULL; } else if ( instr[i] == '$' ) { /* environment variable is $$ (PID) */ state=TEXT; sprintf(varstr,"%d",getppid()); if ( (iret=AppendStr(outstr, varstr)) != 0 ) return NULL; strcpy(varstr,""); } else { /* not an environment variable */ state=TEXT; sprintf(varstr,"$%c",instr[i]); if ( (iret=AppendStr(outstr, varstr)) != 0 ) return NULL; strcpy(varstr,""); } break; case NOPARVAR : /* environment variable starts with no { */ if ( isalnum(instr[i]) || (instr[i] == '_') ) { /* environment variable continue */ sprintf(tmpstr,"%c",instr[i]); if ( (iret=AppendStr(varstr, tmpstr)) != 0 ) return NULL; } else if ( instr[i] == '$' ) { /* environment variable ends with another variable start */ state=VARSTART; if ( (iret=AppendStr(outstr,(char *)getenv(varstr))) != 0 ) return NULL; strcpy(varstr,""); } else if ( instr[i] == ' ' ) { /* environment variable ends with blank space */ state=TEXT; if ( (iret=AppendStr(outstr,(char *)getenv(varstr))) != 0 ) return NULL; if ( (iret=AppendStr(outstr, " ")) != 0 ) return NULL; strcpy(varstr,""); } else { /* environment variable ends */ state=TEXT; if ( (iret=AppendStr(outstr,(char *)getenv(varstr))) != 0 ) return NULL; strcpy(varstr,""); sprintf(tmpstr,"%c",instr[i]); if ( (iret=AppendStr(outstr,tmpstr)) != 0 ) return NULL; } break; case PARVAR : /* environment variable starts with { */ if ( instr[i] == '}' ) { /* environment variable ends */ state=TEXT; if ( (iret=AppendStr(outstr,(char *)getenv(varstr))) != 0 ) return NULL; strcpy(varstr,""); } else { /* environment variable continue */ sprintf(tmpstr,"%c",instr[i]); if ( (iret=AppendStr(varstr,tmpstr)) != 0 ) return NULL; } break; } } switch(state) { case PARVAR : /* environment variable is not ended yet */ uerror("envVarConv2Str transform fail"); return NULL; break; case NOPARVAR : /* environment ended at the end of the string */ if ( (iret=AppendStr(outstr,(char *)getenv(varstr))) != 0 ) return NULL; udebug("envVarConv2Str transform ok"); break; case VARSTART : /* $ ended at the end of the string */ sprintf(tmpstr,"$",instr[i]); if ( (iret=AppendStr(outstr,tmpstr)) != 0 ) return NULL; udebug("envVarConv2Str transform ok,output string:\"%s\"", outstr); break; default : break; } return outstr; } =============================================================================== Robb Kambic Unidata Program Center Software Engineer III Univ. Corp for Atmospheric Research address@hidden WWW: http://www.unidata.ucar.edu/ ===============================================================================