#!/usr/bin/perl
#
# $Id: fix-flex 8618 2006-02-19 08:36:21Z dajobe $
#
# Format output generated by flex 2.5.31
#
# Usage:
#  flex -o$output $input
#  perl fix-flex $output > $tmp
#  mv $tmp $output
#
# Copyright (C) 2004-2006, David Beckett http://purl.org/net/dajobe/
# Copyright (C) 2004, University of Bristol, UK http://www.bristol.ac.uk/
#

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

#ifdef WIN32
#include <win32_rasqal_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;
}
