// ---------------------------------------------------------------- cli_opts_t* parse_command_line(int argc, char** argv) { cli_opts_t* popts = mlr_malloc_or_die(sizeof(cli_opts_t)); memset(popts, 0, sizeof(*popts)); popts->irs = DEFAULT_RS; popts->ifs = DEFAULT_FS; popts->ips = DEFAULT_PS; popts->allow_repeat_ifs = FALSE; popts->allow_repeat_ips = FALSE; popts->ors = DEFAULT_RS; popts->ofs = DEFAULT_FS; popts->ops = DEFAULT_PS; popts->ofmt = DEFAULT_OFMT; popts->plrec_reader = NULL; popts->plrec_writer = NULL; popts->filenames = NULL; popts->ifmt = "dkvp"; char* ofmt = "dkvp"; popts->use_mmap_for_read = TRUE; int left_align_pprint = TRUE; int have_rand_seed = FALSE; unsigned rand_seed = 0; int argi = 1; for (; argi < argc; argi++) { if (argv[argi][0] != '-') break; else if (streq(argv[argi], "-h")) main_usage(argv[0], 0); else if (streq(argv[argi], "--help")) main_usage(argv[0], 0); else if (streq(argv[argi], "--help-all-verbs")) usage_all_verbs(argv[0]); else if (streq(argv[argi], "--help-all-functions") || streq(argv[argi], "-f")) { lrec_evaluator_function_usage(stdout, NULL); exit(0); } else if (streq(argv[argi], "--help-function") || streq(argv[argi], "--hf")) { check_arg_count(argv, argi, argc, 2); lrec_evaluator_function_usage(stdout, argv[argi+1]); exit(0); } else if (streq(argv[argi], "--rs")) { check_arg_count(argv, argi, argc, 2); popts->ors = popts->irs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--irs")) { check_arg_count(argv, argi, argc, 2); popts->irs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ors")) { check_arg_count(argv, argi, argc, 2); popts->ors = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--fs")) { check_arg_count(argv, argi, argc, 2); popts->ofs = popts->ifs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ifs")) { check_arg_count(argv, argi, argc, 2); popts->ifs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ofs")) { check_arg_count(argv, argi, argc, 2); popts->ofs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--repifs")) { popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "-p")) { popts->ifmt = "nidx"; ofmt = "nidx"; popts->ifs = ' '; popts->ofs = ' '; popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "--ps")) { check_arg_count(argv, argi, argc, 2); popts->ops = popts->ips = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ips")) { check_arg_count(argv, argi, argc, 2); popts->ips = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ops")) { check_arg_count(argv, argi, argc, 2); popts->ops = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--dkvp")) { popts->ifmt = ofmt = "dkvp"; } else if (streq(argv[argi], "--idkvp")) { popts->ifmt = "dkvp"; } else if (streq(argv[argi], "--odkvp")) { ofmt = "dkvp"; } else if (streq(argv[argi], "--csv")) { popts->ifmt = ofmt = "csv"; } else if (streq(argv[argi], "--icsv")) { popts->ifmt = "csv"; } else if (streq(argv[argi], "--ocsv")) { ofmt = "csv"; } else if (streq(argv[argi], "--nidx")) { popts->ifmt = ofmt = "nidx"; } else if (streq(argv[argi], "--inidx")) { popts->ifmt = "nidx"; } else if (streq(argv[argi], "--onidx")) { ofmt = "nidx"; } else if (streq(argv[argi], "--xtab")) { popts->ifmt = ofmt = "xtab"; } else if (streq(argv[argi], "--ixtab")) { popts->ifmt = "xtab"; } else if (streq(argv[argi], "--oxtab")) { ofmt = "xtab"; } else if (streq(argv[argi], "--ipprint")) { popts->ifmt = "csv"; popts->ifs = ' '; popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "--opprint")) { ofmt = "pprint"; } else if (streq(argv[argi], "--pprint")) { popts->ifmt = "csv"; popts->ifs = ' '; popts->allow_repeat_ifs = TRUE; ofmt = "pprint"; } else if (streq(argv[argi], "--right")) { left_align_pprint = FALSE; } else if (streq(argv[argi], "--ofmt")) { check_arg_count(argv, argi, argc, 2); popts->ofmt = argv[argi+1]; argi++; } // xxx put into online help. else if (streq(argv[argi], "--mmap")) { popts->use_mmap_for_read = TRUE; } else if (streq(argv[argi], "--no-mmap")) { popts->use_mmap_for_read = FALSE; } else if (streq(argv[argi], "--seed")) { check_arg_count(argv, argi, argc, 2); if (sscanf(argv[argi+1], "0x%x", &rand_seed) == 1) { have_rand_seed = TRUE; } else if (sscanf(argv[argi+1], "%u", &rand_seed) == 1) { have_rand_seed = TRUE; } else { main_usage(argv[0], 1); } argi++; } else nusage(argv[0], argv[argi]); } if (streq(ofmt, "dkvp")) popts->plrec_writer = lrec_writer_dkvp_alloc(popts->ors, popts->ofs, popts->ops); else if (streq(ofmt, "csv")) popts->plrec_writer = lrec_writer_csv_alloc(popts->ors, popts->ofs); else if (streq(ofmt, "nidx")) popts->plrec_writer = lrec_writer_nidx_alloc(popts->ors, popts->ofs); else if (streq(ofmt, "xtab")) popts->plrec_writer = lrec_writer_xtab_alloc(); else if (streq(ofmt, "pprint")) popts->plrec_writer = lrec_writer_pprint_alloc(left_align_pprint); else { main_usage(argv[0], 1); } if ((argc - argi) < 1) { main_usage(argv[0], 1); } popts->pmapper_list = sllv_alloc(); while (TRUE) { check_arg_count(argv, argi, argc, 1); char* verb = argv[argi]; mapper_setup_t* pmapper_setup = look_up_mapper_setup(verb); if (pmapper_setup == NULL) { fprintf(stderr, "%s: verb \"%s\" not found. Please use \"%s --help\" for a list.\n", argv[0], verb, argv[0]); exit(1); } if ((argc - argi) >= 2) { if (streq(argv[argi+1], "-h") || streq(argv[argi+1], "--help")) { pmapper_setup->pusage_func(argv[0], verb); exit(0); } } // It's up to the parse func to print its usage on CLI-parse failure. mapper_t* pmapper = pmapper_setup->pparse_func(&argi, argc, argv); if (pmapper == NULL) { exit(1); } sllv_add(popts->pmapper_list, pmapper); // xxx cmt if (argi >= argc || !streq(argv[argi], "then")) break; argi++; } popts->filenames = &argv[argi]; // No filenames means read from standard input, and standard input cannot be mmapped. if (argi == argc) popts->use_mmap_for_read = FALSE; popts->plrec_reader = lrec_reader_alloc(popts->ifmt, popts->use_mmap_for_read, popts->irs, popts->ifs, popts->allow_repeat_ifs, popts->ips, popts->allow_repeat_ips); if (popts->plrec_reader == NULL) main_usage(argv[0], 1); if (have_rand_seed) { mtrand_init(rand_seed); } else { mtrand_init_default(); } return popts; }
// ---------------------------------------------------------------- cli_opts_t* parse_command_line(int argc, char** argv) { cli_opts_t* popts = mlr_malloc_or_die(sizeof(cli_opts_t)); memset(popts, 0, sizeof(*popts)); popts->irs = NULL; popts->ifs = NULL; popts->ips = NULL; popts->allow_repeat_ifs = NEITHER_TRUE_NOR_FALSE; popts->allow_repeat_ips = NEITHER_TRUE_NOR_FALSE; popts->ors = NULL; popts->ofs = NULL; popts->ops = NULL; popts->ofmt = DEFAULT_OFMT; popts->oquoting = DEFAULT_OQUOTING; popts->plrec_reader = NULL; popts->plrec_writer = NULL; popts->filenames = NULL; popts->ifile_fmt = "dkvp"; popts->ofile_fmt = "dkvp"; popts->use_mmap_for_read = TRUE; int left_align_pprint = TRUE; int have_rand_seed = FALSE; unsigned rand_seed = 0; int argi = 1; for (; argi < argc; argi++) { if (argv[argi][0] != '-') break; else if (streq(argv[argi], "--version")) { #ifdef HAVE_CONFIG_H printf("Miller version >= %s.\n", PACKAGE_VERSION); #else printf("Miller version >= %s.\n", MLR_VERSION); #endif // HAVE_CONFIG_H exit(0); } else if (streq(argv[argi], "-h")) main_usage(argv[0], 0); else if (streq(argv[argi], "--help")) main_usage(argv[0], 0); else if (streq(argv[argi], "--help-all-verbs")) usage_all_verbs(argv[0]); else if (streq(argv[argi], "--help-all-functions") || streq(argv[argi], "-f")) { lrec_evaluator_function_usage(stdout, NULL); exit(0); } else if (streq(argv[argi], "--help-function") || streq(argv[argi], "--hf")) { check_arg_count(argv, argi, argc, 2); lrec_evaluator_function_usage(stdout, argv[argi+1]); exit(0); } else if (streq(argv[argi], "--rs")) { check_arg_count(argv, argi, argc, 2); popts->ors = sep_from_arg(argv[argi+1], argv[0]); popts->irs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--irs")) { check_arg_count(argv, argi, argc, 2); popts->irs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ors")) { check_arg_count(argv, argi, argc, 2); popts->ors = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--fs")) { check_arg_count(argv, argi, argc, 2); popts->ofs = sep_from_arg(argv[argi+1], argv[0]); popts->ifs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ifs")) { check_arg_count(argv, argi, argc, 2); popts->ifs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ofs")) { check_arg_count(argv, argi, argc, 2); popts->ofs = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--repifs")) { popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "-p")) { popts->ifile_fmt = "nidx"; popts->ofile_fmt = "nidx"; popts->ifs = " "; popts->ofs = " "; popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "--ps")) { check_arg_count(argv, argi, argc, 2); popts->ops = sep_from_arg(argv[argi+1], argv[0]); popts->ips = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ips")) { check_arg_count(argv, argi, argc, 2); popts->ips = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--ops")) { check_arg_count(argv, argi, argc, 2); popts->ops = sep_from_arg(argv[argi+1], argv[0]); argi++; } else if (streq(argv[argi], "--csv")) { popts->ifile_fmt = popts->ofile_fmt = "csv"; } else if (streq(argv[argi], "--icsv")) { popts->ifile_fmt = "csv"; } else if (streq(argv[argi], "--ocsv")) { popts->ofile_fmt = "csv"; } else if (streq(argv[argi], "--csvlite")) { popts->ifile_fmt = popts->ofile_fmt = "csvlite"; } else if (streq(argv[argi], "--icsvlite")) { popts->ifile_fmt = "csvlite"; } else if (streq(argv[argi], "--ocsvlite")) { popts->ofile_fmt = "csvlite"; } else if (streq(argv[argi], "--dkvp")) { popts->ifile_fmt = popts->ofile_fmt = "dkvp"; } else if (streq(argv[argi], "--idkvp")) { popts->ifile_fmt = "dkvp"; } else if (streq(argv[argi], "--odkvp")) { popts->ofile_fmt = "dkvp"; } else if (streq(argv[argi], "--nidx")) { popts->ifile_fmt = popts->ofile_fmt = "nidx"; } else if (streq(argv[argi], "--inidx")) { popts->ifile_fmt = "nidx"; } else if (streq(argv[argi], "--onidx")) { popts->ofile_fmt = "nidx"; } else if (streq(argv[argi], "--xtab")) { popts->ifile_fmt = popts->ofile_fmt = "xtab"; } else if (streq(argv[argi], "--ixtab")) { popts->ifile_fmt = "xtab"; } else if (streq(argv[argi], "--oxtab")) { popts->ofile_fmt = "xtab"; } else if (streq(argv[argi], "--ipprint")) { popts->ifile_fmt = "csvlite"; popts->ifs = " "; popts->allow_repeat_ifs = TRUE; } else if (streq(argv[argi], "--opprint")) { popts->ofile_fmt = "pprint"; } else if (streq(argv[argi], "--pprint")) { popts->ifile_fmt = "csvlite"; popts->ifs = " "; popts->allow_repeat_ifs = TRUE; popts->ofile_fmt = "pprint"; } else if (streq(argv[argi], "--right")) { left_align_pprint = FALSE; } else if (streq(argv[argi], "--ofmt")) { check_arg_count(argv, argi, argc, 2); popts->ofile_fmt = argv[argi+1]; argi++; } else if (streq(argv[argi], "--quote-all")) { popts->oquoting = QUOTE_ALL; } else if (streq(argv[argi], "--quote-none")) { popts->oquoting = QUOTE_NONE; } else if (streq(argv[argi], "--quote-minimal")) { popts->oquoting = QUOTE_MINIMAL; } else if (streq(argv[argi], "--quote-numeric")) { popts->oquoting = QUOTE_NUMERIC; } // xxx put into online help. else if (streq(argv[argi], "--mmap")) { popts->use_mmap_for_read = TRUE; } else if (streq(argv[argi], "--no-mmap")) { popts->use_mmap_for_read = FALSE; } else if (streq(argv[argi], "--seed")) { check_arg_count(argv, argi, argc, 2); if (sscanf(argv[argi+1], "0x%x", &rand_seed) == 1) { have_rand_seed = TRUE; } else if (sscanf(argv[argi+1], "%u", &rand_seed) == 1) { have_rand_seed = TRUE; } else { main_usage(argv[0], 1); } argi++; } else nusage(argv[0], argv[argi]); } lhmss_t* default_rses = get_default_rses(); lhmss_t* default_fses = get_default_fses(); lhmss_t* default_pses = get_default_pses(); lhmsi_t* default_repeat_ifses = get_default_repeat_ifses(); lhmsi_t* default_repeat_ipses = get_default_repeat_ipses(); if (popts->irs == NULL) popts->irs = lhmss_get(default_rses, popts->ifile_fmt); if (popts->ifs == NULL) popts->ifs = lhmss_get(default_fses, popts->ifile_fmt); if (popts->ips == NULL) popts->ips = lhmss_get(default_pses, popts->ifile_fmt); if (popts->allow_repeat_ifs == NEITHER_TRUE_NOR_FALSE) popts->allow_repeat_ifs = lhmsi_get(default_repeat_ifses, popts->ifile_fmt); if (popts->allow_repeat_ips == NEITHER_TRUE_NOR_FALSE) popts->allow_repeat_ips = lhmsi_get(default_repeat_ipses, popts->ifile_fmt); if (popts->ors == NULL) popts->ors = lhmss_get(default_rses, popts->ofile_fmt); if (popts->ofs == NULL) popts->ofs = lhmss_get(default_fses, popts->ofile_fmt); if (popts->ops == NULL) popts->ops = lhmss_get(default_pses, popts->ofile_fmt); if (popts->irs == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->ifs == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->ips == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->allow_repeat_ifs == NEITHER_TRUE_NOR_FALSE) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->allow_repeat_ips == NEITHER_TRUE_NOR_FALSE) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->ors == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->ofs == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (popts->ops == NULL) { fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n", argv[0], __FILE__, __LINE__); exit(1); } if (streq(popts->ofile_fmt, "pprint") && strlen(popts->ofs) != 1) { fprintf(stderr, "%s: OFS for PPRINT format must be single-character; got \"%s\".\n", argv[0], popts->ofs); return NULL; } if (streq(popts->ofile_fmt, "dkvp")) popts->plrec_writer = lrec_writer_dkvp_alloc(popts->ors, popts->ofs, popts->ops); else if (streq(popts->ofile_fmt, "csv")) popts->plrec_writer = lrec_writer_csv_alloc(popts->ors, popts->ofs, popts->oquoting); else if (streq(popts->ofile_fmt, "csvlite")) popts->plrec_writer = lrec_writer_csvlite_alloc(popts->ors, popts->ofs); else if (streq(popts->ofile_fmt, "nidx")) popts->plrec_writer = lrec_writer_nidx_alloc(popts->ors, popts->ofs); else if (streq(popts->ofile_fmt, "xtab")) popts->plrec_writer = lrec_writer_xtab_alloc(popts->ofs, popts->ops); else if (streq(popts->ofile_fmt, "pprint")) popts->plrec_writer = lrec_writer_pprint_alloc(popts->ors, popts->ofs[0], left_align_pprint); else { main_usage(argv[0], 1); } if ((argc - argi) < 1) { main_usage(argv[0], 1); } popts->pmapper_list = sllv_alloc(); while (TRUE) { check_arg_count(argv, argi, argc, 1); char* verb = argv[argi]; mapper_setup_t* pmapper_setup = look_up_mapper_setup(verb); if (pmapper_setup == NULL) { fprintf(stderr, "%s: verb \"%s\" not found. Please use \"%s --help\" for a list.\n", argv[0], verb, argv[0]); exit(1); } if ((argc - argi) >= 2) { if (streq(argv[argi+1], "-h") || streq(argv[argi+1], "--help")) { pmapper_setup->pusage_func(argv[0], verb); exit(0); } } // It's up to the parse func to print its usage on CLI-parse failure. mapper_t* pmapper = pmapper_setup->pparse_func(&argi, argc, argv); if (pmapper == NULL) { exit(1); } sllv_add(popts->pmapper_list, pmapper); // xxx cmt if (argi >= argc || !streq(argv[argi], "then")) break; argi++; } popts->filenames = &argv[argi]; // No filenames means read from standard input, and standard input cannot be mmapped. if (argi == argc) popts->use_mmap_for_read = FALSE; popts->plrec_reader = lrec_reader_alloc(popts->ifile_fmt, popts->use_mmap_for_read, popts->irs, popts->ifs, popts->allow_repeat_ifs, popts->ips, popts->allow_repeat_ips); if (popts->plrec_reader == NULL) main_usage(argv[0], 1); if (have_rand_seed) { mtrand_init(rand_seed); } else { mtrand_init_default(); } return popts; }