Exemple #1
0
int handle_prrn(char *filename)
{
	struct options opts;
	char fmt_drc[11];
	char type[4];
	char drc[9];
	int rc = 0;
	FILE *fd;

	fd = fopen(filename, "r");
	if (!fd) {
		say(ERROR, "Failed to open the file %s\n", filename);
		return -1;
	}

	set_output_level(4);

	memset(&opts, 0, sizeof(opts));

	while (fscanf(fd, "%3s %8s\n", type, drc) == 2) {
		/* Set up options struct */
		opts.ctype = type;
		sprintf(fmt_drc, "0x%s", drc);
		opts.usr_drc_name = fmt_drc;

		set_timeout(PRRN_TIMEOUT);

		if (!strcmp(type, "mem")) {
			opts.action = REMOVE;
			rc = drslot_chrp_mem(&opts);
			if (rc)
				continue;

			opts.action = ADD;
			drslot_chrp_mem(&opts);
		} else if (!strcmp(type, "cpu")) {
			opts.action = REMOVE;
			rc = drslot_chrp_cpu(&opts);
			if (rc)
				continue;

			opts.action = ADD;
			drslot_chrp_cpu(&opts);
		} else {
			say(ERROR, "Device type \"%s\" not recognized.\n",
			    type);
			continue;
		}
	}

	return 0;
}
Exemple #2
0
/**
 * parse_options
 * @brief parse the command line options and fillin the cmd_opts struct
 *
 * @param argc
 * @param argv
 * @param opts
 */
static void
parse_options(int argc, char *argv[], struct cmd_opts *opts)
{
	int c;

	while ((c = getopt(argc, argv, "abc:d:F:ops:w:")) != EOF) {
		switch (c) {
		    case 'a':
			opts->a_flag = 1;
			break;

		    case 'b':
			opts->b_flag = 1;
			break;

		    case 'c':
			if (! strcmp(optarg, "phb"))
				opts->slot_type = PHB;
			else if (! strcmp(optarg, "slot"))
				opts->slot_type = SLOT;
			else if (! strcmp(optarg, "pci"))
				opts->slot_type = PCI;
			else if (! strcmp(optarg, "cpu"))
				opts->slot_type = CPU;
			else if (! strcmp(optarg, "mem"))
				opts->slot_type = MEM;
			else if (! strcmp(optarg, "port"))
				opts->slot_type = PORT;
			else {
				printf("\nThe specified connector type "
				       "is invalid.\n\n");
				usage();
			}
			break;

		    case 'd':
			set_output_level(atoi(optarg));
			break;

		    case 'F':
			opts->delim = optarg;
			/* make sure the arg specified is only one character
			 * long and is not the '%' character which would
			 * confuse the formatting.
			 */
			if ((opts->delim[1] != '\0')
			    || (opts->delim[0] == '%')) {
				say(ERROR, "You may specify only one character "
				    "for the -F option,\nand it must not "
				    "be the %% character.\n");
				exit(1);
			}
			break;

		    case 'o':
			opts->o_flag = 1;
			break;

		    case 'p':
			opts->p_flag = 1;
			break;

		    case 's':
			opts->s_name = optarg;
			break;

		    case 'w':
			opts->timeout = strtoul(optarg, NULL, 10) * 60;
			if (opts->timeout < 0)
				usage();
			break;

		    default:
			usage();
			break;
		}
	}

	/* Validate the options */
	switch (opts->slot_type) {
	    case SLOT:
	    case PORT:
		/* The a,b,o,p flags are not valid for slot */
		if (opts->a_flag || opts->b_flag || opts->o_flag ||
		    opts->p_flag)
			usage();

		/* Now, to make the code work right (which is wrong) we
		 * need to set the a and o flags if the s flag wasn't
		 * specified.
		 */
		if (opts->s_name == NULL) {
			opts->a_flag = 1;
			opts->o_flag = 1;
		}

		break;

	    case PHB:
		/* The a,b,F,o,p options are not valid for phb */
		if (opts->a_flag || opts->b_flag || opts->delim ||
		    opts->o_flag || opts->p_flag)
			usage();
		break;

	    case PCI:
		/* The b,p flags are valid for pci */
		if (opts->b_flag || opts->p_flag)
			usage();

		/* If no flags specified, then set a_flag and o_flag
		 * so that all slots will be formatted in the output
		 */
		if ((! opts->a_flag) && (! opts->o_flag)
		    && (opts->s_name == NULL)) {
			opts->a_flag = 1;
			opts->o_flag = 1;
		}

		break;

	    case CPU:
		/* The a,F,o,s options are not valid for cpu */
		if (opts->a_flag || opts->delim || opts->o_flag ||
		    opts->s_name)
			usage();

		if (opts->b_flag && opts->p_flag) {
			say(ERROR, "You cannot specify both the -b and -p "
			    "options.\n");
			usage();
		}

		break;
	}
}