head	1.8;
access;
symbols
	netbsd-11-0-RC4:1.8
	netbsd-11-0-RC3:1.8
	netbsd-11-0-RC2:1.8
	netbsd-11-0-RC1:1.8
	perseant-exfatfs-base-20250801:1.8
	netbsd-11:1.8.0.6
	netbsd-11-base:1.8
	netbsd-10-1-RELEASE:1.8
	perseant-exfatfs-base-20240630:1.8
	perseant-exfatfs:1.8.0.4
	perseant-exfatfs-base:1.8
	netbsd-8-3-RELEASE:1.4
	netbsd-9-4-RELEASE:1.4
	netbsd-10-0-RELEASE:1.8
	netbsd-10-0-RC6:1.8
	netbsd-10-0-RC5:1.8
	netbsd-10-0-RC4:1.8
	netbsd-10-0-RC3:1.8
	netbsd-10-0-RC2:1.8
	netbsd-10-0-RC1:1.8
	netbsd-10:1.8.0.2
	netbsd-10-base:1.8
	netbsd-9-3-RELEASE:1.4
	cjep_sun2x-base1:1.6
	cjep_sun2x:1.6.0.4
	cjep_sun2x-base:1.6
	cjep_staticlib_x-base1:1.6
	netbsd-9-2-RELEASE:1.4
	cjep_staticlib_x:1.6.0.2
	cjep_staticlib_x-base:1.6
	netbsd-9-1-RELEASE:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	is-mlppp:1.4.0.18
	is-mlppp-base:1.4
	phil-wifi-20200406:1.4
	netbsd-8-2-RELEASE:1.4
	netbsd-9-0-RELEASE:1.4
	netbsd-9-0-RC2:1.4
	netbsd-9-0-RC1:1.4
	phil-wifi-20191119:1.4
	netbsd-9:1.4.0.16
	netbsd-9-base:1.4
	phil-wifi-20190609:1.4
	netbsd-8-1-RELEASE:1.4
	netbsd-8-1-RC1:1.4
	pgoyette-compat-merge-20190127:1.4
	pgoyette-compat-20190127:1.4
	pgoyette-compat-20190118:1.4
	pgoyette-compat-1226:1.4
	pgoyette-compat-1126:1.4
	pgoyette-compat-1020:1.4
	pgoyette-compat-0930:1.4
	pgoyette-compat-0906:1.4
	netbsd-7-2-RELEASE:1.1.12.2
	pgoyette-compat-0728:1.4
	netbsd-8-0-RELEASE:1.4
	phil-wifi:1.4.0.14
	phil-wifi-base:1.4
	pgoyette-compat-0625:1.4
	netbsd-8-0-RC2:1.4
	pgoyette-compat-0521:1.4
	pgoyette-compat-0502:1.4
	pgoyette-compat-0422:1.4
	netbsd-8-0-RC1:1.4
	pgoyette-compat-0415:1.4
	pgoyette-compat-0407:1.4
	pgoyette-compat-0330:1.4
	pgoyette-compat-0322:1.4
	pgoyette-compat-0315:1.4
	netbsd-7-1-2-RELEASE:1.1.12.2
	pgoyette-compat:1.4.0.12
	pgoyette-compat-base:1.4
	netbsd-7-1-1-RELEASE:1.1.12.2
	matt-nb8-mediatek:1.4.0.10
	matt-nb8-mediatek-base:1.4
	perseant-stdc-iso10646:1.4.0.8
	perseant-stdc-iso10646-base:1.4
	netbsd-8:1.4.0.6
	netbsd-8-base:1.4
	prg-localcount2-base3:1.4
	prg-localcount2-base2:1.4
	prg-localcount2-base1:1.4
	prg-localcount2:1.4.0.4
	prg-localcount2-base:1.4
	pgoyette-localcount-20170426:1.4
	bouyer-socketcan-base1:1.4
	pgoyette-localcount-20170320:1.4
	netbsd-7-1:1.1.12.2.0.6
	netbsd-7-1-RELEASE:1.1.12.2
	netbsd-7-1-RC2:1.1.12.2
	netbsd-7-nhusb-base-20170116:1.1.12.2
	bouyer-socketcan:1.4.0.2
	bouyer-socketcan-base:1.4
	pgoyette-localcount-20170107:1.4
	netbsd-7-1-RC1:1.1.12.2
	pgoyette-localcount-20161104:1.4
	netbsd-7-0-2-RELEASE:1.1.12.2
	localcount-20160914:1.4
	netbsd-7-nhusb:1.1.12.2.0.4
	netbsd-7-nhusb-base:1.1.12.2
	pgoyette-localcount-20160806:1.3
	pgoyette-localcount-20160726:1.3
	pgoyette-localcount:1.3.0.2
	pgoyette-localcount-base:1.3
	netbsd-7-0-1-RELEASE:1.1.12.2
	netbsd-7-0:1.1.12.2.0.2
	netbsd-7-0-RELEASE:1.1.12.2
	netbsd-7-0-RC3:1.1.12.2
	netbsd-7-0-RC2:1.1.12.2
	netbsd-7-0-RC1:1.1.12.2
	netbsd-7:1.1.0.12
	netbsd-7-base:1.1
	yamt-pagecache-base9:1.1
	yamt-pagecache-tag8:1.1.2.2
	tls-earlyentropy:1.1.0.10
	tls-earlyentropy-base:1.1
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1
	riastradh-drm2-base3:1.1
	riastradh-drm2-base2:1.1
	riastradh-drm2-base1:1.1
	riastradh-drm2:1.1.0.4
	riastradh-drm2-base:1.1
	agc-symver:1.1.0.8
	agc-symver-base:1.1
	tls-maxphys-base:1.1
	tls-maxphys:1.1.0.6
	yamt-pagecache:1.1.0.2
	yamt-pagecache-base8:1.1;
