Esempio n. 1
0
/* readline hook to process the entered command */
static void
rl_docmd(char *line)
{
	char buf[NPH_BUF_SIZE];	/* space for an input line */

	/* EOF */
	if (line == NULL)
	{
		rl_callback_handler_remove();
		sighandler_state = HANDLER_DEFAULT;
		ph_close(ph, 0);
		exit(0);
	}

	/* blank line */
	if (*line == '\0')
	{
		free(line);
		return;
	}

	add_history(line);
	strlcpy(buf, line, sizeof(buf));
	free(line);

	/* do command */
	nph_command(buf);
	putchar('\n');
}
Esempio n. 2
0
File: init.c Progetto: chaos/nph
/* change servers */
int
nph_connect(int cmdc, char **cmdv)
{
	PH *newph;

	if (cmdv[1] == NULL
	    || cmdv[2] != NULL)
	{
		print_help(cmdv[0]);
		return -1;
	}

	if (server_init(&newph, cmdv[1]) != 0)
		return -1;

	/* close old connection */
	if (ph != NULL)
	{
		nph_printf(1, "disconnecting from server %s\n", ph_server);
		if (ph_close(ph, 0) == -1)
			nph_printf(1, "warning: ph_close(): %s\n",
				   strerror(errno));
	}

	/* save new handle */
	ph = newph;
	ph_server = strdup(cmdv[1]);
	got_servers = 0;

	return 0;
}
Esempio n. 3
0
/*
 * .
 */
int main(int argc, char *argv[])
{
	PheapIndex* idx;
	Klass* klass;
	Klass* klass2;
	Symbol* symbol;
	
	idx = PheapIndex::instance();
	if (ph_isnew()) {
		klass = new Klass;
		printf("klass=%p\n", klass);
		klass2 = new Klass;
		printf("klass2=%p\n", klass2);
		char* class_name = (char*) ph_malloc(3);
		strcpy(class_name, "AB");
		klass->set_class_name(class_name);
		char* k_n = klass->get_class_name();
		printf("class name: %s\n", k_n);
		symbol = new Symbol;
		char* symbol_name = (char*) ph_malloc(3);
		strcpy(symbol_name, "BC");
		symbol->set_name(symbol_name);
		klass->set_symbol(symbol);
		idx->add_klass(class_name, klass);
		idx->add_klass(symbol_name, klass2);
		// test heap limits
		while (1) {
			ph_malloc(1024);
		}
	} else {
		klass = idx->get_class((char*)"AB");
		if (klass == NULL) printf("Klass not found!");
		char* k_n = klass->get_class_name();
		printf("class name: %s\n", k_n);
		printf("symbol name: %s\n", klass->get_symbol()->get_name());
		klass2 = idx->get_class((char*)"BC");
	}
	
	ph_close();
	
}
Esempio n. 4
0
void
interactive(void)
{
	char cmdbuf[NPH_BUF_SIZE];
#ifdef HAVE_POLL
	struct pollfd pfd[2];
#else
	fd_set rfds, efds;
#endif

#ifdef HAVE_LIBREADLINE
	/* initialize readline */
	if (isatty(fileno(stdin)))
	{
		/* set readline callback handler for polling */
		rl_callback_handler_install(prompt, rl_docmd);

		/* set completion hook */
		rl_attempted_completion_function = (CPPFunction *)rl_completion_hook;
		rl_ignore_completion_duplicates = 1;

		/* set application name for readline init file */
		rl_readline_name = "nph";
	}
#endif /* HAVE_LIBREADLINE */

	while (1)
	{
		/* set state for signal handler */
		sighandler_state = HANDLER_INPUT;

		/* initialize file descriptor multiplexing */
#ifdef HAVE_POLL
		pfd[0].fd = fileno(stdin);
		pfd[0].events = POLLIN | POLLPRI;
		pfd[0].revents = 0;
		pfd[1].fd = ph_rfd(ph);
		pfd[1].events = POLLIN | POLLPRI | POLLERR;
		pfd[1].revents = 0;
#else /* ! HAVE_POLL */
		FD_ZERO(&rfds);
		FD_ZERO(&efds);
		FD_SET(fileno(stdin), &rfds);
		FD_SET(ph_rfd(ph), &rfds);
		FD_SET(ph_rfd(ph), &efds);
#endif /* HAVE_POLL */

#ifndef HAVE_LIBREADLINE
		if (isatty(fileno(stdin)))
		{
			printf("%s", prompt);
			fflush(stdout);		/* prompt */
		}
#endif /* ! HAVE_LIBREADLINE */

#ifdef HAVE_POLL
		if (poll(pfd, 2, -1) == -1)
		{
			if (errno == EINTR)
				continue;
			perror("poll()");
			exit(1);
		}
		if (pfd[1].revents)
#else /* ! HAVE_POLL */
		if (select((fileno(stdin) > ph_rfd(ph)
			    ? fileno(stdin)
			    : ph_rfd(ph)) + 1,
			   &rfds, NULL, &efds, NULL) == -1)
		{
			if (errno == EINTR)
				continue;
			perror("select()");
			exit(1);
		}
		if (FD_ISSET(ph_rfd(ph), &rfds)
		    || FD_ISSET(ph_rfd(ph), &efds))
#endif /* HAVE_POLL */
		{
			puts("\nnph: server closed connection, exiting");
#ifdef HAVE_LIBREADLINE
			rl_callback_handler_remove();
#endif /* HAVE_LIBREADLINE */
			ph_close(ph, PH_CLOSE_FAST);
			exit(1);
		}

		/* command input available */
#ifdef HAVE_POLL
		if (pfd[0].revents)
#else /* ! HAVE_POLL */
		if (FD_ISSET(fileno(stdin), &rfds))
#endif /* HAVE_POLL */
		{
#ifdef HAVE_LIBREADLINE
			if (isatty(fileno(stdin)))
			{
				rl_callback_read_char();
				continue;
			}
#endif /* HAVE_LIBREADLINE */

			if (fgets(cmdbuf, sizeof(cmdbuf), stdin) == NULL)
			{
				if (ferror(stdin))
				{
					perror("fgets()");
					exit(1);
				}

				/* got EOF - exit normally */
				ph_close(ph, 0);
				exit(0);
			}

			/* strip trailing newline */
			if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
				cmdbuf[strlen(cmdbuf) - 1] = '\0';

			/* skip empty lines */
			if (cmdbuf[0] == '\0')
				continue;

			nph_command(cmdbuf);
			putchar('\n');
		}
	}

	/* can't happen */
	fprintf(stderr,
		"nph: internal error: reached the end of interactive()\n");
	exit(1);
}