int
realm_list (RealmClient *client,
            int argc,
            char *argv[])
{
	GOptionContext *context;
	gboolean arg_all = FALSE;
	gboolean arg_name_only = FALSE;
	GError *error = NULL;
	gint ret = 0;

	GOptionEntry option_entries[] = {
		{ "all", 'a', 0, G_OPTION_ARG_NONE, &arg_all, N_("Show all realms"), NULL },
		{ "name-only", 'n', 0, G_OPTION_ARG_NONE, &arg_name_only, N_("Show only the names"), NULL },
		{ NULL, }
	};

	context = g_option_context_new ("realm");
	g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
	g_option_context_add_main_entries (context, option_entries, NULL);
	g_option_context_add_main_entries (context, realm_global_options, NULL);

	if (!g_option_context_parse (context, &argc, &argv, &error)) {
		g_printerr ("%s: %s\n", g_get_prgname (), error->message);
		g_error_free (error);
		ret = 2;

	} else if (argc == 0) {
		g_printerr ("%s: no arguments necessary\n", g_get_prgname ());
		ret = 2;

	} else {
		ret = perform_list (client, arg_all, arg_name_only);
	}

	g_option_context_free (context);
	return ret;
}
Ejemplo n.º 2
0
Archivo: busio.c Proyecto: joaohf/dtree
int main(int argc, char **argv)
{
	// action to be performed
	int act = '\0';

	// address offset to the device
	uint32_t addr  = 0;
	int addr_valid = 0;

	// value to be written
	uint32_t value = 0;
	int value_valid = 0;

	// number of bytes to be written or read
	int len = 4;

	// used device-tree to get address
	const char *dtree = DTREE_PATH;

	// name of the device to access
	const char *dev   = NULL;

	// input for -w when -d is missing
	FILE *finput = stdin;

	int opt;
	opterr = 0;

	while((opt = getopt(argc, argv, GETOPT_STR)) != -1) {
		switch(opt) {
		case 'h':
			return print_help(argv[0]);
		
		case 'V':
			return print_version(argv[0]);

		case 'v':
			verbosity += 1;
			break;

		case 'l':
			act = opt;
			break;

		case 'r':
		case 'w':
			dev = optarg;
			act = opt;
			break;

		case 't':
			dtree = optarg;
			break;

		case 'a':
			addr = parse_addr(optarg);
			addr_valid = 1;
			break;

		case 'd':
			value = parse_value(optarg);
			value_valid = 1;
			break;

		case '1':
		case '2':
		case '3':
		case '4':
			len = opt - '0';
			break;

		case '?':
		default:
			return print_opterr(optopt);
		}
	}

	verbosity_printf(1, "Attempt to open device-tree '%s'", dtree);
	if(dtree_open(dtree) != 0) {
		fprintf(stderr, "dtree_open(%s): %s\n", dtree, dtree_errstr());
		return 1;
	}

	int err = 0;

	switch(act) {
	case 'l':
		err = perform_list();
		goto exit;

	case 'r':
		assert(dev != NULL);
		if(addr_valid) {
			err = perform_read(dev, addr, len);
			goto exit;
		}

		break;

	case 'w':
		assert(dev != NULL);

		if(!addr_valid)
			break;

		if(value_valid) {
			err = perform_write(dev, addr, len, value);
			goto exit;
		}
		else {
			verbosity_printf(1, "Reading from <stdin>");
			err = perform_file_write(dev, addr, len, finput);
			goto exit;
		}

		break;

	case '\0':
		fprintf(stderr, "No action has been specified\n");
		err = 1;
		goto exit;

	default:
		fprintf(stderr, "Unknown action to be performed: %c\n", act);
		err = 1;
		goto exit;
	}

	if(!addr_valid) {
		fprintf(stderr, "Address option (-a) is missing\n");
		err = 1;
		goto exit;
	}
	if(!value_valid) {
		fprintf(stderr, "Data option (-d) is missing\n");
		err = 1;
		goto exit;
	}

	fprintf(stderr, "Unknown option error\n");
	err = 1;

exit:
	dtree_close();
	return err;
}