poldek: poldek/cli/alias.c, poldek/cli/cli.c, poldek/cli/cli.h, po...

mis mis at pld-linux.org
Sat Feb 2 21:39:06 CET 2008


Author: mis                          Date: Sat Feb  2 20:39:06 2008 GMT
Module: poldek                        Tag: HEAD
---- Log message:
- cli.conf and configurable prompt (#5798 at oldbugs)

---- Files affected:
poldek/poldek/cli:
   alias.c (1.10 -> 1.11) , cli.c (1.42 -> 1.43) , cli.h (1.17 -> 1.18) , cmd.h (1.15 -> 1.16) , shell.c (1.31 -> 1.32) 

---- Diffs:

================================================================
Index: poldek/poldek/cli/alias.c
diff -u poldek/poldek/cli/alias.c:1.10 poldek/poldek/cli/alias.c:1.11
--- poldek/poldek/cli/alias.c:1.10	Wed Jun 27 02:44:47 2007
+++ poldek/poldek/cli/alias.c	Sat Feb  2 21:39:01 2008
@@ -1,25 +1,30 @@
-/* 
-  Copyright (C) 2000 - 2003 Pawel A. Gajda (mis at k2.net.pl)
- 
+/*
+  Copyright (C) 2000 - 2008 Pawel A. Gajda <mis at pld-linux.org>
+
   This program is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License published by
-  the Free Software Foundation (see file COPYING for details).
+  it under the terms of the GNU General Public License, version 2 as
+  published by the Free Software Foundation (see file COPYING for details).
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 /*
   $Id$
 */
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <errno.h>
 #include <limits.h>
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
 
-#include <trurl/nassert.h>
-#include <trurl/narray.h>
-#include <trurl/nmalloc.h>
-#include <trurl/nstr.h>
+#include <trurl/trurl.h>
 
 #include "i18n.h"
 #include "log.h"
@@ -158,21 +163,52 @@
     }
 }
 
-int poclidek_load_aliases(struct poclidek_ctx *cctx, const char *path) 
+int poclidek__load_aliases(struct poclidek_ctx *cctx)
 {
-    tn_hash *aliases_htcnf, *ht;
-    tn_array *keys;
-    int i, n = 0;
-    
-    if (access(path, R_OK) != 0)
-        return 0;
+    char *homedir, *sysconfdir = "/etc", path[PATH_MAX];
+
+#ifdef SYSCONFDIR
+    if (n_str_ne(sysconfdir, SYSCONFDIR) && access(SYSCONFDIR, R_OK) == 0)
+        sysconfdir = SYSCONFDIR;
+#endif
+
+    n_snprintf(path, sizeof(path), "%s/poldek/aliases.conf", sysconfdir);
+    if (access(path, R_OK) == 0) {
+        tn_hash *htcnf = poldek_conf_load(path, POLDEK_LDCONF_FOREIGN);
+        if (htcnf) {
+            tn_hash *aliases = poldek_conf_get_section(htcnf, "global");
+            poclidek__add_aliases(cctx, aliases);
+            n_hash_free(htcnf);
+        }
+    }
     
-    aliases_htcnf = poldek_conf_load(path, POLDEK_LDCONF_FOREIGN);
-    if (aliases_htcnf == NULL)
-        return 0;
+    if ((homedir = getenv("HOME")) != NULL) {
+        tn_hash *htcnf;
+        int load = 1;
+        
+		snprintf(path, sizeof(path), "%s/.poldek-aliases.conf", homedir);
+
+        if (access(path, R_OK) != 0) {
+            snprintf(path, sizeof(path), "%s/.poldek.alias", homedir);
+            if (access(path, R_OK) != 0)
+                load = 0;
+        }
+        if (load && (htcnf = poldek_conf_load(path, POLDEK_LDCONF_FOREIGN))) {
+            tn_hash *aliases = poldek_conf_get_section(htcnf, "global");
+            poclidek__add_aliases(cctx, aliases);
+            n_hash_free(htcnf);
+        }
+    }
     
-    ht = poldek_conf_get_section(aliases_htcnf, "global");
-    keys = n_hash_keys(ht);
+    return 1;
+}
+
+int poclidek__add_aliases(struct poclidek_ctx *cctx, tn_hash *htcnf)
+{
+    tn_array *keys;
+    int i, n = 0;
+
+    keys = n_hash_keys(htcnf);
 
     for (i=0; i < n_array_size(keys); i++) {
         const char *name, *cmdline;
@@ -181,18 +217,17 @@
         if (*name == '_')       /* config macro */
             continue;
         
-        if ((cmdline = poldek_conf_get(ht, name, NULL)))
+        if ((cmdline = poldek_conf_get(htcnf, name, NULL)))
             if (add_alias(cctx, name, cmdline))
                 n++;
     }
     
     n_array_free(keys);
-    n_hash_free(aliases_htcnf);
-    
+
     if (n)
         find_aliased_commands(cctx);
         
-    return 1;
+    return n;
 }
 
 

