1dnl $MirOS: src/etc/MAKEDEV.sub,v 1.4 2005/07/24 12:49:10 tg Exp $ 2dnl $OpenBSD: MAKEDEV.sub,v 1.14 2005/02/07 06:14:18 david Exp $ 3dnl 4dnl Copyright (c) 2001-2004 Todd T. Fries <todd@OpenBSD.org> 5dnl 6dnl Permission to use, copy, modify, and distribute this software for any 7dnl purpose with or without fee is hereby granted, provided that the above 8dnl copyright notice and this permission notice appear in all copies. 9dnl 10dnl THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11dnl WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12dnl MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13dnl ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14dnl WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15dnl ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16dnl OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17dnl 18dnl 19dnl This file is an m4 file 20dnl 21dnl Conventions: 22dnl 23dnl o First, a change of quote to make shell statements like: `command` 24dnl possible 25dnl 26changequote(`{-', `-}')dnl 27dnl 28dnl o Next, change the comment character to make shell parameter 29dnl substitution possible 30dnl 31changecom(`/*', `*/')dnl 32dnl 33dnl o version info must be stripped of $ so we can say 'generated from' below 34dnl 35dnl If we just use the version string how are we going to know what arch 36dnl 'MAKEDEV.md,v' came from? 37dnl 38dnl Thus, I have used the below to create a version string looking like 39dnl 'OpenBSD: etc.sparc/MAKEDEV.inc,v...' which works, although requires 40dnl some attention if ported to another Id string setup. 41dnl 42dnl 43dnl Initialize the stacks stored in _m4_* 44dnl 45undefine({-_m4_cvs_ver-})dnl 46pushdef({-_m4_cvs_ver-}, {-done-})dnl 47dnl 48undefine({-_m4_devs-})dnl 49pushdef({-_m4_devs-}, {-done-})dnl 50dnl 51undefine({-_m4_disks-})dnl 52undefine({-_m4_disks2-})dnl 53pushdef({-_m4_disks-}, {-done-})dnl 54pushdef({-_m4_disks2-}, {-done-})dnl 55dnl 56dnl define stack 'add to' functions, only unique names queued 57dnl 58define({-ver_q-}, {-ifelse(index($1_ver, y), 0, , 59{-pushdef({-_m4_cvs_ver-}, {-$2-})define({-$1_ver-}, {-y-})-})-})dnl ver_q 60dnl 61define({-dev_q-}, {-ifelse(index(_q_$1_dev, :), 0, 62{-errprint({-duplicated dev id: $1 at -}__file__{-:-}__line__ originally at _q_$1_dev)-}, 63{-pushdef({-_m4_devs-}, {-$1-})dnl 64define({-_q_$1_dev-}, {-:-}__file__{-:-}__line__)-})-})dnl dev_q 65dnl 66define({-disk_q-}, {-ifelse(index(_q_$1_disk, :), 0, 67{-errprint({-duplicated disk id: $1 at -}__file__{-:-}__line__ originally at _q_$1_disk)-}, {-pushdef({-_m4_disks-}, {-$1-})dnl 68pushdef({-_m4_disks2-}, {-$1-})dnl 69define({-_q_$1_disks-}, {-:-}__file__{-:-}__line__)-})-})dnl disk_q 70dnl 71dnl store a version string for 'this' file 72dnl 73dnl vers ( uniqueidstring, versionstring, subdir ) 74dnl 75dnl example1: vers(__file__, {-$MirOS: src/etc/MAKEDEV.sub,v 1.4 2005/07/24 12:49:10 tg Exp $-}) 76dnl example2: vers(__file__, {-$MirOS: src/etc/MAKEDEV.sub,v 1.4 2005/07/24 12:49:10 tg Exp $-}, etc.MACHINE) 77dnl 78dnl if subdir is defined, prepend it to the filename in the version string 79dnl 80define({-vers-}, 81{-ifelse({-$3-}, {--}, 82{-ver_q(hstcl({-$1-}), {-translit({-{-$2-}-}, {-$-}, {--})-})-}, 83{-ver_q(hstcl({-$1-}), {-_addsubdir({-{-$2-}-}, $3)-})-})-})dnl 84dnl 85dnl Version info for this file: 86dnl 87vers(__file__, {-$MirOS: src/etc/MAKEDEV.sub,v 1.4 2005/07/24 12:49:10 tg Exp $-})dnl 88vers(__file__, {-$OpenBSD: MAKEDEV.sub,v 1.14 2005/02/07 06:14:18 david Exp $-})dnl 89dnl 90dnl 91define({-_addsubdir-}, 92{-patsubst({-$1-}, {-\$(OpenBSD:) ([^\$]*)\$-}, {-\1 $2/\2-})-})dnl 93dnl 94dnl do the 'showing' of the version info 95dnl 96define({-do_vers-}, {-COMM $1-})dnl 97dnl 98dnl show version function, to be called at the place when all versions are 99dnl queued, and it is time to show all of them 100dnl 101define({-show_vers-}, 102{-ifelse(_m4_cvs_ver, {-done-}, {--}, 103{-do_vers(_m4_cvs_ver) 104popdef({-_m4_cvs_ver-})dnl 105show_vers()dnl 106-})-})dnl 107dnl 108dnl show the routines to generate devices 109define({-show_devs-}, 110{-ifelse(_m4_devs, {-done-}, {--}, 111{-_MKDEV(_m4_devs){--}dnl 112popdef({-_m4_devs-})dnl 113show_devs()dnl 114-})-})dnl 115dnl 116dnl routines to generate disks 117define({-show_disks-}, 118{-ifelse(_m4_disks, {-done-}, {--}, 119{-ifcase(_m4_disks, _m4_disks{--}*)dnl 120popdef({-_m4_disks-})dnl 121show_disks()dnl 122-})-})dnl 123dnl 124define({-show_disks2-}, 125{-ifelse(_m4_disks2, {-done-}, {--}, 126{-CasE(_m4_disks2)dnl 127popdef({-_m4_disks2-})dnl 128show_disks2()dnl 129-})-})dnl 130dnl 131dnl 132dnl Some m4 math functions: 133dnl 134dnl Add(a, b) - displays the result of a+b 135dnl Mult(a, b) - displays the result of a*b 136dnl trunc a b - displays the string a minus b removed from the RHS 137dnl hex a - displays the hex equivalent of 0-15 138dnl unt a - s/[a-z]*([0-9]*).*/\1/ aka sd0a -> 0 139dnl 140dnl Functions: 141dnl 142dnl --> Addition 143dnl 144define({-Add-}, {-$({-(-}$1+$2{-)-})-})dnl 145dnl 146dnl --> Multiplication 147dnl 148define({-Mult-}, {-$({-(-}$1*$2{-)-})-})dnl 149dnl 150dnl 151dnl TRUNC 152dnl 153define({-expr_trunc-}, {-$1trunc() 154$1{ 155$1 case {-$-}3 in 156$1 l) {-expr-} {-$-}1 : '\(.*\)'{-$-}2 ;; 157$1 r|*) {-expr-} ${--}1 : ${--}2'\(.*\)' ;; 158$1 esac 159$1}-})dnl 160dnl 161dnl 162define({-_SHELL-}, {-sh-})dnl 163define({-_this-}, {-{-$-}T-})dnl 164dnl define({-_recurse-}, {-_SHELL _this-})dnl 165define({-_recurse-}, {-R-})dnl 166dnl 167dnl _devitem(pattern, description) 168dnl 169define({-_devitem-}, 170{-{-#-} $1 {-$2-}-})dnl 171dnl 172dnl _devtitle(description) 173dnl 174define({-_devtitle-}, {-{-#-} $1:-})dnl 175dnl 176dnl _DEV(name, [character major], [block major]) 177dnl 178define({-_DEV-}, {-$1_dev{--}dnl 179dnl 180dnl _DEV 'ifelse' .. $2 - major_$1_c 181dnl 182ifelse($2, , , {-define(major_$1_c, $2)-})dnl 183dnl 184dnl _DEV 'ifelse' .. $3 - major_$1_b 185dnl 186ifelse($3, , , {-define(major_$1_b, $3)-})dnl 187dnl 188dnl _DEV 'ifelse' .. $4 - step_$1 189dnl 190ifelse($4, , , {-define(step_$1, {-$4-})-})dnl 191dnl 192dnl Some magic here, defining a devitem also defines another 193dnl string so that later we can check if a particular devitem was 194dnl defined, and thus determine if the devices themselves are to 195dnl be built 196dnl 197define({-$1__DEV-}, {-Y-})dnl 198dnl 199dnl More magic, put device string name into a queue of script segments to 200dnl be shown later if it has been defined as a device in MAKEDEV.mi 201dnl 202ifdef({-$1_mkdev-}, {-__mkdev({-$1-})-})dnl 203dnl 204dnl 205-})dnl _DEV 206dnl 207dnl 208define({-ifdev-}, {-ifelse($1__DEV, Y, {-$2-})-})dnl 209dnl 210define({-_MKDEV-}, {-$1_mkdev-})dnl 211define({-_TITLE-}, {-$1_title-})dnl 212define({-__devitem-}, {-define($1_dev, {-_devitem($2, $3)-})-})dnl 213define({-__devtitle-}, {-define($1_title, {-_devtitle($2)-})-})dnl 214dnl 215dnl Beginning and ending of case entries, just incase we change in the 216dnl future, save chasing things down again 217dnl 218define({-_end-}, {- 219 ;; 220 221-})dnl 222define({-_beg-}, {-{-)-} 223 -})dnl 224dnl 225dnl create the script segment for making devices 226dnl $1 $2 $3 227dnl _mkdev(shortname, devpatt, action)dnl 228define({-_mkdev-}, {-define($1_mkdev, {-$2{--}_beg{--}$3{--}_end-})-})dnl 229dnl 230dnl define a major character device 231dnl $1 $2 $3 $4 232dnl _mcdev(shortname, devpatt, devbase, devmajor [, group [ owner ] ])dnl 233dnl 234define({-_mcdev-}, {-define($1_mkdev, {-{-$2-}_beg{--}M $3$U c $4 $U{--}dnl 235ifelse($6, , ifelse($5, , , {- -}$5), {- -}ifelse($5, , 600, $5){- -}$6){--}_end-})-})dnl 236dnl 237dnl $1 $2 $3 $4 $5 $6 238dnl _cdev(shortname, devpatt, devmajor, devminor[, devmod, devgrp])dnl 239define({-_cdev-}, 240{-dnl 241define($1_mkdev, {-$2{--}_beg{--}M $2 c $3 $4 $5 $6{--}_end-}){--}-})dnl 242dnl 243dnl 244define({-__mkdev-}, {-dev_q($1)-})dnl 245dnl 246dnl for showing disks 247dnl 248define({-CasE-}, 249{-ifdev({-$1-}, 250{- $1*) dodisk $1 $U major_$1_b major_$1_c $U 0{--}ifstep($1);; 251-})-})dnl 252dnl 253dnl 254define({-ifcase-}, {-dnl 255ifelse(C_ase, Y, ifdev({-$1-}, {-|$2-}), 256{-ifdev({-$1-}, {-$2define({-C_ase-}, Y)-})-})-})dnl 257dnl 258dnl 259dnl device list .. list devices 'iff' they are defined 260dnl 261dnl input: 262dnl 263dnl _dl({-usb-}, {-usb0 usb1-}, {-urio-}, {-urio-}, ...) 264dnl 265dnl output: 266dnl 267dnl {-<tab>_recurse usb0 usb1 urio 268dnl <tab>_recurse uhid0 uhid2 uhid3-} 269dnl 270dnl wrap if more than 60 chars wide 271dnl 272dnl .. wrap it all up in an 'ifelse({-$1-}, , {- ... -})' for neatness ?? 273dnl 274define({-_dl-}, {-dnl 275ifdef({-_dt-}, , {-define({-_dt-})-})dnl 276ifdef({-_do-}, , {-_dt{--}_recurse{--}define({-_do-}, 0)dnl 277define({-_dt-}, {- -})-})dnl 278ifdef({-$1__DEV-}, 279{-define({-_di-}, {-$2-})-}, 280{-define({-_di-})-})dnl 281ifelse(eval(len(_di)+_do<60), 1, 282{-ifelse(eval(len(_di)>0), 1, 283{- _di{--}define({-_do-}, eval(1+_do+len(_di)))-})-}, 284{- 285_dt{--}_recurse _di{--}dnl 286define({-_do-}, len(_di))-})dnl 287ifelse({-$3-}, {--}, 288{-undefine({-_dt-}, {-_do-})-}, dnl <--- The END 289{-_dl(shift(shift($@)))-})-})dnl 290dnl 291dnl 292define({-_show_target-}, {-dnl 293ifdef({-$1__DEV-}, {-disp_dev({-$2-})-})dnl 294ifelse({-$3-}, {--}, 295{-_disp_dev_end()-}, dnl <--- The END 296{-_show_target(shift(shift($@)))-})-})dnl 297dnl 298define({-disp_dev-}, {-dnl 299ifdef({-_disp_dev_tab-}, , {-define({-_disp_dev_tab-})-})dnl 300ifdef({-_disp_dev_len-}, , {-dnl 301define({-_disp_dev_len-}, 0)dnl 302_disp_dev_tab{--}_recurse{--}dnl 303define({-_disp_dev_tab-}, {- -})-})dnl 304ifelse(eval(len($1)+_disp_dev_len<60), 1, 305{- $1{--}define({-_disp_dev_len-}, eval(1+_disp_dev_len+len($1)))-}, {- 306_disp_dev_tab{--}_recurse $1{--}dnl 307define({-_disp_dev_len-}, len($1))-})dnl 308-})dnl 309define({-_disp_dev_end-}, {-undefine({-_disp_dev_tab-}, {-_disp_dev_len-})-})dnl 310dnl 311dnl A word about the above: 312dnl 313dnl _dt -> 'tab' .. at first, defined to nothing, as the tab is already there 314dnl then define it to a tab every time through 315dnl undefine it at the end 316dnl 317dnl _do -> 'old' count .. stores the length of the old string already displayed 318dnl it is initialized to 0, added to the length plus 1 of '_di' each 319dnl iteration the line is less than 60 chars long 320dnl undefined at the end 321dnl _di -> 'iteration' string .. the string used in this iteration, is empty if 322dnl the device does not exist 323dnl 324dnl 325dnl ifstep(devstr) 326dnl .. if stepping is defined for the particular device, emit ' step', else 327dnl nothing 328define({-ifstep-}, {-ifdef({-step_$1-}, {- -}step_$1{--})-})dnl 329dnl 330dnl 331define({-target-}, {-twrget({-$1-}, {-$2-}, {-$2-}, shift(shift($@)))-})dnl 332dnl 333dnl twrget(target, devname, prefix, str1, str2, str3) 334dnl $1 $2 $3 $4 $5 $6 335dnl 336define({-twrget-}, {-dnl 337dnl 338ifdef({-$1_target-}, , {-pushdef({-$1_target-}, {-done-})-})dnl 339dnl 340ifelse({-$4-}, , , {-dnl 341ifelse({-$4-}, {-_done-}, , {-dnl 342ifelse({-$5-}, , , {-dnl 343dnl errprint({-recurse: $1, $2, $3, $4, $5, $6, ...-})dnl 344twrget({-$1-}, {-$2-}, {-$3-}, shift(shift(shift(shift($@)))))dnl 345-})-})-})dnl 346dnl 347ifelse({-$4-}, {-_done-}, , {-dnl 348dnl errprint({-recurse: $1_$2_dev, $3$4, $3$4, _done-})dnl 349twrget({-$1_$2_dev-}, {-$3$4-}, {-$3$4-}, {-_done-})dnl 350-})dnl 351dnl 352ifdef({-$1$2target-}, , {-dnl 353pushdef({-$1_target-}, {-$2-})define({-$1$2target-})dnl 354-})dnl 355dnl 356-})dnl 357dnl 358dnl 359define({-show_target-}, {-dnl 360ifelse($1_target, {-done-}, {-_disp_dev_end()-}, 361{-dnl $1_target: 362show_dev($1, -}$1_target{-)dnl 363popdef({-$1_target-})dnl 364show_target({-$1-})dnl 365-})-})dnl 366dnl 367define({-show_dev-}, {-dnl 368ifdef({-$2__DEV-}, {-dnl 369ifelse($1_$2_dev_target, {-done-}, , {-dnl 370disp_dev({-$1_$2_dev_target-})dnl 371popdef({-$1_$2_dev_target-})dnl 372show_dev({-$1-}, {-$2-})-})dnl 373-})dnl 374-})dnl 375dnl 376