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