/*
 * This code is just a minor modification of John E. Davis's sltoken.c,
 * to allow a search-path to be set for evalfile and autoloads
 * So it's:
 */

/* Copyright (c) 1998 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

/*
 * ...and:
 * Copyright 1998 Stanley J. Brooks
 */

#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <slang.h>
#include "slirc.h"

#define MAX_FILE_LINE_LEN 256

typedef struct
{
   char *buf;
   FILE *fp;
}
File_Client_Data_Type;

char SLirc_Load_Path[196] = "";

static char *read_from_file (SLang_Load_Type *x)
{
   File_Client_Data_Type *c;

   c = (File_Client_Data_Type *)x->client_data;
   return fgets (c->buf, MAX_FILE_LINE_LEN, c->fp);
}

/* SJB: remark.. this doesn't support f=NULL for <stdin> */
int Local_SLang_load_file(char *f)
{
	File_Client_Data_Type client_data;
	SLang_Load_Type *x;
	char *name, *buf, *fx;
	FILE *fp;

	name = SLang_create_slstring(f);
	if (name == NULL) return -1;

	if ((fx=f))
		fx = SLpath_find_file_in_path(SLirc_Load_Path, f);

	if (!fx || !(x = SLallocate_load_type(name))) {
		SLfree(fx);
		SLang_free_slstring(name);
		return -1;
	}
	buf = NULL;

	fp = fopen(fx, "r");
	if (fp == NULL)
		SLang_verror(SL_OBJ_NOPEN, "Unable to open %s: %s", name, strerror(errno));
	else if (NULL != (buf = SLmalloc(MAX_FILE_LINE_LEN + 1))) {
		client_data.fp = fp;
		client_data.buf = buf;
		x->client_data = (VOID_STAR) & client_data;
		x->read = read_from_file;

		(void) SLang_load_object(x);
	}
	fclose(fp);

	SLfree(fx);
	SLfree(buf);
	SLang_free_slstring(name);
	SLdeallocate_load_type(x);

	if (SLang_Error) return -1;

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1