2018-12-11 12:23:17 +13:00
#!/usr/bin/env python3
2014-03-24 15:02:45 +00:00
#
# Unix SMB/CIFS implementation.
#
# HRESULT Error definitions
#
# Copyright (C) Noel Power <noel.power@suse.com> 2014
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2023-04-27 15:17:18 +12:00
import sys
2024-01-11 11:19:22 +13:00
from gen_error_common import parseErrorDescriptions
2014-03-24 15:02:45 +00:00
def write_license ( out_file ) :
out_file . write ( " /* \n " )
out_file . write ( " * Unix SMB/CIFS implementation. \n " )
out_file . write ( " * \n " )
out_file . write ( " * HRESULT Error definitions \n " )
out_file . write ( " * \n " )
out_file . write ( " * Copyright (C) Noel Power <noel.power@suse.com> 2014 \n " )
out_file . write ( " * \n " )
out_file . write ( " * This program is free software; you can redistribute it and/or modify \n " )
out_file . write ( " * it under the terms of the GNU General Public License as published by \n " )
out_file . write ( " * the Free Software Foundation; either version 3 of the License, or \n " )
out_file . write ( " * (at your option) any later version. \n " )
out_file . write ( " * \n " )
out_file . write ( " * This program is distributed in the hope that it will be useful, \n " )
out_file . write ( " * but WITHOUT ANY WARRANTY; without even the implied warranty of \n " )
out_file . write ( " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \n " )
out_file . write ( " * GNU General Public License for more details. \n " )
out_file . write ( " * \n " )
out_file . write ( " * You should have received a copy of the GNU General Public License \n " )
out_file . write ( " * along with this program. If not, see <http://www.gnu.org/licenses/>. \n " )
out_file . write ( " */ \n " )
out_file . write ( " \n " )
2024-01-11 11:20:59 +13:00
def generateHeaderFile ( out_file , errors ) :
2014-03-24 15:02:45 +00:00
write_license ( out_file )
out_file . write ( " #ifndef _HRESULT_H_ \n " )
out_file . write ( " #define _HRESULT_H_ \n \n " )
macro_magic = " #if defined(HAVE_IMMEDIATE_STRUCTURES) \n "
macro_magic + = " typedef struct { uint32_t h;} HRESULT; \n "
macro_magic + = " #define HRES_ERROR(x) ((HRESULT) { x }) \n "
macro_magic + = " #define HRES_ERROR_V(x) ((x).h) \n "
macro_magic + = " #else \n "
macro_magic + = " typedef uint32_t HRESULT; \n "
macro_magic + = " #define HRES_ERROR(x) (x) \n "
macro_magic + = " #define HRES_ERROR_V(x) (x) \n "
macro_magic + = " #endif \n "
macro_magic + = " \n "
macro_magic + = " #define HRES_IS_OK(x) (HRES_ERROR_V(x) == 0) \n "
macro_magic + = " #define HRES_IS_EQUAL(x,y) (HRES_ERROR_V(x) == HRES_ERROR_V(y)) \n "
out_file . write ( macro_magic )
out_file . write ( " \n \n " )
out_file . write ( " /* \n " )
out_file . write ( " * The following error codes are autogenerated from [MS-ERREF] \n " )
out_file . write ( " * see http://msdn.microsoft.com/en-us/library/cc704587.aspx \n " )
out_file . write ( " */ \n " )
out_file . write ( " \n " )
2024-01-11 11:20:59 +13:00
for err in errors :
2024-01-11 11:19:22 +13:00
line = " #define {0:49} HRES_ERROR(0x {1:08X} ) \n " . format ( err . err_define , err . err_code )
2014-03-24 15:02:45 +00:00
out_file . write ( line )
out_file . write ( " \n const char *hresult_errstr_const(HRESULT err_code); \n " )
2015-03-06 17:42:06 +01:00
out_file . write ( " \n const char *hresult_errstr(HRESULT err_code); \n " )
2014-03-24 15:02:45 +00:00
out_file . write ( " \n #define FACILITY_WIN32 0x0007 \n " )
out_file . write ( " #define WIN32_FROM_HRESULT(x) (HRES_ERROR_V(x) == 0 ? HRES_ERROR_V(x) : ~((FACILITY_WIN32 << 16) | 0x80000000) & HRES_ERROR_V(x)) \n " )
out_file . write ( " #define HRESULT_IS_LIKELY_WERR(x) ((HRES_ERROR_V(x) & 0xFFFF0000) == 0x80070000) \n " )
2016-09-25 19:51:03 +02:00
out_file . write ( " #define HRESULT_FROM_WERROR(x) (HRES_ERROR(0x80070000 | W_ERROR_V(x))) \n " )
2014-03-24 15:02:45 +00:00
out_file . write ( " \n \n \n #endif /*_HRESULT_H_*/ " )
2024-01-11 11:20:59 +13:00
def generateSourceFile ( out_file , errors ) :
2014-03-24 15:02:45 +00:00
write_license ( out_file )
out_file . write ( " #include \" includes.h \" \n " )
out_file . write ( " #include \" hresult.h \" \n " )
out_file . write ( " /* \n " )
out_file . write ( " * The following error codes and descriptions are autogenerated from [MS-ERREF] \n " )
out_file . write ( " * see http://msdn.microsoft.com/en-us/library/cc704587.aspx \n " )
out_file . write ( " */ \n " )
out_file . write ( " \n " )
out_file . write ( " const char *hresult_errstr_const(HRESULT err_code) \n " )
2023-02-24 12:54:16 +13:00
out_file . write ( " { \n " )
2014-03-24 15:02:45 +00:00
out_file . write ( " const char *result = NULL; \n " )
2024-01-22 21:21:54 +01:00
out_file . write ( " \n " )
out_file . write ( " switch (HRES_ERROR_V(err_code)) { \n " )
for err in errors :
out_file . write ( f ' case 0x { err . err_code : X } : \n ' )
out_file . write ( f ' result = \" { err . err_define } \" ; \n ' )
out_file . write ( f ' break; \n ' )
2014-03-24 15:02:45 +00:00
out_file . write ( " } \n " )
2024-01-22 21:21:54 +01:00
out_file . write ( " \n " )
2014-03-24 15:02:45 +00:00
out_file . write ( " /* convert & check win32 error space? */ \n " )
out_file . write ( " if (result == NULL && HRESULT_IS_LIKELY_WERR(err_code)) { \n " )
out_file . write ( " WERROR wErr = W_ERROR(WIN32_FROM_HRESULT(err_code)); \n " )
out_file . write ( " result = get_friendly_werror_msg(wErr); \n " )
out_file . write ( " } \n " )
out_file . write ( " return result; \n " )
2024-01-22 20:57:31 +01:00
out_file . write ( " } \n " )
2015-03-06 17:42:06 +01:00
out_file . write ( " \n " )
out_file . write ( " const char *hresult_errstr(HRESULT err_code) \n " )
2023-02-24 12:54:16 +13:00
out_file . write ( " { \n " )
2016-08-22 14:21:25 +02:00
out_file . write ( " static char msg[22]; \n " )
2015-03-06 17:42:06 +01:00
out_file . write ( " \n " )
2024-01-22 21:21:54 +01:00
out_file . write ( " switch (HRES_ERROR_V(err_code)) { \n " )
for err in errors :
out_file . write ( f ' case 0x { err . err_code : X } : \n ' )
out_file . write ( f ' return \" { err . err_string } \" ; \n ' )
out_file . write ( f ' break; \n ' )
2015-03-06 17:42:06 +01:00
out_file . write ( " } \n " )
out_file . write ( " snprintf(msg, sizeof(msg), \" HRES code 0x %08x \" , HRES_ERROR_V(err_code)); \n " )
out_file . write ( " return msg; \n " )
2024-01-22 20:57:31 +01:00
out_file . write ( " } \n " )
2014-03-24 15:02:45 +00:00
2024-01-11 16:23:55 +13:00
def generatePythonFile ( out_file , errors ) :
out_file . write ( " /* \n " )
out_file . write ( " * Errors generated from \n " )
out_file . write ( " * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704587.aspx \n " )
out_file . write ( " */ \n " )
out_file . write ( " #include \" lib/replace/system/python.h \" \n " )
out_file . write ( " #include \" python/py3compat.h \" \n " )
out_file . write ( " #include \" includes.h \" \n \n " )
out_file . write ( " static struct PyModuleDef moduledef = { \n " )
out_file . write ( " \t PyModuleDef_HEAD_INIT, \n " )
out_file . write ( " \t .m_name = \" hresult \" , \n " )
out_file . write ( " \t .m_doc = \" HRESULT defines \" , \n " )
out_file . write ( " \t .m_size = -1, \n " )
out_file . write ( " \t }; \n \n " )
out_file . write ( " MODULE_INIT_FUNC(hresult) \n " )
out_file . write ( " { \n " )
out_file . write ( " \t PyObject *m = NULL; \n " )
out_file . write ( " \t PyObject *py_obj = NULL; \n " )
out_file . write ( " \t int ret; \n \n " )
out_file . write ( " \t m = PyModule_Create(&moduledef); \n " )
out_file . write ( " \t if (m == NULL) { \n " )
out_file . write ( " \t \t return NULL; \n " )
out_file . write ( " \t } \n \n " )
for err in errors :
out_file . write ( f " \t py_obj = PyLong_FromUnsignedLongLong(HRES_ERROR_V( { err . err_define } )); \n " )
out_file . write ( f " \t ret = PyModule_AddObject(m, \" { err . err_define } \" , py_obj); \n " )
out_file . write ( " \t if (ret) { \n " )
out_file . write ( " \t \t Py_XDECREF(py_obj); \n " )
out_file . write ( " \t \t Py_DECREF(m); \n " )
out_file . write ( " \t \t return NULL; \n " )
out_file . write ( " \t } \n " )
out_file . write ( " \n " )
out_file . write ( " \t return m; \n " )
out_file . write ( " } \n " )
2024-01-11 11:19:22 +13:00
def transformErrorName ( error_name ) :
return " HRES_ " + error_name
2014-03-24 15:02:45 +00:00
# Very simple script to generate files hresult.c & hresult.h
2024-01-11 16:23:55 +13:00
# This script takes four inputs:
2024-01-11 11:25:53 +13:00
# [1]: The name of the text file which is the content of an HTML table
# (such as that found at https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/705fb797-2175-4a90-b5a3-3918024b10b8)
# copied and pasted.
# [2]: The name of the output generated header file with HResult #defines
# [3]: The name of the output generated source file with C arrays
2024-01-11 16:23:55 +13:00
# [4]: The name of the output generated python file
2014-03-24 15:02:45 +00:00
def main ( ) :
2023-02-24 12:54:16 +13:00
input_file1 = None
2024-01-11 11:25:53 +13:00
2024-01-11 16:23:55 +13:00
if len ( sys . argv ) == 5 :
2014-03-24 15:02:45 +00:00
input_file1 = sys . argv [ 1 ]
2024-01-11 11:25:53 +13:00
gen_headerfile_name = sys . argv [ 2 ]
gen_sourcefile_name = sys . argv [ 3 ]
2024-01-11 16:23:55 +13:00
gen_pythonfile_name = sys . argv [ 4 ]
2014-03-24 15:02:45 +00:00
else :
2024-01-11 16:23:55 +13:00
print ( " usage: %s winerrorfile headerfile sourcefile pythonfile " % ( sys . argv [ 0 ] ) )
2014-03-24 15:02:45 +00:00
sys . exit ( )
2024-01-11 11:19:22 +13:00
# read in the data
with open ( input_file1 ) as file_contents :
2024-01-11 11:20:59 +13:00
errors = parseErrorDescriptions ( file_contents , False , transformErrorName )
2024-01-11 11:19:22 +13:00
2024-01-11 11:25:53 +13:00
print ( f " writing new header file: { gen_headerfile_name } " )
with open ( gen_headerfile_name , " w " ) as out_file :
2024-01-11 11:23:53 +13:00
generateHeaderFile ( out_file , errors )
2024-01-11 11:25:53 +13:00
print ( f " writing new source file: { gen_sourcefile_name } " )
with open ( gen_sourcefile_name , " w " ) as out_file :
2024-01-11 11:23:53 +13:00
generateSourceFile ( out_file , errors )
2024-01-11 16:23:55 +13:00
print ( f " writing new python file: { gen_pythonfile_name } " )
with open ( gen_pythonfile_name , " w " ) as out_file :
generatePythonFile ( out_file , errors )
2014-03-24 15:02:45 +00:00
if __name__ == ' __main__ ' :
main ( )