-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogcli.c
More file actions
447 lines (406 loc) · 12 KB
/
logcli.c
File metadata and controls
447 lines (406 loc) · 12 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Logbook: lets you write down in a simple way you thoughts that
* you have, while browsing through your file system.
* It save the date and your current path
*
* Written by Noah Bieler. Inspired by memoir
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <regex.h>
#include <error.h>
#include <errno.h>
#include "utils.h"
char usage[] = "l [log|help|dir REGEX|date REGEX|body REGEX] \n\n"
"This programm lets you log your thoughts together with the date \n"
"(YYYYMMDDhhmm) and the current working directory.\n"
"You can also search for specific date, directory or words in the log entry\n"
"with 'dir', 'date' or 'body'.\n"
"'help' prints this meassage.\n"
"No command logs all arguments written.\n\n"
"Written by Noah Bieler\n";
#define CONFFOLDER "logcli"
#define CONFFILE "logcli.conf"
#define LOGFILE "logcli.log"
#define STRINGSIZE 1024
#define Bool int
#define true 1
#define false 0
/*** Structures ***/
typedef struct files {
char * confdirname; /* directory with all relevant files */
DIR * confdir; /* directory with all relevant files */
FILE * conffile; /* File handler for the config file */
FILE * logfile; /* File handler for the log file */
} Files;
typedef enum command {
log, /* log into the file (default") */
oneliner, /* log a one liner into the file */
dir, /* search for a directory */
date, /* search for a date */
body, /* search in the logs */
help, /* Print help message */
} Command;
typedef struct regex {
regex_t comp_regex; /* The comiled regular expression */
char * myregex; /* The string with the regular expression */
} Regex;
typedef struct conf {
Files files;
Command command;
Regex regex;
} Conf;
/*** utils ***/
/**
* clean up the file pointers
*/
void
clean_conf(Files files){
if(files.confdirname){
free(files.confdirname);
}
if(files.conffile){
fclose(files.conffile);
}
if(files.logfile){
fclose(files.logfile);
}
if(files.confdir){
closedir(files.confdir);
}
}
/*** Initialization ***/
/**
* initializes all file handlers and config direcory
*/
int
init_conf(Conf * conf){
Bool firsttime = false;
char * home = getenv("XDG_CONFIG_HOME");
home = NULL;
if(home == NULL){
home = getenv("HOME");
if(home == NULL){
error(1, 0, "Could not obtain HOME variable!\n");
}
conf->files.confdirname = strdup(home);
cat(&conf->files.confdirname, "/.config");
} else {
conf->files.confdirname = strdup(home);
}
DEBUG("Home: %s\n", home);
cat(&(conf->files.confdirname), "/");
cat(&conf->files.confdirname, CONFFOLDER);
DEBUG("confdirname: %s\n", conf->files.confdirname);
conf->files.confdir = opendir(conf->files.confdirname);
if(conf->files.confdir == NULL){
if((conf->command != log) && (conf->command != oneliner)){
error(1, 0, "There are no log entries yet!\n");
}
mode_t proc_mask = umask(0);
if(mkdir(conf->files.confdirname,
S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH| S_IXOTH) != 0){
fprintf(stderr, "ERROR: Could not create configuration "
"directory %s\n", conf->files.confdirname);
exit(1);
}
firsttime = true;
umask(proc_mask);
}
char * filename;
filename = strdup(conf->files.confdirname);
cat(&filename, "/");
cat(&filename, CONFFILE);
DEBUG("conf file name: %s\n", filename);
if(firsttime){
conf->files.conffile = fopen(filename, "w");
fclose(conf->files.conffile);
}
conf->files.conffile = fopen(filename, "r");
free(filename);
filename = strdup(conf->files.confdirname);
cat(&filename, "/");
cat(&filename, LOGFILE);
DEBUG("log file name: %s\n", filename);
if((conf->command == log) || (conf->command == oneliner)){
conf->files.logfile = fopen(filename, "a");
DEBUG("Logfile with write access\n");
}
else {
conf->files.logfile = fopen(filename, "r");
DEBUG("Logfile with read access\n");
}
free(filename);
return 0;
}
/**
* Make the header
*/
char *
mkheader(char * header){
char * line;
line = (char *) xmalloc((STRINGSIZE + 1) * sizeof(char));
time_t cur_time;
struct tm *loctime;
cur_time = time(NULL);
loctime = localtime(&cur_time);
strftime(header, STRINGSIZE, "%Y%m%d%H%M", loctime);
if(getcwd(line, STRINGSIZE) == NULL){
error(1, 0, "Could not obtain current working directory");
}
DEBUG("cwd in mkheader: %s\n", line);
cat(&header, "\n");
cat(&header, line);
cat(&header, "\n");
free(line);
return header;
}
/* Commands */
/**
* Log the entry. Stop, when empty line is found
*/
int
log_entry(Conf * conf){
char * logentry;
char * line;
char * header;
int byte_read;
int bytes_sofar = 0;
size_t log_size = sizeof(char) * STRINGSIZE;
size_t line_size = sizeof(char) * STRINGSIZE;
logentry = (char *) xmalloc(line_size);
line = (char *) xmalloc(line_size);
header = (char *) xmalloc(line_size);
strcpy(logentry, "");
header = mkheader(header);
while(true){
byte_read = getline(&line, &line_size, stdin);
if(byte_read > 0){
bytes_sofar += byte_read;
if(byte_read < log_size){
cat(&logentry, line);
}
}
if(strcmp(line,"\n") == 0){
if(strcmp(logentry, "\n") != 0){
fprintf(conf->files.logfile, "%s", header);
fprintf(conf->files.logfile, "%s", logentry);
free(line);
free(header);
free(logentry);
return 0;
}
else {
printf("Nothing logged.\n");
free(line);
free(header);
free(logentry);
return 0;
}
}
}
// We should not be here
free(line);
free(header);
free(logentry);
return 1;
}
/**
* print the one liner
*/
int
log_oneline(int argc, char *argv[], Conf * conf){
char * logentry;
char * header;
logentry = strdup("");
header = (char *) xmalloc((STRINGSIZE + 1) * sizeof(char));
header = mkheader(header);
DEBUG("Logging a oneliner\n");
int i;
for(i = 1; i < argc; i++){
cat(&logentry, argv[i]);
cat(&logentry, " ");
}
DEBUG("header in oneliner:\n%s", header);
fprintf(conf->files.logfile, "%s", header);
fprintf(conf->files.logfile, "%s\n\n", logentry);
free(logentry);
free(header);
return 0;
}
/**
* Search for a date or a directory given by a regex
*/
int
search(Conf * conf){
int errcode;
Bool match = false;
int byte_read = 0;
int bytes_sofar;
int body_size;
size_t buffer_size = sizeof(char) * (STRINGSIZE + 1);
char * buffer;
char * date_str = NULL;
char * dir_str = NULL;
char * body_str;
buffer = (char *) xmalloc(buffer_size);
body_str = (char *) xmalloc(buffer_size);
strcpy(body_str, "");
body_str = (char *) xrealloc(body_str, buffer_size);
body_size = STRINGSIZE;
DEBUG("I am in search\n");
if((conf->files.logfile == NULL) && ((conf->command != log) || (conf->command != oneliner))){
error(1, 0, "No log file found!\n");
}
errcode = regcomp(&(conf->regex.comp_regex), conf->regex.myregex,
REG_ICASE);
if(errcode){
int new_size;
new_size = regerror(errcode, &(conf->regex.comp_regex),
buffer, buffer_size);
if(new_size > buffer_size){
xrealloc(buffer, new_size);
buffer_size = new_size;
new_size = regerror(errcode, &(conf->regex.comp_regex),
buffer, buffer_size);
}
regfree(&(conf->regex.comp_regex));
error(1, 0, "%s\n", buffer);
}
strcpy(buffer, "\n");
while((strcmp(buffer, "\n") == 0) && (byte_read != -1)){
byte_read = getline(&buffer, &buffer_size, conf->files.logfile);
}
while(byte_read != -1){
DEBUG("I am reading the file\n");
if(date_str){
free(date_str);
}
if(dir_str){
free(dir_str);
}
date_str = strdup(buffer);
byte_read = getline(&buffer, &buffer_size, conf->files.logfile);
dir_str = strdup(buffer);
bytes_sofar = 0;
byte_read = getline(&buffer, &buffer_size, conf->files.logfile);
bytes_sofar += byte_read;
while((strcmp(buffer, "\n") != 0) && (byte_read != -1)){
DEBUG("Reading the body\n");
if(bytes_sofar > body_size){
DEBUG("Reallocating memory\n");
body_size = bytes_sofar + STRINGSIZE;
xrealloc(body_str, body_size);
}
if(conf->command == body){
DEBUG("We search in the body\n");
if(regexec(&(conf->regex.comp_regex), buffer, 0, NULL, 0) == 0){
match = true;
}
}
cat(&body_str, buffer);
byte_read = getline(&buffer, &buffer_size, conf->files.logfile);
}
DEBUG("date: %s\n", date_str);
DEBUG("dir: %s\n", dir_str);
DEBUG("body: %s\n", body_str);
if(conf->command == dir){
if(regexec(&(conf->regex.comp_regex), dir_str, 0, NULL, 0) == 0){
match = true;
}
}
else if(conf->command == date){
if(regexec(&(conf->regex.comp_regex), date_str, 0, NULL, 0) == 0){
match = true;
}
}
if(match){
printf("%s", date_str);
printf("%s", dir_str);
printf("%s\n", body_str);
fflush(stdout);
match = false;
}
strcpy(body_str, "");
byte_read = getline(&buffer, &buffer_size, conf->files.logfile);
}
free(buffer);
free(date_str);
free(dir_str);
free(body_str);
regfree(&(conf->regex.comp_regex));
return 0;
}
/**
* Main
*/
int
main(int argc, char *argv[]){
Conf conf;
if(argc == 1){
conf.command = log;
}
else{
int num_arg = argc;
if(strcmp(argv[1],"log") == 0){
conf.command = log;
num_arg++;
}
else if((strcmp(argv[1],"date") == 0) || (strcmp(argv[1],"t") == 0)){
conf.command = date;
}
else if((strcmp(argv[1],"dir") == 0) || (strcmp(argv[1],"d") == 0)){
conf.command = dir;
}
else if((strcmp(argv[1],"body") == 0) || (strcmp(argv[1],"b") == 0)){
conf.command = body;
}
else if(strcmp(argv[1],"help") == 0){
conf.command = help;
num_arg++;
}
else {
conf.command = oneliner;
num_arg++;
}
if(num_arg == 2){
error(1, 0, "No regular expression given!");
}
}
if(conf.command == help){
puts(usage);
return 0;
}
if(init_conf(&conf)){
clean_conf(conf.files);
error(0, 0, "in setting configuration files!");
}
if(conf.command == log){
log_entry(&conf);
}
if(conf.command == oneliner){
log_oneline(argc, argv, &conf);
}
if((conf.command == date) || (conf.command == body) || (conf.command == dir)){
if(argc < 3){
error(0, 0, "No regular expression given");
}
if(argc > 3){
fprintf(stderr, "Too many regular expressions. "
"Will only consider first\n");
}
conf.regex.myregex = argv[2];
if(search(&conf)){
clean_conf(conf.files);
error(1, 0, "Couldn't search");
}
}
clean_conf(conf.files);
return 0;
}
/* EOF */