示例#1
0
文件: udp_client.c 项目: Hi-Spy/loho
int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;
	
	/* check args */
	if(argc != 2)
	{
		printf("usage: udpclient <IPaddress>\n");
		exit(1);
	}
	
	/* init servaddr */
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
	{
		printf("[%s] is not a valid IPaddress\n", argv[1]);
		exit(1);
	}
	
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	
	do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	
	return 0;
}
示例#2
0
//=============================main function===============================
int main(int argc,char *argv[])
{
	int sockfd;
	struct sockaddr_in servaddr;
	char error_message[20];

	// check args
	if (argc!=2)
	{
		printf("usage:udpclient <IP address>\n");
		exit(1);
	}

	// init servaddr
	memset(&servaddr,0,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_port=htons(SERV_PORT);
	if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)
	{
		sprintf(error_message,"[%s] is not a valid IP address\n",argv[1]);
		fputs(error_message,stderr);
		exit(1);
	}

	// build the socket, using the UDP
	sockfd=socket(AF_INET,SOCK_DGRAM,0);

	do_cli(stdin,sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));

	return 0;
}
示例#3
0
/* execute commands read from *filename;
 * ret CMD_OK if file was readable (command/parsing problems are OK)
 * ret CMD_FAILED if file was unreadable
 * forward CMD_EXIT if applicable */
static int
command_file(const char *filename)
{
	int rv;
	FILE *prev_instream = instream;

	if ( (instream=fopen(filename, "r"))) {
		rv=do_cli(root_cmd_table, progname, 0, NULL);
		fclose(instream);
		instream=prev_instream;
		return (rv==CMD_EXIT)? CMD_EXIT:CMD_OK;
	}
	instream=prev_instream;
	return CMD_FAILED;
}
示例#4
0
int main(int argc,char** argv){
    int sockfd;
    struct sockaddr_in servaddr;
    if(argc!=3){
        printf("usage:<IPaddress> <Ports>\n");
        exit(0);
    }
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    int SERV_PORT=atoi(argv[2]);
    servaddr.sin_port=htons(SERV_PORT);
    inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    do_cli(stdin,sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
    exit(0);
}
示例#5
0
/* start a cli with <name> as a prompt, and optionally run the <initscript> file */
void
enter_cli(const char *name, const char *initscript)
{
	global_logfp = NULL;
	//progname = name;	//we use the supplied *name instead.

	readline_init();
	set_init();

	if (initscript != NULL) {
		int rv=command_file(initscript);
		switch (rv) {
			case CMD_OK:
				/* script was succesful, start a normal CLI afterwards */
				break;
			case CMD_FAILED:
				printf("Problem with file %s\n", initscript);
				// fallthrough, yes
			default:
			case CMD_EXIT:
				set_close();
				return;
		}
	} else {
		/* print banner only if running without an initscript */
		printf("%s: %s version %s\n", name, projname, PACKAGE_VERSION);
		printf("%s: Type HELP for a list of commands\n", name);
		printf("%s: Type SCAN to start ODBII Scan\n", name);
		printf("%s: Then use MONITOR to monitor real-time data\n", name);
		printf("%s: **** IMPORTANT : this is beta software ! Use at your own risk.\n", name);
		printf("%s: **** Remember, \"debug all -1\" displays all debugging info.\n", name);
	}

	if (rc_file() != CMD_EXIT) {
		printf("\n");
		/* And go start CLI */
		instream = stdin;
		(void)do_cli(root_cmd_table, name, 0, NULL);
	}
	set_close();

}
示例#6
0
文件: php_cli.c 项目: EleTeam/php-src
int main(int argc, char *argv[])
#endif
{
#ifdef PHP_CLI_WIN32_NO_CONSOLE
	int argc = __argc;
	char **argv = __argv;
#endif

	int c;
	int exit_status = SUCCESS;
	int module_started = 0, sapi_started = 0;
	char *php_optarg = NULL;
	int php_optind = 1, use_extended_info = 0;
	char *ini_path_override = NULL;
	char *ini_entries = NULL;
	int ini_entries_len = 0;
	int ini_ignore = 0;
	sapi_module_struct *sapi_module = &cli_sapi_module;

	/*
	 * Do not move this initialization. It needs to happen before argv is used
	 * in any way.
	 */
	argv = save_ps_args(argc, argv);

	cli_sapi_module.additional_functions = additional_functions;

#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
	{
		int tmp_flag;
		_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
		_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
		_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
		_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
		_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
		_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
		tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
		tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
		tmp_flag |= _CRTDBG_LEAK_CHECK_DF;

		_CrtSetDbgFlag(tmp_flag);
	}
#endif

#ifdef HAVE_SIGNAL_H
#if defined(SIGPIPE) && defined(SIG_IGN)
	signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
								that sockets created via fsockopen()
								don't kill PHP if the remote site
								closes it.  in apache|apxs mode apache
								does that for us!  [email protected]
								20000419 */
#endif
#endif


#ifdef ZTS
	tsrm_startup(1, 1, 0, NULL);
	(void)ts_resource(0);
	ZEND_TSRMLS_CACHE_UPDATE();
#endif

#ifdef ZEND_SIGNALS
	zend_signal_startup();
#endif

#ifdef PHP_WIN32
	_fmode = _O_BINARY;			/*sets default for file streams to binary */
	setmode(_fileno(stdin), O_BINARY);		/* make the stdio mode be binary */
	setmode(_fileno(stdout), O_BINARY);		/* make the stdio mode be binary */
	setmode(_fileno(stderr), O_BINARY);		/* make the stdio mode be binary */
#endif

	while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
		switch (c) {
			case 'c':
				if (ini_path_override) {
					free(ini_path_override);
				}
 				ini_path_override = strdup(php_optarg);
				break;
			case 'n':
				ini_ignore = 1;
				break;
			case 'd': {
				/* define ini entries on command line */
				int len = (int)strlen(php_optarg);
				char *val;

				if ((val = strchr(php_optarg, '='))) {
					val++;
					if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
						memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
						ini_entries_len += (int)(val - php_optarg);
						memcpy(ini_entries + ini_entries_len, "\"", 1);
						ini_entries_len++;
						memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
						ini_entries_len += len - (int)(val - php_optarg);
						memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
						ini_entries_len += sizeof("\n\0\"") - 2;
					} else {
						ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
						memcpy(ini_entries + ini_entries_len, php_optarg, len);
						memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
						ini_entries_len += len + sizeof("\n\0") - 2;
					}
				} else {
					ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
					memcpy(ini_entries + ini_entries_len, php_optarg, len);
					memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
					ini_entries_len += len + sizeof("=1\n\0") - 2;
				}
				break;
			}
#ifndef PHP_CLI_WIN32_NO_CONSOLE
			case 'S':
				sapi_module = &cli_server_sapi_module;
				cli_server_sapi_module.additional_functions = server_additional_functions;
				break;
#endif
			case 'h': /* help & quit */
			case '?':
				php_cli_usage(argv[0]);
				goto out;
			case 'i': case 'v': case 'm':
				sapi_module = &cli_sapi_module;
				goto exit_loop;
			case 'e': /* enable extended info output */
				use_extended_info = 1;
				break;
		}
	}
