Fixing iomenu not resetting the terminal on cancel
[iomenu.git] / main.c
blobb36bd2731708854bec0f77556e58357bd119f930
1 #include <ctype.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7 #include <termios.h>
8 #include <unistd.h>
10 #include "main.h"
11 #include "util.h"
12 #include "buffer.h"
13 #include "draw.h"
14 #include "input.h"
16 int
17 main(int argc, char *argv[])
19 int i;
20 Buffer *buffer = NULL;
21 int offset = 3;
22 int tty_fd = open("/dev/tty", O_RDWR);
23 Opt *opt = malloc(sizeof(Opt));
25 opt->line_numbers = FALSE;
26 opt->complete = FALSE;
27 opt->print_numbers = FALSE;
28 opt->validate_key = CONTROL('M');
29 opt->separator = NULL;
30 opt->lines = 30;
31 opt->prompt = "";
33 /* command line arguments */
34 for (i = 0; i < argc; i++) {
35 if (argv[i][0] == '-') {
36 switch (argv[i][1]) {
37 case 'n':
38 opt->line_numbers = TRUE;
39 break;
40 case 'c':
41 opt->complete = TRUE;
42 break;
43 case 'N':
44 opt->print_numbers = TRUE;
45 opt->line_numbers = TRUE;
46 break;
47 case 'k':
48 opt->validate_key = (argv[i++][0] == '^') ?
49 CONTROL(argv[i][1]):
50 argv[i][0];
51 break;
52 case 's':
53 opt->separator = argv[++i];
54 break;
55 case 'l':
56 if (sscanf(argv[++i], "%d", &opt->lines) <= 0)
57 die("Wrong number format after -l.");
58 break;
59 case 'p':
60 opt->prompt = argv[++i];
61 break;
66 /* command line arguments */
67 buffer = fill_buffer(opt->separator);
69 /* set the interface */
70 filter_lines(buffer, opt);
71 draw_screen(buffer, offset, tty_fd, opt);
73 /* listen and interact to input */
74 input_get(buffer, offset, tty_fd, opt);
76 draw_clear(opt->lines);
78 /* close files descriptors and pointers, and free memory */
79 close(tty_fd);
80 free(opt);
82 return 0;