diff --git a/.gitignore b/.gitignore index 3ff478d..af39d48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ a.out .gitconfig +*.o diff --git a/src/Makefile b/src/Makefile index 5df0b27..d8b836f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,3 +1,22 @@ +CC = gcc +CFLAGS = -Wall + +# List of all source files (assuming they're all in the same directory) +SRC_FILES = $(wildcard *.c) + +# Generate a list of object files by replacing the .c extension with .o +OBJ_FILES = $(SRC_FILES:.c=.o) + +ics_analyzer: $(OBJ_FILES) + gcc -Wall $(OBJ_FILES) -o ics_analyzer + +main.o: main.c + $(CC) $(CFLAGS) -c $< + +# use implicit rule to compile C source files to object files +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + .PHONY:all all: gcc -Wall *.c @@ -10,15 +29,15 @@ debug: .PHONY:run run: gcc -Wall *.c - ./a.out + ./ics_analyzer .PHONY:install install: a.out - cp a.out /usr/local/bin/ics_analyzer + cp ics_analyzer /usr/local/bin/ics_analyzer .PHONY:clean clean: - -rm a.out + -rm ics_analyzer *.o .PHONY:uninstall uninstall: @@ -26,8 +45,8 @@ uninstall: .PHONY:test test: - ./a.out + ./ics_analyzer @echo - ./a.out -h + ./ics_analyzer -h @echo - ./a.out -f ../tests/calendar.ics + ./ics_analyzer -f ../tests/calendar.ics diff --git a/src/cli_arg_parsing.c b/src/cli_arg_parsing.c index 167ca6e..db0c35e 100644 --- a/src/cli_arg_parsing.c +++ b/src/cli_arg_parsing.c @@ -1,3 +1,4 @@ +#include "insert_event.h" #include "cli_arg_parsing.h" #include #include @@ -5,37 +6,37 @@ #include void usage() { - printf ("-h\tprint this help\n"); - printf ("-f\tspecify ics file path\n"); - exit(0); + printf ("-h\tprint this help\n"); + printf ("-f\tspecify ics file path\n"); + exit(0); } void get_cli_args(int argc, char **argv, char **file_name) { - int opt = 0; + int opt = 0; - memset(file_name, '\0', strlen(*file_name)); + memset(file_name, '\0', strlen(*file_name)); - if (argc < 2) { - char *home = getenv("HOME"); - *file_name = home; + char *home = getenv("HOME"); + *file_name = home; - if (home != NULL) { - strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics"); - } else { - printf ("Environment variable HOME is not set.\n"); - exit(1); - } - return; - } + if (home != NULL) { + strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics"); + } else { + printf ("Environment variable HOME is not set.\n"); + exit(1); + } - while ((opt = getopt(argc, argv, "f:h")) != -1) { - switch(opt) { - case 'f': - *file_name = optarg; - break; - case 'h': - usage(); - } - } + while ((opt = getopt(argc, argv, "f:hi")) != -1) { + switch(opt) { + case 'f': + *file_name = optarg; + break; + case 'h': + usage(); + break; + case 'i': + insert_event(*file_name); + break; + } + } } - diff --git a/src/insert_event.c b/src/insert_event.c new file mode 100644 index 0000000..75bbace --- /dev/null +++ b/src/insert_event.c @@ -0,0 +1,33 @@ +#include "insert_event.h" +#include +#include +#include +#include +#include + +void insert_event(char *file_name) { + int myfd = open(file_name, O_APPEND | O_WRONLY); + + char summary_buf[256] = "SUMMARY:"; + + char *input_buffer = &summary_buf[8]; + + printf("Insert a new event\n"); + printf("SUMMARY: "); + fgets (input_buffer, (sizeof(summary_buf)-strlen(summary_buf)), stdin); + + if (strchr(input_buffer, '\n') == NULL) + printf ("The input has been truncated to:\n%s\n", input_buffer); + + // modify the string to have \r\n line ending + summary_buf[strlen(summary_buf)+1] = '\0'; + summary_buf[strlen(summary_buf)-1] = '\r'; + summary_buf[strlen(summary_buf)] = '\n'; + + write(myfd, "BEGIN:VEVENT\r\n", strlen("BEGIN:VEVENT\r\n")); + write(myfd, summary_buf, strlen(summary_buf)); + + close(myfd); + + exit(0); +} diff --git a/src/insert_event.h b/src/insert_event.h new file mode 100644 index 0000000..563dec0 --- /dev/null +++ b/src/insert_event.h @@ -0,0 +1,3 @@ +#pragma once + +void insert_event(char *file_name); diff --git a/src/main.c b/src/main.c index 74902f8..07fe3a7 100644 --- a/src/main.c +++ b/src/main.c @@ -14,8 +14,8 @@ #include int main(int argc, char **argv) { - char *ics_path = ""; - get_cli_args(argc, argv, &ics_path); + char *ics_path = ""; + get_cli_args(argc, argv, &ics_path); char my_line[4096] = "";