Added entries to the todo after a Reddit discussion
[iomenu.git] / main.c
blob798945a6768497c84e7bbb65a8f62d167fb54c4e
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"
13 void
14 usage(void)
16 fputs("usage: iomenu ", stdout);
17 puts("[-c] [-n] [-N] [-k key] [-s separator] [-p prompt] [-l lines]");
19 exit(EXIT_FAILURE);
23 int
24 main(int argc, char *argv[])
26 int i;
27 Buffer *buffer = NULL;
28 int tty_fd = open("/dev/tty", O_RDWR);
29 Opt *opt = malloc(sizeof(Opt));
31 opt->line_numbers = FALSE;
32 opt->complete = FALSE;
33 opt->print_number = FALSE;
34 opt->validate_key = CONTROL('M');
35 opt->separator = NULL;
36 opt->lines = 30;
37 opt->prompt = "";
39 /* command line arguments */
40 for (i = 1; i < argc; i++) {
41 if (argv[i][0] == '-' && strlen(argv[i]) == 2) {
42 switch (argv[i][1]) {
43 case 'c':
44 opt->complete = TRUE;
45 break;
46 case 'n':
47 opt->line_numbers = TRUE;
48 break;
49 case 'N':
50 opt->print_number = TRUE;
51 opt->line_numbers = TRUE;
52 break;
53 case 'k':
54 opt->validate_key = (argv[i++][0] == '^') ?
55 CONTROL(argv[i][1]):
56 argv[i][0];
57 break;
58 case 's':
59 opt->separator = argv[++i];
60 break;
61 case 'l':
62 if (sscanf(argv[++i], "%d", &opt->lines) <= 0)
63 die("Wrong number format after -l.");
64 break;
65 case 'p':
66 opt->prompt = argv[++i];
67 break;
68 default:
69 usage();
71 } else {
72 usage();
76 /* command line arguments */
77 buffer = fill_buffer(opt->separator);
79 /* set the interface */
80 filter_lines(buffer, opt);
81 draw_screen(buffer, tty_fd, opt);
83 /* listen and interact to input */
84 input_get(buffer, tty_fd, opt);
86 draw_clear(opt->lines);
88 /* close files descriptors and pointers, and free memory */
89 close(tty_fd);
90 free(opt);
91 free_buffer(buffer);
93 return 0;