⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 console.c

📁 Intercom 是一个 Unix系统上灵活的语音传输软件。支持标准音频压缩比如GSM, G.711, and G.72x和其他音频编码。Intercom专为高速网络设计来传输高品质的语音
💻 C
📖 第 1 页 / 共 3 页
字号:
}if (filename == NULL) {char *home = getenv("HOME");if (home == NULL) {fputs(_("HOME environment variable not set and no -f option specified.\n"), stderr);return;}filename = alloca(strlen(home) + 13);sprintf(filename, "%s/.intercomrc", home);}if ((f = fopen(filename, "w")) == NULL) {fprintf(stderr, _("fopen(%s): %s\n"), filename, strerror(errno));return;}val = malloc(64);ti = time(NULL);tm = localtime(&ti);strftime(val, 64, "%Y-%m-%d %H:%M:%S", tm);fprintf(f, "# This file was generated by intercom version " VERSION "\n""# on %s.\n\n",val);if (!novariable) {fputs("# Configuration variables\n", f);for (o = 0; useropts[o].key != NULL; o++) {if (!strcmp(useropts[o].def_value, useropts[o].value))continue;key = realloc(key, strlen(useropts[o].key) * 2 + 1);val = realloc(val, strlen(useropts[o].value) * 2 + 1);escapestr(useropts[o].key, key);escapestr(useropts[o].value, val);if (useropts[o].desc != NULL)fprintf(f, "# %s\n", _(useropts[o].desc));fprintf(f, "set \"%s\" \"%s\"\n", key, val);}fputs("\n", f);}if (!noalias) {fputs("# Command aliases\n", f);for (ap = alias_list; ap != NULL; ap = ap->next) {key = realloc(key, strlen(ap->cmd) * 2 + 1);val = realloc(val, strlen(ap->newcmd) * 2 + 1);escapestr(ap->cmd, key);escapestr(ap->newcmd, val);fprintf(f, "alias \"%s\" \"%s\"\n", key, val);}fputs("\n", f);}if (!nohooks) {fputs("# Event-hooks\n", f);fputs("hook clearall\n", f);for (h = hook_list; h != NULL; h = h->next) {fputs("hook add ", f);switch(h->type) {case hk_call_incoming:fputs("call_incoming", f);break;case hk_call_outgoing:fputs("call_outgoing", f);break;case hk_call_connect:fputs("call_connect", f);break;case hk_hangup_lc:fputs("hangup_lc", f);break;case hk_hangup_rmt:fputs("hangup_rmt", f);break;case hk_sighup:fputs("sighup", f);break;default:assert(0);};val = realloc(val, strlen(h->cmd) * 2 + 1);escapestr(h->cmd, val);fprintf(f, " \"%s\"\n", val);}fputs("\n", f);}if (key != NULL)free(key);if (val != NULL)free(val);fclose(f);printf(_("Configuration settings written to '%s'\n"), filename);}static void cmd_set(int ARGC, char **ARGV){static const struct option long_options[]={{"help", no_argument, NULL, 'h'},{"verbose", no_argument, NULL, 'v'},{NULL, 0, NULL, 0}};int o;int verbose = 0;optind = 0;while ((o = getopt_long(ARGC, ARGV, "hv", long_options, (int *)0)) != EOF) {switch(o) {case 'h':printf(_("Usage: %s [options] [<variable>] [<value>]\n\n""Valid options:\n""-h, --help\tDisplay this help information\n""-v, --verbose\tDisplay a variable\'s new value when set\n\n""If <value> is omited, the current value is displayed.\n""If <variable> is omited, all variables with their values are displayed.\n\n"),ARGV[0]);return;case 'v':verbose = 1;break;default:fprintf(stderr, _("Try '%s --help' for a usage summary.\n"), ARGV[0]);return;}}if (!(ARGC - optind)) {puts(_("Currently defined variables:\n"));for (o = 0; useropts[o].key != NULL; o++)switch(useropts[o].type) {case opt_int:case opt_float:printf("%s = %s (%s)\n", useropts[o].key, useropts[o].value, useropts[o].desc);break;case opt_str:printf("%s = '%s' (%s)\n", useropts[o].key,(!strcmp(useropts[o].key, "crypt_passphrase")) ? _("(not shown)") :useropts[o].value, useropts[o].desc);break;default:assert(0);}putchar('\n');} else if ((ARGC - optind) == 1) {o = opt_find(ARGV[optind]);if (o == -1)fprintf(stderr, _("%s: No such variable (%s).\n"), ARGV[0], ARGV[optind]);elseswitch(useropts[o].type) {case opt_int:case opt_float:printf("%s = %s\n", useropts[o].key, useropts[o].value);break;case opt_str:printf("%s = '%s'\n", useropts[o].key,(!strcmp(useropts[o].key, "crypt_passphrase")) ? _("(not shown)") :useropts[o].value);break;default:assert(0);}} else if ((ARGC - optind) == 2) {o = opt_find(ARGV[optind]);if (o == -1)fprintf(stderr, _("%s: No such variable (%s).\n"), ARGV[0], ARGV[optind]);elseswitch(useropts[o].type) {case opt_int:opt_set_int(ARGV[optind], atoi(ARGV[optind+1]));break;case opt_float:opt_set_float(ARGV[optind], strtod(ARGV[optind+1], NULL));break;case opt_str:opt_set_str(ARGV[optind], ARGV[optind+1]);break;default:assert(0);}if (strcasecmp(ARGV[optind], "crypt_passphrase"))icsetenv(ARGV[optind], ARGV[optind+1]);} elsefprintf(stderr, _("%s: Too many command-line options. Try '%s --help' for a usage summary."),ARGV[0], ARGV[0]);}static char *munge(int v){int m = 0;int div = 1;/* TODO: fix this obvious memory leak.  I tried * * Making it a static char [64] but that always returned the same value. * * I think gcc might be optimizing where it shouldn't be or some such thing. */char *s = malloc(16);if (v >= 1073741824) {div = 1073741824;m = 'g';} else if (v >= 1048576) {div = 1048576;m = 'm';} else if (v >= 1024) {div = 1024;m = 'k';}if (m)sprintf(s, "%0.2f%c", (float)v / div, m);elsesprintf(s, "%d", v);return s;}static void cmd_stats(int ARGC, char **ARGV){audio_buf_info info;time_t t;struct tm *tm;char *s;if (ARGC > 1) {fprintf(stderr, _("Usage: '%s'.\n"), ARGV[0]);return;}if (!TEST_BIT(call.state, CALL_STATE_CONNECTED)) {fprintf(stderr, _("%s: There is no call currently in progress.\n"), ARGV[0]);return;}t = time(NULL) - call.connstats.start_time;tm = gmtime(&t);printf(_("Current connection statistics:\n""Call duration: %02d:%02d:%02d\n""Control packets sent: %u (%u bytes)\n""Control packets received: %u (%u bytes)\n\n""Audio packets sent: %u (%sb, %sb/sec)\n""Audio packets received: %u (%sb, %sb/sec)\n""Audio packets lost: %u (%u out of sequence)\n\n"),tm->tm_hour, tm->tm_min, tm->tm_sec,call.connstats.cpkts_sent, call.connstats.cbytes_sent,call.connstats.cpkts_recv, call.connstats.cbytes_recv,call.connstats.apkts_sent, munge(call.connstats.abytes_sent),munge(call.connstats.abytes_sent / t),call.connstats.apkts_recv, munge(call.connstats.abytes_recv),munge(call.connstats.abytes_recv / t),call.connstats.apkts_lost, call.connstats.apkts_order);if (TEST_BIT(call.state, CALL_STATE_AUDIOSND)) {s = str_compression(call.snd_aparams.compression);printf(_("Sending audio at %u KHZ, %u bits with compression codec %s.\n"),call.snd_aparams.rate, call.snd_aparams.bitwidth, s);if (ioctl(call.rdsp, SNDCTL_DSP_GETISPACE, &info) == -1)perror("SNDCTL_DSP_GETISPACE");elseprintf(_("Fragment size: %d, Number of fragments: %d\n"),info.fragsize, info.fragstotal);}if (TEST_BIT(call.state, CALL_STATE_AUDIORCV)) {s = str_compression(call.rcv_aparams.compression);printf(_("Receiving audio at %u KHZ, %u bits with compression codec %s.\n"),call.rcv_aparams.rate, call.rcv_aparams.bitwidth, s);if (ioctl(call.pdsp, SNDCTL_DSP_GETOSPACE, &info) == -1)perror("SNDCTL_DSP_GETOSPACE");elseprintf(_("Fragment size: %d, Number of fragments: %d, bytes in output buffer: %d\n"),info.fragsize, info.fragstotal, iob_buffsize(&call.pdspq));}putchar('\n');}static const struct console_cmd cmd_list[]={{"alias", N_("Manage command aliases."), cmd_alias},{"answer", N_("Answer an incoming call."), cmd_answer},{"call", N_("Place a call to a remote host."), cmd_call},{"copyright", N_("Show copyright and license details."), cmd_copyright},{"exec", N_("Execute external program."), cmd_exec},{"hangup", N_("Terminate the current call."), cmd_hangup},{"help", N_("Gives basic help information for valid commands."), cmd_help},{"hook", N_("Administer event-hooks"), cmd_hook},{"if", N_("Execute intercom command based on exit-code of shell command"), cmd_if},{"message", N_("Send a text message to connected party."), cmd_message},{"quit", N_("Exit from Intercom."), cmd_quit},{"read", N_("Read commands from a file."), cmd_read},{"save", N_("Save configuration information."), cmd_save},{"set", N_("Show or set configuration variables."), cmd_set},{"stats", N_("Show call statistics."), cmd_stats},{NULL, NULL, NULL},};static void cmd_help(int ARGC, char **ARGV){size_t i, l;static size_t maxlen = 0;if (ARGC > 1) {char line[strlen(ARGV[1]) + 8];sprintf(line, "%s --help", ARGV[1]);processline(0, line);} else {/* First pass to get maxlen value */if (!maxlen) {for (i = 0; cmd_list[i].cmd != NULL; i++) {l = strlen(cmd_list[i].cmd);if (l > maxlen)maxlen = l;}}puts(_("Valid commands are:"));for (i = 0; cmd_list[i].cmd != NULL; i++) {printf("%-*s %s\n", maxlen, cmd_list[i].cmd, _(cmd_list[i].desc));}putchar('\n');}}void processline(int addhistory, const char *line){wordexp_t wv;int r;static int depth = 0;size_t i;struct alias *ap;if (depth > opt_get_int("cmd_maxdepth")) {fputs(_("Error, command or alias loop detected.\n"), stderr);return;}depth++;#if (DEBUG >= 1)if (verbose)printf(_("debug: processline: processing line '%s' at depth %d.\n"), line, depth);#endif#ifdef HAVE_LIBHISTORYif (addhistory && *line)add_history(line);#endifif ((r = iwordexp(line, &wv, WRDE_UNDEF))) {switch(r) {case WRDE_BADCHAR:fputs(_("Bad character in input.\n"), stderr);break;case WRDE_BADVAL:fputs(_("Undefined shell variable in input.\n"), stderr);break;case WRDE_CMDSUB:fputs(_("Command-substitution is not enabled.\n"), stderr);break;case WRDE_NOSPACE:fputs(_("wordexp: Memory allocation failure.\n"), stderr);break;case WRDE_SYNTAX:fputs(_("Input syntax error.\n"), stderr);break;default:fputs(_("wordexp: Input error: unknown error.\n"), stderr);} /* switch */} else if (wv.we_wordc) {#if (DEBUG >= 2)if (verbose) {for (i = 0; i < wv.we_wordc; i++)printf("Debug: wordex word %d = '%s'\n", i, wv.we_wordv[i]);}#endiffor (ap = alias_list; ap != NULL; ap = ap->next) {if (!strcmp(wv.we_wordv[0], ap->cmd)) {size_t len = strlen(ap->newcmd) + 1, pos = 0, i2;char *newline;for (i2 = 1; i2 < wv.we_wordc; i2++)len += strlen(wv.we_wordv[i2]) + 1;newline = malloc(len);pos += sprintf(newline, "%s", ap->newcmd);for (i2 = 1; i2 < wv.we_wordc; i2++)pos += sprintf(&newline[pos], " %s", wv.we_wordv[i2]);processline(0, newline);goto out;}}for (i = 0; cmd_list[i].cmd != NULL; i++)if (!strcmp(wv.we_wordv[0], cmd_list[i].cmd)) {cmd_list[i].func(wv.we_wordc, wv.we_wordv);goto out;}fprintf(stderr,_("Unknown command: '%s', try 'help' for a list of valid commands.\n"), wv.we_wordv[0]);out:iwordfree(&wv);} else {iwordfree(&wv);} /* endif wordexp */depth--;}static void rl_processline(char *line){if (line == NULL) {putchar('\n');return;}processline(1, line);free(line);}#ifndef HAVE_LIBREADLINEchar *readln(void){char *line = malloc(1024);size_t l;fgets(line, 1024, stdin);l = strlen(line);if (!l) {free(line);return NULL;}if (line[l - 1] == '\n')line[l - 1] = 0;return line;}#endifvoid console_processkey(void){#ifdef HAVE_LIBREADLINErl_callback_read_char();#else/* If we're not using readline, the tty should be line buffered. */rl_processline(readln());if (console_active) {printf("%s", opt_get_str("prompt"));fflush(stdout);}#endif}void console_init(void){#ifdef HAVE_LIBREADLINErl_callback_handler_install(opt_get_str("prompt"), rl_processline);#ifdef HAVE_LIBHISTORYusing_history();asprintf(&historyfile, "%s/.intercom.history", getenv("HOME"));read_history(historyfile);history_set_pos(history_length);#endif#elseconsole_active = 1;printf("%s", opt_get_str("prompt"));fflush(stdout);#endif}void console_done(void){#ifdef HAVE_LIBREADLINErl_callback_handler_remove();#ifdef HAVE_LIBHISTORYstifle_history(opt_get_int("history_limit"));write_history(historyfile);#endif#elseconsole_active = 0;#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -