Blame extension/readfile.c

Packit 575503
/*
Packit 575503
 * readfile.c - Read an entire file into a string.
Packit 575503
 *
Packit 575503
 * Arnold Robbins
Packit 575503
 * Tue Apr 23 17:43:30 IDT 2002
Packit 575503
 * Revised per Peter Tillier
Packit 575503
 * Mon Jun  9 17:05:11 IDT 2003
Packit 575503
 * Revised for new dynamic function facilities
Packit 575503
 * Mon Jun 14 14:53:07 IDT 2004
Packit 575503
 * Revised for formal API May 2012
Packit 575503
 * Added input parser March 2014
Packit 575503
 */
Packit 575503
Packit 575503
/*
Packit 575503
 * Copyright (C) 2002, 2003, 2004, 2011, 2012, 2013, 2014
Packit 575503
 * the Free Software Foundation, Inc.
Packit 575503
 *
Packit 575503
 * This file is part of GAWK, the GNU implementation of the
Packit 575503
 * AWK Programming Language.
Packit 575503
 *
Packit 575503
 * GAWK is free software; you can redistribute it and/or modify
Packit 575503
 * it under the terms of the GNU General Public License as published by
Packit 575503
 * the Free Software Foundation; either version 3 of the License, or
Packit 575503
 * (at your option) any later version.
Packit 575503
 *
Packit 575503
 * GAWK is distributed in the hope that it will be useful,
Packit 575503
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 575503
 * GNU General Public License for more details.
Packit 575503
 *
Packit 575503
 * You should have received a copy of the GNU General Public License
Packit 575503
 * along with this program; if not, write to the Free Software
Packit 575503
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
Packit 575503
 */
Packit 575503
Packit 575503
#ifdef HAVE_CONFIG_H
Packit 575503
#include <config.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#define _BSD_SOURCE
Packit 575503
Packit 575503
#include <stdio.h>
Packit 575503
#include <assert.h>
Packit 575503
#include <errno.h>
Packit 575503
#include <fcntl.h>
Packit 575503
#include <stdlib.h>
Packit 575503
#include <string.h>
Packit 575503
#include <unistd.h>
Packit 575503
Packit 575503
#include <sys/types.h>
Packit 575503
#include <sys/stat.h>
Packit 575503
Packit 575503
#include "gawkapi.h"
Packit 575503
Packit 575503
#include "gettext.h"
Packit 575503
#define _(msgid)  gettext(msgid)
Packit 575503
#define N_(msgid) msgid
Packit 575503
Packit 575503
#ifndef O_BINARY
Packit 575503
#define O_BINARY 0
Packit 575503
#endif
Packit 575503
Packit 575503
static const gawk_api_t *api;	/* for convenience macros to work */
Packit 575503
static awk_ext_id_t ext_id;
Packit 575503
static const char *ext_version = "readfile extension: version 2.0";
Packit 575503
static awk_bool_t init_readfile();
Packit 575503
static awk_bool_t (*init_func)(void) = init_readfile;
Packit 575503
Packit 575503
int plugin_is_GPL_compatible;
Packit 575503
Packit 575503
/* read_file_to_buffer --- handle the mechanics of reading the file */
Packit 575503
Packit 575503
static char *
Packit 575503
read_file_to_buffer(int fd, const struct stat *sbuf)
Packit 575503
{
Packit 575503
	char *text;
Packit 575503
Packit 575503
	if ((sbuf->st_mode & S_IFMT) != S_IFREG) {
Packit 575503
		errno = EINVAL;
Packit 575503
		update_ERRNO_int(errno);
Packit 575503
		return NULL;
Packit 575503
	}
Packit 575503
Packit 575503
	emalloc(text, char *, sbuf->st_size + 1, "do_readfile");
Packit 575503
Packit 575503
	if (read(fd, text, sbuf->st_size) != sbuf->st_size) {
Packit 575503
		update_ERRNO_int(errno);
Packit 575503
		gawk_free(text);
Packit 575503
		return NULL;
Packit 575503
	}
Packit 575503
	text[sbuf->st_size] = '\0';
Packit 575503
	return text;
Packit 575503
}
Packit 575503
Packit 575503
/* do_readfile --- read a file into memory */
Packit 575503
Packit 575503
static awk_value_t *
Packit 575503
do_readfile(int nargs, awk_value_t *result, struct awk_ext_func *unused)
Packit 575503
{
Packit 575503
	awk_value_t filename;
Packit 575503
	int ret;
Packit 575503
	struct stat sbuf;
Packit 575503
	char *text;
Packit 575503
	int fd;
Packit 575503
Packit 575503
	assert(result != NULL);
Packit 575503
	make_null_string(result);	/* default return value */
Packit 575503
Packit 575503
	unset_ERRNO();
Packit 575503
Packit 575503
	if (get_argument(0, AWK_STRING, &filename)) {
Packit 575503
		ret = stat(filename.str_value.str, & sbuf);
Packit 575503
		if (ret < 0) {
Packit 575503
			update_ERRNO_int(errno);
Packit 575503
			goto done;
Packit 575503
		}
Packit 575503
Packit 575503
		if ((fd = open(filename.str_value.str, O_RDONLY|O_BINARY)) < 0) {
Packit 575503
			update_ERRNO_int(errno);
Packit 575503
			goto done;
Packit 575503
		}
Packit 575503
Packit 575503
		text = read_file_to_buffer(fd, & sbuf);
Packit 575503
		if (text == NULL)
Packit 575503
			goto done;	/* ERRNO already updated */
Packit 575503
Packit 575503
		close(fd);
Packit 575503
		make_malloced_string(text, sbuf.st_size, result);
Packit 575503
		goto done;
Packit 575503
	} else if (do_lint)
Packit 575503
		lintwarn(ext_id, _("readfile: called with wrong kind of argument"));
Packit 575503
Packit 575503
done:
Packit 575503
	/* Set the return value */
Packit 575503
	return result;
Packit 575503
}
Packit 575503
Packit 575503
/* readfile_get_record --- read the whole file as one record */
Packit 575503
Packit 575503
static int
Packit 575503
readfile_get_record(char **out, awk_input_buf_t *iobuf, int *errcode,
Packit 575503
			char **rt_start, size_t *rt_len,
Packit 575503
			const awk_fieldwidth_info_t **unused)
Packit 575503
{
Packit 575503
	char *text;
Packit 575503
Packit 575503
	/*
Packit 575503
	 * The caller sets *errcode to 0, so we should set it only if an
Packit 575503
	 * error occurs.
Packit 575503
	 */
Packit 575503
Packit 575503
	if (out == NULL || iobuf == NULL)
Packit 575503
		return EOF;
Packit 575503
Packit 575503
	if (iobuf->opaque != NULL) {
Packit 575503
		/*
Packit 575503
		 * Already read the whole file,
Packit 575503
		 * free up stuff and return EOF
Packit 575503
		 */
Packit 575503
		gawk_free(iobuf->opaque);
Packit 575503
		iobuf->opaque = NULL;
Packit 575503
		return EOF;
Packit 575503
	}
Packit 575503
Packit 575503
	/* read file */
Packit 575503
	text = read_file_to_buffer(iobuf->fd, & iobuf->sbuf);
Packit 575503
	if (text == NULL)
Packit 575503
		return EOF;
Packit 575503
Packit 575503
	/* set up the iobuf for next time */
Packit 575503
	iobuf->opaque = text;
Packit 575503
Packit 575503
	/* set return values */
Packit 575503
	*rt_start = NULL;
Packit 575503
	*rt_len = 0;
Packit 575503
	*out = text;
Packit 575503
Packit 575503
	/* return count */
Packit 575503
	return iobuf->sbuf.st_size;
Packit 575503
}
Packit 575503
Packit 575503
/* readfile_can_take_file --- return true if we want the file */
Packit 575503
Packit 575503
static awk_bool_t
Packit 575503
readfile_can_take_file(const awk_input_buf_t *iobuf)
Packit 575503
{
Packit 575503
	awk_value_t array, index, value;
Packit 575503
Packit 575503
	if (iobuf == NULL)
Packit 575503
		return awk_false;
Packit 575503
Packit 575503
	/*
Packit 575503
	 * This could fail if PROCINFO isn't referenced from
Packit 575503
	 * the awk program. It's not a "can't happen" error.
Packit 575503
	 */
Packit 575503
	if (! sym_lookup("PROCINFO", AWK_ARRAY, & array)) {
Packit 575503
		return awk_false;
Packit 575503
	}
Packit 575503
Packit 575503
	(void) make_const_string("readfile", 8, & index);
Packit 575503
Packit 575503
	if (! get_array_element(array.array_cookie, & index, AWK_UNDEFINED, & value)) {
Packit 575503
		return awk_false;
Packit 575503
	}
Packit 575503
Packit 575503
	return awk_true;
Packit 575503
}
Packit 575503
Packit 575503
/* readfile_take_control_of --- take over the file */
Packit 575503
Packit 575503
static awk_bool_t
Packit 575503
readfile_take_control_of(awk_input_buf_t *iobuf)
Packit 575503
{
Packit 575503
	if (iobuf == NULL)
Packit 575503
		return awk_false;
Packit 575503
Packit 575503
	iobuf->get_record = readfile_get_record;
Packit 575503
	return awk_true;
Packit 575503
}
Packit 575503
Packit 575503
static awk_input_parser_t readfile_parser = {
Packit 575503
	"readfile",
Packit 575503
	readfile_can_take_file,
Packit 575503
	readfile_take_control_of,
Packit 575503
	NULL
Packit 575503
};
Packit 575503
Packit 575503
/* init_readfile --- set things up */
Packit 575503
Packit 575503
static awk_bool_t
Packit 575503
init_readfile()
Packit 575503
{
Packit 575503
	register_input_parser(& readfile_parser);
Packit 575503
Packit 575503
	return awk_true;
Packit 575503
}
Packit 575503
Packit 575503
static awk_ext_func_t func_table[] = {
Packit 575503
	{ "readfile", do_readfile, 1, 1, awk_false, NULL },
Packit 575503
};
Packit 575503
Packit 575503
/* define the dl_load function using the boilerplate macro */
Packit 575503
Packit 575503
dl_load_func(func_table, readfile, "")