mkerrors.bash 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. # Copyright 2019 The Go Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style
  4. # license that can be found in the LICENSE file.
  5. set -e
  6. shopt -s nullglob
  7. [[ $# -eq 1 ]] || { echo "Usage: $0 OUTPUT_FILE.go" >&2; exit 1; }
  8. winerror="$(printf '%s\n' "/mnt/c/Program Files (x86)/Windows Kits/"/*/Include/*/shared/winerror.h | sort -Vr | head -n 1)"
  9. [[ -n $winerror ]] || { echo "Unable to find winerror.h" >&2; exit 1; }
  10. declare -A errors
  11. {
  12. echo "// Code generated by 'go generate'; DO NOT EDIT."
  13. echo
  14. echo "package windows"
  15. echo "import \"syscall\""
  16. echo "const ("
  17. while read -r line; do
  18. unset vtype
  19. if [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +([A-Z0-9_]+\()?([A-Z][A-Z0-9_]+k?)\)? ]]; then
  20. key="${BASH_REMATCH[1]}"
  21. value="${BASH_REMATCH[3]}"
  22. elif [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +([A-Z0-9_]+\()?((0x)?[0-9A-Fa-f]+)L?\)? ]]; then
  23. key="${BASH_REMATCH[1]}"
  24. value="${BASH_REMATCH[3]}"
  25. vtype="${BASH_REMATCH[2]}"
  26. elif [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +\(\(([A-Z]+)\)((0x)?[0-9A-Fa-f]+)L?\) ]]; then
  27. key="${BASH_REMATCH[1]}"
  28. value="${BASH_REMATCH[3]}"
  29. vtype="${BASH_REMATCH[2]}"
  30. else
  31. continue
  32. fi
  33. [[ -n $key && -n $value ]] || continue
  34. [[ -z ${errors["$key"]} ]] || continue
  35. errors["$key"]="$value"
  36. if [[ -v vtype ]]; then
  37. if [[ $key == FACILITY_* || $key == NO_ERROR ]]; then
  38. vtype=""
  39. elif [[ $vtype == *HANDLE* || $vtype == *HRESULT* ]]; then
  40. vtype="Handle"
  41. else
  42. vtype="syscall.Errno"
  43. fi
  44. last_vtype="$vtype"
  45. else
  46. vtype=""
  47. if [[ $last_vtype == Handle && $value == NO_ERROR ]]; then
  48. value="S_OK"
  49. elif [[ $last_vtype == syscall.Errno && $value == NO_ERROR ]]; then
  50. value="ERROR_SUCCESS"
  51. fi
  52. fi
  53. echo "$key $vtype = $value"
  54. done < "$winerror"
  55. echo ")"
  56. } | gofmt > "$1"