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