Allowing nesting with "main" session, removing io-ii.
[iomenu.git] / main.c
blobad454ca9c0d434b9f1adc1fa8f0ed17e39d5d063
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 [-n] [-N] [-k key] [-s separator] ", stderr);
17 fputs("[-p prompt] [-l lines]\n", stderr);
19 exit(EXIT_FAILURE);
23 int
24 main(int argc, char *argv[])
26 int i, exit_code, tty_fd = open("/dev/tty", O_RDWR);
27 Buffer *buffer = NULL;
28 Opt *opt = malloc(sizeof(Opt));
30 opt->line_numbers = 0;
31 opt->print_number = 0;
32 opt->validate_key = CONTROL('M');
33 opt->separator = NULL;
34 opt->lines = 30;
35 opt->prompt = "";
37 /* command line arguments */
38 for (i = 1; i < argc; i++) {
39 if (argv[i][0] != '-' || strlen(argv[i]) != 2)
40 usage();
42 switch (argv[i][1]) {
43 case 'n':
44 opt->line_numbers = 1;
45 break;
46 case 'N':
47 opt->print_number = 1;
48 opt->line_numbers = 1;
49 break;
50 case 'H':
51 opt->print_header = 1;
52 break;
53 case 'k':
54 opt->validate_key = (argv[++i][0] == '^') ?
55 CONTROL(toupper(argv[i][1])): argv[i][0];
56 break;
57 case 's':
58 opt->separator = argv[++i];
59 break;
60 case 'l':
61 if (sscanf(argv[++i], "%d", &opt->lines) <= 0)
62 die("wrong number format after -l");
63 break;
64 case 'p':
65 if (++i >= argc)
66 die("wrong string format after -p");
67 opt->prompt = argv[i];
68 break;
69 default:
70 usage();
74 /* command line arguments */
75 buffer = fill_buffer(opt->separator);
77 /* set the interface */
78 draw_screen(buffer, tty_fd, opt);
80 /* listen and interact to input */
81 exit_code = input_get(buffer, tty_fd, opt);
83 draw_clear(opt->lines);
85 /* close files descriptors and pointers, and free memory */
86 close(tty_fd);
87 free(opt);
88 free_buffer(buffer);
90 return exit_code;