Index: head/bin/kill/kill.c =================================================================== --- head/bin/kill/kill.c (revision 279502) +++ head/bin/kill/kill.c (revision 279503) @@ -1,210 +1,209 @@ /*- * Copyright (c) 1988, 1993, 1994 * 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. * 4. 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. */ /* * Important: This file is used both as a standalone program /bin/kill and * as a builtin for /bin/sh (#define SHELL). */ #if 0 #ifndef lint static char const copyright[] = "@(#) Copyright (c) 1988, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #ifdef SHELL #define main killcmd #include "bltin/bltin.h" -#include "error.h" #endif static void nosig(const char *); static void printsignals(FILE *); static int signame_to_signum(const char *); static void usage(void); int main(int argc, char *argv[]) { int errors, numsig, pid, ret; char *ep; if (argc < 2) usage(); numsig = SIGTERM; argc--, argv++; if (!strcmp(*argv, "-l")) { argc--, argv++; if (argc > 1) usage(); if (argc == 1) { if (!isdigit(**argv)) usage(); numsig = strtol(*argv, &ep, 10); if (!**argv || *ep) errx(2, "illegal signal number: %s", *argv); if (numsig >= 128) numsig -= 128; if (numsig <= 0 || numsig >= sys_nsig) nosig(*argv); printf("%s\n", sys_signame[numsig]); return (0); } printsignals(stdout); return (0); } if (!strcmp(*argv, "-s")) { argc--, argv++; if (argc < 1) { warnx("option requires an argument -- s"); usage(); } if (strcmp(*argv, "0")) { if ((numsig = signame_to_signum(*argv)) < 0) nosig(*argv); } else numsig = 0; argc--, argv++; } else if (**argv == '-' && *(*argv + 1) != '-') { ++*argv; if (isalpha(**argv)) { if ((numsig = signame_to_signum(*argv)) < 0) nosig(*argv); } else if (isdigit(**argv)) { numsig = strtol(*argv, &ep, 10); if (!**argv || *ep) errx(2, "illegal signal number: %s", *argv); if (numsig < 0) nosig(*argv); } else nosig(*argv); argc--, argv++; } if (argc > 0 && strncmp(*argv, "--", 2) == 0) argc--, argv++; if (argc == 0) usage(); for (errors = 0; argc; argc--, argv++) { #ifdef SHELL if (**argv == '%') ret = killjob(*argv, numsig); else #endif { pid = strtol(*argv, &ep, 10); if (!**argv || *ep) errx(2, "illegal process id: %s", *argv); ret = kill(pid, numsig); } if (ret == -1) { warn("%s", *argv); errors = 1; } } return (errors); } static int signame_to_signum(const char *sig) { int n; if (strncasecmp(sig, "SIG", 3) == 0) sig += 3; for (n = 1; n < sys_nsig; n++) { if (!strcasecmp(sys_signame[n], sig)) return (n); } return (-1); } static void nosig(const char *name) { warnx("unknown signal %s; valid signals:", name); printsignals(stderr); #ifdef SHELL error(NULL); #else exit(2); #endif } static void printsignals(FILE *fp) { int n; for (n = 1; n < sys_nsig; n++) { (void)fprintf(fp, "%s", sys_signame[n]); if (n == (sys_nsig / 2) || n == (sys_nsig - 1)) (void)fprintf(fp, "\n"); else (void)fprintf(fp, " "); } } static void usage(void) { (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", "usage: kill [-s signal_name] pid ...", " kill -l [exit_status]", " kill -signal_name pid ...", " kill -signal_number pid ..."); #ifdef SHELL error(NULL); #else exit(2); #endif } Index: head/bin/sh/arith_yacc.h =================================================================== --- head/bin/sh/arith_yacc.h (revision 279502) +++ head/bin/sh/arith_yacc.h (revision 279503) @@ -1,91 +1,93 @@ /*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * Copyright (c) 2007 * Herbert Xu . All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * 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. * * $FreeBSD$ */ #define ARITH_ASS 1 #define ARITH_OR 2 #define ARITH_AND 3 #define ARITH_BAD 4 #define ARITH_NUM 5 #define ARITH_VAR 6 #define ARITH_NOT 7 #define ARITH_BINOP_MIN 8 #define ARITH_LE 8 #define ARITH_GE 9 #define ARITH_LT 10 #define ARITH_GT 11 #define ARITH_EQ 12 #define ARITH_REM 13 #define ARITH_BAND 14 #define ARITH_LSHIFT 15 #define ARITH_RSHIFT 16 #define ARITH_MUL 17 #define ARITH_ADD 18 #define ARITH_BOR 19 #define ARITH_SUB 20 #define ARITH_BXOR 21 #define ARITH_DIV 22 #define ARITH_NE 23 #define ARITH_BINOP_MAX 24 #define ARITH_ASS_MIN 24 #define ARITH_REMASS 24 #define ARITH_BANDASS 25 #define ARITH_LSHIFTASS 26 #define ARITH_RSHIFTASS 27 #define ARITH_MULASS 28 #define ARITH_ADDASS 29 #define ARITH_BORASS 30 #define ARITH_SUBASS 31 #define ARITH_BXORASS 32 #define ARITH_DIVASS 33 #define ARITH_ASS_MAX 34 #define ARITH_LPAREN 34 #define ARITH_RPAREN 35 #define ARITH_BNOT 36 #define ARITH_QMARK 37 #define ARITH_COLON 38 +extern const char *arith_buf; + union yystype { arith_t val; char *name; }; extern union yystype yylval; int yylex(void); Index: head/bin/sh/arith_yylex.c =================================================================== --- head/bin/sh/arith_yylex.c (revision 279502) +++ head/bin/sh/arith_yylex.c (revision 279503) @@ -1,250 +1,248 @@ /*- * Copyright (c) 2002 * Herbert Xu. * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include "shell.h" #include "arith_yacc.h" #include "expand.h" #include "error.h" #include "memalloc.h" #include "parser.h" #include "syntax.h" #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ #error Arithmetic tokens are out of order. #endif -extern const char *arith_buf; - int yylex(void) { int value; const char *buf = arith_buf; char *end; const char *p; for (;;) { value = *buf; switch (value) { case ' ': case '\t': case '\n': buf++; continue; default: return ARITH_BAD; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': yylval.val = strtoarith_t(buf, &end, 0); arith_buf = end; return ARITH_NUM; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': p = buf; while (buf++, is_in_name(*buf)) ; yylval.name = stalloc(buf - p + 1); memcpy(yylval.name, p, buf - p); yylval.name[buf - p] = '\0'; value = ARITH_VAR; goto out; case '=': value += ARITH_ASS - '='; checkeq: buf++; checkeqcur: if (*buf != '=') goto out; value += 11; break; case '>': switch (*++buf) { case '=': value += ARITH_GE - '>'; break; case '>': value += ARITH_RSHIFT - '>'; goto checkeq; default: value += ARITH_GT - '>'; goto out; } break; case '<': switch (*++buf) { case '=': value += ARITH_LE - '<'; break; case '<': value += ARITH_LSHIFT - '<'; goto checkeq; default: value += ARITH_LT - '<'; goto out; } break; case '|': if (*++buf != '|') { value += ARITH_BOR - '|'; goto checkeqcur; } value += ARITH_OR - '|'; break; case '&': if (*++buf != '&') { value += ARITH_BAND - '&'; goto checkeqcur; } value += ARITH_AND - '&'; break; case '!': if (*++buf != '=') { value += ARITH_NOT - '!'; goto out; } value += ARITH_NE - '!'; break; case 0: goto out; case '(': value += ARITH_LPAREN - '('; break; case ')': value += ARITH_RPAREN - ')'; break; case '*': value += ARITH_MUL - '*'; goto checkeq; case '/': value += ARITH_DIV - '/'; goto checkeq; case '%': value += ARITH_REM - '%'; goto checkeq; case '+': if (buf[1] == '+') return ARITH_BAD; value += ARITH_ADD - '+'; goto checkeq; case '-': if (buf[1] == '-') return ARITH_BAD; value += ARITH_SUB - '-'; goto checkeq; case '~': value += ARITH_BNOT - '~'; break; case '^': value += ARITH_BXOR - '^'; goto checkeq; case '?': value += ARITH_QMARK - '?'; break; case ':': value += ARITH_COLON - ':'; break; } break; } buf++; out: arith_buf = buf; return value; } Index: head/bin/sh/bltin/bltin.h =================================================================== --- head/bin/sh/bltin/bltin.h (revision 279502) +++ head/bin/sh/bltin/bltin.h (revision 279503) @@ -1,79 +1,79 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * 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. * 4. 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. * * @(#)bltin.h 8.2 (Berkeley) 5/4/95 * $FreeBSD$ */ /* * This file is included by programs which are optionally built into the * shell. If SHELL is defined, we try to map the standard UNIX library * routines to ash routines using defines. */ #include "../shell.h" #include "../mystring.h" #ifdef SHELL +#include "../error.h" #include "../output.h" #include "builtins.h" #define FILE struct output #undef stdout #define stdout out1 #undef stderr #define stderr out2 #define printf out1fmt #undef putc #define putc(c, file) outc(c, file) #undef putchar #define putchar(c) out1c(c) #define fprintf outfmt #define fputs outstr #define fwrite(ptr, size, nmemb, file) outbin(ptr, (size) * (nmemb), file) #define fflush flushout #define INITARGS(argv) #define warnx warning #define warn(fmt, ...) warning(fmt ": %s", __VA_ARGS__, strerror(errno)) #define errx(exitstatus, ...) error(__VA_ARGS__) #else #undef NULL #include #undef main #define INITARGS(argv) if ((commandname = argv[0]) == NULL) {fputs("Argc is zero\n", stderr); exit(2);} else #endif #include pointer stalloc(int); -void error(const char *, ...) __printf0like(1, 2); int killjob(const char *, int); extern char *commandname; Index: head/bin/sh/mktokens =================================================================== --- head/bin/sh/mktokens (revision 279502) +++ head/bin/sh/mktokens (revision 279503) @@ -1,93 +1,93 @@ #!/bin/sh - #- # Copyright (c) 1991, 1993 # The Regents of the University of California. All rights reserved. # # This code is derived from software contributed to Berkeley by # Kenneth Almquist. # # 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. # 4. 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. # # @(#)mktokens 8.1 (Berkeley) 5/31/93 # $FreeBSD$ # The following is a list of tokens. The second column is nonzero if the # token marks the end of a list. The third column is the name to print in # error messages. temp=`/usr/bin/mktemp -t ka` cat > $temp <<\! TEOF 1 end of file TNL 0 newline TSEMI 0 ";" TBACKGND 0 "&" TAND 0 "&&" TOR 0 "||" TPIPE 0 "|" TLP 0 "(" TRP 1 ")" TENDCASE 1 ";;" TFALLTHRU 1 ";&" TREDIR 0 redirection TWORD 0 word TIF 0 "if" TTHEN 1 "then" TELSE 1 "else" TELIF 1 "elif" TFI 1 "fi" TWHILE 0 "while" TUNTIL 0 "until" TFOR 0 "for" TDO 1 "do" TDONE 1 "done" TBEGIN 0 "{" TEND 1 "}" TCASE 0 "case" TESAC 1 "esac" TNOT 0 "!" ! nl=`wc -l $temp` exec > token.h awk '{print "#define " $1 " " NR-1}' $temp echo ' /* Array indicating which tokens mark the end of a list */ -const char tokendlist[] = {' +static const char tokendlist[] = {' awk '{print "\t" $2 ","}' $temp echo '}; -const char *const tokname[] = {' +static const char *const tokname[] = {' sed -e 's/"/\\"/g' \ -e 's/[^ ]*[ ][ ]*[^ ]*[ ][ ]*\(.*\)/ "\1",/' \ $temp echo '}; ' sed 's/"//g' $temp | awk ' /TIF/{print "#define KWDOFFSET " NR-1; print ""; print "const char *const parsekwd[] = {"} /TIF/,/neverfound/{print " \"" $3 "\","}' echo ' 0 };' rm $temp Index: head/bin/sh/options.h =================================================================== --- head/bin/sh/options.h (revision 279502) +++ head/bin/sh/options.h (revision 279503) @@ -1,115 +1,114 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * 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. * 4. 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. * * @(#)options.h 8.2 (Berkeley) 5/4/95 * $FreeBSD$ */ struct shparam { int nparam; /* # of positional parameters (without $0) */ unsigned char malloc; /* if parameter list dynamically allocated */ unsigned char reset; /* if getopts has been reset */ char **p; /* parameter list */ char **optp; /* parameter list for getopts */ char **optnext; /* next parameter to be processed by getopts */ char *optptr; /* used by getopts */ }; #define eflag optlist[0].val #define fflag optlist[1].val #define Iflag optlist[2].val #define iflag optlist[3].val #define mflag optlist[4].val #define nflag optlist[5].val #define sflag optlist[6].val #define xflag optlist[7].val #define vflag optlist[8].val #define Vflag optlist[9].val #define Eflag optlist[10].val #define Cflag optlist[11].val #define aflag optlist[12].val #define bflag optlist[13].val #define uflag optlist[14].val #define privileged optlist[15].val #define Tflag optlist[16].val #define Pflag optlist[17].val #define hflag optlist[18].val #define NOPTS 19 struct optent { const char *name; const char letter; char val; }; +extern struct optent optlist[NOPTS]; #ifdef DEFINE_OPTIONS struct optent optlist[NOPTS] = { { "errexit", 'e', 0 }, { "noglob", 'f', 0 }, { "ignoreeof", 'I', 0 }, { "interactive",'i', 0 }, { "monitor", 'm', 0 }, { "noexec", 'n', 0 }, { "stdin", 's', 0 }, { "xtrace", 'x', 0 }, { "verbose", 'v', 0 }, { "vi", 'V', 0 }, { "emacs", 'E', 0 }, { "noclobber", 'C', 0 }, { "allexport", 'a', 0 }, { "notify", 'b', 0 }, { "nounset", 'u', 0 }, { "privileged", 'p', 0 }, { "trapsasync", 'T', 0 }, { "physical", 'P', 0 }, { "trackall", 'h', 0 }, }; -#else -extern struct optent optlist[NOPTS]; #endif extern char *minusc; /* argument to -c option */ extern char *arg0; /* $0 */ extern struct shparam shellparam; /* $@ */ extern char **argptr; /* argument list for builtin commands */ extern char *shoptarg; /* set by nextopt */ extern char *nextopt_optptr; /* used by nextopt */ void procargs(int, char **); void optschanged(void); void setparam(char **); void freeparam(struct shparam *); int nextopt(const char *); void getoptsreset(const char *); Index: head/usr.bin/printf/printf.c =================================================================== --- head/usr.bin/printf/printf.c (revision 279502) +++ head/usr.bin/printf/printf.c (revision 279503) @@ -1,687 +1,686 @@ /*- * Copyright 2014 Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 1989, 1993 * 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. * 4. 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. */ /* * Important: This file is used both as a standalone program /usr/bin/printf * and as a builtin for /bin/sh (#define SHELL). */ #ifndef SHELL #ifndef lint static char const copyright[] = "@(#) Copyright (c) 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #endif #ifndef lint #if 0 static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #ifdef SHELL #define main printfcmd #include "bltin/bltin.h" -#include "error.h" #include "options.h" #endif #define PF(f, func) do { \ char *b = NULL; \ if (havewidth) \ if (haveprec) \ (void)asprintf(&b, f, fieldwidth, precision, func); \ else \ (void)asprintf(&b, f, fieldwidth, func); \ else if (haveprec) \ (void)asprintf(&b, f, precision, func); \ else \ (void)asprintf(&b, f, func); \ if (b) { \ (void)fputs(b, stdout); \ free(b); \ } \ } while (0) static int asciicode(void); static char *printf_doformat(char *, int *); static int escape(char *, int, size_t *); static int getchr(void); static int getfloating(long double *, int); static int getint(int *); static int getnum(intmax_t *, uintmax_t *, int); static const char *getstr(void); static char *mknum(char *, char); static void usage(void); static const char digits[] = "0123456789"; static char end_fmt[1]; static int myargc; static char **myargv; static char **gargv; static char **maxargv; int main(int argc, char *argv[]) { size_t len; int end, rval; char *format, *fmt, *start; #ifndef SHELL int ch; (void) setlocale(LC_ALL, ""); #endif #ifdef SHELL nextopt(""); argc -= argptr - argv; argv = argptr; #else while ((ch = getopt(argc, argv, "")) != -1) switch (ch) { case '?': default: usage(); return (1); } argc -= optind; argv += optind; #endif if (argc < 1) { usage(); return (1); } #ifdef SHELL INTOFF; #endif /* * 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. */ fmt = format = *argv; escape(fmt, 1, &len); /* backslash interpretation */ rval = end = 0; gargv = ++argv; for (;;) { maxargv = gargv; myargv = gargv; for (myargc = 0; gargv[myargc]; myargc++) /* nop */; start = fmt; while (fmt < format + len) { if (fmt[0] == '%') { fwrite(start, 1, fmt - start, stdout); if (fmt[1] == '%') { /* %% prints a % */ putchar('%'); fmt += 2; } else { fmt = printf_doformat(fmt, &rval); if (fmt == NULL || fmt == end_fmt) { #ifdef SHELL INTON; #endif return (fmt == NULL ? 1 : rval); } end = 0; } start = fmt; } else fmt++; if (gargv > maxargv) maxargv = gargv; } gargv = maxargv; if (end == 1) { warnx("missing format character"); #ifdef SHELL INTON; #endif return (1); } fwrite(start, 1, fmt - start, stdout); if (!*gargv) { #ifdef SHELL INTON; #endif return (rval); } /* Restart at the beginning of the format string. */ fmt = format; end = 1; } /* NOTREACHED */ } static char * printf_doformat(char *fmt, int *rval) { static const char skip1[] = "#'-+ 0"; int fieldwidth, haveprec, havewidth, mod_ldbl, precision; char convch, nextch; char start[strlen(fmt) + 1]; char **fargv; char *dptr; int l; dptr = start; *dptr++ = '%'; *dptr = 0; fmt++; /* look for "n$" field index specifier */ l = strspn(fmt, digits); if ((l > 0) && (fmt[l] == '$')) { int idx = atoi(fmt); if (idx <= myargc) { gargv = &myargv[idx - 1]; } else { gargv = &myargv[myargc]; } if (gargv > maxargv) maxargv = gargv; fmt += l + 1; /* save format argument */ fargv = gargv; } else { fargv = NULL; } /* skip to field width */ while (*fmt && strchr(skip1, *fmt) != NULL) { *dptr++ = *fmt++; *dptr = 0; } if (*fmt == '*') { fmt++; l = strspn(fmt, digits); if ((l > 0) && (fmt[l] == '$')) { int idx = atoi(fmt); if (fargv == NULL) { warnx("incomplete use of n$"); return (NULL); } if (idx <= myargc) { gargv = &myargv[idx - 1]; } else { gargv = &myargv[myargc]; } fmt += l + 1; } else if (fargv != NULL) { warnx("incomplete use of n$"); return (NULL); } if (getint(&fieldwidth)) return (NULL); if (gargv > maxargv) maxargv = gargv; havewidth = 1; *dptr++ = '*'; *dptr = 0; } else { havewidth = 0; /* skip to possible '.', get following precision */ while (isdigit(*fmt)) { *dptr++ = *fmt++; *dptr = 0; } } if (*fmt == '.') { /* precision present? */ fmt++; *dptr++ = '.'; if (*fmt == '*') { fmt++; l = strspn(fmt, digits); if ((l > 0) && (fmt[l] == '$')) { int idx = atoi(fmt); if (fargv == NULL) { warnx("incomplete use of n$"); return (NULL); } if (idx <= myargc) { gargv = &myargv[idx - 1]; } else { gargv = &myargv[myargc]; } fmt += l + 1; } else if (fargv != NULL) { warnx("incomplete use of n$"); return (NULL); } if (getint(&precision)) return (NULL); if (gargv > maxargv) maxargv = gargv; haveprec = 1; *dptr++ = '*'; *dptr = 0; } else { haveprec = 0; /* skip to conversion char */ while (isdigit(*fmt)) { *dptr++ = *fmt++; *dptr = 0; } } } else haveprec = 0; if (!*fmt) { warnx("missing format character"); return (NULL); } *dptr++ = *fmt; *dptr = 0; /* * Look for a length modifier. POSIX doesn't have these, so * we only support them for floating-point conversions, which * are extensions. This is useful because the L modifier can * be used to gain extra range and precision, while omitting * it is more likely to produce consistent results on different * architectures. This is not so important for integers * because overflow is the only bad thing that can happen to * them, but consider the command printf %a 1.1 */ if (*fmt == 'L') { mod_ldbl = 1; fmt++; if (!strchr("aAeEfFgG", *fmt)) { warnx("bad modifier L for %%%c", *fmt); return (NULL); } } else { mod_ldbl = 0; } /* save the current arg offset, and set to the format arg */ if (fargv != NULL) { gargv = fargv; } convch = *fmt; nextch = *++fmt; *fmt = '\0'; switch (convch) { case 'b': { size_t len; char *p; int getout; p = strdup(getstr()); if (p == NULL) { warnx("%s", strerror(ENOMEM)); return (NULL); } getout = escape(p, 0, &len); fputs(p, stdout); free(p); if (getout) return (end_fmt); break; } case 'c': { char p; p = getchr(); PF(start, p); break; } case 's': { const char *p; p = getstr(); PF(start, p); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { char *f; intmax_t val; uintmax_t uval; int signedconv; signedconv = (convch == 'd' || convch == 'i'); if ((f = mknum(start, convch)) == NULL) return (NULL); if (getnum(&val, &uval, signedconv)) *rval = 1; if (signedconv) PF(f, val); else PF(f, uval); break; } case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': case 'a': case 'A': { long double p; if (getfloating(&p, mod_ldbl)) *rval = 1; if (mod_ldbl) PF(start, p); else PF(start, (double)p); break; } default: warnx("illegal format character %c", convch); return (NULL); } *fmt = nextch; /* return the gargv to the next element */ return (fmt); } static char * mknum(char *str, char ch) { static char *copy; static size_t copy_size; char *newcopy; size_t len, newlen; len = strlen(str) + 2; if (len > copy_size) { newlen = ((len + 1023) >> 10) << 10; if ((newcopy = realloc(copy, newlen)) == NULL) { warnx("%s", strerror(ENOMEM)); return (NULL); } copy = newcopy; copy_size = newlen; } memmove(copy, str, len - 3); copy[len - 3] = 'j'; copy[len - 2] = ch; copy[len - 1] = '\0'; return (copy); } static int escape(char *fmt, int percent, size_t *len) { char *save, *store, c; int value; for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) { if (c != '\\') { *store = c; continue; } switch (*++fmt) { case '\0': /* EOS, user error */ *store = '\\'; *++store = '\0'; *len = store - save; return (0); case '\\': /* backslash */ case '\'': /* single quote */ *store = *fmt; break; case 'a': /* bell/alert */ *store = '\a'; break; case 'b': /* backspace */ *store = '\b'; break; case 'c': if (!percent) { *store = '\0'; *len = store - save; return (1); } *store = 'c'; break; case 'f': /* form-feed */ *store = '\f'; break; case 'n': /* newline */ *store = '\n'; break; case 'r': /* carriage-return */ *store = '\r'; break; case 't': /* horizontal tab */ *store = '\t'; break; case 'v': /* vertical tab */ *store = '\v'; break; /* octal constant */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': c = (!percent && *fmt == '0') ? 4 : 3; for (value = 0; c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { value <<= 3; value += *fmt - '0'; } --fmt; if (percent && value == '%') { *store++ = '%'; *store = '%'; } else *store = (char)value; break; default: *store = *fmt; break; } } *store = '\0'; *len = store - save; return (0); } static int getchr(void) { if (!*gargv) return ('\0'); return ((int)**gargv++); } static const char * getstr(void) { if (!*gargv) return (""); return (*gargv++); } static int getint(int *ip) { intmax_t val; uintmax_t uval; int rval; if (getnum(&val, &uval, 1)) return (1); rval = 0; if (val < INT_MIN || val > INT_MAX) { warnx("%s: %s", *gargv, strerror(ERANGE)); rval = 1; } *ip = (int)val; return (rval); } static int getnum(intmax_t *ip, uintmax_t *uip, int signedconv) { char *ep; int rval; if (!*gargv) { *ip = *uip = 0; return (0); } if (**gargv == '"' || **gargv == '\'') { if (signedconv) *ip = asciicode(); else *uip = asciicode(); return (0); } rval = 0; errno = 0; if (signedconv) *ip = strtoimax(*gargv, &ep, 0); else *uip = strtoumax(*gargv, &ep, 0); if (ep == *gargv) { warnx("%s: expected numeric value", *gargv); rval = 1; } else if (*ep != '\0') { warnx("%s: not completely converted", *gargv); rval = 1; } if (errno == ERANGE) { warnx("%s: %s", *gargv, strerror(ERANGE)); rval = 1; } ++gargv; return (rval); } static int getfloating(long double *dp, int mod_ldbl) { char *ep; int rval; if (!*gargv) { *dp = 0.0; return (0); } if (**gargv == '"' || **gargv == '\'') { *dp = asciicode(); return (0); } rval = 0; errno = 0; if (mod_ldbl) *dp = strtold(*gargv, &ep); else *dp = strtod(*gargv, &ep); if (ep == *gargv) { warnx("%s: expected numeric value", *gargv); rval = 1; } else if (*ep != '\0') { warnx("%s: not completely converted", *gargv); rval = 1; } if (errno == ERANGE) { warnx("%s: %s", *gargv, strerror(ERANGE)); rval = 1; } ++gargv; return (rval); } static int asciicode(void) { int ch; wchar_t wch; mbstate_t mbs; ch = (unsigned char)**gargv; if (ch == '\'' || ch == '"') { memset(&mbs, 0, sizeof(mbs)); switch (mbrtowc(&wch, *gargv + 1, MB_LEN_MAX, &mbs)) { case (size_t)-2: case (size_t)-1: wch = (unsigned char)gargv[0][1]; break; case 0: wch = 0; break; } ch = wch; } ++gargv; return (ch); } static void usage(void) { (void)fprintf(stderr, "usage: printf format [arguments ...]\n"); }