From 7df1e682de0ba47f9fb8fcbf3f6e75f46d4e0ceb Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 15 Oct 2022 21:12:51 +1100
Subject: [PATCH] adapt to new state format

---
 src/66/66-instate.c | 123 +++++++++++++++++++++-----------------------
 1 file changed, 60 insertions(+), 63 deletions(-)

diff --git a/src/66/66-instate.c b/src/66/66-instate.c
index a004a398..0d5898e6 100644
--- a/src/66/66-instate.c
+++ b/src/66/66-instate.c
@@ -31,13 +31,14 @@
 #include <66/utils.h>
 #include <66/constants.h>
 #include <66/state.h>
+#include <66/config.h>
 
-#define MAXOPTS 7
+#define MAXOPTS 11
 
 static wchar_t const field_suffix[] = L" :" ;
 static char fields[INFO_NKEY][INFO_FIELD_MAXLEN] = {{ 0 }} ;
 
-#define USAGE "66-instate [ -h ] [ -z ] [ -t tree ] [ -l ] service"
+#define USAGE "66-instate [ -h ] [ -v verbosity ] [ -z ] service"
 
 static inline void info_help (void)
 {
@@ -49,7 +50,6 @@ static inline void info_help (void)
 "   -h: print this help\n"
 "   -z: use color\n"
 "   -t: only search service at the specified tree\n"
-"   -l: prints information of the associated logger if exist\n"
 ;
 
     log_info(USAGE,"\n",help) ;
@@ -86,27 +86,27 @@ static void info_display_int(char const *field,unsigned int id)
 int main(int argc, char const *const *argv)
 {
     int found = 0 ;
-    uint8_t logger = 0 ;
     resolve_service_t res = RESOLVE_SERVICE_ZERO ;
     resolve_wrapper_t_ref wres = resolve_set_struct(DATA_SERVICE, &res) ;
-    stralloc satree = STRALLOC_ZERO ;
-    stralloc src = STRALLOC_ZERO ;
-    stralloc tmp = STRALLOC_ZERO ;
+    char base[SS_MAX_PATH + 1] ;
     ss_state_t sta = STATE_ZERO ;
-    char const *tname = 0 ;
     char const *svname = 0 ;
     char const *ste = 0 ;
+    char atree[SS_MAX_TREENAME + 1] ;
 
     log_color = &log_color_disable ;
 
     char buf[MAXOPTS][INFO_FIELD_MAXLEN] = {
-        "Reload",
-        "Init",
-        "Unsupervise",
-        "State",
-        "Pid",
-        "Name" ,
-        "Real_logger_name" } ;
+        "toinit",
+        "toreload",
+        "torestart",
+        "tounsupervise",
+        "isdownfile",
+        "isearlier" ,
+        "isenabled" ,
+        "isparsed" ,
+        "issupervised",
+        "isup" } ;
 
     PROG = "66-instate" ;
     {
@@ -114,16 +114,35 @@ int main(int argc, char const *const *argv)
 
         for (;;)
         {
-            int opt = getopt_args(argc,argv, ">hzlt:", &l) ;
+            int opt = getopt_args(argc,argv, ">hv:zt:", &l) ;
             if (opt == -1) break ;
             if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ;
             switch (opt)
             {
-                case 'h' : info_help(); return 0 ;
-                case 'z' : log_color = !isatty(1) ? &log_color_disable : &log_color_enable ; break ;
-                case 't' : tname = l.arg ; break ;
-                case 'l' : logger = 1 ; break ;
-                default : log_usage(USAGE) ;
+                case 'h' :
+
+                    info_help();
+                    return 0 ;
+
+                case 'v' :
+
+                        if (!uint0_scan(l.arg, &VERBOSITY))
+                            log_usage(USAGE) ;
+
+                        break ;
+
+                case 'z' :
+
+                    log_color = !isatty(1) ? &log_color_disable : &log_color_enable ;
+                    break ;
+
+                case 't' :
+
+                    log_1_warn("deprecated option -t -- ignore it") ;
+                    break ;
+
+                default :
+                    log_usage(USAGE) ;
             }
         }
         argc -= l.ind ; argv += l.ind ;
@@ -132,55 +151,33 @@ int main(int argc, char const *const *argv)
     if (!argc) log_usage(USAGE) ;
     svname = *argv ;
 
-    found = service_intree(&src,svname,tname) ;
-    if (found == -1) log_dieu(LOG_EXIT_SYS,"resolve tree source of sv: ",svname) ;
-    else if (!found) {
-        log_info("no tree exist yet") ;
-        goto freed ;
-    }
-    else if (found > 2) {
-        log_die(LOG_EXIT_SYS,svname," is set on different tree -- please use -t options") ;
-    }
-    else if (found == 1) log_die(LOG_EXIT_SYS,"unknown service: ",svname) ;
+    if (!set_ownersysdir_stack(base, getuid()))
+        log_dieu(LOG_EXIT_SYS, "set owner directory") ;
 
-    if (!resolve_read(wres,src.s,svname)) log_dieusys(111,"read resolve file of: ",src.s,"/.resolve/",svname) ;
+    found = service_is_g(atree, svname, STATE_FLAGS_ISPARSED) ;
+    if (found == -1)
+        log_dieusys(LOG_EXIT_SYS, "get information of service: ", svname, " -- please a bug report") ;
+    else if (!found)
+        log_die(LOG_EXIT_USER, "unknown service: ", svname) ;
 
     info_field_align(buf,fields,field_suffix,MAXOPTS) ;
 
-    ste = res.sa.s + res.state ;
-
-    if (!state_check(ste,svname)) log_diesys(111,"unitialized service: ",svname) ;
-    if (!state_read(&sta,ste,svname)) log_dieusys(111,"read state file of: ",ste,"/",svname) ;
+    if (!state_check(base, svname)) log_diesys(111,"unitialized service: ",svname) ;
+    if (!state_read(&sta, base, svname)) log_dieusys(111,"read state file of: ",ste,"/",svname) ;
 
-    info_display_string(fields[5],svname) ;
-    info_display_int(fields[0],sta.reload) ;
-    info_display_int(fields[1],sta.init) ;
-    info_display_int(fields[2],sta.unsupervise) ;
-    info_display_int(fields[3],sta.state) ;
-    info_display_int(fields[4],sta.pid) ;
+    info_display_int(fields[0],sta.toinit) ;
+    info_display_int(fields[1],sta.toreload) ;
+    info_display_int(fields[2],sta.torestart) ;
+    info_display_int(fields[3],sta.tounsupervise) ;
+    info_display_int(fields[4],sta.isdownfile) ;
+    info_display_int(fields[5],sta.isearlier) ;
+    info_display_int(fields[6],sta.isenabled) ;
+    info_display_int(fields[7],sta.isparsed) ;
+    info_display_int(fields[8],sta.issupervised) ;
+    info_display_int(fields[9],sta.isup) ;
 
-    if (res.logger && logger)
-    {
-        svname = res.sa.s + res.logger ;
-        if (!state_check(ste,svname)) log_dieusys(111,"unitialized: ",svname) ;
-        if (!state_read(&sta,ste,svname)) log_dieusys(111,"read state file of: ",ste,"/",svname) ;
-
-        if (buffer_putsflush(buffer_1,"\n") == -1)
-            log_dieusys(LOG_EXIT_SYS,"write to stdout") ;
 
-        info_display_string(fields[6],res.sa.s + res.logreal) ;
-        info_display_int(fields[0],sta.reload) ;
-        info_display_int(fields[1],sta.init) ;
-        info_display_int(fields[2],sta.unsupervise) ;
-        info_display_int(fields[3],sta.state) ;
-        info_display_int(fields[4],sta.pid) ;
-
-    }
-
-    freed:
     resolve_free(wres) ;
-    stralloc_free(&satree) ;
-    stralloc_free(&src) ;
-    stralloc_free(&tmp) ;
+
     return 0 ;
 }
-- 
GitLab