Reindent and cleanup.

This commit is contained in:
Eric S. Raymond 2017-06-18 20:24:37 -04:00
parent 5341a3e2ee
commit fd02259c7f
5 changed files with 93 additions and 99 deletions

View file

@ -1113,8 +1113,8 @@ int action(FILE *input, enum speechpart part, long verb, token_t obj)
blast();
return GO_CLEAROBJ;
case 23: /* SCOR */
score(scoregame);
return GO_CLEAROBJ;
score(scoregame);
return GO_CLEAROBJ;
case 24: /* FOO */
return bigwords(WD1);
case 25: /* BRIEF */

View file

@ -9,7 +9,7 @@
extern const char advent_to_ascii[128];
extern const char ascii_to_advent[128];
enum bug_e {
enum bugtype {
MESSAGE_LINE_GT_70_CHARACTERS, // 0
NULL_LINE_IN_MESSAGE, // 1
TOO_MANY_WORDS_OF_MESSAGES, // 2
@ -30,14 +30,13 @@ enum bug_e {
CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION, // 25
LOCATION_HAS_NO_TRAVEL_ENTRIES, // 26
HINT_NUMBER_EXCEEDS_GOTO_LIST, // 27
INVALID_MOTH_RETURNED_BY_DATA_FUNCTION, // 28
TOO_MANY_PARAMETERS_GIVEN_TO_SETPRM, // 29
TOO_MANY_PARAMETERS_GIVEN_TO_SETPRM, // 28
SPEECHPART_NOT_TRANSITIVE_OR_INTRANSITIVE_OR_UNKNOWN=99, // 99
ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH, // 100
};
static inline void bug(enum bug_e num, const char *error_string) __attribute__((__noreturn__));
static inline void bug(enum bug_e num, const char *error_string)
static inline void bug(enum bugtype num, const char *error_string) __attribute__((__noreturn__));
static inline void bug(enum bugtype num, const char *error_string)
{
fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
exit(EXIT_FAILURE);

View file

@ -230,7 +230,7 @@ static void read_messages(FILE* database, long sect)
if (sect == 10) {
CLSSES = CLSSES + 1;
if (CLSSES > CLSMAX)
BUG(TOO_MANY_CLASS_OR_TURN_MESSAGES);
BUG(TOO_MANY_CLASS_OR_TURN_MESSAGES);
CTEXT[CLSSES] = LINUSE;
CVAL[CLSSES] = loc;
continue;
@ -324,7 +324,7 @@ static void read_conditions(FILE* database)
long loc;
while ((loc = GETNUM(NULL)) != 0) {
if (is_set(COND[loc], K))
BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
COND[loc] = COND[loc] + (1l << K);
}
}
@ -338,7 +338,7 @@ static void read_hints(FILE* database)
HNTMAX = 0;
while ((K = GETNUM(database)) != -1) {
if (K <= 0 || K > HNTSIZ)
BUG(TOO_MANY_HINTS);
BUG(TOO_MANY_HINTS);
for (int I = 1; I <= 4; I++) {
HINTS[K][I] = GETNUM(NULL);
} /* end loop */

24
main.c
View file

@ -104,15 +104,15 @@ int main(int argc, char *argv[])
break;
default:
fprintf(stderr,
"Usage: %s [-l logfilename] [-o] [-r restorefilename] [-s] \n", argv[0]);
"Usage: %s [-l logfilename] [-o] [-r restorefilename] [-s] \n", argv[0]);
fprintf(stderr,
" where -l creates a log file of your game named as specified'\n");
" where -l creates a log file of your game named as specified'\n");
fprintf(stderr,
" -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
" -o 'oldstyle' (no prompt, no command editing, displays 'Initialising...')\n");
fprintf(stderr,
" -r indicates restoring from specified saved game file\n");
" -r indicates restoring from specified saved game file\n");
fprintf(stderr,
" -s indicates playing with command editing suppressed\n");
" -s indicates playing with command editing suppressed\n");
exit(-1);
break;
}
@ -636,10 +636,10 @@ static bool playermove(token_t verb, int motion)
/* else fall through */
} else if (game.prop[motion] != game.newloc / 100 - 3)
break;
L12:
L12:
do {
if (TRAVEL[kk] < 0)
BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION);
BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION);
++kk;
game.newloc = labs(TRAVEL[kk]) / 1000;
} while
@ -661,9 +661,9 @@ static bool playermove(token_t verb, int motion)
/* FIXME: Arithmetic on location numbers */
game.newloc = 99 + 100 - game.loc;
if (game.holdng > 1 || (game.holdng == 1 && !TOTING(EMRALD))) {
game.newloc = game.loc;
RSPEAK(MUST_DROP);
}
game.newloc = game.loc;
RSPEAK(MUST_DROP);
}
return true;
case 2:
/* Travel 302. Plover transport. Drop the emerald (only use
@ -671,7 +671,7 @@ static bool playermove(token_t verb, int motion)
* plover-passage to get it out. Having dropped it, go back and
* pretend he wasn't carrying it after all. */
DROP(EMRALD, game.loc);
goto L12;
goto L12;
case 3:
/* Travel 303. Troll bridge. Must be done only as special
* motion so that dwarves won't wander across and encounter
@ -707,7 +707,7 @@ static bool playermove(token_t verb, int motion)
BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST);
}
} while
(false);
(false);
/* FIXME: Arithmetic on location number, becoming a message number */
RSPEAK(game.newloc - 500);
game.newloc = game.loc;

