int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; bool time_travel = false; bool time_travel_limit = false; int num_of_processes =0; program_name = argv[0]; char c = ' '; for (;;) switch (getopt (argc, argv, "ptj")) { case 'p': print_tree = true;c = 'p'; break; case 't': time_travel = true; c = 't';break; case 'j': time_travel_limit = true; num_of_processes = atoi(argv[2]); c = 'j'; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) { if (argc > 1 && c == 'j' && optind == argc-2) {} else { usage (); } } script_name = argv[argc - 1]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { if (time_travel == false && time_travel_limit == false) { last_command = command; execute_command (command, time_travel); } } } if (time_travel == true) {execute(command_stream);} if (time_travel_limit == true) {execute_limit(command_stream, num_of_processes);} return print_tree || !last_command ? 0 : command_status (last_command); }
int main(int argc, char **argv) { /* Parameter check */ const char *printdb_path = NULL; if (argc < 2) { usage(); return 1; } if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) { usage(); return 1; } printdb_path = get_string_param(argv[1]); if (!printdb_path) { return 1; } if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT, 0666))) { printf("%s: unable to open %s\n", argv[0], printdb_path); return 1; } /* Ensure we are modes 666 */ chmod(printdb_path, 0666); /* Do commands */ if (strcmp(argv[2], "lpq") == 0) { return lpq_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "lprm") == 0) { return lprm_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "print") == 0) { return print_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "queuepause") == 0) { return queuepause_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "queueresume") == 0) { return queueresume_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "lppause") == 0) { return lppause_command(argc - 2, &argv[2]); } if (strcmp(argv[2], "lpresume") == 0) { return lpresume_command(argc - 2, &argv[2]); } /* Unknown command */ printf("%s: invalid command %s\n", argv[0], argv[1]); return 1; }
int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; bool time_travel = false; bool output_to_file=false; bool error_to_file=false; bool all_to_file=false; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "ptvxoehas")) { case 'p': print_tree = true; break; case 't': time_travel = true; break; case 'x': x_option=true;break; case 'v': v_option=true;break; case 'o': output_to_file=true;break; case 'e': error_to_file=true;break; case 'a': all_to_file=true;break; case 's': s_option=true;break; case 'h': fprintf(stderr,"option p to print command trees without execution.\noption t to exploit parallelism between command trees.\n"); fprintf(stderr,"option x to print simple commands and their arguments before execution.\noption v to print shell input before execution.\n"); fprintf(stderr,"option o to save output to output.txt.\noption e to save error to error.txt.\n"); fprintf(stderr,"option a to save output and error to output_and_error.txt.\n"); fprintf(stderr,"option s to slowly go through script, one command tree at a time\n"); fprintf(stderr,"option x and v not available in combination with option t.\n"); fprintf(stderr,"options x or v in combination with option s to tell what the next command is before continuing.\n"); return 0; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); if(time_travel) { if(x_option || v_option || s_option) { no_debug_time_travel(); } } if(print_tree && s_option) { error (1, 0, "usage: %s [-pxvtoehas] SCRIPT-FILE, cannot use step_mode and print tree", program_name); } script_name = argv[optind]; if(s_option) { fprintf(stderr,"Step mode enabled. Press d to disable step mode, press a to abort further execution, or press enter to move onto the next command.\n"); } if (all_to_file==false) { if(output_to_file==true) { int fd=open("output.txt",O_CREAT|O_TRUNC|O_WRONLY,0644); if(fd<0) { fprintf(stderr,"No available file descriptors"); return(1); } dup2(fd,1); close(fd); } if(error_to_file==true) { int fd=open("error.txt",O_CREAT|O_TRUNC|O_WRONLY,0644); if(fd<0) { fprintf(stderr,"No available file descriptors"); return(1); } dup2(fd,2); close(fd); } } else//all to file is true { int fd=open("output_and_error.txt",O_CREAT|O_TRUNC|O_WRONLY,0644); if(fd<0) { fprintf(stderr,"No available file descriptors"); return(1); } dup2(fd,2); dup2(fd,1); close(fd); } FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; if(time_travel) { struct dependency_graph* graph=create_graph(command_stream); int final_status=0; final_status=execute_graph(graph); } if(print_tree==true || time_travel==false) { while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command, time_travel); } } } return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { int opt; int command_number = 1; int print_tree = 0; int time_travel = 0; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = 1; break; case 't': time_travel = 1; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; command_node* inner_current; if(!print_tree && time_travel) { int read_array_len = 0; int write_array_len = 0; // iterate through command stream to build dependency list, information is stored in command nodes while (command_stream->current != NULL) { read_array_len = 0; write_array_len = 0; command_stream->current->read_list = malloc(sizeof(char*) * 32); command_stream->current->write_list = malloc(sizeof(char*) * 32); command_stream->current->dependencies = malloc(sizeof(struct command_node*)*20); command_stream->current->num_dependencies = 0; populate_read_list(command_stream->current, command_stream->current->command, &read_array_len, &write_array_len); /* if(command_stream->current->command->input != NULL) { command_stream->current->read_list[0] = malloc(sizeof(char) * 100); read_array_len++; strcpy(command_stream->current->read_list[0], command_stream->current->command->input); } if(command_stream->current->command->output != NULL) { command_stream->current->write_list[0] = malloc(sizeof(char) * 100); strcpy(command_stream->current->write_list[0], command_stream->current->command->output); } // index starts at 1 so we don't include the command name int iterator = 1; while(command_stream->current->command->u.word[iterator]!=NULL) { command_stream->current->read_list[read_array_len]=malloc(sizeof(char*) * 40); if(command_stream->current->command->type == SIMPLE_COMMAND) strcpy(command_stream->current->read_list[read_array_len],command_stream->current->command->u.word[iterator]); iterator++; read_array_len++; } */ inner_current = command_stream->head; while (inner_current != command_stream->current) { if ((dependency_exists(inner_current->read_list, command_stream->current->write_list)) || // "possible RAW data race" (dependency_exists(inner_current->write_list, command_stream->current->write_list)) || // "possible WAR data race" (dependency_exists(inner_current->write_list, command_stream->current->read_list))) // "possible WAW data race" { // add pointer to inner_current in current's dependencies // printf("hi\n"); command_stream->current->dependencies[command_stream->current->num_dependencies] = inner_current; // printf("Yes\n"); command_stream->current->num_dependencies++; } inner_current = inner_current->next; } /*printf("the readlist is ================\n"); int haha = 0 ; for(haha; haha<read_array_len; haha++) printf("%s\n",command_stream->current->read_list[haha]); printf("the writelist is ================\n"); for(haha = 0; haha<write_array_len; haha++) printf("%s\n",command_stream->current->write_list[haha]); printf("dependency: %d\n", dependency_exists(command_stream->current->read_list, command_stream->current->write_list));*/ command_stream->current = command_stream->current->next; } command_stream->current = command_stream->head; // loop through while (command_stream->current != NULL) { // printf("the command is %s\n",command_stream->current->command->u.word[1]); // printf("child_pid is %d\n",child_pid ); //pid_t child_pid = fork(); // if(child_pid == 0) //child //{ int ind = 0; int ind2 = 0; int num_depp = command_stream->current->num_dependencies; //check if dependencies have been started for(ind; ind<num_depp; ind++) { if(command_stream->current->dependencies[ind]->pid == -1) { ind--; } } //check if dependencies are done int eStatus; for(ind2; ind2< num_depp; ind2++) { waitpid(command_stream->current->dependencies[ind2]->pid, &eStatus,0); command_stream->current->num_dependencies--; } // } pid_t child_pid = fork(); if(child_pid == 0) { // printf("command_stream is %s\n", command_stream->current->command->u.word[1]); execute_command(command_stream->current->command, 1); _exit(0); } else //parent { command_stream->current->pid = child_pid; } command_stream->current = command_stream->current->next; } command_stream->current = command_stream->head; while (command_stream->current != NULL) { int exit_status = 0; waitpid(command_stream->current->pid, &exit_status, 0); command_stream->current->command->status=WEXITSTATUS(exit_status); command_stream->current = command_stream->current->next; } last_command = command_stream->tail->command; } //end of no print_tree else if (print_tree) { while ((command = read_command_stream (command_stream))) { printf ("# %d\n", command_number++); print_command (command); } } // end of print tree else { while ((command = read_command_stream (command_stream))) { last_command = command; execute_command(command, time_travel); } } /* while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command, time_travel); } }*/ return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { total_cmd = 0; total_file = 0; int command_number = 1; bool print_tree = false; bool time_travel = false; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = true; break; case 't': time_travel = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; if(!time_travel){ while ((command = read_command_stream (command_stream))) { if (print_tree){ printf ("# %d\n", command_number++); print_command (command); }else{ last_command = command; execute_command (command, time_travel); } } }else{ int i; //list for child process id. Each index represent the corresponding complete command // -1: executed & one // 0: hasn't been executed // >0: child executing pid_t *cpid_list = (pid_t*) checked_malloc(sizeof(pid_t) * total_cmd); for(i=0; i<total_cmd; i++){ cpid_list[i] = 0; } bool done = false; //while there are still commands waiting to be run while(!done){ cc_node_t temp_cmd = get_root(command_stream); int c_count = 0; while(temp_cmd){ pid_t cpid; //fork and execute commands with no dependency problem and that the command hasn't been run if(cpid_list[c_count] ==0 && no_dependency(c_count)){ cpid = fork(); //child execute the command if(cpid == 0){ execute_command (temp_cmd->c, time_travel); exit(0); //child exit }else if(cpid >0){ //parent add child pid to the global array cpid_list[c_count] = cpid; }else{ error(1, 0, "Forking process failed"); } } temp_cmd = temp_cmd->next; c_count++; } //wait for child for(i=0; i<total_cmd; i++){ //if the child process is runnign the ith command if(cpid_list[i] >0){ int status; waitpid(cpid_list[i], &status, 0); //update the dependency list (remove the ith row or something) update_dependency(i); //set the cpid to -1 cpid_list[i] = -1; } } //if there are no more command done = true; for(i=0; i<total_cmd; i++){ if(cpid_list[i]==0){ done = false; } } } free(cpid_list); while ((command = read_command_stream (command_stream))){} //deallocate dependcy lists int k; for(k=0; k<total_cmd; k++){ free(depend_list[k]); } free(depend_list); free(file_list); } return print_tree || !last_command ? 0 : command_status (last_command); }
command_stream_t main() { char* script_name = "test.sh"; FILE *script_stream = fopen (script_name, "r"); int buffer_size=1000; char* buffer = (char* )malloc(buffer_size); size_t count = 0; char ch; do { ch = get_next_byte(script_stream); if (ch == '#') // for comments: ignore everything until '\n' or EOF { ch = get_next_byte(script_stream); while(ch != EOF && ch!='\n') ch = get_next_byte(script_stream); } //skip all the words until \n if (ch!= -1) { // load into buffer buffer[count] = ch; count++; // expand buffer if necessary //if (count == INT_MAX) //{ // error(1, 0, "Line -1: Input size over INT_MAX."); //} if (count == buffer_size) { buffer_size = buffer_size * 2; buffer = (char*)realloc (buffer, buffer_size); } } } while(ch != -1); //token_stream_t return_point =convert_buffer_to_token_stream(buffer, count); //printf("convert end\n"); token_stream_t return_token_stream = convert_buffer_to_token_stream(buffer, count); /* if(return_token_stream==NULL) printf("NULL pointer\n"); else { if(return_token_stream->head == NULL) printf("NULL head\n"); else { printf("head is not null\n" ); printf("%d\n",return_token_stream->head->t_type); printf("%c\n",return_token_stream->head->next->text[0]); if(return_token_stream->head->next->next != NULL) { printf("linked"); printf("%d\n",return_token_stream->head->next->next->t_type); } } } if(return_token_stream->next==NULL) printf("NEXT is NULL pointer\n"); else { if(return_token_stream->next->head == NULL) printf("NEXT NULL head\n"); else { printf("head is not null\n" ); printf("%d\n",return_token_stream->next->head->t_type); printf("%c\n",return_token_stream->next->head->next->text[0]); if(return_token_stream->next->head->next->next->next == NULL) printf("YEAH"); } } printf("main end");*/ command_t new_command_t = make_command_tree (return_token_stream); //printf("root: %d\n", new_command_t->type); //printf("left: %d\n", new_command_t->u.command[0]->type); //printf("right: %d\n", new_command_t->u.command[1]->type); printf("make tree end\n"); add_command_to_stream(new_command_t ); printf("add first command to stream\n"); printf("command type : %d\n", new_command_t->type); if(new_command_t == NULL) printf("HAHA"); print_command(new_command_t); while (return_token_stream->next != NULL) { //make return_point point to next token stream return_token_stream = return_token_stream->next; //free(return_point); //return_token_stream = temp; new_command_t = make_command_tree (return_token_stream); add_command_to_stream(new_command_t); printf("add another command to stream\n"); print_command(new_command_t); } printf("return to main\n"); // print_command() return command_stream_head; }
int main(int argc, char *argv[]) { command_t *cmd; mem_region_t *mr; target_context_t *tc; #if !defined(WIN32) cmd = get_commands(argc, argv); if(cmd == NULL) return -1; #endif /* open raw ethernet socket if desired, then drop root */ #if !defined(WIN32) if (opt_ethernet) eth_raw_socket(); #endif drop_root(); /* miscellaneous initialization */ init_crc32(); /* open a connection to the target */ if (!(tc = target_open(opt_port, opt_ethernet ? opt_netif : NULL))){ panic("couldn't open connection to target"); return -1; } while (cmd) { command_t *tmp; if (opt_verbose) print_command(cmd); switch (cmd->type) { #ifdef AJ_FIRMUPDATE_SUPPORT case CMD_FIRMUPDATE: mr = slurp_file_or_die(cmd->input_path); if(require_flags(tc, REQ_MEM_MAP) == -1){ return -1; } firmupdate(tc, mr); break; #endif case CMD_DOWNLOAD: if (cmd->region && cmd->info.download.have_address){ panic("can't specify both region and address"); return -1; } mr = slurp_file_or_die(cmd->input_path); if(require_flags(tc, REQ_MEM_MAP) == -1){ return -1; } if (cmd->region) download_to_region(tc, mr, cmd->region); else if (cmd->info.download.have_address) download_to_addr(tc, mr, cmd->addr); else{ warn("download: must specify address or region\n"); return -1; } break; case CMD_ERASE: if (cmd->region && cmd->info.download.have_address){ panic("can't specify both region and address"); return -1; } if(require_flags(tc, REQ_MEM_MAP) == -1){ return -1; } if (cmd->region) erase_region(tc, cmd->region); else if (cmd->info.download.have_address) erase_addr(tc, cmd->addr); else{ warn("erase: must specify address or region\n"); return -1; } break; case CMD_MAP: if(require_flags(tc, REQ_MEM_MAP) == -1){ return -1; } region_print(tc->memmap); return 0; case CMD_TERMINAL: if(restore_interactive(tc) == -1){ return -1; } #if !defined(WIN32) serial_terminal(); #endif return 0; case CMD_MD5SUM: if (cmd->region && cmd->info.download.have_address){ panic("md5sum: can't specify both region and address"); return -1; } if (cmd->size == 0) { warn("md5sum: must specify size\n"); return -1; } if (cmd->region){ if(require_flags(tc, REQ_MEM_MAP) == -1){ return -1; } md5sum_region(tc, cmd->region, cmd->size); } else if (cmd->info.download.have_address) { md5sum_addr(tc, cmd->addr, cmd->size); } else{ warn("md5sum: must specify address or region\n"); return -1; } break; default: warn("unsupported command\n"); return -1; } tmp = cmd; cmd = cmd->next; /* * We don't free paths in the command because we don't * know whether or not they're dynamically allocated. * Thus we leak a little memory. It's not like this * is a long-running program... */ free(tmp); } restore_interactive(tc); return 0; }
int main (int argc, char **argv) { int opt; int command_number = 1; int print_tree = 0; int time_travel = 0; int limit_process=0; int limit=-1; int make=1; program_name = argv[0]; int c; while ((c = getopt (argc, argv, "ptn:")) != -1) { switch (c) { case 'p': print_tree = 1; break; //case 'm': print_tree = 1; make=0; break; case 'n': limit_process=1; limit=atoi(optarg); if (limit<=0) //process limit must be positive usage(); break; case 't': time_travel = 1; break; default: printf("opt parsing error:\n"); usage (); break; } } //prints for debugging bash command argument parsing printf("p: %d n: %d t: %d\nlimit: %d\n", print_tree, limit_process, time_travel, limit); /* int index; for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]); */ // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); /* //used for debugging make_command_stream if (make) command_stream=make_command_stream (get_next_byte, script_stream); else { command_stream=(command_stream_t) malloc(sizeof command_stream); char str[]=SCRIPT; memcpy(command_stream->stream,str,strlen(str)); command_stream->index=0; } */ command_t last_command = NULL; command_t command; command_t* commandArr = (command_t*)checked_malloc(sizeof(command_t)); int commandArrSize = 0; //build the command array while ((command = read_command_stream (command_stream))) { commandArr[commandArrSize] = command; commandArrSize++; commandArr = (command_t*)checked_realloc(commandArr, sizeof(command_t)*(commandArrSize+1)); } commandArr[commandArrSize] = NULL; //print, execute normally or execute time travel int i = 0; if (print_tree) { while(commandArr[i]) { printf ("# %d\n", command_number++); print_command (commandArr[i]); i++; } } else { execute_general(commandArr,commandArrSize, last_command, time_travel, limit_process, limit); } free(command_stream); free(commandArr); return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { int opt; int command_number = 1; int print_tree = 0; int time_travel = 0; int limit_processes = false; int num_processes = -1; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = 1; break; case 't': time_travel = 1; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one or two file argument. if (optind != argc - 1 && optind != argc - 2) usage (); script_name = argv[optind]; //there are two arguments if (optind == argc - 2) { limit_processes = true; parse_ssize(argv[optind+1], &num_processes); } if (limit_processes && num_processes > 0) { printf("Running with %i processes\n", num_processes); update_subprocess_limit(num_processes); } else if (limit_processes && num_processes == 0) { error (1, errno, "Cannot run with 0 processes"); } else { printf("Running with unlimited processes\n"); update_subprocess_limit(-1); } FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; //If time travel option is set make dependency graph int** graph; if(time_travel) { execute_command_time_travel(command_stream); return 1; } else { int processes_needed_count = 0; while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command, time_travel); } } return print_tree || !last_command ? 0 : command_status (last_command); } }
int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; bool time_travel = false; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = true; break; case 't': time_travel = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; if(time_travel) { size_t command_buf = 16; command_t* command_array = NULL; command_array = (command_t*) checked_malloc(command_buf*sizeof(command_t)); int command_count = 0; int i; int previous_command = 0; for (command_count = 0; (command = read_command_stream (command_stream)); command_count++) { if (command_count == (int)command_buf) command_array = (command_t*) checked_grow_alloc(command_array, &command_buf); command_array[command_count] = command; } bool dependency_matrix[command_count][command_count]; int j; int k; bool run_command; pid_t pid; int fd[command_count][2]; int retval; fd_set set; int buf; int m; int z; int read_return; for (j=0; j < command_count; j++) for (k = 0; k < command_count; k++) dependency_matrix[j][k] = false; //Iterating through all commands for (i=0; i < command_count; i++) { //Iterating through all commands preceding it for (previous_command = i-1; previous_command >= 0; previous_command--) { //Iterating through the output files of the current command for (j=0; j < command_array[i]->top_outputs_count; j++) { //Iterating through the input files of the preceding command for (k=0; k < command_array[previous_command]->top_inputs_count; k++) { if (strcmp(command_array[i]->top_outputs[j], command_array[previous_command]->top_inputs[k]) == 0) { dependency_matrix[i][previous_command] = true; break; } } //Iterating through the output files of the preceding command for (k=0; k < command_array[previous_command]->top_outputs_count; k++) { if (strcmp(command_array[i]->top_outputs[j], command_array[previous_command]->top_outputs[k]) == 0) { dependency_matrix[i][previous_command] = true; break; } } } //Iterating through the input files of the current command for (j=0; j < command_array[i]->top_inputs_count; j++) { //Iterating through the input files of the preceding command for (k=0; k < command_array[previous_command]->top_inputs_count; k++) { if (strcmp(command_array[i]->top_inputs[j], command_array[previous_command]->top_inputs[k]) == 0) { dependency_matrix[i][previous_command] = false; break; } } //Iterating through the output files of the preceding command for (k=0; k < command_array[previous_command]->top_outputs_count; k++) { if (strcmp(command_array[i]->top_inputs[j], command_array[previous_command]->top_outputs[k]) == 0) { dependency_matrix[i][previous_command] = true; break; } } } } } for(z=0; z < command_count; z++) fd[z][0] = 0; bool done = false; while(!done) { for (i=0; i < command_count; i++) { run_command = true; for (k=0; k < command_count; k++) { if (dependency_matrix[i][k]) { run_command = false; break; } } if (run_command) { dependency_matrix[i][i] = true; if (pipe(fd[i]) != 0) { fprintf(stderr, "Failed to initialize pipe\n"); exit(1); } //Child process pid = fork(); if(pid == 0) { close(fd[i][0]); execute_command (command_array[i], false); //Setup pipe for writing from child to parent m = i; // fprintf(stderr, "write#:%i\n", m); write(fd[i][1], &m, sizeof(int)); close(fd[i][1]); exit(command_array[i]->status); } //Parent Process else if (pid > 0) { close(fd[i][1]); continue; } else { fprintf(stderr, "Failed to fork\n"); exit(1); } } } FD_ZERO(&set); for(z=0; z < command_count; z++) { if(fd[z][0] != 0) { FD_SET(fd[z][0], &set); //fprintf(stderr, "set\n"); } } retval = select(FD_SETSIZE, &set, NULL, NULL, NULL); if(retval == -1) { fprintf(stderr, "Error monitoring pipes"); exit(1); } for(z=0; z < command_count; z++) { if(fd[z][0] != 0) { //fprintf(stderr, "fjieowajfoew\n"); read_return = read(fd[z][0], &buf, sizeof(int)); if(read_return > 0) { //fprintf(stderr, "Read: %i\n", buf); for(k=0; k < command_count; k++) dependency_matrix[k][buf] = false; //Set child command dependent to itself to indicate that it has already successfully executed dependency_matrix[buf][buf] = true; //close(fd[z][0]); } else if(read_return == -1) { fprintf(stderr, "Error reading\n"); exit(1); } } } for(z=0; z < command_count; z++) { if(!dependency_matrix[z][z]) break; } if(z == command_count) done = true; // FD_ZERO(&set); // FD_SET(fd[0], &set); //When this returns, data has been detected from pipe // retval = select(FD_SETSIZE, &set, NULL, NULL, NULL); // fprintf(stderr, "Select read:%i\n", retval); /* while (read(fd[0], &buf, sizeof(int)) > 0) { fprintf(stderr, "Going through while loop with buf:%i \n", buf); for(k=0; k < command_count; k++) dependency_matrix[k][buf] = false; //Set child command dependent to itself to indicate that it has already successfully executed dependency_matrix[buf][buf] = true; } fprintf(stderr, "z:%i\n", z); */ /* int read_return; while((read_return = read(fd[0], &buf, sizeof(int))) <= 0) { if (read_return == -1) { fprintf(stderr, "Error:%i\n", errno); fprintf(stderr, "Error reading\n"); exit(1); } } fprintf(stderr, "Iteration:%i\nNum_read:%i\n", z, read_return); do { fprintf(stderr, "Bufval:%i\n", buf); for(k=0; k < command_count; k++) dependency_matrix[k][buf] = false; //Set child command dependent to itself to indicate that it has already successfully executed dependency_matrix[buf][buf] = true; } while(read(fd[0], &buf, sizeof(int)) > 0); close(fd[0]); */ } for(z=0; z < command_count; z++) close(fd[z][0]); while((pid = wait(NULL))) { if(pid == -1 && errno == ECHILD) break; else if(pid == -1) { fprintf(stderr, "Error waiting on children"); exit(1); } } /* for (i = 0; i < command_count; i++) { last_command = command_array[i]; execute_command(command_array[i], false); } */ } else { while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command, time_travel); } } } return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; bool time_travel = false; int **dependencies; int i, j, status, wait_val; pid_t* pids; bool can_run, finished; command_t* commands; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = true; break; case 't': time_travel = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); if (time_travel) { commands = command_stream->commands; dependencies = set_dependencies(commands, command_stream->num_commands); pids = (pid_t*) checked_malloc(sizeof(pid_t) * command_stream->num_commands); for (i = 0; i <command_stream->num_commands; i++) pids[i] = -1; } command_t last_command = NULL; command_t command; if (print_tree || !time_travel) { while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command); } } } else { for(;;) { finished = true; for (i = 0; i < command_stream->num_commands; i++) { if (pids[i] == -1) { can_run = true; for (j = 0; dependencies[i][j] != -1; j++) if (commands[dependencies[i][j]]->status == -1) { can_run = false; break; } if (can_run) { pids[i] = fork(); if (pids[i]== -1) error(1, errno, "Error forking process"); if (pids[i] == 0) { execute_command(commands[i]); exit(commands[i]->status); } } } } for (i = 0; i < command_stream->num_commands; i++) { if (pids[i] != -1) { wait_val = waitpid(pids[i], &status, WNOHANG); if (wait_val == 0) finished = false; else commands[i]->status = WEXITSTATUS(status); } else finished = false; } if (finished) { last_command = commands[command_stream->num_commands-1]; break; } } } free_stream(command_stream); if (time_travel) { free(pids); for (i = 0; i<command_stream->num_commands; i++) free(dependencies[i]); free(dependencies); } return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { int opt; int command_number = 1; int print_tree = 0; int time_travel = 0; int interactive = 0; program_name = argv[0]; depend_node_t depend_table = checked_malloc(sizeof(struct depend_node)); //Dependency Table for (;;) switch (getopt (argc, argv, "pti")) { case 'p': print_tree = 1; break; case 't': time_travel = 1; break; case 'i': interactive = 1; break; // Interactive Shell default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (!interactive && optind != argc - 1) usage (); if (interactive) { printf("*******Interactive Shell*******\n"); ishell(); return 0; } script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); command_t last_command = NULL; command_t command; while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { if (last_command !=NULL) free(last_command); last_command = command; execute_command (command, time_travel, depend_table); } } // Deallocate the dependency table if (time_travel) // time_travel { depend_node_t temp = depend_table; depend_table = depend_table->nxt; free(temp); while (depend_table != NULL) { temp = depend_table; depend_table = depend_table->nxt; free(temp->handle); free(temp); } } return print_tree || !last_command ? 0 : command_status (last_command); }
static int internal_command(unsigned char target, unsigned char lun, const void *cmnd, void *buff, int bufflen, int reselect) { int len = 0; unsigned char *data = NULL; struct scatterlist *buffer = NULL; int nobuffs = 0; int clock; int temp; #ifdef SLOW_HANDSHAKE int borken; /* Does the current target require Very Slow I/O ? */ #endif #if (DEBUG & PHASE_DATAIN) || (DEBUG & PHASE_DATOUT) int transfered = 0; #endif #if (((DEBUG & PHASE_ETC) == PHASE_ETC) || (DEBUG & PRINT_COMMAND) || \ (DEBUG & PHASE_EXIT)) int i; #endif #if ((DEBUG & PHASE_ETC) == PHASE_ETC) int phase=0, newphase; #endif int done = 0; unsigned char status = 0; unsigned char message = 0; register unsigned char status_read; unsigned transfersize = 0, underflow = 0; incommand = 0; st0x_aborted = 0; #ifdef SLOW_HANDSHAKE borken = (int) SCint->device->borken; #endif #if (DEBUG & PRINT_COMMAND) printk ("scsi%d : target = %d, command = ", hostno, target); print_command((unsigned char *) cmnd); printk("\n"); #endif #if (DEBUG & PHASE_RESELECT) switch (reselect) { case RECONNECT_NOW : printk("scsi%d : reconnecting\n", hostno); break; #ifdef LINKED case LINKED_RIGHT : printk("scsi%d : connected, can reconnect\n", hostno); break; case LINKED_WRONG : printk("scsi%d : connected to wrong target, can reconnect\n", hostno); break; #endif case CAN_RECONNECT : printk("scsi%d : allowed to reconnect\n", hostno); break; default : printk("scsi%d : not allowed to reconnect\n", hostno); } #endif if (target == (controller_type == SEAGATE ? 7 : 6)) return DID_BAD_TARGET; /* * We work it differently depending on if this is "the first time," * or a reconnect. If this is a reselect phase, then SEL will * be asserted, and we must skip selection / arbitration phases. */ switch (reselect) { case RECONNECT_NOW: #if (DEBUG & PHASE_RESELECT) printk("scsi%d : phase RESELECT \n", hostno); #endif /* * At this point, we should find the logical or of our ID and the original * target's ID on the BUS, with BSY, SEL, and I/O signals asserted. * * After ARBITRATION phase is completed, only SEL, BSY, and the * target ID are asserted. A valid initiator ID is not on the bus * until IO is asserted, so we must wait for that. */ clock = jiffies + 10; for (;;) { temp = STATUS; if ((temp & STAT_IO) && !(temp & STAT_BSY)) break; if (jiffies > clock) { #if (DEBUG & PHASE_RESELECT) printk("scsi%d : RESELECT timed out while waiting for IO .\n", hostno); #endif return (DID_BAD_INTR << 16); } } /* * After I/O is asserted by the target, we can read our ID and its * ID off of the BUS. */ if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) { #if (DEBUG & PHASE_RESELECT) printk("scsi%d : detected reconnect request to different target.\n" "\tData bus = %d\n", hostno, temp); #endif return (DID_BAD_INTR << 16); } if (!(temp & (1 << current_target))) { printk("scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp); return (DID_BAD_INTR << 16); } buffer=current_buffer; cmnd=current_cmnd; /* WDE add */ data=current_data; /* WDE add */ len=current_bufflen; /* WDE add */ nobuffs=current_nobuffs; /* * We have determined that we have been selected. At this point, * we must respond to the reselection by asserting BSY ourselves */ #if 1 CONTROL = (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY); #else CONTROL = (BASE_CMD | CMD_BSY); #endif /* * The target will drop SEL, and raise BSY, at which time we must drop * BSY. */ for (clock = jiffies + 10; (jiffies < clock) && (STATUS & STAT_SEL);); if (jiffies >= clock) { CONTROL = (BASE_CMD | CMD_INTR); #if (DEBUG & PHASE_RESELECT) printk("scsi%d : RESELECT timed out while waiting for SEL.\n", hostno); #endif return (DID_BAD_INTR << 16); } CONTROL = BASE_CMD; /* * At this point, we have connected with the target and can get * on with our lives. */ break; case CAN_RECONNECT: #ifdef LINKED /* * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff. * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT * message on MESSAGE OUT phase, and then loop back to here. */ connect_loop : #endif #if (DEBUG & PHASE_BUS_FREE) printk ("scsi%d : phase = BUS FREE \n", hostno); #endif /* * BUS FREE PHASE * * On entry, we make sure that the BUS is in a BUS FREE * phase, by insuring that both BSY and SEL are low for * at least one bus settle delay. Several reads help * eliminate wire glitch. */ clock = jiffies + ST0X_BUS_FREE_DELAY; #if !defined (ARBITRATE) while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && (jiffies < clock)); if (jiffies > clock) return retcode(DID_BUS_BUSY); else if (st0x_aborted) return retcode(st0x_aborted); #endif #if (DEBUG & PHASE_SELECTION) printk("scsi%d : phase = SELECTION\n", hostno); #endif clock = jiffies + ST0X_SELECTION_DELAY; /* * Arbitration/selection procedure : * 1. Disable drivers * 2. Write HOST adapter address bit * 3. Set start arbitration. * 4. We get either ARBITRATION COMPLETE or SELECT at this * point. * 5. OR our ID and targets on bus. * 6. Enable SCSI drivers and asserted SEL and ATTN */ #if defined(ARBITRATE) cli(); CONTROL = 0; DATA = (controller_type == SEAGATE) ? 0x80 : 0x40; CONTROL = CMD_START_ARB; sti(); while (!((status_read = STATUS) & (STAT_ARB_CMPL | STAT_SEL)) && (jiffies < clock) && !st0x_aborted); if (!(status_read & STAT_ARB_CMPL)) { #if (DEBUG & PHASE_SELECTION) if (status_read & STAT_SEL) printk("scsi%d : arbitration lost\n", hostno); else printk("scsi%d : arbitration timeout.\n", hostno); #endif CONTROL = BASE_CMD; return retcode(DID_NO_CONNECT); }; #if (DEBUG & PHASE_SELECTION) printk("scsi%d : arbitration complete\n", hostno); #endif #endif /* * When the SCSI device decides that we're gawking at it, it will * respond by asserting BUSY on the bus. * * Note : the Seagate ST-01/02 product manual says that we should * twiddle the DATA register before the control register. However, * this does not work reliably so we do it the other way around. * * Probably could be a problem with arbitration too, we really should * try this with a SCSI protocol or logic analyzer to see what is * going on. */ cli(); DATA = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40)); CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0); sti(); while (!((status_read = STATUS) & STAT_BSY) && (jiffies < clock) && !st0x_aborted) #if 0 && (DEBUG & PHASE_SELECTION) { temp = clock - jiffies; if (!(jiffies % 5)) printk("seagate_st0x_timeout : %d \r",temp); } printk("Done. \n"); printk("scsi%d : status = %02x, seagate_st0x_timeout = %d, aborted = %02x \n", hostno, status_read, temp, st0x_aborted); #else ; #endif if ((jiffies >= clock) && !(status_read & STAT_BSY)) { #if (DEBUG & PHASE_SELECTION) printk ("scsi%d : NO CONNECT with target %d, status = %x \n", hostno, target, STATUS); #endif return retcode(DID_NO_CONNECT); } /* * If we have been aborted, and we have a command in progress, IE the * target still has BSY asserted, then we will reset the bus, and * notify the midlevel driver to expect sense. */ if (st0x_aborted) { CONTROL = BASE_CMD; if (STATUS & STAT_BSY) { printk("scsi%d : BST asserted after we've been aborted.\n", hostno); seagate_st0x_reset(NULL, 0); return retcode(DID_RESET); } return retcode(st0x_aborted); } /* Establish current pointers. Take into account scatter / gather */ if ((nobuffs = SCint->use_sg)) { #if (DEBUG & DEBUG_SG) { int i; printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs); for (i = 0; i < nobuffs; ++i) printk("scsi%d : buffer %d address = %08x length = %d\n", hostno, i, buffer[i].address, buffer[i].length); } #endif buffer = (struct scatterlist *) SCint->buffer; len = buffer->length; data = (unsigned char *) buffer->address; } else { #if (DEBUG & DEBUG_SG) printk("scsi%d : scatter gather not requested.\n", hostno); #endif buffer = NULL; len = SCint->request_bufflen; data = (unsigned char *) SCint->request_buffer; } #if (DEBUG & (PHASE_DATAIN | PHASE_DATAOUT)) printk("scsi%d : len = %d\n", hostno, len); #endif break; #ifdef LINKED case LINKED_RIGHT: break; case LINKED_WRONG: break; #endif } /* * There are several conditions under which we wish to send a message : * 1. When we are allowing disconnect / reconnect, and need to establish * the I_T_L nexus via an IDENTIFY with the DiscPriv bit set. * * 2. When we are doing linked commands, are have the wrong I_T_L nexus * established and want to send an ABORT message. */ CONTROL = BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT) #ifdef LINKED || (reselect == LINKED_WRONG) #endif ) ? CMD_ATTN : 0) ; /* * INFORMATION TRANSFER PHASE * * The nasty looking read / write inline assembler loops we use for * DATAIN and DATAOUT phases are approximately 4-5 times as fast as * the 'C' versions - since we're moving 1024 bytes of data, this * really adds up. */ #if ((DEBUG & PHASE_ETC) == PHASE_ETC) printk("scsi%d : phase = INFORMATION TRANSFER\n", hostno); #endif incommand = 1; transfersize = SCint->transfersize; underflow = SCint->underflow; /* * Now, we poll the device for status information, * and handle any requests it makes. Note that since we are unsure of * how much data will be flowing across the system, etc and cannot * make reasonable timeouts, that we will instead have the midlevel * driver handle any timeouts that occur in this phase. */ while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) { #ifdef PARITY if (status_read & STAT_PARITY) { printk("scsi%d : got parity error\n", hostno); st0x_aborted = DID_PARITY; } #endif if (status_read & STAT_REQ) { #if ((DEBUG & PHASE_ETC) == PHASE_ETC) if ((newphase = (status_read & REQ_MASK)) != phase) { phase = newphase; switch (phase) { case REQ_DATAOUT: printk("scsi%d : phase = DATA OUT\n", hostno); break; case REQ_DATAIN : printk("scsi%d : phase = DATA IN\n", hostno); break; case REQ_CMDOUT : printk("scsi%d : phase = COMMAND OUT\n", hostno); break; case REQ_STATIN : printk("scsi%d : phase = STATUS IN\n", hostno); break; case REQ_MSGOUT : printk("scsi%d : phase = MESSAGE OUT\n", hostno); break; case REQ_MSGIN : printk("scsi%d : phase = MESSAGE IN\n", hostno); break; default : printk("scsi%d : phase = UNKNOWN\n", hostno); st0x_aborted = DID_ERROR; } } #endif switch (status_read & REQ_MASK) { case REQ_DATAOUT : /* * If we are in fast mode, then we simply splat the data out * in word-sized chunks as fast as we can. */ #ifdef FAST if (!len) { #if 0 printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun); st0x_aborted = DID_ERROR; fast = 0; #endif break; } if (fast && transfersize && !(len % transfersize) && (len >= transfersize) #ifdef FAST32 && !(transfersize % 4) #endif ) { #if (DEBUG & DEBUG_FAST) printk("scsi%d : FAST transfer, underflow = %d, transfersize = %d\n" " len = %d, data = %08x\n", hostno, SCint->underflow, SCint->transfersize, len, data); #endif { #ifdef FAST32 unsigned int *iop = phys_to_virt (st0x_dr); const unsigned int *dp = (unsigned int *) data; int xferlen = transfersize >> 2; #else unsigned char *iop = phys_to_virt (st0x_dr); const unsigned char *dp = data; int xferlen = transfersize; #endif for (; xferlen; --xferlen) *iop = *dp++; } len -= transfersize; data += transfersize; #if (DEBUG & DEBUG_FAST) printk("scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data); #endif } else #endif { /* * We loop as long as we are in a data out phase, there is data to send, * and BSY is still active. */ while (len) { unsigned char stat; stat = STATUS; if (!(stat & STAT_BSY) || ((stat & REQ_MASK) != REQ_DATAOUT)) break; if (stat & STAT_REQ) { WRITE_DATA (*data++); --len; } } } if (!len && nobuffs) { --nobuffs; ++buffer; len = buffer->length; data = (unsigned char *) buffer->address; #if (DEBUG & DEBUG_SG) printk("scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data); #endif } break; case REQ_DATAIN : #ifdef SLOW_HANDSHAKE if (borken) { #if (DEBUG & (PHASE_DATAIN)) transfered += len; #endif for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) { *data++ = DATA; borken_wait(); } #if (DEBUG & (PHASE_DATAIN)) transfered -= len; #endif } else #endif #ifdef FAST if (fast && transfersize && !(len % transfersize) && (len >= transfersize) #ifdef FAST32 && !(transfersize % 4) #endif ) { #if (DEBUG & DEBUG_FAST) printk("scsi%d : FAST transfer, underflow = %d, transfersize = %d\n" " len = %d, data = %08x\n", hostno, SCint->underflow, SCint->transfersize, len, data); #endif { #ifdef FAST32 const unsigned int *iop = phys_to_virt (st0x_dr); unsigned int *dp = (unsigned int *) data; int xferlen = len >> 2; #else const unsigned char *iop = phys_to_virt (st0x_dr); unsigned char *dp = data; int xferlen = len; #endif for (; xferlen; --xferlen) *dp++ = *iop; } len -= transfersize; data += transfersize; #if (DEBUG & PHASE_DATAIN) printk("scsi%d: transfered += %d\n", hostno, transfersize); transfered += transfersize; #endif #if (DEBUG & DEBUG_FAST) printk("scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data); #endif } else #endif {
int main (int ac, char **av) { int k, ncount; double val, best; double startzeit; int tempcount, *templist; int *incycle = (int *) NULL, *outcycle = (int *) NULL; CCdatagroup dat; int rval = 0; CCrandstate rstate; int allow_dups; int use_gridsize; CCutil_printlabel (); CCutil_init_datagroup (&dat); rval = print_command (ac, av); CCcheck_rval (rval, "print_command failed"); seed = (int) CCutil_real_zeit (); if (parseargs (ac, av)) return 1; CCutil_sprand (seed, &rstate); printf ("Chained Lin-Kernighan with seed %d\n", seed); fflush (stdout); if ((!nnodes_want && !nodefile) || (tsplib_in && !nodefile)) { usage (av[0]); return 1; } startzeit = CCutil_zeit (); if (tsplib_in) { if (CCutil_gettsplib (nodefile, &ncount, &dat)) { fprintf (stderr, "could not read the TSPLIB file\n"); rval = 1; goto CLEANUP; } CCutil_dat_getnorm (&dat, &norm); } else { ncount = nnodes_want; if (gridsize < 0) { use_gridsize = -gridsize; allow_dups = 0; } else if (gridsize > 0) { use_gridsize = gridsize; allow_dups = 1; } else { use_gridsize = nnodes_want; allow_dups = 0; } if (CCutil_getdata (nodefile, binary_in, norm, &ncount, &dat, use_gridsize, allow_dups, &rstate)) { rval = 1; goto CLEANUP; } } if (in_repeater == -1) in_repeater = ncount; incycle = CC_SAFE_MALLOC (ncount, int); if (!incycle) { rval = 1; goto CLEANUP; } if (cycfname) { if (CCutil_getcycle (ncount, cycfname, incycle, binary_edges)) { fprintf (stderr, "CCutil_getcycle failed\n"); rval = 1; goto CLEANUP; } } else if (edgecycfname) { if (CCutil_getcycle_edgelist (ncount, edgecycfname, incycle, binary_edges)) { fprintf (stderr, "CCutil_getcycle_edgelist failed\n"); rval = 1; goto CLEANUP; } } if (goodfname) { int *templen = (int *) NULL; if (CCutil_getedgelist (ncount, goodfname, &tempcount, &templist, &templen, binary_edges)) { rval = 1; goto CLEANUP; } if (templen) CC_FREE (templen, int); printf ("Read good-edge file: %d edges\n", tempcount); fflush (stdout); } else if (edgegenfname) { CCedgegengroup plan; if (CCedgegen_read (edgegenfname, &plan)) { fprintf (stderr, "CCedgegen_read failed\n"); rval = 1; goto CLEANUP; } if (CCedgegen_edges (&plan, ncount, &dat, (double *) NULL, &tempcount, &templist, 0, &rstate)) { fprintf (stderr, "CCedgegen_edges failed\n"); rval = 1; goto CLEANUP; } } if ((norm & CC_NORM_BITS) == CC_KD_NORM_TYPE) { CCkdtree localkt; double kzeit = CCutil_zeit (); if ((!goodfname && !edgegenfname) || (!cycfname && !edgecycfname)) { if (CCkdtree_build (&localkt, ncount, &dat, (double *) NULL, &rstate)) { fprintf (stderr, "CCkdtree_build failed\n"); rval = 1; goto CLEANUP; } printf ("Time to build kdtree: %.2f\n", CCutil_zeit () - kzeit); fflush (stdout); if (!goodfname && !edgegenfname) { kzeit = CCutil_zeit (); if (nearnum) { if (CCkdtree_k_nearest (&localkt, ncount, nearnum, &dat, (double *) NULL, 1, &tempcount, &templist, run_silently, &rstate)) { fprintf (stderr, "CCkdtree_k_nearest failed\n"); rval = 1; goto CLEANUP; } if (!run_silently) { printf ("Time to find %d-nearest: %.2f\n", nearnum, CCutil_zeit () - kzeit); fflush (stdout); } } else { if (CCkdtree_quadrant_k_nearest (&localkt, ncount, quadtry, &dat, (double *) NULL, 1, &tempcount, &templist, run_silently, &rstate)) { fprintf (stderr, "CCkdtree-quad nearest code failed\n"); rval = 1; goto CLEANUP; } if (!run_silently) { printf ("Time to find quad %d-nearest: %.2f\n", quadtry, CCutil_zeit () - kzeit); fflush (stdout); } } } if (!cycfname && !edgecycfname) { kzeit = CCutil_zeit (); if (tour_type == LK_GREEDY) { if (CCkdtree_greedy_tour (&localkt, ncount, &dat, incycle, &val, run_silently, &rstate)) { fprintf (stderr, "CCkdtree greedy-tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_QBORUVKA) { if (CCkdtree_qboruvka_tour (&localkt, ncount, &dat, incycle, &val, &rstate)) { fprintf (stderr, "CCkdtree qboruvka-tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_BORUVKA) { if (CCkdtree_boruvka_tour (&localkt, ncount, &dat, incycle, &val, &rstate)) { fprintf (stderr, "CCkdtree boruvka-tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_RANDOM) { randcycle (ncount, incycle, &rstate); } else { if (CCkdtree_nearest_neighbor_tour (&localkt, ncount, CCutil_lprand (&rstate) % ncount, &dat, incycle, &val, &rstate)) { fprintf (stderr, "CCkdtree NN-tour failed\n"); rval = 1; goto CLEANUP; } } if (!run_silently) { printf ("Time to grow tour: %.2f\n", CCutil_zeit () - kzeit); fflush (stdout); } } CCkdtree_free (&localkt); } } else if ((norm & CC_NORM_BITS) == CC_X_NORM_TYPE) { double xzeit = CCutil_zeit (); if (!goodfname && !edgegenfname) { if (nearnum) { if (CCedgegen_x_k_nearest (ncount, nearnum, &dat, (double *) NULL, 1, &tempcount, &templist, run_silently)) { fprintf (stderr, "CCedgegen_x_k_nearest failed\n"); rval = 1; goto CLEANUP; } if (!run_silently) { printf ("Time to find %d-nearest: %.2f\n", nearnum, CCutil_zeit () - xzeit); fflush (stdout); } } else { if (CCedgegen_x_quadrant_k_nearest (ncount, quadtry, &dat, (double *) NULL, 1, &tempcount, &templist, run_silently)) { fprintf (stderr, "x-quad nearest code failed\n"); rval = 1; goto CLEANUP; } if (!run_silently) { printf ("Time to find quad %d-nearest: %.2f\n", quadtry, CCutil_zeit () - xzeit); fflush (stdout); } } } if (!cycfname && !edgecycfname) { xzeit = CCutil_zeit (); if (tour_type == LK_GREEDY) { if (CCedgegen_x_greedy_tour (ncount, &dat, incycle, &val, tempcount, templist, run_silently)) { fprintf (stderr, "CCedgegen_x_greedy_tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_QBORUVKA) { if (CCedgegen_x_qboruvka_tour (ncount, &dat, incycle, &val, tempcount, templist, run_silently)) { fprintf (stderr, "CCedgegen_x_qboruvka_tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_RANDOM) { randcycle (ncount, incycle, &rstate); } else { if (CCedgegen_x_nearest_neighbor_tour (ncount, CCutil_lprand (&rstate) % ncount, &dat, incycle, &val)) { fprintf (stderr, "CCedgegen_x_nearest_neighbor_tour failed\n"); rval = 1; goto CLEANUP; } } if (!run_silently) { printf ("Time to grow tour: %.2f\n", CCutil_zeit () - xzeit); fflush (stdout); } } } else { double jzeit = CCutil_zeit (); if (!goodfname && !edgegenfname) { if (!nearnum) nearnum = 4 * quadtry; if (CCedgegen_junk_k_nearest (ncount, nearnum, &dat, (double *) NULL, 1, &tempcount, &templist, run_silently)) { fprintf (stderr, "CCedgegen_junk_k_nearest failed\n"); rval = 1; goto CLEANUP; } if (!run_silently) { printf ("Time to find %d nearest: %.2f\n", nearnum, CCutil_zeit () - jzeit); fflush (stdout); } } if (!cycfname && !edgecycfname) { jzeit = CCutil_zeit(); if (tour_type == LK_GREEDY) { if (CCedgegen_junk_greedy_tour (ncount, &dat, incycle, &val, tempcount, templist, run_silently)) { fprintf (stderr, "CCedgegen_junk_greedy_tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_QBORUVKA) { if (CCedgegen_junk_qboruvka_tour (ncount, &dat, incycle, &val, tempcount, templist, run_silently)) { fprintf (stderr, "CCedgegen_junk_qboruvka_tour failed\n"); rval = 1; goto CLEANUP; } } else if (tour_type == LK_RANDOM) { randcycle (ncount, incycle, &rstate); } else { if (CCedgegen_junk_nearest_neighbor_tour (ncount, CCutil_lprand (&rstate) % ncount, &dat, incycle, &val, run_silently)) { fprintf (stderr, "CCedgegen_junk_nearest_neighbor_tour failed\n"); rval = 1; goto CLEANUP; } } if (!run_silently) { printf ("Time to grow tour: %.2f\n", CCutil_zeit () - jzeit); fflush (stdout); } } } outcycle = CC_SAFE_MALLOC (ncount, int); if (!outcycle) { rval = 1; goto CLEANUP; } if (number_runs) { k = 0; best = BIGDOUBLE; do { printf ("\nStarting Run %d\n", k); if (CClinkern_tour (ncount, &dat, tempcount, templist, 100000000, in_repeater, incycle, outcycle, &val, run_silently, time_bound, length_bound, (char *) NULL, kick_type, &rstate)) { fprintf (stderr, "CClinkern_tour failed\n"); rval = 1; goto CLEANUP; } if (val < best) { best = val; if (saveit_final) { if (CCutil_writecycle_edgelist (ncount, saveit_final, outcycle, &dat, binary_edges)) { fprintf (stderr, "could not write the cycle\n"); rval = 1; goto CLEANUP; } } } } while (++k < number_runs); printf ("Overall Best Cycle: %.0f\n", val); fflush (stdout); } else { double lkzeit = CCutil_zeit (); int attempt = 1; do { if (CClinkern_tour (ncount, &dat, tempcount, templist, 10000000, in_repeater, incycle, outcycle, &val, run_silently, time_bound, length_bound, saveit_name, kick_type, &rstate)) { fprintf (stderr, "CClinkern_tour failed\n"); rval = 1; goto CLEANUP; } if (length_bound != -1 && val > length_bound) { printf ("Cycle of value %.0f - did not reach %.0f\n", val, length_bound); printf ("Try again. Number of attempts: %d\n", ++attempt); } } while (length_bound != -1 && val > length_bound); if (saveit_final) { if (CCutil_writecycle_edgelist (ncount, saveit_final, outcycle, &dat, binary_edges)) { fprintf (stderr, "could not write the cycle\n"); rval = 1; goto CLEANUP; } } if (run_silently) printf ("Lin-Kernighan Running Time: %.2f\n", CCutil_zeit () - lkzeit); printf ("Final Cycle: %.0f\n", val); fflush (stdout); } printf ("Total Running Time: %.2f\n", CCutil_zeit () - startzeit); fflush (stdout); CLEANUP: #ifndef BIG_PROBLEM CC_IFFREE (templist, int); #endif CC_IFFREE (incycle, int); CC_IFFREE (outcycle, int); CCutil_freedatagroup (&dat); return rval; }
void main(void) { char ascii_buffer[17]; char fbyte, inchar; int i, j, k, pagenum, value, start, end; char linebuf[80], *p, *buf, ch; brdInit(); sfspi_init(); if (sf_init()) { printf("Flash init failed\n"); exit(-1); } else { printf("Flash init OK\n"); printf("# of blocks: %d\n", sf_blocks); printf("size of block: %d\n\n", sf_blocksize); } print_command(); while (1) { inchar = tolower(getchar()); if (inchar == 'c') { printf("page number to clear?"); pagenum = input_number(); if (pagenum >= 0 && pagenum < sf_blocks) { printf("\nClearing page %d\n", pagenum); memset(flash_buf, 0, sf_blocksize); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } // end if(inchar =='c') else if ((inchar == 'p') || (inchar == 'r')) { if (inchar == 'p') { // Use start for page to print printf("Page number to print out?"); start = input_number(); // Check that it is a valid page if (start < 0 || start >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } // Set single page range for 'p' command end = start; } else { printf("Starting page number to print out?"); start = input_number(); // Check that it is a valid page if (start < 0 || start >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } printf("\nEnding page number to print out?"); end = input_number(); if (end < start || end >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } } // Loop through range of pages (range of 1 page for 'p' command) for (pagenum = start; pagenum <= end; ++pagenum) { printf("\nPage %d", pagenum); sf_pageToRAM(pagenum); sf_readRAM(flash_buf, 0, sf_blocksize); // Test if entire buffer filled with a single value buf = flash_buf; for (j = k = 0, ch = *buf; j < 512; ++j) { if (ch != *buf++) { k = 1; break; } } // See if page is all the same value if (k) { printf("\n"); // No, drop through to print data } else { // Yes, print out message instead printf(" ALL = 0x%02x\n", ch); continue; } k = (sf_blocksize & 0xFFF0) + ((sf_blocksize & 0x000F) ? 16 : 0); ascii_buffer[16] = 0; for (j = 0, buf = flash_buf; j < k; ++j) { if (j % 16 == 0) { p = linebuf; p += sprintf (p, "%04x: ", j); } fbyte = *buf++; if (j >= sf_blocksize) { p += sprintf (p, " "); ascii_buffer[j % 16] = ' '; } else { p += sprintf (p, "%02x ", fbyte); ascii_buffer[j % 16] = isprint (fbyte) ? fbyte : '.'; } if (j % 16 == 15) { printf ("%s %s\n", linebuf, ascii_buffer); } } } } // end if((inchar =='p') || (inchar == 'r')) else if (inchar == 'f') { printf("page number to fill with specified value? "); pagenum = input_number(); if (pagenum >= 0 && pagenum < sf_blocks) { printf("\nPage %d\n", pagenum); printf("enter fill value "); value = input_number(); printf("\nValue is %d dec is %02x hex", value, value); printf("\nFilling page %d with value %02x hex\n", pagenum, value); memset(flash_buf, value, sf_blocksize); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } // end of if(inchar == 'f') else if (inchar == 't') { printf("page number in which to write text? "); pagenum = input_number(); if (pagenum >= 0 && pagenum < sf_blocks) { printf("\nPage %d\n", pagenum); printf("enter character string followed by RETURN \n"); gets(flash_buf); printf("Storing the following text ==> %s \n", flash_buf); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } // end of if(inchar == 't') print_command(); } // end of while }
int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; bool time_travel = false; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "pt")) { case 'p': print_tree = true; break; case 't': time_travel = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); fclose(script_stream); command_t last_command = NULL; command_t command; //hold last_command_status int last_command_status; if(print_tree) { while ((command = read_command_stream (command_stream))) { /*if (print_tree) { */ printf ("# %d\n", command_number++); print_command (command); //} /*else { last_command = command; execute_command (command, time_travel); }*/ } } else { last_command_status = execute_command(command_stream, time_travel); } free(command_stream->cmds); free(command_stream); //return print_tree || !last_command ? 0 : command_status (last_command); return print_tree || !last_command ? 0 : last_command_status; }