================================================================
Index: poldek/poldek/cli/cli.c
diff -u poldek/poldek/cli/cli.c:1.42 poldek/poldek/cli/cli.c:1.43
--- poldek/poldek/cli/cli.c:1.42	Mon Jul  9 19:01:02 2007
+++ poldek/poldek/cli/cli.c	Sat Feb  2 21:39:01 2008
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2000 - 2005 Pawel A. Gajda <mis at k2.net.pl>
+  Copyright (C) 2000 - 2008 Pawel A. Gajda <mis at pld-linux.org>
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2 as
@@ -37,6 +37,7 @@
 #include "pkgdir/pkgdir.h"
 #include "i18n.h"
 #include "log.h"
+#include "conf.h"
 #include "cli.h"
 #include "cmd_chain.h"
 #include "cmd_pipe.h"
@@ -233,8 +234,7 @@
 };
 
 
-static
-int do_exec_cmd_ent(struct cmdctx *cmdctx, int argc, char **argv) 
+static int do_exec_cmd_ent(struct cmdctx *cmdctx, int argc, char **argv) 
 {
     int                  rc = 1;
     unsigned             parse_flags;
@@ -324,9 +324,8 @@
     return *fmt == '!';
 }
 
-static
-int do_cmdctx_printf(struct cmdctx *cmdctx, int color, const char *fmt,
-                     va_list args)
+static int do_cmdctx_printf(struct cmdctx *cmdctx, int color, const char *fmt,
+                            va_list args)
 {
     int is_ctrl, n = 0;
     
@@ -378,8 +377,7 @@
     return 1;
 }
 
-static
-int command_cmp(struct poclidek_cmd *c1, struct poclidek_cmd *c2) 
+static int command_cmp(struct poclidek_cmd *c1, struct poclidek_cmd *c2) 
 {
     return strcmp(c1->name, c2->name);
 }
@@ -427,9 +425,9 @@
 }
 
 
-static void init_commands(struct poclidek_ctx *cctx) 
+static void init_commands(struct poclidek_ctx *cctx, tn_hash *global_aliases,
+                          tn_hash *local_aliases) 
 {
-    char *homedir, *sysconfdir = "/etc", path[PATH_MAX];
     int n = 0;
     
     cctx->commands = n_array_new(16, (tn_fn_free)command_free,
@@ -441,34 +439,93 @@
     }
 	n_array_sort(cctx->commands);
 
+    n = 0;
+    if (global_aliases)
+        n += poclidek__add_aliases(cctx, global_aliases);
+    
+    if (local_aliases)
+        n += poclidek__add_aliases(cctx, local_aliases);
+    
+    if (n == 0)
+        poclidek__load_aliases(cctx); /* load legacy config(s) */
+    
+    n_array_sort(cctx->commands);
+}
+
+static void *dent_alloc(struct poclidek_ctx *cctx, size_t size)
+{
+    return cctx->_dent_na->na_malloc(cctx->_dent_na, size);
+}
+
+static int config_path(char *globalpath, char *localpath, int size)
+{
+    char *homedir, *sysconfdir = "/etc";
+    
 #ifdef SYSCONFDIR
-    if (access(SYSCONFDIR, R_OK) == 0)
+    if (n_str_ne(sysconfdir, SYSCONFDIR) && access(SYSCONFDIR, R_OK) == 0)
         sysconfdir = SYSCONFDIR;
 #endif
-
-    n_snprintf(path, sizeof(path), "%s/poldek/aliases.conf", sysconfdir);
-    poclidek_load_aliases(cctx, path);
-
+    *localpath = *globalpath = '\0';
+    
+    n_snprintf(globalpath, size, "%s/poldek/cli.conf", sysconfdir);
+    if (access(globalpath, R_OK) != 0)
+        *globalpath = '\0';
+    
+    
     if ((homedir = getenv("HOME")) != NULL) {
-		char path[PATH_MAX];
-		snprintf(path, sizeof(path), "%s/.poldek-aliases.conf", homedir);	
-		if (!poclidek_load_aliases(cctx, path)) {
-            snprintf(path, sizeof(path), "%s/.poldek.alias", homedir);
-            poclidek_load_aliases(cctx, path);
+        int n;
+
+        n = n_snprintf(localpath, size, "%s/.poclidekrc", homedir);
+        if (access(localpath, R_OK) != 0) {
+            n = n_snprintf(localpath, size, "%s/.poldekclirc", homedir);
+            if (access(localpath, R_OK) != 0)
+                *localpath = '\0';
         }
-	}
-    n_array_sort(cctx->commands);
+    }
+    
+    DBGF("%s\n", sysconfdir);
+    
+    return *localpath != '\0' || *globalpath != '\0';
 }
 
