syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / sys2ansi.pl
1 #!/usr/bin/perl
2 # $Id: sys2ansi.pl,v 1.8 2003/07/01 00:49:31 hpa Exp $
3 #
4 # Perl script to convert a Syslinux-format screen to PC-ANSI
5 # to display in a color xterm or on the Linux console
6 #
7
8 @ansicol = (0,4,2,6,1,5,3,7);
9
10 $getting_file = 0;
11 $enable = 1;
12
13 while ( read(STDIN, $ch, 1) > 0 ) {
14     if ( $ch eq "\x1A" ) {      # <SUB> <Ctrl-Z> EOF
15         last;
16     } elsif ( $ch eq "\x0C" ) { # <FF>  <Ctrl-L> Clear screen
17         print "\x1b[2J" if ( $enable && !$getting_file );
18     } elsif ( $ch eq "\x0F" ) { # <SI>  <Ctrl-O> Attribute change
19         if ( !$getting_file ) {
20             if ( read(STDIN, $attr, 2) == 2 ) {
21                 $attr = hex $attr;
22                 if ( $enable ) {
23                     print "\x1b[0;";
24                     if ( $attr & 0x80 ) {
25                         print "5;";
26                         $attr &= ~0x80;
27                     }
28                     if ( $attr & 0x08 ) {
29                         print "1;";
30                         $attr &= ~0x08;
31                     }
32                     printf "%d;%dm",
33                     $ansicol[$attr >> 4] + 40, $ansicol[$attr & 7] + 30;
34                 }
35             }
36         }
37     } elsif ( $ch eq "\x18" ) { # <CAN> <Ctrl-X> Display image
38         # We can't display an image; pretend to be a text screen
39         # Ignore all input until end of line
40         $getting_file = 1;
41     } elsif ( (ord($ch) & ~07) == 0x10 ) { # Mode controls
42         $enable = (ord($ch) & 0x01); # Emulate the text screen
43     } elsif ( $ch eq "\x0D" ) { # <CR>  <Ctrl-M> Carriage return
44         # Ignore
45     } elsif ( $ch eq "\x0A" ) { # <LF>  <Ctrl-J> Line feed
46         if ( $getting_file ) {
47             $getting_file = 0;
48         } else {
49             print $ch if ( $enable );
50         }
51     } else {
52         print $ch if ( $enable && !$getting_file );
53     }
54 }