exit_loop:

	sapi_module->ini_defaults = sapi_cli_ini_defaults;
	sapi_module->php_ini_path_override = ini_path_override;
	sapi_module->phpinfo_as_text = 1;
	sapi_module->php_ini_ignore_cwd = 1;
	sapi_startup(sapi_module);
	sapi_started = 1;

	sapi_module->php_ini_ignore = ini_ignore;

	sapi_module->executable_location = argv[0];

	if (sapi_module == &cli_sapi_module) {
		if (ini_entries) {
			ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
			memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
		} else {
			ini_entries = malloc(sizeof(HARDCODED_INI));
			memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
		}
		ini_entries_len += sizeof(HARDCODED_INI) - 2;
	}

	sapi_module->ini_entries = ini_entries;

	/* startup after we get the above ini override se we get things right */
	if (sapi_module->startup(sapi_module) == FAILURE) {
		/* there is no way to see if we must call zend_ini_deactivate()
		 * since we cannot check if EG(ini_directives) has been initialised
		 * because the executor's constructor does not set initialize it.
		 * Apart from that there seems no need for zend_ini_deactivate() yet.
		 * So we goto out_err.*/
		exit_status = 1;
		goto out;
	}
	module_started = 1;

	/* -e option */
	if (use_extended_info) {
		CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
	}

	zend_first_try {
#ifndef PHP_CLI_WIN32_NO_CONSOLE
		if (sapi_module == &cli_sapi_module) {
#endif
			exit_status = do_cli(argc, argv);
#ifndef PHP_CLI_WIN32_NO_CONSOLE
		} else {
			exit_status = do_cli_server(argc, argv);
		}
#endif
	} zend_end_try();
