summaryrefslogtreecommitdiffstats
path: root/termbaud.c
blob: 620a0058dbcd7deef131916c5e28201a6810704f (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
53
54
55
#include <stddef.h>
#include <errno.h>

#include "termbaud_nontrivial.h" /* Don't want inline definitions */
#include "termistriv.h"

#ifdef CFENCSPEED_IS_TRIVIAL

speed_t cfencspeed(baud_t baud)
{
	return baud;
}
baud_t cfdecspeed(speed_t speed)
{
	return speed;
}

#else /* CFENCSPEED_IS_TRIVIAL */

struct speed_baud {
  speed_t speed;
  baud_t baud;
};

static const struct speed_baud speed_baud_tbl[] = {
#include "speedtbl.h"
};

#define NSPEEDS ((sizeof speed_baud_tbl)/(sizeof speed_baud_tbl[0]))

speed_t cfencspeed(baud_t b)
{
  size_t i;

  for (i = 0; i < NSPEEDS; i++)
    if (b == speed_baud_tbl[i].baud)
      return speed_baud_tbl[i].speed;

  errno = EINVAL;
  return (speed_t)-1;
}

baud_t cfdecspeed(speed_t s)
{
  size_t i;

  for (i = 0; i < NSPEEDS; i++)
    if (s == speed_baud_tbl[i].speed)
      return speed_baud_tbl[i].baud;

  errno = EINVAL;
  return (baud_t)-1;
}

#endif	/* CFENCSPEED_IS_TRIVIAL */