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