shithub: oai

Download patch

ref: 745c5d5e5058f5e4ffb40425be424be467add1da
parent: 0c3b7d23fead592c182b75b5bae25110d2f5ac09
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Feb 28 07:55:36 EST 2026

makes cmd work, verified writefile and cmd works

--- a/oai.c
+++ b/oai.c
@@ -59,6 +59,28 @@
 	return smprint("%s request aborted by user!", tc.name);
 }
 
+static char*
+joinstrarray(JSONEl *first, char *join, int quote)
+{
+	JSONEl *el;
+	String *s;
+	char *cs;
+	
+	s = s_new();
+	
+	for (el = first; el; el = el->next) {
+		cs = quote ? quotestrdup(jsonstr(el->val)) : jsonstr(el->val);
+		if (el != first)
+			s = s_append(s, join);
+		s = s_append(s, cs);
+		if (quote)
+			free(cs);
+	}
+	cs = strdup(s_to_c(s));
+	s_free(s);
+	return cs;
+}
+
 #include "tools/listfiles.cinc"
 #include "tools/readfile.cinc"
 #include "tools/man.cinc"
@@ -75,6 +97,7 @@
 	maketool(tools, Function, "search_man", lookmandesc, lookmanargs, lookman, nil);
 	maketool(tools, Function, "read_man", mandesc, manargs, man, nil);
 	maketool(tools, Function, "write_file", writefiledesc, writefileargs, writefile, nil);
+	maketool(tools, Function, "run_command", cmddesc, cmdargs, cmd, nil);
 }
 
 static char*
--- a/tools/cmd.tool
+++ b/tools/cmd.tool
@@ -1,5 +1,46 @@
+static char*
+cmd(OToolcall tc, void*)
+{
+	JSON *j, *jc, *ja;
+	char *cmd, *args;
+	char *ret;
+	
+	if (!allowed(tc, 1))
+		return abortcall(tc);
+	
+	j = jsonparse(tc.arguments);
+	jc = jsonbyname(j, "cmd");
+	ja = jsonbyname(j, "args");
+	
+	if (!jc)
+		goto Enocmd;
+	if (!ja)
+		goto Enoargs;
+	
+	cmd = jsonstr(jc);
+	if (!cmd)
+		goto Enocmd;
+	
+	if (ja->t != JSONArray)
+		goto Enoargs;
+	
+	args = joinstrarray(ja->first, " ", 0);
+	cmd = smprint("%s %s", cmd, args);
+	
+	ret = callfunc(cmd);
+	free(args);
+	jsonfree(j);
+	
+	return ret;
+	
+Enocmd:
+	jsonfree(j);
+	return strdup("no command");
+Enoargs:
+	jsonfree(j);
+	return strdup("no arguments");
+}
 
-
 static char *cmddesc = "call a function in the rc shell.";
 static char *cmdargs = ""
 %%json
@@ -8,11 +49,11 @@
 	"properties": {
 		"cmd": {
 			"type": "string",
-			"description": "name or path of the command"
+			"description": "name or path of the command."
 		},
 		"args": {
 			"type": "array",
-			"description": "command line arguments",
+			"description": "command line arguments. Can be empty. Wildcards like * are allowed.",
 			"items": { "type": "string" }
 		}
 	},
--- a/tools/writefile.tool
+++ b/tools/writefile.tool
@@ -52,7 +52,7 @@
 			"description": "new content of the file. Existing content will be overwritten."
 		}
 	},
-	"required": [ "file", "content" ]"
+	"required": [ "file", "content" ]
 }
 %/json
 ;
\ No newline at end of file
--