#! /bin/sh

# Test checking of C format strings.

tmpfiles=""
trap 'rm -fr $tmpfiles' 1 2 3 15

tmpfiles="$tmpfiles f-gi-2.data"
cat <<\EOF > f-gi-2.data
# Valid: %% doesn't count
msgid  "abc%%def"
msgstr "xyz"
# Invalid: invalid msgstr
msgid  "abc%%def"
msgstr "xyz%"
# Valid: same arguments
msgid  "abc%s%Hdef"
msgstr "xyz%s%H"
# Valid: same arguments, with different widths
msgid  "abc%.*sdef"
msgstr "xyz%i%s"
# Invalid: too few arguments
msgid  "abc%sdef%u"
msgstr "xyz%s"
# Invalid: too many arguments
msgid  "abc%udef"
msgstr "xyz%uvw%c"
# Valid: type compatibility
msgid  "abc%i"
msgstr "xyz%d"
# Valid: type compatibility
msgid  "abc%o"
msgstr "xyz%u"
# Valid: type compatibility
msgid  "abc%u"
msgstr "xyz%x"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%s"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%i"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%u"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%H"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%D"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%c"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%i"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%u"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%H"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%D"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%s"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%u"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%H"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%D"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%i"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%H"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%D"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%u"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%D"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%H"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%F"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%D"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%T"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%F"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%A"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%T"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%C"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%A"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%E"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%C"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%E"
msgstr "xyz%L"
# Invalid: type incompatibility
msgid  "abc%E"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%E"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%E"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%E"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%L"
msgstr "xyz%O"
# Invalid: type incompatibility
msgid  "abc%L"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%L"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%L"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%O"
msgstr "xyz%P"
# Invalid: type incompatibility
msgid  "abc%O"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%O"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%P"
msgstr "xyz%Q"
# Invalid: type incompatibility
msgid  "abc%P"
msgstr "xyz%V"
# Invalid: type incompatibility
msgid  "abc%Q"
msgstr "xyz%V"
# Invalid: type incompatibility for width
msgid  "abc%.*s"
msgstr "xyz%u%s"
EOF

: ${MSGFMT=msgfmt}
n=0
while read comment; do
  read msgid_line
  read msgstr_line
  n=`expr $n + 1`
  tmpfiles="$tmpfiles f-gi-2-$n.po f-gi-2-$n.mo"
  cat <<EOF > f-gi-2-$n.po
#, gcc-internal-format
${msgid_line}
${msgstr_line}
EOF
  fail=
  if echo "$comment" | grep 'Valid:' > /dev/null; then
    if ${MSGFMT} --check-format -o f-gi-2-$n.mo f-gi-2-$n.po; then
      :
    else
      fail=yes
    fi
  else
    ${MSGFMT} --check-format -o f-gi-2-$n.mo f-gi-2-$n.po 2> /dev/null
    if test $? = 1; then
      :
    else
      fail=yes
    fi
  fi
  if test -n "$fail"; then
    echo "Format string checking error:" 1>&2
    cat f-gi-2-$n.po 1>&2
    exit 1
  fi
  rm -f f-gi-2-$n.po f-gi-2-$n.mo
done < f-gi-2.data

rm -fr $tmpfiles

exit 0
