Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit bfc5cc8

Browse files
committed
Update Linux to sync with Mac changes.
1 parent 8432787 commit bfc5cc8

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

src/x11/main.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,9 @@ void run_server(clioptions *opts);
2121

2222
int main(int argc, const char **argv)
2323
{
24-
clioptions *opts = parse_commandline(argc, argv);
25-
if (opts == NULL) {
26-
return 1;
27-
}
28-
29-
if (opts->browser == NULL) {
30-
opts->browser = "xdg-open";
31-
}
32-
33-
if (opts->results == NULL) {
34-
opts->results = "";
35-
}
36-
37-
if (opts->browser_args == NULL) {
38-
opts->browser_args = "";
39-
}
40-
41-
run_server(opts);
42-
free(opts);
24+
clioptions opts;
25+
parse_commandline(argc, argv, &opts);
26+
run_server(&opts);
4327
return 0;
4428
}
4529

src/x11/screenscraper.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,34 @@ void debug_log(const char *message, ...) {
221221
static pid_t browser_process_pid = 0;
222222

223223
bool open_browser(const char *program, const char *args, const char *url) {
224-
size_t buffer_size = 0;
225-
224+
assert(url);
225+
if (browser_process_pid) {
226+
debug_log("Warning: calling open_browser, but browser already open.");
227+
}
228+
if (program == NULL) {
229+
program = "xdg-open";
230+
}
226231
if (args == NULL) {
227232
args = "";
228233
}
229-
buffer_size += strlen(args);
230-
231-
// NOTE: url is already defined in server.c
232-
buffer_size += strlen(url);
233-
234-
// NOTE: program is defined in main.c
235-
buffer_size += strlen(program);
236-
237-
buffer_size += 3; // Account for spaces and EOL
238-
char *buffer = malloc(buffer_size * sizeof(char));
239-
sprintf(buffer, "%s %s %s", program, args, url);
240-
241-
if (strcmp(program, "xdg-open") == 0) {
242-
system(buffer);
243-
} else {
244-
pid_t pid = fork();
245-
if (!pid) {
246-
// child process, launch the browser!
247-
wordexp_t args;
248-
wordexp(buffer, &args, 0);
249-
execv(args.we_wordv[0], args.we_wordv);
250-
exit(1);
251-
} else {
252-
browser_process_pid = pid;
253-
}
234+
235+
char command_line[4096];
236+
snprintf(command_line, sizeof(command_line), "'%s' %s '%s'", program, args, url);
237+
command_line[sizeof(command_line) - 1] = '\0';
238+
239+
wordexp_t expanded_args;
240+
int result = wordexp(command_line, &expanded_args, 0);
241+
if (result) {
242+
debug_log("Failed to parse command line: %s", command_line);
243+
return false;
244+
}
245+
browser_process_pid = fork();
246+
if (!browser_process_pid) {
247+
// child process, launch the browser!
248+
execvp(expanded_args.we_wordv[0], expanded_args.we_wordv);
249+
exit(1);
254250
}
255-
free(buffer);
251+
wordfree(&expanded_args);
256252
return true;
257253
}
258254

0 commit comments

Comments
 (0)