annotate rvinterf/asyncshell/main.c @ 915:7b805007dcfc

etmsync: slight "main" refactoring in preparation for multiple programs
author Space Falcon <falcon@ivan.Harhan.ORG>
date Tue, 08 Sep 2015 18:26:42 +0000
parents 5e46679bdb6a
children bd873572ef2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
872
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module contains the main() function for fc-shell.
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 */
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/errno.h>
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <stdlib.h>
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <unistd.h>
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11 char *socket_pathname = "/tmp/rvinterf_socket";
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12 char *rvinterf_ttyport;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 int ttyhacks, dflag;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 int sock;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17 extern char *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 main(argc, argv)
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 char **argv;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22 extern int optind;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 extern char *optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 int c, sopt = 0;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25 fd_set fds;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 while ((c = getopt(argc, argv, "B:dl:p:s:w:")) != EOF)
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 switch (c) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29 case 'B':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 rvinterf_Bopt = optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 case 'd':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33 dflag++;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 case 'l':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 rvinterf_lopt = optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 case 'p':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39 rvinterf_ttyport = optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 case 's':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 socket_pathname = optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 sopt++;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 case 'w':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 rvinterf_wopt = optarg;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 case '?':
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49 default:
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 usage: fprintf(stderr,
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 "usage: %s [options] [command]\n", argv[0]);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 exit(1);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
54 if (rvinterf_ttyport) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
55 if (sopt) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
56 fprintf(stderr,
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
57 "%s error: -p and -s options are mutually exclusive\n",
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
58 argv[0]);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
59 exit(1);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
60 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
61 launch_rvinterf(rvinterf_ttyport);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
62 } else {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
63 if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
64 fprintf(stderr,
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
65 "%s error: -B, -l and -w options are meaningful only when launching rvinterf\n",
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
66 argv[0]);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
67 exit(1);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
68 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
69 connect_local_socket();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
70 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
71
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
72 ttyhacks = isatty(0) && !dflag;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
73 init();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
74 tty_init();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
75 for (;;) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
76 FD_ZERO(&fds);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
77 FD_SET(0, &fds);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
78 FD_SET(sock, &fds);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
79 c = select(sock+1, &fds, 0, 0, 0);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
80 if (c < 0) {
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
81 if (errno == EINTR)
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
82 continue;
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
83 tty_cleanup();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
84 perror("select");
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
85 exit(1);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
86 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
87 if (FD_ISSET(0, &fds))
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
88 handle_tty_input();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
89 if (FD_ISSET(sock, &fds))
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
90 handle_rvinterf_input();
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
91 fflush(stdout);
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
92 }
5e46679bdb6a fc-shell skeleton created
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
93 }