From e3211286d22c87c23ca145f92c0ee0cf3d650b7b Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Thu, 29 Aug 2019 18:59:40 +1100
Subject: [PATCH] rewrite instance function, mark insta_? as deprecated

---
 src/include/66/utils.h | 14 ++++---
 src/lib66/instance.c   | 93 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/src/include/66/utils.h b/src/include/66/utils.h
index bfc39dbc..1b9b87ad 100644
--- a/src/include/66/utils.h
+++ b/src/include/66/utils.h
@@ -20,6 +20,7 @@
 
 #include <skalibs/stralloc.h>
 #include <skalibs/genalloc.h>
+#include <skalibs/gccattributes.h>
 
 #include <66/ssexec.h>
 #include <66/resolve.h>
@@ -47,10 +48,13 @@ extern int set_livedir(stralloc *live) ;
 extern int set_livescan(stralloc *live,uid_t owner) ;
 extern int set_livetree(stralloc *live,uid_t owner) ;
 extern int set_livestate(stralloc *live,uid_t owner) ;
-extern int insta_check(char const *svname) ;
-extern int insta_create(stralloc *sasv,stralloc *sv, char const *src, int len) ;
-extern int insta_splitname(stralloc *sa,char const *name,int len,int what) ;
-extern int insta_replace(stralloc *sa,char const *src,char const *cpy) ;
+extern int insta_check(char const *svname) gccattr_deprecated ;
+extern int insta_create(stralloc *sasv,stralloc *sv, char const *src, int len) gccattr_deprecated ;
+extern int insta_splitname(stralloc *sa,char const *name,int len,int what) gccattr_deprecated ;
+extern int insta_replace(stralloc *sa,char const *src,char const *cpy) gccattr_deprecated ;
 extern int read_svfile(stralloc *sasv,char const *name,char const *src) ;
-
+extern int instance_check(char const *svname) ;
+extern int instance_splitname(stralloc *sa,char const *name,int len,int what) ;
+//extern int instance_change_name(stralloc *sa,char const *template,char const *copy) ;
+extern int instance_create(stralloc *sasv,char const *svname, char const *regex, char const *src, int len) ;
 #endif
diff --git a/src/lib66/instance.c b/src/lib66/instance.c
index 06f56e1f..e69a6272 100644
--- a/src/lib66/instance.c
+++ b/src/lib66/instance.c
@@ -21,10 +21,103 @@
 #include <oblibs/string.h>
 #include <oblibs/stralist.h>
 #include <oblibs/directory.h>
+#include <oblibs/environ.h>
+#include <oblibs/sastr.h>
 
 #include <skalibs/stralloc.h>
 #include <skalibs/djbunix.h>
 
+#include <66/enum.h>
+
+/** New functions */
+
+int instance_check(char const *svname)
+{
+	int r ;
+	size_t len = strlen(svname) ;
+	r = get_len_until(svname,'@') ;
+	// len == r avoid empty value after the instance template name
+	if (strlen(svname+r) <= 1 && r > 0) return 0 ;
+	
+	return r ;
+}
+
+int instance_splitname(stralloc *sa,char const *name,int len,int what)
+{
+	char const *copy ;
+	size_t tlen = len + 1 ;
+	
+	char template[tlen + 1] ;
+	memcpy(template,name,tlen) ;
+	template[tlen] = 0 ;
+	
+	copy = name + tlen ;
+	
+	if (!what)
+		return stralloc_obreplace(sa,template) ;
+	else
+		return stralloc_obreplace(sa,copy) ;
+}
+/*
+int instance_change_name(stralloc *sa,char const *template,char const *copy)
+{	
+	stralloc tmp = STRALLOC_ZERO ;
+	stralloc iname = STRALLOC_ZERO ;
+	
+	if (!stralloc_cats(&iname,template) ||
+	!stralloc_cats(&iname,copy) ||
+	!stralloc_0(&iname) ||
+	!stralloc_copy(&tmp,sa) ||
+	!environ_get_val_of_key(&tmp,get_keybyid(NAME)) ||
+	!sastr_replace(sa,tmp.s,iname.s)) goto err ;	
+	
+	stralloc_free(&tmp) ;
+	stralloc_free(&iname) ;
+	return 1 ;
+	err:
+		stralloc_free(&tmp) ;
+		stralloc_free(&iname) ;
+		return 0 ;
+}
+*/
+
+int instance_create(stralloc *sasv,char const *svname, char const *regex, char const *src, int len)
+{
+	char const *copy ;
+	size_t tlen = len + 1 ;
+		
+	stralloc tmp = STRALLOC_ZERO ;	
+	
+	char template[tlen + 1] ;
+	memcpy(template,svname,tlen) ;
+	template[tlen] = 0 ;
+	
+	copy = svname + tlen ;
+
+	if (!file_readputsa(&tmp,src,template)) {
+		VERBO3 strerr_warnwu3sys("open: ",src,template) ;
+		goto err ;
+	}
+/*	if (!instance_change_name(&tmp,template,copy)) {
+		VERBO3 strerr_warnwu3x("replace instance name at: ",src,template) ;
+		goto err ;
+	}*/
+	if (!sastr_replace_all(&tmp,regex,copy)){
+		VERBO3 strerr_warnwu3x("replace instance character at: ",src,template) ;
+		goto err ;
+	}
+	if (!stralloc_copy(sasv,&tmp)) goto err ;
+	stralloc_free(&tmp) ;
+	return 1 ;
+	err:
+		stralloc_free(&tmp) ;
+		return 0 ;
+}
+
+/*********************
+ * Deprecated function 
+ * *******************/
+
 int insta_replace(stralloc *sa,char const *src,char const *cpy)
 {
 	
-- 
GitLab