Example #1
0
int
main(int argc, char *argv[])
{
	static char dflt_fmt[] = "%Y-%m-%d\n\0H:%M:%S %Z\n";
	yuck_t argi[1U];
	char *outfmt = dflt_fmt;
	char **infmt;
	size_t ninfmt;
	char **input;
	size_t ninput;
	int quietp;
	int res = 0;

	if (yuck_parse(argi, argc, argv)) {
		res = 1;
		goto out;
	}

	if (argi->format_arg) {
		outfmt = argi->format_arg;
		/* unescape sequences, maybe */
		if (argi->backslash_escapes_flag) {
			dt_io_unescape(outfmt);
		}
	} else if (argi->time_flag) {
		outfmt[8] = ' ';
		outfmt[9] = '%';
	}

	if (!argi->input_format_nargs) {
		infmt = argi->args;
		ninfmt = argi->nargs;
		input = NULL;
		ninput = 0;
	} else {
		infmt = argi->input_format_args;
		ninfmt = argi->input_format_nargs;
		input = argi->args;
		ninput = argi->nargs;
	}
	/* get quiet predicate */
	quietp = argi->quiet_flag;

	/* get lines one by one, apply format string and print date/time */
	if (ninput == 0) {
		/* read from stdin */
		proc_lines((const char*const*)infmt, ninfmt, outfmt, quietp);
	} else {
		const char *const *cinfmt = (const char*const*)infmt;
		for (size_t i = 0; i < ninput; i++) {
			proc_line(input[i], cinfmt, ninfmt, outfmt, quietp);
		}
	}

out:
	yuck_free(argi);
	return res;
}
Example #2
0
int main(int argc, char *argv[])
{
	yuck_t argp[1U];
	yuck_parse(argp, argc, argv);

	if (argp->extra_flag) {
		puts("BLING BLING!");
	}

	switch (argp->cmd) {
	case XMPL_CMD_NONE:
	default:
		puts("no command :(");
		break;
	}

	yuck_free(argp);
	return 0;
}
Example #3
0
int
mux_main(mux_ctx_t ctx, int argc, char *argv[])
{
	static struct mux_ctx_s __ctx[1U];
	static struct sumux_opt_s __opts[1U];
	yuck_t argi[1U];
	int rc = 0;

	if (yuck_parse(argi, argc, argv)) {
		rc = 1;
		goto out;
	} else if (UNLIKELY(ctx == NULL)) {
		ctx = __ctx;
		ctx->opts = __opts;
	}

	if (argi->human_readable_flag) {
		/* wipe off the stuff ute-mux prepared for us */
		const char *fn;

		if (ctx->wrr != NULL &&
		    ctx->opts->outfile == NULL &&
		    (fn = ute_fn(ctx->wrr)) != NULL) {
			unlink(fn);
			ute_close(ctx->wrr);
			ctx->wrr = NULL;
		}
	}
	if (argi->all_flag) {
		ctx->opts->flags |= SUMUX_FLAG_ALL_TICKS;
	}

	for (size_t j = 0U; j < argi->nargs; j++) {
		const char *f = argi->args[j];
		int fd;

		/* open the infile ... */
		if (f[0] == '-' && f[1] == '\0') {
			ctx->infd = fd = STDIN_FILENO;
			ctx->infn = NULL;
			ctx->badfd = STDERR_FILENO;
		} else if ((fd = open(f, 0)) < 0) {
			ctx->infd = -1;
			error("cannot open file '%s'", f);
			/* just try the next bloke */
			continue;
		} else if (argi->guess_flag && guess(ctx, f) < 0) {
			error("cannot guess info from '%s'", f);
			/* well try to do the actual muxing anyway */
			;
		} else {
			/* huuuuge success */
			ctx->infd = fd;
			ctx->infn = f;
			ctx->badfd = STDERR_FILENO;
		}
		/* ... and now mux it */
		if (argi->human_readable_flag) {
			dump_l1bi5(ctx);
		} else {
			mux(ctx);
		}
		/* close the infile */
		close(fd);
	}

out:
	yuck_free(argi);
	return rc;
}
Example #4
0
int
main(int argc, char *argv[])
{
	static char dflt_fmt[] = "%Y-%m-%d\n\0H:%M:%S %Z\n";
	yuck_t argi[1U];
	char *outfmt = dflt_fmt;
	char **infmt;
	size_t ninfmt;
	char **input;
	size_t ninput;
	int quietp;
	int res = 0;

	if (yuck_parse(argi, argc, argv)) {
		res = 1;
		goto out;
	}

	if (argi->format_arg) {
		outfmt = argi->format_arg;
		/* unescape sequences, maybe */
		if (argi->backslash_escapes_flag) {
			dt_io_unescape(outfmt);
		}
	} else if (argi->time_flag) {
		outfmt[8] = ' ';
		outfmt[9] = '%';
	}

	if (!argi->input_format_nargs) {
		infmt = argi->args;
		ninfmt = argi->nargs;
		input = NULL;
		ninput = 0;
	} else {
		infmt = argi->input_format_args;
		ninfmt = argi->input_format_nargs;
		input = argi->args;
		ninput = argi->nargs;
	}
	/* get quiet predicate */
	quietp = argi->quiet_flag;

	/* set locale specific/independent behaviour */
	with (const char *loc) {
		if (!argi->locale_flag) {
			loc = "C";
			/* we need to null out TZ for UTC */
			setenv("TZ", "", 1);
		} else {
			loc = "";
		}
		/* actually set our findings in stone */
		setlocale(LC_TIME, loc);
		tzset();
	}

	/* get lines one by one, apply format string and print date/time */
	if (ninput == 0) {
		/* read from stdin */
		proc_lines((const char*const*)infmt, ninfmt, outfmt, quietp);
	} else {
		const char *const *cinfmt = (const char*const*)infmt;
		for (size_t i = 0; i < ninput; i++) {
			proc_line(input[i], cinfmt, ninfmt, outfmt, quietp);
		}
	}

out:
	yuck_free(argi);
	return res;
}
Example #5
0
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;
}