From 90a723b198577a084ad07376984ceb8247d02f31 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Fri, 17 Feb 2023 21:36:59 +1100
Subject: [PATCH] switch services from tree at -t use. Try the current one, if
 unexisting take the default one.

---
 src/lib66/parse/parse_service.c      | 118 ++++-----------------------
 src/lib66/tree/deps-lib/deps         |   3 +-
 src/lib66/tree/tree_service_add.c    |  63 ++++++++++++++
 src/lib66/tree/tree_service_remove.c |  60 ++++++++++++++
 4 files changed, 143 insertions(+), 101 deletions(-)
 create mode 100644 src/lib66/tree/tree_service_add.c
 create mode 100644 src/lib66/tree/tree_service_remove.c

diff --git a/src/lib66/parse/parse_service.c b/src/lib66/parse/parse_service.c
index cfd2d272..cbdb1b6e 100644
--- a/src/lib66/parse/parse_service.c
+++ b/src/lib66/parse/parse_service.c
@@ -56,102 +56,11 @@ parse_mill_t MILL_GET_VALUE = \
     .skip = " \t\r", .skiplen = 3, .forceskip = 1, \
     .inner.debug = "get_value" } ;
 
-static void parse_notify_add_string(stralloc *sa, char const *name, char const *str)
-{
-    if (!sastr_clean_string(sa, str))
-        log_dieu(LOG_EXIT_SYS, "clean string") ;
-
-    if (!sastr_add_string(sa, name))
-        log_dieu(LOG_EXIT_SYS, "clean string") ;
-
-    if (!sastr_sortndrop_element(sa))
-        log_dieu(LOG_EXIT_SYS, "sort string") ;
-}
-
-static void parse_notify_tree(resolve_service_t *res, char const *base, uint8_t field)
-{
-    log_flow() ;
-
-    char *treename = res->sa.s + res->treename ;
-    char *name = res->sa.s + res->name ;
-
-    resolve_tree_t tres = RESOLVE_TREE_ZERO ;
-    resolve_wrapper_t_ref wres = resolve_set_struct(DATA_TREE, &tres) ;
-    stralloc sa = STRALLOC_ZERO ;
-
-    log_trace("modify field contents of resolve tree file: ", treename) ;
-
-    if (!resolve_read_g(wres, base, treename))
-        log_dieusys(LOG_EXIT_SYS, "read resolve file of tree: ", treename) ;
-
-    if (field == E_RESOLVE_TREE_CONTENTS) {
-
-        char atree[SS_MAX_TREENAME + 1] ;
-
-        if (service_is_g(atree, name, STATE_FLAGS_ISPARSED)) {
-
-            if (strcmp(atree, treename)) {
-
-                /** remove it from the previous used tree */
-                resolve_tree_t res = RESOLVE_TREE_ZERO ;
-                resolve_wrapper_t_ref wtres = resolve_set_struct(DATA_TREE, &res) ;
-
-                if (!resolve_read_g(wtres, base, atree))
-                    log_dieu(LOG_EXIT_SYS, "read resolve file of tree: ", atree) ;
-
-                if (!sastr_clean_string(&sa, res.sa.s + res.contents))
-                    log_dieu(LOG_EXIT_SYS, "clean string") ;
-
-                if (!sastr_remove_element(&sa, name))
-                    log_dieu(LOG_EXIT_SYS, "remove service: ", name, " list") ;
-
-                if (sa.len) {
-                    if (!sastr_rebuild_in_oneline(&sa))
-                        log_dieu(LOG_EXIT_SYS, "rebuild stralloc list") ;
-                } else
-                    stralloc_0(&sa) ;
-
-
-                if (!tree_resolve_modify_field(&res, E_RESOLVE_TREE_CONTENTS, sa.s))
-                    log_dieu(LOG_EXIT_SYS, "modify resolve field of tree: ", atree) ;
-
-                res.ncontents-- ;
-
-                if (!resolve_write_g(wtres, base, atree))
-                    log_dieu(LOG_EXIT_SYS, "write resolve file of tree: ", atree) ;
-
-                resolve_free(wtres) ;
-            }
-            sa.len = 0 ;
-        }
-
-        if (tres.ncontents)
-            parse_notify_add_string(&sa, name, tres.sa.s + tres.contents) ;
-        else if (!sastr_add_string(&sa, name))
-            log_dieu(LOG_EXIT_SYS, "add string") ;
-
-        tres.ncontents++ ;
-
-    } else goto freed ;
-
-    if (!sastr_rebuild_in_oneline(&sa))
-        log_dieu(LOG_EXIT_SYS, "rebuild stralloc list") ;
-
-    if (!resolve_modify_field(wres, field, sa.s))
-        log_dieusys(LOG_EXIT_SYS, "modify resolve file of: ", treename) ;
-
-    if (!resolve_write_g(wres, base, treename))
-        log_dieusys(LOG_EXIT_SYS, "write resolve file of tree: ", treename) ;
-
-    freed :
-    stralloc_free(&sa) ;
-    resolve_free(wres) ;
-}
-
 void parse_service(char const *sv, ssexec_t *info, uint8_t force, uint8_t conf)
 {
     log_flow();
 
+    int r ;
     unsigned int areslen = 0, residx = 0, pos = 0 ;
     resolve_service_t ares[SS_MAX_SERVICE] ;
 
@@ -159,21 +68,30 @@ void parse_service(char const *sv, ssexec_t *info, uint8_t force, uint8_t conf)
     if (!ob_basename(main, sv))
         log_dieu(LOG_EXIT_SYS, "get basename of: ", sv) ;
 
-    int r = parse_frontend(sv, ares, &areslen, info, force, conf, &residx, 0, main) ;
+    r = parse_frontend(sv, ares, &areslen, info, force, conf, &residx, 0, main) ;
     if (r == 2)
         /** already parsed */
         return ;
 
     for (; pos < areslen ; pos++) {
 
-        /** notify first the resolve Master file of the tree
-         * about the location of the service. If the service is
-         * already parsed and the user ask to force it on different tree,
-         * we can know the old place of the service by the old resolve service file*/
-        parse_notify_tree(&ares[pos], info->base.s, E_RESOLVE_TREE_CONTENTS) ;
+        if (force) {
+
+            resolve_service_t res = RESOLVE_SERVICE_ZERO ;
+            resolve_wrapper_t_ref wres = resolve_set_struct(DATA_SERVICE, &res) ;
+            /** warn old tree of the change and modify the desired one.
+             * Force was used so we are supposed to have an existing resolve file of
+             * the service. Before overwritting its resolve file, check if the tree is the same.*/
+            if (resolve_read_g(wres, info->base.s, ares[pos].sa.s + ares[pos].name) &&
+                strcmp(ares[pos].sa.s + ares[pos].treename, res.sa.s + res.treename))
+                    tree_service_remove(info->base.s, res.sa.s + res.treename, res.sa.s + res.name) ;
+        }
+
+        parse_compute_resolve(&ares[pos], info) ;
+        tree_service_add(info->base.s, ares[pos].sa.s + ares[pos].treename, ares[pos].sa.s + ares[pos].name) ;
 
-        char dst[strlen(ares[pos].sa.s + ares[pos].path.tree) + SS_SVDIRS_LEN + 1] ;
-        auto_strings(dst, ares[pos].sa.s + ares[pos].path.tree, SS_SVDIRS) ;
+        char dst[strlen(ares[pos].sa.s + ares[pos].path.home) + SS_SYSTEM_LEN + SS_SERVICE_LEN + 1] ;
+        auto_strings(dst, ares[pos].sa.s + ares[pos].path.home, SS_SYSTEM, SS_SERVICE) ;
 
         write_services(&ares[pos], dst, force) ;
 
diff --git a/src/lib66/tree/deps-lib/deps b/src/lib66/tree/deps-lib/deps
index 55d84ba3..14b66733 100644
--- a/src/lib66/tree/deps-lib/deps
+++ b/src/lib66/tree/deps-lib/deps
@@ -26,8 +26,9 @@ tree_seed_isvalid.o
 tree_seed_parse_file.o
 tree_seed_resolve_path.o
 tree_seed_setseed.o
+tree_service_add.o
+tree_service_remove.o
 tree_sethome.o
-tree_setname.o
 tree_switch_current.o
 -loblibs
 -lskarnet
diff --git a/src/lib66/tree/tree_service_add.c b/src/lib66/tree/tree_service_add.c
new file mode 100644
index 00000000..ee01bd42
--- /dev/null
+++ b/src/lib66/tree/tree_service_add.c
@@ -0,0 +1,63 @@
+/*
+ * tree_service_add.c
+ *
+ * Copyright (c) 2018-2021 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 <oblibs/log.h>
+#include <oblibs/sastr.h>
+
+#include <skalibs/stralloc.h>
+
+#include <66/resolve.h>
+#include <66/tree.h>
+
+void tree_service_add(char const *base, char const *treename, char const *service)
+{
+    log_flow() ;
+
+    resolve_tree_t tres = RESOLVE_TREE_ZERO ;
+    resolve_wrapper_t_ref wres = resolve_set_struct(DATA_TREE, &tres) ;
+    stralloc sa = STRALLOC_ZERO ;
+
+    log_trace("modify field contents of resolve tree file: ", treename) ;
+
+    if (!resolve_read_g(wres, base, treename))
+        log_dieusys(LOG_EXIT_SYS, "read resolve file of tree: ", treename) ;
+
+    if (tres.ncontents) {
+
+        if (!sastr_clean_string(&sa, tres.sa.s + tres.contents))
+            log_dieu(LOG_EXIT_SYS, "clean string") ;
+
+        if (!sastr_add_string(&sa, service))
+            log_dieu(LOG_EXIT_SYS, "clean string") ;
+
+        if (!sastr_sortndrop_element(&sa))
+            log_dieu(LOG_EXIT_SYS, "sort string") ;
+
+    } else if (!sastr_add_string(&sa, service))
+        log_dieu(LOG_EXIT_SYS, "add string") ;
+
+    tres.ncontents++ ;
+
+    if (!sastr_rebuild_in_oneline(&sa))
+        log_dieu(LOG_EXIT_SYS, "rebuild stralloc list") ;
+
+    if (!resolve_modify_field(wres, E_RESOLVE_TREE_CONTENTS, sa.s))
+        log_dieusys(LOG_EXIT_SYS, "modify resolve file of: ", treename) ;
+
+    if (!resolve_write_g(wres, base, treename))
+        log_dieusys(LOG_EXIT_SYS, "write resolve file of tree: ", treename) ;
+
+    stralloc_free(&sa) ;
+    resolve_free(wres) ;
+}
diff --git a/src/lib66/tree/tree_service_remove.c b/src/lib66/tree/tree_service_remove.c
new file mode 100644
index 00000000..3c0839d2
--- /dev/null
+++ b/src/lib66/tree/tree_service_remove.c
@@ -0,0 +1,60 @@
+/*
+ * tree_service_remove.c
+ *
+ * Copyright (c) 2018-2021 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 <oblibs/log.h>
+#include <oblibs/sastr.h>
+
+#include <skalibs/stralloc.h>
+
+#include <66/resolve.h>
+#include <66/tree.h>
+
+void tree_service_remove(char const *base, char const *treename, char const *service)
+{
+    log_flow() ;
+
+    resolve_tree_t tres = RESOLVE_TREE_ZERO ;
+    resolve_wrapper_t_ref wres = resolve_set_struct(DATA_TREE, &tres) ;
+    stralloc sa = STRALLOC_ZERO ;
+    char *str = 0 ;
+
+    log_trace("modify field contents of resolve tree file: ", treename) ;
+
+    if (!resolve_read_g(wres, base, treename))
+        log_dieusys(LOG_EXIT_SYS, "read resolve file of tree: ", treename) ;
+
+    if (!sastr_clean_string(&sa, tres.sa.s + tres.contents))
+        log_dieu(LOG_EXIT_SYS, "clean string") ;
+
+    if (!sastr_remove_element(&sa, service))
+        log_dieu(LOG_EXIT_SYS, "remove service: ", service, " list") ;
+
+    if (sa.len) {
+        if (!sastr_rebuild_in_oneline(&sa))
+            log_dieu(LOG_EXIT_SYS, "rebuild stralloc list") ;
+        str = sa.s ;
+
+    } else str = "" ;
+
+    if (!resolve_modify_field(wres, E_RESOLVE_TREE_CONTENTS, str))
+        log_dieusys(LOG_EXIT_SYS, "modify resolve file of: ", treename) ;
+
+    tres.ncontents-- ;
+
+    if (!resolve_write_g(wres, base, treename))
+        log_dieusys(LOG_EXIT_SYS, "write resolve file of tree: ", treename) ;
+
+    stralloc_free(&sa) ;
+    resolve_free(wres) ;
+}
-- 
GitLab