/sys/tools/usbdevs2h.awk

https://bitbucket.org/iorivur/freebsd-bhyve-with-suspend-resume · AWK · 271 lines · 217 code · 17 blank · 37 comment · 78 complexity · 767f9534f649e5f9b5a23fea6ac57a12 MD5 · raw file

  1. #! /usr/bin/awk -f
  2. #-
  3. # $NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $
  4. # $FreeBSD: head/sys/tools/usbdevs2h.awk 139825 2005-01-07 02:29:27Z imp $
  5. #
  6. # Copyright (c) 1995, 1996 Christopher G. Demetriou
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # 2. Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in the
  16. # documentation and/or other materials provided with the distribution.
  17. # 3. All advertising materials mentioning features or use of this software
  18. # must display the following acknowledgement:
  19. # This product includes software developed by Christopher G. Demetriou.
  20. # 4. The name of the author may not be used to endorse or promote products
  21. # derived from this software without specific prior written permission
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  24. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  28. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #
  34. function usage()
  35. {
  36. print "usage: usbdevs2h.awk <srcfile> [-d|-h]";
  37. exit 1;
  38. }
  39. function header(file)
  40. {
  41. if (os == "NetBSD")
  42. printf("/*\t\$NetBSD\$\t*/\n\n") > file
  43. else if (os == "FreeBSD")
  44. printf("/* \$FreeBSD\$ */\n\n") > file
  45. else if (os == "OpenBSD")
  46. printf("/*\t\$OpenBSD\$\t*/\n\n") > file
  47. else
  48. printf("/* ??? */\n\n") > file
  49. printf("/*\n") > file
  50. printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \
  51. > file
  52. printf(" *\n") > file
  53. printf(" * generated from:\n") > file
  54. printf(" *\t%s\n", VERSION) > file
  55. printf(" */\n") > file
  56. }
  57. function vendor(hfile)
  58. {
  59. nvendors++
  60. vendorindex[$2] = nvendors; # record index for this name, for later.
  61. vendors[nvendors, 1] = $2; # name
  62. vendors[nvendors, 2] = $3; # id
  63. if (hfile)
  64. printf("#define\tUSB_VENDOR_%s\t%s\t", vendors[nvendors, 1],
  65. vendors[nvendors, 2]) > hfile
  66. i = 3; f = 4;
  67. # comments
  68. ocomment = oparen = 0
  69. if (f <= NF) {
  70. if (hfile)
  71. printf("\t/* ") > hfile
  72. ocomment = 1;
  73. }
  74. while (f <= NF) {
  75. if ($f == "#") {
  76. if (hfile)
  77. printf("(") > hfile
  78. oparen = 1
  79. f++
  80. continue
  81. }
  82. if (oparen) {
  83. if (hfile)
  84. printf("%s", $f) > hfile
  85. if (f < NF && hfile)
  86. printf(" ") > hfile
  87. f++
  88. continue
  89. }
  90. vendors[nvendors, i] = $f
  91. if (hfile)
  92. printf("%s", vendors[nvendors, i]) > hfile
  93. if (f < NF && hfile)
  94. printf(" ") > hfile
  95. i++; f++;
  96. }
  97. if (oparen && hfile)
  98. printf(")") > hfile
  99. if (ocomment && hfile)
  100. printf(" */") > hfile
  101. if (hfile)
  102. printf("\n") > hfile
  103. }
  104. function product(hfile)
  105. {
  106. nproducts++
  107. products[nproducts, 1] = $2; # vendor name
  108. products[nproducts, 2] = $3; # product id
  109. products[nproducts, 3] = $4; # id
  110. if (hfile)
  111. printf("#define\tUSB_PRODUCT_%s_%s\t%s\t", \
  112. products[nproducts, 1], products[nproducts, 2], \
  113. products[nproducts, 3]) > hfile
  114. i=4; f = 5;
  115. # comments
  116. ocomment = oparen = 0
  117. if (f <= NF) {
  118. if (hfile)
  119. printf("\t/* ") > hfile
  120. ocomment = 1;
  121. }
  122. while (f <= NF) {
  123. if ($f == "#") {
  124. if (hfile)
  125. printf("(") > hfile
  126. oparen = 1
  127. f++
  128. continue
  129. }
  130. if (oparen) {
  131. if (hfile)
  132. printf("%s", $f) > hfile
  133. if (f < NF && hfile)
  134. printf(" ") > hfile
  135. f++
  136. continue
  137. }
  138. products[nproducts, i] = $f
  139. if (hfile)
  140. printf("%s", products[nproducts, i]) > hfile
  141. if (f < NF && hfile)
  142. printf(" ") > hfile
  143. i++; f++;
  144. }
  145. if (oparen && hfile)
  146. printf(")") > hfile
  147. if (ocomment && hfile)
  148. printf(" */") > hfile
  149. if (hfile)
  150. printf("\n") > hfile
  151. }
  152. function dump_dfile(dfile)
  153. {
  154. printf("\n") > dfile
  155. printf("const struct usb_knowndev usb_knowndevs[] = {\n") > dfile
  156. for (i = 1; i <= nproducts; i++) {
  157. printf("\t{\n") > dfile
  158. printf("\t USB_VENDOR_%s, USB_PRODUCT_%s_%s,\n",
  159. products[i, 1], products[i, 1], products[i, 2]) > dfile
  160. printf("\t ") > dfile
  161. printf("0") > dfile
  162. printf(",\n") > dfile
  163. vendi = vendorindex[products[i, 1]];
  164. printf("\t \"") > dfile
  165. j = 3;
  166. needspace = 0;
  167. while (vendors[vendi, j] != "") {
  168. if (needspace)
  169. printf(" ") > dfile
  170. printf("%s", vendors[vendi, j]) > dfile
  171. needspace = 1
  172. j++
  173. }
  174. printf("\",\n") > dfile
  175. printf("\t \"") > dfile
  176. j = 4;
  177. needspace = 0;
  178. while (products[i, j] != "") {
  179. if (needspace)
  180. printf(" ") > dfile
  181. printf("%s", products[i, j]) > dfile
  182. needspace = 1
  183. j++
  184. }
  185. printf("\",\n") > dfile
  186. printf("\t},\n") > dfile
  187. }
  188. for (i = 1; i <= nvendors; i++) {
  189. printf("\t{\n") > dfile
  190. printf("\t USB_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
  191. printf("\t USB_KNOWNDEV_NOPROD,\n") > dfile
  192. printf("\t \"") > dfile
  193. j = 3;
  194. needspace = 0;
  195. while (vendors[i, j] != "") {
  196. if (needspace)
  197. printf(" ") > dfile
  198. printf("%s", vendors[i, j]) > dfile
  199. needspace = 1
  200. j++
  201. }
  202. printf("\",\n") > dfile
  203. printf("\t NULL,\n") > dfile
  204. printf("\t},\n") > dfile
  205. }
  206. printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
  207. printf("};\n") > dfile
  208. }
  209. BEGIN {
  210. nproducts = nvendors = 0
  211. # Process the command line
  212. for (i = 1; i < ARGC; i++) {
  213. arg = ARGV[i];
  214. if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
  215. usage();
  216. if (arg ~ /^-.*d/)
  217. dfile="usbdevs_data.h"
  218. if (arg ~ /^-.*h/)
  219. hfile="usbdevs.h"
  220. if (arg ~ /devs$/)
  221. srcfile = arg;
  222. }
  223. ARGC = 1;
  224. line=0;
  225. while ((getline < srcfile) > 0) {
  226. line++;
  227. if (line == 1) {
  228. VERSION = $0
  229. gsub("\\$", "", VERSION)
  230. if (dfile)
  231. header(dfile)
  232. if (hfile)
  233. header(hfile)
  234. continue;
  235. }
  236. if ($1 == "vendor") {
  237. vendor(hfile)
  238. continue
  239. }
  240. if ($1 == "product") {
  241. product(hfile)
  242. continue
  243. }
  244. if ($0 == "")
  245. blanklines++
  246. if (hfile)
  247. print $0 > hfile
  248. if (blanklines < 2 && dfile)
  249. print $0 > dfile
  250. }
  251. # print out the match tables
  252. if (dfile)
  253. dump_dfile(dfile)
  254. }