int main(int argc, char ** argv) { struct bootconfig bc; int ret = -1; bc_ll_init( &bc, "/dev/mtd1" ); if ( argc < 2 ) { _print_config( &bc ); exit(0); } if ( 0 == strcmp( argv[1], "info" ) ) { _print_info( &bc ); exit(0); } if ( 0 == strcmp( argv[1], "help" ) ) { ret = 0; } if ( 0 == strcmp( argv[1], "set-kernel-healthy" ) ) { _set_kernel_healthy( &bc ); _print_config( &bc ); exit(0); } if ( 0 == strcmp( argv[1], "set-rootfs-healthy" ) ) { _set_rootfs_healthy( &bc ); _print_config( &bc ); exit(0); } if ( argc == 3 ) { /* TFM TODO / FIXME: this needs refactoring. */ if ( 0 == strcmp( argv[1], "set-kernel" ) ) { _set( &bc, "kernel", argv[2], -2, kernel ); } if ( 0 == strcmp( argv[1], "set-rootfs" ) ) { _set( &bc, "rootfs", argv[2], -4, rootfs ); } } _print_help(); return ret; }
/** parse commandline arguments */ static int _parse_args(int argc, char *argv[]) { int index, argument; static struct option loptions[] = { {"help", no_argument, 0, 'h'}, {"plugin-help", no_argument, 0, 'p'}, {"dimensions", required_argument, 0, 'D'}, {"driver", required_argument, 0, 'd'}, {"input", required_argument, 0, 'i'}, {"actor", required_argument, 0, 'a'}, {"morph", required_argument, 0, 'm'}, {"fps", required_argument, 0, 'f'}, {"seed", required_argument, 0, 's'}, {0, 0, 0, 0 } }; while((argument = getopt_long(argc, argv, "hpD:d:i:a:m:f:s:", loptions, &index)) >= 0) { switch(argument) { /* --help */ case 'h': { _print_help(argv[0]); return EXIT_FAILURE; } /* --plugin-help */ case 'p': { _print_plugin_help(); return EXIT_FAILURE; } /* --dimensions */ case 'D': { if (std::sscanf (optarg, "%dx%d", &width, &height) != 2) { std::cerr << "Invalid dimensions: '" << optarg << "'. Use <width>x<height> (e.g. 320x200)\n"; return EXIT_FAILURE; } break; } /* --driver */ case 'd': { if (!DisplayDriverFactory::instance().has_driver (optarg)) { std::cerr << "Unsupported display driver: " << optarg << "\n"; return EXIT_FAILURE; } driver_name = optarg; break; } /* --input */ case 'i': { /* save name for later */ input_name = optarg; break; } /* --actor */ case 'a': { /* save name for later */ actor_name = optarg; break; } /* --morph */ case 'm': { /* save filename for later */ morph_name = optarg; break; } /* --fps */ case 'f': { /* set framerate */ std::sscanf(optarg, "%d", &framerate); break; } /* --seed */ case 's': { have_seed = 1; std::sscanf(optarg, "%u", &seed); break; } /* invalid argument */ case '?': { std::fprintf(stderr, "argument %d is invalid\n", index); _print_help(argv[0]); return EXIT_FAILURE; } /* unhandled arguments */ default: { std::fprintf(stderr, "argument %d is invalid\n", index); break; } } } return EXIT_SUCCESS; }
/** parse commandline arguments */ static NftResult _parse_args(int argc, char *argv[]) { int index, argument; static struct option loptions[] = { {"help", 0, 0, 'h'}, {"plugin-help", 0, 0, 'p'}, {"loglevel", required_argument, 0, 'l'}, {"config", required_argument, 0, 'c'}, {"dimensions", required_argument, 0, 'd'}, {"fps", required_argument, 0, 'F'}, {"format", required_argument, 0, 'f'}, {"big-endian", no_argument, 0, 'b'}, {"loop", no_argument, 0, 'L'}, #if HAVE_IMAGEMAGICK == 1 {"raw", no_argument, 0, 'r'}, #endif {0,0,0,0} }; #if HAVE_IMAGEMAGICK == 1 const char arglist[] = "hpl:c:d:F:f:bLr"; #else const char arglist[] = "hpl:c:d:F:f:bL"; #endif while((argument = getopt_long(argc, argv, arglist, loptions, &index)) >= 0) { switch(argument) { /* --help */ case 'h': { _print_help(argv[0]); return NFT_FAILURE; } /* --plugin-help */ case 'p': { _print_plugin_help(); return NFT_FAILURE; } /* --config */ case 'c': { /* save filename for later */ strncpy(_c.prefsfile, optarg, sizeof(_c.prefsfile)); break; } /** --loop */ case 'L': { _c.do_loop = TRUE; break; } /** --dimensions */ case 'd': { if(sscanf(optarg, "%dx%d", (int*) &_c.width, (int*) &_c.height) != 2) { NFT_LOG(L_ERROR, "Invalid dimension \"%s\" (Use something like 320x400)", optarg); return NFT_FAILURE; } break; } /** --fps */ case 'F': { if(sscanf(optarg, "%d", (int*) &_c.fps) != 1) { NFT_LOG(L_ERROR, "Invalid framerate \"%s\" (Use an integer)", optarg); return NFT_FAILURE; } break; } /** --loglevel */ case 'l': { if(!nft_log_level_set(nft_log_level_from_string(optarg))) { _print_loglevels(); return NFT_FAILURE; } break; } /** --format */ case 'f': { strncpy(_c.pixelformat, optarg, sizeof(_c.pixelformat)); break; } #if HAVE_IMAGEMAGICK == 1 /** --raw */ case 'r': { _c.raw = TRUE; break; } #endif /** --big-endian */ case 'b': { _c.is_big_endian = TRUE; break; } /* invalid argument */ case '?': { NFT_LOG(L_ERROR, "argument %d is invalid", index); _print_help(argv[0]); return NFT_FAILURE; } /* unhandled arguments */ default: { NFT_LOG(L_ERROR, "argument %d is invalid", index); break; } } } _c.files = &argv[optind]; return NFT_SUCCESS; }
int OnlineSession::run(istream *in, ostream *out, bool interactive) { map<string, int> commands; prepareCommands(commands); int res = 0; int count = 0; int cmd; string command; string sarg1, sarg2; int iarg1, iarg2, iarg3, iarg4, iarg5, iarg6, iarg7; double darg1; double *dparg1; TimeSeriesSet *t; clock_t time; res = 0; while (!in->eof()) { if (res != 0) *out << "Command returned with status " << res << "." << endl; if (interactive) { int width = out->width(); *out << "["; out->width(3); *out << count; out->width(width); *out << "] > "; out->flush(); } *in >> command; time = clock(); if (command.size() > 0) { cmd = commands[command]; } if (command.size() <= 0 || in->eof()) { cmd = _EXIT; *out << endl; } if (cmd == 0) { *out << "Unknown command '" << command << "'. Type 'help' for help." << endl; res = 1; count++; continue; } try { res = 0; switch (cmd) { case _EXIT: *out << "Quitting..." << endl; return iarg1; case _HELP: _print_help(out); break; case _LOAD_TSS: *in >> sarg1; *out << "Loading Time Series Set from file '" << sarg1 << "'." << endl; res = loadDataSet(new TimeSeriesSet(sarg1.c_str())); if (res >= 0) { *out << "Dataset successfully loaded. Index: " << res << endl; res = 0; } else { *out << "Failed to load dataset." << endl; } break; case _SAVE_TSS: *in >> iarg1; *in >> sarg2; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Saving Time Series Set " << iarg1 << ":" << t->name << " to file '" << sarg2 << "'." << endl; res = saveDataSet(iarg1, sarg2.c_str()); if (res == 0) *out << "Dataset successfully saved." << endl; break; case _OLOAD_TSS: *in >> sarg1; *in >> iarg1 >> iarg2 >> iarg3; *out << "Loading Time Series Set from file '" << sarg1 << "' with N=" << iarg1 << ", L=" << iarg2 << ", and D=" << iarg3 << "." << endl; res = loadDataSet(new TimeSeriesSet(sarg1.c_str(), iarg1, iarg2, iarg3)); if (res >= 0) { *out << "Dataset successfully loaded. Index: " << res << endl; res = 0; } else { *out << "Failed to load dataset." << endl; } break; case _OSAVE_TSS: *in >> iarg1; *in >> sarg2; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Saving Time Series Set " << iarg1 << ":" << t->name << " to file '" << sarg2 << "'." << endl; res = saveDataSet(iarg1, sarg2.c_str(), true); if (res == 0) *out << "Dataset successfully saved." << endl; break; case _DROP_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Dropping Time Series Set " << iarg1 << ":" << t->name << "." << endl; res = dropDataSet(iarg1); break; case _RAND_TSS: *in >> iarg1 >> iarg2 >> iarg3; *out << "Generating random Time Series Set with N=" << iarg1 << ", L=" << iarg2 << ", and range=" << iarg3 << "." << endl; res = loadDataSet(&TimeSeriesSet::randomSet(iarg1, iarg2, iarg3)); if (res >= 0) { *out << "Dataset successfully loaded. Index: " << res << endl; res = 0; } else { *out << "Failed to load dataset." << endl; } break; case _LIST_TSS: printDataSets(out); break; case _NORM_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Normalizing Time Series Set " << iarg1 << ":" << t->name << "." << endl; res = normalizeDataSet(iarg1); break; case _KSIM_TSS: *in >> iarg1 >> iarg2 >> iarg3 >> iarg4 >> iarg5 >> iarg6 >> iarg7; checkIndex(iarg1); checkIndex(iarg2); t = dataSets[iarg1]; *out << "Getting the kSimilar for Time Series Set " << iarg1 << ":" << t->name; t = dataSets[iarg2]; *out << ", query string at " << iarg3 << " in dataset " << iarg2 << ":" << t->name; *out << " in interval [" << iarg4 << "," << iarg5 << "] with k=" << iarg6 << " and strict=" << iarg7 << "." << endl; dparg1 = t->getInterval(iarg3,TimeInterval(iarg4, iarg5)).getData(); kSimilar(iarg1, vector<double>(dparg1, dparg1 + (iarg5 - iarg4 + 1)), TimeInterval(iarg4, iarg5), iarg6, iarg7); break; case _OUTLIER_TSS: *in >> iarg1 >> iarg2 >> iarg3; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Gettind dominant outlier on Time Series Set " << iarg1 << ":" << t->name << " in the range [" << iarg2 << ", " << iarg3 << "]." << endl; dominantOutlier(iarg1, TimeInterval(iarg2, iarg3)); break; case _GROUP_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << ((groupings[iarg1] != NULL)?"Regrouping":"Grouping") << " Time Series Set " << iarg1 << ":" << t->name << " with ST " << defaultST << "." << endl; genGrouping(iarg1, defaultST); break; case _NGROUP_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << ((groupings[iarg1] != NULL)?"Regrouping":"Grouping") << " Time Series Set " << iarg1 << ":" << t->name << " with ST " << defaultST << " (naive)." << endl; genNaiveGrouping(iarg1, defaultST); break; case _GROUP_ST_TSS: *in >> iarg1; *in >> darg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << ((groupings[iarg1] == NULL)?"Grouping":"Regrouping") << " Time Series Set " << iarg1 << ":" << t->name << " with ST " << darg1 << "." << endl; genGrouping(iarg1, darg1); break; case _NGROUP_ST_TSS: *in >> iarg1; *in >> darg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << ((groupings[iarg1] == NULL)?"Grouping":"Regrouping") << " Time Series Set " << iarg1 << ":" << t->name << " with ST " << darg1 << " (naive)." << endl; genNaiveGrouping(iarg1, darg1); break; case _LOAD_GROUP_TSS: *in >> iarg1; *in >> sarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Loading grouping data for Time Series Set " << iarg1 << ":" << t->name << " at " << sarg1 << "." << endl; res = loadGrouping(iarg1, sarg1.c_str()); break; case _SAVE_GROUP_TSS: *in >> iarg1; *in >> sarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Saving grouping data for Time Series Set " << iarg1 << ":" << t->name << " to " << sarg1 << "." << endl; res = saveGrouping(iarg1, sarg1.c_str()); break; case _DROP_GROUP_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; *out << "Dropping grouping data for Time Series Set " << iarg1 << ":" << t->name << "." << endl; dropGrouping(iarg1); break; case _DESC_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; t->printDesc(out); break; case _PRINT_TSS: *in >> iarg1; checkIndex(iarg1); t = dataSets[iarg1]; t->printData(out); break; case _DEBUG: *in >> iarg1; *out << "Setting debug to " << iarg1 << "." << endl; debug = iarg1; break; case _SET_DEF_ST: *in >> darg1; *out << "Setting default ST to " << darg1 << "." << endl; defaultST = darg1; break; case _GET_DEF_ST: *out << "default ST = " << defaultST << "." << endl; break; } } catch (exception &e) { *out << "Caught exception attempting operation:" << endl; *out << e.what() << endl; } time = clock() - time; (*out) << "Command used " << ((float)time/CLOCKS_PER_SEC) << " seconds." << endl << endl; count++; } return 0; }
int main( int argc, char* argv[] ) { int status; tox_IP_Port Ip_port; const char* ip, *psend, *plisten; uint16_t port_send, port_listen; const uint8_t* test_bytes = "0123456789012345678901234567890123456789012345678901234567890123456789" "0123456789012345678901234567890123456789012345678901234567890123456789" "0123456789012345678901234567890123456789012345678901234567890123456789" "0123456789012345678901234567890123456789012345678901234567890123456789"; rtp_session_t* _m_session; rtp_msg_t *_m_msg_R, *_m_msg_S; arg_t* _list = parse_args ( argc, argv ); ip = find_arg_duble(_list, "-d"); psend = find_arg_duble(_list, "-p"); plisten = find_arg_duble(_list, "-l"); if ( !ip || !plisten || !psend ) return _print_help(argv[0]); port_send = atoi(psend); port_listen = atoi(plisten); IP_Port local, remote; /* * This is the Local ip. We initiate networking on * this value for it's the local one. To make stuff simpler we receive over this value * and send on the other one ( see remote ) */ local.ip.i = htonl(INADDR_ANY); local.port = port_listen; Networking_Core* _networking = new_networking(local.ip, port_listen); if ( !_networking ) return FAILURE; int _socket = _networking->sock; /* * Now this is the remote. It's used by rtp_session_t to determine the receivers ip etc. */ t_setipport ( ip, port_send, &remote ); _m_session = rtp_init_session(-1, -1); rtp_add_receiver( _m_session, &remote ); /* Now let's start our main loop in both recv and send mode */ for ( ;; ) { /* * This part checks for received messages and if gotten one * display 'Received msg!' indicator and free message */ _m_msg_R = rtp_recv_msg ( _m_session ); if ( _m_msg_R ) { puts ( "Received msg!" ); rtp_free_msg(_m_session, _m_msg_R); } /* -------------------- */ /* * This one makes a test msg and sends that message to the 'remote' */ _m_msg_S = rtp_msg_new ( _m_session, test_bytes, 280 ) ; rtp_send_msg ( _m_session, _m_msg_S, _socket ); usleep ( 10000 ); /* -------------------- */ } return SUCCESS; }
/** parse commandline arguments */ static NftResult _parse_args(int argc, char *argv[]) { int index, argument; static struct option loptions[] = { {"help", 0, 0, 'h'}, {"plugin-help", 0, 0, 'p'}, {"loglevel", required_argument, 0, 'l'}, {"config", required_argument, 0, 'c'}, {"x", required_argument, 0, 'x'}, {"y", required_argument, 0, 'y'}, {"dimensions", required_argument, 0, 'd'}, {"fps", required_argument, 0, 'f'}, {"mechanism", required_argument, 0, 'm'}, {0,0,0,0} }; while((argument = getopt_long(argc, argv, "hpl:c:x:y:d:f:m:", loptions, &index)) >= 0) { switch(argument) { /** --help */ case 'h': { _print_help(argv[0]); return NFT_FAILURE; } /* --plugin-help */ case 'p': { _print_plugin_help(); return NFT_FAILURE; } /* --mechanism */ case 'm': { _c.method = capture_method_from_string(optarg); break; } /** --config */ case 'c': { /* save filename for later */ strncpy(_c.prefsfile, optarg, sizeof(_c.prefsfile)); break; } /** --x */ case 'x': { if(sscanf(optarg, "%d", (int*) &_c.x) != 1) { NFT_LOG(L_ERROR, "Invalid x-coordinate \"%s\" (Use an integer)", optarg); return NFT_FAILURE; } break; } /** --y */ case 'y': { if(sscanf(optarg, "%d", (int*) &_c.y) != 1) { NFT_LOG(L_ERROR, "Invalid y-coordinate \"%s\" (Use an integer)", optarg); return NFT_FAILURE; } break; } /** --dimensions */ case 'd': { if(sscanf(optarg, "%dx%d", (int*) &_c.width, (int*) &_c.height) != 2) { NFT_LOG(L_ERROR, "Invalid dimension \"%s\" (Use something like 320x400)", optarg); return NFT_FAILURE; } break; } /** --fps */ case 'f': { if(sscanf(optarg, "%d", (int*) &_c.fps) != 1) { NFT_LOG(L_ERROR, "Invalid framerate \"%s\" (Use an integer)", optarg); return NFT_FAILURE; } break; } /** --loglevel */ case 'l': { if(!nft_log_level_set(nft_log_level_from_string(optarg))) { _print_loglevels(); return NFT_FAILURE; } break; } /* invalid argument */ case '?': { NFT_LOG(L_ERROR, "argument %d is invalid", index); _print_help(argv[0]); return NFT_FAILURE; } /* unhandled arguments */ default: { NFT_LOG(L_ERROR, "argument %d is invalid", index); break; } } } return NFT_SUCCESS; }
int OnlineSession::run(istream &in, bool interactive) { map<string, int> commands; _prepareCommands(commands); int res = 0; int count = 0; int cmd; string command; string sarg1, sarg2; int iarg1, iarg2, iarg3, iarg4, iarg5, iarg6, iarg7, iarg8; SeriesDistanceMetric *metric; double darg1; GroupableTimeSeriesSet *t; clock_t time; res = 0; while (!in.eof()) { if (res != 0) getout() << "Command returned with status " << res << "." << endl; if (interactive) { int width = getout().width(); getout() << "["; getout().width(3); getout() << count; getout().width(width); getout() << "] > "; getout().flush(); } in >> command; if (command.size() > 0 && !in.eof()) { cmd = commands[command]; } else { cmd = _EXIT; getout() << endl; } time = clock(); try { res = 0; switch (cmd) { case 0: getout() << "Unknown command '" << command << "'. Type 'help' for help." << endl; res = 1; break; case _EXIT: getout() << "Quitting..." << endl; return 0; case _HELP: _print_help(getout()); break; case _DEBUG: in >> iarg1; getout() << "Setting verbosity to " << iarg1 << "." << endl; verbosity = iarg1; break; case _LOAD_TSS: in >> sarg1; getout() << "Loading Time Series Set from file '" << sarg1 << "'." << endl; res = loaddb(sarg1.c_str()); if (res == 0) { getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl; } else { getout() << "Failed to load dataset." << endl; } break; case _SAVE_TSS: in >> iarg1; in >> sarg2; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Saving Time Series Set " << iarg1 << ":" << t->getName() << " to file '" << sarg2 << "'." << endl; res = savedb(iarg1, sarg2.c_str()); if (res == 0) getout() << "Dataset successfully saved." << endl; else getout() << "Failed to save dataset." << endl; break; case _OLOAD_TSS: in >> sarg1; in >> iarg1 >> iarg2 >> iarg3; getout() << "Loading Time Series Set from file '" << sarg1 << "' with N=" << iarg1 << ", L=" << iarg2 << ", and D=" << iarg3 << "." << endl; res = loadolddb(sarg1.c_str(), iarg1, iarg2, iarg3); if (res == 0) { getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl; } else { getout() << "Failed to load dataset." << endl; } break; case _OSAVE_TSS: in >> iarg1; in >> sarg2; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Saving Time Series Set " << iarg1 << ":" << t->getName() << " to file '" << sarg2 << "'." << endl; res = saveolddb(iarg1, sarg2.c_str()); if (res == 0) getout() << "Dataset successfully saved." << endl; else getout() << "Failed to save dataset." << endl; break; case _DROP_TSS: in >> iarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Dropping Time Series Set " << iarg1 << ":" << t->getName() << "." << endl; killdb(iarg1); break; case _RAND_TSS: in >> iarg1 >> iarg2 >> iarg3; getout() << "Generating random Time Series Set with N=" << iarg1 << ", L=" << iarg2 << ", and range=" << iarg3 << "." << endl; res = randdb(iarg3, iarg1, iarg2); if (res == 0) getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl; else getout() << "Failed to load dataset." << endl; break; case _LIST_TSS: res = printdbs(); break; case _NORM_TSS: in >> iarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Normalizing Time Series Set " << iarg1 << ":" << t->getName() << "." << endl; t->normalize(); break; case _KSIM_TSS: in >> iarg1 >> iarg2 >> iarg3 >> iarg4 >> iarg5 >> iarg6; checkIndex(iarg1); checkIndex(iarg2); t = datasets[iarg1]; getout() << "Searching similar sequences for Time Series Set " << iarg1 << ":" << t->getName(); t = datasets[iarg2]; getout() << ", query string at " << iarg3 << " in dataset " << iarg2 << ":" << t->getName(); getout() << " in interval [" << iarg4 << "," << iarg5 << "] with strategy=" << iarg6 << "." << endl; similar(iarg1, iarg2, iarg3, TimeInterval(iarg4, iarg5), iarg6); break; case _OUTLIER_TSS: in >> iarg1 >> iarg2; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Searching for outlier group in dataset " << iarg1 << ":" << t->getName() << " for length=" << iarg2 << "." << endl; outlier(iarg1, iarg2); break; case _TSS_DIST: in >> iarg1 >> iarg2 >> iarg3 >> iarg4; in >> iarg5 >> iarg6 >> iarg7 >> iarg8; in >> sarg1; checkIndex(iarg1); checkIndex(iarg5); metric = getDistMetric(sarg1.c_str()); if (metric == NULL) { getout() << "Unknown method: " << sarg1.c_str() << endl; res = -1; break; } getout() << "Using distance metric " << metric->name << " to find distance:" << endl; getout() << "A: DB:" << iarg1 << ", " << iarg2 << "@[" << iarg3 << "," << iarg4 << "]." << endl; getout() << "B: DB:" << iarg5 << ", " << iarg6 << "@[" << iarg7 << "," << iarg8 << "]." << endl; getout() << "Distance: " << printdist(iarg1, iarg5, iarg2, iarg6, TimeInterval(iarg3, iarg4), TimeInterval(iarg7, iarg8), metric) << endl; break; case _LS_DIST: printDistMetrics(); break; case _GROUP_TSS: in >> iarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Generating new grouping for Time Series Set " << iarg1 << ":" << t->getName() << " with ST " << defaultST << "." << endl; res = initdbgroups(iarg1, defaultST); break; case _GROUP_ST_TSS: in >> iarg1; in >> darg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Generating new grouping for Time Series Set " << iarg1 << ":" << t->getName() << " with ST " << darg1 << "." << endl; res = initdbgroups(iarg1, darg1); break; case _LOAD_GROUP_TSS: in >> iarg1; in >> sarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Loading grouping data for Time Series Set " << iarg1 << ":" << t->getName() << " at " << sarg1 << "." << endl; res = loaddbgroups(iarg1, sarg1.c_str()); break; case _SAVE_GROUP_TSS: in >> iarg1; in >> sarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Saving grouping data for Time Series Set " << iarg1 << ":" << t->getName() << " to " << sarg1 << "." << endl; res = savedbgroups(iarg1, sarg1.c_str()); break; case _DROP_GROUP_TSS: in >> iarg1; checkIndex(iarg1); t = datasets[iarg1]; getout() << "Dropping grouping data for Time Series Set " << iarg1 << ":" << t->getName() << "." << endl; res = killdbgroups(iarg1); break; case _DESC_TSS: in >> iarg1; checkIndex(iarg1); res = descdb(iarg1); break; case _PRINT_TSS: in >> iarg1; checkIndex(iarg1); res = printdb(iarg1); break; case _PRINT_INTERVAL: in >> iarg1 >> iarg2 >> iarg3 >> iarg4; checkIndex(iarg1); res = printint(iarg1, iarg2, TimeInterval(iarg3, iarg4)); break; case _SET_DEF_ST: in >> darg1; getout() << "Setting default ST to " << darg1 << "." << endl; res = setST(darg1); break; case _GET_DEF_ST: getout() << "default ST = " << getST() << "." << endl; break; case _SET_DEF_R: in >> iarg1; getout() << "Setting default R constraint to " << iarg1 << "." << endl; res = setR(iarg1); break; case _GET_DEF_R: getout() << "default R = " << getR() << "." << endl; break; } } catch (exception &e) { getout() << "Caught exception attempting operation:" << endl; getout() << e.what() << endl; } time = clock() - time; getout() << "Command used " << ((float)time/CLOCKS_PER_SEC) << " seconds." << endl << endl; count++; } return 0; }
/* We need this */ int main(int argc, char *argv[]) { int optc, show_help, show_version, show_usage; char *local_file, *cmd_listen_port, *cmd_pid_file; int inetd_mode, no_daemon; /* Set up some globals */ progname = argv[0]; listen_port = x_strdup(DEFAULT_LISTEN_PORT); pid_file = (DEFAULT_PID_FILE ? x_strdup(DEFAULT_PID_FILE) : 0); #ifndef DEBUG no_daemon = 0; #else /* DEBUG */ no_daemon = 1; #endif /* DEBUG */ local_file = cmd_listen_port = cmd_pid_file = 0; show_help = show_version = show_usage = inetd_mode = 0; while ((optc = getopt_long(argc, argv, GETOPTIONS, long_opts, NULL)) != -1) { switch (optc) { case 'h': show_help = 1; break; case 'v': show_version = 1; break; case 'D': #ifndef DEBUG no_daemon = 1; #else /* DEBUG */ no_daemon = 0; #endif /* DEBUG */ break; case 'I': inetd_mode = 1; break; case 'P': free(cmd_listen_port); cmd_listen_port = x_strdup(optarg); break; case 'p': free(cmd_pid_file); cmd_pid_file = x_strdup(optarg); break; case 'f': free(local_file); local_file = x_strdup(optarg); break; default: show_usage = 1; break; } } if (show_usage || (optind < argc)) { _print_usage(); return 1; } if (show_version) { _print_version(); if (!show_help) return 0; } if (show_help) { _print_help(); return 0; } /* If no -f was specified use the home directory */ if (!local_file && !inetd_mode) { struct stat statinfo; struct passwd *pw; pw = getpwuid(geteuid()); if (pw && pw->pw_dir) { local_file = x_sprintf("%s/%s", pw->pw_dir, USER_CONFIG_FILENAME); debug("Local config file: %s", local_file); if (!stat(local_file, &statinfo) && (statinfo.st_mode & 0077)) { fprintf(stderr, "%s: Permissions of %s must be 0700 or " "more restrictive\n", progname, local_file); free(local_file); return 2; } if (cfg_read(local_file, &listen_port, &pid_file, &g)) { /* If the local one didn't exist, set to 0 so we open global one */ free(local_file); local_file = 0; } else { config_file = x_strdup(local_file); } } } else if (local_file) { if (cfg_read(local_file, &listen_port, &pid_file, &g)) { /* This is fatal! */ fprintf(stderr, "%s: Couldn't read configuration from %s: %s\n", progname, local_file, strerror(errno)); free(local_file); return 2; } else { config_file = x_strdup(local_file); } } /* Read global config file if local one not found */ if (!local_file) { char *global_file; /* Not fatal if it doesn't exist */ global_file = x_sprintf("%s/%s", SYSCONFDIR, GLOBAL_CONFIG_FILENAME); debug("Global config file: %s", global_file); cfg_read(global_file, &listen_port, &pid_file, &g); config_file = x_strdup(global_file); free(global_file); } else { free(local_file); } /* Check we got some connection classes */ if (!connclasses) { fprintf(stderr, "%s: No connection classes have been defined.\n", progname); return 2; } /* -P overrides config file */ if (cmd_listen_port) { free(listen_port); listen_port = cmd_listen_port; } /* -p overrides pid file */ if (cmd_pid_file) { free(pid_file); pid_file = cmd_pid_file; } /* Set signal handlers */ signal(SIGTERM, _sig_term); signal(SIGINT, _sig_term); signal(SIGHUP, _sig_hup); signal(SIGCHLD, _sig_child); #ifdef DEBUG_MEMORY signal(SIGUSR1, _sig_usr); signal(SIGUSR2, _sig_usr); #endif /* DEBUG_MEMORY */ /* Broken Pipe? This means that someone disconnected while we were sending stuff. Naughty! */ signal(SIGPIPE, SIG_IGN); if (!inetd_mode) { debug("Ordinary console dodge-monkey mode"); /* Make listening socket before we fork */ if (ircnet_listen(listen_port)) { fprintf(stderr, "%s: Unable to establish listen port\n", progname); return 3; } /* go daemon here */ if (!no_daemon) { switch (go_daemon()) { case -1: return -1; case 0: break; default: return 0; } } } else { /* running under inetd means we are backgrounded right *now* */ in_background = 1; debug("Inetd SuperTed mode!"); /* Hook STDIN into a new proxy */ ircnet_hooksocket(STDIN_FILENO); } /* Open a connection to syslog if we're in the background */ if (in_background) openlog(PACKAGE, LOG_PID, LOG_USER); if (pid_file) { FILE *pidfile; pidfile = fopen(pid_file, "w"); if (pidfile) { fchmod(fileno(pidfile), 0600); fprintf(pidfile, "%d\n", getpid()); fclose(pidfile); } else { syscall_fail("fopen", pid_file, 0); } } /* Main loop! */ while (!stop_poll) { int ns, nt, status; pid_t pid; ircnet_expunge_proxies(); dccnet_expunge_proxies(); ns = net_poll(); nt = timer_poll(); /* Reap any children */ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { debug("Reaped process %d, exit status %d", pid, status); /* Handle any DNS children */ dns_endrequest(pid, status); } /* Reload the configuration file? */ if (reload_config) { _reload_config(); reload_config = 0; } if (!ns && !nt) break; } if (pid_file) { unlink(pid_file); } /* Free up stuff */ ircnet_flush(); dccnet_flush(); dns_flush(); timer_flush(); /* Do a lingering close on all sockets */ net_closeall(); net_flush(); /* Close down and free up memory */ if (!inetd_mode && !no_daemon) closelog(); free(listen_port); free(pid_file); free(config_file); #ifdef DEBUG_MEMORY mem_report("termination"); #endif /* DEBUG_MEMORY */ return 0; }