static int dt_add_dt(void) { static const char str[] = "2012-03-28T23:55:55"; struct dt_dt_s d; struct dt_dt_s dur; int res = 0; fprintf(stderr, "testing %s +1d1h ...\n", str); d = dt_strpdt(str, NULL, NULL); /* we lack some lovely ctors for this */ dur = dt_dt_initialiser(); dt_make_sandwich(&dur, DT_DAISY, DT_TUNK); dur.dur = 1; dur.neg = 0; dur.d.daisy = 1; dur.t.dur = 1; dur.t.neg = 0; dur.t.sdur = 3600; /* the actual addition */ d = dt_dtadd(d, dur); CHECK(d.d.typ != DT_YMD, " DATE TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.d.typ, (unsigned int)DT_YMD); CHECK(d.t.typ != DT_HMS, " TIME TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.t.typ, (unsigned int)DT_HMS); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME NEGATED BIT SET\n"); CHECK_EQ((unsigned int)d.d.ymd.y, 2012U, " YEAR %u ... should be %u\n"); CHECK_EQ((unsigned int)d.d.ymd.m, 3U, " MONTH %u ... should be %u\n"); CHECK_EQ((unsigned int)d.d.ymd.d, 30U, " DAY %u ... should be %u\n"); CHECK_EQ((unsigned int)d.t.hms.h, 00U, " HOUR %u ... should be %u\n"); CHECK_EQ((unsigned int)d.t.hms.m, 55U, " MINUTE %u ... should be %u\n"); CHECK_EQ((unsigned int)d.t.hms.s, 55U, " SECOND %u ... should be %u\n"); /* make sure the padding leaves no garbage */ CHECK_RES(res, d.d.ymd.u & ~0x1fffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.d.ymd.u & ~0x1fffff)); CHECK_RES(res, d.t.hms.u & ~0x1f3f3f3fffffff, " TIME PADDING NOT NAUGHT %x\n", (unsigned int)(d.t.hms.u & ~0x1f3f3f3fffffff)); return res; }
static int test_dt_no_fmt(void) { static const char str[] = "2012-03-28 12:34:56"; struct dt_dt_s d; int res = 0; fprintf(stderr, "testing %s ...\n", str); d = dt_strpdt(str, NULL, NULL); CHECK(!d.sandwich, " NOT A SANDWICH ... but should be\n"); CHECK(!dt_sandwich_p(d), " TYPE is not a sandwich\n"); CHECK(d.d.typ != DT_YMD, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.d.typ, (unsigned int)DT_YMD); CHECK(d.t.typ != DT_HMS, " TIME TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.t.typ, (unsigned int)DT_HMS); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME DURATION BIT SET\n"); CHECK(d.d.ymd.y != 2012, " YEAR %u ... should be 2012\n", (unsigned int)d.d.ymd.y); CHECK(d.d.ymd.m != 3, " MONTH %u ... should be 3\n", (unsigned int)d.d.ymd.m); CHECK(d.d.ymd.d != 28, " DAY %u ... should be 28\n", (unsigned int)d.d.ymd.d); CHECK(d.t.hms.h != 12, " HOUR %u ... should be 12\n", (unsigned int)d.t.hms.h); CHECK(d.t.hms.m != 34, " MINUTE %u ... should be 34\n", (unsigned int)d.t.hms.m); CHECK(d.t.hms.s != 56, " SECOND %u ... should be 56\n", (unsigned int)d.t.hms.s); CHECK(d.t.hms.ns != 0, " NANOSECOND %u ... should be 0\n", (unsigned int)d.t.hms.ns); /* make sure the padding leaves no garbage */ CHECK_RES(res, d.d.ymd.u & ~0x1fffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.d.ymd.u & ~0x1fffff)); CHECK_RES(res, d.t.hms.u & ~0x1f3f3f3fffffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.t.hms.u & ~0x1f3f3f3fffffff)); return res; }
int main(int argc, char *argv[]) { yuck_t argi[1U]; char **fmt; size_t nfmt; dexpr_t root; oper_t o = OP_UNK; int res = 0; if (yuck_parse(argi, argc, argv)) { res = 1; goto out; } /* init and unescape sequences, maybe */ ckv_fmt = fmt = argi->input_format_args; ckv_nfmt = nfmt = argi->input_format_nargs; if (argi->backslash_escapes_flag) { for (size_t i = 0; i < nfmt; i++) { dt_io_unescape(fmt[i]); } } if (argi->base_arg) { struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL); dt_set_base(base); } if (argi->eq_flag) { o = OP_EQ; } else if (argi->ne_flag) { o = OP_NE; } else if (argi->lt_flag || argi->ot_flag) { o = OP_LT; } else if (argi->le_flag) { o = OP_LE; } else if (argi->gt_flag || argi->nt_flag) { o = OP_GT; } else if (argi->ge_flag) { o = OP_GE; } /* parse the expression */ if (argi->nargs == 0U || dexpr_parse(&root, argi->args[0U], strlen(argi->args[0U])) < 0) { res = 1; error("Error: need an expression to grep"); goto out; } /* fixup o, default is OP_EQ */ if (o != OP_UNK && root->type != DEX_VAL) { res = 1; error("\ long opt operators (--lt, --gt, ...) cannot be used in conjunction \n\ with complex expressions"); goto out; } else if (o != OP_UNK) {
static int add_t_only(void) { static const char str[] = "12:34:56"; struct dt_dt_s d; struct dt_dt_s dur; int res = 0; /* 2012-03-28 (using no format) */ fprintf(stderr, "testing %s +1h ...\n", str); d = dt_strpdt(str, NULL, NULL); /* we lack some lovely ctors for this */ dur.d = dt_d_initialiser(); dt_make_t_only(&dur, DT_TUNK); dur.dur = 1; dur.neg = 0; dur.t.dur = 1; dur.t.neg = 0; dur.t.sdur = 3600; /* the actual addition */ d = dt_dtadd(d, dur); CHECK(d.typ != DT_SANDWICH_UNK, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.typ, (unsigned int)DT_SANDWICH_UNK); CHECK(d.t.typ != DT_HMS, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.t.typ, (unsigned int)DT_HMS); CHECK(d.d.u, " DATE COMPONENT NOT NAUGHT %" PRIu64 "\n", (uint64_t)d.d.u); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME NEGATED BIT SET\n"); CHECK_EQ((unsigned int)d.t.hms.h, 13U, " HOUR %u ... should be %u\n"); CHECK_EQ((unsigned int)d.t.hms.m, 34U, " MINUTE %u ... should be %u\n"); CHECK_EQ((unsigned int)d.t.hms.s, 56U, " SECOND %u ... should be %u\n"); /* make sure the padding leaves no garbage */ CHECK_RES(res, d.t.hms.u & ~0x1f3f3f3fffffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.t.hms.u & ~0x1f3f3f3fffffff)); return res; }
static int test_t_only_no_fmt(void) { static const char str[] = "12:34:56"; struct dt_dt_s d; int res = 0; /* 12:34:56 (using no format) */ fprintf(stderr, "testing %s ...\n", str); d = dt_strpdt(str, NULL, NULL); CHECK(!d.sandwich, " NOT A SANDWICH ... but should be\n"); CHECK(!dt_sandwich_only_t_p(d), " TYPE is not a t-only\n"); CHECK(d.typ != DT_SANDWICH_UNK, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.typ, (unsigned int)DT_SANDWICH_UNK); CHECK(d.t.typ != DT_HMS, " TIME TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.t.typ, (unsigned int)DT_HMS); CHECK(d.d.u, " DATE COMPONENT NOT NAUGHT %" PRIu64 "\n", (uint64_t)d.d.u); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME DURATION BIT SET\n"); CHECK(d.t.hms.h != 12, " HOUR %u ... should be 12\n", (unsigned int)d.t.hms.h); CHECK(d.t.hms.m != 34, " MINUTE %u ... should be 34\n", (unsigned int)d.t.hms.m); CHECK(d.t.hms.s != 56, " SECOND %u ... should be 56\n", (unsigned int)d.t.hms.s); CHECK(d.t.hms.ns != 0, " NANOSECOND %u ... should be 0\n", (unsigned int)d.t.hms.ns); /* make sure the padding leaves no garbage */ CHECK_RES(res, d.t.hms.u & ~0x1f3f3f3fffffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.t.hms.u & ~0x1f3f3f3fffffff)); return res; }
static int add_d_only(void) { static const char str[] = "2012-03-28"; struct dt_dt_s d; struct dt_dt_s dur; int res = 0; /* 2012-03-28 (using no format) */ fprintf(stderr, "testing %s +1d ...\n", str); d = dt_strpdt(str, NULL, NULL); /* we lack some lovely ctors for this */ dur.t = dt_t_initialiser(); dt_make_d_only(&dur, DT_DAISY); dur.dur = 1; dur.neg = 0; dur.d.daisy = 1; /* the actual addition */ d = dt_dtadd(d, dur); CHECK(d.d.typ != DT_YMD, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.d.typ, (unsigned int)DT_YMD); CHECK(d.t.u, " TIME COMPONENT NOT NAUGHT %" PRIu64 "\n", (uint64_t)d.t.u); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME NEGATED BIT SET\n"); CHECK_EQ((unsigned int)d.d.ymd.y, 2012U, " YEAR %u ... should be %u\n"); CHECK_EQ((unsigned int)d.d.ymd.m, 3U, " MONTH %u ... should be %u\n"); CHECK_EQ((unsigned int)d.d.ymd.d, 29U, " DAY %u ... should be %u\n"); /* make sure the padding leaves no garbage */ CHECK_RES(res, d.d.ymd.u & ~0x1fffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.d.ymd.u & ~0x1fffff)); return res; }
static int test_d_only_no_fmt(void) { static const char str[] = "2012-03-28"; struct dt_dt_s d; int res = 0; /* 2012-03-28 (using no format) */ fprintf(stderr, "testing %s ...\n", str); d = dt_strpdt(str, NULL, NULL); CHECK(d.sandwich, " IS A SANDWICH ... but should be not\n"); CHECK(!dt_sandwich_only_d_p(d), " TYPE is not a d-only\n"); CHECK(d.d.typ != DT_YMD, " TYPE DIFFERS %u ... should be %u\n", (unsigned int)d.d.typ, (unsigned int)DT_YMD); CHECK(d.t.u, " TIME COMPONENT NOT NAUGHT %" PRIu64 "\n", (uint64_t)d.t.u); CHECK(d.dur, " DURATION BIT SET\n"); CHECK(d.neg, " NEGATED BIT SET\n"); CHECK(d.t.dur, " TIME DURATION BIT SET\n"); CHECK(d.t.neg, " TIME NEGATED BIT SET\n"); CHECK(d.d.ymd.y != 2012, " YEAR %u ... should be 2012\n", (unsigned int)d.d.ymd.y); CHECK(d.d.ymd.m != 3, " MONTH %u ... should be 3\n", (unsigned int)d.d.ymd.m); CHECK(d.d.ymd.d != 28, " DAY %u ... should be 28\n", (unsigned int)d.d.ymd.d); /* make sure the padding leaves no garbage, not fatal tho */ CHECK_RES(res, d.d.ymd.u & ~0x1fffff, " PADDING NOT NAUGHT %x\n", (unsigned int)(d.d.ymd.u & ~0x1fffff)); return res; }
int main(int argc, char *argv[]) { yuck_t argi[1U]; struct dt_dt_s d; struct __strpdtdur_st_s st = __strpdtdur_st_initialiser(); const char *ofmt; char **fmt; size_t nfmt; int rc = 0; bool dt_given_p = false; zif_t fromz = NULL; zif_t z = NULL; zif_t hackz = NULL; if (yuck_parse(argi, argc, argv)) { rc = 1; goto out; } else if (argi->nargs == 0) { error("Error: DATE or DURATION must be specified\n"); yuck_auto_help(argi); rc = 1; goto out; } /* init and unescape sequences, maybe */ ofmt = argi->format_arg; fmt = argi->input_format_args; nfmt = argi->input_format_nargs; if (argi->backslash_escapes_flag) { dt_io_unescape(argi->format_arg); for (size_t i = 0; i < nfmt; i++) { dt_io_unescape(fmt[i]); } } /* try and read the from and to time zones */ if (argi->from_zone_arg) { fromz = dt_io_zone(argi->from_zone_arg); } if (argi->zone_arg) { z = dt_io_zone(argi->zone_arg); } if (argi->base_arg) { struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL); dt_set_base(base); } /* sanity checks, decide whether we're a mass date adder * or a mass duration adder, or both, a date and durations are * present on the command line */ with (const char *inp = argi->args[0U]) { /* date parsing needed postponing as we need to find out * about the durations */ if (!dt_unk_p(dt_io_strpdt(inp, fmt, nfmt, NULL))) { dt_given_p = true; } } /* check first arg, if it's a date the rest of the arguments are * durations, if not, dates must be read from stdin */ for (size_t i = dt_given_p; i < argi->nargs; i++) { const char *inp = argi->args[i]; do { if (dt_io_strpdtdur(&st, inp) < 0) { serror("Error: \ cannot parse duration string `%s'", st.istr); rc = 1; goto dur_out; } } while (__strpdtdur_more_p(&st)); } /* check if there's only d durations */ hackz = durs_only_d_p(st.durs, st.ndurs) ? NULL : fromz; /* read the first argument again in light of a completely parsed * duration sequence */ if (dt_given_p) { const char *inp = argi->args[0U]; if (dt_unk_p(d = dt_io_strpdt(inp, fmt, nfmt, hackz))) { error("\ Error: cannot interpret date/time string `%s'", inp); rc = 1; goto dur_out; } } /* start the actual work */ if (dt_given_p && st.ndurs) { if (!dt_unk_p(d = dadd_add(d, st.durs, st.ndurs))) { if (UNLIKELY(d.fix) && !argi->quiet_flag) { rc = 2; } if (hackz == NULL && fromz != NULL) { /* fixup zone */ d = dtz_forgetz(d, fromz); } dt_io_write(d, ofmt, z, '\n'); } else { rc = 1; } } else if (st.ndurs) { /* read dates from stdin */ struct grep_atom_s __nstk[16], *needle = __nstk; size_t nneedle = countof(__nstk); struct grep_atom_soa_s ndlsoa; struct mass_add_clo_s clo[1]; void *pctx; /* no threads reading this stream */ __io_setlocking_bycaller(stdout); /* lest we overflow the stack */ if (nfmt >= nneedle) { /* round to the nearest 8-multiple */ nneedle = (nfmt | 7) + 1; needle = calloc(nneedle, sizeof(*needle)); } /* and now build the needle */ ndlsoa = build_needle(needle, nneedle, fmt, nfmt); /* using the prchunk reader now */ if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) { serror("could not open stdin"); goto ndl_free; } /* build the clo and then loop */ clo->pctx = pctx; clo->gra = &ndlsoa; clo->st = st; clo->fromz = fromz; clo->hackz = hackz; clo->z = z; clo->ofmt = ofmt; clo->sed_mode_p = argi->sed_mode_flag; clo->quietp = argi->quiet_flag; while (prchunk_fill(pctx) >= 0) { rc |= mass_add_dur(clo); } /* get rid of resources */ free_prchunk(pctx); ndl_free: if (needle != __nstk) { free(needle); } } else { /* mass-adding durations to reference date */ struct mass_add_clo_s clo[1]; void *pctx; /* no threads reading this stream */ __io_setlocking_bycaller(stdout); /* using the prchunk reader now */ if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) { serror("could not open stdin"); goto dur_out; } /* build the clo and then loop */ clo->pctx = pctx; clo->rd = d; clo->fromz = fromz; clo->hackz = hackz; clo->z = z; clo->ofmt = ofmt; clo->sed_mode_p = argi->sed_mode_flag; clo->quietp = argi->quiet_flag; while (prchunk_fill(pctx) >= 0) { rc |= mass_add_d(clo); } /* get rid of resources */ free_prchunk(pctx); } dur_out: /* free the strpdur status */ __strpdtdur_free(&st); dt_io_clear_zones(); out: yuck_free(argi); return rc; }
int main(int argc, char *argv[]) { static struct dt_dtdur_s ite_p1; yuck_t argi[1U]; struct dt_dt_s tmp; char **ifmt; size_t nifmt; char *ofmt; dt_dttyp_t tgttyp; int rc = 0; struct dseq_clo_s clo = { .ite = &ite_p1, .nite = 1, .altite = NULL, .naltite = 0, .ss = 0, .dir = 0, .flags = 0, }; if (yuck_parse(argi, argc, argv)) { rc = 1; goto out; } /* assign ofmt/ifmt */ ofmt = argi->format_arg; if (argi->backslash_escapes_flag) { dt_io_unescape(ofmt); } nifmt = argi->input_format_nargs; ifmt = argi->input_format_args; if (argi->from_locale_arg) { setilocale(argi->from_locale_arg); } if (argi->locale_arg) { setflocale(argi->locale_arg); } if (argi->base_arg) { struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL); dt_set_base(base); } for (size_t i = 0; i < argi->skip_nargs; i++) { clo.ss = set_skip(clo.ss, argi->skip_args[i]); } if (argi->alt_inc_arg) { struct __strpdtdur_st_s st = __strpdtdur_st_initialiser(); do { if (dt_io_strpdtdur(&st, argi->alt_inc_arg) < 0) { if (!argi->quiet_flag) { error("Error: \ cannot parse duration string `%s'", argi->alt_inc_arg); } rc = 1; goto out; } } while (__strpdtdur_more_p(&st)); /* assign values */ clo.altite = st.durs; clo.naltite = st.ndurs; } switch (argi->nargs) { struct dt_dt_s fst, lst; default: yuck_auto_help(argi); rc = 1; goto out; case 2: lst = dt_io_strpdt(argi->args[1U], ifmt, nifmt, NULL); if (dt_unk_p(lst)) { if (!argi->quiet_flag) { dt_io_warn_strpdt(argi->args[1U]); } rc = 1; goto out; } else if (UNLIKELY(lst.fix) && !argi->quiet_flag) { rc = 2; } /* fallthrough */ case 1: fst = dt_io_strpdt(argi->args[0U], ifmt, nifmt, NULL); if (dt_unk_p(fst)) { if (!argi->quiet_flag) { dt_io_warn_strpdt(argi->args[0U]); } rc = 1; goto out; } else if (UNLIKELY(fst.fix) && !argi->quiet_flag) { rc = 2; } /* check the input arguments and do the sane thing now * if it's all dates, use DURD iterator * if it's all times, use DURS/DURM/DURH iterators * if one of them is a dt, promote the other */ if (dt_sandwich_only_d_p(fst)) { /* emulates old dseq(1) */ if (argi->nargs == 1U) { lst.d = dt_date(fst.d.typ); dt_make_d_only(&lst, fst.d.typ); } clo.ite->d = dt_make_ddur(DT_DURD, 1); } else if (dt_sandwich_only_t_p(fst)) { /* emulates old tseq(1) */ if (argi->nargs == 1U) { lst.t = dt_time(); dt_make_t_only(&lst, DT_HMS); } } else if (dt_sandwich_p(fst)) { if (argi->nargs == 1U) { lst = dt_datetime(fst.typ); dt_make_sandwich(&lst, fst.d.typ, DT_HMS); } clo.ite->d = dt_make_ddur(DT_DURD, 1); } else { error("\ don't know how to handle single argument case"); rc = 1; goto out; } goto make_compat; case 3: { struct __strpdtdur_st_s st = __strpdtdur_st_initialiser(); /* get lower bound */ fst = dt_io_strpdt(argi->args[0U], ifmt, nifmt, NULL); if (dt_unk_p(fst)) { if (!argi->quiet_flag) { dt_io_warn_strpdt(argi->args[0U]); } rc = 1; goto out; } else if (UNLIKELY(fst.fix) && !argi->quiet_flag) { rc = 2; } /* get increment */ do { if (dt_io_strpdtdur(&st, argi->args[1U]) < 0) { error("Error: \ cannot parse duration string `%s'", argi->args[1U]); rc = 1; goto out; } } while (__strpdtdur_more_p(&st)); /* assign values */ clo.ite = st.durs; clo.nite = st.ndurs; clo.flags |= CLO_FL_FREE_ITE; /* get upper bound */ lst = dt_io_strpdt(argi->args[2U], ifmt, nifmt, NULL); if (dt_unk_p(lst)) { if (!argi->quiet_flag) { dt_io_warn_strpdt(argi->args[2U]); } rc = 1; goto out; } else if (UNLIKELY(lst.fix) && !argi->quiet_flag) { rc = 2; } goto make_compat; } make_compat: if (LIKELY(fst.typ == lst.typ)) { clo.fst = fst; clo.lst = lst; } else { clo.fst = fst; clo.lst = dt_dtconv(fst.typ, lst); } break; } /* promote the args maybe */ if ((dt_sandwich_only_d_p(clo.fst) && dt_sandwich_only_t_p(clo.lst)) || (dt_sandwich_only_t_p(clo.fst) && dt_sandwich_only_d_p(clo.lst))) { error("\ cannot mix dates and times as arguments"); rc = 1; goto out; } else if (dt_sandwich_only_d_p(clo.fst) && dt_sandwich_p(clo.lst)) {
int main(int argc, char *argv[]) { struct gengetopt_args_info argi[1]; const char *ofmt; char **fmt; size_t nfmt; int res = 0; zif_t fromz = NULL; zif_t z = NULL; if (cmdline_parser(argc, argv, argi)) { res = 1; goto out; } /* init and unescape sequences, maybe */ ofmt = argi->format_arg; fmt = argi->input_format_arg; nfmt = argi->input_format_given; if (argi->backslash_escapes_given) { dt_io_unescape(argi->format_arg); for (size_t i = 0; i < nfmt; i++) { dt_io_unescape(fmt[i]); } } /* try and read the from and to time zones */ if (argi->from_zone_given) { fromz = zif_open(argi->from_zone_arg); } if (argi->zone_given) { z = zif_open(argi->zone_arg); } if (argi->default_given) { struct dt_dt_s dflt = dt_strpdt(argi->default_arg, NULL, NULL); dt_set_default(dflt); } if (argi->inputs_num) { for (size_t i = 0; i < argi->inputs_num; i++) { const char *inp = argi->inputs[i]; struct dt_dt_s d = dt_io_strpdt(inp, fmt, nfmt, fromz); if (!dt_unk_p(d)) { dt_io_write(d, ofmt, z, '\n'); } else if (!argi->quiet_given) { dt_io_warn_strpdt(inp); } } } else { /* read from stdin */ size_t lno = 0; struct grep_atom_s __nstk[16], *needle = __nstk; size_t nneedle = countof(__nstk); struct grep_atom_soa_s ndlsoa; void *pctx; struct prln_ctx_s prln = { .ndl = &ndlsoa, .ofmt = ofmt, .fromz = fromz, .outz = z, .sed_mode_p = argi->sed_mode_given, .quietp = argi->quiet_given, }; /* no threads reading this stream */ __io_setlocking_bycaller(stdout); /* lest we overflow the stack */ if (nfmt >= nneedle) { /* round to the nearest 8-multiple */ nneedle = (nfmt | 7) + 1; needle = calloc(nneedle, sizeof(*needle)); } /* and now build the needles */ ndlsoa = build_needle(needle, nneedle, fmt, nfmt); /* using the prchunk reader now */ if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) { error(errno, "Error: could not open stdin"); goto ndl_free; } while (prchunk_fill(pctx) >= 0) { for (char *line; prchunk_haslinep(pctx); lno++) { size_t llen = prchunk_getline(pctx, &line); proc_line(prln, line, llen); } } /* get rid of resources */ free_prchunk(pctx); ndl_free: if (needle != __nstk) { free(needle); } } if (argi->from_zone_given) { zif_close(fromz); } if (argi->zone_given) { zif_close(z); } out: cmdline_parser_free(argi); return res; }
int main(int argc, char *argv[]) { yuck_t argi[1U]; struct dt_dt_s d; struct __strpdtdur_st_s st = __strpdtdur_st_initialiser(); char *inp; const char *ofmt; char **fmt; size_t nfmt; int rc = 0; bool dt_given_p = false; bool nextp = false; zif_t fromz = NULL; zif_t z = NULL; zif_t hackz = NULL; if (yuck_parse(argi, argc, argv)) { rc = 1; goto out; } else if (argi->nargs == 0U) { error("Error: DATE or DURATION must be specified\n"); yuck_auto_help(argi); rc = 1; goto out; } /* init and unescape sequences, maybe */ ofmt = argi->format_arg; fmt = argi->input_format_args; nfmt = argi->input_format_nargs; if (argi->backslash_escapes_flag) { dt_io_unescape(argi->format_arg); for (size_t i = 0; i < nfmt; i++) { dt_io_unescape(fmt[i]); } } /* try and read the from and to time zones */ if (argi->from_zone_arg) { fromz = dt_io_zone(argi->from_zone_arg); } if (argi->zone_arg) { z = dt_io_zone(argi->zone_arg); } if (argi->next_flag) { nextp = true; } if (argi->base_arg) { struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL); dt_set_base(base); } /* check first arg, if it's a date the rest of the arguments are * durations, if not, dates must be read from stdin */ for (size_t i = 0U; i < argi->nargs; i++) { inp = argi->args[i]; do { if (dt_io_strpdtrnd(&st, inp) < 0) { if (UNLIKELY(i == 0)) { /* that's ok, must be a date then */ dt_given_p = true; } else { serror("Error: \ cannot parse duration/rounding string `%s'", st.istr); } } } while (__strpdtdur_more_p(&st)); } /* check if there's only d durations */ hackz = durs_only_d_p(st.durs, st.ndurs) ? NULL : fromz; /* sanity checks */ if (dt_given_p) { /* date parsing needed postponing as we need to find out * about the durations */ inp = argi->args[0U]; if (dt_unk_p(d = dt_io_strpdt(inp, fmt, nfmt, hackz))) { error("Error: \ cannot interpret date/time string `%s'", argi->args[0U]); rc = 1; goto out; } } else if (st.ndurs == 0) {