xref: /freebsd/usr.bin/ministat/ministat.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * SPDX-License-Identifier: Beerware
3  *
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  *
11  */
12 
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
15 
16 #include <sys/capsicum.h>
17 #include <sys/ioctl.h>
18 #include <sys/queue.h>
19 #include <sys/ttycom.h>
20 
21 #include <assert.h>
22 #include <capsicum_helpers.h>
23 #include <ctype.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #define NSTUDENT 100
33 #define NCONF 6
34 static double const studentpct[] = { 80, 90, 95, 98, 99, 99.5 };
35 static double const student[NSTUDENT + 1][NCONF] = {
36 /* inf */	{	1.282,	1.645,	1.960,	2.326,	2.576,	3.090  },
37 /* 1. */	{	3.078,	6.314,	12.706,	31.821,	63.657,	318.313  },
38 /* 2. */	{	1.886,	2.920,	4.303,	6.965,	9.925,	22.327  },
39 /* 3. */	{	1.638,	2.353,	3.182,	4.541,	5.841,	10.215  },
40 /* 4. */	{	1.533,	2.132,	2.776,	3.747,	4.604,	7.173  },
41 /* 5. */	{	1.476,	2.015,	2.571,	3.365,	4.032,	5.893  },
42 /* 6. */	{	1.440,	1.943,	2.447,	3.143,	3.707,	5.208  },
43 /* 7. */	{	1.415,	1.895,	2.365,	2.998,	3.499,	4.782  },
44 /* 8. */	{	1.397,	1.860,	2.306,	2.896,	3.355,	4.499  },
45 /* 9. */	{	1.383,	1.833,	2.262,	2.821,	3.250,	4.296  },
46 /* 10. */	{	1.372,	1.812,	2.228,	2.764,	3.169,	4.143  },
47 /* 11. */	{	1.363,	1.796,	2.201,	2.718,	3.106,	4.024  },
48 /* 12. */	{	1.356,	1.782,	2.179,	2.681,	3.055,	3.929  },
49 /* 13. */	{	1.350,	1.771,	2.160,	2.650,	3.012,	3.852  },
50 /* 14. */	{	1.345,	1.761,	2.145,	2.624,	2.977,	3.787  },
51 /* 15. */	{	1.341,	1.753,	2.131,	2.602,	2.947,	3.733  },
52 /* 16. */	{	1.337,	1.746,	2.120,	2.583,	2.921,	3.686  },
53 /* 17. */	{	1.333,	1.740,	2.110,	2.567,	2.898,	3.646  },
54 /* 18. */	{	1.330,	1.734,	2.101,	2.552,	2.878,	3.610  },
55 /* 19. */	{	1.328,	1.729,	2.093,	2.539,	2.861,	3.579  },
56 /* 20. */	{	1.325,	1.725,	2.086,	2.528,	2.845,	3.552  },
57 /* 21. */	{	1.323,	1.721,	2.080,	2.518,	2.831,	3.527  },
58 /* 22. */	{	1.321,	1.717,	2.074,	2.508,	2.819,	3.505  },
59 /* 23. */	{	1.319,	1.714,	2.069,	2.500,	2.807,	3.485  },
60 /* 24. */	{	1.318,	1.711,	2.064,	2.492,	2.797,	3.467  },
61 /* 25. */	{	1.316,	1.708,	2.060,	2.485,	2.787,	3.450  },
62 /* 26. */	{	1.315,	1.706,	2.056,	2.479,	2.779,	3.435  },
63 /* 27. */	{	1.314,	1.703,	2.052,	2.473,	2.771,	3.421  },
64 /* 28. */	{	1.313,	1.701,	2.048,	2.467,	2.763,	3.408  },
65 /* 29. */	{	1.311,	1.699,	2.045,	2.462,	2.756,	3.396  },
66 /* 30. */	{	1.310,	1.697,	2.042,	2.457,	2.750,	3.385  },
67 /* 31. */	{	1.309,	1.696,	2.040,	2.453,	2.744,	3.375  },
68 /* 32. */	{	1.309,	1.694,	2.037,	2.449,	2.738,	3.365  },
69 /* 33. */	{	1.308,	1.692,	2.035,	2.445,	2.733,	3.356  },
70 /* 34. */	{	1.307,	1.691,	2.032,	2.441,	2.728,	3.348  },
71 /* 35. */	{	1.306,	1.690,	2.030,	2.438,	2.724,	3.340  },
72 /* 36. */	{	1.306,	1.688,	2.028,	2.434,	2.719,	3.333  },
73 /* 37. */	{	1.305,	1.687,	2.026,	2.431,	2.715,	3.326  },
74 /* 38. */	{	1.304,	1.686,	2.024,	2.429,	2.712,	3.319  },
75 /* 39. */	{	1.304,	1.685,	2.023,	2.426,	2.708,	3.313  },
76 /* 40. */	{	1.303,	1.684,	2.021,	2.423,	2.704,	3.307  },
77 /* 41. */	{	1.303,	1.683,	2.020,	2.421,	2.701,	3.301  },
78 /* 42. */	{	1.302,	1.682,	2.018,	2.418,	2.698,	3.296  },
79 /* 43. */	{	1.302,	1.681,	2.017,	2.416,	2.695,	3.291  },
80 /* 44. */	{	1.301,	1.680,	2.015,	2.414,	2.692,	3.286  },
81 /* 45. */	{	1.301,	1.679,	2.014,	2.412,	2.690,	3.281  },
82 /* 46. */	{	1.300,	1.679,	2.013,	2.410,	2.687,	3.277  },
83 /* 47. */	{	1.300,	1.678,	2.012,	2.408,	2.685,	3.273  },
84 /* 48. */	{	1.299,	1.677,	2.011,	2.407,	2.682,	3.269  },
85 /* 49. */	{	1.299,	1.677,	2.010,	2.405,	2.680,	3.265  },
86 /* 50. */	{	1.299,	1.676,	2.009,	2.403,	2.678,	3.261  },
87 /* 51. */	{	1.298,	1.675,	2.008,	2.402,	2.676,	3.258  },
88 /* 52. */	{	1.298,	1.675,	2.007,	2.400,	2.674,	3.255  },
89 /* 53. */	{	1.298,	1.674,	2.006,	2.399,	2.672,	3.251  },
90 /* 54. */	{	1.297,	1.674,	2.005,	2.397,	2.670,	3.248  },
91 /* 55. */	{	1.297,	1.673,	2.004,	2.396,	2.668,	3.245  },
92 /* 56. */	{	1.297,	1.673,	2.003,	2.395,	2.667,	3.242  },
93 /* 57. */	{	1.297,	1.672,	2.002,	2.394,	2.665,	3.239  },
94 /* 58. */	{	1.296,	1.672,	2.002,	2.392,	2.663,	3.237  },
95 /* 59. */	{	1.296,	1.671,	2.001,	2.391,	2.662,	3.234  },
96 /* 60. */	{	1.296,	1.671,	2.000,	2.390,	2.660,	3.232  },
97 /* 61. */	{	1.296,	1.670,	2.000,	2.389,	2.659,	3.229  },
98 /* 62. */	{	1.295,	1.670,	1.999,	2.388,	2.657,	3.227  },
99 /* 63. */	{	1.295,	1.669,	1.998,	2.387,	2.656,	3.225  },
100 /* 64. */	{	1.295,	1.669,	1.998,	2.386,	2.655,	3.223  },
101 /* 65. */	{	1.295,	1.669,	1.997,	2.385,	2.654,	3.220  },
102 /* 66. */	{	1.295,	1.668,	1.997,	2.384,	2.652,	3.218  },
103 /* 67. */	{	1.294,	1.668,	1.996,	2.383,	2.651,	3.216  },
104 /* 68. */	{	1.294,	1.668,	1.995,	2.382,	2.650,	3.214  },
105 /* 69. */	{	1.294,	1.667,	1.995,	2.382,	2.649,	3.213  },
106 /* 70. */	{	1.294,	1.667,	1.994,	2.381,	2.648,	3.211  },
107 /* 71. */	{	1.294,	1.667,	1.994,	2.380,	2.647,	3.209  },
108 /* 72. */	{	1.293,	1.666,	1.993,	2.379,	2.646,	3.207  },
109 /* 73. */	{	1.293,	1.666,	1.993,	2.379,	2.645,	3.206  },
110 /* 74. */	{	1.293,	1.666,	1.993,	2.378,	2.644,	3.204  },
111 /* 75. */	{	1.293,	1.665,	1.992,	2.377,	2.643,	3.202  },
112 /* 76. */	{	1.293,	1.665,	1.992,	2.376,	2.642,	3.201  },
113 /* 77. */	{	1.293,	1.665,	1.991,	2.376,	2.641,	3.199  },
114 /* 78. */	{	1.292,	1.665,	1.991,	2.375,	2.640,	3.198  },
115 /* 79. */	{	1.292,	1.664,	1.990,	2.374,	2.640,	3.197  },
116 /* 80. */	{	1.292,	1.664,	1.990,	2.374,	2.639,	3.195  },
117 /* 81. */	{	1.292,	1.664,	1.990,	2.373,	2.638,	3.194  },
118 /* 82. */	{	1.292,	1.664,	1.989,	2.373,	2.637,	3.193  },
119 /* 83. */	{	1.292,	1.663,	1.989,	2.372,	2.636,	3.191  },
120 /* 84. */	{	1.292,	1.663,	1.989,	2.372,	2.636,	3.190  },
121 /* 85. */	{	1.292,	1.663,	1.988,	2.371,	2.635,	3.189  },
122 /* 86. */	{	1.291,	1.663,	1.988,	2.370,	2.634,	3.188  },
123 /* 87. */	{	1.291,	1.663,	1.988,	2.370,	2.634,	3.187  },
124 /* 88. */	{	1.291,	1.662,	1.987,	2.369,	2.633,	3.185  },
125 /* 89. */	{	1.291,	1.662,	1.987,	2.369,	2.632,	3.184  },
126 /* 90. */	{	1.291,	1.662,	1.987,	2.368,	2.632,	3.183  },
127 /* 91. */	{	1.291,	1.662,	1.986,	2.368,	2.631,	3.182  },
128 /* 92. */	{	1.291,	1.662,	1.986,	2.368,	2.630,	3.181  },
129 /* 93. */	{	1.291,	1.661,	1.986,	2.367,	2.630,	3.180  },
130 /* 94. */	{	1.291,	1.661,	1.986,	2.367,	2.629,	3.179  },
131 /* 95. */	{	1.291,	1.661,	1.985,	2.366,	2.629,	3.178  },
132 /* 96. */	{	1.290,	1.661,	1.985,	2.366,	2.628,	3.177  },
133 /* 97. */	{	1.290,	1.661,	1.985,	2.365,	2.627,	3.176  },
134 /* 98. */	{	1.290,	1.661,	1.984,	2.365,	2.627,	3.175  },
135 /* 99. */	{	1.290,	1.660,	1.984,	2.365,	2.626,	3.175  },
136 /* 100. */	{	1.290,	1.660,	1.984,	2.364,	2.626,	3.174  }
137 };
138 
139 #define	MAX_DS	8
140 static char symbol[MAX_DS] = { ' ', 'x', '+', '*', '%', '#', '@', 'O' };
141 
142 struct dataset {
143 	char *name;
144 	double	*points;
145 	size_t lpoints;
146 	double sy, syy;
147 	size_t n;
148 };
149 
150 static struct dataset *
151 NewSet(void)
152 {
153 	struct dataset *ds;
154 
155 	ds = calloc(1, sizeof *ds);
156 	assert(ds != NULL);
157 	ds->lpoints = 100000;
158 	ds->points = calloc(sizeof *ds->points, ds->lpoints);
159 	assert(ds->points != NULL);
160 	ds->syy = NAN;
161 	return(ds);
162 }
163 
164 static void
165 AddPoint(struct dataset *ds, double a)
166 {
167 	double *dp;
168 
169 	if (ds->n >= ds->lpoints) {
170 		dp = ds->points;
171 		ds->lpoints *= 4;
172 		ds->points = calloc(sizeof *ds->points, ds->lpoints);
173 		assert(ds->points != NULL);
174 		memcpy(ds->points, dp, sizeof *dp * ds->n);
175 		free(dp);
176 	}
177 	ds->points[ds->n++] = a;
178 	ds->sy += a;
179 }
180 
181 static double
182 Min(const struct dataset *ds)
183 {
184 
185 	return (ds->points[0]);
186 }
187 
188 static double
189 Max(const struct dataset *ds)
190 {
191 
192 	return (ds->points[ds->n -1]);
193 }
194 
195 static double
196 Avg(const struct dataset *ds)
197 {
198 
199 	return(ds->sy / ds->n);
200 }
201 
202 static double
203 Median(const struct dataset *ds)
204 {
205 	const size_t m = ds->n / 2;
206 
207 	if ((ds->n % 2) == 0)
208 		return ((ds->points[m] + (ds->points[m - 1])) / 2);
209 	return (ds->points[m]);
210 }
211 
212 static double
213 Var(struct dataset *ds)
214 {
215 	size_t z;
216 	const double a = Avg(ds);
217 
218 	if (isnan(ds->syy)) {
219 		ds->syy = 0.0;
220 		for (z = 0; z < ds->n; z++)
221 			ds->syy += (ds->points[z] - a) * (ds->points[z] - a);
222 	}
223 
224 	return (ds->syy / (ds->n - 1.0));
225 }
226 
227 static double
228 Stddev(struct dataset *ds)
229 {
230 
231 	return sqrt(Var(ds));
232 }
233 
234 static void
235 VitalsHead(void)
236 {
237 
238 	printf("    N           Min           Max        Median           Avg        Stddev\n");
239 }
240 
241 static void
242 Vitals(struct dataset *ds, int flag)
243 {
244 
245 	printf("%c %3zu %13.8g %13.8g %13.8g %13.8g %13.8g", symbol[flag],
246 	    ds->n, Min(ds), Max(ds), Median(ds), Avg(ds), Stddev(ds));
247 	printf("\n");
248 }
249 
250 static void
251 Relative(struct dataset *ds, struct dataset *rs, int confidx)
252 {
253 	double spool, s, d, e, t;
254 	double re;
255 	size_t z;
256 
257 	z = ds->n + rs->n - 2;
258 	if (z > NSTUDENT)
259 		t = student[0][confidx];
260 	else
261 		t = student[z][confidx];
262 	spool = (ds->n - 1) * Var(ds) + (rs->n - 1) * Var(rs);
263 	spool /= ds->n + rs->n - 2;
264 	spool = sqrt(spool);
265 	s = spool * sqrt(1.0 / ds->n + 1.0 / rs->n);
266 	d = Avg(ds) - Avg(rs);
267 	e = t * s;
268 
269 	re = (ds->n - 1) * Var(ds) + (rs->n - 1) * Var(rs) *
270 	    (Avg(ds) * Avg(ds)) / (Avg(rs) * Avg(rs));
271 	re *= (ds->n + rs->n) / (ds->n * rs->n * (ds->n + rs->n - 2.0));
272 	re = t * sqrt(re);
273 
274 	if (fabs(d) > e) {
275 		printf("Difference at %.1f%% confidence\n", studentpct[confidx]);
276 		printf("	%g +/- %g\n", d, e);
277 		printf("	%g%% +/- %g%%\n", d * 100 / Avg(rs), re * 100 / Avg(rs));
278 		printf("	(Student's t, pooled s = %g)\n", spool);
279 	} else {
280 		printf("No difference proven at %.1f%% confidence\n",
281 		    studentpct[confidx]);
282 	}
283 }
284 
285 struct plot {
286 	double		min;
287 	double		max;
288 	double		span;
289 	int		width;
290 
291 	double		x0, dx;
292 	size_t		height;
293 	char		*data;
294 	char		**bar;
295 	int		separate_bars;
296 	int		num_datasets;
297 };
298 
299 static struct plot plot;
300 
301 static void
302 SetupPlot(int width, int separate, int num_datasets)
303 {
304 	struct plot *pl;
305 
306 	pl = &plot;
307 	pl->width = width;
308 	pl->height = 0;
309 	pl->data = NULL;
310 	pl->bar = NULL;
311 	pl->separate_bars = separate;
312 	pl->num_datasets = num_datasets;
313 	pl->min = 999e99;
314 	pl->max = -999e99;
315 }
316 
317 static void
318 AdjPlot(double a)
319 {
320 	struct plot *pl;
321 
322 	pl = &plot;
323 	if (a < pl->min)
324 		pl->min = a;
325 	if (a > pl->max)
326 		pl->max = a;
327 	pl->span = pl->max - pl->min;
328 	pl->dx = pl->span / (pl->width - 1.0);
329 	pl->x0 = pl->min - .5 * pl->dx;
330 }
331 
332 static void
333 DimPlot(struct dataset *ds)
334 {
335 	AdjPlot(Min(ds));
336 	AdjPlot(Max(ds));
337 	AdjPlot(Avg(ds) - Stddev(ds));
338 	AdjPlot(Avg(ds) + Stddev(ds));
339 }
340 
341 static void
342 PlotSet(struct dataset *ds, int val)
343 {
344 	struct plot *pl;
345 	int i, x;
346 	size_t m, j, z;
347 	size_t n;
348 	int bar;
349 	double av, sd;
350 
351 	pl = &plot;
352 	if (pl->span == 0)
353 		return;
354 
355 	if (pl->separate_bars)
356 		bar = val-1;
357 	else
358 		bar = 0;
359 
360 	if (pl->bar == NULL) {
361 		pl->bar = calloc(sizeof(char *), pl->num_datasets);
362 		assert(pl->bar != NULL);
363 	}
364 
365 	if (pl->bar[bar] == NULL) {
366 		pl->bar[bar] = malloc(pl->width);
367 		assert(pl->bar[bar] != NULL);
368 		memset(pl->bar[bar], 0, pl->width);
369 	}
370 
371 	m = 1;
372 	i = -1;
373 	j = 0;
374 	/* Set m to max(j) + 1, to allocate required memory */
375 	for (n = 0; n < ds->n; n++) {
376 		x = (ds->points[n] - pl->x0) / pl->dx;
377 		if (x == i) {
378 			j++;
379 			if (j > m)
380 				m = j;
381 		} else {
382 			j = 1;
383 			i = x;
384 		}
385 	}
386 	m += 1;
387 	if (m > pl->height) {
388 		pl->data = realloc(pl->data, pl->width * m);
389 		assert(pl->data != NULL);
390 		memset(pl->data + pl->height * pl->width, 0,
391 		    (m - pl->height) * pl->width);
392 	}
393 	pl->height = m;
394 	i = -1;
395 	for (n = 0; n < ds->n; n++) {
396 		x = (ds->points[n] - pl->x0) / pl->dx;
397 		if (x == i) {
398 			j++;
399 		} else {
400 			j = 1;
401 			i = x;
402 		}
403 		pl->data[j * pl->width + x] |= val;
404 	}
405 	av = Avg(ds);
406 	sd = Stddev(ds);
407 	if (!isnan(sd)) {
408 		x = ((av - sd) - pl->x0) / pl->dx;
409 		m = ((av + sd) - pl->x0) / pl->dx;
410 		pl->bar[bar][m] = '|';
411 		pl->bar[bar][x] = '|';
412 		for (z = x + 1; z < m; z++)
413 			if (pl->bar[bar][z] == 0)
414 				pl->bar[bar][z] = '_';
415 	}
416 	x = (Median(ds) - pl->x0) / pl->dx;
417 	pl->bar[bar][x] = 'M';
418 	x = (av - pl->x0) / pl->dx;
419 	pl->bar[bar][x] = 'A';
420 }
421 
422 static void
423 DumpPlot(void)
424 {
425 	struct plot *pl;
426 	int i, j, k;
427 	size_t z;
428 
429 	pl = &plot;
430 	if (pl->span == 0) {
431 		printf("[no plot, span is zero width]\n");
432 		return;
433 	}
434 
435 	putchar('+');
436 	for (i = 0; i < pl->width; i++)
437 		putchar('-');
438 	putchar('+');
439 	putchar('\n');
440 	for (z = 1; z < pl->height; z++) {
441 		putchar('|');
442 		for (j = 0; j < pl->width; j++) {
443 			k = pl->data[(pl->height - z) * pl->width + j];
444 			if (k >= 0 && k < MAX_DS)
445 				putchar(symbol[k]);
446 			else
447 				printf("[%02x]", k);
448 		}
449 		putchar('|');
450 		putchar('\n');
451 	}
452 	for (i = 0; i < pl->num_datasets; i++) {
453 		if (pl->bar[i] == NULL)
454 			continue;
455 		putchar('|');
456 		for (j = 0; j < pl->width; j++) {
457 			k = pl->bar[i][j];
458 			if (k == 0)
459 				k = ' ';
460 			putchar(k);
461 		}
462 		putchar('|');
463 		putchar('\n');
464 	}
465 	putchar('+');
466 	for (i = 0; i < pl->width; i++)
467 		putchar('-');
468 	putchar('+');
469 	putchar('\n');
470 }
471 
472 static int
473 dbl_cmp(const void *a, const void *b)
474 {
475 	const double *aa = a;
476 	const double *bb = b;
477 
478 	if (*aa < *bb)
479 		return (-1);
480 	else if (*aa > *bb)
481 		return (1);
482 	else
483 		return (0);
484 }
485 
486 static struct dataset *
487 ReadSet(FILE *f, const char *n, int column, const char *delim)
488 {
489 	char buf[BUFSIZ], *p, *t;
490 	struct dataset *s;
491 	double d;
492 	int line;
493 	int i;
494 
495 	s = NewSet();
496 	s->name = strdup(n);
497 	assert(s->name != NULL);
498 	line = 0;
499 	while (fgets(buf, sizeof buf, f) != NULL) {
500 		line++;
501 
502 		i = strlen(buf);
503 		while (i > 0 && isspace(buf[i - 1]))
504 			buf[--i] = '\0';
505 		for (i = 1, t = strtok(buf, delim);
506 		     t != NULL && *t != '#';
507 		     i++, t = strtok(NULL, delim)) {
508 			if (i == column)
509 				break;
510 		}
511 		if (t == NULL || *t == '#')
512 			continue;
513 
514 		d = strtod(t, &p);
515 		if (p != NULL && *p != '\0')
516 			errx(2, "Invalid data on line %d in %s", line, n);
517 		if (*buf != '\0')
518 			AddPoint(s, d);
519 	}
520 	if (s->n < 3) {
521 		fprintf(stderr,
522 		    "Dataset %s must contain at least 3 data points\n", n);
523 		exit (2);
524 	}
525 	qsort(s->points, s->n, sizeof *s->points, dbl_cmp);
526 	return (s);
527 }
528 
529 static void
530 usage(char const *whine)
531 {
532 	int i;
533 
534 	fprintf(stderr, "%s\n", whine);
535 	fprintf(stderr,
536 	    "Usage: ministat [-C column] [-c confidence] [-d delimiter(s)] [-Anqs] [-w width] [file [file ...]]\n");
537 	fprintf(stderr, "\tconfidence = {");
538 	for (i = 0; i < NCONF; i++) {
539 		fprintf(stderr, "%s%g%%",
540 		    i ? ", " : "",
541 		    studentpct[i]);
542 	}
543 	fprintf(stderr, "}\n");
544 	fprintf(stderr, "\t-A : print statistics only. suppress the graph.\n");
545 	fprintf(stderr, "\t-C : column number to extract (starts and defaults to 1)\n");
546 	fprintf(stderr, "\t-d : delimiter(s) string, default to \" \\t\"\n");
547 	fprintf(stderr, "\t-n : print summary statistics only, no graph/test\n");
548 	fprintf(stderr, "\t-q : suppress printing summary-statistics headers and data-set names\n");
549 	fprintf(stderr, "\t-s : print avg/median/stddev bars on separate lines\n");
550 	fprintf(stderr, "\t-w : width of graph/test output (default 74 or terminal width)\n");
551 	exit (2);
552 }
553 
554 int
555 main(int argc, char **argv)
556 {
557 	const char *setfilenames[MAX_DS - 1];
558 	struct dataset *ds[MAX_DS - 1];
559 	FILE *setfiles[MAX_DS - 1];
560 	int nds;
561 	double a;
562 	const char *delim = " \t";
563 	char *p;
564 	int c, i, ci;
565 	int column = 1;
566 	int flag_s = 0;
567 	int flag_n = 0;
568 	int flag_q = 0;
569 	int termwidth = 74;
570 	int suppress_plot = 0;
571 
572 	if (isatty(STDOUT_FILENO)) {
573 		struct winsize wsz;
574 
575 		if ((p = getenv("COLUMNS")) != NULL && *p != '\0')
576 			termwidth = atoi(p);
577 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsz) != -1 &&
578 			 wsz.ws_col > 0)
579 			termwidth = wsz.ws_col - 2;
580 	}
581 
582 	ci = -1;
583 	while ((c = getopt(argc, argv, "AC:c:d:snqw:")) != -1)
584 		switch (c) {
585 		case 'A':
586 			suppress_plot = 1;
587 			break;
588 		case 'C':
589 			column = strtol(optarg, &p, 10);
590 			if (p != NULL && *p != '\0')
591 				usage("Invalid column number.");
592 			if (column <= 0)
593 				usage("Column number should be positive.");
594 			break;
595 		case 'c':
596 			a = strtod(optarg, &p);
597 			if (p != NULL && *p != '\0')
598 				usage("Not a floating point number");
599 			for (i = 0; i < NCONF; i++)
600 				if (a == studentpct[i])
601 					ci = i;
602 			if (ci == -1)
603 				usage("No support for confidence level");
604 			break;
605 		case 'd':
606 			if (*optarg == '\0')
607 				usage("Can't use empty delimiter string");
608 			delim = optarg;
609 			break;
610 		case 'n':
611 			flag_n = 1;
612 			break;
613 		case 'q':
614 			flag_q = 1;
615 			break;
616 		case 's':
617 			flag_s = 1;
618 			break;
619 		case 'w':
620 			termwidth = strtol(optarg, &p, 10);
621 			if (p != NULL && *p != '\0')
622 				usage("Invalid width, not a number.");
623 			if (termwidth < 0)
624 				usage("Unable to move beyond left margin.");
625 			break;
626 		default:
627 			usage("Unknown option");
628 			break;
629 		}
630 	if (ci == -1)
631 		ci = 2;
632 	argc -= optind;
633 	argv += optind;
634 
635 	if (argc == 0) {
636 		setfilenames[0] = "<stdin>";
637 		setfiles[0] = stdin;
638 		nds = 1;
639 	} else {
640 		if (argc > (MAX_DS - 1))
641 			usage("Too many datasets.");
642 		nds = argc;
643 		for (i = 0; i < nds; i++) {
644 			setfilenames[i] = argv[i];
645 			if (!strcmp(argv[i], "-"))
646 				setfiles[0] = stdin;
647 			else
648 				setfiles[i] = fopen(argv[i], "r");
649 			if (setfiles[i] == NULL)
650 				err(2, "Cannot open %s", argv[i]);
651 		}
652 	}
653 
654 	if (caph_limit_stdio() < 0)
655 		err(2, "capsicum");
656 
657 	for (i = 0; i < nds; i++)
658 		if (caph_limit_stream(fileno(setfiles[i]), CAPH_READ) < 0)
659 			err(2, "unable to limit rights for %s",
660 			    setfilenames[i]);
661 
662 	/* Enter Capsicum sandbox. */
663 	if (caph_enter() < 0)
664 		err(2, "unable to enter capability mode");
665 
666 	for (i = 0; i < nds; i++) {
667 		ds[i] = ReadSet(setfiles[i], setfilenames[i], column, delim);
668 		if (setfiles[i] != stdin)
669 			fclose(setfiles[i]);
670 	}
671 
672 	if (!flag_q) {
673 		for (i = 0; i < nds; i++)
674 			printf("%c %s\n", symbol[i+1], ds[i]->name);
675 	}
676 
677 	if (!flag_n && !suppress_plot) {
678 		SetupPlot(termwidth, flag_s, nds);
679 		for (i = 0; i < nds; i++)
680 			DimPlot(ds[i]);
681 		for (i = 0; i < nds; i++)
682 			PlotSet(ds[i], i + 1);
683 		DumpPlot();
684 	}
685 	if (!flag_q)
686 		VitalsHead();
687 	Vitals(ds[0], 1);
688 	for (i = 1; i < nds; i++) {
689 		Vitals(ds[i], i + 1);
690 		if (!flag_n)
691 			Relative(ds[i], ds[0], ci);
692 	}
693 	exit(0);
694 }
695