#!/usr/bin/perl
#
# $Id: fix-flex,v 1.2 2004/08/13 16:02:48 cmdjb Exp $
#
# 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 <Dave.Beckett@bristol.ac.uk>
# 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;
}
