Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 101 lines (87 sloc) 2.768 kb
e2bbefc Wesley Darlington Deliberately broken.
authored
1 /* cmdline.c
2 *
3 * Parse command line arguments for charade.
4 */
c02878a Wesley Darlington Still figuring out command line arguments.
authored
5
12d3397 Wesley Darlington Sanify the copyright messages.
authored
6 #include <getopt.h>
c02878a Wesley Darlington Still figuring out command line arguments.
authored
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
12d3397 Wesley Darlington Sanify the copyright messages.
authored
11 #include "copyright.h"
c02878a Wesley Darlington Still figuring out command line arguments.
authored
12 #include "cmdline.h"
13 #include "eprintf.h"
14
15 int g_csh_flag = 0;
16 int g_sh_flag = 0;
17 int g_kill_flag = 0;
77c80f6 Wesley Darlington OK, it doesn't exit immediately after working, which is nice.
authored
18 int g_dontfork_flag = 0;
c02878a Wesley Darlington Still figuring out command line arguments.
authored
19 char *g_socket_name = 0;
20
3485e8b Wesley Darlington Clean build, zero functionality.
authored
21 int g_subprocess_argc;
22 char **g_subprocess_argv;
23
c02878a Wesley Darlington Still figuring out command line arguments.
authored
24 static void
25 usage(void)
26 {
27 EPRINTF(0, "usage: charade [options] [command [arg ...]]\n");
28 EPRINTF(0, "Options:\n");
29 EPRINTF(0, " -c Generate C-shell commands on stdout.\n");
30 EPRINTF(0, " -s Generate Bourne shell commands on stdout.\n");
31 EPRINTF(0, " -k Kill the current agent.\n");
77c80f6 Wesley Darlington OK, it doesn't exit immediately after working, which is nice.
authored
32 EPRINTF(0, " -d Don't fork.\n");
33 EPRINTF(0, " -v [-v ...] More trace output.\n");
c02878a Wesley Darlington Still figuring out command line arguments.
authored
34 EPRINTF(0, " -a socket Bind agent socket to given name.\n");
12d3397 Wesley Darlington Sanify the copyright messages.
authored
35 EPRINTF(0, " --copyright Print out copyright message & licence.\n");
c02878a Wesley Darlington Still figuring out command line arguments.
authored
36 exit(1);
37 }
38
12d3397 Wesley Darlington Sanify the copyright messages.
authored
39 static struct option opts[] = {
40 {"copyright", 0, NULL, 'C'},
41 {NULL, 0, NULL, 0}
42 };
43
c02878a Wesley Darlington Still figuring out command line arguments.
authored
44 void
e2bbefc Wesley Darlington Deliberately broken.
authored
45 parse_cmdline(int argc, char **argv)
c02878a Wesley Darlington Still figuring out command line arguments.
authored
46 {
47 int ch;
989e640 Wesley Darlington Fix link errors on getopt variables.
authored
48
49 /* I seem to get link errors when I declare these getopt variables.
50 Things seem to work fine when I don't declare them, so I'm
51 just commenting them out!
c02878a Wesley Darlington Still figuring out command line arguments.
authored
52 extern int optind, opterr;
53 extern char *optarg;
989e640 Wesley Darlington Fix link errors on getopt variables.
authored
54 */
c02878a Wesley Darlington Still figuring out command line arguments.
authored
55
12d3397 Wesley Darlington Sanify the copyright messages.
authored
56 while (-1 != (ch = getopt_long(argc, argv, "cskdva:", opts, NULL))) {
c02878a Wesley Darlington Still figuring out command line arguments.
authored
57 switch (ch) {
58 case 'c': ++g_csh_flag;
59 break;
60 case 's': ++g_sh_flag;
61 break;
62 case 'k': ++g_kill_flag;
63 break;
77c80f6 Wesley Darlington OK, it doesn't exit immediately after working, which is nice.
authored
64 case 'd': g_dontfork_flag = 1;
65 break;
66 case 'v': louder();
c02878a Wesley Darlington Still figuring out command line arguments.
authored
67 break;
68 case 'a': g_socket_name = optarg;
69 break;
12d3397 Wesley Darlington Sanify the copyright messages.
authored
70 case 'C': print_copyright();
71 break;
c02878a Wesley Darlington Still figuring out command line arguments.
authored
72 default: usage();
73 }
74 }
e2bbefc Wesley Darlington Deliberately broken.
authored
75 argc -= optind;
76 argv += optind;
c02878a Wesley Darlington Still figuring out command line arguments.
authored
77
78 // If any flags given, there better not be process-to-run-args...
e2bbefc Wesley Darlington Deliberately broken.
authored
79 if (argc > 0 && (g_csh_flag || g_sh_flag || g_kill_flag)) {
c02878a Wesley Darlington Still figuring out command line arguments.
authored
80 EPRINTF(1, "Can't produce shell commands *and* run subprocess!\n");
81 usage();
82 }
83
84 // If there's no shell and no process-to-run-args, assume C shell...
e2bbefc Wesley Darlington Deliberately broken.
authored
85 if (argc == 0 && !g_csh_flag && !g_sh_flag) {
c02878a Wesley Darlington Still figuring out command line arguments.
authored
86 char *shell = getenv("SHELL");
87 size_t len;
88 if (shell != NULL && (len = strlen(shell)) > 2
89 && strncmp(shell + len - 3, "csh", 3) == 0)
90 g_csh_flag = 1;
91 }
e2bbefc Wesley Darlington Deliberately broken.
authored
92
000e155 Wesley Darlington Tidy up a bit.
authored
93 if (argc > 0 && g_dontfork_flag) {
94 EPRINTF(1, "Can't not fork *and* run a subprocess!\n");
95 usage();
96 }
97
e2bbefc Wesley Darlington Deliberately broken.
authored
98 g_subprocess_argc = argc;
99 g_subprocess_argv = argv;
c02878a Wesley Darlington Still figuring out command line arguments.
authored
100 }
Something went wrong with that request. Please try again.