Commits

Evan Gates  committed 432a665

rearange, cleanup

  • Participants
  • Parent commits afa6e1d

Comments (0)

Files changed (1)

 #define cursor_org()     printf("\033[1;1H")
 #define cursor_crd(i, j) printf("\033[%d;%dH", i, j)
 
-char *cur, *nxt, *tmp;
-char *field_a, *field_b;
-int i_org, j_org, cols, rows, gen, fd;
-struct termios set, dfl;
-char *outfile = NULL, *infile = "/dev/urandom";
-int fps = 15, size = 32, paused = 0;
-int show_coord = 0, show_fps = 0, show_gen = 0;
 
-void sigwinch(int unused)
-{
-	struct winsize ws;
+static void print_coord(int i, int j);
+static void print_fps();
+static void print_gen();
+static void print(void);
+static void quit(int sig);
+static void run(void);
+static void sigwinch(int unused);
+static void stop(void);
 
-	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
-		err(1, "failed to install SIGWINCH handler");
 
-	if (ioctl(1, TIOCGWINSZ, &ws) < 0)
-		err(1, "failed ioctl");
-
-	cols = ws.ws_col / 2;
-	rows = ws.ws_row;
-
-	i_org = (i_org + rows > size) ? MAX(0, size - rows) : i_org;
-	j_org = (j_org + cols > size) ? MAX(0, size - cols) : j_org;
-}
+static char *cur, *nxt, *tmp;
+static char *field_a, *field_b;
+static int i_org, j_org, cols, rows, gen, fd;
+static struct termios set, dfl;
+static char *outfile = NULL, *infile = "/dev/urandom";
+static int fps = 15, size = 32, paused = 0;
+static int show_coord = 0, show_fps = 0, show_gen = 0;
 
 void print_coord(int i, int j)
 {
 	fflush(stdout);
 }
 
-void print_gen()
-{
-	if (!show_gen)
-		return;
-	cursor_crd(MIN(rows, size), 1);
-	printf("Generation: %d", gen);
-	fflush(stdout);
-}
-
 void print_fps()
 {
 	if (!show_fps)
 	fflush(stdout);
 }
 
-void quit(int sig)
+void print_gen()
 {
-	tcsetattr(0, TCSANOW, &dfl);
-	cursor_org();
-	cursor_clr();
+	if (!show_gen)
+		return;
+	cursor_crd(MIN(rows, size), 1);
+	printf("Generation: %d", gen);
 	fflush(stdout);
-	if (outfile) {
-		if ((fd = open(outfile, O_WRONLY | O_CREAT, 0644)) < 0)
-			err(1, "failed to open outfile %s", outfile);
-		write(fd, cur, size * BITNSLOTS(size));
-		close(fd);
-	}
-
-	exit(sig);
 }
 
 void print(void)
 	print_fps();
 }
 
