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
This commit is contained in:
Brian Barto 2016-05-01 17:09:38 -04:00
parent 2eaafe1ef3
commit ed5e9f689f
4 changed files with 36 additions and 10 deletions

View File

@ -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:

View File

@ -1,13 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#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);

View File

@ -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();

View File

@ -5,7 +5,7 @@
#include <stdbool.h>
// 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