Преглед на файлове

vcc: bug fix and support -if

intirain преди 4 години
родител
ревизия
d2621150d8
променени са 9 файла, в които са добавени 146 реда и са изтрити 12 реда
  1. 2 2
      Makefile
  2. 1 1
      cmd.c
  3. 2 2
      include/vcc/version.h
  4. 1 3
      main.c
  5. 2 2
      plugins.c
  6. 1 1
      plugins/Makefile
  7. 1 1
      plugins/m4/m4.c
  8. 14 0
      plugins/script/Makefile
  9. 122 0
      plugins/script/script.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 16
-VCC_PATCH 		= 2
+VCC_MINOR 		= 17
+VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)

+ 1 - 1
cmd.c

@@ -186,7 +186,7 @@ int do_cmd(char *s) {
 
 	pretty_unknown_cmd(s);
 
-	return 0;
+	return 1;
 }
 
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.16.2 Wed May  4 16:17:38 CST 2022 root"
-#define VCC_SEMVER "vcc 1.16.2-rc "
+#define VCC_VERSION "VCC 1.17.0 Wed May  4 17:43:28 CST 2022 root"
+#define VCC_SEMVER "vcc 1.17.0-rc "

+ 1 - 3
main.c

@@ -239,9 +239,7 @@ int handle_command(char *buf) {
 		break;
 
 	case '-':
-		do_cmd(buf);
-
-		break;
+		return do_cmd(buf);
 	
 	default:
 		if (msg_hook) 

+ 2 - 2
plugins.c

@@ -42,11 +42,11 @@
 #include <vcc/pretty.h>
 
 
-extern struct plugin plg_hello, plg_robot, plg_m4;
+extern struct plugin plg_hello, plg_robot, plg_m4, plg_script;
 hookfn_t msg_hook, recv_hook;
 
 struct plugin *plugins[] = {
-	&plg_hello, &plg_robot, &plg_m4, 
+	&plg_hello, &plg_robot, &plg_m4, &plg_script, 
 	(struct plugin *) NULL
 };
 

+ 1 - 1
plugins/Makefile

@@ -1,4 +1,4 @@
-plugins 	= hello robot m4 
+plugins 	= hello robot m4 script 
 target 		= libplugins.a 
 
 all: 

+ 1 - 1
plugins/m4/m4.c

@@ -124,7 +124,7 @@ char *m4_msghook(char *s) {
 		return NULL;
 	}
 
-	memset(p, 0, size);
+	memset(p, 0, MSG_SIZE);
 	fread(p, 1, MSG_SIZE, fp);
 
 	return p;

+ 14 - 0
plugins/script/Makefile

@@ -0,0 +1,14 @@
+objs 	= script.o 
+target 	= ../plg_script.o 
+CFLAGS 	= -std=gnu11 -Wall -Wextra -Werror -I../../include -g 
+
+$(target): $(objs)
+	@cp $(objs) $(target)
+
+%.o: %.c
+	@$(CC) -c $*.c $(CFLAGS)
+	@echo -e "CC\t\t\t$*.c"
+
+.PHONY clean: 
+	rm -f $(target) $(objs)
+

+ 122 - 0
plugins/script/script.c

@@ -0,0 +1,122 @@
+/* vcc/plugins/script/script.c 
+ * 
+ * This file is part of vcc. 
+ * 
+ * vcc is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vcc is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vcc. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+
+
+int do_cmd_if(int argc, char **argv);
+int handle_command(char *s);
+
+struct cmdtable script_cmdtab[] = {
+	{ "-if", do_cmd_if, "if condition then ... else ..." }, 
+
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_script = {
+	.state = 0, 
+	.name = "script", 
+	.descr = "Standard vcc script. ", 
+	.cmdtab = script_cmdtab
+};
+
+#define this_plg (&plg_script)
+
+static char *copy_until(char **argv, char *until, int *dst) {
+	int i;
+	char buf[MSG_SIZE];
+
+	memset(buf, 0, MSG_SIZE);
+
+	for (i = *dst; argv[i] && strcmp(argv[i], until); i++) {
+		strcat(buf, argv[i]);
+		strcat(buf, " ");
+	}
+
+	if (!argv[i]) {
+		if (i == *dst) 
+			return NULL;
+		
+		else 
+			return strdup(buf);
+	}
+
+	*dst = ++i;
+	return strdup(buf);
+}
+
+
+static int parse_if(char **argv, char **cond, char **then, char **else_then) {
+	int i = 1;
+
+	*cond = copy_until(argv, "then", &i);
+	*then = copy_until(argv, "else", &i);
+
+	/* it must present */
+	*else_then = copy_until(argv, "", &i);
+
+	if (!*cond || !*then || !*else_then) 
+		return 1;
+
+	return 0;
+}
+
+
+int do_cmd_if(int argc, char **argv) {
+	char *cond, *then, *else_then;
+
+	plg_check_enabled();
+
+	if (argc < 4) {
+		fprintf(stderr, "usage: if condition then ... [else] [...]\n");
+
+		return 1;
+	}
+
+	if (parse_if(argv, &cond, &then, &else_then)) 
+		return 1;
+
+	/* we always use '0' for success ... */
+	if (!handle_command(cond)) 
+		handle_command(then);
+
+	else {
+		if (!else_then) 
+			return 0;
+
+		handle_command(else_then);
+	}
+	
+	free(cond);
+	free(then);
+	free(else_then);
+
+	return 0;
+}
+
+