Asm-C

 view release on metacpan or  search on metacpan

lib/Asm/C.pm  view on Meta::CPAN

sub extractCStructureSize($$)                                                   # Extract the size of a C structure
 {my ($input, $structure) = @_;                                                 # Input file, structure name
  if (my $s = extractCStructure $input)                                         # Structures in file
   {if (my $S = $$s{$structure})                                                # Found structure
     {return $S->size;                                                          # Return structure size
     }
   }
  undef                                                                         # Parse failed or no such structure
 } # extractCStructureSize

#D2 Macros                                                                      # Extract macro values from C header files

my %extractMacroDefinitionsFromCHeaderFile;                                     # Cache macro definitions

sub extractMacroDefinitionsFromCHeaderFile($)                                   # Extract the macro definitions found in a C header file using gcc
 {my ($includeFile) = @_;                                                       # C Header file name as it would be entered in a C program
  my $d = $extractMacroDefinitionsFromCHeaderFile{$includeFile};                # Cached macro definitions
  return $d if $d;                                                              # Return cached value

  confirmHasCommandLineCommand("gcc");                                          # Check gcc
  my @l = qx(gcc -E -dM -include "$includeFile" - < /dev/null);                 # Use gcc to extract macro definitions

  my %d;
  for my $l(@l)                                                                 # Extract macro definitions
   {if ($l =~ m(\A#define\s+(\S+)\s+(\S+)(.*)))
     {$d{$1} = $2;
     }
   }

  $extractMacroDefinitionsFromCHeaderFile{$includeFile} = \%d;                  # Return definitions
 }

sub extractMacroDefinitionFromCHeaderFile($$)                                   # Extract a macro definitions found in a C header file using gcc
 {my ($includeFile, $macro) = @_;                                               # C Header file name as it would be entered in a C program, macro name
  if (my $d = extractMacroDefinitionsFromCHeaderFile($includeFile))             # Get macro definitions
   {return $$d{$macro};
   }
  undef
 }

#d
#-------------------------------------------------------------------------------
# Export - eeee
#-------------------------------------------------------------------------------

lib/Asm/C.pm  view on Meta::CPAN

@ISA          = qw(Exporter);
@EXPORT       = qw();
@EXPORT_OK    = qw(
extractCField
extractCFieldLoc
extractCFieldSize
extractCFieldType
extractCStructure
extractCStructureFields
extractCStructureSize
extractMacroDefinitionFromCHeaderFile
extractMacroDefinitionsFromCHeaderFile
);
%EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);

# podDocumentation
=pod

=encoding utf-8

=head1 Name

lib/Asm/C.pm  view on Meta::CPAN

       b => bless({field => "b", loc => 4, size => 4, type => "int"}, "field"),
       c => bless({field => "c", loc => 8, size => 4, type => "int"}, "field"),
     },
     size   => 12,
  }, "structure")};

=head2 Extract macro values from a C header file.

Find the value of a macro definition in a C header file:

  my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");

  is_deeply $$m{MAP_ANONYMOUS}, "0x20";

=head1 Description

Extract macro values and structure details from C programs.


Version "20210328".

lib/Asm/C.pm  view on Meta::CPAN


  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;


=head2 Macros

Extract macro values from C header files

=head3 extractMacroDefinitionsFromCHeaderFile($includeFile)

Extract the macro definitions found in a C header file using gcc

     Parameter     Description
  1  $includeFile  C Header file name as it would be entered in a C program

B<Example:>


  if (1)
   {my $h = "linux/mman.h";

    my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

    is_deeply $$m{MAP_ANONYMOUS}, "0x20";
    ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2";
   }


=head3 extractMacroDefinitionFromCHeaderFile($includeFile, $macro)

Extract a macro definitions found in a C header file using gcc

     Parameter     Description
  1  $includeFile  C Header file name as it would be entered in a C program
  2  $macro        Macro name

B<Example:>


  if (1)
   {my $h = "linux/mman.h";
    my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");
    is_deeply $$m{MAP_ANONYMOUS}, "0x20";

    ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

   }



=head1 Index


1 L<extractCField|/extractCField> - Extract the details of a field in a structure in a C file

lib/Asm/C.pm  view on Meta::CPAN

3 L<extractCFieldSize|/extractCFieldSize> - Extract the size of a field in a structure in a C file

4 L<extractCFieldType|/extractCFieldType> - Extract the type of a field in a structure in a C file

5 L<extractCStructure|/extractCStructure> - Extract the details of a structure

6 L<extractCStructureFields|/extractCStructureFields> - Extract the names of the fields in a C structure

7 L<extractCStructureSize|/extractCStructureSize> - Extract the size of a C structure

8 L<extractMacroDefinitionFromCHeaderFile|/extractMacroDefinitionFromCHeaderFile> - Extract a macro definitions found in a C header file using gcc

9 L<extractMacroDefinitionsFromCHeaderFile|/extractMacroDefinitionsFromCHeaderFile> - Extract the macro definitions found in a C header file using gcc

=head1 Installation

This module is written in 100% Pure Perl and, thus, it is easy to read,
comprehend, use, modify and install via B<cpan>:

  sudo cpan install Asm::C

=head1 Author

lib/Asm/C.pm  view on Meta::CPAN


  is_deeply extractCFieldType($input, q(S), q(a)), q(int);
  is_deeply extractCFieldType($input, q(S), q(b)), q(int);
  is_deeply extractCFieldType($input, q(S), q(c)), q(int);
 }

if (0)
 {my $s = extractCStructure q(#include <time.h>);
 }

if (1)                                                                          #TextractMacroDefinitionsFromCHeaderFile #TextractMacroDefinitionFromCHeaderFile
 {my $h = "linux/mman.h";
  my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");
  is_deeply $$m{MAP_ANONYMOUS}, "0x20";
  ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2";
 }

lll "Finished:", time - $start;



( run in 0.681 second using v1.01-cache-2.11-cpan-49f99fa48dc )