/* Command line function. The only actual valid command line option is * -i, which creates a new database. If any other option is used (or if * an arg is passed), we print a usage message. */ static int command_mode(int argc, char *argv[]) { int c; int result = EXIT_SUCCESS; char *prog_name = argv[0]; // used by getopt extern char *optarg; extern int optind, opterr, optopt; while ((c = getopt(argc, argv, ":i")) != -1) { switch(c) { case 'i': if (!database_initialize(1)) { result = EXIT_FAILURE; fprintf(stderr, "Failed to initialize database\n"); break; case ':': case '?': default: fprintf(stderr, "Usage: %s [-i]\n", prog_name); result = EXIT_FAILURE; break; } } } return result; }
int main(int argc, char *argv[]) { char account_id[ACCOUNT_LEN+1]; menu_options_e current_option; /* Initian account_id. */ memset(account_id, '\0', sizeof(account_id)); current_option = mo_login; /* make sure current_option not equal mo_exit. */ announce(); database_initialize(); /* Now we wait for user to input command. */ while (current_option != mo_exit) { current_option = show_menu(account_id); switch(current_option) { case mo_new: create_new_account(); break; case mo_reset: reset_passwd(); break; case mo_loss: report_loss(); break; case mo_login: login(account_id); break; case mo_query: query(account_id); break; case mo_store: store_money(account_id); break; case mo_draw: draw_money(account_id); break; case mo_renewal: renewal(account_id); break; case mo_change_passwd: change_passwd(account_id); break; case mo_logout: logout(account_id); break; case mo_exit: case mo_invalid: default: break; } /* switch */ } /* while */ database_close(); exit(EXIT_SUCCESS); }
int main() { struct sigaction new_action, old_action; message_t mess_command; new_action.sa_handler = catch_signals; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if ((sigaction(SIGINT, &new_action, &old_action) != 0) || (sigaction(SIGHUP, &new_action, &old_action) != 0) || (sigaction(SIGTERM, &new_action, &old_action) != 0)) { fprintf(stderr, "Sever starup error, signal catching failed.\n"); exit(EXIT_FAILURE); } if (!database_initialize()) { fprintf(stderr, "Server error:-\ could not initialize database\n"); exit(EXIT_FAILURE); }
/* The main function loops over reading from the server mqueue (which is * actually done in the read_request_from_client function). Each time it * gets a request, it hands the request to process_command, which handles * - figuring out what operation the client wanted (the same switch * statement that is in main of app_ui.c, in effect) * - delegating to some frunction from cd_dbm.c * - coppying some of the data from the message_db_t struct coming in from * the client to the response, and then filling out other data depending * on the action and the database results * - sending the response message_db_t struct back to the client * * The functions that interact directly with the mqueue all live in mqueue_imp.c; * this module provides the main loop and the "glue" between the server-side * mqueue handling and the server-side data api. * * Note that the actual calls to the server-side data api from here wind up * being identical to the actual calls to the client-side data api (the proxy) * made from app_ui.c. */ int main(int argc, char *argv[]) { struct sigaction new_action, old_action; message_db_t mess_command; int database_init_type = 0; new_action.sa_handler = catch_signals; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if ((sigaction(SIGINT, &new_action, &old_action) != 0) || (sigaction(SIGHUP, &new_action, &old_action) != 0) || (sigaction(SIGTERM, &new_action, &old_action) != 0)) { fprintf(stderr, "Server startup error, signal catching failed\n"); exit(EXIT_FAILURE); } if (argc > 1) { argv++; if (strncmp("-i", *argv, 2) == 0) database_init_type = 1; } if (!database_initialize(database_init_type)) { fprintf(stderr, "Server error: could not initialize database\n"); exit(EXIT_FAILURE); } if (!server_starting()) exit(EXIT_FAILURE); while(server_running) { if (read_request_from_client(&mess_command)) { process_command(mess_command); } else { if(server_running) fprintf(stderr, "Server ended - can not \ read mqueue\n"); server_running = 0; } } /* while */ server_ending(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; // this block provides a command line api via `command_mode` if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } // if we get here, then we are in interactive mode.... // start with an announcement message announce(); // initialize the db. Using 0 here means open an exiting db (actually it // will create one, but it won't delete old data for you). To clear the // dbs and initialize a new one, use cmd line mode with -i. if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } // loop over an interactive console menu // the semantics are kind of similar to the ch6 menu, where there's // an "active" cd, and the operations you can do affect that cd while(current_option != mo_exit) { current_option = get_menu_choice(¤t_cdc_entry); switch(current_option) { case mo_add_cat: // add a new catalog item. Lots of error checking here. if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { // print message, but also make sure to zero out data fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: case mo_invalid: default: // in all three cases, do nothing break; } } // close db and exit database_close(); exit(EXIT_SUCCESS); } // end of main
void main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } /* We're now ready to process user input. We sit in a loop, asking for a menu choice and processing it, until the user selects the exit option. We pass the current_cdc_entry structure to the show_menu function. We do this to allow the menu choices to change if a catalog entry is currently selected. */ while(current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch(current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } /* switch */ } /* while */ /* When the main loop exits, we close the database and exit back to the environment. The welcoming sentence is printed by the announce function. */ database_close(); exit(EXIT_SUCCESS); } /* main */
int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); // if there's an arg, switch to command mode and exit if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } // print a greeting, and initialize the database (it must already exist) announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } /* loop over menu choices, calling show_menu every time. * the behavior of show_menu depends on whether a track is currently * selected (which is determined by the var `current_cdc_entry`) */ while(current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch(current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } /* switch */ } /* while */ // when the loop exits, clean up and quit database_close(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } while (current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch (current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } } database_close(); return 0; }