options() { /** change options... **/ /* return: * > 0 if restort was done - to indicate we might need to * change the page of our headers as a consequence * of the new sort order * < 0 if user entered 'x' to quit elm immediately * 0 otherwise */ int ch, resort = 0; char *strcpy(), temp[SLEN]; /* needed when an option is run through * expand_env(), because that function * is destructive of the original */ display_options(); clearerr(stdin); while(1) { ClearLine(LINES-4); Centerline(LINES-4, "Select first letter of option line, '>' to save, or 'i' to return to index."); PutLine0(LINES-2, 0, "Command: "); ch = ReadCh(); ch = tolower(ch); clear_error(); /* remove possible "sorting" message etc... */ one_liner(one_liner_for(ch)); switch (ch) { case 'c' : optionally_enter(raw_calendar_file, 2, 23, FALSE, FALSE); strcpy(temp, raw_calendar_file); expand_env(calendar_file, temp); break; case 'd' : optionally_enter(raw_pager, 3, 23, FALSE, FALSE); strcpy(temp, raw_pager); expand_env(pager, temp); clear_pages = (equal(pager, "builtin+") || equal(pager, "internal+")); break; case 'e' : optionally_enter(raw_editor, 4, 23, FALSE, FALSE); strcpy(temp, raw_editor); expand_env(editor, temp); break; case 'f' : optionally_enter(raw_folders, 5, 23, FALSE, FALSE); strcpy(temp, raw_folders); expand_env(folders, temp); break; case 's' : if(change_sort(6,23)) resort++; break; case 'o' : optionally_enter(raw_sentmail, 7, 23, FALSE, FALSE); strcpy(temp, raw_sentmail); expand_env(sent_mail, temp); break; case 'p' : optionally_enter(raw_printout, 8, 23, FALSE, FALSE); strcpy(temp, raw_printout); expand_env(printout, temp); break; case 'y' : optionally_enter(full_username, 9, 23, FALSE, FALSE); break; case 'a' : on_or_off(&arrow_cursor, 12, 23); break; case 'm' : on_or_off(&mini_menu, 13, 23); headers_per_page = LINES - (mini_menu ? 13 : 8); break; case 'u' : switch_user_level(&user_level,15, 23); break; case 'n' : on_or_off(&names_only, 16, 23); break; case '?' : options_help(); PutLine0(LINES-2,0,"Command: "); break; case '>' : printf("Save options in .elm/elmrc..."); fflush(stdout); save_options(); break; case 'x' : return(-1); /* exit elm */ case 'q' : /* pop back up to previous level, in this case == 'i' */ case 'i' : /* return to index screen */ return(resort ? 1 : 0); case ctrl('L'): display_options(); break; default: error("Command unknown!"); } } }
void options_usage(void) { options_help(); exit(1); }
int options_parse(int *_argc, char *argv[]) { int argc = *_argc; int i, j; /* Get command name */ options_command = strrchr(argv[0], '/'); if (options_command == NULL) { options_command = strrchr(argv[0], '\\'); } if (options_command == NULL) { options_command = argv[0]; } else { options_command++; } /* Get config file name */ char *conf_file = options_conf_file(argc, argv); //log_str("Config file: %s", conf_file); /* Parse options from config file */ options_conf_parse(conf_file); /* Parse options from command line */ i = 1; while (i < argc) { char *args = argv[i]; if (args[0] == '-') { options_entry_t *found = NULL; char *value = NULL; int stride = 0; options_entry_t *entry = (options_entry_t *) options_entries; while ((found == NULL) && (entry->long_opt != NULL)) { if (args[1] == '-') { char *eq = strchr(args+2, '='); char eq_c = '\0'; if (eq != NULL) { eq_c = *eq; *eq = '\0'; } if (strcmp(args+2, entry->long_opt) == 0) { found = entry; if (eq != NULL) { value = eq+1; } stride++; } if (eq != NULL) { *eq = eq_c; } } else { if ((args[1] != '\0') && (args[1] == entry->short_opt)) { found = entry; stride++; if (args[2] == '\0') { if (entry->value_symbol != NULL) { int i1 = i+1; if (i1 < argc) { value = argv[i1]; stride++; } else { options_help(); return -1; } } } else { if (entry->value_symbol != NULL) { value = &args[2]; } else { options_help(); return -1; } } } } entry++; } if (found != NULL) { switch (found->type) { case OPTIONS_TYPE_INT: if (value == NULL) { options_help(); return -1; } *((int *) found->value_ptr) = strtol(value, NULL, 0); break; case OPTIONS_TYPE_STRING: if (value == NULL) { options_help(); return -1; } *((char **) found->value_ptr) = strdup(value); break; default: *((int *) found->value_ptr) = 1; break; } argc -= stride; for (j = i; j <= argc; j++) { argv[j] = argv[j+stride]; } } else { options_help(); if ((strcmp(args, "--help") == 0) || (strcmp(args, "-h") == 0)) { return 0; } else { return -1; } } } else { break; } } *_argc = argc; return 0; }
int main( int argc, char **argv ) { int a, rc; int ec = 0; options_t options; modbus_t *ctx; uint8_t *tab_bit; uint16_t *tab_reg; if(argc > 1) { options_init(&options); for (a = 1; a < argc; a++) { options_execute(&options, argv[a], strlen(argv[a])+1); rc = options_finish(&options); DEBUG("argument: '%s' end state: %d\n",argv[a], rc); if (rc == -1) { options_err_disp(&options,argv[a]); ec = 1; goto exit; } } if(rc == 0) { fprintf(stderr, "Missing action argument\n"); ec = 1; goto exit; } if(options.action == _UNDEF) goto exit; // Options are valid options_dump(&options); ctx = modbus_init_con(&options); modbus_set_debug(ctx, options.debug); modbus_set_response_timeout(ctx, &options.timeout); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); ec = 1; goto destroy; } switch(options.action) { case _RC: case _RD: tab_bit = (uint8_t *) malloc(options.count * sizeof(uint8_t)); DBG_ASSERT(tab_bit != NULL, "Unable to allocate tab_bit!"); memset(tab_bit, 0, options.count * sizeof(uint8_t)); switch(options.action) { case _RC: rc = modbus_read_bits(ctx, options.address, options.count, tab_bit); break; case _RD: rc = modbus_read_input_bits(ctx, options.address, options.count, tab_bit); break; } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { display_8bit(&options, tab_bit); } free(tab_bit); break; case _RH: case _RI: tab_reg = (uint16_t *) malloc(options.count * sizeof(uint16_t)); DBG_ASSERT(tab_reg != NULL, "Unable to allocate tab_reg!"); switch(options.action) { case _RH: rc = modbus_read_registers(ctx, options.address, options.count, tab_reg); break; case _RI: rc = modbus_read_input_registers(ctx, options.address, options.count, tab_reg); break; } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { display_16bit(&options, tab_reg); } free(tab_reg); break; case _WC: if(options.count == 1) rc = modbus_write_bit(ctx, options.address, options.coil_values[0]); else { rc = modbus_write_bits(ctx, options.address, options.count, options.coil_values); } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { printf("Success\n"); } break; case _WH: if(options.count == 1) rc = modbus_write_register(ctx, options.address, options.reg_values[0]); else { rc = modbus_write_registers(ctx, options.address, options.count, options.reg_values); } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { printf("Success\n"); } break; default: DBG_ASSERT(0,"Unhandled action enum constant!"); } } else { options_help(); ec = 1; goto exit; } close: modbus_close(ctx); destroy: modbus_free(ctx); exit: return ec; }
// TODO: error handling // TODO: better exit point handling // (freeing resources for valgrind-cleanliness is too tedious at the moment) void options_parse(int argc, char * argv[], struct Options * options) { int opt; // modified when config is loaded on the command line config_t config; config_init(&config); set_default_config(&config); // only to used for -g: print default config config_t config_default; config_init(&config_default); set_default_config(&config_default); // -i flag should only print options after all other // command line arguments have been applied bool print_options = false; while (-1 != (opt = getopt(argc, argv, OPTSTR))) { switch (opt) { case 'c': config_set_auto_convert(&config, CONFIG_TRUE); if (CONFIG_FALSE == config_read_file(&config, optarg)) { // failure reporting fprintf(stderr, "Failed to use config file '%s'!\n", optarg); switch (config_error_type(&config)) { case CONFIG_ERR_NONE: fprintf(stderr, "\tno error reported\n" "\t(This might be a libconfig problem.)\n"); break; case CONFIG_ERR_FILE_IO: fprintf(stderr, "\tfile I/O error\n"); break; case CONFIG_ERR_PARSE: fprintf(stderr, "\tparse error on line %d:\n" "\t%s\n", config_error_line(&config), config_error_text(&config)); break; default: fprintf(stderr, "\tunknown error\n" "\t(A new libconfig version might have introduced" " new kinds of warnings.)\n"); } } break; case 'g': config_write(&config_default, stdout); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); exit(EXIT_SUCCESS); break; case 'i': print_options = true; break; default: /* '?' 'h' */ options_help(*argv); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); exit(EXIT_FAILURE); } } config2options(&config, options); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); if (print_options) options_print(stderr, "", options); }
void options ( uint8_t &defaultHeight , uint8_t &defaultWidth , uint8_t &defaultAlignWinSize , uint8_t &defaultAlignWinTotal , uint8_t &defaultAI_prof ) { // options menu uint8_t selected ( 1 ); uint8_t i; uint8_t row , col; uint8_t backupHeight ( defaultHeight ); uint8_t backupWidth ( defaultWidth ); uint8_t backupAlignWinSize ( defaultAlignWinSize ); uint8_t backupAlignWinTotal ( defaultAlignWinTotal ); uint8_t backupAI_prof ( defaultAI_prof ); int32_t Key; std::string STR_SELECTED; while ( true ) { getmaxyx ( stdscr , row , col ); if ( selected < 1 ) selected = 1; if ( selected > 5 ) selected = 5; if ( defaultHeight < 3 ) defaultHeight = 3; if ( defaultHeight > 20 ) defaultHeight = 20; if ( defaultWidth < 3 ) defaultWidth = 3; if ( defaultWidth > 20 ) defaultWidth = 20; if ( defaultAlignWinSize > defaultWidth ) defaultAlignWinSize = defaultWidth; if ( defaultAlignWinSize > defaultHeight ) defaultAlignWinSize = defaultHeight; if ( defaultAlignWinSize < 3 ) defaultAlignWinSize = 3; if ( defaultAlignWinTotal < 1 ) defaultAlignWinTotal = 1; if ( defaultAlignWinTotal > 10 ) defaultAlignWinTotal = 10; if ( defaultAI_prof < 3 ) defaultAI_prof = 9; if ( defaultAI_prof > 9 ) defaultAI_prof = 3; switch ( selected ) { case 1: STR_SELECTED = STR_HEIGHT; break; case 2: STR_SELECTED = STR_WIDTH; break; case 3: STR_SELECTED = STR_ALIGN_SIZE; break; case 4: STR_SELECTED = STR_ALIGN_TOTAL; break; case 5: STR_SELECTED = STR_AI_PROF; break; } clear (); attron ( A_BOLD ); mvprintw ( row / 4 , ( col - STR_TITLE_OPTIONS.length () + 1 ) / 2 , STR_TITLE_OPTIONS.c_str () ); mvprintw ( ( row / 2 ) + ( ( selected - 1 ) * 2 ) , col / 20 , STR_ARROW_RIGHT.c_str () ); mvprintw ( ( row / 2 ) + ( ( selected - 1 ) * 2 ) , 19 * col / 20 , STR_ARROW_LEFT.c_str () ); mvprintw ( 2 , 2 , STR_HELP.c_str () ); attroff ( A_BOLD ); mvprintw ( ( row / 2 ) , ( col - STR_HEIGHT.length() + 1 ) / 3 , STR_HEIGHT.c_str() ); mvprintw ( ( row / 2 ) + 2 , ( col - STR_WIDTH.length() + 1 ) / 3 , STR_WIDTH.c_str() ); mvprintw ( ( row / 2 ) + 4 , ( col - STR_ALIGN_SIZE.length() + 1 ) / 3 , STR_ALIGN_SIZE.c_str() ); mvprintw ( ( row / 2 ) + 6 , ( col - STR_ALIGN_TOTAL.length() + 1 ) / 3 , STR_ALIGN_TOTAL.c_str() ); mvprintw ( ( row / 2 ) + 8 , ( col - STR_AI_PROF.length () + 1 ) / 3 , STR_AI_PROF.c_str () ); mvprintw ( ( row / 2 ) , ( col / 2 ) , "%d" , defaultHeight ); mvprintw ( ( row / 2 ) , ( col / 2 ) + ( col / 20 ) , "[" ); for ( i = 0 ; i < defaultHeight ; ++i ) printw ( "-" ); for ( i = 20 ; i > defaultHeight ; --i ) printw ( " " ); printw ( "]" ); mvprintw ( ( row / 2 ) + 2 , ( col / 2 ) , "%d" , defaultWidth ); mvprintw ( ( row / 2 ) + 2 , ( col / 2 ) + ( col / 20 ) , "[" ); for ( i = 0 ; i < defaultWidth ; ++i ) printw ( "-" ); for ( i = 20 ; i > defaultWidth ; --i ) printw ( " " ); printw ( "]" ); mvprintw ( ( row / 2 ) + 4 , ( col / 2 ) , "%d" , defaultAlignWinSize ); mvprintw ( ( row / 2 ) + 4 , ( col / 2 ) + ( col / 20 ) , "[" ); for ( i = 0 ; i < defaultAlignWinSize ; ++i ) printw ( "-" ); for ( i = 20 ; i > defaultAlignWinSize ; --i ) printw ( " " ); printw ( "]" ); mvprintw ( ( row / 2 ) + 6 , ( col / 2 ) , "%d" , defaultAlignWinTotal ); mvprintw ( ( row / 2 ) + 6 , ( col / 2 ) + ( col / 20 ) , "[" ); for ( i = 0 ; i < defaultAlignWinTotal ; ++i ) printw ( "--" ); for ( i = 10 ; i > defaultAlignWinTotal ; --i ) printw ( " " ); printw ( "]" ); switch ( defaultAI_prof ) { case 3: mvprintw ( ( row / 2 ) + 8 , ( col / 2 ) + ( col / 20 ) + 9 , "EASY" ); break; case 6: mvprintw ( ( row / 2 ) + 8 , ( col / 2 ) + ( col / 20 ) + 8 , "MEDIUM" ); break; case 9: mvprintw ( ( row / 2 ) + 8 , ( col / 2 ) + ( col / 20 ) + 9 , "HARD" ); break; } mvprintw ( row - 2 , ( col - STR_OK.length () + 1 ) / 4 , STR_OK.c_str () ); mvprintw ( row - 2 , 3 * ( col - STR_CANCEL.length () + 1 ) / 4 , STR_CANCEL.c_str () ); refresh (); Key = getch (); if ( Key == KEY_LEFT ) { switch ( selected ) { case 1: defaultHeight--; break; case 2: defaultWidth--; break; case 3: defaultAlignWinSize--; break; case 4: defaultAlignWinTotal--; break; case 5: defaultAI_prof -= 3; break; } } if ( Key == KEY_RIGHT ) { switch ( selected ) { case 1: defaultHeight++; break; case 2: defaultWidth++; break; case 3: defaultAlignWinSize++; break; case 4: defaultAlignWinTotal++; break; case 5: defaultAI_prof += 3; break; } } if ( Key == KEY_DOWN ) selected++; if ( Key == KEY_UP ) selected--; if ( Key == ENTER || Key == KEY_O ) break; if ( Key == ESC || Key == KEY_C ) { defaultHeight = backupHeight; defaultWidth = backupWidth; defaultAlignWinSize = backupAlignWinSize; defaultAlignWinTotal = backupAlignWinTotal; defaultAI_prof = backupAI_prof; break; } if ( Key == KEY_F ( 5 ) ) options_help (); } }