zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: strftime -s
@ 2004-01-22 17:28 Peter Stephenson
  2004-01-22 17:52 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2004-01-22 17:28 UTC (permalink / raw)
  To: Zsh hackers list

This adds the option `-s scalar' to strftime to do an assignment instead
of a print.  Otherwise there's no gain over using date when assigning to
a parameter.

It also fixes a bug that the EPOCHSECONDS wasn't unset when the module
was unloaded, which could have nasty effects.  There ought to be
something in the development guide about this.  (Ideally, there ought to
be an easier way of removing module parameters --- it's a bit hairy at
the moment.)

Index: Src/Modules/datetime.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/datetime.c,v
retrieving revision 1.6
diff -u -r1.6 datetime.c
--- Src/Modules/datetime.c	8 Oct 2003 13:37:13 -0000	1.6
+++ Src/Modules/datetime.c	22 Jan 2004 17:24:28 -0000
@@ -35,10 +35,18 @@
 bin_strftime(char *nam, char **argv, Options ops, int func)
 {
     int bufsize, x;
-    char *endptr = NULL, *buffer;
+    char *endptr = NULL, *scalar = NULL, *buffer;
     time_t secs;
     struct tm *t;
 
+    if (OPT_ISSET(ops,'s')) {
+	scalar = OPT_ARG(ops, 's');
+	if (!isident(scalar)) {
+	    zwarnnam(nam, "not an identifier: %s", scalar, 0);
+	    return 1;
+	}
+    }
+
     secs = (time_t)strtoul(argv[1], &endptr, 10);
     if (secs == ULONG_MAX) {
 	zwarnnam(nam, "%s: %e", argv[1], errno);
@@ -58,7 +66,11 @@
 	buffer = zrealloc(buffer, bufsize *= 2);
     }
 
-    printf("%s\n", buffer);
+    if (scalar) {
+	setsparam(scalar, ztrdup(buffer));
+    } else {
+	printf("%s\n", buffer);
+    }
     zfree(buffer, bufsize);
 
     return 0;
@@ -71,7 +83,7 @@
 }
 
 static struct builtin bintab[] = {
-    BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, NULL, NULL),
+    BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, "s:", NULL),
 };
 
 static struct paramdef patab[] = {
@@ -99,7 +111,14 @@
 int
 cleanup_(Module m)
 {
+    Param pm;
+
     deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
+    pm = (Param) paramtab->getnode(paramtab, "EPOCHSECONDS");
+    if (pm && (pm->flags & PM_SPECIAL)) {
+	pm->flags &= ~PM_READONLY;
+	unsetparam_pm(pm, 0, 1);
+    }
     return 0;
 }
 
Index: Doc/Zsh/mod_datetime.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_datetime.yo,v
retrieving revision 1.1
diff -u -r1.1 mod_datetime.yo
--- Doc/Zsh/mod_datetime.yo	25 Oct 2003 17:38:19 -0000	1.1
+++ Doc/Zsh/mod_datetime.yo	22 Jan 2004 17:24:29 -0000
@@ -6,9 +6,12 @@
 startitem()
 findex(strftime)
 cindex(date string, printing)
-item(tt(strftime) var(format) var(epochtime) )(
+item(tt(strftime) [ tt(-s) var(scalar) ] var(format) var(epochtime) )(
 Output the date denoted by var(epochtime) in the var(format)
 specified.
+
+If tt(-s) var(scalar) is given, assign the date to var(scalar) instead
+of printing it.
 )
 enditem()
 

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: PATCH: strftime -s
  2004-01-22 17:28 PATCH: strftime -s Peter Stephenson
@ 2004-01-22 17:52 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2004-01-22 17:52 UTC (permalink / raw)
  Cc: Zsh hackers list

Peter Stephenson wrote:
> It also fixes a bug that the EPOCHSECONDS wasn't unset when the module
> was unloaded, which could have nasty effects.  There ought to be
> something in the development guide about this.  (Ideally, there ought to
> be an easier way of removing module parameters --- it's a bit hairy at
> the moment.)

grrr... I'm telling me... EPOCHSECONDS didn't have an unsetfn, so still
crashed when the module was unloaded.  Should we guard against this?
It's odd that readonly variables need an unsetfn (though I think it's
reasonable that you need to explicitly remove the readonly flag when
unsetting).

Committed with that change.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-22 17:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-22 17:28 PATCH: strftime -s Peter Stephenson
2004-01-22 17:52 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).