mod_export int optlookup(char const *name) { char *s, *t; Optname n; s = t = dupstring(name); /* exorcise underscores, and change to lowercase */ while (*t) if (*t == '_') chuck(t); else { *t = tulower(*t); t++; } /* look up name in the table */ if (s[0] == 'n' && s[1] == 'o' && (n = (Optname) optiontab->getnode(optiontab, s + 2))) { return -n->optno; } else if ((n = (Optname) optiontab->getnode(optiontab, s))) return n->optno; else return OPT_INVALID; }
int getzlequery(void) { int c; #ifdef FIONREAD int val; /* check for typeahead, which is treated as a negative response */ ioctl(SHTTY, FIONREAD, (char *)&val); if (val) { putc('n', shout); return 'n'; } #endif /* get a character from the tty and interpret it */ c = getkey(0); if (c == '\t') c = 'y'; else if (icntrl(c) || c == EOF) c = 'n'; else c = tulower(c); /* echo response and return */ putc(c, shout); return c; }
int invcstrpcmp(const void *a, const void *b) { #ifdef HAVE_STRCOLL # ifdef __GNUC__ char c[strlen(*(char **) a) + 1]; char d[strlen(*(char **) b) + 1]; # else char *c = halloc(strlen(*(char **) a) + 1); char *d = halloc(strlen(*(char **) b) + 1); # endif char *s, *t; int cmp; for (s = *(char **) a, t = c; (*t++ = tulower(*s++));); for (s = *(char **) b, t = d; (*t++ = tulower(*s++));); cmp = strcoll(c, d); return -cmp; #else char *c = *(char **)a, *d = *(char **)b; for (; *c && tulower(*c) == tulower(*d); c++, d++); return (int)STOUC(tulower(*d)) - (int)STOUC(tulower(*c)); #endif }
void visetbuffer(void) { int ch; if (gotvibufspec || (((ch = getkey(0)) < '1' || ch > '9') && (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z'))) { feep(); return; } if (ch >= 'A' && ch <= 'Z') /* needed in cut() */ vibufappend = 1; else vibufappend = 0; vibufspec = tulower(ch) + (idigit(ch) ? -'1' + 26 : -'a'); gotvibufspec = 1; }
void viswapcase(void) { int eol; startvichange(-1); if (zmult < 1) return; eol = findeol(); while (cs < eol && zmult--) { if (islower(line[cs])) line[cs] = tuupper(line[cs]); else if (isupper(line[cs])) line[cs] = tulower(line[cs]); cs++; } if (cs && cs == eol) cs--; }
void vioperswapcase(void) { int oldcs, c2; /* get the range */ startvichange(1); if ((c2 = getvirange(0)) != -1) { oldcs = cs; /* swap the case of all letters within range */ while (cs < c2) { if (islower(line[cs])) line[cs] = tuupper(line[cs]); else if (isupper(line[cs])) line[cs] = tulower(line[cs]); cs++; } cs = oldcs; } vichgflag = vilinerange = 0; }
int bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun) { int action, optno, match = 0; /* With no arguments or options, display options. */ if (!*args) { scanhashtable(optiontab, 1, 0, OPT_ALIAS, optiontab->printnode, !isun); return 0; } /* loop through command line options (begins with "-" or "+") */ while (*args && (**args == '-' || **args == '+')) { action = (**args == '-') ^ isun; if(!args[0][1]) *args = "--"; while (*++*args) { if(**args == Meta) *++*args ^= 32; /* The pseudo-option `--' signifies the end of options. */ if (**args == '-') { args++; goto doneoptions; } else if (**args == 'o') { if (!*++*args) args++; if (!*args) { zwarnnam(nam, "string expected after -o", NULL, 0); inittyptab(); return 1; } if(!(optno = optlookup(*args))) zwarnnam(nam, "no such option: %s", *args, 0); else if(dosetopt(optno, action, 0)) zwarnnam(nam, "can't change option: %s", *args, 0); break; } else if(**args == 'm') { match = 1; } else { if (!(optno = optlookupc(**args))) zwarnnam(nam, "bad option: -%c", NULL, **args); else if(dosetopt(optno, action, 0)) zwarnnam(nam, "can't change option: -%c", NULL, **args); } } args++; } doneoptions: if (!match) { /* Not globbing the arguments -- arguments are simply option names. */ while (*args) { if(!(optno = optlookup(*args++))) zwarnnam(nam, "no such option: %s", args[-1], 0); else if(dosetopt(optno, !isun, 0)) zwarnnam(nam, "can't change option: %s", args[-1], 0); } } else { /* Globbing option (-m) set. */ while (*args) { Patprog pprog; char *s, *t; t = s = dupstring(*args); while (*t) if (*t == '_') chuck(t); else { *t = tulower(*t); t++; } /* Expand the current arg. */ tokenize(s); if (!(pprog = patcompile(s, PAT_STATIC, NULL))) { zwarnnam(nam, "bad pattern: %s", *args, 0); continue; } /* Loop over expansions. */ scanmatchtable(optiontab, pprog, 0, OPT_ALIAS, setoption, !isun); args++; } } inittyptab(); return 0; }
mod_export void strmetasort(char **array, int sortwhat, int *unmetalenp) { char **arrptr; /* * Array of elements containing stuff to sort. Note sortptrarr * is an array of pointers, since that's more efficient * for qsort() to manipulate. sortarr is the array of * structures. */ SortElt *sortptrarr, *sortptrarrptr; SortElt sortarr, sortarrptr; int oldsortdir, oldsortnumeric, nsort; nsort = arrlen(array); if (nsort < 2) return; pushheap(); sortptrarr = (SortElt *) zhalloc(nsort * sizeof(SortElt)); sortarr = (SortElt) zhalloc(nsort * sizeof(struct sortelt)); for (arrptr = array, sortptrarrptr = sortptrarr, sortarrptr = sortarr; *arrptr; arrptr++, sortptrarrptr++, sortarrptr++) { char *metaptr; int needlen, needalloc; *sortptrarrptr = sortarrptr; sortarrptr->orig = *arrptr; if (unmetalenp) { /* * Already unmetafied. We just need to check for * embededded nulls. */ int count = unmetalenp[arrptr-array]; /* Remember this length for sorted array */ sortarrptr->origlen = count; for (metaptr = *arrptr; *metaptr != '\0' && count--; metaptr++) ; /* *metaptr must now be \0, even if we reached the end */ needlen = (count != 0); } else { /* * Not yet unmetafied. See if it needs unmetafying. * If it doesn't, there can't be any embedded nulls, * since these are metafied. */ needlen = 0; for (metaptr = *arrptr; *metaptr && *metaptr != Meta; metaptr++); } /* * See if we need to do some special checking. * Either we're going to need to copy it to transform it, * or we need to unmetafy it. */ if ((needalloc = (sortwhat & (SORTIT_IGNORING_CASE|SORTIT_IGNORING_BACKSLASHES))) || *metaptr == Meta) { char *s, *t, *src = *arrptr, *dst; int len; sortarrptr->cmp = dst = (char *)zhalloc(((sortwhat & SORTIT_IGNORING_CASE)?2:1)*strlen(src)+1); if (unmetalenp) { /* Already unmetafied and we have the length. */ len = unmetalenp[arrptr-array]; } else if (*metaptr != '\0') { /* * Needs unmetafying. We need to check for * embedded nulls while we do this. */ char *t = dst + (metaptr - src); if (metaptr != src) memcpy(dst, src, metaptr - src); while ((*t = *metaptr++)) { if (*t++ == Meta) { if ((t[-1] = *metaptr++ ^ 32) == '\0') needlen = 1; } } len = t - dst; src = dst; } else { /* * Doesn't need unmetafying. * This means metaptr is the NULL at the * end of the string, so we have the length, and * there are no embedded nulls, so we don't * need the length later. * We're only copying the string to transform it * below. */ len = metaptr - src; } if (sortwhat & SORTIT_IGNORING_CASE) { char *send = src + len; #ifdef MULTIBYTE_SUPPORT if (isset(MULTIBYTE)) { /* * Lower the case the hard way. Convert to a wide * character, process that, and convert back. We * don't assume the characters have the same * multibyte length. We can't use casemodify() * because we have unmetafied data, which may have * been passed down to use. */ mbstate_t mbsin, mbsout; int clen; wchar_t wc; memset(&mbsin, 0, sizeof(mbstate_t)); memset(&mbsout, 0, sizeof(mbstate_t)); for (s = src, t = dst; s < send; ) { clen = mbrtowc(&wc, s, send-s, &mbsin); if (clen < 0) { /* invalid or unfinished: treat as single bytes */ while (s < send) *t++ = tulower(*s++); break; } if (clen == 0) { /* embedded null */ *t++ = '\0'; s++; continue; } s += clen; wc = towlower(wc); clen = wcrtomb(t, wc, &mbsout); t += clen; DPUTS(clen < 0, "Bad conversion when lowering case"); } *t = '\0'; len = t - dst; } else #endif for (s = src, t = dst; s < send; ) *t++ = tulower(*s++); src = dst; } if (sortwhat & SORTIT_IGNORING_BACKSLASHES) { char *end = src + len + 1; /* copy null byte, so increment length */ for (s = src, t = dst; s < end; ) { if (*s == '\\') { s++; len--; } *t++ = *s++; } } /* Do we need to store the length (embedded null)? */ sortarrptr->len = needlen ? len : -1; } else { /* * We can use the string as is, although it's possible * we still need to take account of an embedded null. */ sortarrptr->cmp = *arrptr; sortarrptr->len = needlen ? unmetalenp[arrptr-array] : -1; } } /* * We probably don't need to restore the following, but it's pretty cheap. */ oldsortdir = sortdir; oldsortnumeric = sortnumeric; sortdir = (sortwhat & SORTIT_BACKWARDS) ? -1 : 1; sortnumeric = (sortwhat & SORTIT_NUMERICALLY) ? 1 : 0; qsort(sortptrarr, nsort, sizeof(SortElt), eltpcmp); sortnumeric = oldsortnumeric; sortdir = oldsortdir; for (arrptr = array, sortptrarrptr = sortptrarr; nsort--; ) { if (unmetalenp) unmetalenp[arrptr-array] = (*sortptrarrptr)->origlen; *arrptr++ = (*sortptrarrptr++)->orig; } popheap(); }