#!/usr/bin/perl
#
# Format output generated by flex 2.5.31
#
# Usage:
#  flex -o$output $input
#  perl fix-flex $output > $tmp
#  mv $tmp $output
#
# (C) Copyright 2004 Dave Beckett http://purl.org/net/dajobe/
# (C) Copyright 2004 University of Bristol
#

print <<'EOT';
#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif

#ifdef WIN32
#include <win32_raptor_config.h>
#endif

EOT

while(<>) {
  # the default parser tries to free(NULL)
  s/(^\s+)(free\(.*ptr.*\))/${1}if(ptr) $2/;

  # flex has %option nounistd however it does not work in 2.5.31
  # It is safe to add yet another wrapper. 
  $_=<<"EOT" if m%^(\#include \<unistd.h\>)$%;
#ifndef YY_NO_UNISTD_H
$1
#endif
EOT

  print;
}
