Rename nmsterm modules to nmstermio, and update function names to align with new module name

modified:   Makefile
	modified:   src/nmseffect.c
	deleted:    src/nmsterm.h
	renamed:    src/nmsterm.c -> src/nmstermio.c
	new file:   src/nmstermio.h
	renamed:    src/nmsterm_ncurses.c -> src/nmstermio_ncurses.c
	modified:   src/sneakers.c
This commit is contained in:
Brian Barto 2017-01-19 11:42:29 -05:00
parent bd5614363f
commit 8a09f36cee
7 changed files with 116 additions and 118 deletions

View File

@ -17,20 +17,20 @@ CFLAGS ?= -Wextra -Wall
.PHONY: all install uninstall clean
nms: $(OBJ)/nmsterm.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
nms: $(OBJ)/nmstermio.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
$(CC) $(CFLAGS) -o $(BIN)/$@ $^
sneakers: $(OBJ)/nmsterm.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN)
sneakers: $(OBJ)/nmstermio.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN)
$(CC) $(CFLAGS) -o $(BIN)/$@ $^
all: nms sneakers
all-ncurses: nms-ncurses sneakers-ncurses
nms-ncurses: $(OBJ)/nmsterm_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
nms-ncurses: $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN)
$(CC) $(CFLAGS) -o $(BIN)/nms $^ -lncurses
sneakers-ncurses: $(OBJ)/nmsterm_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN)
sneakers-ncurses: $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN)
$(CC) $(CFLAGS) -o $(BIN)/sneakers $^ -lncurses
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)

View File

