]> git.sur5r.net Git - tio/blob - src/main.c
New upstream version 1.34
[tio] / src / main.c
1 /*
2  * tio - a simple TTY terminal I/O tool
3  *
4  * Copyright (c) 2014-2022  Martin Lund
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include "options.h"
27 #include "tty.h"
28 #include "log.h"
29 #include "error.h"
30 #include "print.h"
31
32 int main(int argc, char *argv[])
33 {
34     int status = 0;
35
36     /* Add error exit handler */
37     atexit(&error_exit);
38
39     /* Parse options */
40     parse_options(argc, argv);
41
42     /* List available serial devices */
43     if (option.list_devices)
44     {
45         list_serial_devices();
46         return status;
47     }
48
49     /* Configure tty device */
50     tty_configure();
51
52     /* Configure input terminal */
53     stdin_configure();
54
55     /* Configure output terminal */
56     stdout_configure();
57
58     /* Add log exit handler */
59     atexit(&log_exit);
60
61     /* Create log file */
62     if (option.log)
63         log_open(option.log_filename);
64
65     /* Enable ANSI text formatting (colors etc.) */
66     print_enable_ansi_formatting();
67
68     /* Print launch hints */
69     tio_printf("tio v%s", VERSION);
70     tio_printf("Press ctrl-t q to quit");
71
72     /* Connect to tty device */
73     if (option.no_autoconnect)
74         status = tty_connect();
75     else
76     {
77         /* Enter connect loop */
78         while (true)
79         {
80             tty_wait_for_device();
81             status = tty_connect();
82         }
83     }
84
85     return status;
86 }