Example #1
0
File: view.c Project: lyuts/vifm
/* Reads data to be displayed handling error cases.  Returns zero on success, 1
 * if file is a directory, 2 on file reading error, 3 on issues with viewer or
 * 4 on empty input. */
static int
get_view_data(view_info_t *vi, const char file_to_view[])
{
	FILE *fp;

	const char *const viewer =
		get_viewer_for_file(get_last_path_component(file_to_view));

	if(is_null_or_empty(viewer))
	{
		if(is_dir(file_to_view))
		{
			return 1;
		}
		else if((fp = fopen(file_to_view, "rb")) == NULL)
		{
			return 2;
		}
	}
	else if((fp = use_info_prog(viewer)) == NULL)
	{
		return 3;
	}

	vi->lines = is_null_or_empty(viewer)
		? read_file_lines(fp, &vi->nlines)
		: read_stream_lines(fp, &vi->nlines);

	fclose(fp);

	if(vi->lines == NULL || vi->nlines == 0)
	{
		return 4;
	}

	return 0;
}
Example #2
0
void
quick_view_file(FileView *view)
{
	char path[PATH_MAX];
	const dir_entry_t *entry;

	if(curr_stats.load_stage < 2)
	{
		return;
	}

	if(vle_mode_is(VIEW_MODE))
	{
		return;
	}

	if(curr_stats.number_of_windows == 1)
	{
		return;
	}

	if(draw_abandoned_view_mode())
	{
		return;
	}

	ui_view_erase(other_view);

	entry = &view->dir_entry[view->list_pos];
	get_full_path_of(entry, sizeof(path), path);

	switch(view->dir_entry[view->list_pos].type)
	{
		case FT_CHAR_DEV:
			mvwaddstr(other_view->win, LINE, COL, "File is a Character Device");
			break;
		case FT_BLOCK_DEV:
			mvwaddstr(other_view->win, LINE, COL, "File is a Block Device");
			break;
#ifndef _WIN32
		case FT_SOCK:
			mvwaddstr(other_view->win, LINE, COL, "File is a Socket");
			break;
#endif
		case FT_FIFO:
			mvwaddstr(other_view->win, LINE, COL, "File is a Named Pipe");
			break;
		case FT_LINK:
			if(get_link_target_abs(path, entry->origin, path, sizeof(path)) != 0)
			{
				mvwaddstr(other_view->win, LINE, COL, "Cannot resolve Link");
				break;
			}
			if(!ends_with_slash(path) && is_dir(path))
			{
				strncat(path, "/", sizeof(path) - strlen(path) - 1);
			}
			/* break intensionally omitted */
		case FT_UNK:
		default:
			{
				const char *viewer;
				FILE *fp;

				char *const typed_fname = get_typed_fname(path);
				viewer = ft_get_viewer(typed_fname);
				free(typed_fname);

				if(viewer == NULL && is_dir(path))
				{
					mvwaddstr(other_view->win, LINE, COL, "File is a Directory");
					break;
				}
				if(is_null_or_empty(viewer))
				{
					fp = os_fopen(path, "rb");
				}
				else
				{
					fp = use_info_prog(viewer);
				}

				if(fp == NULL)
				{
					mvwaddstr(other_view->win, LINE, COL, "Cannot open file");
					break;
				}

				ui_view_clear(other_view);
				wattrset(other_view->win, 0);
				view_file(fp, cfg.wrap_quick_view);

				fclose(fp);
				break;
			}
	}
	refresh_view_win(other_view);

	ui_view_title_update(other_view);
}
Example #3
0
/* Reads data to be displayed handling error cases.  Returns zero on success, 1
 * if file is a directory, 2 on file reading error, 3 on issues with viewer or
 * 4 on empty input. */
static int
get_view_data(view_info_t *vi, const char file_to_view[])
{
	FILE *fp;
	const char *const viewer = gv_get_viewer(file_to_view);

	if(is_null_or_empty(viewer))
	{
		if(is_dir(file_to_view))
		{
			return 1;
		}

		fp = os_fopen(file_to_view, "rb");
		if(fp == NULL)
		{
			return 2;
		}

		vi->lines = read_file_lines(fp, &vi->nlines);
	}
	else
	{
		const int graphics = is_graphics_viewer(viewer);
		FileView *const curr = curr_view;
		curr_view = curr_stats.view ? curr_view
		          : (vi->view != NULL) ? vi->view : curr_view;
		curr_stats.preview_hint = vi->view;

		if(graphics)
		{
			/* Wait a bit to let terminal emulator do actual refresh (at least some
			 * of them need this). */
			usleep(50000);
		}
		fp = use_info_prog(viewer);

		curr_view = curr;
		curr_stats.preview_hint = NULL;

		if(fp == NULL)
		{
			return 3;
		}

		if(graphics)
		{
			vi->graphics = 1;
		}

		vi->lines = read_stream_lines(fp, &vi->nlines);
	}

	fclose(fp);

	if(vi->lines == NULL || vi->nlines == 0)
	{
		return 4;
	}

	return 0;
}
Example #4
0
void
quick_view_file(FileView *view)
{
	char buf[PATH_MAX];

	if(curr_stats.load_stage < 2)
		return;

	if(get_mode() == VIEW_MODE)
		return;

	if(curr_stats.number_of_windows == 1)
		return;

	werase(other_view->win);
	werase(other_view->title);
	mvwaddstr(other_view->title, 0, 0, "File: ");
	wprint(other_view->title, view->dir_entry[view->list_pos].name);

	snprintf(buf, sizeof(buf), "%s/%s", view->curr_dir,
			view->dir_entry[view->list_pos].name);

	switch(view->dir_entry[view->list_pos].type)
	{
		case CHARACTER_DEVICE:
			mvwaddstr(other_view->win, LINE, COL, "File is a Character Device");
			break;
		case BLOCK_DEVICE:
			mvwaddstr(other_view->win, LINE, COL, "File is a Block Device");
			break;
#ifndef _WIN32
		case SOCKET:
			mvwaddstr(other_view->win, LINE, COL, "File is a Socket");
			break;
#endif
		case FIFO:
			mvwaddstr(other_view->win, LINE, COL, "File is a Named Pipe");
			break;
		case LINK:
			if(get_link_target_abs(buf, view->curr_dir, buf, sizeof(buf)) != 0)
			{
				mvwaddstr(other_view->win, LINE, COL, "Cannot resolve Link");
				break;
			}
			if(!ends_with_slash(buf) && is_dir(buf))
			{
				strncat(buf, "/", sizeof(buf) - strlen(buf) - 1);
			}
			/* break intensionally omitted */
		case UNKNOWN:
		default:
			{
				const char *viewer;
				FILE *fp;

				viewer = get_viewer_for_file(buf);
				if(viewer == NULL && is_dir(buf))
				{
					mvwaddstr(other_view->win, LINE, COL, "File is a Directory");
					break;
				}
				if(is_null_or_empty(viewer))
					fp = fopen(buf, "r");
				else
					fp = use_info_prog(viewer);

				if(fp == NULL)
				{
					mvwaddstr(other_view->win, LINE, COL, "Cannot open file");
					break;
				}

				colmgr_reset();
				wattrset(other_view->win, 0);
				view_file(fp, cfg.wrap_quick_view);

				fclose(fp);
			}
			break;
	}
	refresh_view_win(other_view);
	wrefresh(other_view->title);
}