shithub: oai

ref: 09836b88b3d499cc89fa7d22e8ff4bf4b5913cac
dir: /tools/readfile.tool/

View raw version
static char*
read_file(OToolcall toolcall, void*)
{
	JSON *j, *fj;
	char *file;
	Biobuf *io;
	char *s;
	Dir *dir;
	
	if (!allowed(toolcall, 0))
		return abortcall(toolcall);
	
	j = jsonparse(toolcall.arguments);
	fj = jsonbyname(j, "file");
	if (!fj) {
		fprint(2, "no file in read_file request!\n");
		jsonfree(j);
		return strdup("bad request in read_file!\n");
	}
	
	file = jsonstr(fj);
	if (!(file && file[0])) {
		fprint(2, "invalid file in read_file request!\n");
		jsonfree(j);
		return strdup("bad request in read_file: no file!\n");
	}
	
	dir = dirstat(file);
	if (!dir)
		return smprint("error opening file: %r\n");
	
	if (dir->mode&DMDIR) {
		s = smprint("error: file is a directory\n");
		free(dir);
		return s;
	}
	
	io = Bopen(file, OREAD);
	if (!io) {
		fprint(2, "open file: %r\n");
		return smprint("open file in read_file: %r");
	}
	
	fprint(2, "read_file: %s\n", file);
	
	s = Brdstr(io, 0, 0);
	jsonfree(j);
	Bterm(io);
	return s;
}

static char *readfiledesc = "read the contents of a specified file, similar to the `cat` unix command. Especially useful on a Plan 9 system for interaction with filesystems.";
static char *readfileargs = ""
%%json
{
	"type": "object",
	"properties": {
		"file": {
			"type": "string",
			"description": "relative or absolute path to the file"
		}
	},
	"required": [ "file" ]
}
%/json
;