pax_global_header00006660000000000000000000000064122136536460014522gustar00rootroot0000000000000052 comment=cf65935708217bffa1ffa0e81fa4a3e9b1ee2faf perl-ExtUtils-H2PM-0.09/000075500000000000000000000000001221365364600146755ustar00rootroot00000000000000perl-ExtUtils-H2PM-0.09/Build.PL000064400000000000000000000007501221365364600161730ustar00rootroot00000000000000use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'ExtUtils::H2PM', requires => { 'Exporter' => '5.57', 'ExtUtils::CBuilder' => 0, }, build_requires => { 'Test::More' => '0.88', # done_testing }, auto_configure_requires => 0, # Don't add M::B to configure_requires license => 'perl', create_makefile_pl => 'traditional', create_license => 1, create_readme => 1, ); $build->create_build_script; perl-ExtUtils-H2PM-0.09/Changes000064400000000000000000000036571221365364600162030ustar00rootroot00000000000000Revision history for ExtUtils-H2PM 0.09 CHANGES: * Allow member elements to be constants, which don't consume an input value or return one BUGFIXES: * Specify 'signed char' in test.h to be specific to compilers (RT87583) 0.08 CHANGES: * Unpack functions now print the actual as well as expected length on failures * Updated FSF address in LICENSE file 0.07 CHANGES: * Allow constants and structures to be guarded by #ifdef preprocessor BUGFIXES: * Avoid C99's %zu and %td printf(3) formats 0.06 BUGFIXES: * Accept member names containing '.' * gen_output() before opening the output file in case it fails * Ensure warning-free handling of __unpack_u64 when given empty string, in case of no_length_check 0.05 CHANGES: * Emulate pack/unpack 'Q' on 32bit perls to allow use of 64bit fields BUGFIXES: * Don't declare Test::Output as a dependency since it isn't required 0.04 CHANGES: * Allow member elements to be character arrays * Allow generation of pack/unpack functions that use a hashref for values, rather than a plain list 0.03 CHANGES: * Avoid warning about comparing unsigned int < 0 * Removed evil action-at-a-distance END{} block; now users must call write_output() * Try to use Module::Build's configuration if present - useful for include_dirs, extra_compiler_flags, extra_linker_flags 0.02 CHANGES: * Allow structures to have a data tail; extra bytes at the end * Allow renaming of generated constant functions BUGFIXES: * Cast struct member pointers to char* so ptrdiff_t actually makes sense * Use the correct printf format specifiers for ptrdiff_t and size_t 0.01 First version, released on an unsuspecting world. perl-ExtUtils-H2PM-0.09/LICENSE000064400000000000000000000437601221365364600157140ustar00rootroot00000000000000This software is copyright (c) 2013 by Paul Evans . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2013 by Paul Evans . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2013 by Paul Evans . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End perl-ExtUtils-H2PM-0.09/MANIFEST000064400000000000000000000004771221365364600160360ustar00rootroot00000000000000Build.PL Changes lib/ExtUtils/H2PM.pm LICENSE Makefile.PL MANIFEST This list of files META.json META.yml README t/00use.t t/01constant.t t/02structure-numeric.t t/03structure-strarray.t t/04structure-constant.t t/10structure-with-tail.t t/11structure-args-hashref.t t/12structure-no-length-check.t t/99pod.t t/test.h perl-ExtUtils-H2PM-0.09/META.json000064400000000000000000000017171221365364600163240ustar00rootroot00000000000000{ "abstract" : "automatically generate perl modules to wrap C header files", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4005, CPAN::Meta::Converter version 2.120921", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "ExtUtils-H2PM", "prereqs" : { "build" : { "requires" : { "Test::More" : "0.88" } }, "runtime" : { "requires" : { "Exporter" : "5.57", "ExtUtils::CBuilder" : "0" } } }, "provides" : { "ExtUtils::H2PM" : { "file" : "lib/ExtUtils/H2PM.pm", "version" : "0.09" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.09" } perl-ExtUtils-H2PM-0.09/META.yml000064400000000000000000000011041221365364600161420ustar00rootroot00000000000000--- abstract: 'automatically generate perl modules to wrap C header files' author: - 'Paul Evans ' build_requires: Test::More: 0.88 dynamic_config: 1 generated_by: 'Module::Build version 0.4005, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: ExtUtils-H2PM provides: ExtUtils::H2PM: file: lib/ExtUtils/H2PM.pm version: 0.09 requires: Exporter: 5.57 ExtUtils::CBuilder: 0 resources: license: http://dev.perl.org/licenses/ version: 0.09 perl-ExtUtils-H2PM-0.09/Makefile.PL000064400000000000000000000006571221365364600166570ustar00rootroot00000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.4005 use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'ExtUtils::H2PM', 'VERSION_FROM' => 'lib/ExtUtils/H2PM.pm', 'PREREQ_PM' => { 'Exporter' => '5.57', 'ExtUtils::CBuilder' => 0, 'Test::More' => '0.88' }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ) ; perl-ExtUtils-H2PM-0.09/README000064400000000000000000000272321221365364600155630ustar00rootroot00000000000000NAME `ExtUtils::H2PM' - automatically generate perl modules to wrap C header files DESCRIPTION This module assists in generating wrappers around system functionallity, such as `socket()' types or `ioctl()' calls, where the only interesting features required are the values of some constants or layouts of structures normally only known to the C header files. Rather than writing an entire XS module just to contain some constants and pack/unpack functions, this module allows the author to generate, at module build time, a pure perl module containing constant declarations and structure utility functions. The module then requires no XS module to be loaded at run time. In comparison to h2ph, `C::Scan::Constants', and so on, this module works by generating a small C program containing `printf()' lines to output the values of the constants, compiling it, and running it. This allows it to operate without needing tricky syntax parsing or guessing of the contents of C header files. It can also automatically build pack/unpack functions for simple structure layouts, whose members are all simple integer or character array fields. It is not intended as a full replacement of arbitrary code written in XS modules. If structures should contain pointers, or require special custom handling, then likely an XS module will need to be written. FUNCTIONS module $name Sets the name of the perl module to generate. This will apply a `package' header. include $file Adds a file to the list of headers which will be included by the C program, to obtain the constants or structures from constant $name, %args Adds a numerical constant. The following additional named arguments are also recognised: * name => STRING Use the given name for the generated constant function. If not specified, the C name for the constant will be used. * ifdef => STRING If present, guard the constant with an `#ifdef STRING' preprocessor macro. If the given string is not defined, no constant will be generated. structure $name, %args Adds a structure definition. This requires a named argument, `members'. This should be an ARRAY ref containing an even number of name-definition pairs. The first of each pair should be a member name. The second should be one of the following structure member definitions. The following additional named arguments are also recognised: * pack_func => STRING * unpack_func => STRING Use the given names for the generated pack or unpack functions. * with_tail => BOOL If true, the structure is a header with more data behind it. The pack function takes an optional extra string value for the data tail, and the unpack function will return an extra string value containing it. * no_length_check => BOOL If true, the generated unpack function will not first check the length of its argument before attempting to unpack it. If the buffer is not long enough to unpack all the required values, the remaining ones will not be returned. This may be useful, for example, in cases where various versions of a structure have been designed, later versions adding extra members, but where the exact version found may not be easy to determine beforehand. * arg_style => STRING Defines the style in which the functions take arguments or return values. Defaults to `list', which take or return a list of values in the given order. The other allowed value is `hashref', where the pack function takes a HASH reference and the unpack function returns one. Each will consist of keys named after the structure members. If a data tail is included, it will use the hash key of `_tail'. * ifdef => STRING If present, guard the structure with an `#ifdef STRING' preprocessor macro. If the given string is not defined, no functions will be generated. The following structure member definitions are allowed: * member_numeric The field contains a single signed or unsigned number. Its size and signedness will be automatically detected. * member_strarray The field contains a NULL-padded string of characters. Its size will be automatically detected. * member_constant($code) The field contains a single number as for `member_numeric'. Instead of consuming/returning a value in the arguments list, this member will be packed from an expression, or asserted that it contains the given value. The string `$code' will be inserted into the generated pack and unpack functions, so it can be used for constants generated by the `constant' directive. The structure definition results in two new functions being created, `pack_$name' and `unpack_$name', where `$name' is the name of the structure (with the leading `struct' prefix stripped). These behave similarly to the familiar functions such as `pack_sockaddr_in'; the `pack_' function will take a list of fields and return a packed string, the `unpack_' function will take a string and return a list of fields. no_export, use_export, use_export_ok Controls the export behaviour of the generated symbols. `no_export' creates symbols that are not exported by their package, they must be used fully- qualified. `use_export' creates symbols that are exported by default. `use_export_ok' creates symbols that are exported if they are specifically requested at `use' time. The mode can be changed at any time to affect only the symbols that follow it. It defaults to `use_export_ok'. $perl = gen_output Returns the generated perl code. This is used internally for testing purposes but normally would not be necessary; see instead `write_output'. write_output $filename Write the generated perl code into the named file. This would normally be used as the last function in the containing script, to generate the output file. In the case of `ExtUtils::MakeMaker' or `Module::Build' invoking the script, the path to the file to be generated should be given in `$ARGV[0]'. Normally, therefore, the script would end with write_output $ARGV[0]; EXAMPLES Normally this module would be used by another module at build time, to construct the relevant constants and structure functions from system headers. For example, suppose your operating system defines a new type of socket, which has its own packet and address families, and perhaps some new socket options which are valid on this socket. We can build a module to contain the relevant constants and structure functions by writing, for example: #!/usr/bin/perl use ExtUtils::H2PM; module "Socket::Moonlaser"; include "moon/laser.h"; constant "AF_MOONLASER"; constant "PF_MOONLASER"; constant "SOL_MOONLASER"; constant "MOONLASER_POWER", name => "POWER"; constant "MOONLASER_WAVELENGTH", name => "WAVELENGTH"; structure "struct laserwl", members => [ lwl_nm_coarse => member_numeric, lwl_nm_fine => member_numeric, ]; write_output $ARGV[0]; If we save this script as, say, lib/Socket/Moonlaser.pm.PL, then when the distribution is built, the script will be used to generate the contents of the file lib/Socket/Moonlaser.pm. Once installed, any other code can simply use Socket::Moonlaser qw( AF_MOONLASER ); to import a constant. The method described above doesn't allow us any room to actually include other code in the module. Perhaps, as well as these simple constants, we'd like to include functions, documentation, etc... To allow this, name the script instead something like lib/Socket/Moonlaser_const.pm.PL, so that this is the name used for the generated output. The code can then be included in the actual lib/Socket/Moonlaser.pm (which will just be a normal perl module) by package Socket::Moonlaser; use Socket::Moonlaser_const; sub get_power { getsockopt( $_[0], SOL_MOONLASER, POWER ); } sub set_power { setsockopt( $_[0], SOL_MOONLASER, POWER, $_[1] ); } sub get_wavelength { my $wl = getsockopt( $_[0], SOL_MOONLASER, WAVELENGTH ); defined $wl or return; unpack_laserwl( $wl ); } sub set_wavelength { my $wl = pack_laserwl( $_[1], $_[2] ); setsockopt( $_[0], SOL_MOONLASER, WAVELENGTH, $wl ); } 1; Sometimes, the actual C structure layout may not exactly match the semantics we wish to present to perl modules using this extension wrapper. Socket address structures typically contain their address family as the first member, whereas this detail isn't exposed by, for example, the `sockaddr_in' and `sockaddr_un' functions. To cope with this case, the low-level structure packing and unpacking functions can be generated with a different name, and wrapped in higher-level functions in the main code. For example, in Moonlaser_const.pm.PL: no_export; structure "struct sockaddr_ml", pack_func => "_pack_sockaddr_ml", unpack_func => "_unpack_sockaddr_ml", members => [ ml_family => member_numeric, ml_lat_deg => member_numeric, ml_long_deg => member_numeric, ml_lat_fine => member_numeric, ml_long_fine => member_numeric, ]; This will generate a pack/unpack function pair taking or returning five arguments; these functions will not be exported. In our main Moonlaser.pm file we can wrap these to actually expose a different API: sub pack_sockaddr_ml { @_ == 2 or croak "usage: pack_sockaddr_ml(lat, long)"; my ( $lat, $long ) = @_; return _pack_sockaddr_ml( AF_MOONLASER, int $lat, int $long, ($lat - int $lat) * 1_000_000, ($long - int $long) * 1_000_000); } sub unpack_sockaddr_ml { my ( $family, $lat, $long, $lat_fine, $long_fine ) = _unpack_sockaddr_ml( $_[0] ); $family == AF_MOONLASER or croak "expected family AF_MOONLASER"; return ( $lat + $lat_fine/1_000_000, $long + $long_fine/1_000_000 ); } Sometimes, a structure will contain members which are themselves structures. Suppose a different definition of the above address, which at the C layer is defined as struct angle { short deg; unsigned long fine; }; struct sockaddr_ml { short ml_family; struct angle ml_lat, ml_long; }; We can instead "flatten" this structure tree to obtain the five fields by naming the sub-members of the outer structure: structure "struct sockaddr_ml", members => [ "ml_family" => member_numeric, "ml_lat.deg" => member_numeric, "ml_lat.fine" => member_numeric, "ml_long.deg" => member_numeric, "ml_long.fine" => member_numeric, ]; TODO * Consider more structure members. With strings comes the requirement to have members that store a size. This requires cross-referential members. And while we're at it it might be nice to have constant members; fill in constants without consuming arguments when packing, assert the right value on unpacking. AUTHOR Paul Evans perl-ExtUtils-H2PM-0.09/lib/000075500000000000000000000000001221365364600154435ustar00rootroot00000000000000perl-ExtUtils-H2PM-0.09/lib/ExtUtils/000075500000000000000000000000001221365364600172245ustar00rootroot00000000000000perl-ExtUtils-H2PM-0.09/lib/ExtUtils/H2PM.pm000064400000000000000000000564531221365364600203050ustar00rootroot00000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2013 -- leonerd@leonerd.org.uk package ExtUtils::H2PM; use strict; use warnings; use Carp; our $VERSION = '0.09'; use Exporter 'import'; our @EXPORT = qw( module include constant structure member_numeric member_strarray member_constant no_export use_export use_export_ok gen_output write_output ); use ExtUtils::CBuilder; =head1 NAME C - automatically generate perl modules to wrap C header files =head1 DESCRIPTION This module assists in generating wrappers around system functionallity, such as C types or C calls, where the only interesting features required are the values of some constants or layouts of structures normally only known to the C header files. Rather than writing an entire XS module just to contain some constants and pack/unpack functions, this module allows the author to generate, at module build time, a pure perl module containing constant declarations and structure utility functions. The module then requires no XS module to be loaded at run time. In comparison to F, C, and so on, this module works by generating a small C program containing C lines to output the values of the constants, compiling it, and running it. This allows it to operate without needing tricky syntax parsing or guessing of the contents of C header files. It can also automatically build pack/unpack functions for simple structure layouts, whose members are all simple integer or character array fields. It is not intended as a full replacement of arbitrary code written in XS modules. If structures should contain pointers, or require special custom handling, then likely an XS module will need to be written. =cut my $output = ""; my @preamble; my @fragments; my $done_carp; my @perlcode; my @genblocks; my $export_mode; use_export_ok(); my @exports; my @exports_ok; =head1 FUNCTIONS =cut sub push_export { my $name = shift; if( $export_mode eq "OK" ) { push @exports_ok, $name; } elsif( $export_mode ) { push @exports, $name; } } =head2 module $name Sets the name of the perl module to generate. This will apply a C header. =cut my $modulename; sub module { $modulename = shift; $output .= gen_perl() if @fragments; $output .= "package $modulename;\n" . "# This module was generated automatically by ExtUtils::H2PM from $0\n" . "\n"; undef $done_carp; } =head2 include $file Adds a file to the list of headers which will be included by the C program, to obtain the constants or structures from =cut sub include { my ( $file, %params ) = @_; # undocumented but useful for testing if( $params{local} ) { push @preamble, qq[#include "$file"]; } else { push @preamble, "#include <$file>"; } } # undocumented so far sub perlcode { my ( $code ) = @_; push @perlcode, $code; } =head2 constant $name, %args Adds a numerical constant. The following additional named arguments are also recognised: =over 8 =item * name => STRING Use the given name for the generated constant function. If not specified, the C name for the constant will be used. =item * ifdef => STRING If present, guard the constant with an C<#ifdef STRING> preprocessor macro. If the given string is not defined, no constant will be generated. =back =cut sub constant { my $constname = shift; my %args = @_; my $name = $args{name} || $constname; push @fragments, qq{ printf("$constname=%ld\\n", (long)$constname);}; if( my $symbol = $args{ifdef} ) { $fragments[-1] = "#ifdef $symbol\n$fragments[-1]\n#endif"; } push @genblocks, [ $constname => sub { my ( $result ) = @_; return () unless defined $result; push_export $name; "use constant $name => $result;"; } ]; } =head2 structure $name, %args Adds a structure definition. This requires a named argument, C. This should be an ARRAY ref containing an even number of name-definition pairs. The first of each pair should be a member name. The second should be one of the following structure member definitions. The following additional named arguments are also recognised: =over 8 =item * pack_func => STRING =item * unpack_func => STRING Use the given names for the generated pack or unpack functions. =item * with_tail => BOOL If true, the structure is a header with more data behind it. The pack function takes an optional extra string value for the data tail, and the unpack function will return an extra string value containing it. =item * no_length_check => BOOL If true, the generated unpack function will not first check the length of its argument before attempting to unpack it. If the buffer is not long enough to unpack all the required values, the remaining ones will not be returned. This may be useful, for example, in cases where various versions of a structure have been designed, later versions adding extra members, but where the exact version found may not be easy to determine beforehand. =item * arg_style => STRING Defines the style in which the functions take arguments or return values. Defaults to C, which take or return a list of values in the given order. The other allowed value is C, where the pack function takes a HASH reference and the unpack function returns one. Each will consist of keys named after the structure members. If a data tail is included, it will use the hash key of C<_tail>. =item * ifdef => STRING If present, guard the structure with an C<#ifdef STRING> preprocessor macro. If the given string is not defined, no functions will be generated. =back =cut sub structure { my ( $name, %params ) = @_; ( my $basename = $name ) =~ s/^struct //; my $packfunc = $params{pack_func} || "pack_$basename"; my $unpackfunc = $params{unpack_func} || "unpack_$basename"; my $with_tail = $params{with_tail}; my $no_length_check = $params{no_length_check}; my $arg_style = $params{arg_style} || "list"; my @membernames; my @argnames; my @memberhandlers; my $argindex = 0; my @members = @{ $params{members} }; for( my $i = 0; $i < @members; $i+=2 ) { my ( $memname, $handler ) = @members[$i,$i+1]; push @membernames, $memname; push @memberhandlers, $handler; $handler->{set_names}->( $basename, $memname ); my $wasindex = $argindex; $handler->{set_arg}( $argindex ); push @argnames, $memname if $argindex > $wasindex; } push @fragments, "#ifdef $params{ifdef}" if $params{ifdef}; push @fragments, " {", " $name $basename;", qq[ printf("$basename=%lu,", (unsigned long)sizeof($basename));], ( map { " " . $_->{gen_c}->() } @memberhandlers ), qq[ printf("\\n");], " }"; push @fragments, "#endif" if $params{ifdef}; push @genblocks, [ $basename => sub { my ( $result ) = @_; return () unless defined $result; my @result = split m/,/, $result; my $curpos = 0; my $format = ""; my $sizeof = shift @result; my ( @postargs, @preret ); foreach my $def ( @result ) { my $handler = shift @memberhandlers; $format .= $handler->{gen_format}( $def, $curpos, \@postargs, \@preret ) . " "; } if( $curpos < $sizeof ) { $format .= "x" . ( $sizeof - $curpos ); } my $eq = "=="; if( $with_tail ) { $format .= "a*"; $eq = ">="; } unshift( @perlcode, "use Carp;" ), $done_carp++ unless $done_carp; my ( @argcode, @retcode ); if( $arg_style eq "list" ) { my $members = join( ", ", @argnames, ( $with_tail ? "[tail]" : () ) ); @argcode = ( qq{ \@_ $eq $argindex or croak "usage: $packfunc($members)";}, qq{ my \@v = \@_;} ); @retcode = ( qq{ \@v;} ); } elsif( $arg_style eq "hashref" ) { my $qmembers = join( ", ", map { "'$_'" } @membernames, ( $with_tail ? "_tail" : () ) ); @argcode = ( qq{ ref(\$_[0]) eq "HASH" or croak "usage: $packfunc(\\%args)";}, qq( my \@v = \@{\$_[0]}{$qmembers};) ); @retcode = ( # Seems we can't easily do this without a temporary qq( my %ret; \@ret{$qmembers} = \@v;), qq{ \\%ret;} ); } else { carp "Unrecognised arg_style $arg_style"; } push_export $packfunc; push_export $unpackfunc; join( "\n", "", "sub $packfunc", "{", @argcode, @postargs, qq{ pack "$format", \@v;}, "}", "", "sub $unpackfunc", "{", ( $no_length_check ? '' : qq{ length \$_[0] $eq $sizeof or croak "$unpackfunc: expected $sizeof bytes, got " . length \$_[0];} ), qq{ my \@v = unpack "$format", \$_[0];}, @preret, @retcode, "}" ); } ]; } =pod The following structure member definitions are allowed: =over 8 =cut my %struct_formats = ( map { my $bytes = length( pack "$_", 0 ); "${bytes}u" => uc $_, "${bytes}s" => lc $_ } qw( C S L ) ); if( eval { pack "Q", 0 } ) { my $bytes = length( pack "Q", 0 ); $struct_formats{"${bytes}u"} = "Q"; $struct_formats{"${bytes}s"} = "q"; } =item * member_numeric The field contains a single signed or unsigned number. Its size and signedness will be automatically detected. =cut my $done_u64; sub member_numeric { my $varname; my $membername; my $argindex; return { set_names => sub { ( $varname, $membername ) = @_; }, set_arg => sub { $argindex = $_[0]++; }, gen_c => sub { qq{printf("$membername@%ld+%lu%c,", } . "(long)((char*)&$varname.$membername-(char*)&$varname), " . # offset "(unsigned long)sizeof($varname.$membername), " . # size "($varname.$membername=-1)>0?'u':'s'" . # signedness ");"; }, gen_format => sub { my ( $def, undef, $postarg, $preret ) = @_; # ( undef, curpos ) = @_; my ( $member, $offs, $size, $sign ) = $def =~ m/^([\w.]+)@(\d+)\+(\d+)([us])$/ or die "Could not parse member definition out of '$def'"; $member eq $membername or die "Expected definition of $membername but found $member instead"; my $format = ""; if( $offs > $_[1] ) { my $pad = $offs - $_[1]; $format .= "x" x $pad; $_[1] += $pad; } elsif( $offs < $_[1] ) { die "Err.. need to go backwards for structure $varname member $member"; } if( exists $struct_formats{"$size$sign"} ) { $format .= $struct_formats{"$size$sign"}; } elsif( $size == 8 and $sign eq "u" ) { # 64bit int on a 64bit-challenged perl. We'll have to improvise unless( $done_u64 ) { my $hilo = pack("S",0x0201) eq "\x02\x01" ? "\$hi, \$lo" : "\$lo, \$hi"; perlcode join "\n", "require Math::BigInt;", "", "sub __pack_u64 {", " my ( \$hi, \$lo ) = ( int(\$_[0] / 2**32), \$_[0] & 0xffffffff );", " pack( \"L L\", $hilo );", "}", "", "sub __unpack_u64 {", " length \$_[0] == 8 or return undef;", # in case of no_length_check " my ( $hilo ) = unpack( \"L L\", \$_[0] );", " return \$lo if \$hi == 0;", " my \$n = Math::BigInt->new(\$hi); \$n <<= 32; \$n |= \$lo;", " return \$n;", "}", ""; $done_u64++; } push @$postarg, " \$v[$argindex] = __pack_u64( \$v[$argindex] );"; push @$preret, " \$v[$argindex] = __unpack_u64( \$v[$argindex] );"; $format .= "a8"; } else { die "Cannot find a pack format for size $size sign $sign"; } $_[1] += $size; return $format; }, }; } =item * member_strarray The field contains a NULL-padded string of characters. Its size will be automatically detected. =cut sub member_strarray { my $varname; my $membername; my $argindex; return { set_names => sub { ( $varname, $membername ) = @_; }, set_arg => sub { $argindex = $_[0]++; }, gen_c => sub { qq{printf("$membername@%ld+%lu,", } . "(long)((char*)&$varname.$membername-(char*)&$varname), " . # offset "(unsigned long)sizeof($varname.$membername)" . # size ");"; }, gen_format => sub { my ( $def ) = @_; my ( $member, $offs, $size ) = $def =~ m/^([\w.]+)@(\d+)\+(\d+)$/ or die "Could not parse member definition out of '$def'"; $member eq $membername or die "Expected definition of $membername but found $member instead"; my $format = ""; if( $offs > $_[1] ) { my $pad = $offs - $_[1]; $format .= "x" x $pad; $_[1] += $pad; } elsif( $offs < $_[1] ) { die "Err.. need to go backwards for structure $varname member $member"; } $format .= "Z$size"; $_[1] += $size; return $format; }, }; } =item * member_constant($code) The field contains a single number as for C. Instead of consuming/returning a value in the arguments list, this member will be packed from an expression, or asserted that it contains the given value. The string C<$code> will be inserted into the generated pack and unpack functions, so it can be used for constants generated by the C directive. =cut sub member_constant { my $value = shift; my $constant = member_numeric; my $arg_index; $constant->{set_arg} = sub { $arg_index = $_[0] }; # no inc my $gen_format = delete $constant->{gen_format}; $constant->{gen_format} = sub { my ( $def, undef, $postarg, $preret ) = @_; my ( $member ) = $def =~ m/^([\w.]+)@/ or die "Could not parse member definition out of '$def'"; push @$postarg, " splice \@v, $arg_index, 0, $value;"; my $format = $gen_format->( @_ ); push @$preret, " splice( \@v, $arg_index, 1 ) == $value or croak \"expected $member == $value\";"; return $format; }; $constant; } =back The structure definition results in two new functions being created, C and C, where C<$name> is the name of the structure (with the leading C prefix stripped). These behave similarly to the familiar functions such as C; the C function will take a list of fields and return a packed string, the C function will take a string and return a list of fields. =cut =head2 no_export, use_export, use_export_ok Controls the export behaviour of the generated symbols. C creates symbols that are not exported by their package, they must be used fully- qualified. C creates symbols that are exported by default. C creates symbols that are exported if they are specifically requested at C time. The mode can be changed at any time to affect only the symbols that follow it. It defaults to C. =cut sub no_export { $export_mode = 0 } sub use_export { $export_mode = 1 } sub use_export_ok { $export_mode = "OK" } my $cbuilder = ExtUtils::CBuilder->new( quiet => 1 ); my %compile_args; my %link_args; if( my $mb = eval { require Module::Build and Module::Build->current } ) { $compile_args{include_dirs} = $mb->include_dirs; $compile_args{extra_compiler_flags} = $mb->extra_compiler_flags; $link_args{extra_linker_flags} = $mb->extra_linker_flags; } sub gen_perl { return "" unless @fragments; my $c_file = join "\n", "#include ", @preamble, "", "int main(void) {", @fragments, " return 0;", "}\n"; undef @preamble; undef @fragments; die "Cannot generate a C file yet - no module name\n" unless defined $modulename; my $tempname = "gen-$modulename"; my $sourcename = "$tempname.c"; { open( my $source_fh, "> $sourcename" ) or die "Cannot write $sourcename - $!"; print $source_fh $c_file; } my $objname = eval { $cbuilder->compile( source => $sourcename, %compile_args ) }; unlink $sourcename; if( !defined $objname ) { die "Failed to compile source\n"; } my $exename = eval { $cbuilder->link_executable( objects => $objname, %link_args ) }; unlink $objname; if( !defined $exename ) { die "Failed to link executable\n"; } my $output; { open( my $runh, "./$exename |" ) or die "Cannot pipeopen $exename - $!"; local $/; $output = <$runh>; } unlink $exename; my %results = map { m/^(\w+)=(.*)$/ } split m/\n/, $output; my $perl = ""; my @bodylines; # Evaluate these first, so they have a chance to push_export() foreach my $genblock ( @genblocks ) { my ( $key, $code ) = @$genblock; push @bodylines, $code->( $results{$key} ); } if( @exports ) { $perl .= "push \@EXPORT, " . join( ", ", map { "'$_'" } @exports ) . ";\n"; undef @exports; } if( @exports_ok ) { $perl .= "push \@EXPORT_OK, " . join( ", ", map { "'$_'" } @exports_ok ) . ";\n"; undef @exports_ok; } $perl .= join "", map { "$_\n" } @bodylines; undef @genblocks; my @thisperlcode = @perlcode; undef @perlcode; return join "\n", @thisperlcode, $perl; } =head2 $perl = gen_output Returns the generated perl code. This is used internally for testing purposes but normally would not be necessary; see instead C. =cut sub gen_output { my $ret = $output . gen_perl . "\n1;\n"; $output = ""; return $ret; } =head2 write_output $filename Write the generated perl code into the named file. This would normally be used as the last function in the containing script, to generate the output file. In the case of C or C invoking the script, the path to the file to be generated should be given in C<$ARGV[0]>. Normally, therefore, the script would end with write_output $ARGV[0]; =cut sub write_output { my ( $filename ) = @_; my $output = gen_output(); open( my $outfile, ">", $filename ) or die "Cannot write '$filename' - $!"; print $outfile $output; } =head1 EXAMPLES Normally this module would be used by another module at build time, to construct the relevant constants and structure functions from system headers. For example, suppose your operating system defines a new type of socket, which has its own packet and address families, and perhaps some new socket options which are valid on this socket. We can build a module to contain the relevant constants and structure functions by writing, for example: #!/usr/bin/perl use ExtUtils::H2PM; module "Socket::Moonlaser"; include "moon/laser.h"; constant "AF_MOONLASER"; constant "PF_MOONLASER"; constant "SOL_MOONLASER"; constant "MOONLASER_POWER", name => "POWER"; constant "MOONLASER_WAVELENGTH", name => "WAVELENGTH"; structure "struct laserwl", members => [ lwl_nm_coarse => member_numeric, lwl_nm_fine => member_numeric, ]; write_output $ARGV[0]; If we save this script as, say, F, then when the distribution is built, the script will be used to generate the contents of the file F. Once installed, any other code can simply use Socket::Moonlaser qw( AF_MOONLASER ); to import a constant. The method described above doesn't allow us any room to actually include other code in the module. Perhaps, as well as these simple constants, we'd like to include functions, documentation, etc... To allow this, name the script instead something like F, so that this is the name used for the generated output. The code can then be included in the actual F (which will just be a normal perl module) by package Socket::Moonlaser; use Socket::Moonlaser_const; sub get_power { getsockopt( $_[0], SOL_MOONLASER, POWER ); } sub set_power { setsockopt( $_[0], SOL_MOONLASER, POWER, $_[1] ); } sub get_wavelength { my $wl = getsockopt( $_[0], SOL_MOONLASER, WAVELENGTH ); defined $wl or return; unpack_laserwl( $wl ); } sub set_wavelength { my $wl = pack_laserwl( $_[1], $_[2] ); setsockopt( $_[0], SOL_MOONLASER, WAVELENGTH, $wl ); } 1; Sometimes, the actual C structure layout may not exactly match the semantics we wish to present to perl modules using this extension wrapper. Socket address structures typically contain their address family as the first member, whereas this detail isn't exposed by, for example, the C and C functions. To cope with this case, the low-level structure packing and unpacking functions can be generated with a different name, and wrapped in higher-level functions in the main code. For example, in F: no_export; structure "struct sockaddr_ml", pack_func => "_pack_sockaddr_ml", unpack_func => "_unpack_sockaddr_ml", members => [ ml_family => member_numeric, ml_lat_deg => member_numeric, ml_long_deg => member_numeric, ml_lat_fine => member_numeric, ml_long_fine => member_numeric, ]; This will generate a pack/unpack function pair taking or returning five arguments; these functions will not be exported. In our main F file we can wrap these to actually expose a different API: sub pack_sockaddr_ml { @_ == 2 or croak "usage: pack_sockaddr_ml(lat, long)"; my ( $lat, $long ) = @_; return _pack_sockaddr_ml( AF_MOONLASER, int $lat, int $long, ($lat - int $lat) * 1_000_000, ($long - int $long) * 1_000_000); } sub unpack_sockaddr_ml { my ( $family, $lat, $long, $lat_fine, $long_fine ) = _unpack_sockaddr_ml( $_[0] ); $family == AF_MOONLASER or croak "expected family AF_MOONLASER"; return ( $lat + $lat_fine/1_000_000, $long + $long_fine/1_000_000 ); } Sometimes, a structure will contain members which are themselves structures. Suppose a different definition of the above address, which at the C layer is defined as struct angle { short deg; unsigned long fine; }; struct sockaddr_ml { short ml_family; struct angle ml_lat, ml_long; }; We can instead "flatten" this structure tree to obtain the five fields by naming the sub-members of the outer structure: structure "struct sockaddr_ml", members => [ "ml_family" => member_numeric, "ml_lat.deg" => member_numeric, "ml_lat.fine" => member_numeric, "ml_long.deg" => member_numeric, "ml_long.fine" => member_numeric, ]; =head1 TODO =over 4 =item * Consider more structure members. With strings comes the requirement to have members that store a size. This requires cross-referential members. And while we're at it it might be nice to have constant members; fill in constants without consuming arguments when packing, assert the right value on unpacking. =back =head1 AUTHOR Paul Evans =cut 0x55AA; perl-ExtUtils-H2PM-0.09/t/000075500000000000000000000000001221365364600151405ustar00rootroot00000000000000perl-ExtUtils-H2PM-0.09/t/00use.t000064400000000000000000000001501221365364600162550ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use_ok( "ExtUtils::H2PM" ); done_testing; perl-ExtUtils-H2PM-0.09/t/01constant.t000064400000000000000000000053321221365364600173220ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; constant "DEFINED_CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 push \@EXPORT_OK, 'DEFINED_CONSTANT'; use constant DEFINED_CONSTANT => 10; 1; EOPERL 'Simple constant' ); ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); $INC{"TEST.pm"} = '$code'; is( evalordie("TEST::DEFINED_CONSTANT()" ), 10, 'Code exports a constant of the right value' ); $code = do { module "TEST"; include "t/test.h", local => 1; constant "DEFINED_CONSTANT", name => "CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 push \@EXPORT_OK, 'CONSTANT'; use constant CONSTANT => 10; 1; EOPERL 'Simple constant renamed' ); $code = do { module "TEST"; include "t/test.h", local => 1; no_export; constant "DEFINED_CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use constant DEFINED_CONSTANT => 10; 1; EOPERL 'No-export constant' ); $code = do { module "TEST"; include "t/test.h", local => 1; constant "ENUMERATED_CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use constant ENUMERATED_CONSTANT => 20; 1; EOPERL 'Enumerated constant' ); $code = do { module "TEST"; include "t/test.h", local => 1; constant "STATIC_CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use constant STATIC_CONSTANT => 30; 1; EOPERL 'Static constant' ); $code = do { module "TEST"; use_export; include "t/test.h", local => 1; constant "MISSING_CONSTANT", ifdef => "MISSING_CONSTANT"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 1; EOPERL 'Missing constant' ); done_testing; perl-ExtUtils-H2PM-0.09/t/02structure-numeric.t000064400000000000000000000106151221365364600211720ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; use constant LITTLE_ENDIAN => (pack("s",1) eq "\1\0"); use constant BIG_ENDIAN => (pack("s",1) eq "\0\1"); BEGIN { LITTLE_ENDIAN or BIG_ENDIAN or die "Cannot determine platform endian" } sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct point", members => [ x => member_numeric, y => member_numeric, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'pack_point', 'unpack_point'; sub pack_point { \@_ == 2 or croak "usage: pack_point(x, y)"; my \@v = \@_; pack "l l ", \@v; } sub unpack_point { length \$_[0] == 8 or croak "unpack_point: expected 8 bytes, got " . length \$_[0]; my \@v = unpack "l l ", \$_[0]; \@v; } 1; EOPERL 'Simple structure' ); ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); $INC{"TEST.pm"} = '$code'; is( TEST::pack_point(0x1234,0x5678), BIG_ENDIAN ? "\0\0\x12\x34\0\0\x56\x78" : "\x34\x12\0\0\x78\x56\0\0", 'pack_point()' ); is_deeply( [ TEST::unpack_point( BIG_ENDIAN ? "\0\0\x12\x34\0\0\x56\x78" : "\x34\x12\0\0\x78\x56\0\0" ) ], [ 0x1234, 0x5678 ], 'unpack_point()' ); $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct point", pack_func => "point_packing_function", unpack_func => "point_unpacking_function", members => [ x => member_numeric, y => member_numeric, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'point_packing_function', 'point_unpacking_function'; sub point_packing_function { \@_ == 2 or croak "usage: point_packing_function(x, y)"; my \@v = \@_; pack "l l ", \@v; } sub point_unpacking_function { length \$_[0] == 8 or croak "point_unpacking_function: expected 8 bytes, got " . length \$_[0]; my \@v = unpack "l l ", \$_[0]; \@v; } 1; EOPERL 'Structure with different function names' ); $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct msghdr", members => [ cmd => member_numeric, vers => member_numeric, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'pack_msghdr', 'unpack_msghdr'; sub pack_msghdr { \@_ == 2 or croak "usage: pack_msghdr(cmd, vers)"; my \@v = \@_; pack "l c x3", \@v; } sub unpack_msghdr { length \$_[0] == 8 or croak "unpack_msghdr: expected 8 bytes, got " . length \$_[0]; my \@v = unpack "l c x3", \$_[0]; \@v; } 1; EOPERL 'Structure with trailing padding' ); $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct llq", members => [ l1 => member_numeric, l2 => member_numeric, q => member_numeric, ]; gen_output; }; # Can't string check it since we don't know precisely what it might be yet ok( evalordie("no strict; $code"), 'llq code compiles cleanly' ); is( TEST::pack_llq(1,2,3), BIG_ENDIAN ? "\0\0\0\1\0\0\0\2\0\0\0\0\0\0\0\3" : "\1\0\0\0\2\0\0\0\3\0\0\0\0\0\0\0", 'pack_llq()' ); is_deeply( [ TEST::unpack_llq( BIG_ENDIAN ? "\0\0\0\1\0\0\0\2\0\0\0\0\0\0\0\3" : "\1\0\0\0\2\0\0\0\3\0\0\0\0\0\0\0" ) ], [ 1, 2, 3 ], 'unpack_llq()' ); $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct missing", members => [ none => member_numeric, ], ifdef => "HAS_STRUCT_MISSING"; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 1; EOPERL 'Missing structure' ); done_testing; perl-ExtUtils-H2PM-0.09/t/03structure-strarray.t000064400000000000000000000031021221365364600213710ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; use constant LITTLE_ENDIAN => (pack("s",1) eq "\1\0"); use constant BIG_ENDIAN => (pack("s",1) eq "\0\1"); BEGIN { LITTLE_ENDIAN or BIG_ENDIAN or die "Cannot determine platform endian" } sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct idname", members => [ id => member_numeric, name => member_strarray, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'pack_idname', 'unpack_idname'; sub pack_idname { \@_ == 2 or croak "usage: pack_idname(id, name)"; my \@v = \@_; pack "l Z12 ", \@v; } sub unpack_idname { length \$_[0] == 16 or croak "unpack_idname: expected 16 bytes, got " . length \$_[0]; my \@v = unpack "l Z12 ", \$_[0]; \@v; } 1; EOPERL 'Structure with string' ); ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); $INC{"TEST.pm"} = '$code'; is( TEST::pack_idname(0x1234, "Hello"), BIG_ENDIAN ? "\0\0\x12\x34Hello\0\0\0\0\0\0\0" : "\x34\x12\0\0Hello\0\0\0\0\0\0\0", 'pack_idname()' ); is_deeply( [ TEST::unpack_idname( BIG_ENDIAN ? "\0\0\x12\x34Hello\0\0\0\0\0\0\0" : "\x34\x12\0\0Hello\0\0\0\0\0\0\0" ) ], [ 0x1234, "Hello" ], 'unpack_idname()' ); done_testing; perl-ExtUtils-H2PM-0.09/t/04structure-constant.t000064400000000000000000000022561221365364600213650ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; my $code; $code = do { module "TEST"; include "t/test.h", local => 1; constant "ENUMERATED_CONSTANT"; structure "struct idname", members => [ id => member_constant("ENUMERATED_CONSTANT"), name => member_strarray, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from t/04structure-constant.t use Carp; push \@EXPORT_OK, 'ENUMERATED_CONSTANT', 'pack_idname', 'unpack_idname'; use constant ENUMERATED_CONSTANT => 20; sub pack_idname { \@_ == 1 or croak "usage: pack_idname(name)"; my \@v = \@_; splice \@v, 0, 0, ENUMERATED_CONSTANT; pack "l Z12 ", \@v; } sub unpack_idname { length \$_[0] == 16 or croak "unpack_idname: expected 16 bytes, got " . length \$_[0]; my \@v = unpack "l Z12 ", \$_[0]; splice( \@v, 0, 1 ) == ENUMERATED_CONSTANT or croak "expected id == ENUMERATED_CONSTANT"; \@v; } 1; EOPERL 'Simple structure with a constant' ); done_testing; perl-ExtUtils-H2PM-0.09/t/10structure-with-tail.t000064400000000000000000000031541221365364600214310ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; use constant LITTLE_ENDIAN => (pack("s",1) eq "\1\0"); use constant BIG_ENDIAN => (pack("s",1) eq "\0\1"); BEGIN { LITTLE_ENDIAN or BIG_ENDIAN or die "Cannot determine platform endian" } sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct msghdr", with_tail => 1, members => [ cmd => member_numeric, vers => member_numeric, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'pack_msghdr', 'unpack_msghdr'; sub pack_msghdr { \@_ >= 2 or croak "usage: pack_msghdr(cmd, vers, [tail])"; my \@v = \@_; pack "l c x3a*", \@v; } sub unpack_msghdr { length \$_[0] >= 8 or croak "unpack_msghdr: expected 8 bytes, got " . length \$_[0]; my \@v = unpack "l c x3a*", \$_[0]; \@v; } 1; EOPERL 'Structure with tail' ); ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); $INC{"TEST.pm"} = '$code'; is( TEST::pack_msghdr(0x1234, 0x56, "hello\0"), BIG_ENDIAN ? "\0\0\x12\x34\x56\0\0\0hello\0" : "\x34\x12\0\0\x56\0\0\0hello\0", 'pack_point()' ); is_deeply( [ TEST::unpack_msghdr( BIG_ENDIAN ? "\0\0\x12\x34\x56\0\0\0hello\0" : "\x34\x12\0\0\x56\0\0\0hello\0" ) ], [ 0x1234, 0x56, "hello\0" ], 'unpack_point()' ); done_testing; perl-ExtUtils-H2PM-0.09/t/11structure-args-hashref.t000064400000000000000000000032041221365364600220760ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; use constant LITTLE_ENDIAN => (pack("s",1) eq "\1\0"); use constant BIG_ENDIAN => (pack("s",1) eq "\0\1"); BEGIN { LITTLE_ENDIAN or BIG_ENDIAN or die "Cannot determine platform endian" } sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct point", arg_style => "hashref", members => [ x => member_numeric, y => member_numeric, ]; gen_output; }; is_deeply( [ split m/\n/, $code ], [ split m/\n/, <<"EOPERL" ], package TEST; # This module was generated automatically by ExtUtils::H2PM from $0 use Carp; push \@EXPORT_OK, 'pack_point', 'unpack_point'; sub pack_point { ref(\$_[0]) eq "HASH" or croak "usage: pack_point(\\%args)"; my \@v = \@{\$_[0]}{'x', 'y'}; pack "l l ", \@v; } sub unpack_point { length \$_[0] == 8 or croak "unpack_point: expected 8 bytes, got " . length \$_[0]; my \@v = unpack "l l ", \$_[0]; my %ret; \@ret{'x', 'y'} = \@v; \\%ret; } 1; EOPERL 'Simple structure' ); ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); $INC{"TEST.pm"} = '$code'; is( TEST::pack_point( { x => 0x1234, y => 0x5678 } ), BIG_ENDIAN ? "\0\0\x12\x34\0\0\x56\x78" : "\x34\x12\0\0\x78\x56\0\0", 'pack_point()' ); is_deeply( TEST::unpack_point( BIG_ENDIAN ? "\0\0\x12\x34\0\0\x56\x78" : "\x34\x12\0\0\x78\x56\0\0" ), { x => 0x1234, y => 0x5678 }, 'unpack_point()' ); done_testing; perl-ExtUtils-H2PM-0.09/t/12structure-no-length-check.t000064400000000000000000000016371221365364600225030ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use ExtUtils::H2PM; use constant LITTLE_ENDIAN => (pack("s",1) eq "\1\0"); use constant BIG_ENDIAN => (pack("s",1) eq "\0\1"); BEGIN { LITTLE_ENDIAN or BIG_ENDIAN or die "Cannot determine platform endian" } sub evalordie { my $code = shift; my $ret = eval $code; $@ and die $@; $ret; } my $code; $code = do { module "TEST"; include "t/test.h", local => 1; structure "struct point", no_length_check => 1, members => [ x => member_numeric, y => member_numeric, ]; gen_output; }; ok( evalordie("no strict; $code"), 'Code evaluates successfully' ); is_deeply( [ TEST::unpack_point( BIG_ENDIAN ? "\0\0\x12\x34\0\0\x56\x78\0\0\x9a\xbc" : "\x34\x12\0\0\x78\x56\0\0\xbc\x9a\0\0" ) ], [ 0x1234, 0x5678 ], 'unpack_point()' ); done_testing; perl-ExtUtils-H2PM-0.09/t/99pod.t000064400000000000000000000002571221365364600162750ustar00rootroot00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); perl-ExtUtils-H2PM-0.09/t/test.h000064400000000000000000000005601221365364600162710ustar00rootroot00000000000000#define DEFINED_CONSTANT 10 #include enum { ENUMERATED_CONSTANT = 20 }; static const int STATIC_CONSTANT = 30; struct point { int x, y; }; struct msghdr { int cmd; signed char vers; // Be specific on signedness /* hope there's a hole here */ }; struct idname { int id; char name[12]; }; struct llq { uint32_t l1, l2; uint64_t q; };