Information hiding - moved all of command input parsing to misc.c

get_command_input() is effectively an I/O function that takes raw input
and makes it into a command_t. With other I/O functions, it belongs in
misc.c.

This alos allowed me to make 4 other functions static, as they were only
called by get_command_input();
This commit is contained in:
Aaron Traas 2017-07-20 16:02:54 -04:00
parent 10d681ebeb
commit a167945e19
3 changed files with 89 additions and 90 deletions

View file

@ -200,18 +200,15 @@ struct command_t {
extern struct game_t game; extern struct game_t game;
extern struct settings_t settings; extern struct settings_t settings;
extern void tokenize(char*, struct command_t *); extern bool get_command_input(struct command_t *);
extern void wordclear(token_t *); extern void wordclear(token_t *);
extern void speak(const char*, ...); extern void speak(const char*, ...);
extern void sspeak(long msg, ...); extern void sspeak(long msg, ...);
extern void pspeak(vocab_t, enum speaktype, int, bool, ...); extern void pspeak(vocab_t, enum speaktype, int, bool, ...);
extern void rspeak(vocab_t, ...); extern void rspeak(vocab_t, ...);
extern void echo_input(FILE*, const char*, const char*); extern void echo_input(FILE*, const char*, const char*);
extern int word_count(char*);
extern char* get_input(void);
extern bool silent_yes(void); extern bool silent_yes(void);
extern bool yes(const char*, const char*, const char*); extern bool yes(const char*, const char*, const char*);
extern void get_vocab_metadata(const char*, long*, enum wordtype*);
extern void juggle(obj_t); extern void juggle(obj_t);
extern void move(obj_t, loc_t); extern void move(obj_t, loc_t);
extern long put(obj_t, long, long); extern long put(obj_t, long, long);

64
main.c
View file

@ -986,36 +986,6 @@ static void listobjects(void)
} }
} }
static bool get_command_input(struct command_t *command)
{
char inputbuf[LINESIZE];
char* input;
for (;;) {
input = get_input();
if (input == NULL)
return false;
if (word_count(input) > 2) {
rspeak(TWO_WORDS);
free(input);
continue;
}
if (strcmp(input, "") != 0)
break;
free(input);
}
strncpy(inputbuf, input, LINESIZE - 1);
free(input);
tokenize(inputbuf, command);
get_vocab_metadata(command->raw1, &(command->id1), &(command->type1));
get_vocab_metadata(command->raw2, &(command->id2), &(command->type2));
return true;
}
static bool do_command() static bool do_command()
/* Get and execute a command */ /* Get and execute a command */
{ {
@ -1116,7 +1086,7 @@ Lclearobj:
lampcheck(); lampcheck();
if (command.type1 == MOTION && command.id1 == ENTER if (command.type1 == MOTION && command.id1 == ENTER
&& (command.id2 == STREAM || command.id2 == WATER)) { && (command.id2 == STREAM || command.id2 == WATER)) {
if (LIQLOC(game.loc) == WATER) if (LIQLOC(game.loc) == WATER)
rspeak(FEET_WET); rspeak(FEET_WET);
else else
@ -1126,22 +1096,22 @@ Lclearobj:
} }
if (command.type1 == OBJECT) { if (command.type1 == OBJECT) {
if (command.id1 == GRATE) { if (command.id1 == GRATE) {
command.type1 = MOTION; command.type1 = MOTION;
if (game.loc == LOC_START || if (game.loc == LOC_START ||
game.loc == LOC_VALLEY || game.loc == LOC_VALLEY ||
game.loc == LOC_SLIT) { game.loc == LOC_SLIT) {
command.id1 = DEPRESSION; command.id1 = DEPRESSION;
} }
if (game.loc == LOC_COBBLE || if (game.loc == LOC_COBBLE ||
game.loc == LOC_DEBRIS || game.loc == LOC_DEBRIS ||
game.loc == LOC_AWKWARD || game.loc == LOC_AWKWARD ||
game.loc == LOC_BIRD || game.loc == LOC_BIRD ||
game.loc == LOC_PITTOP) { game.loc == LOC_PITTOP) {
command.id1 = ENTRANCE; command.id1 = ENTRANCE;
} }
} }
if (!((command.id1 != WATER && command.id1 != OIL) || (command.id2 != PLANT && command.id2 != DOOR))) { if (!((command.id1 != WATER && command.id1 != OIL) || (command.id2 != PLANT && command.id2 != DOOR))) {
if (AT(command.id2)) { if (AT(command.id2)) {
command.id2 = POUR; command.id2 = POUR;
command.type2 = ACTION; command.type2 = ACTION;

110
misc.c
View file

@ -23,38 +23,6 @@ static void* xcalloc(size_t size)
return (ptr); return (ptr);
} }
void tokenize(char* raw, struct command_t *cmd)
{
memset(cmd, '\0', sizeof(struct command_t));
/* Bound prefix on the %s would be needed to prevent buffer
* overflow. but we shortstop this more simply by making each
* raw-input buffer as long as the enrire inout buffer. */
sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
/* (ESR) In oldstyle mode, simulate the uppercasing and truncating
* effect on raw tokens of packing them into sixbit characters, 5
* to a 32-bit word. This is something the FORTRAN version did
* becuse archaic FORTRAN had no string types. Don Wood's
* mechanical translation of 2.5 to C retained the packing and
* thus this misfeature.
*
* It's philosophically questionable whether this is the right
* thing to do even in oldstyle mode. On one hand, the text
* mangling was not authorial intent, but a result of limitations
* in their tools. On the other, not simulating this misbehavior
* goes against the goal of making oldstyle as accurate as
* possible an emulation of the original UI.
*/
if (settings.oldstyle) {
cmd->raw1[TOKLEN + TOKLEN] = cmd->raw2[TOKLEN + TOKLEN] = '\0';
for (size_t i = 0; i < strlen(cmd->raw1); i++)
cmd->raw1[i] = toupper(cmd->raw1[i]);
for (size_t i = 0; i < strlen(cmd->raw2); i++)
cmd->raw2[i] = toupper(cmd->raw2[i]);
}
}
/* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */ /* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
static void vspeak(const char* msg, bool blank, va_list ap) static void vspeak(const char* msg, bool blank, va_list ap)
@ -101,18 +69,18 @@ static void vspeak(const char* msg, bool blank, va_list ap)
// packed tokens, stringify everything. Future work may // packed tokens, stringify everything. Future work may
// eliminate the need for this. // eliminate the need for this.
if (msg[i] == 'd') { if (msg[i] == 'd') {
long arg = va_arg(ap, long); long arg = va_arg(ap, long);
int ret = snprintf(renderp, size, "%ld", arg); int ret = snprintf(renderp, size, "%ld", arg);
if (ret < size) { if (ret < size) {
renderp += ret; renderp += ret;
size -= ret; size -= ret;
} }
pluralize = (arg != 1); pluralize = (arg != 1);
} }
// Unmodified string specifier. // Unmodified string specifier.
if (msg[i] == 's') { if (msg[i] == 's') {
char *arg = va_arg(ap, char *); char *arg = va_arg(ap, char *);
strncat(renderp, arg, size - 1); strncat(renderp, arg, size - 1);
size_t len = strlen(renderp); size_t len = strlen(renderp);
renderp += len; renderp += len;
@ -121,7 +89,7 @@ static void vspeak(const char* msg, bool blank, va_list ap)
// Singular/plural specifier. // Singular/plural specifier.
if (msg[i] == 'S') { if (msg[i] == 'S') {
// look at the *previous* numeric parameter // look at the *previous* numeric parameter
if (pluralize) { if (pluralize) {
*renderp++ = 's'; *renderp++ = 's';
size--; size--;
@ -211,7 +179,7 @@ void echo_input(FILE* destination, const char* input_prompt, const char* input)
free(prompt_and_input); free(prompt_and_input);
} }
int word_count(char* str) static int word_count(char* str)
{ {
char delims[] = " \t"; char delims[] = " \t";
int count = 0; int count = 0;
@ -232,7 +200,7 @@ int word_count(char* str)
return (count); return (count);
} }
char* get_input() static char* get_input()
{ {
// Set up the prompt // Set up the prompt
char input_prompt[] = "> "; char input_prompt[] = "> ";
@ -434,7 +402,7 @@ static int get_special_vocab_id(const char* word)
return (WORD_NOT_FOUND); return (WORD_NOT_FOUND);
} }
void get_vocab_metadata(const char* word, long* id, enum wordtype* type) static void get_vocab_metadata(const char* word, long* id, enum wordtype* type)
{ {
/* Check for an empty string */ /* Check for an empty string */
if (strncmp(word, "", sizeof("")) == 0) { if (strncmp(word, "", sizeof("")) == 0) {
@ -485,6 +453,70 @@ void get_vocab_metadata(const char* word, long* id, enum wordtype* type)
return; return;
} }
static void tokenize(char* raw, struct command_t *cmd)
{
memset(cmd, '\0', sizeof(struct command_t));
/* Bound prefix on the %s would be needed to prevent buffer
* overflow. but we shortstop this more simply by making each
* raw-input buffer as long as the enrire inout buffer. */
sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
/* (ESR) In oldstyle mode, simulate the uppercasing and truncating
* effect on raw tokens of packing them into sixbit characters, 5
* to a 32-bit word. This is something the FORTRAN version did
* becuse archaic FORTRAN had no string types. Don Wood's
* mechanical translation of 2.5 to C retained the packing and
* thus this misfeature.
*
* It's philosophically questionable whether this is the right
* thing to do even in oldstyle mode. On one hand, the text
* mangling was not authorial intent, but a result of limitations
* in their tools. On the other, not simulating this misbehavior
* goes against the goal of making oldstyle as accurate as
* possible an emulation of the original UI.
*/
if (settings.oldstyle) {
cmd->raw1[TOKLEN + TOKLEN] = cmd->raw2[TOKLEN + TOKLEN] = '\0';
for (size_t i = 0; i < strlen(cmd->raw1); i++)
cmd->raw1[i] = toupper(cmd->raw1[i]);
for (size_t i = 0; i < strlen(cmd->raw2); i++)
cmd->raw2[i] = toupper(cmd->raw2[i]);
}
/* populate command with parsed vocab metadata */
get_vocab_metadata(cmd->raw1, &(cmd->id1), &(cmd->type1));
get_vocab_metadata(cmd->raw2, &(cmd->id2), &(cmd->type2));
}
bool get_command_input(struct command_t *command)
/* Get user input on stdin, parse and map to command */
{
char inputbuf[LINESIZE];
char* input;
for (;;) {
input = get_input();
if (input == NULL)
return false;
if (word_count(input) > 2) {
rspeak(TWO_WORDS);
free(input);
continue;
}
if (strcmp(input, "") != 0)
break;
free(input);
}
strncpy(inputbuf, input, LINESIZE - 1);
free(input);
tokenize(inputbuf, command);
return true;
}
void juggle(obj_t object) void juggle(obj_t object)
/* Juggle an object by picking it up and putting it down again, the purpose /* Juggle an object by picking it up and putting it down again, the purpose
* being to get the object to the front of the chain of things at its loc. */ * being to get the object to the front of the chain of things at its loc. */