-void stop(void)
+void quit(int sig)
 {
-	char c;
-	int print_flag = 0;
-	static int i = -1, j = -1;
-
-	if (i < i_org || i > i_org + rows || j < j_org || j > j_org + cols) {
-		i = size > rows ? i_org + rows / 2 : size / 2;
-		j = size > cols ? j_org + cols / 2 : size / 2;
+	tcsetattr(0, TCSANOW, &dfl);
+	cursor_org();
+	cursor_clr();
+	fflush(stdout);
+	if (outfile) {
+		if ((fd = open(outfile, O_WRONLY | O_CREAT, 0644)) < 0)
+			err(1, "failed to open outfile %s", outfile);
+		write(fd, cur, size * BITNSLOTS(size));
+		close(fd);
 	}
 
-	set.c_cc[VMIN] = 1;
-	tcsetattr(0, TCSANOW, &set);
-
-	print();
-	print_coord(i, j);
-
-	cursor_crd(i - i_org + 1, (j - j_org + 1) * 2 - 1);
-	fflush(stdout);
-
-	while (read(0, &c, sizeof(c)) > 0) {
-		switch (c) {
-			case ' ' :
-				BITXOR(cur + i * BITNSLOTS(size), j);
-				print_flag = 1;
-				break;
-			case 'c' :
-				show_coord = !show_coord;
-				print_flag = 1;
-				break;
-			case 'r' :
-				show_fps = !show_fps;
-				print_flag = 1;
-				break;
-			case 'g' :
-				show_gen = !show_gen;
-				print_flag = 1;
-				break;
-			case 'J' :
-				BITXOR(cur + i * BITNSLOTS(size), j);
-				print_flag = 1; // fall through
-			case 'j' :
-				i = MIN(i + 1, size - 1);
-				if (i >= i_org + rows) {
-					i_org = i - rows + 1;
-					print_flag = 1;
-				}
-				break;
-			case 'K' :
-				BITXOR(cur + i * BITNSLOTS(size), j);
-				print_flag = 1; // fall through
-			case 'k' :
-				i = MAX(i - 1, 0);
-				if (i < i_org) {
-					i_org = i;
-					print_flag = 1;
-				}
-				break;
-			case 'H' :
-				BITXOR(cur + i * BITNSLOTS(size), j);
-				print_flag = 1; // fall through
-			case 'h' :
-				j = MAX(j - 1, 0);
-				if (j < j_org) {
-					j_org = j;
-					print_flag = 1;
-				}
-				break;
-			case 'L' :
-				BITXOR(cur + i * BITNSLOTS(size), j);
-				print_flag = 1; // fall through
-			case 'l' :
-				j = MIN(j + 1, size - 1);
-				if (j >= j_org + cols) {
-					j_org = j - cols + 1;
-					print_flag = 1;
-				}
-				break;
-			case 'p' :
-				paused = 0; // fall through
-			case 'n' :
-				set.c_cc[VMIN] = 0;
-				tcsetattr(0, TCSANOW, &set);
-				return;
-			case 'q' :
-				quit(0);
-			case '+' :
-				fps++;
-				print_flag = 1;
-				break;
-			case '-' :
-				fps--;
-				print_flag = 1;
-				break;
-		}
-		if (print_flag) {
-			print_flag = 0;
-			print();
-		}
-		print_coord(i, j);
-		cursor_crd(i - i_org + 1, (j - j_org + 1) * 2 - 1);
-		fflush(stdout);
-	}
+	exit(sig);
 }
 
+void sigwinch(int unused)
+{
+	struct winsize ws;
+
+	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
+		err(1, "failed to install SIGWINCH handler");
+
+	if (ioctl(1, TIOCGWINSZ, &ws) < 0)
+		err(1, "failed ioctl");
+
+	cols = ws.ws_col / 2;
+	rows = ws.ws_row;
+
+	i_org = (i_org + rows > size) ? MAX(0, size - rows) : i_org;
+	j_org = (j_org + cols > size) ? MAX(0, size - cols) : j_org;
+}
 
 void run(void)
 {
 	}
 }
 
+void stop(void)
+{
+	char c;
+	int print_flag = 0;
+	static int i = -1, j = -1;
+
+	if (i < i_org || i > i_org + rows || j < j_org || j > j_org + cols) {
+		i = size > rows ? i_org + rows / 2 : size / 2;
+		j = size > cols ? j_org + cols / 2 : size / 2;
+	}
+
+	set.c_cc[VMIN] = 1;
+	tcsetattr(0, TCSANOW, &set);
+
+	print();
+	print_coord(i, j);
+
+	cursor_crd(i - i_org + 1, (j - j_org + 1) * 2 - 1);
+	fflush(stdout);
+
+	while (read(0, &c, sizeof(c)) > 0) {
+		switch (c) {
+			case '+' : fps++;                                print_flag = 1; break;
+			case '-' : fps--;                                print_flag = 1; break;
+			case ' ' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; break;
+			case 'c' : show_coord = !show_coord;             print_flag = 1; break;
+			case 'r' : show_fps   = !show_fps;               print_flag = 1; break;
+			case 'g' : show_gen   = !show_gen;               print_flag = 1; break;
+			case 'J' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; // fall through
+			case 'j' : i = MIN(i + 1, size - 1);
+					   if (i >= i_org + rows) {
+						   i_org = i - rows + 1;
+						   print_flag = 1;
+					   }
+					   break;
+			case 'K' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; // fall through
+			case 'k' : i = MAX(i - 1, 0);
+					   if (i < i_org) {
+						   i_org = i;
+						   print_flag = 1;
+					   }
+					   break;
+			case 'H' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; // fall through
+			case 'h' : j = MAX(j - 1, 0);
+					   if (j < j_org) {
+						   j_org = j;
+						   print_flag = 1;
+					   }
+					   break;
+			case 'L' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; // fall through
+			case 'l' : j = MIN(j + 1, size - 1);
+					   if (j >= j_org + cols) {
+						   j_org = j - cols + 1;
+						   print_flag = 1;
+					   }
+					   break;
+			case 'p' : paused = 0; // fall through
+			case 'n' : set.c_cc[VMIN] = 0;
+					   tcsetattr(0, TCSANOW, &set);
+					   return;
+			case 'q' : quit(0);
+		}
+		if (print_flag) {
+			print_flag = 0;
+			print();
+		}
+		print_coord(i, j);
+		cursor_crd(i - i_org + 1, (j - j_org + 1) * 2 - 1);
+		fflush(stdout);
+	}
+}
+
+
 int main(int argc, char **argv)
 {
 	int i;
 
 	return 0;
 }
+
+/* vim: ts=4 sw=4 noexpandtab */