pkgsrc-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[pkgsrc/trunk]: pkgsrc/shells/mksh mksh: Pull in the printf(1) builtin from M...
details: https://anonhg.NetBSD.org/pkgsrc/rev/09bf5eaf9b48
branches: trunk
changeset: 390445:09bf5eaf9b48
user: jperkin <jperkin%pkgsrc.org@localhost>
date: Mon Dec 19 13:42:34 2022 +0000
description:
mksh: Pull in the printf(1) builtin from MirBSD.
Note that we are specifically using -r1.21 as newer versions do not build.
This improves performance on systems that use mksh as the bootstrap shell, due
to widespread use of printf in the pkgsrc infrastructure that previously had
to fork /usr/bin/printf.
Tested on macOS and SmartOS. Bump PKGREVISION.
diffstat:
shells/mksh/Makefile | 6 +-
shells/mksh/files/printf.c | 650 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 654 insertions(+), 2 deletions(-)
diffs (truncated from 677 to 300 lines):
diff -r 434dbb311c7f -r 09bf5eaf9b48 shells/mksh/Makefile
--- a/shells/mksh/Makefile Mon Dec 19 13:32:27 2022 +0000
+++ b/shells/mksh/Makefile Mon Dec 19 13:42:34 2022 +0000
@@ -1,8 +1,8 @@
-# $NetBSD: Makefile,v 1.44 2022/06/28 11:35:52 wiz Exp $
+# $NetBSD: Makefile,v 1.45 2022/12/19 13:42:34 jperkin Exp $
DISTNAME= mksh-R59b
PKGNAME= ${DISTNAME:S/-R/-/}
-PKGREVISION= 2
+PKGREVISION= 3
CATEGORIES= shells
MASTER_SITES= # maintained locally
DISTFILES= # empty
@@ -19,6 +19,8 @@
LIBS.Interix+= -lcrypt
+MAKE_ENV+= USE_PRINTF_BUILTIN=1
+
INSTALLATION_DIRS= bin ${PKGMANDIR}/man1 share/examples/mksh
.include "../../mk/bsd.prefs.mk"
diff -r 434dbb311c7f -r 09bf5eaf9b48 shells/mksh/files/printf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/shells/mksh/files/printf.c Mon Dec 19 13:42:34 2022 +0000
@@ -0,0 +1,650 @@
+/* $OpenBSD: printf.c,v 1.17 2009/10/27 23:59:41 deraadt Exp $ */
+
+/*-
+ * Copyright (c) 1989 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifdef MKSH_PRINTF_BUILTIN
+/* MirBSD Korn Shell */
+#include "sh.h"
+#else
+/* stand-alone executable */
+#include <sys/types.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define vstrchr strchr
+#endif
+
+__RCSID("$MirOS: src/usr.bin/printf/printf.c,v 1.21 2020/01/22 01:57:44 tg Exp $");
+
+static int print_escape_str(const char *);
+static int print_escape(const char *);
+
+static int getchr(void);
+#ifndef MKSH_PRINTF_BUILTIN
+static double getdouble(void);
+#endif
+static int getinteger(void);
+static long getlong(void);
+static unsigned long getulong(void);
+static const char *getstr(void);
+static char *mklong(const char *, int);
+static void check_conversion(const char *, const char *);
+static int usage(void);
+static int real_main(char *, const char *[]);
+
+static int rval;
+static const char **gargv;
+
+#ifdef MKSH_PRINTF_BUILTIN
+static int s_get(void);
+static void s_put(int);
+static void s_prt(int);
+
+static const char *s_ptr;
+
+#define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
+#endif
+
+#define isodigit(c) ((c) >= '0' && (c) <= '7')
+#define octtobin(c) ((c) - '0')
+#define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
+
+#define PF(f, func) do { \
+ if (fieldwidth) \
+ if (precision) \
+ uprintf(f, fieldwidth, precision, func); \
+ else \
+ uprintf(f, fieldwidth, func); \
+ else if (precision) \
+ uprintf(f, precision, func); \
+ else \
+ uprintf(f, func); \
+} while (/* CONSTCOND */ 0)
+
+#ifndef vstrchr
+#define vstrchr strchr
+#endif
+
+#ifndef cstrerror
+#define cstrerror(e) ((const char *)strerror(e))
+#endif
+
+#ifdef MKSH_PRINTF_BUILTIN
+#define ufprintf shf_fprintf
+#define uprintf shprintf
+#define uputc(c) shf_putchar((c), shl_stdout)
+#define ustderr shl_out
+#define uwarnx warningf
+#define UWARNX false,
+
+int
+c_printf(const char **wp)
+{
+ int rv;
+ const char *old_kshname;
+ char *fmt;
+ uint8_t old_utfmode;
+
+ old_kshname = kshname;
+ kshname = wp[0];
+ ++wp;
+ if (wp[0] && !strcmp(wp[0], "--"))
+ ++wp;
+ if (wp[0]) {
+ strdupx(fmt, wp[0], ATEMP);
+ old_utfmode = UTFMODE;
+ UTFMODE = 0;
+ rv = real_main(fmt, wp);
+ UTFMODE = old_utfmode;
+ afree(fmt, ATEMP);
+ } else
+ rv = usage();
+ kshname = old_kshname;
+ return (rv);
+}
+#else
+#define ufprintf fprintf
+#define uprintf printf
+#define uputc putchar
+#define ustderr stderr
+#define uwarnx warnx
+#define UWARNX /* nothing */
+
+int
+main(int argc, char *argv[])
+{
+ setlocale(LC_ALL, "");
+
+ if (argc > 1 && !strcmp(argv[1], "--")) {
+ --argc;
+ ++argv;
+ }
+
+ ++argv;
+ return (argc < 2 ? usage() : real_main(argv[0], (const char **)argv));
+}
+#endif
+
+static int
+real_main(char *format, const char *argv[])
+{
+ char *fmt, *start;
+ int fieldwidth, precision;
+ char convch, nextch;
+
+ gargv = ++argv;
+
+#define SKIP1 "#-+ 0"
+#define SKIP2 "0123456789"
+ do {
+ /*
+ * Basic algorithm is to scan the format string for conversion
+ * specifications -- once one is found, find out if the field
+ * width or precision is a '*'; if it is, gather up value.
+ * Note, format strings are reused as necessary to use up the
+ * provided arguments, arguments of zero/null string are
+ * provided to use up the format string.
+ */
+
+ /* find next format specification */
+ for (fmt = format; *fmt; fmt++) {
+ switch (*fmt) {
+ case '%':
+ start = fmt++;
+
+ if (*fmt == '%') {
+ uputc('%');
+ break;
+ } else if (*fmt == 'b') {
+ const char *p = getstr();
+ if (print_escape_str(p)) {
+ return (rval);
+ }
+ break;
+ } else if (!*fmt) {
+ goto synerr;
+ }
+
+ /* skip to field width */
+ for (; vstrchr(SKIP1, *fmt); ++fmt)
+ ;
+ if (*fmt == '*') {
+ ++fmt;
+ fieldwidth = getinteger();
+ } else if (!*fmt) {
+ goto synerr;
+ } else
+ fieldwidth = 0;
+
+ /* skip to field precision */
+ for (; vstrchr(SKIP2, *fmt); ++fmt)
+ ;
+ precision = 0;
+ if (*fmt == '.') {
+ ++fmt;
+ if (*fmt == '*') {
+ ++fmt;
+ precision = getinteger();
+ }
+ if (!*fmt)
+ goto synerr;
+ for (; vstrchr(SKIP2, *fmt); ++fmt)
+ ;
+ }
+
+ if (!*fmt) {
+ synerr:
+ uwarnx(UWARNX "missing format character");
+ return (1);
+ }
+
+ convch = *fmt;
+ nextch = *(fmt + 1);
+ *(fmt + 1) = '\0';
+ switch(convch) {
+ case 'c': {
+ char p = getchr();
+ PF(start, p);
+ break;
+ }
+ case 's': {
+ const char *p = getstr();
+ PF(start, p);
+ break;
+ }
+ case 'd':
+ case 'i': {
+ long p;
+ char *f = mklong(start, convch);
+ if (!f) {
+ uwarnx(UWARNX "out of memory");
+ return (1);
+ }
+ p = getlong();
+ PF(f, p);
+ break;
+ }
+ case 'o':
+ case 'u':
+ case 'x':
+ case 'X': {
+ unsigned long p;
+ char *f = mklong(start, convch);
+ if (!f) {
+ uwarnx(UWARNX "out of memory");
+ return (1);
+ }
+ p = getulong();
+ PF(f, p);
+ break;
+ }
Home |
Main Index |
Thread Index |
Old Index