out:
	if (ini_path_override) {
		free(ini_path_override);
	}
	if (ini_entries) {
		free(ini_entries);
	}
	if (module_started) {
		php_module_shutdown();
	}
	if (sapi_started) {
		sapi_shutdown();
	}
#ifdef ZTS
	tsrm_shutdown();
#endif

	/*
	 * Do not move this de-initialization. It needs to happen right before
	 * exiting.
	 */
	cleanup_ps_args(argv);
	exit(exit_status);
}
示例#7
0
/*
 * CLI, returns results as CMD_xxx (such as CMD_EXIT)
 * If argc is supplied, then this is one shot cli, ie run the command
 */
static int
do_cli(const struct cmd_tbl_entry *cmd_tbl, const char *prompt, int argc, char **argv)
{
	/* Build up argc/argv */
	const struct cmd_tbl_entry *ctp;
	int cmd_argc;
	char *cmd_argv[20];
	char *input = NULL;
	int rv, done;
	int i;

	char promptbuf[PROMPTBUFSIZE];	/* Was 1024, who needs that long a prompt? (the part before user input up to '>') */
	static char nullstr[2]={0,0};

#ifdef HAVE_LIBREADLINE
	//set the current command level for command completion
	current_cmd_level = cmd_tbl;
#endif

	rv = 0, done = 0;
	snprintf(promptbuf, PROMPTBUFSIZE, "%s> ", prompt);
	while (!done) {
		char *inptr, *s;

		if (argc == 0) {
			/* Get Input */
			if (input)
				free(input);
			input = command_line_input(promptbuf);
			if (!input) {
					if (instream == stdin)
						printf("\n");
					break;
			}

			/* Parse it */
			inptr = input;
			if (*inptr == '@') 	//printing comment
			{
				printf("%s\n", inptr);
				continue;
			}
			if (*inptr == '#')		//non-printing comment
			{
				continue;
			}
			cmd_argc = 0;
			while ( (s = strtok(inptr, " \t")) != NULL ) {
				cmd_argv[cmd_argc] = s;
				inptr = NULL;
				if (cmd_argc == (ARRAY_SIZE(cmd_argv)-1))
					break;
				cmd_argc++;
			}
			cmd_argv[cmd_argc] = nullstr;
		} else {
			/* Use supplied argc */
			cmd_argc = argc;
			for (i=0; i<=argc; i++)
				cmd_argv[i] = argv[i];
		}

		if (cmd_argc != 0) {
			ctp = cmd_tbl;
			while (ctp->command) {
				if (strcasecmp(ctp->command, cmd_argv[0]) == 0) {
					if (ctp->sub_cmd_tbl) {
						log_command(1, cmd_argv);
						snprintf(promptbuf, PROMPTBUFSIZE,"%s/%s",
							prompt, ctp->command);
						/* Sub menu */
						rv = do_cli(ctp->sub_cmd_tbl,
							promptbuf,
							cmd_argc-1,
							&cmd_argv[1]);
#ifdef HAVE_LIBREADLINE
						//went out of the sub-menu, so update the command level for command completion
						current_cmd_level = cmd_tbl;
#endif
						if (rv==CMD_EXIT)	//allow exiting prog. from a submenu
							done=1;
						snprintf(promptbuf, PROMPTBUFSIZE, "%s> ", prompt);
					} else {
						/* Found command */
						log_command(cmd_argc, cmd_argv);
						rv = ctp->routine(cmd_argc, cmd_argv);
						switch (rv) {
							case CMD_USAGE:
								printf("Usage: %s\n", ctp->usage);
								break;
							case CMD_EXIT:
								rv = CMD_EXIT;
								done = 1;
								break;
							case CMD_UP:
								rv = CMD_UP;
								done = 1;
								break;
						}
					}
					break;
				}
				if (!done)
					ctp++;
			}
			if (ctp->command == NULL) {
				printf("Huh? Try \"help\"\n");
			}
			if (argc) {
				/* Single command */
				done = 1;
				break;
			}
		}
		if (done)
			break;
	}	//while !done
	if (input)
			free(input);
	if (rv == CMD_UP)
		return CMD_OK;
	if (rv == CMD_EXIT) {
		char *disco="disconnect";
		if (global_logfp != NULL)
			cmd_stoplog(0, NULL);
		if (global_state > STATE_IDLE) {
			do_cli(diag_cmd_table, "", 1, &disco);	//XXX should be called recursively in case there are >1 active L3 conns...
		}
		rv=diag_end();
		if (rv)
			fprintf(stderr, FLFMT "diag_end failed !?\n", FL);
		rv = CMD_EXIT;
	}
	return rv;
}