Merge pull request #90 from alobbs/fix-89

Ports conmon from getopt() to glib's parsing mechanism
This commit is contained in:
Antonio Murdaca 2016-10-07 13:42:51 +02:00 committed by GitHub
commit 8601a70a39

View file

@ -60,13 +60,20 @@ static void tty_restore(void)
#define CMD_SIZE 1024 #define CMD_SIZE 1024
#define MAX_EVENTS 10 #define MAX_EVENTS 10
static bool terminal = false;
static char *cid = NULL;
static char *runtime_path = NULL;
static GOptionEntry entries[] =
{
{ "terminal", 't', 0, G_OPTION_ARG_NONE, &terminal, "Terminal", NULL },
{ "cid", 'c', 0, G_OPTION_ARG_STRING, &cid, "Container ID", NULL },
{ "runtime", 'r', 0, G_OPTION_ARG_STRING, &runtime_path, "Runtime path", NULL },
{ NULL }
};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int ret; int ret;
int opt;
bool terminal = false;
const char *cid = NULL;
const char *runtime_path = NULL;
char cmd[CMD_SIZE]; char cmd[CMD_SIZE];
GError *err = NULL; GError *err = NULL;
_cleanup_free_ char *contents; _cleanup_free_ char *contents;
@ -84,40 +91,24 @@ int main(int argc, char *argv[])
int child_pipe = -1; int child_pipe = -1;
char *sync_pipe, *endptr; char *sync_pipe, *endptr;
int len; int len;
GError *error = NULL;
GOptionContext *context;
while ((opt = getopt(argc, argv, "tc:r:")) != -1) { /* Command line parameters */
switch (opt) { context = g_option_context_new ("- conmon utility");
case 't': g_option_context_add_main_entries (context, entries, "conmon");
terminal = true; if (!g_option_context_parse (context, &argc, &argv, &error)) {
break; g_print ("option parsing failed: %s\n", error->message);
case 'c': exit (1);
cid = optarg;
break;
case 'r':
runtime_path = optarg;
break;
case '?':
if (optopt == 'c' || optopt == 'r')
nexit("Option -%c requires an argument.",
optopt);
else if (isprint(optopt))
nexit("Unknown option `-%c'.", optopt);
else
nexit("Unknown option character `\\x%x'.\n",
optopt);
default:
nexit
("Usage: %s -r runtime_path [-c container_id] [-t]",
argv[0]);
}
} }
if (cid == NULL) if (cid == NULL)
nexit("Container ID not provided"); nexit("Container ID not provided. Use --cid");
if (runtime_path == NULL) if (runtime_path == NULL)
nexit("Runtime path not provided"); nexit("Runtime path not provided. Use --runtime");
/* Environment variables */
sync_pipe = getenv("_OCI_SYNCPIPE"); sync_pipe = getenv("_OCI_SYNCPIPE");
if (sync_pipe) { if (sync_pipe) {
errno = 0; errno = 0;