70be662001-06-19Fredrik Hübinette (Hubbe) #!/bin/sh
812b942004-09-19Martin Nilsson # Avoid clever UTF-8 assumptions. LANG=C
2354f82005-01-28Henrik Grubbström (Grubba) LC_CTYPE=C
304c832010-11-11Per Hedbor LC_ALL=C
2354f82005-01-28Henrik Grubbström (Grubba) 
304c832010-11-11Per Hedbor export LANG LC_CTYPE LC_ALL
812b942004-09-19Martin Nilsson 
0019d22003-08-16Henrik Grubbström (Grubba) # Notes about limitations in SysV sed: # # o The *-operator only operates on single character regexps. # o The |-operator does not exist. # o Labels are only terminated by newline (ie ; does not terminate # a label). # o Character ranges may not contain literal newlines.
86b3c02003-08-15Henrik Grubbström (Grubba) sed -e '
9b52732003-12-10Dan Nelson # First pass-through and convert #line directives.
8807a32003-08-18Henrik Grubbström (Grubba)  /^#line/b
86b3c02003-08-15Henrik Grubbström (Grubba)  s/^# \([0-9]* "[^"]*"\).*$/#line \1/ s/^# \([0-9]*\).*$/#line \1/ t
9b52732003-12-10Dan Nelson # Output all complete OPCODE[0-9].* --- expressions # accumulated so far.
99a2af2003-08-15Henrik Grubbström (Grubba) : loop
86b3c02003-08-15Henrik Grubbström (Grubba)  /OPCODE[0-9]/!b keepnl
9b52732003-12-10Dan Nelson # Stupid IRIX cpp turns --- into -- - # SysV sed does not support multi-character *-expressions, # so we use ½ instead.
86b3c02003-08-15Henrik Grubbström (Grubba)  s/ -- \{0,1\}- / ½ /g
9b52732003-12-10Dan Nelson # Make sure the substitute flag is cleared.
86b3c02003-08-15Henrik Grubbström (Grubba)  t loop h s/^[^½]*\(OPCODE[0-9][^½]*\) ½.*$/\1¤/p t more N b loop
9b52732003-12-10Dan Nelson # Remove the match from the hold space, # and check for more.
cb398d2003-08-14Henrik Grubbström (Grubba) : more
86b3c02003-08-15Henrik Grubbström (Grubba)  g s/^[^½]*OPCODE[0-9][^½]* ½\(.*\)$/\1/ b loop
9b52732003-12-10Dan Nelson # Keep only the newlines.
cb398d2003-08-14Henrik Grubbström (Grubba) : keepnl
86b3c02003-08-15Henrik Grubbström (Grubba)  s/.//g' | sed -e '
9b52732003-12-10Dan Nelson # Redundant or extra newline and #line removal.
8807a32003-08-18Henrik Grubbström (Grubba) 
9b52732003-12-10Dan Nelson # Remove newlines that were inserted by p.
86b3c02003-08-15Henrik Grubbström (Grubba) : loop /¤$/{ N s/¤\n// b loop }
9b52732003-12-10Dan Nelson # Add empty lines to hold, and start the next cycle.
86b3c02003-08-15Henrik Grubbström (Grubba)  /^[ ]*$/{
9b52732003-12-10Dan Nelson # We are only interrested in the newline...
9aa8172003-08-15Henrik Grubbström (Grubba)  s/.//g
86b3c02003-08-15Henrik Grubbström (Grubba)  H
8807a32003-08-18Henrik Grubbström (Grubba)  x
9b52732003-12-10Dan Nelson # RLE encode newlines so that we can avoid overflowing hold.
8807a32003-08-18Henrik Grubbström (Grubba)  s/\n\{16,16\}/¤/ s/¤\{16,16\}/½/ x
86b3c02003-08-15Henrik Grubbström (Grubba)  d }
9b52732003-12-10Dan Nelson # Clear hold, and put #line directives there, # and start the next cycle.
86b3c02003-08-15Henrik Grubbström (Grubba)  /^#line/{ h d }
9b52732003-12-10Dan Nelson # Print hold except for a possible initial newline.
86b3c02003-08-15Henrik Grubbström (Grubba)  x
9b52732003-12-10Dan Nelson # Unpack the RLE.
8807a32003-08-18Henrik Grubbström (Grubba)  s/½/¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤/g s/¤/\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ /g
9aa8172003-08-15Henrik Grubbström (Grubba)  s/^\n//
86b3c02003-08-15Henrik Grubbström (Grubba)  p
9b52732003-12-10Dan Nelson # Clear hold.
86b3c02003-08-15Henrik Grubbström (Grubba)  s/.//g
9aa8172003-08-15Henrik Grubbström (Grubba)  s/\n//g
86b3c02003-08-15Henrik Grubbström (Grubba)  x'