Skip to content

Commit 1d6d66b

Browse files
author
peter
committed
-l opt
1 parent e0e7b10 commit 1d6d66b

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

main.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#define WIDTH 70
1111

12-
static int log_opt = 0;
12+
static int opt_log = 0;
1313
static int opt_debug = 0;
1414
static int opt_width = WIDTH;
1515

@@ -96,6 +96,14 @@ static void footer(void)
9696
}
9797
}
9898

99+
static void reopen_stderr(const char *fname)
100+
{
101+
if (*fname && freopen(fname, "w", stderr) != stderr) {
102+
fprintf(stderr, "Error opening '%s': %s\n", fname, strerror(errno));
103+
exit(1);
104+
}
105+
}
106+
99107
int main(int argc, const char **argv)
100108
{
101109
int rc, i;
@@ -106,27 +114,26 @@ int main(int argc, const char **argv)
106114
SetConsoleCP(1251);
107115
#endif
108116
for (i = 1; i < argc; i++) {
109-
if (!strcmp(argv[i], "-d"))
117+
if (!strncmp(argv[i], "-l", 2)) {
118+
opt_log = 1;
119+
reopen_stderr(argv[i] + 2);
120+
} else if (!strncmp(argv[i], "-d", 2)) {
110121
opt_debug = 1;
111-
else if (!strncmp(argv[i], "-w", 2))
122+
reopen_stderr(argv[i] + 2);
123+
} else if (!strncmp(argv[i], "-w", 2)) {
112124
opt_width = atoi(argv[i] + 2);
113-
else if (!strncmp(argv[i], "-i", 2)) {
125+
} else if (!strncmp(argv[i], "-i", 2)) {
114126
if (freopen(argv[i] + 2, "r", stdin) != stdin) {
115127
fprintf(stderr, "Error opening '%s': %s\n", argv[i] + 2, strerror(errno));
116128
exit(1);
117129
}
118-
} else if (!strncmp(argv[i], "-e", 2)) {
119-
if (freopen(argv[i] + 2, "w", stderr) != stderr) {
120-
fprintf(stderr, "Error opening '%s': %s\n", argv[i] + 2, strerror(errno));
121-
exit(1);
122-
}
123130
} else if (!game) {
124131
game = argv[i];
125132
}
126133
}
127134

128135
if (!game) {
129-
fprintf(stderr, "Usage: %s [-d] [-w<width>] <game>\n", argv[0]);
136+
fprintf(stdout, "Usage: %s [-d] [-w<width>] <game>\n", argv[0]);
130137
exit(1);
131138
}
132139

@@ -135,17 +142,17 @@ int main(int argc, const char **argv)
135142
exit(1);
136143
}
137144

138-
if (!opt_debug)
145+
if (!opt_debug && !opt_log)
139146
fclose(stderr);
140147

141148
instead_set_debug(opt_debug);
142149

143150
if (instead_init(game)) {
144-
fprintf(stderr, "Can not init game: %s\n", game);
151+
fprintf(stdout, "Can not init game: %s\n", game);
145152
exit(1);
146153
}
147154
if (instead_load(NULL)) {
148-
fprintf(stderr, "Can not load game: %s\n", instead_err());
155+
fprintf(stdout, "Can not load game: %s\n", instead_err());
149156
exit(1);
150157
}
151158
#if 0 /* no autoload */
@@ -169,10 +176,6 @@ int main(int argc, const char **argv)
169176
p[strcspn(p, "\n\r")] = 0;
170177
if (!strcmp(p, "quit"))
171178
break;
172-
if (!strcmp(p, "log")) {
173-
log_opt = 1;
174-
continue;
175-
}
176179

177180
if (!strncmp(p, "load ", 5) || !strncmp(p, "save ", 5)) {
178181
rc = 1; str = NULL;
@@ -202,7 +205,7 @@ int main(int argc, const char **argv)
202205
free(str);
203206
if (!rc) /* no parser */
204207
footer();
205-
if (log_opt)
208+
if (opt_log)
206209
fprintf(stderr, "%s\n", p);
207210
}
208211
instead_cmd("save autosave", NULL);

0 commit comments

Comments
 (0)