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(); blast();
return GO_CLEAROBJ; return GO_CLEAROBJ;
case 23: /* SCOR */ case 23: /* SCOR */
score(scoregame); score(scoregame);
return GO_CLEAROBJ; return GO_CLEAROBJ;
case 24: /* FOO */ case 24: /* FOO */
return bigwords(WD1); return bigwords(WD1);
case 25: /* BRIEF */ case 25: /* BRIEF */

View file

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

View file

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

24
main.c
View file

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

135
misc.c
View file

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