-static void *dent_alloc(struct poclidek_ctx *cctx, size_t size)
+static tn_hash *loadconf(void) 
 {
-    return cctx->_dent_na->na_malloc(cctx->_dent_na, size);
+    char path[PATH_MAX], localpath[PATH_MAX];
+    tn_hash *htcnf = NULL, *local_htcnf = NULL;
+    
+    if (!config_path(path, localpath, sizeof(path)))
+        return NULL;
+    
+    if (*path)
+        htcnf = poldek_conf_load(path, POLDEK_LDCONF_FOREIGN);
+
+    if (*localpath)
+        local_htcnf = poldek_conf_load(localpath, POLDEK_LDCONF_FOREIGN);
+
+    if (htcnf == NULL) {
+        htcnf = local_htcnf;
+        local_htcnf = NULL;
+    }
+
+    if (htcnf && local_htcnf) {
+        tn_array *aliases = poldek_conf_get_sections(htcnf, "aliases");
+        if (aliases) /* XXX: yep, hacky a bit; sections should be movable
+                        via conf API */
+            n_hash_insert(local_htcnf, "global_aliases", n_ref(aliases));
+        
+        n_hash_free(htcnf);
+        htcnf = local_htcnf;
+        local_htcnf = NULL;
+    }
+    
+    return htcnf;
 }
 
-static
-int poclidek_init(struct poclidek_ctx *cctx, struct poldek_ctx *ctx)
+static int poclidek_init(struct poclidek_ctx *cctx, struct poldek_ctx *ctx)
 {
-    n_assert (cctx->ctx == NULL);
+    tn_hash *htcnf;
+    
+    n_assert(cctx->ctx == NULL);
 
     cctx->_flags = 0;
     cctx->ctx = poldek_link(ctx);
@@ -479,7 +536,12 @@
     cctx->rootdir = pkg_dent_add_dir(cctx, NULL, "/");
     cctx->currdir = cctx->rootdir;
     cctx->homedir = NULL;
-    init_commands(cctx);
+    cctx->htcnf = htcnf = loadconf();
+    
+    init_commands(cctx,
+                  htcnf? poldek_conf_get_section(htcnf, "global_aliases"):NULL,
+                  htcnf? poldek_conf_get_section(htcnf, "aliases"):NULL);
+    
     return 1;
 }
 
@@ -495,6 +557,11 @@
 static
 void poclidek_destroy(struct poclidek_ctx *cctx) 
 {
+    if (cctx->htcnf) {
+        n_hash_free(cctx->htcnf);
+        cctx->htcnf = NULL;
+    }
+    
     cctx->pkgs_available = NULL;
     cctx->pkgs_installed = NULL;
 
@@ -739,7 +806,7 @@
             pkg_dent_remove_pkg(ent, pkg);
         
         n++;
-        DBGF("- %s\n", pkg_id(pkg));
+        DBGF_F("- %s\n", pkg_id(pkg));
     }
 
     for (i=0; i < n_array_size(ts->pkgs_installed); i++) {
@@ -757,7 +824,7 @@
         pkgdir_add_package(cctx->dbpkgdir, pkg);
         if (ent)
             pkg_dent_add_pkg(cctx, ent, pkg);
-        DBGF("+ %s\n", pkg_id(pkg));
+        DBGF_F("+ %s\n", pkg_id(pkg));
         n++;
     }
         