locks; strict;
comment	@# @;


1.8
date	2021.08.08.10.48.35;	author martin;	state Exp;
branches;
next	1.7;
commitid	dpq93hvM4HO8M94D;

1.7
date	2021.08.05.12.52.47;	author kre;	state Exp;
branches;
next	1.6;
commitid	gV37a7mNu8yExM3D;

1.6
date	2021.01.09.16.25.19;	author mlelstv;	state Exp;
branches;
next	1.5;
commitid	negqxUFolVXgq4DC;

1.5
date	2021.01.09.14.00.05;	author mlelstv;	state Exp;
branches;
next	1.4;
commitid	0raHY28SmAujC3DC;

1.4
date	2016.08.15.12.54.40;	author mlelstv;	state Exp;
branches;
next	1.3;
commitid	9onBNtk6K6QyWoiz;

1.3
date	2015.03.01.14.21.17;	author mlelstv;	state Exp;
branches;
next	1.2;
commitid	u4HqH7xXVP1ryUby;

1.2
date	2015.02.15.15.56.30;	author jmcneill;	state Exp;
branches;
next	1.1;
commitid	c42v2ueVTHS6w7ay;

1.1
date	2013.01.11.23.49.23;	author mlelstv;	state Exp;
branches
	1.1.2.1
	1.1.6.1
	1.1.12.1;
next	;

1.1.2.1
date	2013.01.11.23.49.23;	author yamt;	state dead;
branches;
next	1.1.2.2;

1.1.2.2
date	2013.01.23.00.05.28;	author yamt;	state Exp;
branches;
next	;

1.1.6.1
date	2013.01.11.23.49.23;	author tls;	state dead;
branches;
next	1.1.6.2;

1.1.6.2
date	2013.02.25.00.28.04;	author tls;	state Exp;
branches;
next	;

1.1.12.1
date	2015.02.17.14.45.31;	author martin;	state Exp;
branches;
next	1.1.12.2;
commitid	wcriglz21o1P4nay;

