diff --git a/bin/dd/args.c b/bin/dd/args.c index 9d2aa6a35341..7e66b7fd96aa 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -1,472 +1,472 @@ /*- * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego and Lance * Visser of Convex Computer Corporation. * * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 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. */ #ifndef lint #if 0 static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "dd.h" #include "extern.h" static int c_arg(const void *, const void *); static int c_conv(const void *, const void *); static void f_bs(char *); static void f_cbs(char *); static void f_conv(char *); static void f_count(char *); static void f_files(char *); static void f_ibs(char *); static void f_if(char *); static void f_obs(char *); static void f_of(char *); static void f_seek(char *); static void f_skip(char *); static uintmax_t get_num(const char *); static off_t get_off_t(const char *); static const struct arg { const char *name; void (*f)(char *); u_int set, noset; } args[] = { { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC }, { "cbs", f_cbs, C_CBS, C_CBS }, { "conv", f_conv, 0, 0 }, { "count", f_count, C_COUNT, C_COUNT }, { "files", f_files, C_FILES, C_FILES }, { "ibs", f_ibs, C_IBS, C_BS|C_IBS }, { "if", f_if, C_IF, C_IF }, { "iseek", f_skip, C_SKIP, C_SKIP }, { "obs", f_obs, C_OBS, C_BS|C_OBS }, { "of", f_of, C_OF, C_OF }, { "oseek", f_seek, C_SEEK, C_SEEK }, { "seek", f_seek, C_SEEK, C_SEEK }, { "skip", f_skip, C_SKIP, C_SKIP }, }; static char *oper; /* * args -- parse JCL syntax of dd. */ void jcl(char **argv) { struct arg *ap, tmp; char *arg; in.dbsz = out.dbsz = 512; while ((oper = *++argv) != NULL) { if ((oper = strdup(oper)) == NULL) errx(1, "unable to allocate space for the argument \"%s\"", *argv); if ((arg = strchr(oper, '=')) == NULL) errx(1, "unknown operand %s", oper); *arg++ = '\0'; if (!*arg) errx(1, "no value specified for %s", oper); tmp.name = oper; if (!(ap = (struct arg *)bsearch(&tmp, args, sizeof(args)/sizeof(struct arg), sizeof(struct arg), c_arg))) errx(1, "unknown operand %s", tmp.name); if (ddflags & ap->noset) errx(1, "%s: illegal argument combination or already set", tmp.name); ddflags |= ap->set; ap->f(arg); } /* Final sanity checks. */ if (ddflags & C_BS) { /* * Bs is turned off by any conversion -- we assume the user * just wanted to set both the input and output block sizes * and didn't want the bs semantics, so we don't warn. */ if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE | C_UNBLOCK)) ddflags &= ~C_BS; /* Bs supersedes ibs and obs. */ if (ddflags & C_BS && ddflags & (C_IBS | C_OBS)) warnx("bs supersedes ibs and obs"); } /* * Ascii/ebcdic and cbs implies block/unblock. * Block/unblock requires cbs and vice-versa. */ if (ddflags & (C_BLOCK | C_UNBLOCK)) { if (!(ddflags & C_CBS)) errx(1, "record operations require cbs"); if (cbsz == 0) errx(1, "cbs cannot be zero"); cfunc = ddflags & C_BLOCK ? block : unblock; } else if (ddflags & C_CBS) { if (ddflags & (C_ASCII | C_EBCDIC)) { if (ddflags & C_ASCII) { ddflags |= C_UNBLOCK; cfunc = unblock; } else { ddflags |= C_BLOCK; cfunc = block; } } else errx(1, "cbs meaningless if not doing record operations"); } else cfunc = def; /* * Bail out if the calculation of a file offset would overflow. */ if (in.offset > OFF_MAX / (ssize_t)in.dbsz || out.offset > OFF_MAX / (ssize_t)out.dbsz) errx(1, "seek offsets cannot be larger than %jd", (intmax_t)OFF_MAX); } static int c_arg(const void *a, const void *b) { return (strcmp(((const struct arg *)a)->name, ((const struct arg *)b)->name)); } static void f_bs(char *arg) { uintmax_t res; res = get_num(arg); if (res < 1 || res > SSIZE_MAX) - errx(1, "bs must be between 1 and %d", SSIZE_MAX); + errx(1, "bs must be between 1 and %zd", SSIZE_MAX); in.dbsz = out.dbsz = (size_t)res; } static void f_cbs(char *arg) { uintmax_t res; res = get_num(arg); if (res < 1 || res > SSIZE_MAX) - errx(1, "cbs must be between 1 and %d", SSIZE_MAX); + errx(1, "cbs must be between 1 and %zd", SSIZE_MAX); cbsz = (size_t)res; } static void f_count(char *arg) { intmax_t res; res = (intmax_t)get_num(arg); if (res < 0) errx(1, "count cannot be negative"); if (res == 0) cpy_cnt = (uintmax_t)-1; else cpy_cnt = (uintmax_t)res; } static void f_files(char *arg) { files_cnt = get_num(arg); if (files_cnt < 1) errx(1, "files must be between 1 and %jd", (uintmax_t)-1); } static void f_ibs(char *arg) { uintmax_t res; if (!(ddflags & C_BS)) { res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "ibs must be between 1 and %zd", SSIZE_MAX); in.dbsz = (size_t)res; } } static void f_if(char *arg) { in.name = arg; } static void f_obs(char *arg) { uintmax_t res; if (!(ddflags & C_BS)) { res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "obs must be between 1 and %zd", SSIZE_MAX); out.dbsz = (size_t)res; } } static void f_of(char *arg) { out.name = arg; } static void f_seek(char *arg) { out.offset = get_off_t(arg); } static void f_skip(char *arg) { in.offset = get_off_t(arg); } static const struct conv { const char *name; u_int set, noset; const u_char *ctab; } clist[] = { { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX }, { "block", C_BLOCK, C_UNBLOCK, NULL }, { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX }, { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX }, { "lcase", C_LCASE, C_UCASE, NULL }, { "noerror", C_NOERROR, 0, NULL }, { "notrunc", C_NOTRUNC, 0, NULL }, { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, { "osync", C_OSYNC, C_BS, NULL }, { "sparse", C_SPARSE, 0, NULL }, { "swab", C_SWAB, 0, NULL }, { "sync", C_SYNC, 0, NULL }, { "ucase", C_UCASE, C_LCASE, NULL }, { "unblock", C_UNBLOCK, C_BLOCK, NULL }, }; static void f_conv(char *arg) { struct conv *cp, tmp; while (arg != NULL) { tmp.name = strsep(&arg, ","); cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv), sizeof(struct conv), c_conv); if (cp == NULL) errx(1, "unknown conversion %s", tmp.name); if (ddflags & cp->noset) errx(1, "%s: illegal conversion combination", tmp.name); ddflags |= cp->set; if (cp->ctab) ctab = cp->ctab; } } static int c_conv(const void *a, const void *b) { return (strcmp(((const struct conv *)a)->name, ((const struct conv *)b)->name)); } /* * Convert an expression of the following forms to a uintmax_t. * 1) A positive decimal number. * 2) A positive decimal number followed by a b (mult by 512). * 3) A positive decimal number followed by a k (mult by 1 << 10). * 4) A positive decimal number followed by a m (mult by 1 << 20). * 5) A positive decimal number followed by a g (mult by 1 << 30). * 5) A positive decimal number followed by a w (mult by sizeof int). * 6) Two or more positive decimal numbers (with/without [bkmgw]) * separated by x (also * for backwards compatibility), specifying * the product of the indicated values. */ static uintmax_t get_num(const char *val) { uintmax_t num, mult, prevnum; char *expr; errno = 0; num = strtouq(val, &expr, 0); if (errno != 0) /* Overflow or underflow. */ err(1, "%s", oper); if (expr == val) /* No valid digits. */ errx(1, "%s: illegal numeric value", oper); mult = 0; switch (*expr) { case 'b': mult = 512; break; case 'k': mult = 1 << 10; break; case 'm': mult = 1 << 20; break; case 'g': mult = 1 << 30; break; case 'w': mult = sizeof(int); break; default: ; } if (mult != 0) { prevnum = num; num *= mult; /* Check for overflow. */ if (num / mult != prevnum) goto erange; expr++; } switch (*expr) { case '\0': break; case '*': /* Backward compatible. */ case 'x': mult = get_num(expr + 1); prevnum = num; num *= mult; if (num / mult == prevnum) break; erange: errx(1, "%s: %s", oper, strerror(ERANGE)); default: errx(1, "%s: illegal numeric value", oper); } return (num); } /* * Convert an expression of the following forms to an off_t. This is the * same as get_num(), but it uses signed numbers. * * The major problem here is that an off_t may not necessarily be a intmax_t. */ static off_t get_off_t(const char *val) { intmax_t num, mult, prevnum; char *expr; errno = 0; num = strtoq(val, &expr, 0); if (errno != 0) /* Overflow or underflow. */ err(1, "%s", oper); if (expr == val) /* No valid digits. */ errx(1, "%s: illegal numeric value", oper); mult = 0; switch (*expr) { case 'b': mult = 512; break; case 'k': mult = 1 << 10; break; case 'm': mult = 1 << 20; break; case 'g': mult = 1 << 30; break; case 'w': mult = sizeof(int); break; } if (mult != 0) { prevnum = num; num *= mult; /* Check for overflow. */ if ((prevnum > 0) != (num > 0) || num / mult != prevnum) goto erange; expr++; } switch (*expr) { case '\0': break; case '*': /* Backward compatible. */ case 'x': mult = (intmax_t)get_off_t(expr + 1); prevnum = num; num *= mult; if ((prevnum > 0) == (num > 0) && num / mult == prevnum) break; erange: errx(1, "%s: %s", oper, strerror(ERANGE)); default: errx(1, "%s: illegal numeric value", oper); } return (num); } diff --git a/bin/sh/expand.c b/bin/sh/expand.c index c4ee6f83604d..e3748ab0095d 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -1,1545 +1,1546 @@ /*- * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 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. */ #ifndef lint #if 0 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include /* * Routines to expand arguments to commands. We have to deal with * backquotes, shell variables, and file metacharacters. */ #include "shell.h" #include "main.h" #include "nodes.h" #include "eval.h" #include "expand.h" #include "syntax.h" #include "parser.h" #include "jobs.h" #include "options.h" #include "var.h" #include "input.h" #include "output.h" #include "memalloc.h" #include "error.h" #include "mystring.h" #include "arith.h" #include "show.h" /* * Structure specifying which parts of the string should be searched * for IFS characters. */ struct ifsregion { struct ifsregion *next; /* next region in list */ int begoff; /* offset of start of region */ int endoff; /* offset of end of region */ int nulonly; /* search for nul bytes only */ }; char *expdest; /* output of current string */ struct nodelist *argbackq; /* list of back quote expressions */ struct ifsregion ifsfirst; /* first struct in list of ifs regions */ struct ifsregion *ifslastp; /* last struct in list */ struct arglist exparg; /* holds expanded arg list */ STATIC void argstr(char *, int); STATIC char *exptilde(char *, int); STATIC void expbackq(union node *, int, int); STATIC int subevalvar(char *, char *, int, int, int, int); STATIC char *evalvar(char *, int); STATIC int varisset(char *, int); STATIC void varvalue(char *, int, int); STATIC void recordregion(int, int, int); STATIC void removerecordregions(int); STATIC void ifsbreakup(char *, struct arglist *); STATIC void expandmeta(struct strlist *, int); STATIC void expmeta(char *, char *); STATIC void addfname(char *); STATIC struct strlist *expsort(struct strlist *); STATIC struct strlist *msort(struct strlist *, int); STATIC int pmatch(char *, char *, int); STATIC char *cvtnum(int, char *); STATIC int collate_range_cmp(int, int); STATIC int collate_range_cmp (int c1, int c2) { static char s1[2], s2[2]; int ret; c1 &= UCHAR_MAX; c2 &= UCHAR_MAX; if (c1 == c2) return (0); s1[0] = c1; s2[0] = c2; if ((ret = strcoll(s1, s2)) != 0) return (ret); return (c1 - c2); } /* * Expand shell variables and backquotes inside a here document. * union node *arg the document * int fd; where to write the expanded version */ void expandhere(union node *arg, int fd) { herefd = fd; expandarg(arg, (struct arglist *)NULL, 0); xwrite(fd, stackblock(), expdest - stackblock()); } /* * Perform variable substitution and command substitution on an argument, * placing the resulting list of arguments in arglist. If EXP_FULL is true, * perform splitting and file name expansion. When arglist is NULL, perform * here document expansion. */ void expandarg(union node *arg, struct arglist *arglist, int flag) { struct strlist *sp; char *p; argbackq = arg->narg.backquote; STARTSTACKSTR(expdest); ifsfirst.next = NULL; ifslastp = NULL; argstr(arg->narg.text, flag); if (arglist == NULL) { return; /* here document expanded */ } STPUTC('\0', expdest); p = grabstackstr(expdest); exparg.lastp = &exparg.list; /* * TODO - EXP_REDIR */ if (flag & EXP_FULL) { ifsbreakup(p, &exparg); *exparg.lastp = NULL; exparg.lastp = &exparg.list; expandmeta(exparg.list, flag); } else { if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ rmescapes(p); sp = (struct strlist *)stalloc(sizeof (struct strlist)); sp->text = p; *exparg.lastp = sp; exparg.lastp = &sp->next; } while (ifsfirst.next != NULL) { struct ifsregion *ifsp; INTOFF; ifsp = ifsfirst.next->next; ckfree(ifsfirst.next); ifsfirst.next = ifsp; INTON; } *exparg.lastp = NULL; if (exparg.list) { *arglist->lastp = exparg.list; arglist->lastp = exparg.lastp; } } /* * Perform variable and command substitution. If EXP_FULL is set, output CTLESC * characters to allow for further processing. Otherwise treat * $@ like $* since no splitting will be performed. */ STATIC void argstr(char *p, int flag) { char c; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ int firsteq = 1; if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) p = exptilde(p, flag); for (;;) { switch (c = *p++) { case '\0': case CTLENDVAR: /* ??? */ goto breakloop; case CTLQUOTEMARK: /* "$@" syntax adherence hack */ if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') break; if ((flag & EXP_FULL) != 0) STPUTC(c, expdest); break; case CTLESC: if (quotes) STPUTC(c, expdest); c = *p++; STPUTC(c, expdest); break; case CTLVAR: p = evalvar(p, flag); break; case CTLBACKQ: case CTLBACKQ|CTLQUOTE: expbackq(argbackq->n, c & CTLQUOTE, flag); argbackq = argbackq->next; break; case CTLENDARI: expari(flag); break; case ':': case '=': /* * sort of a hack - expand tildes in variable * assignments (after the first '=' and after ':'s). */ STPUTC(c, expdest); if (flag & EXP_VARTILDE && *p == '~') { if (c == '=') { if (firsteq) firsteq = 0; else break; } p = exptilde(p, flag); } break; default: STPUTC(c, expdest); } } breakloop:; } STATIC char * exptilde(char *p, int flag) { char c, *startp = p; struct passwd *pw; char *home; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); while ((c = *p) != '\0') { switch(c) { case CTLESC: return (startp); case CTLQUOTEMARK: return (startp); case ':': if (flag & EXP_VARTILDE) goto done; break; case '/': goto done; } p++; } done: *p = '\0'; if (*(startp+1) == '\0') { if ((home = lookupvar("HOME")) == NULL) goto lose; } else { if ((pw = getpwnam(startp+1)) == NULL) goto lose; home = pw->pw_dir; } if (*home == '\0') goto lose; *p = c; while ((c = *home++) != '\0') { if (quotes && SQSYNTAX[(int)c] == CCTL) STPUTC(CTLESC, expdest); STPUTC(c, expdest); } return (p); lose: *p = c; return (startp); } STATIC void removerecordregions(int endoff) { if (ifslastp == NULL) return; if (ifsfirst.endoff > endoff) { while (ifsfirst.next != NULL) { struct ifsregion *ifsp; INTOFF; ifsp = ifsfirst.next->next; ckfree(ifsfirst.next); ifsfirst.next = ifsp; INTON; } if (ifsfirst.begoff > endoff) ifslastp = NULL; else { ifslastp = &ifsfirst; ifsfirst.endoff = endoff; } return; } ifslastp = &ifsfirst; while (ifslastp->next && ifslastp->next->begoff < endoff) ifslastp=ifslastp->next; while (ifslastp->next != NULL) { struct ifsregion *ifsp; INTOFF; ifsp = ifslastp->next->next; ckfree(ifslastp->next); ifslastp->next = ifsp; INTON; } if (ifslastp->endoff > endoff) ifslastp->endoff = endoff; } /* * Expand arithmetic expression. Backup to start of expression, * evaluate, place result in (backed up) result, adjust string position. */ void expari(int flag) { char *p, *start; int result; int begoff; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); int quoted; /* * This routine is slightly over-complicated for * efficiency. First we make sure there is * enough space for the result, which may be bigger * than the expression if we add exponentiation. Next we * scan backwards looking for the start of arithmetic. If the * next previous character is a CTLESC character, then we * have to rescan starting from the beginning since CTLESC * characters have to be processed left to right. */ #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10 #error "integers with more than 10 digits are not supported" #endif CHECKSTRSPACE(12 - 2, expdest); USTPUTC('\0', expdest); start = stackblock(); p = expdest - 2; while (p >= start && *p != CTLARI) --p; if (p < start || *p != CTLARI) error("missing CTLARI (shouldn't happen)"); if (p > start && *(p - 1) == CTLESC) for (p = start; *p != CTLARI; p++) if (*p == CTLESC) p++; if (p[1] == '"') quoted=1; else quoted=0; begoff = p - start; removerecordregions(begoff); if (quotes) rmescapes(p+2); result = arith(p+2); fmtstr(p, 12, "%d", result); while (*p++) ; if (quoted == 0) recordregion(begoff, p - 1 - start, 0); result = expdest - p + 1; STADJUST(-result, expdest); } /* * Expand stuff in backwards quotes. */ STATIC void expbackq(union node *cmd, int quoted, int flag) { struct backcmd in; int i; char buf[128]; char *p; char *dest = expdest; struct ifsregion saveifs, *savelastp; struct nodelist *saveargbackq; char lastc; int startloc = dest - stackblock(); char const *syntax = quoted? DQSYNTAX : BASESYNTAX; int saveherefd; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); INTOFF; saveifs = ifsfirst; savelastp = ifslastp; saveargbackq = argbackq; saveherefd = herefd; herefd = -1; p = grabstackstr(dest); evalbackcmd(cmd, &in); ungrabstackstr(p, dest); ifsfirst = saveifs; ifslastp = savelastp; argbackq = saveargbackq; herefd = saveherefd; p = in.buf; lastc = '\0'; for (;;) { if (--in.nleft < 0) { if (in.fd < 0) break; while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR); TRACE(("expbackq: read returns %d\n", i)); if (i <= 0) break; p = buf; in.nleft = i - 1; } lastc = *p++; if (lastc != '\0') { if (quotes && syntax[(int)lastc] == CCTL) STPUTC(CTLESC, dest); STPUTC(lastc, dest); } } /* Eat all trailing newlines */ for (p--; lastc == '\n'; lastc = *--p) STUNPUTC(dest); if (in.fd >= 0) close(in.fd); if (in.buf) ckfree(in.buf); if (in.jp) exitstatus = waitforjob(in.jp, (int *)NULL); if (quoted == 0) recordregion(startloc, dest - stackblock(), 0); TRACE(("evalbackq: size=%d: \"%.*s\"\n", (dest - stackblock()) - startloc, (dest - stackblock()) - startloc, stackblock() + startloc)); expdest = dest; INTON; } STATIC int subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags) { char *startp; char *loc = NULL; char *q; int c = 0; int saveherefd = herefd; struct nodelist *saveargbackq = argbackq; int amount; herefd = -1; argstr(p, 0); STACKSTRNUL(expdest); herefd = saveherefd; argbackq = saveargbackq; startp = stackblock() + startloc; if (str == NULL) str = stackblock() + strloc; switch (subtype) { case VSASSIGN: setvar(str, startp, 0); amount = startp - expdest; STADJUST(amount, expdest); varflags &= ~VSNUL; if (c != 0) *loc = c; return 1; case VSQUESTION: if (*p != CTLENDVAR) { outfmt(&errout, "%s\n", startp); error((char *)NULL); } - error("%.*s: parameter %snot set", p - str - 1, + error("%.*s: parameter %snot set", (int)(p - str - 1), str, (varflags & VSNUL) ? "null or " : nullstr); return 0; case VSTRIMLEFT: for (loc = startp; loc < str; loc++) { c = *loc; *loc = '\0'; if (patmatch(str, startp, varflags & VSQUOTE)) { *loc = c; goto recordleft; } *loc = c; if ((varflags & VSQUOTE) && *loc == CTLESC) loc++; } return 0; case VSTRIMLEFTMAX: for (loc = str - 1; loc >= startp;) { c = *loc; *loc = '\0'; if (patmatch(str, startp, varflags & VSQUOTE)) { *loc = c; goto recordleft; } *loc = c; loc--; if ((varflags & VSQUOTE) && loc > startp && *(loc - 1) == CTLESC) { for (q = startp; q < loc; q++) if (*q == CTLESC) q++; if (q > loc) loc--; } } return 0; case VSTRIMRIGHT: for (loc = str - 1; loc >= startp;) { if (patmatch(str, loc, varflags & VSQUOTE)) { amount = loc - expdest; STADJUST(amount, expdest); return 1; } loc--; if ((varflags & VSQUOTE) && loc > startp && *(loc - 1) == CTLESC) { for (q = startp; q < loc; q++) if (*q == CTLESC) q++; if (q > loc) loc--; } } return 0; case VSTRIMRIGHTMAX: for (loc = startp; loc < str - 1; loc++) { if (patmatch(str, loc, varflags & VSQUOTE)) { amount = loc - expdest; STADJUST(amount, expdest); return 1; } if ((varflags & VSQUOTE) && *loc == CTLESC) loc++; } return 0; default: abort(); } recordleft: amount = ((str - 1) - (loc - startp)) - expdest; STADJUST(amount, expdest); while (loc != str - 1) *startp++ = *loc++; return 1; } /* * Expand a variable, and return a pointer to the next character in the * input string. */ STATIC char * evalvar(char *p, int flag) { int subtype; int varflags; char *var; char *val; int patloc; int c; int set; int special; int startloc; int varlen; int easy; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); varflags = *p++; subtype = varflags & VSTYPE; var = p; special = 0; if (! is_name(*p)) special = 1; p = strchr(p, '=') + 1; again: /* jump here after setting a variable with ${var=text} */ if (special) { set = varisset(var, varflags & VSNUL); val = NULL; } else { val = bltinlookup(var, 1); if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { val = NULL; set = 0; } else set = 1; } varlen = 0; startloc = expdest - stackblock(); if (!set && uflag) { switch (subtype) { case VSNORMAL: case VSTRIMLEFT: case VSTRIMLEFTMAX: case VSTRIMRIGHT: case VSTRIMRIGHTMAX: case VSLENGTH: - error("%.*s: parameter not set", p - var - 1, var); + error("%.*s: parameter not set", (int)(p - var - 1), + var); } } if (set && subtype != VSPLUS) { /* insert the value of the variable */ if (special) { varvalue(var, varflags & VSQUOTE, flag & EXP_FULL); if (subtype == VSLENGTH) { varlen = expdest - stackblock() - startloc; STADJUST(-varlen, expdest); } } else { char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX : BASESYNTAX; if (subtype == VSLENGTH) { for (;*val; val++) varlen++; } else { while (*val) { if (quotes && syntax[(int)*val] == CCTL) STPUTC(CTLESC, expdest); STPUTC(*val++, expdest); } } } } if (subtype == VSPLUS) set = ! set; easy = ((varflags & VSQUOTE) == 0 || (*var == '@' && shellparam.nparam != 1)); switch (subtype) { case VSLENGTH: expdest = cvtnum(varlen, expdest); goto record; case VSNORMAL: if (!easy) break; record: recordregion(startloc, expdest - stackblock(), varflags & VSQUOTE); break; case VSPLUS: case VSMINUS: if (!set) { argstr(p, flag); break; } if (easy) goto record; break; case VSTRIMLEFT: case VSTRIMLEFTMAX: case VSTRIMRIGHT: case VSTRIMRIGHTMAX: if (!set) break; /* * Terminate the string and start recording the pattern * right after it */ STPUTC('\0', expdest); patloc = expdest - stackblock(); if (subevalvar(p, NULL, patloc, subtype, startloc, varflags) == 0) { int amount = (expdest - stackblock() - patloc) + 1; STADJUST(-amount, expdest); } /* Remove any recorded regions beyond start of variable */ removerecordregions(startloc); goto record; case VSASSIGN: case VSQUESTION: if (!set) { if (subevalvar(p, var, 0, subtype, startloc, varflags)) { varflags &= ~VSNUL; /* * Remove any recorded regions beyond * start of variable */ removerecordregions(startloc); goto again; } break; } if (easy) goto record; break; default: abort(); } if (subtype != VSNORMAL) { /* skip to end of alternative */ int nesting = 1; for (;;) { if ((c = *p++) == CTLESC) p++; else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { if (set) argbackq = argbackq->next; } else if (c == CTLVAR) { if ((*p++ & VSTYPE) != VSNORMAL) nesting++; } else if (c == CTLENDVAR) { if (--nesting == 0) break; } } } return p; } /* * Test whether a specialized variable is set. */ STATIC int varisset(char *name, int nulok) { if (*name == '!') return backgndpid != -1; else if (*name == '@' || *name == '*') { if (*shellparam.p == NULL) return 0; if (nulok) { char **av; for (av = shellparam.p; *av; av++) if (**av != '\0') return 1; return 0; } } else if (is_digit(*name)) { char *ap; int num = atoi(name); if (num > shellparam.nparam) return 0; if (num == 0) ap = arg0; else ap = shellparam.p[num - 1]; if (nulok && (ap == NULL || *ap == '\0')) return 0; } return 1; } /* * Add the value of a specialized variable to the stack string. */ STATIC void varvalue(char *name, int quoted, int allow_split) { int num; char *p; int i; extern int oexitstatus; char sep; char **ap; char const *syntax; #define STRTODEST(p) \ do {\ if (allow_split) { \ syntax = quoted? DQSYNTAX : BASESYNTAX; \ while (*p) { \ if (syntax[(int)*p] == CCTL) \ STPUTC(CTLESC, expdest); \ STPUTC(*p++, expdest); \ } \ } else \ while (*p) \ STPUTC(*p++, expdest); \ } while (0) switch (*name) { case '$': num = rootpid; goto numvar; case '?': num = oexitstatus; goto numvar; case '#': num = shellparam.nparam; goto numvar; case '!': num = backgndpid; numvar: expdest = cvtnum(num, expdest); break; case '-': for (i = 0 ; i < NOPTS ; i++) { if (optlist[i].val) STPUTC(optlist[i].letter, expdest); } break; case '@': if (allow_split && quoted) { for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { STRTODEST(p); if (*ap) STPUTC('\0', expdest); } break; } /* FALLTHROUGH */ case '*': if (ifsset() != 0) sep = ifsval()[0]; else sep = ' '; for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { STRTODEST(p); if (*ap && sep) STPUTC(sep, expdest); } break; case '0': p = arg0; STRTODEST(p); break; default: if (is_digit(*name)) { num = atoi(name); if (num > 0 && num <= shellparam.nparam) { p = shellparam.p[num - 1]; STRTODEST(p); } } break; } } /* * Record the the fact that we have to scan this region of the * string for IFS characters. */ STATIC void recordregion(int start, int end, int nulonly) { struct ifsregion *ifsp; if (ifslastp == NULL) { ifsp = &ifsfirst; } else { ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); ifslastp->next = ifsp; } ifslastp = ifsp; ifslastp->next = NULL; ifslastp->begoff = start; ifslastp->endoff = end; ifslastp->nulonly = nulonly; } /* * Break the argument string into pieces based upon IFS and add the * strings to the argument list. The regions of the string to be * searched for IFS characters have been stored by recordregion. */ STATIC void ifsbreakup(char *string, struct arglist *arglist) { struct ifsregion *ifsp; struct strlist *sp; char *start; char *p; char *q; char *ifs; int ifsspc; int nulonly; start = string; ifsspc = 0; nulonly = 0; if (ifslastp != NULL) { ifsp = &ifsfirst; do { p = string + ifsp->begoff; nulonly = ifsp->nulonly; ifs = nulonly ? nullstr : ( ifsset() ? ifsval() : " \t\n" ); ifsspc = 0; while (p < string + ifsp->endoff) { q = p; if (*p == CTLESC) p++; if (strchr(ifs, *p)) { if (!nulonly) ifsspc = (strchr(" \t\n", *p) != NULL); /* Ignore IFS whitespace at start */ if (q == start && ifsspc) { p++; start = p; continue; } *q = '\0'; sp = (struct strlist *)stalloc(sizeof *sp); sp->text = start; *arglist->lastp = sp; arglist->lastp = &sp->next; p++; if (!nulonly) { for (;;) { if (p >= string + ifsp->endoff) { break; } q = p; if (*p == CTLESC) p++; if (strchr(ifs, *p) == NULL ) { p = q; break; } else if (strchr(" \t\n",*p) == NULL) { if (ifsspc) { p++; ifsspc = 0; } else { p = q; break; } } else p++; } } start = p; } else p++; } } while ((ifsp = ifsp->next) != NULL); if (*start || (!ifsspc && start > string && (nulonly || 1))) { sp = (struct strlist *)stalloc(sizeof *sp); sp->text = start; *arglist->lastp = sp; arglist->lastp = &sp->next; } } else { sp = (struct strlist *)stalloc(sizeof *sp); sp->text = start; *arglist->lastp = sp; arglist->lastp = &sp->next; } } /* * Expand shell metacharacters. At this point, the only control characters * should be escapes. The results are stored in the list exparg. */ char *expdir; STATIC void expandmeta(struct strlist *str, int flag __unused) { char *p; struct strlist **savelastp; struct strlist *sp; char c; /* TODO - EXP_REDIR */ while (str) { if (fflag) goto nometa; p = str->text; for (;;) { /* fast check for meta chars */ if ((c = *p++) == '\0') goto nometa; if (c == '*' || c == '?' || c == '[' || c == '!') break; } savelastp = exparg.lastp; INTOFF; if (expdir == NULL) { int i = strlen(str->text); expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ } expmeta(expdir, str->text); ckfree(expdir); expdir = NULL; INTON; if (exparg.lastp == savelastp) { /* * no matches */ nometa: *exparg.lastp = str; rmescapes(str->text); exparg.lastp = &str->next; } else { *exparg.lastp = NULL; *savelastp = sp = expsort(*savelastp); while (sp->next != NULL) sp = sp->next; exparg.lastp = &sp->next; } str = str->next; } } /* * Do metacharacter (i.e. *, ?, [...]) expansion. */ STATIC void expmeta(char *enddir, char *name) { char *p; char *q; char *start; char *endname; int metaflag; struct stat statb; DIR *dirp; struct dirent *dp; int atend; int matchdot; metaflag = 0; start = name; for (p = name ; ; p++) { if (*p == '*' || *p == '?') metaflag = 1; else if (*p == '[') { q = p + 1; if (*q == '!' || *q == '^') q++; for (;;) { while (*q == CTLQUOTEMARK) q++; if (*q == CTLESC) q++; if (*q == '/' || *q == '\0') break; if (*++q == ']') { metaflag = 1; break; } } } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) { metaflag = 1; } else if (*p == '\0') break; else if (*p == CTLQUOTEMARK) continue; else if (*p == CTLESC) p++; if (*p == '/') { if (metaflag) break; start = p + 1; } } if (metaflag == 0) { /* we've reached the end of the file name */ if (enddir != expdir) metaflag++; for (p = name ; ; p++) { if (*p == CTLQUOTEMARK) continue; if (*p == CTLESC) p++; *enddir++ = *p; if (*p == '\0') break; } if (metaflag == 0 || stat(expdir, &statb) >= 0) addfname(expdir); return; } endname = p; if (start != name) { p = name; while (p < start) { while (*p == CTLQUOTEMARK) p++; if (*p == CTLESC) p++; *enddir++ = *p++; } } if (enddir == expdir) { p = "."; } else if (enddir == expdir + 1 && *expdir == '/') { p = "/"; } else { p = expdir; enddir[-1] = '\0'; } if ((dirp = opendir(p)) == NULL) return; if (enddir != expdir) enddir[-1] = '/'; if (*endname == 0) { atend = 1; } else { atend = 0; *endname++ = '\0'; } matchdot = 0; p = start; while (*p == CTLQUOTEMARK) p++; if (*p == CTLESC) p++; if (*p == '.') matchdot++; while (! int_pending() && (dp = readdir(dirp)) != NULL) { if (dp->d_name[0] == '.' && ! matchdot) continue; if (patmatch(start, dp->d_name, 0)) { if (atend) { scopy(dp->d_name, enddir); addfname(expdir); } else { char *q; for (p = enddir, q = dp->d_name; (*p++ = *q++) != '\0';) continue; p[-1] = '/'; expmeta(p, endname); } } } closedir(dirp); if (! atend) endname[-1] = '/'; } /* * Add a file name to the list. */ STATIC void addfname(char *name) { char *p; struct strlist *sp; p = stalloc(strlen(name) + 1); scopy(name, p); sp = (struct strlist *)stalloc(sizeof *sp); sp->text = p; *exparg.lastp = sp; exparg.lastp = &sp->next; } /* * Sort the results of file name expansion. It calculates the number of * strings to sort and then calls msort (short for merge sort) to do the * work. */ STATIC struct strlist * expsort(struct strlist *str) { int len; struct strlist *sp; len = 0; for (sp = str ; sp ; sp = sp->next) len++; return msort(str, len); } STATIC struct strlist * msort(struct strlist *list, int len) { struct strlist *p, *q = NULL; struct strlist **lpp; int half; int n; if (len <= 1) return list; half = len >> 1; p = list; for (n = half ; --n >= 0 ; ) { q = p; p = p->next; } q->next = NULL; /* terminate first half of list */ q = msort(list, half); /* sort first half of list */ p = msort(p, len - half); /* sort second half */ lpp = &list; for (;;) { if (strcmp(p->text, q->text) < 0) { *lpp = p; lpp = &p->next; if ((p = *lpp) == NULL) { *lpp = q; break; } } else { *lpp = q; lpp = &q->next; if ((q = *lpp) == NULL) { *lpp = p; break; } } } return list; } /* * Returns true if the pattern matches the string. */ int patmatch(char *pattern, char *string, int squoted) { #ifdef notdef if (pattern[0] == '!' && pattern[1] == '!') return 1 - pmatch(pattern + 2, string); else #endif return pmatch(pattern, string, squoted); } STATIC int pmatch(char *pattern, char *string, int squoted) { char *p, *q; char c; p = pattern; q = string; for (;;) { switch (c = *p++) { case '\0': goto breakloop; case CTLESC: if (squoted && *q == CTLESC) q++; if (*q++ != *p++) return 0; break; case CTLQUOTEMARK: continue; case '?': if (squoted && *q == CTLESC) q++; if (*q++ == '\0') return 0; break; case '*': c = *p; while (c == CTLQUOTEMARK || c == '*') c = *++p; if (c != CTLESC && c != CTLQUOTEMARK && c != '?' && c != '*' && c != '[') { while (*q != c) { if (squoted && *q == CTLESC && q[1] == c) break; if (*q == '\0') return 0; if (squoted && *q == CTLESC) q++; q++; } } do { if (pmatch(p, q, squoted)) return 1; if (squoted && *q == CTLESC) q++; } while (*q++ != '\0'); return 0; case '[': { char *endp; int invert, found; char chr; endp = p; if (*endp == '!' || *endp == '^') endp++; for (;;) { while (*endp == CTLQUOTEMARK) endp++; if (*endp == '\0') goto dft; /* no matching ] */ if (*endp == CTLESC) endp++; if (*++endp == ']') break; } invert = 0; if (*p == '!' || *p == '^') { invert++; p++; } found = 0; chr = *q++; if (squoted && chr == CTLESC) chr = *q++; if (chr == '\0') return 0; c = *p++; do { if (c == CTLQUOTEMARK) continue; if (c == CTLESC) c = *p++; if (*p == '-' && p[1] != ']') { p++; while (*p == CTLQUOTEMARK) p++; if (*p == CTLESC) p++; if ( collate_range_cmp(chr, c) >= 0 && collate_range_cmp(chr, *p) <= 0 ) found = 1; p++; } else { if (chr == c) found = 1; } } while ((c = *p++) != ']'); if (found == invert) return 0; break; } dft: default: if (squoted && *q == CTLESC) q++; if (*q++ != c) return 0; break; } } breakloop: if (*q != '\0') return 0; return 1; } /* * Remove any CTLESC characters from a string. */ void rmescapes(char *str) { char *p, *q; p = str; while (*p != CTLESC && *p != CTLQUOTEMARK) { if (*p++ == '\0') return; } q = p; while (*p) { if (*p == CTLQUOTEMARK) { p++; continue; } if (*p == CTLESC) p++; *q++ = *p++; } *q = '\0'; } /* * See if a pattern matches in a case statement. */ int casematch(union node *pattern, char *val) { struct stackmark smark; int result; char *p; setstackmark(&smark); argbackq = pattern->narg.backquote; STARTSTACKSTR(expdest); ifslastp = NULL; argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); STPUTC('\0', expdest); p = grabstackstr(expdest); result = patmatch(p, val, 0); popstackmark(&smark); return result; } /* * Our own itoa(). */ STATIC char * cvtnum(int num, char *buf) { char temp[32]; int neg = num < 0; char *p = temp + 31; temp[31] = '\0'; do { *--p = num % 10 + '0'; } while ((num /= 10) != 0); if (neg) *--p = '-'; while (*p) STPUTC(*p++, buf); return buf; } /* * Do most of the work for wordexp(3). */ int wordexpcmd(int argc, char **argv) { size_t len; int i; out1fmt("%08x", argc - 1); for (i = 1, len = 0; i < argc; i++) len += strlen(argv[i]); out1fmt("%08x", (int)len); for (i = 1; i < argc; i++) { out1str(argv[i]); out1c('\0'); } return (0); } diff --git a/sbin/camcontrol/modeedit.c b/sbin/camcontrol/modeedit.c index 7aeac58782be..c8dd20ab9afb 100644 --- a/sbin/camcontrol/modeedit.c +++ b/sbin/camcontrol/modeedit.c @@ -1,912 +1,912 @@ /*- * Copyright (c) 2000 Kelly Yancey * Derived from work done by Julian Elischer , 1993, and Peter Dufault , 1994. * 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, * without modification, immediately at the beginning of the file. * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "camcontrol.h" int verbose = 0; #define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes" #define DEFAULT_EDITOR "vi" #define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */ #define MAX_PAGENUM_LEN 10 /* Max characters in page num. */ #define MAX_PAGENAME_LEN 64 /* Max characters in page name. */ #define PAGEDEF_START '{' /* Page definition delimiter. */ #define PAGEDEF_END '}' /* Page definition delimiter. */ #define PAGENAME_START '"' /* Page name delimiter. */ #define PAGENAME_END '"' /* Page name delimiter. */ #define PAGEENTRY_END ';' /* Page entry terminator (optional). */ #define MAX_COMMAND_SIZE 255 /* Mode/Log sense data buffer size. */ #define PAGE_CTRL_SHIFT 6 /* Bit offset to page control field. */ /* Macros for working with mode pages. */ #define MODE_PAGE_HEADER(mh) \ (struct scsi_mode_page_header *)find_mode_page_6(mh) #define MODE_PAGE_DATA(mph) \ (u_int8_t *)(mph) + sizeof(struct scsi_mode_page_header) struct editentry { STAILQ_ENTRY(editentry) link; char *name; char type; int editable; int size; union { int ivalue; char *svalue; } value; }; STAILQ_HEAD(, editentry) editlist; /* List of page entries. */ int editlist_changed = 0; /* Whether any entries were changed. */ struct pagename { SLIST_ENTRY(pagename) link; int pagenum; char *name; }; SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */ static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */ static FILE *edit_file = NULL; /* File handle for edit file. */ static char edit_path[] = "/tmp/camXXXXXX"; /* Function prototypes. */ static void editentry_create(void *hook, int letter, void *arg, int count, char *name); static void editentry_update(void *hook, int letter, void *arg, int count, char *name); static int editentry_save(void *hook, char *name); static struct editentry *editentry_lookup(char *name); static int editentry_set(char *name, char *newvalue, int editonly); static void editlist_populate(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout); static void editlist_save(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout); static void nameentry_create(int pagenum, char *name); static struct pagename *nameentry_lookup(int pagenum); static int load_format(char *pagedb_path, int page); static int modepage_write(FILE *file, int editonly); static int modepage_read(FILE *file); static void modepage_edit(void); static void modepage_dump(struct cam_device *device, int page, int page_control, int dbd, int retries, int timeout); static void cleanup_editfile(void); void mode_edit(struct cam_device *device, int page, int page_control, int dbd, int edit, int binary, int retry_count, int timeout); void mode_list(struct cam_device *device, int page_control, int dbd, int retry_count, int timeout); #define returnerr(code) do { \ errno = code; \ return (-1); \ } while (0) #define RTRIM(string) do { \ int _length; \ while (isspace(string[_length = strlen(string) - 1])) \ string[_length] = '\0'; \ } while (0) static void editentry_create(void *hook, int letter, void *arg, int count, char *name) { struct editentry *newentry; /* Buffer to hold new entry. */ /* Allocate memory for the new entry and a copy of the entry name. */ if ((newentry = malloc(sizeof(struct editentry))) == NULL || (newentry->name = strdup(name)) == NULL) err(EX_OSERR, NULL); /* Trim any trailing whitespace for the entry name. */ RTRIM(newentry->name); newentry->editable = (arg != NULL); newentry->type = letter; newentry->size = count; /* Placeholder; not accurate. */ newentry->value.svalue = NULL; STAILQ_INSERT_TAIL(&editlist, newentry, link); } static void editentry_update(void *hook, int letter, void *arg, int count, char *name) { struct editentry *dest; /* Buffer to hold entry to update. */ dest = editentry_lookup(name); assert(dest != NULL); dest->type = letter; dest->size = count; /* We get the real size now. */ switch (dest->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': dest->value.ivalue = (intptr_t)arg; break; case 'c': /* Character array. */ case 'z': /* Null-padded string. */ editentry_set(name, (char *)arg, 0); break; default: ; /* NOTREACHED */ } } static int editentry_save(void *hook, char *name) { struct editentry *src; /* Entry value to save. */ src = editentry_lookup(name); assert(src != NULL); switch (src->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': return (src->value.ivalue); /* NOTREACHED */ case 'c': /* Character array. */ case 'z': /* Null-padded string. */ return ((intptr_t)src->value.svalue); /* NOTREACHED */ default: ; /* NOTREACHED */ } return (0); /* This should never happen. */ } static struct editentry * editentry_lookup(char *name) { struct editentry *scan; assert(name != NULL); STAILQ_FOREACH(scan, &editlist, link) { if (strcasecmp(scan->name, name) == 0) return (scan); } /* Not found during list traversal. */ return (NULL); } static int editentry_set(char *name, char *newvalue, int editonly) { struct editentry *dest; /* Modepage entry to update. */ char *cval; /* Pointer to new string value. */ char *convertend; /* End-of-conversion pointer. */ int ival; /* New integral value. */ int resolution; /* Resolution in bits for integer conversion. */ /* * Macro to determine the maximum value of the given size for the current * resolution. * XXX Lovely x86's optimize out the case of shifting by 32 and gcc doesn't * currently workaround it (even for int64's), so we have to kludge it. */ #define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \ 0xffffffff: (1 << (resolution * (size))) - 1) assert(newvalue != NULL); if (*newvalue == '\0') return (0); /* Nothing to do. */ if ((dest = editentry_lookup(name)) == NULL) returnerr(ENOENT); if (!dest->editable && editonly) returnerr(EPERM); switch (dest->type) { case 'i': /* Byte-sized integral type. */ case 'b': /* Bit-sized integral types. */ case 't': /* Convert the value string to an integer. */ resolution = (dest->type == 'i')? 8: 1; ival = (int)strtol(newvalue, &convertend, 0); if (*convertend != '\0') returnerr(EINVAL); if (ival > RESOLUTION_MAX(dest->size) || ival < 0) { int newival = (ival < 0)? 0: RESOLUTION_MAX(dest->size); warnx("value %d is out of range for entry %s; clipping " "to %d", ival, name, newival); ival = newival; } if (dest->value.ivalue != ival) editlist_changed = 1; dest->value.ivalue = ival; break; case 'c': /* Character array. */ case 'z': /* Null-padded string. */ if ((cval = malloc(dest->size + 1)) == NULL) err(EX_OSERR, NULL); bzero(cval, dest->size + 1); strncpy(cval, newvalue, dest->size); if (dest->type == 'z') { /* Convert trailing spaces to nulls. */ char *convertend; for (convertend = cval + dest->size; convertend >= cval; convertend--) { if (*convertend == ' ') *convertend = '\0'; else if (*convertend != '\0') break; } } if (strncmp(dest->value.svalue, cval, dest->size) == 0) { /* Nothing changed, free the newly allocated string. */ free(cval); break; } if (dest->value.svalue != NULL) { /* Free the current string buffer. */ free(dest->value.svalue); dest->value.svalue = NULL; } dest->value.svalue = cval; editlist_changed = 1; break; default: ; /* NOTREACHED */ } return (0); #undef RESOLUTION_MAX } static void nameentry_create(int pagenum, char *name) { struct pagename *newentry; if (pagenum < 0 || name == NULL || name[0] == '\0') return; /* Allocate memory for the new entry and a copy of the entry name. */ if ((newentry = malloc(sizeof(struct pagename))) == NULL || (newentry->name = strdup(name)) == NULL) err(EX_OSERR, NULL); /* Trim any trailing whitespace for the page name. */ RTRIM(newentry->name); newentry->pagenum = pagenum; SLIST_INSERT_HEAD(&namelist, newentry, link); } static struct pagename * nameentry_lookup(int pagenum) { struct pagename *scan; SLIST_FOREACH(scan, &namelist, link) { if (pagenum == scan->pagenum) return (scan); } /* Not found during list traversal. */ return (NULL); } static int load_format(char *pagedb_path, int page) { FILE *pagedb; char str_pagenum[MAX_PAGENUM_LEN]; char str_pagename[MAX_PAGENAME_LEN]; int pagenum; int depth; /* Quoting depth. */ int found; int lineno; enum { LOCATE, PAGENAME, PAGEDEF } state; char c; #define SETSTATE_LOCATE do { \ str_pagenum[0] = '\0'; \ str_pagename[0] = '\0'; \ pagenum = -1; \ state = LOCATE; \ } while (0) #define SETSTATE_PAGENAME do { \ str_pagename[0] = '\0'; \ state = PAGENAME; \ } while (0) #define SETSTATE_PAGEDEF do { \ format[0] = '\0'; \ state = PAGEDEF; \ } while (0) #define UPDATE_LINENO do { \ if (c == '\n') \ lineno++; \ } while (0) #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer)) if ((pagedb = fopen(pagedb_path, "r")) == NULL) returnerr(ENOENT); SLIST_INIT(&namelist); depth = 0; lineno = 0; found = 0; SETSTATE_LOCATE; while ((c = fgetc(pagedb)) != EOF) { /* Keep a line count to make error messages more useful. */ UPDATE_LINENO; /* Skip over comments anywhere in the mode database. */ if (c == '#') { do { c = fgetc(pagedb); } while (c != '\n' && c != EOF); UPDATE_LINENO; continue; } /* Strip out newline characters. */ if (c == '\n') continue; /* Keep track of the nesting depth for braces. */ if (c == PAGEDEF_START) depth++; else if (c == PAGEDEF_END) { depth--; if (depth < 0) { errx(EX_OSFILE, "%s:%d: %s", pagedb_path, lineno, "mismatched bracket"); } } switch (state) { case LOCATE: /* * Locate the page the user is interested in, skipping * all others. */ if (isspace(c)) { /* Ignore all whitespace between pages. */ break; } else if (depth == 0 && c == PAGEENTRY_END) { /* * A page entry terminator will reset page * scanning (useful for assigning names to * modes without providing a mode definition). */ /* Record the name of this page. */ pagenum = strtol(str_pagenum, NULL, 0); nameentry_create(pagenum, str_pagename); SETSTATE_LOCATE; } else if (depth == 0 && c == PAGENAME_START) { SETSTATE_PAGENAME; } else if (c == PAGEDEF_START) { pagenum = strtol(str_pagenum, NULL, 0); if (depth == 1) { /* Record the name of this page. */ nameentry_create(pagenum, str_pagename); /* * Only record the format if this is * the page we are interested in. */ if (page == pagenum && !found) SETSTATE_PAGEDEF; } } else if (c == PAGEDEF_END) { /* Reset the processor state. */ SETSTATE_LOCATE; } else if (depth == 0 && ! BUFFERFULL(str_pagenum)) { strncat(str_pagenum, &c, 1); } else if (depth == 0) { - errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path, + errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page identifier exceeds", sizeof(str_pagenum) - 1, "characters"); } break; case PAGENAME: if (c == PAGENAME_END) { /* * Return to LOCATE state without resetting the * page number buffer. */ state = LOCATE; } else if (! BUFFERFULL(str_pagename)) { strncat(str_pagename, &c, 1); } else { - errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path, + errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page name exceeds", sizeof(str_pagenum) - 1, "characters"); } break; case PAGEDEF: /* * Transfer the page definition into a format buffer * suitable for use with CDB encoding/decoding routines. */ if (depth == 0) { found = 1; SETSTATE_LOCATE; } else if (! BUFFERFULL(format)) { strncat(format, &c, 1); } else { - errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path, + errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path, lineno, "page definition exceeds", sizeof(format) - 1, "characters"); } break; default: ; /* NOTREACHED */ } /* Repeat processing loop with next character. */ } if (ferror(pagedb)) err(EX_OSFILE, "%s", pagedb_path); /* Close the SCSI page database. */ fclose(pagedb); if (!found) /* Never found a matching page. */ returnerr(ESRCH); return (0); } static void editlist_populate(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; STAILQ_INIT(&editlist); /* Fetch changeable values; use to build initial editlist. */ mode_sense(device, modepage, 1, dbd, retries, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Decode the value data, creating edit_entries for each value. */ buff_decode_visit(mode_pars, mh->data_length, format, editentry_create, 0); /* Fetch the current/saved values; use to set editentry values. */ mode_sense(device, modepage, page_control, dbd, retries, timeout, data, sizeof(data)); buff_decode_visit(mode_pars, mh->data_length, format, editentry_update, 0); } static void editlist_save(struct cam_device *device, int modepage, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; /* Make sure that something changed before continuing. */ if (! editlist_changed) return; /* * Preload the CDB buffer with the current mode page data. * XXX If buff_encode_visit would return the number of bytes encoded * we *should* use that to build a header from scratch. As it is * now, we need mode_sense to find out the page length. */ mode_sense(device, modepage, page_control, dbd, retries, timeout, data, sizeof(data)); /* Initial headers & offsets. */ mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Encode the value data to be passed back to the device. */ buff_encode_visit(mode_pars, mh->data_length, format, editentry_save, 0); /* Eliminate block descriptors. */ bcopy(mph, ((u_int8_t *)mh) + sizeof(*mh), sizeof(*mph) + mph->page_length); /* Recalculate headers & offsets. */ mh->blk_desc_len = 0; /* No block descriptors. */ mh->dev_spec = 0; /* Clear device-specific parameters. */ mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); mph->page_code &= SMS_PAGE_CODE;/* Isolate just the page code. */ mh->data_length = 0; /* Reserved for MODE SELECT command. */ /* * Write the changes back to the device. If the user editted control * page 3 (saved values) then request the changes be permanently * recorded. */ mode_select(device, (page_control << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED), retries, timeout, (u_int8_t *)mh, sizeof(*mh) + mh->blk_desc_len + sizeof(*mph) + mph->page_length); } static int modepage_write(FILE *file, int editonly) { struct editentry *scan; int written = 0; STAILQ_FOREACH(scan, &editlist, link) { if (scan->editable || !editonly) { written++; if (scan->type == 'c' || scan->type == 'z') { fprintf(file, "%s: %s\n", scan->name, scan->value.svalue); } else { fprintf(file, "%s: %d\n", scan->name, scan->value.ivalue); } } } return (written); } static int modepage_read(FILE *file) { char *buffer; /* Pointer to dynamic line buffer. */ char *line; /* Pointer to static fgetln buffer. */ char *name; /* Name portion of the line buffer. */ char *value; /* Value portion of line buffer. */ size_t length; /* Length of static fgetln buffer. */ #define ABORT_READ(message, param) do { \ warnx(message, param); \ free(buffer); \ returnerr(EAGAIN); \ } while (0) while ((line = fgetln(file, &length)) != NULL) { /* Trim trailing whitespace (including optional newline). */ while (length > 0 && isspace(line[length - 1])) length--; /* Allocate a buffer to hold the line + terminating null. */ if ((buffer = malloc(length + 1)) == NULL) err(EX_OSERR, NULL); memcpy(buffer, line, length); buffer[length] = '\0'; /* Strip out comments. */ if ((value = strchr(buffer, '#')) != NULL) *value = '\0'; /* The name is first in the buffer. Trim whitespace.*/ name = buffer; RTRIM(name); while (isspace(*name)) name++; /* Skip empty lines. */ if (strlen(name) == 0) continue; /* The name ends at the colon; the value starts there. */ if ((value = strrchr(buffer, ':')) == NULL) ABORT_READ("no value associated with %s", name); *value = '\0'; /* Null-terminate name. */ value++; /* Value starts afterwards. */ /* Trim leading and trailing whitespace. */ RTRIM(value); while (isspace(*value)) value++; /* Make sure there is a value left. */ if (strlen(value) == 0) ABORT_READ("no value associated with %s", name); /* Update our in-memory copy of the modepage entry value. */ if (editentry_set(name, value, 1) != 0) { if (errno == ENOENT) { /* No entry by the name. */ ABORT_READ("no such modepage entry \"%s\"", name); } else if (errno == EINVAL) { /* Invalid value. */ ABORT_READ("Invalid value for entry \"%s\"", name); } else if (errno == ERANGE) { /* Value out of range for entry type. */ ABORT_READ("value out of range for %s", name); } else if (errno == EPERM) { /* Entry is not editable; not fatal. */ warnx("modepage entry \"%s\" is read-only; " "skipping.", name); } } free(buffer); } return (ferror(file)? -1: 0); #undef ABORT_READ } static void modepage_edit(void) { char *editor; char *commandline; int fd; int written; if (!isatty(fileno(stdin))) { /* Not a tty, read changes from stdin. */ modepage_read(stdin); return; } /* Lookup editor to invoke. */ if ((editor = getenv("EDITOR")) == NULL) editor = DEFAULT_EDITOR; /* Create temp file for editor to modify. */ if ((fd = mkstemp(edit_path)) == -1) errx(EX_CANTCREAT, "mkstemp failed"); atexit(cleanup_editfile); if ((edit_file = fdopen(fd, "w")) == NULL) err(EX_NOINPUT, "%s", edit_path); written = modepage_write(edit_file, 1); fclose(edit_file); edit_file = NULL; if (written == 0) { warnx("no editable entries"); cleanup_editfile(); return; } /* * Allocate memory to hold the command line (the 2 extra characters * are to hold the argument separator (a space), and the terminating * null character. */ commandline = malloc(strlen(editor) + strlen(edit_path) + 2); if (commandline == NULL) err(EX_OSERR, NULL); sprintf(commandline, "%s %s", editor, edit_path); /* Invoke the editor on the temp file. */ if (system(commandline) == -1) err(EX_UNAVAILABLE, "could not invoke %s", editor); free(commandline); if ((edit_file = fopen(edit_path, "r")) == NULL) err(EX_NOINPUT, "%s", edit_path); /* Read any changes made to the temp file. */ modepage_read(edit_file); cleanup_editfile(); } static void modepage_dump(struct cam_device *device, int page, int page_control, int dbd, int retries, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; int index; /* Index for scanning mode params. */ mode_sense(device, page, page_control, dbd, retries, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; mph = MODE_PAGE_HEADER(mh); mode_pars = MODE_PAGE_DATA(mph); /* Print the raw mode page data with newlines each 8 bytes. */ for (index = 0; index < mph->page_length; index++) { printf("%02x%c",mode_pars[index], (((index + 1) % 8) == 0) ? '\n' : ' '); } putchar('\n'); } static void cleanup_editfile(void) { if (edit_file == NULL) return; if (fclose(edit_file) != 0 || unlink(edit_path) != 0) warn("%s", edit_path); edit_file = NULL; } void mode_edit(struct cam_device *device, int page, int page_control, int dbd, int edit, int binary, int retry_count, int timeout) { char *pagedb_path; /* Path to modepage database. */ if (edit && binary) errx(EX_USAGE, "cannot edit in binary mode."); if (! binary) { if ((pagedb_path = getenv("SCSI_MODES")) == NULL) pagedb_path = DEFAULT_SCSI_MODE_DB; if (load_format(pagedb_path, page) != 0 && (edit || verbose)) { if (errno == ENOENT) { /* Modepage database file not found. */ warn("cannot open modepage database \"%s\"", pagedb_path); } else if (errno == ESRCH) { /* Modepage entry not found in database. */ warnx("modepage %d not found in database" "\"%s\"", page, pagedb_path); } /* We can recover in display mode, otherwise we exit. */ if (!edit) { warnx("reverting to binary display only"); binary = 1; } else exit(EX_OSFILE); } editlist_populate(device, page, page_control, dbd, retry_count, timeout); } if (edit) { if (page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT && page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED) errx(EX_USAGE, "it only makes sense to edit page 0 " "(current) or page 3 (saved values)"); modepage_edit(); editlist_save(device, page, page_control, dbd, retry_count, timeout); } else if (binary || STAILQ_EMPTY(&editlist)) { /* Display without formatting information. */ modepage_dump(device, page, page_control, dbd, retry_count, timeout); } else { /* Display with format. */ modepage_write(stdout, 0); } } void mode_list(struct cam_device *device, int page_control, int dbd, int retry_count, int timeout) { u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */ u_int8_t *mode_pars; /* Pointer to modepage params. */ struct scsi_mode_header_6 *mh; /* Location of mode header. */ struct scsi_mode_page_header *mph; struct pagename *nameentry; char *pagedb_path; int len; if ((pagedb_path = getenv("SCSI_MODES")) == NULL) pagedb_path = DEFAULT_SCSI_MODE_DB; if (load_format(pagedb_path, 0) != 0 && verbose && errno == ENOENT) { /* Modepage database file not found. */ warn("cannot open modepage database \"%s\"", pagedb_path); } /* Build the list of all mode pages by querying the "all pages" page. */ mode_sense(device, SMS_ALL_PAGES_PAGE, page_control, dbd, retry_count, timeout, data, sizeof(data)); mh = (struct scsi_mode_header_6 *)data; len = mh->blk_desc_len; /* Skip block descriptors. */ /* Iterate through the pages in the reply. */ while (len < mh->data_length) { /* Locate the next mode page header. */ mph = (struct scsi_mode_page_header *) ((intptr_t)mh + sizeof(*mh) + len); mode_pars = MODE_PAGE_DATA(mph); mph->page_code &= SMS_PAGE_CODE; nameentry = nameentry_lookup(mph->page_code); if (nameentry == NULL || nameentry->name == NULL) printf("0x%02x\n", mph->page_code); else printf("0x%02x\t%s\n", mph->page_code, nameentry->name); len += mph->page_length + sizeof(*mph); } }