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