2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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_COMPAT(\S+)(0x[0-9a-fA-F]{8})L$/,
72 if ($op ne '<' && $op ne '>=') {
73 die "Error: unacceptable operator $op: $_[0]\n";
75 my ($major, $minor, $edit) =
79 my $t = "DEPRECATEDIN_" .
81 ? "${major}_${minor}_${edit}"
83 my $cond = $op eq '<' ? 'ifndef' : 'ifdef';
91 ##################################################################
94 { regexp => qr/#ifdef ?(.*)/,
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/#ifndef ?(.*)/,
110 if (ref($_[$#_]) eq "HASH") {
114 push @preprocessor_conds, [ '!'.$1 ];
115 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
120 { regexp => qr/#if (0|1)/,
123 if (ref($_[$#_]) eq "HASH") {
128 push @preprocessor_conds, [ "TRUE" ];
130 push @preprocessor_conds, [ "!TRUE" ];
132 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
137 { regexp => qr/#if ?(.*)/,
140 if (ref($_[$#_]) eq "HASH") {
146 if ($conds =~ m|^defined<<<\(([^\)]*)\)>>>(.*)$|) {
147 push @results, $1; # Handle the simple case
149 my $re = qr/^(?:\|\|defined<<<\([^\)]*\)>>>)*$/;
150 print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
152 if ($rest =~ m/$re/) {
153 my @rest = split /\|\|/, $rest;
156 m|^defined<<<\(([^\)]*)\)>>>$|;
157 die "Something wrong...$opts{PLACE}" if $1 eq "";
161 $conds =~ s/<<<|>>>//g;
162 warn "Warning: complicated #if expression(1): $conds$opts{PLACE}"
165 } elsif ($conds =~ m|^!defined<<<\(([^\)]*)\)>>>(.*)$|) {
166 push @results, '!'.$1; # Handle the simple case
168 my $re = qr/^(?:\&\&!defined<<<\([^\)]*\)>>>)*$/;
169 print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
171 if ($rest =~ m/$re/) {
172 my @rest = split /\&\&/, $rest;
175 m|^!defined<<<\(([^\)]*)\)>>>$|;
176 die "Something wrong...$opts{PLACE}" if $1 eq "";
177 push @results, '!'.$1;
180 $conds =~ s/<<<|>>>//g;
181 warn "Warning: complicated #if expression(2): $conds$opts{PLACE}"
185 $conds =~ s/<<<|>>>//g;
186 warn "Warning: complicated #if expression(3): $conds$opts{PLACE}"
189 print STDERR "DEBUG[",$opts{debug_type},"]: Added preprocessor conds: '", join("', '", @results), "'\n"
191 push @preprocessor_conds, [ @results ];
192 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
197 { regexp => qr/#elif (.*)/,
200 if (ref($_[$#_]) eq "HASH") {
204 die "An #elif without corresponding condition$opts{PLACE}"
205 if !@preprocessor_conds;
206 pop @preprocessor_conds;
207 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
214 { regexp => qr/#else/,
217 if (ref($_[$#_]) eq "HASH") {
221 die "An #else without corresponding condition$opts{PLACE}"
222 if !@preprocessor_conds;
223 # Invert all conditions on the last level
224 my $stuff = pop @preprocessor_conds;
225 push @preprocessor_conds, [
226 map { m|^!(.*)$| ? $1 : '!'.$_ } @$stuff
228 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
233 { regexp => qr/#endif ?/,
236 if (ref($_[$#_]) eq "HASH") {
240 die "An #endif without corresponding condition$opts{PLACE}"
241 if !@preprocessor_conds;
242 pop @preprocessor_conds;
243 print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
248 { regexp => qr/#define ([[:alpha:]_]\w*)(<<<\(.*?\)>>>)?( (.*))?/,
252 my $spaceval = $3||"";
255 $1, 'M', "", $params ? "$name$params$spaceval" : $val,
259 massager => sub { return (); }
263 my @opensslchandlers = (
264 ##################################################################
267 # They are really preprocessor stuff, but they look like C stuff
268 # to this parser. All of these do replacements, anything else is
272 # Global variable stuff
273 { regexp => qr/OPENSSL_DECLARE_GLOBAL<<<\((.*),(.*)\)>>>;/,
274 massager => sub { return (<<"EOF");
275 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
276 OPENSSL_EXPORT $1 _shadow_$2;
278 $1 *_shadow_$2(void);
285 # Deprecated stuff, by OpenSSL release.
287 # We trick the parser by pretending that the declaration is wrapped in a
288 # check if the DEPRECATEDIN macro is defined or not. Callers of parse()
289 # will have to decide what to do with it.
290 { regexp => qr/(DEPRECATEDIN_\d+(?:_\d+_\d+)?)<<<\((.*)\)>>>/,
291 massager => sub { return (<<"EOF");
302 # LHASH_OF(foo) is used as a type, but the chandlers won't take it
303 # gracefully, so we expand it here.
304 { regexp => qr/(.*)\bLHASH_OF<<<\((.*?)\)>>>(.*)/,
305 massager => sub { return ("$1struct lhash_st_$2$3"); }
307 { regexp => qr/DEFINE_LHASH_OF<<<\((.*)\)>>>/,
310 static ossl_inline LHASH_OF($1) * lh_$1_new(unsigned long (*hfn)(const $1 *),
311 int (*cfn)(const $1 *, const $1 *));
312 static ossl_inline void lh_$1_free(LHASH_OF($1) *lh);
313 static ossl_inline $1 *lh_$1_insert(LHASH_OF($1) *lh, $1 *d);
314 static ossl_inline $1 *lh_$1_delete(LHASH_OF($1) *lh, const $1 *d);
315 static ossl_inline $1 *lh_$1_retrieve(LHASH_OF($1) *lh, const $1 *d);
316 static ossl_inline int lh_$1_error(LHASH_OF($1) *lh);
317 static ossl_inline unsigned long lh_$1_num_items(LHASH_OF($1) *lh);
318 static ossl_inline void lh_$1_node_stats_bio(const LHASH_OF($1) *lh, BIO *out);
319 static ossl_inline void lh_$1_node_usage_stats_bio(const LHASH_OF($1) *lh,
321 static ossl_inline void lh_$1_stats_bio(const LHASH_OF($1) *lh, BIO *out);
322 static ossl_inline unsigned long lh_$1_get_down_load(LHASH_OF($1) *lh);
323 static ossl_inline void lh_$1_set_down_load(LHASH_OF($1) *lh, unsigned long dl);
324 static ossl_inline void lh_$1_doall(LHASH_OF($1) *lh, void (*doall)($1 *));
333 # STACK_OF(foo) is used as a type, but the chandlers won't take it
334 # gracefully, so we expand it here.
335 { regexp => qr/(.*)\bSTACK_OF<<<\((.*?)\)>>>(.*)/,
336 massager => sub { return ("$1struct stack_st_$2$3"); }
338 # { regexp => qr/(.*)\bSTACK_OF\((.*?)\)(.*)/,
341 # my $stack_of = "struct stack_st_$2";
343 # if ($after =~ m|^\w|) { $after = " ".$after; }
344 # return ("$before$stack_of$after");
347 { regexp => qr/SKM_DEFINE_STACK_OF<<<\((.*),(.*),(.*)\)>>>/,
351 typedef int (*sk_$1_compfunc)(const $3 * const *a, const $3 *const *b);
352 typedef void (*sk_$1_freefunc)($3 *a);
353 typedef $3 * (*sk_$1_copyfunc)(const $3 *a);
354 static ossl_inline int sk_$1_num(const STACK_OF($1) *sk);
355 static ossl_inline $2 *sk_$1_value(const STACK_OF($1) *sk, int idx);
356 static ossl_inline STACK_OF($1) *sk_$1_new(sk_$1_compfunc compare);
357 static ossl_inline STACK_OF($1) *sk_$1_new_null(void);
358 static ossl_inline STACK_OF($1) *sk_$1_new_reserve(sk_$1_compfunc compare,
360 static ossl_inline int sk_$1_reserve(STACK_OF($1) *sk, int n);
361 static ossl_inline void sk_$1_free(STACK_OF($1) *sk);
362 static ossl_inline void sk_$1_zero(STACK_OF($1) *sk);
363 static ossl_inline $2 *sk_$1_delete(STACK_OF($1) *sk, int i);
364 static ossl_inline $2 *sk_$1_delete_ptr(STACK_OF($1) *sk, $2 *ptr);
365 static ossl_inline int sk_$1_push(STACK_OF($1) *sk, $2 *ptr);
366 static ossl_inline int sk_$1_unshift(STACK_OF($1) *sk, $2 *ptr);
367 static ossl_inline $2 *sk_$1_pop(STACK_OF($1) *sk);
368 static ossl_inline $2 *sk_$1_shift(STACK_OF($1) *sk);
369 static ossl_inline void sk_$1_pop_free(STACK_OF($1) *sk,
370 sk_$1_freefunc freefunc);
371 static ossl_inline int sk_$1_insert(STACK_OF($1) *sk, $2 *ptr, int idx);
372 static ossl_inline $2 *sk_$1_set(STACK_OF($1) *sk, int idx, $2 *ptr);
373 static ossl_inline int sk_$1_find(STACK_OF($1) *sk, $2 *ptr);
374 static ossl_inline int sk_$1_find_ex(STACK_OF($1) *sk, $2 *ptr);
375 static ossl_inline void sk_$1_sort(STACK_OF($1) *sk);
376 static ossl_inline int sk_$1_is_sorted(const STACK_OF($1) *sk);
377 static ossl_inline STACK_OF($1) * sk_$1_dup(const STACK_OF($1) *sk);
378 static ossl_inline STACK_OF($1) *sk_$1_deep_copy(const STACK_OF($1) *sk,
379 sk_$1_copyfunc copyfunc,
380 sk_$1_freefunc freefunc);
381 static ossl_inline sk_$1_compfunc sk_$1_set_cmp_func(STACK_OF($1) *sk,
382 sk_$1_compfunc compare);
386 { regexp => qr/DEFINE_SPECIAL_STACK_OF<<<\((.*),(.*)\)>>>/,
387 massager => sub { return ("SKM_DEFINE_STACK_OF($1,$2,$2)"); },
389 { regexp => qr/DEFINE_STACK_OF<<<\((.*)\)>>>/,
390 massager => sub { return ("SKM_DEFINE_STACK_OF($1,$1,$1)"); },
392 { regexp => qr/DEFINE_SPECIAL_STACK_OF_CONST<<<\((.*),(.*)\)>>>/,
393 massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $2,$2)"); },
395 { regexp => qr/DEFINE_STACK_OF_CONST<<<\((.*)\)>>>/,
396 massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $1,$1)"); },
398 { regexp => qr/PREDECLARE_STACK_OF<<<\((.*)\)>>>/,
399 massager => sub { return ("STACK_OF($1);"); }
401 { regexp => qr/DECLARE_STACK_OF<<<\((.*)\)>>>/,
402 massager => sub { return ("STACK_OF($1);"); }
404 { regexp => qr/DECLARE_SPECIAL_STACK_OF<<<\((.*?),(.*?)\)>>>/,
405 massager => sub { return ("STACK_OF($1);"); }
411 { regexp => qr/TYPEDEF_D2I_OF<<<\((.*)\)>>>/,
413 return ("typedef $1 *d2i_of_$1($1 **,const unsigned char **,long)");
416 { regexp => qr/TYPEDEF_I2D_OF<<<\((.*)\)>>>/,
418 return ("typedef $1 *i2d_of_$1($1 *,unsigned char **)");
421 { regexp => qr/TYPEDEF_D2I2D_OF<<<\((.*)\)>>>/,
423 return ("TYPEDEF_D2I_OF($1); TYPEDEF_I2D_OF($1)");
426 { regexp => qr/DECLARE_ASN1_ITEM<<<\((.*)\)>>>/,
429 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
430 OPENSSL_EXTERN const ASN1_ITEM *$1_it;
432 const ASN1_ITEM *$1_it(void);
437 { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS<<<\((.*),(.*),(.*)\)>>>/,
442 DECLARE_ASN1_ITEM($2)
446 { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS_const<<<\((.*),(.*)\)>>>/,
451 DECLARE_ASN1_ITEM($2)
455 { regexp => qr/DECLARE_ASN1_ALLOC_FUNCTIONS<<<\((.*)\)>>>/,
463 { regexp => qr/DECLARE_ASN1_FUNCTIONS_name<<<\((.*),(.*)\)>>>/,
470 DECLARE_ASN1_ITEM($2)
474 { regexp => qr/DECLARE_ASN1_FUNCTIONS_fname<<<\((.*),(.*),(.*)\)>>>/,
475 massager => sub { return (<<"EOF");
480 DECLARE_ASN1_ITEM($2)
484 { regexp => qr/DECLARE_ASN1_FUNCTIONS(?:_const)?<<<\((.*)\)>>>/,
485 massager => sub { return (<<"EOF");
490 DECLARE_ASN1_ITEM($1)
494 { regexp => qr/DECLARE_ASN1_NDEF_FUNCTION<<<\((.*)\)>>>/,
497 int i2d_$1_NDEF(void);
501 { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION<<<\((.*)\)>>>/,
504 int $1_print_ctx(void);
508 { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION_name<<<\((.*),(.*)\)>>>/,
511 int $2_print_ctx(void);
515 { regexp => qr/DECLARE_ASN1_SET_OF<<<\((.*)\)>>>/,
516 massager => sub { return (); }
518 { regexp => qr/DECLARE_PKCS12_SET_OF<<<\((.*)\)>>>/,
519 massager => sub { return (); }
521 { regexp => qr/DECLARE_PEM(?|_rw|_rw_cb|_rw_const)<<<\((.*?),.*\)>>>/,
522 massager => sub { return (<<"EOF");
523 #ifndef OPENSSL_NO_STDIO
524 int PEM_read_$1(void);
525 int PEM_write_$1(void);
527 int PEM_read_bio_$1(void);
528 int PEM_write_bio_$1(void);
535 { regexp => qr/DECLARE_PEM(?|_write|_write_cb|_write_const)<<<\((.*?),.*\)>>>/,
536 massager => sub { return (<<"EOF");
537 #ifndef OPENSSL_NO_STDIO
538 int PEM_write_$1(void);
540 int PEM_write_bio_$1(void);
544 { regexp => qr/DECLARE_PEM(?|_read|_read_cb)<<<\((.*?),.*\)>>>/,
545 massager => sub { return (<<"EOF");
546 #ifndef OPENSSL_NO_STDIO
547 int PEM_read_$1(void);
549 int PEM_read_bio_$1(void);
554 # Spurious stuff found in the OpenSSL headers
555 # Usually, these are just macros that expand to, well, something
556 { regexp => qr/__NDK_FPABI__/,
557 massager => sub { return (); }
564 ##################################################################
567 # extern "C" of individual items
568 # Note that the main parse function has a special hack for 'extern "C" {'
569 # which can't be done in handlers
570 # We simply ignore it.
571 { regexp => qr/extern "C" (.*;)/,
572 massager => sub { return ($1); },
574 # union, struct and enum definitions
575 # Because this one might appear a little everywhere within type
576 # definitions, we take it out and replace it with just
577 # 'union|struct|enum name' while registering it.
578 # This makes use of the parser trick to surround the outer braces
580 { regexp => qr/(.*) # Anything before ($1)
581 \b # word to non-word boundary
582 (union|struct|enum) # The word used ($2)
583 (?:\s([[:alpha:]_]\w*))? # Struct or enum name ($3)
584 <<<(\{.*?\})>>> # Struct or enum definition ($4)
585 (.*) # Anything after ($5)
592 || sprintf("__anon%03d", ++$anoncnt); # Anonymous struct
595 my $type = $word eq "struct" ? 'S' : 'E';
596 if ($before ne "" || $after ne ";") {
597 if ($after =~ m|^\w|) { $after = " ".$after; }
598 return ("$before$word $name$after;",
599 "$word $name", $type, "", "$word$definition", all_conds());
601 # If there was no before nor after, make the return much simple
602 return ("", "$word $name", $type, "", "$word$definition", all_conds());
605 # Named struct and enum forward declarations
606 # We really just ignore them, but we need to parse them or the variable
607 # declaration handler further down will think it's a variable declaration.
608 { regexp => qr/^(union|struct|enum) ([[:alpha:]_]\w*);/,
609 massager => sub { return (); }
611 # Function returning function pointer declaration
612 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
613 ((?:\w|\*|\s)*?) # Return type ($2)
616 ([[:alpha:]_]\w*) # Function name ($3)
617 (\(.*\)) # Parameters ($4)
619 <<<(\(.*\))>>> # F.p. parameters ($5)
623 return ("", $3, 'F', "", "$2(*$4)$5", all_conds())
625 return ("", $3, 'F', "$2(*)$5", "$2(*$4)$5", all_conds()); }
627 # Function pointer declaration, or typedef thereof
628 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
629 ((?:\w|\*|\s)*?) # Return type ($2)
630 <<<\(\*([[:alpha:]_]\w*)\)>>> # T.d. or var name ($3)
631 <<<(\(.*\))>>> # F.p. parameters ($4)
635 return ("", $3, 'T', "", "$2(*)$4", all_conds())
637 return ("", $3, 'V', "$2(*)$4", "$2(*)$4", all_conds());
640 # Function declaration, or typedef thereof
641 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
642 ((?:\w|\*|\s)*?) # Return type ($2)
644 ([[:alpha:]_]\w*) # Function name ($3)
645 <<<(\(.*\))>>> # Parameters ($4)
649 return ("", $3, 'T', "", "$2$4", all_conds())
651 return ("", $3, 'F', $2, "$2$4", all_conds());
654 # Variable declaration, including arrays, or typedef thereof
655 { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
656 ((?:\w|\*|\s)*?) # Type ($2)
658 ([[:alpha:]_]\w*) # Variable name ($3)
659 ((?:<<<\[[^\]]*\]>>>)*) # Possible array declaration ($4)
663 return ("", $3, 'T', "", $2.($4||""), all_conds())
665 return ("", $3, 'V', $2.($4||""), $2.($4||""), all_conds());
670 # End handlers are almost the same as handlers, except they are run through
671 # ONCE when the input has been parsed through. These are used to check for
672 # remaining stuff, such as an unfinished #ifdef and stuff like that that the
673 # main parser can't check on its own.
678 die "Unfinished preprocessor conditions levels: ",scalar(@preprocessor_conds),($opts{filename} ? " in file ".$opts{filename}: ""),$opts{PLACE}
679 if @preprocessor_conds;
684 # takes a list of strings that can each contain one or several lines of code
685 # also takes a hash of options as last argument.
687 # returns a list of hashes with information:
689 # name name of the thing
690 # type type, see the massage handler function
691 # returntype return type of functions and variables
692 # value value for macros, signature for functions, variables
694 # conds preprocessor conditions (array ref)
698 if (ref($_[$#_]) eq "HASH") {
703 in_extern_C => 0, # An exception to parenthesis processing.
704 cpp_parens => [], # A list of ending parens and braces found in
705 # preprocessor directives
706 c_parens => [], # A list of ending parens and braces found in
708 in_string => "", # empty string when outside a string, otherwise
709 # "'" or '"' depending on the starting quote.
710 in_comment => "", # empty string when outside a comment, otherwise
711 # "/*" or "//" depending on the type of comment
712 # found. The latter will never be multiline
713 # NOTE: in_string and in_comment will never be
714 # true (in perl semantics) at the same time.
718 my $normalized_line = ""; # $input_line, but normalized. In essence, this
719 # means that ALL whitespace is removed unless
720 # it absolutely has to be present, and in that
721 # case, there's only one space.
722 # The cases where a space needs to stay present
725 # 2. between words and number
726 # 3. after the first word of a preprocessor
728 # 4. for the #define directive, between the macro
729 # name/args and its value, so we end up with:
731 # #define BAR(x) something(x)
732 my $collected_stmt = ""; # Where we're building up a C line until it's a
733 # complete definition/declaration, as determined
734 # by any handler being capable of matching it.
736 # We use $_ shamelessly when looking through @lines.
737 # In case we find a \ at the end, we keep filling it up with more lines.
740 foreach my $line (@_) {
741 # split tries to be smart when a string ends with the thing we split on
742 $line .= "\n" unless $line =~ m|\R$|;
745 # We use ¦undef¦ as a marker for a new line from the file.
746 # Since we convert one line to several and unshift that into @lines,
747 # that's the only safe way we have to track the original lines
748 my @lines = map { ( undef, $_ ) } split $/, $line;
750 # Remember that extra # we added above? Now we remove it
752 pop @lines; # Don't forget the undef
755 if (!defined($lines[0])) {
757 $state{current_line}++;
759 $opts{PLACE} = " at ".$opts{filename}." line ".$state{current_line}."\n";
760 $opts{PLACE2} = $opts{filename}.":".$state{current_line};
765 $_ = "" unless defined $_;
774 print STDERR "DEBUG:----------------------------\n";
775 print STDERR "DEBUG: \$_ = '$_'\n";
778 ##########################################################
779 # Now that we have a full line, let's process through it
781 unless ($state{in_comment}) {
782 # Begin with checking if the current $normalized_line
783 # contains a preprocessor directive
784 # This is only done if we're not inside a comment and
785 # if it's a preprocessor directive and it's finished.
786 if ($normalized_line =~ m|^#| && $_ eq "") {
787 print STDERR "DEBUG[OPENSSL CPP]: \$normalized_line = '$normalized_line'\n"
789 $opts{debug_type} = "OPENSSL CPP";
790 my @r = ( _run_handlers($normalized_line,
794 # Checking if there are lines to inject.
796 @r = split $/, (pop @r).$_;
797 print STDERR "DEBUG[OPENSSL CPP]: injecting '", join("', '", @r),"'\n"
798 if $opts{debug} && @r;
799 @lines = ( @r, @lines );
804 print STDERR "DEBUG[CPP]: \$normalized_line = '$normalized_line'\n"
806 $opts{debug_type} = "CPP";
807 my @r = ( _run_handlers($normalized_line,
811 if (ref($r[0]) eq "HASH") {
812 push @result, shift @r;
815 # Now, check if there are lines to inject.
816 # Really, this should never happen, it IS a
817 # preprocessor directive after all...
819 @r = split $/, pop @r;
820 print STDERR "DEBUG[CPP]: injecting '", join("', '", @r),"'\n"
821 if $opts{debug} && @r;
822 @lines = ( @r, @lines );
828 # Note: we simply ignore all directives that no
830 $normalized_line = "";
833 # If the two strings end and start with a character that
834 # shouldn't get concatenated, add a space
836 ($collected_stmt =~ m/(?:"|')$/
837 || ($collected_stmt =~ m/(?:\w|\d)$/
838 && $normalized_line =~ m/^(?:\w|\d)/)) ? " " : "";
840 # Now, unless we're building up a preprocessor directive or
841 # are in the middle of a string, or the parens et al aren't
842 # balanced up yet, let's try and see if there's a OpenSSL
843 # or C handler that can make sense of what we have so far.
844 if ( $normalized_line !~ m|^#|
845 && ($collected_stmt ne "" || $normalized_line ne "")
846 && ! @{$state{c_parens}}
847 && ! $state{in_string} ) {
849 print STDERR "DEBUG[OPENSSL C]: \$collected_stmt = '$collected_stmt'\n";
850 print STDERR "DEBUG[OPENSSL C]: \$normalized_line = '$normalized_line'\n";
852 $opts{debug_type} = "OPENSSL C";
853 my @r = ( _run_handlers($collected_stmt
859 # Checking if there are lines to inject.
861 @r = split $/, (pop @r).$_;
862 print STDERR "DEBUG[OPENSSL]: injecting '", join("', '", @r),"'\n"
863 if $opts{debug} && @r;
864 @lines = ( @r, @lines );
868 $normalized_line = "";
869 $collected_stmt = "";
872 print STDERR "DEBUG[C]: \$collected_stmt = '$collected_stmt'\n";
873 print STDERR "DEBUG[C]: \$normalized_line = '$normalized_line'\n";
875 $opts{debug_type} = "C";
876 my @r = ( _run_handlers($collected_stmt
882 if (ref($r[0]) eq "HASH") {
883 push @result, shift @r;
886 # Checking if there are lines to inject.
888 @r = split $/, (pop @r).$_;
889 print STDERR "DEBUG[C]: injecting '", join("', '", @r),"'\n"
890 if $opts{debug} && @r;
891 @lines = ( @r, @lines );
895 $normalized_line = "";
896 $collected_stmt = "";
901 $collected_stmt .= $space.$normalized_line;
902 $normalized_line = "";
911 # Take care of inside string first.
912 if ($state{in_string}) {
913 if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
914 $state{in_string} # Look for matching quote
916 $normalized_line .= $`.$&;
917 $state{in_string} = "";
921 die "Unfinished string without continuation found$opts{PLACE}\n";
924 # ... or inside comments, whichever happens to apply
925 elsif ($state{in_comment}) {
927 # This should never happen
928 die "Something went seriously wrong, multiline //???$opts{PLACE}\n"
929 if ($state{in_comment} eq "//");
931 # A note: comments are simply discarded.
933 if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
934 \*\/ # Look for C comment end
936 $state{in_comment} = "";
938 print STDERR "DEBUG: Found end of comment, followed by '$_'\n"
947 # At this point, it's safe to remove leading whites, but
948 # we need to be careful with some preprocessor lines
953 if ($normalized_line =~ m/^
954 \#define\s\w(?:\w|\d)*(?:<<<\([^\)]*\)>>>)?
957 print STDERR "DEBUG: Processing leading spaces: \$normalized_line = '$normalized_line', \$space = '$space', \$rest = '$rest'\n"
963 $normalized_line =~ m|^#| ? 'cpp_parens' : 'c_parens';
964 (my $paren_singular = $parens) =~ s|s$||;
966 # Now check for specific tokens, and if they are parens,
967 # check them against $state{$parens}. Note that we surround
968 # the outermost parens with extra "<<<" and ">>>". Those
969 # are for the benefit of handlers who to need to detect
970 # them, and they will be removed from the final output.
974 if (!@{$state{$parens}}) {
975 if ("$normalized_line$body" =~ m|^extern "C"\{$|) {
976 $state{in_extern_C} = 1;
977 print STDERR "DEBUG: found start of 'extern \"C\"' ($normalized_line$body)\n"
979 $normalized_line = "";
981 $normalized_line .= "<<<".$body;
984 $normalized_line .= $body;
987 if ($normalized_line ne "") {
988 print STDERR "DEBUG: found $paren_singular start '$body'\n"
990 $body =~ tr|\{\[\(|\}\]\)|;
991 print STDERR "DEBUG: pushing $paren_singular end '$body'\n"
993 push @{$state{$parens}}, $body;
995 } elsif (m|^[\}\]\)]|) {
998 if (!@{$state{$parens}}
999 && $& eq '}' && $state{in_extern_C}) {
1000 print STDERR "DEBUG: found end of 'extern \"C\"'\n"
1002 $state{in_extern_C} = 0;
1004 print STDERR "DEBUG: Trying to match '$&' against '"
1005 ,join("', '", @{$state{$parens}})
1008 die "Unmatched parentheses$opts{PLACE}\n"
1009 unless (@{$state{$parens}}
1010 && pop @{$state{$parens}} eq $&);
1011 if (!@{$state{$parens}}) {
1012 $normalized_line .= $&.">>>";
1014 $normalized_line .= $&;
1017 } elsif (m|^["']|) { # string start
1021 # We want to separate strings from \w and \d with one space.
1022 $normalized_line .= " " if $normalized_line =~ m/(\w|\d)$/;
1023 $normalized_line .= $body;
1024 $state{in_string} = $body;
1025 } elsif (m|^\/\*|) { # C style comment
1026 print STDERR "DEBUG: found start of C style comment\n"
1028 $state{in_comment} = $&;
1030 } elsif (m|^\/\/|) { # C++ style comment
1031 print STDERR "DEBUG: found C++ style comment\n"
1033 $_ = ""; # (just discard it entirely)
1034 } elsif (m/^ (?| (?: 0[xX][[:xdigit:]]+ | 0[bB][01]+ | [0-9]+ )
1035 (?i: U | L | UL | LL | ULL )?
1036 | [0-9]+\.[0-9]+(?:[eE][\-\+]\d+)? (?i: F | L)?
1038 print STDERR "DEBUG: Processing numbers: \$normalized_line = '$normalized_line', \$& = '$&', \$' = '$''\n"
1040 $normalized_line .= $&;
1042 } elsif (m/^[[:alpha:]_]\w*/) {
1047 # Now, only add a space if it's needed to separate
1048 # two \w characters, and we also surround strings with
1049 # a space. In this case, that's if $normalized_line ends
1050 # with a \w, \d, " or '.
1052 if ($normalized_line =~ m/("|')$/
1053 || ($normalized_line =~ m/(\w|\d)$/
1054 && $body =~ m/^(\w|\d)/));
1056 print STDERR "DEBUG: Processing words: \$normalized_line = '$normalized_line', \$space = '$space', \$body = '$body', \$rest = '$rest'\n"
1058 $normalized_line .= $space.$body;
1060 } elsif (m|^(?:\\)?.|) { # Catch-all
1061 $normalized_line .= $&;
1067 foreach my $handler (@endhandlers) {
1068 if ($handler->{massager}) {
1069 $handler->{massager}->(\%opts);
1075 # arg1: line to check
1076 # arg2...: handlers to check
1077 # return undef when no handler matched
1080 if (ref($_[$#_]) eq "HASH") {
1087 foreach my $handler (@handlers) {
1088 if ($handler->{regexp}
1089 && $line =~ m|^$handler->{regexp}$|) {
1090 if ($handler->{massager}) {
1092 print STDERR "DEBUG[",$opts{debug_type},"]: Trying to handle '$line'\n";
1093 print STDERR "DEBUG[",$opts{debug_type},"]: (matches /\^",$handler->{regexp},"\$/)\n";
1095 my $saved_line = $line;
1097 map { s/(<<<|>>>)//g; $_ }
1098 $handler->{massager}->($saved_line, \%opts);
1099 print STDERR "DEBUG[",$opts{debug_type},"]: Got back '"
1100 , join("', '", @massaged), "'\n"
1103 # Because we may get back new lines to be
1104 # injected before whatever else that follows,
1105 # and the injected stuff might include
1106 # preprocessor lines, we need to inject them
1107 # in @lines and set $_ to the empty string to
1108 # break out from the inner loops
1109 my $injected_lines = shift @massaged || "";
1114 name => shift @massaged,
1115 type => shift @massaged,
1116 returntype => shift @massaged,
1117 value => shift @massaged,
1118 conds => [ @massaged ]
1123 print STDERR "DEBUG[",$opts{debug_type},"]: (ignore, possible side effects)\n"
1124 if $opts{debug} && $injected_lines eq "";
1125 return (1, $injected_lines);