void process_internal_cmd(char* cmd, char* arg[]) { if(strcmp(cmd, "exit") == 0) { exit(1); } else if(strcmp(cmd, "cd") == 0) { char the_path[256]; if (arg[1] == NULL) chdir ("/"); else chdir (arg[1]); getcwd(the_path, 255); name_shell(the_path); perror (cmd); } else if(strcmp(cmd, "jobs") == 0) { print_jobs(); } }
//******************************************** // function name: ExeCmd // Description: interperts and executes built-in commands // Parameters: pointer to jobs, command string // Returns: 0 - success,1 - failure //************************************************************************************** int ExeCmd(pJob* head, char* lineSize, char* cmdString) { char* cmd; char* args[MAX_ARG]; char pwd[MAX_LINE_SIZE]; static char last_dir[MAX_LINE_SIZE]; char* delimiters = " \t\n"; int i = 0; /*========used in kill command=======*/ int signal_number, Job_number, signal_sent; int found_job_pid; char found_job_name[MAX_LINE_SIZE]; /*===================================*/ /*========used in fg command=========*/ int child_done_id, fg_runner_pid, fg_req_JID; /*===================================*/ /*========used in bg command=========*/ int bg_runner_pid; /*===================================*/ bool illegal_cmd = FALSE; // illegal command cmd = strtok(lineSize, delimiters); if (cmd == NULL) return 0; args[0] = cmd; L_Fg_Cmd = cmd; for (i=1; i<MAX_ARG; i++) { args[i] = strtok(NULL, delimiters); } /*************************************************/ // Built in Commands /*************************************************/ if (!strcmp(cmd, "cd") ) { getcwd(pwd,sizeof(pwd)); if(!strcmp(args[1], "-")) { if(last_dir[0]!=0) { chdir(last_dir); } else { perror("smash error: > no previous path exists\n"); } } else { if(!chdir(args[1])) { strcpy(last_dir,pwd); } else { printf("smash error: > \"%s\" -path not found\n",args[1]); illegal_cmd = TRUE; } } } /*************************************************/ else if (!strcmp(cmd, "pwd")) { if(getcwd(pwd,sizeof(pwd))!= NULL) { fprintf(stdout,"%s\n",pwd); } else { perror("smash error: > pwd error\n"); } } /*************************************************/ else if (!strcmp(cmd, "mkdir")) { if(mkdir(args[1], 0755)) { if(EEXIST == errno) { printf("smash error: > \"%s\" -directory already exists\n",args[1]); } else { printf("smash error: > \"%s\" -cannot create directory\n",args[1]); } } } /*************************************************/ else if (!strcmp(cmd, "jobs")) { print_jobs(*head); } /*************************************************/ else if (!strcmp(cmd, "kill")) { if(args[1][0] != '-') { printf("smash error: > \"%s\"",cmdString); return -1; } signal_number=atoi(args[1]+1); Job_number=atoi(args[2]); //search requested job found_job_pid=find_job_PID(*head,Job_number); if(found_job_pid==-1) { printf("smash error: > kill %d - job does not exist\n",Job_number); return -1; } else { switch (signal_number) { case SIGTSTP: signal_sent=kill(found_job_pid,SIGTSTP); printf("signal SIGTSTP was sent to pid %d\n",found_job_pid); if(signal_sent!=0) { printf("smash error: > kill %d - cannot send signal\n",Job_number); } strcpy(found_job_name,find_job_name(*head,found_job_pid)); remove_job(head,found_job_pid); insert_job(head, found_job_pid, found_job_name, SUSPENDED); break; case SIGINT: signal_sent=kill(found_job_pid,SIGINT); printf("signal SIGINT was sent to pid %d\n",found_job_pid); if(signal_sent!=0) { printf("smash error: > kill %d - cannot send signal\n",Job_number); } remove_job(head,found_job_pid); break; case SIGTERM: signal_sent=kill(found_job_pid,SIGTERM); printf("signal SIGTERM was sent to pid %d\n",found_job_pid); if(signal_sent!=0) { printf("smash error: > kill %d - cannot send signal\n",Job_number); } remove_job(head,found_job_pid); break; default: signal_sent=kill(found_job_pid,signal_number); if(signal_sent!=0) { printf("smash error: > kill %d - cannot send signal\n",Job_number); } printf("signal %d was sent to pid %d\n",signal_number,found_job_pid); } } } /*************************************************/ else if (!strcmp(cmd, "showpid")) { printf("smash pid is %d\n",getpid()); } /*************************************************/ else if (!strcmp(cmd, "fg")) { if(args[1]==NULL) { Last_Bg_Pid=get_last_job_pid(*head); fg_runner_pid=Last_Bg_Pid; } else { fg_req_JID=atoi(args[1]); fg_runner_pid=find_job_PID(*head,fg_req_JID); } if (fg_runner_pid== -1)// No jobs in the Bg { perror("smash error: requested process not found\n"); } else if(is_suspended(*head,fg_runner_pid)== SUSPENDED) { kill(fg_runner_pid,SIGCONT); printf("signal SIGCONT was sent to pid %d\n",fg_runner_pid); unsuspend(*head,fg_runner_pid); Susp_Bg_Pid=get_last_suspended_job_pid(*head); } GPid=fg_runner_pid; L_Fg_Cmd = find_job_name(*head,fg_runner_pid); do { child_done_id = wait(NULL); if((child_done_id==-1)&&(errno==EINTR)) //child is gone due to inerrupt { break; } } while((child_done_id != fg_runner_pid) && (fg_runner_pid != Susp_Bg_Pid)); remove_job(head,fg_runner_pid); GPid= -1; } /*************************************************/ else if (!strcmp(cmd, "bg")) { if(args[1]==NULL) { bg_runner_pid=get_last_suspended_job_pid(*head); } else { fg_req_JID=atoi(args[1]); bg_runner_pid=find_job_PID(*head, fg_req_JID); if(is_suspended(*head,bg_runner_pid)==NOT_SUSPENDED) { perror("smash error: > bg called on un-suspended job\n"); return -1; } } strcpy(found_job_name,find_job_name(*head,bg_runner_pid)); printf("%s",found_job_name); kill(bg_runner_pid,SIGCONT); printf("signal SIGCONT was sent to pid %d\n",bg_runner_pid); unsuspend(*head,bg_runner_pid); Susp_Bg_Pid=get_last_suspended_job_pid(*head); } /*************************************************/ else if (!strcmp(cmd, "quit")) { if(args[1]==NULL) { destroy_list(head); exit(0); } else if (!strcmp(args[1], "kill")) { if (kill_jobs(*head) != -1) { destroy_list(head); exit(0); } else { exit (-1); } } else perror("smash error: > quit called with illegal argument\n"); return -1; } /*************************************************/ else // external command { ExeExternal(args, cmdString); return 0; } if (illegal_cmd == TRUE) { printf("smash error: > \"%s\"\n", cmdString); return 1; } return 0; }
int main() { int bg; char *args[ARGS_SIZE]; int cnt; char new_dir[MAX_PATH + 1]; // new_dir[0] = '\0'; char buf[MAX_PATH + 1]; memcpy(curdir, getcwd(buf, MAX_PATH + 1), MAX_PATH + 1); signal(SIGCHLD, handle_child_death); // free(shell_command_history); shell_command_history = (struct history_item **) malloc(HISTORY_SIZE*sizeof(struct history_item *)); for(int i = 0; i < HISTORY_SIZE; i++) { shell_command_history[i] = (struct history_item *) malloc(sizeof(struct history_item)); } while(1) { bg = 0; cnt = getcmd("\n>> ", args, &bg); args[cnt] = NULL; /* for (int i = 0; i < cnt; i++) printf("\nArg[%d] = %s", i, args[i]); */ printf("\n"); int history_element_to_execute; if ( (history_element_to_execute = atoi(args[0])) > 0) { printf("history_element_to_execute: %d\n", history_element_to_execute); memcpy(args, shell_command_history[history_element_to_execute-1], ARGS_SIZE); } save_to_history(args); if (strcmp(args[0],"history") == 0) { if (args[1] != NULL) { printf("history does not require any arguments.\n"); exit(-1); } show_history(); continue; } if (strcmp(args[0], "cd") == 0) { new_dir[0] = '\0'; strcat(new_dir, curdir); printf("%s\n", new_dir); if (args[1][0] == '/') { new_dir[0] = '\0'; } char slash[] = {'/', '\0'}; strcat(new_dir, slash); strcat(new_dir, args[1]); strcat(new_dir, slash); curdir[0] = '\0'; strcat(curdir, new_dir); chdir(new_dir); continue; } if (strcmp(args[0], "pwd") == 0) { char* cwd; cwd = getcwd(buf, MAX_PATH + 1); printf("%s", cwd); continue; } if (strcmp(args[0], "exit") == 0) { exit(0); } if (strcmp(args[0], "jobs") == 0) { print_jobs(); continue; } if (strcmp(args[0], "fg") == 0) { // waitpid(); int pos = strtol(args[1], NULL, 10); struct job * selected_job = get_job_at_pos(pos); if (selected_job == NULL) { printf("There was an issue. The selected job, %d, has not been found.\n", pos); continue; } int status = 0; waitpid(selected_job->pid, &status, 0); continue; } // char * file = malloc(sizeof); int child_pid = 0; if((child_pid = fork()) == 0) { int output_filename_index = cnt - 1; if (bg) { output_filename_index--; } if (output_filename_index < 1) { output_filename_index = 1; } printf("cnt: %d.\n", cnt); char* output_filename = malloc((MAX_PATH+1)*sizeof(char)); if (strcmp(args[output_filename_index - 1], ">") == 0) { char* arg = args[output_filename_index]; memcpy(output_filename, arg, strlen(arg)+1); printf("%s\n", output_filename); close(1); open(output_filename, O_WRONLY | O_CREAT, 0666); // int fd[2] = {0,1}; // pipe(fd); args[output_filename_index - 1] = NULL; } int result = execvp(args[0], args); if (result < 0) { printf("We got the errno %d", errno); } } else { // continue execution } if (bg) { // printf("\nBackground enabled..\n"); int inserted_job_pos = insert_job(child_pid, args); printf("Created job with pid: %d, at pos: %d", child_pid, inserted_job_pos); } else { // printf("\nBackground not enabled \n"); int status = 0; waitpid(child_pid, &status, 0); } printf("\n\n"); } }
int main(int argc, char *argv[]) { cal_path(); setlocale(LC_MESSAGES, ""); bindtextdomain("opvdm", get_lang_path()); textdomain("opvdm"); timer_init(0); timer_init(1); dbus_init(); set_ewe_lock_file("", ""); cell.onlypos = FALSE; char pwd[1000]; if (getcwd(pwd, 1000) == NULL) { ewe("IO error\n"); } dump_init(&cell); dump_load_config(&cell); remove("snapshots.zip"); remove("light_dump.zip"); hard_limit_init(); //char path[PATH_MAX]; //char dest[PATH_MAX]; //pid_t pid = getpid(); //sprintf(path, "/proc/%d/exe", pid); //if (readlink(path, dest, PATH_MAX) == -1) //{ // printf("error\n"); // exit(1); //} // char *base = strrchr(dest, '/'); //*base='/'; //*(base+1)=0; set_plot_script_dir(pwd); //set_plot_script_dir(char * in) if (scanarg(argv, argc, "--help") == TRUE) { printf("opvdm_core - Organic Photovoltaic Device Model\n"); printf(copyright); printf("\n"); printf("Usage: opvdm_core [options]\n"); printf("\n"); printf("Options:\n"); printf("\n"); printf("\t--outputpath\toutput data path"); printf("\t--inputpath\t sets the input path\n"); printf("\t--version\tdisplays the current version\n"); printf("\t--zip_results\t zip the results\n"); printf("\t--optics\t runs only optical simulation\n"); printf("\t--cpus\t sets the number of CPUs\n"); printf("\n"); printf ("Additional information about opvdm is available at www.opvdm.com.\n"); printf("\n"); printf ("Report bugs to: [email protected]\n\n"); exit(0); } if (scanarg(argv, argc, "--version") == TRUE) { printf("opvdm_core, Version %s\n", opvdm_ver); printf(copyright); printf(this_is_free_software); printf ("There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or\n"); printf("FITNESS FOR A PARTICULAR PURPOSE.\n"); printf("\n"); exit(0); } if (geteuid() == 0) { ewe("Don't run me as root!\n"); } set_dump_status(dump_stop_plot, FALSE); set_dump_status(dump_print_text, TRUE); set_io_dump(FALSE); srand(time(0)); textcolor(fg_green); randomprint(_("Organic Photovoltaic Device Model (www.opvdm.com)\n")); randomprint(_ ("You should have received a copy of the GNU General Public License\n")); randomprint(_ ("along with this software. If not, see www.gnu.org/licenses/.\n")); randomprint("\n"); randomprint(_ ("If you wish to collaborate in anyway please get in touch:\n")); randomprint(_("[email protected]\n")); randomprint(_("www.roderickmackenzie.eu/contact.html\n")); randomprint("\n"); textcolor(fg_reset); globalserver.on = FALSE; globalserver.cpus = 1; globalserver.readconfig = TRUE; if (scanarg(argv, argc, "--outputpath") == TRUE) { strcpy(sim_output_path(), get_arg_plusone(argv, argc, "--outputpath")); } else { strcpy(sim_output_path(), pwd); } if (scanarg(argv, argc, "--inputpath") == TRUE) { strcpy(sim_input_path(), get_arg_plusone(argv, argc, "--inputpath")); } else { strcpy(sim_input_path(), sim_output_path()); } dump_load_config(&cell); if (scanarg(argv, argc, "--onlypos") == TRUE) { cell.onlypos = TRUE; } char name[200]; struct inp_file inp; inp_init(&inp); inp_load_from_path(&inp, sim_input_path(), "ver.inp"); inp_check(&inp, 1.0); inp_search_string(&inp, name, "#core"); inp_free(&inp); if (strcmp(name, opvdm_ver) != 0) { printf ("Software is version %s and the input files are version %s\n", opvdm_ver, name); exit(0); } gui_start(); if (scanarg(argv, argc, "--optics") == FALSE) server_init(&globalserver); if (scanarg(argv, argc, "--lock") == TRUE) { server_set_dbus_finish_signal(&globalserver, get_arg_plusone(argv, argc, "--lock")); } int ret = 0; int do_fit = FALSE; if (scanarg(argv, argc, "--fit") == TRUE) do_fit = TRUE; FILE *f = fopen("fit.inp", "r"); if (f != NULL) { fclose(f); inp_init(&inp); inp_load_from_path(&inp, pwd, "fit.inp"); int fit_temp; inp_search_int(&inp, &fit_temp, "#do_fit"); if (fit_temp == 1) { do_fit = TRUE; } inp_free(&inp); } if (do_fit == TRUE) { set_dump_status(dump_lock, FALSE); set_dump_status(dump_print_text, FALSE); set_dump_status(dump_iodump, FALSE); set_dump_status(dump_optics, FALSE); set_dump_status(dump_newton, FALSE); set_dump_status(dump_plot, FALSE); set_dump_status(dump_stop_plot, FALSE); set_dump_status(dump_opt_for_fit, FALSE); set_dump_status(dump_print_newtonerror, FALSE); set_dump_status(dump_print_converge, FALSE); set_dump_status(dump_print_pos_error, FALSE); set_dump_status(dump_lock, TRUE); } #include "main_args.c" if (scanarg(argv, argc, "--optics") == TRUE) { gui_start(); struct light two; light_init(&two, &cell, pwd); //light_set_dx(&cell.mylight,cell.ymesh[1]-cell.ymesh[0]); light_load_config(&two); two.disable_transfer_to_electrical_mesh = TRUE; set_dump_status(dump_lock, FALSE); set_dump_status(dump_optics, TRUE); set_dump_status(dump_optics_verbose, TRUE); double Psun; inp_init(&inp); inp_load_from_path(&inp, pwd, "light.inp"); inp_search_double(&inp, &(Psun), "#Psun"); Psun = 1.0; //fabs(Psun); inp_free(&inp); light_solve_and_update(&cell, &two, Psun, 0.0); light_dump(&two); light_free(&two); complex_solver_free(); } else { gen_dos_fd_gaus_fd(); server_add_job(&globalserver, cell.outputpath, cell.inputpath); print_jobs(&globalserver); ret = server_run_jobs(&globalserver); } server_shut_down(&globalserver); if (scanarg(argv, argc, "--zip_results") == TRUE) { printf("zipping results\n"); int ret; char temp[200]; DIR *dir = opendir("snapshots"); if (dir) { closedir(dir); ret = system("zip -r -j -q snapshots.zip ./snapshots/*"); if (ret == -1) { printf("tar returned error\n"); } join_path(2, temp, cell.outputpath, "snapshots"); remove_dir(temp); } dir = opendir("light_dump"); if (dir) { closedir(dir); ret = system ("zip -r -j -q light_dump.zip ./light_dump/*"); if (ret == -1) { printf("tar returned error\n"); } join_path(2, temp, cell.outputpath, "light_dump"); remove_dir(temp); } } hard_limit_free(); if (ret != 0) { return 1; } return 0; }