int main(int argc, char** argv) { std::string line; int ret; if(argc == 1) { ret = interactive_mode(); } else if(argc == 2) { std::string filename(argv[1]); try { ret = file_input_mode(filename); } catch(bad_file_exception& e) { std::cerr << e.what() << std::endl; return 1; } } else { std::cerr << "bad number of arguments, please refer to documentation for the right arguments\n"; return 1; } return ret; /*big_num a("-1"); big_num b("2.1"); std::cout << a + b << std::endl; return 0;*/ }
/******************************************************************** * FUNCTION do_show_cli (sub-mode of local RPC) * * show CLI parms * * INPUTS: * server_cb == server control block to use *********************************************************************/ static void do_show_cli (server_cb_t *server_cb) { session_cb_t *session_cb = server_cb->cur_session_cb; val_value_t *mgrset; logfn_t logfn; boolean imode = interactive_mode(); if (imode) { logfn = log_stdout; } else { logfn = log_write; } mgrset = get_mgr_cli_valset(); /* CLI Parameters */ if (mgrset && val_child_cnt(mgrset)) { (*logfn)("\nCLI Variables\n"); val_dump_value_max(mgrset, 0,session_cb->defindent, (imode) ? DUMP_VAL_STDOUT : DUMP_VAL_LOG, session_cb->display_mode, FALSE, FALSE); (*logfn)("\n"); } else { (*logfn)("\nNo CLI variables\n"); } } /* do_show_cli */
/******************************************************************** * FUNCTION do_show_one_object (sub-mode of show objects local RPC) * * show objects: 1 of N * * INPUTS: * obj == object to show * mode == requested help mode * anyout == address of return anyout status * * OUTPUTS: * *anyout set to TRUE only if any suitable objects found * * RETURNS: * status *********************************************************************/ static status_t do_show_one_object (obj_template_t *obj, help_mode_t mode, boolean *anyout) { boolean imode; imode = interactive_mode(); if (obj_is_data_db(obj) && obj_has_name(obj) && !obj_is_hidden(obj) && !obj_is_abstract(obj)) { if (mode == HELP_MODE_BRIEF) { if (imode) { log_stdout("\n%s:%s", obj_get_mod_name(obj), obj_get_name(obj)); } else { log_write("\n%s:%s", obj_get_mod_name(obj), obj_get_name(obj)); } } else { obj_dump_template(obj, mode-1, 0, 0); } *anyout = TRUE; } return NO_ERR; } /* do_show_one_object */
/******************************************************************** * FUNCTION do_show_var (sub-mode of local RPC) * * show full info for one user var * * INPUTS: * server_cb == server control block to use * name == variable name to find * isglobal == TRUE if global var, FALSE if local var * isany == TRUE if don't care (global or local) * == FALSE to force local or global with 'isglobal' * mode == help mode requested * * RETURNS: * status *********************************************************************/ static status_t do_show_var (server_cb_t *server_cb, const xmlChar *name, var_type_t vartype, boolean isany, help_mode_t mode) { val_value_t *val; logfn_t logfn; boolean imode; imode = interactive_mode(); if (imode) { logfn = log_stdout; } else { logfn = log_write; } if (isany) { /* skipping VAR_TYP_SESSION for now */ val = var_get_local(server_cb->runstack_context, name); if (val) { vartype = VAR_TYP_LOCAL; } else { val = var_get(server_cb->runstack_context, name, VAR_TYP_GLOBAL); if (val) { vartype = VAR_TYP_GLOBAL; } else { val = var_get(server_cb->runstack_context, name, VAR_TYP_CONFIG); if (val) { vartype = VAR_TYP_CONFIG; } else { val = var_get(server_cb->runstack_context, name, VAR_TYP_SYSTEM); if (val) { vartype = VAR_TYP_SYSTEM; } } } } } else { val = var_get(server_cb->runstack_context, name, vartype); } if (val) { show_user_var(server_cb, name, vartype, val, mode); (*logfn)("\n"); } else { (*logfn)("\nVariable '%s' not found", name); return ERR_NCX_DEF_NOT_FOUND; } return NO_ERR; } /* do_show_var */
/******************************************************************** * FUNCTION do_show_modules (sub-mode of local RPC) * * show modules * * INPUTS: * server_cb == server control block to use * mode == requested help mode * * RETURNS: * status *********************************************************************/ static status_t do_show_modules (server_cb_t *server_cb, help_mode_t mode) { session_cb_t *session_cb = server_cb->cur_session_cb; ncx_module_t *mod; modptr_t *modptr; boolean imode = interactive_mode(); boolean anyout = FALSE; status_t res = NO_ERR; if (use_session_cb(session_cb)) { for (modptr = (modptr_t *)dlq_firstEntry(&session_cb->modptrQ); modptr != NULL && res == NO_ERR; modptr = (modptr_t *)dlq_nextEntry(modptr)) { res = do_show_one_module(modptr->mod, mode); anyout = TRUE; } for (modptr = (modptr_t *)dlq_firstEntry(get_mgrloadQ()); modptr != NULL && res == NO_ERR; modptr = (modptr_t *)dlq_nextEntry(modptr)) { res = do_show_one_module(modptr->mod, mode); anyout = TRUE; } } else { mod = ncx_get_first_module(); while (mod && res == NO_ERR) { res = do_show_one_module(mod, mode); anyout = TRUE; mod = ncx_get_next_module(mod); } } if (anyout) { if (imode) { log_stdout("\n"); } else { log_write("\n"); } } else { if (imode) { log_stdout("\nyangcli-pro: no modules loaded\n"); } else { log_error("\nyangcli-pro: no modules loaded\n"); } } return res; } /* do_show_modules */
/******************************************************************** * FUNCTION do_show_one_module (sub-mode of show modules RPC) * * for 1 of N: show modules * * INPUTS: * mod == module to show * mode == requested help mode * * RETURNS: * status *********************************************************************/ static status_t do_show_one_module (ncx_module_t *mod, help_mode_t mode) { boolean imode; imode = interactive_mode(); if (mode == HELP_MODE_BRIEF) { if (imode) { log_stdout("\n %s", mod->name); } else { log_write("\n %s", mod->name); } } else if (mode == HELP_MODE_NORMAL) { if (imode) { if (mod->version) { log_stdout("\n %s:%s@%s", ncx_get_mod_xmlprefix(mod), mod->name, mod->version); } else { log_stdout("\n %s:%s", ncx_get_mod_xmlprefix(mod), mod->name); } } else { if (mod->version) { log_write("\n %s@%s", mod->name, mod->version); } else { log_write("\n %s", mod->name); } } } else { help_data_module(mod, HELP_MODE_BRIEF); } return NO_ERR; } /* do_show_one_module */
int main(int argc, char *argv[]) { struct s_context context; context.f_lib = NULL; context.f_var = NULL; context.errno = NO_ERROR; if (argc == 2) { print_error(&context); set_error(NO_ERROR, NULL, NULL, &context); return 0; } if (argc >= 3 && (!my_strcmp(argv[1], "-f") || !my_strcmp(argv[1], "--file"))) { noninteractive_mode(&context, argv[2]); return 0; } interactive_mode(&context); return 0; }
int main(int argc, char *argv[]) { gint rc; GOptionContext *ctx; GError *error = NULL; z_mem_trace_init("syslog-ng.trace"); g_process_set_argv_space(argc, (gchar **) argv); setup_caps(); resolved_configurable_paths_init(&resolvedConfigurablePaths); ctx = g_option_context_new("syslog-ng"); g_process_add_option_group(ctx); msg_add_option_group(ctx); g_option_context_add_main_entries(ctx, syslogng_options, NULL); main_loop_add_options(ctx); if (!g_option_context_parse(ctx, &argc, &argv, &error)) { fprintf(stderr, "Error parsing command line arguments: %s\n", error ? error->message : "Invalid arguments"); g_option_context_free(ctx); return 1; } g_option_context_free(ctx); if (argc > 1) { fprintf(stderr, "Excess number of arguments\n"); return 1; } if (display_version) { interactive_mode(); version(); return 0; } if (display_module_registry) { interactive_mode(); plugin_list_modules(stdout, TRUE); return 0; } if(startup_debug_flag && debug_flag) { startup_debug_flag = FALSE; } if(startup_debug_flag) { debug_flag = TRUE; } if (debug_flag) { log_stderr = TRUE; } if (syntax_only || debug_flag) { g_process_set_mode(G_PM_FOREGROUND); } g_process_set_name("syslog-ng"); /* in this case we switch users early while retaining a limited set of * credentials in order to initialize/reinitialize the configuration. */ g_process_start(); app_startup(); main_loop_init(); rc = main_loop_read_and_init_config(); if (rc) { g_process_startup_failed(rc, TRUE); return rc; } else { if (syntax_only) g_process_startup_failed(0, TRUE); else g_process_startup_ok(); } /* we are running as a non-root user from this point */ app_post_daemonized(); app_post_config_loaded(); if(startup_debug_flag) { debug_flag = FALSE; log_stderr = FALSE; } /* from now on internal messages are written to the system log as well */ main_loop_run(); main_loop_deinit(); app_shutdown(); z_mem_trace_dump(); g_process_finish(); return rc; }
int server_loop_top(void) { extern int sigios_to_handle; int call_continue = 0; if (options.interactive) { interactive_mode(); } if (sigios_to_handle > 0) { notification_off(); PRINT_TIME(NOFD, &tnow, &tprev, "select_loop: new conns - race " "handling"); #ifdef ONE_LISTENER do_new_connections(server_sd, FROM_SERVER_LOOP); #else printf("sigio_handler: Support for multiple listeners and " "SIGIO is not implemented yet\n"); exit(1); #endif /* ONE_LISTENER */ if (options.accepts_only) { DEBG(MSG_SERVER_LOOP, "select_loop: back to new connections " "(sigios)\n"); notification_on(); call_continue = 1; return call_continue; } notification_on(); notification_off(); if (options.process_sds_order == OPT_PROCESS_SDS_LIFO) { q_sync(Q_ADD_TO_FRONT); } else if (options.process_sds_order == OPT_PROCESS_SDS_FIFO) { q_sync(Q_ADD_TO_REAR); } notification_on(); } else if ((options.get_connections == OPT_CONN_WITH_SIGIO) && (sigio_pending())) { notification_off(); PRINT_TIME(NOFD, &tnow, &tprev, "select_loop: new conns - " "(pending)"); #ifdef ONE_LISTENER do_new_connections(server_sd, FROM_SERVER_LOOP); #else printf("sigio_handler: Support for multiple listeners and " "SIGIO is not implemented yet\n"); exit(1); #endif /* ONE_LISTENER */ if (options.accepts_only) { DEBG(MSG_SERVER_LOOP, "select_loop: back to new connections " "(pending)\n"); notification_on(); call_continue = 1; return call_continue; } notification_on(); notification_off(); if (options.process_sds_order == OPT_PROCESS_SDS_LIFO) { q_sync(Q_ADD_TO_FRONT); } else if (options.process_sds_order == OPT_PROCESS_SDS_FIFO) { q_sync(Q_ADD_TO_REAR); } notification_on(); } return call_continue; }
int main(int argc, char *argv[]) { int err, i; char *host=NULL; int port=7777; int nmixers=0; extern int mixlib_trace; int interactive=0; //mixlib_trace=1; while ((i = getopt(argc, argv, "ip:h:")) != EOF) switch (i) { case 'i': interactive=1; break; case 'p': port = atoi(optarg); break; case 'h': host=optarg; break; } if ((err=ossmix_init())<0) { fprintf(stderr, "ossmix_init() failed, err=%d\n"); exit(EXIT_FAILURE); } if ((err=ossmix_connect(host, port))<0) { fprintf(stderr, "ossmix_connect() failed, err=%d\n", err); exit(EXIT_FAILURE); } if ((nmixers=ossmix_get_nmixers())<0) { fprintf(stderr, "ossmix_get_nmixers() failed, err=%d\n", nmixers); exit(EXIT_FAILURE); } printf("Number of mixers=%d\n", nmixers); for (i=0;i<nmixers;i++) { oss_mixerinfo mi; int n, ctl; if (ossmix_get_mixerinfo(i, &mi)<0) { fprintf(stderr, "ossmix_get_mixerinfo(%d) failed\n", i); exit(EXIT_FAILURE); } printf("Mixer %2d: %s\n", i, mi.name); if (ossmix_open_mixer(i)<0) { fprintf(stderr, "ossmix_open_mixer(%d) failed\n", i); exit(EXIT_FAILURE); } if ((n=ossmix_get_nrext(i))<0) { fprintf(stderr, "ossmix_get_nrext(%d) failed, err=\n", i, n); exit(EXIT_FAILURE); } printf("Mixer has %d nodes\n", n); for (ctl=0;ctl<n;ctl++) { oss_mixext node; int value=0; if (ossmix_get_nodeinfo(i, ctl, &node)<0) { fprintf(stderr, "ossmix_get_nodeinfo(%d, %d) failed\n", i, ctl); exit(EXIT_FAILURE); } if (node.type != MIXT_DEVROOT && node.type != MIXT_GROUP && node.type != MIXT_MARKER) if ((value=ossmix_get_value(i, ctl, node.timestamp))<0) { fprintf(stderr, "ossmix_get_value(%d, %d, %d) failed, err=%d\n", i, ctl, node.timestamp, value); } printf("%3d: %s = 0x%08x\n", ctl, node.extname, value); if (node.type == MIXT_ENUM) print_enum_list(i, ctl); if (node.flags & MIXF_DESCR) print_description(i, ctl); } if (!interactive) ossmix_close_mixer(i); } if (interactive) interactive_mode(); printf("Disconnecting\n"); ossmix_disconnect(); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { /* Command line options */ static struct option long_options[] = { {"nofork", no_argument, 0, 'n'}, {"verbose", no_argument, 0, 'v'}, {"socket-port", required_argument, 0, 'p'}, {"interactive", no_argument, 0, 'i'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int opt, opt_index = 0; /* parse command line options */ int nofork = 0, verbose = 0, socket_port = SOCKET_DEFAULT_PORT, interactive = 0; while ((opt = getopt_long(argc, argv, "nvp:iVh", long_options, &opt_index)) != -1) { switch (opt) { case 'n': nofork = 1; break; case 'v': verbose = 1; nofork = 1; break; case 'p': socket_port = atoi(optarg); break; case 'i': interactive = 1; nofork = 1; break; case 'V': printf( "%s version: %s\n" "source code: https://github.com/moddevices/mod-host\n", argv[0], version); exit(EXIT_SUCCESS); break; case 'h': printf( "Usage: %s [-vih] [-p <port>]\n" " -v, --verbose verbose messages\n" " -p, --socket-port=<port> socket port definition\n" " -i, --interactive interactive mode\n" " -V, --version print program version and exit\n" " -h, --help print this help and exit\n", argv[0]); exit(EXIT_SUCCESS); } } if (! nofork) { int pid; pid = fork(); if (pid != 0) { printf("Forking... child PID: %d\n", pid); FILE *fd; fd = fopen(PID_FILE, "w"); if (fd == NULL) { fprintf(stderr, "can't open PID File\n"); } else { fprintf(fd, "%d\n", pid); fclose(fd); } exit(EXIT_SUCCESS); } } if (mod_host_init(NULL, socket_port) != 0) { exit(EXIT_FAILURE); return 1; } /* Interactice mode */ if (interactive) interactive_mode(); /* Verbose */ protocol_verbose(verbose); /* Report ready */ printf("mod-host ready!\n"); fflush(stdout); while (1) socket_run(1); socket_finish(); effects_finish(1); protocol_remove_commands(); return 0; }
int main(int argc, char **argv) { bool success = false; int cmd_idx = 0; mems_data data; mems_data_frame_80 frame80; mems_data_frame_7d frame7d; librosco_version ver; mems_info info; uint8_t* frameptr; uint8_t bufidx; uint8_t readval = 0; uint8_t iac_limit_count = 80; // number of times to re-send an IAC move command when // the ECU is already reporting that the valve has // reached its requested position int read_loop_count = 1; bool read_inf = false; // this is twice as large as the micro's on-chip ROM, so it's probably sufficient uint8_t response_buffer[16384]; char win32devicename[16]; ver = mems_get_lib_version(); if (argc < 3) { printf("readmems using librosco v%d.%d.%d\n", ver.major, ver.minor, ver.patch); printf("Diagnostic utility using ROSCO protocol for MEMS 1.6 systems\n"); printf("Usage: %s <serial device> <command> [read-loop-count]\n", basename(argv[0])); printf(" where <command> is one of the following:\n"); for (cmd_idx = 0; cmd_idx < MC_Num_Commands; ++cmd_idx) { printf("\t%s\n", commands[cmd_idx]); } printf(" and [read-loop-count] is either a number or 'inf' to read forever.\n"); return 0; } while ((cmd_idx < MC_Num_Commands) && (strcasecmp(argv[2], commands[cmd_idx]) != 0)) { cmd_idx += 1; } if (cmd_idx >= MC_Num_Commands) { printf("Invalid command: %s\n", argv[2]); return -1; } if (argc >= 4) { if (strcmp(argv[3], "inf") == 0) { read_inf = true; } else { read_loop_count = strtoul(argv[3], NULL, 0); } } if (cmd_idx != MC_Interactive) { printf("Running command: %s\n", commands[cmd_idx]); } mems_init(&info); #if defined(WIN32) // correct for microsoft's legacy nonsense by prefixing with "\\.\" strcpy(win32devicename, "\\\\.\\"); strncat(win32devicename, argv[1], 16); if (mems_connect(&info, win32devicename)) #else if (mems_connect(&info, argv[1])) #endif { if (mems_init_link(&info, response_buffer)) { printf("ECU responded to D0 command with: %02X %02X %02X %02X\n\n", response_buffer[0], response_buffer[1], response_buffer[2], response_buffer[3]); switch (cmd_idx) { case MC_Read: while (read_inf || (read_loop_count-- > 0)) { if (mems_read(&info, &data)) { printf("RPM: %u\nCoolant (deg F): %u\nAmbient (deg F): %u\nIntake air (deg F): %u\n" "Fuel temp (deg F): %u\nMAP (kPa): %f\nMain voltage: %f\nThrottle pot voltage: %f\n" "Idle switch: %u\nPark/neutral switch: %u\nFault codes: %u\nIAC position: %u\n" "-------------\n", data.engine_rpm, data.coolant_temp_f, data.ambient_temp_f, data.intake_air_temp_f, data.fuel_temp_f, data.map_kpa, data.battery_voltage, data.throttle_pot_voltage, data.idle_switch, data.park_neutral_switch, data.fault_codes, data.iac_position); success = true; } } break; case MC_Read_Raw: while (read_inf || (read_loop_count-- > 0)) { if (mems_read_raw(&info, &frame80, &frame7d)) { frameptr = (uint8_t*)&frame80; printf("80: "); for (bufidx = 0; bufidx < sizeof(mems_data_frame_80); ++bufidx) { printf("%02X ", frameptr[bufidx]); } printf("\n"); frameptr = (uint8_t*)&frame7d; printf("7D: "); for (bufidx = 0; bufidx < sizeof(mems_data_frame_7d); ++bufidx) { printf("%02X ", frameptr[bufidx]); } printf("\n"); success = true; } } break; case MC_Read_IAC: if (mems_read_iac_position(&info, &readval)) { printf("0x%02X\n", readval); success = true; } break; case MC_PTC: if (mems_test_actuator(&info, MEMS_PTCRelayOn, NULL)) { sleep(2); success = mems_test_actuator(&info, MEMS_PTCRelayOff, NULL); } break; case MC_FuelPump: if (mems_test_actuator(&info, MEMS_FuelPumpOn, NULL)) { sleep(2); success = mems_test_actuator(&info, MEMS_FuelPumpOff, NULL); } break; case MC_IAC_Close: do { success = mems_test_actuator(&info, MEMS_CloseIAC, &readval); // For some reason, diagnostic tools will continue to send send the // 'close' command many times after the IAC has already reached the // fully-closed position. Emulate that behavior here. if (success && (readval == 0x00)) { iac_limit_count -= 1; } } while (success && iac_limit_count); break; case MC_IAC_Open: // The SP Rover 1 pod considers a value of 0xB4 to represent an opened // IAC valve, so repeat the open command until the valve is opened to // that point. do { success = mems_test_actuator(&info, MEMS_OpenIAC, &readval); } while (success && (readval < 0xB4)); break; case MC_AC: if (mems_test_actuator(&info, MEMS_ACRelayOn, NULL)) { sleep(2); success = mems_test_actuator(&info, MEMS_ACRelayOff, NULL); } break; case MC_Coil: success = mems_test_actuator(&info, MEMS_FireCoil, NULL); break; case MC_Injectors: success = mems_test_actuator(&info, MEMS_TestInjectors, NULL); break; case MC_Interactive: success = interactive_mode(&info, response_buffer); break; default: printf("Error: invalid command\n"); break; } } else { printf("Error in initialization sequence.\n"); } mems_disconnect(&info); } else { #if defined(WIN32) printf("Error: could not open serial device (%s).\n", win32devicename); #else printf("Error: could not open serial device (%s).\n", argv[1]); #endif } mems_cleanup(&info); return success ? 0 : -2; }
/* * Start the Lua interpreter, export IMAP core and system functions, load the * Lua interface functions, load and execute imapfilter's configuration file. */ void start_lua() { lua = luaL_newstate(); luaL_openlibs(lua); luaopen_ifcore(lua); luaopen_ifsys(lua); luaopen_ifre(lua); lua_settop(lua, 0); init_options(); if (luaL_loadfile(lua, PATHNAME_COMMON) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_SET) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_REGEX) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_ACCOUNT) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_MAILBOX) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_MESSAGE) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_OPTIONS) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (luaL_loadfile(lua, PATHNAME_AUXILIARY) || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); if (opts.oneline != NULL) { if (luaL_loadbuffer(lua, opts.oneline, strlen(opts.oneline), "=<command line>") || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); } else { if (luaL_loadfile(lua, strcmp(opts.config, "-") == 0 ? NULL : opts.config)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); lua_pushcfunction(lua, traceback_handler); lua_insert(lua, 1); if (lua_pcall(lua, 0, LUA_MULTRET, -2)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); } if (opts.interactive) interactive_mode(); }
/******************************************************************** * FUNCTION show_user_var * * generate the output for a global or local variable * * INPUTS: * server_cb == server control block to use * varname == variable name to show * vartype == type of user variable * val == value associated with this variable * mode == help mode in use * * RETURNS: * status *********************************************************************/ static status_t show_user_var (server_cb_t *server_cb, const xmlChar *varname, var_type_t vartype, val_value_t *val, help_mode_t mode) { session_cb_t *session_cb = server_cb->cur_session_cb; xmlChar *objbuff; logfn_t logfn; boolean imode = interactive_mode(); int32 doubleindent = 1; status_t res = NO_ERR; if (imode) { logfn = log_stdout; } else { logfn = log_write; } switch (vartype) { case VAR_TYP_GLOBAL: case VAR_TYP_LOCAL: case VAR_TYP_SESSION: case VAR_TYP_SYSTEM: case VAR_TYP_CONFIG: if (xml_strcmp(varname, val->name)) { doubleindent = 2; (*logfn)("\n %s ", varname); if (val->obj && obj_is_data_db(val->obj)) { res = obj_gen_object_id(val->obj, &objbuff); if (res != NO_ERR) { (*logfn)("[no object id]\n "); } else { (*logfn)("[%s]\n ", objbuff); m__free(objbuff); } } } else if (session_cb->display_mode == NCX_DISPLAY_MODE_JSON) { (*logfn)("\n %s: ", varname); if (!typ_is_simple(val->btyp)) { (*logfn)("\n"); } } break; default: ; } if (!typ_is_simple(val->btyp) && mode == HELP_MODE_BRIEF) { if (doubleindent == 1) { (*logfn)("\n %s (%s)", varname, tk_get_btype_sym(val->btyp)); } else { (*logfn)("\n (%s)", tk_get_btype_sym(val->btyp)); } } else { val_dump_value_max(val, session_cb->defindent * doubleindent, session_cb->defindent, (imode) ? DUMP_VAL_STDOUT : DUMP_VAL_LOG, session_cb->display_mode, FALSE, FALSE); } return res; } /* show_user_var */
/******************************************************************** * FUNCTION do_show (local RPC) * * show module=mod-name * modules * def=def-nmae * * Get the specified parameter and show the internal info, * based on the parameter * * INPUTS: * server_cb == server control block to use * rpc == RPC method for the show command * line == CLI input in progress * len == offset into line buffer to start parsing * * RETURNS: * status *********************************************************************/ status_t do_show (server_cb_t *server_cb, obj_template_t *rpc, const xmlChar *line, uint32 len) { val_value_t *valset, *parm; ncx_module_t *mod; status_t res; boolean imode, done; help_mode_t mode; xmlChar versionbuffer[NCX_VERSION_BUFFSIZE]; res = NO_ERR; imode = interactive_mode(); valset = get_valset(server_cb, rpc, &line[len], &res); if (valset && res == NO_ERR) { mode = HELP_MODE_NORMAL; /* check if the 'brief' flag is set first */ parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_BRIEF); if (parm && parm->res == NO_ERR) { mode = HELP_MODE_BRIEF; } else { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_FULL); if (parm && parm->res == NO_ERR) { mode = HELP_MODE_FULL; } } /* get the 1 of N 'showtype' choice */ done = FALSE; /* show cli */ parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_CLI); if (parm) { do_show_cli(server_cb); done = TRUE; } /* show local <foo> */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_LOCAL); if (parm) { res = do_show_var(server_cb, VAL_STR(parm), VAR_TYP_LOCAL, FALSE, mode); done = TRUE; } } /* show locals */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_LOCALS); if (parm) { res = do_show_vars(server_cb, mode, TRUE, FALSE, FALSE); done = TRUE; } } /* show objects */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_OBJECTS); if (parm) { res = do_show_objects(server_cb, mode); done = TRUE; } } /* show global */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_GLOBAL); if (parm) { res = do_show_var(server_cb, VAL_STR(parm), VAR_TYP_GLOBAL, FALSE, mode); done = TRUE; } } /* show globals */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_GLOBALS); if (parm) { res = do_show_vars(server_cb, mode, TRUE, TRUE, FALSE); done = TRUE; } } /* show session */ parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_SESSION); if (parm) { do_show_session(server_cb, mode); done = TRUE; } /* show system */ parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_SYSTEM); if (parm) { do_show_system(server_cb, mode); done = TRUE; } /* show var <foo> */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_VAR); if (parm) { res = do_show_var(server_cb, VAL_STR(parm), VAR_TYP_NONE, TRUE, mode); done = TRUE; } } /* show vars */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_VARS); if (parm) { res = do_show_vars(server_cb, mode, FALSE, FALSE, TRUE); done = TRUE; } } /* show module <foo> */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_MODULE); if (parm) { mod = find_module(server_cb, VAL_STR(parm)); if (mod) { res = do_show_module(mod, mode); } else { if (imode) { log_stdout("\nyangcli-pro: module (%s) not loaded", VAL_STR(parm)); } else { log_error("\nyangcli-pro: module (%s) not loaded", VAL_STR(parm)); } } done = TRUE; } } /* show modules */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, YANGCLI_MODULES); if (parm) { res = do_show_modules(server_cb, mode); done = TRUE; } } /* show version */ if (!done) { parm = val_find_child(valset, YANGCLI_MOD, NCX_EL_VERSION); if (parm) { res = ncx_get_version(versionbuffer, NCX_VERSION_BUFFSIZE); if (res == NO_ERR) { if (imode) { log_stdout("\nyangcli version %s\n", versionbuffer); } else { log_write("\nyangcli version %s\n", versionbuffer); } } done = TRUE; } } } if (valset) { val_free_value(valset); } return res; } /* do_show */
/******************************************************************** * FUNCTION do_show_objects (sub-mode of local RPC) * * show objects * * INPUTS: * server_cb == server control block to use * mode == requested help mode * * RETURNS: * status *********************************************************************/ static status_t do_show_objects (server_cb_t *server_cb, help_mode_t mode) { session_cb_t *session_cb = server_cb->cur_session_cb; ncx_module_t *mod; obj_template_t *obj; modptr_t *modptr; boolean imode = interactive_mode(); boolean anyout = FALSE; status_t res = NO_ERR; if (use_session_cb(session_cb)) { for (modptr = (modptr_t *) dlq_firstEntry(&session_cb->modptrQ); modptr != NULL; modptr = (modptr_t *)dlq_nextEntry(modptr)) { for (obj = ncx_get_first_object(modptr->mod); obj != NULL && res == NO_ERR; obj = ncx_get_next_object(modptr->mod, obj)) { res = do_show_one_object(obj, mode, &anyout); } } for (modptr = (modptr_t *) dlq_firstEntry(get_mgrloadQ()); modptr != NULL; modptr = (modptr_t *)dlq_nextEntry(modptr)) { for (obj = ncx_get_first_object(modptr->mod); obj != NULL && res == NO_ERR; obj = ncx_get_next_object(modptr->mod, obj)) { res = do_show_one_object(obj, mode, &anyout); } } } else { mod = ncx_get_first_module(); while (mod) { for (obj = ncx_get_first_object(mod); obj != NULL && res == NO_ERR; obj = ncx_get_next_object(mod, obj)) { res = do_show_one_object(obj, mode, &anyout); } mod = (ncx_module_t *)ncx_get_next_module(mod); } } if (anyout) { if (imode) { log_stdout("\n"); } else { log_write("\n"); } } return res; } /* do_show_objects */
/******************************************************************** * FUNCTION do_show_system (sub-mode of local RPC) * * show system parms * * INPUTS: * server_cb == server control block to use * mode == help mode *********************************************************************/ static void do_show_system (server_cb_t *server_cb, help_mode_t mode) { ncx_var_t *var; dlq_hdr_t *que; logfn_t logfn; boolean imode, first; imode = interactive_mode(); if (imode) { logfn = log_stdout; } else { logfn = log_write; } /* System Script Variables */ que = runstack_get_que(server_cb->runstack_context, ISGLOBAL); first = TRUE; for (var = (ncx_var_t *)dlq_firstEntry(que); var != NULL; var = (ncx_var_t *)dlq_nextEntry(var)) { if (var->vartype != VAR_TYP_SYSTEM) { continue; } if (first) { (*logfn)("\nRead-only environment variables\n"); first = FALSE; } show_user_var(server_cb, var->name, var->vartype, var->val, mode); } if (first) { (*logfn)("\nNo read-only environment variables\n"); } (*logfn)("\n"); /* System Config Variables */ que = runstack_get_que(server_cb->runstack_context, ISGLOBAL); first = TRUE; for (var = (ncx_var_t *)dlq_firstEntry(que); var != NULL; var = (ncx_var_t *)dlq_nextEntry(var)) { if (var->vartype != VAR_TYP_CONFIG) { continue; } if (first) { (*logfn)("\nRead-write system variables\n"); first = FALSE; } show_user_var(server_cb, var->name, var->vartype, var->val, mode); } if (first) { (*logfn)("\nNo system config variables\n"); } (*logfn)("\n"); } /* do_show_system */
/* * process command-line parameters */ int process_options(int argc, char *argv[]) { int i; int opt_ihavename = 0; int opt_nomore = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { if (opt_nomore) { if (strlen(opt_command) + strlen(argv[i]) + 2 < OPT_CMD_SZ) { // +1 for space +1 for the trailing zero strcat(opt_command, " "); strcat(opt_command, argv[i]); } else { fprintf(stderr, "Too long command line! (%s)\n", argv[i]); return 1; } } else { switch (argv[i][1]) { case '-': // the following parameters are going to script // (COMMAND$) opt_nomore = 1; break; case 's': // decompile opt_decomp++; break; case 'c': // syntax check opt_syntaxcheck++; break; case 'u': dev_setenv("UNITPATH", &argv[i][2]); break; case 'v': // verbose check if (!opt_quiet) { // -v -v opt_verbose = 1; } opt_quiet = 0; break; case 'i': opt_ide = IDE_EXTERNAL; break; case 'x': opt_nosave = 0; break; case 'p': if (strcmp(argv[i] + 1, "pkw") == 0) { print_keywords(); } break; case 'm': // load run-time modules opt_loadmod = 1; if (i + 1 < argc) { strcpy(opt_modlist, argv[++i]); } break; case 'q': // shutup opt_quiet = 1; break; case 'h': // print command-line parameters fprintf(stdout, "SmallBASIC version %s - kw:%d, pc:%d, fc:%d, ae:%d\n", SB_STR_VER, kwNULL, (kwNULLPROC - kwCLS) + 1, (kwNULLFUNC - kwASC) + 1, (int)(65536 / sizeof(var_t))); fprintf(stdout, "http://smallbasic.sourceforge.net\n\n"); if (argv[i][2] == '-' || argv[i][2] == 'x') { /* * search for command, or print all doc */ if (argv[i][2] == '-') { #ifdef HELP_SUBSYS char *command = argv[i] + 3; help_printinfo(command); #endif } else if (argv[i][2] == 'x') { // print all // printf("%s\n", help_text); ; } } else { show_help(); } return 1; default: fprintf(stderr, "unknown option: %s\n", argv[i]); return 1; }; } } else { // no - switch // this is the filename or script-parameters if (opt_ihavename == 0) { strcpy(g_file, argv[i]); if (access(g_file, F_OK)) { strcat(g_file, ".bas"); if (access(g_file, F_OK)) { fprintf(stderr, "file not accessible - %s\n", g_file); return 1; } } if (access(g_file, R_OK)) { fprintf(stderr, "file not readable - %s\n", g_file); return 1; } opt_ihavename = 1; } else { if (strlen(opt_command) + strlen(argv[i]) + 2 < OPT_CMD_SZ) { // +1 for space +1 for the trailing zero strcat(opt_command, " "); strcat(opt_command, argv[i]); } else { fprintf(stderr, "Too long command line! (%s)\n", argv[i]); return 1; } } } } // initialization if (strlen(g_file) == 0) { // stdin if (isatty(STDIN_FILENO)) { // check if it is a terminal. opt_interactive = 1; } #if defined(_Win32) || defined(_DOS) char *slash = strchr(argv[0], OS_DIRSEP); if (slash) { strcpy(g_file, argv[0]); slash = strrchr(g_file, OS_DIRSEP); *slash = OS_DIRSEP; *(slash + 1) = '\0'; strcat(g_file, "sbasic.tmp"); } else { sprintf(g_file, "sbasic.tmp"); } #elif defined(_UnixOS) sprintf(g_file, "%ctmp%csb%d.bas", OS_DIRSEP, OS_DIRSEP, getpid()); #else sprintf(g_file, "sb%d.bas", getpid()); // for minimal GNU systems like // MINGW #endif // its a temporary and it must be deleted atexit(remove_temp_file); if (opt_interactive) { // get it from console #ifdef INTERACTIVE_CONSOLE interactive_mode(g_file); #endif } else { // get it from stdin FILE *fp = fopen(g_file, "wb"); int c; if (fp) { while ((c = fgetc(stdin)) != EOF) { fputc(c, fp); } fclose(fp); } else { fprintf(stderr, "file not writeable - %s\n", g_file); return 1; } } } return 0; }
int main(int argc, char **argv) { int return_code = 0; int fd = (-1); /* fd == -1 means connection to fcron is not currently open */ struct passwd *pass = NULL; rootuid = get_user_uid_safe(ROOTNAME); rootgid = get_group_gid_safe(ROOTGROUP); if (strrchr(argv[0], '/') == NULL) prog_name = argv[0]; else prog_name = strrchr(argv[0], '/') + 1; user_uid = getuid(); user_gid = getgid(); if ((pass = getpwuid(user_uid)) == NULL) die("user \"%s\" is not in passwd file. Aborting.", USERNAME); user_str = strdup2(pass->pw_name); /* drop suid rights that we don't need, but keep the sgid rights * for now as we will need them for read_conf() and is_allowed() */ #ifdef USE_SETE_ID seteuid_safe(user_uid); #endif if (setuid(user_uid) < 0) die_e("could not setuid() to %d", user_uid); /* interpret command line options */ parseopt(argc, argv); /* read fcron.conf and update global parameters */ read_conf(); if (!is_allowed(user_str)) { die("User \"%s\" is not allowed to use %s. Aborting.", user_str, prog_name); } /* we don't need anymore special rights : drop remaining ones */ #ifdef USE_SETE_ID setegid_safe(user_gid); #endif if (setgid(user_gid) < 0) die_e("could not setgid() to %d", user_gid); /* check for broken pipes ... */ signal(SIGPIPE, sigpipe_handler); if (cmd_str == NULL) return_code = interactive_mode(fd); else return_code = talk_fcron(cmd_str, fd); xexit((return_code == OK) ? EXIT_OK : EXIT_ERR); /* never reached */ return EXIT_OK; }
int main(int argc, char **argv) { int verbose, socket_port, interactive; /* Command line options */ struct arg_lit *_verbose = arg_lit0("v", "verbose,debug", "verbose messages"); struct arg_int *_socket = arg_int0("p", "socket-port", "<port>", "socket port definition"); struct arg_lit *_interactive = arg_lit0("i", "interactive", "interactive mode"); struct arg_lit *_help = arg_lit0("h", "help", "print this help and exit"); struct arg_end *_end = arg_end(20); void *argtable[] = {_verbose, _socket, _interactive, _help, _end}; if (arg_nullcheck(argtable)) { fprintf(stderr, "argtable error: insufficient memory\n"); exit(EXIT_FAILURE); } /* Default value of command line arguments */ _socket->ival[0] = SOCKET_DEFAULT_PORT; /* Run the argument parser */ if (arg_parse(argc, argv, argtable) == 0) { if (_help->count > 0) { fprintf(stdout, "Usage: %s", argv[0]); arg_print_syntax(stdout, argtable, "\n"); arg_print_glossary(stdout, argtable, " %-30s %s\n"); exit(EXIT_SUCCESS); } verbose = _verbose->count; socket_port = _socket->ival[0]; interactive = _interactive->count; } else { arg_print_errors(stderr, _end, argv[0]); exit(EXIT_FAILURE); } arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0])); /* If verbose or interactive, don't fork */ if (!verbose && !interactive) { int pid; pid = fork(); if (pid != 0) { printf("Forking... child PID: %d\n", pid); FILE *fd; fd = fopen(PID_FILE, "w"); if (fd == NULL) { fprintf(stderr, "can't open PID File\n"); } else { fprintf(fd, "%d\n", pid); fclose(fd); } exit(EXIT_SUCCESS); } } /* Setup the protocol */ protocol_add_command(EFFECT_ADD, effects_add_cb); protocol_add_command(EFFECT_REMOVE, effects_remove_cb); protocol_add_command(EFFECT_CONNECT, effects_connect_cb); protocol_add_command(EFFECT_DISCONNECT, effects_disconnect_cb); protocol_add_command(EFFECT_BYPASS, effects_bypass_cb); protocol_add_command(EFFECT_PARAM_SET, effects_set_param_cb); protocol_add_command(EFFECT_PARAM_GET, effects_get_param_cb); protocol_add_command(EFFECT_PARAM_MON, effects_monitor_param_cb); protocol_add_command(MONITOR_ADDR_SET, monitor_addr_set_cb); protocol_add_command(LOAD_COMMANDS, load_cb); protocol_add_command(SAVE_COMMANDS, save_cb); protocol_add_command(HELP, help_cb); protocol_add_command(QUIT, quit_cb); /* Startup the effects */ if (effects_init()) return -1; /* Setup the socket */ if (socket_start(socket_port, SOCKET_MSG_BUFFER_SIZE) < 0) { exit(EXIT_FAILURE); } socket_set_receive_cb(protocol_parse); /* Interactice mode */ if (interactive) interactive_mode(); /* Verbose */ protocol_verbose(verbose); while (1) socket_run(); protocol_remove_commands(); socket_finish(); effects_finish(); return 0; }
/******************************************************************** * FUNCTION do_show_vars (sub-mode of local RPC) * * show brief info for all user variables * * INPUTS: * server_cb == server control block to use * mode == help mode requested * shortmode == TRUE if printing just global or local variables * FALSE to print everything * isglobal == TRUE if print just globals * FALSE to print just locals * Ignored unless shortmode==TRUE * isany == TRUE to choose global or local * FALSE to use 'isglobal' valuse only * RETURNS: * status *********************************************************************/ static status_t do_show_vars (server_cb_t *server_cb, help_mode_t mode, boolean shortmode, boolean isglobal, boolean isany) { ncx_var_t *var; dlq_hdr_t *que; logfn_t logfn; boolean first, imode; imode = interactive_mode(); if (imode) { logfn = log_stdout; } else { logfn = log_write; } if (mode > HELP_MODE_BRIEF && !shortmode) { /* CLI Parameters */ do_show_cli(server_cb); } /* System Script Variables */ if (!shortmode) { do_show_system(server_cb, mode); } /* Global Script Variables */ if (!shortmode || isglobal) { que = runstack_get_que(server_cb->runstack_context, ISGLOBAL); first = TRUE; for (var = (ncx_var_t *)dlq_firstEntry(que); var != NULL; var = (ncx_var_t *)dlq_nextEntry(var)) { if (var->vartype != VAR_TYP_GLOBAL) { continue; } if (first) { (*logfn)("\nGlobal variables\n"); first = FALSE; } show_user_var(server_cb, var->name, var->vartype, var->val, mode); } if (first) { (*logfn)("\nNo global variables\n"); } (*logfn)("\n"); } /* Local Script Variables */ if (!shortmode || !isglobal || isany) { que = runstack_get_que(server_cb->runstack_context, ISLOCAL); first = TRUE; for (var = (ncx_var_t *)dlq_firstEntry(que); var != NULL; var = (ncx_var_t *)dlq_nextEntry(var)) { if (first) { (*logfn)("\nLocal variables\n"); first = FALSE; } show_user_var(server_cb, var->name, var->vartype, var->val, mode); } if (first) { (*logfn)("\nNo local variables\n"); } (*logfn)("\n"); } return NO_ERR; } /* do_show_vars */