-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.c
More file actions
132 lines (107 loc) · 3.64 KB
/
run.c
File metadata and controls
132 lines (107 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Copyright 2018-2023 Jack Conger */
/*
* This file is part of notcat.
*
* notcat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* notcat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with notcat. If not, see <http://www.gnu.org/licenses/>.
*/
// Used for setenv() (for now) and posix_spawnp()
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <spawn.h>
#include "notlib/notlib.h"
#include "notcat.h"
int shell_run_opt = 0;
int use_env_opt = 0;
extern void print_note(const NLNote *n) {
buffer *buf = new_buffer(BUF_LEN);
size_t i;
for (i = 0; i < fmt.len; i++) {
fmt_note_buf(buf, &fmt.terms[i], n);
if (i < fmt.len - 1)
put_char(buf, ' ');
}
put_char(buf, '\n');
char *fin = dump_buffer(buf);
fputs(fin, stdout);
free(fin);
}
extern void run_cmd(char *cmd, const NLNote *n) {
size_t prefix_len = (shell_run_opt ? 4 : 1);
size_t fmt_len = (use_env_opt ? 0 : fmt.len);
char **cmd_argv = malloc(sizeof(char *) * (1 + prefix_len + fmt_len));
static char *sh = NULL;
if (!sh && !(sh = getenv("SHELL")))
sh = "/bin/sh";
if (shell_run_opt) {
cmd_argv[0] = sh;
cmd_argv[1] = "-c";
cmd_argv[2] = cmd;
cmd_argv[3] = "notcat";
} else {
cmd_argv[0] = cmd;
}
size_t i;
for (i = 0; i < fmt_len; i++) {
cmd_argv[i+prefix_len] = fmt_note(&fmt.terms[i], n);
}
cmd_argv[fmt_len + prefix_len] = NULL;
// FIXME: Build a new cmd_env instead of munging the local environment
if (use_env_opt) {
setenv("NOTCAT_EVENT", current_event, 1);
if (n != NULL) {
char str[12]; // big enough to store a 32-bit int
setenv("NOTE_APP_NAME", n->appname, 1);
setenv("NOTE_SUMMARY", n->summary, 1);
setenv("NOTE_BODY", n->body, 1);
setenv("NOTE_URGENCY", str_urgency(n->urgency), 1);
snprintf(str, 12, "%u", n->id);
setenv("NOTE_ID", str, 1);
snprintf(str, 12, "%d", n->timeout);
setenv("NOTE_TIMEOUT", str, 1);
char *h = nl_get_hint_as_string(n, "category");
if (h != NULL) {
setenv("NOTE_CATEGORY", h, 1);
free(h);
}
// TODO: Figure out a sensible way to do hints and actions
}
}
int err;
pid_t cpid;
extern char **environ;
if ((err = posix_spawnp(&cpid, cmd_argv[0], NULL, NULL, cmd_argv, environ))) {
char *fmt = "posix_spawnp(%s) on %s event";
int msglen = strlen(cmd_argv[0]) + strlen(fmt) + strlen(current_event);
char errmsg[msglen + 1];
snprintf(errmsg, msglen, "posix_spawnp(%s) on %s event", cmd_argv[0], current_event);
errno = err;
perror(errmsg);
}
// TODO: properly handle signals, like https://www.cons.org/cracauer/sigint.html
while ((err = wait(NULL)) == -1) {
if (errno != EINTR) {
perror("wait");
break;
}
}
for (i = 0; i < fmt_len; i++)
free(cmd_argv[i+prefix_len]);
free(cmd_argv);
}