135
misc.c
View file

@ -12,13 +12,12 @@
void* xmalloc(size_t size)
{
void* ptr = malloc(size);
if (ptr == NULL)
{
fprintf(stderr, "Out of memory!\n");
exit(EXIT_FAILURE);
void* ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(EXIT_FAILURE);
}
return(ptr);
return (ptr);
}
char* xstrdup(const char* s)
@ -198,59 +197,57 @@ bool GETIN(FILE *input,
void echo_input(FILE* destination, char* input_prompt, char* input)
{
size_t len = strlen(input_prompt) + strlen(input) + 1;
char* prompt_and_input = (char*) xmalloc(len);
strcpy(prompt_and_input, input_prompt);
strcat(prompt_and_input, input);
fprintf(destination, "%s\n", prompt_and_input);
free(prompt_and_input);
size_t len = strlen(input_prompt) + strlen(input) + 1;
char* prompt_and_input = (char*) xmalloc(len);
strcpy(prompt_and_input, input_prompt);
strcat(prompt_and_input, input);
fprintf(destination, "%s\n", prompt_and_input);
free(prompt_and_input);
}
char* get_input()
{
// Set up the prompt
char input_prompt[] = "> ";
if (!prompt)
input_prompt[0] = '\0';
// Set up the prompt
char input_prompt[] = "> ";
if (!prompt)
input_prompt[0] = '\0';
// Print a blank line if game.blklin tells us to.
if (game.blklin == true)
printf("\n");
// Print a blank line if game.blklin tells us to.
if (game.blklin == true)
printf("\n");
char* input;
while (true)
{
if (editline)
input = linenoise(input_prompt);
else
{
input = NULL;
size_t n = 0;
if (isatty(0))
printf("%s", input_prompt);
IGNORE(getline(&input, &n, stdin));
}
char* input;
while (true) {
if (editline)
input = linenoise(input_prompt);
else {
input = NULL;
size_t n = 0;
if (isatty(0))
printf("%s", input_prompt);
IGNORE(getline(&input, &n, stdin));
}
if (input == NULL) // Got EOF; quit.
exit(EXIT_SUCCESS);
else if (input[0] == '#') // Ignore comments.
continue;
else // We have a 'normal' line; leave the loop.
break;
if (input == NULL) // Got EOF; quit.
exit(EXIT_SUCCESS);
else if (input[0] == '#') // Ignore comments.
continue;
else // We have a 'normal' line; leave the loop.
break;
}
// Strip trailing newlines from the input
input[strcspn(input, "\n")] = 0;
// Strip trailing newlines from the input
input[strcspn(input, "\n")] = 0;
linenoiseHistoryAdd(input);
linenoiseHistoryAdd(input);
if (!isatty(0))
echo_input(stdout, input_prompt, input);
if (!isatty(0))
echo_input(stdout, input_prompt, input);
if (logfp)
echo_input(logfp, input_prompt, input);
if (logfp)
echo_input(logfp, input_prompt, input);
return(input);
return (input);
}
bool YES(vocab_t question, vocab_t yes_response, vocab_t no_response)
@ -261,38 +258,36 @@ bool YES(vocab_t question, vocab_t yes_response, vocab_t no_response)
bool outcome;
for (;;) {
RSPEAK(question);
RSPEAK(question);
reply = get_input();
reply = get_input();
char* firstword = (char*) xmalloc(strlen(reply));
sscanf(reply, "%s", firstword);
char* firstword = (char*) xmalloc(strlen(reply));
sscanf(reply, "%s", firstword);
for (int i = 0; i < (int)strlen(firstword); ++i)
firstword[i] = tolower(firstword[i]);
for (int i = 0; i < (int)strlen(firstword); ++i)
firstword[i] = tolower(firstword[i]);
int yes = strncmp("yes", firstword, sizeof("yes") - 1);
int y = strncmp("y", firstword, sizeof("y") - 1);
int no = strncmp("no", firstword, sizeof("no") - 1);
int n = strncmp("n", firstword, sizeof("n") - 1);
int yes = strncmp("yes", firstword, sizeof("yes") - 1);
int y = strncmp("y", firstword, sizeof("y") - 1);
int no = strncmp("no", firstword, sizeof("no") - 1);
int n = strncmp("n", firstword, sizeof("n") - 1);
free(firstword);
free(firstword);
if (yes == 0 || y == 0) {
RSPEAK(yes_response);
outcome = true;
break;
}
else if (no == 0 || n == 0) {
RSPEAK(no_response);
outcome = false;
break;
}
else
RSPEAK(PLEASE_ANSWER);
if (yes == 0 || y == 0) {
RSPEAK(yes_response);
outcome = true;
break;
} else if (no == 0 || n == 0) {
RSPEAK(no_response);
outcome = false;
break;
} else
RSPEAK(PLEASE_ANSWER);
}
linenoiseFree(reply);
return(outcome);
return (outcome);
}
/* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT) */