static void proc_line(struct prln_ctx_s ctx, char *line, size_t llen) { struct dt_dt_s d; char *sp = NULL; char *ep = NULL; do { d = dt_io_find_strpdt2(line, ctx.ndl, &sp, &ep, ctx.fromz); /* check if line matches */ if (!dt_unk_p(d) && ctx.sed_mode_p) { __io_write(line, sp - line, stdout); dt_io_write(d, ctx.ofmt, ctx.outz, '\0'); llen -= (ep - line); line = ep; } else if (!dt_unk_p(d)) { dt_io_write(d, ctx.ofmt, ctx.outz, '\n'); break; } else if (ctx.sed_mode_p) { line[llen] = '\n'; __io_write(line, llen + 1, stdout); break; } else { /* obviously unmatched, warn about it in non -q mode */ if (!ctx.quietp) { dt_io_warn_strpdt(line); } break; } } while (1); return; }
static int mass_add_d(const struct mass_add_clo_s *clo) { /* read lines from stdin * interpret as durations * add to reference date * output */ size_t lno = 0; struct dt_dt_s d; struct __strpdtdur_st_s st = __strpdtdur_st_initialiser(); int rc = 0; for (char *line; prchunk_haslinep(clo->pctx); lno++) { size_t llen; int has_dur_p = 1; llen = prchunk_getline(clo->pctx, &line); /* check for durations on this line */ do { if (dt_io_strpdtdur(&st, line) < 0) { has_dur_p = 0; } } while (__strpdtdur_more_p(&st)); /* finish with newline again */ line[llen] = '\n'; if (has_dur_p) { if (UNLIKELY(clo->rd.fix) && !clo->quietp) { rc = 2; } /* perform addition now */ d = dadd_add(clo->rd, st.durs, st.ndurs); if (clo->hackz == NULL && clo->fromz != NULL) { /* fixup zone */ d = dtz_forgetz(d, clo->fromz); } /* no sed mode here */ dt_io_write(d, clo->ofmt, clo->z, '\n'); } else if (clo->sed_mode_p) { __io_write(line, llen + 1, stdout); } else if (!clo->quietp) { line[llen] = '\0'; dt_io_warn_strpdt(line); rc = 2; } /* just reset the ndurs slot */ st.ndurs = 0; } /* free associated duration resources */ __strpdtdur_free(&st); return rc; }
static int proc_line(const struct mass_add_clo_s *clo, char *line, size_t llen) { struct dt_dt_s d; char *sp = NULL; char *ep = NULL; int rc = 0; do { /* check if line matches, */ d = dt_io_find_strpdt2( line, llen, clo->gra, &sp, &ep, clo->fromz); if (!dt_unk_p(d)) { if (UNLIKELY(d.fix) && !clo->quietp) { rc = 2; } /* perform addition now */ d = dadd_add(d, clo->st.durs, clo->st.ndurs); if (clo->hackz == NULL && clo->fromz != NULL) { /* fixup zone */ d = dtz_forgetz(d, clo->fromz); } if (clo->sed_mode_p) { __io_write(line, sp - line, stdout); dt_io_write(d, clo->ofmt, clo->z, '\0'); llen -= (ep - line); line = ep; } else { dt_io_write(d, clo->ofmt, clo->z, '\n'); break; } } else if (clo->sed_mode_p) { line[llen] = '\n'; __io_write(line, llen + 1, stdout); break; } else { /* obviously unmatched, warn about it in non -q mode */ if (!clo->quietp) { dt_io_warn_strpdt(line); rc = 2; } break; } } while (1); return rc; }
static int proc_line(struct prln_ctx_s ctx, char *line, size_t llen) { struct dt_dt_s d; char *sp = NULL; char *ep = NULL; int rc = 0; do { /* check if line matches, */ d = dt_io_find_strpdt2( line, llen, ctx.ndl, &sp, &ep, ctx.fromz); if (!dt_unk_p(d)) { if (UNLIKELY(d.fix) && !ctx.quietp) { rc = 2; } /* perform addition now */ d = dround(d, ctx.st->durs, ctx.st->ndurs, ctx.nextp); if (ctx.hackz == NULL && ctx.fromz != NULL) { /* fixup zone */ d = dtz_forgetz(d, ctx.fromz); } if (ctx.sed_mode_p) { __io_write(line, sp - line, stdout); dt_io_write(d, ctx.ofmt, ctx.outz, '\0'); llen -= (ep - line); line = ep; } else { dt_io_write(d, ctx.ofmt, ctx.outz, '\n'); break; } } else if (ctx.sed_mode_p) { line[llen] = '\n'; __io_write(line, llen + 1, stdout); break; } else { /* obviously unmatched, warn about it in non -q mode */ if (!ctx.quietp) { dt_io_warn_strpdt(line); rc = 2; } break; } } while (1); return rc; }
static void proc_line( const char *ln, const char *const *fmt, size_t nfmt, const char *ofmt, int quietp) { struct tm tm = __tm_initialiser(); if (pars_line(&tm, fmt, nfmt, ln) < 0) { if (!quietp) { dt_io_warn_strpdt(ln); } } else { prnt_line(ofmt, &tm); } return; }
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; }