--- gstat.c.orig	Sun Sep 23 15:39:44 2007
+++ gstat.c	Sun Sep 23 18:12:14 2007
@@ -51,7 +51,7 @@
 #include <sysexits.h>
 #include <unistd.h>
 
-static int flag_a, flag_c, flag_d;
+static int flag_a, flag_c, flag_d, flag_b;
 static int flag_I = 500000;
 
 static void usage(void);
@@ -66,7 +66,7 @@
 int
 main(int argc, char **argv)
 {
-	int error, i, quit;
+	int error, i, quit, x;
 	int curx, cury, maxx, maxy, line_len, max_flen;
 	struct devstat *gsp, *gsq;
 	void *sp, *sq;
@@ -88,11 +88,14 @@
 	HistEvent hist_ev;
 
 	f_s[0] = '\0';
-	while ((i = getopt(argc, argv, "adcf:I:")) != -1) {
+	while ((i = getopt(argc, argv, "adcf:I:b")) != -1) {
 		switch (i) {
 		case 'a':
 			flag_a = 1;
 			break;
+		case 'b':
+			flag_b = 1;
+			break;
 		case 'c':
 			flag_c = 1;
 			break;
@@ -141,35 +144,38 @@
 	sq = geom_stats_snapshot_get();
 	if (sq == NULL)
 		err(1, "geom_stats_snapshot()");
-	/* Setup curses */
-	initscr();
-	start_color();
-	use_default_colors();
-	pair_content(0, &cf, &cb);
-	init_pair(1, COLOR_GREEN, cb);
-	init_pair(2, COLOR_MAGENTA, cb);
-	init_pair(3, COLOR_RED, cb);
-	cbreak();
-	noecho();
-	nonl();
-	nodelay(stdscr, 1);
-	intrflush(stdscr, FALSE);
-	keypad(stdscr, TRUE);
-	/* Setup libedit */
-	hist = history_init();
-	if (hist == NULL)
-		err(EX_SOFTWARE, "history_init()");
-	history(hist, &hist_ev, H_SETSIZE, 100);
-	el = el_init("gstat", stdin, stdout, stderr);
-	if (el == NULL)
-		err(EX_SOFTWARE, "el_init");
-	el_set(el, EL_EDITOR, "emacs");
-	el_set(el, EL_SIGNAL, 1);
-	el_set(el, EL_HIST, history, hist);
-	el_set(el, EL_PROMPT, el_prompt);
-	if (f_s[0] != '\0')
-		history(hist, &hist_ev, H_ENTER, f_s);
+	if (!flag_b) {
+		/* Setup curses */
+		initscr();
+		start_color();
+		use_default_colors();
+		pair_content(0, &cf, &cb);
+		init_pair(1, COLOR_GREEN, cb);
+		init_pair(2, COLOR_MAGENTA, cb);
+		init_pair(3, COLOR_RED, cb);
+		cbreak();
+		noecho();
+		nonl();
+		nodelay(stdscr, 1);
+		intrflush(stdscr, FALSE);
+		keypad(stdscr, TRUE);
+		/* Setup libedit */
+		hist = history_init();
+		if (hist == NULL)
+			err(EX_SOFTWARE, "history_init()");
+		history(hist, &hist_ev, H_SETSIZE, 100);
+		el = el_init("gstat", stdin, stdout, stderr);
+		if (el == NULL)
+			err(EX_SOFTWARE, "el_init");
+		el_set(el, EL_EDITOR, "emacs");
+		el_set(el, EL_SIGNAL, 1);
+		el_set(el, EL_HIST, history, hist);
+		el_set(el, EL_PROMPT, el_prompt);
+		if (f_s[0] != '\0')
+			history(hist, &hist_ev, H_ENTER, f_s);
+	}
 	geom_stats_snapshot_timestamp(sq, &tq);
+	x = 0;
 	for (quit = 0; !quit;) {
 		sp = geom_stats_snapshot_get();
 		if (sp == NULL)
@@ -182,12 +188,22 @@
 		geom_stats_snapshot_reset(sp);
 		geom_stats_snapshot_reset(sq);
 		move(0,0);
-		printw("dT: %5.3fs  w: %.3fs",
-		    dt, (float)flag_I / 1000000);
+		if (flag_b) {
+			if (x != 0)
+				printf("dT: %5.3fs  w: %.3fs",
+				    dt, (float)flag_I / 1000000);
+		} else {
+			printw("dT: %5.3fs  w: %.3fs",
+			    dt, (float)flag_I / 1000000);
+		}
 		if (f_s[0] != '\0') {
-			printw("  filter: ");
-			getyx(stdscr, cury, curx);
-			getmaxyx(stdscr, maxy, maxx);
+			if (flag_b) {
+				printf("  filter: ");
+			} else {
+				printw("  filter: ");
+				getyx(stdscr, cury, curx);
+				getmaxyx(stdscr, maxy, maxx);
+			}
 			strncpy(pf_s, f_s, sizeof(pf_s));
 			max_flen = maxx - curx - 1;
 			if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
@@ -199,15 +215,37 @@
 					pf_s[max_flen - 1] = '.';
 				pf_s[max_flen] = '\0';
 			}
-			printw("%s", pf_s);
+			if (flag_b) {
+				printf("%s", pf_s);
+			} else {
+				printw("%s", pf_s);
+			}
+		}
+		if (flag_b) {
+			if (x != 0) {
+				printf("\n");
+				printf(" L(q)  ops/s   ");
+				printf(" r/s   kBps   ms/r   ");
+				printf(" w/s   kBps   ms/w   ");
+			}
+		} else {
+			printw("\n");
+			printw(" L(q)  ops/s   ");
+			printw(" r/s   kBps   ms/r   ");
+			printw(" w/s   kBps   ms/w   ");
+		}
+		if (flag_d) {
+			if (flag_b)
+				printf(" d/s   kBps   ms/d   ");
+			else
+				printw(" d/s   kBps   ms/d   ");
+		}
+		if (flag_b) {
+			if (x != 0)
+				printf("%%busy Name\n");
+		} else {
+			printw("%%busy Name\n");
 		}
-		printw("\n");
-		printw(" L(q)  ops/s   ");
-		printw(" r/s   kBps   ms/r   ");
-		printw(" w/s   kBps   ms/w   ");
-		if (flag_d)
-			printw(" d/s   kBps   ms/d   ");
-		printw("%%busy Name\n");
 		for (;;) {
 			gsp = geom_stats_snapshot_next(sp);
 			gsq = geom_stats_snapshot_next(sq);
@@ -228,9 +266,11 @@
 			if (gid->lg_what == ISCONSUMER && !flag_c)
 				continue;
 			/* Do not print past end of window */
-			getyx(stdscr, cury, curx);
-			if (curx > 0)
-				continue;
+			if (!flag_b) {
+				getyx(stdscr, cury, curx);
+				if (curx > 0)
+					continue;
+			}
 			if ((gid->lg_what == ISPROVIDER
 			    || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
 				pp = gid->lg_ptr;
@@ -239,7 +279,10 @@
 				  continue;
 			}
 			if (gsp->sequence0 != gsp->sequence1) {
-				printw("*\n");
+				if (flag_b)
+					printf("*\n");
+				else
+					printw("*\n");
 				continue;
 			}
 			devstat_compute_statistics(gsp, gsq, dt, 
@@ -265,19 +308,40 @@
 				continue;
 			}
 
-			printw(" %4ju", (uintmax_t)u64);
-			printw(" %6.0f", (double)ld[0]);
-			printw(" %6.0f", (double)ld[1]);
-			printw(" %6.0f", (double)ld[2] * 1024);
-			printw(" %6.1f", (double)ld[3]);
-			printw(" %6.0f", (double)ld[4]);
-			printw(" %6.0f", (double)ld[5] * 1024);
-			printw(" %6.1f", (double)ld[6]);
+			if (flag_b) {
+				if (x != 0) {
+					printf(" %4ju", (uintmax_t)u64);
+					printf(" %6.0f", (double)ld[0]);
+					printf(" %6.0f", (double)ld[1]);
+					printf(" %6.0f", (double)ld[2] * 1024);
+					printf(" %6.1f", (double)ld[3]);
+					printf(" %6.0f", (double)ld[4]);
+					printf(" %6.0f", (double)ld[5] * 1024);
+					printf(" %6.1f", (double)ld[6]);
+				}
+			} else {
+				printw(" %4ju", (uintmax_t)u64);
+				printw(" %6.0f", (double)ld[0]);
+				printw(" %6.0f", (double)ld[1]);
+				printw(" %6.0f", (double)ld[2] * 1024);
+				printw(" %6.1f", (double)ld[3]);
+				printw(" %6.0f", (double)ld[4]);
+				printw(" %6.0f", (double)ld[5] * 1024);
+				printw(" %6.1f", (double)ld[6]);
+			}
 
 			if (flag_d) {
-				printw(" %6.0f", (double)ld[8]);
-				printw(" %6.0f", (double)ld[9] * 1024);
-				printw(" %6.1f", (double)ld[10]);
+				if (flag_b) {
+					if (x != 0) {
+						printf(" %6.0f", (double)ld[8]);
+						printf(" %6.0f", (double)ld[9] * 1024);
+						printf(" %6.1f", (double)ld[10]);
+					}
+				} else {
+					printw(" %6.0f", (double)ld[8]);
+					printw(" %6.0f", (double)ld[9] * 1024);
+					printw(" %6.1f", (double)ld[10]);
+				}
 			}
 
 			if (ld[7] > 80)
@@ -286,34 +350,77 @@
 				i = 2;
 			else 
 				i = 1;
-			attron(COLOR_PAIR(i));
-			printw(" %6.1lf", (double)ld[7]);
-			attroff(COLOR_PAIR(i));
-			printw("|");
+			if (!flag_b) {
+				attron(COLOR_PAIR(i));
+				printw(" %6.1lf", (double)ld[7]);
+				attroff(COLOR_PAIR(i));
+				printw("|");
+			} else {
+				if (x != 0) {
+					printf(" %6.1lf", (double)ld[7]);
+					printf("|");
+				}
+			}
 			if (gid == NULL) {
-				printw(" ??");
+				if (flag_b) {
+					if (x != 0)
+						printf(" ??");
+				} else {
+					printw(" ??");
+				}
 			} else if (gid->lg_what == ISPROVIDER) {
 				pp = gid->lg_ptr;
-				printw(" %s", pp->lg_name);
+				if (flag_b) {
+					if (x != 0) {
+						printf(" %s", pp->lg_name);
+					}
+				} else {
+					printw(" %s", pp->lg_name);
+				}
 			} else if (gid->lg_what == ISCONSUMER) {
 				cp = gid->lg_ptr;
-				printw(" %s/%s/%s",
-				    cp->lg_geom->lg_class->lg_name,
-				    cp->lg_geom->lg_name,
-				    cp->lg_provider->lg_name);
+				if (flag_b) {
+					if (x != 0) {
+						printf(" %s/%s/%s",
+						    cp->lg_geom->lg_class->lg_name,
+						    cp->lg_geom->lg_name,
+						    cp->lg_provider->lg_name);
+					}
+				} else {
+					printw(" %s/%s/%s",
+					    cp->lg_geom->lg_class->lg_name,
+					    cp->lg_geom->lg_name,
+					    cp->lg_provider->lg_name);
+				}
+			}
+			if (flag_b) {
+				if (x != 0)
+					printf("\n");
+			} else {
+				clrtoeol();
+				printw("\n");
 			}
-			clrtoeol();
-			printw("\n");
 			*gsq = *gsp;
 		}
 		geom_stats_snapshot_free(sp);
-		getyx(stdscr, cury, curx);
-		getmaxyx(stdscr, maxy, maxx);
-		clrtobot();
-		if (maxy - 1 <= cury)
-			move(maxy - 1, 0);
-		refresh();
-		usleep(flag_I);
+		if (flag_b) {
+			if (x == 0) {
+				usleep(flag_I);
+				x++;
+				continue;
+			} else {
+				quit = 1;
+				break;
+			}
+		} else {
+			getyx(stdscr, cury, curx);
+			getmaxyx(stdscr, maxy, maxx);
+			clrtobot();
+			if (maxy - 1 <= cury)
+				move(maxy - 1, 0);
+			refresh();
+			usleep(flag_I);
+		}
 		while((i = getch()) != ERR) {
 			switch (i) {
 			case '>':
@@ -369,15 +476,17 @@
 		}
 	}
 
-	endwin();
-	el_end(el);
+	if (!flag_b) {
+		endwin();
+		el_end(el);
+	}
 	exit(EX_OK);
 }
 
 static void
 usage(void)
 {
-	fprintf(stderr, "usage: gstat [-acd] [-f filter] [-I interval]\n");
+	fprintf(stderr, "usage: gstat [-abcd] [-f filter] [-I interval]\n");
 	exit(EX_USAGE);
         /* NOTREACHED */
 }
--- gstat.8.orig	Sun Sep 23 17:31:57 2007
+++ gstat.8	Sun Sep 23 17:33:04 2007
@@ -46,6 +46,8 @@
 .Bl -tag -width indent
 .It Fl a
 Only display providers that are at least 0.1% busy.
+.It Fl b
+Batch mode. Collect numbers, print and exit.
 .It Fl c
 Enable display of
 .Xr geom 4
