Skip to content

Commit 41a2948

Browse files
Refactor quit handling with a helper function
Previously, "do_quit" was used outside of command dispatching in "record_error", violating the intended function structure. This change introduces "force_quit", a dedicated helper function for handling forced console termination. Now, both "do_quit" and "record_error" call "force_quit", reducing code duplication while ensuring that "do_" functions remain strictly for command dispatching. Co-authored-by: charliechiou <[email protected]> Change-Id: Id1bd4177f352c4e4e0980e5ae5873ae55a3e916b
1 parent e07fd10 commit 41a2948

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

console.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,43 @@ static char **parse_args(char *line, int *argcp)
160160
return argv;
161161
}
162162

163-
static bool do_quit(int argc, char *argv[]);
163+
/* Handles forced console termination for record_error and do_quit */
164+
static bool force_quit(int argc, char *argv[])
165+
{
166+
cmd_element_t *c = cmd_list;
167+
bool ok = true;
168+
while (c) {
169+
cmd_element_t *ele = c;
170+
c = c->next;
171+
free_block(ele, sizeof(cmd_element_t));
172+
}
173+
174+
param_element_t *p = param_list;
175+
while (p) {
176+
param_element_t *ele = p;
177+
p = p->next;
178+
free_block(ele, sizeof(param_element_t));
179+
}
180+
181+
while (buf_stack)
182+
pop_file();
183+
184+
for (int i = 0; i < quit_helper_cnt; i++) {
185+
ok = ok && quit_helpers[i](argc, argv);
186+
}
187+
188+
quit_flag = true;
189+
return ok;
190+
}
191+
164192
static void record_error()
165193
{
166194
err_cnt++;
167195
if (err_cnt >= err_limit) {
168196
report(
169197
1,
170198
"Error limit exceeded. Stopping command execution, and quitting");
171-
do_quit(0, NULL);
199+
force_quit(0, NULL);
172200
}
173201
}
174202

@@ -229,30 +257,7 @@ void set_echo(bool on)
229257
/* Built-in commands */
230258
static bool do_quit(int argc, char *argv[])
231259
{
232-
cmd_element_t *c = cmd_list;
233-
bool ok = true;
234-
while (c) {
235-
cmd_element_t *ele = c;
236-
c = c->next;
237-
free_block(ele, sizeof(cmd_element_t));
238-
}
239-
240-
param_element_t *p = param_list;
241-
while (p) {
242-
param_element_t *ele = p;
243-
p = p->next;
244-
free_block(ele, sizeof(param_element_t));
245-
}
246-
247-
while (buf_stack)
248-
pop_file();
249-
250-
for (int i = 0; i < quit_helper_cnt; i++) {
251-
ok = ok && quit_helpers[i](argc, argv);
252-
}
253-
254-
quit_flag = true;
255-
return ok;
260+
return force_quit(argc, argv);
256261
}
257262

258263
static bool do_help(int argc, char *argv[])

0 commit comments

Comments
 (0)