void		ft_get_options(t_args *args, char *av)
{
	if (args->options->f == 1)
		args->options->color = 0;
	else if (is_legal(av) == 1)
		find_options(args->options, av);
	else
		invalid_option(args, av);
}
void	ft_get_options(t_args *args, char *av)
{
	if (ft_strcmp(av, "--color") == 0)
	{
		if (args->options->f == 0)
			args->options->color = 1;
	}
	else if (is_legal(av) == 1)
		find_options(args->options, av);
	else
		invalid_option(args, av);
}
Exemple #3
0
static int	check_invalid_options(char *s)
{
	char	*invalid;

	if (is_multi_minus(s, &invalid))
	{
		invalid_option(invalid);
		ft_strclean(invalid);
		return (1);
	}
	while (*(++s) != '\0')
	{
		if (!is_option(*s))
		{
			invalid = ft_strndup(s, 1);
			invalid_option(invalid);
			ft_strclean(invalid);
			return (1);
		}
	}
	return (0);
}
Exemple #4
0
int main(const int argc, const char* argv[])
{
	_Bool binary_mode = 0;
	_Bool check = 0;
	int hash = NONE;

	// parse options
	for (int i = 1; i < argc; i++) {
		unsigned long len = strlen(argv[i]);

		// argv[i] must start with - to be an option
		if (len == 0 || (len > 1 && argv[i][0] != '-'))
			continue;

		// if the second character is also - then it is a long option
		if (len > 1 && argv[i][1] == '-') {
			if (strcmp("--help", argv[i]) == 0) {
				print_help();
				return 0;
			} else if (strcmp("--version", argv[i]) == 0) {
				print_version();
				return 0;
			} else if (strcmp("--binary", argv[i]) == 0) {
				binary_mode = 1;
			} else if (strcmp("--text", argv[i]) == 0) {
				binary_mode = 0;
			} else if (strcmp("--check", argv[i]) == 0) {
				check = 1;
			} else if (strcmp("--even-parity", argv[i]) == 0) {
				hash = EVEN_PARITY;
			} else if (strcmp("--odd-parity", argv[i]) == 0) {
				hash = ODD_PARITY;
			} else if (strcmp("--crc32", argv[i]) == 0) {
				hash = CRC32;
			} else if (strcmp("--sha1", argv[i]) == 0) {
				hash = SHA1;
			} else {
				invalid_option(argv[i]);
				return 1;
			}
		} else {
			for (unsigned long j = 1; j < len; j++) {
				switch (argv[i][j]) {
				case 'b':
					binary_mode = 1;
					break;
				case 't':
					binary_mode = 0;
					break;
				case 'c':
					check = 1;
					break;
				default:
					invalid_option(argv[i]);
					return 1;
				}
			}
		}
	}

	if (hash == NONE) {
		fprintf(stderr, "No hashing algorithm was specified\n");
		fprintf(stderr, "Try 'mhash --help' for more information\n");
		return 1;
	}

	char* mode = binary_mode ? "rb" : "r";

	// make sure that stdin is open in right mode
	freopen(NULL, mode, stdin);
	
	if (check) {
		fprintf(stderr, "file checking is not yet implemented\n");
		return 1;
	} else {

		for (int i = 1; i < argc; i++) {
			unsigned long len = strlen(argv[i]);
			if ((len > 1 && argv[i][0] == '-') || len == 0)
				continue;

			FILE* fp;
			if (len == 1 && argv[i][0] == '-')
				fp = stdin;
			else
				fp = fopen(argv[i], mode);
			
			// switches don't work nicely when values are initialized there
			if (EVEN_PARITY == hash) {
				uint8_t out = mhash_parity_file(fp, MHASH_PARITY_EVEN);
				printf("%" PRIu8, out);
			} else if (ODD_PARITY == hash) {
				uint8_t out = mhash_parity_file(fp, MHASH_PARITY_ODD);
				printf("%" PRIu8, out);
			} else if (CRC32 == hash) {
				uint32_t out = mhash_crc32_file(fp);
				printf("%08" PRIx32, out);
			} else if (SHA1 == hash) {
				uint8_t out[MHASH_SHA1_LENGTH];
				mhash_sha1_file(fp, out);
				for (uint8_t j = 0; j < 20; j++)
					printf("%02" PRIx8, out[j]);
			}
			printf("  %s\n", argv[i]);

			fclose(fp);
		}
	}

	return 0;
}
Exemple #5
0
int
main(int argc, char **argv)
{
	int argc_save = argc;
	char **argv_save = argv;
	gfarm_error_t e, e_save = GFARM_ERR_NO_ERROR;
	char opt_operation = '\0'; /* default operation */
	int opt_concurrency = DEFAULT_CONCURRENCY;
	int opt_alter_aliases = 0;
	char *opt_architecture = NULL;
	char *opt_domainname = NULL;
	long opt_ncpu = 0;
	int opt_port = 0, opt_flags = -1;
	int opt_plain_order = 0; /* i.e. do not sort */
	int opt_sort_by_loadavg = 0;
	int i, c, opt_use_metadb = 1;
	char *s;

	if (argc > 0)
		program_name = basename(argv[0]);
	while ((c = getopt(argc, argv, "AD:HLMPUa:cdf:ij:lmn:p:ruv?")) != -1) {
		switch (c) {
		case 'A':
			opt_alter_aliases = 1;
			break;
		case 'L':
			opt_sort_by_loadavg = 1;
			break;
		case 'M':
		case 'H':
		case 'R':
		case 'c':
		case 'd':
		case 'l':
		case 'm':
			if (opt_operation != '\0' && opt_operation != c)
				inconsistent_option(opt_operation, c);
			opt_operation = c;
			break;
		case 'a':
			opt_architecture = optarg;
			s = validate_architecture(opt_architecture);
			if (s != NULL) {
				fprintf(stderr, "%s: "
				    "invalid character '%c' in \"-a %s\"\n",
				    program_name, *s, opt_architecture);
				exit(1);
			}
			break;
		case 'D':
			opt_domainname = optarg;
			s = validate_hostname(opt_domainname);
			if (s != NULL) {
				fprintf(stderr, "%s: "
				    "invalid character '%c' in \"-a %s\"\n",
				    program_name, *s, opt_domainname);
				exit(1);
			}
			break;
		case 'i':
			opt_resolv_addr = resolv_addr_without_address_use;
			break;
		case 'j':
			opt_concurrency = parse_opt_long(optarg,
			    c, "<concurrency>");
			if (opt_concurrency <= 0) {
				fprintf(stderr, "%s: invalid value: -%c %d\n",
				    program_name, c, opt_concurrency);
				usage();
			}
			break;
		case 'f':
			opt_flags = parse_opt_long(optarg, c, "<flags>");
			break;
		case 'n':
			opt_ncpu = parse_opt_long(optarg, c, "<ncpu>");
			break;
		case 'p':
			opt_port = parse_opt_long(optarg, c, "<port>");
			break;
		case 'r':
			output_sort_reverse = 1;
			break;
		case 'U':
			opt_udp_only = 1;
			break;
		case 'u':
			opt_plain_order = 1;
			break;
		case 'v':
			opt_verbose = 1;
			break;
		case '?':
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	switch (opt_operation) {
	case OP_CREATE_ENTRY:
		if (opt_architecture == NULL) {
			fprintf(stderr, "%s: missing -a <architecture>\n",
			    program_name);
			usage();
		}
		if (opt_ncpu == 0)
			opt_ncpu = 1;
		if (opt_flags == -1)
			opt_flags = 0;
		/* opt_alter_aliases is meaningless, but allowed */
		break;
	case OP_REGISTER_DB:
	case OP_DELETE_ENTRY:
		if (opt_architecture != NULL)
			invalid_option('a');
		if (opt_domainname != NULL)
			invalid_option('D');
		/* fall through */
	case OP_NODENAME:
	case OP_LIST_GFSD_INFO:
	case OP_LIST_LONG:
	case OP_DUMP_METADB:
		if (opt_ncpu != 0)
			invalid_option('n');
		if (opt_alter_aliases)
			invalid_option('A');
		break;
	case OP_MODIFY_ENTRY:
		if (opt_domainname != NULL)
			invalid_option('D');
		break;
	default:
		;
	}

	for (i = 0; i < argc; i++) {
		s = validate_hostname(argv[i]);
		if (s != NULL) {
			fprintf(stderr, "%s: "
			    "invalid character '%c' in hostname \"%s\"\n",
			    program_name, *s, argv[i]);
			exit(1);
		}
	}

	e = gfarm_initialize(&argc_save, &argv_save);
	if (opt_operation == OP_LIST_GFSD_INFO && argc > 0 &&
	    opt_resolv_addr == resolv_addr_without_address_use) {
		/*
		 * An implicit feature to access gfsd directly
		 * without having working gfmd.
		 * e.g. gfhost -Hi <hostname>
		 *
		 * XXX	should describe this in the manual?
		 *	or use explicit and different option?
		 */
		opt_use_metadb = 0;
		opt_resolv_addr = resolv_addr_without_metadb;
	} else if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}

	switch (opt_operation) {
	case OP_CREATE_ENTRY:
		if (argc > 0) {
			if (opt_port == 0) {
				fprintf(stderr, "%s: option -p <port> is "
				    "mandatory with -c\n", program_name);
				usage();
			}
			e_save = add_host(argv[0], opt_port, &argv[1],
			    opt_architecture, opt_ncpu, opt_flags);
			if (e_save != GFARM_ERR_NO_ERROR)
				fprintf(stderr, "%s: %s: %s\n", program_name,
				    argv[0], gfarm_error_string(e_save));
		}
		break;
	case OP_MODIFY_ENTRY:
		if (argc > 0) {
			e_save = gfarm_modify_host(argv[0], opt_port, &argv[1],
			    opt_architecture, opt_ncpu, opt_flags,
			    !opt_alter_aliases);
			if (e_save != GFARM_ERR_NO_ERROR)
				fprintf(stderr, "%s: %s: %s\n", program_name,
				    argv[0], gfarm_error_string(e_save));
		}
		break;
	case OP_DELETE_ENTRY:
		for (i = 0; i < argc; i++) {
			e = gfm_client_host_info_remove(gfarm_metadb_server,
			    argv[i]);
			if (e != GFARM_ERR_NO_ERROR) {
				fprintf(stderr, "%s: %s\n", argv[i],
				    gfarm_error_string(e));
				if (e_save == GFARM_ERR_NO_ERROR)
					e_save = e;
			}
		}
		break;
	case OP_REGISTER_DB:
		if (argc > 0) {
			fprintf(stderr, "%s: too many argument: %s\n",
			    program_name, argv[0]);
			exit(1);
		}
		e_save = register_db();
		break;
	case OP_LIST_GFSD_INFO:
		e = paraccess_list(opt_concurrency, opt_udp_only,
		    opt_architecture, opt_domainname, opt_port,
		    opt_plain_order, opt_sort_by_loadavg,
		    opt_use_metadb, argc, argv,
		    request_gfsd_info, callback_gfsd_info);
		break;
	case OP_NODENAME:
		e = paraccess_list(opt_concurrency, opt_udp_only,
		    opt_architecture, opt_domainname, opt_port,
		    opt_plain_order, opt_sort_by_loadavg,
		    opt_use_metadb, argc, argv,
		    request_nodename, callback_nodename);
		break;
	case OP_LIST_LONG:
		e = paraccess_list(opt_concurrency, opt_udp_only,
		    opt_architecture, opt_domainname, opt_port,
		    opt_plain_order, opt_sort_by_loadavg,
		    opt_use_metadb, argc, argv,
		    request_long_format, callback_long_format);
		break;
	case OP_DUMP_METADB:
		if (argc == 0) {
			e_save = list_all(opt_architecture, opt_domainname,
				print_host_info, NULL);
		} else {
			e_save = list(argc, argv, print_host_info, NULL);
		}
		break;
	}
	e = gfarm_terminate();
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}
	exit(e_save == GFARM_ERR_NO_ERROR ? 0 : 1);
}
Exemple #6
0
void
parse_args(int argc, const char *argv[])
{
  // parse command line arguments
  for (; option.argi < argc; option.argi++) {

    // not an option
    if (!is_option(argv[option.argi])) return;

    // set accumulation filename [setup]
    if (!strcmp(argv[option.argi], "--accum") || (!option.lgopt && !strcmp(argv[option.argi], "-a"))) {
      option.accum = argv[++option.argi]; 
      debug("accumulation filename set to '%s'", option.accum);
      continue;
    }

    // set blank mode [setup]
    if (!strcmp(argv[option.argi], "--blank") || (!option.lgopt && !strcmp(argv[option.argi], "-b"))) {
      debug("blank spaces ignored");
      option.blank = 1;
      continue;
    }

    // set config extension [setup]
    if (!strcmp(argv[option.argi], "--cfgext")) {
      option.cfg_e = argv[++option.argi]; 
      debug("config extension set to '%s'", option.cfg_e);
      continue;
    }

    // set comment characters [setup]
    if (!strcmp(argv[option.argi], "--comment") || (!option.lgopt && !strcmp(argv[option.argi], "-c"))) {
      option.cchr = argv[++option.argi]; 
      debug("comment characters set to '%s'", option.cchr);
      continue;
    }

    // set debug mode [setup]
    if (!strcmp(argv[option.argi], "--debug") || (!option.lgopt && !strcmp(argv[option.argi], "-d"))) {
      logmsg_config.level = debug_level;
      logmsg_config.locate = 1;
      debug("debug mode on");
      option.debug = 1;
      option.check = 1;
      continue;
    }

    // display help [action]
    if (!strcmp(argv[option.argi], "--help") || (!option.lgopt && !strcmp(argv[option.argi], "-h"))) {
      usage();
      continue;
    }

    // set info mode [setup]
    if (!strcmp(argv[option.argi], "--info") || (!option.lgopt && !strcmp(argv[option.argi], "-i"))) {
      debug("info mode on");
      logmsg_config.level = inform_level;
      logmsg_config.locate = 0;
      continue;
    }

    // set keep number [setup]
    if (!strcmp(argv[option.argi], "--keep") || (!option.lgopt && !strcmp(argv[option.argi], "-k"))) {
      option.keep = strtoul(argv[++option.argi],0,0);
      debug("keep set to %d", option.keep);
      continue;
    }

    // enable left result [setup]
    if (!strcmp(argv[option.argi], "--lhsrec")) {
      debug("recycling left file enabled");
      option.recycle = ndiff_recycle_left;
      continue;
    }

    // enable left result [setup]
    if (!strcmp(argv[option.argi], "--lhsres")) {
      debug("left results enabled");
      option.lhs_res = 1;
      continue;
    }

    // set list mode [setup]
    if (!strcmp(argv[option.argi], "--list") || (!option.lgopt && !strcmp(argv[option.argi], "-l"))) {
      debug("list mode on");
      option.list = 1;
      continue;
    }

    // disable short options [setup]
    if (!strcmp(argv[option.argi], "--long")) {
      debug("short options disabled");
      option.lgopt = 1;
      continue;
    }


    // disable color [setup]
    if (!strcmp(argv[option.argi], "--nocolor")) {
      debug("color output disabled");
      fail_str = "FAIL";
      pass_str = "PASS";
      continue;
    }

    // disable location trace [setup]
    if (!strcmp(argv[option.argi], "--noloc")) {
      debug("trace of location disabled");
      logmsg_config.locate = 0;
      continue;
    }

    // disable warnings [setup]
    if (!strcmp(argv[option.argi], "--nowarn")) {
      debug("no warning mode on");
      logmsg_config.level = error_level;
      logmsg_config.locate = 0;
      option.nowarn = 1;
      continue;
    }

    // set number of registers [setup]
    if (!strcmp(argv[option.argi], "--nregs")) {
      option.nregs = imin(REG_MAX, strtoul(argv[++option.argi],0,0));
      debug("number of registers set to %d", option.nregs);
      continue;
    }

    // set output extension [setup]
    if (!strcmp(argv[option.argi], "--outext")) {
      option.out_e = argv[++option.argi]; 
      debug("output extension set to '%s'", option.out_e);
      continue;
    }

    // set punctuation characters [setup]
    if (!strcmp(argv[option.argi], "--punct")) {
      option.pchr = argv[++option.argi]; 
      debug("punctuation characters set to '%s'", option.pchr);
      continue;
    }

    // set quiet mode [setup]
    if (!strcmp(argv[option.argi], "--quiet") || (!option.lgopt && !strcmp(argv[option.argi], "-q"))) {
      debug("quiet mode on");
      logmsg_config.level = warning_level;
      logmsg_config.locate = 0;
      continue;
    }

    // set reference extension [setup]
    if (!strcmp(argv[option.argi], "--refext")) {
      option.ref_e = argv[++option.argi]; 
      debug("reference extension set to '%s'", option.ref_e);
      continue;
    }

    // set register format [setup]
    if (!strcmp(argv[option.argi], "--regfmt")) {
      option.rfmt = argv[++option.argi];
      debug("register format set to '%s'", option.rfmt);
      continue;
    }

    // set result extension [setup]
    if (!strcmp(argv[option.argi], "--resext")) {
      option.res_e = argv[++option.argi]; 
      debug("result extension set to '%s'", option.res_e);
      continue;
    }

    // reset accumulation information [action]
    if (!strcmp(argv[option.argi], "--reset") || (!option.lgopt && !strcmp(argv[option.argi], "-r"))) {
      ensure(option.accum, "no accumulation file specified");
      debug("reseting file '%s'", option.accum);
      option.reset = 1;
      accum_summary(0, 0, 0, 0);
      continue;
    }

    // enable right result [setup]
    if (!strcmp(argv[option.argi], "--rhsrec")) {
      debug("recycling right file enabled");
      option.recycle = ndiff_recycle_right;
      continue;
    }

    // enable right result [setup]
    if (!strcmp(argv[option.argi], "--rhsres")) {
      debug("right results enabled");
      option.rhs_res = 1;
      continue;
    }

    // set serie mode [setup]
    if (!strcmp(argv[option.argi], "--serie") || (!option.lgopt && !strcmp(argv[option.argi], "-n"))) {
      debug("serie mode on");
      option.serie = 1;
      continue;
    }

    // set serie format [setup]
    if (!strcmp(argv[option.argi], "--seriefmt")) {
      option.fmt = argv[++option.argi];
      debug("serie format set to '%s'", option.fmt);
      continue;
    }

    // set suite name [setup]
    if (!strcmp(argv[option.argi], "--suite") || (!option.lgopt && !strcmp(argv[option.argi], "-s"))) {
      option.suite = argv[++option.argi];
      debug("suite name set to '%s'", option.suite);
      continue;
    }

    // set suite format [setup]
    if (!strcmp(argv[option.argi], "--suitefmt")) {
      option.sfmt = argv[++option.argi];
      debug("suite format set to '%s'", option.sfmt);
      continue;
    }

    // set test name [setup]
    if (!strcmp(argv[option.argi], "--test") || (!option.lgopt && !strcmp(argv[option.argi], "-t"))) {
      option.test = argv[++option.argi];
      debug("test name set to '%s'", option.test);
      continue;
    }

    // set trace mode [setup]
    if (!strcmp(argv[option.argi], "--trace")) {
      logmsg_config.level = trace_level;
      logmsg_config.locate = 1;
      debug("trace mode on");
      option.debug = 1;
      option.check = 1;
      continue;
    }

    // enable truncation [setup]
    if (!strcmp(argv[option.argi], "--trunc")) {
      debug("premature truncation allowed");
      option.trunc = 1;
      continue;
    }

    // run utests [action]
    if (!strcmp(argv[option.argi], "--utest")) {
      run_utest();
      option.utest += 1;
      continue;
    }

    // set check mode [setup]
    if (!strcmp(argv[option.argi], "--xcheck") || (!option.lgopt && !strcmp(argv[option.argi], "-x"))) {
      debug("check mode on");
      option.check = 1;
      continue;
    }

// ---- [decompression]

    // set tertiary unzip command [setup]
    if (!strcmp(argv[option.argi], "--bzip2")) {
      option.unzip[2] = argv[++option.argi]; 
      debug("bzip2 command set to '%s'", option.unzip[2]);
      continue;
    }

    // set secondary unzip command [setup]
    if (!strcmp(argv[option.argi], "--gzip")) {
      option.unzip[1] = argv[++option.argi]; 
      debug("gzip command set to '%s'", option.unzip[1]);
      continue;
    }

    // set primary unzip command [setup]
    if (!strcmp(argv[option.argi], "--unzip")) {
      option.unzip[0] = argv[++option.argi]; 
      debug("unzip command set to '%s'", option.unzip[0]);
      continue;
    }

// ---- [unknown]
    invalid_option(argv[option.argi]);
  }

  exit(EXIT_SUCCESS);
}