Quellcode durchsuchen

tripl: support an interesting thing that INTERPRETER-IN-INTERPRETER

rain vor 4 Jahren
Ursprung
Commit
affb3fdc41
5 geänderte Dateien mit 168 neuen und 5 gelöschten Zeilen
  1. 1 1
      Makefile
  2. 102 0
      arch/arch_templ.c
  3. 4 1
      include/tripl/tripl.h
  4. 23 2
      sample.trp
  5. 38 1
      tripl.c

+ 1 - 1
Makefile

@@ -1,7 +1,7 @@
 tripl-objs 	= main.o tripl.o error.o klist.o  
 tripl-out 	= tripl
 
-CFLAGS 		= -Wall -Werror -Iinclude -g -ldl -O2 
+CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
 
 ifeq ($(install-dir),)
 	install-dir = /usr/bin

+ 102 - 0
arch/arch_templ.c

@@ -21,7 +21,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <string.h>
 
 #include <errno.h>
@@ -36,6 +38,7 @@
 char 	*psave;
 struct klist_node names_list = KLIST_NODE_INIT(&names_list);
 
+extern char *line_buffer;
 extern char *line_eof;
 
 
@@ -44,6 +47,7 @@ __arch_templ static int new_var(char *p, struct arch *);
 __arch_templ static int set_name(char *p, struct arch *);
 __arch_templ static int math_set_name(char *p, struct arch *);
 __arch_templ static int declare_object(char *p, struct arch *);
+__arch_templ static int interp_in_interp(char *p, struct arch *);
 
 __arch_templ int cstyle_declare(char *p, struct arch *);
 
@@ -85,6 +89,13 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 
 		break;
 
+	case '`':
+		/* running code in another interpreter */
+
+		interp_in_interp(p, arch_fn);
+
+		break;
+
 	case '+':
 	case '-':
 		if (!math_set_name(p, arch_fn)) 
@@ -462,4 +473,95 @@ skip:	attr = getattrmask(p, declare_attrtab);
 }
 
 
+static int iini_run(char *interp_path, char *code) {
+	int pid, stat;
+
+	/* we use the command-line '-c' */
+
+	printf("\nRun %s as interpreter-in-interpreter: \n", interp_path);
+
+	if (!(pid = fork())) {
+		/* child */
+
+		execl(interp_path, interp_path, "-c", code, NULL);
+
+		error("execl: %s\n", strerror(errno));
+
+		exit(generic_error("interp-in-interp failed", psave, lno));
+	}
+
+	waitpid(pid, &stat, 0);
+
+	printf("\n");
+
+	if (WIFEXITED(stat)) 
+		printf("%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
+
+	else if (WIFSIGNALED(stat)) 
+		error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat), 
+
+#ifdef WCOREDUMP
+				WCOREDUMP(stat) ? " (core dumped)" : ""
+#else
+				""
+#endif
+	);
+
+	return 0;
+}
+
+
+
+
+static int interp_in_interp(char *p, struct arch *arch_fn) {
+	int type, out;
+	char *interp, *code, *end;
+
+	/* first get the interpreter name */
+
+	p++;
+	_skip_spaces(p);
+
+	if (!getword(p, &type, &out)) 
+		exit(generic_error("interpreter name required", psave, lno));
+
+
+	interp = strndup(p + 1, out);
+
+	p += out + 2;
+	_skip_spaces(p);
+
+	
+	if (!getword(p, &type, &out) || out != '{') 
+		exit(generic_error("start mark '{' required", psave, lno));
+
+	/* get the end mark '}' */
+
+	end = read_code_block(p);
+
+	_skip_spaces(end);
+
+	/* ; -> next_line */
+	end++;
+	
+	p++;
+
+	code = strndup(p, end - p - 1);
+
+	/* now let's do something interesting */
+
+	iini_run(interp, code);
+
+	free(code);
+	free(interp);
+
+	_skip_spaces(end);
+
+	/* this, parse_line will not bark. */
+	line_buffer = end;
+
+	return 0;
+}
+
+
 

+ 4 - 1
include/tripl/tripl.h

@@ -71,13 +71,15 @@
 
 
 #define error(msg, ...) \
+	do { \
 	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_WHITE, BG_BLACK); \
 	fprintf(stderr, "tripl: "); \
 	ansi_csi_ed(stderr); \
 	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_RED, BG_BLACK); \
 	fprintf(stderr, msg, ##__VA_ARGS__); \
 	ansi_csi_ed(stderr); \
-	fprintf(stderr, "\n")
+	fprintf(stderr, "\n"); \
+	} while (0)
 
 
 /* oh no, isdigit() is a function :-( */
@@ -174,6 +176,7 @@ extern const struct attr_table dump_attrtab[];
 int do_tripl_compile(char *, off_t);
 
 char *getword(char *fbuffer, int *type, int *out);
+char *read_code_block(char *p);
 int getattrmask(char *p, const struct attr_table *tab);
 int generic_error(char *msg, char *line, int lno);
 

+ 23 - 2
sample.trp

@@ -1,7 +1,6 @@
 #! /usr/bin/tripl
 
 < "libc-2.33.so" 	@ shared;
-< "libc-2.33.so";
 
 
 extern int *function(int arg, const asdf);
@@ -17,6 +16,28 @@ $ a = +10;
 
 -10 -> a;
 
-!;
+!;	# this is a comment
 
+` "/usr/bin/bash" {
+	echo "greeting from bash :-)"
+
+	read -p "input a number, please: " nr
+
+	case $nr in 
+		1) 
+			echo "a"
+			;;
+
+		2)
+			echo "b"
+			;;
+
+		3) 
+			echo "c"
+			;;
+		*)
+			echo "well... "
+			;;
+	esac
+};
 

+ 38 - 1
tripl.c

@@ -211,7 +211,8 @@ static int next_line(char *fbuffer) {
 
 	/* skip current line */
 
-	line_buffer = to_eol(line_buffer, eof);
+	if (unlikely(!(line_buffer = to_eol(line_buffer, eof)))) 
+		return 0;
 
 	line_buffer++;
 	_skip_spaces(line_buffer)
@@ -378,6 +379,42 @@ char *getword(char *p, int *type, int *out) {
 }
 
 
+char *read_code_block(char *p) {
+	int level = 0, in = 0;
+
+	/* skip to next '{' (not just next, but end of this code-block) */
+
+	for (; p < eof; p++) {
+		/* strings and comments... */
+
+		if (*p == '\"') {
+			if (in) 
+				in = 0;
+
+			else 
+				in = 1;
+		}
+
+		if (!in) {
+			__skip_comment(p);
+
+			if (*p == '{') 
+				level++;
+
+			else if (*p == '}') {
+				level--;
+
+				if (!level) 
+					return p;
+			}
+		}
+	}
+
+	/* eof */
+
+	return exit(generic_error("1 or more than 1 '}'s require", linebuffer_save, lno)), NULL;
+}
+
 
 static inline int find_attr(char *attrname, const struct attr_table *tab) {
 	int i;