================================================================
Index: poldek/poldek/cli/cli.h
diff -u poldek/poldek/cli/cli.h:1.17 poldek/poldek/cli/cli.h:1.18
--- poldek/poldek/cli/cli.h:1.17	Fri Jun 29 00:28:21 2007
+++ poldek/poldek/cli/cli.h	Sat Feb  2 21:39:01 2008
@@ -23,7 +23,9 @@
 #define OPT_GID_OP_SPLIT      (1400 + OPT_GID_BASE)
 #define OPT_GID_OP_OTHER      (1600 + OPT_GID_BASE)
 
-int poclidek_load_aliases(struct poclidek_ctx *cctx, const char *path);
+int poclidek__load_aliases(struct poclidek_ctx *cctx);
+int poclidek__add_aliases(struct poclidek_ctx *cctx, tn_hash *htcnf);
+
 
 void poclidek_apply_iinf(struct poclidek_ctx *cctx, struct poldek_ts *ts);
 

================================================================
Index: poldek/poldek/cli/cmd.h
diff -u poldek/poldek/cli/cmd.h:1.15 poldek/poldek/cli/cmd.h:1.16
--- poldek/poldek/cli/cmd.h:1.15	Wed Jun 20 19:15:45 2007
+++ poldek/poldek/cli/cmd.h	Sat Feb  2 21:39:01 2008
@@ -110,6 +110,7 @@
     unsigned            flags;
     struct poldek_ctx   *ctx;
     tn_array            *commands;
+    tn_hash             *htcnf;
     const tn_array      *pkgs_available;   /* array of available pkgs  */
     const tn_array      *pkgs_installed;   /* array of installed pkgs  */
 

================================================================
Index: poldek/poldek/cli/shell.c
diff -u poldek/poldek/cli/shell.c:1.31 poldek/poldek/cli/shell.c:1.32
--- poldek/poldek/cli/shell.c:1.31	Mon Jul  9 22:52:44 2007
+++ poldek/poldek/cli/shell.c	Sat Feb  2 21:39:01 2008
@@ -39,6 +39,7 @@
 #include <sigint/sigint.h>
 #include "i18n.h"
 #include "log.h"
+#include "conf.h"
 #include "pkg.h"
 #include "poldek_term.h"
 #include "cmd.h"
@@ -342,8 +343,18 @@
 
 int poclidek_shell(struct poclidek_ctx *cctx)
 {
+    const char *prompt_prefix = "poldek";
     char *line, *s, *home;
-    
+
+    if (cctx->htcnf) {
+        tn_hash *global = poldek_conf_get_section(cctx->htcnf, "global");
+        const char *s = global ? poldek_conf_get(global, "prompt", NULL) : NULL;
+        if (s) {
+            prompt_prefix = s;
+            DBGF_F("prompt_prefix %s\n", s);
+        }
+        
+    }
     
     if (!isatty(fileno(stdout))) {
         logn(LOGERR, _("not a tty"));
@@ -377,9 +388,9 @@
         char prompt[255];
         
         sigint_reset();
-        snprintf(prompt, sizeof(prompt), "poldek:%s%s> ",
-                 currdir == NULL ? "/" : *currdir->name == '/' ? "" : "/",
-                 currdir == NULL ? "" : currdir->name);
+        n_snprintf(prompt, sizeof(prompt), "%s:%s%s> ", prompt_prefix,
+                   currdir == NULL ? "/" : *currdir->name == '/' ? "" : "/",
+                   currdir == NULL ? "" : currdir->name);
 
         if ((line = readline(prompt)) == NULL)
             break;
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/poldek/poldek/cli/alias.c?r1=1.10&r2=1.11&f=u
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/poldek/poldek/cli/cli.c?r1=1.42&r2=1.43&f=u
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/poldek/poldek/cli/cli.h?r1=1.17&r2=1.18&f=u
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/poldek/poldek/cli/cmd.h?r1=1.15&r2=1.16&f=u
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/poldek/poldek/cli/shell.c?r1=1.31&r2=1.32&f=u



More information about the pld-cvs-commit mailing list