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

PATCH: zsystem supports



It occurs to me it would be useful to be able to tell easily whether
"zsystem flock" is supported, and indeed if "zsystem" is going to expand
it would be useful to be able to tell if the version has the subcommand
you want.

Index: Doc/Zsh/mod_system.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_system.yo,v
retrieving revision 1.6
diff -p -u -r1.6 mod_system.yo
--- Doc/Zsh/mod_system.yo	24 Feb 2010 21:38:08 -0000	1.6
+++ Doc/Zsh/mod_system.yo	25 Feb 2010 10:35:05 -0000
@@ -148,6 +148,16 @@ If the option tt(-r) is given, the lock 
 it is for reading and writing.  The file descriptor is opened
 accordingly.
 )
+item(tt(zsystem supports) var(subcommand))(
+The builtin tt(zsystem)'s subcommand tt(supports) tests whether a
+given subcommand is supported.  It returns status 0 if so, else
+status 1.  It operates silently unless there was a syntax error
+(i.e. the wrong number of arguments), in which case status 255
+is returned.  Status 1 can indicate one of two things:  var(subcommand)
+is known but not supported by the current operating system, or
+var(subcommand) is not known (possibly because this is an older
+version of the shell before it was implemented).
+)
 enditem()
 
 subsect(Parameters)
Index: Src/Modules/system.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/system.c,v
retrieving revision 1.10
diff -p -u -r1.10 system.c
--- Src/Modules/system.c	24 Feb 2010 21:38:10 -0000	1.10
+++ Src/Modules/system.c	25 Feb 2010 10:35:05 -0000
@@ -491,6 +491,35 @@ bin_zsystem_flock(char *nam, char **args
 }
 
 
+/*
+ * Return status zero if the zsystem feature is supported, else 1.
+ * Operates silently for future-proofing.
+ */
+/**/
+static int
+bin_zsystem_supports(char *nam, char **args,
+		     UNUSED(Options ops), UNUSED(int func))
+{
+    if (!args[0]) {
+	zwarnnam(nam, "supports: not enough arguments");
+	return 255;
+    }
+    if (args[1]) {
+	zwarnnam(nam, "supports: too many arguments");
+	return 255;
+    }
+
+    /* stupid but logically this should work... */
+    if (!strcmp(*args, "supports"))
+	return 0;
+#ifdef HAVE_FCNTL_H
+    if (!strcmp(*args, "flock"))
+	return 0;
+#endif
+    return 1;
+}
+
+
 /**/
 static int
 bin_zsystem(char *nam, char **args, Options ops, int func)
@@ -498,6 +527,8 @@ bin_zsystem(char *nam, char **args, Opti
     /* If more commands are implemented, this can be more sophisticated */
     if (!strcmp(*args, "flock")) {
 	return bin_zsystem_flock(nam, args+1, ops, func);
+    } else if (!strcmp(*args, "supports")) {
+	return bin_zsystem_supports(nam, args+1, ops, func);
     }
     zwarnnam(nam, "unknown subcommand: %s", *args);
     return 1;


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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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