mkerrors.bash 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. winerror="$(printf '%s\n' "/mnt/c/Program Files (x86)/Windows Kits/"/*/Include/*/shared/winerror.h | sort -Vr | head -n 1)"
  8. [[ -n $winerror ]] || { echo "Unable to find winerror.h" >&2; exit 1; }
  9. declare -A errors
  10. {
  11. echo "// Code generated by 'mkerrors.bash'; DO NOT EDIT."
  12. echo
  13. echo "package windows"
  14. echo "import \"syscall\""
  15. echo "const ("
  16. while read -r line; do
  17. unset vtype
  18. if [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +([A-Z0-9_]+\()?([A-Z][A-Z0-9_]+k?)\)? ]]; then
  19. key="${BASH_REMATCH[1]}"
  20. value="${BASH_REMATCH[3]}"
  21. elif [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +([A-Z0-9_]+\()?((0x)?[0-9A-Fa-f]+)L?\)? ]]; then
  22. key="${BASH_REMATCH[1]}"
  23. value="${BASH_REMATCH[3]}"
  24. vtype="${BASH_REMATCH[2]}"
  25. elif [[ $line =~ ^#define\ +([A-Z0-9_]+k?)\ +\(\(([A-Z]+)\)((0x)?[0-9A-Fa-f]+)L?\) ]]; then
  26. key="${BASH_REMATCH[1]}"
  27. value="${BASH_REMATCH[3]}"
  28. vtype="${BASH_REMATCH[2]}"
  29. else
  30. continue
  31. fi
  32. [[ -n $key && -n $value ]] || continue
  33. [[ -z ${errors["$key"]} ]] || continue
  34. errors["$key"]="$value"
  35. if [[ -v vtype ]]; then
  36. if [[ $key == FACILITY_* || $key == NO_ERROR ]]; then
  37. vtype=""
  38. elif [[ $vtype == *HANDLE* || $vtype == *HRESULT* ]]; then
  39. vtype="Handle"
  40. else
  41. vtype="syscall.Errno"
  42. fi
  43. last_vtype="$vtype"
  44. else
  45. vtype=""
  46. if [[ $last_vtype == Handle && $value == NO_ERROR ]]; then
  47. value="S_OK"
  48. elif [[ $last_vtype == syscall.Errno && $value == NO_ERROR ]]; then
  49. value="ERROR_SUCCESS"
  50. fi
  51. fi
  52. echo "$key $vtype = $value"
  53. done < "$winerror"
  54. echo ")"
  55. } | gofmt > "zerrors_windows.go"