aboujouj83
IS-IT--Management
I am writing a unix shell in C. One of the required function of the shell is to be able to make variable substitution i.e if we write cd $var1, the shell should convert it to cd folder1, if $var1=folder1.
My shell is working fine if I don't include the code of variable substitution. I will write the relevant part of the code here (the whole program is very long, and won't help much to solve the problem):
char* findVar(myshenvTP *env, char* gname){
int j;
for (j = 0; j < MAXNOENV; j++){
/* returns null when it finds a variable name = " " meaning there are no more variables */
if (strcmp(env -> envarray[j].NAME," "== 0)
return "notfound";
else if (strcmp(env -> envarray[j].NAME,gname) == 0){
/* here the variable is found, so we return it */
return (env -> envarray [j].VALUE);
}
}
/* the function hasn't returned yet i.e variable not found */
/* returns also NULL in this case */
return "notfound";
}
**********************************************************
int parseline(myshenvTP *env, char *line, char **token, char *dlimiter) {
int i = 0;
token = strtok(line, dlimiter);
while(token != NULL) {
/* check each token for environmental variables
replace the variable by its value if it exists
return variable not defined if variable not found */
if (strcmp ((replaceVariables(env, token)),"varNotDefined" == 0)
return -1;
*/
i++;
token = strtok(NULL, dlimiter);
}
if(i > MAX_ARG) {
fprintf(stderr, "Exceeds defined max # of args! Omit those out of index!\n"
i = MAX_ARG;
}
return i;
}
***********************************************************
/* replace environmental variable by its value
assumes that only one environmental variable used in the beginning of the token */
char* replaceVariables (myshenvTP *env, char *aToken){
char *subtoken1 = malloc((1)*sizeof(char));
char *valueOfVar = malloc((1)*sizeof(char));
char *subtoken2 = malloc((1)*sizeof(char));
if (aToken[0] != '$')
return "noVariablefound";
else{
subtoken1= strtok(aToken, "/"
if (subtoken1 == NULL){ /* no token found */
/* here we search for the variable in the enviromental variable array */
valueOfVar = findVar(env, aToken + 1);
if(strcmp(valueOfVar, "notfound" == 0)
{
printf("%s: Undefined variable.\n", aToken + 1);
return "varNotDefined";
}
else strcpy (aToken, valueOfVar);
return "success";
/* here we found a token delimiter
So we replace subtoken1 by its value and then concatenate it with subtoken2*/
}else{
subtoken2 = strtok(NULL, "/"
strcpy(aToken, valueOfVar);
strcat(aToken, "/"
strcat(aToken, subtoken2);
/*free(subtoken1);
free(subtoken2);
free(valueOfVar); */
return "success";
}
}
}
***********************************************************
Note that these types are declared in the header of my file:
#define MAX_STR_LEN 64
#define MAX_LEN 1023 /* maximum length of a line */
#define MAX_ARG 100 /* maximum # of command arguments */
#define MAXNOCMD 32 /* max No of previous commands shown for history */
#define MAXNOENV 100 /* max No of environment variables allowed */
#define MALLOC(p,n,type) ((p) = malloc(*sizeof(type))) == NULL)
extern int errno; /* To use errno in the program */
/* internal variable structure */
typedef struct {
char NAME[MAX_STR_LEN];
char VALUE[MAX_STR_LEN];
}envarT;
/* Shell environmental var structure */
typedef struct {
envarT envarray[MAXNOENV];
} myshenvTP;
/* Command structure */
typedef struct {
char *Argv[MAX_ARG]; /* Argv[0] is command name */
int argc;
}cmdT, *cmdTP;
***********************************************************
WHEN I COMMENT THE replaceVariables method in the parseLine method, the program works...BUT I CAN'T SEE THE ERROR...IT'S PROBABLY MEMORY RELATED since the messages that I get are:
"Segmentation fault" and "Bus error" depending on the command that I request.
ANY HELP IS VERY APPRECIATED
ABOUJOUJ
My shell is working fine if I don't include the code of variable substitution. I will write the relevant part of the code here (the whole program is very long, and won't help much to solve the problem):
char* findVar(myshenvTP *env, char* gname){
int j;
for (j = 0; j < MAXNOENV; j++){
/* returns null when it finds a variable name = " " meaning there are no more variables */
if (strcmp(env -> envarray[j].NAME," "== 0)
return "notfound";
else if (strcmp(env -> envarray[j].NAME,gname) == 0){
/* here the variable is found, so we return it */
return (env -> envarray [j].VALUE);
}
}
/* the function hasn't returned yet i.e variable not found */
/* returns also NULL in this case */
return "notfound";
}
**********************************************************
int parseline(myshenvTP *env, char *line, char **token, char *dlimiter) {
int i = 0;
token = strtok(line, dlimiter);
while(token != NULL) {
/* check each token for environmental variables
replace the variable by its value if it exists
return variable not defined if variable not found */
if (strcmp ((replaceVariables(env, token)),"varNotDefined" == 0)
return -1;
*/
i++;
token = strtok(NULL, dlimiter);
}
if(i > MAX_ARG) {
fprintf(stderr, "Exceeds defined max # of args! Omit those out of index!\n"
i = MAX_ARG;
}
return i;
}
***********************************************************
/* replace environmental variable by its value
assumes that only one environmental variable used in the beginning of the token */
char* replaceVariables (myshenvTP *env, char *aToken){
char *subtoken1 = malloc((1)*sizeof(char));
char *valueOfVar = malloc((1)*sizeof(char));
char *subtoken2 = malloc((1)*sizeof(char));
if (aToken[0] != '$')
return "noVariablefound";
else{
subtoken1= strtok(aToken, "/"
if (subtoken1 == NULL){ /* no token found */
/* here we search for the variable in the enviromental variable array */
valueOfVar = findVar(env, aToken + 1);
if(strcmp(valueOfVar, "notfound" == 0)
{
printf("%s: Undefined variable.\n", aToken + 1);
return "varNotDefined";
}
else strcpy (aToken, valueOfVar);
return "success";
/* here we found a token delimiter
So we replace subtoken1 by its value and then concatenate it with subtoken2*/
}else{
subtoken2 = strtok(NULL, "/"
strcpy(aToken, valueOfVar);
strcat(aToken, "/"
strcat(aToken, subtoken2);
/*free(subtoken1);
free(subtoken2);
free(valueOfVar); */
return "success";
}
}
}
***********************************************************
Note that these types are declared in the header of my file:
#define MAX_STR_LEN 64
#define MAX_LEN 1023 /* maximum length of a line */
#define MAX_ARG 100 /* maximum # of command arguments */
#define MAXNOCMD 32 /* max No of previous commands shown for history */
#define MAXNOENV 100 /* max No of environment variables allowed */
#define MALLOC(p,n,type) ((p) = malloc(*sizeof(type))) == NULL)
extern int errno; /* To use errno in the program */
/* internal variable structure */
typedef struct {
char NAME[MAX_STR_LEN];
char VALUE[MAX_STR_LEN];
}envarT;
/* Shell environmental var structure */
typedef struct {
envarT envarray[MAXNOENV];
} myshenvTP;
/* Command structure */
typedef struct {
char *Argv[MAX_ARG]; /* Argv[0] is command name */
int argc;
}cmdT, *cmdTP;
***********************************************************
WHEN I COMMENT THE replaceVariables method in the parseLine method, the program works...BUT I CAN'T SEE THE ERROR...IT'S PROBABLY MEMORY RELATED since the messages that I get are:
"Segmentation fault" and "Bus error" depending on the command that I request.
ANY HELP IS VERY APPRECIATED
ABOUJOUJ