MAKEDEV.sub revision 1.1
1dnl	$OpenBSD: MAKEDEV.sub,v 1.1 2002/01/04 09:25:35 todd Exp $
2dnl
3dnl Copyright (c) 2001 Todd T. Fries <todd@OpenBSD.org>
4dnl All rights reserved.
5dnl
6dnl Redistribution and use in source and binary forms, with or without
7dnl modification, are permitted provided that the following conditions
8dnl are met:
9dnl 1. Redistributions of source code must retain the above copyright
10dnl    notice, this list of conditions and the following disclaimer.
11dnl 2. The name of the author may not be used to endorse or promote products
12dnl    derived from this software without specific prior written permission.
13dnl
14dnl THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15dnl INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16dnl AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
17dnl THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18dnl EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19dnl PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20dnl OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21dnl WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22dnl OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23dnl ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24dnl
25dnl
26dnl This file is an m4 file
27dnl
28dnl Conventions:
29dnl
30dnl o First, a change of quote to make shell statements like: `command`
31dnl   possible
32dnl
33changequote(`{-',`-}')dnl
34dnl
35dnl o version info must be stripped of $ so we can say 'generated from' below
36dnl
37dnl  If we just use the version string how are we going to know what arch
38dnl  'MAKEDEV.md,v' came from?
39dnl
40dnl  Thus, I have used the below to create a version string looking like
41dnl  'OpenBSD: etc.sparc/MAKEDEV.inc,v...' which works, although requires
42dnl  some attention if ported to another Id string setup.
43dnl
44dnl
45dnl Initialize the stacks stored in _m4_*
46dnl
47undefine({-_m4_cvs_ver-})dnl
48pushdef({-_m4_cvs_ver-},{-done-})dnl
49dnl
50undefine({-_m4_devs-})dnl
51pushdef({-_m4_devs-},{-done-})dnl
52dnl
53undefine({-_m4_disks-})dnl
54undefine({-_m4_disks2-})dnl
55pushdef({-_m4_disks-},{-done-})dnl
56pushdef({-_m4_disks2-},{-done-})dnl
57dnl
58dnl define stack 'add to' functions, only unique names queued
59dnl
60define({-ver_q-},{-ifelse(index($1_ver,y),0,,
61{-pushdef({-_m4_cvs_ver-},{-$2-})define({-$1_ver-},{-y-})-})-})dnl ver_q
62dnl
63define({-dev_q-},{-ifelse(index(_q_$1_dev,:),0,
64{-errprint({-duplicated dev id: $1 at -}__file__{-:-}__line__ originally at _q_$1_dev)-},
65{-pushdef({-_m4_devs-},{-$1-})dnl
66define({-_q_$1_dev-},{-:-}__file__{-:-}__line__)-})-})dnl dev_q
67dnl
68define({-disk_q-},{-ifelse(index(_q_$1_disk,:),0,
69{-errprint({-duplicated disk id: $1 at -}__file__{-:-}__line__ originally at _q_$1_disk)-},{-pushdef({-_m4_disks-},{-$1-})dnl
70pushdef({-_m4_disks2-},{-$1-})dnl
71define({-_q_$1_disks-},{-:-}__file__{-:-}__line__)-})-})dnl disk_q
72dnl
73dnl store a version string for 'this' file
74dnl
75dnl vers ( uniqueidstring, versionstring, subdir )
76dnl
77dnl example1: vers(__file__,{-$OpenBSD: MAKEDEV.sub,v 1.1 2002/01/04 09:25:35 todd Exp $-})
78dnl example2: vers(__file__,{-$OpenBSD: MAKEDEV.sub,v 1.1 2002/01/04 09:25:35 todd Exp $-},etc.MACHINE)
79dnl
80dnl if subdir is defined, prepend it to the filename in the version string
81dnl
82define({-vers-},
83{-ifelse({-$3-},{--},
84{-ver_q(hstcl({-$1-}),{-translit({-{-$2-}-},{-$-},{--})-})-},
85{-ver_q(hstcl({-$1-}),{-_addsubdir({-{-$2-}-},$3)-})-})-})dnl
86dnl
87dnl Version info for this file:
88dnl
89vers(__file__,{-$OpenBSD: MAKEDEV.sub,v 1.1 2002/01/04 09:25:35 todd Exp $-})dnl
90dnl
91dnl
92define({-_addsubdir-},
93{-patsubst({-$1-},{-\$(OpenBSD:) ([^\$]*)\$-},{-\1 $2/\2-})-})dnl
94dnl
95dnl do the 'showing' of the version info
96dnl
97define({-do_vers-},{-#	$1-})dnl
98dnl
99dnl show version function, to be called at the place when all versions are
100dnl   queued, and it is time to show all of them
101dnl
102define({-show_vers-},
103{-ifelse(_m4_cvs_ver,{-done-},{--},
104{-do_vers(_m4_cvs_ver)
105popdef({-_m4_cvs_ver-})dnl
106show_vers()dnl
107-})-})dnl
108dnl
109dnl show the routines to generate devices
110define({-show_devs-},
111{-ifelse(_m4_devs,{-done-},{--},
112{-_MKDEV(_m4_devs){--}dnl
113popdef({-_m4_devs-})dnl
114show_devs()dnl
115-})-})dnl
116dnl
117dnl routines to generate disks
118define({-show_disks-},
119{-ifelse(_m4_disks,{-done-},{--},
120{-ifcase(_m4_disks,_m4_disks{--}*)dnl
121popdef({-_m4_disks-})dnl
122show_disks()dnl
123-})-})dnl
124dnl
125define({-show_disks2-},
126{-ifelse(_m4_disks2,{-done-},{--},
127{-CasE(_m4_disks2)dnl
128popdef({-_m4_disks2-})dnl
129show_disks2()dnl
130-})-})dnl
131dnl
132dnl
133dnl Some m4 math functions:
134dnl
135dnl   Add(a,b)           - displays the result of a+b
136dnl   Mult(a,b)          - displays the result of a*b
137dnl   trunc a b          - displays the string a minus b removed from the RHS
138dnl   hex a              - displays the hex equivalent of 0-15
139dnl   unt a              - s/[a-z]*([0-9]*).*/\1/ aka sd0a -> 0
140dnl
141dnl Functions:
142dnl
143dnl --> Addition
144dnl
145define({-Add-},{-$({-(-}$1+$2{-)-})-})dnl
146dnl
147dnl --> Multiplication
148dnl
149define({-Mult-},{-$({-(-}$1*$2{-)-})-})dnl
150dnl
151dnl
152dnl TRUNC
153dnl
154define({-expr_trunc-},{-$1trunc()
155$1{
156$1	case {-$-}3 in
157$1	l)   {-expr-} {-$-}1 : '\(.*\)'{-$-}2 ;;
158$1	r|*) {-expr-} ${--}1 : ${--}2'\(.*\)' ;;
159$1	esac
160$1}-})dnl
161dnl
162dnl
163define({-_SHELL-},{-sh-})dnl
164define({-_this-},{-{-$-}T-})dnl
165dnl define({-_recurse-},{-_SHELL _this-})dnl
166define({-_recurse-},{-R-})dnl
167dnl
168dnl _devitem(pattern,description)
169dnl
170define({-_devitem-},
171{-{-#-}	$1	{-$2-}-})dnl
172dnl
173dnl _devtitle(description)
174dnl
175define({-_devtitle-},{-{-#-} $1:-})dnl
176dnl
177dnl _DEV(name,[character major],[block major])
178dnl
179define({-_DEV-},{-$1_dev{--}dnl
180dnl
181dnl _DEV 'ifelse' .. $2 - major_$1_c
182dnl
183ifelse($2,,,{-define(major_$1_c,$2)-})dnl
184dnl
185dnl _DEV 'ifelse' .. $4 - major_$1_b
186dnl
187ifelse($3,,,{-define(major_$1_b,$3)-})dnl
188dnl
189dnl Some magic here, defining a devitem also defines another
190dnl string so that later we can check if a particular devitem was
191dnl defined, and thus determine if the devices themselves are to
192dnl be built
193dnl
194define({-$1__DEV-},{-Y-})dnl
195dnl
196dnl More magic, put device string name into a queue of script segments to
197dnl be shown later
198dnl
199ifdef({-$1_mkdev-},{-__mkdev({-$1-})-})dnl
200dnl
201dnl
202-})dnl _DEV
203define({-ifdev-},{-ifelse($1__DEV,Y,{-$2-})-})dnl
204dnl
205define({-_MKDEV-},{-$1_mkdev-})dnl
206define({-_TITLE-},{-$1_title-})dnl
207define({-__devitem-},{-define($1_dev,{-_devitem($2,$3)-})$4-})dnl
208define({-__devtitle-},{-define($1_title,{-_devtitle($2)-})-})dnl
209dnl
210dnl Beginning and ending of case entries, just incase we change in the
211dnl future, save chasing things down again
212dnl
213define({-_end-},{-
214	;;
215
216-})dnl
217define({-_beg-},{-{-)-}
218	-})dnl
219dnl
220dnl  create the script segment for making devices
221dnl             $1     $2      $3
222dnl _mkdev(shortname,devpatt,action)dnl
223define({-_mkdev-},{-define($1_mkdev,{-$2{--}_beg{--}$3{--}_end-})-})dnl
224dnl
225dnl  define a major character device
226dnl             $1     $2      $3      $4
227dnl _mcdev(shortname,devpatt,devbase,devmajor)dnl
228dnl
229define({-_mcdev-},{-define($1_mkdev,{-{-$2-}_beg{--}M $3$U c $4 $U{--}dnl
230ifelse($6,,ifelse($5,,,{- -}$5),{- -}ifelse($5,,600,$5){- -}$6){--}_end-})-})dnl
231dnl
232dnl         $1        $2      $3        $4       $5      $6
233dnl _cdev(shortname,devpatt,devmajor,devminor[,devmod,devgrp])dnl
234define({-_cdev-},
235{-dnl
236define($1_mkdev,{-$2{--}_beg{--}M $2 c $3 $4 $5 $6{--}_end-}){--}-})dnl
237dnl
238dnl
239define({-__mkdev-},{-dev_q($1)-})dnl
240dnl
241dnl for showing disks
242dnl
243define({-CasE-},
244{-ifdev({-$1-},
245{-	$1*) n=$1 b=major_$1_b c=major_$1_c;;
246-})-})dnl
247dnl
248dnl
249define({-ifcase-},{-dnl
250ifelse(C_ase,Y,ifdev({-$1-},{-|$2-}),
251{-ifdev({-$1-},{-$2define({-C_ase-},Y)-})-})-})dnl
252dnl
253dnl
254dnl device list .. list devices 'iff' they are defined
255dnl
256dnl input:
257dnl
258dnl    _dl({-usb-},{-usb0 usb1-},{-urio-},{-urio-},...)
259dnl
260dnl output:
261dnl
262dnl {-<tab>_recurse usb0 usb1 urio
263dnl   <tab>_recurse uhid0 uhid2 uhid3-}
264dnl
265dnl  wrap if more than 60 chars wide
266dnl
267dnl .. wrap it all up in an 'ifelse({-$1-},,{- ... -})' for neatness ??
268dnl
269define({-_dl-},{-dnl
270ifdef({-_dt-},,{-define({-_dt-})-})dnl
271ifdef({-_do-},,{-_dt{--}_recurse{--}define({-_do-},0)dnl
272define({-_dt-},{-	-})-})dnl
273ifdef({-$1__DEV-},
274{-define({-_di-},{-$2-})-},
275{-define({-_di-})-})dnl
276ifelse(eval(len(_di)+_do<60),1,
277{-ifelse(eval(len(_di)>0),1,
278{- _di{--}define({-_do-},eval(1+_do+len(_di)))-})-},
279{-
280_dt{--}_recurse _di{--}dnl
281define({-_do-},len(_di))-})dnl
282ifelse({-$3-},{--},
283{-undefine({-_dt-},{-_do-})-},dnl <--- The END
284{-_dl(shift(shift($@)))-})-})dnl
285dnl
286dnl A word about the above:
287dnl
288dnl _dt -> 'tab' .. at first, defined to nothing, as the tab is already there
289dnl        then define it to a tab every time through
290dnl        undefine it at the end
291dnl
292dnl _do -> 'old' count .. stores the length of the old string already displayed
293dnl        it is initialized to 0, added to the length plus 1 of '_di' each
294dnl        iteration the line is less than 60 chars long
295dnl	   undefined at the end
296dnl _di -> 'iteration' string .. the string used in this iteration, is empty if
297dnl        the device does not exist
298dnl
299