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

📄 asterisk.c

📁 在asterisk平台写注册命令的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
									case 1:										snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg1);										break;									case 2:										snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg2);										break;									case 3:										snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg3);										break;									case 4:										snprintf(p, sizeof(prompt) - strlen(prompt), "%d/%d", actproc, totproc);										break;									case 5:										snprintf(p, sizeof(prompt) - strlen(prompt), "%d", npid);										break;								}							}						}						break;#endif					case 't': /* time */						memset(&tm, 0, sizeof(struct tm));						time(&ts);						if (localtime_r(&ts, &tm)) {							strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);						}						break;					case '#': /* process console or remote? */						if (! option_remote) {							strncat(p, "#", sizeof(prompt) - strlen(prompt) - 1);						} else {							strncat(p, ">", sizeof(prompt) - strlen(prompt) - 1);						}						break;					case '%': /* literal % */						strncat(p, "%", sizeof(prompt) - strlen(prompt) - 1);						break;					case '\0': /* % is last character - prevent bug */						t--;						break;				}				while (*p != '\0') {					p++;				}				t++;			} else {				*p = *t;				p++;				t++;			}		}		if (color_used) {			/* Force colors back to normal at end */			term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code));			if (strlen(term_code) > sizeof(prompt) - strlen(prompt)) {				strncat(prompt + sizeof(prompt) - strlen(term_code) - 1, term_code, strlen(term_code));			} else {				strncat(p, term_code, sizeof(term_code));			}		}	} else if (remotehostname)		snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);	else		snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT);	return(prompt);	}static char **ast_el_strtoarr(char *buf){	char **match_list = NULL, *retstr;	size_t match_list_len;	int matches = 0;	match_list_len = 1;	while ( (retstr = strsep(&buf, " ")) != NULL) {		if (!strcmp(retstr, AST_CLI_COMPLETE_EOF))			break;		if (matches + 1 >= match_list_len) {			match_list_len <<= 1;			match_list = realloc(match_list, match_list_len * sizeof(char *));		}		match_list[matches++] = strdup(retstr);	}	if (!match_list)		return (char **) NULL;	if (matches>= match_list_len)		match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));	match_list[matches] = (char *) NULL;	return match_list;}static int ast_el_sort_compare(const void *i1, const void *i2){	char *s1, *s2;	s1 = ((char **)i1)[0];	s2 = ((char **)i2)[0];	return strcasecmp(s1, s2);}static int ast_cli_display_match_list(char **matches, int len, int max){	int i, idx, limit, count;	int screenwidth = 0;	int numoutput = 0, numoutputline = 0;	screenwidth = ast_get_termcols(STDOUT_FILENO);	/* find out how many entries can be put on one line, with two spaces between strings */	limit = screenwidth / (max + 2);	if (limit == 0)		limit = 1;	/* how many lines of output */	count = len / limit;	if (count * limit < len)		count++;	idx = 1;	qsort(&matches[0], (size_t)(len + 1), sizeof(char *), ast_el_sort_compare);	for (; count > 0; count--) {		numoutputline = 0;		for (i=0; i < limit && matches[idx]; i++, idx++) {			/* Don't print dupes */			if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {				i--;				free(matches[idx]);				matches[idx] = NULL;				continue;			}			numoutput++;			numoutputline++;			fprintf(stdout, "%-*s  ", max, matches[idx]);			free(matches[idx]);			matches[idx] = NULL;		}		if (numoutputline > 0)			fprintf(stdout, "\n");	}	return numoutput;}static char *cli_complete(EditLine *el, int ch){	int len=0;	char *ptr;	int nummatches = 0;	char **matches;	int retval = CC_ERROR;	char buf[2048];	int res;	LineInfo *lf = (LineInfo *)el_line(el);	*(char *)lf->cursor = '\0';	ptr = (char *)lf->cursor;	if (ptr) {		while (ptr > lf->buffer) {			if (isspace(*ptr)) {				ptr++;				break;			}			ptr--;		}	}	len = lf->cursor - ptr;	if (option_remote) {		snprintf(buf, sizeof(buf),"_COMMAND NUMMATCHES \"%s\" \"%s\"", lf->buffer, ptr); 		fdprint(ast_consock, buf);		res = read(ast_consock, buf, sizeof(buf));		buf[res] = '\0';		nummatches = atoi(buf);		if (nummatches > 0) {			char *mbuf;			int mlen = 0, maxmbuf = 2048;			/* Start with a 2048 byte buffer */			mbuf = malloc(maxmbuf);			if (!mbuf)				return (char *)(CC_ERROR);			snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); 			fdprint(ast_consock, buf);			res = 0;			mbuf[0] = '\0';			while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {				if (mlen + 1024 > maxmbuf) {					/* Every step increment buffer 1024 bytes */					maxmbuf += 1024;					mbuf = realloc(mbuf, maxmbuf);					if (!mbuf)						return (char *)(CC_ERROR);				}				/* Only read 1024 bytes at a time */				res = read(ast_consock, mbuf + mlen, 1024);				if (res > 0)					mlen += res;			}			mbuf[mlen] = '\0';			matches = ast_el_strtoarr(mbuf);			free(mbuf);		} else			matches = (char **) NULL;	} else {		nummatches = ast_cli_generatornummatches((char *)lf->buffer,ptr);		matches = ast_cli_completion_matches((char *)lf->buffer,ptr);	}	if (matches) {		int i;		int matches_num, maxlen, match_len;		if (matches[0][0] != '\0') {			el_deletestr(el, (int) len);			el_insertstr(el, matches[0]);			retval = CC_REFRESH;		}		if (nummatches == 1) {			/* Found an exact match */			el_insertstr(el, " ");			retval = CC_REFRESH;		} else {			/* Must be more than one match */			for (i=1, maxlen=0; matches[i]; i++) {				match_len = strlen(matches[i]);				if (match_len > maxlen)					maxlen = match_len;			}			matches_num = i - 1;			if (matches_num >1) {				fprintf(stdout, "\n");				ast_cli_display_match_list(matches, nummatches, maxlen);				retval = CC_REDISPLAY;			} else { 				el_insertstr(el," ");				retval = CC_REFRESH;			}		}	free(matches);	}	return (char *)(long)retval;}static int ast_el_initialize(void){	HistEvent ev;	char *editor = getenv("AST_EDITOR");	if (el != NULL)		el_end(el);	if (el_hist != NULL)		history_end(el_hist);	el = el_init("asterisk", stdin, stdout, stderr);	el_set(el, EL_PROMPT, cli_prompt);	el_set(el, EL_EDITMODE, 1);			el_set(el, EL_EDITOR, editor ? editor : "emacs");			el_hist = history_init();	if (!el || !el_hist)		return -1;	/* setup history with 100 entries */	history(el_hist, &ev, H_SETSIZE, 100);	el_set(el, EL_HIST, history, el_hist);	el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);	/* Bind <tab> to command completion */	el_set(el, EL_BIND, "^I", "ed-complete", NULL);	/* Bind ? to command completion */	el_set(el, EL_BIND, "?", "ed-complete", NULL);	/* Bind ^D to redisplay */	el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);	return 0;}static int ast_el_add_history(char *buf){	HistEvent ev;	if (el_hist == NULL || el == NULL)		ast_el_initialize();	if (strlen(buf) > 256)		return 0;	return (history(el_hist, &ev, H_ENTER, buf));}static int ast_el_write_history(char *filename){	HistEvent ev;	if (el_hist == NULL || el == NULL)		ast_el_initialize();	return (history(el_hist, &ev, H_SAVE, filename));}static int ast_el_read_history(char *filename){	char buf[256];	FILE *f;	int ret = -1;	if (el_hist == NULL || el == NULL)		ast_el_initialize();	if ((f = fopen(filename, "r")) == NULL)		return ret;	while (!feof(f)) {		fgets(buf, sizeof(buf), f);		if (!strcmp(buf, "_HiStOrY_V2_\n"))			continue;		if (ast_all_zeros(buf))			continue;		if ((ret = ast_el_add_history(buf)) == -1)			break;	}	fclose(f);	return ret;}static void ast_remotecontrol(char * data){	char buf[80];	int res;	char filename[80] = "";	char *hostname;	char *cpid;	char *version;	int pid;	char tmp[80];	char *stringp=NULL;	char *ebuf;	int num = 0;	read(ast_consock, buf, sizeof(buf));	if (data)		write(ast_consock, data, strlen(data) + 1);	stringp=buf;	hostname = strsep(&stringp, "/");	cpid = strsep(&stringp, "/");	version = strsep(&stringp, "\n");	if (!version)		version = "<Version Unknown>";	stringp=hostname;	strsep(&stringp, ".");	if (cpid)		pid = atoi(cpid);	else		pid = -1;	snprintf(tmp, sizeof(tmp), "set verbose atleast %d", option_verbose);	fdprint(ast_consock, tmp);	snprintf(tmp, sizeof(tmp), "set debug atleast %d", option_debug);	fdprint(ast_consock, tmp);	ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);	remotehostname = hostname;	if (getenv("HOME")) 		snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));	if (el_hist == NULL || el == NULL)		ast_el_initialize();	el_set(el, EL_GETCFN, ast_el_read_char);	if (!ast_strlen_zero(filename))		ast_el_read_history(filename);	if (option_exec && data) {  /* hack to print output then exit if asterisk -rx is used */		char tempchar;		struct pollfd fds;		fds.fd = ast_consock;		fds.events = POLLIN;		fds.revents = 0;		while (poll(&fds, 1, 100) > 0)			ast_el_read_char(el, &tempchar);		return;	}	for(;;) {		ebuf = (char *)el_gets(el, &num);		if (!ast_strlen_zero(ebuf)) {			if (ebuf[strlen(ebuf)-1] == '\n')				ebuf[strlen(ebuf)-1] = '\0';			if (!remoteconsolehandler(ebuf)) {				res = write(ast_consock, ebuf, strlen(ebuf) + 1);				if (res < 1) {					ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));					break;				}			}		}	}	printf("\nDisconnected from Asterisk server\n");}static int show_version(void){	printf("Asterisk " ASTERISK_VERSION "\n");	return 0;}static int show_cli_help(void) {	printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2005, Digium, Inc. and others.\n");	printf("Usage: asterisk [OPTIONS]\n");	printf("Valid Options:\n");	printf("   -V              Display version number and exit\n");	printf("   -C <configfile> Use an alternate configuration file\n");	printf("   -G <group>      Run as a group other than the caller\n");	printf("   -U <user>       Run as a user other than the caller\n");	printf("   -c              Provide console CLI\n");	printf("   -d              Enable extra debugging\n");	printf("   -f              Do not fork\n");	printf("   -g              Dump core in case of a crash\n");	printf("   -h              This help screen\n");	printf("   -i              Initialize crypto keys at startup\n");	printf("   -n              Disable console colorization\n");	printf("   -p              Run as pseudo-realtime thread\n");	printf("   -q              Quiet mode (suppress output)\n");	printf("   -r              Connect to Asterisk on this machine\n");	printf("   -R              Connect to Asterisk, and attempt to reconnect if disconnected\n");	printf("   -t              Record soundfiles in /var/tmp and move them where they belong after they are done.\n");	printf("   -T              Display the time in [Mmm dd hh:mm:ss] format for each line of output to the CLI.\n");	printf("   -v              Increase verbosity (multiple v's = more verbose)\n");	printf("   -x <cmd>        Execute command <cmd> (only valid with -r)\n");	printf("\n");	return 0;}static void ast_readconfig(void) {	struct ast_config *cfg;	struct ast_variable *v;	char *config = AST_CONFIG_FILE;	if (option_overrideconfig == 1) {		cfg = ast_config_load(ast_config_AST_CONFIG_FILE);		if (!cfg)			ast_log(LOG_WARNING, "Unable to open specified master config file '%s', using built-in defaults\n", ast_config_AST_CONFIG_FILE);	} else {		cfg = ast_config_load(config);	}	/* init with buildtime config */

⌨️ 快捷键说明

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