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