mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-28 18:21:52 +00:00
mlr_globals factor-away
This commit is contained in:
parent
ccc022064a
commit
7c5cc18c19
9 changed files with 32 additions and 44 deletions
|
|
@ -22,6 +22,7 @@
|
|||
#include "lib/mlr_globals.h"
|
||||
#include "lib/mlrutil.h"
|
||||
#include "containers/lhmsi.h"
|
||||
#include "containers/free_flags.h"
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Allow compile-time override, e.g using gcc -D.
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@
|
|||
#include <libgen.h>
|
||||
#include "lib/mlr_globals.h"
|
||||
|
||||
mlr_globals_t MLR_GLOBALS = { .bargv0 = "mlr", .ofmt = NULL, .popts = NULL };
|
||||
void mlr_global_init(char* argv0, char* ofmt, cli_opts_t* popts) {
|
||||
mlr_globals_t MLR_GLOBALS = { .bargv0 = "mlr", .ofmt = NULL };
|
||||
void mlr_global_init(char* argv0, char* ofmt) {
|
||||
MLR_GLOBALS.bargv0 = basename(argv0);
|
||||
MLR_GLOBALS.ofmt = ofmt;
|
||||
MLR_GLOBALS.popts = popts;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,11 @@
|
|||
#ifndef MLR_GLOBALS_H
|
||||
#define MLR_GLOBALS_H
|
||||
#include "cli/mlrcli.h"
|
||||
|
||||
typedef struct _mlr_globals_t {
|
||||
char* bargv0; // basename of argv0
|
||||
char* ofmt;
|
||||
// These are shared by mlrcli.c and mlrmain.c. The only reason for their
|
||||
// exposure anywhere else is to communicate format and separator flags to
|
||||
// mapper_join, which (unlike other mappers) needs to do its own file I/O.
|
||||
cli_opts_t* popts;
|
||||
} mlr_globals_t;
|
||||
extern mlr_globals_t MLR_GLOBALS;
|
||||
void mlr_global_init(char* argv0, char* ofmt, cli_opts_t* popts);
|
||||
void mlr_global_init(char* argv0, char* ofmt);
|
||||
|
||||
#endif // MLR_GLOBALS_H
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <sys/time.h>
|
||||
#include "lib/mlrregex.h"
|
||||
#include "lib/mlr_globals.h"
|
||||
#include "containers/free_flags.h"
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Succeeds or aborts the process. cflag REG_EXTENDED is already included.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include <regex.h>
|
||||
#include "cli/argparse.h"
|
||||
#include "cli/mlrcli.h"
|
||||
#include "mapping/mappers.h"
|
||||
#include "lib/mlr_globals.h"
|
||||
#include "lib/mlrutil.h"
|
||||
|
|
@ -10,12 +11,14 @@ typedef struct _mapper_grep_state_t {
|
|||
ap_state_t* pargp;
|
||||
int exclude;
|
||||
regex_t regex;
|
||||
cli_writer_opts_t* pwriter_opts;
|
||||
} mapper_grep_state_t;
|
||||
|
||||
static void mapper_grep_usage(FILE* o, char* argv0, char* verb);
|
||||
static mapper_t* mapper_grep_parse_cli(int* pargi, int argc, char** argv,
|
||||
cli_reader_opts_t* _, cli_writer_opts_t* __);
|
||||
static mapper_t* mapper_grep_alloc(ap_state_t* pargp, char* regex_string, int exclude, int ignore_case);
|
||||
static mapper_t* mapper_grep_alloc(ap_state_t* pargp, char* regex_string, int exclude, int ignore_case,
|
||||
cli_writer_opts_t* pwriter_opts);
|
||||
static void mapper_grep_free(mapper_t* pmapper);
|
||||
static sllv_t* mapper_grep_process(lrec_t* pinrec, context_t* pctx, void* pvstate);
|
||||
|
||||
|
|
@ -28,7 +31,7 @@ mapper_setup_t mapper_grep_setup = {
|
|||
|
||||
// ----------------------------------------------------------------
|
||||
static mapper_t* mapper_grep_parse_cli(int* pargi, int argc, char** argv,
|
||||
cli_reader_opts_t* _, cli_writer_opts_t* __)
|
||||
cli_reader_opts_t* _, cli_writer_opts_t* pwriter_opts)
|
||||
{
|
||||
char* regex_string = NULL;
|
||||
int exclude = FALSE;
|
||||
|
|
@ -57,7 +60,7 @@ static mapper_t* mapper_grep_parse_cli(int* pargi, int argc, char** argv,
|
|||
|
||||
regex_string = argv[(*pargi)++];
|
||||
|
||||
mapper_t* pmapper = mapper_grep_alloc(pstate, regex_string, exclude, ignore_case);
|
||||
mapper_t* pmapper = mapper_grep_alloc(pstate, regex_string, exclude, ignore_case, pwriter_opts);
|
||||
return pmapper;
|
||||
}
|
||||
static void mapper_grep_usage(FILE* o, char* argv0, char* verb) {
|
||||
|
|
@ -80,7 +83,9 @@ static void mapper_grep_usage(FILE* o, char* argv0, char* verb) {
|
|||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static mapper_t* mapper_grep_alloc(ap_state_t* pargp, char* regex_string, int exclude, int ignore_case) {
|
||||
static mapper_t* mapper_grep_alloc(ap_state_t* pargp, char* regex_string, int exclude, int ignore_case,
|
||||
cli_writer_opts_t* pwriter_opts)
|
||||
{
|
||||
mapper_t* pmapper = mlr_malloc_or_die(sizeof(mapper_t));
|
||||
|
||||
mapper_grep_state_t* pstate = mlr_malloc_or_die(sizeof(mapper_grep_state_t));
|
||||
|
|
@ -90,6 +95,7 @@ static mapper_t* mapper_grep_alloc(ap_state_t* pargp, char* regex_string, int ex
|
|||
cflags |= REG_ICASE;
|
||||
regcomp_or_die_quoted(&pstate->regex, regex_string, cflags);
|
||||
pstate->exclude = exclude;
|
||||
pstate->pwriter_opts = pwriter_opts;
|
||||
|
||||
pmapper->pvstate = pstate;
|
||||
pmapper->pprocess_func = mapper_grep_process;
|
||||
|
|
@ -112,9 +118,9 @@ static sllv_t* mapper_grep_process(lrec_t* pinrec, context_t* pctx, void* pvstat
|
|||
mapper_grep_state_t* pstate = (mapper_grep_state_t*)pvstate;
|
||||
|
||||
char* line = lrec_sprint(pinrec,
|
||||
MLR_GLOBALS.popts->writer_opts.ors,
|
||||
MLR_GLOBALS.popts->writer_opts.ofs,
|
||||
MLR_GLOBALS.popts->writer_opts.ops);
|
||||
pstate->pwriter_opts->ors,
|
||||
pstate->pwriter_opts->ofs,
|
||||
pstate->pwriter_opts->ops);
|
||||
|
||||
int matches = regmatch_or_die(&pstate->regex, line, 0, NULL);
|
||||
sllv_t* poutrecs = NULL;
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ static void mapper_join_usage(FILE* o, char* argv0, char* verb) {
|
|||
|
||||
// ----------------------------------------------------------------
|
||||
static mapper_t* mapper_join_parse_cli(int* pargi, int argc, char** argv,
|
||||
cli_reader_opts_t* _, cli_writer_opts_t* __)
|
||||
cli_reader_opts_t* pmain_reader_opts, cli_writer_opts_t* __)
|
||||
{
|
||||
mapper_join_opts_t* popts = mlr_malloc_or_die(sizeof(mapper_join_opts_t));
|
||||
|
||||
|
|
@ -199,6 +199,8 @@ static mapper_t* mapper_join_parse_cli(int* pargi, int argc, char** argv,
|
|||
|
||||
}
|
||||
|
||||
cli_merge_reader_opts(&popts->reader_opts, pmain_reader_opts);
|
||||
|
||||
// popen is a stdio construct, not an mmap construct, and it can't be supported here.
|
||||
if (popts->prepipe != NULL)
|
||||
popts->reader_opts.use_mmap_for_read = FALSE;
|
||||
|
|
@ -249,7 +251,13 @@ static mapper_t* mapper_join_alloc(mapper_join_opts_t* popts) {
|
|||
pstate->popts = popts;
|
||||
pstate->pleft_field_name_set = hss_from_slls(popts->pleft_join_field_names);
|
||||
pstate->pright_field_name_set = hss_from_slls(popts->pright_join_field_names);
|
||||
pstate->pjoin_bucket_keeper = NULL;
|
||||
|
||||
pstate->pjoin_bucket_keeper = join_bucket_keeper_alloc(
|
||||
popts->prepipe,
|
||||
popts->left_file_name,
|
||||
&popts->reader_opts,
|
||||
popts->pleft_join_field_names);
|
||||
|
||||
pstate->pleft_buckets_by_join_field_values = NULL;
|
||||
pstate->pleft_unpaired_records = NULL;
|
||||
|
||||
|
|
@ -308,28 +316,6 @@ static void mapper_join_free(mapper_t* pmapper) {
|
|||
static sllv_t* mapper_join_process_sorted(lrec_t* pright_rec, context_t* pctx, void* pvstate) {
|
||||
mapper_join_state_t* pstate = (mapper_join_state_t*)pvstate;
|
||||
|
||||
// xxx fix
|
||||
|
||||
// This can't be done in the CLI-parser since it requires information which
|
||||
// isn't known until after the CLI-parser is called.
|
||||
//
|
||||
// Format and separator flags are passed to mapper_join in MLR_GLOBALS rather
|
||||
// than on the stack, since the latter would require complicating the interface
|
||||
// for all the other mappers which don't do their own file I/O. (Also, while
|
||||
// some of the information needed to construct an lrec_reader is available on
|
||||
// the command line before the mapper-allocators are called, some is not
|
||||
// available until after. Hence our obtaining these flags after mapper-alloc.)
|
||||
if (pstate->pjoin_bucket_keeper == NULL) {
|
||||
mapper_join_opts_t* popts = pstate->popts;
|
||||
|
||||
cli_merge_reader_opts(&pstate->popts->reader_opts, &MLR_GLOBALS.popts->reader_opts);
|
||||
|
||||
pstate->pjoin_bucket_keeper = join_bucket_keeper_alloc(
|
||||
popts->prepipe,
|
||||
popts->left_file_name,
|
||||
&popts->reader_opts,
|
||||
popts->pleft_join_field_names);
|
||||
}
|
||||
join_bucket_keeper_t* pkeeper = pstate->pjoin_bucket_keeper; // keystroke-saver
|
||||
|
||||
sllv_t* pleft_records = NULL;
|
||||
|
|
@ -508,7 +494,6 @@ static void mapper_join_form_pairs(sllv_t* pleft_records, lrec_t* pright_rec, ma
|
|||
// ----------------------------------------------------------------
|
||||
static void ingest_left_file(mapper_join_state_t* pstate) {
|
||||
mapper_join_opts_t* popts = pstate->popts;
|
||||
cli_merge_reader_opts(&popts->reader_opts, &MLR_GLOBALS.popts->reader_opts);
|
||||
|
||||
lrec_reader_t* plrec_reader = lrec_reader_alloc(&popts->reader_opts);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef STATS1_ACCUMULATORS_H
|
||||
#define STATS1_ACCUMULATORS_H
|
||||
|
||||
#include "containers/lrec.h"
|
||||
#include "containers/slls.h"
|
||||
#include "containers/lhmsv.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@
|
|||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
mlr_global_init(argv[0], NULL, NULL);
|
||||
mlr_global_init(argv[0], NULL);
|
||||
cli_opts_t* popts = parse_command_line(argc, argv);
|
||||
mlr_global_init(argv[0], popts->ofmt, popts);
|
||||
mlr_global_init(argv[0], popts->ofmt);
|
||||
|
||||
char* prepipe = popts->reader_opts.prepipe;
|
||||
lrec_reader_t* plrec_reader = popts->plrec_reader;
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ static char * all_tests() {
|
|||
|
||||
// ----------------------------------------------------------------
|
||||
int main(int argc, char **argv) {
|
||||
mlr_global_init(argv[0], "%lf", NULL);
|
||||
mlr_global_init(argv[0], "%lf");
|
||||
|
||||
printf("TEST_RVAL_EVALUATORS ENTER\n");
|
||||
char *result = all_tests();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue