fukasawa e60969
#!/bin/awk -f
fukasawa e60969
# scripts/dfn.awk - process a .dfn file
fukasawa e60969
#
fukasawa e60969
# last changed in libpng version 1.5.19 - August 21, 2014
fukasawa e60969
#
fukasawa e60969
# Copyright (c) 2013-2014 Glenn Randers-Pehrson
fukasawa e60969
#
fukasawa e60969
# This code is released under the libpng license.
fukasawa e60969
# For conditions of distribution and use, see the disclaimer
fukasawa e60969
# and license in png.h
fukasawa e60969
fukasawa e60969
# The output of this script is written to the file given by
fukasawa e60969
# the variable 'out', which should be set on the command line.
fukasawa e60969
# Error messages are printed to stdout and if any are printed
fukasawa e60969
# the script will exit with error code 1.
fukasawa e60969
fukasawa e60969
BEGIN{
fukasawa e60969
   out="/dev/null"       # as a flag
fukasawa e60969
   out_count=0           # count of output lines
fukasawa e60969
   err=0                 # set if an error occurred
fukasawa e60969
   sort=0                # sort the output
fukasawa e60969
   array[""]=""
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# The output file must be specified before any input:
fukasawa e60969
NR==1 && out == "/dev/null" {
fukasawa e60969
   print "out=output.file must be given on the command line"
fukasawa e60969
   # but continue without setting the error code; this allows the
fukasawa e60969
   # script to be checked easily
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# Output can be sorted; two lines are recognized
fukasawa e60969
$1 == "PNG_DFN_START_SORT"{
fukasawa e60969
   sort=0+$2
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
$1 ~ /^PNG_DFN_END_SORT/{
fukasawa e60969
   # Do a very simple, slow, sort; notice that blank lines won't be
fukasawa e60969
   # output by this
fukasawa e60969
   for (entry in array) {
fukasawa e60969
      while (array[entry] != "") {
fukasawa e60969
         key = entry
fukasawa e60969
         value = array[key]
fukasawa e60969
         array[key] = ""
fukasawa e60969
fukasawa e60969
         for (alt in array) {
fukasawa e60969
            if (array[alt] != "" && alt < key) {
fukasawa e60969
               array[key] = value
fukasawa e60969
               value = array[alt]
fukasawa e60969
               key = alt
fukasawa e60969
               array[alt] = ""
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         print value >out
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   sort=0
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/^[^"]*PNG_DFN *".*"[^"]*$/{
fukasawa e60969
   # A definition line, apparently correctly formatted; extract the
fukasawa e60969
   # definition then replace any doubled "" that remain with a single
fukasawa e60969
   # double quote.  Notice that the original doubled double quotes
fukasawa e60969
   # may have been split by tokenization
fukasawa e60969
   #
fukasawa e60969
   # Sometimes GCC splits the PNG_DFN lines; we know this has happened
fukasawa e60969
   # if the quotes aren't closed and must read another line.  In this
fukasawa e60969
   # case it is essential to reject lines that start with '#' because those
fukasawa e60969
   # are introduced #line directives.
fukasawa e60969
   orig=$0
fukasawa e60969
   line=$0
fukasawa e60969
   lineno=FNR
fukasawa e60969
   if (lineno == "") lineno=NR
fukasawa e60969
fukasawa e60969
   if (sub(/^[^"]*PNG_DFN *"/,"",line) != 1) {
fukasawa e60969
	print "line", lineno ": processing failed:"
fukasawa e60969
	print orig
fukasawa e60969
	err=1
fukasawa e60969
       next
fukasawa e60969
   } else {
fukasawa e60969
	++out_count
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   # Now examine quotes within the value:
fukasawa e60969
   #
fukasawa e60969
   #   @" - delete this and any following spaces
fukasawa e60969
   #   "@ - delete this and any preceding spaces
fukasawa e60969
   #   @' - replace this by a double quote
fukasawa e60969
   #
fukasawa e60969
   # This allows macro substitution by the C compiler thus:
fukasawa e60969
   #
fukasawa e60969
   #   #define first_name John
fukasawa e60969
   #   #define last_name Smith
fukasawa e60969
   #
fukasawa e60969
   #	PNG_DFN"#define name @'@" first_name "@ @" last_name "@@'"
fukasawa e60969
   #
fukasawa e60969
   # Might get C preprocessed to:
fukasawa e60969
   #
fukasawa e60969
   #   PNG_DFN "#define foo @'@" John "@ @" Smith "@@'"
fukasawa e60969
   #
fukasawa e60969
   # Which this script reduces to:
fukasawa e60969
   #
fukasawa e60969
   #	#define name "John Smith"
fukasawa e60969
   #
fukasawa e60969
   while (1) {
fukasawa e60969
      # While there is an @" remove it and the next "@
fukasawa e60969
      if (line ~ /@"/) {
fukasawa e60969
         if (line ~ /@".*"@/) {
fukasawa e60969
            # Do this special case first to avoid swallowing extra spaces
fukasawa e60969
            # before or after the @ stuff:
fukasawa e60969
            if (!sub(/@" *"@/, "", line)) {
fukasawa e60969
               # Ok, do it in pieces - there has to be a non-space between the
fukasawa e60969
               # two.  NOTE: really weird things happen if a leading @" is
fukasawa e60969
               # lost - the code will error out below (I believe).
fukasawa e60969
               if (!sub(/@" */, "", line) || !sub(/ *"@/, "", line)) {
fukasawa e60969
                  print "line", lineno, ": internal error:", orig
fukasawa e60969
                  exit 1
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         # There is no matching "@.  Assume a split line
fukasawa e60969
         else while (1) {
fukasawa e60969
            if (getline nextline) {
fukasawa e60969
               # If the line starts with '#' it is a preprocesor line directive
fukasawa e60969
               # from cc -E; skip it:
fukasawa e60969
               if (nextline !~ /^#/) {
fukasawa e60969
                  line = line " " nextline
fukasawa e60969
                  break
fukasawa e60969
               }
fukasawa e60969
            } else {
fukasawa e60969
               # This is end-of-input - probably a missing "@ on the first line:
fukasawa e60969
               print "line", lineno ": unbalanced @\" ... \"@ pair"
fukasawa e60969
               err=1
fukasawa e60969
               next
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         # Keep going until all the @" have gone
fukasawa e60969
         continue
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      # Attempt to remove a trailing " (not preceded by '@') - if this can
fukasawa e60969
      # be done, stop now; if not assume a split line again
fukasawa e60969
      if (sub(/"[^"]*$/, "", line))
fukasawa e60969
         break
fukasawa e60969
fukasawa e60969
      # Read another line
fukasawa e60969
      while (1) {
fukasawa e60969
         if (getline nextline) {
fukasawa e60969
            if (nextline !~ /^#/) {
fukasawa e60969
               line = line " " nextline
fukasawa e60969
               # Go back to stripping @" "@ pairs
fukasawa e60969
               break
fukasawa e60969
            }
fukasawa e60969
         } else {
fukasawa e60969
            print "line", lineno ": unterminated PNG_DFN string"
fukasawa e60969
            err=1
fukasawa e60969
            next
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   # Put any needed double quotes in (at the end, because these would otherwise
fukasawa e60969
   # interfere with the processing above.)
fukasawa e60969
   gsub(/@'/,"\"", line)
fukasawa e60969
fukasawa e60969
   # Remove any trailing spaces (not really required, but for
fukasawa e60969
   # editorial consistency
fukasawa e60969
   sub(/ *$/, "", line)
fukasawa e60969
fukasawa e60969
   # Remove trailing CR
fukasawa e60969
   sub(/
$/, "", line)
fukasawa e60969
fukasawa e60969
   if (sort) {
fukasawa e60969
      if (split(line, parts) < sort) {
fukasawa e60969
         print "line", lineno ": missing sort field:", line
fukasawa e60969
         err=1
fukasawa e60969
      } else
fukasawa e60969
         array[parts[sort]] = line
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      print line >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/PNG_DFN/{
fukasawa e60969
   print "line", NR, "incorrectly formatted PNG_DFN line:"
fukasawa e60969
   print $0
fukasawa e60969
   err = 1
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
END{
fukasawa e60969
   if (out_count > 0 || err > 0)
fukasawa e60969
	exit err
fukasawa e60969
fukasawa e60969
   print "no definition lines found"
fukasawa e60969
   exit 1
fukasawa e60969
}