//main program int main(){ int cntin,cntout,cntpipe,index,i; CommandLine* cmnd1 = (CommandLine*) malloc(sizeof(CommandLine)); CommandLine* cmnd2 = (CommandLine*) malloc(sizeof(CommandLine)); command = (CommandLine*) malloc(sizeof(CommandLine)); input=(char *)malloc(MAX*sizeof(char)); current_dir=(char *)malloc(MAX*sizeof(char)); getcwd(current_dir,MAX); current_user=(char *)malloc(MAX*sizeof(char)); current_user=getlogin(); current_system=(char *)malloc(MAX*sizeof(char)); gethostname(current_system,MAX); printf("\n"); while(1) { cntin=0,cntout=0,cntpipe=0; amper=0; for(i=0;i<MAX;i++) input[i]='\0'; printf("mySHELL~"); printf("%s",current_user); printf("@"); printf("%s",current_system); printf(":"); getcwd(current_dir,MAX); printf("%s",current_dir); printf("> "); string_parse(); if(command->name == NULL) continue; strcat(input,command->name); if(command->argc >= 2) { strcat(input," "); strcat(input,command->argv[1]); } if(command->argc >= 3) { strcat(input," "); strcat(input,command->argv[2]); } if(command->argc == 4) { strcat(input," "); strcat(input,command->argv[3]); } command->argv[command->argc]=(char *) 0; if (strcmp(command->argv[0], ">") == 0 || strcmp(command->argv[0], "<") == 0 || strcmp(command->argv[0], "|") == 0 || strcmp(command->argv[command->argc-1], ">") == 0 || strcmp(command->argv[command->argc-1], "<") == 0 || strcmp(command->argv[command->argc-1], "|") == 0 ) continue; for(i=1;i<command->argc-1;i++) { if (strcmp(command->argv[i], ">") == 0) { index=i; cntout++; } if (strcmp(command->argv[i], "<") == 0) { index=i; cntin++; } if (strcmp(command->argv[i], "|") == 0) { index=i; cntpipe++; } } if((cntout+cntin+cntpipe) >=2) { printf("more than one level redirection not supported\n"); continue; } if(!strcmp(command->argv[command->argc-1],"&")) { amper=1; puts(command->argv[command->argc-1]); command->argv[command->argc-1]=(char *) 0; command->argc--; } if(cntout == 1) { redirect_command(index,1); continue; } if(cntin == 1) { redirect_command(index,0); continue; } if(cntpipe == 1) { cmnd1->argv = (char**)calloc(16, sizeof(char*)); cmnd1->name = (char*)calloc(16, sizeof(char)); cmnd1->argv[0] = (char*)calloc(16, sizeof(char)); cmnd1->argv[1] = (char*)calloc(16, sizeof(char)); cmnd1->argv[2] = (char*)calloc(16, sizeof(char)); cmnd1->argv[3] = (char*)calloc(16, sizeof(char)); cmnd2->argv = (char**)calloc(16, sizeof(char*)); cmnd2->name = (char*)calloc(16, sizeof(char)); cmnd2->argv[0] = (char*)calloc(16, sizeof(char)); cmnd2->argv[1] = (char*)calloc(16, sizeof(char)); cmnd2->argv[2] = (char*)calloc(16, sizeof(char)); cmnd2->argv[3] = (char*)calloc(16, sizeof(char)); strcat(cmnd1->name,command->name); for(i=0;i<index;i++) strcat(cmnd1->argv[i],command->argv[i]); cmnd1->argv[i]=(char *) 0; strcat(cmnd2->name,command->argv[index+1]); for(i=index+1;i<command->argc;i++) strcat(cmnd2->argv[i-index-1],command->argv[i]); cmnd2->argv[i-index-1]=(char *) 0; piped_command(cmnd1,cmnd2); continue; } if (strcmp(command->name, "exit") == 0 || strcmp(command->name, "logout") == 0 || strcmp(command->name, "cd") == 0 || strcmp(command->name, "cd..") == 0 || strcmp(command->name, "pwd") == 0 || strcmp(command->name, "whoami") == 0) { internal_command_handler(); } else { external_command_handler(); } } }
int imf_init_user(IMF *imf,char *imf_string_original){ int n,i; double value; double value2; char *pos; char *seg_pos; char * imf_string; char *parameter; char *segment; char *function; segment = (char *) malloc((strlen(imf_string_original)+1)*sizeof(char)); parameter = (char *) malloc((strlen(imf_string_original)+1)*sizeof(char)); imf_string = (char *) malloc((strlen(imf_string_original)+1)*sizeof(char)); function = (char *) malloc((strlen(imf_string_original)+1)*sizeof(char)); strcpy(imf_string,imf_string_original); n = 0; pos = imf_string; /** count bracketts **/ while((*pos) != 0){ if((*pos) == '(') n++; pos++; } imf->n = n; imf->k = 1.; imf->m_max_physical = IMF_MASSINF; imf->m_cl = 0.; imf->m = (double *) malloc((imf->n+1) * sizeof(double)); imf->f = (double(**)(double,double *)) malloc((imf->n+1) * sizeof(double(*)(double,double *))); imf->a_f = (double **) malloc((imf->n+1) * sizeof(double*)); imf->mf = (double(**)(double,double *)) malloc((imf->n+1) * sizeof(double(*)(double,double *))); imf->a_mf = (double **) malloc((imf->n+1) * sizeof(double*)); imf->F = (double(**)(double,double *)) malloc((imf->n+1) * sizeof(double(*)(double,double *))); imf->a_F = (double **) malloc((imf->n+1) * sizeof(double*)); imf->mF = (double(**)(double,double *)) malloc((imf->n+1) * sizeof(double(*)(double,double *))); imf->a_mF = (double **) malloc((imf->n+1) * sizeof(double*)); imf->invF = (double(**)(double,double *)) malloc((imf->n+1) * sizeof(double(*)(double,double *))); imf->a_invF = (double **) malloc((imf->n+1) * sizeof(double*)); for(i=1;i<=imf->n;i++){ imf->a_f[i] = (double *) malloc(PARA_MAX * sizeof(double)); imf->a_mf[i] = (double *) malloc(PARA_MAX * sizeof(double)); imf->a_F[i] = (double *) malloc(PARA_MAX * sizeof(double)); imf->a_mF[i] = (double *) malloc(PARA_MAX * sizeof(double)); imf->a_invF[i] = (double *) malloc(PARA_MAX * sizeof(double)); } pos = imf_string; pos = string_parse(pos,parameter,"("); imf->m[0] = assign_double(parameter); for(i=1;i<=imf->n;i++){ pos = string_parse(pos,segment,")"); pos = string_parse(pos,parameter,"("); imf->m[i] = assign_double(parameter); seg_pos = segment; seg_pos = string_parse(seg_pos,function,":"); if(strcmp(function,"pow") == 0){ seg_pos = string_parse(seg_pos,parameter,")"); value = assign_double(parameter); imf->f[i] = &imf_power; imf->a_f[i][0] = value; imf->mf[i] = &imf_power; imf->a_mf[i][0] = value+1.; imf->F[i] = &imf_prim_power; imf->a_F[i][0] = value; imf->mF[i] = &imf_prim_power; imf->a_mF[i][0] = value+1; imf->invF[i] = &imf_inv_prim_power; imf->a_invF[i][0] = value; } if(strcmp(function,"log-norm") == 0){ seg_pos = string_parse(seg_pos,parameter,":"); value = assign_double(parameter); seg_pos = string_parse(seg_pos,parameter,")"); value2 = assign_double(parameter); imf->f[i] = &imf_log_normal; imf->a_f[i][0] = log10(value); imf->a_f[i][1] = value2; imf->mf[i] = &imf_mlog_normal; imf->a_mf[i][0] = log10(value); imf->a_mf[i][1] = value2; imf->F[i] = &imf_prim_log_normal; imf->a_F[i][0] = log10(value); imf->a_F[i][1] = value2; imf->mF[i] = &imf_prim_mlog_normal; imf->a_mF[i][0] = log10(value); imf->a_mF[i][1] = value2; imf->invF[i] = &imf_inv_prim_log_normal; imf->a_invF[i][0] = log10(value); imf->a_invF[i][1] = value2; } } init_psi(imf); free(segment); free(imf_string); free(parameter); free(function); return 0; }
// Look through text_string, ignore backslash and match quoting varibles // allocates a new string with the token static char * string_parse( char * text_string, char delim, char ** last_char ) { char * copy_string ; char * old_string_pointer = text_string ; char * new_string_pointer ; //printf("->>>STRING (%c) PARSE <%s>\n",delim,text_string); if ( text_string == NULL ) { *last_char = text_string ; return owstrdup("") ; } new_string_pointer = copy_string = owstrdup( text_string ) ; // more than enough room if ( copy_string == NULL ) { *last_char = text_string ; return NULL ; } while ( 1 ) { char current_char = old_string_pointer[0] ; new_string_pointer[0] = current_char ; // default copy if ( current_char == '\0' ) { // end of string *last_char = old_string_pointer ; //printf("<<<-STRING (%c) PARSE <%s>, end:<%s>\n",delim,copy_string,*last_char); return copy_string ; } else if ( current_char == '\\' ) { // backslash, use next char literally if ( old_string_pointer[1] ) { new_string_pointer[0] = old_string_pointer[1] ; ++ new_string_pointer ; old_string_pointer += 2 ; } else { // unless there is no next char *last_char = old_string_pointer + 1 ; //printf("<<<-STRING (%c) PARSE <%s>, end:<%s>\n",delim,copy_string,*last_char); return copy_string ; } } else if ( current_char == delim ) { // found delimitter match // point to it, and null terminate *last_char = old_string_pointer ; new_string_pointer[1] = '\0' ; //printf("<<<-STRING (%c) PARSE <%s>, end:<%s>\n",delim,copy_string,*last_char); return copy_string ; } else if ( current_char == '"' || current_char == '\'' ) { // quotation -- find matching end char * quote_last_char ; char * quoted_string = string_parse( old_string_pointer+1, current_char, "e_last_char ) ; //printf("----STRING (%c) PARSE Quote <%s> end:<%s>\n",delim,quoted_string,quote_last_char); if ( quoted_string ) { strcpy( new_string_pointer+1, quoted_string ) ; new_string_pointer += strlen( quoted_string ) + 1 ; if ( quote_last_char[0] == current_char ) { quote_last_char[0] = '\0' ; // clear trailing quote old_string_pointer = quote_last_char + 1 ; } else { old_string_pointer = quote_last_char ; } owfree( quoted_string ) ; } else { new_string_pointer[1] = '\0' ; *last_char = old_string_pointer ; //printf("<<<-STRING (%c) PARSE <%s>, end:<%s>\n",delim,copy_string,*last_char); return copy_string ; } } else { ++ old_string_pointer ; ++ new_string_pointer ; } } }