summaryrefslogtreecommitdiffstats
path: root/speedcvt.pl
blob: 564725a9c319b05b1e54db06d8b0ef140747295a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl

use Fcntl;

($out) = @ARGV;

sysopen(OUT, $out, O_WRONLY|O_CREAT|O_TRUNC, 0666)
    or die "$0: Cannot open $out: $!\n";

@speeds = ();

while ( defined($_ = <STDIN>) ) {
    if ( /^\#\s*define\s+B([0-9]+)\s+/ ) {
	$b = $1 + 0;
	if ( $b ) {
	    push(@speeds, $b);
	}
    }
}

@speeds = sort { $a <=> $b } @speeds;

print OUT "#include \"attachtty.h\"\n";
print OUT "\n";
print OUT "speed_t speed_to_termios(unsigned long speed)\n";
print OUT "{\n";
print OUT "\tswitch ( speed ) {\n";
foreach $s ( @speeds ) {
    print OUT "\t\tcase ${s}UL:\n";
    print OUT "\t\t\treturn B${s};\n";
}
print OUT "\t\tdefault:\n";
print OUT "\t\t\treturn B0;\n";
print OUT "\t}\n";
print OUT "}\n";
print OUT "\n";
print OUT "unsigned long speed_from_termios(speed_t speed)\n";
print OUT "{\n";
print OUT "\tswitch ( speed ) {\n";
foreach $s ( @speeds ) {
    print OUT "\t\tcase B${s}:\n";
    print OUT "\t\t\treturn ${s}UL;\n";
}
print OUT "\t\tdefault:\n";
print OUT "\t\t\treturn 0UL;\n";
print OUT "\t}\n";
print OUT "}\n";

close(OUT);

exit(0);