rofi 1.7.8
history.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27#include "config.h"
28
29#include "history.h"
30#include "rofi.h"
31#include "settings.h"
32#include <errno.h>
33#include <glib.h>
34#include <glib/gstdio.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/types.h>
39#include <unistd.h>
40
44typedef struct __element {
46 long int index;
48 char *name;
50
51static int __element_sort_func(const void *ea, const void *eb,
52 void *data __attribute__((unused))) {
53 _element *a = *(_element **)ea;
54 _element *b = *(_element **)eb;
55 return b->index - a->index;
56}
57
58static void __history_write_element_list(FILE *fd, _element **list,
59 unsigned int length) {
60 if (list == NULL || length == 0) {
61 return;
62 }
63 // Sort the list before writing out.
64 g_qsort_with_data(list, length, sizeof(_element *), __element_sort_func,
65 NULL);
66
67 // Get minimum index.
68 int min_value = list[length - 1]->index;
69
70 // Set the max length of the list.
71 length =
72 (length > config.max_history_size) ? config.max_history_size : length;
73
74 // Write out entries.
75 for (unsigned int iter = 0; iter < length; iter++) {
76 fprintf(fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name);
77 }
78}
79
80static char **__history_get_element_list_fields(FILE *fd,
81 unsigned int *length) {
82 unsigned int real_length = 0;
83 char **retv = NULL;
84 ;
85 if (length == NULL) {
86 return NULL;
87 }
88 *length = 0;
89
90 if (fd == NULL) {
91 return NULL;
92 }
93 char *buffer = NULL;
94 size_t buffer_length = 0;
95 ssize_t l = 0;
96 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
97 // Jump to the first space.
98 const char *start = strchr(buffer, ' ');
99 // not found, skip.
100 if (start == NULL) {
101 continue;
102 }
103 start++;
104 // remove trailing \n
105 buffer[l - 1] = '\0';
106 if (real_length < (*length + 2)) {
107 real_length += 15;
108 // Resize and check.
109 retv = g_realloc(retv, (real_length) * sizeof(char *));
110 }
111 // Parse the number of times.
112 retv[(*length)] = g_strndup(start, l - 1 - (start - buffer));
113 // Force trailing '\0'
114 retv[(*length) + 1] = NULL;
115
116 (*length)++;
117 }
118 if (buffer_length > 0) {
119 g_free(buffer);
120 }
121 return retv;
122}
123
124static _element **__history_get_element_list(FILE *fd, unsigned int *length) {
125 unsigned int real_length = 0;
126 _element **retv = NULL;
127
128 if (length == NULL) {
129 return NULL;
130 }
131 *length = 0;
132
133 if (fd == NULL) {
134 return NULL;
135 }
136 char *buffer = NULL;
137 size_t buffer_length = 0;
138 ssize_t l = 0;
139 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
140 char *start = NULL;
141 // Skip empty lines.
142 if (l <= 1) {
143 continue;
144 }
145
146 long int index = strtol(buffer, &start, 10);
147 if (start == buffer || *start == '\0') {
148 continue;
149 }
150 start++;
151 if ((l - (start - buffer)) < 2) {
152 continue;
153 }
154 if (real_length < (*length + 2)) {
155 real_length += 15;
156 // Resize and check.
157 retv = g_realloc(retv, (real_length) * sizeof(_element *));
158 }
159
160 retv[(*length)] = g_malloc(sizeof(_element));
161
162 // remove trailing \n
163 buffer[l - 1] = '\0';
164 // Parse the number of times.
165 retv[(*length)]->index = index;
166 retv[(*length)]->name = g_strndup(start, l - 1 - (start - buffer));
167 // Force trailing '\0'
168 retv[(*length) + 1] = NULL;
169
170 (*length)++;
171 }
172 if (buffer != NULL) {
173 free(buffer);
174 buffer = NULL;
175 }
176 return retv;
177}
178
179void history_set(const char *filename, const char *entry) {
181 return;
182 }
183
184 // Check if program should be ignored
185 for (char *checked_prefix = strtok(config.ignored_prefixes, ";");
186 checked_prefix != NULL; checked_prefix = strtok(NULL, ";")) {
187 // For each ignored prefix
188
189 while (g_unichar_isspace(g_utf8_get_char(checked_prefix))) {
190 checked_prefix = g_utf8_next_char(
191 checked_prefix); // Some users will probably want "; " as their
192 // separator for aesthetics.
193 }
194
195 if (g_str_has_prefix(entry, checked_prefix)) {
196 return;
197 }
198 }
199
200 int found = 0;
201 unsigned int curr = 0;
202 unsigned int length = 0;
203 _element **list = NULL;
204 // Open file for reading and writing.
205 FILE *fd = g_fopen(filename, "r");
206 if (fd != NULL) {
207 // Get list.
208 list = __history_get_element_list(fd, &length);
209 // Close file, if fails let user know on stderr.
210 if (fclose(fd) != 0) {
211 g_warning("Failed to close history file: %s", g_strerror(errno));
212 }
213 }
214 // Look if the entry exists.
215 for (unsigned int iter = 0; !found && iter < length; iter++) {
216 if (strcmp(list[iter]->name, entry) == 0) {
217 curr = iter;
218 found = 1;
219 }
220 }
221
222 if (found) {
223 // If exists, increment list index number
224 list[curr]->index++;
225 } else {
226 // If not exists, add it.
227 // Increase list by one
228 list = g_realloc(list, (length + 2) * sizeof(_element *));
229 list[length] = g_malloc(sizeof(_element));
230 // Copy name
231 if (list[length] != NULL) {
232 list[length]->name = g_strdup(entry);
233 // set # hits
234 list[length]->index = 1;
235
236 length++;
237 list[length] = NULL;
238 }
239 }
240
241 fd = fopen(filename, "w");
242 if (fd == NULL) {
243 g_warning("Failed to open file: %s", g_strerror(errno));
244 } else {
245 // Write list.
246 __history_write_element_list(fd, list, length);
247 // Close file, if fails let user know on stderr.
248 if (fclose(fd) != 0) {
249 g_warning("Failed to close history file: %s", g_strerror(errno));
250 }
251 }
252 // Free the list.
253 for (unsigned int iter = 0; iter < length; iter++) {
254 g_free(list[iter]->name);
255 g_free(list[iter]);
256 }
257 g_free(list);
258}
259
260void history_remove(const char *filename, const char *entry) {
262 return;
263 }
264 _element **list = NULL;
265 int found = 0;
266 unsigned int curr = 0;
267 unsigned int length = 0;
268 // Open file for reading and writing.
269 FILE *fd = g_fopen(filename, "r");
270 if (fd == NULL) {
271 g_warning("Failed to open file: %s", g_strerror(errno));
272 return;
273 }
274 // Get list.
275 list = __history_get_element_list(fd, &length);
276
277 // Close file, if fails let user know on stderr.
278 if (fclose(fd) != 0) {
279 g_warning("Failed to close history file: %s", g_strerror(errno));
280 }
281 // Find entry.
282 for (unsigned int iter = 0; !found && iter < length; iter++) {
283 if (strcmp(list[iter]->name, entry) == 0) {
284 curr = iter;
285 found = 1;
286 }
287 }
288
289 // If found, remove it and write out new file.
290 if (found) {
291 // Remove the entry.
292 g_free(list[curr]->name);
293 g_free(list[curr]);
294 // Swap last to here (if list is size 1, we just swap empty sets).
295 list[curr] = list[length - 1];
296 // Empty last.
297 list[length - 1] = NULL;
298 length--;
299
300 fd = g_fopen(filename, "w");
301 // Clear list.
302 if (fd != NULL) {
303 // Write list.
304 __history_write_element_list(fd, list, length);
305 // Close file, if fails let user know on stderr.
306 if (fclose(fd) != 0) {
307 g_warning("Failed to close history file: %s", g_strerror(errno));
308 }
309 } else {
310 g_warning("Failed to open file: %s", g_strerror(errno));
311 }
312 }
313
314 // Free the list.
315 for (unsigned int iter = 0; iter < length; iter++) {
316 g_free(list[iter]->name);
317 g_free(list[iter]);
318 }
319 if (list != NULL) {
320 g_free(list);
321 }
322}
323
324char **history_get_list(const char *filename, unsigned int *length) {
325 *length = 0;
326
328 return NULL;
329 }
330 char **retv = NULL;
331 // Open file.
332 FILE *fd = g_fopen(filename, "r");
333 if (fd == NULL) {
334 // File that does not exists is not an error, so ignore it.
335 // Everything else? panic.
336 if (errno != ENOENT) {
337 g_warning("Failed to open file: %s", g_strerror(errno));
338 }
339 return NULL;
340 }
341 // Get list.
342 retv = __history_get_element_list_fields(fd, length);
343
344 // Close file, if fails let user know on stderr.
345 if (fclose(fd) != 0) {
346 g_warning("Failed to close history file: %s", g_strerror(errno));
347 }
348 return retv;
349}
void history_set(const char *filename, const char *entry)
Definition history.c:179
void history_remove(const char *filename, const char *entry)
Definition history.c:260
char ** history_get_list(const char *filename, unsigned int *length)
Definition history.c:324
struct __element _element
static char ** __history_get_element_list_fields(FILE *fd, unsigned int *length)
Definition history.c:80
static void __history_write_element_list(FILE *fd, _element **list, unsigned int length)
Definition history.c:58
static _element ** __history_get_element_list(FILE *fd, unsigned int *length)
Definition history.c:124
static int __element_sort_func(const void *ea, const void *eb, void *data __attribute__((unused)))
Definition history.c:51
Settings config
unsigned int disable_history
Definition settings.h:95
char * ignored_prefixes
Definition settings.h:97
unsigned int max_history_size
Definition settings.h:158
char * name
Definition history.c:48
long int index
Definition history.c:46