void exit_with_sig(int sig, command_t *comm_list) { /*Clean up before*/ destroy_commands(comm_list); printf("Shell exited with signal: %d\n", sig); exit(sig); }
int main( void ) { char *input = NULL; size_t read_count = 0; token_t *tokens; command_t *commands; int num_commands; /*Prompt user until interrupt, error or quit*/ while(1) { num_commands = 0; printf("> "); getline(&input, &read_count, stdin); input[strlen(input) - 1] = '\0'; /*Strip newline char*/ if(feof(stdin)) { exit_with_sig(0, NULL); } tokens = build_tokens(input); commands = build_commands(tokens, &num_commands); execute_comm_list(commands, num_commands); destroy_commands(commands); } }
/* * PURPOSE: The command line driven program does matrix creation, reading, writing, and other * miscellaneous operations. The program automatically creates a matrix and writes * that out called temp_mat (in binary do not use the cat command on it). You are * able to display any matrix by using the display command. You can create a new * blank matrix with the command create. To fill a matrix with random values use the * random command between a range of values. To get some experience with bit shifting * there is a command called shift. If you want to write and read in a matrix from * the filesystem use the respective read and write commands. To see memory operations * in action use the duplicate and equal commands. The others commands are sum and add. * To exit the program use the exit command. * INPUTS: No inputs needed * RETURN: Returns 0 on successful exectution otherwise -1 if there was an error **/ int main (int argc, char **argv) { srand(time(NULL)); char *line = NULL; Commands_t* cmd; Matrix_t *mats[10]; memset(&mats,0, sizeof(Matrix_t*) * 10); // IMPORTANT C FUNCTION TO LEARN Matrix_t *temp = NULL; // TODO ERROR CHECK if(!create_matrix (&temp,"temp_mat", 5, 5)) { return -1; } //TODO ERROR CHECK NEEDED if( (add_matrix_to_array(mats,temp, 10)) < 0) { return -1; } int mat_idx = find_matrix_given_name(mats,10,"temp_mat"); if (mat_idx < 0) { perror("PROGRAM FAILED TO INIT\n"); return -1; } random_matrix(mats[mat_idx], 10, 15); // TODO ERROR CHECK if(!write_matrix("temp_mat", mats[mat_idx])) { return -1; } line = readline("> "); while (strncmp(line,"exit", strlen("exit") + 1) != 0) { if (!parse_user_input(line,&cmd)) { printf("Failed at parsing command\n\n"); } if (cmd->num_cmds > 1) { run_commands(cmd,mats,10); } if (line) { free(line); } destroy_commands(&cmd); line = readline("> "); } free(line); destroy_remaining_heap_allocations(mats,10); return 0; }
int main (int argc, char **argv) { srand(time(NULL)); char *line = NULL; Commands_t* cmd; line = readline("> "); Matrix_t mats[10]; memset(&mats,0, sizeof(Matrix_t) * 10); while (strncmp(line,"exit", strlen("exit") + 1) != 0) { if (!parse_user_input(line,&cmd)) { printf("ERROR\n\n"); } run_commands(cmd,mats,10); free(line); destroy_commands(&cmd); line = readline("> "); } free(line); return 0; }
int main(int argc, const char **argv) { vector<const char*> args; Config_t config; char *config_file; int i; global_config = &config; //** Make the global point to what's loaded memset(global_config, 0, sizeof(Config_t)); //** init the data global_network = NULL; if (argc < 2) { printf("ibp_server [-d] config_file\n\n"); printf("-d - Run as a daemon\n"); printf("config_file - Configuration file\n"); return(0); } int astart = 1; int daemon = 0; if (strcmp(argv[astart], "-d") == 0) { daemon = 1; argv[astart] = ""; astart++; } config_file = (char *)argv[astart]; argv_to_vec(argc, argv, args); parse_config_options(args); // These are for EBOFS //*** Open the config file ***** printf("Config file: %s\n\n", config_file); GKeyFile *keyfile; GKeyFileFlags flags; GError *error = NULL; keyfile = g_key_file_new(); flags = G_KEY_FILE_NONE; /* Load the GKeyFile from disk or return. */ if (!g_key_file_load_from_file (keyfile, config_file, flags, &error)) { g_error (error->message); return(-1); } //** Parse the global options first *** parse_config(keyfile, &config); init_thread_slots(2*config.server.max_threads); //** Make pigeon holes dns_cache_init(1000); init_subnet_list(config.server.iface[0].hostname); //*** Install the commands: loads Vectable info and parses config options only **** install_commands(keyfile); g_key_file_free(keyfile); //Free the keyfile context set_starttime(); log_preamble(&config); configure_signals(); //** Setup the signal handlers //*** Set up the shutdown variables pthread_mutex_init(&shutdown_lock, NULL); pthread_mutex_unlock(&shutdown_lock); shutdown_now = 0; //*** Make the searchable version of the resources *** config.rl = create_resource_list(config.res, config.n_resources); // log_printf(0, "Looking up resource 2 and printing info.....\n") // print_resource(resource_lookup(config.rl, "2"), log_fd()); init_stats(config.server.stats_size); lock_alloc_init(); //***Launch as a daemon if needed*** if (args.size() == 2) { //*** Launch as a daemon *** if ((strcmp(config.server.logfile, "stdout") == 0) || (strcmp(config.server.logfile, "stderr") == 0)) { log_printf(0, "Can't launch as a daemom because log_file is either stdout or stderr\n"); log_printf(0, "Running in normal mode\n"); } else if (fork() == 0) { //** This is the daemon log_printf(0, "Running as a daemon.\n"); flush_log(); fclose(stdin); //** Need to close all the std* devices ** fclose(stdout); fclose(stderr); char fname[1024]; fname[1023] = '\0'; snprintf(fname, 1023, "%s.stdout", config.server.logfile); assert((stdout = fopen(fname, "w")) != NULL); snprintf(fname, 1023, "%s.stderr", config.server.logfile); assert((stderr = fopen(fname, "w")) != NULL); // stdout = stderr = log_fd(); //** and reassign them to the log device printf("ibp_server.c: STDOUT=STDERR=LOG_FD() dnoes not work!!!!!!!!!!!!!!!!!!!!!!!!\n"); } else { //** Parent exits exit(0); } } // test_alloc(); //** Used for testing allocation speed only //*** Initialize all command data structures. This is mainly 3rd party commands *** initialize_commands(); //** Launch the garbage collection threads for (i=0; i<config.n_resources; i++) launch_resource_cleanup_thread(&(config.res[i])); //*** Start the activity log *** alog_open(); server_loop(&config); //***** Main processing loop ****** //*** Shutdown the activity log *** alog_close(); //*** Destroy all the 3rd party structures *** destroy_commands(); lock_alloc_destroy(); destroy_thread_slots(); shutdown(&config); free_resource_list(config.rl); free_stats(); log_printf(0, "main: Completed shutdown. Exiting\n"); // close_log(); // close_debug(); }