This commit is contained in:
choc 2024-08-13 12:25:26 +08:00
parent 25cb232842
commit 44ade79c6b
No known key found for this signature in database

127
ssm.c
View file

@ -31,7 +31,7 @@ get_database_path()
if (db[0] == '~') { if (db[0] == '~') {
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home == NULL) { if (home == NULL) {
fprintf(stderr, "HOME not defined"); fprintf(stderr, "HOME not defined\n");
return NULL; return NULL;
} }
db_path = malloc((strlen(db) + strlen(home)) * sizeof(char)); db_path = malloc((strlen(db) + strlen(home)) * sizeof(char));
@ -41,27 +41,28 @@ get_database_path()
} else { } else {
db_path = strdup(db); db_path = strdup(db);
} }
if (db_path == NULL) { return db_path;
return NULL;
} else {
return db_path;
}
} }
/* typedef enum {
* Convert timestmap to string FULL, // YYYY-MM-DD HH:MM:SS
* format: 0 - YYYY-MM-DD HH:MM:SS HHMM, // HH:MM
* format: 1 - HH:MM } datefmt;
*/
char * char *
convert_timestamp(time_t timestamp, int format) convert_timestamp(time_t timestamp, datefmt format)
{ {
struct tm *time_info = localtime(&timestamp); struct tm *time_info = localtime(&timestamp);
char *time_buf = malloc(20 * sizeof(char)); char *time_buf = malloc(20 * sizeof(char));
if (format == 0) { switch(format) {
case FULL:
strftime(time_buf, 20, "%Y-%m-%d %H:%M:%S", time_info); strftime(time_buf, 20, "%Y-%m-%d %H:%M:%S", time_info);
} else { break;
case HHMM:
strftime(time_buf, 6, "%H:%M", time_info); strftime(time_buf, 6, "%H:%M", time_info);
break;
default:
fprintf(stderr, "Invalid datefmt\n");
} }
return time_buf; return time_buf;
} }
@ -70,35 +71,33 @@ void
load_events(event **events, int *num_events) load_events(event **events, int *num_events)
{ {
char *db_path = get_database_path(); char *db_path = get_database_path();
if (db_path != NULL) { if (db_path == NULL)
FILE *file = fopen(db_path, "r"); return;
if (!file) { FILE *file = fopen(db_path, "r");
fprintf(stderr, "Cannot open database file\n"); if (!file) {
exit(EXIT_FAILURE); fprintf(stderr, "Cannot open database file: %s\n", db_path);
} exit(EXIT_FAILURE);
char line[MAX_LINE_LEN];
*num_events = 0;
while (fgets(line, sizeof(line), file)) {
(*num_events)++;
}
*events = malloc(sizeof(event) * (*num_events));
if (*events == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_SET);
int i = 0;
while (fgets(line, sizeof(line), file)) {
sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description);
i++;
}
fclose(file);
free(db_path);
} }
char line[MAX_LINE_LEN];
*num_events = 0;
while (fgets(line, sizeof(line), file)) {
(*num_events)++;
}
*events = malloc(sizeof(event) * (*num_events));
if (*events == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_SET);
for (int i = 0; fgets(line, sizeof(line), file); i++) {
sscanf(line, "%ld\t%[^\t]\t%[^\n]", &(*events)[i].timestamp, (*events)[i].name, (*events)[i].description);
}
fclose(file);
free(db_path);
} }
void void
@ -106,18 +105,14 @@ add_event(time_t timestamp, char *name, char *description)
{ {
char *db_path = get_database_path(); char *db_path = get_database_path();
FILE *file; FILE *file;
if (access(db_path, F_OK) != 0) { file = fopen(db_path, access(db_path, F_OK) ? "w" : "a");
file = fopen(db_path, "w"); if (!file) {
} else { perror("fopen");
file = fopen(db_path, "a"); exit(EXIT_FAILURE);
} }
if (!file) {
perror("fopen");
exit(EXIT_FAILURE);
}
fprintf(file, "%ld\t%s\t%s\n", timestamp, name, description); fprintf(file, "%ld\t%s\t%s\n", timestamp, name, description);
fclose(file); fclose(file);
free(db_path); free(db_path);
char *time_buf = convert_timestamp(timestamp, 0); char *time_buf = convert_timestamp(timestamp, 0);
printf("Added \"%s\" with description \"%s\" at \"%s\"\n", name, description, time_buf); printf("Added \"%s\" with description \"%s\" at \"%s\"\n", name, description, time_buf);
@ -165,7 +160,7 @@ list_events(event *events, int *num_events)
{ {
load_events(&events, num_events); load_events(&events, num_events);
for (int i = 0; i < *num_events; i++) { for (int i = 0; i < *num_events; i++) {
printf("Timestamp: %d\nName: %s\nDescription: %s\n\n", events[i].timestamp, events[i].name, events[i].description); printf("Timestamp: %ld\nName: %s\nDescription: %s\n\n", events[i].timestamp, events[i].name, events[i].description);
} }
free(events); free(events);
} }
@ -225,19 +220,19 @@ watch_file(event *events, int *num_events)
} }
static void static _Noreturn void
usage(int code) usage(int code)
{ {
fprintf(code ? stderr : stdout, fprintf(code ? stderr : stdout,
"Simple Scheduler Manager %s\n\n" "Simple Scheduler Manager " VERSION "\n\n"
"Usage: ssm <command>\n\n" "Usage: ssm <command>\n\n"
" help Show this help message\n" " help Show this help message\n"
" sched <time> <title> [description] Schedule an event\n" " sched <time> <title> [description] Schedule an event\n"
" edit Edit schedule with $EDITOR\n" " edit Edit schedule with $EDITOR\n"
" list <timerange> List all upcoming events\n" " list <timerange> List all upcoming events\n"
" search Search for events\n" " search Search for events\n"
" run Spawn notifier daemon\n" " run Spawn notifier daemon\n"
, VERSION); );
exit(code); exit(code);
} }
@ -264,17 +259,18 @@ main(int argc, char **argv)
} else if (strcmp(argv[1], "edit") == 0) { } else if (strcmp(argv[1], "edit") == 0) {
char *editor = getenv("EDITOR"); char *editor = getenv("EDITOR");
if (editor == NULL) { if (editor == NULL) {
fprintf(stderr, "EDITOR not defined"); fprintf(stderr, "EDITOR not defined\n");
return EXIT_FAILURE;
} }
char *db_path = get_database_path(); char *db_path = get_database_path();
char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char)); char *cmd = malloc((strlen(editor) + strlen(db_path) + 1) * sizeof(char));
if (cmd == NULL) { if (cmd == NULL) {
perror("malloc"); perror("malloc");
} }
sprintf(cmd, "%s %s", editor, db_path); sprintf(cmd, "%s %s", editor, db_path);
/* spawn $EDITOR */ /* spawn $EDITOR */
system(cmd); system(cmd);
free(cmd); free(cmd);
free(db_path); free(db_path);
} else if (strcmp(argv[1], "list") == 0) { } else if (strcmp(argv[1], "list") == 0) {
/* accept argv[2] as timerange */ /* accept argv[2] as timerange */
@ -284,9 +280,10 @@ main(int argc, char **argv)
} else if (strcmp(argv[1], "run") == 0) { } else if (strcmp(argv[1], "run") == 0) {
watch_file(events, &num_events); watch_file(events, &num_events);
} else if (strcmp(argv[1], "help") == 0) { } else if (strcmp(argv[1], "help") == 0) {
usage(1); usage(0);
}else { } else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
usage(1); usage(1);
} }
return EXIT_FAILURE; return EXIT_SUCCESS;
} }