-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
202 lines (172 loc) · 5.8 KB
/
main.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* This file is part of clipsim.
* Copyright (C) 2024 Lucas Mior
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clipsim.h"
typedef struct Command {
const char *shortname;
const char *longname;
const char *description;
} Command;
static const Command commands[] = {
[COMMAND_PRINT] = {"-p", "--print",
"print entire history, with trimmed whitespace" },
[COMMAND_INFO] = {"-i", "--info",
"print entry number <n>, with original whitespace" },
[COMMAND_COPY] = {"-c", "--copy",
"copy entry number <n>, with original whitespace" },
[COMMAND_REMOVE] = {"-r", "--remove",
"remove entry number <n>" },
[COMMAND_SAVE] = {"-s", "--save",
"save history to $XDG_CACHE_HOME/clipsim/history" },
[COMMAND_DAEMON] = {"-d", "--daemon",
"spawn daemon (clipboard watcher and command fifo)" },
[COMMAND_HELP] = {"-h", "--help",
"print this help message" },
};
Entry entries[HISTORY_BUFFER_SIZE] = {0};
const char TEXT_TAG = (char) 0x01;
const char IMAGE_TAG = (char) 0x02;
mtx_t lock;
char *program;
static bool main_check_cmdline(char *);
static bool main_check_running(void);
static void main_usage(FILE *) __attribute__((noreturn));
static void main_launch_daemon(void) __attribute__((noreturn));
int
main(int argc, char *argv[]) {
DEBUG_PRINT("%d, %s", argc, argv[0]);
int32 id;
bool spell_error = true;
program = basename(argv[0]);
signal(SIGSEGV, util_segv_handler);
signal(SIGTERM, history_delete_tmp);
signal(SIGINT, history_delete_tmp);
if (argc <= 1 || argc >= 4)
main_usage(stderr);
for (uint i = 0; i < LENGTH(commands); i += 1) {
if (!strcmp(argv[1], commands[i].shortname)
|| !strcmp(argv[1], commands[i].longname)) {
spell_error = false;
switch (i) {
case COMMAND_PRINT:
ipc_client_speak_fifo(COMMAND_PRINT, 0);
break;
case COMMAND_INFO:
case COMMAND_COPY:
case COMMAND_REMOVE:
if ((argc != 3) || util_string_int32(&id, argv[2]) < 0)
main_usage(stderr);
ipc_client_speak_fifo(i, id);
break;
case COMMAND_SAVE:
ipc_client_speak_fifo(COMMAND_SAVE, 0);
break;
case COMMAND_DAEMON:
main_launch_daemon();
case COMMAND_HELP:
main_usage(stdout);
default:
main_usage(stderr);
}
}
}
if (spell_error)
main_usage(stderr);
exit(EXIT_SUCCESS);
}
void
main_usage(FILE *stream) {
DEBUG_PRINT("%p", (void *) stream);
fprintf(stream, "usage: %s COMMAND [n]\n", "clipsim");
fprintf(stream, "Available commands:\n");
for (uint i = 0; i < LENGTH(commands); i += 1) {
fprintf(stream, "%s | %-*s : %s\n",
commands[i].shortname, 8, commands[i].longname,
commands[i].description);
}
exit(stream != stdout);
}
bool
main_check_cmdline(char *pid) {
char buffer[256];
char command[256];
int n;
isize r;
int cmdline;
char cmd1[] = {'c', 'l', 'i', 'p', 's', 'i', 'm', '\0',
'-', 'd', '\0'};
char cmd2[] = {'c', 'l', 'i', 'p', 's', 'i', 'm', '\0',
'-', '-', 'd', 'a', 'e', 'm', 'o', 'n', '\0'};
n = snprintf(buffer, sizeof(buffer), "/proc/%s/cmdline", pid);
if (n < 0) {
error("Error printing buffer name.\n");
return false;
}
buffer[sizeof(buffer) - 1] = '\0';
if ((cmdline = open(buffer, O_RDONLY)) < 0)
return false;
if ((r = read(cmdline, command, sizeof(command))) <= 0) {
close(cmdline);
return false;
}
close(cmdline);
if (r == sizeof(cmd1)) {
if (!memcmp(command, cmd1, (usize) r))
return true;
} else if (r == sizeof(cmd2)) {
if (!memcmp(command, cmd2, (usize) r))
return true;
}
return false;
}
bool
main_check_running(void) {
DEBUG_PRINT("void");
DIR *processes;
struct dirent *process;
pid_t pid_this = getpid();
if ((processes = opendir("/proc")) == NULL) {
error("Error opening /proc: %s\n", strerror(errno));
return false;
}
while ((process = readdir(processes))) {
pid_t pid;
if ((pid = atoi(process->d_name)) <= 0)
continue;
if (pid == pid_this)
continue;
if (main_check_cmdline(process->d_name)) {
closedir(processes);
return true;
}
}
closedir(processes);
return false;
}
void
main_launch_daemon(void) {
DEBUG_PRINT("");
thrd_t ipc_thread;
int mtx_error;
if (main_check_running()) {
error("clipsim --daemon is already running.\n");
exit(EXIT_FAILURE);
}
if ((mtx_error = mtx_init(&lock, mtx_plain)) != thrd_success) {
error("Error initializing lock: %s\n", strerror(mtx_error));
exit(EXIT_FAILURE);
}
history_read();
thrd_create(&ipc_thread, ipc_daemon_listen_fifo, NULL);
clipboard_daemon_watch();
}