1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* kdc/t_tdumputil.c - test tdumputil.c functions */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert #include <stdio.h>
34*7f2fe78bSCy Schubert #include <stdlib.h>
35*7f2fe78bSCy Schubert #include <unistd.h>
36*7f2fe78bSCy Schubert
37*7f2fe78bSCy Schubert #include "tdumputil.h"
38*7f2fe78bSCy Schubert
39*7f2fe78bSCy Schubert static char *argv0;
40*7f2fe78bSCy Schubert
41*7f2fe78bSCy Schubert static void
usage(void)42*7f2fe78bSCy Schubert usage(void)
43*7f2fe78bSCy Schubert {
44*7f2fe78bSCy Schubert fprintf(stderr,
45*7f2fe78bSCy Schubert "usage: %s: [-T rectype] [-c] nfields {fieldnames} {fields}\n",
46*7f2fe78bSCy Schubert argv0);
47*7f2fe78bSCy Schubert exit(1);
48*7f2fe78bSCy Schubert }
49*7f2fe78bSCy Schubert
50*7f2fe78bSCy Schubert int
main(int argc,char ** argv)51*7f2fe78bSCy Schubert main(int argc, char **argv)
52*7f2fe78bSCy Schubert {
53*7f2fe78bSCy Schubert int ch, csv = 0, i, nf;
54*7f2fe78bSCy Schubert char **a, *rectype = NULL;
55*7f2fe78bSCy Schubert struct rechandle *h;
56*7f2fe78bSCy Schubert
57*7f2fe78bSCy Schubert argv0 = argv[0];
58*7f2fe78bSCy Schubert while ((ch = getopt(argc, argv, "T:c")) != -1) {
59*7f2fe78bSCy Schubert switch (ch) {
60*7f2fe78bSCy Schubert case 'T':
61*7f2fe78bSCy Schubert rectype = optarg;
62*7f2fe78bSCy Schubert break;
63*7f2fe78bSCy Schubert case 'c':
64*7f2fe78bSCy Schubert csv = 1;
65*7f2fe78bSCy Schubert break;
66*7f2fe78bSCy Schubert default:
67*7f2fe78bSCy Schubert usage();
68*7f2fe78bSCy Schubert break;
69*7f2fe78bSCy Schubert }
70*7f2fe78bSCy Schubert }
71*7f2fe78bSCy Schubert argc -= optind;
72*7f2fe78bSCy Schubert argv += optind;
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert if (csv)
75*7f2fe78bSCy Schubert h = rechandle_csv(stdout, rectype);
76*7f2fe78bSCy Schubert else
77*7f2fe78bSCy Schubert h = rechandle_tabsep(stdout, rectype);
78*7f2fe78bSCy Schubert if (h == NULL)
79*7f2fe78bSCy Schubert exit(1);
80*7f2fe78bSCy Schubert
81*7f2fe78bSCy Schubert if (*argv == NULL)
82*7f2fe78bSCy Schubert usage();
83*7f2fe78bSCy Schubert nf = atoi(*argv);
84*7f2fe78bSCy Schubert argc--;
85*7f2fe78bSCy Schubert argv++;
86*7f2fe78bSCy Schubert a = calloc(nf + 1, sizeof(*a));
87*7f2fe78bSCy Schubert if (a == NULL)
88*7f2fe78bSCy Schubert exit(1);
89*7f2fe78bSCy Schubert
90*7f2fe78bSCy Schubert for (i = 0; argv[i] != NULL && i < nf; i++)
91*7f2fe78bSCy Schubert a[i] = argv[i];
92*7f2fe78bSCy Schubert if (i != nf)
93*7f2fe78bSCy Schubert usage();
94*7f2fe78bSCy Schubert argv += nf;
95*7f2fe78bSCy Schubert a[nf] = NULL;
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert if (rectype == NULL && writeheader(h, a) < 0)
98*7f2fe78bSCy Schubert exit(1);
99*7f2fe78bSCy Schubert free(a);
100*7f2fe78bSCy Schubert
101*7f2fe78bSCy Schubert while (*argv != NULL) {
102*7f2fe78bSCy Schubert if (startrec(h) < 0)
103*7f2fe78bSCy Schubert exit(1);
104*7f2fe78bSCy Schubert for (i = 0; argv[i] != NULL && i < nf; i++) {
105*7f2fe78bSCy Schubert if (writefield(h, "%s", argv[i]) < 0)
106*7f2fe78bSCy Schubert exit(1);
107*7f2fe78bSCy Schubert }
108*7f2fe78bSCy Schubert if (i != nf)
109*7f2fe78bSCy Schubert usage();
110*7f2fe78bSCy Schubert argv += nf;
111*7f2fe78bSCy Schubert if (endrec(h) < 0)
112*7f2fe78bSCy Schubert exit(1);
113*7f2fe78bSCy Schubert }
114*7f2fe78bSCy Schubert exit(0);
115*7f2fe78bSCy Schubert }
116