1.1.12.2
date	2015.03.07.06.00.01;	author snj;	state Exp;
branches;
next	;
commitid	YEpkHoHJCBVDADcy;


desc
@@


1.8
log
@Fix obvious editor mishap in previous
@
text
@#!/bin/sh
#
# $NetBSD: 02-wedgenames,v 1.7 2021/08/05 12:52:47 kre Exp $
#
# Try to maintain symlinks to wedge devices
#

export LC_ALL=C

event="$1"
shift

wedgedir=/dev/wedges

recurse()
{
	test -d "$1" &&
	    ls -1af "$1" | while read n; do
		case $n in
		.|..)	;;
		*)
			echo "$1/$n"
			if [ -L "$1/$n" ]; then
				: #nothing
			elif [ -d "$1/$n" ]; then
				recurse "$1/$n"
			fi
			;;
		esac
	    done
}

simple_readlink()
{
	local x

	x=$(test -e "$1" && ls -ld "$1")
	case $x in
	*'-> '*) echo ${x#*-> };;
	esac
}

#ordtable=$(
#	for n1 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
#	for n2 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
#		echo "\$'\x$n1$n2') x=$n1$n2;;"
#	done
#	done
#)
#
#ord()
#{
#	local x
#	eval "case \$1 in $ordtable esac"
#	echo -n $x
#}

ord()
{
	printf %2.2x "'$1"
}

encode()
{
	local a b c

	a=$1
	b=
	while [ -n "$a" ]; do
		c="${a%"${a#?}"}"
		a=${a#?}
		case $c in
		[][:alnum:]._:\;!^$\&~\(\)[{}=,+/-])
			;;
		*)
			c=%%$(ord "$c")
			;;
		esac
		b=${b}${c}
	done
	printf %s "$b"
}

