Exemple #1
0
int cmd_help(int argc, char ** argv)
{
    struct cmd_entry * ep;

    if (argc > 2)
        return show_cmd_usage(argv[0]);

    if (argc > 1) {
        ep = (struct cmd_entry *)cmd_tab;
        while (*ep->name != '\0') {
            if (strcmp(argv[1], ep->name) == 0) {
                printf("%s.\n", ep->desc);
                printf("usage: %s %s\n", ep->name,  ep->usage);
                return 0;
            }
            ep++;
        }
        return -1;
    }

    printf("\n");
    ep = (struct cmd_entry *)cmd_tab;
    while (*ep->name != '\0') {
        printf(" %-12s %s\n", ep->name, ep->desc);
        ep++;
    }

    return 0;
}
Exemple #2
0
const jim_subcmd_type *Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type * command_table,
    int argc, Jim_Obj *const *argv)
{
    const jim_subcmd_type *ct;
    const jim_subcmd_type *partial = 0;
    int cmdlen;
    Jim_Obj *cmd;
    const char *cmdstr;
    const char *cmdname;
    int help = 0;

    cmdname = Jim_String(argv[0]);

    if (argc < 2) {
        Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
        Jim_AppendStrings(interp, Jim_GetResult(interp), "wrong # args: should be \"", cmdname,
            " command ...\"\n", NULL);
        Jim_AppendStrings(interp, Jim_GetResult(interp), "Use \"", cmdname, " -help ?command?\" for help", NULL);
        return 0;
    }

    cmd = argv[1];

    /* Check for the help command */
    if (Jim_CompareStringImmediate(interp, cmd, "-help")) {
        if (argc == 2) {
            /* Usage for the command, not the subcommand */
            show_cmd_usage(interp, command_table, argc, argv);
            return &dummy_subcmd;
        }
        help = 1;

        /* Skip the 'help' command */
        cmd = argv[2];
    }

    /* Check for special builtin '-commands' command first */
    if (Jim_CompareStringImmediate(interp, cmd, "-commands")) {
        /* Build the result here */
        Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
        add_commands(interp, command_table, " ");
        return &dummy_subcmd;
    }

    cmdstr = Jim_GetString(cmd, &cmdlen);

    for (ct = command_table; ct->cmd; ct++) {
        if (Jim_CompareStringImmediate(interp, cmd, ct->cmd)) {
            /* Found an exact match */
            break;
        }
        if (strncmp(cmdstr, ct->cmd, cmdlen) == 0) {
            if (partial) {
                /* Ambiguous */
                if (help) {
                    /* Just show the top level help here */
                    show_cmd_usage(interp, command_table, argc, argv);
                    return &dummy_subcmd;
                }
                bad_subcmd(interp, command_table, "ambiguous", argv[0], argv[1 + help]);
                return 0;
            }
            partial = ct;
        }
        continue;
    }

    /* If we had an unambiguous partial match */
    if (partial && !ct->cmd) {
        ct = partial;
    }

    if (!ct->cmd) {
        /* No matching command */
        if (help) {
            /* Just show the top level help here */
            show_cmd_usage(interp, command_table, argc, argv);
            return &dummy_subcmd;
        }
        bad_subcmd(interp, command_table, "unknown", argv[0], argv[1 + help]);
        return 0;
    }

    if (help) {
        Jim_SetResultString(interp, "Usage: ", -1);
        /* subcmd */
        add_cmd_usage(interp, ct, argv[0]);
        return &dummy_subcmd;
    }

    /* Check the number of args */
    if (argc - 2 < ct->minargs || (ct->maxargs >= 0 && argc - 2 > ct->maxargs)) {
        Jim_SetResultString(interp, "wrong # args: should be \"", -1);
        /* subcmd */
        add_cmd_usage(interp, ct, argv[0]);
        Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL);

        return 0;
    }

    /* Good command */
    return ct;
}
Exemple #3
0
int main(int argc, char **argv)
{
	void *dest_addr;
	struct cmd_struct *cmd;
	struct admin_opts opts;
	int i, ret, addr_type, status = 0;
	int cmd_num = ARRAY_SIZE(admin_cmds);
	int rsock;

	ret = parse_opts(argc, argv, &status);
	if (ret)
		exit(status);

	for (i = 0; i < cmd_num; i++) {
		cmd = admin_cmds + i;
		if (!strncmp(argv[optind], cmd->cmd, strlen(cmd->cmd)))
			break;
	}

	if (i == cmd_num) {
		fprintf(stderr, "Non-existing command specified\n");
		show_usage();
		exit(-1);
	}

	if (!strncmp(cmd->cmd, "help", 4)) {
		if (argc - optind <= 1) {
			fprintf(stderr, "No command was specified\n");
			exit(-1);
		}

		for (i = 0; i < cmd_num; i++) {
			cmd = admin_cmds + i;
			if (!strncmp(argv[optind + 1], cmd->cmd,
				     strlen(cmd->cmd)))
				break;
		}

		if (i == cmd_num) {
			fprintf(stderr, "Non-existing command specified\n");
			show_usage();
			exit(-1);
		}

		if (cmd)
			show_cmd_usage(cmd->cmd, admin_cmd_help(cmd->id),
				       admin_get_cmd_opts(cmd->id));

		exit(0);
	}

	if (dest_lid) {
		dest_addr = &dest_lid;
		addr_type = ADMIN_ADDR_TYPE_LID;
	} else {
		if (!dest_gid)
			dest_gid = "::1"; /* local host GID */
		dest_addr = dest_gid;
		addr_type = ADMIN_ADDR_TYPE_GID;
	}

	if (admin_init(short_option, long_option) < 0) {
		fprintf(stderr, "ERROR - unable to init admin client\n");
		exit(-1);
	}

	opts.dev = ca_name;
	opts.src_port = src_port;
	opts.admin_port = admin_port;
	opts.pkey = pkey;
	opts.timeout = timeout;

	rsock = admin_connect(dest_addr, addr_type, &opts);
	if (rsock < 0) {
		fprintf(stderr, "ERROR - unable to connect\n");
		exit(-1);
	}

	optind = 1;
	ret = admin_exec_recursive(rsock, cmd->id, recursive, argc, argv);
	if (ret) {
		fprintf(stderr, "Failed executing '%s' command (%s)\n",
		       cmd->cmd, admin_cmd_help(cmd->id)->desc);
		exit(-1);
	}

	admin_disconnect(rsock);
	admin_cleanup();

	return 0;
}