vm_connection_t vm_connect() { unsigned long port; char* host = NULL; char* inet = NULL; vm_connection_t con = VOICEMAN_BAD_CONNECTION; char* vm=getenv("VOICEMAN"); if (vm == NULL || vm[0] == '\0') return vm_connect_unix(VOICEMAN_DEFAULT_SOCKET); inet=check_inet_prefix(vm); if (inet == NULL) return vm_connect_unix(vm); host = (char*)malloc(strlen(inet) + 1); if (host == NULL) return VOICEMAN_BAD_CONNECTION; if (!parse_inet_str(inet, host, &port)) { free(host); return VOICEMAN_BAD_CONNECTION; } con = vm_connect_inet(host, port); free(host); return con; }
int main(int argc, char* argv[]) { if (!cmdLine.parse(argc, argv)) return 1; if (cmdLine.used("help")) { printHelp(); return 0; } //Establishing connection; if (cmdLine.used("host")) { const std::string param = cmdLine["host"]; std::string h=getDelimitedSubStr(param, 0, ':'); const std::string p=getDelimitedSubStr(param, 1, ':'); if (trim(h).empty()) h="localhost"; size_t port; if (!trim(p).empty()) { if (!checkTypeUnsignedInt(p)) { std::cerr << ERROR_PREFIX << "\'" << trim(p) << "\' is not a valid port number." << std::endl; return 1; } port = parseAsUnsignedInt(p); } else port=VOICEMAN_DEFAULT_PORT; con = vm_connect_inet((char*)h.c_str(), port); if (con == VOICEMAN_BAD_CONNECTION) { std::cerr << ERROR_PREFIX << "ERROR:Could not connect to host \'" << trim(h) << "\' with port " << port << "." << std::endl; return 1; } } else if (cmdLine.used("socket")) { std::string p = cmdLine["socket"]; if (trim(p).empty()) { std::cerr << ERROR_PREFIX << "Missed name of UNIX domain socket." << std::endl; return 1; } if (trim(p) == "-") p = VOICEMAN_DEFAULT_SOCKET; con = vm_connect_unix((char*)p.c_str()); if (con == VOICEMAN_BAD_CONNECTION) { std::cerr << ERROR_PREFIX << "Could not connect to server via UNIX domain socket \'" << p << "\'." << std::endl; return 1; } } else { con = vm_connect(); if (con == VOICEMAN_BAD_CONNECTION) { std::cerr << ERROR_PREFIX << "Could not connect to voicemand with default settings." << std::endl; return 1; } } ConnectionAutoClosing autoClosing(con); struct sigaction sa; sigaction(SIGPIPE, NULL, &sa); sa.sa_handler = sigPipeHandler; sa.sa_flags |= SA_RESTART; sigaction(SIGPIPE, &sa, NULL); //INitial connection parameters; assert(con != VOICEMAN_BAD_CONNECTION); if (cmdLine.used("stop")) vm_stop(con); if (cmdLine.used("pitch")) { const std::string value = trim(cmdLine["pitch"]); if (!checkTypeUnsignedInt(value)) { std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid pitch value" << std::endl; return 1; } vm_pitch(con, parseAsUnsignedInt(value)); } if (cmdLine.used("rate")) { const std::string value = trim(cmdLine["rate"]); if (!checkTypeUnsignedInt(value)) { std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid rate value" << std::endl; return 1; } vm_rate(con, parseAsUnsignedInt(value)); } if (cmdLine.used("volume")) { const std::string value = trim(cmdLine["volume"]); if (!checkTypeUnsignedInt(value)) { std::cerr << ERROR_PREFIX << "\'" << value << "\' is not a valid volume value" << std::endl; return 1; } vm_volume(con, parseAsUnsignedInt(value)); } if (cmdLine.used("family")) { //Neither trim() nor toLower() functions must be applied to teh value; //Let do it by the server itself; const std::string value = cmdLine["family"]; if (trim(value).empty()) { std::cerr << ERROR_PREFIX << "voice family specification has an empty string value" << std::endl; return 1; } for(std::string::size_type i = 0;i < value.length();i++) if (value[i] == ':' || value[i] == '\n') { std::cerr << ERROR_PREFIX << "voice family specification cannot contain \':\' and new line characters" << std::endl; return 1; } vm_family(con, VOICEMAN_LANG_NONE, (char*)value.c_str()); } if (cmdLine.used("punc")) { const std::string value = trim(toLower(cmdLine["punc"])); if (value == "all") vm_procmode(con, VOICEMAN_PROCMODE_ALL); else if (value == "some") vm_procmode(con, VOICEMAN_PROCMODE_SOME); else if (value == "none") vm_procmode(con, VOICEMAN_PROCMODE_NONE); else { std::cerr << ERROR_PREFIX << "punctuation mode can be only \'all\', \'some\' or \'none\'" << std::endl; return 1; } } if (cmdLine.used("say")) { std::string value; for(size_t i = 0;i < cmdLine.files.size();i++) attachStringWithSpace(value, cmdLine.files[i]); vm_text(con, (char*)encodeUTF8(IO2WString(trim(value))).c_str()); return 0; } if (cmdLine.used("stop")) return 0; std::cout << "VOICEMAN speech system. Version: " << PACKAGE_VERSION << "." << std::endl; std::cout << "Type \'quit\' or press Ctrl+D to leave this prompt." << std::endl; while(1) { std::string l; bool toQuit = 0; std::cout << "voiceman>"; while(1) { char c; if (!std::cin.get(c)) { toQuit = 1; break; } if (c == '\r') continue; if (c == '\n') break; l += c; } if (toQuit || trim(toLower(l)) == "quit") break; process(l); } std::cout << std::endl; std::cout << "Bye!!!" << std::endl; return 0; }