Permalink
Switch branches/tags
master projects/altix projects/altix2 projects/amd64_xen_pv projects/arm_eabi_vfp projects/arm_eabi projects/arm_intrng projects/arm64 projects/armv6 projects/arpv2_merge_1 projects/ath_hal projects/atomic64 projects/bectl projects/bhyve_graphics projects/bhyve_npt_pmap projects/bhyve_ref projects/bhyve_svm projects/bhyve projects/bigbb projects/binutils-2.17 projects/bmake projects/bpfjit projects/bsd_rdma_4_9_stable_11 projects/bsd_rdma_4_9 projects/bsnmp-improved-ipv6-support projects/bsnmp-ipv6-mib projects/building-blocks projects/calloutng projects/cambria projects/camlock projects/capabilities8 projects/ci20_mips projects/cider projects/clang-sparc64 projects/clang-trunk projects/clang350-import projects/clang360-import projects/clang370-import projects/clang380-import projects/clang390-import projects/clang391-import projects/clang400-import projects/clang500-import projects/clang600-import projects/clang700-import projects/clangbsd-import projects/clangbsd projects/collation projects/contrib-netbsd-update-12 projects/counters projects/csup_wip projects/cxl_iscsi projects/diffused_head projects/doctools projects/dtrace projects/efi projects/efika_mx projects/elftoolchain-update-r3130 projects/elftoolchain projects/em_mq projects/enhanced_coredumps projects/etc_ipv6 projects/fix-libarchive-tests-after-r304075 projects/flex-sf projects/fuse projects/geom-events projects/geom_raid5 projects/graid/head projects/graid/7 projects/graid/8 projects/gvinum projects/hastd_wan projects/head_mfi projects/hid projects/hps_head projects/hwpmc projects/hyperv projects/ifnet projects/ifq projects/ino64 projects/intel_net_refactor projects/iosched projects/ipfw projects/ipsec projects/iscsi_opt projects/jailconf projects/jbuild projects/jeff_mbuf projects/jeff_mbuf2 projects/krb5 projects/l2filter projects/libpcap-update projects/libprocstat projects/libzfs_core projects/lld-import projects/lldb-r201577 projects/llvm-ia64 projects/lua-bootloader projects/lwref projects/make-check-sandbox projects/mbuf_iovec projects/mips projects/mips64-clang projects/mpsutil projects/mtree projects/multi-fibv6 projects/nand projects/net80211_testsuite projects/netbsd-tests-update-12 projects/netbsd-tests-upstream-01-2017 projects/nfsv4-packrats projects/nfsv4.1-client projects/nfsv4.1-server projects/nfsv42 projects/ngroups projects/numa projects/numa2 projects/ofed/base projects/ofed/head projects/onewire projects/openssl-1.0.2 projects/openssl_098_merge_8 projects/openssl111 projects/paravirt projects/pciehp projects/pf projects/physbio projects/pmac_pmu projects/pms projects/pnet projects/pnfs-planb-server-stable11 projects/pnfs-planb-server projects/pnfs-server projects/portbuild projects/power8_bringup_hacks projects/powernv projects/powerpcspe projects/ppc_linuxemu projects/ppc64 projects/pseries projects/quota64 projects/random_number_generator projects/release-arm-redux projects/release-arm64 projects/release-debugdist projects/release-embedded projects/release-install-debug projects/release-mips projects/release-noxdev projects/release-pkg projects/release-vmimage projects/release_6_3_xen projects/releng_6_xen projects/releng_7_xen projects/relnotes-restructure projects/routing_mpath projects/routing projects/rrs_mqueue projects/rrs_socrypto_tls projects/rtentry_cache projects/runtime-coverage projects/sbruno_firewire projects/sbruno_64cpus projects/sendfile projects/specific_leg projects/stable-10-backport-test-changes projects/stable-10-opts-mk projects/stable_7_xen projects/stress2 projects/suj projects/sv projects/tcp_cc_head projects/tcp_cc_7.x projects/tcp_cc_8.x projects/tcp_ffcaia2008_head projects/tcp_ffcaia2008_8.x projects/uefi projects/umtx projects/utrace2 projects/vap7 projects/varsym projects/virtio projects/vmware_pvscsi projects/vmxnet projects/vnet projects/vps projects/vxlan projects/x2apic projects/zfsd release/2.0 release/2.0.5 release/2.1.0 release/2.1.5 release/2.1.6 release/2.1.6.1 release/2.1.7 release/2.2.0 release/2.2.1 release/2.2.2 release/2.2.5 release/2.2.6 release/2.2.7 release/2.2.8 release/3.0.0 release/3.1.0 release/3.2.0 release/3.3.0 release/3.4.0 release/3.5.0 release/4.0.0 release/4.1.0 release/4.1.1 release/4.2.0 release/4.3.0 release/4.4.0 release/4.5.0 release/4.6.0 release/4.6.1 release/4.6.2 release/4.7.0 release/4.8.0 release/4.9.0 release/4.10.0 release/4.11.0 release/5.0.0 release/5.1.0 release/5.2.0 release/5.2.1 release/5.3.0 release/5.4.0 release/5.5.0 release/6.0.0 release/6.1.0 release/6.2.0 release/6.3.0 release/6.4.0 release/7.0.0 release/7.1.0 release/7.2.0 release/7.3.0 release/7.4.0 release/8.0.0 release/8.1.0 release/8.2.0 release/8.3.0 release/8.4.0 release/9.0.0 release/9.1.0 release/9.2.0 release/9.3.0 release/10.0.0 release/10.1.0 release/10.2.0 release/10.3.0 release/10.4.0 release/11.0.0 release/11.0.1 release/11.1.0 release/11.2.0 release/12.0.0 releng/2.0a releng/2.0b releng/2.0.5 releng/4.3 releng/4.4 releng/4.5 releng/4.6 releng/4.7 releng/4.8 releng/4.9 releng/4.10 releng/4.11 releng/5.0 releng/5.1 releng/5.2 releng/5.3 releng/5.4 releng/5.5 releng/6.0 releng/6.1 releng/6.2 releng/6.3 releng/6.4 releng/7.0 releng/7.1 releng/7.2 releng/7.3 releng/7.4 releng/8.0 releng/8.1 releng/8.2 releng/8.3 releng/8.4 releng/9.0 releng/9.1 releng/9.2 releng/9.3 releng/10.0 releng/10.1 releng/10.2 releng/10.3 releng/10.4 releng/11.0 releng/11.1 releng/11.2 releng/12.0 stable/2.0.5 stable/2.1 stable/2.2 stable/3 stable/4 stable/5 stable/6 stable/7 stable/8 stable/9 stable/10 stable/11 stable/12 svn_head user/adrian/ath_radar_stuff user/adrian/if_ath_devel user/adrian/if_ath_tx user/adrian/net80211_amsdu_ff user/adrian/net80211_tx user/adrian/spoof_bind user/ae/bootcode user/ae/inet6 user/ae/usr.sbin user/alc/PG_CACHED user/alc/PQ_LAUNDRY user/alc/cachefree user/alc/pagelock user/alc/pmap user/alc/pvh_global_lock user/alc/superpages user/alc/zerocopy user/alfred/ewatchdog user/alfred/so_discard user/alfred/9-alfred user/andre/mbuf_staging user/andre/routelocking user/andre/tcp-ao user/andre/tcp_workqueue user/asomers/style9 user/attilio/jeff-numa user/attilio/membarclean user/attilio/rm_vmobj_cache user/attilio/vmc-playground user/attilio/vmcontention user/attilio/vmobj-fullread user/attilio/vmobj-readlock user/attilio/vmobj-rwlock user/avg/xcpu user/bapt/diff user/brooks/openssh-hpn user/brooks/tchacks user/bryanv/vtnetmq user/bugmeister/gnats user/bz/unbound_dns64 user/cperciva/EC2-build user/cperciva/freebsd-update-build user/cperciva/freebsd-update-mirror user/cperciva/panicmail user/cperciva/pkesh user/cperciva/portsnap-build user/cperciva/portsnap-mirror user/crees/rclint user/davidxu/libthr user/davidxu/pth_objdestroy user/dchagin/lemul user/delphij/libz-8 user/delphij/patch user/delphij/zfs-arc-rebase user/delphij/zfs-lz4 user/des/bsddom user/des/compat user/des/fbce user/des/fbp user/des/fetch user/des/fmerge user/des/ndr user/des/phybs user/des/refetch user/des/sizes user/des/struct user/des/svnsup user/des/tinderbox user/des/zfs-backup user/dfr/gssapi user/dfr/xenhvm user/dim/merge-test user/dim/merge-test2 user/dougb/portmaster user/ed/newcons user/edwin/calendar user/edwin/locale user/edwin/releasenotes user/edwin/tftpd user/edwin/top user/edwin/tzcode user/eri/net user/eri/netinet user/eri/pf45 user/fabient/svctest user/gabor/grep user/gabor/tre-integration user/gad/singlemod user/gjb/hacking user/gjb/release-embedded user/gjb/thermite-v2 user/gjb/thermite user/glebius/course user/gnn/fasttrap user/gnn/geom user/gonzo/bugzilla-freebsd user/hrs/arm user/hrs/ipv6 user/hrs/rc.d user/hrs/releng user/imp/camdoc user/imp/config_reorg_sys user/imp/ext_comp user/imp/extern_cc user/imp/masq user/imp/newcard user/imp/nopriv user/imp/tbemd user/jceel/soc2012_armv6 user/jceel/soc2014_evdev user/jchandra/xlp-merge user/jchandra/8-stable-mips user/jeff/numa user/jgh/committers-guide user/jgh/icedtea6 user/jimharris/ioat user/jimharris/isci user/jmallett/octeon user/jmmv/autotest user/keramida/tzcode user/keramida/vendor user/kib/vm6 user/kmacy/HEAD_ECMP user/kmacy/HEAD_fast_multi_xmit user/kmacy/HEAD_fast_net_merge user/kmacy/HEAD_fast_net user/kmacy/HEAD_fast_xmit user/kmacy/HEAD_flowtable user/kmacy/HEAD_multi_tx user/kmacy/head_arpv2 user/kmacy/head_flowtable_v6 user/kmacy/head_page_lock_incr user/kmacy/head_page_lock user/kmacy/head_page_lock_2 user/kmacy/head_ppacket user/kmacy/head_zfs_merge user/kmacy/head_zfs_updates user/kmacy/head_zlib user/kmacy/releng_7_fast_net user/kmacy/releng_7_2_fast_net user/kmacy/releng_7_2_fcs user/kmacy/releng_7_2_fcs_1 user/kmacy/releng_7_2_xen user/kmacy/releng_7_2_zfs user/kmacy/releng_8_fcs_buf_xen user/kmacy/releng_8_fcs_buf user/kmacy/releng_8_fcs user/kmacy/releng_8_page_lock user/kmacy/releng_8_rump user/kmacy/stable_7_page_lock user/lstewart/alq_varlen_7.x user/lstewart/alq_varlen_8.x user/lstewart/dummynet_7.x user/lstewart/dummynet_8.x user/lstewart/misc_7.x user/lstewart/misc_8.x user/luigi/geom_sched user/luigi/ipfw user/luigi/ipfw_80 user/luigi/netmap user/marcel/altix-eol user/marcel/ia64-eol user/marcel/libvdsk user/marcel/mkimg user/marcel/vtc.sys user/markj/PQ_LAUNDRY_11 user/markj/netdump user/markj/vm-playground user/mjacob/sys user/mlaier/pf44head user/mr/ficl user/netchild/calendar-utf8 user/netchild/coverity user/netchild/deskjail user/netchild/doxygen user/netchild/jailmount user/netchild/linuxaio user/netchild/linuxulator-dtrace user/netchild/misc user/netchild/obsolete user/ngie/add-pjdfstest user/ngie/bsnmp_cleanup user/ngie/bug203673 user/ngie/checkdpadd-fixes user/ngie/detangle-rc user/ngie/fix-progs user/ngie/github-test-cherrypicks user/ngie/make_check user/ngie/more-tests.broken user/ngie/more-tests user/ngie/more-tests2 user/ngie/ntb-hacking user/ngie/optional-require user/ngie/release-pkg-fix-tests user/ngie/socket-tests user/ngie/stable-10-fix-LINT-NOINET user/ngie/stable-10-ixgbe-upgrade user/ngie/stable-10-libnv user/ngie/10.1-bug-198062 user/np/cxl_tuning user/np/stable_9_toe user/np/toe_iwarp user/nwhitehorn/bsdinstall user/nwhitehorn/ps3 user/nyan/pc98 user/obrien/krb5bld user/obrien/libmd-ossl user/peter/kinfo user/peter/long_cpumask user/pho/stress2 user/piso/ipfw_old user/piso/ipfw user/rafan/ncurses user/rpaulo/aird user/rpaulo/armpmc user/rpaulo/eeemon user/rpaulo/ubthidctl user/sam/wifi user/sbruno/ard user/sbruno/head_146698 user/sbruno/head_177038 user/sbruno/head_189017 user/sbruno/mfid user/sbruno/mptd user/sbruno/pxe_http-gsoc2007 user/sbruno/pxe_http_head user/sbruno/pxe_http user/sbruno/pxestuff user/sbruno/1394dev user/syuu/bhyve_standalone_guest_test1 user/syuu/bhyve_standalone_guest user/syuu/mq_bpf user/thompsa/usb user/thompsa/vaptq user/uqs/scan-world user/weongyo/usb user/yar/rcguard
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
390 lines (361 sloc) 8.6 KB
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1991, 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.
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#ifndef lint
static const char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
#endif
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "extern.h"
static int backslash(STR *, int *);
static int bracket(STR *);
static void genclass(STR *);
static void genequiv(STR *);
static int genrange(STR *, int);
static void genseq(STR *);
wint_t
next(STR *s)
{
int is_octal;
wint_t ch;
wchar_t wch;
size_t clen;
switch (s->state) {
case EOS:
return (0);
case INFINITE:
return (1);
case NORMAL:
switch (*s->str) {
case '\0':
s->state = EOS;
return (0);
case '\\':
s->lastch = backslash(s, &is_octal);
break;
case '[':
if (bracket(s))
return (next(s));
/* FALLTHROUGH */
default:
clen = mbrtowc(&wch, s->str, MB_LEN_MAX, NULL);
if (clen == (size_t)-1 || clen == (size_t)-2 ||
clen == 0)
errc(1, EILSEQ, NULL);
is_octal = 0;
s->lastch = wch;
s->str += clen;
break;
}
/* We can start a range at any time. */
if (s->str[0] == '-' && genrange(s, is_octal))
return (next(s));
return (1);
case RANGE:
if (s->cnt-- == 0) {
s->state = NORMAL;
return (next(s));
}
++s->lastch;
return (1);
case SEQUENCE:
if (s->cnt-- == 0) {
s->state = NORMAL;
return (next(s));
}
return (1);
case CCLASS:
case CCLASS_UPPER:
case CCLASS_LOWER:
s->cnt++;
ch = nextwctype(s->lastch, s->cclass);
if (ch == -1) {
s->state = NORMAL;
return (next(s));
}
s->lastch = ch;
return (1);
case SET:
if ((ch = s->set[s->cnt++]) == OOBCH) {
s->state = NORMAL;
return (next(s));
}
s->lastch = ch;
return (1);
default:
return (0);
}
/* NOTREACHED */
}
static int
bracket(STR *s)
{
char *p;
switch (s->str[1]) {
case ':': /* "[:class:]" */
if ((p = strchr(s->str + 2, ']')) == NULL)
return (0);
if (*(p - 1) != ':' || p - s->str < 4)
goto repeat;
*(p - 1) = '\0';
s->str += 2;
genclass(s);
s->str = p + 1;
return (1);
case '=': /* "[=equiv=]" */
if (s->str[2] == '\0' || (p = strchr(s->str + 3, ']')) == NULL)
return (0);
if (*(p - 1) != '=' || p - s->str < 4)
goto repeat;
s->str += 2;
genequiv(s);
return (1);
default: /* "[\###*n]" or "[#*n]" */
repeat:
if ((p = strpbrk(s->str + 2, "*]")) == NULL)
return (0);
if (p[0] != '*' || strchr(p, ']') == NULL)
return (0);
s->str += 1;
genseq(s);
return (1);
}
/* NOTREACHED */
}
static void
genclass(STR *s)
{
if ((s->cclass = wctype(s->str)) == 0)
errx(1, "unknown class %s", s->str);
s->cnt = 0;
s->lastch = -1; /* incremented before check in next() */
if (strcmp(s->str, "upper") == 0)
s->state = CCLASS_UPPER;
else if (strcmp(s->str, "lower") == 0)
s->state = CCLASS_LOWER;
else
s->state = CCLASS;
}
static void
genequiv(STR *s)
{
int i, p, pri;
char src[2], dst[3];
size_t clen;
wchar_t wc;
if (*s->str == '\\') {
s->equiv[0] = backslash(s, NULL);
if (*s->str != '=')
errx(1, "misplaced equivalence equals sign");
s->str += 2;
} else {
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0)
errc(1, EILSEQ, NULL);
s->equiv[0] = wc;
if (s->str[clen] != '=')
errx(1, "misplaced equivalence equals sign");
s->str += clen + 2;
}
/*
* Calculate the set of all characters in the same equivalence class
* as the specified character (they will have the same primary
* collation weights).
* XXX Knows too much about how strxfrm() is implemented. Assumes
* it fills the string with primary collation weight bytes. Only one-
* to-one mappings are supported.
* XXX Equivalence classes not supported in multibyte locales.
*/
src[0] = (char)s->equiv[0];
src[1] = '\0';
if (MB_CUR_MAX == 1 && strxfrm(dst, src, sizeof(dst)) == 1) {
pri = (unsigned char)*dst;
for (p = 1, i = 1; i < NCHARS_SB; i++) {
*src = i;
if (strxfrm(dst, src, sizeof(dst)) == 1 && pri &&
pri == (unsigned char)*dst)
s->equiv[p++] = i;
}
s->equiv[p] = OOBCH;
}
s->cnt = 0;
s->state = SET;
s->set = s->equiv;
}
static int
genrange(STR *s, int was_octal)
{
int stopval, octal;
char *savestart;
int n, cnt, *p;
size_t clen;
wchar_t wc;
octal = 0;
savestart = s->str;
if (*++s->str == '\\')
stopval = backslash(s, &octal);
else {
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
if (clen == (size_t)-1 || clen == (size_t)-2)
errc(1, EILSEQ, NULL);
stopval = wc;
s->str += clen;
}
/*
* XXX Characters are not ordered according to collating sequence in
* multibyte locales.
*/
if (octal || was_octal || MB_CUR_MAX > 1) {
if (stopval < s->lastch) {
s->str = savestart;
return (0);
}
s->cnt = stopval - s->lastch + 1;
s->state = RANGE;
--s->lastch;
return (1);
}
if (charcoll((const void *)&stopval, (const void *)&(s->lastch)) < 0) {
s->str = savestart;
return (0);
}
if ((s->set = p = malloc((NCHARS_SB + 1) * sizeof(int))) == NULL)
err(1, "genrange() malloc");
for (cnt = 0; cnt < NCHARS_SB; cnt++)
if (charcoll((const void *)&cnt, (const void *)&(s->lastch)) >= 0 &&
charcoll((const void *)&cnt, (const void *)&stopval) <= 0)
*p++ = cnt;
*p = OOBCH;
n = p - s->set;
s->cnt = 0;
s->state = SET;
if (n > 1)
mergesort(s->set, n, sizeof(*(s->set)), charcoll);
return (1);
}
static void
genseq(STR *s)
{
char *ep;
wchar_t wc;
size_t clen;
if (s->which == STRING1)
errx(1, "sequences only valid in string2");
if (*s->str == '\\')
s->lastch = backslash(s, NULL);
else {
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
if (clen == (size_t)-1 || clen == (size_t)-2)
errc(1, EILSEQ, NULL);
s->lastch = wc;
s->str += clen;
}
if (*s->str != '*')
errx(1, "misplaced sequence asterisk");
switch (*++s->str) {
case '\\':
s->cnt = backslash(s, NULL);
break;
case ']':
s->cnt = 0;
++s->str;
break;
default:
if (isdigit((u_char)*s->str)) {
s->cnt = strtol(s->str, &ep, 0);
if (*ep == ']') {
s->str = ep + 1;
break;
}
}
errx(1, "illegal sequence count");
/* NOTREACHED */
}
s->state = s->cnt ? SEQUENCE : INFINITE;
}
/*
* Translate \??? into a character. Up to 3 octal digits, if no digits either
* an escape code or a literal character.
*/
static int
backslash(STR *s, int *is_octal)
{
int ch, cnt, val;
if (is_octal != NULL)
*is_octal = 0;
for (cnt = val = 0;;) {
ch = (u_char)*++s->str;
if (!isdigit(ch) || ch > '7')
break;
val = val * 8 + ch - '0';
if (++cnt == 3) {
++s->str;
break;
}
}
if (cnt) {
if (is_octal != NULL)
*is_octal = 1;
return (val);
}
if (ch != '\0')
++s->str;
switch (ch) {
case 'a': /* escape characters */
return ('\7');
case 'b':
return ('\b');
case 'f':
return ('\f');
case 'n':
return ('\n');
case 'r':
return ('\r');
case 't':
return ('\t');
case 'v':
return ('\13');
case '\0': /* \" -> \ */
s->state = EOS;
return ('\\');
default: /* \x" -> x */
return (ch);
}
}