notmuch setup: Exit if EOF is encountered at any prompt.
authorCarl Worth <cworth@cworth.org>
Tue, 1 Dec 2009 16:06:09 +0000 (08:06 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 1 Dec 2009 16:06:09 +0000 (08:06 -0800)
If the user is explicitly providing EOF, then terminating the program
is the most likely desired thing to do. This also avoids undefined
behavior from continuing with an uninitialized response after ignoring
the return value of getline().

notmuch-setup.c

index 5ec176d372a74e9b65c31846dc33fbe7ed95d22c..622bbaa68343c7633ade295d794e86e66c64d137 100644 (file)
@@ -100,12 +100,15 @@ notmuch_setup_command (unused (void *ctx),
     unsigned int i;
     int is_new;
 
-#define prompt(format, ...)                            \
-    do {                                               \
-       printf (format, ##__VA_ARGS__);                 \
-       fflush (stdout);                                \
-       getline (&response, &response_size, stdin);     \
-       chomp_newline (response);                       \
+#define prompt(format, ...)                                    \
+    do {                                                       \
+       printf (format, ##__VA_ARGS__);                         \
+       fflush (stdout);                                        \
+       if (getline (&response, &response_size, stdin) < 0) {   \
+           printf ("Exiting.\n");                              \
+           exit (1);                                           \
+       }                                                       \
+       chomp_newline (response);                               \
     } while (0)
 
     config = notmuch_config_open (ctx, NULL, &is_new);