From 983ce83467b2d57757ba8905a6808fa368a90bea Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Wed, 8 May 2019 15:59:54 +1100
Subject: [PATCH] add environ file and function

---
 src/include/66/66.h      |   3 +-
 src/include/66/environ.h |  26 +++++++
 src/lib66/deps-lib/66    |   1 +
 src/lib66/environ.c      | 158 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 src/include/66/environ.h
 create mode 100644 src/lib66/environ.c

diff --git a/src/include/66/66.h b/src/include/66/66.h
index 2cde0576..fcbee9ac 100644
--- a/src/include/66/66.h
+++ b/src/include/66/66.h
@@ -20,12 +20,13 @@
 #include <66/constants.h>
 #include <66/db.h>
 #include <66/enum.h>
+#include <66/environ.h>
 #include <66/parser.h>
+#include <66/rc.h>
 #include <66/resolve.h>
 #include <66/ssexec.h>
 #include <66/state.h>
 #include <66/svc.h>
-#include <66/rc.h>
 #include <66/tree.h>
 #include <66/utils.h>
 
diff --git a/src/include/66/environ.h b/src/include/66/environ.h
new file mode 100644
index 00000000..6103377c
--- /dev/null
+++ b/src/include/66/environ.h
@@ -0,0 +1,26 @@
+/* 
+ * environ.h
+ * 
+ * Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
+ * 
+ * All rights reserved.
+ * 
+ * This file is part of Obarun. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution.
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file./
+ */
+ 
+#ifndef ENVIRON_H
+#define ENVIRON_H
+
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+
+extern int env_clean(stralloc *src) ;
+extern int env_split_one(char *line,genalloc *ga,stralloc *sa) ;
+extern int env_split(genalloc *gaenv,stralloc *saenv,stralloc *src) ;
+extern int env_parsenclean(stralloc *modifs,stralloc *src) ;
+
+#endif
diff --git a/src/lib66/deps-lib/66 b/src/lib66/deps-lib/66
index 45c50739..8427aa85 100644
--- a/src/lib66/deps-lib/66
+++ b/src/lib66/deps-lib/66
@@ -8,6 +8,7 @@ db_get_permissions.o
 db_ok.o
 db_switch_to.o
 db_update.o
+environ.o
 get_enum.o
 get_uidgid.o
 get_userhome.o
