Skip to content

Commit

Permalink
Add serial port support
Browse files Browse the repository at this point in the history
With this patch we add serial port support via -s (--serial) command
line option.
  • Loading branch information
acerv committed Oct 4, 2023
1 parent d6d1509 commit e0d34d6
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,71 @@
* Copyright (c) 2023 Andrea Cervesato <[email protected]>
*/

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#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;
}

0 comments on commit e0d34d6

Please sign in to comment.