Skip to content

Commit ec4cd60

Browse files
committed
Split proxallium_main into modular functions
1 parent d8bbcc9 commit ec4cd60

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

proxallium.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,34 @@ struct OutputHandler {
2929
void process_cmdline_options(int argc, char *argv[]);
3030
noreturn void print_help(bool error, char *program_name);
3131
void proxallium_main(void);
32+
bool proxallium_gui_main(void);
33+
void proxallium_cleanup(void);
34+
void proxallium_process_line(char *line);
3235
char *proxallium_gen_torrc();
3336
bool tor_start(void);
3437
bool handler_bootstrap(char *line);
3538
bool handler_warnings_and_errors(char *line);
3639

3740
struct TorInstance *tor_instance;
3841

42+
// Output handlers
43+
struct OutputHandler handlers[] = {
44+
{handler_bootstrap, false},
45+
{handler_warnings_and_errors, false}
46+
};
47+
size_t handlers_num = sizeof handlers / sizeof(struct OutputHandler);
48+
3949
int main(int argc, char *argv[]) {
4050
// Process command-line arguments
4151
process_cmdline_options(argc, argv);
4252

43-
4453
// Start Tor
4554
tor_start();
4655

4756
// Handle main loop
4857
proxallium_main();
4958

5059
// Clean up and exit
51-
allium_clean(tor_instance);
5260
free(tor_instance);
5361
return EXIT_SUCCESS;
5462
}
@@ -105,22 +113,24 @@ void noreturn print_help(bool error, char *program_name) {
105113
}
106114

107115
void proxallium_main() {
108-
// Read output and call handlers
109-
struct OutputHandler handlers[] = {
110-
{handler_bootstrap, false},
111-
{handler_warnings_and_errors, false}
112-
};
113-
size_t handlers_num = sizeof handlers / sizeof(struct OutputHandler);
114-
115116
// Main event loop
116-
char *output;
117-
while (output = allium_read_stdout_line(tor_instance)) {
118-
for (int handler = 0; handler < handlers_num; handler++) {
119-
if (!handlers[handler].finished)
120-
handlers[handler].finished = handlers[handler].function(output);
121-
}
117+
char *line;
118+
while (line = allium_read_stdout_line(tor_instance)) {
119+
proxallium_process_line(line);
122120
}
121+
proxallium_cleanup();
122+
}
123+
124+
void proxallium_cleanup() {
123125
log_output("\nFinished reading Tor's output! Tor exited with exit code %i.", allium_get_exit_code(tor_instance));
126+
allium_clean(tor_instance);
127+
}
128+
129+
void proxallium_process_line(char *line) {
130+
for (int handler = 0; handler < handlers_num; handler++) {
131+
if (!handlers[handler].finished)
132+
handlers[handler].finished = handlers[handler].function(line);
133+
}
124134
}
125135

126136
char *proxallium_gen_torrc() {

0 commit comments

Comments
 (0)