zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <okiddle@yahoo.co.uk>
To: zsh-workers@sunsite.dk
Subject: PATCH: printf builtin
Date: Thu, 20 Sep 2001 20:10:35 +0100	[thread overview]
Message-ID: <3BAA3F2A.86D73181@yahoo.co.uk> (raw)

Ksh and bash both have a printf builtin. This adds one to zsh.

I'm inclined to think this should be in a loadable module - it isn't
that useful interactively but then it isn't particularly large either.
Any views on that? If it should be a module, should it be compiled in
by default for a static zsh?

There seems to be a bit of inconsistency over what the \c escape means.
In zsh it cancels a trailing newline but in most other things, it
cancels all following output.

I've not used zstrtol because it doesn't set errno to ERANGE for
overflows. POSIX requires the user to be notified of such errors.
Someone more familiar with the zsh source and with C should maybe have
a quick look through: I'm not sure whether there shouldn't be a free()
or two in there and if so which one. I intend to do a bit more testing
and tidying on it tomorrow anyway. We don't seem to have any tests of
builtins so where in the tests should I stick tests for this?

I have included the bash and ksh %b and %q extensions to the format
which are like %s but interpret posix escapes and shell quote the
argument respectively. Note that you can't specify a width or precision
with %b because I wanted to allow \0 to be used to output a null
character and printf(3) would stop at the null.

I've not included ksh's %H (HTML quoting) or %R/%P (regex/glob
conversion) because I don't think they have any place being there. ksh
also has %(...)T which is like date(1). And, I've not worked out what
ksh's %Z does.