@ -16,7 +16,7 @@
#include <locale.h>
#include <wchar.h>
#include "nmseffect.h"
#include "nmsterm.h"
#include "nmstermio.h"
// Program settings
#define TYPE_EFFECT_SPEED 4 // miliseconds per char
@ -115,20 +115,20 @@ char nmseffect_exec(char *string) {
setlocale(LC_ALL, "");
// Initialize terminal
nmsterm_init_terminal();
nmstermio_init_terminal();
if (!nmsterm_get_clearscr()) {
if (!nmstermio_get_clearscr()) {
// Get current row position
origRow = nmsterm_get_cursor_row();
origRow = nmstermio_get_cursor_row();
// nms_get_cursor_row() may display output in some terminals. So
// we need to reposition the cursor to the start of the row.
nmsterm_move_cursor(origRow, 0);
nmstermio_move_cursor(origRow, 0);
}
// Get terminal window rows/cols
maxRows = nmsterm_get_rows();
maxCols = nmsterm_get_cols();
maxRows = nmstermio_get_rows();
maxCols = nmstermio_get_cols();
// Seed my random number generator with the current time
srand(time(NULL));
@ -162,7 +162,7 @@ char nmseffect_exec(char *string) {
i += (mblen(&string[i], 4) - 1);
} else {
fprintf(stderr, "Unknown character encountered. Quitting.\n");
nmsterm_restore_terminal();
nmstermio_restore_terminal();
return 0;
}
@ -202,55 +202,55 @@ char nmseffect_exec(char *string) {
// Print mask character (or space)
if (list_pointer->is_space) {
nmsterm_print_string(list_pointer->source);
nmstermio_print_string(list_pointer->source);
continue;
}
// print mask character
nmsterm_print_string(list_pointer->mask);
nmstermio_print_string(list_pointer->mask);
if (list_pointer->width == 2) {
nmsterm_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
nmstermio_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
}
// flush output and sleep
nmsterm_refresh();
nmstermio_refresh();
nmseffect_sleep(TYPE_EFFECT_SPEED);
}
// Flush any input up to this point
nmsterm_clear_input();
nmstermio_clear_input();
// If autoDecrypt flag is set, we sleep. Otherwise require user to
// press a key to continue.
if (autoDecrypt)
sleep(1);
else
nmsterm_get_char();
nmstermio_get_char();
// Jumble loop
for (i = 0; i < (JUMBLE_SECONDS * 1000) / JUMBLE_LOOP_SPEED; ++i) {
// Move cursor to start position
nmsterm_move_cursor(origRow, origCol);
nmstermio_move_cursor(origRow, origCol);
// Print new mask for all characters
for (list_pointer = list_head; list_pointer != NULL; list_pointer = list_pointer->next) {
// Print mask character (or space)
if (list_pointer->is_space) {
nmsterm_print_string(list_pointer->source);
nmstermio_print_string(list_pointer->source);
continue;
}
// print new mask character
nmsterm_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
nmstermio_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
if (list_pointer->width == 2) {
nmsterm_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
nmstermio_print_string(maskCharTable[rand() % MASK_CHAR_COUNT]);
}
}
// flush output and sleep
nmsterm_refresh();
nmstermio_refresh();
nmseffect_sleep(JUMBLE_LOOP_SPEED);
}
@ -258,7 +258,7 @@ char nmseffect_exec(char *string) {
while (!revealed) {
// Move cursor to start position
nmsterm_move_cursor(origRow, origCol);
nmstermio_move_cursor(origRow, origCol);
// Set revealed flag
revealed = 1;
@ -267,7 +267,7 @@ char nmseffect_exec(char *string) {
// Print mask character (or space)
if (list_pointer->is_space) {
nmsterm_print_string(list_pointer->source);
nmstermio_print_string(list_pointer->source);
continue;
}
@ -286,7 +286,7 @@ char nmseffect_exec(char *string) {
}
// Print mask
nmsterm_print_string(list_pointer->mask);
nmstermio_print_string(list_pointer->mask);
// Decrement reveal time
list_pointer->time -= REVEAL_LOOP_SPEED;
@ -296,43 +296,43 @@ char nmseffect_exec(char *string) {
} else {
// print source character
nmsterm_print_reveal_string(list_pointer->source, colorOn);
nmstermio_print_reveal_string(list_pointer->source, colorOn);
}
}
// flush output and sleep
nmsterm_refresh();
nmstermio_refresh();
nmseffect_sleep(REVEAL_LOOP_SPEED);
}
// Flush any input up to this point
nmsterm_clear_input();
nmstermio_clear_input();
// Check if user must select from a set of options
if (returnOpts != NULL && strlen(returnOpts) > 0) {
// Position cursor if necessary
if (inputPositionY >= 0 && inputPositionX >= 0) {
nmsterm_move_cursor(inputPositionY, inputPositionX);
nmstermio_move_cursor(inputPositionY, inputPositionX);
}
nmsterm_show_cursor();
nmstermio_show_cursor();
// Get and validate user selection
while (strchr(returnOpts, ret = nmsterm_get_char()) == NULL) {
nmsterm_beep();
while (strchr(returnOpts, ret = nmstermio_get_char()) == NULL) {
nmstermio_beep();
}
}
// User must press a key to continue when clearSrc is set
// without returnOpts
else if (nmsterm_get_clearscr()) {
nmsterm_get_char();
else if (nmstermio_get_clearscr()) {
nmstermio_get_char();
}
// Restore terminal
nmsterm_restore_terminal();
nmstermio_restore_terminal();
// Freeing the list.
list_pointer = list_head;
@ -354,7 +354,7 @@ char nmseffect_exec(char *string) {
* passed an invalid color. No value is returned.
*/
void nmseffect_set_foregroundcolor(char *color) {
nmsterm_set_foregroundcolor(color);
nmstermio_set_foregroundcolor(color);
}
/*
@ -382,7 +382,7 @@ void nmseffect_set_autodecrypt(int setting) {
* true/false value of the 'setting' argument.
*/
void nmseffect_set_clearscr(int s) {
nmsterm_set_clearscr(s);
nmstermio_set_clearscr(s);
}
/*

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2017 Brian Barto
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the MIT License. See LICENSE for more details.
*/
#ifndef NMSTERM_H
#define NMSTERM_H 1
// Function prototypes
void nmsterm_init_terminal(void);
void nmsterm_restore_terminal(void);
int nmsterm_get_rows(void);
int nmsterm_get_cols(void);
int nmsterm_get_cursor_row(void);
void nmsterm_move_cursor(int, int);
void nmsterm_print_string(char *);
void nmsterm_refresh(void);
void nmsterm_clear_input(void);
char nmsterm_get_char(void);
void nmsterm_print_reveal_string(char *, int);
void nmsterm_show_cursor(void);
void nmsterm_beep(void);
int nmsterm_get_clearscr(void);
void nmsterm_set_clearscr(int);
void nmsterm_set_foregroundcolor(char *);
int nmsterm_get_cursor_row(void);
#endif

View File

@ -42,13 +42,13 @@ static int clearScr = 0; // clearScr flag
static int foregroundColor = COLOR_BLUE; // Foreground color setting
// Function prototypes
static void nmsterm_set_terminal(int);
static void nmstermio_set_terminal(int);
// Initialize terminal window
void nmsterm_init_terminal(void) {
void nmstermio_init_terminal(void) {
// Turn off line buffering and echo
nmsterm_set_terminal(0);
nmstermio_set_terminal(0);
// Save terminal state, clear screen, and home/hide the cursor
if (clearScr) {
@ -60,7 +60,7 @@ void nmsterm_init_terminal(void) {
}
}
void nmsterm_restore_terminal(void) {
void nmstermio_restore_terminal(void) {
// Restore screen and cursor is clearSrc is set
if (clearScr) {
@ -70,14 +70,14 @@ void nmsterm_restore_terminal(void) {
}
// Turn on line buffering and echo
nmsterm_set_terminal(1);
nmstermio_set_terminal(1);
}
/*
* nms_term_rows() gets and returns the number of rows in the current
* Gets and returns the number of rows in the current
* terminal window.
*/
int nmsterm_get_rows(void) {
int nmstermio_get_rows(void) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
@ -85,29 +85,29 @@ int nmsterm_get_rows(void) {
}
/*
* nms_term_cols() gets and returns the number of cols in the current
* Gets and returns the number of cols in the current
* terminal window.
*/
int nmsterm_get_cols(void) {
int nmstermio_get_cols(void) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
}
void nmsterm_move_cursor(int y, int x) {
void nmstermio_move_cursor(int y, int x) {
CURSOR_MOVE(y, x);
}
void nmsterm_print_string(char *s) {
void nmstermio_print_string(char *s) {
printf("%s", s);
}
void nmsterm_refresh(void) {
void nmstermio_refresh(void) {
fflush(stdout);
}
void nmsterm_clear_input(void) {
void nmstermio_clear_input(void) {
int i;
ioctl(STDIN_FILENO, FIONREAD, &i);
@ -117,7 +117,7 @@ void nmsterm_clear_input(void) {
}
}
char nmsterm_get_char(void) {
char nmstermio_get_char(void) {
struct timespec ts;
int t = 50;
char c;
@ -132,7 +132,7 @@ char nmsterm_get_char(void) {
return c;
}
void nmsterm_print_reveal_string(char *s, int colorOn) {
void nmstermio_print_reveal_string(char *s, int colorOn) {
// Set bold and foreground color
BOLD();
@ -147,23 +147,23 @@ void nmsterm_print_reveal_string(char *s, int colorOn) {
CLEAR_ATTR();
}
void nmsterm_show_cursor(void) {
void nmstermio_show_cursor(void) {
CURSOR_SHOW();
}
void nmsterm_beep(void) {
void nmstermio_beep(void) {
BEEP();
}
int nmsterm_get_clearscr(void) {
int nmstermio_get_clearscr(void) {
return clearScr;
}
/*
* nmsterm_set_clearscr() sets the clearScr flag according to the
* Sets the clearScr flag according to the
* true/false value of the 's' argument.
*/
void nmsterm_set_clearscr(int s) {
void nmstermio_set_clearscr(int s) {
if (s)
clearScr = 1;
else
@ -171,13 +171,13 @@ void nmsterm_set_clearscr(int s) {
}
/*
* nms_set_foreground_color() sets the foreground color of the unencrypted
* Sets the foreground color of the unencrypted
* characters as they are revealed to the color indicated by the 'color'
* argument. Valid arguments are "white", "yellow", "magenta", "blue",
* "green", "red", and "cyan". This function will default to blue if
* passed an invalid color. No value is returned.
*/
void nmsterm_set_foregroundcolor(char *c) {
void nmstermio_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;
@ -200,10 +200,10 @@ void nmsterm_set_foregroundcolor(char *c) {
}
/*
* nms_get_cursor_row() returns the row position of the cursor as reported
* Returns the row position of the cursor as reported
* by the terminal program via the ANSI escape code
*/
int nmsterm_get_cursor_row(void) {
int nmstermio_get_cursor_row(void) {
int i, r = 0;
int row = 0;
char buf[10];
@ -233,12 +233,12 @@ int nmsterm_get_cursor_row(void) {
}
/*
* nmsterm_set_terminal() turns off terminal echo and line buffering when
* Turns off terminal echo and line buffering when
* passed an integer value that evaluates to true. It restores the
* original terminal values when passed an integer value that evaluates
* to false.
*/
static void nmsterm_set_terminal(int s) {
static void nmstermio_set_terminal(int s) {
struct termios tp;
static struct termios save;
static int state = 1;

31
src/nmstermio.h Normal file
View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2017 Brian Barto
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the MIT License. See LICENSE for more details.
*/
#ifndef NMSTERMIO_H
#define NMSTERMIO_H 1
// Function prototypes
void nmstermio_init_terminal(void);
void nmstermio_restore_terminal(void);
int nmstermio_get_rows(void);
int nmstermio_get_cols(void);
int nmstermio_get_cursor_row(void);
void nmstermio_move_cursor(int, int);
void nmstermio_print_string(char *);
void nmstermio_refresh(void);
void nmstermio_clear_input(void);
char nmstermio_get_char(void);
void nmstermio_print_reveal_string(char *, int);
void nmstermio_show_cursor(void);
void nmstermio_beep(void);
int nmstermio_get_clearscr(void);
void nmstermio_set_clearscr(int);
void nmstermio_set_foregroundcolor(char *);
int nmstermio_get_cursor_row(void);
#endif

View File

@ -8,13 +8,12 @@
#include <string.h>
#include <ncurses.h>
// Static settings
// Static settings
static int clearScr = 1; // clearScr flag
static int foregroundColor = COLOR_BLUE; // Foreground color setting
// Initialize terminal window
void nmsterm_init_terminal(void) {
void nmstermio_init_terminal(void) {
initscr();
cbreak();
noecho();
@ -26,54 +25,54 @@ void nmsterm_init_terminal(void) {
}
}
void nmsterm_restore_terminal(void) {
void nmstermio_restore_terminal(void) {
endwin();
}
/*
* nms_term_rows() gets and returns the number of rows in the current
* Gets and returns the number of rows in the current
* terminal window.
*/
int nmsterm_get_rows(void) {
int nmstermio_get_rows(void) {
return getmaxy(stdscr);
}
/*
* nms_term_cols() gets and returns the number of cols in the current
* Gets and returns the number of cols in the current
* terminal window.
*/
int nmsterm_get_cols(void) {
int nmstermio_get_cols(void) {
return getmaxx(stdscr);
}
/*
* nms_get_cursor_row() returns the row position of the cursor
* Returns the row position of the cursor
*/
int nmsterm_get_cursor_row(void) {
int nmstermio_get_cursor_row(void) {
return getcury(stdscr);
}
void nmsterm_move_cursor(int y, int x) {
void nmstermio_move_cursor(int y, int x) {
move(y, x);
}
void nmsterm_print_string(char *s) {
void nmstermio_print_string(char *s) {
addstr(s);
}
void nmsterm_refresh(void) {
void nmstermio_refresh(void) {
refresh();
}
void nmsterm_clear_input(void) {
void nmstermio_clear_input(void) {
flushinp();
}
void nmsterm_get_char(void) {
void nmstermio_get_char(void) {
getch();
}
void nmsterm_print_reveal_string(char *s, int colorOn) {
void nmstermio_print_reveal_string(char *s, int colorOn) {
(void) colorOn;
(void) foregroundColor;
@ -91,24 +90,24 @@ void nmsterm_print_reveal_string(char *s, int colorOn) {
attroff(A_BOLD);
}
void nmsterm_show_cursor(void) {
void nmstermio_show_cursor(void) {
curs_set(1);
refresh();
}
void nmsterm_beep(void) {
void nmstermio_beep(void) {
beep();
}
int nmsterm_get_clearscr(void) {
int nmstermio_get_clearscr(void) {
return clearScr;
}
/*
* nmsterm_set_clearscr() sets the clearScr flag according to the
* Sets the clearScr flag according to the
* true/false value of the 's' argument.
*/
void nmsterm_set_clearscr(int s) {
void nmstermio_set_clearscr(int s) {
if (s)
clearScr = 1;
else
@ -116,13 +115,13 @@ void nmsterm_set_clearscr(int s) {
}
/*
* nms_set_foreground_color() sets the foreground color of the unencrypted
* Sets the foreground color of the unencrypted
* characters as they are revealed to the color indicated by the 'color'
* argument. Valid arguments are "white", "yellow", "magenta", "blue",
* "green", "red", and "cyan". This function will default to blue if
* passed an invalid color. No value is returned.
*/
void nmsterm_set_foregroundcolor(char *c) {
void nmstermio_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;

View File

@ -10,7 +10,6 @@
#include <string.h>
#include <sys/ioctl.h>
#include "nmseffect.h"
#include "nmsterm.h"
int main(void) {
int termCols, spaces = 0;