diff --git a/src/lib66/environ.c b/src/lib66/environ.c
new file mode 100644
index 00000000..e01b49f5
--- /dev/null
+++ b/src/lib66/environ.c
@@ -0,0 +1,158 @@
+/* 
+ * environ.c
+ * 
+ * Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
+ * 
+ * All rights reserved.
+ * 
+ * This file is part of Obarun. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution.
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file./
+ */
+ 
+#include <stddef.h>
+#include <stdio.h>
+
+#include <oblibs/string.h>
+#include <oblibs/stralist.h>
+
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+#include <skalibs/diuint32.h>
+
+#include <66/parser.h>
+
+/* @Return 1 on success
+ * @Return 0 on fail
+ * @Return -1 for empty line */
+int env_clean(stralloc *src)
+{
+	int r, e = 1 ;
+	unsigned int i ;
+	genalloc gatmp =GENALLOC_ZERO ;//stralist
+	stralloc kp = STRALLOC_ZERO ;
+	stralloc tmp = STRALLOC_ZERO ;
+		
+	size_t pos = 0 ;
+	char const *file = "env_clean" ;
+	parse_mill_t line = { .open = '@', .close = '=', \
+							.skip = " \t\r", .skiplen = 3, \
+							.end = "\n", .endlen = 1, \
+							.jump = "#", .jumplen = 1,\
+							.check = 0, .flush = 0, \
+							.forceskip = 0, .force = 1, \
+							.inner = PARSE_MILL_INNER_ZERO } ;
+	
+	size_t blen = src->len, n = 0 ;
+	if (!stralloc_inserts(src,0,"@")) goto err ;
+	while(pos < (blen+n) && n < 2)
+	{
+		kp.len = 0 ;
+		genalloc_deepfree(stralist,&gatmp,stra_free) ;
+		line.inner.nopen = line.inner.nclose = 0 ;
+		r = parse_config(&line,file,src,&kp,&pos) ;
+		if (!r) goto err ;
+		if (r < 0 && !n){ e = -1 ; goto freed ; }
+		if (!stralloc_0(&kp)) goto err ;
+		if (!clean_val(&gatmp,kp.s)) goto err ;
+		for (i = 0 ; i < genalloc_len(stralist,&gatmp) ; i++)
+		{	
+			if ((i+1) < genalloc_len(stralist,&gatmp))
+			{
+				if (!stralloc_cats(&tmp,gaistr(&gatmp,i))) goto err ;
+				if (!stralloc_cats(&tmp," ")) goto err ;
+			}
+			else if (!stralloc_cats(&tmp,gaistr(&gatmp,i))) goto err ;
+		}
+		if (!n) if (!stralloc_cats(&tmp,"=")) goto err ;
+		if (!stralloc_inserts(src,pos,"@")) goto err ;
+		n++;
+	}
+	
+	if (!stralloc_0(&tmp)) goto err ;
+	if (!stralloc_copy(src,&tmp)) goto err ;
+	
+	freed:
+		stralloc_free(&kp) ;
+		stralloc_free(&tmp) ;
+		genalloc_deepfree(stralist,&gatmp,stra_free) ;
+		return e ;
+	
+	err:
+		stralloc_free(&kp) ;
+		stralloc_free(&tmp) ;
+		genalloc_deepfree(stralist,&gatmp,stra_free) ;
+		return 0 ;
+}
+
+int env_split_one(char *line,genalloc *ga,stralloc *sa)
+{
+	char *k = 0 ;
+	char *v = 0 ;
+	diuint32 tmp = DIUINT32_ZERO ;
+	k = line ;
+	v = line ;
+	
+	obstr_sep(&v,"=") ;
+	if (v == NULL) return 0 ;
+		
+	tmp.left = sa->len ;
+	if(!stralloc_catb(sa,k,strlen(k)+1)) return 0 ;
+	
+	if (!obstr_trim(v,'\n')) return 0 ;
+	tmp.right = sa->len ;
+	if(!stralloc_catb(sa,v,strlen(v)+1)) return 0 ;
+	
+	if (!genalloc_append(diuint32,ga,&tmp)) return 0 ;
+		
+	return 1 ;
+}
+
+int env_split(genalloc *gaenv,stralloc *saenv,stralloc *src)
+{
+	int nbline = 0, i = 0 ;
+	printf("src::%s\n",src->s) ;
+	genalloc gatmp = GENALLOC_ZERO ;//stralist
+	nbline = get_nbline_ga(src->s,src->len,&gatmp) ;
+	printf("nbline::%i\n",nbline) ;
+	for (; i < nbline ; i++)
+	{
+		char *line = gaistr(&gatmp,i) ;
+		printf("line::%s\n",line) ;
+		if (!env_split_one(line,gaenv,saenv)) goto err ;
+	}
+	genalloc_deepfree(stralist,&gatmp,stra_free) ;
+	return 1 ;
+	err: 
+		genalloc_deepfree(stralist,&gatmp,stra_free) ;
+		return 0 ;
+}
+
+int env_parsenclean(stralloc *modifs,stralloc *src)
+{
+	int nbline = 0, i = 0 ;
+	genalloc gatmp = GENALLOC_ZERO ;//stralist
+	
+	if (!parse_env(src)) goto err ;
+	if (!env_clean(src)) goto err ;
+	nbline = get_nbline_ga(src->s,src->len,&gatmp) ;
+printf("src + u::%s\n",src->s ) ;
+	for (; i < nbline ; i++)
+	{
+		char *line = gaistr(&gatmp,i) ;
+		int u = 0 ;
+		if (line[0] == '!') u++ ;
+		printf("line + u::%s\n",gaistr(&gatmp,i) ) ;
+		if (!stralloc_catb(modifs,line + u ,strlen(line) - u) ||
+		!stralloc_0(modifs)) goto err ;
+	}
+	
+	genalloc_deepfree(stralist,&gatmp,stra_free) ;
+	
+	return 1 ;
+	err:
+		genalloc_deepfree(stralist,&gatmp,stra_free) ;
+		return 0 ;
+}
-- 
GitLab