shell - Parser generating segfaults in C -
i've been trying work past 2 weeks no avail. have project create shell implements parsing , built-in commands. issue i'm having when pass char* parse function , returns, when try access part of it, segfault. i've tried different methods including struct holding char** same problems, i'm guessing it's issue parser. appreciate help. code parser.c:
#define bufsize 1024 #define tok_bufsize 64 #define tok_delim " \t\r\n\a" char*** parse(char *line0){ char* null_ptr = 0; char*** cmd = malloc(max_size * sizeof(char**)); /* char arg[] = argument char* argv[] = argument array char** cmd[] = array of argument arrays */ int bufsize = max_size, cmdp = 0, argp = 0, com = false, redir = false; char *token; char* line = malloc(100*sizeof(char)); strcpy(line,line0); token = strtok(line, tok_delim); while (token){ if (*token == ';'){ // new command string char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[0]=tmp1; cmd[cmdp] = tmpa; argp = 0; cmdp++; com = false; redir = false; } else if (*token == '>' || *token == '<' || token == ">>"){ // redirects argp = 0; char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[argp]=tmp1; argp++; printf("redirect: %s\n",tmp1); com = false; redir = true; } else if (*token == '|'){ // pipe printf("pipe\n"); cmdp++; argp = 0; com = false; } else if (redir){ // redirect file name // redirect token stored in arg[] char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[argp]=tmp1; cmd[cmdp]=tmpa; argp = 0; cmdp++; redir = false; com = false; printf("file: %s\n", token); } else if (token == "&") // background { cmdp++; argp = 0; char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[0]=tmp1; cmd[cmdp]=tmpa; printf("background"); } else if (!com && !redir){ // command entered argp = 0; char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[argp] = tmp1; argp++; printf("command %s\n", token); com = true; } else if (com){ // argument command, other redirects , pipes taken care of char* tmp1 = malloc(bufsize * sizeof(char)); char** tmpa = malloc(bufsize * sizeof(char*)); strcpy(tmp1, token); tmp1[sizeof(token)] = null_ptr; tmpa[argp] = tmp1; argp++; printf("argument: %s\n", token); //cmd[cmdp] = argv; // save current working argument array //cmdp++; } // end of if else statements token = strtok(null, tok_delim); } // end of while cmdp++; cmd[cmdp] = null; return &cmd; }
when compiled code on command-line typing in:
gcc /path/to/yourcodefilename.c -wall -wextra
but replacing /path/to/yourcodefilename.c
actual filename of code containing main function calls function (my file test2.c), received warnings. first being:
./test2.c:21: error: 'aaa' undeclared (first use in function) ./test2.c:21: error: (each undeclared identifier reported once ./test2.c:21: error: each function appears in.)
and received few of those. "aaa" replaced named of used inside function has not been defined. includes word true , false. correct this, can use @ top of program:
#define false n #define true y
where n , y numbers representing false , true respectively. way correct include header files containing definitions "true" , "false".
the second thing noticed on few lines is:
warning: assignment makes integer pointer without cast
make sure don't convert data on 1 type another. example, don't set character variable pointer value.
for example, change:
tmp1[sizeof(token)] = null_ptr;
to:
tmp1[sizeof(token)] = '\0';
because specifying index char*
means specifying char
, , null_ptr of type char*
, char*
, char
not same. did assigned null value char
.
i hope helps troubleshooting
Comments
Post a Comment