bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#18987: the bourne shell printf-vs-\xHH portability trap


From: Paul Eggert
Subject: bug#18987: the bourne shell printf-vs-\xHH portability trap
Date: Sat, 08 Nov 2014 19:52:13 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

I have some qualms about that patch. It assumes the C locale, and it's a bit safer to spell it out as in '0-9abcdefABCDEF'. Also, the temporary streams (i.e., the output of 'COMMAND inside '$(COMMAND)') are not text, and arguably this does not conform to POSIX (POSIX is murky here) and anyway I suspect some picky shells will complain. Third and most important, it'd be nicer if hex_printf_ worked like 'printf', except with support for hexadecimal escapes.

How about something like the following instead? It's brute-force, but it should be portable.

  hex_printf_()
  {
    hex_printf_format=$(printf '%s\n' "$1" | sed '
      s/^/_/
      s/$/_/
      s/\([^\\]\(\\\\\)*\\x\)\([0-9aAbBcCdDeEfF][^0-9aAbBcCdDeEfF]\)/\10\3/g
      s/\([^\\]\(\\\\\)*\\x\)\([0-3]\)/\10\3/g
      s/\([^\\]\(\\\\\)*\\x\)\([4-7]\)/\11\3/g
      s/\([^\\]\(\\\\\)*\\x\)\([89aAbB]\)/\12\3/g
      s/\([^\\]\(\\\\\)*\\x\)\([cCdDeEfF]\)/\13\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[048cC]\([0-7]\)/\1,0\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[048cC]\([89aAbBcCdDeEfF]\)/\1,1\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[159dD]\([0-7]\)/\1,2\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[159dD]\([89abcdef]\)/\1,3\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[26aAeE]\([0-7]\)/\1,4\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[26aAeE]\([89aAbBcCdDeEfF]\)/\1,5\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[37bBfF]\([0-7]\)/\1,6\3/g
      s/\([^\\]\(\\\\\)*\\x[0-3]\)[37bBfF]\([89aAbBcCdDeEfF]\)/\1,7\3/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[08]/\1\3\40/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[19]/\1\3\41/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[2aA]/\1\3\42/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[3bB]/\1\3\43/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[4cC]/\1\3\44/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[5dD]/\1\3\45/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[6eE]/\1\3\46/g
      s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[7fF]/\1\3\47/g
      s/^_//
      s/_$//
    ')
    shift
    printf "$hex_printf_format" "$@"
  }

  hex_printf_ '\x34\\x%dX\x45\n' 100






reply via email to

[Prev in Thread] Current Thread [Next in Thread]