aboutsummaryrefslogtreecommitdiffstats
path: root/src/speedgen
blob: 478007dbe6caee489ea9597a95662ffbf2f92b14 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh -e

out="$1"
tmp="$out.tmp"

if [ -z "$out" ]; then
    echo "Usage: $0 outfile" 2>&1
    exit 1
fi

s='[[:space:]]'			# For brevity's sake

trap "rm -f '$tmp'" EXIT
trap "rm -f '$tmp' '$out'" HUP INT QUIT TERM

# Fallback list of speeds that are always tested for.
#
# It is safe to add additional speeds to this list; speeds not defined
# on any particular platform are simply ignored.  It is specifically
# encouraged to add additional speeds here if some are found to be
# defined on a platform for which:
#
# - the C compiler(s) normally used does not support the -dM option
#   (gcc, clang, and derived compilers do support this option), AND
# - there are baud rate constants (B<num>) defined in <termios.h>
#   that are not identity-mapped (i.e., B<num> != <num> for at least
#   some <num>).
#
defspeeds="0 50 75 110 134 150 200 300 600 1200 1800 2400 4800 7200 9600 \
14400 19200 28800 33600 38400 57600 76800 115200 153600 230400 307200 \
460800 500000 576000 614400 921600 1000000 1152000 1500000 \
2000000 2500000 3000000 3500000 4000000 5000000 10000000"
(
    sed -n -e "s/^$s*\#$s*define$s$s*B\\([1-9][0-9]*\\)$s.*\$/\\1/p"
    for s in $defspeeds; do echo "$s"; done
) | sort -n | uniq > "$tmp"

cat > "$out" <<'EOF'
#ifndef SPEEDLIST_H
# define SPEEDLIST_H 1

# if 1 \
EOF

sed -e 's/^.*$/ \&\& (!defined(B&) || B& == &) \\/' < "$tmp" >> "$out"

cat >> "$out" <<'EOF'

#  define TERMIOS_SPEED_T_SANE 1

# endif

ATTRIBUTE_CONST
static unsigned long int
baud_to_value (speed_t speed)
{
# ifdef TERMIOS_SPEED_T_SANE
  return speed;
# else
  switch (speed)
    {
EOF

while read n; do
  printf '#  ifdef B%s\n      case B%s: return %s;\n#  endif\n' "$n" "$n" "$n"
done < "$tmp" >> "$out"

cat >> "$out" <<'EOF'
      default: return -1;
    }
# endif
}

ATTRIBUTE_CONST
static speed_t
value_to_baud (unsigned long int value)
{
# ifdef TERMIOS_SPEED_T_SANE
  speed_t speed = value;
  if (speed != value)
    speed = (speed_t) -1;	/* Unrepresentable (overflow?) */
  return speed;
# else
  switch (value)
    {
EOF

while read n; do
  printf '#  ifdef B%s\n      case %s: return B%s;\n#  endif\n' "$n" "$n" "$n"
done < "$tmp" >> "$out"

cat >> "$out" <<'EOF'
      default: return (speed_t) -1;
    }
# endif
}

#endif
EOF