-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (70 loc) · 1.62 KB
/
main.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2022 Richard Palethorpe <[email protected]>
* Copyright (c) 2023 Andrea Cervesato <[email protected]>
*/
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ltx.h"
int main(int argc, char *argv[])
{
int opt;
int option_index = 0;
char *serial_port = NULL;
int stdin_fd = STDIN_FILENO;
int stdout_fd = STDOUT_FILENO;
static struct option long_options[] = {
{"serial", required_argument, NULL, 's'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(
argc,
argv,
"s:vh",
long_options,
&option_index)) != -1) {
switch(opt) {
case 's':
serial_port = optarg;
break;
case 'v':
printf("%s\n", VERSION);
return 0;
case 'h':
default:
printf(
"Usage: ltx [-s|-v|-h]\n\n"
" -s | --serial communicate via serial port\n"
" -v | --version print version\n"
" -h | --help print help message\n\n"
);
return 0;
}
}
if (serial_port) {
if (access(serial_port, F_OK) != 0) {
printf("%s doesn't exist\n", serial_port);
return 1;
}
stdin_fd = stdout_fd = open(serial_port, O_RDWR | O_NOCTTY);
if (stdin_fd == -1) {
printf("Can't open %s (%s)\n", serial_port, strerror(errno));
return 1;
}
}
struct ltx_session *session;
session = ltx_session_init(stdin_fd, stdout_fd);
ltx_start_event_loop(session);
ltx_session_destroy(session);
close(stdin_fd);
close(stdout_fd);
return 0;
}