ia64/xen-unstable
changeset 1623:7a572a6fa64d
bitkeeper revision 1.1026.1.8 (40e1b09foCFBM0EuIgrSA1uLJrWuzA)
Restructuring the python code and libs - first stage.
Restructuring the python code and libs - first stage.
line diff
1.1 --- a/.rootkeys Tue Jun 29 14:50:09 2004 +0000 1.2 +++ b/.rootkeys Tue Jun 29 18:10:39 2004 +0000 1.3 @@ -151,6 +151,7 @@ 3f108af5VxPkLv13tXpXgoRKALQtXQ linux-2.4 1.4 3e5a4e681xMPdF9xCMwpyfuYMySU5g linux-2.4.26-xen-sparse/mm/mremap.c 1.5 409ba2e7akOFqQUg6Qyg2s28xcXiMg linux-2.4.26-xen-sparse/mm/page_alloc.c 1.6 3e5a4e683HKVU-sxtagrDasRB8eBVw linux-2.4.26-xen-sparse/mm/swapfile.c 1.7 +40e1b09db5mN69Ijj0X_Eol-S7dXiw tools/Make.defs 1.8 3f776bd1Hy9rn69ntXBhPReUFw9IEA tools/Makefile 1.9 401d7e160vaxMBAUSLSicuZ7AQjJ3w tools/examples/Makefile 1.10 401d7e16UgeqroJQTIhwkrDVkoWgZQ tools/examples/README 1.11 @@ -159,49 +160,69 @@ 401d7e16NoWaBGC1RXbBcqAOr5Uaag tools/exa 1.12 405ff55dawQyCHFEnJ067ChPRoXBBA tools/examples/init.d/xend 1.13 40278d94cIUWl2eRgnwZtr4hTyWT1Q tools/examples/init.d/xendomains 1.14 40278d91ZjLhxdjjrGe8HEdwHLj5xQ tools/examples/netbsd 1.15 +40e15b7edWEtBf_oe3eBwGKuh1dyzQ tools/examples/vifctl 1.16 401d7e16NpnVrFSsR7lKKKfTwCYvWA tools/examples/xc_dom_control.py 1.17 401d7e16RJj-lbtsVEjua6HYAIiKiA tools/examples/xc_dom_create.py 1.18 -40c9c468pXANclL7slGaoD0kSrIwoQ tools/examples/xm_dom_create.py 1.19 40cf2937oKlROYOJTN8GWwWM5AmjBg tools/examples/xmdefaults 1.20 40dfd40auJwNnb8NoiSnRkvZaaXkUg tools/examples/xmnetbsd 1.21 -40e033325Sjqs-_4TuzeUEprP_gYFg tools/lib/allocate.c 1.22 -40e03332KYz7o1bn2MG_KPbBlyoIMA tools/lib/allocate.h 1.23 -40e03332IyRttYoXKoJla5qCC514SQ tools/lib/debug.h 1.24 -40e03332qV5tJ-GJZjo-LBCeGuEjJA tools/lib/enum.c 1.25 -40e03332wwMVxfobgA1PSMTSAGLiCw tools/lib/enum.h 1.26 -40e03332p5Dc_owJQRuN72ymJZddFQ tools/lib/file_stream.c 1.27 -40e03332jWfB2viAhLSkq1WK0r_iDQ tools/lib/file_stream.h 1.28 -40e03332rUjNMGg11n2rN6V4DCrvOg tools/lib/gzip_stream.c 1.29 -40e033321O5Qg22haLoq5lpmk4tooQ tools/lib/gzip_stream.h 1.30 -40e03332QrTR96tc6yS2rMBpd2mq1A tools/lib/hash_table.c 1.31 -40e033325KoIb0d_uy8s7b5DUR9fPQ tools/lib/hash_table.h 1.32 -40e03332ihnBGzHykVwZnFmkAppb4g tools/lib/iostream.c 1.33 -40e03332UGwbLR4wsw4ft14p0Yw5pg tools/lib/iostream.h 1.34 -40e0333245DLDzJemeSVBLuutHtzEQ tools/lib/kernel_stream.c 1.35 -40e03332aK0GkgpDdc-PVTkWKTeOBg tools/lib/kernel_stream.h 1.36 -40e03332HJ0cDcZDKDUUT-tEiBWOZw tools/lib/lexis.c 1.37 -40e03332tnH9Ggzxbfi3xY9Vh2hUlg tools/lib/lexis.h 1.38 -40e03332aYIW0BNBh6wXuKKn_P7Yyg tools/lib/lzi_stream.c 1.39 -40e0333233voTffE4cJSMGJARfiSSQ tools/lib/lzi_stream.h 1.40 -40e03332FXuMoUnfsAKSgV8X4rFbYQ tools/lib/lzo_stream.c 1.41 -40e03332InJaiLfpDcIXBy2fI0RFGQ tools/lib/lzo_stream.h 1.42 -40e03332a5SCuRsejHStTuWzMQNv8Q tools/lib/marshal.c 1.43 -40e03332TwKyJrZQiiQfNq4vc2hpgw tools/lib/marshal.h 1.44 -40e033328ccHlJuTR1FswYL_EC6LFA tools/lib/socket_stream.c 1.45 -40e03332P0KVQGkmahj47aafo1X0nA tools/lib/socket_stream.h 1.46 -40e03332KT_tnnoAMbPVAZBB7kSOAQ tools/lib/string_stream.c 1.47 -40e03332-VtK6_OZa1vMHXFil8uq6w tools/lib/string_stream.h 1.48 -40e03332dDtczi6YX7_mMxhYjJeAdQ tools/lib/sxpr.c 1.49 -40e03332QPuyNKDOTIYVvkwK5qO-vg tools/lib/sxpr.h 1.50 -40e03332Pi0_osJ3XPBi38ADPqdl4A tools/lib/sxpr_parser.c 1.51 -40e033324v5QFMvWEXXzv38uUT9kHg tools/lib/sxpr_parser.h 1.52 -40e03332gKUInsqtxQOV4mPiMqf_dg tools/lib/sys_ctype.h 1.53 -40e03332Rkvq6nn_UNjzAAK_Tk9v1g tools/lib/sys_net.c 1.54 -40e03332lQHvQHw4Rh7VsT1_sui29A tools/lib/sys_net.h 1.55 -40e033321smklZd7bDSdWvQCeIshtg tools/lib/sys_string.c 1.56 -40e03332h5V611rRWURRLqb1Ekatxg tools/lib/sys_string.h 1.57 -40e03332u4q5kgF0N7RfqB4s0pZVew tools/lib/xdr.c 1.58 -40e03332hY16nfRXF4gGd5S1aUJUBw tools/lib/xdr.h 1.59 +3fbba6dbDfYvJSsw9500b4SZyUhxjQ tools/libxc/Makefile 1.60 +3fbba6dc1uU7U3IFeF6A-XEOYF2MkQ tools/libxc/rpm.spec 1.61 +3fbba6dcrNxtygEcgJYAJJ1gCQqfsA tools/libxc/xc.h 1.62 +40589968oCfoUlXd460CjVAkBE8IBA tools/libxc/xc_atropos.c 1.63 +3fbba6dbEVkVMX0JuDFzap9jeaucGA tools/libxc/xc_bvtsched.c 1.64 +3fbba6dbasJQV-MVElDC0DGSHMiL5w tools/libxc/xc_domain.c 1.65 +40278d99BLsfUv3qxv0I8C1sClZ0ow tools/libxc/xc_elf.h 1.66 +403e0977Bjsm_e82pwvl9VvaJxh8Gg tools/libxc/xc_evtchn.c 1.67 +40e03333Eegw8czSWvHsbKxrRZJjRA tools/libxc/xc_io.c 1.68 +40e03333vrWGbLAhyJjXlqCHaJt7eA tools/libxc/xc_io.h 1.69 +3fbba6dbNCU7U6nsMYiXzKkp3ztaJg tools/libxc/xc_linux_build.c 1.70 +3fbba6dbl267zZOAVHYLOdLCdhcZMw tools/libxc/xc_linux_restore.c 1.71 +3fbba6db7li3FJiABYtCmuGxOJxEGw tools/libxc/xc_linux_save.c 1.72 +3fbba6db7WnnJr0KFrIFrqNlSKvFYg tools/libxc/xc_misc.c 1.73 +40278d9ctaHVDaEuwhXI3Om2JOjx9w tools/libxc/xc_netbsd_build.c 1.74 +4051bce6CHAsYh8P5t2OHDtRWOP9og tools/libxc/xc_physdev.c 1.75 +3fbba6dctWRWlFJkYb6hdix2X4WMuw tools/libxc/xc_private.c 1.76 +3fbba6dcbVrG2hPzEzwdeV_UC8kydQ tools/libxc/xc_private.h 1.77 +40589968UQFnJeOMn8UIFLbXBuwXjw tools/libxc/xc_rrobin.c 1.78 +40e1b09dMYB4ItGCqcMIzirdMd9I-w tools/libxutil/Makefile 1.79 +40e033325Sjqs-_4TuzeUEprP_gYFg tools/libxutil/allocate.c 1.80 +40e03332KYz7o1bn2MG_KPbBlyoIMA tools/libxutil/allocate.h 1.81 +40e03332IyRttYoXKoJla5qCC514SQ tools/libxutil/debug.h 1.82 +40e03332qV5tJ-GJZjo-LBCeGuEjJA tools/libxutil/enum.c 1.83 +40e03332wwMVxfobgA1PSMTSAGLiCw tools/libxutil/enum.h 1.84 +40e03332p5Dc_owJQRuN72ymJZddFQ tools/libxutil/file_stream.c 1.85 +40e03332jWfB2viAhLSkq1WK0r_iDQ tools/libxutil/file_stream.h 1.86 +40e03332rUjNMGg11n2rN6V4DCrvOg tools/libxutil/gzip_stream.c 1.87 +40e033321O5Qg22haLoq5lpmk4tooQ tools/libxutil/gzip_stream.h 1.88 +40e03332QrTR96tc6yS2rMBpd2mq1A tools/libxutil/hash_table.c 1.89 +40e033325KoIb0d_uy8s7b5DUR9fPQ tools/libxutil/hash_table.h 1.90 +40e03332ihnBGzHykVwZnFmkAppb4g tools/libxutil/iostream.c 1.91 +40e03332UGwbLR4wsw4ft14p0Yw5pg tools/libxutil/iostream.h 1.92 +40e0333245DLDzJemeSVBLuutHtzEQ tools/libxutil/kernel_stream.c 1.93 +40e03332aK0GkgpDdc-PVTkWKTeOBg tools/libxutil/kernel_stream.h 1.94 +40e03332HJ0cDcZDKDUUT-tEiBWOZw tools/libxutil/lexis.c 1.95 +40e03332tnH9Ggzxbfi3xY9Vh2hUlg tools/libxutil/lexis.h 1.96 +40e03332aYIW0BNBh6wXuKKn_P7Yyg tools/libxutil/lzi_stream.c 1.97 +40e0333233voTffE4cJSMGJARfiSSQ tools/libxutil/lzi_stream.h 1.98 +40e03332FXuMoUnfsAKSgV8X4rFbYQ tools/libxutil/lzo_stream.c 1.99 +40e03332InJaiLfpDcIXBy2fI0RFGQ tools/libxutil/lzo_stream.h 1.100 +40e03332a5SCuRsejHStTuWzMQNv8Q tools/libxutil/marshal.c 1.101 +40e03332TwKyJrZQiiQfNq4vc2hpgw tools/libxutil/marshal.h 1.102 +40e033328ccHlJuTR1FswYL_EC6LFA tools/libxutil/socket_stream.c 1.103 +40e03332P0KVQGkmahj47aafo1X0nA tools/libxutil/socket_stream.h 1.104 +40e03332KT_tnnoAMbPVAZBB7kSOAQ tools/libxutil/string_stream.c 1.105 +40e03332-VtK6_OZa1vMHXFil8uq6w tools/libxutil/string_stream.h 1.106 +40e03332dDtczi6YX7_mMxhYjJeAdQ tools/libxutil/sxpr.c 1.107 +40e03332QPuyNKDOTIYVvkwK5qO-vg tools/libxutil/sxpr.h 1.108 +40e03332Pi0_osJ3XPBi38ADPqdl4A tools/libxutil/sxpr_parser.c 1.109 +40e033324v5QFMvWEXXzv38uUT9kHg tools/libxutil/sxpr_parser.h 1.110 +40e03332gKUInsqtxQOV4mPiMqf_dg tools/libxutil/sys_ctype.h 1.111 +40e03332Rkvq6nn_UNjzAAK_Tk9v1g tools/libxutil/sys_net.c 1.112 +40e03332lQHvQHw4Rh7VsT1_sui29A tools/libxutil/sys_net.h 1.113 +40e033321smklZd7bDSdWvQCeIshtg tools/libxutil/sys_string.c 1.114 +40e03332h5V611rRWURRLqb1Ekatxg tools/libxutil/sys_string.h 1.115 +40e03332u4q5kgF0N7RfqB4s0pZVew tools/libxutil/xdr.c 1.116 +40e03332hY16nfRXF4gGd5S1aUJUBw tools/libxutil/xdr.h 1.117 3f776bd2Xd-dUcPKlPN2vG89VGtfvQ tools/misc/Makefile 1.118 40ab2cfawIw8tsYo0dQKtp83h4qfTQ tools/misc/fakei386xen 1.119 3f6dc136ZKOjd8PIqLbFBl_v-rnkGg tools/misc/miniterm/Makefile 1.120 @@ -213,99 +234,76 @@ 3f5ef5a2ir1kVAthS14Dc5QIRCEFWg tools/mis 1.121 3f5ef5a2dTZP0nnsFoeq2jRf3mWDDg tools/misc/xen-clone.README 1.122 3f870808zS6T6iFhqYPGelroZlVfGQ tools/misc/xen_cpuperf.c 1.123 405eedf6_nnNhFQ1I85lhCkLK6jFGA tools/misc/xencons 1.124 +40c9c4697z76HDfkCLdMhmaEwzFoNQ tools/misc/xend 1.125 4056f5155QYZdsk-1fLdjsZPFTnlhg tools/misc/xensymoops.py 1.126 -3fbca441SjQr8vJwTQIgH1laysaWog tools/xc/Makefile 1.127 -3fbba6dbDfYvJSsw9500b4SZyUhxjQ tools/xc/lib/Makefile 1.128 -3fbba6dc1uU7U3IFeF6A-XEOYF2MkQ tools/xc/lib/rpm.spec 1.129 -3fbba6dcrNxtygEcgJYAJJ1gCQqfsA tools/xc/lib/xc.h 1.130 -40589968oCfoUlXd460CjVAkBE8IBA tools/xc/lib/xc_atropos.c 1.131 -3fbba6dbEVkVMX0JuDFzap9jeaucGA tools/xc/lib/xc_bvtsched.c 1.132 -3fbba6dbasJQV-MVElDC0DGSHMiL5w tools/xc/lib/xc_domain.c 1.133 -40278d99BLsfUv3qxv0I8C1sClZ0ow tools/xc/lib/xc_elf.h 1.134 -403e0977Bjsm_e82pwvl9VvaJxh8Gg tools/xc/lib/xc_evtchn.c 1.135 -40e03333Eegw8czSWvHsbKxrRZJjRA tools/xc/lib/xc_io.c 1.136 -40e03333vrWGbLAhyJjXlqCHaJt7eA tools/xc/lib/xc_io.h 1.137 -3fbba6dbNCU7U6nsMYiXzKkp3ztaJg tools/xc/lib/xc_linux_build.c 1.138 -3fbba6dbl267zZOAVHYLOdLCdhcZMw tools/xc/lib/xc_linux_restore.c 1.139 -3fbba6db7li3FJiABYtCmuGxOJxEGw tools/xc/lib/xc_linux_save.c 1.140 -3fbba6db7WnnJr0KFrIFrqNlSKvFYg tools/xc/lib/xc_misc.c 1.141 -40278d9ctaHVDaEuwhXI3Om2JOjx9w tools/xc/lib/xc_netbsd_build.c 1.142 -4051bce6CHAsYh8P5t2OHDtRWOP9og tools/xc/lib/xc_physdev.c 1.143 -3fbba6dctWRWlFJkYb6hdix2X4WMuw tools/xc/lib/xc_private.c 1.144 -3fbba6dcbVrG2hPzEzwdeV_UC8kydQ tools/xc/lib/xc_private.h 1.145 -40589968UQFnJeOMn8UIFLbXBuwXjw tools/xc/lib/xc_rrobin.c 1.146 -3fbd0a3dTwnDcfdw0-v46dPbX98zDw tools/xc/py/Makefile 1.147 -3fbd0a42l40lM0IICw2jXbQBVZSdZg tools/xc/py/Xc.c 1.148 -3fbd0a40yT6G3M9hMpaz5xTUdl0E4g tools/xc/py/setup.py 1.149 -40c9c468icGyC5RAF1bRKsCXPDCvsA tools/xen/Makefile 1.150 -40dc4076hGpwa8-sWRN0jtXZeQJuKg tools/xen/lib/__init__.py 1.151 -40dfd40aMOhnw_cQLve9462UR5yYxQ tools/xen/lib/ext/__init__.py 1.152 -40d8915cyoVA0hJxiBFNymL7YvDaRg tools/xen/lib/util/Brctl.py 1.153 -40dfd40aGqGkiopOOgJxSF4iCbHM0Q tools/xen/lib/util/__init__.py 1.154 -4055ee4dwy4l0MghZosxoiu6zmhc9Q tools/xen/lib/util/console_client.py 1.155 -40c9c468IienauFHQ_xJIcqnPJ8giQ tools/xen/lib/util/ip.py 1.156 -4059c6a0pnxhG8hwSOivXybbGOwuXw tools/xen/lib/util/tempfile.py 1.157 -40c9c468SNuObE_YWARyS0hzTPSzKg tools/xen/lib/xend/Args.py 1.158 -40c9c468Um_qc66OQeLEceIz1pgD5g tools/xen/lib/xend/EventServer.py 1.159 -40c9c468U8EVl0d3G--8YXVg6VJD3g tools/xen/lib/xend/EventTypes.py 1.160 -40c9c468QJTEuk9g4qHxGpmIi70PEQ tools/xen/lib/xend/PrettyPrint.py 1.161 -40e15b7eeQxWE_hUPB2YTgM9fsZ1PQ tools/xen/lib/xend/Vifctl.py 1.162 -40c9c4688m3eqnC8fhLu1APm36VOVA tools/xen/lib/xend/XendClient.py 1.163 -40c9c468t6iIKTjwuYoe-UMCikDcOQ tools/xen/lib/xend/XendConsole.py 1.164 -40c9c468WnXs6eOUSff23IIGI4kMfQ tools/xen/lib/xend/XendDB.py 1.165 -40c9c468fSl3H3IypyT0ppkbb0ZT9A tools/xen/lib/xend/XendDomain.py 1.166 -40c9c468bbKq3uC7_fuNUkiMMjArdw tools/xen/lib/xend/XendDomainConfig.py 1.167 -40c9c4685ykq87_n1kVUbMr9flx9fg tools/xen/lib/xend/XendDomainInfo.py 1.168 -40c9c46854nsHmuxHQHncKk5rAs5NA tools/xen/lib/xend/XendMigrate.py 1.169 -40c9c468M96gA1EYDvNa5w5kQNYLFA tools/xen/lib/xend/XendNode.py 1.170 -40c9c4686jruMyZIqiaZRMiMoqMJtg tools/xen/lib/xend/XendRoot.py 1.171 -40c9c468xzANp6o2D_MeCYwNmOIUsQ tools/xen/lib/xend/XendVnet.py 1.172 -40c9c468x191zetrVlMnExfsQWHxIQ tools/xen/lib/xend/__init__.py 1.173 -40c9c468S2YnCEKmk4ey8XQIST7INg tools/xen/lib/xend/encode.py 1.174 -40c9c468DCpMe542varOolW1Xc68ew tools/xen/lib/xend/server/SrvBase.py 1.175 -40c9c468IxQabrKJSWs0aEjl-27mRQ tools/xen/lib/xend/server/SrvConsole.py 1.176 -40c9c4689Io5bxfbYIfRiUvsiLX0EQ tools/xen/lib/xend/server/SrvConsoleDir.py 1.177 -40c9c468woSmBByfeXA4o_jGf2gCgA tools/xen/lib/xend/server/SrvDaemon.py 1.178 -40c9c468kACsmkqjxBWKHRo071L26w tools/xen/lib/xend/server/SrvDeviceDir.py 1.179 -40c9c468EQZJVkCLds-OhesJVVyZbQ tools/xen/lib/xend/server/SrvDir.py 1.180 -40c9c468TyHZUq8sk0FF_vxM6Sozrg tools/xen/lib/xend/server/SrvDomain.py 1.181 -40c9c469WzajDjutou3X7FmL9hMf3g tools/xen/lib/xend/server/SrvDomainDir.py 1.182 -40c9c469-8mYEJJTAR6w_ClrJRAfwQ tools/xen/lib/xend/server/SrvEventDir.py 1.183 -40c9c4694eu5759Dehr4Uhakei0EMg tools/xen/lib/xend/server/SrvNode.py 1.184 -40c9c469TaZ83ypsrktmPSHLEZiP5w tools/xen/lib/xend/server/SrvRoot.py 1.185 -40c9c469W3sgDMbBJYQdz5wbQweL0Q tools/xen/lib/xend/server/SrvServer.py 1.186 -40c9c469aq7oXrE1Ngqf3_lBqL0RoQ tools/xen/lib/xend/server/SrvVnetDir.py 1.187 -40c9c469Y_aimoOFfUZoS-4eV8gEKg tools/xen/lib/xend/server/__init__.py 1.188 -40c9c4692hckPol_EK0EGB16ZyDsyQ tools/xen/lib/xend/server/blkif.py 1.189 -40c9c469N2-b3GqpLHHHPZykJPLVvA tools/xen/lib/xend/server/channel.py 1.190 -40c9c469hJ_IlatRne-9QEa0-wlquw tools/xen/lib/xend/server/console.py 1.191 -40c9c469UcNJh_NuLU0ytorM0Lk5Ow tools/xen/lib/xend/server/controller.py 1.192 -40c9c469vHh-qLiiubdbKEQbJf18Zw tools/xen/lib/xend/server/cstruct.py 1.193 -40d83983OXjt-y3HjSCcuoPp9rzvmw tools/xen/lib/xend/server/domain.py 1.194 -40c9c469yrm31i60pGKslTi2Zgpotg tools/xen/lib/xend/server/messages.py 1.195 -40c9c46925x-Rjb0Cv2f1-l2jZrPYg tools/xen/lib/xend/server/netif.py 1.196 -40c9c469ZqILEQ8x6yWy0_51jopiCg tools/xen/lib/xend/server/params.py 1.197 -40c9c469LNxLVizOUpOjEaTKKCm8Aw tools/xen/lib/xend/sxp.py 1.198 -40d05079aFRp6NQdo5wIh5Ly31c0cg tools/xen/lib/xm/__init__.py 1.199 -40cf2937gKQcATgXKGtNeWb1PDH5nA tools/xen/lib/xm/create.py 1.200 -40cf2937isyS250zyd0Q2GuEDoNXfQ tools/xen/lib/xm/main.py 1.201 -40cf2937PSslwBliN1g7ofDy2H_RhA tools/xen/lib/xm/opts.py 1.202 -40cf2937Z8WCNOnO2FcWdubvEAF9QQ tools/xen/lib/xm/shutdown.py 1.203 -40c9c469n2RRwCmjWdjdyyVRWKmgWg tools/xen/setup.py 1.204 -40e15b7edWEtBf_oe3eBwGKuh1dyzQ tools/xen/vifctl 1.205 -40c9c4697z76HDfkCLdMhmaEwzFoNQ tools/xen/xend 1.206 -40cf2937dqM1jWW87O5OoOYND8leuA tools/xen/xm 1.207 +40cf2937dqM1jWW87O5OoOYND8leuA tools/misc/xm 1.208 +40c9c468icGyC5RAF1bRKsCXPDCvsA tools/python/Makefile 1.209 +40c9c469n2RRwCmjWdjdyyVRWKmgWg tools/python/setup.py 1.210 +40dc4076hGpwa8-sWRN0jtXZeQJuKg tools/python/xen/__init__.py 1.211 +40dfd40aMOhnw_cQLve9462UR5yYxQ tools/python/xen/ext/__init__.py 1.212 +3fbd0a3dTwnDcfdw0-v46dPbX98zDw tools/python/xen/ext/xc/Makefile 1.213 +3fbd0a40yT6G3M9hMpaz5xTUdl0E4g tools/python/xen/ext/xc/setup.py 1.214 +3fbd0a42l40lM0IICw2jXbQBVZSdZg tools/python/xen/ext/xc/xc.c 1.215 +40dc4076St6AmPTmQPrtQ6LGHPxGmw tools/python/xen/ext/xu/__init__.py 1.216 +40dc4076pVeE1kEEWzcUaNZin65kCA tools/python/xen/ext/xu/domain_controller.h 1.217 +40dc4076CwBYRTUQDdbdU1L6KcLgSw tools/python/xen/ext/xu/xu.c 1.218 +40d8915cyoVA0hJxiBFNymL7YvDaRg tools/python/xen/util/Brctl.py 1.219 +40dfd40aGqGkiopOOgJxSF4iCbHM0Q tools/python/xen/util/__init__.py 1.220 +4055ee4dwy4l0MghZosxoiu6zmhc9Q tools/python/xen/util/console_client.py 1.221 +40c9c468IienauFHQ_xJIcqnPJ8giQ tools/python/xen/util/ip.py 1.222 +4059c6a0pnxhG8hwSOivXybbGOwuXw tools/python/xen/util/tempfile.py 1.223 +40c9c468SNuObE_YWARyS0hzTPSzKg tools/python/xen/xend/Args.py 1.224 +40c9c468Um_qc66OQeLEceIz1pgD5g tools/python/xen/xend/EventServer.py 1.225 +40c9c468U8EVl0d3G--8YXVg6VJD3g tools/python/xen/xend/EventTypes.py 1.226 +40c9c468QJTEuk9g4qHxGpmIi70PEQ tools/python/xen/xend/PrettyPrint.py 1.227 +40e15b7eeQxWE_hUPB2YTgM9fsZ1PQ tools/python/xen/xend/Vifctl.py 1.228 +40c9c4688m3eqnC8fhLu1APm36VOVA tools/python/xen/xend/XendClient.py 1.229 +40c9c468t6iIKTjwuYoe-UMCikDcOQ tools/python/xen/xend/XendConsole.py 1.230 +40c9c468WnXs6eOUSff23IIGI4kMfQ tools/python/xen/xend/XendDB.py 1.231 +40c9c468fSl3H3IypyT0ppkbb0ZT9A tools/python/xen/xend/XendDomain.py 1.232 +40c9c468bbKq3uC7_fuNUkiMMjArdw tools/python/xen/xend/XendDomainConfig.py 1.233 +40c9c4685ykq87_n1kVUbMr9flx9fg tools/python/xen/xend/XendDomainInfo.py 1.234 +40c9c46854nsHmuxHQHncKk5rAs5NA tools/python/xen/xend/XendMigrate.py 1.235 +40c9c468M96gA1EYDvNa5w5kQNYLFA tools/python/xen/xend/XendNode.py 1.236 +40c9c4686jruMyZIqiaZRMiMoqMJtg tools/python/xen/xend/XendRoot.py 1.237 +40c9c468xzANp6o2D_MeCYwNmOIUsQ tools/python/xen/xend/XendVnet.py 1.238 +40c9c468x191zetrVlMnExfsQWHxIQ tools/python/xen/xend/__init__.py 1.239 +40c9c468S2YnCEKmk4ey8XQIST7INg tools/python/xen/xend/encode.py 1.240 +40c9c468DCpMe542varOolW1Xc68ew tools/python/xen/xend/server/SrvBase.py 1.241 +40c9c468IxQabrKJSWs0aEjl-27mRQ tools/python/xen/xend/server/SrvConsole.py 1.242 +40c9c4689Io5bxfbYIfRiUvsiLX0EQ tools/python/xen/xend/server/SrvConsoleDir.py 1.243 +40c9c468woSmBByfeXA4o_jGf2gCgA tools/python/xen/xend/server/SrvDaemon.py 1.244 +40c9c468kACsmkqjxBWKHRo071L26w tools/python/xen/xend/server/SrvDeviceDir.py 1.245 +40c9c468EQZJVkCLds-OhesJVVyZbQ tools/python/xen/xend/server/SrvDir.py 1.246 +40c9c468TyHZUq8sk0FF_vxM6Sozrg tools/python/xen/xend/server/SrvDomain.py 1.247 +40c9c469WzajDjutou3X7FmL9hMf3g tools/python/xen/xend/server/SrvDomainDir.py 1.248 +40c9c469-8mYEJJTAR6w_ClrJRAfwQ tools/python/xen/xend/server/SrvEventDir.py 1.249 +40c9c4694eu5759Dehr4Uhakei0EMg tools/python/xen/xend/server/SrvNode.py 1.250 +40c9c469TaZ83ypsrktmPSHLEZiP5w tools/python/xen/xend/server/SrvRoot.py 1.251 +40c9c469W3sgDMbBJYQdz5wbQweL0Q tools/python/xen/xend/server/SrvServer.py 1.252 +40c9c469aq7oXrE1Ngqf3_lBqL0RoQ tools/python/xen/xend/server/SrvVnetDir.py 1.253 +40c9c469Y_aimoOFfUZoS-4eV8gEKg tools/python/xen/xend/server/__init__.py 1.254 +40c9c4692hckPol_EK0EGB16ZyDsyQ tools/python/xen/xend/server/blkif.py 1.255 +40c9c469N2-b3GqpLHHHPZykJPLVvA tools/python/xen/xend/server/channel.py 1.256 +40c9c469hJ_IlatRne-9QEa0-wlquw tools/python/xen/xend/server/console.py 1.257 +40c9c469UcNJh_NuLU0ytorM0Lk5Ow tools/python/xen/xend/server/controller.py 1.258 +40c9c469vHh-qLiiubdbKEQbJf18Zw tools/python/xen/xend/server/cstruct.py 1.259 +40d83983OXjt-y3HjSCcuoPp9rzvmw tools/python/xen/xend/server/domain.py 1.260 +40c9c469yrm31i60pGKslTi2Zgpotg tools/python/xen/xend/server/messages.py 1.261 +40c9c46925x-Rjb0Cv2f1-l2jZrPYg tools/python/xen/xend/server/netif.py 1.262 +40c9c469ZqILEQ8x6yWy0_51jopiCg tools/python/xen/xend/server/params.py 1.263 +40c9c469LNxLVizOUpOjEaTKKCm8Aw tools/python/xen/xend/sxp.py 1.264 +40d05079aFRp6NQdo5wIh5Ly31c0cg tools/python/xen/xm/__init__.py 1.265 +40cf2937gKQcATgXKGtNeWb1PDH5nA tools/python/xen/xm/create.py 1.266 +40cf2937isyS250zyd0Q2GuEDoNXfQ tools/python/xen/xm/main.py 1.267 +40cf2937PSslwBliN1g7ofDy2H_RhA tools/python/xen/xm/opts.py 1.268 +40cf2937Z8WCNOnO2FcWdubvEAF9QQ tools/python/xen/xm/shutdown.py 1.269 403a3edbrr8RE34gkbR40zep98SXbg tools/xentrace/Makefile 1.270 40a107afN60pFdURgBv9KwEzgRl5mQ tools/xentrace/formats 1.271 4050c413PhhLNAYk3TEwP37i_iLw9Q tools/xentrace/xentrace.8 1.272 403a3edbVpV2E_wq1zeEkJ_n4Uu2eg tools/xentrace/xentrace.c 1.273 403a3edblCUrzSj0mmKhO5HOPrOrSQ tools/xentrace/xentrace_format 1.274 4050c413NtuyIq5lsYJV4P7KIjujXw tools/xentrace/xentrace_format.1 1.275 -40dfd40a0QtsSGigB9TCpVGWZmhlNA tools/xu/Makefile 1.276 -40dc4076St6AmPTmQPrtQ6LGHPxGmw tools/xu/lib/__init__.py 1.277 -40dc4076pVeE1kEEWzcUaNZin65kCA tools/xu/lib/domain_controller.h 1.278 -40dc4076CwBYRTUQDdbdU1L6KcLgSw tools/xu/lib/xu.c 1.279 -40dc4076FyWUYS2nX9YufgglUzKX2A tools/xu/setup.py 1.280 3f72f1bdJPsV3JCnBqs9ddL9tr6D2g xen/COPYING 1.281 3ddb79bcbOVHh38VJzc97-JEGD4dJQ xen/Makefile 1.282 3ddb79bcWnTwYsQRWl_PaneJfa6p0w xen/Rules.mk
2.1 --- a/BitKeeper/etc/ignore Tue Jun 29 14:50:09 2004 +0000 2.2 +++ b/BitKeeper/etc/ignore Tue Jun 29 18:10:39 2004 +0000 2.3 @@ -53,3 +53,22 @@ xen/xen 2.4 xen/xen-syms 2.5 xen/xen.* 2.6 tools/xc/lib/.xc_rrobin.o.d 2.7 +tools/libxc/.xc_atropos.o.d 2.8 +tools/libxc/.xc_bvtsched.o.d 2.9 +tools/libxc/.xc_domain.o.d 2.10 +tools/libxc/.xc_evtchn.o.d 2.11 +tools/libxc/.xc_io.o.d 2.12 +tools/libxc/.xc_linux_build.o.d 2.13 +tools/libxc/.xc_linux_restore.o.d 2.14 +tools/libxc/.xc_linux_save.o.d 2.15 +tools/libxc/.xc_misc.o.d 2.16 +tools/libxc/.xc_netbsd_build.o.d 2.17 +tools/libxc/.xc_physdev.o.d 2.18 +tools/libxc/.xc_private.o.d 2.19 +tools/libxc/.xc_rrobin.o.d 2.20 +tools/libxutil/.allocate.o.d 2.21 +tools/libxutil/.file_stream.o.d 2.22 +tools/libxutil/.gzip_stream.o.d 2.23 +tools/libxutil/.iostream.o.d 2.24 +tools/libxutil/.sys_net.o.d 2.25 +tools/libxutil/.sys_string.o.d
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/tools/Make.defs Tue Jun 29 18:10:39 2004 +0000 3.3 @@ -0,0 +1,7 @@ 3.4 +# -*- mode: Makefile; -*- 3.5 +XEN_HYPERVISOR_IFS = $(XEN_ROOT)/xen/include/hypervisor-ifs 3.6 +XEN_LINUX_INCLUDE = $(XEN_ROOT)/linux-xen-sparse/include 3.7 +XEN_XU = $(XEN_ROOT)/tools/python/xen/ext/xu 3.8 +XEN_XC = $(XEN_ROOT)/tools/python/xen/ext/xc 3.9 +XEN_LIBXC = $(XEN_ROOT)/tools/libxc 3.10 +XEN_LIBXUTIL = $(XEN_ROOT)/tools/libxutil
4.1 --- a/tools/Makefile Tue Jun 29 14:50:09 2004 +0000 4.2 +++ b/tools/Makefile Tue Jun 29 18:10:39 2004 +0000 4.3 @@ -1,29 +1,29 @@ 4.4 4.5 all: 4.6 - $(MAKE) -C xc 4.7 - $(MAKE) -C xu 4.8 + $(MAKE) -C libxutil 4.9 + $(MAKE) -C libxc 4.10 $(MAKE) -C misc 4.11 $(MAKE) -C examples 4.12 $(MAKE) -C xentrace 4.13 - $(MAKE) -C xen 4.14 + $(MAKE) -C python 4.15 4.16 install: all 4.17 - $(MAKE) -C xc install 4.18 - $(MAKE) -C xu install 4.19 + $(MAKE) -C libxutil install 4.20 + $(MAKE) -C libxc install 4.21 $(MAKE) -C misc install 4.22 $(MAKE) -C examples install 4.23 $(MAKE) -C xentrace install 4.24 - $(MAKE) -C xen install 4.25 + $(MAKE) -C python install 4.26 4.27 dist: $(TARGET) 4.28 $(MAKE) prefix=`pwd`/../../install dist=yes install 4.29 4.30 4.31 clean: 4.32 - $(MAKE) -C xc clean 4.33 - $(MAKE) -C xu clean 4.34 + $(MAKE) -C libxutil clean 4.35 + $(MAKE) -C libxc clean 4.36 $(MAKE) -C misc clean 4.37 $(MAKE) -C examples clean 4.38 $(MAKE) -C xentrace clean 4.39 - $(MAKE) -C xen clean 4.40 + $(MAKE) -C python clean 4.41
5.1 --- a/tools/examples/Makefile Tue Jun 29 14:50:09 2004 +0000 5.2 +++ b/tools/examples/Makefile Tue Jun 29 18:10:39 2004 +0000 5.3 @@ -1,21 +1,40 @@ 5.4 5.5 INSTALL = $(wildcard *.py) 5.6 + 5.7 ETC = defaults democd netbsd xmdefaults 5.8 ETCDIR = /etc/xen 5.9 + 5.10 INITD = init.d/xendomains init.d/xend 5.11 5.12 +XEND = vifctl 5.13 +XEND_DIR = $(ETCDIR)/xend 5.14 + 5.15 all: 5.16 5.17 -install: all 5.18 +install: all install-bin install-initd install-etc install-xend 5.19 + 5.20 +install-bin: 5.21 mkdir -p $(prefix)/usr/bin 5.22 + install -m0755 $(INSTALL) $(prefix)/usr/bin 5.23 + 5.24 +install-initd: 5.25 + mkdir -p $(prefix)/etc/init.d 5.26 + install -m0755 $(INITD) $(prefix)/etc/init.d 5.27 + 5.28 +install-etc: 5.29 mkdir -p $(prefix)$(ETCDIR) 5.30 mkdir -p $(prefix)$(ETCDIR)/auto 5.31 - mkdir -p $(prefix)/etc/init.d 5.32 - install -m0755 $(INSTALL) $(prefix)/usr/bin 5.33 for i in $(ETC); \ 5.34 do [ -a $(prefix)/$(ETCDIR)/$$i ] || \ 5.35 install -m0644 $$i $(prefix)$(ETCDIR); \ 5.36 done 5.37 - install -m0755 $(INITD) $(prefix)/etc/init.d 5.38 + 5.39 +install-xend: 5.40 + mkdir -p $(prefix)$(XEND_DIR) 5.41 + for i in $(XEND); \ 5.42 + do [ -a $(prefix)/$(XEND_DIR)/$$i ] || \ 5.43 + install -m0755 $$i $(prefix)$(XEND_DIR); \ 5.44 + done 5.45 + 5.46 5.47 clean:
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/tools/examples/vifctl Tue Jun 29 18:10:39 2004 +0000 6.3 @@ -0,0 +1,149 @@ 6.4 +#!/usr/bin/python 6.5 +# -*- mode: python; -*- 6.6 +#============================================================================ 6.7 +# Xen vif control script. Lives in /etc/xen/xend. 6.8 +# 6.9 +# vifctl init [bridge=<bridge>] [interface=<interface>] 6.10 +# 6.11 +# Called when xend starts up. Default behaviour is to create <bridge> 6.12 +# and add <interface> to it, moving its IP address to <bridge> and adjusting routes. 6.13 +# 6.14 +# vifctl (up|down) vif=<vif> mac=<mac> [bridge=<bridge>] (ipaddr=<ipaddr>)* 6.15 +# 6.16 +# Called when a vif is brought up or down. Default behaviour is to add 6.17 +# the vif to <bridge> on up and remove it from the bridge on down. 6.18 +# If ipaddr is specified, iptables rules for the ip addresses are 6.19 +# added on up and removed on down. The bridge a vif is added to can 6.20 +# be set in the vm config. 6.21 +# 6.22 +# The default bridge is nbe-br. 6.23 +# The default interface is eth0. 6.24 +# 6.25 +#============================================================================ 6.26 + 6.27 +import sys 6.28 +import types 6.29 + 6.30 +from xen.util import Brctl 6.31 + 6.32 +from xen.xend import XendRoot 6.33 +xroot = XendRoot.instance() 6.34 + 6.35 +class VifControl: 6.36 + 6.37 + prefix = 'vifctl_' 6.38 + 6.39 + DEFAULT_BRIDGE = 'nbe-br' 6.40 + DEFAULT_INTERFACE = 'eth0' 6.41 + 6.42 + def __init__(self): 6.43 + self.name = 'vifctl' 6.44 + 6.45 + def main(self, args): 6.46 + #print self.name, args 6.47 + if len(args) < 2: 6.48 + usage(args) 6.49 + self.name = args[0] 6.50 + cmd = self.prefix + args[1] 6.51 + meth = getattr(self, cmd, self.unknown) 6.52 + meth(args[1:]) 6.53 + 6.54 + def usage(self, args, out=sys.stderr): 6.55 + print >>out, 'Missing command, try \n%s help' % self.name 6.56 + 6.57 + def unknown(self, args, out=sys.stderr): 6.58 + print >>out, 'Unknown command:', args[1] 6.59 + self.help(out=out) 6.60 + sys.exit(1) 6.61 + 6.62 + def help(self, out=sys.stdout): 6.63 + print >>out, 'Commands are:', 6.64 + for x in vars(self): 6.65 + if x.startswith(prefix): 6.66 + cmd = x[len(prefix):] 6.67 + print >>out, cmd, 6.68 + print >>out 6.69 + 6.70 + def getparams(self, d, args, req=[]): 6.71 + """Parse args of the form 'key=val'. Valid keys are the ones 6.72 + in the dict 'd' passed in. If entries in 'd' have list values the 6.73 + values of the keys are appended. 6.74 + 6.75 + If 'req' is specified it is a list of required keys. 6.76 + """ 6.77 + for x in args: 6.78 + (k, v) = x.split('=') 6.79 + k = k.strip() 6.80 + v = v.strip() 6.81 + if k not in d: 6.82 + print >>sys.stderr, 'Invalid parameter: ', k 6.83 + sys.exit(1) 6.84 + vold = d[k] 6.85 + if isinstance(vold , types.ListType): 6.86 + d[k] = vold + v 6.87 + else: 6.88 + d[k] = v 6.89 + for x in req: 6.90 + if not d[x]: 6.91 + print >>sys.stderr, 'Missing parameter:', x 6.92 + sys.exit(1) 6.93 + return d 6.94 + 6.95 + def vifctl_help(self, args): 6.96 + self.help() 6.97 + 6.98 + def default_bridge(self): 6.99 + return xroot.get_config_value('bridge', self.DEFAULT_BRIDGE) 6.100 + 6.101 + def default_interface(self): 6.102 + return xroot.get_config_value('interface', self.DEFAULT_INTERFACE) 6.103 + 6.104 + def vifctl_init(self, args): 6.105 + """Entry point for 'vifctl init'. 6.106 + """ 6.107 + d = { 'bridge' : self.default_bridge(), 6.108 + 'interface': self.default_interface() } 6.109 + params = self.getparams(d, args[1:]) 6.110 + interface = params['interface'] 6.111 + bridge = params['bridge'] 6.112 + # Create bridge 'bridge'. 6.113 + Brctl.bridge_create(bridge) 6.114 + # Reconfigure so that 'interface' is added to 'bridge', 6.115 + # and 'bridge' has the IP address from 'interface'. 6.116 + Brctl.reconfigure(interface, bridge) 6.117 + 6.118 + def vifparams(self, args): 6.119 + d = { 'vif' : None, 6.120 + 'mac' : None, 6.121 + 'bridge': self.default_bridge(), 6.122 + 'ipaddr': [] } 6.123 + d = self.getparams(d, args, req=['vif', 'mac']) 6.124 + return d 6.125 + 6.126 + def vifctl_up(self, args): 6.127 + """Entry point for 'vifctl up'. 6.128 + """ 6.129 + params = self.vifparams(args[1:]) 6.130 + # Add the vif to its bridge. 6.131 + Brctl.vif_bridge_add(params) 6.132 + if params['ipaddr']: 6.133 + # Add iptables rules for the ip addresses. 6.134 + vif = params['vif'] 6.135 + for ipaddr in params['ipaddr']: 6.136 + Brctl.vif_restrict_addr(vif, ipaddr) 6.137 + 6.138 + def vifctl_down(self, args): 6.139 + """Entry point for 'vifctl down'. 6.140 + """ 6.141 + params = self.vifparams(args[1:]) 6.142 + # Remove the vif from its bridge. 6.143 + Brctl.vif_bridge_rem(params) 6.144 + if params['ip']: 6.145 + # Remove iptables rules for the ip addresses. 6.146 + vif = params['vif'] 6.147 + for ip in params['ip']: 6.148 + Brctl.vif_restrict_addr(vif, ip, delete=1) 6.149 + 6.150 + 6.151 +if __name__ == "__main__": 6.152 + VifControl().main(sys.argv)
7.1 --- a/tools/examples/xm_dom_create.py Tue Jun 29 14:50:09 2004 +0000 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,406 +0,0 @@ 7.4 -#!/usr/bin/env python 7.5 - 7.6 -import string 7.7 -import sys 7.8 -import os 7.9 -import os.path 7.10 -import time 7.11 -import socket 7.12 -import getopt 7.13 -import signal 7.14 -import syslog 7.15 -import xenctl.ip 7.16 -import xenctl.utils 7.17 -import xenctl.console_client 7.18 - 7.19 -from xenmgr import sxp 7.20 -from xenmgr import PrettyPrint 7.21 -from xenmgr.XendClient import server 7.22 - 7.23 -config_dir = '/etc/xc/' 7.24 -config_file = xc_config_file = config_dir + 'defaults' 7.25 - 7.26 -def main_usage (): 7.27 - print >>sys.stderr,""" 7.28 -Usage: %s <args> 7.29 - 7.30 -This tool is used to create and start new domains. It reads defaults 7.31 -from a file written in Python, having allowed variables to be set and 7.32 -passed into the file. Further command line arguments allow the 7.33 -defaults to be overridden. The defaults for each parameter are listed 7.34 -in [] brackets. Arguments are as follows: 7.35 - 7.36 -Arguments to control the parsing of the defaults file: 7.37 - -f config_file -- Use the specified defaults script. 7.38 - Default: ['%s'] 7.39 - -L state_file -- Load virtual machine memory state from state_file 7.40 - -D foo=bar -- Set variable foo=bar before parsing config 7.41 - E.g. '-D vmid=3;ip=1.2.3.4' 7.42 - -h -- Print extended help message, including all arguments 7.43 - -n -- Dry run only, don't actually create domain 7.44 - Prints the config, suitable for -F. 7.45 - -q -- Quiet - write output only to the system log 7.46 - -F domain_config -- Build domain using the config in the file. 7.47 - Suitable files can be made using '-n' to output a config. 7.48 -""" % (sys.argv[0], xc_config_file) 7.49 - 7.50 -def extra_usage (): 7.51 - print >>sys.stderr,""" 7.52 -Arguments to override current config read from '%s': 7.53 - -c -- Turn into console terminal after domain is created 7.54 - -k image -- Path to kernel image ['%s'] 7.55 - -r ramdisk -- Path to ramdisk (or empty) ['%s'] 7.56 - -b builder_fn -- Function to use to build domain ['%s'] 7.57 - -m mem_size -- Initial memory allocation in MB [%dMB] 7.58 - -N domain_name -- Set textual name of domain ['%s'] 7.59 - -a auto_restart -- Restart domain on exit, yes/no ['%d'] 7.60 - -e vbd_expert -- Safety catch to avoid some disk accidents ['%s'] 7.61 - -d udisk,dev,rw -- Add disk, partition, or virtual disk to domain. E.g. to 7.62 - make partion sda4 available to the domain as hda1 with 7.63 - read-write access: '-d phy:sda4,hda1,rw' To add 7.64 - multiple disks use multiple -d flags or seperate with ';' 7.65 - Default: ['%s'] 7.66 - -i vfr_ipaddr -- Add IP address to the list which Xen will route to 7.67 - the domain. Use multiple times to add more IP addrs. 7.68 - Default: ['%s'] 7.69 - 7.70 -Args to override the kernel command line, which is concatenated from these: 7.71 - -I cmdline_ip -- Override 'ip=ipaddr:nfsserv:gateway:netmask::eth0:off' 7.72 - Default: ['%s'] 7.73 - -R cmdline_root -- Override root device parameters. 7.74 - Default: ['%s'] 7.75 - -E cmdline_extra -- Override extra kernel args and rc script env vars. 7.76 - Default: ['%s'] 7.77 - 7.78 -""" % (config_file, 7.79 - image, ramdisk, builder_fn, mem_size, domain_name, auto_restart, 7.80 - vbd_expert, 7.81 - printvbds( vbd_list ), 7.82 - reduce ( (lambda a,b: a+':'+b), vfr_ipaddr,'' )[1:], 7.83 - cmdline_ip, cmdline_root, cmdline_extra) 7.84 - 7.85 -def config_usage (): pass 7.86 - 7.87 -def answer ( s ): 7.88 - s = string.lower(s) 7.89 - if s == 'yes' or s == 'true' or s == '1': return 1 7.90 - return 0 7.91 - 7.92 -def printvbds ( v ): 7.93 - s='' 7.94 - for (a,b,c) in v: 7.95 - s = s + '; %s,%s,%s' % (a,b,c) 7.96 - return s[2:] 7.97 - 7.98 -def output(string): 7.99 - global quiet 7.100 - syslog.syslog(string) 7.101 - if not quiet: 7.102 - print string 7.103 - return 7.104 - 7.105 -bail=False; dryrun=False; extrahelp=False; quiet = False 7.106 -image=''; ramdisk=''; builder_fn='linux'; restore=0; state_file='' 7.107 -mem_size=0; domain_name=''; vfr_ipaddr=[]; 7.108 -vbd_expert='rr'; auto_restart=False; 7.109 -vbd_list = []; cmdline_ip = ''; cmdline_root=''; cmdline_extra='' 7.110 -pci_device_list = []; console_port = -1 7.111 -auto_console = False 7.112 -config_from_file = False 7.113 - 7.114 -##### Determine location of defaults file 7.115 -##### 7.116 - 7.117 -try: 7.118 - opts, args = getopt.getopt(sys.argv[1:], "h?nqcf:F:D:k:r:b:m:N:a:e:d:i:I:R:E:L:" ) 7.119 - 7.120 - for opt in opts: 7.121 - if opt[0] == '-f': config_file= opt[1] 7.122 - if opt[0] == '-h' or opt[0] == '-?' : bail=True; extrahelp=True 7.123 - if opt[0] == '-n': dryrun=True 7.124 - if opt[0] == '-D': 7.125 - for o in string.split( opt[1], ';' ): 7.126 - (l,r) = string.split( o, '=' ) 7.127 - exec "%s='%s'" % (l,r) 7.128 - if opt[0] == '-q': quiet = True 7.129 - if opt[0] == '-L': restore = True; state_file = opt[1] 7.130 - if opt[0] == '-F': config_from_file = True; domain_config = opt[1] 7.131 - 7.132 - 7.133 -except getopt.GetoptError: 7.134 - bail=True 7.135 - 7.136 -if not config_from_file: 7.137 - try: 7.138 - os.stat( config_file ) 7.139 - except: 7.140 - try: 7.141 - d = config_dir + config_file 7.142 - os.stat( d ) 7.143 - config_file = d 7.144 - except: 7.145 - print >> sys.stderr, "Unable to open config file '%s'" % config_file 7.146 - bail = True 7.147 - 7.148 - 7.149 -##### Parse the config file 7.150 -##### 7.151 - 7.152 -if not config_from_file: 7.153 - if not quiet: 7.154 - print "Parsing config file '%s'" % config_file 7.155 - 7.156 - try: 7.157 - execfile ( config_file ) 7.158 - except (AssertionError,IOError): 7.159 - print >>sys.stderr,"Exiting %s" % sys.argv[0] 7.160 - bail = True 7.161 - 7.162 -##### Print out config if necessary 7.163 -##### 7.164 - 7.165 -def bailout(): 7.166 - global extrahelp 7.167 - main_usage() 7.168 - config_usage() 7.169 - if extrahelp: extra_usage() 7.170 - sys.exit(1) 7.171 - 7.172 -if bail: 7.173 - bailout() 7.174 - 7.175 -##### Parse any command line overrides 7.176 -##### 7.177 - 7.178 -x_vbd_list = [] 7.179 -x_vfr_ipaddr = [] 7.180 - 7.181 -for opt in opts: 7.182 - if opt[0] == '-k': image = opt[1] 7.183 - if opt[0] == '-r': ramdisk = opt[1] 7.184 - if opt[0] == '-b': builder_fn = opt[1] 7.185 - if opt[0] == '-m': mem_size = int(opt[1]) 7.186 - if opt[0] == '-C': cpu = int(opt[1]) 7.187 - if opt[0] == '-N': domain_name = opt[1] 7.188 - if opt[0] == '-a': auto_restart = answer(opt[1]) 7.189 - if opt[0] == '-e': vbd_expert = opt[1] 7.190 - if opt[0] == '-I': cmdline_ip = opt[1] 7.191 - if opt[0] == '-R': cmdline_root = opt[1] 7.192 - if opt[0] == '-E': cmdline_extra = opt[1] 7.193 - if opt[0] == '-i': x_vfr_ipaddr.append(opt[1]) 7.194 - if opt[0] == '-c': auto_console = True 7.195 - if opt[0] == '-d': 7.196 - try: 7.197 - vv = string.split(opt[1],';') 7.198 - for v in vv: 7.199 - (udisk,dev,mode) = string.split(v,',') 7.200 - x_vbd_list.append( (udisk,dev,mode) ) 7.201 - except: 7.202 - print >>sys.stderr, "Invalid block device specification : %s" % opt[1] 7.203 - sys.exit(1) 7.204 - 7.205 -if x_vbd_list: vbd_list = x_vbd_list 7.206 -if x_vfr_ipaddr: vfr_ipaddr = x_vfr_ipaddr 7.207 - 7.208 -syslog.openlog('xc_dom_create.py %s' % config_file, 0, syslog.LOG_DAEMON) 7.209 - 7.210 -def strip(pre, s): 7.211 - if s.startswith(pre): 7.212 - return s[len(pre):] 7.213 - else: 7.214 - return s 7.215 - 7.216 -def make_domain_config(): 7.217 - global builder_fn, image, ramdisk, mem_size, domain_name 7.218 - global cpu 7.219 - global cmdline, cmdline_ip, cmdline_root 7.220 - global vfr_ipaddr, vbd_list, vbd_expert 7.221 - 7.222 - config = ['config', 7.223 - ['name', domain_name ], 7.224 - ['memory', mem_size ], 7.225 - ] 7.226 - if cpu: 7.227 - config.append(['cpu', cpu]) 7.228 - 7.229 - config_image = [ builder_fn ] 7.230 - config_image.append([ 'kernel', os.path.abspath(image) ]) 7.231 - if ramdisk: 7.232 - config_image.append([ 'ramdisk', os.path.abspath(ramdisk) ]) 7.233 - if cmdline_ip: 7.234 - cmdline_ip = strip("ip=", cmdline_ip) 7.235 - config_image.append(['ip', cmdline_ip]) 7.236 - if cmdline_root: 7.237 - cmdline_root = strip("root=", cmdline_root) 7.238 - config_image.append(['root', cmdline_root]) 7.239 - if cmdline_extra: 7.240 - config_image.append(['args', cmdline_extra]) 7.241 - config.append(['image', config_image ]) 7.242 - 7.243 - config_devs = [] 7.244 - for (uname, dev, mode) in vbd_list: 7.245 - config_vbd = ['vbd', 7.246 - ['uname', uname], 7.247 - ['dev', dev ], 7.248 - ['mode', mode ] ] 7.249 - if vbd_expert != 'rr': 7.250 - config_vbd.append(['sharing', vbd_expert]) 7.251 - config_devs.append(['device', config_vbd]) 7.252 - 7.253 - for (bus, dev, func) in pci_device_list: 7.254 - config_pci = ['pci', 7.255 - ['bus', bus ], 7.256 - ['dev', dev ], 7.257 - ['func', func] ] 7.258 - config_devs.append(['device', config_pci]) 7.259 - 7.260 - # Add one vif with unspecified MAC. 7.261 - config_devs.append(['device', ['vif']]) 7.262 - 7.263 - config += config_devs 7.264 - 7.265 - config_vfr = ['vfr'] 7.266 - idx = 0 # No way of saying which IP is for which vif? 7.267 - for ip in vfr_ipaddr: 7.268 - config_vfr.append(['vif', ['id', idx], ['ip', ip]]) 7.269 - 7.270 - config.append(config_vfr) 7.271 - return config 7.272 - 7.273 -def parse_config_file(domain_file): 7.274 - config = None 7.275 - fin = None 7.276 - try: 7.277 - fin = file(domain_file, "rb") 7.278 - config = sxp.parse(fin) 7.279 - if len(config) >= 1: 7.280 - config = config[0] 7.281 - else: 7.282 - raise StandardError("Invalid configuration") 7.283 - except StandardError, ex: 7.284 - print >> sys.stderr, "Error :", ex 7.285 - sys.exit(1) 7.286 - #finally: 7.287 - if fin: fin.close() 7.288 - return config 7.289 - 7.290 -# This function creates, builds and starts a domain, using the values 7.291 -# in the global variables, set above. It is used in the subsequent 7.292 -# code for starting the new domain and rebooting it if appropriate. 7.293 -def make_domain(config): 7.294 - """Create, build and start a domain. 7.295 - Returns: [int] the ID of the new domain. 7.296 - """ 7.297 - global restore 7.298 - 7.299 - if restore: 7.300 - dominfo = server.xend_domain_restore(state_file, config) 7.301 - else: 7.302 - dominfo = server.xend_domain_create(config) 7.303 - 7.304 - dom = int(sxp.child_value(dominfo, 'id')) 7.305 - console_info = sxp.child(dominfo, 'console') 7.306 - if console_info: 7.307 - console_port = int(sxp.child_value(console_info, 'port')) 7.308 - else: 7.309 - console_port = None 7.310 - 7.311 - if server.xend_domain_unpause(dom) < 0: 7.312 - print "Error starting domain" 7.313 - server.xend_domain_halt(dom) 7.314 - sys.exit() 7.315 - 7.316 - return (dom, console_port) 7.317 - 7.318 -PID_DIR = '/var/run/xendomains/' 7.319 - 7.320 -def pidfile(dom): 7.321 - return PID_DIR + '%d.pid' % dom 7.322 - 7.323 -def mkpidfile(): 7.324 - global current_id 7.325 - if not os.path.isdir(PID_DIR): 7.326 - os.mkdir(PID_DIR) 7.327 - 7.328 - fd = open(pidfile(current_id), 'w') 7.329 - print >> fd, str(os.getpid()) 7.330 - fd.close() 7.331 - return 7.332 - 7.333 -def rmpidfile(): 7.334 - global current_id 7.335 - os.unlink(pidfile(current_id)) 7.336 - 7.337 -def death_handler(dummy1,dummy2): 7.338 - global current_id 7.339 - os.unlink(pidfile(current_id)) 7.340 - output('Auto-restart daemon: daemon PID = %d for domain %d is now exiting' 7.341 - % (os.getpid(), current_id)) 7.342 - sys.exit(0) 7.343 - return 7.344 - 7.345 -#============================================================================ 7.346 -# The starting / monitoring of the domain actually happens here... 7.347 - 7.348 -if config_from_file: 7.349 - config = parse_config_file(domain_config) 7.350 -else: 7.351 - config = make_domain_config() 7.352 - 7.353 -if dryrun: 7.354 - print "# %s" % ' '.join(sys.argv) 7.355 - PrettyPrint.prettyprint(config) 7.356 - sys.exit(0) 7.357 -elif quiet: 7.358 - pass 7.359 -else: 7.360 - PrettyPrint.prettyprint(config) 7.361 - 7.362 -# start the domain and record its ID number 7.363 -(current_id, current_port) = make_domain(config) 7.364 - 7.365 -def start_msg(prefix, dom, port): 7.366 - output(prefix + "VM started in domain %d" % dom) 7.367 - if port: 7.368 - output(prefix + "Console I/O available on TCP port %d." % port) 7.369 - 7.370 -start_msg('', current_id, current_port) 7.371 - 7.372 -if current_port and auto_console: 7.373 - xenctl.console_client.connect('127.0.0.1', current_port) 7.374 - 7.375 -# if the auto_restart flag is set then keep polling to see if the domain is 7.376 -# alive - restart if it is not by calling make_domain() again (it's necessary 7.377 -# to update the id variable, since the new domain may have a new ID) 7.378 - 7.379 -#todo: Replace this - get xend to watch them. 7.380 -if auto_restart: 7.381 - ARD = "Auto-restart daemon: " 7.382 - # turn ourselves into a background daemon 7.383 - try: 7.384 - pid = os.fork() 7.385 - if pid > 0: 7.386 - sys.exit(0) 7.387 - os.setsid() 7.388 - pid = os.fork() 7.389 - if pid > 0: 7.390 - output(ARD + 'PID = %d' % pid) 7.391 - sys.exit(0) 7.392 - signal.signal(signal.SIGTERM,death_handler) 7.393 - except OSError: 7.394 - print >> sys.stderr, ARD+'Startup failed' 7.395 - sys.exit(1) 7.396 - 7.397 - mkpidfile() 7.398 - 7.399 - while True: 7.400 - time.sleep(1) 7.401 - # todo: use new interface 7.402 - info = xc.domain_getinfo(current_id, 1) 7.403 - if info == [] or info[0]['dom'] != current_id: 7.404 - output(ARD + "Domain %d terminated, restarting VM in new domain" 7.405 - % current_id) 7.406 - rmpidfile() 7.407 - (current_id, current_port) = make_domain() 7.408 - mkpidfile() 7.409 - start_msg(ARD, current_id, current_port)
8.1 --- a/tools/lib/allocate.c Tue Jun 29 14:50:09 2004 +0000 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,116 +0,0 @@ 8.4 -/* 8.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 8.6 - * 8.7 - * This library is free software; you can redistribute it and/or modify 8.8 - * it under the terms of the GNU Lesser General Public License as published by 8.9 - * the Free Software Foundation; either version 2.1 of the License, or 8.10 - * (at your option) any later version. 8.11 - * 8.12 - * This library is distributed in the hope that it will be useful, 8.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 8.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 8.15 - * GNU Lesser General Public License for more details. 8.16 - * 8.17 - * You should have received a copy of the GNU Lesser General Public License 8.18 - * along with this library; if not, write to the Free Software 8.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 8.20 - */ 8.21 - 8.22 -#include "allocate.h" 8.23 - 8.24 -/** @file 8.25 - * Support for allocating memory. 8.26 - * Usable from user code or kernel code (with __KERNEL__ defined). 8.27 - * In user code will use GC if USE_GC is defined. 8.28 - */ 8.29 - 8.30 -#ifdef __KERNEL__ 8.31 -/*----------------------------------------------------------------------------*/ 8.32 -# include <linux/config.h> 8.33 -# include <linux/slab.h> 8.34 -# include <linux/string.h> 8.35 -# include <linux/types.h> 8.36 - 8.37 -# define DEFAULT_TYPE 0 8.38 -# define MALLOC(n, type) kmalloc(n, type) 8.39 -# define FREE(ptr) kfree(ptr) 8.40 - 8.41 -/*----------------------------------------------------------------------------*/ 8.42 -#else /* ! __KERNEL__ */ 8.43 - 8.44 -# include <stdlib.h> 8.45 -# include <string.h> 8.46 - 8.47 -# define DEFAULT_TYPE 0 8.48 - 8.49 -#ifdef USE_GC 8.50 -# include "gc.h" 8.51 -# define MALLOC(n, typ) GC_malloc(n) 8.52 -# define FREE(ptr) (ptr=NULL) 8.53 -//typedef void *GC_PTR; 8.54 -//GC_PTR (*GC_oom_fn)(size_t n); 8.55 -#else 8.56 -# define MALLOC(n, type) malloc(n) 8.57 -# define FREE(ptr) free(ptr) 8.58 -#endif 8.59 - 8.60 -/*----------------------------------------------------------------------------*/ 8.61 -#endif 8.62 - 8.63 -/** Function to call when memory cannot be allocated. */ 8.64 -AllocateFailedFn *allocate_failed_fn = NULL; 8.65 - 8.66 -/** Allocate memory and zero it. 8.67 - * The type is only relevant when calling from kernel code, 8.68 - * from user code it is ignored. 8.69 - * In kernel code the values accepted by kmalloc can be used: 8.70 - * GFP_USER, GFP_ATOMIC, GFP_KERNEL. 8.71 - * 8.72 - * @param size number of bytes to allocate 8.73 - * @param type memory type to allocate (kernel only) 8.74 - * @return pointer to the allocated memory or zero 8.75 - * if malloc failed 8.76 - */ 8.77 -void *allocate_type(int size, int type){ 8.78 - void *p = MALLOC(size, type); 8.79 - if(p){ 8.80 - memzero(p, size); 8.81 - } else if(allocate_failed_fn){ 8.82 - allocate_failed_fn(size, type); 8.83 - } 8.84 - return p; 8.85 -} 8.86 - 8.87 -/** Allocate memory and zero it. 8.88 - * 8.89 - * @param size number of bytes to allocate 8.90 - * @return pointer to the allocated memory or zero 8.91 - * if malloc failed 8.92 - */ 8.93 -void *allocate(int size){ 8.94 - return allocate_type(size, DEFAULT_TYPE); 8.95 -} 8.96 - 8.97 -/** Free memory allocated by allocate(). 8.98 - * No-op if 'p' is null. 8.99 - * 8.100 - * @param p memory to free 8.101 - */ 8.102 -void deallocate(void *p){ 8.103 - if(p){ 8.104 - FREE(p); 8.105 - } 8.106 -} 8.107 - 8.108 -/** Set bytes to zero. 8.109 - * No-op if 'p' is null. 8.110 - * 8.111 - * @param p memory to zero 8.112 - * @param size number of bytes to zero 8.113 - */ 8.114 -void memzero(void *p, int size){ 8.115 - if(p){ 8.116 - memset(p, 0, (size_t)size); 8.117 - } 8.118 -} 8.119 -
9.1 --- a/tools/lib/allocate.h Tue Jun 29 14:50:09 2004 +0000 9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 9.3 @@ -1,45 +0,0 @@ 9.4 -/* 9.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 9.6 - * 9.7 - * This library is free software; you can redistribute it and/or modify 9.8 - * it under the terms of the GNU Lesser General Public License as published by 9.9 - * the Free Software Foundation; either version 2.1 of the License, or 9.10 - * (at your option) any later version. 9.11 - * 9.12 - * This library is distributed in the hope that it will be useful, 9.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 9.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9.15 - * GNU Lesser General Public License for more details. 9.16 - * 9.17 - * You should have received a copy of the GNU Lesser General Public License 9.18 - * along with this library; if not, write to the Free Software 9.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 9.20 - */ 9.21 - 9.22 -#ifndef _XEN_LIB_ALLOCATE_H_ 9.23 -#define _XEN_LIB_ALLOCATE_H_ 9.24 - 9.25 -/** Allocate memory for a given type, and cast. */ 9.26 -#define ALLOCATE(ctype) (ctype *)allocate(sizeof(ctype)) 9.27 - 9.28 -/** Allocate memory for a given type, and cast. */ 9.29 -#define ALLOCATE_TYPE(ctype, type) (ctype *)allocate(sizeof(ctype)) 9.30 - 9.31 -extern void *allocate_type(int size, int type); 9.32 -extern void *allocate(int size); 9.33 -extern void deallocate(void *); 9.34 -extern void memzero(void *p, int size); 9.35 - 9.36 -typedef void AllocateFailedFn(int size, int type); 9.37 -extern AllocateFailedFn *allocate_failed_fn; 9.38 - 9.39 -#endif /* _XEN_LIB_ALLOCATE_H_ */ 9.40 - 9.41 - 9.42 - 9.43 - 9.44 - 9.45 - 9.46 - 9.47 - 9.48 -
10.1 --- a/tools/lib/debug.h Tue Jun 29 14:50:09 2004 +0000 10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 10.3 @@ -1,72 +0,0 @@ 10.4 -/* 10.5 - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com> 10.6 - * 10.7 - * This library is free software; you can redistribute it and/or modify 10.8 - * it under the terms of the GNU Lesser General Public License as published by 10.9 - * the Free Software Foundation; either version 2.1 of the License, or 10.10 - * (at your option) any later version. 10.11 - * 10.12 - * This library is distributed in the hope that it will be useful, 10.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 10.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10.15 - * GNU Lesser General Public License for more details. 10.16 - * 10.17 - * You should have received a copy of the GNU Lesser General Public License 10.18 - * along with this library; if not, write to the Free Software 10.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 10.20 - */ 10.21 -#ifndef _XEN_LIB_DEBUG_H_ 10.22 -#define _XEN_LIB_DEBUG_H_ 10.23 - 10.24 -#ifndef MODULE_NAME 10.25 -#define MODULE_NAME "" 10.26 -#endif 10.27 - 10.28 -#ifdef __KERNEL__ 10.29 -#include <linux/config.h> 10.30 -#include <linux/kernel.h> 10.31 - 10.32 -#ifdef DEBUG 10.33 - 10.34 -#define dprintf(fmt, args...) printk(KERN_DEBUG "[DBG] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.35 -#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.36 -#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.37 -#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.38 - 10.39 -#else 10.40 - 10.41 -#define dprintf(fmt, args...) do {} while(0) 10.42 -#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME fmt, ##args) 10.43 -#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME fmt, ##args) 10.44 -#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME fmt, ##args) 10.45 - 10.46 -#endif 10.47 - 10.48 -#else 10.49 - 10.50 -#include <stdio.h> 10.51 - 10.52 -#ifdef DEBUG 10.53 - 10.54 -#define dprintf(fmt, args...) fprintf(stdout, "[DBG] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.55 -#define wprintf(fmt, args...) fprintf(stderr, "[WRN] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.56 -#define iprintf(fmt, args...) fprintf(stderr, "[INF] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.57 -#define eprintf(fmt, args...) fprintf(stderr, "[ERR] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) 10.58 - 10.59 -#else 10.60 - 10.61 -#define dprintf(fmt, args...) do {} while(0) 10.62 -#define wprintf(fmt, args...) fprintf(stderr, "[WRN] " MODULE_NAME fmt, ##args) 10.63 -#define iprintf(fmt, args...) fprintf(stderr, "[INF] " MODULE_NAME fmt, ##args) 10.64 -#define eprintf(fmt, args...) fprintf(stderr, "[ERR] " MODULE_NAME fmt, ##args) 10.65 - 10.66 -#endif 10.67 - 10.68 -#endif 10.69 - 10.70 -/** Print format for an IP address. 10.71 - * See NIPQUAD(), HIPQUAD() 10.72 - */ 10.73 -#define IPFMT "%u.%u.%u.%u" 10.74 - 10.75 -#endif /* ! _XEN_LIB_DEBUG_H_ */
11.1 --- a/tools/lib/enum.c Tue Jun 29 14:50:09 2004 +0000 11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 11.3 @@ -1,61 +0,0 @@ 11.4 -/* 11.5 - * Copyright (C) 2002, 2004 Mike Wray <mike.wray@hp.com> 11.6 - * 11.7 - * This library is free software; you can redistribute it and/or modify 11.8 - * it under the terms of the GNU Lesser General Public License as 11.9 - * published by the Free Software Foundation; either version 2.1 of the 11.10 - * License, or (at your option) any later version. This library is 11.11 - * distributed in the hope that it will be useful, but WITHOUT ANY 11.12 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.13 - * FITNESS FOR A PARTICULAR PURPOSE. 11.14 - * See the GNU Lesser General Public License for more details. 11.15 - * 11.16 - * You should have received a copy of the GNU Lesser General Public License 11.17 - * along with this library; if not, write to the Free Software Foundation, 11.18 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 11.19 - */ 11.20 - 11.21 -#ifdef __KERNEL__ 11.22 -#include <linux/errno.h> 11.23 -#else 11.24 -#include <errno.h> 11.25 -#endif 11.26 - 11.27 -#include "sys_string.h" 11.28 -#include "enum.h" 11.29 - 11.30 -/** Map an enum name to its value using a table. 11.31 - * 11.32 - * @param name enum name 11.33 - * @param defs enum definitions 11.34 - * @return enum value or -1 if not known 11.35 - */ 11.36 -int enum_name_to_val(char *name, EnumDef *defs){ 11.37 - int val = -1; 11.38 - for(; defs->name; defs++){ 11.39 - if(!strcmp(defs->name, name)){ 11.40 - val = defs->val; 11.41 - break; 11.42 - } 11.43 - } 11.44 - return val; 11.45 -} 11.46 - 11.47 -/** Map an enum value to its name using a table. 11.48 - * 11.49 - * @param val enum value 11.50 - * @param defs enum definitions 11.51 - * @param defs_n number of definitions 11.52 - * @return enum name or NULL if not known 11.53 - */ 11.54 -char *enum_val_to_name(int val, EnumDef *defs){ 11.55 - char *name = NULL; 11.56 - for(; defs->name; defs++){ 11.57 - if(val == defs->val){ 11.58 - name = defs->name; 11.59 - break; 11.60 - } 11.61 - } 11.62 - return name; 11.63 -} 11.64 -
12.1 --- a/tools/lib/enum.h Tue Jun 29 14:50:09 2004 +0000 12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 12.3 @@ -1,30 +0,0 @@ 12.4 -/* 12.5 - * Copyright (C) 2002, 2004 Mike Wray <mike.wray@hp.com> 12.6 - * 12.7 - * This library is free software; you can redistribute it and/or modify 12.8 - * it under the terms of the GNU Lesser General Public License as 12.9 - * published by the Free Software Foundation; either version 2.1 of the 12.10 - * License, or (at your option) any later version. This library is 12.11 - * distributed in the hope that it will be useful, but WITHOUT ANY 12.12 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.13 - * FITNESS FOR A PARTICULAR PURPOSE. 12.14 - * See the GNU Lesser General Public License for more details. 12.15 - * 12.16 - * You should have received a copy of the GNU Lesser General Public License 12.17 - * along with this library; if not, write to the Free Software Foundation, 12.18 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 12.19 - */ 12.20 - 12.21 -#ifndef _XEN_LIB_ENUM_H_ 12.22 -#define _XEN_LIB_ENUM_H_ 12.23 - 12.24 -/** Mapping of an enum value to a name. */ 12.25 -typedef struct EnumDef { 12.26 - int val; 12.27 - char *name; 12.28 -} EnumDef; 12.29 - 12.30 -extern int enum_name_to_val(char *name, EnumDef *defs); 12.31 -extern char *enum_val_to_name(int val, EnumDef *defs); 12.32 - 12.33 -#endif /* _XEN_LIB_ENUM_H_ */
13.1 --- a/tools/lib/file_stream.c Tue Jun 29 14:50:09 2004 +0000 13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 13.3 @@ -1,202 +0,0 @@ 13.4 -/* 13.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 13.6 - * 13.7 - * This library is free software; you can redistribute it and/or modify 13.8 - * it under the terms of the GNU Lesser General Public License as published by 13.9 - * the Free Software Foundation; either version 2.1 of the License, or 13.10 - * (at your option) any later version. 13.11 - * 13.12 - * This library is distributed in the hope that it will be useful, 13.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 13.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13.15 - * GNU Lesser General Public License for more details. 13.16 - * 13.17 - * You should have received a copy of the GNU Lesser General Public License 13.18 - * along with this library; if not, write to the Free Software 13.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 13.20 - */ 13.21 - 13.22 -/** @file 13.23 - * An IOStream implementation using FILE*. 13.24 - */ 13.25 -#ifndef __KERNEL__ 13.26 -#include <stdio.h> 13.27 -#include <stdlib.h> 13.28 -#include "allocate.h" 13.29 -#include "file_stream.h" 13.30 - 13.31 -static int file_read(IOStream *s, void *buf, size_t n); 13.32 -static int file_write(IOStream *s, const void *buf, size_t n); 13.33 -static int file_error(IOStream *s); 13.34 -static int file_close(IOStream *s); 13.35 -static void file_free(IOStream *s); 13.36 -static int file_flush(IOStream *s); 13.37 - 13.38 -/** Methods used by a FILE* IOStream. */ 13.39 -static const IOMethods file_methods = { 13.40 - read: file_read, 13.41 - write: file_write, 13.42 - error: file_error, 13.43 - close: file_close, 13.44 - free: file_free, 13.45 - flush: file_flush, 13.46 -}; 13.47 - 13.48 -/** IOStream for stdin. */ 13.49 -static IOStream _iostdin = { 13.50 - methods: &file_methods, 13.51 - data: (void*)1, 13.52 -}; 13.53 - 13.54 -/** IOStream for stdout. */ 13.55 -static IOStream _iostdout = { 13.56 - methods: &file_methods, 13.57 - data: (void*)2, 13.58 -}; 13.59 - 13.60 -/** IOStream for stderr. */ 13.61 -static IOStream _iostderr = { 13.62 - methods: &file_methods, 13.63 - data: (void*)3, 13.64 -}; 13.65 - 13.66 -/** IOStream for stdin. */ 13.67 -IOStream *iostdin = &_iostdin; 13.68 - 13.69 -/** IOStream for stdout. */ 13.70 -IOStream *iostdout = &_iostdout; 13.71 - 13.72 -/** IOStream for stderr. */ 13.73 -IOStream *iostderr = &_iostderr; 13.74 - 13.75 -/** Get the underlying FILE*. 13.76 - * 13.77 - * @param s file stream 13.78 - * @return the stream s wraps 13.79 - */ 13.80 -static inline FILE *get_file(IOStream *s){ 13.81 - switch((long)s->data){ 13.82 - case 1: s->data = stdin; break; 13.83 - case 2: s->data = stdout; break; 13.84 - case 3: s->data = stderr; break; 13.85 - } 13.86 - return (FILE*)s->data; 13.87 -} 13.88 - 13.89 -/** Control buffering on the underlying stream, like setvbuf(). 13.90 - * 13.91 - * @param io file stream 13.92 - * @param buf buffer 13.93 - * @param mode buffering mode (see man setvbuf()) 13.94 - * @param size buffer size 13.95 - * @return 0 on success, non-zero otherwise 13.96 - */ 13.97 -int file_stream_setvbuf(IOStream *io, char *buf, int mode, size_t size){ 13.98 - return setvbuf(get_file(io), buf, mode, size); 13.99 -} 13.100 - 13.101 -/** Write to the underlying stream using fwrite(); 13.102 - * 13.103 - * @param stream input 13.104 - * @param buf where to put input 13.105 - * @param n number of bytes to write 13.106 - * @return number of bytes written 13.107 - */ 13.108 -static int file_write(IOStream *s, const void *buf, size_t n){ 13.109 - return fwrite(buf, 1, n, get_file(s)); 13.110 -} 13.111 - 13.112 -/** Read from the underlying stream using fread(); 13.113 - * 13.114 - * @param stream input 13.115 - * @param buf where to put input 13.116 - * @param n number of bytes to read 13.117 - * @return number of bytes read 13.118 - */ 13.119 -static int file_read(IOStream *s, void *buf, size_t n){ 13.120 - return fread(buf, 1, n, get_file(s)); 13.121 -} 13.122 - 13.123 -/** Fush the underlying stream using fflush(). 13.124 - * 13.125 - * @param s file stream 13.126 - * @return 0 on success, error code otherwise 13.127 - */ 13.128 -static int file_flush(IOStream *s){ 13.129 - return fflush(get_file(s)); 13.130 -} 13.131 - 13.132 -/** Check if a stream has an error. 13.133 - * 13.134 - * @param s file stream 13.135 - * @return 1 if has an error, 0 otherwise 13.136 - */ 13.137 -static int file_error(IOStream *s){ 13.138 - return ferror(get_file(s)); 13.139 -} 13.140 - 13.141 -/** Close a file stream. 13.142 - * 13.143 - * @param s file stream to close 13.144 - * @return result of the close 13.145 - */ 13.146 -static int file_close(IOStream *s){ 13.147 - return fclose(get_file(s)); 13.148 -} 13.149 - 13.150 -/** Free a file stream. 13.151 - * 13.152 - * @param s file stream 13.153 - */ 13.154 -static void file_free(IOStream *s){ 13.155 - // Do nothing - fclose does it all? 13.156 -} 13.157 - 13.158 -/** Create an IOStream for a stream. 13.159 - * 13.160 - * @param f stream to wrap 13.161 - * @return new IOStream using f for i/o 13.162 - */ 13.163 -IOStream *file_stream_new(FILE *f){ 13.164 - IOStream *io = ALLOCATE(IOStream); 13.165 - if(io){ 13.166 - io->methods = &file_methods; 13.167 - io->data = (void*)f; 13.168 - } 13.169 - return io; 13.170 -} 13.171 - 13.172 -/** IOStream version of fopen(). 13.173 - * 13.174 - * @param file name of the file to open 13.175 - * @param flags giving the mode to open in (as for fopen()) 13.176 - * @return new stream for the open file, or 0 if failed 13.177 - */ 13.178 -IOStream *file_stream_fopen(const char *file, const char *flags){ 13.179 - IOStream *io = 0; 13.180 - FILE *fin = fopen(file, flags); 13.181 - if(fin){ 13.182 - io = file_stream_new(fin); 13.183 - if(!io){ 13.184 - fclose(fin); 13.185 - //free(fin); // fclose frees ? 13.186 - } 13.187 - } 13.188 - return io; 13.189 -} 13.190 - 13.191 -/** IOStream version of fdopen(). 13.192 - * 13.193 - * @param fd file descriptor 13.194 - * @param flags giving the mode to open in (as for fdopen()) 13.195 - * @return new stream for the open file, or 0 if failed 13.196 - */ 13.197 -IOStream *file_stream_fdopen(int fd, const char *flags){ 13.198 - IOStream *io = 0; 13.199 - FILE *fin = fdopen(fd, flags); 13.200 - if(fin){ 13.201 - io = file_stream_new(fin); 13.202 - } 13.203 - return io; 13.204 -} 13.205 -#endif
14.1 --- a/tools/lib/file_stream.h Tue Jun 29 14:50:09 2004 +0000 14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 14.3 @@ -1,35 +0,0 @@ 14.4 -/* 14.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 14.6 - * 14.7 - * This library is free software; you can redistribute it and/or modify 14.8 - * it under the terms of the GNU Lesser General Public License as published by 14.9 - * the Free Software Foundation; either version 2.1 of the License, or 14.10 - * (at your option) any later version. 14.11 - * 14.12 - * This library is distributed in the hope that it will be useful, 14.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 14.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14.15 - * GNU Lesser General Public License for more details. 14.16 - * 14.17 - * You should have received a copy of the GNU Lesser General Public License 14.18 - * along with this library; if not, write to the Free Software 14.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 14.20 - */ 14.21 - 14.22 -#ifndef _XEN_LIB_FILE_STREAM_H_ 14.23 -#define _XEN_LIB_FILE_STREAM_H_ 14.24 - 14.25 -#ifndef __KERNEL__ 14.26 -#include "iostream.h" 14.27 -#include <stdio.h> 14.28 - 14.29 -extern IOStream *file_stream_new(FILE *f); 14.30 -extern IOStream *file_stream_fopen(const char *file, const char *flags); 14.31 -extern IOStream *file_stream_fdopen(int fd, const char *flags); 14.32 -extern IOStream get_stream_stdout(void); 14.33 -extern IOStream get_stream_stderr(void); 14.34 -extern IOStream get_stream_stdin(void); 14.35 - 14.36 -extern int file_stream_setvbuf(IOStream *io, char *buf, int mode, size_t size); 14.37 -#endif 14.38 -#endif /* !_XEN_LIB_FILE_STREAM_H_ */
15.1 --- a/tools/lib/gzip_stream.c Tue Jun 29 14:50:09 2004 +0000 15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 15.3 @@ -1,171 +0,0 @@ 15.4 -/* $Id: gzip_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $ */ 15.5 -/* 15.6 - * Copyright (C) 2003 Hewlett-Packard Company. 15.7 - * 15.8 - * This library is free software; you can redistribute it and/or modify 15.9 - * it under the terms of the GNU Lesser General Public License as published by 15.10 - * the Free Software Foundation; either version 2.1 of the License, or 15.11 - * (at your option) any later version. 15.12 - * 15.13 - * This library is distributed in the hope that it will be useful, 15.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 15.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15.16 - * GNU Lesser General Public License for more details. 15.17 - * 15.18 - * You should have received a copy of the GNU Lesser General Public License 15.19 - * along with this library; if not, write to the Free Software 15.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15.21 - */ 15.22 - 15.23 -/** @file 15.24 - * An IOStream implementation using zlib gzFile to provide 15.25 - * compression and decompression. 15.26 - */ 15.27 -#ifndef __KERNEL__ 15.28 - 15.29 -#include <stdio.h> 15.30 -#include <stdlib.h> 15.31 - 15.32 -#include "zlib.h" 15.33 - 15.34 -#include "allocate.h" 15.35 -#include "gzip_stream.h" 15.36 - 15.37 -static int gzip_read(IOStream *s, void *buf, size_t n); 15.38 -static int gzip_write(IOStream *s, const void *buf, size_t n); 15.39 -static int gzip_error(IOStream *s); 15.40 -static int gzip_close(IOStream *s); 15.41 -static void gzip_free(IOStream *s); 15.42 -static int gzip_flush(IOStream *s); 15.43 - 15.44 -/** Methods used by a gzFile* IOStream. */ 15.45 -static const IOMethods gzip_methods = { 15.46 - read: gzip_read, 15.47 - write: gzip_write, 15.48 - error: gzip_error, 15.49 - close: gzip_close, 15.50 - free: gzip_free, 15.51 - flush: gzip_flush, 15.52 -}; 15.53 - 15.54 -/** Get the underlying gzFile*. 15.55 - * 15.56 - * @param s gzip stream 15.57 - * @return the stream s wraps 15.58 - */ 15.59 -static inline gzFile get_gzfile(IOStream *s){ 15.60 - return (gzFile)s->data; 15.61 -} 15.62 - 15.63 -/** Write to the underlying stream. 15.64 - * 15.65 - * @param stream destination 15.66 - * @param buf data 15.67 - * @param n number of bytes to write 15.68 - * @return number of bytes written 15.69 - */ 15.70 -static int gzip_write(IOStream *s, const void *buf, size_t n){ 15.71 - return gzwrite(get_gzfile(s), (void*)buf, n); 15.72 -} 15.73 - 15.74 -/** Read from the underlying stream. 15.75 - * 15.76 - * @param stream input 15.77 - * @param buf where to put input 15.78 - * @param n number of bytes to read 15.79 - * @return number of bytes read 15.80 - */ 15.81 -static int gzip_read(IOStream *s, void *buf, size_t n){ 15.82 - return gzread(get_gzfile(s), buf, n); 15.83 -} 15.84 - 15.85 -/** Flush the underlying stream. 15.86 - * 15.87 - * @param s gzip stream 15.88 - * @return 0 on success, error code otherwise 15.89 - */ 15.90 -static int gzip_flush(IOStream *s){ 15.91 - //return gzflush(get_gzfile(s), Z_NO_FLUSH); 15.92 - return gzflush(get_gzfile(s), Z_SYNC_FLUSH); 15.93 - //return gzflush(get_gzfile(s), Z_FULL_FLUSH); 15.94 -} 15.95 - 15.96 -/** Check if a stream has an error. 15.97 - * 15.98 - * @param s gzip stream 15.99 - * @return 1 if has an error, 0 otherwise 15.100 - */ 15.101 -static int gzip_error(IOStream *s){ 15.102 - int err; 15.103 - gzFile *gz = get_gzfile(s); 15.104 - gzerror(gz, &err); 15.105 - return (err == Z_ERRNO ? 1 /* ferror(gzfile(gz)) */ : err); 15.106 -} 15.107 - 15.108 -/** Close a gzip stream. 15.109 - * 15.110 - * @param s gzip stream to close 15.111 - * @return result of the close 15.112 - */ 15.113 -static int gzip_close(IOStream *s){ 15.114 - return gzclose(get_gzfile(s)); 15.115 -} 15.116 - 15.117 -/** Free a gzip stream. 15.118 - * 15.119 - * @param s gzip stream 15.120 - */ 15.121 -static void gzip_free(IOStream *s){ 15.122 - // Do nothing - fclose does it all? 15.123 -} 15.124 - 15.125 -/** Create an IOStream for a gzip stream. 15.126 - * 15.127 - * @param f stream to wrap 15.128 - * @return new IOStream using f for i/o 15.129 - */ 15.130 -IOStream *gzip_stream_new(gzFile *f){ 15.131 - IOStream *io = ALLOCATE(IOStream); 15.132 - if(io){ 15.133 - io->methods = &gzip_methods; 15.134 - io->data = (void*)f; 15.135 - } 15.136 - return io; 15.137 -} 15.138 - 15.139 -/** IOStream version of fopen(). 15.140 - * 15.141 - * @param file name of the file to open 15.142 - * @param flags giving the mode to open in (as for fopen()) 15.143 - * @return new stream for the open file, or NULL if failed 15.144 - */ 15.145 -IOStream *gzip_stream_fopen(const char *file, const char *flags){ 15.146 - IOStream *io = NULL; 15.147 - gzFile *fgz; 15.148 - fgz = gzopen(file, flags); 15.149 - if(fgz){ 15.150 - io = gzip_stream_new(fgz); 15.151 - if(!io){ 15.152 - gzclose(fgz); 15.153 - //free(fgz); // gzclose frees ? 15.154 - } 15.155 - } 15.156 - return io; 15.157 -} 15.158 - 15.159 -/** IOStream version of fdopen(). 15.160 - * 15.161 - * @param fd file descriptor 15.162 - * @param flags giving the mode to open in (as for fdopen()) 15.163 - * @return new stream for the open file, or NULL if failed 15.164 - */ 15.165 -IOStream *gzip_stream_fdopen(int fd, const char *flags){ 15.166 - IOStream *io = NULL; 15.167 - gzFile *fgz; 15.168 - fgz = gzdopen(fd, flags); 15.169 - if(fgz){ 15.170 - io = gzip_stream_new(fgz); 15.171 - } 15.172 - return io; 15.173 -} 15.174 -#endif
16.1 --- a/tools/lib/gzip_stream.h Tue Jun 29 14:50:09 2004 +0000 16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 16.3 @@ -1,31 +0,0 @@ 16.4 -#/* $Id: gzip_stream.h,v 1.3 2003/09/30 15:22:53 mjw Exp $ */ 16.5 -/* 16.6 - * Copyright (C) 2003 Hewlett-Packard Company. 16.7 - * 16.8 - * This library is free software; you can redistribute it and/or modify 16.9 - * it under the terms of the GNU Lesser General Public License as published by 16.10 - * the Free Software Foundation; either version 2.1 of the License, or 16.11 - * (at your option) any later version. 16.12 - * 16.13 - * This library is distributed in the hope that it will be useful, 16.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 16.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16.16 - * GNU Lesser General Public License for more details. 16.17 - * 16.18 - * You should have received a copy of the GNU Lesser General Public License 16.19 - * along with this library; if not, write to the Free Software 16.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16.21 - */ 16.22 - 16.23 -#ifndef _SP_GZIP_STREAM_H_ 16.24 -#define _SP_GZIP_STREAM_H_ 16.25 - 16.26 -#ifndef __KERNEL__ 16.27 -#include "iostream.h" 16.28 -#include "zlib.h" 16.29 - 16.30 -extern IOStream *gzip_stream_new(gzFile *f); 16.31 -extern IOStream *gzip_stream_fopen(const char *file, const char *flags); 16.32 -extern IOStream *gzip_stream_fdopen(int fd, const char *flags); 16.33 -#endif 16.34 -#endif /* !_SP_FILE_STREAM_H_ */
17.1 --- a/tools/lib/hash_table.c Tue Jun 29 14:50:09 2004 +0000 17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 17.3 @@ -1,640 +0,0 @@ 17.4 -/* 17.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 17.6 - * 17.7 - * This library is free software; you can redistribute it and/or modify 17.8 - * it under the terms of the GNU Lesser General Public License as published by 17.9 - * the Free Software Foundation; either version 2.1 of the License, or 17.10 - * (at your option) any later version. 17.11 - * 17.12 - * This library is distributed in the hope that it will be useful, 17.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 17.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17.15 - * GNU Lesser General Public License for more details. 17.16 - * 17.17 - * You should have received a copy of the GNU Lesser General Public License 17.18 - * along with this library; if not, write to the Free Software 17.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17.20 - */ 17.21 - 17.22 -#ifdef __KERNEL__ 17.23 -# include <linux/config.h> 17.24 -# include <linux/module.h> 17.25 -# include <linux/kernel.h> 17.26 -# include <linux/errno.h> 17.27 -#else 17.28 -# include <errno.h> 17.29 -# include <stddef.h> 17.30 -#endif 17.31 - 17.32 -//#include <limits.h> 17.33 - 17.34 -#include "allocate.h" 17.35 -#include "hash_table.h" 17.36 - 17.37 -/** @file 17.38 - * Base support for hashtables. 17.39 - * 17.40 - * Hash codes are reduced modulo the number of buckets to index tables, 17.41 - * so there is no need for hash functions to limit the range of hashcodes. 17.42 - * In fact it is assumed that hashcodes do not change when the number of 17.43 - * buckets in the table changes. 17.44 - */ 17.45 - 17.46 -/*==========================================================================*/ 17.47 -/** Number of bits in half a word. */ 17.48 -//#if __WORDSIZE == 64 17.49 -//#define HALF_WORD_BITS 32 17.50 -//#else 17.51 -#define HALF_WORD_BITS 16 17.52 -//#endif 17.53 - 17.54 -/** Mask for lo half of a word. On 32-bit this is 17.55 - * (1<<16) - 1 = 65535 = 0xffff 17.56 - * It's 4294967295 = 0xffffffff on 64-bit. 17.57 - */ 17.58 -#define LO_HALF_MASK ((1 << HALF_WORD_BITS) - 1) 17.59 - 17.60 -/** Get the lo half of a word. */ 17.61 -#define LO_HALF(x) ((x) & LO_HALF_MASK) 17.62 - 17.63 -/** Get the hi half of a word. */ 17.64 -#define HI_HALF(x) ((x) >> HALF_WORD_BITS) 17.65 - 17.66 -/** Do a full hash on both inputs, using DES-style non-linear scrambling. 17.67 - * Both inputs are replaced with the results of the hash. 17.68 - * 17.69 - * @param pleft input/output word 17.70 - * @param pright input/output word 17.71 - */ 17.72 -void pseudo_des(unsigned long *pleft, unsigned long *pright){ 17.73 - // Bit-rich mixing constant. 17.74 - static const unsigned long a_mixer[] = { 17.75 - 0xbaa96887L, 0x1e17d32cL, 0x03bcdc3cL, 0x0f33d1b2L, }; 17.76 - 17.77 - // Bit-rich mixing constant. 17.78 - static const unsigned long b_mixer[] = { 17.79 - 0x4b0f3b58L, 0xe874f0c3L, 0x6955c5a6L, 0x55a7ca46L, }; 17.80 - 17.81 - // Number of iterations - must be 2 or 4. 17.82 - static const int ncycle = 4; 17.83 - //static const int ncycle = 2; 17.84 - 17.85 - unsigned long left = *pleft, right = *pright; 17.86 - unsigned long v, v_hi, v_lo; 17.87 - int i; 17.88 - 17.89 - for(i=0; i<ncycle; i++){ 17.90 - // Flip some bits in right to get v. 17.91 - v = right; 17.92 - v ^= a_mixer[i]; 17.93 - // Get lo and hi halves of v. 17.94 - v_lo = LO_HALF(v); 17.95 - v_hi = HI_HALF(v); 17.96 - // Non-linear mix of the halves of v. 17.97 - v = ((v_lo * v_lo) + ~(v_hi * v_hi)); 17.98 - // Swap the halves of v. 17.99 - v = (HI_HALF(v) | (LO_HALF(v) << HALF_WORD_BITS)); 17.100 - // Flip some bits. 17.101 - v ^= b_mixer[i]; 17.102 - // More non-linear mixing. 17.103 - v += (v_lo * v_hi); 17.104 - v ^= left; 17.105 - left = right; 17.106 - right = v; 17.107 - } 17.108 - *pleft = left; 17.109 - *pright = right; 17.110 -} 17.111 - 17.112 -/** Hash a string. 17.113 - * 17.114 - * @param s input to hash 17.115 - * @return hashcode 17.116 - */ 17.117 -Hashcode hash_string(char *s){ 17.118 - Hashcode h = 0; 17.119 - if(s){ 17.120 - for( ; *s; s++){ 17.121 - h = hash_2ul(h, *s); 17.122 - } 17.123 - } 17.124 - return h; 17.125 -} 17.126 - 17.127 -/** Get the bucket for a hashcode in a hash table. 17.128 - * 17.129 - * @param table to get bucket from 17.130 - * @param hashcode to get bucket for 17.131 - * @return bucket 17.132 - */ 17.133 -inline HTBucket * get_bucket(HashTable *table, Hashcode hashcode){ 17.134 - return table->buckets + (hashcode % table->buckets_n); 17.135 -} 17.136 - 17.137 -/** Initialize a hash table. 17.138 - * Can be safely called more than once. 17.139 - * 17.140 - * @param table to initialize 17.141 - */ 17.142 -void HashTable_init(HashTable *table){ 17.143 - int i; 17.144 - 17.145 - if(!table->init_done){ 17.146 - table->init_done = 1; 17.147 - table->next_id = 0; 17.148 - for(i=0; i<table->buckets_n; i++){ 17.149 - HTBucket *bucket = get_bucket(table, i); 17.150 - bucket->head = 0; 17.151 - bucket->count = 0; 17.152 - } 17.153 - table->entry_count = 0; 17.154 - } 17.155 -} 17.156 - 17.157 -/** Allocate a new hashtable. 17.158 - * If the number of buckets is not positive the default is used. 17.159 - * The number of buckets should usually be prime. 17.160 - * 17.161 - * @param buckets_n number of buckets 17.162 - * @return new hashtable or null 17.163 - */ 17.164 -HashTable *HashTable_new(int buckets_n){ 17.165 - HashTable *z = ALLOCATE(HashTable); 17.166 - if(!z) goto exit; 17.167 - if(buckets_n <= 0){ 17.168 - buckets_n = HT_BUCKETS_N; 17.169 - } 17.170 - z->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket)); 17.171 - if(!z->buckets){ 17.172 - deallocate(z); 17.173 - z = 0; 17.174 - goto exit; 17.175 - } 17.176 - z->buckets_n = buckets_n; 17.177 - HashTable_init(z); 17.178 - exit: 17.179 - return z; 17.180 -} 17.181 - 17.182 -/** Free a hashtable. 17.183 - * Any entries are removed and freed. 17.184 - * 17.185 - * @param h hashtable (ignored if null) 17.186 - */ 17.187 -void HashTable_free(HashTable *h){ 17.188 - if(h){ 17.189 - HashTable_clear(h); 17.190 - deallocate(h->buckets); 17.191 - deallocate(h); 17.192 - } 17.193 -} 17.194 - 17.195 -/** Push an entry on the list in the bucket for a given hashcode. 17.196 - * 17.197 - * @param table to add entry to 17.198 - * @param hashcode for the entry 17.199 - * @param entry to add 17.200 - */ 17.201 -static inline void push_on_bucket(HashTable *table, Hashcode hashcode, 17.202 - HTEntry *entry){ 17.203 - HTBucket *bucket; 17.204 - HTEntry *old_head; 17.205 - 17.206 - bucket = get_bucket(table, hashcode); 17.207 - old_head = bucket->head; 17.208 - bucket->count++; 17.209 - bucket->head = entry; 17.210 - entry->next = old_head; 17.211 -} 17.212 - 17.213 -/** Change the number of buckets in a hashtable. 17.214 - * No-op if the number of buckets is not positive. 17.215 - * Existing entries are reallocated to buckets based on their hashcodes. 17.216 - * The table is unmodified if the number of buckets cannot be changed. 17.217 - * 17.218 - * @param table hashtable 17.219 - * @param buckets_n new number of buckets 17.220 - * @return 0 on success, error code otherwise 17.221 - */ 17.222 -int HashTable_set_buckets_n(HashTable *table, int buckets_n){ 17.223 - int err = 0; 17.224 - HTBucket *old_buckets = table->buckets; 17.225 - int old_buckets_n = table->buckets_n; 17.226 - int i; 17.227 - 17.228 - if(buckets_n <= 0){ 17.229 - err = -EINVAL; 17.230 - goto exit; 17.231 - } 17.232 - table->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket)); 17.233 - if(!table->buckets){ 17.234 - err = -ENOMEM; 17.235 - table->buckets = old_buckets; 17.236 - goto exit; 17.237 - } 17.238 - table->buckets_n = buckets_n; 17.239 - for(i=0; i<old_buckets_n; i++){ 17.240 - HTBucket *bucket = old_buckets + i; 17.241 - HTEntry *entry, *next; 17.242 - for(entry = bucket->head; entry; entry = next){ 17.243 - next = entry->next; 17.244 - push_on_bucket(table, entry->hashcode, entry); 17.245 - } 17.246 - } 17.247 - deallocate(old_buckets); 17.248 - exit: 17.249 - return err; 17.250 -} 17.251 - 17.252 -/** Adjust the number of buckets so the table is neither too full nor too empty. 17.253 - * The table is unmodified if adjusting fails. 17.254 - * 17.255 - * @param table hash table 17.256 - * @param buckets_min minimum number of buckets (use default if 0 or negative) 17.257 - * @return 0 on success, error code otherwise 17.258 - */ 17.259 -int HashTable_adjust(HashTable *table, int buckets_min){ 17.260 - int buckets_n = 0; 17.261 - int err = 0; 17.262 - if(buckets_min <= 0) buckets_min = HT_BUCKETS_N; 17.263 - if(table->entry_count >= table->buckets_n){ 17.264 - // The table is dense - expand it. 17.265 - buckets_n = 2 * table->buckets_n; 17.266 - } else if((table->buckets_n > buckets_min) && 17.267 - (4 * table->entry_count < table->buckets_n)){ 17.268 - // The table is more than minimum size and sparse - shrink it. 17.269 - buckets_n = 2 * table->entry_count; 17.270 - if(buckets_n < buckets_min) buckets_n = buckets_min; 17.271 - } 17.272 - if(buckets_n){ 17.273 - err = HashTable_set_buckets_n(table, buckets_n); 17.274 - } 17.275 - return err; 17.276 -} 17.277 - 17.278 -/** Allocate a new entry for a given value. 17.279 - * 17.280 - * @param value to put in the entry 17.281 - * @return entry, or 0 on failure 17.282 - */ 17.283 -HTEntry * HTEntry_new(Hashcode hashcode, void *key, void *value){ 17.284 - HTEntry *z = ALLOCATE(HTEntry); 17.285 - if(z){ 17.286 - z->hashcode = hashcode; 17.287 - z->key = key; 17.288 - z->value = value; 17.289 - } 17.290 - return z; 17.291 -} 17.292 - 17.293 -/** Free an entry. 17.294 - * 17.295 - * @param z entry to free 17.296 - */ 17.297 -inline void HTEntry_free(HTEntry *z){ 17.298 - if(z){ 17.299 - deallocate(z); 17.300 - } 17.301 -} 17.302 - 17.303 -/** Free an entry in a hashtable. 17.304 - * The table's entry_free_fn is used is defined, otherwise 17.305 - * the HTEntry itself is freed. 17.306 - * 17.307 - * @param table hashtable 17.308 - * @param entry to free 17.309 - */ 17.310 -inline void HashTable_free_entry(HashTable *table, HTEntry *entry){ 17.311 - if(!entry)return; 17.312 - if(table && table->entry_free_fn){ 17.313 - table->entry_free_fn(table, entry); 17.314 - } else { 17.315 - HTEntry_free(entry); 17.316 - } 17.317 -} 17.318 - 17.319 -/** Get the first entry satisfying a test from the bucket for the 17.320 - * given hashcode. 17.321 - * 17.322 - * @param table to look in 17.323 - * @param hashcode indicates the bucket 17.324 - * @param test_fn test to apply to elements 17.325 - * @param arg first argument to calls to test_fn 17.326 - * @return entry found, or 0 17.327 - */ 17.328 -inline HTEntry * HashTable_find_entry(HashTable *table, Hashcode hashcode, 17.329 - TableTestFn *test_fn, TableArg arg){ 17.330 - HTBucket *bucket; 17.331 - HTEntry *entry = 0; 17.332 - HTEntry *next; 17.333 - 17.334 - bucket = get_bucket(table, hashcode); 17.335 - for(entry = bucket->head; entry; entry = next){ 17.336 - next = entry->next; 17.337 - if(test_fn(arg, table, entry)){ 17.338 - break; 17.339 - } 17.340 - } 17.341 - return entry; 17.342 -} 17.343 - 17.344 -/** Test hashtable keys for equality. 17.345 - * Uses the table's key_equal_fn if defined, otherwise pointer equality. 17.346 - * 17.347 - * @param key1 key to compare 17.348 - * @param key2 key to compare 17.349 - * @return 1 if equal, 0 otherwise 17.350 - */ 17.351 -inline int HashTable_key_equal(HashTable *table, void *key1, void *key2){ 17.352 - return (table->key_equal_fn ? table->key_equal_fn(key1, key2) : key1==key2); 17.353 -} 17.354 - 17.355 -/** Compute the hashcode of a hashtable key. 17.356 - * The table's key_hash_fn is used if defined, otherwise the address of 17.357 - * the key is hashed. 17.358 - * 17.359 - * @param table hashtable 17.360 - * @param key to hash 17.361 - * @return hashcode 17.362 - */ 17.363 -inline Hashcode HashTable_key_hash(HashTable *table, void *key){ 17.364 - return (table->key_hash_fn ? table->key_hash_fn(key) : hash_ul((unsigned long)key)); 17.365 -} 17.366 - 17.367 -/** Test if an entry has a given key. 17.368 - * 17.369 - * @param arg containing key to test for 17.370 - * @param table the entry is in 17.371 - * @param entry to test 17.372 - * @return 1 if the entry has the key, 0 otherwise 17.373 - */ 17.374 -static inline int has_key(TableArg arg, HashTable *table, HTEntry *entry){ 17.375 - return HashTable_key_equal(table, arg.ptr, entry->key); 17.376 -} 17.377 - 17.378 -/** Get an entry with a given key. 17.379 - * 17.380 - * @param table to search 17.381 - * @param key to look for 17.382 - * @return entry if found, null otherwise 17.383 - */ 17.384 -#if 0 17.385 -inline HTEntry * HashTable_get_entry(HashTable *table, void *key){ 17.386 - TableArg arg = { ptr: key }; 17.387 - return HashTable_find_entry(table, HashTable_key_hash(table, key), has_key, arg); 17.388 -} 17.389 -#else 17.390 -inline HTEntry * HashTable_get_entry(HashTable *table, void *key){ 17.391 - Hashcode hashcode; 17.392 - HTBucket *bucket; 17.393 - HTEntry *entry = 0; 17.394 - HTEntry *next; 17.395 - 17.396 - hashcode = HashTable_key_hash(table, key); 17.397 - bucket = get_bucket(table, hashcode); 17.398 - for(entry = bucket->head; entry; entry = next){ 17.399 - next = entry->next; 17.400 - if(HashTable_key_equal(table, key, entry->key)){ 17.401 - break; 17.402 - } 17.403 - } 17.404 - return entry; 17.405 -} 17.406 -#endif 17.407 - 17.408 -/** Get the value of an entry with a given key. 17.409 - * 17.410 - * @param table to search 17.411 - * @param key to look for 17.412 - * @return value if an entry was found, null otherwise 17.413 - */ 17.414 -inline void * HashTable_get(HashTable *table, void *key){ 17.415 - HTEntry *entry = HashTable_get_entry(table, key); 17.416 - return (entry ? entry->value : 0); 17.417 -} 17.418 - 17.419 -/** Print the buckets in a table. 17.420 - * 17.421 - * @param table to print 17.422 - */ 17.423 -void show_buckets(HashTable *table, IOStream *io){ 17.424 - int i,j ; 17.425 - IOStream_print(io, "entry_count=%d buckets_n=%d\n", table->entry_count, table->buckets_n); 17.426 - for(i=0; i<table->buckets_n; i++){ 17.427 - if(0 || table->buckets[i].count>0){ 17.428 - IOStream_print(io, "bucket %3d %3d %10p ", i, 17.429 - table->buckets[i].count, 17.430 - table->buckets[i].head); 17.431 - for(j = table->buckets[i].count; j>0; j--){ 17.432 - IOStream_print(io, "+"); 17.433 - } 17.434 - IOStream_print(io, "\n"); 17.435 - } 17.436 - } 17.437 - HashTable_print(table, io); 17.438 -} 17.439 - 17.440 -/** Print an entry in a table. 17.441 - * 17.442 - * @param entry to print 17.443 - * @param arg a pointer to an IOStream to print to 17.444 - * @return 0 17.445 - */ 17.446 -static int print_entry(TableArg arg, HashTable *table, HTEntry *entry){ 17.447 - IOStream *io = (IOStream*)arg.ptr; 17.448 - IOStream_print(io, " b=%4lx h=%08lx i=%08lx |-> e=%8p k=%8p v=%8p\n", 17.449 - entry->hashcode % table->buckets_n, 17.450 - entry->hashcode, 17.451 - entry->index, 17.452 - entry, entry->key, entry->value); 17.453 - return 0; 17.454 -} 17.455 - 17.456 -/** Print a hash table. 17.457 - * 17.458 - * @param table to print 17.459 - */ 17.460 -void HashTable_print(HashTable *table, IOStream *io){ 17.461 - IOStream_print(io, "{\n"); 17.462 - HashTable_map(table, print_entry, (TableArg){ ptr: io }); 17.463 - IOStream_print(io, "}\n"); 17.464 -} 17.465 -/*==========================================================================*/ 17.466 - 17.467 -/** Get the next entry id to use for a table. 17.468 - * 17.469 - * @param table hash table 17.470 - * @return non-zero entry id 17.471 - */ 17.472 -static inline unsigned long get_next_id(HashTable *table){ 17.473 - unsigned long id; 17.474 - 17.475 - if(table->next_id == 0){ 17.476 - table->next_id = 1; 17.477 - } 17.478 - id = table->next_id++; 17.479 - return id; 17.480 -} 17.481 - 17.482 -/** Add an entry to the bucket for the 17.483 - * given hashcode. 17.484 - * 17.485 - * @param table to insert in 17.486 - * @param hashcode indicates the bucket 17.487 - * @param key to add an entry for 17.488 - * @param value to add an entry for 17.489 - * @return entry on success, 0 on failure 17.490 - */ 17.491 -inline HTEntry * HashTable_add_entry(HashTable *table, Hashcode hashcode, void *key, void *value){ 17.492 - HTEntry *entry = HTEntry_new(hashcode, key, value); 17.493 - if(entry){ 17.494 - entry->index = get_next_id(table); 17.495 - push_on_bucket(table, hashcode, entry); 17.496 - table->entry_count++; 17.497 - } 17.498 - return entry; 17.499 -} 17.500 - 17.501 -/** Move the front entry for a bucket to the correct point in the bucket order as 17.502 - * defined by the order function. If this is called every time a new entry is added 17.503 - * the bucket will be maintained in sorted order. 17.504 - * 17.505 - * @param table to modify 17.506 - * @param hashcode indicates the bucket 17.507 - * @param order entry comparison function 17.508 - * @return 0 if an entry was moved, 1 if not 17.509 - */ 17.510 -int HashTable_order_bucket(HashTable *table, Hashcode hashcode, TableOrderFn *order){ 17.511 - HTEntry *new_entry = NULL, *prev = NULL, *entry = NULL; 17.512 - HTBucket *bucket; 17.513 - int err = 1; 17.514 - 17.515 - bucket = get_bucket(table, hashcode); 17.516 - new_entry = bucket->head; 17.517 - if(!new_entry || !new_entry->next) goto exit; 17.518 - for(entry = new_entry->next; entry; prev = entry, entry = entry->next){ 17.519 - if(order(new_entry, entry) <= 0) break; 17.520 - } 17.521 - if(prev){ 17.522 - err = 0; 17.523 - bucket->head = new_entry->next; 17.524 - new_entry->next = entry; 17.525 - prev->next = new_entry; 17.526 - } 17.527 - exit: 17.528 - return err; 17.529 -} 17.530 - 17.531 -/** Add an entry to a hashtable. 17.532 - * The entry is added to the bucket for its key's hashcode. 17.533 - * 17.534 - * @param table to insert in 17.535 - * @param key to add an entry for 17.536 - * @param value to add an entry for 17.537 - * @return entry on success, 0 on failure 17.538 - */ 17.539 -inline HTEntry * HashTable_add(HashTable *table, void *key, void *value){ 17.540 - return HashTable_add_entry(table, HashTable_key_hash(table, key), key, value); 17.541 -} 17.542 - 17.543 - 17.544 -/** Remove entries satisfying a test from the bucket for the 17.545 - * given hashcode. 17.546 - * 17.547 - * @param table to remove from 17.548 - * @param hashcode indicates the bucket 17.549 - * @param test_fn test to apply to elements 17.550 - * @param arg first argument to calls to test_fn 17.551 - * @return number of entries removed 17.552 - */ 17.553 -inline int HashTable_remove_entry(HashTable *table, Hashcode hashcode, 17.554 - TableTestFn *test_fn, TableArg arg){ 17.555 - HTBucket *bucket; 17.556 - HTEntry *entry, *prev = 0, *next; 17.557 - int removed_count = 0; 17.558 - 17.559 - bucket = get_bucket(table, hashcode); 17.560 - for(entry = bucket->head; entry; entry = next){ 17.561 - next = entry->next; 17.562 - if(test_fn(arg, table, entry)){ 17.563 - if(prev){ 17.564 - prev->next = next; 17.565 - } else { 17.566 - bucket->head = next; 17.567 - } 17.568 - bucket->count--; 17.569 - table->entry_count--; 17.570 - removed_count++; 17.571 - HashTable_free_entry(table, entry); 17.572 - entry = 0; 17.573 - } 17.574 - prev = entry; 17.575 - } 17.576 - return removed_count; 17.577 -} 17.578 - 17.579 -/** Remove entries with a given key. 17.580 - * 17.581 - * @param table to remove from 17.582 - * @param key of entries to remove 17.583 - * @return number of entries removed 17.584 - */ 17.585 -inline int HashTable_remove(HashTable *table, void *key){ 17.586 -#if 1 17.587 - Hashcode hashcode; 17.588 - HTBucket *bucket; 17.589 - HTEntry *entry, *prev = 0, *next; 17.590 - int removed_count = 0; 17.591 - 17.592 - hashcode = HashTable_key_hash(table, key); 17.593 - bucket = get_bucket(table, hashcode); 17.594 - for(entry = bucket->head; entry; entry = next){ 17.595 - next = entry->next; 17.596 - if(HashTable_key_equal(table, key, entry->key)){ 17.597 - if(prev){ 17.598 - prev->next = next; 17.599 - } else { 17.600 - bucket->head = next; 17.601 - } 17.602 - bucket->count--; 17.603 - table->entry_count--; 17.604 - removed_count++; 17.605 - HashTable_free_entry(table, entry); 17.606 - entry = 0; 17.607 - } 17.608 - prev = entry; 17.609 - } 17.610 - return removed_count; 17.611 -#else 17.612 - return HashTable_remove_entry(table, HashTable_key_hash(table, key), 17.613 - has_key, (TableArg){ ptr: key}); 17.614 -#endif 17.615 -} 17.616 - 17.617 -/** Remove (and free) all the entries in a bucket. 17.618 - * 17.619 - * @param bucket to clear 17.620 - */ 17.621 -static inline void bucket_clear(HashTable *table, HTBucket *bucket){ 17.622 - HTEntry *entry, *next; 17.623 - 17.624 - for(entry = bucket->head; entry; entry = next){ 17.625 - next = entry->next; 17.626 - HashTable_free_entry(table, entry); 17.627 - } 17.628 - bucket->head = 0; 17.629 - table->entry_count -= bucket->count; 17.630 - bucket->count = 0; 17.631 -} 17.632 - 17.633 -/** Remove (and free) all the entries in a table. 17.634 - * 17.635 - * @param table to clear 17.636 - */ 17.637 -void HashTable_clear(HashTable *table){ 17.638 - int i, n = table->buckets_n; 17.639 - 17.640 - for(i=0; i<n; i++){ 17.641 - bucket_clear(table, table->buckets + i); 17.642 - } 17.643 -}
18.1 --- a/tools/lib/hash_table.h Tue Jun 29 14:50:09 2004 +0000 18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 18.3 @@ -1,295 +0,0 @@ 18.4 -/* $Id: hash_table.h,v 1.1 2004/03/30 16:21:26 mjw Exp $ */ 18.5 -/* 18.6 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 18.7 - * 18.8 - * This library is free software; you can redistribute it and/or modify 18.9 - * it under the terms of the GNU Lesser General Public License as published by 18.10 - * the Free Software Foundation; either version 2.1 of the License, or 18.11 - * (at your option) any later version. 18.12 - * 18.13 - * This library is distributed in the hope that it will be useful, 18.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 18.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18.16 - * GNU Lesser General Public License for more details. 18.17 - * 18.18 - * You should have received a copy of the GNU Lesser General Public License 18.19 - * along with this library; if not, write to the Free Software 18.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18.21 - */ 18.22 - 18.23 -#ifndef _XEN_LIB_HASH_TABLE_H_ 18.24 -#define _XEN_LIB_HASH_TABLE_H_ 18.25 - 18.26 -#include "iostream.h" 18.27 - 18.28 -typedef unsigned long Hashcode; 18.29 - 18.30 -/** Type used to pass parameters to table functions. */ 18.31 -typedef union TableArg { 18.32 - unsigned long ul; 18.33 - void *ptr; 18.34 -} TableArg; 18.35 - 18.36 -/** An entry in a bucket list. */ 18.37 -typedef struct HTEntry { 18.38 - /** Hashcode of the entry's key. */ 18.39 - Hashcode hashcode; 18.40 - /** Identifier for this entry in the table. */ 18.41 - int index; 18.42 - /** The key for this entry. */ 18.43 - void *key; 18.44 - /** The value in this entry. */ 18.45 - void *value; 18.46 - /** The next entry in the list. */ 18.47 - struct HTEntry *next; 18.48 -} HTEntry; 18.49 - 18.50 -/** A bucket in a rule table. */ 18.51 -typedef struct HTBucket { 18.52 - /** Number of entries in the bucket. */ 18.53 - int count; 18.54 - /** First entry in the bucket (may be null). */ 18.55 - HTEntry *head; 18.56 -} HTBucket; 18.57 - 18.58 -/** Default number of buckets in a hash table. 18.59 - * You want enough buckets so the lists in the buckets will typically be short. 18.60 - * It's a good idea if this is prime, since that will help to spread hashcodes 18.61 - * around the table. 18.62 - */ 18.63 -//#define HT_BUCKETS_N 1 18.64 -//#define HT_BUCKETS_N 3 18.65 -//#define HT_BUCKETS_N 7 18.66 -//#define HT_BUCKETS_N 17 18.67 -//#define HT_BUCKETS_N 97 18.68 -//#define HT_BUCKETS_N 211 18.69 -//#define HT_BUCKETS_N 401 18.70 -#define HT_BUCKETS_N 1021 18.71 - 18.72 -typedef struct HashTable HashTable; 18.73 - 18.74 -/** Type for a function used to select table entries. */ 18.75 -typedef int TableTestFn(TableArg arg, HashTable *table, HTEntry *entry); 18.76 - 18.77 -/** Type for a function to map over table entries. */ 18.78 -typedef int TableMapFn(TableArg arg, HashTable *table, HTEntry *entry); 18.79 - 18.80 -/** Type for a function to free table entries. */ 18.81 -typedef void TableFreeFn(HashTable *table, HTEntry *entry); 18.82 - 18.83 -/** Type for a function to hash table keys. */ 18.84 -typedef Hashcode TableHashFn(void *key); 18.85 - 18.86 -/** Type for a function to test table keys for equality. */ 18.87 -typedef int TableEqualFn(void *key1, void *key2); 18.88 - 18.89 -/** Type for a function to order table entries. */ 18.90 -typedef int TableOrderFn(HTEntry *e1, HTEntry *e2); 18.91 - 18.92 -/** General hash table. 18.93 - * A hash table with a list in each bucket. 18.94 - * Functions can be supplied for freeing entries, hashing keys, and comparing keys. 18.95 - * These all default to 0, when default behaviour treating keys as integers is used. 18.96 - */ 18.97 -struct HashTable { 18.98 - /** Flag indicating whether the table has been initialised. */ 18.99 - int init_done; 18.100 - /** Next value for the id field in inserted rules. */ 18.101 - unsigned long next_id; 18.102 - /** Number of buckets in the bucket array. */ 18.103 - int buckets_n; 18.104 - /** Array of buckets, each with its own list. */ 18.105 - HTBucket *buckets; 18.106 - /** Number of entries in the table. */ 18.107 - int entry_count; 18.108 - /** Function to free keys and values in entries. */ 18.109 - TableFreeFn *entry_free_fn; 18.110 - /** Function to hash keys. */ 18.111 - TableHashFn *key_hash_fn; 18.112 - /** Function to compare keys for equality. */ 18.113 - TableEqualFn *key_equal_fn; 18.114 - /** Place for the user of the table to hang extra data. */ 18.115 - void *user_data; 18.116 -}; 18.117 - 18.118 -extern HashTable *HashTable_new(int bucket_n); 18.119 -extern void HashTable_free(HashTable *table); 18.120 -extern HTEntry * HTEntry_new(Hashcode hashcode, void *key, void *value); 18.121 -extern void HTEntry_free(HTEntry *entry); 18.122 -extern int HashTable_set_bucket_n(HashTable *table, int bucket_n); 18.123 -extern void HashTable_clear(HashTable *table); 18.124 -extern HTEntry * HashTable_add_entry(HashTable *table, Hashcode hashcode, void *key, void *value); 18.125 -extern HTEntry * HashTable_get_entry(HashTable *table, void *key); 18.126 -extern HTEntry * HashTable_add(HashTable *table, void *key, void *value); 18.127 -extern void * HashTable_get(HashTable *table, void *key); 18.128 -extern int HashTable_remove(HashTable *table, void *key); 18.129 -extern HTEntry * HashTable_find_entry(HashTable *table, Hashcode hashcode, 18.130 - TableTestFn *test_fn, TableArg arg); 18.131 -extern int HashTable_remove_entry(HashTable *table, Hashcode hashcode, 18.132 - TableTestFn *test_fn, TableArg arg); 18.133 -//extern int HashTable_map(HashTable *table, TableMapFn *map_fn, TableArg arg); 18.134 -extern void HashTable_print(HashTable *table, IOStream *out); 18.135 -extern int HashTable_set_buckets_n(HashTable *table, int buckets_n); 18.136 -extern int HashTable_adjust(HashTable *table, int buckets_min); 18.137 -extern void pseudo_des(unsigned long *pleft, unsigned long *pright); 18.138 -extern Hashcode hash_string(char *s); 18.139 - 18.140 -extern int HashTable_order_bucket(HashTable *table, Hashcode hashcode, TableOrderFn *order); 18.141 - 18.142 -/** Control whether to use hashing based on DES or simple 18.143 - * hashing. DES hashing is `more random' but much more expensive. 18.144 - */ 18.145 -#define HASH_PSEUDO_DES 0 18.146 - 18.147 -/** Hash a long using a quick and dirty linear congruential random number generator. 18.148 - * See `Numerical Recipes in C', Chapter 7, "An Even Quicker Generator". 18.149 - * 18.150 - * @param a value to hash 18.151 - * @return hashed input 18.152 - */ 18.153 -static inline unsigned long lcrng_hash(unsigned long a){ 18.154 - return (1664525L * a + 1013904223L); 18.155 -} 18.156 - 18.157 -/** Hash an unsigned long. 18.158 - * 18.159 - * @param a input to hash 18.160 - * @return hashcode 18.161 - */ 18.162 -static inline Hashcode hash_ul(unsigned long a){ 18.163 -#if HASH_PSEUDO_DES 18.164 - unsigned long left = a; 18.165 - unsigned long right = 0L; 18.166 - pseudo_des(&left, &right); 18.167 - return right; 18.168 -#else 18.169 - a = lcrng_hash(a); 18.170 - a = lcrng_hash(a); 18.171 - return a; 18.172 -#endif 18.173 -} 18.174 - 18.175 -/** Hash two unsigned longs together. 18.176 - * 18.177 - * @param a input to hash 18.178 - * @param b input to hash 18.179 - * @return hashcode 18.180 - */ 18.181 -static inline Hashcode hash_2ul(unsigned long a, unsigned long b){ 18.182 -#if HASH_PSEUDO_DES 18.183 - unsigned long left = a; 18.184 - unsigned long right = b; 18.185 - pseudo_des(&left, &right); 18.186 - return right; 18.187 -#else 18.188 - a = lcrng_hash(a); 18.189 - a ^= b; 18.190 - a = lcrng_hash(a); 18.191 - return a; 18.192 -#endif 18.193 -} 18.194 - 18.195 -/** Hash a hashcode and an unsigned long together. 18.196 - * 18.197 - * @param a input hashcode 18.198 - * @param b input to hash 18.199 - * @return hashcode 18.200 - */ 18.201 -static inline Hashcode hash_hul(Hashcode a, unsigned long b){ 18.202 -#if HASH_PSEUDO_DES 18.203 - unsigned long left = a; 18.204 - unsigned long right = b; 18.205 - pseudo_des(&left, &right); 18.206 - return right; 18.207 -#else 18.208 - a ^= b; 18.209 - a = lcrng_hash(a); 18.210 - return a; 18.211 -#endif 18.212 -} 18.213 - 18.214 -/** Macro to declare variables for HashTable_for_each() to use. 18.215 - * 18.216 - * @param entry variable that is set to entries in the table 18.217 - */ 18.218 -#define HashTable_for_decl(entry) \ 18.219 - HashTable *_var_table; \ 18.220 - HTBucket *_var_bucket; \ 18.221 - HTBucket *_var_end; \ 18.222 - HTEntry *_var_next; \ 18.223 - HTEntry *entry 18.224 - 18.225 -/** Macro to iterate over the entries in a hashtable. 18.226 - * Must be in a scope where HashTable_for_decl() has been used to declare 18.227 - * variables for it to use. 18.228 - * The variable 'entry' is iterated over entries in the table. 18.229 - * The code produced is syntactically a loop, so it must be followed by 18.230 - * a loop body, typically some statements in braces: 18.231 - * HashTable_for_each(entry, table){ ...loop body... } 18.232 - * 18.233 - * HashTable_for_each() and HashTable_for_decl() cannot be used for nested 18.234 - * loops as variables will clash. 18.235 - * 18.236 - * @note The simplest way to code a direct loop over the entries in a hashtable 18.237 - * is to use a loop over the buckets, with a nested loop over the entries 18.238 - * in a bucket. Using this approach in a macro means the macro contains 18.239 - * an opening brace, and calls to it must be followed by 2 braces! 18.240 - * To avoid this the code has been restructured so that it is a for loop. 18.241 - * So that statements could be used in the test expression of the for loop, 18.242 - * we have used the gcc statement expression extension ({ ... }). 18.243 - * 18.244 - * @param entry variable to iterate over the entries 18.245 - * @param table to iterate over (non-null) 18.246 - */ 18.247 -#define HashTable_for_each(entry, table) \ 18.248 - _var_table = table; \ 18.249 - _var_bucket = _var_table->buckets; \ 18.250 - _var_end = _var_bucket + _var_table->buckets_n; \ 18.251 - for(entry=0, _var_next=0; \ 18.252 - ({ if(_var_next){ \ 18.253 - entry = _var_next; \ 18.254 - _var_next = entry->next; \ 18.255 - } else { \ 18.256 - while(_var_bucket < _var_end){ \ 18.257 - entry = _var_bucket->head; \ 18.258 - _var_bucket++; \ 18.259 - if(entry){ \ 18.260 - _var_next = entry->next; \ 18.261 - break; \ 18.262 - } \ 18.263 - } \ 18.264 - }; \ 18.265 - entry; }); \ 18.266 - entry = _var_next ) 18.267 - 18.268 -/** Map a function over the entries in a table. 18.269 - * Mapping stops when the function returns a non-zero value. 18.270 - * Uses the gcc statement expression extension ({ ... }). 18.271 - * 18.272 - * @param table to map over 18.273 - * @param fn function to apply to entries 18.274 - * @param arg first argument to call the function with 18.275 - * @return 0 if fn always returned 0, first non-zero value otherwise 18.276 - */ 18.277 -#define HashTable_map(table, fn, arg) \ 18.278 - ({ HashTable_for_decl(_var_entry); \ 18.279 - TableArg _var_arg = arg; \ 18.280 - int _var_value = 0; \ 18.281 - HashTable_for_each(_var_entry, table){ \ 18.282 - if((_var_value = fn(_var_arg, _var_table, _var_entry))) break; \ 18.283 - } \ 18.284 - _var_value; }) 18.285 - 18.286 -/** Cast x to the type for a key or value in a hash table. 18.287 - * This avoids compiler warnings when using short integers 18.288 - * as keys or values (especially on 64-bit platforms). 18.289 - */ 18.290 -#define HKEY(x) ((void*)(unsigned long)(x)) 18.291 - 18.292 -/** Cast x from the type for a key or value in a hash table. 18.293 - * to an unsigned long. This avoids compiler warnings when using 18.294 - * short integers as keys or values (especially on 64-bit platforms). 18.295 - */ 18.296 -#define HVAL(x) ((unsigned long)(x)) 18.297 - 18.298 -#endif /* !_XEN_LIB_HASH_TABLE_H_ */
19.1 --- a/tools/lib/iostream.c Tue Jun 29 14:50:09 2004 +0000 19.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 19.3 @@ -1,37 +0,0 @@ 19.4 -#include "iostream.h" 19.5 -#include "sys_string.h" 19.6 - 19.7 -/** Print on a stream, like vfprintf(). 19.8 - * 19.9 - * @param stream to print to 19.10 - * @param format for the print (as fprintf()) 19.11 - * @param args arguments to print 19.12 - * @return result code from the print 19.13 - */ 19.14 -int IOStream_vprint(IOStream *stream, const char *format, va_list args){ 19.15 - char buffer[1024]; 19.16 - int k = sizeof(buffer), n; 19.17 - 19.18 - n = vsnprintf(buffer, k, (char*)format, args); 19.19 - if(n < 0 || n > k ){ 19.20 - n = k; 19.21 - } 19.22 - n = IOStream_write(stream, buffer, n); 19.23 - return n; 19.24 -} 19.25 - 19.26 -/** Print on a stream, like fprintf(). 19.27 - * 19.28 - * @param stream to print to 19.29 - * @param format for the print (as fprintf()) 19.30 - * @return result code from the print 19.31 - */ 19.32 -int IOStream_print(IOStream *stream, const char *format, ...){ 19.33 - va_list args; 19.34 - int result = -1; 19.35 - 19.36 - va_start(args, format); 19.37 - result = IOStream_vprint(stream, format, args); 19.38 - va_end(args); 19.39 - return result; 19.40 -}
20.1 --- a/tools/lib/iostream.h Tue Jun 29 14:50:09 2004 +0000 20.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 20.3 @@ -1,243 +0,0 @@ 20.4 -#ifndef _XC_LINUX_SAVE_H_ 20.5 -#define _XC_LINUX_SAVE_H_ 20.6 - 20.7 -#include <stdarg.h> 20.8 -#include <stdint.h> 20.9 -#include <stddef.h> 20.10 - 20.11 -#ifdef __KERNEL__ 20.12 -#include <linux/errno.h> 20.13 -#else 20.14 -#include <errno.h> 20.15 -#endif 20.16 - 20.17 -#include "allocate.h" 20.18 - 20.19 -/** End of input return value. */ 20.20 -#define IOSTREAM_EOF -1 20.21 - 20.22 -/** An input/output abstraction. 20.23 - */ 20.24 -typedef struct IOStream IOStream; 20.25 - 20.26 -/** Record of the functions to use for operations on an 20.27 - * IOStream implementation. 20.28 - */ 20.29 -typedef struct IOMethods { 20.30 - /** Read function. Called with the user data, buffer to read into 20.31 - * and number of bytes to read. Must return number of bytes read 20.32 - * on success, less than zero on error. 20.33 - */ 20.34 - int (*read)(IOStream *stream, void *buf, size_t n); 20.35 - 20.36 - /** Write function. Called with user data, buffer to write and 20.37 - * number of bytes to write. Must return number of bytes written on 20.38 - * success, less than zero otherwise. 20.39 - */ 20.40 - int (*write)(IOStream *stream, const void *buf, size_t n); 20.41 - 20.42 - int (*flush)(IOStream *s); 20.43 - 20.44 - int (*error)(IOStream *s); 20.45 - 20.46 - int (*close)(IOStream *s); 20.47 - 20.48 - void (*free)(IOStream *s); 20.49 - 20.50 - void (*lock)(IOStream *s); 20.51 - void (*unlock)(IOStream *s); 20.52 - 20.53 -} IOMethods; 20.54 - 20.55 -/** Abstract i/o object. 20.56 - */ 20.57 -struct IOStream { 20.58 - /** Methods to use to implement operations. */ 20.59 - const IOMethods *methods; 20.60 - /** Private state for the implementation. */ 20.61 - const void *data; 20.62 - /** Flag indicating whether the stream is closed. */ 20.63 - int closed; 20.64 - /** Number of bytes written. */ 20.65 - int written; 20.66 - /** Number of bytes read. */ 20.67 - int read; 20.68 -}; 20.69 - 20.70 - 20.71 -/** IOStream version of stdin. */ 20.72 -extern IOStream *iostdin; 20.73 - 20.74 -/** IOStream version of stdout, */ 20.75 -extern IOStream *iostdout; 20.76 - 20.77 -/** IOStream version of stderr. */ 20.78 -extern IOStream *iostderr; 20.79 - 20.80 -extern int IOStream_print(IOStream *io, const char *format, ...); 20.81 -extern int IOStream_vprint(IOStream *io, const char *format, va_list args); 20.82 - 20.83 -/** Read from a stream. 20.84 - * 20.85 - * @param stream input 20.86 - * @param buf where to put input 20.87 - * @param n number of bytes to read 20.88 - * @return if ok, number of bytes read, otherwise negative error code 20.89 - */ 20.90 -static inline int IOStream_read(IOStream *stream, void *buf, size_t n){ 20.91 - int result = 0; 20.92 - if(stream->closed) goto exit; 20.93 - if(!stream->methods || !stream->methods->read){ 20.94 - result = -EINVAL; 20.95 - goto exit; 20.96 - } 20.97 - result = stream->methods->read(stream, buf, n); 20.98 - if(result > 0){ 20.99 - stream->read += result; 20.100 - } 20.101 - exit: 20.102 - return result; 20.103 -} 20.104 - 20.105 -/** Write to a stream. 20.106 - * 20.107 - * @param stream input 20.108 - * @param buf where to put input 20.109 - * @param n number of bytes to write 20.110 - * @return if ok, number of bytes read, otherwise negative error code 20.111 - */ 20.112 -static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){ 20.113 - int result = 0; 20.114 - if(stream->closed) goto exit; 20.115 - if(!stream->methods || !stream->methods->write){ 20.116 - result = -EINVAL; 20.117 - goto exit; 20.118 - } 20.119 - result = stream->methods->write(stream, buf, n); 20.120 - if(result > 0){ 20.121 - stream->written += result; 20.122 - } 20.123 - exit: 20.124 - return result; 20.125 -} 20.126 - 20.127 -/** Flush the stream. 20.128 - * 20.129 - * @param stream stream 20.130 - * @return 0 on success, IOSTREAM_EOF otherwise 20.131 - */ 20.132 -static inline int IOStream_flush(IOStream *stream){ 20.133 - int result = 0; 20.134 - if(stream->closed){ 20.135 - result = IOSTREAM_EOF; 20.136 - } else if(stream->methods->flush){ 20.137 - result = stream->methods->flush(stream); 20.138 - if(result < 0) result = IOSTREAM_EOF; 20.139 - } 20.140 - return result; 20.141 -} 20.142 - 20.143 -/** Check whether the stream has an error. 20.144 - * 20.145 - * @param stream to check 20.146 - * @return 1 for error, 0 otherwise 20.147 - */ 20.148 -static inline int IOStream_error(IOStream *stream){ 20.149 - int err = 0; 20.150 - if(stream->methods && stream->methods->error){ 20.151 - err = stream->methods->error(stream); 20.152 - } 20.153 - return err; 20.154 -} 20.155 - 20.156 -/** Close the stream. 20.157 - * 20.158 - * @param stream to close 20.159 - * @return 1 for error, 0 otherwise 20.160 - */ 20.161 -static inline int IOStream_close(IOStream *stream){ 20.162 - int err = 1; 20.163 - if(stream->methods && stream->methods->close){ 20.164 - err = stream->methods->close(stream); 20.165 - } 20.166 - return err; 20.167 -} 20.168 - 20.169 -/** Test if the stream has been closed. 20.170 - * 20.171 - * @param stream to check 20.172 - * @return 1 if closed, 0 otherwise 20.173 - */ 20.174 -static inline int IOStream_is_closed(IOStream *stream){ 20.175 - return stream->closed; 20.176 -} 20.177 - 20.178 -/** Free the memory used by the stream. 20.179 - * 20.180 - * @param stream to free 20.181 - */ 20.182 -static inline void IOStream_free(IOStream *stream){ 20.183 - if(stream->methods && stream->methods->free){ 20.184 - stream->methods->free(stream); 20.185 - } 20.186 - *stream = (IOStream){}; 20.187 - deallocate(stream); 20.188 -} 20.189 - 20.190 - 20.191 -/** Print a character to a stream, like fputc(). 20.192 - * 20.193 - * @param stream to print to 20.194 - * @param c character to print 20.195 - * @return result code from the print 20.196 - */ 20.197 -static inline int IOStream_putc(IOStream *stream, int c){ 20.198 - int err; 20.199 - unsigned char b = (unsigned char)c; 20.200 - err = IOStream_write(stream, &b, 1); 20.201 - if(err < 1){ 20.202 - err = IOSTREAM_EOF; 20.203 - } else { 20.204 - err = b; 20.205 - } 20.206 - return err; 20.207 -} 20.208 - 20.209 -/** Read from a stream, like fgetc(). 20.210 - * 20.211 - * @param stream to read from 20.212 - * @return IOSTREAM_EOF on error, character read otherwise 20.213 - */ 20.214 -static inline int IOStream_getc(IOStream *stream){ 20.215 - int err, rc; 20.216 - unsigned char b; 20.217 - 20.218 - err = IOStream_read(stream, &b, 1); 20.219 - if(err < 1){ 20.220 - rc = IOSTREAM_EOF; 20.221 - } else { 20.222 - rc = b; 20.223 - } 20.224 - return rc; 20.225 -} 20.226 - 20.227 -/** Get number of bytes read. 20.228 - * 20.229 - * @param stream to get from 20.230 - * @return number of bytes read 20.231 - */ 20.232 -static inline int IOStream_get_read(IOStream *stream){ 20.233 - return stream->read; 20.234 -} 20.235 - 20.236 -/** Get number of bytes written. 20.237 - * 20.238 - * @param stream to get from 20.239 - * @return number of bytes written 20.240 - */ 20.241 -static inline int IOStream_get_written(IOStream *stream){ 20.242 - return stream->written; 20.243 -} 20.244 - 20.245 - 20.246 -#endif /* ! _XC_LINUX_SAVE_H_ */
21.1 --- a/tools/lib/kernel_stream.c Tue Jun 29 14:50:09 2004 +0000 21.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 21.3 @@ -1,177 +0,0 @@ 21.4 -/* 21.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 21.6 - * 21.7 - * This library is free software; you can redistribute it and/or modify 21.8 - * it under the terms of the GNU Lesser General Public License as published by 21.9 - * the Free Software Foundation; either version 2.1 of the License, or 21.10 - * (at your option) any later version. 21.11 - * 21.12 - * This library is distributed in the hope that it will be useful, 21.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 21.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21.15 - * GNU Lesser General Public License for more details. 21.16 - * 21.17 - * You should have received a copy of the GNU Lesser General Public License 21.18 - * along with this library; if not, write to the Free Software 21.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21.20 - */ 21.21 - 21.22 -/** @file 21.23 - * An IOStream implementation using printk() for output. 21.24 - * Input is not implemented. 21.25 - */ 21.26 -#ifdef __KERNEL__ 21.27 - 21.28 -#include <linux/config.h> 21.29 -#include <linux/module.h> 21.30 -#include <linux/kernel.h> 21.31 -#include <linux/types.h> 21.32 -#include <linux/errno.h> 21.33 -#include <linux/slab.h> 21.34 -#include <linux/spinlock.h> 21.35 - 21.36 -#include "kernel_stream.h" 21.37 -#include "allocate.h" 21.38 - 21.39 -/** Number of characters in the output buffer. 21.40 - * The kernel uses 1024 for printk, so that should suffice. 21.41 - */ 21.42 -#define BUF_N 1024 21.43 - 21.44 -/** State for a kernel stream. */ 21.45 -typedef struct KernelData { 21.46 - /** Stream lock. We need a lock to serialize access to the stream. */ 21.47 - spinlock_t lock; 21.48 - /** Saved flags for locking. */ 21.49 - unsigned long flags; 21.50 - /** Size of the output buffer. */ 21.51 - int buf_n; 21.52 - /** Output buffer. */ 21.53 - char buf[BUF_N]; 21.54 -} KernelData; 21.55 - 21.56 -static int kernel_write(IOStream *s, const char *msg, int n); 21.57 -static void kernel_free(IOStream *s); 21.58 -static void kernel_stream_lock(IOStream *s); 21.59 -static void kernel_stream_unlock(IOStream *s); 21.60 - 21.61 -/** Methods for a kernel stream. Output only. */ 21.62 -static const IOMethods kernel_methods = { 21.63 - write: kernel_write, 21.64 - free: kernel_free, 21.65 - lock: kernel_stream_lock, 21.66 - unlock: kernel_stream_unlock, 21.67 -}; 21.68 - 21.69 -/** Shared state for kernel streams. 21.70 - * All implementations write using printk, so we can use 21.71 - * shared state and avoid allocating it. 21.72 - */ 21.73 -static const KernelData kernel_data = { 21.74 - lock: SPIN_LOCK_UNLOCKED, 21.75 - flags: 0, 21.76 - buf_n: BUF_N, 21.77 -}; 21.78 - 21.79 -/** Stream for kernel printk. */ 21.80 -static IOStream iokernel = { 21.81 - methods: &kernel_methods, 21.82 - data: &kernel_data, 21.83 -}; 21.84 - 21.85 -/** Stream for kernel printk. */ 21.86 -IOStream *iostdout = &iokernel; 21.87 - 21.88 -/** Stream for kernel printk. */ 21.89 -IOStream *iostdin = &iokernel; 21.90 - 21.91 -/** Stream for kernel printk. */ 21.92 -IOStream *iostderr = &iokernel; 21.93 - 21.94 -/** Get an output-only stream implementation using 21.95 - * printk(). The stream uses static storage, and must not be freed. 21.96 - * 21.97 - * @return kernel stream 21.98 - */ 21.99 -IOStream get_stream_kernel(void){ 21.100 - return iokernel; 21.101 -} 21.102 - 21.103 -/** Obtain the lock on the stream state. 21.104 - * 21.105 - * @param kdata stream state 21.106 - */ 21.107 -static inline void KernelData_lock(KernelData *kdata){ 21.108 - spin_lock_irqsave(&kdata->lock, kdata->flags); 21.109 -} 21.110 - 21.111 -/** Release the lock on the stream state. 21.112 - * 21.113 - * @param kdata stream state 21.114 - */ 21.115 -static inline void KernelData_unlock(KernelData *kdata){ 21.116 - spin_unlock_irqrestore(&kdata->lock, kdata->flags); 21.117 -} 21.118 - 21.119 -/** Get the stream state. 21.120 - * 21.121 - * @param s kernel stream 21.122 - * @return stream state 21.123 - */ 21.124 -static inline KernelData *get_kernel_data(IOStream *s){ 21.125 - return (KernelData*)s->data; 21.126 -} 21.127 - 21.128 -/** Obtain the lock on the stream state. 21.129 - * 21.130 - * @param s stream 21.131 - */ 21.132 -void kernel_stream_lock(IOStream *s){ 21.133 - KernelData_lock(get_kernel_data(s)); 21.134 -} 21.135 - 21.136 -/** Release the lock on the stream state. 21.137 - * 21.138 - * @param s stream 21.139 - */ 21.140 -void kernel_stream_unlock(IOStream *s){ 21.141 - KernelData_unlock(get_kernel_data(s)); 21.142 -} 21.143 - 21.144 -/** Write to a kernel stream. 21.145 - * 21.146 - * @param stream kernel stream 21.147 - * @param format print format 21.148 - * @param args print arguments 21.149 - * @return result of the print 21.150 - */ 21.151 -static int kernel_write(IOStream *stream, const char *buf, int n){ 21.152 - KernelData *kdata = get_kernel_data(stream); 21.153 - int k; 21.154 - k = kdata->buf_n - 1; 21.155 - if(n < k) k = n; 21.156 - memcpy(kdata->buf, buf, k); 21.157 - kdata->buf[k] = '\0' 21.158 - printk(kdata->buf); 21.159 - return k; 21.160 -} 21.161 - 21.162 -/** Free a kernel stream. 21.163 - * Frees the internal state of the stream. 21.164 - * Do not call this unless the stream was dynamically allocated. 21.165 - * Do not call this on a stream returned from get_stream_kernel(). 21.166 - * 21.167 - * @param io stream to free 21.168 - */ 21.169 -static void kernel_free(IOStream *io){ 21.170 - KernelData *kdata; 21.171 - if(io == &iokernel) return; 21.172 - kdata = get_kernel_data(io); 21.173 - zero(kdata, sizeof(*kdata)); 21.174 - deallocate(kdata); 21.175 -} 21.176 -#endif /* __KERNEL__ */ 21.177 - 21.178 - 21.179 - 21.180 -
22.1 --- a/tools/lib/kernel_stream.h Tue Jun 29 14:50:09 2004 +0000 22.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 22.3 @@ -1,29 +0,0 @@ 22.4 -/* 22.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 22.6 - * 22.7 - * This library is free software; you can redistribute it and/or modify 22.8 - * it under the terms of the GNU Lesser General Public License as published by 22.9 - * the Free Software Foundation; either version 2.1 of the License, or 22.10 - * (at your option) any later version. 22.11 - * 22.12 - * This library is distributed in the hope that it will be useful, 22.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 22.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22.15 - * GNU Lesser General Public License for more details. 22.16 - * 22.17 - * You should have received a copy of the GNU Lesser General Public License 22.18 - * along with this library; if not, write to the Free Software 22.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22.20 - */ 22.21 - 22.22 -#ifndef _XEN_LIB_KERNEL_STREAM_H_ 22.23 -#define _XEN_LIB_KERNEL_STREAM_H_ 22.24 - 22.25 -#ifdef __KERNEL__ 22.26 -#include "iostream.h" 22.27 - 22.28 -extern IOStream get_stream_kernel(void); 22.29 -#define get_stream_stdout get_stream_kernel 22.30 - 22.31 -#endif /* __KERNEL__ */ 22.32 -#endif /* !_XEN_LIB_KERNEL_STREAM_H_ */
23.1 --- a/tools/lib/lexis.c Tue Jun 29 14:50:09 2004 +0000 23.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 23.3 @@ -1,93 +0,0 @@ 23.4 -/* 23.5 - * 23.6 - * This library is free software; you can redistribute it and/or modify 23.7 - * it under the terms of the GNU Lesser General Public License as 23.8 - * published by the Free Software Foundation; either version 2.1 of the 23.9 - * License, or (at your option) any later version. This library is 23.10 - * distributed in the hope that it will be useful, but WITHOUT ANY 23.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 23.12 - * FITNESS FOR A PARTICULAR PURPOSE. 23.13 - * See the GNU Lesser General Public License for more details. 23.14 - * 23.15 - * You should have received a copy of the GNU Lesser General Public License 23.16 - * along with this library; if not, write to the Free Software Foundation, 23.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23.18 - */ 23.19 - 23.20 -/** @file 23.21 - * Lexical analysis. 23.22 - */ 23.23 - 23.24 -#include "sys_string.h" 23.25 -#include "lexis.h" 23.26 -#include <errno.h> 23.27 - 23.28 -/** Check if a value lies in a (closed) range. 23.29 - * 23.30 - * @param x value to test 23.31 - * @param lo low end of the range 23.32 - * @param hi high end of the range 23.33 - * @return 1 if x is in the interval [lo, hi], 0 otherwise 23.34 - */ 23.35 -inline static int in_range(int x, int lo, int hi){ 23.36 - return (lo <= x) && (x <= hi); 23.37 -} 23.38 - 23.39 -/** Determine if a string is an (unsigned) decimal number. 23.40 - * 23.41 - * @param s pointer to characters to test 23.42 - * @param n length of string 23.43 - * @return 1 if s is a decimal number, 0 otherwise. 23.44 - */ 23.45 -int is_decimal_number(const char *s, int n){ 23.46 - int i; 23.47 - if(n <= 0)return 0; 23.48 - for(i = 0; i < n; i++){ 23.49 - if(!in_decimal_digit_class(s[i])) return 0; 23.50 - } 23.51 - return 1; 23.52 -} 23.53 - 23.54 -/** Determine if a string is a hex number. 23.55 - * Hex numbers are 0, or start with 0x or 0X followed 23.56 - * by a non-zero number of hex digits (0-9,a-f,A-F). 23.57 - * 23.58 - * @param s pointer to characters to test 23.59 - * @param n length of string 23.60 - * @return 1 if s is a hex number, 0 otherwise. 23.61 - */ 23.62 -int is_hex_number(const char *s, int n){ 23.63 - int i; 23.64 - if(n <= 0) return 0; 23.65 - if(n == 1){ 23.66 - return s[0]=='0'; 23.67 - } 23.68 - if(n <= 3) return 0; 23.69 - if(s[0] != '0' || (s[1] != 'x' && s[1] != 'X')) return 0; 23.70 - for(i = 2; i < n; i++){ 23.71 - if(!in_hex_digit_class(s[i])) return 0; 23.72 - } 23.73 - return 1; 23.74 -} 23.75 - 23.76 -/** Test if a string matches a keyword. 23.77 - * The comparison is case-insensitive. 23.78 - * The comparison fails if either argument is null. 23.79 - * 23.80 - * @param s string 23.81 - * @param k keyword 23.82 - * @return 1 if they match, 0 otherwise 23.83 - */ 23.84 -int is_keyword(const char *s, const char *k){ 23.85 - return s && k && !strcasecmp(s, k); 23.86 -} 23.87 - 23.88 -/** Test if a string matches a character. 23.89 - * 23.90 - * @param s string 23.91 - * @param c character (non-null) 23.92 - * @return 1 if s contains exactly c, 0 otherwise 23.93 - */ 23.94 -int is_keychar(const char *s, char c){ 23.95 - return c && (s[0] == c) && !s[1]; 23.96 -}
24.1 --- a/tools/lib/lexis.h Tue Jun 29 14:50:09 2004 +0000 24.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 24.3 @@ -1,122 +0,0 @@ 24.4 -/* 24.5 - * 24.6 - * This library is free software; you can redistribute it and/or modify 24.7 - * it under the terms of the GNU Lesser General Public License as 24.8 - * published by the Free Software Foundation; either version 2.1 of the 24.9 - * License, or (at your option) any later version. This library is 24.10 - * distributed in the hope that it will be useful, but WITHOUT ANY 24.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 24.12 - * FITNESS FOR A PARTICULAR PURPOSE. 24.13 - * See the GNU Lesser General Public License for more details. 24.14 - * 24.15 - * You should have received a copy of the GNU Lesser General Public License 24.16 - * along with this library; if not, write to the Free Software Foundation, 24.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24.18 - */ 24.19 - 24.20 -#ifndef _SP_LEXIS_H_ 24.21 -#define _SP_LEXIS_H_ 24.22 - 24.23 -#include "sys_string.h" 24.24 -#include "sys_ctype.h" 24.25 - 24.26 -/** @file 24.27 - * Lexical analysis. 24.28 - */ 24.29 - 24.30 -/** Class of characters treated as space. */ 24.31 -#define space_class ((char []){ '\n', '\r', '\t', ' ', '\f' , 0 }) 24.32 - 24.33 -/** Class of separator characters. */ 24.34 -#define sep_class "{}()<>[]@!;" 24.35 - 24.36 -#define comment_class "#" 24.37 - 24.38 -/** Determine if a character is in a given class. 24.39 - * 24.40 - * @param c character to test 24.41 - * @param s null-terminated string of characters in the class 24.42 - * @return 1 if c is in the class, 0 otherwise. 24.43 - */ 24.44 -static inline int in_class(int c, const char *s){ 24.45 - return s && (strchr(s, c) != 0); 24.46 -} 24.47 - 24.48 -/** Determine if a character is in the space class. 24.49 - * 24.50 - * @param c character to test 24.51 - * @return 1 if c is in the class, 0 otherwise. 24.52 - */ 24.53 -static inline int in_space_class(int c){ 24.54 - return in_class(c, space_class); 24.55 -} 24.56 - 24.57 -static inline int in_comment_class(int c){ 24.58 - return in_class(c, comment_class); 24.59 -} 24.60 - 24.61 -/** Determine if a character is in the separator class. 24.62 - * Separator characters terminate tokens, and do not need space 24.63 - * to separate them. 24.64 - * 24.65 - * @param c character to test 24.66 - * @return 1 if c is in the class, 0 otherwise. 24.67 - */ 24.68 -static inline int in_sep_class(int c){ 24.69 - return in_class(c, sep_class); 24.70 -} 24.71 - 24.72 -/** Determine if a character is in the alpha class. 24.73 - * 24.74 - * @param c character to test 24.75 - * @return 1 if c is in the class, 0 otherwise. 24.76 - */ 24.77 -static inline int in_alpha_class(int c){ 24.78 - return isalpha(c); 24.79 -} 24.80 - 24.81 -/** Determine if a character is in the octal digit class. 24.82 - * 24.83 - * @param c character to test 24.84 - * @return 1 if c is in the class, 0 otherwise. 24.85 - */ 24.86 -static inline int in_octal_digit_class(int c){ 24.87 - return '0' <= c && c <= '7'; 24.88 -} 24.89 - 24.90 -/** Determine if a character is in the decimal digit class. 24.91 - * 24.92 - * @param c character to test 24.93 - * @return 1 if c is in the class, 0 otherwise. 24.94 - */ 24.95 -static inline int in_decimal_digit_class(int c){ 24.96 - return isdigit(c); 24.97 -} 24.98 - 24.99 -/** Determine if a character is in the hex digit class. 24.100 - * 24.101 - * @param c character to test 24.102 - * @return 1 if c is in the class, 0 otherwise. 24.103 - */ 24.104 -static inline int in_hex_digit_class(int c){ 24.105 - return isdigit(c) || in_class(c, "abcdefABCDEF"); 24.106 -} 24.107 - 24.108 - 24.109 -static inline int in_string_quote_class(int c){ 24.110 - return in_class(c, "'\""); 24.111 -} 24.112 - 24.113 -static inline int in_printable_class(int c){ 24.114 - return ('A' <= c && c <= 'Z') 24.115 - || ('a' <= c && c <= 'z') 24.116 - || ('0' <= c && c <= '9') 24.117 - || in_class(c, "!$%&*+,-./:;<=>?@^_`{|}~"); 24.118 -} 24.119 - 24.120 -extern int is_decimal_number(const char *s, int n); 24.121 -extern int is_hex_number(const char *s, int n); 24.122 -extern int is_keyword(const char *s, const char *k); 24.123 -extern int is_keychar(const char *s, char c); 24.124 - 24.125 -#endif /* !_SP_LEXIS_H_ */
25.1 --- a/tools/lib/lzi_stream.c Tue Jun 29 14:50:09 2004 +0000 25.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 25.3 @@ -1,590 +0,0 @@ 25.4 -/* $Id: lzi_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $ */ 25.5 -#define __FILE_ID_INFO "$Id: lzi_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $" 25.6 -#include <what.h> 25.7 -static char __rcsid[] __attribute__((unused)) = WHAT_ID __FILE_ID_INFO; 25.8 -/* 25.9 - * Copyright (C) 2003 Hewlett-Packard Company. 25.10 - * 25.11 - * This library is free software; you can redistribute it and/or modify 25.12 - * it under the terms of the GNU Lesser General Public License as published by 25.13 - * the Free Software Foundation; either version 2.1 of the License, or 25.14 - * (at your option) any later version. 25.15 - * 25.16 - * This library is distributed in the hope that it will be useful, 25.17 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 25.18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25.19 - * GNU Lesser General Public License for more details. 25.20 - * 25.21 - * You should have received a copy of the GNU Lesser General Public License 25.22 - * along with this library; if not, write to the Free Software 25.23 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25.24 - */ 25.25 - 25.26 -/** @file 25.27 - * An IOStream implementation using LZI to provide compression and decompression. 25.28 - * This is designed to provide compression without output latency. 25.29 - * Flushing an LZI stream flushes all pending data to the underlying stream. 25.30 - * This is essential for stream-based (e.g. networked) applications. 25.31 - * 25.32 - * A compressed data stream is a sequence of blocks. 25.33 - * Each block is the block size followed by the compressed data. 25.34 - * The last block has size zero. 25.35 - * Sizes are 4-byte unsigned in network order. 25.36 - * 25.37 - * This format allows compressed data to be read from a stream without reading 25.38 - * past the logical end of compressed data. 25.39 - * 25.40 - * @author Mike Wray <mike.wray@hpl.hp.com> 25.41 - */ 25.42 -#ifndef __KERNEL__ 25.43 - 25.44 -#include <stdio.h> 25.45 -#include <stdlib.h> 25.46 -#include <errno.h> 25.47 -#include <string.h> 25.48 - 25.49 -#include "zlib.h" 25.50 - 25.51 -#include "allocate.h" 25.52 -#include "lzi_stream.h" 25.53 -#include "file_stream.h" 25.54 -#include "marshal.h" 25.55 - 25.56 -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] LZI>%s" fmt, __FUNCTION__, ##args) 25.57 -#define wprintf(fmt, args...) fprintf(stderr, "[WARN] LZI>%s" fmt, __FUNCTION__, ##args) 25.58 -#define iprintf(fmt, args...) fprintf(stdout, "[INFO] LZI>%s" fmt, __FUNCTION__, ##args) 25.59 -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] LZI>%s" fmt, __FUNCTION__, ##args) 25.60 - 25.61 -static int lzi_read(IOStream *s, void *buf, size_t size, size_t count); 25.62 -static int lzi_write(IOStream *s, const void *buf, size_t size, size_t count); 25.63 -static int lzi_print(IOStream *s, const char *msg, va_list args); 25.64 -static int lzi_getc(IOStream *s); 25.65 -static int lzi_error(IOStream *s); 25.66 -static int lzi_close(IOStream *s); 25.67 -static void lzi_free(IOStream *s); 25.68 -static int lzi_flush(IOStream *s); 25.69 - 25.70 -enum { 25.71 - LZI_WRITE = 1, 25.72 - LZI_READ = 2, 25.73 -}; 25.74 - 25.75 -/** Methods used by a gzFile* IOStream. */ 25.76 -static const IOMethods lzi_methods = { 25.77 - read: lzi_read, 25.78 - write: lzi_write, 25.79 - print: lzi_print, 25.80 - getc: lzi_getc, 25.81 - error: lzi_error, 25.82 - close: lzi_close, 25.83 - free: lzi_free, 25.84 - flush: lzi_flush, 25.85 -}; 25.86 - 25.87 -#define BUFFER_SIZE (512 * 1024) 25.88 - 25.89 -typedef struct LZIState { 25.90 - z_stream zstream; 25.91 - void *inbuf; 25.92 - uint32_t inbuf_size; 25.93 - void *outbuf; 25.94 - uint32_t outbuf_size; 25.95 - /** Underlying stream for I/O. */ 25.96 - IOStream *io; 25.97 - /** Flags. */ 25.98 - int flags; 25.99 - /** Error indicator. */ 25.100 - int error; 25.101 - int eof; 25.102 - int plain_bytes; 25.103 - int comp_bytes; 25.104 - int zstream_initialized; 25.105 - int flushed; 25.106 -} LZIState; 25.107 - 25.108 -static inline int LZIState_writeable(LZIState *s){ 25.109 - return (s->flags & LZI_WRITE) != 0; 25.110 -} 25.111 - 25.112 -static inline int LZIState_readable(LZIState *s){ 25.113 - return (s->flags & LZI_READ) != 0; 25.114 -} 25.115 - 25.116 -void LZIState_free(LZIState *z){ 25.117 - if(!z) return; 25.118 - if(z->zstream_initialized){ 25.119 - if(LZIState_writeable(z)){ 25.120 - deflateEnd(&z->zstream); 25.121 - } else if(LZIState_readable(z)){ 25.122 - inflateEnd(&z->zstream); 25.123 - } 25.124 - } 25.125 - deallocate(z->inbuf); 25.126 - deallocate(z->outbuf); 25.127 - deallocate(z); 25.128 -} 25.129 - 25.130 -static int mode_flags(const char *mode, int *flags){ 25.131 - int err = 0; 25.132 - int r=0, w=0; 25.133 - if(!mode){ 25.134 - err = -EINVAL; 25.135 - goto exit; 25.136 - } 25.137 - for(; *mode; mode++){ 25.138 - if(*mode == 'w') w = 1; 25.139 - if(*mode == 'r') r = 1; 25.140 - } 25.141 - if(r + w != 1){ 25.142 - err = -EINVAL; 25.143 - goto exit; 25.144 - } 25.145 - if(r) *flags |= LZI_READ; 25.146 - if(w) *flags |= LZI_WRITE; 25.147 - exit: 25.148 - return err; 25.149 -} 25.150 - 25.151 -/** Get the stream state. 25.152 - * 25.153 - * @param s lzi stream 25.154 - * @return stream state. 25.155 - */ 25.156 -static inline LZIState * lzi_state(IOStream *io){ 25.157 - return io->data; 25.158 -} 25.159 - 25.160 -IOStream *lzi_stream_io(IOStream *io){ 25.161 - LZIState *s = lzi_state(io); 25.162 - return s->io; 25.163 -} 25.164 - 25.165 -static inline void set_error(LZIState *s, int err){ 25.166 - if(err < 0 && !s->error){ 25.167 - s->error = err; 25.168 - } 25.169 -} 25.170 - 25.171 -static int zerror(LZIState *s, int err){ 25.172 - if(err){ 25.173 - //dprintf("> err=%d\n", err); 25.174 - if(err < 0) set_error(s, -EIO); 25.175 - } 25.176 - return s->error; 25.177 -} 25.178 - 25.179 -int lzi_stream_plain_bytes(IOStream *io){ 25.180 - LZIState *s = lzi_state(io); 25.181 - return s->plain_bytes; 25.182 -} 25.183 - 25.184 -int lzi_stream_comp_bytes(IOStream *io){ 25.185 - LZIState *s = lzi_state(io); 25.186 - return s->comp_bytes; 25.187 -} 25.188 - 25.189 -float lzi_stream_ratio(IOStream *io){ 25.190 - LZIState *s = lzi_state(io); 25.191 - float ratio = 0.0; 25.192 - if(s->comp_bytes){ 25.193 - ratio = ((float) s->comp_bytes)/((float) s->plain_bytes); 25.194 - } 25.195 - return ratio; 25.196 -} 25.197 - 25.198 -static int alloc(void **p, int n){ 25.199 - *p = allocate(n); 25.200 - return (p ? 0 : -ENOMEM); 25.201 -} 25.202 - 25.203 -LZIState * LZIState_new(IOStream *io, int flags){ 25.204 - int err = -ENOMEM; 25.205 - int zlevel = Z_BEST_SPEED; // Level 1 compression - fastest. 25.206 - int zstrategy = Z_DEFAULT_STRATEGY; 25.207 - int zwindow = MAX_WBITS; 25.208 - int zmemory = 8; 25.209 - LZIState *z = ALLOCATE(LZIState); 25.210 - 25.211 - //dprintf(">\n"); 25.212 - if(!z) goto exit; 25.213 - z->io = io; 25.214 - z->flags = flags; 25.215 - 25.216 - if(LZIState_writeable(z)){ 25.217 - z->outbuf_size = BUFFER_SIZE; 25.218 - /* windowBits is passed < 0 to suppress zlib header */ 25.219 - err = deflateInit2(&z->zstream, zlevel, Z_DEFLATED, -zwindow, zmemory, zstrategy); 25.220 - if (err != Z_OK) goto exit; 25.221 - z->zstream_initialized = 1; 25.222 - err = alloc(&z->outbuf, z->outbuf_size); 25.223 - if(err) goto exit; 25.224 - z->zstream.next_out = z->outbuf; 25.225 - z->zstream.avail_out = z->outbuf_size; 25.226 - } else { 25.227 - z->inbuf_size = BUFFER_SIZE; 25.228 - err = alloc(&z->inbuf, z->inbuf_size); 25.229 - if(err) goto exit; 25.230 - ///z->zstream.next_in = z->inbuf; 25.231 - 25.232 - /* windowBits is passed < 0 to tell that there is no zlib header. 25.233 - * Note that in this case inflate *requires* an extra "dummy" byte 25.234 - * after the compressed stream in order to complete decompression and 25.235 - * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are 25.236 - * present after the compressed stream. 25.237 - */ 25.238 - err = inflateInit2(&z->zstream, -zwindow); 25.239 - if(err != Z_OK) goto exit; 25.240 - z->zstream_initialized = 1; 25.241 - } 25.242 - 25.243 - exit: 25.244 - if(err){ 25.245 - LZIState_free(z); 25.246 - z = NULL; 25.247 - } 25.248 - //dprintf("< z=%p\n", z); 25.249 - return z; 25.250 -} 25.251 - 25.252 -int read_block(LZIState *s){ 25.253 - int err = 0, k = 0; 25.254 - //dprintf(">\n"); 25.255 - if(s->eof) goto exit; 25.256 - err = unmarshal_uint32(s->io, &k); 25.257 - if(err) goto exit; 25.258 - if(k > s->inbuf_size){ 25.259 - err = -EINVAL; 25.260 - goto exit; 25.261 - } 25.262 - if(k){ 25.263 - err = unmarshal_bytes(s->io, s->inbuf, k); 25.264 - if(err) goto exit; 25.265 - } else { 25.266 - s->eof = 1; 25.267 - } 25.268 - s->zstream.avail_in = k; 25.269 - s->zstream.next_in = s->inbuf; 25.270 - s->comp_bytes += 4; 25.271 - s->comp_bytes += k; 25.272 - exit: 25.273 - //dprintf("< err=%d\n", err); 25.274 - return err; 25.275 -} 25.276 - 25.277 -int write_block(LZIState *s){ 25.278 - int err = 0; 25.279 - int k = ((char*)s->zstream.next_out) - ((char*)s->outbuf); 25.280 - int k2 = s->outbuf_size - s->zstream.avail_out; 25.281 - //dprintf("> k=%d k2=%d\n", k, k2); 25.282 - if(!k) goto exit; 25.283 - err = marshal_uint32(s->io, k); 25.284 - if(err) goto exit; 25.285 - err = marshal_bytes(s->io, s->outbuf, k); 25.286 - if(err) goto exit; 25.287 - s->zstream.next_out = s->outbuf; 25.288 - s->zstream.avail_out = s->outbuf_size; 25.289 - s->comp_bytes += 4; 25.290 - s->comp_bytes += k; 25.291 - exit: 25.292 - //dprintf("< err=%d\n", err); 25.293 - return err; 25.294 -} 25.295 - 25.296 -int write_terminator(LZIState *s){ 25.297 - int err = 0; 25.298 - char c = 0; 25.299 - err = marshal_uint32(s->io, 1); 25.300 - if(err) goto exit; 25.301 - err = marshal_bytes(s->io, &c, 1); 25.302 - if(err) goto exit; 25.303 - err = marshal_uint32(s->io, 0); 25.304 - if(err) goto exit; 25.305 - s->comp_bytes += 9; 25.306 - exit: 25.307 - return err; 25.308 -} 25.309 - 25.310 -/** Write to the underlying stream using fwrite(); 25.311 - * 25.312 - * @param io destination 25.313 - * @param buf data 25.314 - * @param size size of data elements 25.315 - * @param count number of data elements to write 25.316 - * @return number of data elements written 25.317 - */ 25.318 -static int lzi_write(IOStream *io, const void *buf, size_t size, size_t count){ 25.319 - int err = 0; 25.320 - int n = size * count; 25.321 - LZIState *s = lzi_state(io); 25.322 - 25.323 - //dprintf("> buf=%p size=%d count=%d n=%d\n", buf, size, count, n); 25.324 - if(!LZIState_writeable(s)){ 25.325 - err = -EINVAL; 25.326 - goto exit; 25.327 - } 25.328 - s->flushed = 0; 25.329 - s->zstream.next_in = (void*)buf; 25.330 - s->zstream.avail_in = n; 25.331 - while(s->zstream.avail_in){ 25.332 - if(s->zstream.avail_out == 0){ 25.333 - err = write_block(s); 25.334 - if(err) goto exit; 25.335 - } 25.336 - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.337 - //dprintf("> 1 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out); 25.338 - err = zerror(s, deflate(&s->zstream, Z_NO_FLUSH)); 25.339 - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.340 - //dprintf("> 2 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out); 25.341 - if(err) goto exit; 25.342 - } 25.343 - err = n; 25.344 - s->plain_bytes += n; 25.345 - if(size != 1) err /= size; 25.346 - exit: 25.347 - //dprintf("< err=%d\n", err); 25.348 - return err; 25.349 -} 25.350 - 25.351 - 25.352 -/** Read from the underlying stream. 25.353 - * 25.354 - * @param io input 25.355 - * @param buf where to put input 25.356 - * @param size size of data elements 25.357 - * @param count number of data elements to read 25.358 - * @return number of data elements read 25.359 - */ 25.360 -static int lzi_read(IOStream *io, void *buf, size_t size, size_t count){ 25.361 - int err, zerr; 25.362 - int n = size * count; 25.363 - LZIState *s = lzi_state(io); 25.364 - 25.365 - //dprintf("> size=%d count=%d n=%d\n", size, count, n); 25.366 - if(!LZIState_readable(s)){ 25.367 - err = -EINVAL; 25.368 - goto exit; 25.369 - } 25.370 - s->zstream.next_out = buf; 25.371 - s->zstream.avail_out = n; 25.372 - while(s->zstream.avail_out){ 25.373 - if(s->zstream.avail_in == 0){ 25.374 - err = read_block(s); 25.375 - } 25.376 - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.377 - zerr = inflate(&s->zstream, Z_NO_FLUSH); 25.378 - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.379 - if(zerr == Z_STREAM_END) break; 25.380 - //dprintf("> zerr=%d\n", zerr); 25.381 - err = zerror(s, zerr); 25.382 - if(err) goto exit; 25.383 - } 25.384 - err = n - s->zstream.avail_out; 25.385 - s->plain_bytes += err; 25.386 - if(size != 1) err /= size; 25.387 - exit: 25.388 - set_error(s, err); 25.389 - //dprintf("< err=%d\n", err); 25.390 - return err; 25.391 -} 25.392 - 25.393 -/** Print to the underlying stream. 25.394 - * Returns 0 if the formatted output is too big for the internal buffer. 25.395 - * 25.396 - * @param io lzi stream 25.397 - * @param msg format to use 25.398 - * @param args arguments 25.399 - * @return result of the print 25.400 - */ 25.401 -static int lzi_print(IOStream *io, const char *msg, va_list args){ 25.402 - char buf[1024]; 25.403 - int buf_n = sizeof(buf); 25.404 - int n; 25.405 - LZIState *s = lzi_state(io); 25.406 - if(!LZIState_writeable(s)){ 25.407 - n = -EINVAL; 25.408 - goto exit; 25.409 - } 25.410 - n = vsnprintf(buf, buf_n, (char*)msg, args); 25.411 - if(n < 0) goto exit; 25.412 - if(n > buf_n){ 25.413 - n = 0; 25.414 - } else { 25.415 - n = lzi_write(io, buf, 1, n); 25.416 - } 25.417 - exit: 25.418 - return n; 25.419 -} 25.420 - 25.421 -/** Read a character from the underlying stream 25.422 - * 25.423 - * @param io lzi stream 25.424 - * @return character read, IOSTREAM_EOF on end of file (or error) 25.425 - */ 25.426 -static int lzi_getc(IOStream *io){ 25.427 - int err; 25.428 - char c; 25.429 - err = lzi_read(io, &c, 1, 1); 25.430 - if(err < 1) c = EOF; 25.431 - err = (c==EOF ? IOSTREAM_EOF : c); 25.432 - return err; 25.433 -} 25.434 - 25.435 -static int flush_output(LZIState *s, int mode){ 25.436 - int err = 0, zerr; 25.437 - int done = 0; 25.438 - int avail_out_old; 25.439 - int count = 10; 25.440 - 25.441 - //dprintf("> avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.442 - if(s->flushed == 1 + mode) goto exit; 25.443 - //s->zstream.avail_in = 0; /* should be zero already anyway */ 25.444 - for(;;){ 25.445 - // Write any available output. 25.446 - if(done || s->zstream.avail_out == 0){ 25.447 - err = write_block(s); 25.448 - if(err) goto exit; 25.449 - if(done) break; 25.450 - } 25.451 - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.452 - avail_out_old = s->zstream.avail_out; 25.453 - zerr = deflate(&s->zstream, mode); 25.454 - err = zerror(s, zerr); 25.455 - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); 25.456 - //dprintf("> deflate=%d\n", err); 25.457 - //done = (s->zstream.avail_out != 0); 25.458 - //done = (s->zstream.avail_in == 0) && (s->zstream.avail_out == avail_out_old); 25.459 - if(0 && mode == Z_FINISH){ 25.460 - done = (zerr == Z_STREAM_END); 25.461 - } else { 25.462 - done = (s->zstream.avail_in == 0) 25.463 - //&& (s->zstream.avail_out == avail_out_old) 25.464 - && (s->zstream.avail_out != 0); 25.465 - } 25.466 - } 25.467 - s->flushed = 1 + mode; 25.468 - exit: 25.469 - //dprintf("< err=%d\n", err); 25.470 - return err; 25.471 -} 25.472 - 25.473 -/** Flush any pending input to the underlying stream. 25.474 - * 25.475 - * @param s lzi stream 25.476 - * @return 0 on success, error code otherwise 25.477 - */ 25.478 -static int lzi_flush(IOStream *io){ 25.479 - int err = 0; 25.480 - LZIState *s = lzi_state(io); 25.481 - //dprintf(">\n"); 25.482 - if(!LZIState_writeable(s)){ 25.483 - err = -EINVAL; 25.484 - goto exit; 25.485 - } 25.486 - err = flush_output(s, Z_SYNC_FLUSH); 25.487 - if(err) goto exit; 25.488 - err = IOStream_flush(s->io); 25.489 - exit: 25.490 - set_error(s, err); 25.491 - //dprintf("< err=%d\n", err); 25.492 - return (err < 0 ? err : 0); 25.493 -} 25.494 - 25.495 -/** Check if a stream has an error. 25.496 - * 25.497 - * @param s lzi stream 25.498 - * @return code if has an error, 0 otherwise 25.499 - */ 25.500 -static int lzi_error(IOStream *s){ 25.501 - int err = 0; 25.502 - LZIState *state = lzi_state(s); 25.503 - err = state->error; 25.504 - if(err) goto exit; 25.505 - err = IOStream_error(state->io); 25.506 - exit: 25.507 - return err; 25.508 -} 25.509 - 25.510 -/** Close an lzi stream. 25.511 - * 25.512 - * @param s lzi stream to close 25.513 - * @return result of the close 25.514 - */ 25.515 -static int lzi_close(IOStream *io){ 25.516 - int err = 0; 25.517 - LZIState *s = lzi_state(io); 25.518 - if(LZIState_writeable(s)){ 25.519 - err = flush_output(s, Z_FINISH); 25.520 - if(err) goto exit; 25.521 - err = write_terminator(s); 25.522 - if(err) goto exit; 25.523 - err = IOStream_flush(s->io); 25.524 - } 25.525 - exit: 25.526 - err = IOStream_close(s->io); 25.527 - set_error(s, err); 25.528 - return err; 25.529 -} 25.530 - 25.531 -/** Free an lzi stream. 25.532 - * 25.533 - * @param s lzi stream 25.534 - */ 25.535 -static void lzi_free(IOStream *s){ 25.536 - LZIState *state = lzi_state(s); 25.537 - IOStream_free(state->io); 25.538 - LZIState_free(state); 25.539 - s->data = NULL; 25.540 -} 25.541 - 25.542 -/** Create an lzi stream for an IOStream. 25.543 - * 25.544 - * @param io stream to wrap 25.545 - * @return new IOStream using f for i/o 25.546 - */ 25.547 -IOStream *lzi_stream_new(IOStream *io, const char *mode){ 25.548 - int err = -ENOMEM; 25.549 - int flags = 0; 25.550 - IOStream *zio = NULL; 25.551 - LZIState *state = NULL; 25.552 - 25.553 - zio = ALLOCATE(IOStream); 25.554 - if(!zio) goto exit; 25.555 - err = mode_flags(mode, &flags); 25.556 - if(err) goto exit; 25.557 - state = LZIState_new(io, flags); 25.558 - if(!state) goto exit; 25.559 - err = 0; 25.560 - zio->data = state; 25.561 - zio->methods = &lzi_methods; 25.562 - exit: 25.563 - if(err){ 25.564 - if(state) LZIState_free(state); 25.565 - if(zio) deallocate(zio); 25.566 - zio = NULL; 25.567 - } 25.568 - return zio; 25.569 -} 25.570 - 25.571 -/** IOStream version of fdopen(). 25.572 - * 25.573 - * @param fd file descriptor 25.574 - * @param flags giving the mode to open in (as for fdopen()) 25.575 - * @return new stream for the open file, or NULL if failed 25.576 - */ 25.577 -IOStream *lzi_stream_fdopen(int fd, const char *mode){ 25.578 - int err = -ENOMEM; 25.579 - IOStream *io = NULL, *zio = NULL; 25.580 - io = file_stream_fdopen(fd, mode); 25.581 - if(!io) goto exit; 25.582 - zio = lzi_stream_new(io, mode); 25.583 - if(!io) goto exit; 25.584 - err = 0; 25.585 - exit: 25.586 - if(err){ 25.587 - IOStream_free(io); 25.588 - IOStream_free(zio); 25.589 - zio = NULL; 25.590 - } 25.591 - return zio; 25.592 -} 25.593 -#endif
26.1 --- a/tools/lib/lzi_stream.h Tue Jun 29 14:50:09 2004 +0000 26.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 26.3 @@ -1,36 +0,0 @@ 26.4 -#/* $Id: lzi_stream.h,v 1.3 2003/09/30 15:22:53 mjw Exp $ */ 26.5 -/* 26.6 - * Copyright (C) 2003 Hewlett-Packard Company. 26.7 - * 26.8 - * This library is free software; you can redistribute it and/or modify 26.9 - * it under the terms of the GNU Lesser General Public License as published by 26.10 - * the Free Software Foundation; either version 2.1 of the License, or 26.11 - * (at your option) any later version. 26.12 - * 26.13 - * This library is distributed in the hope that it will be useful, 26.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 26.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26.16 - * GNU Lesser General Public License for more details. 26.17 - * 26.18 - * You should have received a copy of the GNU Lesser General Public License 26.19 - * along with this library; if not, write to the Free Software 26.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26.21 - */ 26.22 - 26.23 -#ifndef _SP_LZI_STREAM_H_ 26.24 -#define _SP_LZI_STREAM_H_ 26.25 - 26.26 -#ifndef __KERNEL__ 26.27 -#include "iostream.h" 26.28 - 26.29 -extern IOStream *lzi_stream_new(IOStream *io, const char *mode); 26.30 -extern IOStream *lzi_stream_fopen(const char *file, const char *mode); 26.31 -extern IOStream *lzi_stream_fdopen(int fd, const char *mode); 26.32 -extern IOStream *lzi_stream_io(IOStream *zio); 26.33 - 26.34 -extern int lzi_stream_plain_bytes(IOStream *io); 26.35 -extern int lzi_stream_comp_bytes(IOStream *io); 26.36 -extern float lzi_stream_ratio(IOStream *io); 26.37 - 26.38 -#endif 26.39 -#endif /* !_SP_FILE_STREAM_H_ */
27.1 --- a/tools/lib/lzo_stream.c Tue Jun 29 14:50:09 2004 +0000 27.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 27.3 @@ -1,596 +0,0 @@ 27.4 -/* $Id: lzo_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $ */ 27.5 -#define __FILE_ID_INFO "$Id: lzo_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $" 27.6 -#include <what.h> 27.7 -static char __rcsid[] __attribute__((unused)) = WHAT_ID __FILE_ID_INFO; 27.8 -/* 27.9 - * Copyright (C) 2003 Hewlett-Packard Company. 27.10 - * 27.11 - * This library is free software; you can redistribute it and/or modify 27.12 - * it under the terms of the GNU Lesser General Public License as published by 27.13 - * the Free Software Foundation; either version 2.1 of the License, or 27.14 - * (at your option) any later version. 27.15 - * 27.16 - * This library is distributed in the hope that it will be useful, 27.17 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 27.18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27.19 - * GNU Lesser General Public License for more details. 27.20 - * 27.21 - * You should have received a copy of the GNU Lesser General Public License 27.22 - * along with this library; if not, write to the Free Software 27.23 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 27.24 - */ 27.25 - 27.26 -/** @file 27.27 - * An IOStream implementation using LZO to provide compression and decompression. 27.28 - * This is designed to provide reasonable compression without output latency. 27.29 - * Flushing an LZO stream flushes all pending data to the underlying stream. 27.30 - * This is essential for stream-based (e.g. networked) applications. 27.31 - * 27.32 - * A compressed data stream is a sequence of blocks. 27.33 - * Each block except the last is the plain data size followed by the compressed data size 27.34 - * and the compressed data. The last block has plain data size zero and omits the rest. 27.35 - * Sizes are 4-byte unsigned in network order. If the compressed size is smaller than 27.36 - * the plain size the block data is compressed, otherwise it is plain (uncompressed). 27.37 - * 27.38 - * This format allows compressed data to be read from a stream without reading 27.39 - * past the logical end of compressed data. 27.40 - * 27.41 - * @author Mike Wray <mike.wray@hpl.hp.com> 27.42 - */ 27.43 -#ifndef __KERNEL__ 27.44 - 27.45 -#include <stdio.h> 27.46 -#include <stdlib.h> 27.47 -#include <errno.h> 27.48 -#include <string.h> 27.49 - 27.50 -#include "lzo1x.h" 27.51 - 27.52 -#include "allocate.h" 27.53 -#include "lzo_stream.h" 27.54 -#include "file_stream.h" 27.55 -#include "marshal.h" 27.56 - 27.57 -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] LZO>%s" fmt, __FUNCTION__, ##args) 27.58 -#define wprintf(fmt, args...) fprintf(stderr, "[WARN] LZO>%s" fmt, __FUNCTION__, ##args) 27.59 -#define iprintf(fmt, args...) fprintf(stdout, "[INFO] LZO>%s" fmt, __FUNCTION__, ##args) 27.60 -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] LZO>%s" fmt, __FUNCTION__, ##args) 27.61 - 27.62 -static int lzo_read(IOStream *s, void *buf, size_t size, size_t count); 27.63 -static int lzo_write(IOStream *s, const void *buf, size_t size, size_t count); 27.64 -static int lzo_print(IOStream *s, const char *msg, va_list args); 27.65 -static int lzo_getc(IOStream *s); 27.66 -static int lzo_error(IOStream *s); 27.67 -static int lzo_close(IOStream *s); 27.68 -static void lzo_free(IOStream *s); 27.69 -static int lzo_flush(IOStream *s); 27.70 - 27.71 -enum { 27.72 - LZO_WRITE = 1, 27.73 - LZO_READ = 2, 27.74 -}; 27.75 - 27.76 -/** Methods used by a gzFile* IOStream. */ 27.77 -static const IOMethods lzo_methods = { 27.78 - read: lzo_read, 27.79 - write: lzo_write, 27.80 - print: lzo_print, 27.81 - getc: lzo_getc, 27.82 - error: lzo_error, 27.83 - close: lzo_close, 27.84 - free: lzo_free, 27.85 - flush: lzo_flush, 27.86 -}; 27.87 - 27.88 -//#define PLAIN_SIZE (64 * 1024) 27.89 -//#define PLAIN_SIZE (128 * 1024) 27.90 -#define PLAIN_SIZE (512 * 1024) 27.91 - 27.92 -//#define NOCOMPRESS 27.93 - 27.94 -typedef struct LZOState { 27.95 - /** Flags. */ 27.96 - int flags; 27.97 - /** Error indicator. */ 27.98 - int error; 27.99 - /** Underlying stream for I/O. */ 27.100 - IOStream *io; 27.101 - /** Working memory (only needed for compression, not decompression). */ 27.102 - lzo_byte *memory; 27.103 - /** Buffer for plain (uncompressed) data. */ 27.104 - lzo_byte *plain; 27.105 - /** Size of the plain buffer. */ 27.106 - lzo_uint plain_size; 27.107 - /** Pointer into the plain buffer. */ 27.108 - lzo_byte *plain_ptr; 27.109 - /** Number of bytes of plain data available. */ 27.110 - lzo_uint plain_n; 27.111 - /** Buffer for compressed data. */ 27.112 - lzo_byte *comp; 27.113 - /** Size of the compressed buffer. */ 27.114 - lzo_uint comp_size; 27.115 - 27.116 - int plain_bytes; 27.117 - int comp_bytes; 27.118 -} LZOState; 27.119 - 27.120 -void LZOState_free(LZOState *z){ 27.121 - if(!z) return; 27.122 - deallocate(z->memory); 27.123 - deallocate(z->plain); 27.124 - deallocate(z->comp); 27.125 - deallocate(z); 27.126 -} 27.127 - 27.128 -/** Maximum size of compressed data for the given plain data size. 27.129 - * 27.130 - * @param plain_size size of plain data 27.131 - * @return maximum size of compressed data 27.132 - */ 27.133 -static int comp_size(int plain_size){ 27.134 - return plain_size + (plain_size / 64) + 16 + 3; 27.135 -} 27.136 - 27.137 -static int mode_flags(const char *mode, int *flags){ 27.138 - int err = 0; 27.139 - int r=0, w=0; 27.140 - if(!mode){ 27.141 - err = -EINVAL; 27.142 - goto exit; 27.143 - } 27.144 - for(; *mode; mode++){ 27.145 - if(*mode == 'w') w = 1; 27.146 - if(*mode == 'r') r = 1; 27.147 - } 27.148 - if(r + w != 1){ 27.149 - err = -EINVAL; 27.150 - goto exit; 27.151 - } 27.152 - if(r) *flags |= LZO_READ; 27.153 - if(w) *flags |= LZO_WRITE; 27.154 - exit: 27.155 - return err; 27.156 -} 27.157 - 27.158 -/** Get the stream state. 27.159 - * 27.160 - * @param s lzo stream 27.161 - * @return stream state. 27.162 - */ 27.163 -static inline LZOState * lzo_state(IOStream *s){ 27.164 - return s->data; 27.165 -} 27.166 - 27.167 -IOStream *lzo_stream_io(IOStream *s){ 27.168 - LZOState *state = lzo_state(s); 27.169 - return state->io; 27.170 -} 27.171 - 27.172 -static inline void set_error(LZOState *state, int err){ 27.173 - if(err < 0 && !state->error){ 27.174 - state->error = err; 27.175 - } 27.176 -} 27.177 - 27.178 -int lzo_stream_plain_bytes(IOStream *s){ 27.179 - LZOState *state = lzo_state(s); 27.180 - return state->plain_bytes; 27.181 -} 27.182 - 27.183 -int lzo_stream_comp_bytes(IOStream *s){ 27.184 - LZOState *state = lzo_state(s); 27.185 - return state->comp_bytes; 27.186 -} 27.187 - 27.188 -float lzo_stream_ratio(IOStream *s){ 27.189 - LZOState *state = lzo_state(s); 27.190 - float ratio = 0.0; 27.191 - if(state->comp_bytes){ 27.192 - ratio = ((float) state->comp_bytes)/((float) state->plain_bytes); 27.193 - } 27.194 - return ratio; 27.195 -} 27.196 - 27.197 -static inline int LZOState_writeable(LZOState *state){ 27.198 - return (state->flags & LZO_WRITE) != 0; 27.199 -} 27.200 - 27.201 -static inline int LZOState_readable(LZOState *state){ 27.202 - return (state->flags & LZO_READ) != 0; 27.203 -} 27.204 - 27.205 -LZOState * LZOState_new(IOStream *io, int flags){ 27.206 - int err = -ENOMEM; 27.207 - LZOState *z = ALLOCATE(LZOState); 27.208 - //dprintf(">\n"); 27.209 - if(!z) goto exit; 27.210 - z->io = io; 27.211 - z->flags = flags; 27.212 - if(LZOState_writeable(z)){ 27.213 - z->memory = allocate(LZO1X_1_MEM_COMPRESS); 27.214 - if(!z->memory) goto exit; 27.215 - } 27.216 - z->plain_size = PLAIN_SIZE; 27.217 - z->plain = allocate(z->plain_size); 27.218 - if(!z->plain) goto exit; 27.219 - z->plain_ptr = z->plain; 27.220 - z->comp_size = comp_size(z->plain_size); 27.221 - z->comp = allocate(z->comp_size); 27.222 - if(!z->comp) goto exit; 27.223 - err = 0; 27.224 - exit: 27.225 - if(err){ 27.226 - LZOState_free(z); 27.227 - z = NULL; 27.228 - } 27.229 - //dprintf("< z=%p\n", z); 27.230 - return z; 27.231 -} 27.232 - 27.233 -static int lzo_compress(LZOState *state){ 27.234 - int err = 0; 27.235 - int k, comp_n; 27.236 - //dprintf(">\n"); 27.237 - //dprintf(">plain=%p plain_n=%d comp=%p memory=%p\n", state->plain, state->plain_n, state->comp, state->memory); 27.238 - // Compress the plain buffer. 27.239 - err = lzo1x_1_compress(state->plain, state->plain_n, 27.240 - state->comp, &comp_n, 27.241 - state->memory); 27.242 - //dprintf("> err=%d plain_n=%d comp_n=%d\n", err, state->plain_n, comp_n); 27.243 - // Write plain size, compressed size. 27.244 - err = marshal_uint32(state->io, state->plain_n); 27.245 - if(err) goto exit; 27.246 - err = marshal_uint32(state->io, comp_n); 27.247 - if(err) goto exit; 27.248 - //dprintf("> write data...\n"); 27.249 - // Write the smaller of the compressed and plain data. 27.250 - if(state->plain_n < comp_n){ 27.251 - k = state->plain_n; 27.252 - err = marshal_bytes(state->io, state->plain, state->plain_n); 27.253 - } else { 27.254 - k = comp_n; 27.255 - err = marshal_bytes(state->io, state->comp, comp_n); 27.256 - } 27.257 - if(err) goto exit; 27.258 - // Total output bytes. 27.259 - k+= 8; 27.260 - //dprintf("> wrote %d bytes\n", k); 27.261 - state->plain_bytes += state->plain_n; 27.262 - state->comp_bytes += k; 27.263 - //dprintf("> plain=%d, comp=%d, ratio=%3.2f\n", 27.264 - // state->plain_bytes, state->comp_bytes, 27.265 - // ((float)state->comp_bytes)/((float)state->plain_bytes)); 27.266 - // Reset the plain buffer. 27.267 - state->plain_ptr = state->plain; 27.268 - state->plain_n = 0; 27.269 - err = k; 27.270 - exit: 27.271 - //dprintf("< err=%d\n", err); 27.272 - return err; 27.273 -} 27.274 - 27.275 -static int lzo_decompress(LZOState *state){ 27.276 - int plain_n, comp_n; 27.277 - int err, k; 27.278 - //dprintf(">\n"); 27.279 - err = unmarshal_uint32(state->io, &plain_n); 27.280 - //dprintf("> err=%d plain_n=%d\n", err, plain_n); 27.281 - if(err) goto exit; 27.282 - state->comp_bytes += 4; 27.283 - if(plain_n == 0) goto exit; 27.284 - err = unmarshal_uint32(state->io, &comp_n); 27.285 - //dprintf("> err=%d comp_n=%d\n", err, comp_n); 27.286 - if(err) goto exit; 27.287 - state->comp_bytes += 4; 27.288 - if(plain_n > state->plain_size){ 27.289 - err = -EINVAL; 27.290 - goto exit; 27.291 - } 27.292 - if(comp_n > plain_n){ 27.293 - //dprintf("> reading plain data %d...\n", plain_n); 27.294 - k = plain_n; 27.295 - err = unmarshal_bytes(state->io, state->plain, plain_n); 27.296 - state->plain_n = plain_n; 27.297 - } else { 27.298 - //dprintf("> reading comp data %d...\n", comp_n); 27.299 - k = comp_n; 27.300 - err = unmarshal_bytes(state->io, state->comp, comp_n); 27.301 - //dprintf("> decompress comp_n=%d\n", comp_n); 27.302 - err = lzo1x_decompress(state->comp, comp_n, 27.303 - state->plain, &state->plain_n, 27.304 - state->memory); 27.305 - //dprintf("> err=%d plain=%d state->plain_n=%d\n", err, plain_n, state->plain_n); 27.306 - if(err != LZO_E_OK || state->plain_n != plain_n){ 27.307 - // Bad. Corrupted input. 27.308 - err = -EINVAL; 27.309 - eprintf("> Corrupted!\n"); 27.310 - goto exit; 27.311 - } 27.312 - } 27.313 - state->comp_bytes += k; 27.314 - state->plain_bytes += state->plain_n; 27.315 - state->plain_ptr = state->plain; 27.316 - err = k; 27.317 - exit: 27.318 - //dprintf("< err=%d\n", err); 27.319 - return err; 27.320 -} 27.321 - 27.322 -/** Write to the underlying stream using fwrite(); 27.323 - * 27.324 - * @param stream destination 27.325 - * @param buf data 27.326 - * @param size size of data elements 27.327 - * @param count number of data elements to write 27.328 - * @return number of data elements written 27.329 - */ 27.330 -static int lzo_write(IOStream *s, const void *buf, size_t size, size_t count){ 27.331 - int err = 0; 27.332 - int n = size * count; // Total number of bytes to write. 27.333 - int chunk; // Size of chunk to write. 27.334 - int remaining; // Number of bytes remaining to write. 27.335 - int space; // Amount of space left in plain buffer. 27.336 - LZOState *state = lzo_state(s); 27.337 -#ifdef NOCOMPRESS 27.338 - //dprintf("> buf=%p size=%d count=%d\n", buf, size, count); 27.339 - err = IOStream_write(state->io, buf, size, count); 27.340 - //dprintf("< err=%d\n", err); 27.341 -#else 27.342 - //dprintf("> buf=%p size=%d count=%d n=%d\n", buf, size, count, n); 27.343 - remaining = n; 27.344 - space = state->plain_size - state->plain_n; 27.345 - //dprintf("> plain=%p plain_ptr=%p plain_n=%d space=%d\n", 27.346 - // state->plain, state->plain_ptr, state->plain_n, space); 27.347 - while(remaining){ 27.348 - chunk = remaining; 27.349 - if(chunk > space) chunk = space; 27.350 - //dprintf("> memcpy %p %p %d\n", state->plain_ptr, buf, chunk); 27.351 - memcpy(state->plain_ptr, buf, chunk); 27.352 - remaining -= chunk; 27.353 - space -= chunk; 27.354 - state->plain_ptr += chunk; 27.355 - state->plain_n += chunk; 27.356 - if(space == 0){ 27.357 - // Input buffer is full. Compress and write it. 27.358 - err = lzo_compress(state); 27.359 - if(err < 0) goto exit; 27.360 - space = state->plain_size - state->plain_n; 27.361 - } 27.362 - } 27.363 - err = (size > 1 ? n / size : n); 27.364 - exit: 27.365 - set_error(state, err); 27.366 -#endif 27.367 - return err; 27.368 -} 27.369 - 27.370 - 27.371 -/** Read from the underlying stream. 27.372 - * 27.373 - * @param stream input 27.374 - * @param buf where to put input 27.375 - * @param size size of data elements 27.376 - * @param count number of data elements to read 27.377 - * @return number of data elements read 27.378 - */ 27.379 -static int lzo_read(IOStream *s, void *buf, size_t size, size_t count){ 27.380 - int err = 0; 27.381 - int k = 0; // Number of (plain) bytes read. 27.382 - int remaining = size * count; // Number of bytes remaining to read. 27.383 - int chunk; // Size of chunk to read. 27.384 - LZOState *state = lzo_state(s); 27.385 -#ifdef NOCOMPRESS 27.386 - //dprintf("> buf=%p size=%d count=%d\n", buf, size, count); 27.387 - err = IOStream_read(state->io, buf, size, count); 27.388 - //dprintf("< err=%d\n", err); 27.389 -#else 27.390 - if(!(state->flags & LZO_READ)){ 27.391 - err = -EINVAL; 27.392 - goto exit; 27.393 - } 27.394 - while(remaining){ 27.395 - if(state->plain_n == 0){ 27.396 - // No more plain input, decompress some more. 27.397 - err = lzo_decompress(state); 27.398 - if(err < 0) goto exit; 27.399 - // Stop reading if there is no more input. 27.400 - if(err == 0 || state->plain_n == 0) break; 27.401 - } 27.402 - chunk = remaining; 27.403 - if(chunk > state->plain_n) chunk = state->plain_n; 27.404 - memcpy(buf, state->plain_ptr, chunk); 27.405 - k += chunk; 27.406 - buf += chunk; 27.407 - state->plain_ptr += chunk; 27.408 - state->plain_n -= chunk; 27.409 - remaining -= chunk; 27.410 - } 27.411 - err = k; 27.412 - exit: 27.413 - set_error(state, err); 27.414 -#endif 27.415 - return err; 27.416 -} 27.417 - 27.418 -/** Print to the underlying stream. 27.419 - * Returns 0 if the formatted output is too big for the internal buffer. 27.420 - * 27.421 - * @param s lzo stream 27.422 - * @param msg format to use 27.423 - * @param args arguments 27.424 - * @return result of the print 27.425 - */ 27.426 -static int lzo_print(IOStream *s, const char *msg, va_list args){ 27.427 - char buf[1024]; 27.428 - int buf_n = sizeof(buf); 27.429 - int n; 27.430 - LZOState *state = lzo_state(s); 27.431 - if(!LZOState_writeable(state)){ 27.432 - n = -EINVAL; 27.433 - goto exit; 27.434 - } 27.435 - n = vsnprintf(buf, buf_n, (char*)msg, args); 27.436 - if(n < 0) goto exit; 27.437 - if(n > buf_n){ 27.438 - n = 0; 27.439 - } else { 27.440 - n = lzo_write(s, buf, 1, n); 27.441 - } 27.442 - exit: 27.443 - return n; 27.444 -} 27.445 - 27.446 -/** Read a character from the underlying stream 27.447 - * 27.448 - * @param s lzo stream 27.449 - * @return character read, IOSTREAM_EOF on end of file (or error) 27.450 - */ 27.451 -static int lzo_getc(IOStream *s){ 27.452 - int err; 27.453 - char c; 27.454 - err = lzo_read(s, &c, 1, 1); 27.455 - if(err < 1) c = EOF; 27.456 - err = (c==EOF ? IOSTREAM_EOF : c); 27.457 - return err; 27.458 -} 27.459 - 27.460 -/** Flush any pending input to the underlying stream. 27.461 - * 27.462 - * @param s lzo stream 27.463 - * @return 0 on success, error code otherwise 27.464 - */ 27.465 -static int lzo_flush(IOStream *s){ 27.466 - int err = 0; 27.467 - LZOState *state = lzo_state(s); 27.468 - //dprintf(">\n"); 27.469 -#ifdef NOCOMPRESS 27.470 - err = IOStream_flush(state->io); 27.471 -#else 27.472 - if(!LZOState_writeable(state)){ 27.473 - err = -EINVAL; 27.474 - goto exit; 27.475 - } 27.476 - if(state->plain_n){ 27.477 - err = lzo_compress(state); 27.478 - if(err < 0) goto exit; 27.479 - } 27.480 - err = IOStream_flush(state->io); 27.481 - exit: 27.482 - set_error(state, err); 27.483 -#endif 27.484 - //dprintf("< err=%d\n", err); 27.485 - return (err < 0 ? err : 0); 27.486 -} 27.487 - 27.488 -/** Check if a stream has an error. 27.489 - * 27.490 - * @param s lzo stream 27.491 - * @return code if has an error, 0 otherwise 27.492 - */ 27.493 -static int lzo_error(IOStream *s){ 27.494 - int err = 0; 27.495 - LZOState *state = lzo_state(s); 27.496 - err = state->error; 27.497 - if(err) goto exit; 27.498 - err = IOStream_error(state->io); 27.499 - exit: 27.500 - return err; 27.501 -} 27.502 - 27.503 -int lzo_stream_finish(IOStream *s){ 27.504 - int err = 0; 27.505 - LZOState *state = lzo_state(s); 27.506 - if(!LZOState_writeable(state)){ 27.507 - err = -EINVAL; 27.508 - goto exit; 27.509 - } 27.510 - err = lzo_flush(s); 27.511 - if(err < 0) goto exit; 27.512 - err = marshal_int32(state->io, 0); 27.513 - exit: 27.514 - return err; 27.515 -} 27.516 - 27.517 -/** Close an lzo stream. 27.518 - * 27.519 - * @param s lzo stream to close 27.520 - * @return result of the close 27.521 - */ 27.522 -static int lzo_close(IOStream *s){ 27.523 - int err = 0; 27.524 - LZOState *state = lzo_state(s); 27.525 -#ifdef NOCOMPRESS 27.526 - err = IOStream_close(state->io); 27.527 -#else 27.528 - if(LZOState_writeable(state)){ 27.529 - err = lzo_stream_finish(s); 27.530 - } 27.531 - err = IOStream_close(state->io); 27.532 - set_error(state, err); 27.533 -#endif 27.534 - return err; 27.535 -} 27.536 - 27.537 -/** Free an lzo stream. 27.538 - * 27.539 - * @param s lzo stream 27.540 - */ 27.541 -static void lzo_free(IOStream *s){ 27.542 - LZOState *state = lzo_state(s); 27.543 - IOStream_free(state->io); 27.544 - LZOState_free(state); 27.545 - s->data = NULL; 27.546 -} 27.547 - 27.548 -/** Create an lzo stream for an IOStream. 27.549 - * 27.550 - * @param io stream to wrap 27.551 - * @return new IOStream using f for i/o 27.552 - */ 27.553 -IOStream *lzo_stream_new(IOStream *io, const char *mode){ 27.554 - int err = -ENOMEM; 27.555 - int flags = 0; 27.556 - IOStream *zio = NULL; 27.557 - LZOState *state = NULL; 27.558 - 27.559 - zio = ALLOCATE(IOStream); 27.560 - if(!zio) goto exit; 27.561 - err = mode_flags(mode, &flags); 27.562 - if(err) goto exit; 27.563 - state = LZOState_new(io, flags); 27.564 - if(!state) goto exit; 27.565 - err = 0; 27.566 - zio->data = state; 27.567 - zio->methods = &lzo_methods; 27.568 - exit: 27.569 - if(err){ 27.570 - if(state) LZOState_free(state); 27.571 - if(zio) deallocate(zio); 27.572 - zio = NULL; 27.573 - } 27.574 - return zio; 27.575 -} 27.576 - 27.577 -/** IOStream version of fdopen(). 27.578 - * 27.579 - * @param fd file descriptor 27.580 - * @param flags giving the mode to open in (as for fdopen()) 27.581 - * @return new stream for the open file, or NULL if failed 27.582 - */ 27.583 -IOStream *lzo_stream_fdopen(int fd, const char *mode){ 27.584 - int err = -ENOMEM; 27.585 - IOStream *io = NULL, *zio = NULL; 27.586 - io = file_stream_fdopen(fd, mode); 27.587 - if(!io) goto exit; 27.588 - zio = lzo_stream_new(io, mode); 27.589 - if(!io) goto exit; 27.590 - err = 0; 27.591 - exit: 27.592 - if(err){ 27.593 - IOStream_free(io); 27.594 - IOStream_free(zio); 27.595 - zio = NULL; 27.596 - } 27.597 - return zio; 27.598 -} 27.599 -#endif
28.1 --- a/tools/lib/lzo_stream.h Tue Jun 29 14:50:09 2004 +0000 28.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 28.3 @@ -1,36 +0,0 @@ 28.4 -#/* $Id: lzo_stream.h,v 1.3 2003/09/30 15:22:53 mjw Exp $ */ 28.5 -/* 28.6 - * Copyright (C) 2003 Hewlett-Packard Company. 28.7 - * 28.8 - * This library is free software; you can redistribute it and/or modify 28.9 - * it under the terms of the GNU Lesser General Public License as published by 28.10 - * the Free Software Foundation; either version 2.1 of the License, or 28.11 - * (at your option) any later version. 28.12 - * 28.13 - * This library is distributed in the hope that it will be useful, 28.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 28.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28.16 - * GNU Lesser General Public License for more details. 28.17 - * 28.18 - * You should have received a copy of the GNU Lesser General Public License 28.19 - * along with this library; if not, write to the Free Software 28.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 28.21 - */ 28.22 - 28.23 -#ifndef _SP_LZO_STREAM_H_ 28.24 -#define _SP_LZO_STREAM_H_ 28.25 - 28.26 -#ifndef __KERNEL__ 28.27 -#include "iostream.h" 28.28 - 28.29 -extern IOStream *lzo_stream_new(IOStream *io, const char *mode); 28.30 -extern IOStream *lzo_stream_fopen(const char *file, const char *mode); 28.31 -extern IOStream *lzo_stream_fdopen(int fd, const char *mode); 28.32 -extern IOStream *lzo_stream_io(IOStream *zio); 28.33 - 28.34 -extern int lzo_stream_plain_bytes(IOStream *io); 28.35 -extern int lzo_stream_comp_bytes(IOStream *io); 28.36 -extern float lzo_stream_ratio(IOStream *io); 28.37 - 28.38 -#endif 28.39 -#endif /* !_SP_FILE_STREAM_H_ */
29.1 --- a/tools/lib/marshal.c Tue Jun 29 14:50:09 2004 +0000 29.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 29.3 @@ -1,207 +0,0 @@ 29.4 -#include <errno.h> 29.5 -#include "sys_net.h" 29.6 -#include "allocate.h" 29.7 -#include "marshal.h" 29.8 - 29.9 -#define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args) 29.10 -#define wprintf(fmt, args...) IOStream_print(iostderr, "[WARN] %s" fmt, __FUNCTION__, ##args) 29.11 -#define iprintf(fmt, args...) IOStream_print(iostdout, "[INFO] %s" fmt, __FUNCTION__, ##args) 29.12 -#define eprintf(fmt, args...) IOStream_print(iostderr, "[ERROR] %s" fmt, __FUNCTION__, ##args) 29.13 - 29.14 - 29.15 -#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0])) 29.16 - 29.17 -/* Messages are coded as msgid followed by message fields. 29.18 - * Initial message on any channel is hello - so can check version 29.19 - * compatibility. 29.20 - * 29.21 - * char* -> uint16_t:n <n bytes> 29.22 - * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes). 29.23 - * optional fields go as '1' <val> or '0' (the 0/1 is 1 byte). 29.24 - * lists go as ('1' <elt>)* '0' 29.25 - */ 29.26 - 29.27 -int marshal_flush(IOStream *io){ 29.28 - int err = 0; 29.29 - err = IOStream_flush(io); 29.30 - return err; 29.31 -} 29.32 - 29.33 -int marshal_bytes(IOStream *io, void *s, uint32_t s_n){ 29.34 - int err = 0; 29.35 - int n; 29.36 - n = IOStream_write(io, s, s_n); 29.37 - if(n < 0){ 29.38 - err = n; 29.39 - } else if (n < s_n){ 29.40 - wprintf("> Wanted %d, got %d\n", s_n, n); 29.41 - err = -EIO; 29.42 - } 29.43 - return err; 29.44 -} 29.45 - 29.46 -int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){ 29.47 - int err = 0; 29.48 - int n; 29.49 - //dprintf("> s_n=%d\n", s_n); 29.50 - n = IOStream_read(io, s, s_n); 29.51 - //dprintf("> n=%d\n", n); 29.52 - if(n < 0){ 29.53 - err = n; 29.54 - } else if(n < s_n){ 29.55 - wprintf("> Wanted %d, got %d\n", s_n, n); 29.56 - err = -EIO; 29.57 - } 29.58 - //dprintf("< err=%d\n", err); 29.59 - return err; 29.60 -} 29.61 - 29.62 -int marshal_uint8(IOStream *io, uint8_t x){ 29.63 - return marshal_bytes(io, &x, sizeof(x)); 29.64 -} 29.65 - 29.66 -int unmarshal_uint8(IOStream *io, uint8_t *x){ 29.67 - return unmarshal_bytes(io, x, sizeof(*x)); 29.68 -} 29.69 - 29.70 -int marshal_uint16(IOStream *io, uint16_t x){ 29.71 - x = htons(x); 29.72 - return marshal_bytes(io, &x, sizeof(x)); 29.73 -} 29.74 - 29.75 -int unmarshal_uint16(IOStream *io, uint16_t *x){ 29.76 - int err = 0; 29.77 - err = unmarshal_bytes(io, x, sizeof(*x)); 29.78 - *x = ntohs(*x); 29.79 - return err; 29.80 -} 29.81 - 29.82 -int marshal_int32(IOStream *io, int32_t x){ 29.83 - int err = 0; 29.84 - //dprintf("> x=%d\n", x); 29.85 - x = htonl(x); 29.86 - err = marshal_bytes(io, &x, sizeof(x)); 29.87 - //dprintf("< err=%d\n", err); 29.88 - return err; 29.89 -} 29.90 - 29.91 -int unmarshal_int32(IOStream *io, int32_t *x){ 29.92 - int err = 0; 29.93 - //dprintf(">\n"); 29.94 - err = unmarshal_bytes(io, x, sizeof(*x)); 29.95 - *x = ntohl(*x); 29.96 - //dprintf("< err=%d x=%d\n", err, *x); 29.97 - return err; 29.98 -} 29.99 - 29.100 -int marshal_uint32(IOStream *io, uint32_t x){ 29.101 - int err = 0; 29.102 - //dprintf("> x=%u\n", x); 29.103 - x = htonl(x); 29.104 - err = marshal_bytes(io, &x, sizeof(x)); 29.105 - //dprintf("< err=%d\n", err); 29.106 - return err; 29.107 -} 29.108 - 29.109 -int unmarshal_uint32(IOStream *io, uint32_t *x){ 29.110 - int err = 0; 29.111 - //dprintf(">\n"); 29.112 - err = unmarshal_bytes(io, x, sizeof(*x)); 29.113 - *x = ntohl(*x); 29.114 - //dprintf("< err=%d x=%u\n", err, *x); 29.115 - return err; 29.116 -} 29.117 - 29.118 -int marshal_uint64(IOStream *io, uint64_t x){ 29.119 - int err; 29.120 - err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff)); 29.121 - if(err) goto exit; 29.122 - err = marshal_uint32(io, (uint32_t) ( x & 0xffffffff)); 29.123 - exit: 29.124 - return err; 29.125 -} 29.126 - 29.127 -int unmarshal_uint64(IOStream *io, uint64_t *x){ 29.128 - int err = 0; 29.129 - uint32_t hi, lo; 29.130 - err = unmarshal_uint32(io, &hi); 29.131 - if(err) goto exit; 29.132 - err = unmarshal_uint32(io, &lo); 29.133 - *x = (((uint64_t) hi) << 32) | lo; 29.134 - exit: 29.135 - return err; 29.136 -} 29.137 - 29.138 -int marshal_net16(IOStream *io, net16_t x){ 29.139 - return marshal_bytes(io, &x, sizeof(x)); 29.140 -} 29.141 - 29.142 -int unmarshal_net16(IOStream *io, net16_t *x){ 29.143 - int err = 0; 29.144 - err = unmarshal_bytes(io, x, sizeof(*x)); 29.145 - return err; 29.146 -} 29.147 - 29.148 -int marshal_net32(IOStream *io, net32_t x){ 29.149 - return marshal_bytes(io, &x, sizeof(x)); 29.150 -} 29.151 - 29.152 -int unmarshal_net32(IOStream *io, net32_t *x){ 29.153 - int err = 0; 29.154 - err = unmarshal_bytes(io, x, sizeof(*x)); 29.155 - return err; 29.156 -} 29.157 - 29.158 -int marshal_string(IOStream *io, char *s, uint32_t s_n){ 29.159 - int err; 29.160 - //dprintf("> s=%s\n", s); 29.161 - err = marshal_uint32(io, s_n); 29.162 - if(err) goto exit; 29.163 - err = marshal_bytes(io, s, s_n); 29.164 - exit: 29.165 - //dprintf("< err=%d\n", err); 29.166 - return err; 29.167 -} 29.168 - 29.169 -int unmarshal_string(IOStream *io, char *s, uint32_t s_n){ 29.170 - int err = 0, val_n = 0; 29.171 - //dprintf(">\n"); 29.172 - err = unmarshal_uint32(io, &val_n); 29.173 - if(err) goto exit; 29.174 - if(val_n >= s_n){ 29.175 - err = -EINVAL; 29.176 - goto exit; 29.177 - } 29.178 - err = unmarshal_bytes(io, s, val_n); 29.179 - if(err) goto exit; 29.180 - s[val_n] = '\0'; 29.181 - exit: 29.182 - //dprintf("< err=%d s=%s\n", err, s); 29.183 - return err; 29.184 -} 29.185 - 29.186 -int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){ 29.187 - int err = 0, val_n = 0; 29.188 - char *val = NULL; 29.189 - //dprintf(">\n"); 29.190 - err = unmarshal_uint32(io, &val_n); 29.191 - if(err) goto exit; 29.192 - val = allocate(val_n + 1); 29.193 - if(!val){ 29.194 - err = -ENOMEM; 29.195 - goto exit; 29.196 - } 29.197 - err = unmarshal_bytes(io, val, val_n); 29.198 - if(err) goto exit; 29.199 - val[val_n] = '\0'; 29.200 - exit: 29.201 - if(err){ 29.202 - if(val) deallocate(val); 29.203 - val = NULL; 29.204 - val_n = 0; 29.205 - } 29.206 - *s = val; 29.207 - if(s_n) *s_n = val_n; 29.208 - //dprintf("< err=%d s=%s\n", err, *s); 29.209 - return err; 29.210 -}
30.1 --- a/tools/lib/marshal.h Tue Jun 29 14:50:09 2004 +0000 30.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 30.3 @@ -1,43 +0,0 @@ 30.4 -/* $Id: marshal.h,v 1.1 2003/10/17 15:48:43 mjw Exp $ */ 30.5 -#ifndef _SP_MARSHAL_H_ 30.6 -#define _SP_MARSHAL_H_ 30.7 - 30.8 -#include "iostream.h" 30.9 - 30.10 -/** A 16-bit uint in network order, e.g. a port number. */ 30.11 -typedef uint16_t net16_t; 30.12 - 30.13 -/** A 32-bit uint in network order, e.g. an IP address. */ 30.14 -typedef uint32_t net32_t; 30.15 - 30.16 -extern int marshal_flush(IOStream *io); 30.17 - 30.18 -extern int marshal_bytes(IOStream *io, void *s, uint32_t s_n); 30.19 -extern int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n); 30.20 - 30.21 -extern int marshal_uint8(IOStream *io, uint8_t x); 30.22 -extern int unmarshal_uint8(IOStream *io, uint8_t *x); 30.23 - 30.24 -extern int marshal_uint16(IOStream *io, uint16_t x); 30.25 -extern int unmarshal_uint16(IOStream *io, uint16_t *x); 30.26 - 30.27 -extern int marshal_uint32(IOStream *io, uint32_t x); 30.28 -extern int unmarshal_uint32(IOStream *io, uint32_t *x); 30.29 - 30.30 -extern int marshal_int32(IOStream *io, int32_t x); 30.31 -extern int unmarshal_int32(IOStream *io, int32_t *x); 30.32 - 30.33 -extern int marshal_uint64(IOStream *io, uint64_t x); 30.34 -extern int unmarshal_uint64(IOStream *io, uint64_t *x); 30.35 - 30.36 -extern int marshal_net16(IOStream *io, net16_t x); 30.37 -extern int unmarshal_net16(IOStream *io, net16_t *x); 30.38 - 30.39 -extern int marshal_net32(IOStream *io, net32_t x); 30.40 -extern int unmarshal_net32(IOStream *io, net32_t *x); 30.41 - 30.42 -extern int marshal_string(IOStream *io, char *s, uint32_t s_n); 30.43 -extern int unmarshal_string(IOStream *io, char *s, uint32_t s_n); 30.44 -extern int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n); 30.45 - 30.46 -#endif /* ! _SP_MARSHAL_H_ */
31.1 --- a/tools/lib/socket_stream.c Tue Jun 29 14:50:09 2004 +0000 31.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 31.3 @@ -1,259 +0,0 @@ 31.4 -/* $Id: socket_stream.c,v 1.9 2004/03/05 14:45:34 mjw Exp $ */ 31.5 -/* 31.6 - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com> 31.7 - * 31.8 - * This library is free software; you can redistribute it and/or modify 31.9 - * it under the terms of the GNU Lesser General Public License as published by 31.10 - * the Free Software Foundation; either version 2.1 of the License, or 31.11 - * (at your option) any later version. 31.12 - * 31.13 - * This library is distributed in the hope that it will be useful, 31.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 31.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31.16 - * GNU Lesser General Public License for more details. 31.17 - * 31.18 - * You should have received a copy of the GNU Lesser General Public License 31.19 - * along with this library; if not, write to the Free Software 31.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 31.21 - */ 31.22 - 31.23 -/** @file 31.24 - * An IOStream implementation using sockets. 31.25 - */ 31.26 - 31.27 -#include <stdio.h> 31.28 -#include <stdlib.h> 31.29 -#include <string.h> 31.30 -#include <unistd.h> 31.31 -#include <errno.h> 31.32 -#include "allocate.h" 31.33 -#include "socket_stream.h" 31.34 - 31.35 -#define MODULE_NAME "sock" 31.36 -#define DEBUG 0 31.37 -//#undef DEBUG 31.38 -#include "debug.h" 31.39 - 31.40 -static int socket_read(IOStream *s, void *buf, size_t n); 31.41 -static int socket_write(IOStream *s, const void *buf, size_t n); 31.42 -static int socket_error(IOStream *s); 31.43 -static int socket_close(IOStream *s); 31.44 -static void socket_free(IOStream *s); 31.45 -static int socket_flush(IOStream *s); 31.46 - 31.47 -/** Methods used by a socket IOStream. */ 31.48 -static const IOMethods socket_methods = { 31.49 - read: socket_read, 31.50 - write: socket_write, 31.51 - error: socket_error, 31.52 - close: socket_close, 31.53 - free: socket_free, 31.54 - flush: socket_flush, 31.55 -}; 31.56 - 31.57 -/** Get the socket data. 31.58 - * 31.59 - * @param io socket stream 31.60 - * @return data 31.61 - */ 31.62 -static inline SocketData * socket_data(IOStream *io){ 31.63 - return (SocketData *)io->data; 31.64 -} 31.65 - 31.66 -/** Test if a stream is a socket stream. 31.67 - * 31.68 - * @param io stream 31.69 - * @return 0 if a socket stream, -EINVAL if not 31.70 - */ 31.71 -int socket_stream_check(IOStream *io){ 31.72 - return (io && io->methods == &socket_methods ? 0 : -EINVAL); 31.73 -} 31.74 - 31.75 -/** Get the data for a socket stream. 31.76 - * 31.77 - * @param io stream 31.78 - * @param data return value for the data 31.79 - * @return 0 if a socket stream, -EINVAL if not 31.80 - */ 31.81 -int socket_stream_data(IOStream *io, SocketData **data){ 31.82 - int err = socket_stream_check(io); 31.83 - if(err){ 31.84 - *data = NULL; 31.85 - } else { 31.86 - *data = socket_data(io); 31.87 - } 31.88 - return err; 31.89 -} 31.90 - 31.91 -/** Set the destination address for a socket stream. 31.92 - * 31.93 - * @param io stream 31.94 - * @param addr address 31.95 - * @return 0 if a socket stream, -EINVAL if not 31.96 - */ 31.97 -int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr){ 31.98 - int err = 0; 31.99 - SocketData *data = NULL; 31.100 - err = socket_stream_data(io, &data); 31.101 - if(!err){ 31.102 - data->daddr = *addr; 31.103 - } 31.104 - return err; 31.105 -} 31.106 - 31.107 -/** Set the send flags for a socket stream. 31.108 - * 31.109 - * @param io stream 31.110 - * @param flags flags 31.111 - * @return 0 if a socket stream, -EINVAL if not 31.112 - */ 31.113 -int socket_stream_set_flags(IOStream *io, int flags){ 31.114 - int err = 0; 31.115 - SocketData *data = NULL; 31.116 - err = socket_stream_data(io, &data); 31.117 - if(!err){ 31.118 - data->flags = flags; 31.119 - } 31.120 - return err; 31.121 -} 31.122 - 31.123 -/** Write to the underlying socket using sendto. 31.124 - * 31.125 - * @param stream input 31.126 - * @param buf where to put input 31.127 - * @param n number of bytes to write 31.128 - * @return number of bytes written 31.129 - */ 31.130 -static int socket_write(IOStream *s, const void *buf, size_t n){ 31.131 - SocketData *data = socket_data(s); 31.132 - struct sockaddr *daddr = (struct sockaddr *)&data->daddr; 31.133 - socklen_t daddr_n = sizeof(data->daddr); 31.134 - int k; 31.135 - dprintf("> sock=%d addr=%s:%d n=%d\n", 31.136 - data->fd, inet_ntoa(data->daddr.sin_addr), ntohs(data->daddr.sin_port), n); 31.137 - if(0){ 31.138 - struct sockaddr_in self = {}; 31.139 - socklen_t self_n; 31.140 - getsockname(data->fd, (struct sockaddr *)&self, &self_n); 31.141 - dprintf("> sockname sock=%d %s:%d\n", 31.142 - data->fd, inet_ntoa(self.sin_addr), ntohs(self.sin_port)); 31.143 - } 31.144 - k = sendto(data->fd, buf, n, data->flags, daddr, daddr_n); 31.145 - dprintf("> sendto=%d\n", k); 31.146 - return k; 31.147 -} 31.148 - 31.149 -/** Read from the underlying stream using recv(); 31.150 - * 31.151 - * @param stream input 31.152 - * @param buf where to put input 31.153 - * @param n number of bytes to read 31.154 - * @return number of bytes read 31.155 - */ 31.156 -static int socket_read(IOStream *s, void *buf, size_t n){ 31.157 - SocketData *data = socket_data(s); 31.158 - int k; 31.159 - struct sockaddr *saddr = (struct sockaddr *)&data->saddr; 31.160 - socklen_t saddr_n = sizeof(data->saddr); 31.161 - k = recvfrom(data->fd, buf, n, data->flags, saddr, &saddr_n); 31.162 - return k; 31.163 -} 31.164 - 31.165 -/** Print to the underlying socket. 31.166 - * 31.167 - * @param s socket stream 31.168 - * @param msg format to use 31.169 - * @param args arguments 31.170 - * @return result of the print 31.171 - */ 31.172 -static int socket_print(IOStream *s, const char *msg, va_list args){ 31.173 - SocketData *data = socket_data(s); 31.174 - int n; 31.175 - n = vsnprintf(data->buf, data->buf_n - 1, msg, args); 31.176 - if(0 < n && n < data->buf_n){ 31.177 - socket_write(s, data->buf, n); 31.178 - } 31.179 - return n; 31.180 -} 31.181 - 31.182 -/** Read a character from the underlying socket 31.183 - * 31.184 - * @param s socket stream 31.185 - * @return character read, IOSTREAM_EOF on end of socket (or error) 31.186 - */ 31.187 -static int socket_getc(IOStream *s){ 31.188 - char b; 31.189 - int n, c; 31.190 - n = socket_read(s, &b, 1); 31.191 - c = (n <= 0 ? IOSTREAM_EOF : b); 31.192 - return c; 31.193 -} 31.194 - 31.195 -/** Flush the socket (no-op). 31.196 - * 31.197 - * @param s socket stream 31.198 - * @return 0 on success, error code otherwise 31.199 - */ 31.200 -static int socket_flush(IOStream *s){ 31.201 - return 0; 31.202 -} 31.203 - 31.204 -/** Check if a socket stream has an error (no-op). 31.205 - * 31.206 - * @param s socket stream 31.207 - * @return 1 if has an error, 0 otherwise 31.208 - */ 31.209 -static int socket_error(IOStream *s){ 31.210 - // Read SOL_SOCKET/SO_ERROR ? 31.211 - return 0; 31.212 -} 31.213 - 31.214 -/** Close a socket stream. 31.215 - * 31.216 - * @param s socket stream to close 31.217 - * @return result of the close 31.218 - */ 31.219 -static int socket_close(IOStream *s){ 31.220 - SocketData *data = socket_data(s); 31.221 - return close(data->fd); 31.222 -} 31.223 - 31.224 -/** Free a socket stream. 31.225 - * 31.226 - * @param s socket stream 31.227 - */ 31.228 -static void socket_free(IOStream *s){ 31.229 - SocketData *data = socket_data(s); 31.230 - deallocate(data); 31.231 -} 31.232 - 31.233 -/** Create an IOStream for a socket. 31.234 - * 31.235 - * @param fd socket to wtap 31.236 - * @return new IOStream using fd for i/o 31.237 - */ 31.238 -IOStream *socket_stream_new(int fd){ 31.239 - int err = -ENOMEM; 31.240 - IOStream *io = NULL; 31.241 - SocketData *data = NULL; 31.242 - 31.243 - io = ALLOCATE(IOStream); 31.244 - if(!io) goto exit; 31.245 - io->methods = &socket_methods; 31.246 - data = ALLOCATE(SocketData); 31.247 - if(!data) goto exit; 31.248 - io->data = data; 31.249 - data->fd = fd; 31.250 - data->buf_n = sizeof(data->buf); 31.251 - err = 0; 31.252 - exit: 31.253 - if(err){ 31.254 - if(io){ 31.255 - if(data) deallocate(data); 31.256 - deallocate(io); 31.257 - io = NULL; 31.258 - } 31.259 - } 31.260 - return io; 31.261 -} 31.262 -
32.1 --- a/tools/lib/socket_stream.h Tue Jun 29 14:50:09 2004 +0000 32.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 32.3 @@ -1,54 +0,0 @@ 32.4 -/* $Id: socket_stream.h,v 1.2 2004/03/04 17:38:13 mjw Exp $ */ 32.5 -/* 32.6 - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com> 32.7 - * 32.8 - * This library is free software; you can redistribute it and/or modify 32.9 - * it under the terms of the GNU Lesser General Public License as published by 32.10 - * the Free Software Foundation; either version 2.1 of the License, or 32.11 - * (at your option) any later version. 32.12 - * 32.13 - * This library is distributed in the hope that it will be useful, 32.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 32.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32.16 - * GNU Lesser General Public License for more details. 32.17 - * 32.18 - * You should have received a copy of the GNU Lesser General Public License 32.19 - * along with this library; if not, write to the Free Software 32.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 32.21 - */ 32.22 - 32.23 -#ifndef _XEN_LIB_SOCKET_STREAM_H_ 32.24 -#define _XEN_LIB_SOCKET_STREAM_H_ 32.25 - 32.26 -#ifndef __KERNEL__ 32.27 -#include "iostream.h" 32.28 -#include <stdio.h> 32.29 - 32.30 -#include <sys/socket.h> 32.31 -#include <netinet/in.h> 32.32 -#include <arpa/inet.h> 32.33 - 32.34 -/** Data associated with a socket stream. */ 32.35 -typedef struct SocketData { 32.36 - /** The socket file descriptor. */ 32.37 - int fd; 32.38 - /** Source address from last read (recvfrom). */ 32.39 - struct sockaddr_in saddr; 32.40 - /** Destination address for writes (sendto). */ 32.41 - struct sockaddr_in daddr; 32.42 - /** Write flags (sendto). */ 32.43 - int flags; 32.44 - /** Buffer size. */ 32.45 - int buf_n; 32.46 - /** Buffer for formatted printing. */ 32.47 - char buf[1024]; 32.48 -} SocketData; 32.49 - 32.50 -extern IOStream *socket_stream_new(int fd); 32.51 -extern int socket_stream_data(IOStream *io, SocketData **data); 32.52 -extern int socket_stream_check(IOStream *io); 32.53 -extern int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr); 32.54 -extern int socket_stream_set_flags(IOStream *io, int flags); 32.55 - 32.56 -#endif 32.57 -#endif /* !_XEN_LIB_SOCKET_STREAM_H_ */
33.1 --- a/tools/lib/string_stream.c Tue Jun 29 14:50:09 2004 +0000 33.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 33.3 @@ -1,174 +0,0 @@ 33.4 -/* 33.5 - * Copyright (C) 2001, 2002 Hewlett-Packard Company. 33.6 - * 33.7 - * This library is free software; you can redistribute it and/or modify 33.8 - * it under the terms of the GNU Lesser General Public License as published by 33.9 - * the Free Software Foundation; either version 2.1 of the License, or 33.10 - * (at your option) any later version. 33.11 - * 33.12 - * This library is distributed in the hope that it will be useful, 33.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 33.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33.15 - * GNU Lesser General Public License for more details. 33.16 - * 33.17 - * You should have received a copy of the GNU Lesser General Public License 33.18 - * along with this library; if not, write to the Free Software 33.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 33.20 - */ 33.21 - 33.22 -/** @file 33.23 - * IOStream subtype for input and output to strings. 33.24 - * Usable from user or kernel code (with __KERNEL__ defined). 33.25 - */ 33.26 - 33.27 -#include "sys_string.h" 33.28 -#include "string_stream.h" 33.29 -#include "allocate.h" 33.30 - 33.31 -static int string_print(IOStream *io, const char *msg, va_list args); 33.32 -static int string_getc(IOStream *io); 33.33 -static int string_error(IOStream *io); 33.34 -static int string_close(IOStream *io); 33.35 -static void string_free(IOStream *io); 33.36 - 33.37 -/** Methods for a string stream. */ 33.38 -static IOMethods string_methods = { 33.39 - //print: string_print, 33.40 - //getc: string_getc, 33.41 - error: string_error, 33.42 - close: string_close, 33.43 - free: string_free, 33.44 -}; 33.45 - 33.46 -/** Get the string stream state. 33.47 - * 33.48 - * @param io string stream 33.49 - * @return state 33.50 - */ 33.51 -static inline StringData *get_string_data(IOStream *io){ 33.52 - return (StringData*)io->data; 33.53 -} 33.54 - 33.55 -/** Read a character from a string stream. 33.56 - * 33.57 - * @param io string stream 33.58 - * @return character read, IOSTREAM_EOF if no more input 33.59 - */ 33.60 -static int string_getc(IOStream *io){ 33.61 - StringData *data = get_string_data(io); 33.62 - int c = IOSTREAM_EOF; 33.63 - char *s = data->in; 33.64 - 33.65 - if(s && s < data->end){ 33.66 - c = (unsigned)*s; 33.67 - data->in = s+1; 33.68 - } 33.69 - return c; 33.70 -} 33.71 - 33.72 -/** Print to a string stream. 33.73 - * Formats the data to an internal buffer and prints it. 33.74 - * The formatted data must fit into the internal buffer. 33.75 - * 33.76 - * @param io string stream 33.77 - * @param format print format 33.78 - * @param args print arguments 33.79 - * @return result of the print 33.80 - */ 33.81 -static int string_print(IOStream *io, const char *msg, va_list args){ 33.82 - StringData *data = get_string_data(io); 33.83 - int k = data->end - data->out; 33.84 - int n = vsnprintf(data->out, k, (char*)msg, args); 33.85 - if(n < 0 || n > k ){ 33.86 - n = k; 33.87 - IOStream_close(io); 33.88 - } else { 33.89 - data->out += n; 33.90 - } 33.91 - return n; 33.92 -} 33.93 - 33.94 -/** Test if a string stream has an error. 33.95 - * 33.96 - * @param io string stream 33.97 - * @return 0 if ok, error code otherwise 33.98 - */ 33.99 -static int string_error(IOStream *io){ 33.100 - StringData *data = get_string_data(io); 33.101 - return data->out == NULL; 33.102 -} 33.103 - 33.104 -/** Close a string stream. 33.105 - * 33.106 - * @param io string stream 33.107 - * @return 0 33.108 - */ 33.109 -static int string_close(IOStream *io){ 33.110 - StringData *data = get_string_data(io); 33.111 - data->in = NULL; 33.112 - data->out = NULL; 33.113 - return 0; 33.114 -} 33.115 - 33.116 -/** Free a string stream. 33.117 - * The stream must have been allocated, not statically created. 33.118 - * The stream state is freed, but the underlying string is not. 33.119 - * 33.120 - * @param io string stream 33.121 - */ 33.122 -static void string_free(IOStream *io){ 33.123 - StringData *data = get_string_data(io); 33.124 - zero(data, sizeof(*data)); 33.125 - deallocate(data); 33.126 -} 33.127 - 33.128 -/** Get the methods to use for a string stream. 33.129 - * 33.130 - * @return methods 33.131 - */ 33.132 -IOMethods *string_stream_get_methods(void){ 33.133 - return &string_methods; 33.134 -} 33.135 - 33.136 -/** Initialise a string stream, usually from static data. 33.137 - * 33.138 - * @param io address of IOStream to fill in 33.139 - * @param data address of StringData to fill in 33.140 - * @param s string to use 33.141 - * @param n length of the string 33.142 - */ 33.143 -void string_stream_init(IOStream *io, StringData *data, char *s, int n){ 33.144 - if(data && io){ 33.145 - zero(data, sizeof(*data)); 33.146 - data->string = (char*)s; 33.147 - data->in = data->string; 33.148 - data->out = data->string; 33.149 - data->size = n; 33.150 - data->end = data->string + n; 33.151 - zero(io, sizeof(*io)); 33.152 - io->methods = &string_methods; 33.153 - io->data = data; 33.154 - } 33.155 -} 33.156 - 33.157 -/** Allocate and initialise a string stream. 33.158 - * 33.159 - * @param s string to use 33.160 - * @param n length of the string 33.161 - * @return new stream (free using IOStream_free) 33.162 - */ 33.163 -IOStream *string_stream_new(char *s, int n){ 33.164 - int ok = 0; 33.165 - StringData *data = ALLOCATE(StringData); 33.166 - IOStream *io = ALLOCATE(IOStream); 33.167 - if(data && io){ 33.168 - ok = 1; 33.169 - string_stream_init(io, data, s, n); 33.170 - } 33.171 - if(!ok){ 33.172 - deallocate(data); 33.173 - deallocate(io); 33.174 - io = NULL; 33.175 - } 33.176 - return io; 33.177 -}
34.1 --- a/tools/lib/string_stream.h Tue Jun 29 14:50:09 2004 +0000 34.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 34.3 @@ -1,46 +0,0 @@ 34.4 -/* $Id: string_stream.h,v 1.1 2003/08/22 14:25:48 mjw Exp $ */ 34.5 -/* 34.6 - * Copyright (C) 2001, 2002 Hewlett-Packard Company. 34.7 - * 34.8 - * This library is free software; you can redistribute it and/or modify 34.9 - * it under the terms of the GNU Lesser General Public License as published by 34.10 - * the Free Software Foundation; either version 2.1 of the License, or 34.11 - * (at your option) any later version. 34.12 - * 34.13 - * This library is distributed in the hope that it will be useful, 34.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 34.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 34.16 - * GNU Lesser General Public License for more details. 34.17 - * 34.18 - * You should have received a copy of the GNU Lesser General Public License 34.19 - * along with this library; if not, write to the Free Software 34.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34.21 - */ 34.22 - 34.23 -#ifndef _SP_STRING_STREAM_H_ 34.24 -#define _SP_STRING_STREAM_H_ 34.25 - 34.26 -#include "iostream.h" 34.27 - 34.28 -/** Internal state for a string stream. 34.29 - * Exposed here so that string streams can be statically created, using 34.30 - * string_stream_init(). 34.31 - */ 34.32 -typedef struct { 34.33 - /** The string used for input and ouput. */ 34.34 - char *string; 34.35 - /** Output pointer. */ 34.36 - char *out; 34.37 - /** Input pointer. */ 34.38 - char *in; 34.39 - /** Length of string. */ 34.40 - int size; 34.41 - /** End marker. */ 34.42 - char *end; 34.43 -} StringData; 34.44 - 34.45 -extern IOMethods *string_stream_get_methods(void); 34.46 -extern IOStream *string_stream_new(char *s, int n); 34.47 -extern void string_stream_init(IOStream *stream, StringData *data, char *s, int n); 34.48 - 34.49 -#endif /* !_SP_STRING_STREAM_H_ */
35.1 --- a/tools/lib/sxpr.c Tue Jun 29 14:50:09 2004 +0000 35.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 35.3 @@ -1,935 +0,0 @@ 35.4 -/* 35.5 - * 35.6 - * This library is free software; you can redistribute it and/or modify 35.7 - * it under the terms of the GNU Lesser General Public License as 35.8 - * published by the Free Software Foundation; either version 2.1 of the 35.9 - * License, or (at your option) any later version. This library is 35.10 - * distributed in the hope that it will be useful, but WITHOUT ANY 35.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 35.12 - * FITNESS FOR A PARTICULAR PURPOSE. 35.13 - * See the GNU Lesser General Public License for more details. 35.14 - * 35.15 - * You should have received a copy of the GNU Lesser General Public License 35.16 - * along with this library; if not, write to the Free Software Foundation, 35.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 35.18 - */ 35.19 - 35.20 -#include <stdarg.h> 35.21 -#include "sys_string.h" 35.22 -#include "lexis.h" 35.23 -#include "sys_net.h" 35.24 -#include "hash_table.h" 35.25 -#include "sxpr.h" 35.26 - 35.27 -#include <errno.h> 35.28 -#undef free 35.29 - 35.30 -/** @file 35.31 - * General representation of sxprs. 35.32 - * Includes print, equal, and free functions for the sxpr types. 35.33 - * 35.34 - * Zero memory containing an Sxpr will have the value ONONE - this is intentional. 35.35 - * When a function returning an sxpr cannot allocate memory we return ONOMEM. 35.36 - * 35.37 - */ 35.38 - 35.39 -static int atom_print(IOStream *io, Sxpr obj, unsigned flags); 35.40 -static int atom_equal(Sxpr x, Sxpr y); 35.41 -static void atom_free(Sxpr obj); 35.42 - 35.43 -static int string_print(IOStream *io, Sxpr obj, unsigned flags); 35.44 -static int string_equal(Sxpr x, Sxpr y); 35.45 -static void string_free(Sxpr obj); 35.46 - 35.47 -static int cons_print(IOStream *io, Sxpr obj, unsigned flags); 35.48 -static int cons_equal(Sxpr x, Sxpr y); 35.49 -static void cons_free(Sxpr obj); 35.50 - 35.51 -static int null_print(IOStream *io, Sxpr obj, unsigned flags); 35.52 -static int none_print(IOStream *io, Sxpr obj, unsigned flags); 35.53 -static int int_print(IOStream *io, Sxpr obj, unsigned flags); 35.54 -static int bool_print(IOStream *io, Sxpr obj, unsigned flags); 35.55 - 35.56 -/** Type definitions. */ 35.57 -static SxprType types[1024] = { 35.58 - [T_NONE] { type: T_NONE, name: "none", print: none_print }, 35.59 - [T_NULL] { type: T_NULL, name: "null", print: null_print }, 35.60 - [T_UINT] { type: T_UINT, name: "int", print: int_print, }, 35.61 - [T_BOOL] { type: T_BOOL, name: "bool", print: bool_print, }, 35.62 - [T_ATOM] { type: T_ATOM, name: "atom", print: atom_print, 35.63 - pointer: TRUE, 35.64 - free: atom_free, 35.65 - equal: atom_equal, 35.66 - }, 35.67 - [T_STRING] { type: T_STRING, name: "string", print: string_print, 35.68 - pointer: TRUE, 35.69 - free: string_free, 35.70 - equal: string_equal, 35.71 - }, 35.72 - [T_CONS] { type: T_CONS, name: "cons", print: cons_print, 35.73 - pointer: TRUE, 35.74 - free: cons_free, 35.75 - equal: cons_equal, 35.76 - }, 35.77 -}; 35.78 - 35.79 -/** Number of entries in the types array. */ 35.80 -static int type_sup = sizeof(types)/sizeof(types[0]); 35.81 - 35.82 -/** Get the type definition for a given type code. 35.83 - * 35.84 - * @param ty type code 35.85 - * @return type definition or null 35.86 - */ 35.87 -SxprType *get_sxpr_type(int ty){ 35.88 - if(0 <= ty && ty < type_sup){ 35.89 - return types+ty; 35.90 - } 35.91 - return NULL; 35.92 -} 35.93 - 35.94 -/** The default print function. 35.95 - * 35.96 - * @param io stream to print to 35.97 - * @param x sxpr to print 35.98 - * @param flags print flags 35.99 - * @return number of bytes written on success 35.100 - */ 35.101 -int default_print(IOStream *io, Sxpr x, unsigned flags){ 35.102 - return IOStream_print(io, "#<%u %lu>\n", get_type(x), get_ul(x)); 35.103 -} 35.104 - 35.105 -/** The default equal function. 35.106 - * Uses eq(). 35.107 - * 35.108 - * @param x sxpr to compare 35.109 - * @param y sxpr to compare 35.110 - * @return 1 if equal, 0 otherwise 35.111 - */ 35.112 -int default_equal(Sxpr x, Sxpr y){ 35.113 - return eq(x, y); 35.114 -} 35.115 - 35.116 -/** General sxpr print function. 35.117 - * Prints an sxpr on a stream using the print function for the sxpr type. 35.118 - * Printing is controlled by flags from the PrintFlags enum. 35.119 - * If PRINT_TYPE is in the flags the sxpr type is printed before the sxpr 35.120 - * (for debugging). 35.121 - * 35.122 - * @param io stream to print to 35.123 - * @param x sxpr to print 35.124 - * @param flags print flags 35.125 - * @return number of bytes written 35.126 - */ 35.127 -int objprint(IOStream *io, Sxpr x, unsigned flags){ 35.128 - SxprType *def = get_sxpr_type(get_type(x)); 35.129 - ObjPrintFn *print_fn = (def && def->print ? def->print : default_print); 35.130 - int k = 0; 35.131 - if(!io) return k; 35.132 - if(flags & PRINT_TYPE){ 35.133 - k += IOStream_print(io, "%s:", def->name); 35.134 - } 35.135 - k += print_fn(io, x, flags); 35.136 - return k; 35.137 -} 35.138 - 35.139 -/** General sxpr free function. 35.140 - * Frees an sxpr using the free function for its type. 35.141 - * Free functions must recursively free any subsxprs. 35.142 - * If no function is defined then the default is to 35.143 - * free sxprs whose type has pointer true. 35.144 - * Sxprs must not be used after freeing. 35.145 - * 35.146 - * @param x sxpr to free 35.147 - */ 35.148 -void objfree(Sxpr x){ 35.149 - SxprType *def = get_sxpr_type(get_type(x)); 35.150 - 35.151 - if(def){ 35.152 - if(def->free){ 35.153 - def->free(x); 35.154 - } else if (def->pointer){ 35.155 - hfree(x); 35.156 - } 35.157 - } 35.158 -} 35.159 - 35.160 -/** General sxpr equality function. 35.161 - * Compares x and y using the equal function for x. 35.162 - * Uses default_equal() if x has no equal function. 35.163 - * 35.164 - * @param x sxpr to compare 35.165 - * @param y sxpr to compare 35.166 - * @return 1 if equal, 0 otherwise 35.167 - */ 35.168 -int objequal(Sxpr x, Sxpr y){ 35.169 - SxprType *def = get_sxpr_type(get_type(x)); 35.170 - ObjEqualFn *equal_fn = (def && def->equal ? def->equal : default_equal); 35.171 - return equal_fn(x, y); 35.172 -} 35.173 - 35.174 -/** Search for a key in an alist. 35.175 - * An alist is a list of conses, where the cars 35.176 - * of the conses are the keys. Compares keys using equality. 35.177 - * 35.178 - * @param k key 35.179 - * @param l alist to search 35.180 - * @return first element of l with car k, or ONULL 35.181 - */ 35.182 -Sxpr assoc(Sxpr k, Sxpr l){ 35.183 - for( ; CONSP(l) ; l = CDR(l)){ 35.184 - Sxpr x = CAR(l); 35.185 - if(CONSP(x) && objequal(k, CAR(x))){ 35.186 - return x; 35.187 - } 35.188 - } 35.189 - return ONULL; 35.190 -} 35.191 - 35.192 -/** Search for a key in an alist. 35.193 - * An alist is a list of conses, where the cars 35.194 - * of the conses are the keys. Compares keys using eq. 35.195 - * 35.196 - * @param k key 35.197 - * @param l alist to search 35.198 - * @return first element of l with car k, or ONULL 35.199 - */ 35.200 -Sxpr assocq(Sxpr k, Sxpr l){ 35.201 - for( ; CONSP(l); l = CDR(l)){ 35.202 - Sxpr x = CAR(l); 35.203 - if(CONSP(x) && eq(k, CAR(x))){ 35.204 - return x; 35.205 - } 35.206 - } 35.207 - return ONULL; 35.208 -} 35.209 - 35.210 -/** Add a new key and value to an alist. 35.211 - * 35.212 - * @param k key 35.213 - * @param l value 35.214 - * @param l alist 35.215 - * @return l with the new cell added to the front 35.216 - */ 35.217 -Sxpr acons(Sxpr k, Sxpr v, Sxpr l){ 35.218 - Sxpr x, y; 35.219 - x = cons_new(k, v); 35.220 - if(NOMEMP(x)) return x; 35.221 - y = cons_new(x, l); 35.222 - if(NOMEMP(y)) cons_free_cells(x); 35.223 - return y; 35.224 -} 35.225 - 35.226 -/** Test if a list contains an element. 35.227 - * Uses sxpr equality. 35.228 - * 35.229 - * @param l list 35.230 - * @param x element to look for 35.231 - * @return a tail of l with x as car, or ONULL 35.232 - */ 35.233 -Sxpr cons_member(Sxpr l, Sxpr x){ 35.234 - for( ; CONSP(l) && !eq(x, CAR(l)); l = CDR(l)){} 35.235 - return l; 35.236 -} 35.237 - 35.238 -/** Test if a list contains an element satisfying a test. 35.239 - * The test function is called with v and an element of the list. 35.240 - * 35.241 - * @param l list 35.242 - * @param test_fn test function to use 35.243 - * @param v value for first argument to the test 35.244 - * @return a tail of l with car satisfying the test, or 0 35.245 - */ 35.246 -Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v){ 35.247 - for( ; CONSP(l) && !test_fn(v, CAR(l)); l = CDR(l)){ } 35.248 - return l; 35.249 -} 35.250 - 35.251 -/** Test if the elements of list 't' are a subset of the elements 35.252 - * of list 's'. Element order is not significant. 35.253 - * 35.254 - * @param s element list to check subset of 35.255 - * @param t element list to check if is a subset 35.256 - * @return 1 if is a subset, 0 otherwise 35.257 - */ 35.258 -int cons_subset(Sxpr s, Sxpr t){ 35.259 - for( ; CONSP(t); t = CDR(t)){ 35.260 - if(!CONSP(cons_member(s, CAR(t)))){ 35.261 - return 0; 35.262 - } 35.263 - } 35.264 - return 1; 35.265 -} 35.266 - 35.267 -/** Test if two lists have equal sets of elements. 35.268 - * Element order is not significant. 35.269 - * 35.270 - * @param s list to check 35.271 - * @param t list to check 35.272 - * @return 1 if equal, 0 otherwise 35.273 - */ 35.274 -int cons_set_equal(Sxpr s, Sxpr t){ 35.275 - return cons_subset(s, t) && cons_subset(t, s); 35.276 -} 35.277 - 35.278 -#ifdef USE_GC 35.279 -/*============================================================================*/ 35.280 -/* The functions inside this ifdef are only safe if GC is used. 35.281 - * Otherwise they may leak memory. 35.282 - */ 35.283 - 35.284 -/** Remove an element from a list (GC only). 35.285 - * Uses sxpr equality and removes all instances, even 35.286 - * if there are more than one. 35.287 - * 35.288 - * @param l list to remove elements from 35.289 - * @param x element to remove 35.290 - * @return modified input list 35.291 - */ 35.292 -Sxpr cons_remove(Sxpr l, Sxpr x){ 35.293 - return cons_remove_if(l, eq, x); 35.294 -} 35.295 - 35.296 -/** Remove elements satisfying a test (GC only). 35.297 - * The test function is called with v and an element of the set. 35.298 - * 35.299 - * @param l list to remove elements from 35.300 - * @param test_fn function to use to decide if an element should be removed 35.301 - * @return modified input list 35.302 - */ 35.303 -Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v){ 35.304 - Sxpr prev = ONULL, elt, next; 35.305 - 35.306 - for(elt = l; CONSP(elt); elt = next){ 35.307 - next = CDR(elt); 35.308 - if(test_fn(v, CAR(elt))){ 35.309 - if(NULLP(prev)){ 35.310 - l = next; 35.311 - } else { 35.312 - CDR(prev) = next; 35.313 - } 35.314 - } 35.315 - } 35.316 - return l; 35.317 -} 35.318 - 35.319 -/** Set the value for a key in an alist (GC only). 35.320 - * If the key is present, changes the value, otherwise 35.321 - * adds a new cell. 35.322 - * 35.323 - * @param k key 35.324 - * @param v value 35.325 - * @param l alist 35.326 - * @return modified or extended list 35.327 - */ 35.328 -Sxpr setf(Sxpr k, Sxpr v, Sxpr l){ 35.329 - Sxpr e = assoc(k, l); 35.330 - if(NULLP(e)){ 35.331 - l = acons(k, v, l); 35.332 - } else { 35.333 - CAR(CDR(e)) = v; 35.334 - } 35.335 - return l; 35.336 -} 35.337 -/*============================================================================*/ 35.338 -#endif /* USE_GC */ 35.339 - 35.340 -/** Create a new atom with the given name. 35.341 - * 35.342 - * @param name the name 35.343 - * @return new atom 35.344 - */ 35.345 -Sxpr atom_new(char *name){ 35.346 - Sxpr n, obj = ONOMEM; 35.347 - 35.348 - n = string_new(name); 35.349 - if(NOMEMP(n)) goto exit; 35.350 - obj = HALLOC(ObjAtom, T_ATOM); 35.351 - if(NOMEMP(obj)) goto exit; 35.352 - OBJ_ATOM(obj)->name = n; 35.353 - exit: 35.354 - return obj; 35.355 -} 35.356 - 35.357 -/** Free an atom. 35.358 - * 35.359 - * @param obj to free 35.360 - */ 35.361 -void atom_free(Sxpr obj){ 35.362 - // Interned atoms are shared, so do not free. 35.363 - if(OBJ_ATOM(obj)->interned) return; 35.364 - objfree(OBJ_ATOM(obj)->name); 35.365 - hfree(obj); 35.366 -} 35.367 - 35.368 -/** Print an atom. Prints the atom name. 35.369 - * 35.370 - * @param io stream to print to 35.371 - * @param obj to print 35.372 - * @param flags print flags 35.373 - * @return number of bytes printed 35.374 - */ 35.375 -int atom_print(IOStream *io, Sxpr obj, unsigned flags){ 35.376 - //return string_print(io, OBJ_ATOM(obj)->name, (flags | PRINT_RAW)); 35.377 - return string_print(io, OBJ_ATOM(obj)->name, flags); 35.378 -} 35.379 - 35.380 -/** Atom equality. 35.381 - * 35.382 - * @param x to compare 35.383 - * @param y to compare 35.384 - * @return 1 if equal, 0 otherwise 35.385 - */ 35.386 -int atom_equal(Sxpr x, Sxpr y){ 35.387 - int ok; 35.388 - ok = eq(x, y); 35.389 - if(ok) goto exit; 35.390 - ok = ATOMP(y) && string_equal(OBJ_ATOM(x)->name, OBJ_ATOM(y)->name); 35.391 - if(ok) goto exit; 35.392 - ok = STRINGP(y) && string_equal(OBJ_ATOM(x)->name, y); 35.393 - exit: 35.394 - return ok; 35.395 -} 35.396 - 35.397 -/** Get the name of an atom. 35.398 - * 35.399 - * @param obj atom 35.400 - * @return name 35.401 - */ 35.402 -char * atom_name(Sxpr obj){ 35.403 - return string_string(OBJ_ATOM(obj)->name); 35.404 -} 35.405 - 35.406 -/** Get the C string from a string sxpr. 35.407 - * 35.408 - * @param obj string sxpr 35.409 - * @return string 35.410 - */ 35.411 -char * string_string(Sxpr obj){ 35.412 - return OBJ_STRING(obj); 35.413 -} 35.414 - 35.415 -/** Get the length of a string. 35.416 - * 35.417 - * @param obj string 35.418 - * @return length 35.419 - */ 35.420 -int string_length(Sxpr obj){ 35.421 - return strlen(OBJ_STRING(obj)); 35.422 -} 35.423 - 35.424 -/** Create a new string. The input string is copied, 35.425 - * and must be null-terminated. 35.426 - * 35.427 - * @param s characters to put in the string 35.428 - * @return new sxpr 35.429 - */ 35.430 -Sxpr string_new(char *s){ 35.431 - int n = (s ? strlen(s) : 0); 35.432 - Sxpr obj; 35.433 - obj = halloc(n+1, T_STRING); 35.434 - if(!NOMEMP(obj)){ 35.435 - char *str = OBJ_STRING(obj); 35.436 - strncpy(str, s, n); 35.437 - str[n] = '\0'; 35.438 - } 35.439 - return obj; 35.440 -} 35.441 - 35.442 -/** Free a string. 35.443 - * 35.444 - * @param obj to free 35.445 - */ 35.446 -void string_free(Sxpr obj){ 35.447 - hfree(obj); 35.448 -} 35.449 - 35.450 -/** Determine if a string needs escapes when printed 35.451 - * using the given flags. 35.452 - * 35.453 - * @param str string to check 35.454 - * @param flags print flags 35.455 - * @return 1 if needs escapes, 0 otherwise 35.456 - */ 35.457 -int needs_escapes(char *str, unsigned flags){ 35.458 - char *c; 35.459 - int val = 0; 35.460 - 35.461 - if(str){ 35.462 - for(c=str; *c; c++){ 35.463 - if(in_alpha_class(*c)) continue; 35.464 - if(in_decimal_digit_class(*c)) continue; 35.465 - if(in_class(*c, "/._+:@~-")) continue; 35.466 - val = 1; 35.467 - break; 35.468 - } 35.469 - } 35.470 - //printf("\n> val=%d str=|%s|\n", val, str); 35.471 - return val; 35.472 -} 35.473 - 35.474 -/** Print a string to a stream, with escapes if necessary. 35.475 - * 35.476 - * @param io stream to print to 35.477 - * @param str string 35.478 - * @param flags print flags 35.479 - * @return number of bytes written 35.480 - */ 35.481 -int _string_print(IOStream *io, char *str, unsigned flags){ 35.482 - int k = 0; 35.483 - if((flags & PRINT_RAW) || !needs_escapes(str, flags)){ 35.484 - k += IOStream_print(io, str); 35.485 - } else { 35.486 - k += IOStream_print(io, "\""); 35.487 - if(str){ 35.488 - char *s; 35.489 - for(s = str; *s; s++){ 35.490 - if(*s < ' ' || *s >= 127 ){ 35.491 - switch(*s){ 35.492 - case '\a': k += IOStream_print(io, "\\a"); break; 35.493 - case '\b': k += IOStream_print(io, "\\b"); break; 35.494 - case '\f': k += IOStream_print(io, "\\f"); break; 35.495 - case '\n': k += IOStream_print(io, "\\n"); break; 35.496 - case '\r': k += IOStream_print(io, "\\r"); break; 35.497 - case '\t': k += IOStream_print(io, "\\t"); break; 35.498 - case '\v': k += IOStream_print(io, "\\v"); break; 35.499 - default: 35.500 - // Octal escape; 35.501 - k += IOStream_print(io, "\\%o", *s); 35.502 - break; 35.503 - } 35.504 - } else if(*s == c_double_quote || 35.505 - *s == c_single_quote || 35.506 - *s == c_escape){ 35.507 - k += IOStream_print(io, "\\%c", *s); 35.508 - } else { 35.509 - k+= IOStream_print(io, "%c", *s); 35.510 - } 35.511 - } 35.512 - } 35.513 - k += IOStream_print(io, "\""); 35.514 - } 35.515 - return k; 35.516 -} 35.517 - 35.518 -/** Print a string to a stream, with escapes if necessary. 35.519 - * 35.520 - * @param io stream to print to 35.521 - * @param obj string 35.522 - * @param flags print flags 35.523 - * @return number of bytes written 35.524 - */ 35.525 -int string_print(IOStream *io, Sxpr obj, unsigned flags){ 35.526 - return _string_print(io, OBJ_STRING(obj), flags); 35.527 -} 35.528 - 35.529 -/** Compare an sxpr with a string for equality. 35.530 - * 35.531 - * @param x string to compare with 35.532 - * @param y sxpr to compare 35.533 - * @return 1 if equal, 0 otherwise 35.534 - */ 35.535 -int string_equal(Sxpr x, Sxpr y){ 35.536 - int ok = 0; 35.537 - ok = eq(x,y); 35.538 - if(ok) goto exit; 35.539 - ok = has_type(y, T_STRING) && !strcmp(OBJ_STRING(x), OBJ_STRING(y)); 35.540 - if(ok) goto exit; 35.541 - ok = has_type(y, T_ATOM) && !strcmp(OBJ_STRING(x), atom_name(y)); 35.542 - exit: 35.543 - return ok; 35.544 -} 35.545 - 35.546 -/** Create a new cons cell. 35.547 - * The cell is ONOMEM if either argument is. 35.548 - * 35.549 - * @param car sxpr for the car 35.550 - * @param cdr sxpr for the cdr 35.551 - * @return new cons 35.552 - */ 35.553 -Sxpr cons_new(Sxpr car, Sxpr cdr){ 35.554 - Sxpr obj; 35.555 - if(NOMEMP(car) || NOMEMP(cdr)){ 35.556 - obj = ONOMEM; 35.557 - } else { 35.558 - obj = HALLOC(ObjCons, T_CONS); 35.559 - if(!NOMEMP(obj)){ 35.560 - ObjCons *z = OBJ_CONS(obj); 35.561 - z->car = car; 35.562 - z->cdr = cdr; 35.563 - } 35.564 - } 35.565 - return obj; 35.566 -} 35.567 - 35.568 -/** Push a new element onto a list. 35.569 - * 35.570 - * @param list list to add to 35.571 - * @param elt element to add 35.572 - * @return 0 if successful, error code otherwise 35.573 - */ 35.574 -int cons_push(Sxpr *list, Sxpr elt){ 35.575 - Sxpr l; 35.576 - l = cons_new(elt, *list); 35.577 - if(NOMEMP(l)) return -ENOMEM; 35.578 - *list = l; 35.579 - return 0; 35.580 -} 35.581 - 35.582 -/** Free a cons. Recursively frees the car and cdr. 35.583 - * 35.584 - * @param obj to free 35.585 - */ 35.586 -void cons_free(Sxpr obj){ 35.587 - Sxpr next; 35.588 - for(; CONSP(obj); obj = next){ 35.589 - next = CDR(obj); 35.590 - objfree(CAR(obj)); 35.591 - hfree(obj); 35.592 - } 35.593 - if(!NULLP(obj)){ 35.594 - objfree(obj); 35.595 - } 35.596 -} 35.597 - 35.598 -/** Free a cons and its cdr cells, but not the car sxprs. 35.599 - * Does nothing if called on something that is not a cons. 35.600 - * 35.601 - * @param obj to free 35.602 - */ 35.603 -void cons_free_cells(Sxpr obj){ 35.604 - Sxpr next; 35.605 - for(; CONSP(obj); obj = next){ 35.606 - next = CDR(obj); 35.607 - hfree(obj); 35.608 - } 35.609 -} 35.610 - 35.611 -/** Print a cons. 35.612 - * Prints the cons in list format if the cdrs are conses. 35.613 - * uses pair (dot) format if the last cdr is not a cons (or null). 35.614 - * 35.615 - * @param io stream to print to 35.616 - * @param obj to print 35.617 - * @param flags print flags 35.618 - * @return number of bytes written 35.619 - */ 35.620 -int cons_print(IOStream *io, Sxpr obj, unsigned flags){ 35.621 - int first = 1; 35.622 - int k = 0; 35.623 - k += IOStream_print(io, "("); 35.624 - for( ; CONSP(obj) ; obj = CDR(obj)){ 35.625 - if(first){ 35.626 - first = 0; 35.627 - } else { 35.628 - k += IOStream_print(io, " "); 35.629 - } 35.630 - k += objprint(io, CAR(obj), flags); 35.631 - } 35.632 - if(!NULLP(obj)){ 35.633 - k += IOStream_print(io, " . "); 35.634 - k += objprint(io, obj, flags); 35.635 - } 35.636 - k += IOStream_print(io, ")"); 35.637 - return (IOStream_error(io) ? -1 : k); 35.638 -} 35.639 - 35.640 -/** Compare a cons with another sxpr for equality. 35.641 - * If y is a cons, compares the cars and cdrs recursively. 35.642 - * 35.643 - * @param x cons to compare 35.644 - * @param y sxpr to compare 35.645 - * @return 1 if equal, 0 otherwise 35.646 - */ 35.647 -int cons_equal(Sxpr x, Sxpr y){ 35.648 - return CONSP(y) && 35.649 - objequal(CAR(x), CAR(y)) && 35.650 - objequal(CDR(x), CDR(y)); 35.651 -} 35.652 - 35.653 -/** Return the length of a cons list. 35.654 - * 35.655 - * @param obj list 35.656 - * @return length 35.657 - */ 35.658 -int cons_length(Sxpr obj){ 35.659 - int count = 0; 35.660 - for( ; CONSP(obj); obj = CDR(obj)){ 35.661 - count++; 35.662 - } 35.663 - return count; 35.664 -} 35.665 - 35.666 -/** Destructively reverse a cons list in-place. 35.667 - * If the argument is not a cons it is returned unchanged. 35.668 - * 35.669 - * @param l to reverse 35.670 - * @return reversed list 35.671 - */ 35.672 -Sxpr nrev(Sxpr l){ 35.673 - if(CONSP(l)){ 35.674 - // Iterate down the cells in the list making the cdr of 35.675 - // each cell point to the previous cell. The last cell 35.676 - // is the head of the reversed list. 35.677 - Sxpr prev = ONULL; 35.678 - Sxpr cell = l; 35.679 - Sxpr next; 35.680 - 35.681 - while(1){ 35.682 - next = CDR(cell); 35.683 - CDR(cell) = prev; 35.684 - if(!CONSP(next)) break; 35.685 - prev = cell; 35.686 - cell = next; 35.687 - } 35.688 - l = cell; 35.689 - } 35.690 - return l; 35.691 -} 35.692 - 35.693 -/** Print the null sxpr. 35.694 - * 35.695 - * @param io stream to print to 35.696 - * @param obj to print 35.697 - * @param flags print flags 35.698 - * @return number of bytes written 35.699 - */ 35.700 -static int null_print(IOStream *io, Sxpr obj, unsigned flags){ 35.701 - return IOStream_print(io, "()"); 35.702 -} 35.703 - 35.704 -/** Print the `unspecified' sxpr none. 35.705 - * 35.706 - * @param io stream to print to 35.707 - * @param obj to print 35.708 - * @param flags print flags 35.709 - * @return number of bytes written 35.710 - */ 35.711 -static int none_print(IOStream *io, Sxpr obj, unsigned flags){ 35.712 - return IOStream_print(io, "<none>"); 35.713 -} 35.714 - 35.715 -/** Print an integer. 35.716 - * 35.717 - * @param io stream to print to 35.718 - * @param obj to print 35.719 - * @param flags print flags 35.720 - * @return number of bytes written 35.721 - */ 35.722 -static int int_print(IOStream *io, Sxpr obj, unsigned flags){ 35.723 - return IOStream_print(io, "%d", OBJ_INT(obj)); 35.724 -} 35.725 - 35.726 -/** Print a boolean. 35.727 - * 35.728 - * @param io stream to print to 35.729 - * @param obj to print 35.730 - * @param flags print flags 35.731 - * @return number of bytes written 35.732 - */ 35.733 -static int bool_print(IOStream *io, Sxpr obj, unsigned flags){ 35.734 - return IOStream_print(io, (OBJ_UINT(obj) ? k_true : k_false)); 35.735 -} 35.736 - 35.737 -int sxprp(Sxpr obj, Sxpr name){ 35.738 - return CONSP(obj) && objequal(CAR(obj), name); 35.739 -} 35.740 - 35.741 -/** Get the name of an element. 35.742 - * 35.743 - * @param obj element 35.744 - * @return name 35.745 - */ 35.746 -Sxpr sxpr_name(Sxpr obj){ 35.747 - Sxpr val = ONONE; 35.748 - if(CONSP(obj)){ 35.749 - val = CAR(obj); 35.750 - } else if(STRINGP(obj) || ATOMP(obj)){ 35.751 - val = obj; 35.752 - } 35.753 - return val; 35.754 -} 35.755 - 35.756 -int sxpr_is(Sxpr obj, char *s){ 35.757 - if(ATOMP(obj)) return !strcmp(atom_name(obj), s); 35.758 - if(STRINGP(obj)) return !strcmp(string_string(obj), s); 35.759 - return 0; 35.760 -} 35.761 - 35.762 -int sxpr_elementp(Sxpr obj, Sxpr name){ 35.763 - return CONSP(obj) && objequal(CAR(obj), name); 35.764 -} 35.765 - 35.766 -/** Get the attributes of an sxpr. 35.767 - * 35.768 - * @param obj sxpr 35.769 - * @return attributes 35.770 - */ 35.771 -Sxpr sxpr_attributes(Sxpr obj){ 35.772 - Sxpr val = ONULL; 35.773 - if(CONSP(obj)){ 35.774 - obj = CDR(obj); 35.775 - if(CONSP(obj)){ 35.776 - obj = CAR(obj); 35.777 - if(sxprp(obj, intern("@"))){ 35.778 - val = CDR(obj); 35.779 - } 35.780 - } 35.781 - } 35.782 - return val; 35.783 -} 35.784 - 35.785 -Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def){ 35.786 - Sxpr val = ONONE; 35.787 - val = assoc(sxpr_attributes(obj), key); 35.788 - if(CONSP(val) && CONSP(CDR(val))){ 35.789 - val = CADR(def); 35.790 - } else { 35.791 - val = def; 35.792 - } 35.793 - return val; 35.794 -} 35.795 - 35.796 -/** Get the children of an sxpr. 35.797 - * 35.798 - * @param obj sxpr 35.799 - * @return children 35.800 - */ 35.801 -Sxpr sxpr_children(Sxpr obj){ 35.802 - Sxpr val = ONULL; 35.803 - if(CONSP(obj)){ 35.804 - val = CDR(obj); 35.805 - if(CONSP(val) && sxprp(CAR(val), intern("@"))){ 35.806 - val = CDR(val); 35.807 - } 35.808 - } 35.809 - return val; 35.810 -} 35.811 - 35.812 -Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def){ 35.813 - Sxpr val = ONONE; 35.814 - Sxpr l; 35.815 - for(l = sxpr_children(obj); CONSP(l); l = CDR(l)){ 35.816 - if(sxprp(CAR(l), name)){ 35.817 - val = CAR(l); 35.818 - break; 35.819 - } 35.820 - } 35.821 - if(NONEP(val)) val = def; 35.822 - return val; 35.823 -} 35.824 - 35.825 -Sxpr sxpr_child0(Sxpr obj, Sxpr def){ 35.826 - Sxpr val = ONONE; 35.827 - Sxpr l = sxpr_children(obj); 35.828 - if(CONSP(l)){ 35.829 - val = CAR(l); 35.830 - } else { 35.831 - val = def; 35.832 - } 35.833 - return val; 35.834 -} 35.835 - 35.836 -Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def){ 35.837 - Sxpr val = ONONE; 35.838 - val = sxpr_child(obj, name, ONONE); 35.839 - if(NONEP(val)){ 35.840 - val = def; 35.841 - } else { 35.842 - val = sxpr_child0(val, def); 35.843 - } 35.844 - return val; 35.845 -} 35.846 - 35.847 -/** Table of interned symbols. Indexed by symbol name. */ 35.848 -static HashTable *symbols = NULL; 35.849 - 35.850 -/** Hash function for entries in the symbol table. 35.851 - * 35.852 - * @param key to hash 35.853 - * @return hashcode 35.854 - */ 35.855 -static Hashcode sym_hash_fn(void *key){ 35.856 - return hash_string((char*)key); 35.857 -} 35.858 - 35.859 -/** Key equality function for the symbol table. 35.860 - * 35.861 - * @param x to compare 35.862 - * @param y to compare 35.863 - * @return 1 if equal, 0 otherwise 35.864 - */ 35.865 -static int sym_equal_fn(void *x, void *y){ 35.866 - return !strcmp((char*)x, (char*)y); 35.867 -} 35.868 - 35.869 -/** Entry free function for the symbol table. 35.870 - * 35.871 - * @param table the entry is in 35.872 - * @param entry being freed 35.873 - */ 35.874 -static void sym_free_fn(HashTable *table, HTEntry *entry){ 35.875 - if(entry){ 35.876 - objfree(((ObjAtom*)entry->value)->name); 35.877 - HTEntry_free(entry); 35.878 - } 35.879 -} 35.880 - 35.881 -/** Initialize the symbol table. 35.882 - * 35.883 - * @return 0 on sucess, error code otherwise 35.884 - */ 35.885 -static int init_symbols(void){ 35.886 - symbols = HashTable_new(100); 35.887 - if(symbols){ 35.888 - symbols->key_hash_fn = sym_hash_fn; 35.889 - symbols->key_equal_fn = sym_equal_fn; 35.890 - symbols->entry_free_fn = sym_free_fn; 35.891 - return 0; 35.892 - } 35.893 - return -1; 35.894 -} 35.895 - 35.896 -/** Cleanup the symbol table. Frees the table and all its symbols. 35.897 - */ 35.898 -void cleanup_symbols(void){ 35.899 - HashTable_free(symbols); 35.900 - symbols = NULL; 35.901 -} 35.902 - 35.903 -/** Get the interned symbol with the given name. 35.904 - * No new symbol is created. 35.905 - * 35.906 - * @return symbol or null 35.907 - */ 35.908 -Sxpr get_symbol(char *sym){ 35.909 - HTEntry *entry; 35.910 - if(!symbols){ 35.911 - if(init_symbols()) return ONOMEM; 35.912 - return ONULL; 35.913 - } 35.914 - entry = HashTable_get_entry(symbols, sym); 35.915 - if(entry){ 35.916 - return OBJP(T_ATOM, entry->value); 35.917 - } else { 35.918 - return ONULL; 35.919 - } 35.920 -} 35.921 - 35.922 -/** Get the interned symbol with the given name. 35.923 - * Creates a new symbol if necessary. 35.924 - * 35.925 - * @return symbol 35.926 - */ 35.927 -Sxpr intern(char *sym){ 35.928 - Sxpr symbol = get_symbol(sym); 35.929 - if(NULLP(symbol)){ 35.930 - if(!symbols) return ONOMEM; 35.931 - symbol = atom_new(sym); 35.932 - if(!NOMEMP(symbol)){ 35.933 - OBJ_ATOM(symbol)->interned = TRUE; 35.934 - HashTable_add(symbols, atom_name(symbol), get_ptr(symbol)); 35.935 - } 35.936 - } 35.937 - return symbol; 35.938 -}
36.1 --- a/tools/lib/sxpr.h Tue Jun 29 14:50:09 2004 +0000 36.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 36.3 @@ -1,413 +0,0 @@ 36.4 -/* 36.5 - * 36.6 - * This library is free software; you can redistribute it and/or modify 36.7 - * it under the terms of the GNU Lesser General Public License as 36.8 - * published by the Free Software Foundation; either version 2.1 of the 36.9 - * License, or (at your option) any later version. This library is 36.10 - * distributed in the hope that it will be useful, but WITHOUT ANY 36.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 36.12 - * FITNESS FOR A PARTICULAR PURPOSE. 36.13 - * See the GNU Lesser General Public License for more details. 36.14 - * 36.15 - * You should have received a copy of the GNU Lesser General Public License 36.16 - * along with this library; if not, write to the Free Software Foundation, 36.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 36.18 - */ 36.19 -#ifndef _XEN_LIB_SXPR_H_ 36.20 -#define _XEN_LIB_SXPR_H_ 36.21 - 36.22 -#include <stdint.h> 36.23 - 36.24 -#include "hash_table.h" 36.25 -#include "iostream.h" 36.26 -#include "allocate.h" 36.27 - 36.28 -/** @file 36.29 - * Definitions for rules and sxprs. 36.30 - */ 36.31 - 36.32 -#ifndef NULL 36.33 -#define NULL 0 36.34 -#endif 36.35 - 36.36 -#ifndef TRUE 36.37 -#define TRUE 1 36.38 -#endif 36.39 - 36.40 -#ifndef FALSE 36.41 -#define FALSE 0 36.42 -#endif 36.43 - 36.44 -/** Sxpr type. */ 36.45 -typedef int16_t TypeCode; 36.46 - 36.47 -/** A typed sxpr handle.*/ 36.48 -typedef struct Sxpr { 36.49 - /** Sxpr type. */ 36.50 - TypeCode type; 36.51 - union { 36.52 - /** Sxpr value. */ 36.53 - unsigned long ul; 36.54 - /** Pointer. */ 36.55 - void *ptr; 36.56 - } v; 36.57 -} Sxpr; 36.58 - 36.59 -/** Sxpr type to indicate out of memory. */ 36.60 -#define T_NOMEM ((TypeCode)-1) 36.61 -/** The 'unspecified' sxpr. */ 36.62 -#define T_NONE ((TypeCode)0) 36.63 -/** The empty list. */ 36.64 -#define T_NULL ((TypeCode)1) 36.65 -/** Unsigned integer. */ 36.66 -#define T_UINT ((TypeCode)2) 36.67 -/** A string. */ 36.68 -#define T_STRING ((TypeCode)3) 36.69 -/** An atom. */ 36.70 -#define T_ATOM ((TypeCode)4) 36.71 -/** A boolean. */ 36.72 -#define T_BOOL ((TypeCode)5) 36.73 - 36.74 -/** A cons (pair or list). */ 36.75 -#define T_CONS ((TypeCode)10) 36.76 - 36.77 -/** An error. */ 36.78 -#define T_ERR ((TypeCode)40) 36.79 - 36.80 -/** An atom. */ 36.81 -typedef struct ObjAtom { 36.82 - Sxpr name; 36.83 - Hashcode hashcode; 36.84 - int interned; 36.85 -} ObjAtom; 36.86 - 36.87 -/** A cons (pair). */ 36.88 -typedef struct ObjCons { 36.89 - Sxpr car; 36.90 - Sxpr cdr; 36.91 -} ObjCons; 36.92 - 36.93 -/** A vector. */ 36.94 -typedef struct ObjVector { 36.95 - int n; 36.96 - Sxpr data[0]; 36.97 -} ObjVector; 36.98 - 36.99 -/** Flags for sxpr printing. */ 36.100 -enum PrintFlags { 36.101 - PRINT_RAW = 0x001, 36.102 - PRINT_TYPE = 0x002, 36.103 - PRINT_PRETTY = 0x004, 36.104 - PRINT_NUM = 0x008, 36.105 -}; 36.106 - 36.107 -/** An integer sxpr. 36.108 - * 36.109 - * @param ty type 36.110 - * @param val integer value 36.111 - */ 36.112 -#define OBJI(ty, val) (Sxpr){ type: (ty), v: { ul: (val) }} 36.113 - 36.114 -/** A pointer sxpr. 36.115 - * If the pointer is non-null, returns an sxpr containing it. 36.116 - * If the pointer is null, returns ONOMEM. 36.117 - * 36.118 - * @param ty type 36.119 - * @param val pointer 36.120 - */ 36.121 -#define OBJP(ty, val) ((val) ? (Sxpr){ type: (ty), v: { ptr: (val) }} : ONOMEM) 36.122 - 36.123 -/** Make an integer sxpr containing a pointer. 36.124 - * 36.125 - * @param val pointer 36.126 - */ 36.127 -#define PTR(val) OBJP(T_UINT, (void*)(val)) 36.128 - 36.129 -/** Make an integer sxpr. 36.130 - * @param x value 36.131 - */ 36.132 -#define OINT(x) OBJI(T_UINT, x) 36.133 - 36.134 -/** Make an error sxpr. 36.135 - * 36.136 - * @param x value 36.137 - */ 36.138 -#define OERR(x) OBJI(T_ERR, x) 36.139 - 36.140 -/** Out of memory constant. */ 36.141 -#define ONOMEM OBJI(T_NOMEM, 0) 36.142 - 36.143 -/** The `unspecified' constant. */ 36.144 -#define ONONE OBJI(T_NONE, 0) 36.145 - 36.146 -/** Empty list constant. */ 36.147 -#define ONULL OBJI(T_NULL, 0) 36.148 - 36.149 -/** False constant. */ 36.150 -#define OFALSE OBJI(T_BOOL, 0) 36.151 - 36.152 -/** True constant. */ 36.153 -#define OTRUE OBJI(T_BOOL, 1) 36.154 - 36.155 -/* Recognizers for the various sxpr types. */ 36.156 -#define ATOMP(obj) has_type(obj, T_ATOM) 36.157 -#define BOOLP(obj) has_type(obj, T_BOOL) 36.158 -#define CONSP(obj) has_type(obj, T_CONS) 36.159 -#define ERRP(obj) has_type(obj, T_ERR) 36.160 -#define INTP(obj) has_type(obj, T_UINT) 36.161 -#define NOMEMP(obj) has_type(obj, T_NOMEM) 36.162 -#define NONEP(obj) has_type(obj, T_NONE) 36.163 -#define NULLP(obj) has_type(obj, T_NULL) 36.164 -#define STRINGP(obj) has_type(obj, T_STRING) 36.165 - 36.166 -#define TRUEP(obj) get_ul(obj) 36.167 - 36.168 -/** Convert an sxpr to an unsigned integer. */ 36.169 -#define OBJ_UINT(x) get_ul(x) 36.170 -/** Convert an sxpr to an integer. */ 36.171 -#define OBJ_INT(x) (int)get_ul(x) 36.172 - 36.173 -/* Conversions of sxprs to their values. 36.174 - * No checking is done. 36.175 - */ 36.176 -#define OBJ_STRING(x) ((char*)get_ptr(x)) 36.177 -#define OBJ_CONS(x) ((ObjCons*)get_ptr(x)) 36.178 -#define OBJ_ATOM(x) ((ObjAtom*)get_ptr(x)) 36.179 -#define OBJ_SET(x) ((ObjSet*)get_ptr(x)) 36.180 -#define CAR(x) (OBJ_CONS(x)->car) 36.181 -#define CDR(x) (OBJ_CONS(x)->cdr) 36.182 - 36.183 -#define CAAR(x) (CAR(CAR(x))) 36.184 -#define CADR(x) (CAR(CDR(x))) 36.185 -#define CDAR(x) (CDR(CAR(x))) 36.186 -#define CDDR(x) (CDR(CDR(x))) 36.187 - 36.188 -/** Get the integer value from an sxpr. 36.189 - * 36.190 - * @param obj sxpr 36.191 - * @return value 36.192 - */ 36.193 -static inline unsigned long get_ul(Sxpr obj){ 36.194 - return obj.v.ul; 36.195 -} 36.196 - 36.197 -/** Get the pointer value from an sxpr. 36.198 - * 36.199 - * @param obj sxpr 36.200 - * @return value 36.201 - */ 36.202 -static inline void * get_ptr(Sxpr obj){ 36.203 - return obj.v.ptr; 36.204 -} 36.205 - 36.206 -/** Create an sxpr containing a pointer. 36.207 - * 36.208 - * @param type typecode 36.209 - * @param val pointer 36.210 - * @return sxpr 36.211 - */ 36.212 -static inline Sxpr obj_ptr(TypeCode type, void *val){ 36.213 - return (Sxpr){ type: type, v: { ptr: val } }; 36.214 -} 36.215 - 36.216 -/** Create an sxpr containing an integer. 36.217 - * 36.218 - * @param type typecode 36.219 - * @param val integer 36.220 - * @return sxpr 36.221 - */ 36.222 -static inline Sxpr obj_ul(TypeCode type, unsigned long val){ 36.223 - return (Sxpr){ type: type, v: { ul: val } }; 36.224 -} 36.225 - 36.226 -/** Get the type of an sxpr. 36.227 - * 36.228 - * @param obj sxpr 36.229 - * @return type 36.230 - */ 36.231 -static inline TypeCode get_type(Sxpr obj){ 36.232 - return obj.type; 36.233 -} 36.234 - 36.235 -/** Check the type of an sxpr. 36.236 - * 36.237 - * @param obj sxpr 36.238 - * @param type to check 36.239 - * @return 1 if has the type, 0 otherwise 36.240 - */ 36.241 -static inline int has_type(Sxpr obj, TypeCode type){ 36.242 - return get_type(obj) == type; 36.243 -} 36.244 - 36.245 -/** Compare sxprs for literal equality of type and value. 36.246 - * 36.247 - * @param x sxpr to compare 36.248 - * @param y sxpr to compare 36.249 - * @return 1 if equal, 0 otherwise 36.250 - */ 36.251 -static inline int eq(Sxpr x, Sxpr y){ 36.252 - return ((get_type(x) == get_type(y)) && (get_ul(x) == get_ul(y))); 36.253 -} 36.254 - 36.255 -/** Checked version of CAR 36.256 - * 36.257 - * @param x sxpr 36.258 - * @return CAR if a cons, x otherwise 36.259 - */ 36.260 -static inline Sxpr car(Sxpr x){ 36.261 - return (CONSP(x) ? CAR(x) : x); 36.262 -} 36.263 - 36.264 -/** Checked version of CDR. 36.265 - * 36.266 - * @param x sxpr 36.267 - * @return CDR if a cons, null otherwise 36.268 - */ 36.269 -static inline Sxpr cdr(Sxpr x){ 36.270 - return (CONSP(x) ? CDR(x) : ONULL); 36.271 -} 36.272 - 36.273 -/** Allocate some memory and return an sxpr containing it. 36.274 - * Returns ONOMEM if allocation failed. 36.275 - * 36.276 - * @param n number of bytes to allocate 36.277 - * @param ty typecode 36.278 - * @return sxpr 36.279 - */ 36.280 -static inline Sxpr halloc(size_t n, TypeCode ty){ 36.281 - return OBJP(ty, allocate(n)); 36.282 -} 36.283 - 36.284 -/** Allocate an sxpr containing a pointer to the given type. 36.285 - * 36.286 - * @param ty type (uses sizeof to determine how many bytes to allocate) 36.287 - * @param code typecode 36.288 - * @return sxpr, ONOMEM if allocation failed 36.289 - */ 36.290 -#define HALLOC(ty, code) halloc(sizeof(ty), code) 36.291 - 36.292 -typedef int ObjPrintFn(IOStream *io, Sxpr obj, unsigned flags); 36.293 -typedef int ObjEqualFn(Sxpr obj, Sxpr other); 36.294 -typedef void ObjFreeFn(Sxpr obj); 36.295 - 36.296 -/** An sxpr type definition. */ 36.297 -typedef struct SxprType { 36.298 - TypeCode type; 36.299 - char *name; 36.300 - int pointer; 36.301 - ObjPrintFn *print; 36.302 - ObjEqualFn *equal; 36.303 - ObjFreeFn *free; 36.304 -} SxprType; 36.305 - 36.306 - 36.307 -extern SxprType *get_sxpr_type(int ty); 36.308 - 36.309 -/** Free the pointer in an sxpr. 36.310 - * 36.311 - * @param x sxpr containing a pointer 36.312 - */ 36.313 -static inline void hfree(Sxpr x){ 36.314 - deallocate(get_ptr(x)); 36.315 -} 36.316 - 36.317 -extern int objprint(IOStream *io, Sxpr x, unsigned flags); 36.318 -extern int objequal(Sxpr x, Sxpr y); 36.319 -extern void objfree(Sxpr x); 36.320 - 36.321 -extern void cons_free_cells(Sxpr obj); 36.322 -extern Sxpr intern(char *s); 36.323 - 36.324 -extern Sxpr assoc(Sxpr k, Sxpr l); 36.325 -extern Sxpr assocq(Sxpr k, Sxpr l); 36.326 -extern Sxpr acons(Sxpr k, Sxpr v, Sxpr l); 36.327 -extern Sxpr nrev(Sxpr l); 36.328 -extern Sxpr cons_member(Sxpr l, Sxpr x); 36.329 -extern Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v); 36.330 -extern int cons_subset(Sxpr s, Sxpr t); 36.331 -extern int cons_set_equal(Sxpr s, Sxpr t); 36.332 - 36.333 -#ifdef USE_GC 36.334 -extern Sxpr cons_remove(Sxpr l, Sxpr x); 36.335 -extern Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v); 36.336 -#endif 36.337 - 36.338 -extern Sxpr atom_new(char *name); 36.339 -extern char * atom_name(Sxpr obj); 36.340 - 36.341 -extern Sxpr string_new(char *s); 36.342 -extern char * string_string(Sxpr obj); 36.343 -extern int string_length(Sxpr obj); 36.344 - 36.345 -extern Sxpr cons_new(Sxpr car, Sxpr cdr); 36.346 -extern int cons_push(Sxpr *list, Sxpr elt); 36.347 -extern int cons_length(Sxpr obj); 36.348 - 36.349 -Sxpr sxpr_name(Sxpr obj); 36.350 -int sxpr_is(Sxpr obj, char *s); 36.351 -int sxpr_elementp(Sxpr obj, Sxpr name); 36.352 -Sxpr sxpr_attributes(Sxpr obj); 36.353 -Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def); 36.354 -Sxpr sxpr_children(Sxpr obj); 36.355 -Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def); 36.356 -Sxpr sxpr_child0(Sxpr obj, Sxpr def); 36.357 -Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def); 36.358 - 36.359 -/** Create a new atom. 36.360 - * 36.361 - * @param s atom name 36.362 - * @return new atom 36.363 - */ 36.364 -static inline Sxpr mkatom(char *s){ 36.365 - return atom_new(s); 36.366 -} 36.367 - 36.368 -/** Create a new string sxpr. 36.369 - * 36.370 - * @param s string bytes (copied) 36.371 - * @return new string 36.372 - */ 36.373 -static inline Sxpr mkstring(char *s){ 36.374 - return string_new(s); 36.375 -} 36.376 - 36.377 -/** Create an integer sxpr. 36.378 - * 36.379 - * @param i value 36.380 - * @return sxpr 36.381 - */ 36.382 -static inline Sxpr mkint(int i){ 36.383 - return OBJI(T_UINT, i); 36.384 -} 36.385 - 36.386 -/** Create a boolean sxpr. 36.387 - * 36.388 - * @param b value 36.389 - * @return sxpr 36.390 - */ 36.391 -static inline Sxpr mkbool(int b){ 36.392 - return OBJI(T_BOOL, (b ? 1 : 0)); 36.393 -} 36.394 - 36.395 -/* Constants used in parsing and printing. */ 36.396 -#define k_list_open "(" 36.397 -#define c_list_open '(' 36.398 -#define k_list_close ")" 36.399 -#define c_list_close ')' 36.400 -#define k_true "true" 36.401 -#define k_false "false" 36.402 - 36.403 -#define c_var '$' 36.404 -#define c_escape '\\' 36.405 -#define c_single_quote '\'' 36.406 -#define c_double_quote '"' 36.407 -#define c_string_open c_double_quote 36.408 -#define c_string_close c_double_quote 36.409 -#define c_data_open '[' 36.410 -#define c_data_close ']' 36.411 -#define c_binary '*' 36.412 -#define c_eval '!' 36.413 -#define c_concat_open '{' 36.414 -#define c_concat_close '}' 36.415 - 36.416 -#endif /* ! _XEN_LIB_SXPR_H_ */
37.1 --- a/tools/lib/sxpr_parser.c Tue Jun 29 14:50:09 2004 +0000 37.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 37.3 @@ -1,897 +0,0 @@ 37.4 - 37.5 -#ifdef __KERNEL__ 37.6 -# include <linux/config.h> 37.7 -# include <linux/module.h> 37.8 -# include <linux/kernel.h> 37.9 -# include <linux/string.h> 37.10 -# include <linux/errno.h> 37.11 -#else 37.12 -# include <stdlib.h> 37.13 -# include <errno.h> 37.14 -#endif 37.15 - 37.16 -#include "iostream.h" 37.17 -#include "lexis.h" 37.18 -#include "sxpr_parser.h" 37.19 -#include "sys_string.h" 37.20 - 37.21 -/** @file 37.22 - * Sxpr parsing. 37.23 - * 37.24 - * So that the parser does not leak memory, all sxprs constructed by 37.25 - * the parser must be freed on error. On successful parse the sxpr 37.26 - * returned becomes the responsibility of the caller. 37.27 - * 37.28 - * @author Mike Wray <mike.wray@hpl.hp.com> 37.29 - */ 37.30 - 37.31 -#define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args) 37.32 -#define printf(fmt, args...) IOStream_print(iostdout, fmt, ##args) 37.33 - 37.34 -static void reset(Parser *z); 37.35 -static int inputchar(Parser *p, char c); 37.36 -static int savechar(Parser *p, char c); 37.37 -extern void parse_error(Parser *in); 37.38 -extern void parse_error_id(Parser *in, ParseErrorId id); 37.39 - 37.40 -static int begin_start(Parser *p, char c); 37.41 -static int state_start(Parser *p, char c); 37.42 -static int end_start(Parser *p); 37.43 - 37.44 -static int begin_comment(Parser *p, char c); 37.45 -static int state_comment(Parser *p, char c); 37.46 -static int end_comment(Parser *p); 37.47 - 37.48 -static int begin_string(Parser *p, char c); 37.49 -static int state_string(Parser *p, char c); 37.50 -static int end_string(Parser *p); 37.51 -static int state_escape(Parser *p, char c); 37.52 -static int state_octal(Parser *p, char c); 37.53 -static int state_hex(Parser *p, char c); 37.54 - 37.55 -static int begin_atom(Parser *p, char c); 37.56 -static int state_atom(Parser *p, char c); 37.57 -static int end_atom(Parser *p); 37.58 - 37.59 -static int state_list(Parser *p, char c); 37.60 -static int begin_list(Parser *p, char c); 37.61 -static int end_list(Parser *p); 37.62 - 37.63 -/** Print a parse error. 37.64 - * 37.65 - * @param in parser 37.66 - * @param msg format followed by printf arguments 37.67 - */ 37.68 -void eprintf(Parser *in, char *msg, ...){ 37.69 - va_list args; 37.70 - if(in->error_out){ 37.71 - va_start(args, msg); 37.72 - IOStream_vprint(in->error_out, msg, args); 37.73 - va_end(args); 37.74 - } 37.75 -} 37.76 - 37.77 -/** Print a parse warning. 37.78 - * 37.79 - * @param in parser 37.80 - * @param msg format followed by printf arguments 37.81 - */ 37.82 -void wprintf(Parser *in, char *msg, ...){ 37.83 - va_list args; 37.84 - if(in->error_out){ 37.85 - va_start(args, msg); 37.86 - IOStream_vprint(in->error_out, msg, args); 37.87 - va_end(args); 37.88 - } 37.89 -} 37.90 - 37.91 -/*============================================================================*/ 37.92 - 37.93 -/** Record defining the message for a parse error. */ 37.94 -typedef struct { 37.95 - ParseErrorId id; 37.96 - char *message; 37.97 -} ParseError; 37.98 - 37.99 -/** Format for printing parse error messages. */ 37.100 -#define PARSE_ERR_FMT "parse error> line %3d, column %2d: %s" 37.101 - 37.102 -/** Message catalog for the parse error codes. */ 37.103 -static ParseError catalog[] = { 37.104 - { PARSE_ERR_UNSPECIFIED, "unspecified error" }, 37.105 - { PARSE_ERR_NOMEM, "out of memory" }, 37.106 - { PARSE_ERR_UNEXPECTED_EOF, "unexpected end of input" }, 37.107 - { PARSE_ERR_TOKEN_TOO_LONG, "token too long" }, 37.108 - { PARSE_ERR_INVALID_SYNTAX, "syntax error" }, 37.109 - { PARSE_ERR_INVALID_ESCAPE, "invalid escape" }, 37.110 - { 0, NULL } 37.111 -}; 37.112 - 37.113 -/** Number of entries in the message catalog. */ 37.114 -const static int catalog_n = sizeof(catalog)/sizeof(ParseError); 37.115 - 37.116 -void ParserState_free(ParserState *z){ 37.117 - if(!z) return; 37.118 - objfree(z->val); 37.119 - deallocate(z); 37.120 -} 37.121 - 37.122 -int ParserState_new(ParserStateFn *fn, ParserState *parent, ParserState **val){ 37.123 - int err = 0; 37.124 - ParserState *z; 37.125 - z = ALLOCATE(ParserState); 37.126 - if(z){ 37.127 - z->fn = fn; 37.128 - z->parent = parent; 37.129 - z->val = ONULL; 37.130 - } else { 37.131 - err = -ENOMEM; 37.132 - } 37.133 - if(!err) *val = z; 37.134 - return err; 37.135 -} 37.136 - 37.137 -/** Free a parser. 37.138 - * No-op if the parser is null. 37.139 - * 37.140 - * @param z parser 37.141 - */ 37.142 -void Parser_free(Parser *z){ 37.143 - if(!z) return; 37.144 - objfree(z->val); 37.145 - z->val = ONONE; 37.146 - deallocate(z); 37.147 -} 37.148 - 37.149 -/** Create a new parser. The error stream defaults to null. 37.150 - */ 37.151 -Parser * Parser_new(void){ 37.152 - Parser *z = ALLOCATE(Parser); 37.153 - int err = -ENOMEM; 37.154 - 37.155 - if(!z) goto exit; 37.156 - err = 0; 37.157 - reset(z); 37.158 - exit: 37.159 - if(err){ 37.160 - Parser_free(z); 37.161 - z = NULL; 37.162 - } 37.163 - return z; 37.164 -} 37.165 - 37.166 -/** Get the next character. 37.167 - * Records the character read in the parser, 37.168 - * and sets the line and character counts. 37.169 - * 37.170 - * @param p parser 37.171 - * @return error flag: 0 on success, non-zero on error 37.172 - */ 37.173 -static int inputchar(Parser *p, char c){ 37.174 - int err = 0; 37.175 - if(c=='\n'){ 37.176 - p->line_no++; 37.177 - p->char_no = 0; 37.178 - } else { 37.179 - p->char_no++; 37.180 - } 37.181 - return err; 37.182 -} 37.183 - 37.184 -static int savechar(Parser *p, char c){ 37.185 - int err = 0; 37.186 - if(p->buf_i >= p->buf_n){ 37.187 - err = -ENOMEM; 37.188 - goto exit; 37.189 - } 37.190 - p->buf[p->buf_i] = c; 37.191 - p->buf_i++; 37.192 - exit: 37.193 - return err; 37.194 -} 37.195 - 37.196 -int Parser_input_char(Parser *p, char c){ 37.197 - int err = 0; 37.198 - if(at_eof(p)){ 37.199 - //skip; 37.200 - } else { 37.201 - inputchar(p, c); 37.202 - } 37.203 - if(!p->state){ 37.204 - err = begin_start(p, c); 37.205 - if(err) goto exit; 37.206 - } 37.207 - err = p->state->fn(p, c); 37.208 - exit: 37.209 - return err; 37.210 -} 37.211 - 37.212 -int Parser_input_eof(Parser *p){ 37.213 - int err = 0; 37.214 - p->eof = 1; 37.215 - err = Parser_input_char(p, IOSTREAM_EOF); 37.216 - return err; 37.217 -} 37.218 - 37.219 -int Parser_input(Parser *p, char *buf, int buf_n){ 37.220 - int err = 0; 37.221 - int i = 0; 37.222 - if(buf_n <= 0){ 37.223 - err = Parser_input_eof(p); 37.224 - goto exit; 37.225 - } 37.226 - for(i = 0; i<buf_n; i++){ 37.227 - err = Parser_input_char(p, buf[i]); 37.228 - if(err) goto exit; 37.229 - } 37.230 - exit: 37.231 - err = (err < 0 ? err : buf_n); 37.232 - return err; 37.233 -} 37.234 - 37.235 -int Parser_push(Parser *p, ParserStateFn *fn){ 37.236 - int err = 0; 37.237 - err = ParserState_new(fn, p->state, &p->state); 37.238 - return err; 37.239 -} 37.240 - 37.241 -int Parser_pop(Parser *p){ 37.242 - int err = 0; 37.243 - ParserState *s = p->state; 37.244 - p->state = s->parent; 37.245 - ParserState_free(s); 37.246 - return err; 37.247 -} 37.248 - 37.249 -int Parser_return(Parser *p){ 37.250 - int err = 0; 37.251 - Sxpr val = ONONE; 37.252 - if(!p->state){ 37.253 - err = -EINVAL; 37.254 - goto exit; 37.255 - } 37.256 - val = p->state->val; 37.257 - p->state->val = ONONE; 37.258 - err = Parser_pop(p); 37.259 - if(err) goto exit; 37.260 - if(p->state){ 37.261 - err = cons_push(&p->state->val, val); 37.262 - } else { 37.263 - val = nrev(val); 37.264 - p->val = val; 37.265 - } 37.266 - exit: 37.267 - if(err){ 37.268 - objfree(val); 37.269 - } 37.270 - return err; 37.271 -} 37.272 - 37.273 -/** Determine if a character is a separator. 37.274 - * 37.275 - * @param p parser 37.276 - * @param c character to test 37.277 - * @return 1 if a separator, 0 otherwise 37.278 - */ 37.279 -static int is_separator(Parser *p, char c){ 37.280 - return in_sep_class(c); 37.281 -} 37.282 - 37.283 -/** Return the current token. 37.284 - * The return value points at the internal buffer, so 37.285 - * it must not be modified (or freed). Use copy_token() if you need a copy. 37.286 - * 37.287 - * @param p parser 37.288 - * @return token 37.289 - */ 37.290 -char *peek_token(Parser *p){ 37.291 - return p->buf; 37.292 -} 37.293 - 37.294 -/** Return a copy of the current token. 37.295 - * The returned value should be freed when finished with. 37.296 - * 37.297 - * @param p parser 37.298 - * @return copy of token 37.299 - */ 37.300 -char *copy_token(Parser *p){ 37.301 - return strdup(peek_token(p)); 37.302 -} 37.303 - 37.304 -static int do_intern(Parser *p){ 37.305 - int err = 0; 37.306 - Sxpr obj = intern(peek_token(p)); 37.307 - if(NOMEMP(obj)){ 37.308 - err = -ENOMEM; 37.309 - } else { 37.310 - p->state->val = obj; 37.311 - } 37.312 - return err; 37.313 -} 37.314 - 37.315 -static int do_string(Parser *p){ 37.316 - int err = 0; 37.317 - Sxpr obj; 37.318 - obj = string_new(peek_token(p)); 37.319 - if(NOMEMP(obj)){ 37.320 - err = -ENOMEM; 37.321 - } else { 37.322 - p->state->val = obj; 37.323 - } 37.324 - return err; 37.325 -} 37.326 - 37.327 -void newtoken(Parser *p){ 37.328 - memset(p->buf, 0, p->buf_n); 37.329 - p->buf_i = 0; 37.330 - p->tok_begin_line = p->line_no; 37.331 - p->tok_begin_char = p->char_no; 37.332 -} 37.333 - 37.334 -int get_escape(char c, char *d){ 37.335 - int err = 0; 37.336 - switch(c){ 37.337 - case 'a': *d = '\a'; break; 37.338 - case 'b': *d = '\b'; break; 37.339 - case 'f': *d = '\f'; break; 37.340 - case 'n': *d = '\n'; break; 37.341 - case 'r': *d = '\r'; break; 37.342 - case 't': *d = '\t'; break; 37.343 - case 'v': *d = '\v'; break; 37.344 - case c_escape: *d = c_escape; break; 37.345 - case c_single_quote: *d = c_single_quote; break; 37.346 - case c_double_quote: *d = c_double_quote; break; 37.347 - default: 37.348 - err = -EINVAL; 37.349 - } 37.350 - return err; 37.351 -} 37.352 - 37.353 - 37.354 -int begin_start(Parser *p, char c){ 37.355 - return Parser_push(p, state_start); 37.356 -} 37.357 - 37.358 -int state_start(Parser *p, char c){ 37.359 - int err = 0; 37.360 - if(at_eof(p)){ 37.361 - err = end_start(p); 37.362 - } else if(in_space_class(c)){ 37.363 - //skip 37.364 - } else if(in_comment_class(c)){ 37.365 - begin_comment(p, c); 37.366 - } else if(c == c_list_open){ 37.367 - begin_list(p, c); 37.368 - } else if(c == c_list_close){ 37.369 - parse_error(p); 37.370 - err = -EINVAL; 37.371 - } else if(in_string_quote_class(c)){ 37.372 - begin_string(p, c); 37.373 - } else if(in_printable_class(c)){ 37.374 - begin_atom(p, c); 37.375 - } else if(c == 0x04){ 37.376 - //ctrl-D, EOT: end-of-text. 37.377 - Parser_input_eof(p); 37.378 - } else { 37.379 - parse_error(p); 37.380 - err = -EINVAL; 37.381 - } 37.382 - return err; 37.383 -} 37.384 - 37.385 -int end_start(Parser *p){ 37.386 - int err = 0; 37.387 - err = Parser_return(p); 37.388 - return err; 37.389 -} 37.390 - 37.391 -int begin_comment(Parser *p, char c){ 37.392 - int err = 0; 37.393 - err = Parser_push(p, state_comment); 37.394 - if(err) goto exit; 37.395 - err = inputchar(p, c); 37.396 - exit: 37.397 - return err; 37.398 -} 37.399 - 37.400 -int state_comment(Parser *p, char c){ 37.401 - int err = 0; 37.402 - if(c == '\n' || at_eof(p)){ 37.403 - err = end_comment(p); 37.404 - } else { 37.405 - err = inputchar(p, c); 37.406 - } 37.407 - return err; 37.408 -} 37.409 - 37.410 -int end_comment(Parser *p){ 37.411 - return Parser_pop(p); 37.412 -} 37.413 - 37.414 -int begin_string(Parser *p, char c){ 37.415 - int err = 0; 37.416 - err = Parser_push(p, state_string); 37.417 - if(err) goto exit; 37.418 - newtoken(p); 37.419 - p->state->delim = c; 37.420 - exit: 37.421 - return err; 37.422 -} 37.423 - 37.424 -int state_string(Parser *p, char c){ 37.425 - int err = 0; 37.426 - if(at_eof(p)){ 37.427 - parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF); 37.428 - err = -EINVAL; 37.429 - } else if(c == p->state->delim){ 37.430 - err = end_string(p); 37.431 - } else if(c == '\\'){ 37.432 - err = Parser_push(p, state_escape); 37.433 - } else { 37.434 - err = savechar(p, c); 37.435 - } 37.436 - return err; 37.437 -} 37.438 - 37.439 -int end_string(Parser *p){ 37.440 - int err = 0; 37.441 - err = do_string(p); 37.442 - if(err) goto exit; 37.443 - err = Parser_return(p); 37.444 - exit: 37.445 - return err; 37.446 -} 37.447 - 37.448 -int state_escape(Parser *p, char c){ 37.449 - int err = 0; 37.450 - char d; 37.451 - if(at_eof(p)){ 37.452 - parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF); 37.453 - err = -EINVAL; 37.454 - goto exit; 37.455 - } 37.456 - if(get_escape(c, &d) == 0){ 37.457 - err = savechar(p, d); 37.458 - if(err) goto exit; 37.459 - err = Parser_pop(p); 37.460 - } else if(c == 'x'){ 37.461 - p->state->fn = state_hex; 37.462 - p->state->ival = 0; 37.463 - p->state->count = 0; 37.464 - } else { 37.465 - p->state->fn = state_octal; 37.466 - p->state->ival = 0; 37.467 - p->state->count = 0; 37.468 - err = Parser_input_char(p, c); 37.469 - } 37.470 - exit: 37.471 - return err; 37.472 -} 37.473 - 37.474 -int octaldone(Parser *p){ 37.475 - int err = 0; 37.476 - char d = (char)(p->state->ival & 0xff); 37.477 - err = Parser_pop(p); 37.478 - if(err) goto exit; 37.479 - err = Parser_input_char(p, d); 37.480 - exit: 37.481 - return err; 37.482 -} 37.483 - 37.484 -int octaldigit(Parser *p, char c){ 37.485 - int err = 0; 37.486 - p->state->ival *= 8; 37.487 - p->state->ival += c - '0'; 37.488 - p->state->count++; 37.489 - if(err) goto exit; 37.490 - if(p->state->ival < 0 || p->state->ival > 0xff){ 37.491 - parse_error(p); 37.492 - err = -EINVAL; 37.493 - goto exit; 37.494 - } 37.495 - if(p->state->count == 3){ 37.496 - err = octaldone(p); 37.497 - } 37.498 - exit: 37.499 - return err; 37.500 -} 37.501 - 37.502 -int state_octal(Parser *p, char c){ 37.503 - int err = 0; 37.504 - if(at_eof(p)){ 37.505 - parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF); 37.506 - err = -EINVAL; 37.507 - goto exit; 37.508 - } else if('0' <= c && c <= '7'){ 37.509 - err = octaldigit(p, c); 37.510 - } else { 37.511 - err = octaldone(p); 37.512 - if(err) goto exit; 37.513 - Parser_input_char(p, c); 37.514 - } 37.515 - exit: 37.516 - return err; 37.517 -} 37.518 - 37.519 -int hexdone(Parser *p){ 37.520 - int err = 0; 37.521 - char d = (char)(p->state->ival & 0xff); 37.522 - err = Parser_pop(p); 37.523 - if(err) goto exit; 37.524 - err = Parser_input_char(p, d); 37.525 - exit: 37.526 - return err; 37.527 -} 37.528 - 37.529 -int hexdigit(Parser *p, char c, char d){ 37.530 - int err = 0; 37.531 - p->state->ival *= 16; 37.532 - p->state->ival += c - d; 37.533 - p->state->count++; 37.534 - if(err) goto exit; 37.535 - if(p->state->ival < 0 || p->state->ival > 0xff){ 37.536 - parse_error(p); 37.537 - err = -EINVAL; 37.538 - goto exit; 37.539 - } 37.540 - if(p->state->count == 2){ 37.541 - err = hexdone(p); 37.542 - } 37.543 - exit: 37.544 - return err; 37.545 -} 37.546 - 37.547 -int state_hex(Parser *p, char c){ 37.548 - int err = 0; 37.549 - if(at_eof(p)){ 37.550 - parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF); 37.551 - err = -EINVAL; 37.552 - goto exit; 37.553 - } else if('0' <= c && c <= '9'){ 37.554 - err = hexdigit(p, c, '0'); 37.555 - } else if('A' <= c && c <= 'F'){ 37.556 - err = hexdigit(p, c, 'A'); 37.557 - } else if('a' <= c && c <= 'f'){ 37.558 - err = hexdigit(p, c, 'a'); 37.559 - } else if(p->state->count){ 37.560 - err =hexdone(p); 37.561 - if(err) goto exit; 37.562 - Parser_input_char(p, c); 37.563 - } 37.564 - exit: 37.565 - return err; 37.566 -} 37.567 - 37.568 -int begin_atom(Parser *p, char c){ 37.569 - int err = 0; 37.570 - err = Parser_push(p, state_atom); 37.571 - if(err) goto exit; 37.572 - newtoken(p); 37.573 - err = savechar(p, c); 37.574 - exit: 37.575 - return err; 37.576 -} 37.577 - 37.578 -int state_atom(Parser *p, char c){ 37.579 - int err = 0; 37.580 - if(at_eof(p)){ 37.581 - err = end_atom(p); 37.582 - } else if(is_separator(p, c) || 37.583 - in_space_class(c) || 37.584 - in_comment_class(c)){ 37.585 - err = end_atom(p); 37.586 - if(err) goto exit; 37.587 - err = Parser_input_char(p, c); 37.588 - } else { 37.589 - err = savechar(p, c); 37.590 - } 37.591 - exit: 37.592 - return err; 37.593 -} 37.594 - 37.595 -int end_atom(Parser *p){ 37.596 - int err = 0; 37.597 - err = do_intern(p); 37.598 - if(err) goto exit; 37.599 - err = Parser_return(p); 37.600 - exit: 37.601 - return err; 37.602 -} 37.603 - 37.604 -int state_list(Parser *p, char c){ 37.605 - int err = 0; 37.606 - if(at_eof(p)){ 37.607 - parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF); 37.608 - err = -EINVAL; 37.609 - } else if(c == c_list_close){ 37.610 - p->state->val = nrev(p->state->val); 37.611 - err = end_list(p); 37.612 - } else { 37.613 - err = state_start(p, c); 37.614 - } 37.615 - return err; 37.616 - 37.617 -} 37.618 - 37.619 -int begin_list(Parser *p, char c){ 37.620 - return Parser_push(p, state_list); 37.621 -} 37.622 - 37.623 -int end_list(Parser *p){ 37.624 - return Parser_return(p); 37.625 -} 37.626 - 37.627 -/** Reset the fields of a parser to initial values. 37.628 - * 37.629 - * @param z parser 37.630 - */ 37.631 -static void reset(Parser *z){ 37.632 - IOStream *error_out = z->error_out; 37.633 - int flags = z->flags; 37.634 - zero(z, sizeof(Parser)); 37.635 - z->buf_n = sizeof(z->buf) - 1; 37.636 - z->buf_i = 0; 37.637 - z->line_no = 1; 37.638 - z->char_no = 0; 37.639 - z->error_out = error_out; 37.640 - z->flags = flags; 37.641 -} 37.642 - 37.643 -/** Set the parser error stream. 37.644 - * Parse errors are reported on the the error stream if it is non-null. 37.645 - * 37.646 - * @param z parser 37.647 - * @param error_out error stream 37.648 - */ 37.649 -void set_error_stream(Parser *z, IOStream *error_out){ 37.650 - if(z){ 37.651 - z->error_out = error_out; 37.652 - } 37.653 -} 37.654 - 37.655 -/** Get the parser error message for an error code. 37.656 - * 37.657 - * @param id error code 37.658 - * @return error message (empty string if the code is unknown) 37.659 - */ 37.660 -static char *get_message(ParseErrorId id){ 37.661 - int i; 37.662 - for(i=0; i<catalog_n; i++){ 37.663 - if(id == catalog[i].id){ 37.664 - return catalog[i].message; 37.665 - } 37.666 - } 37.667 - return ""; 37.668 -} 37.669 - 37.670 -/** Get the line number. 37.671 - * 37.672 - * @param in parser 37.673 - */ 37.674 -int get_line(Parser *in){ 37.675 - return in->line_no; 37.676 -} 37.677 - 37.678 -/** Get the column number. 37.679 - * 37.680 - * @param in parser 37.681 - */ 37.682 -int get_column(Parser *in){ 37.683 - return in->char_no; 37.684 -} 37.685 - 37.686 -/** Get the line number the current token started on. 37.687 - * 37.688 - * @param in parser 37.689 - */ 37.690 -int get_tok_line(Parser *in){ 37.691 - return in->tok_begin_line; 37.692 -} 37.693 - 37.694 -/** Get the column number the current token started on. 37.695 - * 37.696 - * @param in parser 37.697 - */ 37.698 -int get_tok_column(Parser *in){ 37.699 - return in->tok_begin_char; 37.700 -} 37.701 - 37.702 -/** Report a parse error. 37.703 - * Does nothing if the error stream is null or there is no error. 37.704 - * 37.705 - * @param in parser 37.706 - */ 37.707 -static void report_error(Parser *in){ 37.708 - if(in->error_out && in->err){ 37.709 - char *msg = get_message(in->err); 37.710 - char *tok = peek_token(in); 37.711 - IOStream_print(in->error_out, PARSE_ERR_FMT, 37.712 - get_tok_line(in), get_tok_column(in), msg); 37.713 - if(tok && tok[0]){ 37.714 - IOStream_print(in->error_out, " '%s'", tok); 37.715 - } 37.716 - IOStream_print(in->error_out, "\n"); 37.717 - } 37.718 -} 37.719 - 37.720 -/** Get the error message for the current parse error code. 37.721 - * Does nothing if there is no error. 37.722 - * 37.723 - * @param in parser 37.724 - * @param buf where to place the message 37.725 - * @param n maximum number of characters to place in buf 37.726 - * @return current error code (zero for no error) 37.727 - */ 37.728 -int parse_error_message(Parser *in, char *buf, int n){ 37.729 - if(in->err){ 37.730 - char *msg = get_message(in->err); 37.731 - snprintf(buf, n, PARSE_ERR_FMT, get_tok_line(in), get_tok_column(in), msg); 37.732 - } 37.733 - return in->err; 37.734 -} 37.735 - 37.736 -/** Flag an unspecified parse error. All subsequent reads will fail. 37.737 - * 37.738 - * @param in parser 37.739 - */ 37.740 -void parse_error(Parser *in){ 37.741 - parse_error_id(in, PARSE_ERR_INVALID_SYNTAX); 37.742 -} 37.743 - 37.744 -/** Flag a parse error. All subsequent reads will fail. 37.745 - * Does not change the parser error code if it is already set. 37.746 - * 37.747 - * @param in parser 37.748 - * @param id error code 37.749 - */ 37.750 -void parse_error_id(Parser *in, ParseErrorId id){ 37.751 - if(!in->err){ 37.752 - in->err = id; 37.753 - report_error(in); 37.754 - } 37.755 -} 37.756 - 37.757 -/** Test if the parser's error flag is set. 37.758 - * 37.759 - * @param in parser 37.760 - * @return 1 if set, 0 otherwise 37.761 - */ 37.762 -int has_error(Parser *in){ 37.763 - return (in->err > 0); 37.764 -} 37.765 - 37.766 -/** Test if the parser is at end of input. 37.767 - * 37.768 - * @param in parser 37.769 - * @return 1 if at EOF, 0 otherwise 37.770 - */ 37.771 -int at_eof(Parser *p){ 37.772 - return p->eof; 37.773 -} 37.774 - 37.775 -#ifdef SXPR_PARSER_MAIN 37.776 -/* Stuff for standalone testing. */ 37.777 - 37.778 -#include "file_stream.h" 37.779 -#include "string_stream.h" 37.780 - 37.781 -int stringof(Sxpr exp, char **s){ 37.782 - int err = 0; 37.783 - if(ATOMP(exp)){ 37.784 - *s = atom_name(exp); 37.785 - } else if(STRINGP(exp)){ 37.786 - *s = string_string(exp); 37.787 - } else { 37.788 - err = -EINVAL; 37.789 - *s = NULL; 37.790 - } 37.791 - return err; 37.792 -} 37.793 - 37.794 -int child_string(Sxpr exp, Sxpr key, char **s){ 37.795 - int err = 0; 37.796 - Sxpr val = sxpr_child_value(exp, key, ONONE); 37.797 - err = stringof(val, s); 37.798 - return err; 37.799 -} 37.800 - 37.801 -int intof(Sxpr exp, int *v){ 37.802 - int err = 0; 37.803 - char *s; 37.804 - unsigned long l; 37.805 - if(INTP(exp)){ 37.806 - *v = OBJ_INT(exp); 37.807 - } else { 37.808 - err = stringof(exp, &s); 37.809 - if(err) goto exit; 37.810 - err = convert_atoul(s, &l); 37.811 - *v = (int)l; 37.812 - } 37.813 - exit: 37.814 - return err; 37.815 -} 37.816 - 37.817 -int child_int(Sxpr exp, Sxpr key, int *v){ 37.818 - int err = 0; 37.819 - Sxpr val = sxpr_child_value(exp, key, ONONE); 37.820 - err = intof(val, v); 37.821 - return err; 37.822 -} 37.823 - 37.824 -int eval_vnet(Sxpr exp){ 37.825 - int err = 0; 37.826 - Sxpr oid = intern("id"); 37.827 - int id; 37.828 - err = child_int(exp, oid, &id); 37.829 - if(err) goto exit; 37.830 - dprintf("> vnet id=%d\n", id); 37.831 - exit: 37.832 - dprintf("< err=%d\n", err); 37.833 - return err; 37.834 -} 37.835 - 37.836 -int eval_connect(Sxpr exp){ 37.837 - int err = 0; 37.838 - Sxpr ovif = intern("vif"); 37.839 - Sxpr ovnet = intern("vnet"); 37.840 - char *vif; 37.841 - int vnet; 37.842 - 37.843 - err = child_string(exp, ovif, &vif); 37.844 - if(err) goto exit; 37.845 - err = child_int(exp, ovnet, &vnet); 37.846 - if(err) goto exit; 37.847 - dprintf("> connect vif=%s vnet=%d\n", vif, vnet); 37.848 - exit: 37.849 - dprintf("< err=%d\n", err); 37.850 - return err; 37.851 -} 37.852 - 37.853 -int eval(Sxpr exp){ 37.854 - int err = 0; 37.855 - Sxpr oconnect = intern("connect"); 37.856 - Sxpr ovnet = intern("vnet"); 37.857 - 37.858 - if(sxpr_elementp(exp, ovnet)){ 37.859 - err = eval_vnet(exp); 37.860 - } else if(sxpr_elementp(exp, oconnect)){ 37.861 - err = eval_connect(exp); 37.862 - } else { 37.863 - err = -EINVAL; 37.864 - } 37.865 - return err; 37.866 -} 37.867 - 37.868 -/** Main program for testing. 37.869 - * Parses input and prints it. 37.870 - * 37.871 - * @param argc number of arguments 37.872 - * @param argv arguments 37.873 - * @return error code 37.874 - */ 37.875 -int main(int argc, char *argv[]){ 37.876 - Parser *pin; 37.877 - int err = 0; 37.878 - char buf[1024]; 37.879 - int k; 37.880 - Sxpr obj, l, x; 37.881 - 37.882 - pin = Parser_new(); 37.883 - set_error_stream(pin, iostdout); 37.884 - dprintf("> parse...\n"); 37.885 - while(1){ 37.886 - k = fread(buf, 1, 1024, stdin); 37.887 - err = Parser_input(pin, buf, k); 37.888 - dprintf("> Parser_input=%d\n", err); 37.889 - if(k <= 0) break; 37.890 - } 37.891 - obj = pin->val; 37.892 - for(l = obj ; CONSP(l); l = CDR(l)){ 37.893 - x = CAR(l); 37.894 - objprint(iostdout, x, 0); printf("\n"); 37.895 - eval(x); 37.896 - } 37.897 - dprintf("> err=%d\n", err); 37.898 - return 0; 37.899 -} 37.900 -#endif
38.1 --- a/tools/lib/sxpr_parser.h Tue Jun 29 14:50:09 2004 +0000 38.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 38.3 @@ -1,125 +0,0 @@ 38.4 -/* 38.5 - * 38.6 - * This library is free software; you can redistribute it and/or modify 38.7 - * it under the terms of the GNU Lesser General Public License as 38.8 - * published by the Free Software Foundation; either version 2.1 of the 38.9 - * License, or (at your option) any later version. This library is 38.10 - * distributed in the hope that it will be useful, but WITHOUT ANY 38.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 38.12 - * FITNESS FOR A PARTICULAR PURPOSE. 38.13 - * See the GNU Lesser General Public License for more details. 38.14 - * 38.15 - * You should have received a copy of the GNU Lesser General Public License 38.16 - * along with this library; if not, write to the Free Software Foundation, 38.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 38.18 - */ 38.19 - 38.20 -#ifndef _XEN_LIB_SXPR_PARSER_H_ 38.21 -#define _XEN_LIB_SXPR_PARSER_H_ 38.22 - 38.23 -#include "sxpr.h" 38.24 -#include "iostream.h" 38.25 - 38.26 -/** @file 38.27 - * Sxpr parsing definitions. 38.28 - */ 38.29 - 38.30 -/** Size of a parser input buffer. 38.31 - * Tokens read must fit into this size (including trailing null). 38.32 - */ 38.33 -#define PARSER_BUF_SIZE 1024 38.34 - 38.35 -struct Parser; 38.36 -typedef int ParserStateFn(struct Parser *, char c); 38.37 - 38.38 -typedef struct ParserState { 38.39 - struct ParserState *parent; 38.40 - Sxpr val; 38.41 - int ival; 38.42 - int count; 38.43 - char delim; 38.44 - ParserStateFn *fn; 38.45 -} ParserState; 38.46 - 38.47 -/** Structure representing an input source for the parser. 38.48 - * Can read from any IOStream implementation. 38.49 - */ 38.50 -typedef struct Parser { 38.51 - Sxpr val; 38.52 - /** Error reporting stream (null for no reports). */ 38.53 - IOStream *error_out; 38.54 - int eof; 38.55 - /** Error flag. Non-zero if there has been a read error. */ 38.56 - int err; 38.57 - /** Line number on input (from 1). */ 38.58 - int line_no; 38.59 - /** Column number of input (reset on new line). */ 38.60 - int char_no; 38.61 - /** Lookahead character. */ 38.62 - char c; 38.63 - /** Buffer for reading tokens. */ 38.64 - char buf[PARSER_BUF_SIZE]; 38.65 - /** Size of token buffer. */ 38.66 - int buf_n; 38.67 - int buf_i; 38.68 - /** Line the last token started on. */ 38.69 - int tok_begin_line; 38.70 - /** Character number the last token started on. */ 38.71 - int tok_begin_char; 38.72 - /** Parsing flags. */ 38.73 - int flags; 38.74 - ParserState *state; 38.75 -} Parser; 38.76 - 38.77 -/** Parser error codes. */ 38.78 -typedef enum { 38.79 - PARSE_ERR_NONE=0, 38.80 - PARSE_ERR_UNSPECIFIED, 38.81 - PARSE_ERR_NOMEM, 38.82 - PARSE_ERR_UNEXPECTED_EOF, 38.83 - PARSE_ERR_TOKEN_TOO_LONG, 38.84 - PARSE_ERR_INVALID_SYNTAX, 38.85 - PARSE_ERR_INVALID_ESCAPE, 38.86 -} ParseErrorId; 38.87 - 38.88 - 38.89 -/** Parser flags. */ 38.90 -//enum { 38.91 -//}; 38.92 - 38.93 -/** Raise some parser flags. 38.94 - * 38.95 - * @param in parser 38.96 - * @param flags flags mask 38.97 - */ 38.98 -inline static void parser_flags_raise(Parser *in, int flags){ 38.99 - in->flags |= flags; 38.100 -} 38.101 - 38.102 -/** Lower some parser flags. 38.103 - * 38.104 - * @param in parser 38.105 - * @param flags flags mask 38.106 - */ 38.107 -inline static void parser_flags_lower(Parser *in, int flags){ 38.108 - in->flags &= ~flags; 38.109 -} 38.110 - 38.111 -/** Clear all parser flags. 38.112 - * 38.113 - * @param in parser 38.114 - */ 38.115 -inline static void parser_flags_clear(Parser *in){ 38.116 - in->flags = 0; 38.117 -} 38.118 - 38.119 -extern void Parser_free(Parser *z); 38.120 -extern Parser * Parser_new(void); 38.121 -extern int Parser_input(Parser *p, char *buf, int buf_n); 38.122 -extern int Parser_input_eof(Parser *p); 38.123 - 38.124 -extern int parse_error_message(Parser *in, char *buf, int n); 38.125 -extern int has_error(Parser *in); 38.126 -extern int at_eof(Parser *in); 38.127 - 38.128 -#endif /* ! _XEN_LIB_SXPR_PARSER_H_ */
39.1 --- a/tools/lib/sys_ctype.h Tue Jun 29 14:50:09 2004 +0000 39.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 39.3 @@ -1,12 +0,0 @@ 39.4 -#ifndef _XENO_SYS_CTYPE_H_ 39.5 -#define _XENO_SYS_CTYPE_H_ 39.6 -/** @file 39.7 - ** Replacement for ctype include that can be used 39.8 - * from user or kernel code. 39.9 - */ 39.10 -#ifdef __KERNEL__ 39.11 -# include <linux/ctype.h> 39.12 -#else 39.13 -# include <ctype.h> 39.14 -#endif 39.15 -#endif /* ! _XENO_SYS_CTYPE_H_ */
40.1 --- a/tools/lib/sys_net.c Tue Jun 29 14:50:09 2004 +0000 40.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 40.3 @@ -1,309 +0,0 @@ 40.4 -/* 40.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 40.6 - * 40.7 - * This library is free software; you can redistribute it and/or modify 40.8 - * it under the terms of the GNU Lesser General Public License as 40.9 - * published by the Free Software Foundation; either version 2.1 of the 40.10 - * License, or (at your option) any later version. This library is 40.11 - * distributed in the hope that it will be useful, but WITHOUT ANY 40.12 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or 40.13 - * FITNESS FOR A PARTICULAR PURPOSE. 40.14 - * See the GNU Lesser General Public License for more details. 40.15 - * 40.16 - * You should have received a copy of the GNU Lesser General Public License 40.17 - * along with this library; if not, write to the Free Software Foundation, 40.18 - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 40.19 - */ 40.20 - 40.21 -#include "sys_net.h" 40.22 -#include "sys_string.h" 40.23 - 40.24 -/** @file 40.25 - * All network data are kept in network order and only converted to 40.26 - * host order for display. Network data includes IP addresses, port numbers and 40.27 - * network masks. 40.28 - */ 40.29 - 40.30 -/** Maximum value for a port. */ 40.31 -#define PORT_MAX 0xffff 40.32 - 40.33 -/** Convert a number of bits to a network mask 40.34 - * for IP addresses. The number of bits must 40.35 - * be in the range 1-31. 40.36 - * 40.37 - * @param n number of bits to set in the mask 40.38 - * @return value with n high bits set (in network order) 40.39 - */ 40.40 -unsigned long bits_to_mask(int n){ 40.41 - unsigned long mask = (n ? (1 << 31) : 0); 40.42 - int i; 40.43 - for(i=1; i<n; i++){ 40.44 - mask |= (mask >> 1); 40.45 - } 40.46 - return htonl(mask); 40.47 -} 40.48 - 40.49 -/** Convert a network mask to a number of bits. 40.50 - * 40.51 - * @param mask network mask in network order 40.52 - * @return number of bits in mask 40.53 - */ 40.54 -int mask_to_bits(unsigned long mask){ 40.55 - // Start with n set to the number of bits in the mask. Then reduce n by 40.56 - // the number of low zero bits in the mask. 40.57 - int n = 32; 40.58 - for(mask = ntohl(mask); 40.59 - (mask & 1)==0 && n>0; 40.60 - mask >>= 1){ 40.61 - n--; 40.62 - } 40.63 - return n; 40.64 -} 40.65 - 40.66 -/** Get the index of the first occurrence of a character in a string. 40.67 - * Stops at end of string or after n characters. 40.68 - * 40.69 - * @param s input string 40.70 - * @param n maximum number of charactes to search 40.71 - * @param c character to look for 40.72 - * @return index of first occurrence, -1 if not found 40.73 - */ 40.74 -inline static int indexof(const char *s, int n, char c){ 40.75 - int i; 40.76 - for(i=0; i<n && *s; i++, s++){ 40.77 - if(*s == c) return i; 40.78 - } 40.79 - return -1; 40.80 -} 40.81 - 40.82 -/** Convert an IPv4 address in dot notation into an unsigned long (in network order). 40.83 - * 40.84 - * @param s input string 40.85 - * @param address where to put the address 40.86 - * @return 0 on success, -1 on error 40.87 - */ 40.88 -int get_inet_addr(const char *s, unsigned long *address){ 40.89 - // Number of bits in a byte. 40.90 - const int BYTE_BITS = 8; 40.91 - // Number of bytes in a word. 40.92 - const int WORD_BYTES = 4; 40.93 - // Max value for a component of an address. 40.94 - const int ADDR_MAX = 255; 40.95 - // Separator for components of an address. 40.96 - const char dot = '.'; 40.97 - 40.98 - int n; 40.99 - unsigned long addr = 0; 40.100 - unsigned long v; 40.101 - int i; 40.102 - int err = -1; 40.103 - // Bit shift for the current byte. 40.104 - int shift = BYTE_BITS * (WORD_BYTES - 1); 40.105 - char buf[64]; 40.106 - 40.107 - n = strlen(s); 40.108 - if(n >= sizeof(buf)){ 40.109 - goto exit; 40.110 - } 40.111 - for(i=0; i < WORD_BYTES; i++){ 40.112 - int idx = indexof(s, n, dot); 40.113 - idx = (idx < 0 ? strlen(s) : idx); 40.114 - strncpy(buf, s, idx); buf[idx]='\0'; 40.115 - if(convert_atoul(buf, &v)){ 40.116 - goto exit; 40.117 - } 40.118 - if(v < 0 || v > ADDR_MAX){ 40.119 - goto exit; 40.120 - } 40.121 - addr |= (v << shift); 40.122 - if(idx == n) break; 40.123 - shift -= BYTE_BITS; 40.124 - s += idx+1; 40.125 - } 40.126 - err = 0; 40.127 - exit: 40.128 - addr = htonl(addr); 40.129 - *address = (err ? 0 : addr); 40.130 - return err; 40.131 -} 40.132 - 40.133 -#ifdef __KERNEL__ 40.134 -/** Convert an address in network order to IPv4 dot notation. 40.135 - * The return value is a static buffer which is overwritten on each call. 40.136 - * 40.137 - * @param inaddr address (in network order) 40.138 - * @return address in dot notation 40.139 - */ 40.140 -char *inet_ntoa(struct in_addr inaddr){ 40.141 - static char address[16] = {}; 40.142 - uint32_t addr = ntohl(inaddr.s_addr); 40.143 - snprintf(address, sizeof(address), "%d.%d.%d.%d", 40.144 - (unsigned)((addr >> 24) & 0xff), 40.145 - (unsigned)((addr >> 16) & 0xff), 40.146 - (unsigned)((addr >> 8) & 0xff), 40.147 - (unsigned)((addr ) & 0xff)); 40.148 - return address; 40.149 -} 40.150 - 40.151 - 40.152 -/** Convert a string in IPv4 dot notation to an int in network order. 40.153 - * 40.154 - * @param address address in dot notation 40.155 - * @param inp result of conversion (in network order) 40.156 - * @return 0 on success, error code on error 40.157 - */ 40.158 -int inet_aton(const char *address, struct in_addr *inp){ 40.159 - int err = 0; 40.160 - unsigned long addr; 40.161 - 40.162 - err = get_inet_addr(address, &addr); 40.163 - if(err) goto exit; 40.164 - inp->s_addr = addr; 40.165 - exit: 40.166 - return err; 40.167 -} 40.168 -#endif 40.169 - 40.170 -/** Convert a hostname or IPv4 address string to an address in network order. 40.171 - * 40.172 - * @param name input hostname or address string 40.173 - * @param address where to put the address 40.174 - * @return 1 if address found OK, 0 otherwise 40.175 - */ 40.176 -int get_host_address(const char *name, unsigned long *address){ 40.177 -#ifdef __KERNEL__ 40.178 - return get_inet_addr(name, address) == 0; 40.179 -#else 40.180 - struct hostent *host = gethostbyname(name); 40.181 - if(!host){ 40.182 - return 0; 40.183 - } 40.184 - *address = ((struct in_addr *)(host->h_addr))->s_addr; 40.185 - return 1; 40.186 -#endif 40.187 -} 40.188 - 40.189 -/** Convert a service name to a port (in network order). 40.190 - * 40.191 - * @param name service name 40.192 - * @param port where to put the port 40.193 - * @return 1 if service port found OK, 0 otherwise 40.194 - */ 40.195 -int get_service_port(const char *name, unsigned long *port){ 40.196 -#ifdef __KERNEL__ 40.197 - return 0; 40.198 -#else 40.199 - struct servent *service; 40.200 - service = getservbyname(name, 0); 40.201 - if(!service){ 40.202 - return 0; 40.203 - } 40.204 - *port = service->s_port; 40.205 - return 1; 40.206 -#endif 40.207 -} 40.208 - 40.209 -/** Convert a port number (in network order) to a service name. 40.210 - * 40.211 - * @param port the port number 40.212 - * @return service name if found OK, 0 otherwise 40.213 - */ 40.214 -char *get_port_service(unsigned long port){ 40.215 -#ifdef __KERNEL__ 40.216 - return 0; 40.217 -#else 40.218 - struct servent *service = getservbyport(port, 0); 40.219 - return (service ? service->s_name : 0); 40.220 -#endif 40.221 -} 40.222 - 40.223 -/** Convert a decimal integer or service name to a port (in network order). 40.224 - * 40.225 - * @param s input to convert 40.226 - * @param port where to put the port 40.227 - * @return 1 if port found OK, 0 otherwise 40.228 - */ 40.229 -int convert_service_to_port(const char *s, unsigned long *port){ 40.230 - int ok = 0; 40.231 - unsigned long value; 40.232 - if(convert_atoul(s, &value)){ 40.233 - ok = get_service_port(s, &value); 40.234 - } else { 40.235 - ok = (0 <= value) && (value <= PORT_MAX); 40.236 - value = htons((unsigned short)value); 40.237 - } 40.238 - *port = (ok ? value : 0); 40.239 - return ok; 40.240 -} 40.241 - 40.242 -#define MAC_ELEMENT_N 6 // Number of elements in a MAC address. 40.243 -#define MAC_DIGIT_N 2 // Number of digits in an element in a MAC address. 40.244 -#define MAC_LENGTH 17 //((MAC_ELEMENT_N * MAC_DIGIT_N) + MAC_ELEMENT_N - 1) 40.245 - 40.246 -/** Convert a mac address from a string of the form 40.247 - * XX:XX:XX:XX:XX:XX to numerical form (an array of 6 unsigned chars). 40.248 - * Each X denotes a hex digit: 0..9, a..f, A..F. 40.249 - * Also supports using '-' as the separator instead of ':'. 40.250 - * 40.251 - * @param mac_in string to convert 40.252 - * @param mac destination for the value 40.253 - * @return 0 on success, -1 on error 40.254 - */ 40.255 -int mac_aton(const char *mac_in, unsigned char *mac){ 40.256 - int err = 0; 40.257 - int i, j; 40.258 - const char *p; 40.259 - char sep = 0; 40.260 - unsigned char d; 40.261 - if(!mac_in || strlen(mac_in) != MAC_LENGTH){ 40.262 - err = -1; 40.263 - goto exit; 40.264 - } 40.265 - for(i = 0, p = mac_in; i < MAC_ELEMENT_N; i++){ 40.266 - d = 0; 40.267 - if(i){ 40.268 - if(!sep){ 40.269 - if(*p == ':' || *p == '-') sep = *p; 40.270 - } 40.271 - if(sep && *p == sep){ 40.272 - p++; 40.273 - } else { 40.274 - err = -1; 40.275 - goto exit; 40.276 - } 40.277 - } 40.278 - for(j = 0; j < MAC_DIGIT_N; j++, p++){ 40.279 - if(j) d <<= 4; 40.280 - if(*p >= '0' && *p <= '9'){ 40.281 - d += (*p - '0'); 40.282 - } else if(*p >= 'A' && *p <= 'F'){ 40.283 - d += (*p - 'A') + 10; 40.284 - } else if(*p >= 'a' && *p <= 'f'){ 40.285 - d += (*p - 'a') + 10; 40.286 - } else { 40.287 - err = -1; 40.288 - goto exit; 40.289 - } 40.290 - } 40.291 - mac[i] = d; 40.292 - } 40.293 - exit: 40.294 - return err; 40.295 -} 40.296 - 40.297 -/** Convert a MAC address from numerical form to a string. 40.298 - * 40.299 - * @param mac address to convert 40.300 - * @return static string value 40.301 - */ 40.302 -char *mac_ntoa(const unsigned char *mac){ 40.303 - static char buf[MAC_LENGTH + 1]; 40.304 - int buf_n = sizeof(buf); 40.305 - 40.306 - memset(buf, buf_n, 0); 40.307 - snprintf(buf, buf_n, "%02x:%02x:%02x:%02x:%02x:%02x", 40.308 - mac[0], mac[1], mac[2], 40.309 - mac[3], mac[4], mac[5]); 40.310 - buf[buf_n - 1] = '\0'; 40.311 - return buf; 40.312 -}
41.1 --- a/tools/lib/sys_net.h Tue Jun 29 14:50:09 2004 +0000 41.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 41.3 @@ -1,78 +0,0 @@ 41.4 -/* 41.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 41.6 - * 41.7 - * This library is free software; you can redistribute it and/or modify 41.8 - * it under the terms of the GNU Lesser General Public License as published by 41.9 - * the Free Software Foundation; either version 2.1 of the License, or 41.10 - * (at your option) any later version. 41.11 - * 41.12 - * This library is distributed in the hope that it will be useful, 41.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 41.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 41.15 - * GNU Lesser General Public License for more details. 41.16 - * 41.17 - * You should have received a copy of the GNU Lesser General Public License 41.18 - * along with this library; if not, write to the Free Software 41.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 41.20 - */ 41.21 - 41.22 -#ifndef _XEN_LIB_SYS_NET_H_ 41.23 -#define _XEN_LIB_SYS_NET_H_ 41.24 -/** @file 41.25 - * 41.26 - * Replacement for standard network includes. 41.27 - * Works in user or kernel code. 41.28 - */ 41.29 - 41.30 -extern int get_inet_addr(const char *s, unsigned long *address); 41.31 -extern unsigned long bits_to_mask(int n); 41.32 -extern int mask_to_bits(unsigned long mask); 41.33 -extern int get_host_address(const char *name, unsigned long *address); 41.34 -extern int get_service_port(const char *name, unsigned long *port); 41.35 -extern char *get_port_service(unsigned long port); 41.36 -extern int convert_service_to_port(const char *s, unsigned long *port); 41.37 - 41.38 -#ifdef __KERNEL__ 41.39 -#include <linux/kernel.h> 41.40 -#include <linux/types.h> 41.41 -#include <linux/errno.h> 41.42 -#include <linux/slab.h> 41.43 -#include <asm/byteorder.h> 41.44 - 41.45 -#ifndef htonl 41.46 -#define htonl(x) __constant_htonl(x) 41.47 -#endif 41.48 - 41.49 -#ifndef ntohl 41.50 -#define ntohl(x) __constant_ntohl(x) 41.51 -#endif 41.52 - 41.53 -#ifndef htons 41.54 -#define htons(x) __constant_htons(x) 41.55 -#endif 41.56 - 41.57 -#ifndef ntohs 41.58 -#define ntohs(x) __constant_ntohs(x) 41.59 -#endif 41.60 - 41.61 -#include <linux/in.h> 41.62 -extern char *inet_ntoa(struct in_addr inaddr); 41.63 -extern int inet_aton(const char *address, struct in_addr *inp); 41.64 - 41.65 -#else 41.66 - 41.67 -#include <limits.h> 41.68 -#include <sys/socket.h> 41.69 -#include <netinet/in.h> 41.70 -#include <netdb.h> 41.71 -#include <arpa/inet.h> 41.72 - 41.73 -#endif 41.74 - 41.75 -extern char *mac_ntoa(const unsigned char *macaddr); 41.76 -extern int mac_aton(const char *addr, unsigned char *macaddr); 41.77 - 41.78 -#endif /* !_SP_SYS_NET_H_ */ 41.79 - 41.80 - 41.81 -
42.1 --- a/tools/lib/sys_string.c Tue Jun 29 14:50:09 2004 +0000 42.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 42.3 @@ -1,138 +0,0 @@ 42.4 -/* 42.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 42.6 - * 42.7 - * This library is free software; you can redistribute it and/or modify 42.8 - * it under the terms of the GNU Lesser General Public License as published by 42.9 - * the Free Software Foundation; either version 2.1 of the License, or 42.10 - * (at your option) any later version. 42.11 - * 42.12 - * This library is distributed in the hope that it will be useful, 42.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 42.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 42.15 - * GNU Lesser General Public License for more details. 42.16 - * 42.17 - * You should have received a copy of the GNU Lesser General Public License 42.18 - * along with this library; if not, write to the Free Software 42.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 42.20 - */ 42.21 - 42.22 -#ifdef __KERNEL__ 42.23 -# include <linux/config.h> 42.24 -# include <linux/module.h> 42.25 -# include <linux/kernel.h> 42.26 -# include <linux/errno.h> 42.27 -#else 42.28 -# include <errno.h> 42.29 -#endif 42.30 - 42.31 -#include "allocate.h" 42.32 -#include "sys_string.h" 42.33 - 42.34 -/** Set the base to use for converting a string to a number. Base is 42.35 - * hex if starts with 0x, otherwise decimal. 42.36 - * 42.37 - * @param s input string 42.38 - * @param base where to put the base 42.39 - * @return rest of s to parse as a number 42.40 - */ 42.41 -inline static const char * convert_set_base(const char *s, int *base){ 42.42 - *base = 10; 42.43 - if(s){ 42.44 - if(*s=='0'){ 42.45 - s++; 42.46 - if(*s=='x' || *s=='X'){ 42.47 - *base = 16; 42.48 - s++; 42.49 - } 42.50 - } 42.51 - } 42.52 - return s; 42.53 -} 42.54 - 42.55 -/** Get the numerical value of a digit in the given base. 42.56 - * 42.57 - * @param c digit character 42.58 - * @param base to use 42.59 - * @return numerical value of digit in range 0..base-1 or 42.60 - * -1 if not in range for the base 42.61 - */ 42.62 -inline static int convert_get_digit(char c, int base){ 42.63 - int d; 42.64 - 42.65 - if('0'<=c && c<='9'){ 42.66 - d = c - '0'; 42.67 - } else if('a'<=c && c<='f'){ 42.68 - d = c - 'a' + 10; 42.69 - } else if('A'<=c && c<='F'){ 42.70 - d = c - 'A' + 10; 42.71 - } else { 42.72 - d = -1; 42.73 - } 42.74 - return (d < base ? d : -1); 42.75 -} 42.76 - 42.77 -/** Convert a string to an unsigned long by parsing it as a number. 42.78 - * Will accept hex or decimal in usual C syntax. 42.79 - * 42.80 - * @param str input string 42.81 - * @param val where to put the result 42.82 - * @return 0 if converted OK, negative otherwise 42.83 - */ 42.84 -int convert_atoul(const char *str, unsigned long *val){ 42.85 - int err = 0; 42.86 - unsigned long v = 0; 42.87 - int base; 42.88 - const char *s = str; 42.89 - 42.90 - if(!s) { 42.91 - err = -EINVAL; 42.92 - goto exit; 42.93 - } 42.94 - s = convert_set_base(s, &base); 42.95 - for( ; !err && *s; s++){ 42.96 - int digit = convert_get_digit(*s, base); 42.97 - if(digit<0){ 42.98 - err = -EINVAL; 42.99 - goto exit; 42.100 - } 42.101 - v *= base; 42.102 - v += digit; 42.103 - } 42.104 - exit: 42.105 - *val = (err ? 0 : v); 42.106 - return err; 42.107 -} 42.108 - 42.109 -/** Combine a directory path with a relative path to produce 42.110 - * a new path. 42.111 - * 42.112 - * @param s directory path 42.113 - * @param t relative path 42.114 - * @return new combined path s/t 42.115 - */ 42.116 -int path_concat(char *s, char *t, char **val){ 42.117 - int err = 0; 42.118 - int sn, tn, vn; 42.119 - char *v; 42.120 - sn = strlen(s); 42.121 - if(sn > 0 && s[sn-1] == '/'){ 42.122 - sn--; 42.123 - } 42.124 - tn = strlen(t); 42.125 - if(tn > 0 && t[0] == '/'){ 42.126 - tn--; 42.127 - } 42.128 - vn = sn+tn+1; 42.129 - v = (char*)allocate(vn+1); 42.130 - if(!v){ 42.131 - err = -ENOMEM; 42.132 - goto exit; 42.133 - } 42.134 - strncpy(v, s, sn); 42.135 - v[sn] = '/'; 42.136 - strncpy(v+sn+1, t, tn); 42.137 - v[vn] = '\0'; 42.138 - exit: 42.139 - *val = (err ? NULL : v); 42.140 - return err; 42.141 -}
43.1 --- a/tools/lib/sys_string.h Tue Jun 29 14:50:09 2004 +0000 43.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 43.3 @@ -1,91 +0,0 @@ 43.4 -/* 43.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> 43.6 - * 43.7 - * This library is free software; you can redistribute it and/or modify 43.8 - * it under the terms of the GNU Lesser General Public License as published by 43.9 - * the Free Software Foundation; either version 2.1 of the License, or 43.10 - * (at your option) any later version. 43.11 - * 43.12 - * This library is distributed in the hope that it will be useful, 43.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 43.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 43.15 - * GNU Lesser General Public License for more details. 43.16 - * 43.17 - * You should have received a copy of the GNU Lesser General Public License 43.18 - * along with this library; if not, write to the Free Software 43.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 43.20 - */ 43.21 - 43.22 -#ifndef _XEN_LIB_SYS_STRING_H_ 43.23 -#define _XEN_LIB_SYS_STRING_H_ 43.24 -/** @file 43.25 - * Replacement for standard string includes. 43.26 - * Works in user or kernel code. 43.27 - */ 43.28 -/*============================================================================*/ 43.29 -#ifdef __KERNEL__ 43.30 - 43.31 -#include <linux/config.h> 43.32 -#include <linux/kernel.h> 43.33 -#include <linux/string.h> 43.34 -#include <linux/types.h> 43.35 -#include <stdarg.h> 43.36 -#include "allocate.h" 43.37 - 43.38 -#if 0 43.39 -static inline int tolower(int c){ 43.40 - return (c>='A' && c<='Z' ? (c-'A')+'a' : c); 43.41 -} 43.42 -#endif 43.43 - 43.44 -static inline int isalpha(int c){ 43.45 - return (c>='A' && c<='Z') || (c>='a' && c<='z'); 43.46 -} 43.47 - 43.48 -static inline int isdigit(int c){ 43.49 - return (c>='0' && c<='9'); 43.50 -} 43.51 - 43.52 -#if 0 43.53 -static inline int strcasecmp(const char *s1, const char *s2){ 43.54 - int c1, c2; 43.55 - 43.56 - do { 43.57 - c1 = tolower(*s1++); 43.58 - c2 = tolower(*s2++); 43.59 - } while (c1 && c1 == c2); 43.60 - return c1 - c2; 43.61 -} 43.62 -#endif 43.63 - 43.64 -static inline char * strdup(const char *s){ 43.65 - int n = (s ? 1+strlen(s) : 0); 43.66 - char *copy = (n ? allocate(n) : NULL); 43.67 - if(copy){ 43.68 - strcpy(copy, s); 43.69 - } 43.70 - return copy; 43.71 -} 43.72 - 43.73 -/*============================================================================*/ 43.74 -#else 43.75 -#include <string.h> 43.76 -#include <stdio.h> 43.77 - 43.78 -#ifndef _GNU_SOURCE 43.79 -static inline size_t strnlen(const char *s, size_t n){ 43.80 - int k = 0; 43.81 - if(s){ 43.82 - for(k=0; *s && k<n; s++, k++){} 43.83 - } 43.84 - return k; 43.85 -} 43.86 -#endif 43.87 - 43.88 -#endif 43.89 -/*============================================================================*/ 43.90 - 43.91 -extern int convert_atoul(const char *s, unsigned long *v); 43.92 -extern int path_concat(char *s, char *t, char **val); 43.93 - 43.94 -#endif /* !_XEN_LIB_SYS_STRING_H_ */
44.1 --- a/tools/lib/xdr.c Tue Jun 29 14:50:09 2004 +0000 44.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 44.3 @@ -1,246 +0,0 @@ 44.4 -/* $Id: xdr.c,v 1.3 2003/09/29 13:40:00 mjw Exp $ */ 44.5 -#include "xdr.h" 44.6 -#include <errno.h> 44.7 -/** @file 44.8 - * XDR packer/unpacker for elements. 44.9 - * 44.10 - * string -> [T_STRING] [len:u32] <len bytes> 44.11 - * atom -> [T_ATOM] [len:u32] <len bytes> 44.12 - * uint -> [T_UINT] [value] 44.13 - * cons -> [T_CONS] <car> <cdr> 44.14 - * null -> [T_NULL] 44.15 - * none -> [T_NONE] 44.16 - * bool -> [T_BOOL] { 0:u8 | 1:u8 } 44.17 - * 44.18 - * types packed as u16. 44.19 - * 44.20 - * So (a b c) -> [T_CONS] a [T_CONS] b [T_CONS] c [T_NULL] 44.21 - * () -> [T_NULL] 44.22 - */ 44.23 - 44.24 -int pack_bool(IOStream *io, int x){ 44.25 - int err=0; 44.26 - err = IOStream_print(io, "%c", 0xff & x); 44.27 - if(err > 0) err = 0; 44.28 - return err; 44.29 -} 44.30 - 44.31 -int unpack_bool(IOStream *io, int *x){ 44.32 - int err = 0; 44.33 - int c; 44.34 - c = IOStream_getc(io); 44.35 - *x = (c < 0 ? 0 : c); 44.36 - err = IOStream_error(io); 44.37 - if(c < 0 && !err) err = -EIO; 44.38 - return err; 44.39 -} 44.40 - 44.41 -int pack_ushort(IOStream *io, unsigned short x){ 44.42 - int err=0; 44.43 - err = IOStream_print(io, "%c%c", 44.44 - 0xff & (x >> 8), 44.45 - 0xff & (x )); 44.46 - if(err > 0) err = 0; 44.47 - return err; 44.48 -} 44.49 - 44.50 -int unpack_ushort(IOStream *io, unsigned short *x){ 44.51 - int err = 0; 44.52 - int i, c = 0; 44.53 - *x = 0; 44.54 - for(i = 0; i< 2; i++){ 44.55 - c = IOStream_getc(io); 44.56 - if(c < 0) break; 44.57 - *x <<= 8; 44.58 - *x |= (0xff & c); 44.59 - } 44.60 - err = IOStream_error(io); 44.61 - if(c < 0 && !err) err = -EIO; 44.62 - return err; 44.63 -} 44.64 - 44.65 -int pack_uint(IOStream *io, unsigned int x){ 44.66 - int err=0; 44.67 - err = IOStream_print(io, "%c%c%c%c", 44.68 - 0xff & (x >> 24), 44.69 - 0xff & (x >> 16), 44.70 - 0xff & (x >> 8), 44.71 - 0xff & (x )); 44.72 - if(err > 0) err = 0; 44.73 - return err; 44.74 -} 44.75 - 44.76 -int unpack_uint(IOStream *io, unsigned int *x){ 44.77 - int err = 0; 44.78 - int i, c = 0; 44.79 - *x = 0; 44.80 - for(i = 0; i< 4; i++){ 44.81 - c = IOStream_getc(io); 44.82 - if(c < 0) break; 44.83 - *x <<= 8; 44.84 - *x |= (0xff & c); 44.85 - } 44.86 - err = IOStream_error(io); 44.87 - if(c < 0 && !err) err = -EIO; 44.88 - return err; 44.89 -} 44.90 - 44.91 -int pack_string(IOStream *io, Sxpr x){ 44.92 - int err = 0; 44.93 - int n = string_length(x); 44.94 - char *s = string_string(x); 44.95 - int i; 44.96 - err = pack_uint(io, n); 44.97 - if(err) goto exit; 44.98 - for(i = 0; i < n; i++){ 44.99 - err = IOStream_print(io, "%c", s[i]); 44.100 - if(err < 0) break; 44.101 - } 44.102 - if(err > 0) err = 0; 44.103 - exit: 44.104 - return err; 44.105 -} 44.106 - 44.107 -int unpack_string(IOStream *io, Sxpr *x){ 44.108 - int err; 44.109 - unsigned int n; 44.110 - int i, c = 0; 44.111 - char *s; 44.112 - Sxpr val = ONONE; 44.113 - 44.114 - err = unpack_uint(io, &n); 44.115 - if(err) goto exit; 44.116 - val = halloc(n+1, T_STRING); 44.117 - if(NOMEMP(val)){ 44.118 - err = -ENOMEM; 44.119 - goto exit; 44.120 - } 44.121 - s = string_string(val); 44.122 - for(i=0; i<n; i++){ 44.123 - c = IOStream_getc(io); 44.124 - if(c < 0) break; 44.125 - s[i] = (char)c; 44.126 - } 44.127 - s[n] = '\0'; 44.128 - exit: 44.129 - err = IOStream_error(io); 44.130 - if(c < 0 && !err) err = -EIO; 44.131 - if(err){ 44.132 - objfree(val); 44.133 - val = ONONE; 44.134 - } 44.135 - *x = val; 44.136 - return err; 44.137 -} 44.138 - 44.139 -int pack_cons(IOStream *io, Sxpr x){ 44.140 - int err = 0; 44.141 - err = pack_sxpr(io, CAR(x)); 44.142 - if(err) goto exit; 44.143 - err = pack_sxpr(io, CDR(x)); 44.144 - exit: 44.145 - return err; 44.146 -} 44.147 - 44.148 -int unpack_cons(IOStream *io, Sxpr *x){ 44.149 - int err = 0; 44.150 - Sxpr u = ONONE, v = ONONE, val = ONONE; 44.151 - err = unpack_sxpr(io, &u); 44.152 - if(err) goto exit; 44.153 - err = unpack_sxpr(io, &v); 44.154 - if(err) goto exit; 44.155 - val = cons_new(u, v); 44.156 - if(NOMEMP(val)){ 44.157 - err = -ENOMEM; 44.158 - } 44.159 - exit: 44.160 - if(err){ 44.161 - objfree(u); 44.162 - objfree(v); 44.163 - val = ONONE; 44.164 - } 44.165 - *x = val; 44.166 - return err; 44.167 -} 44.168 - 44.169 -int pack_sxpr(IOStream *io, Sxpr x){ 44.170 - int err = 0; 44.171 - unsigned short type = get_type(x); 44.172 - err = pack_ushort(io, type); 44.173 - if(err) goto exit; 44.174 - switch(type){ 44.175 - case T_NULL: 44.176 - break; 44.177 - case T_NONE: 44.178 - break; 44.179 - break; 44.180 - case T_BOOL: 44.181 - err = pack_bool(io, get_ul(x)); 44.182 - break; 44.183 - case T_CONS: 44.184 - err = pack_cons(io, x); 44.185 - break; 44.186 - case T_ATOM: 44.187 - err = pack_string(io, OBJ_ATOM(x)->name); 44.188 - break; 44.189 - case T_STRING: 44.190 - err = pack_string(io, x); 44.191 - break; 44.192 - case T_UINT: 44.193 - err = pack_uint(io, get_ul(x)); 44.194 - break; 44.195 - default: 44.196 - err = -EINVAL; 44.197 - IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type); 44.198 - break; 44.199 - } 44.200 - exit: 44.201 - return err; 44.202 -} 44.203 - 44.204 -int unpack_sxpr(IOStream *io, Sxpr *x){ 44.205 - int err = 0; 44.206 - unsigned short type; 44.207 - unsigned int u; 44.208 - Sxpr val = ONONE, y; 44.209 - 44.210 - err = unpack_ushort(io, &type); 44.211 - if(err) goto exit; 44.212 - switch(type){ 44.213 - case T_NULL: 44.214 - val = ONULL; 44.215 - break; 44.216 - case T_NONE: 44.217 - val = ONONE; 44.218 - break; 44.219 - case T_CONS: 44.220 - err = unpack_cons(io, &val); 44.221 - break; 44.222 - case T_BOOL: 44.223 - err = unpack_bool(io, &u); 44.224 - if(err) goto exit; 44.225 - val = (u ? OTRUE : OFALSE); 44.226 - break; 44.227 - case T_ATOM: 44.228 - err = unpack_string(io, &y); 44.229 - if(err) goto exit; 44.230 - val = intern(string_string(y)); 44.231 - objfree(y); 44.232 - break; 44.233 - case T_STRING: 44.234 - err = unpack_string(io, &val); 44.235 - break; 44.236 - case T_UINT: 44.237 - err = unpack_uint(io, &u); 44.238 - if(err) goto exit; 44.239 - val = OBJI(type, u); 44.240 - break; 44.241 - default: 44.242 - err = -EINVAL; 44.243 - IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type); 44.244 - break; 44.245 - } 44.246 - exit: 44.247 - *x = (err ? ONONE : val); 44.248 - return err; 44.249 -}
45.1 --- a/tools/lib/xdr.h Tue Jun 29 14:50:09 2004 +0000 45.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 45.3 @@ -1,14 +0,0 @@ 45.4 -/* $Id: xdr.h,v 1.2 2003/09/29 13:40:00 mjw Exp $ */ 45.5 -#ifndef _SP_XDR_H_ 45.6 -#define _SP_XDR_H_ 45.7 -#include "iostream.h" 45.8 -#include "sxpr.h" 45.9 -int pack_uint(IOStream *out, unsigned int x); 45.10 -int unpack_uint(IOStream *in, unsigned int *x); 45.11 -int pack_string(IOStream *out, Sxpr x); 45.12 -int unpack_string(IOStream *in, Sxpr *x); 45.13 -int pack_cons(IOStream *out, Sxpr x); 45.14 -int unpack_cons(IOStream *in, Sxpr *x); 45.15 -int pack_sxpr(IOStream *out, Sxpr x); 45.16 -int unpack_sxpr(IOStream *in, Sxpr *x); 45.17 -#endif /* _SP_XDR_H_ */
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 46.2 +++ b/tools/libxc/Makefile Tue Jun 29 18:10:39 2004 +0000 46.3 @@ -0,0 +1,115 @@ 46.4 + 46.5 +MAJOR = 1.3 46.6 +MINOR = 0 46.7 +SONAME = libxc.so.$(MAJOR) 46.8 + 46.9 +CC = gcc 46.10 + 46.11 +XEN_ROOT = ../.. 46.12 +include $(XEN_ROOT)/tools/Make.defs 46.13 + 46.14 +vpath %.h $(XEN_HYPERVISOR_IFS) 46.15 +INCLUDES += -I $(XEN_HYPERVISOR_IFS) 46.16 + 46.17 +vpath %h $(XEN_LINUX_INCLUDE) 46.18 +INCLUDES += -I $(XEN_LINUX_INCLUDE) 46.19 + 46.20 +vpath %.h $(XEN_XU) 46.21 +INCLUDES += -I $(XEN_XU) 46.22 + 46.23 +vpath %c $(XEN_LIBXUTIL) 46.24 +INCLUDES += -I $(XEN_LIBXUTIL) 46.25 + 46.26 +LIB_SRCS := 46.27 +LIB_SRCS += allocate.c 46.28 +#LIB_SRCS += enum.c 46.29 +LIB_SRCS += file_stream.c 46.30 +LIB_SRCS += gzip_stream.c 46.31 +#LIB_SRCS += hash_table.c 46.32 +LIB_SRCS += iostream.c 46.33 +#LIB_SRCS += kernel_stream.c 46.34 +#LIB_SRCS += lexis.c 46.35 +#LIB_SRCS += lzi_stream.c 46.36 +#LIB_SRCS += lzo_stream.c 46.37 +#LIB_SRCS += marshal.c 46.38 +#LIB_SRCS += socket_stream.c 46.39 +#LIB_SRCS += string_stream.c 46.40 +#LIB_SRCS += sxpr.c 46.41 +#LIB_SRCS += sxpr_parser.c 46.42 +LIB_SRCS += sys_net.c 46.43 +LIB_SRCS += sys_string.c 46.44 +#LIB_SRCS += xdr.c 46.45 + 46.46 +SRCS := 46.47 +SRCS += xc_atropos.c 46.48 +SRCS += xc_bvtsched.c 46.49 +SRCS += xc_domain.c 46.50 +SRCS += xc_evtchn.c 46.51 +SRCS += xc_io.c 46.52 +SRCS += xc_linux_build.c 46.53 +SRCS += xc_linux_restore.c 46.54 +SRCS += xc_linux_save.c 46.55 +SRCS += xc_misc.c 46.56 +SRCS += xc_netbsd_build.c 46.57 +SRCS += xc_physdev.c 46.58 +SRCS += xc_private.c 46.59 +SRCS += xc_rrobin.c 46.60 + 46.61 +#SRCS += $(LIB_SRCS) 46.62 + 46.63 +CFLAGS += -Wall 46.64 +CFLAGS += -Werror 46.65 +CFLAGS += -g 46.66 +CFLAGS += -O3 46.67 +CFLAGS += -fno-strict-aliasing 46.68 +CFLAGS += $(INCLUDES) 46.69 +# Get gcc to generate the dependencies for us. 46.70 +CFLAGS += -Wp,-MD,.$(@F).d 46.71 +DEPS = .*.d 46.72 + 46.73 +OBJS = $(patsubst %.c,%.o,$(SRCS)) 46.74 + 46.75 +LIB = libxc.so libxc.so.$(MAJOR) libxc.so.$(MAJOR).$(MINOR) 46.76 + 46.77 +all: check-for-zlib $(LIB) 46.78 + 46.79 +check-for-zlib: 46.80 + @if [ ! -e /usr/include/zlib.h ]; then \ 46.81 + echo "***********************************************************"; \ 46.82 + echo "ERROR: install zlib header files (http://www.gzip.org/zlib)"; \ 46.83 + echo "***********************************************************"; \ 46.84 + false; \ 46.85 + fi 46.86 + 46.87 +install: all 46.88 + mkdir -p $(prefix)/usr/lib 46.89 + mkdir -p $(prefix)/usr/include 46.90 + install -m0755 $(LIB) $(prefix)/usr/lib 46.91 + install -m0644 xc.h $(prefix)/usr/include 46.92 + 46.93 +clean: 46.94 + $(RM) *.a *.so *.o *.rpm $(LIB) 46.95 + $(RM) *~ 46.96 + $(RM) $(DEPS) 46.97 + 46.98 +rpm: all 46.99 + rm -rf staging 46.100 + mkdir staging 46.101 + mkdir staging/i386 46.102 + rpmbuild --define "staging$$PWD/staging" --define '_builddir.' \ 46.103 + --define "_rpmdir$$PWD/staging" -bb rpm.spec 46.104 + mv staging/i386/*.rpm . 46.105 + rm -rf staging 46.106 + 46.107 +libxc.so: 46.108 + ln -sf libxc.so.$(MAJOR) $@ 46.109 +libxc.so.$(MAJOR): 46.110 + ln -sf libxc.so.$(MAJOR).$(MINOR) $@ 46.111 +libxc.so.$(MAJOR).$(MINOR): $(OBJS) 46.112 + $(CC) -Wl,-soname -Wl,$(SONAME) -shared -o $@ $^ ../libxutil/libxutil.a -lz 46.113 + 46.114 +%.o: %.c Makefile 46.115 + 46.116 +# $(CC) $(CFLAGS) -o $@ $< 46.117 + 46.118 +-include $(DEPS)
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 47.2 +++ b/tools/libxc/rpm.spec Tue Jun 29 18:10:39 2004 +0000 47.3 @@ -0,0 +1,28 @@ 47.4 +Summary: Xen control interface library 47.5 +Name: xen-internal-library 47.6 +Version: 1.2 47.7 +Release: 1 47.8 +License: Xen 47.9 +Group: Xen 47.10 +BuildRoot: %{staging} 47.11 +%description 47.12 +Library to make it easier to access the Xen control interfaces. 47.13 + 47.14 +%pre 47.15 +%preun 47.16 +%install 47.17 +install -m 0755 -d $RPM_BUILD_ROOT/lib 47.18 +install -m 0755 libxc.a $RPM_BUILD_ROOT/lib/libxc.a 47.19 +install -m 0755 libxc.so $RPM_BUILD_ROOT/lib/libxc.so 47.20 +install -m 0755 -d $RPM_BUILD_ROOT/include 47.21 +install -m 0644 xc.h $RPM_BUILD_ROOT/include/xc.h 47.22 +%clean 47.23 +%post 47.24 +%postun 47.25 +%files 47.26 +%defattr(-,root,root) 47.27 +%dir /lib 47.28 +/lib/libxc.a 47.29 +/lib/libxc.so 47.30 +%dir /include 47.31 +/include/xc.h
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 48.2 +++ b/tools/libxc/xc.h Tue Jun 29 18:10:39 2004 +0000 48.3 @@ -0,0 +1,203 @@ 48.4 +/****************************************************************************** 48.5 + * xc.h 48.6 + * 48.7 + * A library for low-level access to the Xen control interfaces. 48.8 + * 48.9 + * Copyright (c) 2003, K A Fraser. 48.10 + */ 48.11 + 48.12 +#ifndef __XC_H__ 48.13 +#define __XC_H__ 48.14 + 48.15 +typedef unsigned char u8; 48.16 +typedef unsigned short u16; 48.17 +typedef unsigned long u32; 48.18 +typedef unsigned long long u64; 48.19 +typedef signed char s8; 48.20 +typedef signed short s16; 48.21 +typedef signed long s32; 48.22 +typedef signed long long s64; 48.23 + 48.24 +/* Obtain or relinquish a handle on the 'xc' library. */ 48.25 +int xc_interface_open(void); 48.26 +int xc_interface_close(int xc_handle); 48.27 + 48.28 +typedef struct { 48.29 + u32 domid; 48.30 + unsigned int cpu; 48.31 + unsigned int dying:1, crashed:1, shutdown:1, 48.32 + paused:1, blocked:1, running:1; 48.33 + unsigned int shutdown_reason; /* only meaningful if shutdown==1 */ 48.34 + unsigned long nr_pages; 48.35 + unsigned long shared_info_frame; 48.36 + u64 cpu_time; 48.37 +#define XC_DOMINFO_MAXNAME 16 48.38 + char name[XC_DOMINFO_MAXNAME]; 48.39 + unsigned long max_memkb; 48.40 +} xc_dominfo_t; 48.41 + 48.42 +typedef struct xc_shadow_control_stats_st 48.43 +{ 48.44 + unsigned long fault_count; 48.45 + unsigned long dirty_count; 48.46 + unsigned long dirty_net_count; 48.47 + unsigned long dirty_block_count; 48.48 +} xc_shadow_control_stats_t; 48.49 + 48.50 +int xc_domain_create(int xc_handle, 48.51 + unsigned int mem_kb, 48.52 + const char *name, 48.53 + int cpu, 48.54 + u32 *pdomid); 48.55 +int xc_domain_pause(int xc_handle, 48.56 + u32 domid); 48.57 +int xc_domain_unpause(int xc_handle, 48.58 + u32 domid); 48.59 +int xc_domain_destroy(int xc_handle, 48.60 + u32 domid); 48.61 +int xc_domain_pincpu(int xc_handle, 48.62 + u32 domid, 48.63 + int cpu); 48.64 +int xc_domain_getinfo(int xc_handle, 48.65 + u32 first_domid, 48.66 + unsigned int max_doms, 48.67 + xc_dominfo_t *info); 48.68 + 48.69 +int xc_shadow_control(int xc_handle, 48.70 + u32 domid, 48.71 + unsigned int sop, 48.72 + unsigned long *dirty_bitmap, 48.73 + unsigned long pages, 48.74 + xc_shadow_control_stats_t *stats); 48.75 + 48.76 + 48.77 +#define XCFLAGS_VERBOSE 1 48.78 +#define XCFLAGS_LIVE 2 48.79 +#define XCFLAGS_DEBUG 4 48.80 + 48.81 +struct XcIOContext; 48.82 +int xc_linux_save(int xc_handle, struct XcIOContext *ioctxt); 48.83 +int xc_linux_restore(int xc_handle, struct XcIOContext *ioctxt); 48.84 + 48.85 +int xc_linux_build(int xc_handle, 48.86 + u32 domid, 48.87 + const char *image_name, 48.88 + const char *ramdisk_name, 48.89 + const char *cmdline, 48.90 + unsigned int control_evtchn, 48.91 + unsigned long flags); 48.92 + 48.93 +int xc_netbsd_build(int xc_handle, 48.94 + u32 domid, 48.95 + const char *image_name, 48.96 + const char *cmdline, 48.97 + unsigned int control_evtchn); 48.98 + 48.99 +int xc_bvtsched_global_set(int xc_handle, 48.100 + unsigned long ctx_allow); 48.101 + 48.102 +int xc_bvtsched_domain_set(int xc_handle, 48.103 + u32 domid, 48.104 + unsigned long mcuadv, 48.105 + unsigned long warp, 48.106 + unsigned long warpl, 48.107 + unsigned long warpu); 48.108 + 48.109 +int xc_bvtsched_global_get(int xc_handle, 48.110 + unsigned long *ctx_allow); 48.111 + 48.112 +int xc_bvtsched_domain_get(int xc_handle, 48.113 + u32 domid, 48.114 + unsigned long *mcuadv, 48.115 + unsigned long *warp, 48.116 + unsigned long *warpl, 48.117 + unsigned long *warpu); 48.118 + 48.119 +int xc_atropos_domain_set(int xc_handle, 48.120 + u32 domid, 48.121 + u64 period, u64 slice, u64 latency, 48.122 + int xtratime); 48.123 + 48.124 +int xc_atropos_domain_get(int xc_handle, 48.125 + u32 domid, 48.126 + u64* period, u64 *slice, u64 *latency, 48.127 + int *xtratime); 48.128 + 48.129 +int xc_rrobin_global_set(int xc_handle, u64 slice); 48.130 + 48.131 +int xc_rrobin_global_get(int xc_handle, u64 *slice); 48.132 + 48.133 +#define DOMID_SELF (0x7FFFFFFEU) 48.134 + 48.135 +typedef struct { 48.136 +#define EVTCHNSTAT_closed 0 /* Chennel is not in use. */ 48.137 +#define EVTCHNSTAT_unbound 1 /* Channel is not bound to a source. */ 48.138 +#define EVTCHNSTAT_interdomain 2 /* Channel is connected to remote domain. */ 48.139 +#define EVTCHNSTAT_pirq 3 /* Channel is bound to a phys IRQ line. */ 48.140 +#define EVTCHNSTAT_virq 4 /* Channel is bound to a virtual IRQ line */ 48.141 + int status; 48.142 + union { 48.143 + struct { 48.144 + u32 dom; 48.145 + int port; 48.146 + } interdomain; 48.147 + int pirq; 48.148 + int virq; 48.149 + } u; 48.150 +} xc_evtchn_status_t; 48.151 + 48.152 +int xc_evtchn_bind_interdomain(int xc_handle, 48.153 + u32 dom1, /* may be DOMID_SELF */ 48.154 + u32 dom2, /* may be DOMID_SELF */ 48.155 + int *port1, 48.156 + int *port2); 48.157 +int xc_evtchn_bind_virq(int xc_handle, 48.158 + int virq, 48.159 + int *port); 48.160 +int xc_evtchn_close(int xc_handle, 48.161 + u32 dom, /* may be DOMID_SELF */ 48.162 + int port); 48.163 +int xc_evtchn_send(int xc_handle, 48.164 + int local_port); 48.165 +int xc_evtchn_status(int xc_handle, 48.166 + u32 dom, /* may be DOMID_SELF */ 48.167 + int port, 48.168 + xc_evtchn_status_t *status); 48.169 + 48.170 +int xc_physdev_pci_access_modify(int xc_handle, 48.171 + u32 domid, 48.172 + int bus, 48.173 + int dev, 48.174 + int func, 48.175 + int enable); 48.176 + 48.177 +int xc_readconsolering(int xc_handle, 48.178 + char *str, 48.179 + unsigned int max_chars, 48.180 + int clear); 48.181 + 48.182 +typedef struct { 48.183 + int ht_per_core; 48.184 + int cores; 48.185 + unsigned long total_pages; 48.186 + unsigned long free_pages; 48.187 + unsigned long cpu_khz; 48.188 +} xc_physinfo_t; 48.189 + 48.190 +int xc_physinfo(int xc_handle, 48.191 + xc_physinfo_t *info); 48.192 + 48.193 +int xc_domain_setname(int xc_handle, 48.194 + u32 domid, 48.195 + char *name); 48.196 + 48.197 +int xc_domain_setinitialmem(int xc_handle, 48.198 + u32 domid, 48.199 + unsigned int initial_memkb); 48.200 + 48.201 +int xc_domain_setmaxmem(int xc_handle, 48.202 + u32 domid, 48.203 + unsigned int max_memkb); 48.204 + 48.205 + 48.206 +#endif /* __XC_H__ */
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 49.2 +++ b/tools/libxc/xc_atropos.c Tue Jun 29 18:10:39 2004 +0000 49.3 @@ -0,0 +1,51 @@ 49.4 +/****************************************************************************** 49.5 + * xc_atropos.c 49.6 + * 49.7 + * API for manipulating parameters of the Atropos scheduler. 49.8 + * 49.9 + * by Mark Williamson, Copyright (c) 2004 Intel Research Cambridge. 49.10 + */ 49.11 + 49.12 +#include "xc_private.h" 49.13 + 49.14 +int xc_atropos_domain_set(int xc_handle, 49.15 + u32 domid, u64 period, u64 slice, u64 latency, 49.16 + int xtratime) 49.17 +{ 49.18 + dom0_op_t op; 49.19 + struct atropos_adjdom *p = &op.u.adjustdom.u.atropos; 49.20 + 49.21 + op.cmd = DOM0_ADJUSTDOM; 49.22 + op.u.adjustdom.domain = (domid_t)domid; 49.23 + op.u.adjustdom.sched_id = SCHED_ATROPOS; 49.24 + op.u.adjustdom.direction = SCHED_INFO_PUT; 49.25 + 49.26 + p->nat_period = period; 49.27 + p->nat_slice = slice; 49.28 + p->latency = latency; 49.29 + p->xtratime = xtratime; 49.30 + 49.31 + return do_dom0_op(xc_handle, &op); 49.32 +} 49.33 + 49.34 +int xc_atropos_domain_get(int xc_handle, u32 domid, u64 *period, 49.35 + u64 *slice, u64 *latency, int *xtratime) 49.36 +{ 49.37 + dom0_op_t op; 49.38 + int ret; 49.39 + struct atropos_adjdom *p = &op.u.adjustdom.u.atropos; 49.40 + 49.41 + op.cmd = DOM0_ADJUSTDOM; 49.42 + op.u.adjustdom.domain = (domid_t)domid; 49.43 + op.u.adjustdom.sched_id = SCHED_ATROPOS; 49.44 + op.u.adjustdom.direction = SCHED_INFO_GET; 49.45 + 49.46 + ret = do_dom0_op(xc_handle, &op); 49.47 + 49.48 + *period = p->nat_period; 49.49 + *slice = p->nat_slice; 49.50 + *latency = p->latency; 49.51 + *xtratime = p->xtratime; 49.52 + 49.53 + return ret; 49.54 +}
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 50.2 +++ b/tools/libxc/xc_bvtsched.c Tue Jun 29 18:10:39 2004 +0000 50.3 @@ -0,0 +1,88 @@ 50.4 +/****************************************************************************** 50.5 + * xc_bvtsched.c 50.6 + * 50.7 + * API for manipulating parameters of the Borrowed Virtual Time scheduler. 50.8 + * 50.9 + * Copyright (c) 2003, K A Fraser. 50.10 + */ 50.11 + 50.12 +#include "xc_private.h" 50.13 + 50.14 +int xc_bvtsched_global_set(int xc_handle, 50.15 + unsigned long ctx_allow) 50.16 +{ 50.17 + dom0_op_t op; 50.18 + 50.19 + op.cmd = DOM0_SCHEDCTL; 50.20 + op.u.schedctl.sched_id = SCHED_BVT; 50.21 + op.u.schedctl.direction = SCHED_INFO_PUT; 50.22 + op.u.schedctl.u.bvt.ctx_allow = ctx_allow; 50.23 + 50.24 + return do_dom0_op(xc_handle, &op); 50.25 +} 50.26 + 50.27 +int xc_bvtsched_global_get(int xc_handle, 50.28 + unsigned long *ctx_allow) 50.29 +{ 50.30 + dom0_op_t op; 50.31 + int ret; 50.32 + 50.33 + op.cmd = DOM0_SCHEDCTL; 50.34 + op.u.schedctl.sched_id = SCHED_BVT; 50.35 + op.u.schedctl.direction = SCHED_INFO_GET; 50.36 + 50.37 + ret = do_dom0_op(xc_handle, &op); 50.38 + 50.39 + *ctx_allow = op.u.schedctl.u.bvt.ctx_allow; 50.40 + 50.41 + return ret; 50.42 +} 50.43 + 50.44 +int xc_bvtsched_domain_set(int xc_handle, 50.45 + u32 domid, 50.46 + unsigned long mcuadv, 50.47 + unsigned long warp, 50.48 + unsigned long warpl, 50.49 + unsigned long warpu) 50.50 +{ 50.51 + dom0_op_t op; 50.52 + struct bvt_adjdom *bvtadj = &op.u.adjustdom.u.bvt; 50.53 + 50.54 + op.cmd = DOM0_ADJUSTDOM; 50.55 + op.u.adjustdom.domain = (domid_t)domid; 50.56 + op.u.adjustdom.sched_id = SCHED_BVT; 50.57 + op.u.adjustdom.direction = SCHED_INFO_PUT; 50.58 + 50.59 + bvtadj->mcu_adv = mcuadv; 50.60 + bvtadj->warp = warp; 50.61 + bvtadj->warpl = warpl; 50.62 + bvtadj->warpu = warpu; 50.63 + return do_dom0_op(xc_handle, &op); 50.64 +} 50.65 + 50.66 + 50.67 +int xc_bvtsched_domain_get(int xc_handle, 50.68 + u32 domid, 50.69 + unsigned long *mcuadv, 50.70 + unsigned long *warp, 50.71 + unsigned long *warpl, 50.72 + unsigned long *warpu) 50.73 +{ 50.74 + 50.75 + dom0_op_t op; 50.76 + int ret; 50.77 + struct bvt_adjdom *adjptr = &op.u.adjustdom.u.bvt; 50.78 + 50.79 + op.cmd = DOM0_ADJUSTDOM; 50.80 + op.u.adjustdom.domain = (domid_t)domid; 50.81 + op.u.adjustdom.sched_id = SCHED_BVT; 50.82 + op.u.adjustdom.direction = SCHED_INFO_GET; 50.83 + 50.84 + ret = do_dom0_op(xc_handle, &op); 50.85 + 50.86 + *mcuadv = adjptr->mcu_adv; 50.87 + *warp = adjptr->warp; 50.88 + *warpl = adjptr->warpl; 50.89 + *warpu = adjptr->warpu; 50.90 + return ret; 50.91 +}
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 51.2 +++ b/tools/libxc/xc_domain.c Tue Jun 29 18:10:39 2004 +0000 51.3 @@ -0,0 +1,194 @@ 51.4 +/****************************************************************************** 51.5 + * xc_domain.c 51.6 + * 51.7 + * API for manipulating and obtaining information on domains. 51.8 + * 51.9 + * Copyright (c) 2003, K A Fraser. 51.10 + */ 51.11 + 51.12 +#include "xc_private.h" 51.13 + 51.14 +int xc_domain_create(int xc_handle, 51.15 + unsigned int mem_kb, 51.16 + const char *name, 51.17 + int cpu, 51.18 + u32 *pdomid) 51.19 +{ 51.20 + int err; 51.21 + dom0_op_t op; 51.22 + 51.23 + op.cmd = DOM0_CREATEDOMAIN; 51.24 + op.u.createdomain.memory_kb = mem_kb; 51.25 + strncpy(op.u.createdomain.name, name, MAX_DOMAIN_NAME); 51.26 + op.u.createdomain.name[MAX_DOMAIN_NAME-1] = '\0'; 51.27 + op.u.createdomain.cpu = cpu; 51.28 + 51.29 + if ( (err = do_dom0_op(xc_handle, &op)) == 0 ) 51.30 + *pdomid = (u32)op.u.createdomain.domain; 51.31 + 51.32 + return err; 51.33 +} 51.34 + 51.35 + 51.36 +int xc_domain_pause(int xc_handle, 51.37 + u32 domid) 51.38 +{ 51.39 + dom0_op_t op; 51.40 + op.cmd = DOM0_PAUSEDOMAIN; 51.41 + op.u.pausedomain.domain = (domid_t)domid; 51.42 + return do_dom0_op(xc_handle, &op); 51.43 +} 51.44 + 51.45 + 51.46 +int xc_domain_unpause(int xc_handle, 51.47 + u32 domid) 51.48 +{ 51.49 + dom0_op_t op; 51.50 + op.cmd = DOM0_UNPAUSEDOMAIN; 51.51 + op.u.unpausedomain.domain = (domid_t)domid; 51.52 + return do_dom0_op(xc_handle, &op); 51.53 +} 51.54 + 51.55 + 51.56 +int xc_domain_destroy(int xc_handle, 51.57 + u32 domid) 51.58 +{ 51.59 + dom0_op_t op; 51.60 + op.cmd = DOM0_DESTROYDOMAIN; 51.61 + op.u.destroydomain.domain = (domid_t)domid; 51.62 + return do_dom0_op(xc_handle, &op); 51.63 +} 51.64 + 51.65 +int xc_domain_pincpu(int xc_handle, 51.66 + u32 domid, 51.67 + int cpu) 51.68 +{ 51.69 + dom0_op_t op; 51.70 + op.cmd = DOM0_PINCPUDOMAIN; 51.71 + op.u.pincpudomain.domain = (domid_t)domid; 51.72 + op.u.pincpudomain.cpu = cpu; 51.73 + return do_dom0_op(xc_handle, &op); 51.74 +} 51.75 + 51.76 + 51.77 +int xc_domain_getinfo(int xc_handle, 51.78 + u32 first_domid, 51.79 + unsigned int max_doms, 51.80 + xc_dominfo_t *info) 51.81 +{ 51.82 + unsigned int nr_doms; 51.83 + u32 next_domid = first_domid; 51.84 + dom0_op_t op; 51.85 + 51.86 + for ( nr_doms = 0; nr_doms < max_doms; nr_doms++ ) 51.87 + { 51.88 + op.cmd = DOM0_GETDOMAININFO; 51.89 + op.u.getdomaininfo.domain = (domid_t)next_domid; 51.90 + op.u.getdomaininfo.ctxt = NULL; /* no exec context info, thanks. */ 51.91 + if ( do_dom0_op(xc_handle, &op) < 0 ) 51.92 + break; 51.93 + info->domid = (u32)op.u.getdomaininfo.domain; 51.94 + 51.95 + info->cpu = 51.96 + (op.u.getdomaininfo.flags>>DOMFLAGS_CPUSHIFT) & DOMFLAGS_CPUMASK; 51.97 + 51.98 + info->dying = !!(op.u.getdomaininfo.flags & DOMFLAGS_DYING); 51.99 + info->crashed = !!(op.u.getdomaininfo.flags & DOMFLAGS_CRASHED); 51.100 + info->shutdown = !!(op.u.getdomaininfo.flags & DOMFLAGS_SHUTDOWN); 51.101 + info->paused = !!(op.u.getdomaininfo.flags & DOMFLAGS_PAUSED); 51.102 + info->blocked = !!(op.u.getdomaininfo.flags & DOMFLAGS_BLOCKED); 51.103 + info->running = !!(op.u.getdomaininfo.flags & DOMFLAGS_RUNNING); 51.104 + 51.105 + info->shutdown_reason = 51.106 + (op.u.getdomaininfo.flags>>DOMFLAGS_SHUTDOWNSHIFT) & 51.107 + DOMFLAGS_SHUTDOWNMASK; 51.108 + 51.109 + info->nr_pages = op.u.getdomaininfo.tot_pages; 51.110 + info->max_memkb = op.u.getdomaininfo.max_pages<<(PAGE_SHIFT-10); 51.111 + info->shared_info_frame = op.u.getdomaininfo.shared_info_frame; 51.112 + info->cpu_time = op.u.getdomaininfo.cpu_time; 51.113 + strncpy(info->name, op.u.getdomaininfo.name, XC_DOMINFO_MAXNAME); 51.114 + info->name[XC_DOMINFO_MAXNAME-1] = '\0'; 51.115 + 51.116 + next_domid = (u32)op.u.getdomaininfo.domain + 1; 51.117 + info++; 51.118 + } 51.119 + 51.120 + return nr_doms; 51.121 +} 51.122 + 51.123 +int xc_domain_getfullinfo(int xc_handle, 51.124 + u32 domid, 51.125 + dom0_op_t *op, 51.126 + full_execution_context_t *ctxt ) 51.127 +{ 51.128 + int rc; 51.129 + op->cmd = DOM0_GETDOMAININFO; 51.130 + op->u.getdomaininfo.domain = (domid_t)domid; 51.131 + op->u.getdomaininfo.ctxt = ctxt; 51.132 + 51.133 + rc = do_dom0_op(xc_handle, op); 51.134 + if ( ((u32)op->u.getdomaininfo.domain != domid) && rc > 0 ) 51.135 + return -ESRCH; 51.136 + else 51.137 + return rc; 51.138 +} 51.139 + 51.140 + 51.141 +int xc_shadow_control(int xc_handle, 51.142 + u32 domid, 51.143 + unsigned int sop, 51.144 + unsigned long *dirty_bitmap, 51.145 + unsigned long pages, 51.146 + xc_shadow_control_stats_t *stats ) 51.147 +{ 51.148 + int rc; 51.149 + dom0_op_t op; 51.150 + op.cmd = DOM0_SHADOW_CONTROL; 51.151 + op.u.shadow_control.domain = (domid_t)domid; 51.152 + op.u.shadow_control.op = sop; 51.153 + op.u.shadow_control.dirty_bitmap = dirty_bitmap; 51.154 + op.u.shadow_control.pages = pages; 51.155 + 51.156 + rc = do_dom0_op(xc_handle, &op); 51.157 + 51.158 + if ( stats ) 51.159 + memcpy(stats, &op.u.shadow_control.stats, 51.160 + sizeof(xc_shadow_control_stats_t)); 51.161 + 51.162 + return (rc == 0) ? op.u.shadow_control.pages : rc; 51.163 +} 51.164 + 51.165 +int xc_domain_setname(int xc_handle, 51.166 + u32 domid, 51.167 + char *name) 51.168 +{ 51.169 + dom0_op_t op; 51.170 + op.cmd = DOM0_SETDOMAINNAME; 51.171 + op.u.setdomainname.domain = (domid_t)domid; 51.172 + strncpy(op.u.setdomainname.name, name, MAX_DOMAIN_NAME); 51.173 + return do_dom0_op(xc_handle, &op); 51.174 +} 51.175 + 51.176 +int xc_domain_setinitialmem(int xc_handle, 51.177 + u32 domid, 51.178 + unsigned int initial_memkb) 51.179 +{ 51.180 + dom0_op_t op; 51.181 + op.cmd = DOM0_SETDOMAININITIALMEM; 51.182 + op.u.setdomaininitialmem.domain = (domid_t)domid; 51.183 + op.u.setdomaininitialmem.initial_memkb = initial_memkb; 51.184 + return do_dom0_op(xc_handle, &op); 51.185 +} 51.186 + 51.187 +int xc_domain_setmaxmem(int xc_handle, 51.188 + u32 domid, 51.189 + unsigned int max_memkb) 51.190 +{ 51.191 + dom0_op_t op; 51.192 + op.cmd = DOM0_SETDOMAINMAXMEM; 51.193 + op.u.setdomainmaxmem.domain = (domid_t)domid; 51.194 + op.u.setdomainmaxmem.max_memkb = max_memkb; 51.195 + return do_dom0_op(xc_handle, &op); 51.196 +} 51.197 +
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 52.2 +++ b/tools/libxc/xc_elf.h Tue Jun 29 18:10:39 2004 +0000 52.3 @@ -0,0 +1,523 @@ 52.4 +/* 52.5 + * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. 52.6 + * 52.7 + * Redistribution and use in source and binary forms, with or without 52.8 + * modification, are permitted provided that the following conditions 52.9 + * are met: 52.10 + * 1. Redistributions of source code must retain the above copyright 52.11 + * notice, this list of conditions and the following disclaimer. 52.12 + * 2. Redistributions in binary form must reproduce the above copyright 52.13 + * notice, this list of conditions and the following disclaimer in the 52.14 + * documentation and/or other materials provided with the distribution. 52.15 + * 3. The name of the author may not be used to endorse or promote products 52.16 + * derived from this software without specific prior written permission 52.17 + * 52.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 52.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 52.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 52.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52.28 + */ 52.29 + 52.30 +typedef u_int8_t Elf_Byte; 52.31 + 52.32 +typedef u_int32_t Elf32_Addr; /* Unsigned program address */ 52.33 +typedef u_int32_t Elf32_Off; /* Unsigned file offset */ 52.34 +typedef int32_t Elf32_Sword; /* Signed large integer */ 52.35 +typedef u_int32_t Elf32_Word; /* Unsigned large integer */ 52.36 +typedef u_int16_t Elf32_Half; /* Unsigned medium integer */ 52.37 + 52.38 +typedef u_int64_t Elf64_Addr; 52.39 +typedef u_int64_t Elf64_Off; 52.40 +typedef int32_t Elf64_Shalf; 52.41 + 52.42 +typedef int32_t Elf64_Sword; 52.43 +typedef u_int32_t Elf64_Word; 52.44 + 52.45 +typedef int64_t Elf64_Sxword; 52.46 +typedef u_int64_t Elf64_Xword; 52.47 + 52.48 +typedef u_int32_t Elf64_Half; 52.49 +typedef u_int16_t Elf64_Quarter; 52.50 + 52.51 +/* 52.52 + * e_ident[] identification indexes 52.53 + * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html 52.54 + */ 52.55 +#define EI_MAG0 0 /* file ID */ 52.56 +#define EI_MAG1 1 /* file ID */ 52.57 +#define EI_MAG2 2 /* file ID */ 52.58 +#define EI_MAG3 3 /* file ID */ 52.59 +#define EI_CLASS 4 /* file class */ 52.60 +#define EI_DATA 5 /* data encoding */ 52.61 +#define EI_VERSION 6 /* ELF header version */ 52.62 +#define EI_OSABI 7 /* OS/ABI ID */ 52.63 +#define EI_ABIVERSION 8 /* ABI version */ 52.64 +#define EI_PAD 9 /* start of pad bytes */ 52.65 +#define EI_NIDENT 16 /* Size of e_ident[] */ 52.66 + 52.67 +/* e_ident[] magic number */ 52.68 +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ 52.69 +#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ 52.70 +#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ 52.71 +#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ 52.72 +#define ELFMAG "\177ELF" /* magic */ 52.73 +#define SELFMAG 4 /* size of magic */ 52.74 + 52.75 +/* e_ident[] file class */ 52.76 +#define ELFCLASSNONE 0 /* invalid */ 52.77 +#define ELFCLASS32 1 /* 32-bit objs */ 52.78 +#define ELFCLASS64 2 /* 64-bit objs */ 52.79 +#define ELFCLASSNUM 3 /* number of classes */ 52.80 + 52.81 +/* e_ident[] data encoding */ 52.82 +#define ELFDATANONE 0 /* invalid */ 52.83 +#define ELFDATA2LSB 1 /* Little-Endian */ 52.84 +#define ELFDATA2MSB 2 /* Big-Endian */ 52.85 +#define ELFDATANUM 3 /* number of data encode defines */ 52.86 + 52.87 +/* e_ident[] Operating System/ABI */ 52.88 +#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ 52.89 +#define ELFOSABI_HPUX 1 /* HP-UX operating system */ 52.90 +#define ELFOSABI_NETBSD 2 /* NetBSD */ 52.91 +#define ELFOSABI_LINUX 3 /* GNU/Linux */ 52.92 +#define ELFOSABI_HURD 4 /* GNU/Hurd */ 52.93 +#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ 52.94 +#define ELFOSABI_SOLARIS 6 /* Solaris */ 52.95 +#define ELFOSABI_MONTEREY 7 /* Monterey */ 52.96 +#define ELFOSABI_IRIX 8 /* IRIX */ 52.97 +#define ELFOSABI_FREEBSD 9 /* FreeBSD */ 52.98 +#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ 52.99 +#define ELFOSABI_MODESTO 11 /* Novell Modesto */ 52.100 +#define ELFOSABI_OPENBSD 12 /* OpenBSD */ 52.101 +#define ELFOSABI_ARM 97 /* ARM */ 52.102 +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ 52.103 + 52.104 +/* e_ident */ 52.105 +#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ 52.106 + (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ 52.107 + (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ 52.108 + (ehdr).e_ident[EI_MAG3] == ELFMAG3) 52.109 + 52.110 +/* ELF Header */ 52.111 +typedef struct elfhdr { 52.112 + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ 52.113 + Elf32_Half e_type; /* object file type */ 52.114 + Elf32_Half e_machine; /* machine */ 52.115 + Elf32_Word e_version; /* object file version */ 52.116 + Elf32_Addr e_entry; /* virtual entry point */ 52.117 + Elf32_Off e_phoff; /* program header table offset */ 52.118 + Elf32_Off e_shoff; /* section header table offset */ 52.119 + Elf32_Word e_flags; /* processor-specific flags */ 52.120 + Elf32_Half e_ehsize; /* ELF header size */ 52.121 + Elf32_Half e_phentsize; /* program header entry size */ 52.122 + Elf32_Half e_phnum; /* number of program header entries */ 52.123 + Elf32_Half e_shentsize; /* section header entry size */ 52.124 + Elf32_Half e_shnum; /* number of section header entries */ 52.125 + Elf32_Half e_shstrndx; /* section header table's "section 52.126 + header string table" entry offset */ 52.127 +} Elf32_Ehdr; 52.128 + 52.129 +typedef struct { 52.130 + unsigned char e_ident[EI_NIDENT]; /* Id bytes */ 52.131 + Elf64_Quarter e_type; /* file type */ 52.132 + Elf64_Quarter e_machine; /* machine type */ 52.133 + Elf64_Half e_version; /* version number */ 52.134 + Elf64_Addr e_entry; /* entry point */ 52.135 + Elf64_Off e_phoff; /* Program hdr offset */ 52.136 + Elf64_Off e_shoff; /* Section hdr offset */ 52.137 + Elf64_Half e_flags; /* Processor flags */ 52.138 + Elf64_Quarter e_ehsize; /* sizeof ehdr */ 52.139 + Elf64_Quarter e_phentsize; /* Program header entry size */ 52.140 + Elf64_Quarter e_phnum; /* Number of program headers */ 52.141 + Elf64_Quarter e_shentsize; /* Section header entry size */ 52.142 + Elf64_Quarter e_shnum; /* Number of section headers */ 52.143 + Elf64_Quarter e_shstrndx; /* String table index */ 52.144 +} Elf64_Ehdr; 52.145 + 52.146 +/* e_type */ 52.147 +#define ET_NONE 0 /* No file type */ 52.148 +#define ET_REL 1 /* relocatable file */ 52.149 +#define ET_EXEC 2 /* executable file */ 52.150 +#define ET_DYN 3 /* shared object file */ 52.151 +#define ET_CORE 4 /* core file */ 52.152 +#define ET_NUM 5 /* number of types */ 52.153 +#define ET_LOPROC 0xff00 /* reserved range for processor */ 52.154 +#define ET_HIPROC 0xffff /* specific e_type */ 52.155 + 52.156 +/* e_machine */ 52.157 +#define EM_NONE 0 /* No Machine */ 52.158 +#define EM_M32 1 /* AT&T WE 32100 */ 52.159 +#define EM_SPARC 2 /* SPARC */ 52.160 +#define EM_386 3 /* Intel 80386 */ 52.161 +#define EM_68K 4 /* Motorola 68000 */ 52.162 +#define EM_88K 5 /* Motorola 88000 */ 52.163 +#define EM_486 6 /* Intel 80486 - unused? */ 52.164 +#define EM_860 7 /* Intel 80860 */ 52.165 +#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ 52.166 +/* 52.167 + * Don't know if EM_MIPS_RS4_BE, 52.168 + * EM_SPARC64, EM_PARISC, 52.169 + * or EM_PPC are ABI compliant 52.170 + */ 52.171 +#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ 52.172 +#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ 52.173 +#define EM_PARISC 15 /* HPPA */ 52.174 +#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ 52.175 +#define EM_PPC 20 /* PowerPC */ 52.176 +#define EM_ARM 40 /* Advanced RISC Machines ARM */ 52.177 +#define EM_ALPHA 41 /* DEC ALPHA */ 52.178 +#define EM_SPARCV9 43 /* SPARC version 9 */ 52.179 +#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ 52.180 +#define EM_X86_64 62 /* AMD x86-64 architecture */ 52.181 +#define EM_VAX 75 /* DEC VAX */ 52.182 +#define EM_NUM 15 /* number of machine types */ 52.183 + 52.184 +/* Version */ 52.185 +#define EV_NONE 0 /* Invalid */ 52.186 +#define EV_CURRENT 1 /* Current */ 52.187 +#define EV_NUM 2 /* number of versions */ 52.188 + 52.189 +/* Section Header */ 52.190 +typedef struct { 52.191 + Elf32_Word sh_name; /* name - index into section header 52.192 + string table section */ 52.193 + Elf32_Word sh_type; /* type */ 52.194 + Elf32_Word sh_flags; /* flags */ 52.195 + Elf32_Addr sh_addr; /* address */ 52.196 + Elf32_Off sh_offset; /* file offset */ 52.197 + Elf32_Word sh_size; /* section size */ 52.198 + Elf32_Word sh_link; /* section header table index link */ 52.199 + Elf32_Word sh_info; /* extra information */ 52.200 + Elf32_Word sh_addralign; /* address alignment */ 52.201 + Elf32_Word sh_entsize; /* section entry size */ 52.202 +} Elf32_Shdr; 52.203 + 52.204 +typedef struct { 52.205 + Elf64_Half sh_name; /* section name */ 52.206 + Elf64_Half sh_type; /* section type */ 52.207 + Elf64_Xword sh_flags; /* section flags */ 52.208 + Elf64_Addr sh_addr; /* virtual address */ 52.209 + Elf64_Off sh_offset; /* file offset */ 52.210 + Elf64_Xword sh_size; /* section size */ 52.211 + Elf64_Half sh_link; /* link to another */ 52.212 + Elf64_Half sh_info; /* misc info */ 52.213 + Elf64_Xword sh_addralign; /* memory alignment */ 52.214 + Elf64_Xword sh_entsize; /* table entry size */ 52.215 +} Elf64_Shdr; 52.216 + 52.217 +/* Special Section Indexes */ 52.218 +#define SHN_UNDEF 0 /* undefined */ 52.219 +#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ 52.220 +#define SHN_LOPROC 0xff00 /* reserved range for processor */ 52.221 +#define SHN_HIPROC 0xff1f /* specific section indexes */ 52.222 +#define SHN_ABS 0xfff1 /* absolute value */ 52.223 +#define SHN_COMMON 0xfff2 /* common symbol */ 52.224 +#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ 52.225 + 52.226 +/* sh_type */ 52.227 +#define SHT_NULL 0 /* inactive */ 52.228 +#define SHT_PROGBITS 1 /* program defined information */ 52.229 +#define SHT_SYMTAB 2 /* symbol table section */ 52.230 +#define SHT_STRTAB 3 /* string table section */ 52.231 +#define SHT_RELA 4 /* relocation section with addends*/ 52.232 +#define SHT_HASH 5 /* symbol hash table section */ 52.233 +#define SHT_DYNAMIC 6 /* dynamic section */ 52.234 +#define SHT_NOTE 7 /* note section */ 52.235 +#define SHT_NOBITS 8 /* no space section */ 52.236 +#define SHT_REL 9 /* relation section without addends */ 52.237 +#define SHT_SHLIB 10 /* reserved - purpose unknown */ 52.238 +#define SHT_DYNSYM 11 /* dynamic symbol table section */ 52.239 +#define SHT_NUM 12 /* number of section types */ 52.240 +#define SHT_LOPROC 0x70000000 /* reserved range for processor */ 52.241 +#define SHT_HIPROC 0x7fffffff /* specific section header types */ 52.242 +#define SHT_LOUSER 0x80000000 /* reserved range for application */ 52.243 +#define SHT_HIUSER 0xffffffff /* specific indexes */ 52.244 + 52.245 +/* Section names */ 52.246 +#define ELF_BSS ".bss" /* uninitialized data */ 52.247 +#define ELF_DATA ".data" /* initialized data */ 52.248 +#define ELF_DEBUG ".debug" /* debug */ 52.249 +#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ 52.250 +#define ELF_DYNSTR ".dynstr" /* dynamic string table */ 52.251 +#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ 52.252 +#define ELF_FINI ".fini" /* termination code */ 52.253 +#define ELF_GOT ".got" /* global offset table */ 52.254 +#define ELF_HASH ".hash" /* symbol hash table */ 52.255 +#define ELF_INIT ".init" /* initialization code */ 52.256 +#define ELF_REL_DATA ".rel.data" /* relocation data */ 52.257 +#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ 52.258 +#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ 52.259 +#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ 52.260 +#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ 52.261 +#define ELF_REL_TEXT ".rel.text" /* relocation code */ 52.262 +#define ELF_RODATA ".rodata" /* read-only data */ 52.263 +#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ 52.264 +#define ELF_STRTAB ".strtab" /* string table */ 52.265 +#define ELF_SYMTAB ".symtab" /* symbol table */ 52.266 +#define ELF_TEXT ".text" /* code */ 52.267 + 52.268 + 52.269 +/* Section Attribute Flags - sh_flags */ 52.270 +#define SHF_WRITE 0x1 /* Writable */ 52.271 +#define SHF_ALLOC 0x2 /* occupies memory */ 52.272 +#define SHF_EXECINSTR 0x4 /* executable */ 52.273 +#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ 52.274 + /* specific section attributes */ 52.275 + 52.276 +/* Symbol Table Entry */ 52.277 +typedef struct elf32_sym { 52.278 + Elf32_Word st_name; /* name - index into string table */ 52.279 + Elf32_Addr st_value; /* symbol value */ 52.280 + Elf32_Word st_size; /* symbol size */ 52.281 + unsigned char st_info; /* type and binding */ 52.282 + unsigned char st_other; /* 0 - no defined meaning */ 52.283 + Elf32_Half st_shndx; /* section header index */ 52.284 +} Elf32_Sym; 52.285 + 52.286 +typedef struct { 52.287 + Elf64_Half st_name; /* Symbol name index in str table */ 52.288 + Elf_Byte st_info; /* type / binding attrs */ 52.289 + Elf_Byte st_other; /* unused */ 52.290 + Elf64_Quarter st_shndx; /* section index of symbol */ 52.291 + Elf64_Xword st_value; /* value of symbol */ 52.292 + Elf64_Xword st_size; /* size of symbol */ 52.293 +} Elf64_Sym; 52.294 + 52.295 +/* Symbol table index */ 52.296 +#define STN_UNDEF 0 /* undefined */ 52.297 + 52.298 +/* Extract symbol info - st_info */ 52.299 +#define ELF32_ST_BIND(x) ((x) >> 4) 52.300 +#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) 52.301 +#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 52.302 + 52.303 +#define ELF64_ST_BIND(x) ((x) >> 4) 52.304 +#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) 52.305 +#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 52.306 + 52.307 +/* Symbol Binding - ELF32_ST_BIND - st_info */ 52.308 +#define STB_LOCAL 0 /* Local symbol */ 52.309 +#define STB_GLOBAL 1 /* Global symbol */ 52.310 +#define STB_WEAK 2 /* like global - lower precedence */ 52.311 +#define STB_NUM 3 /* number of symbol bindings */ 52.312 +#define STB_LOPROC 13 /* reserved range for processor */ 52.313 +#define STB_HIPROC 15 /* specific symbol bindings */ 52.314 + 52.315 +/* Symbol type - ELF32_ST_TYPE - st_info */ 52.316 +#define STT_NOTYPE 0 /* not specified */ 52.317 +#define STT_OBJECT 1 /* data object */ 52.318 +#define STT_FUNC 2 /* functio