2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 package OpenSSL::ParseC;
15 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
21 my @preprocessor_conds; # A list of simple preprocessor conditions,
22 # each item being a list of macros defined
27 return map { ( @$_ ) } @preprocessor_conds;
30 # A list of handlers that will look at a "complete" string and try to
31 # figure out what to make of it.
32 # Each handler is a hash with the following keys:
34 # regexp a regexp to compare the "complete" string with.
35 # checker a function that does a more complex comparison.
36 # Use this instead of regexp if that isn't enough.
37 # massager massages the "complete" string into an array with
38 # the following elements:
40 # [0] String that needs further processing (this
41 # applies to typedefs of structs), or empty.
42 # [1] The name of what was found.
43 # [2] A character that denotes what type of thing
44 # this is: 'F' for function, 'S' for struct,
45 # 'T' for typedef, 'M' for macro, 'V' for
47 # [3] Return type (only for type 'F' and 'V')
48 # [4] Value (for type 'M') or signature (for type 'F',
50 # [5...] The list of preprocessor conditions this is
51 # found in, as in checks for macro definitions
52 # (stored as the macro's name) or the absence
53 # of definition (stored as the macro's name
56 # If the massager returns an empty list, it means the
57 # "complete" string has side effects but should otherwise
59 # If the massager is undefined, the "complete" string
61 my @opensslcpphandlers = (
62 ##################################################################
63 # OpenSSL CPP specials
65 # These are used to convert certain pre-precessor expressions into
66 # others that @cpphandlers have a better chance to understand.
68 { regexp => qr/#if (!?)OPENSSL_API_([0-9_]+)$/,
70 my $cnd = $1 eq '!' ? 'ndef' : 'def';
72 #if$cnd DEPRECATEDIN_$2
78 ##################################################################
81 { regexp => qr/#ifdef ?(.*)/,
84 if (ref($_[$#_]) eq "HASH") {
88 push @preprocessor_conds, [ $1 ];
89 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
94 { regexp => qr/#ifndef ?(.*)/,
97 if (ref($_[$#_]) eq "HASH") {
101 push @preprocessor_conds, [ '!'.$1 ];
102 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
107 { regexp => qr/#if (0|1)/,
110 if (ref($_[$#_]) eq "HASH") {
115 push @preprocessor_conds, [ "TRUE" ];
117 push @preprocessor_conds, [ "!TRUE" ];
119 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
124 { regexp => qr/#if ?(.*)/,
127 if (ref($_[$#_]) eq "HASH") {
133 if ($conds =~ m|^defined<<<\(([^\)]*)\)>>>(.*)$|) {
134 push @results, $1; # Handle the simple case
136 my $re = qr/^(?:\|\|defined<<<\([^\)]*\)>>>)*$/;
137 print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
139 if ($rest =~ m/$re/) {
140 my @rest = split /\|\|/, $rest;
143 m|^defined<<<\(([^\)]*)\)>>>$|;
144 die "Something wrong...$opts{PLACE}" if $1 eq "";
148 $conds =~ s/<<<|>>>//g;
149 warn "Warning: complicated #if expression(1): $conds$opts{PLACE}"
152 } elsif ($conds =~ m|^!defined<<<\(([^\)]*)\)>>>(.*)$|) {
153 push @results, '!'.$1; # Handle the simple case
155 my $re = qr/^(?:\&\&!defined<<<\([^\)]*\)>>>)*$/;
156 print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
158 if ($rest =~ m/$re/) {
159 my @rest = split /\&\&/, $rest;
162 m|^!defined<<<\(([^\)]*)\)>>>$|;
163 die "Something wrong...$opts{PLACE}" if $1 eq "";
164 push @results, '!'.$1;
167 $conds =~ s/<<<|>>>//g;
168 warn "Warning: complicated #if expression(2): $conds$opts{PLACE}"
172 $conds =~ s/<<<|>>>//g;
173 warn "Warning: complicated #if expression(3): $conds$opts{PLACE}"
176 print STDERR "DEBUG[",$opts{debug_type},"]: Added preprocessor conds: '", join("', '", @results), "'\n"
178 push @preprocessor_conds, [ @results ];
179 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
184 { regexp => qr/#elif (.*)/,
187 if (ref($_[$#_]) eq "HASH") {
191 die "An #elif without corresponding condition$opts{PLACE}"
192 if !@preprocessor_conds;
193 pop @preprocessor_conds;
194 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
201 { regexp => qr/#else/,
204 if (ref($_[$#_]) eq "HASH") {
208 die "An #else without corresponding condition$opts{PLACE}"
209 if !@preprocessor_conds;
210 # Invert all conditions on the last level
211 my $stuff = pop @preprocessor_conds;
212 push @preprocessor_conds, [
213 map { m|^!(.*)$| ? $1 : '!'.$_ } @$stuff
215 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
220 { regexp => qr/#endif ?/,
223 if (ref($_[$#_]) eq "HASH") {
227 die "An #endif without corresponding condition$opts{PLACE}"
228 if !@preprocessor_conds;
229 pop @preprocessor_conds;
230 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
235 { regexp => qr/#define ([[:alpha:]_]\w*)(<<<\(.*?\)>>>)?( (.*))?/,
239 my $spaceval = $3||"";
242 $1, 'M', "", $params ? "$name$params$spaceval" : $val,
246 massager => sub { return (); }
250 my @opensslchandlers = (
251 ##################################################################
254 # They are really preprocessor stuff, but they look like C stuff
255 # to this parser. All of these do replacements, anything else is
259 # Deprecated stuff, by OpenSSL release.
261 # We trick the parser by pretending that the declaration is wrapped in a
262 # check if the DEPRECATEDIN macro is defined or not. Callers of parse()
263 # will have to decide what to do with it.
264 { regexp => qr/(DEPRECATEDIN_\d+(?:_\d+_\d+)?)<<<\((.*)\)>>>/,
265 massager => sub { return (<<"EOF");
276 # LHASH_OF(foo) is used as a type, but the chandlers won't take it
277 # gracefully, so we expand it here.
278 { regexp => qr/(.*)\bLHASH_OF<<<\((.*?)\)>>>(.*)/,
279 massager => sub { return ("$1struct lhash_st_$2$3"); }
281 { regexp => qr/DEFINE_LHASH_OF<<<\((.*)\)>>>/,
284 static ossl_inline LHASH_OF($1) * lh_$1_new(unsigned long (*hfn)(const $1 *),
285 int (*cfn)(const $1 *, const $1 *));
286 static ossl_inline void lh_$1_free(LHASH_OF($1) *lh);
287 static ossl_inline $1 *lh_$1_insert(LHASH_OF($1) *lh, $1 *d);
288 static ossl_inline $1 *lh_$1_delete(LHASH_OF($1) *lh, const $1 *d);
289 static ossl_inline $1 *lh_$1_retrieve(LHASH_OF($1) *lh, const $1 *d);
290 static ossl_inline int lh_$1_error(LHASH_OF($1) *lh);
291 static ossl_inline unsigned long lh_$1_num_items(LHASH_OF($1) *lh);
292 static ossl_inline void lh_$1_node_stats_bio(const LHASH_OF($1) *lh, BIO *out);
293 static ossl_inline void lh_$1_node_usage_stats_bio(const LHASH_OF($1) *lh,
295 static ossl_inline void lh_$1_stats_bio(const LHASH_OF($1) *lh, BIO *out);
296 static ossl_inline unsigned long lh_$1_get_down_load(LHASH_OF($1) *lh);
297 static ossl_inline void lh_$1_set_down_load(LHASH_OF($1) *lh, unsigned long dl);
298 static ossl_inline void lh_$1_doall(LHASH_OF($1) *lh, void (*doall)($1 *));
307 # STACK_OF(foo) is used as a type, but the chandlers won't take it
308 # gracefully, so we expand it here.
309 { regexp => qr/(.*)\bSTACK_OF<<<\((.*?)\)>>>(.*)/,
310 massager => sub { return ("$1struct stack_st_$2$3"); }
312 # { regexp => qr/(.*)\bSTACK_OF\((.*?)\)(.*)/,
315 # my $stack_of = "struct stack_st_$2";
317 # if ($after =~ m|^\w|) { $after = " ".$after; }
318 # return ("$before$stack_of$after");
321 { regexp => qr/SKM_DEFINE_STACK_OF<<<\((.*),\s*(.*),\s*(.*)\)>>>/,
325 typedef int (*sk_$1_compfunc)(const $3 * const *a, const $3 *const *b);
326 typedef void (*sk_$1_freefunc)($3 *a);
327 typedef $3 * (*sk_$1_copyfunc)(const $3 *a);
328 static ossl_inline int sk_$1_num(const STACK_OF($1) *sk);
329 static ossl_inline $2 *sk_$1_value(const STACK_OF($1) *sk, int idx);
330 static ossl_inline STACK_OF($1) *sk_$1_new(sk_$1_compfunc compare);
331 static ossl_inline STACK_OF($1) *sk_$1_new_null(void);
332 static ossl_inline STACK_OF($1) *sk_$1_new_reserve(sk_$1_compfunc compare,
334 static ossl_inline int sk_$1_reserve(STACK_OF($1) *sk, int n);
335 static ossl_inline void sk_$1_free(STACK_OF($1) *sk);
336 static ossl_inline void sk_$1_zero(STACK_OF($1) *sk);
337 static ossl_inline $2 *sk_$1_delete(STACK_OF($1) *sk, int i);
338 static ossl_inline $2 *sk_$1_delete_ptr(STACK_OF($1) *sk, $2 *ptr);
339 static ossl_inline int sk_$1_push(STACK_OF($1) *sk, $2 *ptr);
340 static ossl_inline int sk_$1_unshift(STACK_OF($1) *sk, $2 *ptr);
341 static ossl_inline $2 *sk_$1_pop(STACK_OF($1) *sk);
342 static ossl_inline $2 *sk_$1_shift(STACK_OF($1) *sk);
343 static ossl_inline void sk_$1_pop_free(STACK_OF($1) *sk,
344 sk_$1_freefunc freefunc);
345 static ossl_inline int sk_$1_insert(STACK_OF($1) *sk, $2 *ptr, int idx);
346 static ossl_inline $2 *sk_$1_set(STACK_OF($1) *sk, int idx, $2 *ptr);
347 static ossl_inline int sk_$1_find(STACK_OF($1) *sk, $2 *ptr);
348 static ossl_inline int sk_$1_find_ex(STACK_OF($1) *sk, $2 *ptr);
349 static ossl_inline void sk_$1_sort(STACK_OF($1) *sk);
350 static ossl_inline int sk_$1_is_sorted(const STACK_OF($1) *sk);
351 static ossl_inline STACK_OF($1) * sk_$1_dup(const STACK_OF($1) *sk);
352 static ossl_inline STACK_OF($1) *sk_$1_deep_copy(const STACK_OF($1) *sk,
353 sk_$1_copyfunc copyfunc,
354 sk_$1_freefunc freefunc);
355 static ossl_inline sk_$1_compfunc sk_$1_set_cmp_func(STACK_OF($1) *sk,
356 sk_$1_compfunc compare);
360 { regexp => qr/DEFINE_SPECIAL_STACK_OF<<<\((.*),\s*(.*)\)>>>/,
361 massager => sub { return ("SKM_DEFINE_STACK_OF($1,$2,$2)"); },
363 { regexp => qr/DEFINE_STACK_OF<<<\((.*)\)>>>/,
364 massager => sub { return ("SKM_DEFINE_STACK_OF($1,$1,$1)"); },
366 { regexp => qr/DEFINE_SPECIAL_STACK_OF_CONST<<<\((.*),\s*(.*)\)>>>/,
367 massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $2,$2)"); },
369 { regexp => qr/DEFINE_STACK_OF_CONST<<<\((.*)\)>>>/,
370 massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $1,$1)"); },
372 { regexp => qr/PREDECLARE_STACK_OF<<<\((.*)\)>>>/,
373 massager => sub { return ("STACK_OF($1);"); }
375 { regexp => qr/DECLARE_STACK_OF<<<\((.*)\)>>>/,
376 massager => sub { return ("STACK_OF($1);"); }
378 { regexp => qr/DECLARE_SPECIAL_STACK_OF<<<\((.*?),\s*(.*?)\)>>>/,
379 massager => sub { return ("STACK_OF($1);"); }
385 { regexp => qr/TYPEDEF_D2I_OF<<<\((.*)\)>>>/,
387 return ("typedef $1 *d2i_of_$1($1 **,const unsigned char **,long)");
390 { regexp => qr/TYPEDEF_I2D_OF<<<\((.*)\)>>>/,
392 return ("typedef $1 *i2d_of_$1($1 *,unsigned char **)");
395 { regexp => qr/TYPEDEF_D2I2D_OF<<<\((.*)\)>>>/,
397 return ("TYPEDEF_D2I_OF($1); TYPEDEF_I2D_OF($1)");
400 { regexp => qr/DECLARE_ASN1_ITEM<<<\((.*)\)>>>/,
403 const ASN1_ITEM *$1_it(void);
407 { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS_only<<<\((.*),\s*(.*)\)>>>/,
415 { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS<<<\((.*),\s*(.*),\s*(.*)\)>>>/,
420 DECLARE_ASN1_ITEM($2)
424 { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
429 DECLARE_ASN1_ITEM($2)
433 { regexp => qr/DECLARE_ASN1_ALLOC_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
441 { regexp => qr/DECLARE_ASN1_ALLOC_FUNCTIONS<<<\((.*)\)>>>/,
449 { regexp => qr/DECLARE_ASN1_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
456 DECLARE_ASN1_ITEM($2)
460 { regexp => qr/DECLARE_ASN1_FUNCTIONS<<<\((.*)\)>>>/,
461 massager => sub { return (<<"EOF");
466 DECLARE_ASN1_ITEM($1)
470 { regexp => qr/DECLARE_ASN1_NDEF_FUNCTION<<<\((.*)\)>>>/,
473 int i2d_$1_NDEF(void);
477 { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION<<<\((.*)\)>>>/,
480 int $1_print_ctx(void);
484 { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION_name<<<\((.*),\s*(.*)\)>>>/,
487 int $2_print_ctx(void);
491 { regexp => qr/DECLARE_ASN1_SET_OF<<<\((.*)\)>>>/,
492 massager => sub { return (); }
494 { regexp => qr/DECLARE_ASN1_DUP_FUNCTION<<<\((.*)\)>>>/,
501 { regexp => qr/DECLARE_ASN1_DUP_FUNCTION_name<<<\((.*),\s*(.*)\)>>>/,
508 { regexp => qr/DECLARE_PKCS12_SET_OF<<<\((.*)\)>>>/,
509 massager => sub { return (); }
511 { regexp => qr/DECLARE_PEM(?|_rw|_rw_cb|_rw_const)<<<\((.*?),.*\)>>>/,
512 massager => sub { return (<<"EOF");
513 #ifndef OPENSSL_NO_STDIO
514 int PEM_read_$1(void);
515 int PEM_write_$1(void);
517 int PEM_read_bio_$1(void);
518 int PEM_write_bio_$1(void);
525 { regexp => qr/DECLARE_PEM(?|_write|_write_cb|_write_const)<<<\((.*?),.*\)>>>/,
526 massager => sub { return (<<"EOF");
527 #ifndef OPENSSL_NO_STDIO
528 int PEM_write_$1(void);
530 int PEM_write_bio_$1(void);
534 { regexp => qr/DECLARE_PEM(?|_read|_read_cb)<<<\((.*?),.*\)>>>/,
535 massager => sub { return (<<"EOF");
536 #ifndef OPENSSL_NO_STDIO
537 int PEM_read_$1(void);
539 int PEM_read_bio_$1(void);
544 # Spurious stuff found in the OpenSSL headers
545 # Usually, these are just macros that expand to, well, something
546 { regexp => qr/__NDK_FPABI__/,
547 massager => sub { return (); }
554 ##################################################################
557 # extern "C" of individual items
558 # Note that the main parse function has a special hack for 'extern "C" {'
559 # which can't be done in handlers
560 # We simply ignore it.
561 { regexp => qr/extern "C" (.*;)/,
562 massager => sub { return ($1); },
564 # any other extern is just ignored
565 { regexp => qr/^\s* # Any spaces before
566 extern # The keyword we look for
567 \b # word to non-word boundary
571 massager => sub { return (); },
573 # union, struct and enum definitions
574 # Because this one might appear a little everywhere within type
575 # definitions, we take it out and replace it with just
576 # 'union|struct|enum name' while registering it.
577 # This makes use of the parser trick to surround the outer braces
579 { regexp => qr/(.*) # Anything before ($1)
580 \b # word to non-word boundary
581 (union|struct|enum) # The word used ($2)
582 (?:\s([[:alpha:]_]\w*))? # Struct or enum name ($3)
583 <<<(\{.*?\})>>> # Struct or enum definition ($4)
584 (.*) # Anything after ($5)
591 || sprintf("__anon%03d", ++$anoncnt); # Anonymous struct
594 my $type = $word eq "struct" ? 'S' : 'E';
595 if ($before ne "" || $after ne ";") {
596 if ($after =~ m|^\w|) { $after = " ".$after; }
597 return ("$before$word $name$after;",
598 "$word $name", $type, "", "$word$definition", all_conds());
600 # If there was no before nor after, make the return much simple
601 return ("", "$word $name", $type, "", "$word$definition", all_conds());
604 # Named struct and enum forward declarations
605 # We really just ignore them, but we need to parse them or the variable
606 # declaration handler further down will think it's a variable declaration.
607 { regexp => qr/^(union|struct|enum) ([[:alpha:]_]\w*);/,
608 massager => sub { return (); }
610 # Function returning function pointer declaration
611 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
612 ((?:\w|\*|\s)*?) # Return type ($2)
615 ([[:alpha:]_]\w*) # Function name ($3)
616 (\(.*\)) # Parameters ($4)
618 <<<(\(.*\))>>> # F.p. parameters ($5)
622 return ("", $3, 'F', "", "$2(*$4)$5", all_conds())
624 return ("", $3, 'F', "$2(*)$5", "$2(*$4)$5", all_conds()); }
626 # Function pointer declaration, or typedef thereof
627 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
628 ((?:\w|\*|\s)*?) # Return type ($2)
629 <<<\(\*([[:alpha:]_]\w*)\)>>> # T.d. or var name ($3)
630 <<<(\(.*\))>>> # F.p. parameters ($4)
634 return ("", $3, 'T', "", "$2(*)$4", all_conds())
636 return ("", $3, 'V', "$2(*)$4", "$2(*)$4", all_conds());
639 # Function declaration, or typedef thereof
640 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
641 ((?:\w|\*|\s)*?) # Return type ($2)
643 ([[:alpha:]_]\w*) # Function name ($3)
644 <<<(\(.*\))>>> # Parameters ($4)
648 return ("", $3, 'T', "", "$2$4", all_conds())
650 return ("", $3, 'F', $2, "$2$4", all_conds());
653 # Variable declaration, including arrays, or typedef thereof
654 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
655 ((?:\w|\*|\s)*?) # Type ($2)
657 ([[:alpha:]_]\w*) # Variable name ($3)
658 ((?:<<<\[[^\]]*\]>>>)*) # Possible array declaration ($4)
662 return ("", $3, 'T', "", $2.($4||""), all_conds())
664 return ("", $3, 'V', $2.($4||""), $2.($4||""), all_conds());
669 # End handlers are almost the same as handlers, except they are run through
670 # ONCE when the input has been parsed through. These are used to check for
671 # remaining stuff, such as an unfinished #ifdef and stuff like that that the
672 # main parser can't check on its own.
677 die "Unfinished preprocessor conditions levels: ",scalar(@preprocessor_conds),($opts{filename} ? " in file ".$opts{filename}: ""),$opts{PLACE}
678 if @preprocessor_conds;
683 # takes a list of strings that can each contain one or several lines of code
684 # also takes a hash of options as last argument.
686 # returns a list of hashes with information:
688 # name name of the thing
689 # type type, see the massage handler function
690 # returntype return type of functions and variables
691 # value value for macros, signature for functions, variables
693 # conds preprocessor conditions (array ref)
697 if (ref($_[$#_]) eq "HASH") {
702 in_extern_C => 0, # An exception to parenthesis processing.
703 cpp_parens => [], # A list of ending parens and braces found in
704 # preprocessor directives
705 c_parens => [], # A list of ending parens and braces found in
707 in_string => "", # empty string when outside a string, otherwise
708 # "'" or '"' depending on the starting quote.
709 in_comment => "", # empty string when outside a comment, otherwise
710 # "/*" or "//" depending on the type of comment
711 # found. The latter will never be multiline
712 # NOTE: in_string and in_comment will never be
713 # true (in perl semantics) at the same time.
717 my $normalized_line = ""; # $input_line, but normalized. In essence, this
718 # means that ALL whitespace is removed unless
719 # it absolutely has to be present, and in that
720 # case, there's only one space.
721 # The cases where a space needs to stay present
724 # 2. between words and number
725 # 3. after the first word of a preprocessor
727 # 4. for the #define directive, between the macro
728 # name/args and its value, so we end up with:
730 # #define BAR(x) something(x)
731 my $collected_stmt = ""; # Where we're building up a C line until it's a
732 # complete definition/declaration, as determined
733 # by any handler being capable of matching it.
735 # We use $_ shamelessly when looking through @lines.
736 # In case we find a \ at the end, we keep filling it up with more lines.
739 foreach my $line (@_) {
740 # split tries to be smart when a string ends with the thing we split on
741 $line .= "\n" unless $line =~ m|\R$|;
744 # We use ¦undef¦ as a marker for a new line from the file.
745 # Since we convert one line to several and unshift that into @lines,
746 # that's the only safe way we have to track the original lines
747 my @lines = map { ( undef, $_ ) } split $/, $line;
749 # Remember that extra # we added above? Now we remove it
751 pop @lines; # Don't forget the undef
754 if (!defined($lines[0])) {
756 $state{current_line}++;
758 $opts{PLACE} = " at ".$opts{filename}." line ".$state{current_line}."\n";
759 $opts{PLACE2} = $opts{filename}.":".$state{current_line};
764 $_ = "" unless defined $_;
773 print STDERR "DEBUG:----------------------------\n";
774 print STDERR "DEBUG: \$_ = '$_'\n";
777 ##########################################################
778 # Now that we have a full line, let's process through it
780 unless ($state{in_comment}) {
781 # Begin with checking if the current $normalized_line
782 # contains a preprocessor directive
783 # This is only done if we're not inside a comment and
784 # if it's a preprocessor directive and it's finished.
785 if ($normalized_line =~ m|^#| && $_ eq "") {
786 print STDERR "DEBUG[OPENSSL CPP]: \$normalized_line = '$normalized_line'\n"
788 $opts{debug_type} = "OPENSSL CPP";
789 my @r = ( _run_handlers($normalized_line,
793 # Checking if there are lines to inject.
795 @r = split $/, (pop @r).$_;
796 print STDERR "DEBUG[OPENSSL CPP]: injecting '", join("', '", @r),"'\n"
797 if $opts{debug} && @r;
798 @lines = ( @r, @lines );
803 print STDERR "DEBUG[CPP]: \$normalized_line = '$normalized_line'\n"
805 $opts{debug_type} = "CPP";
806 my @r = ( _run_handlers($normalized_line,
810 if (ref($r[0]) eq "HASH") {
811 push @result, shift @r;
814 # Now, check if there are lines to inject.
815 # Really, this should never happen, it IS a
816 # preprocessor directive after all...
818 @r = split $/, pop @r;
819 print STDERR "DEBUG[CPP]: injecting '", join("', '", @r),"'\n"
820 if $opts{debug} && @r;
821 @lines = ( @r, @lines );
827 # Note: we simply ignore all directives that no
829 $normalized_line = "";
832 # If the two strings end and start with a character that
833 # shouldn't get concatenated, add a space
835 ($collected_stmt =~ m/(?:"|')$/
836 || ($collected_stmt =~ m/(?:\w|\d)$/
837 && $normalized_line =~ m/^(?:\w|\d)/)) ? " " : "";
839 # Now, unless we're building up a preprocessor directive or
840 # are in the middle of a string, or the parens et al aren't
841 # balanced up yet, let's try and see if there's a OpenSSL
842 # or C handler that can make sense of what we have so far.
843 if ( $normalized_line !~ m|^#|
844 && ($collected_stmt ne "" || $normalized_line ne "")
845 && ! @{$state{c_parens}}
846 && ! $state{in_string} ) {
848 print STDERR "DEBUG[OPENSSL C]: \$collected_stmt = '$collected_stmt'\n";
849 print STDERR "DEBUG[OPENSSL C]: \$normalized_line = '$normalized_line'\n";
851 $opts{debug_type} = "OPENSSL C";
852 my @r = ( _run_handlers($collected_stmt
858 # Checking if there are lines to inject.
860 @r = split $/, (pop @r).$_;
861 print STDERR "DEBUG[OPENSSL]: injecting '", join("', '", @r),"'\n"
862 if $opts{debug} && @r;
863 @lines = ( @r, @lines );
867 $normalized_line = "";
868 $collected_stmt = "";
871 print STDERR "DEBUG[C]: \$collected_stmt = '$collected_stmt'\n";
872 print STDERR "DEBUG[C]: \$normalized_line = '$normalized_line'\n";
874 $opts{debug_type} = "C";
875 my @r = ( _run_handlers($collected_stmt
881 if (ref($r[0]) eq "HASH") {
882 push @result, shift @r;
885 # Checking if there are lines to inject.
887 @r = split $/, (pop @r).$_;
888 print STDERR "DEBUG[C]: injecting '", join("', '", @r),"'\n"
889 if $opts{debug} && @r;
890 @lines = ( @r, @lines );
894 $normalized_line = "";
895 $collected_stmt = "";
900 $collected_stmt .= $space.$normalized_line;
901 $normalized_line = "";
910 # Take care of inside string first.
911 if ($state{in_string}) {
912 if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
913 $state{in_string} # Look for matching quote
915 $normalized_line .= $`.$&;
916 $state{in_string} = "";
920 die "Unfinished string without continuation found$opts{PLACE}\n";
923 # ... or inside comments, whichever happens to apply
924 elsif ($state{in_comment}) {
926 # This should never happen
927 die "Something went seriously wrong, multiline //???$opts{PLACE}\n"
928 if ($state{in_comment} eq "//");
930 # A note: comments are simply discarded.
932 if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
933 \*\/ # Look for C comment end
935 $state{in_comment} = "";
937 print STDERR "DEBUG: Found end of comment, followed by '$_'\n"
946 # At this point, it's safe to remove leading whites, but
947 # we need to be careful with some preprocessor lines
952 if ($normalized_line =~ m/^
953 \#define\s\w(?:\w|\d)*(?:<<<\([^\)]*\)>>>)?
956 print STDERR "DEBUG: Processing leading spaces: \$normalized_line = '$normalized_line', \$space = '$space', \$rest = '$rest'\n"
962 $normalized_line =~ m|^#| ? 'cpp_parens' : 'c_parens';
963 (my $paren_singular = $parens) =~ s|s$||;
965 # Now check for specific tokens, and if they are parens,
966 # check them against $state{$parens}. Note that we surround
967 # the outermost parens with extra "<<<" and ">>>". Those
968 # are for the benefit of handlers who to need to detect
969 # them, and they will be removed from the final output.
973 if (!@{$state{$parens}}) {
974 if ("$normalized_line$body" =~ m|^extern "C"\{$|) {
975 $state{in_extern_C} = 1;
976 print STDERR "DEBUG: found start of 'extern \"C\"' ($normalized_line$body)\n"
978 $normalized_line = "";
980 $normalized_line .= "<<<".$body;
983 $normalized_line .= $body;
986 if ($normalized_line ne "") {
987 print STDERR "DEBUG: found $paren_singular start '$body'\n"
989 $body =~ tr|\{\[\(|\}\]\)|;
990 print STDERR "DEBUG: pushing $paren_singular end '$body'\n"
992 push @{$state{$parens}}, $body;
994 } elsif (m|^[\}\]\)]|) {
997 if (!@{$state{$parens}}
998 && $& eq '}' && $state{in_extern_C}) {
999 print STDERR "DEBUG: found end of 'extern \"C\"'\n"
1001 $state{in_extern_C} = 0;
1003 print STDERR "DEBUG: Trying to match '$&' against '"
1004 ,join("', '", @{$state{$parens}})
1007 die "Unmatched parentheses$opts{PLACE}\n"
1008 unless (@{$state{$parens}}
1009 && pop @{$state{$parens}} eq $&);
1010 if (!@{$state{$parens}}) {
1011 $normalized_line .= $&.">>>";
1013 $normalized_line .= $&;
1016 } elsif (m|^["']|) { # string start
1020 # We want to separate strings from \w and \d with one space.
1021 $normalized_line .= " " if $normalized_line =~ m/(\w|\d)$/;
1022 $normalized_line .= $body;
1023 $state{in_string} = $body;
1024 } elsif (m|^\/\*|) { # C style comment
1025 print STDERR "DEBUG: found start of C style comment\n"
1027 $state{in_comment} = $&;
1029 } elsif (m|^\/\/|) { # C++ style comment
1030 print STDERR "DEBUG: found C++ style comment\n"
1032 $_ = ""; # (just discard it entirely)
1033 } elsif (m/^ (?| (?: 0[xX][[:xdigit:]]+ | 0[bB][01]+ | [0-9]+ )
1034 (?i: U | L | UL | LL | ULL )?
1035 | [0-9]+\.[0-9]+(?:[eE][\-\+]\d+)? (?i: F | L)?
1037 print STDERR "DEBUG: Processing numbers: \$normalized_line = '$normalized_line', \$& = '$&', \$' = '$''\n"
1039 $normalized_line .= $&;
1041 } elsif (m/^[[:alpha:]_]\w*/) {
1046 # Now, only add a space if it's needed to separate
1047 # two \w characters, and we also surround strings with
1048 # a space. In this case, that's if $normalized_line ends
1049 # with a \w, \d, " or '.
1051 if ($normalized_line =~ m/("|')$/
1052 || ($normalized_line =~ m/(\w|\d)$/
1053 && $body =~ m/^(\w|\d)/));
1055 print STDERR "DEBUG: Processing words: \$normalized_line = '$normalized_line', \$space = '$space', \$body = '$body', \$rest = '$rest'\n"
1057 $normalized_line .= $space.$body;
1059 } elsif (m|^(?:\\)?.|) { # Catch-all
1060 $normalized_line .= $&;
1066 foreach my $handler (@endhandlers) {
1067 if ($handler->{massager}) {
1068 $handler->{massager}->(\%opts);
1074 # arg1: line to check
1075 # arg2...: handlers to check
1076 # return undef when no handler matched
1079 if (ref($_[$#_]) eq "HASH") {
1086 foreach my $handler (@handlers) {
1087 if ($handler->{regexp}
1088 && $line =~ m|^$handler->{regexp}$|) {
1089 if ($handler->{massager}) {
1091 print STDERR "DEBUG[",$opts{debug_type},"]: Trying to handle '$line'\n";
1092 print STDERR "DEBUG[",$opts{debug_type},"]: (matches /\^",$handler->{regexp},"\$/)\n";
1094 my $saved_line = $line;
1096 map { s/(<<<|>>>)//g; $_ }
1097 $handler->{massager}->($saved_line, \%opts);
1098 print STDERR "DEBUG[",$opts{debug_type},"]: Got back '"
1099 , join("', '", @massaged), "'\n"
1102 # Because we may get back new lines to be
1103 # injected before whatever else that follows,
1104 # and the injected stuff might include
1105 # preprocessor lines, we need to inject them
1106 # in @lines and set $_ to the empty string to
1107 # break out from the inner loops
1108 my $injected_lines = shift @massaged || "";
1113 name => shift @massaged,
1114 type => shift @massaged,
1115 returntype => shift @massaged,
1116 value => shift @massaged,
1117 conds => [ @massaged ]
1122 print STDERR "DEBUG[",$opts{debug_type},"]: (ignore, possible side effects)\n"
1123 if $opts{debug} && $injected_lines eq "";
1124 return (1, $injected_lines);