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