Beispiel #1
0
/** Read in one line of input with initial text provided.
 *
 * @param ti   Text input
 * @param istr Initial string
 * @param dstr Place to save pointer to new string
 *
 * @return EOK on success
 * @return ENOENT if user requested abort
 * @return EIO if communication with console failed
 *
 */
int tinput_read_i(tinput_t *ti, const char *istr, char **dstr)
{
	console_flush(ti->console);
	if (console_get_size(ti->console, &ti->con_cols, &ti->con_rows) != EOK)
		return EIO;
	
	tinput_set_str(ti, istr);

	ti->sel_start = 0;
	ti->done = false;
	ti->exit_clui = false;
	
	if (tinput_display(ti) != EOK)
		return EIO;
	
	while (!ti->done) {
		console_flush(ti->console);
		
		cons_event_t ev;
		if (!console_get_event(ti->console, &ev))
			return EIO;
		
		switch (ev.type) {
		case CEV_KEY:
			if (ev.ev.key.type == KEY_PRESS)
				tinput_key_press(ti, &ev.ev.key);
			else
				tinput_key_release(ti, &ev.ev.key);
			break;
		case CEV_POS:
			tinput_pos(ti, &ev.ev.pos);
			break;
		}
	}
	
	if (ti->exit_clui)
		return ENOENT;
	
	ti->pos = ti->nc;
	tinput_position_caret(ti);
	putchar('\n');
	
	char *str = tinput_get_str(ti);
	if (str_cmp(str, "") != 0)
		tinput_history_insert(ti, str);
	
	ti->hpos = 0;
	
	*dstr = str;
	return EOK;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
	kbd_event_t ev;
	coord_t coord;
	bool new_file;

	spt_t pt;

	con = console_init(stdin, stdout);
	console_clear(con);

	console_get_size(con, &scr_columns, &scr_rows);

	pane.rows = scr_rows - 1;
	pane.columns = scr_columns;
	pane.sh_row = 1;
	pane.sh_column = 1;

	/* Start with an empty sheet. */
	sheet_init(&doc.sh);

	/* Place caret at the beginning of file. */
	coord.row = coord.column = 1;
	sheet_get_cell_pt(&doc.sh, &coord, dir_before, &pt);
	sheet_place_tag(&doc.sh, &pt, &pane.caret_pos);
	pane.ideal_column = coord.column;

	if (argc == 2) {
		doc.file_name = str_dup(argv[1]);
	} else if (argc > 1) {
		printf("Invalid arguments.\n");
		return -2;
	} else {
		doc.file_name = NULL;
	}

	new_file = false;

	if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
		new_file = true;

	/* Move to beginning of file. */
	caret_move(-ED_INFTY, -ED_INFTY, dir_before);

	/* Place selection start tag. */
	tag_get_pt(&pane.caret_pos, &pt);
	sheet_place_tag(&doc.sh, &pt, &pane.sel_start);

	/* Initial display */
	cursor_visible = true;

	cursor_hide();
	console_clear(con);
	pane_text_display();
	pane_status_display();
	if (new_file && doc.file_name != NULL)
		status_display("File not found. Starting empty file.");
	pane_caret_display();
	cursor_show();

	done = false;

	while (!done) {
		console_get_kbd_event(con, &ev);
		pane.rflags = 0;

		if (ev.type == KEY_PRESS) {
			/* Handle key press. */
			if (((ev.mods & KM_ALT) == 0) &&
			    ((ev.mods & KM_SHIFT) == 0) &&
			     (ev.mods & KM_CTRL) != 0) {
				key_handle_ctrl(&ev);
			} else if (((ev.mods & KM_ALT) == 0) &&
			    ((ev.mods & KM_CTRL) == 0) &&
			     (ev.mods & KM_SHIFT) != 0) {
				key_handle_shift(&ev);
			} else if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
				key_handle_unmod(&ev);
			}
		}

		/* Redraw as necessary. */

		cursor_hide();

		if (pane.rflags & REDRAW_TEXT)
			pane_text_display();
		if (pane.rflags & REDRAW_ROW)
			pane_row_display();
		if (pane.rflags & REDRAW_STATUS)
			pane_status_display();
		if (pane.rflags & REDRAW_CARET)
			pane_caret_display();

		cursor_show();
	}

	console_clear(con);

	return 0;
}
Beispiel #3
0
/* Main entry point for cat, accepts an array of arguments */
int cmd_cat(char **argv)
{
	unsigned int argc, i, ret = 0;
	size_t buffer = 0;
	int c, opt_ind;
	aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;
	bool hex = false;
	bool more = false;
	bool tailFirst = false;
	sysarg_t rows, cols;
	int rc;
	
	/*
	 * reset global state
	 * TODO: move to structure?
	 */
	paging_enabled = false;
	chars_remaining = 0;
	lines_remaining = 0;
	console_cols = 0;
	console_rows = 0;
	should_quit = false;
	console = console_init(stdin, stdout);
	number = false;
	lineno = 0;
	/* This enables printing of the first number. */
	last_char_was_newline = true;


	argc = cli_count_args(argv);

	for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
		c = getopt_long(argc, argv, "xhvmH:t:b:s:n", long_options, &opt_ind);
		switch (c) {
		case 'h':
			help_cmd_cat(HELP_LONG);
			return CMD_SUCCESS;
		case 'v':
			printf("%s\n", CAT_VERSION);
			return CMD_SUCCESS;
		case 'H':
			if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) {
				puts("Invalid head size\n");
				return CMD_FAILURE;
			}
			break;
		case 't':
			if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) {
				puts("Invalid tail size\n");
				return CMD_FAILURE;
			}
			if (head == CAT_FULL_FILE)
				tailFirst = true;
			break;
		case 'b':
			if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) {
				puts("Invalid buffer size\n");
				return CMD_FAILURE;
			}
			break;
		case 'm':
			more = true;
			break;
		case 'x':
			hex = true;
			break;
		case 's':
			dash_represents_stdin = true;
			break;
		case 'n':
			number = true;
			break;
		}
	}

	argc -= optind;

	if (argc < 1) {
		printf("%s - incorrect number of arguments. Try `%s --help'\n",
			cmdname, cmdname);
		return CMD_FAILURE;
	}

	if (buffer < 4)
		buffer = CAT_DEFAULT_BUFLEN;
	
	if (more) {
		rc = console_get_size(console, &cols, &rows);
		if (rc != EOK) {
			printf("%s - cannot get console size\n", cmdname);
			return CMD_FAILURE;
		}
		console_cols = cols;
		console_rows = rows;
		paging_enabled = true;
		newpage();
	}

	for (i = optind; argv[i] != NULL && !should_quit; i++)
		ret += cat_file(argv[i], buffer, hex, head, tail, tailFirst);

	if (ret)
		return CMD_FAILURE;
	else
		return CMD_SUCCESS;
}
Beispiel #4
0
static int get_display_size(winsize_t *ws)
{
	return console_get_size(console, &ws->ws_col, &ws->ws_row);
}