ParseC.pm 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. #! /usr/bin/env perl
  2. # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  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
  8. package OpenSSL::ParseC;
  9. use strict;
  10. use warnings;
  11. use Exporter;
  12. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  13. $VERSION = "0.9";
  14. @ISA = qw(Exporter);
  15. @EXPORT = qw(parse);
  16. # Global handler data
  17. my @preprocessor_conds; # A list of simple preprocessor conditions,
  18. # each item being a list of macros defined
  19. # or not defined.
  20. # Handler helpers
  21. sub all_conds {
  22. return map { ( @$_ ) } @preprocessor_conds;
  23. }
  24. # A list of handlers that will look at a "complete" string and try to
  25. # figure out what to make of it.
  26. # Each handler is a hash with the following keys:
  27. #
  28. # regexp a regexp to compare the "complete" string with.
  29. # checker a function that does a more complex comparison.
  30. # Use this instead of regexp if that isn't enough.
  31. # massager massages the "complete" string into an array with
  32. # the following elements:
  33. #
  34. # [0] String that needs further processing (this
  35. # applies to typedefs of structs), or empty.
  36. # [1] The name of what was found.
  37. # [2] A character that denotes what type of thing
  38. # this is: 'F' for function, 'S' for struct,
  39. # 'T' for typedef, 'M' for macro, 'V' for
  40. # variable.
  41. # [3] Return type (only for type 'F' and 'V')
  42. # [4] Value (for type 'M') or signature (for type 'F',
  43. # 'V', 'T' or 'S')
  44. # [5...] The list of preprocessor conditions this is
  45. # found in, as in checks for macro definitions
  46. # (stored as the macro's name) or the absence
  47. # of definition (stored as the macro's name
  48. # prefixed with a '!'
  49. #
  50. # If the massager returns an empty list, it means the
  51. # "complete" string has side effects but should otherwise
  52. # be ignored.
  53. # If the massager is undefined, the "complete" string
  54. # should be ignored.
  55. my @opensslcpphandlers = (
  56. ##################################################################
  57. # OpenSSL CPP specials
  58. #
  59. # These are used to convert certain pre-precessor expressions into
  60. # others that @cpphandlers have a better chance to understand.
  61. { regexp => qr/#if (!?)OPENSSL_API_([0-9_]+)$/,
  62. massager => sub {
  63. my $cnd = $1 eq '!' ? 'ndef' : 'def';
  64. return (<<"EOF");
  65. #if$cnd DEPRECATEDIN_$2
  66. EOF
  67. }
  68. }
  69. );
  70. my @cpphandlers = (
  71. ##################################################################
  72. # CPP stuff
  73. { regexp => qr/#ifdef ?(.*)/,
  74. massager => sub {
  75. my %opts;
  76. if (ref($_[$#_]) eq "HASH") {
  77. %opts = %{$_[$#_]};
  78. pop @_;
  79. }
  80. push @preprocessor_conds, [ $1 ];
  81. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  82. if $opts{debug};
  83. return ();
  84. },
  85. },
  86. { regexp => qr/#ifndef ?(.*)/,
  87. massager => sub {
  88. my %opts;
  89. if (ref($_[$#_]) eq "HASH") {
  90. %opts = %{$_[$#_]};
  91. pop @_;
  92. }
  93. push @preprocessor_conds, [ '!'.$1 ];
  94. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  95. if $opts{debug};
  96. return ();
  97. },
  98. },
  99. { regexp => qr/#if (0|1)/,
  100. massager => sub {
  101. my %opts;
  102. if (ref($_[$#_]) eq "HASH") {
  103. %opts = %{$_[$#_]};
  104. pop @_;
  105. }
  106. if ($1 eq "1") {
  107. push @preprocessor_conds, [ "TRUE" ];
  108. } else {
  109. push @preprocessor_conds, [ "!TRUE" ];
  110. }
  111. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  112. if $opts{debug};
  113. return ();
  114. },
  115. },
  116. { regexp => qr/#if ?(.*)/,
  117. massager => sub {
  118. my %opts;
  119. if (ref($_[$#_]) eq "HASH") {
  120. %opts = %{$_[$#_]};
  121. pop @_;
  122. }
  123. my @results = ();
  124. my $conds = $1;
  125. if ($conds =~ m|^defined<<<\(([^\)]*)\)>>>(.*)$|) {
  126. push @results, $1; # Handle the simple case
  127. my $rest = $2;
  128. my $re = qr/^(?:\|\|defined<<<\([^\)]*\)>>>)*$/;
  129. print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
  130. if $opts{debug};
  131. if ($rest =~ m/$re/) {
  132. my @rest = split /\|\|/, $rest;
  133. shift @rest;
  134. foreach (@rest) {
  135. m|^defined<<<\(([^\)]*)\)>>>$|;
  136. die "Something wrong...$opts{PLACE}" if $1 eq "";
  137. push @results, $1;
  138. }
  139. } else {
  140. $conds =~ s/<<<|>>>//g;
  141. warn "Warning: complicated #if expression(1): $conds$opts{PLACE}"
  142. if $opts{warnings};
  143. }
  144. } elsif ($conds =~ m|^!defined<<<\(([^\)]*)\)>>>(.*)$|) {
  145. push @results, '!'.$1; # Handle the simple case
  146. my $rest = $2;
  147. my $re = qr/^(?:\&\&!defined<<<\([^\)]*\)>>>)*$/;
  148. print STDERR "DEBUG[",$opts{debug_type},"]: Matching '$rest' with '$re'\n"
  149. if $opts{debug};
  150. if ($rest =~ m/$re/) {
  151. my @rest = split /\&\&/, $rest;
  152. shift @rest;
  153. foreach (@rest) {
  154. m|^!defined<<<\(([^\)]*)\)>>>$|;
  155. die "Something wrong...$opts{PLACE}" if $1 eq "";
  156. push @results, '!'.$1;
  157. }
  158. } else {
  159. $conds =~ s/<<<|>>>//g;
  160. warn "Warning: complicated #if expression(2): $conds$opts{PLACE}"
  161. if $opts{warnings};
  162. }
  163. } else {
  164. $conds =~ s/<<<|>>>//g;
  165. warn "Warning: complicated #if expression(3): $conds$opts{PLACE}"
  166. if $opts{warnings};
  167. }
  168. print STDERR "DEBUG[",$opts{debug_type},"]: Added preprocessor conds: '", join("', '", @results), "'\n"
  169. if $opts{debug};
  170. push @preprocessor_conds, [ @results ];
  171. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  172. if $opts{debug};
  173. return ();
  174. },
  175. },
  176. { regexp => qr/#elif (.*)/,
  177. massager => sub {
  178. my %opts;
  179. if (ref($_[$#_]) eq "HASH") {
  180. %opts = %{$_[$#_]};
  181. pop @_;
  182. }
  183. die "An #elif without corresponding condition$opts{PLACE}"
  184. if !@preprocessor_conds;
  185. pop @preprocessor_conds;
  186. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  187. if $opts{debug};
  188. return (<<"EOF");
  189. #if $1
  190. EOF
  191. },
  192. },
  193. { regexp => qr/#else/,
  194. massager => sub {
  195. my %opts;
  196. if (ref($_[$#_]) eq "HASH") {
  197. %opts = %{$_[$#_]};
  198. pop @_;
  199. }
  200. die "An #else without corresponding condition$opts{PLACE}"
  201. if !@preprocessor_conds;
  202. # Invert all conditions on the last level
  203. my $stuff = pop @preprocessor_conds;
  204. push @preprocessor_conds, [
  205. map { m|^!(.*)$| ? $1 : '!'.$_ } @$stuff
  206. ];
  207. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  208. if $opts{debug};
  209. return ();
  210. },
  211. },
  212. { regexp => qr/#endif ?/,
  213. massager => sub {
  214. my %opts;
  215. if (ref($_[$#_]) eq "HASH") {
  216. %opts = %{$_[$#_]};
  217. pop @_;
  218. }
  219. die "An #endif without corresponding condition$opts{PLACE}"
  220. if !@preprocessor_conds;
  221. pop @preprocessor_conds;
  222. print STDERR "DEBUG[",$opts{debug_type},"]: preprocessor level: ", scalar(@preprocessor_conds), "\n"
  223. if $opts{debug};
  224. return ();
  225. },
  226. },
  227. { regexp => qr/#define ([[:alpha:]_]\w*)(<<<\(.*?\)>>>)?( (.*))?/,
  228. massager => sub {
  229. my $name = $1;
  230. my $params = $2;
  231. my $spaceval = $3||"";
  232. my $val = $4||"";
  233. return ("",
  234. $1, 'M', "", $params ? "$name$params$spaceval" : $val,
  235. all_conds()); }
  236. },
  237. { regexp => qr/#.*/,
  238. massager => sub { return (); }
  239. },
  240. );
  241. my @opensslchandlers = (
  242. ##################################################################
  243. # OpenSSL C specials
  244. #
  245. # They are really preprocessor stuff, but they look like C stuff
  246. # to this parser. All of these do replacements, anything else is
  247. # an error.
  248. #####
  249. # Global variable stuff
  250. { regexp => qr/OPENSSL_DECLARE_GLOBAL<<<\((.*),\s*(.*)\)>>>;/,
  251. massager => sub { return (<<"EOF");
  252. #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
  253. OPENSSL_EXPORT $1 _shadow_$2;
  254. #else
  255. $1 *_shadow_$2(void);
  256. #endif
  257. EOF
  258. },
  259. },
  260. #####
  261. # Deprecated stuff, by OpenSSL release.
  262. # We trick the parser by pretending that the declaration is wrapped in a
  263. # check if the DEPRECATEDIN macro is defined or not. Callers of parse()
  264. # will have to decide what to do with it.
  265. { regexp => qr/(DEPRECATEDIN_\d+(?:_\d+_\d+)?)<<<\((.*)\)>>>/,
  266. massager => sub { return (<<"EOF");
  267. #ifndef $1
  268. $2;
  269. #endif
  270. EOF
  271. },
  272. },
  273. #####
  274. # LHASH stuff
  275. # LHASH_OF(foo) is used as a type, but the chandlers won't take it
  276. # gracefully, so we expand it here.
  277. { regexp => qr/(.*)\bLHASH_OF<<<\((.*?)\)>>>(.*)/,
  278. massager => sub { return ("$1struct lhash_st_$2$3"); }
  279. },
  280. { regexp => qr/DEFINE_LHASH_OF<<<\((.*)\)>>>/,
  281. massager => sub {
  282. return (<<"EOF");
  283. static ossl_inline LHASH_OF($1) * lh_$1_new(unsigned long (*hfn)(const $1 *),
  284. int (*cfn)(const $1 *, const $1 *));
  285. static ossl_inline void lh_$1_free(LHASH_OF($1) *lh);
  286. static ossl_inline $1 *lh_$1_insert(LHASH_OF($1) *lh, $1 *d);
  287. static ossl_inline $1 *lh_$1_delete(LHASH_OF($1) *lh, const $1 *d);
  288. static ossl_inline $1 *lh_$1_retrieve(LHASH_OF($1) *lh, const $1 *d);
  289. static ossl_inline int lh_$1_error(LHASH_OF($1) *lh);
  290. static ossl_inline unsigned long lh_$1_num_items(LHASH_OF($1) *lh);
  291. static ossl_inline void lh_$1_node_stats_bio(const LHASH_OF($1) *lh, BIO *out);
  292. static ossl_inline void lh_$1_node_usage_stats_bio(const LHASH_OF($1) *lh,
  293. BIO *out);
  294. static ossl_inline void lh_$1_stats_bio(const LHASH_OF($1) *lh, BIO *out);
  295. static ossl_inline unsigned long lh_$1_get_down_load(LHASH_OF($1) *lh);
  296. static ossl_inline void lh_$1_set_down_load(LHASH_OF($1) *lh, unsigned long dl);
  297. static ossl_inline void lh_$1_doall(LHASH_OF($1) *lh, void (*doall)($1 *));
  298. LHASH_OF($1)
  299. EOF
  300. }
  301. },
  302. #####
  303. # STACK stuff
  304. # STACK_OF(foo) is used as a type, but the chandlers won't take it
  305. # gracefully, so we expand it here.
  306. { regexp => qr/(.*)\bSTACK_OF<<<\((.*?)\)>>>(.*)/,
  307. massager => sub { return ("$1struct stack_st_$2$3"); }
  308. },
  309. # { regexp => qr/(.*)\bSTACK_OF\((.*?)\)(.*)/,
  310. # massager => sub {
  311. # my $before = $1;
  312. # my $stack_of = "struct stack_st_$2";
  313. # my $after = $3;
  314. # if ($after =~ m|^\w|) { $after = " ".$after; }
  315. # return ("$before$stack_of$after");
  316. # }
  317. # },
  318. { regexp => qr/SKM_DEFINE_STACK_OF<<<\((.*),\s*(.*),\s*(.*)\)>>>/,
  319. massager => sub {
  320. return (<<"EOF");
  321. STACK_OF($1);
  322. typedef int (*sk_$1_compfunc)(const $3 * const *a, const $3 *const *b);
  323. typedef void (*sk_$1_freefunc)($3 *a);
  324. typedef $3 * (*sk_$1_copyfunc)(const $3 *a);
  325. static ossl_inline int sk_$1_num(const STACK_OF($1) *sk);
  326. static ossl_inline $2 *sk_$1_value(const STACK_OF($1) *sk, int idx);
  327. static ossl_inline STACK_OF($1) *sk_$1_new(sk_$1_compfunc compare);
  328. static ossl_inline STACK_OF($1) *sk_$1_new_null(void);
  329. static ossl_inline STACK_OF($1) *sk_$1_new_reserve(sk_$1_compfunc compare,
  330. int n);
  331. static ossl_inline int sk_$1_reserve(STACK_OF($1) *sk, int n);
  332. static ossl_inline void sk_$1_free(STACK_OF($1) *sk);
  333. static ossl_inline void sk_$1_zero(STACK_OF($1) *sk);
  334. static ossl_inline $2 *sk_$1_delete(STACK_OF($1) *sk, int i);
  335. static ossl_inline $2 *sk_$1_delete_ptr(STACK_OF($1) *sk, $2 *ptr);
  336. static ossl_inline int sk_$1_push(STACK_OF($1) *sk, $2 *ptr);
  337. static ossl_inline int sk_$1_unshift(STACK_OF($1) *sk, $2 *ptr);
  338. static ossl_inline $2 *sk_$1_pop(STACK_OF($1) *sk);
  339. static ossl_inline $2 *sk_$1_shift(STACK_OF($1) *sk);
  340. static ossl_inline void sk_$1_pop_free(STACK_OF($1) *sk,
  341. sk_$1_freefunc freefunc);
  342. static ossl_inline int sk_$1_insert(STACK_OF($1) *sk, $2 *ptr, int idx);
  343. static ossl_inline $2 *sk_$1_set(STACK_OF($1) *sk, int idx, $2 *ptr);
  344. static ossl_inline int sk_$1_find(STACK_OF($1) *sk, $2 *ptr);
  345. static ossl_inline int sk_$1_find_ex(STACK_OF($1) *sk, $2 *ptr);
  346. static ossl_inline void sk_$1_sort(STACK_OF($1) *sk);
  347. static ossl_inline int sk_$1_is_sorted(const STACK_OF($1) *sk);
  348. static ossl_inline STACK_OF($1) * sk_$1_dup(const STACK_OF($1) *sk);
  349. static ossl_inline STACK_OF($1) *sk_$1_deep_copy(const STACK_OF($1) *sk,
  350. sk_$1_copyfunc copyfunc,
  351. sk_$1_freefunc freefunc);
  352. static ossl_inline sk_$1_compfunc sk_$1_set_cmp_func(STACK_OF($1) *sk,
  353. sk_$1_compfunc compare);
  354. EOF
  355. }
  356. },
  357. { regexp => qr/DEFINE_SPECIAL_STACK_OF<<<\((.*),\s*(.*)\)>>>/,
  358. massager => sub { return ("SKM_DEFINE_STACK_OF($1,$2,$2)"); },
  359. },
  360. { regexp => qr/DEFINE_STACK_OF<<<\((.*)\)>>>/,
  361. massager => sub { return ("SKM_DEFINE_STACK_OF($1,$1,$1)"); },
  362. },
  363. { regexp => qr/DEFINE_SPECIAL_STACK_OF_CONST<<<\((.*),\s*(.*)\)>>>/,
  364. massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $2,$2)"); },
  365. },
  366. { regexp => qr/DEFINE_STACK_OF_CONST<<<\((.*)\)>>>/,
  367. massager => sub { return ("SKM_DEFINE_STACK_OF($1,const $1,$1)"); },
  368. },
  369. { regexp => qr/PREDECLARE_STACK_OF<<<\((.*)\)>>>/,
  370. massager => sub { return ("STACK_OF($1);"); }
  371. },
  372. { regexp => qr/DECLARE_STACK_OF<<<\((.*)\)>>>/,
  373. massager => sub { return ("STACK_OF($1);"); }
  374. },
  375. { regexp => qr/DECLARE_SPECIAL_STACK_OF<<<\((.*?),\s*(.*?)\)>>>/,
  376. massager => sub { return ("STACK_OF($1);"); }
  377. },
  378. #####
  379. # ASN1 stuff
  380. { regexp => qr/TYPEDEF_D2I_OF<<<\((.*)\)>>>/,
  381. massager => sub {
  382. return ("typedef $1 *d2i_of_$1($1 **,const unsigned char **,long)");
  383. },
  384. },
  385. { regexp => qr/TYPEDEF_I2D_OF<<<\((.*)\)>>>/,
  386. massager => sub {
  387. return ("typedef $1 *i2d_of_$1($1 *,unsigned char **)");
  388. },
  389. },
  390. { regexp => qr/TYPEDEF_D2I2D_OF<<<\((.*)\)>>>/,
  391. massager => sub {
  392. return ("TYPEDEF_D2I_OF($1); TYPEDEF_I2D_OF($1)");
  393. },
  394. },
  395. { regexp => qr/DECLARE_ASN1_ITEM<<<\((.*)\)>>>/,
  396. massager => sub {
  397. return (<<"EOF");
  398. #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
  399. OPENSSL_EXTERN const ASN1_ITEM *$1_it;
  400. #else
  401. const ASN1_ITEM *$1_it(void);
  402. #endif
  403. EOF
  404. },
  405. },
  406. { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS_only<<<\((.*),\s*(.*)\)>>>/,
  407. massager => sub {
  408. return (<<"EOF");
  409. int d2i_$2(void);
  410. int i2d_$2(void);
  411. EOF
  412. },
  413. },
  414. { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS<<<\((.*),\s*(.*),\s*(.*)\)>>>/,
  415. massager => sub {
  416. return (<<"EOF");
  417. int d2i_$3(void);
  418. int i2d_$3(void);
  419. DECLARE_ASN1_ITEM($2)
  420. EOF
  421. },
  422. },
  423. { regexp => qr/DECLARE_ASN1_ENCODE_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
  424. massager => sub {
  425. return (<<"EOF");
  426. int d2i_$2(void);
  427. int i2d_$2(void);
  428. DECLARE_ASN1_ITEM($2)
  429. EOF
  430. },
  431. },
  432. { regexp => qr/DECLARE_ASN1_ALLOC_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
  433. massager => sub {
  434. return (<<"EOF");
  435. int $2_free(void);
  436. int $2_new(void);
  437. EOF
  438. },
  439. },
  440. { regexp => qr/DECLARE_ASN1_ALLOC_FUNCTIONS<<<\((.*)\)>>>/,
  441. massager => sub {
  442. return (<<"EOF");
  443. int $1_free(void);
  444. int $1_new(void);
  445. EOF
  446. },
  447. },
  448. { regexp => qr/DECLARE_ASN1_FUNCTIONS_name<<<\((.*),\s*(.*)\)>>>/,
  449. massager => sub {
  450. return (<<"EOF");
  451. int d2i_$2(void);
  452. int i2d_$2(void);
  453. int $2_free(void);
  454. int $2_new(void);
  455. DECLARE_ASN1_ITEM($2)
  456. EOF
  457. },
  458. },
  459. { regexp => qr/DECLARE_ASN1_FUNCTIONS<<<\((.*)\)>>>/,
  460. massager => sub { return (<<"EOF");
  461. int d2i_$1(void);
  462. int i2d_$1(void);
  463. int $1_free(void);
  464. int $1_new(void);
  465. DECLARE_ASN1_ITEM($1)
  466. EOF
  467. }
  468. },
  469. { regexp => qr/DECLARE_ASN1_NDEF_FUNCTION<<<\((.*)\)>>>/,
  470. massager => sub {
  471. return (<<"EOF");
  472. int i2d_$1_NDEF(void);
  473. EOF
  474. }
  475. },
  476. { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION<<<\((.*)\)>>>/,
  477. massager => sub {
  478. return (<<"EOF");
  479. int $1_print_ctx(void);
  480. EOF
  481. }
  482. },
  483. { regexp => qr/DECLARE_ASN1_PRINT_FUNCTION_name<<<\((.*),\s*(.*)\)>>>/,
  484. massager => sub {
  485. return (<<"EOF");
  486. int $2_print_ctx(void);
  487. EOF
  488. }
  489. },
  490. { regexp => qr/DECLARE_ASN1_SET_OF<<<\((.*)\)>>>/,
  491. massager => sub { return (); }
  492. },
  493. { regexp => qr/DECLARE_ASN1_DUP_FUNCTION<<<\((.*)\)>>>/,
  494. massager => sub {
  495. return (<<"EOF");
  496. int $1_dup(void);
  497. EOF
  498. }
  499. },
  500. { regexp => qr/DECLARE_ASN1_DUP_FUNCTION_name<<<\((.*),\s*(.*)\)>>>/,
  501. massager => sub {
  502. return (<<"EOF");
  503. int $2_dup(void);
  504. EOF
  505. }
  506. },
  507. { regexp => qr/DECLARE_PKCS12_SET_OF<<<\((.*)\)>>>/,
  508. massager => sub { return (); }
  509. },
  510. { regexp => qr/DECLARE_PEM(?|_rw|_rw_cb|_rw_const)<<<\((.*?),.*\)>>>/,
  511. massager => sub { return (<<"EOF");
  512. #ifndef OPENSSL_NO_STDIO
  513. int PEM_read_$1(void);
  514. int PEM_write_$1(void);
  515. #endif
  516. int PEM_read_bio_$1(void);
  517. int PEM_write_bio_$1(void);
  518. EOF
  519. },
  520. },
  521. #####
  522. # PEM stuff
  523. { regexp => qr/DECLARE_PEM(?|_write|_write_cb|_write_const)<<<\((.*?),.*\)>>>/,
  524. massager => sub { return (<<"EOF");
  525. #ifndef OPENSSL_NO_STDIO
  526. int PEM_write_$1(void);
  527. #endif
  528. int PEM_write_bio_$1(void);
  529. EOF
  530. },
  531. },
  532. { regexp => qr/DECLARE_PEM(?|_read|_read_cb)<<<\((.*?),.*\)>>>/,
  533. massager => sub { return (<<"EOF");
  534. #ifndef OPENSSL_NO_STDIO
  535. int PEM_read_$1(void);
  536. #endif
  537. int PEM_read_bio_$1(void);
  538. EOF
  539. },
  540. },
  541. # Spurious stuff found in the OpenSSL headers
  542. # Usually, these are just macros that expand to, well, something
  543. { regexp => qr/__NDK_FPABI__/,
  544. massager => sub { return (); }
  545. },
  546. );
  547. my $anoncnt = 0;
  548. my @chandlers = (
  549. ##################################################################
  550. # C stuff
  551. # extern "C" of individual items
  552. # Note that the main parse function has a special hack for 'extern "C" {'
  553. # which can't be done in handlers
  554. # We simply ignore it.
  555. { regexp => qr/extern "C" (.*;)/,
  556. massager => sub { return ($1); },
  557. },
  558. # any other extern is just ignored
  559. { regexp => qr/^\s* # Any spaces before
  560. extern # The keyword we look for
  561. \b # word to non-word boundary
  562. .* # Anything after
  563. ;
  564. /x,
  565. massager => sub { return (); },
  566. },
  567. # union, struct and enum definitions
  568. # Because this one might appear a little everywhere within type
  569. # definitions, we take it out and replace it with just
  570. # 'union|struct|enum name' while registering it.
  571. # This makes use of the parser trick to surround the outer braces
  572. # with <<< and >>>
  573. { regexp => qr/(.*) # Anything before ($1)
  574. \b # word to non-word boundary
  575. (union|struct|enum) # The word used ($2)
  576. (?:\s([[:alpha:]_]\w*))? # Struct or enum name ($3)
  577. <<<(\{.*?\})>>> # Struct or enum definition ($4)
  578. (.*) # Anything after ($5)
  579. ;
  580. /x,
  581. massager => sub {
  582. my $before = $1;
  583. my $word = $2;
  584. my $name = $3
  585. || sprintf("__anon%03d", ++$anoncnt); # Anonymous struct
  586. my $definition = $4;
  587. my $after = $5;
  588. my $type = $word eq "struct" ? 'S' : 'E';
  589. if ($before ne "" || $after ne ";") {
  590. if ($after =~ m|^\w|) { $after = " ".$after; }
  591. return ("$before$word $name$after;",
  592. "$word $name", $type, "", "$word$definition", all_conds());
  593. }
  594. # If there was no before nor after, make the return much simple
  595. return ("", "$word $name", $type, "", "$word$definition", all_conds());
  596. }
  597. },
  598. # Named struct and enum forward declarations
  599. # We really just ignore them, but we need to parse them or the variable
  600. # declaration handler further down will think it's a variable declaration.
  601. { regexp => qr/^(union|struct|enum) ([[:alpha:]_]\w*);/,
  602. massager => sub { return (); }
  603. },
  604. # Function returning function pointer declaration
  605. { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
  606. ((?:\w|\*|\s)*?) # Return type ($2)
  607. \s? # Possible space
  608. <<<\(\*
  609. ([[:alpha:]_]\w*) # Function name ($3)
  610. (\(.*\)) # Parameters ($4)
  611. \)>>>
  612. <<<(\(.*\))>>> # F.p. parameters ($5)
  613. ;
  614. /x,
  615. massager => sub {
  616. return ("", $3, 'F', "", "$2(*$4)$5", all_conds())
  617. if defined $1;
  618. return ("", $3, 'F', "$2(*)$5", "$2(*$4)$5", all_conds()); }
  619. },
  620. # Function pointer declaration, or typedef thereof
  621. { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
  622. ((?:\w|\*|\s)*?) # Return type ($2)
  623. <<<\(\*([[:alpha:]_]\w*)\)>>> # T.d. or var name ($3)
  624. <<<(\(.*\))>>> # F.p. parameters ($4)
  625. ;
  626. /x,
  627. massager => sub {
  628. return ("", $3, 'T', "", "$2(*)$4", all_conds())
  629. if defined $1;
  630. return ("", $3, 'V', "$2(*)$4", "$2(*)$4", all_conds());
  631. },
  632. },
  633. # Function declaration, or typedef thereof
  634. { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
  635. ((?:\w|\*|\s)*?) # Return type ($2)
  636. \s? # Possible space
  637. ([[:alpha:]_]\w*) # Function name ($3)
  638. <<<(\(.*\))>>> # Parameters ($4)
  639. ;
  640. /x,
  641. massager => sub {
  642. return ("", $3, 'T', "", "$2$4", all_conds())
  643. if defined $1;
  644. return ("", $3, 'F', $2, "$2$4", all_conds());
  645. },
  646. },
  647. # Variable declaration, including arrays, or typedef thereof
  648. { regexp => qr/(?:(typedef)\s?)? # Possible typedef ($1)
  649. ((?:\w|\*|\s)*?) # Type ($2)
  650. \s? # Possible space
  651. ([[:alpha:]_]\w*) # Variable name ($3)
  652. ((?:<<<\[[^\]]*\]>>>)*) # Possible array declaration ($4)
  653. ;
  654. /x,
  655. massager => sub {
  656. return ("", $3, 'T', "", $2.($4||""), all_conds())
  657. if defined $1;
  658. return ("", $3, 'V', $2.($4||""), $2.($4||""), all_conds());
  659. },
  660. },
  661. );
  662. # End handlers are almost the same as handlers, except they are run through
  663. # ONCE when the input has been parsed through. These are used to check for
  664. # remaining stuff, such as an unfinished #ifdef and stuff like that that the
  665. # main parser can't check on its own.
  666. my @endhandlers = (
  667. { massager => sub {
  668. my %opts = %{$_[0]};
  669. die "Unfinished preprocessor conditions levels: ",scalar(@preprocessor_conds),($opts{filename} ? " in file ".$opts{filename}: ""),$opts{PLACE}
  670. if @preprocessor_conds;
  671. }
  672. }
  673. );
  674. # takes a list of strings that can each contain one or several lines of code
  675. # also takes a hash of options as last argument.
  676. #
  677. # returns a list of hashes with information:
  678. #
  679. # name name of the thing
  680. # type type, see the massage handler function
  681. # returntype return type of functions and variables
  682. # value value for macros, signature for functions, variables
  683. # and structs
  684. # conds preprocessor conditions (array ref)
  685. sub parse {
  686. my %opts;
  687. if (ref($_[$#_]) eq "HASH") {
  688. %opts = %{$_[$#_]};
  689. pop @_;
  690. }
  691. my %state = (
  692. in_extern_C => 0, # An exception to parenthesis processing.
  693. cpp_parens => [], # A list of ending parens and braces found in
  694. # preprocessor directives
  695. c_parens => [], # A list of ending parens and braces found in
  696. # C statements
  697. in_string => "", # empty string when outside a string, otherwise
  698. # "'" or '"' depending on the starting quote.
  699. in_comment => "", # empty string when outside a comment, otherwise
  700. # "/*" or "//" depending on the type of comment
  701. # found. The latter will never be multiline
  702. # NOTE: in_string and in_comment will never be
  703. # true (in perl semantics) at the same time.
  704. current_line => 0,
  705. );
  706. my @result = ();
  707. my $normalized_line = ""; # $input_line, but normalized. In essence, this
  708. # means that ALL whitespace is removed unless
  709. # it absolutely has to be present, and in that
  710. # case, there's only one space.
  711. # The cases where a space needs to stay present
  712. # are:
  713. # 1. between words
  714. # 2. between words and number
  715. # 3. after the first word of a preprocessor
  716. # directive.
  717. # 4. for the #define directive, between the macro
  718. # name/args and its value, so we end up with:
  719. # #define FOO val
  720. # #define BAR(x) something(x)
  721. my $collected_stmt = ""; # Where we're building up a C line until it's a
  722. # complete definition/declaration, as determined
  723. # by any handler being capable of matching it.
  724. # We use $_ shamelessly when looking through @lines.
  725. # In case we find a \ at the end, we keep filling it up with more lines.
  726. $_ = undef;
  727. foreach my $line (@_) {
  728. # split tries to be smart when a string ends with the thing we split on
  729. $line .= "\n" unless $line =~ m|\R$|;
  730. $line .= "#";
  731. # We use ¦undef¦ as a marker for a new line from the file.
  732. # Since we convert one line to several and unshift that into @lines,
  733. # that's the only safe way we have to track the original lines
  734. my @lines = map { ( undef, $_ ) } split $/, $line;
  735. # Remember that extra # we added above? Now we remove it
  736. pop @lines;
  737. pop @lines; # Don't forget the undef
  738. while (@lines) {
  739. if (!defined($lines[0])) {
  740. shift @lines;
  741. $state{current_line}++;
  742. if (!defined($_)) {
  743. $opts{PLACE} = " at ".$opts{filename}." line ".$state{current_line}."\n";
  744. $opts{PLACE2} = $opts{filename}.":".$state{current_line};
  745. }
  746. next;
  747. }
  748. $_ = "" unless defined $_;
  749. $_ .= shift @lines;
  750. if (m|\\$|) {
  751. $_ = $`;
  752. next;
  753. }
  754. if ($opts{debug}) {
  755. print STDERR "DEBUG:----------------------------\n";
  756. print STDERR "DEBUG: \$_ = '$_'\n";
  757. }
  758. ##########################################################
  759. # Now that we have a full line, let's process through it
  760. while(1) {
  761. unless ($state{in_comment}) {
  762. # Begin with checking if the current $normalized_line
  763. # contains a preprocessor directive
  764. # This is only done if we're not inside a comment and
  765. # if it's a preprocessor directive and it's finished.
  766. if ($normalized_line =~ m|^#| && $_ eq "") {
  767. print STDERR "DEBUG[OPENSSL CPP]: \$normalized_line = '$normalized_line'\n"
  768. if $opts{debug};
  769. $opts{debug_type} = "OPENSSL CPP";
  770. my @r = ( _run_handlers($normalized_line,
  771. @opensslcpphandlers,
  772. \%opts) );
  773. if (shift @r) {
  774. # Checking if there are lines to inject.
  775. if (@r) {
  776. @r = split $/, (pop @r).$_;
  777. print STDERR "DEBUG[OPENSSL CPP]: injecting '", join("', '", @r),"'\n"
  778. if $opts{debug} && @r;
  779. @lines = ( @r, @lines );
  780. $_ = "";
  781. }
  782. } else {
  783. print STDERR "DEBUG[CPP]: \$normalized_line = '$normalized_line'\n"
  784. if $opts{debug};
  785. $opts{debug_type} = "CPP";
  786. my @r = ( _run_handlers($normalized_line,
  787. @cpphandlers,
  788. \%opts) );
  789. if (shift @r) {
  790. if (ref($r[0]) eq "HASH") {
  791. push @result, shift @r;
  792. }
  793. # Now, check if there are lines to inject.
  794. # Really, this should never happen, it IS a
  795. # preprocessor directive after all...
  796. if (@r) {
  797. @r = split $/, pop @r;
  798. print STDERR "DEBUG[CPP]: injecting '", join("', '", @r),"'\n"
  799. if $opts{debug} && @r;
  800. @lines = ( @r, @lines );
  801. $_ = "";
  802. }
  803. }
  804. }
  805. # Note: we simply ignore all directives that no
  806. # handler matches
  807. $normalized_line = "";
  808. }
  809. # If the two strings end and start with a character that
  810. # shouldn't get concatenated, add a space
  811. my $space =
  812. ($collected_stmt =~ m/(?:"|')$/
  813. || ($collected_stmt =~ m/(?:\w|\d)$/
  814. && $normalized_line =~ m/^(?:\w|\d)/)) ? " " : "";
  815. # Now, unless we're building up a preprocessor directive or
  816. # are in the middle of a string, or the parens et al aren't
  817. # balanced up yet, let's try and see if there's a OpenSSL
  818. # or C handler that can make sense of what we have so far.
  819. if ( $normalized_line !~ m|^#|
  820. && ($collected_stmt ne "" || $normalized_line ne "")
  821. && ! @{$state{c_parens}}
  822. && ! $state{in_string} ) {
  823. if ($opts{debug}) {
  824. print STDERR "DEBUG[OPENSSL C]: \$collected_stmt = '$collected_stmt'\n";
  825. print STDERR "DEBUG[OPENSSL C]: \$normalized_line = '$normalized_line'\n";
  826. }
  827. $opts{debug_type} = "OPENSSL C";
  828. my @r = ( _run_handlers($collected_stmt
  829. .$space
  830. .$normalized_line,
  831. @opensslchandlers,
  832. \%opts) );
  833. if (shift @r) {
  834. # Checking if there are lines to inject.
  835. if (@r) {
  836. @r = split $/, (pop @r).$_;
  837. print STDERR "DEBUG[OPENSSL]: injecting '", join("', '", @r),"'\n"
  838. if $opts{debug} && @r;
  839. @lines = ( @r, @lines );
  840. $_ = "";
  841. }
  842. $normalized_line = "";
  843. $collected_stmt = "";
  844. } else {
  845. if ($opts{debug}) {
  846. print STDERR "DEBUG[C]: \$collected_stmt = '$collected_stmt'\n";
  847. print STDERR "DEBUG[C]: \$normalized_line = '$normalized_line'\n";
  848. }
  849. $opts{debug_type} = "C";
  850. my @r = ( _run_handlers($collected_stmt
  851. .$space
  852. .$normalized_line,
  853. @chandlers,
  854. \%opts) );
  855. if (shift @r) {
  856. if (ref($r[0]) eq "HASH") {
  857. push @result, shift @r;
  858. }
  859. # Checking if there are lines to inject.
  860. if (@r) {
  861. @r = split $/, (pop @r).$_;
  862. print STDERR "DEBUG[C]: injecting '", join("', '", @r),"'\n"
  863. if $opts{debug} && @r;
  864. @lines = ( @r, @lines );
  865. $_ = "";
  866. }
  867. $normalized_line = "";
  868. $collected_stmt = "";
  869. }
  870. }
  871. }
  872. if ($_ eq "") {
  873. $collected_stmt .= $space.$normalized_line;
  874. $normalized_line = "";
  875. }
  876. }
  877. if ($_ eq "") {
  878. $_ = undef;
  879. last;
  880. }
  881. # Take care of inside string first.
  882. if ($state{in_string}) {
  883. if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
  884. $state{in_string} # Look for matching quote
  885. /x) {
  886. $normalized_line .= $`.$&;
  887. $state{in_string} = "";
  888. $_ = $';
  889. next;
  890. } else {
  891. die "Unfinished string without continuation found$opts{PLACE}\n";
  892. }
  893. }
  894. # ... or inside comments, whichever happens to apply
  895. elsif ($state{in_comment}) {
  896. # This should never happen
  897. die "Something went seriously wrong, multiline //???$opts{PLACE}\n"
  898. if ($state{in_comment} eq "//");
  899. # A note: comments are simply discarded.
  900. if (m/ (?:^|(?<!\\)) # Make sure it's not escaped
  901. \*\/ # Look for C comment end
  902. /x) {
  903. $state{in_comment} = "";
  904. $_ = $';
  905. print STDERR "DEBUG: Found end of comment, followed by '$_'\n"
  906. if $opts{debug};
  907. next;
  908. } else {
  909. $_ = "";
  910. next;
  911. }
  912. }
  913. # At this point, it's safe to remove leading whites, but
  914. # we need to be careful with some preprocessor lines
  915. if (m|^\s+|) {
  916. my $rest = $';
  917. my $space = "";
  918. $space = " "
  919. if ($normalized_line =~ m/^
  920. \#define\s\w(?:\w|\d)*(?:<<<\([^\)]*\)>>>)?
  921. | \#[a-z]+
  922. $/x);
  923. print STDERR "DEBUG: Processing leading spaces: \$normalized_line = '$normalized_line', \$space = '$space', \$rest = '$rest'\n"
  924. if $opts{debug};
  925. $_ = $space.$rest;
  926. }
  927. my $parens =
  928. $normalized_line =~ m|^#| ? 'cpp_parens' : 'c_parens';
  929. (my $paren_singular = $parens) =~ s|s$||;
  930. # Now check for specific tokens, and if they are parens,
  931. # check them against $state{$parens}. Note that we surround
  932. # the outermost parens with extra "<<<" and ">>>". Those
  933. # are for the benefit of handlers who to need to detect
  934. # them, and they will be removed from the final output.
  935. if (m|^[\{\[\(]|) {
  936. my $body = $&;
  937. $_ = $';
  938. if (!@{$state{$parens}}) {
  939. if ("$normalized_line$body" =~ m|^extern "C"\{$|) {
  940. $state{in_extern_C} = 1;
  941. print STDERR "DEBUG: found start of 'extern \"C\"' ($normalized_line$body)\n"
  942. if $opts{debug};
  943. $normalized_line = "";
  944. } else {
  945. $normalized_line .= "<<<".$body;
  946. }
  947. } else {
  948. $normalized_line .= $body;
  949. }
  950. if ($normalized_line ne "") {
  951. print STDERR "DEBUG: found $paren_singular start '$body'\n"
  952. if $opts{debug};
  953. $body =~ tr|\{\[\(|\}\]\)|;
  954. print STDERR "DEBUG: pushing $paren_singular end '$body'\n"
  955. if $opts{debug};
  956. push @{$state{$parens}}, $body;
  957. }
  958. } elsif (m|^[\}\]\)]|) {
  959. $_ = $';
  960. if (!@{$state{$parens}}
  961. && $& eq '}' && $state{in_extern_C}) {
  962. print STDERR "DEBUG: found end of 'extern \"C\"'\n"
  963. if $opts{debug};
  964. $state{in_extern_C} = 0;
  965. } else {
  966. print STDERR "DEBUG: Trying to match '$&' against '"
  967. ,join("', '", @{$state{$parens}})
  968. ,"'\n"
  969. if $opts{debug};
  970. die "Unmatched parentheses$opts{PLACE}\n"
  971. unless (@{$state{$parens}}
  972. && pop @{$state{$parens}} eq $&);
  973. if (!@{$state{$parens}}) {
  974. $normalized_line .= $&.">>>";
  975. } else {
  976. $normalized_line .= $&;
  977. }
  978. }
  979. } elsif (m|^["']|) { # string start
  980. my $body = $&;
  981. $_ = $';
  982. # We want to separate strings from \w and \d with one space.
  983. $normalized_line .= " " if $normalized_line =~ m/(\w|\d)$/;
  984. $normalized_line .= $body;
  985. $state{in_string} = $body;
  986. } elsif (m|^\/\*|) { # C style comment
  987. print STDERR "DEBUG: found start of C style comment\n"
  988. if $opts{debug};
  989. $state{in_comment} = $&;
  990. $_ = $';
  991. } elsif (m|^\/\/|) { # C++ style comment
  992. print STDERR "DEBUG: found C++ style comment\n"
  993. if $opts{debug};
  994. $_ = ""; # (just discard it entirely)
  995. } elsif (m/^ (?| (?: 0[xX][[:xdigit:]]+ | 0[bB][01]+ | [0-9]+ )
  996. (?i: U | L | UL | LL | ULL )?
  997. | [0-9]+\.[0-9]+(?:[eE][\-\+]\d+)? (?i: F | L)?
  998. ) /x) {
  999. print STDERR "DEBUG: Processing numbers: \$normalized_line = '$normalized_line', \$& = '$&', \$' = '$''\n"
  1000. if $opts{debug};
  1001. $normalized_line .= $&;
  1002. $_ = $';
  1003. } elsif (m/^[[:alpha:]_]\w*/) {
  1004. my $body = $&;
  1005. my $rest = $';
  1006. my $space = "";
  1007. # Now, only add a space if it's needed to separate
  1008. # two \w characters, and we also surround strings with
  1009. # a space. In this case, that's if $normalized_line ends
  1010. # with a \w, \d, " or '.
  1011. $space = " "
  1012. if ($normalized_line =~ m/("|')$/
  1013. || ($normalized_line =~ m/(\w|\d)$/
  1014. && $body =~ m/^(\w|\d)/));
  1015. print STDERR "DEBUG: Processing words: \$normalized_line = '$normalized_line', \$space = '$space', \$body = '$body', \$rest = '$rest'\n"
  1016. if $opts{debug};
  1017. $normalized_line .= $space.$body;
  1018. $_ = $rest;
  1019. } elsif (m|^(?:\\)?.|) { # Catch-all
  1020. $normalized_line .= $&;
  1021. $_ = $';
  1022. }
  1023. }
  1024. }
  1025. }
  1026. foreach my $handler (@endhandlers) {
  1027. if ($handler->{massager}) {
  1028. $handler->{massager}->(\%opts);
  1029. }
  1030. }
  1031. return @result;
  1032. }
  1033. # arg1: line to check
  1034. # arg2...: handlers to check
  1035. # return undef when no handler matched
  1036. sub _run_handlers {
  1037. my %opts;
  1038. if (ref($_[$#_]) eq "HASH") {
  1039. %opts = %{$_[$#_]};
  1040. pop @_;
  1041. }
  1042. my $line = shift;
  1043. my @handlers = @_;
  1044. foreach my $handler (@handlers) {
  1045. if ($handler->{regexp}
  1046. && $line =~ m|^$handler->{regexp}$|) {
  1047. if ($handler->{massager}) {
  1048. if ($opts{debug}) {
  1049. print STDERR "DEBUG[",$opts{debug_type},"]: Trying to handle '$line'\n";
  1050. print STDERR "DEBUG[",$opts{debug_type},"]: (matches /\^",$handler->{regexp},"\$/)\n";
  1051. }
  1052. my $saved_line = $line;
  1053. my @massaged =
  1054. map { s/(<<<|>>>)//g; $_ }
  1055. $handler->{massager}->($saved_line, \%opts);
  1056. print STDERR "DEBUG[",$opts{debug_type},"]: Got back '"
  1057. , join("', '", @massaged), "'\n"
  1058. if $opts{debug};
  1059. # Because we may get back new lines to be
  1060. # injected before whatever else that follows,
  1061. # and the injected stuff might include
  1062. # preprocessor lines, we need to inject them
  1063. # in @lines and set $_ to the empty string to
  1064. # break out from the inner loops
  1065. my $injected_lines = shift @massaged || "";
  1066. if (@massaged) {
  1067. return (1,
  1068. {
  1069. name => shift @massaged,
  1070. type => shift @massaged,
  1071. returntype => shift @massaged,
  1072. value => shift @massaged,
  1073. conds => [ @massaged ]
  1074. },
  1075. $injected_lines
  1076. );
  1077. } else {
  1078. print STDERR "DEBUG[",$opts{debug_type},"]: (ignore, possible side effects)\n"
  1079. if $opts{debug} && $injected_lines eq "";
  1080. return (1, $injected_lines);
  1081. }
  1082. }
  1083. return (1);
  1084. }
  1085. }
  1086. return (0);
  1087. }