remove_wedge() {
	recurse "$wedgedir" | while read w; do
		t=$(simple_readlink "$w")
		if [ x"$t" = x"/dev/$1" ]; then
			rm -f "$w"
			basedir=${w%/*}
			rmdir -p "$basedir" 2>/dev/null
		fi
	done
}

wedge_label() {
	local l

	# dkctl getwedgeinfo always outputs 2 "lines", the first
	# contains the label (and may contain embedded \n chars)
	# the second contains the size, offset, and type, and one
	# trailing \n (stripped by the $()) - so we can safely
	# extract the label by deleting from the final \n in the
	# value getwedgeinfo prints to the end

	l=$(dkctl "$1" getwedgeinfo)
	l=${l%$'\n'*}
	case "${l}" in
	$1' at '*': '*)
		l=${l#*: }
		;;
	*)	
		l=$1
		;;
	esac

	# The trailing <END> is to ensure a trailing \n in the label
	# is not deleted by a command substitution which invokes us.
	# That will be rmeoved by the caller.
	printf %s "${l}<END>"
}

add_wedge() {
	local l n

	l=$(wedge_label "$1")
	l=${l%'<END>'}
	case "$l" in */) l="${l}Wedge";; esac

	n=$(encode "${l}")

	(
		umask 022

		test -d "$wedgedir" || mkdir -m 755 "$wedgedir"
		basedir="$wedgedir/$n"
		basedir=${basedir%/*}
		test -d "$basedir" || mkdir -p -m 755 "$basedir"
		if oldlink=$(simple_readlink "$wedgedir/$n"); then
			if [ x"$oldlink" != x"/dev/$1" ]; then
				rm -f "$wedgedir/$n"
				ln -s "/dev/$1" "$wedgedir/$n"
			fi
		else
			ln -s "/dev/$1" "$wedgedir/$n"
		fi
	)
}

for device do
	case $device in
	dk*)
		case $event in
		device-attach)
			remove_wedge "$device"
			add_wedge "$device"
			;;
		device-detach)
			remove_wedge "$device"
			;;
		esac
		;;
	esac
done
@


1.7
log
@
Obliterate bogus $@@ usage.

While here, fix some quoting, change some style, and attempt
to properly handle wedge names with embedded newlines, and those
that end with a '/' character (not particularly happy with the
solution to that last one, but it is better than it was).

Is there a reason that characters that need encoding in wedge names
(white space, and more) are encoded as %%XX (XX is the hex value of
the char - but 2 % chars?  Why?).   That remains unchanged, but as
the script already relied upon sh's $'...' quoting, I think we can rely
upon printf as well, so replace the old (very elegant, but slow) encoding
function with a much simpler one (does the same thing).
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.6 2021/01/09 16:25:19 mlelstv Exp $
d163 1
a163 1
ce-attach
@


1.6
log
@Also replace calls to dirname.
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.5 2021/01/09 14:00:05 mlelstv Exp $
a11 1
devices=$@@
d17 2
a18 2
	test -d "$1" && ls -1af "$1" \
	| while read n; do
d20 1
a20 1
		.|..) ;;
d30 1
a30 1
	done
d43 14
a56 7
ordtable=$(
	for n1 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
	for n2 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
		echo "\$'\x$n1$n2') x=$n1$n2;;"
	done
	done
)
d60 1
a60 3
	local x
	eval "case \$1 in $ordtable esac"
	echo -n $x
d65 1
a65 1
	local a
d68 1
d73 1
a73 1
		[[:alnum:]._:\;!^$\&~\(\)[\]{}=,+\-/])
d76 1
a76 1
			c='%%'$(ord "$c")
d79 1
a79 1
		echo -n "$c"
d81 1
d85 1
a85 2
	recurse "$wedgedir" \
	| while read w; do
d95 27
d123 18
a140 16
	dkctl "$1" getwedgeinfo \
	| while read l; do
		case $l in
		*': '*)
			n="${l#*: }"
			n=$(encode "$n")
			test -d $wedgedir || mkdir -m 755 $wedgedir
			basedir="$wedgedir/$n"
			basedir=${basedir%/*}
			test -d "$basedir" || mkdir -p -m 755 "$basedir"
			if oldlink=$(simple_readlink "$wedgedir/$n"); then
				if [ x"$oldlink" != x"/dev/$1" ]; then
					rm -f "$wedgedir/$n"
					ln -s "/dev/$1" "$wedgedir/$n"
				fi
			else
d143 4
a146 4
			;;
		esac
		break
	done
d149 1
a149 1
for device in $devices; do
d154 2
a155 2
			remove_wedge $device
			add_wedge $device
d158 1
a158 1
			remove_wedge $device
d163 1
a163 1
done
@


1.5
log
@Remove need for awk/sed/readlink tools.
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.4 2016/08/15 12:54:40 mlelstv Exp $
d84 1
a84 1
			basedir=$(dirname "$w")
d98 2
a99 1
			basedir=$(dirname "$wedgedir/$n")
@


1.4
log
@Avoid error message when /dev/wedges doesn't exist yet.
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.3 2015/03/01 14:21:17 mlelstv Exp $
d8 2
d16 62
d79 1
a79 2
	test -d $wedgedir && find $wedgedir -print \
	| sed -e 's# #\\ #g' \
d81 1
a81 1
		t=$(readlink "$w")
d91 15
a105 30
	n=$(dkctl "$1" getwedgeinfo \
		| sed -ne '1s#^[^:]*: ##p' \
		| awk -v GOOD='._:;!^$&~()[]{}=,+-/' '
		BEGIN {
			for (i=0; i<256; ++i)
				ord[sprintf("%c",i)] = i
		}
		{
			n = length($0)
			o = ""
			for (i=1; i<=n; ++i) {
				c = substr($0,i,1)
				if (c ~ /^[[:alnum:]]$/ || index(GOOD,c) > 0) {
					o = o c
				} else {
					o = o sprintf("%%%02x",ord[c])
				}
			}
			printf "%s",o
		}'
	)
	case $n in
	"") ;;
	*)
		test -d $wedgedir || mkdir -m 755 $wedgedir
		basedir=$(dirname "$wedgedir/$n")
		test -d "$basedir" || mkdir -p -m 755 "$basedir"
		if oldlink=$(readlink "$wedgedir/$n"); then
			if [ x"$oldlink" != "/dev/$1" ]; then
				rm -f "$wedgedir/$n"
d108 4
a111 5
		else
			ln -s "/dev/$1" "$wedgedir/$n"
		fi
		;;
	esac
@


1.3
log
@safely update existing wedge links
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.2 2015/02/15 15:56:30 jmcneill Exp $
d15 1
a15 1
	find $wedgedir -print \
@


1.2
log
@At startup, instead of doing run-hooks for each device, call run-hooks
once with a list of all found devices. This lets us batch calls to MAKEDEV
which results in a noticeable improvement in Raspberry Pi boot time.

Run the initial device enumeration hooks before detaching from the foreground,
ensuring that any required devices have been created before the rc.d script
exits.
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.1 2013/01/11 23:49:23 mlelstv Exp $
d55 8
a62 1
		ln -s "/dev/$1" "$wedgedir/$n"
@


1.1
log
@Provide a script for devpubd(8) that creates symlinks for each dk(4)
device under /dev/wedges so that you can access them by name.
@
text
@d3 1
a3 1
# $NetBSD: $
d9 2
a10 1
device="$2"
d19 1
a19 1
		if [ x"$t" = x"/dev/$device" ]; then
d28 1
a28 1
	n=$(dkctl "$device" getwedgeinfo \
d55 1
a55 1
		ln -s "/dev/$device" "$wedgedir/$n"
d60 12
a71 9
case $device in
dk*)
	case $event in
	device-attach)
		remove_wedge
		add_wedge
		;;
	device-detach)
		remove_wedge
d74 1
a74 2
	;;
esac
@


1.1.12.1
log
@Pull up following revision(s) (requested by jmcneill in ticket #525):
	sbin/devpubd/hooks/01-makedev: revision 1.2
	sbin/devpubd/devpubd-run-hooks.in: revision 1.3
	sbin/devpubd/Makefile: revision 1.5
	sbin/devpubd/hooks/02-wedgenames: revision 1.2
	sbin/devpubd/devpubd.c: revision 1.3
	sbin/devpubd/devpubd.c: revision 1.4
At startup, instead of doing run-hooks for each device, call run-hooks
once with a list of all found devices. This lets us batch calls to MAKEDEV
which results in a noticeable improvement in Raspberry Pi boot time.
Run the initial device enumeration hooks before detaching from the foreground,
ensuring that any required devices have been created before the rc.d script
exits.
let's make this compile again.
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.1 2013/01/11 23:49:23 mlelstv Exp $
d9 1
a9 2
shift
devices=$@@
d18 1
a18 1
		if [ x"$t" = x"/dev/$1" ]; then
d27 1
a27 1
	n=$(dkctl "$1" getwedgeinfo \
d54 1
a54 1
		ln -s "/dev/$1" "$wedgedir/$n"
d59 9
a67 12
for device in $devices; do
	case $device in
	dk*)
		case $event in
		device-attach)
			remove_wedge $device
			add_wedge $device
			;;
		device-detach)
			remove_wedge $device
			;;
		esac
d70 2
a71 1
done
@


1.1.12.2
log
@Pull up following revision(s) (requested by mlelstv in ticket #561):
	sbin/devpubd/hooks/02-wedgenames: revision 1.3
safely update existing wedge links
@
text
@d3 1
a3 1
# $NetBSD: 02-wedgenames,v 1.1.12.1 2015/02/17 14:45:31 martin Exp $
d55 1
a55 8
		if oldlink=$(readlink "$wedgedir/$n"); then
			if [ x"$oldlink" != "/dev/$1" ]; then
				rm -f "$wedgedir/$n"
				ln -s "/dev/$1" "$wedgedir/$n"
			fi
		else
			ln -s "/dev/$1" "$wedgedir/$n"
		fi
@


1.1.6.1
log
@file 02-wedgenames was added on branch tls-maxphys on 2013-02-25 00:28:04 +0000
@
text
@d1 71
@


1.1.6.2
log
@resync with head
@
text
@a0 71
#!/bin/sh
#
# $NetBSD$
#
# Try to maintain symlinks to wedge devices
#

event="$1"
device="$2"

wedgedir=/dev/wedges

remove_wedge() {
	find $wedgedir -print \
	| sed -e 's# #\\ #g' \
	| while read w; do
		t=$(readlink "$w")
		if [ x"$t" = x"/dev/$device" ]; then
			rm -f "$w"
			basedir=$(dirname "$w")
			rmdir -p "$basedir" 2>/dev/null
		fi
	done
}

add_wedge() {
	n=$(dkctl "$device" getwedgeinfo \
		| sed -ne '1s#^[^:]*: ##p' \
		| awk -v GOOD='._:;!^$&~()[]{}=,+-/' '
		BEGIN {
			for (i=0; i<256; ++i)
				ord[sprintf("%c",i)] = i
		}
		{
			n = length($0)
			o = ""
			for (i=1; i<=n; ++i) {
				c = substr($0,i,1)
				if (c ~ /^[[:alnum:]]$/ || index(GOOD,c) > 0) {
					o = o c
				} else {
					o = o sprintf("%%%02x",ord[c])
				}
			}
			printf "%s",o
		}'
	)
	case $n in
	"") ;;
	*)
		test -d $wedgedir || mkdir -m 755 $wedgedir
		basedir=$(dirname "$wedgedir/$n")
		test -d "$basedir" || mkdir -p -m 755 "$basedir"
		ln -s "/dev/$device" "$wedgedir/$n"
		;;
	esac
}

case $device in
dk*)
	case $event in
	device-attach)
		remove_wedge
		add_wedge
		;;
	device-detach)
		remove_wedge
		;;
	esac
	;;
esac
@


1.1.2.1
log
@file 02-wedgenames was added on branch yamt-pagecache on 2013-01-23 00:05:28 +0000
@
text
@d1 71
@


1.1.2.2
log
@sync with head
@
text
@a0 71
#!/bin/sh
#
# $NetBSD$
#
# Try to maintain symlinks to wedge devices
#

event="$1"
device="$2"

wedgedir=/dev/wedges

remove_wedge() {
	find $wedgedir -print \
	| sed -e 's# #\\ #g' \
	| while read w; do
		t=$(readlink "$w")
		if [ x"$t" = x"/dev/$device" ]; then
			rm -f "$w"
			basedir=$(dirname "$w")
			rmdir -p "$basedir" 2>/dev/null
		fi
	done
}

add_wedge() {
	n=$(dkctl "$device" getwedgeinfo \
		| sed -ne '1s#^[^:]*: ##p' \
		| awk -v GOOD='._:;!^$&~()[]{}=,+-/' '
		BEGIN {
			for (i=0; i<256; ++i)
				ord[sprintf("%c",i)] = i
		}
		{
			n = length($0)
			o = ""
			for (i=1; i<=n; ++i) {
				c = substr($0,i,1)
				if (c ~ /^[[:alnum:]]$/ || index(GOOD,c) > 0) {
					o = o c
				} else {
					o = o sprintf("%%%02x",ord[c])
				}
			}
			printf "%s",o
		}'
	)
	case $n in
	"") ;;
	*)
		test -d $wedgedir || mkdir -m 755 $wedgedir
		basedir=$(dirname "$wedgedir/$n")
		test -d "$basedir" || mkdir -p -m 755 "$basedir"
		ln -s "/dev/$device" "$wedgedir/$n"
		;;
	esac
}

case $device in
dk*)
	case $event in
	device-attach)
		remove_wedge
		add_wedge
		;;
	device-detach)
		remove_wedge
		;;
	esac
	;;
esac
@


