aboutsummaryrefslogtreecommitdiff
path: root/sd.c
diff options
context:
space:
mode:
Diffstat (limited to 'sd.c')
-rw-r--r--sd.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/sd.c b/sd.c
index cd8195d..0e39156 100644
--- a/sd.c
+++ b/sd.c
@@ -10,19 +10,21 @@
int main(int argc, char* argv[]) {
char *file = "cards";
bool verbose = false;
+ bool noninteractive = false;
/* Proccess args */
static struct option long_options[] = {
- {"file", required_argument, NULL, 'f'},
- {"verbose", no_argument, NULL, 'v'}
+ {"file", required_argument, NULL, 'f'},
+ {"verbose", no_argument, NULL, 'v'},
+ {"noninteractive", no_argument, NULL, 'n'}
};
while (true) {
- int option_index = 0;
- int c = getopt_long(argc, argv, "f:v", long_options, &option_index);
+ int c = getopt_long(argc, argv, "f:vn", long_options, NULL);
if (c == -1) break;
switch (c) {
case 'f': file = optarg; break;
case 'v': verbose = true; break;
+ case 'n': noninteractive = true; break;
default: abort();
}
}
@@ -54,9 +56,11 @@ int main(int argc, char* argv[]) {
printf("\n");
}
- /* Disable input buffering */
- assert(system("stty -F /dev/tty cbreak min 1") == 0);
- assert(system("stty -F /dev/tty -echo") == 0);
+ if (!noninteractive) {
+ /* Disable input buffering */
+ assert(system("stty -F /dev/tty cbreak min 1") == 0);
+ assert(system("stty -F /dev/tty -echo") == 0);
+ }
while (true) {
/* Make sure sum of weights is positive */
@@ -76,14 +80,28 @@ int main(int argc, char* argv[]) {
sqlite3_bind_int(stmt, 1, i);
sqlite3_step(stmt);
printf("> %s\n", sqlite3_column_text(stmt, 0));
+ if (noninteractive) {
+ fflush(stdout);
+ }
/* Wait for confirmation */
- char b = getchar();
+ getchar();
+ if (noninteractive) {
+ /* Skip newline */
+ getchar();
+ }
printf("%s\n", sqlite3_column_text(stmt, 1));
+ if (noninteractive) {
+ fflush(stdout);
+ }
sqlite3_finalize(stmt);
/* Read user input */
- b = getchar();
+ char b = getchar();
+ if (noninteractive) {
+ /* Skip newline */
+ getchar();
+ }
if (b == 'y') w >>= 1;
else if (b == 'n') w <<= 3;
else break;
@@ -99,5 +117,4 @@ int main(int argc, char* argv[]) {
/* Cleanup */
sqlite3_close(db);
- assert(system("stty -F /dev/tty echo") == 0);
}