Pārlūkot izejas kodu

Delay as an option

David Marec 8 mēneši atpakaļ
vecāks
revīzija
83431d1245
1 mainītis faili ar 25 papildinājumiem un 2 dzēšanām
  1. 25 2
      dummy/pepper2.c

+ 25 - 2
dummy/pepper2.c

@@ -1,3 +1,4 @@
+#include <getopt.h>
 #include <curses.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -19,8 +20,12 @@ enum dirs {
 	D /* down */	
 };
 
+static size_t delay = 500;
 
+/* result window */
 static WINDOW * winR;
+
+/* sprite */
 static void worm(WINDOW *win, int x, int y, char c0, char repl)
 {
 	static int ax, ay;
@@ -35,9 +40,10 @@ static void worm(WINDOW *win, int x, int y, char c0, char repl)
 	ax = x;
 	ay = y;
 	ac = repl;
-	usleep(150 * 1000);
+	usleep(delay * 1000);
 }
 
+/* path lookup */
 static __attribute((malloc)) __attribute__((nonnull)) char *get_it(truc_array const *src)
 {
 	char *r = mallocx(src->len, MALLOCX_ZERO);
@@ -159,6 +165,7 @@ end:
 	return r;
 }
 
+/* puzzle display */
 static void print_it(truc_array src)
 {
 	WINDOW * win = newwin(14, 18, 1, 1);
@@ -174,7 +181,23 @@ int main(int argc, char *argv[])
 {
 	truc_array a= {60, };
 	char *r = NULL;
-
+	static struct option longopts[] = {
+		{ "delay",   required_argument,      NULL, 'd' },
+		{ NULL,	0, NULL, 0 }
+	};
+	int ch;
+	while ((ch = getopt_long(argc, argv, "d:", longopts, NULL)) != -1) {
+		switch (ch) {
+			case 'd':
+				delay = strtoul(optarg, NULL, 10);
+				break;
+			case 0:
+				/* long option */
+				break;
+			default:
+				break;
+		}
+	}
 
 	/*            012345789ABCDEF */
 	a.items[0] = "*01**         $";