tty.c 3.93 KB
Newer Older
Mart Lubbers's avatar
Mart Lubbers committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>

#include "Clean.h"

#define INITIAL_BUFFERSIZE 1024
#define die(s) {perror(s);exit(EXIT_FAILURE);}

static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
	B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200,
	B230400};
static int bytesizes[4] = {CS5, CS6, CS7, CS8};
static char *error = "";

Mart Lubbers's avatar
Mart Lubbers committed
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
struct termioslist
{
	int fd;
	struct termios to;
	struct termioslist *next;
};

struct termioslist *head = NULL;

static struct termios *getTermios(int fd)
{
	struct termioslist *h = head;
	while(h != NULL)
		if(h->fd == fd)
			return &h->to;
	return NULL;
}

static void remTermios(int fd)
{
	struct termioslist *beforeit = NULL;
	struct termioslist *it = head;
	while(it != NULL){
		if(it->fd == fd){
			if(beforeit == NULL){
				head = it->next;
			} else {
				beforeit->next = it->next;
			}
			free(it);
			break;
		}
		beforeit = it;
		it = it->next;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
56
57
58
59
60
61
62
63
64
static char *cleanStringToCString(CleanString s)
{
	unsigned long len = CleanStringLength(s);
	char *cs = (char *)malloc(len+1);
	if(cs == NULL)
		die("malloc");
	memcpy(cs, CleanStringCharacters(s), len);
	cs[len] = '\0';
	return cs;
Mart Lubbers's avatar
update    
Mart Lubbers committed
65
}
Mart Lubbers's avatar
Mart Lubbers committed
66

Mart Lubbers's avatar
Mart Lubbers committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
static void addTermios(int fd, struct termios *t)
{
	struct termioslist *new = malloc(sizeof(struct termioslist));
	if(new == NULL)
		die("malloc");
	new->fd = fd;
	memcpy(&new->to, t, sizeof(struct termios));
	new->next = NULL;
	if(head == NULL){
		head = new;
	} else {
		struct termioslist *h = head;
		while(h->next != NULL)
			h = h->next;
		h->next = new;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
85
86
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
	int stopbits, int xonoff, int *status, FILE **f)
Mart Lubbers's avatar
Mart Lubbers committed
87
88
89
90
91
92
93
94
95
96
{
	struct termios tio;
	char *cs_fn = cleanStringToCString(fn);
	int fd = open(cs_fn, O_RDWR | O_NOCTTY);
	*status = 0;
	if(fd < 0){
		error = strerror(errno);
	} else {
		//Get
		tcgetattr(fd, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
97
		addTermios(fd, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
98
99
100
101
102
103
104
105
106
		//Baudrate
		cfsetispeed(&tio, baudrates[baudrate]);
		//Bytesize
		tio.c_cflag |= CS5 | CS6 | CS7 | CS8;
		tio.c_cflag -= CS5 | CS6 | CS7 | CS8;
		tio.c_cflag |= bytesizes[bytesize];
		//Parity
		tio.c_cflag |= PARENB | PARODD | CMSPAR;
		tio.c_cflag -= PARENB | PARODD | CMSPAR;
Mart Lubbers's avatar
Mart Lubbers committed
107
		if(parity == 1)
Mart Lubbers's avatar
Mart Lubbers committed
108
			tio.c_cflag |= PARENB | PARODD;
Mart Lubbers's avatar
Mart Lubbers committed
109
		else if(parity == 2)
Mart Lubbers's avatar
Mart Lubbers committed
110
			tio.c_cflag |= PARENB;
Mart Lubbers's avatar
Mart Lubbers committed
111
		else if(parity == 3)
Mart Lubbers's avatar
Mart Lubbers committed
112
			tio.c_cflag |= PARODD | PARENB | CMSPAR;
Mart Lubbers's avatar
Mart Lubbers committed
113
		else if(parity == 4)
Mart Lubbers's avatar
Mart Lubbers committed
114
115
116
			tio.c_cflag |= PARENB | CMSPAR;
		//Stopbits
		tio.c_cflag |= CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
117
		tio.c_cflag -= stopbits == 0 ? 0 : CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
118
119
120
121
122
123
124
		//Xonoff
		tio.c_cflag |= IXON;
		tio.c_cflag -= xonoff == 1 ? 0 : IXON;
		//Set
		tcsetattr(fd, TCSANOW, &tio);

		*f = fdopen(fd, "r+");
Mart Lubbers's avatar
Mart Lubbers committed
125
		if(*f != NULL){
Mart Lubbers's avatar
Mart Lubbers committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
			setbuf(*f, NULL);
			*status = 1;
		}
		error = strerror(errno);
	}
	free(cs_fn);
}

void ttyerror(CleanString *result)
{
	CleanStringVariable(clean_string, strlen(error));
	*result = (CleanString) clean_string;
	memcpy(CleanStringCharacters(clean_string), error, strlen(error));
	CleanStringLength(clean_string) = strlen(error);
}

void ttyreadc(FILE *fd, int *c, FILE **fdo)
{
	*c = fgetc(fd);
	*fdo = fd;
}

void ttyreadline(FILE *fd, CleanString *result, FILE **fdo)
{
	size_t bufsize = INITIAL_BUFFERSIZE;
	char *buf = (char *)malloc(bufsize+1);
	int c, i = 0;

	if(buf == NULL)
		die("malloc");

	while((c = fgetc(fd)) != EOF && c != '\n'){
		if(i >= bufsize)
			if((buf = realloc(buf, bufsize *= 2)) == NULL)
				die("realloc");
		buf[i++] = c;
	}
	buf[i] = '\0';

	CleanStringVariable(cleanOutput, strlen(buf));
	*result = (CleanString) cleanOutput;
	memcpy(CleanStringCharacters(cleanOutput), buf, strlen(buf));
	CleanStringLength(cleanOutput) = strlen(buf);
	*fdo = fd;
	free(buf);
}

Mart Lubbers's avatar
Mart Lubbers committed
173
FILE *ttywrite(FILE *fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
174
{
Mart Lubbers's avatar
Mart Lubbers committed
175
176
	fwrite(CleanStringCharacters(s), 1, CleanStringLength(s), fd);
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
177
178
}

Mart Lubbers's avatar
Mart Lubbers committed
179
int ttyclose(FILE *fdes)
Mart Lubbers's avatar
Mart Lubbers committed
180
{
Mart Lubbers's avatar
Mart Lubbers committed
181
182
183
184
185
186
	int fd = fileno(fdes);
	struct termios *to = getTermios(fd);
	fflush(fdes);
	tcsetattr(fd, TCSANOW, to);
	remTermios(fd);
	return fclose(fdes) == 0 ? 1 : 0;
Mart Lubbers's avatar
Mart Lubbers committed
187
}