int main(int argc, char *argv[]) { bool source = true; struct agent_data_t data; char *password = NULL; enum action verb = ACTION_NONE; enum agent type = AGENT_DEFAULT; void (*print_env)(struct agent_data_t *data) = print_sh_env; static const struct option opts[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, { "add", no_argument, 0, 'a' }, { "clear", no_argument, 0, 'k' }, { "kill", no_argument, 0, 'K' }, { "list", no_argument, 0, 'l' }, { "unlock", optional_argument, 0, 'u' }, { "print", no_argument, 0, 'p' }, { "sh", no_argument, 0, 's' }, { "csh", no_argument, 0, 'c' }, { "fish", no_argument, 0, 'f' }, { "agent", required_argument, 0, 't' }, { 0, 0, 0, 0 } }; while (true) { int opt = getopt_long(argc, argv, "hvakKlu::pscft:", opts, NULL); if (opt == -1) break; switch (opt) { case 'h': usage(stdout); break; case 'v': printf("%s %s\n", program_invocation_short_name, ENVOY_VERSION); return 0; case 'a': verb = ACTION_FORCE_ADD; break; case 'k': verb = ACTION_CLEAR; source = false; break; case 'K': verb = ACTION_KILL; source = false; break; case 'l': verb = ACTION_LIST; break; case 'u': verb = ACTION_UNLOCK; password = optarg; break; case 'p': verb = ACTION_PRINT; break; case 's': print_env = print_sh_env; break; case 'c': print_env = print_csh_env; break; case 'f': print_env = print_fish_env; break; case 't': type = lookup_agent(optarg); if (type < 0) errx(EXIT_FAILURE, "unknown agent: %s", optarg); break; default: usage(stderr); } } if (get_agent(&data, type, source) < 0) errx(EXIT_FAILURE, "recieved no data, did the agent fail to start?"); if (data.status == ENVOY_STOPPED) return 0; if (source) source_env(&data); switch (verb) { case ACTION_PRINT: print_env(&data); /* fall through */ case ACTION_NONE: if (data.status != ENVOY_STARTED || data.type == AGENT_GPG_AGENT) break; /* fall through */ case ACTION_FORCE_ADD: add_keys(&argv[optind], argc - optind); break; case ACTION_CLEAR: if (data.type == AGENT_GPG_AGENT) kill(data.pid, SIGHUP); else errx(EXIT_FAILURE, "only gpg-agent supports this operation"); break; case ACTION_KILL: kill(data.pid, SIGTERM); break; case ACTION_LIST: execlp("ssh-add", "ssh-add", "-l", NULL); err(EXIT_FAILURE, "failed to launch ssh-add"); case ACTION_UNLOCK: unlock(&data, password); break; default: break; } return 0; }
int main(int argc, char *argv[]) { bool source = true; bool defer = false; struct agent_data_t data; char *password = NULL; enum action verb = ACTION_NONE; enum agent type = AGENT_DEFAULT; void (*print_env)(struct agent_data_t *data) = print_sh_env; static const struct option opts[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, { "defer", no_argument, 0, 'd' }, { "add", no_argument, 0, 'a' }, { "expunge", no_argument, 0, 'x' }, { "kill", no_argument, 0, 'k' }, { "reload", no_argument, 0, 'r' }, { "list", no_argument, 0, 'l' }, { "unlock", optional_argument, 0, 'u' }, { "print", no_argument, 0, 'p' }, { "sh", no_argument, 0, 's' }, { "csh", no_argument, 0, 'c' }, { "fish", no_argument, 0, 'f' }, { "agent", required_argument, 0, 't' }, { 0, 0, 0, 0 } }; while (true) { int opt = getopt_long(argc, argv, "hvdaxkrlu::pscft:", opts, NULL); if (opt == -1) break; switch (opt) { case 'h': usage(stdout); break; case 'v': printf("%s %s\n", program_invocation_short_name, ENVOY_VERSION); return 0; case 'd': defer = true; break; case 'a': verb = ACTION_FORCE_ADD; defer = false; break; case 'x': verb = ACTION_FORCE_EXPUNGE; defer = false; break; case 'k': verb = ACTION_KILL; source = false; break; case 'r': verb = ACTION_RELOAD; source = false; break; case 'l': verb = ACTION_LIST; break; case 'u': verb = ACTION_UNLOCK; password = optarg; break; case 'p': verb = ACTION_PRINT; break; case 's': print_env = print_sh_env; break; case 'c': print_env = print_csh_env; break; case 'f': print_env = print_fish_env; break; case 't': type = lookup_agent(optarg); if (type < 0) errx(EXIT_FAILURE, "unknown agent: %s", optarg); break; default: usage(stderr); } } if (get_agent(&data, type, source, defer) < 0) errx(EXIT_FAILURE, "recieved no data, did the agent fail to start?"); if (data.status == ENVOY_STOPPED) return 0; if (source) source_env(&data); switch (verb) { case ACTION_PRINT: print_env(&data); /* fall through */ case ACTION_NONE: if (data.type == AGENT_GPG_AGENT || !agent_started(&data)) break; if (defer) break; /* fall through */ case ACTION_FORCE_ADD: add_keys(&argv[optind], argc - optind); break; case ACTION_FORCE_EXPUNGE: expunge_keys(&argv[optind], argc - optind); break; case ACTION_KILL: if (envoy_kill_agent(type) < 0) errx(EXIT_FAILURE, "failed to kill agent"); break; case ACTION_RELOAD: reload_agent(&data); break; case ACTION_LIST: execlp("ssh-add", "ssh-add", "-l", NULL); err(EXIT_FAILURE, "failed to launch ssh-add"); case ACTION_UNLOCK: unlock(&data, password); break; default: break; } return 0; }