From ed5e9f689fa28c6fe88be1f3076e6d9804ab3a01 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Sun, 1 May 2016 17:09:38 -0400 Subject: [PATCH] Support for the '-a' command line option to auto-start the decryption effect modified: README.md modified: src/main.c modified: src/nms.c modified: src/nms.h --- README.md | 11 +++++++++++ src/main.c | 21 +++++++++++++++++++-- src/nms.c | 11 ++++------- src/nms.h | 3 ++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 17dc70f..d1a14f2 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,14 @@ Once the "encrypted" data is displayed, the program will pause until you press a decryption effect will start. After that is completed, it will again pause until you press a key, at which point the program will terminate. +#### Command Line Options + +Use the `-a` option to set the auto_decrypt flag. This will automatically start the decryption effect, +eliminating the need for the user to press a key to start it. +``` +ls -l / | nms -a +``` + Using the Module in Your Program --------------------------------- @@ -120,6 +128,7 @@ typedef struct { int input_cursor_x; int input_cursor_y; bool show_cursor; + bool auto_decrypt; } NmsArgs; ``` * `char *src` @@ -133,6 +142,8 @@ Useful for displaying menus: * If your menu has a specific location that you'd like to place the cursor for user input, use these to set the x and y screen coordinates for the position. * `bool show_cursor` * Set to `true` if you want the cursor to be visible during the text decryption effect. It is set to `false` by default. +* `bool auto_decrypt` + * Set to `true` to automatically start the decryption effect, eliminating the need for the user to press a key to start it. Assign values to the structure members as needed. Then simply pass a pointer to the structure to the nms_exec() function: diff --git a/src/main.c b/src/main.c index 60c23fd..3fae0be 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,30 @@ #include #include +#include +#include #include "nms.h" -int main(void) { +int main(int argc, char *argv[]) { + int c, o, inSize = 0; char *input = NULL; NmsArgs args = INIT_NMSARGS; + // Processing command arguments + while ((o = getopt(argc, argv, "a")) != -1) { + switch (o) { + case 'a': + args.auto_decrypt = true; + break; + case '?': + if (isprint(optopt)) + fprintf (stderr, "Unknown option '-%c'.\n", optopt); + else + fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt); + return 1; + } + } + // Geting input - int c, inSize = 0; while ((c = getchar()) != EOF) { ++inSize; input = realloc(input, inSize + 1); diff --git a/src/nms.c b/src/nms.c index 39b80d3..4de277c 100644 --- a/src/nms.c +++ b/src/nms.c @@ -153,13 +153,10 @@ char nms_exec(NmsArgs *args) { // Flush any input up to this point flushinp(); - // Reopen stdin for interactive input (keyboard), then require user - // to press a key to continue. - if (!isatty(STDIN_FILENO)) - if (!freopen ("/dev/tty", "r", stdin)) - sleep(1); - else - getch(); + // If auto_decrypt flag is set, we sleep. Otherwise, reopen stdin for interactive + // input (keyboard), then require user to press a key to continue. + if (args->auto_decrypt == true || (!isatty(STDIN_FILENO) && !freopen ("/dev/tty", "r", stdin))) + sleep(1); else getch(); diff --git a/src/nms.h b/src/nms.h index c7d63e2..e614aff 100644 --- a/src/nms.h +++ b/src/nms.h @@ -5,7 +5,7 @@ #include // Default arguments for nms_exec() -#define INIT_NMSARGS { NULL, NULL, -1, -1, false } +#define INIT_NMSARGS { NULL, NULL, -1, -1, false, false } // Argument structure for nms_exec() typedef struct { @@ -14,6 +14,7 @@ typedef struct { int input_cursor_x; int input_cursor_y; bool show_cursor; + bool auto_decrypt; } NmsArgs; // Display the characters stored in the display queue