Dillo v3.2.0-143-gabad1053
Loading...
Searching...
No Matches
actions.c
Go to the documentation of this file.
1/*
2 * File: actions.c
3 *
4 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 */
11
12#include "actions.h"
13#include "msg.h"
14#include "../dlib/dlib.h"
15#include <errno.h>
16#include <unistd.h>
17#include <stdlib.h>
18
19static Dlist *link_actions = NULL;
20static Dlist *page_actions = NULL;
21
22void
23action_parse(Dlist *actions, char *line)
24{
25 char *label = strtok(line, ":");
26
27 if (label == NULL || strlen(label) == 0) {
28 MSG("Missing action label, ignoring '%s'\n", line);
29 return;
30 }
31
32 //MSG("Got label='%s'\n", label);
33
34 char *cmd = strtok(NULL, "");
35
36 if (cmd == NULL || strlen(cmd) == 0) {
37 MSG("Missing action command, ignoring '%s'\n", line);
38 return;
39 }
40
41 //MSG("Got action label='%s' cmd='%s'\n", label, cmd);
42
43 Action *action = dMalloc(sizeof(Action));
44 action->label = dStrdup(label);
45 action->cmd = dStrdup(cmd);
46
47 dList_append(actions, action);
48}
49
50void
51actions_parse(Dlist **actions, Dlist *pref_actions)
52{
53 int n = dList_length(pref_actions);
54 *actions = dList_new(n);
55 for (int i = 0; i < n; i++) {
56 char *line = dList_nth_data(pref_actions, i);
57 if (line)
58 action_parse(*actions, line);
59 }
60}
61
62void
68
69Dlist *
71{
72 return link_actions;
73}
74
75Dlist *
77{
78 return page_actions;
79}
80
81int
83{
84 char pid[64];
85 snprintf(pid, 64, "%d", (int) getpid());
86
87 MSG("Running action '%s': %s\n", action->label, action->cmd);
88
89 int f = fork();
90 if (f == -1) {
91 MSG("Cannot run action '%s', fork failed: %s\n",
92 action->label, strerror(errno));
93 return -1;
94 }
95
96 if (f == 0) {
97 /* Set the pid of the dillo browser */
98 setenv("DILLO_PID", pid, 1);
99
100 /* Child */
101 errno = 0;
102 int ret = system(action->cmd);
103 if (ret == -1) {
104 MSG("Cannot run '%s': %s\n", action->cmd, strerror(errno));
105 exit(1);
106 } else if (ret != 0) {
107 MSG("Command exited with '%d': %s\n", ret, action->cmd);
108 exit(1);
109 } else {
110 /* All good, terminate the child */
111 exit(0);
112 }
113 }
114
115 return 0;
116}
Dlist * a_Actions_page_get(void)
Definition actions.c:76
int a_Actions_run(Action *action)
Definition actions.c:82
void actions_parse(Dlist **actions, Dlist *pref_actions)
Definition actions.c:51
static Dlist * page_actions
Definition actions.c:20
Dlist * a_Actions_link_get(void)
Definition actions.c:70
void a_Actions_init(void)
Definition actions.c:63
void action_parse(Dlist *actions, char *line)
Definition actions.c:23
static Dlist * link_actions
Definition actions.c:19
#define MSG(...)
Definition bookmarks.c:46
char * dStrdup(const char *s)
Definition dlib.c:77
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:576
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:641
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:690
void * dMalloc(size_t size)
Definition dlib.c:45
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
Definition dlib.c:625
DilloPrefs prefs
Global Data.
Definition prefs.c:33
char * cmd
Definition actions.h:23
char * label
Definition actions.h:22
Dlist * link_actions
Definition prefs.h:128
Dlist * page_actions
Definition prefs.h:129
Definition dlib.h:150