Let me know if you have any other ideas for extensions or any thoughts
on whether I should add the following:

    Support for %n would be easily doable taking the argument as the
    name of a parameter.

    Allow the argument to be specified by writing `%m$' in place of '%'
    so '%3$s' will print the third argument. IRIX's printf(1) does
    this. It would involve disabling the feature of reusing the format
    for remaining arguments to avoid infinite loops.

Oliver

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.53
diff -u -r1.53 builtin.c
--- Src/builtin.c	2001/09/18 17:50:26	1.53
+++ Src/builtin.c	2001/09/20 18:49:33
@@ -92,6 +92,7 @@
 
     BUILTIN("popd", 0, bin_cd, 0, 2, BIN_POPD, NULL, NULL),
     BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "RDPbnrslzNu0123456789pioOcm-", NULL),
+    BUILTIN("printf", 0, bin_printf, 1, -1, 0, NULL, NULL),
     BUILTIN("pushd", 0, bin_cd, 0, 2, BIN_PUSHD, NULL, NULL),
     BUILTIN("pushln", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
     BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
@@ -3051,6 +3052,176 @@
     if (fout != stdout)
 	fclose(fout);
     return 0;
+}
+
+/* printf */
+
+/**/
+int
+bin_printf(char *name, char **args, char *ops, int func)
+{
+    int len, nnl, width, prec, type, ret = 0;
+    char *start, *endptr, *c, *fmt = getkeystring(*args, &len, 0, &nnl);
+    char **first = ++args;
+    char save;
+    union {
+	zlong l;
+	double d;
+	unsigned long u;
+	char *s;
+	int c;
+    } val;
+    
+    do {
+
+        for (c = fmt;c-fmt < len;c++) {
+	    type = prec = width = -1;
+
+	    if (*c != '%') {
+        	putchar(*c);
+		continue;
+	    }
+
+	    start = c++;
+	    if (*c == '%') {
+		putchar('%');
+		continue;
+	    }
+	    
+	    if (strchr("+- #", *c)) c++;
+	    
+	    if (*c == '*') {
+		width = (*args) ? strtoul(*args++, NULL, 0) : 0;
+		c++;
+	    } else {
+		while (idigit(*c)) c++;
+	    }
+	    
+	    if (*c == '.') {
+		c++;
+		if (*c == '*') {
+		    prec = (*args) ? strtoul(*args++, NULL, 0) : 0;
+		    c++;
+	    	} else {
+		    while (idigit(*c)) c++;
+		}
+	    }
+	    
+	    if (*c == 'l' || *c == 'L' || *c == 'h') c++;
+    	    
+	    switch (*c) {
+	    case 'c':
+		if (*args) {
+		    if (**args == Meta)
+		        val.c = (*args)[1] ^ 32;
+		    else
+		        val.c = **args;
+		    args++;
+		} else
+		    val.c = 0;
+		break;
+	    case 's':
+		if (*args)
+		    val.s = unmetafy(*args++, NULL);
+		else
+		    continue;
+		break;
+	    case 'b':
+		if (*args) {
+		    int l;
+		    char *b = getkeystring(*args++, &l, 0, &nnl);
+		    fwrite(b, l, 1, stdout);
+		}
+		continue;
+	    case 'q':
+		if (*args) {
+		    *c = 's';
+		    val.s = bslashquote(unmetafy(*args++, NULL), NULL, 0);
+		} else
+		    continue;
+		break;
+    	    case 'd':
+	    case 'i':
+	    	type=1;
+		break;
+    	    case 'e':
+	    case 'E':
+	    case 'f':
+	    case 'g':
+	    case 'G':
+	    	type=2;
+		break;
+	    case 'o':
+	    case 'u':
+	    case 'x':
+	    case 'X':
+		type=3;
+		break;
+	    default:
+		if (*c) {
+		    save = c[1];
+		    c[1] = '\0';
+		}
+	    	zerrnam(name, "%s: invalid directive", start, 0);
+	    	if (*c) c[1] = save;
+	    	ret = 1;
+		continue;
+	    }
+	    
+	    if (type > 0) {
+		if (*args && (**args == '\'' || **args == '"' )) {
+		    if (type == 2)
+		    	val.d = (*args)[1];
+		    else
+		    	val.l = (*args)[1];
+		    args++;
+		} else {
+	    	    switch (type) {
+		    case 1:
+			val.l = (*args) ? strtol(*args, &endptr, 0) : 0;
+			break;
+    	    	    case 2:
+			val.d = (*args) ? strtod(*args, &endptr) : 0;
+			break;
+		    case 3:
+			val.u = (*args) ? strtoul(*args, &endptr, 0) : 0;
+		    }
+		    if (*args) {
+			if (errno == ERANGE) {
+			    zerrnam(name, "`%s' arithmetic overflow", *args, 0);
+			    ret = 1;
+			} else if (**args && endptr == *args) {
+			    zerrnam(name, "`%s' expected numeric value", endptr, 0);
+			    ret = 1;
+			} else if (*endptr) {
+			    zerrnam(name, "`%s' not completely converted", *args, 0);
+			    ret = 1;
+			}
+			args++;
+		    }
+		}
+	    }
+	    save = c[1];
+	    c[1] = '\0';
+	    if (width >= 0) {
+		if (prec >= 0)
+		    printf(start, width, prec, val);
+		else
+		    printf(start, width, val);
+	    } else {
+		if (prec >= 0)
+		    printf(start, prec, val);
+		else
+		    printf(start, val);
+	    }
+	    c[1] = save;
+	
+	}
+
+    /* if there are remaining args, reuse format string */
+    } while (*args && args != first);
+
+    return ret;
 }
 
 /* shift builtin */

_____________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp


             reply	other threads:[~2001-09-20 19:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-20 19:10 Oliver Kiddle [this message]
2001-09-20 20:33 ` Bart Schaefer
2001-09-21 10:58   ` Oliver Kiddle
2001-09-21 14:42     ` Bart Schaefer
2001-09-21 14:55 ` Peter Stephenson
2001-09-21 16:13   ` Oliver Kiddle
2001-09-21 17:10     ` Peter Stephenson
2001-09-24  4:42     ` print -z (Re: PATCH: printf builtin) Bart Schaefer
2001-09-24  9:18       ` Peter Stephenson
2001-09-24 16:41         ` PATCH (docs): " Bart Schaefer
2001-09-24 10:50   ` PATCH: printf builtin Peter Stephenson
2001-09-25 17:17     ` Oliver Kiddle
2001-09-25 17:57       ` Peter Stephenson
2001-10-05 14:54     ` Oliver Kiddle
2001-10-05 17:03       ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3BAA3F2A.86D73181@yahoo.co.uk \
    --to=okiddle@yahoo.co.uk \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).