From e0d34d6e6c3898791f2a7f548e8aed5cadb5c262 Mon Sep 17 00:00:00 2001 From: Andrea Cervesato Date: Wed, 4 Oct 2023 13:56:32 +0200 Subject: [PATCH] Add serial port support With this patch we add serial port support via -s (--serial) command line option. --- main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index baff766..01ec37c 100644 --- a/main.c +++ b/main.c @@ -4,17 +4,71 @@ * Copyright (c) 2023 Andrea Cervesato */ -#include +#include #include +#include +#include +#include +#include +#include #include "ltx.h" -int main(void) +int main(int argc, char *argv[]) { + int stdin_fd = STDIN_FILENO; + int stdout_fd = STDOUT_FILENO;; + + if (argc >= 2 && strcmp(argv[1], "-i") && strcmp(argv[1], "--interactive")) { + if (!strcmp(argv[1], "-s") || !strcmp(argv[1], "--serial")) { + if (argc != 3) { + printf("Serial port is not defined\n"); + return 1; + } + + const char *port = argv[2]; + + if (access(port, F_OK) != 0) { + printf("%s doesn't exist\n", port); + return 1; + } + + stdin_fd = open(port, O_RDONLY | O_NOCTTY); + if (stdin_fd == -1) { + printf("Can't open stdin %s (%s)\n", port, strerror(errno)); + return 1; + } + + stdout_fd = open(port, O_WRONLY | O_NOCTTY); + if (stdout_fd == -1) { + printf("Can't open stdout %s (%s)\n", port, strerror(errno)); + return 1; + } + } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { + printf( + "Usage: ./ltx [-h|-i|-s]\n\n" + " -i | --interactive communicate via stdin|stdout (default)\n" + " -s | --serial communicate via serial port\n" + " -h | --help print help message\n" + " -v | --version print version\n\n" + ); + return 0; + } else if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) { + printf("%s\n", VERSION); + return 0; + } else { + printf("Unknown parameter: %s\n", argv[1]); + return 1; + } + } + struct ltx_session *session; - session = ltx_session_init(STDIN_FILENO, STDOUT_FILENO); + 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; }