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