github.com/tristanisham/sys@v0.0.0-20240326010300-a16cbabb7555/windows/mkerrors.bash (about)

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