Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: $sysparams in zsh/system



This adds the readonly associative array sysparams to the zsh/system
module.  The keys are names of dynamically change system parameters;
currently only pid and ppid but it's now very easy to extend.  Hope the
name is OK:  I didn't want to make it *too* obscure.

The associative array handling is a little hairy, but seems to be
working so far.

Index: Doc/Zsh/mod_system.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_system.yo,v
retrieving revision 1.3
diff -u -r1.3 mod_system.yo
--- Doc/Zsh/mod_system.yo	1 Apr 2005 12:04:22 -0000	1.3
+++ Doc/Zsh/mod_system.yo	5 Jun 2006 13:15:05 -0000
@@ -2,7 +2,7 @@
 A builtin interface to various low-level system features.
 !MOD!)
 The tt(zsh/system) module makes available three builtin commands and
-a parameter.
+two parameters.
 
 sect(Builtins)
 
@@ -114,6 +114,7 @@
 sect(Parameters)
 
 startitem()
+vindex(errnos)
 item(tt(errnos))(
 A readonly array of the names of errors defined on the system.  These
 are typically macros defined in C by including the system header file
@@ -125,4 +126,19 @@
 Note that aliases for errors are not handled; only the canonical name is
 used.
 )
+vindex(sysparams)
+item(tt(sysparams))(
+A readonly associative array.  The keys are:
+startitem()
+item(tt(pid))(
+Returns the process ID of the current process, even in subshells.  Compare
+tt($$), which returns the process ID of the main shell process.
+)
+item(tt(ppid))
+Returns the process ID of the parent of the current process, even in
+subshells.  Compare tt($PPID), which returns the process ID of the parent
+of the main shell process.
+)
+enditem()
+)
 enditem()
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.113
diff -u -r1.113 params.c
--- Src/params.c	30 May 2006 22:35:03 -0000	1.113
+++ Src/params.c	5 Jun 2006 13:15:12 -0000
@@ -2516,7 +2516,7 @@
 /* Function to set value of a scalar (string) parameter */
 
 /**/
-static void
+mod_export void
 strsetfn(Param pm, char *x)
 {
     zsfree(pm->u.str);
@@ -2587,7 +2587,7 @@
 /* Function to set value of an association parameter using key/value pairs */
 
 /**/
-static void
+mod_export void
 arrhashsetfn(Param pm, char **val, int augment)
 {
     /* Best not to shortcut this by using the existing hash table,   *
Index: Src/Modules/system.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/system.c,v
retrieving revision 1.6
diff -u -r1.6 system.c
--- Src/Modules/system.c	30 May 2006 22:35:03 -0000	1.6
+++ Src/Modules/system.c	5 Jun 2006 13:15:16 -0000
@@ -340,6 +340,12 @@
     return 0;
 }
 
+static struct builtin bintab[] = {
+    BUILTIN("syserror", 0, bin_syserror, 0, 1, 0, "e:p:", NULL),
+    BUILTIN("sysread", 0, bin_sysread, 0, 1, 0, "c:i:o:s:t:", NULL),
+    BUILTIN("syswrite", 0, bin_syswrite, 1, 1, 0, "c:o:", NULL),
+};
+
 
 /* Functions for the errnos special parameter. */
 
@@ -351,16 +357,54 @@
     return arrdup((char **)sys_errnames);
 }
 
-
-static struct builtin bintab[] = {
-    BUILTIN("syserror", 0, bin_syserror, 0, 1, 0, "e:p:", NULL),
-    BUILTIN("sysread", 0, bin_sysread, 0, 1, 0, "c:i:o:s:t:", NULL),
-    BUILTIN("syswrite", 0, bin_syswrite, 1, 1, 0, "c:o:", NULL),
-};
-
 static const struct gsu_array errnos_gsu =
 { errnosgetfn, arrsetfn, stdunsetfn };
 
+
+/* Functions for the sysparams special parameter. */
+
+/**/
+static char *
+sysparamgetfn(Param pm)
+{
+    char buf[DIGBUFSIZE];
+    int num;
+
+    if (!strcmp(pm->node.nam, "pid")) {
+	num = (int)getpid();
+    } else if (!strcmp(pm->node.nam, "ppid")) {
+	num = (int)getppid();
+    }
+    else {
+#ifdef DEBUG
+	dputs("Bad sysparam parameter");
+#endif
+	return "";
+    }
+
+    sprintf(buf, "%d", num);
+    return dupstring(buf);
+}
+
+static const struct gsu_scalar sysparam_gsu =
+{ sysparamgetfn, strsetfn, stdunsetfn };
+
+static void
+fixsysparams(HashNode hn, int flags)
+{
+    Param pm = (Param)hn;
+
+    if (flags) {
+	/* prepare to free */
+	pm->node.flags &= ~PM_READONLY;
+    } else {
+	/* assign */
+	pm->gsu.s = &sysparam_gsu;
+	pm->node.flags |= PM_READONLY;
+    }
+}
+
+
 /* The load/unload routines required by the zsh library interface */
 
 /**/
@@ -385,7 +429,12 @@
 int
 boot_(Module m)
 {
-    Param pm_nos;
+    Param pm_nos, pm_params;
+    HashTable ht;
+    const char *sysparams_args[] = {
+	"pid", 	"ppid", NULL
+    }, **srcptr;
+    char **arglist, **dstptr;
 
     /* this takes care of an autoload on errnos */
     unsetparam("errnos");
@@ -394,8 +443,31 @@
 	return 1;
     pm_nos->gsu.a = &errnos_gsu;
 
+    if (!(pm_params = createparam("sysparams", PM_HASHED|PM_SPECIAL|
+				  PM_HIDE|PM_HIDEVAL|PM_REMOVABLE))) {
+	tidyparam(pm_nos);
+	return 1;
+    }
+    pm_params->level = pm_params->old ? locallevel : 0;
+    pm_params->gsu.h = &stdhash_gsu;
+    pm_params->u.hash = ht = newparamtable(0, "sysparams");
+
+    arglist = (char **)zshcalloc((2*arrlen((char **)sysparams_args) + 1) *
+			       sizeof(char *));
+    for (srcptr = sysparams_args, dstptr = arglist; *srcptr; ) {
+	*dstptr++ = ztrdup(*srcptr++);
+	*dstptr++ = ztrdup("");
+    }
+    *dstptr = NULL;
+    /* make sure we don't overwrite the hash table: use the "augment" arg */
+    arrhashsetfn(pm_params, arglist, 1);
+    scanhashtable(ht, 0, 0, 0, fixsysparams, 0);
+
+    pm_params->node.flags |= PM_READONLY;
+
     if (!addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab))) {
 	tidyparam(pm_nos);
+	tidyparam(pm_params);
 	return 1;
     }
     return 0;
@@ -406,7 +478,14 @@
 int
 cleanup_(Module m)
 {
-    tidyparam((Param)paramtab->getnode(paramtab, "errnos"));
+    Param pm;
+    if ((pm = (Param)paramtab->getnode(paramtab, "errnos")))
+	tidyparam(pm);
+    if ((pm = (Param)paramtab->getnode(paramtab, "sysparams")))
+    {
+	scanhashtable(pm->u.hash, 0, 0, 0, fixsysparams, 1);
+	tidyparam(pm);
+    }
 
     deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
     return 0;

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author