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