1 1.17 christos # $NetBSD: parselist.awk,v 1.17 2024/04/16 23:40:36 christos Exp $ 2 1.1 lukem # 3 1.1 lukem # Copyright (c) 2002 The NetBSD Foundation, Inc. 4 1.1 lukem # All rights reserved. 5 1.1 lukem # 6 1.1 lukem # This code is derived from software contributed to The NetBSD Foundation 7 1.1 lukem # by Luke Mewburn of Wasabi Systems. 8 1.1 lukem # 9 1.1 lukem # Redistribution and use in source and binary forms, with or without 10 1.1 lukem # modification, are permitted provided that the following conditions 11 1.1 lukem # are met: 12 1.1 lukem # 1. Redistributions of source code must retain the above copyright 13 1.1 lukem # notice, this list of conditions and the following disclaimer. 14 1.1 lukem # 2. Redistributions in binary form must reproduce the above copyright 15 1.1 lukem # notice, this list of conditions and the following disclaimer in the 16 1.1 lukem # documentation and/or other materials provided with the distribution. 17 1.1 lukem # 18 1.1 lukem # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 lukem # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 lukem # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 lukem # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 lukem # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 lukem # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 lukem # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 lukem # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 lukem # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 lukem # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 lukem # POSSIBILITY OF SUCH DAMAGE. 29 1.1 lukem # 30 1.3 lukem 31 1.3 lukem # 32 1.3 lukem # awk -f parselist.awk -v mode=MODE [var=val ...] file1 [...] 33 1.1 lukem # 34 1.1 lukem # Parse list files file1 [...], generating different output, 35 1.7 lukem # depending upon the value of MODE: 36 1.9 lukem # 37 1.1 lukem # crunch crunchgen(1) config 38 1.9 lukem # 39 1.12 lukem # install make(1) Makefile to install commands into ${TARGETDIR}, 40 1.12 lukem # with an `install' target. 41 1.12 lukem # The following environment variables need to be set: 42 1.12 lukem # TARGETDIR Directory to populate 43 1.12 lukem # 44 1.1 lukem # mtree mtree(8) specfile 45 1.9 lukem # 46 1.9 lukem # populate sh(1) commands to populate ${TARGETDIR} from ${CURDIR} 47 1.9 lukem # The following environment variables need to be set: 48 1.9 lukem # CURDIR Source directory; make(1)'s ${.CURDIR} 49 1.9 lukem # TARGETDIR Directory to populate 50 1.9 lukem # 51 1.12 lukem # The following environment variables need to be set for all modes: 52 1.12 lukem # CRUNCHBIN Name of crunchgen(1) target binary 53 1.12 lukem # OBJDIR Object directory; make(1)'s ${.OBJDIR} 54 1.1 lukem # 55 1.1 lukem # Each line of the input is either a comment (starts with `#'), 56 1.1 lukem # or contains one of the following keywords and arguments. 57 1.1 lukem # 58 1.9 lukem # Before each line is parsed for a keyword, words surrounded by 59 1.9 lukem # "${" and "}", and containing only letters, numbers, and `_' 60 1.9 lukem # will be replaced by the value of the environment variable of 61 1.9 lukem # the same name. I.e., "${MACHINE_ARCH}" will be replaced with the 62 1.9 lukem # value of ENVIRON["MACHINE_ARCH"]. 63 1.3 lukem # 64 1.1 lukem # mode key operation 65 1.1 lukem # -------- --------- 66 1.1 lukem # C crunch 67 1.12 lukem # I install 68 1.1 lukem # M mtree 69 1.1 lukem # P populate 70 1.1 lukem # 71 1.1 lukem # mode keyword arg1 [...] description 72 1.1 lukem # ---- ------------------ ----------- 73 1.1 lukem # 74 1.1 lukem # C ARGVLN prog link as per crunchgen(1) `ln' 75 1.1 lukem # 76 1.1 lukem # P CMD arg1 [...] run CMD as a shell command 77 1.1 lukem # 78 1.12 lukem # IMP COPY src dest [perm] copy src to dest. perm defaults to 0444 79 1.4 lukem # 80 1.12 lukem # IMP COPYDIR src dest recursively copy files under src to 81 1.10 lukem # dest. for M, dest is listed first, 82 1.10 lukem # followed by the subdirectories in src. 83 1.11 lukem # copied directories have mode 0755. 84 1.11 lukem # copied files have mode 0444. 85 1.1 lukem # 86 1.1 lukem # C LIBS libspec ... as per crunchgen(1) `libs' 87 1.1 lukem # 88 1.12 lukem # IMP LINK src d1 [d2 ...] hard link src to d1, d2, ... 89 1.1 lukem # 90 1.6 lukem # M MTREE arg1 [...] output arguments `as-is' to specfile 91 1.6 lukem # 92 1.12 lukem # CIMP PROG prog [links...] program(s) to crunch/mtree/populate. 93 1.12 lukem # for I, M & P, the first prog listed 94 1.1 lukem # is copied from ${OBJDIR}/${CRUNCHBIN} 95 1.1 lukem # and then used as the name to link 96 1.1 lukem # all other PROG entries to. 97 1.1 lukem # 98 1.1 lukem # C SPECIAL prog cmd ... as per crunchgen(1) `special' 99 1.1 lukem # 100 1.1 lukem # C SRCDIRS dirname ... as per crunchgen(1) `srcdirs' 101 1.1 lukem # 102 1.12 lukem # IMP SYMLINK src dest [...] symlink src to dest, [...] 103 1.1 lukem # 104 1.1 lukem 105 1.1 lukem BEGIN \ 106 1.1 lukem { 107 1.1 lukem crunchprog = ""; 108 1.1 lukem 109 1.12 lukem if (mode != "crunch" && mode != "install" && 110 1.12 lukem mode != "mtree" && mode != "populate") 111 1.9 lukem errx("Unknown parselist mode '" mode "'"); 112 1.9 lukem 113 1.12 lukem needvars["CRUNCHBIN"]++ 114 1.12 lukem needvars["OBJDIR"]++ 115 1.12 lukem if (mode == "install") { 116 1.12 lukem needvars["TARGETDIR"]++ 117 1.12 lukem } 118 1.12 lukem else if (mode == "populate") { 119 1.12 lukem needvars["CURDIR"]++ 120 1.12 lukem } 121 1.12 lukem for (nv in needvars) { 122 1.12 lukem if (! (nv in ENVIRON)) 123 1.12 lukem errx("Environment variable " nv " not defined"); 124 1.9 lukem } 125 1.9 lukem 126 1.1 lukem print "#"; 127 1.1 lukem print "# This file is automatically generated by"; 128 1.1 lukem print "#\tparselist mode=" mode; 129 1.1 lukem print "#"; 130 1.1 lukem print ""; 131 1.12 lukem if (mode == "install") { 132 1.12 lukem print ".include <bsd.own.mk>" 133 1.12 lukem print "install:" 134 1.1 lukem } else if (mode == "mtree") { 135 1.2 lukem print "/unset\tall"; 136 1.1 lukem print "/set\ttype=file uname=root gname=wheel"; 137 1.1 lukem print; 138 1.12 lukem } else if (mode == "populate") { 139 1.12 lukem print "cd " ENVIRON["CURDIR"]; 140 1.12 lukem print; 141 1.1 lukem } 142 1.1 lukem } 143 1.1 lukem 144 1.1 lukem /^$/ || /^#/ \ 145 1.1 lukem { 146 1.1 lukem print; 147 1.1 lukem next; 148 1.1 lukem } 149 1.1 lukem 150 1.9 lukem # replace ${FOO} with ENVIRON["FOO"] 151 1.9 lukem # 152 1.13 thorpej /\$\{[A-Za-z0-9_]+\}/ \ 153 1.3 lukem { 154 1.13 thorpej while (match($0, /\$\{[A-Za-z0-9_]+\}/) > 0) { 155 1.9 lukem v = substr($0, RSTART + 2, RLENGTH - 3); 156 1.9 lukem if (! (v in ENVIRON)) 157 1.9 lukem err("Variable " v " is not in the environment"); 158 1.9 lukem else 159 1.13 thorpej sub(/\$\{[A-Za-z0-9_]+\}/, ENVIRON[v]); 160 1.9 lukem } 161 1.3 lukem } 162 1.3 lukem 163 1.1 lukem $1 == "COPY" \ 164 1.1 lukem { 165 1.3 lukem if (NF < 3 || NF > 4) 166 1.11 lukem err("Usage: COPY src dest [perm]"); 167 1.12 lukem if (mode == "install" || mode == "mtree" || mode == "populate") 168 1.3 lukem copy($2, $3, $4); 169 1.1 lukem next; 170 1.1 lukem } 171 1.1 lukem 172 1.4 lukem $1 == "COPYDIR" \ 173 1.4 lukem { 174 1.4 lukem if (NF != 3) 175 1.4 lukem err("Usage: COPYDIR src dest"); 176 1.5 lukem srcdir=$2; 177 1.5 lukem destdir=$3; 178 1.4 lukem if (mode == "mtree") { 179 1.5 lukem printf("./%s type=dir mode=755\n", destdir); 180 1.17 christos command="cd " srcdir " && find . -type d -print | LC_ALL=C sort" 181 1.4 lukem while (command | getline dir) { 182 1.4 lukem gsub(/^\.\//, "", dir); 183 1.5 lukem if (dir == ".") 184 1.5 lukem continue; 185 1.5 lukem printf("./%s/%s type=dir mode=755\n", destdir, dir); 186 1.4 lukem } 187 1.4 lukem close(command); 188 1.4 lukem } 189 1.12 lukem if (mode == "install" || mode == "mtree" || mode == "populate") { 190 1.17 christos command="cd " srcdir " && find . -type f -print | LC_ALL=C sort" 191 1.4 lukem while (command | getline srcfile) { 192 1.4 lukem gsub(/^\.\//, "", srcfile); 193 1.4 lukem copy(srcdir "/" srcfile, destdir "/" srcfile, ""); 194 1.4 lukem } 195 1.4 lukem close(command); 196 1.4 lukem } 197 1.4 lukem next; 198 1.4 lukem } 199 1.4 lukem 200 1.1 lukem $1 == "LIBS" || $1 == "SPECIAL" || $1 == "SRCDIRS" \ 201 1.1 lukem { 202 1.3 lukem if (NF < 2) 203 1.3 lukem err("Usage: " $1 " args..."); 204 1.1 lukem if (mode == "crunch") { 205 1.1 lukem $1 = tolower($1); 206 1.1 lukem print; 207 1.1 lukem } 208 1.1 lukem next; 209 1.1 lukem } 210 1.1 lukem 211 1.1 lukem $1 == "PROG" \ 212 1.1 lukem { 213 1.3 lukem if (NF < 2) 214 1.3 lukem err("Usage: PROG prog [link ...]"); 215 1.1 lukem if (mode == "crunch") { 216 1.1 lukem prog = basename($2); 217 1.1 lukem print "progs " prog; 218 1.1 lukem for (i = 3; i <= NF; i++) 219 1.1 lukem print "ln " prog " " basename($i); 220 1.1 lukem } else { 221 1.1 lukem for (i = 2; i <= NF; i++) { 222 1.1 lukem if (crunchprog == "") { 223 1.1 lukem crunchprog = $i; 224 1.9 lukem copy(ENVIRON["OBJDIR"] "/" ENVIRON["CRUNCHBIN"], 225 1.11 lukem crunchprog, 555); 226 1.1 lukem continue; 227 1.1 lukem } 228 1.16 apb link(crunchprog, $i, 555); 229 1.1 lukem } 230 1.1 lukem } 231 1.1 lukem next; 232 1.1 lukem } 233 1.1 lukem 234 1.1 lukem $1 == "ARGVLN" \ 235 1.1 lukem { 236 1.3 lukem if (NF != 3) 237 1.3 lukem err("Usage: ARGVLN prog link"); 238 1.1 lukem if (mode == "crunch") { 239 1.1 lukem $1 = "ln"; 240 1.1 lukem print; 241 1.1 lukem } 242 1.1 lukem next; 243 1.1 lukem } 244 1.1 lukem 245 1.1 lukem $1 == "LINK" \ 246 1.1 lukem { 247 1.3 lukem if (NF < 3) 248 1.3 lukem err("Usage: LINK prog link [...]"); 249 1.12 lukem if (mode == "install" || mode == "mtree" || mode == "populate") { 250 1.1 lukem for (i = 3; i <= NF; i++) 251 1.16 apb link($2, $i, 444); 252 1.1 lukem } 253 1.1 lukem next; 254 1.1 lukem } 255 1.1 lukem 256 1.1 lukem $1 == "SYMLINK" \ 257 1.1 lukem { 258 1.3 lukem if (NF < 3) 259 1.3 lukem err("Usage: SYMLINK prog link [...]"); 260 1.12 lukem if (mode == "install" || mode == "mtree" || mode == "populate") { 261 1.1 lukem for (i = 3; i <= NF; i++) 262 1.1 lukem symlink($2, $i); 263 1.1 lukem } 264 1.1 lukem next; 265 1.1 lukem } 266 1.1 lukem 267 1.1 lukem $1 == "CMD" \ 268 1.1 lukem { 269 1.3 lukem if (NF < 2) 270 1.3 lukem err("Usage: CMD ..."); 271 1.1 lukem if (mode == "populate") { 272 1.9 lukem printf("(cd %s;", ENVIRON["TARGETDIR"]); 273 1.1 lukem for (i = 2; i <= NF; i++) 274 1.1 lukem printf(" %s", $i); 275 1.11 lukem print ") || exit 1"; 276 1.1 lukem } 277 1.1 lukem next; 278 1.1 lukem } 279 1.6 lukem 280 1.6 lukem $1 == "MTREE" \ 281 1.6 lukem { 282 1.6 lukem if (NF < 2) 283 1.6 lukem err("Usage: MTREE ..."); 284 1.6 lukem if (mode == "mtree") { 285 1.6 lukem sub(/^[^ \t]+[ \t]+/, ""); # strip first word ("MTREE") 286 1.6 lukem print; 287 1.6 lukem } 288 1.6 lukem next; 289 1.6 lukem } 290 1.6 lukem 291 1.1 lukem 292 1.1 lukem { 293 1.3 lukem err("Unknown keyword '" $1 "'"); 294 1.1 lukem } 295 1.1 lukem 296 1.1 lukem 297 1.1 lukem function basename (file) \ 298 1.1 lukem { 299 1.1 lukem gsub(/[^\/]+\//, "", file); 300 1.1 lukem return file; 301 1.1 lukem } 302 1.1 lukem 303 1.3 lukem function copy (src, dest, perm) \ 304 1.1 lukem { 305 1.11 lukem if (perm == "") 306 1.11 lukem perm = 444; 307 1.12 lukem if (mode == "install") { 308 1.16 apb printf("\t${INSTALL_FILE} -o ${BINOWN} -g ${BINGRP}" \ 309 1.16 apb " -m %s %s %s/%s\n", 310 1.12 lukem perm, src, ENVIRON["TARGETDIR"], dest) 311 1.12 lukem } else if (mode == "mtree") { 312 1.11 lukem printf("./%s mode=%s\n", dest, perm); 313 1.1 lukem } else { 314 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest); 315 1.9 lukem printf("cp %s %s/%s\n", src, ENVIRON["TARGETDIR"], dest); 316 1.11 lukem printf("chmod %s %s/%s\n", perm, ENVIRON["TARGETDIR"], dest); 317 1.1 lukem } 318 1.1 lukem } 319 1.1 lukem 320 1.16 apb function link (src, dest, perm) \ 321 1.1 lukem { 322 1.12 lukem if (mode == "install") { 323 1.16 apb printf("\t${INSTALL_LINK} -o ${BINOWN} -g ${BINGRP}" \ 324 1.16 apb " -m %s %s/%s %s/%s\n", 325 1.16 apb perm, ENVIRON["TARGETDIR"], src, ENVIRON["TARGETDIR"], dest) 326 1.12 lukem } else if (mode == "mtree") { 327 1.16 apb printf("./%s mode=%s\n", dest, perm); 328 1.1 lukem } else { 329 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest); 330 1.11 lukem printf("(cd %s; ln %s %s) || exit 1\n", 331 1.11 lukem ENVIRON["TARGETDIR"], src, dest); 332 1.1 lukem } 333 1.1 lukem } 334 1.1 lukem 335 1.1 lukem function symlink (src, dest) \ 336 1.1 lukem { 337 1.12 lukem if (mode == "install") { 338 1.14 thorpej printf("\t${INSTALL_SYMLINK} %s/%s %s/%s\n", 339 1.12 lukem ENVIRON["TARGETDIR"], src, ENVIRON["TARGETDIR"], dest) 340 1.12 lukem } else if (mode == "mtree") { 341 1.1 lukem printf("./%s type=link link=%s\n", dest, src); 342 1.1 lukem } else { 343 1.9 lukem printf("rm -rf %s/%s\n", ENVIRON["TARGETDIR"], dest); 344 1.11 lukem printf("(cd %s; ln -s %s %s) || exit 1\n", 345 1.11 lukem ENVIRON["TARGETDIR"], src, dest); 346 1.1 lukem } 347 1.3 lukem } 348 1.3 lukem 349 1.3 lukem function err(msg) \ 350 1.3 lukem { 351 1.9 lukem printf("parselist: %s at line %d of input.\n", msg, NR) >"/dev/stderr"; 352 1.9 lukem exit 1; 353 1.9 lukem } 354 1.9 lukem 355 1.9 lukem function errx(msg) \ 356 1.9 lukem { 357 1.9 lukem printf("parselist: %s.\n", msg) >"/dev/stderr"; 358 1.3 lukem exit 1; 359 1.1 lukem } 360