summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAriadna Vigo <arivigodr@gmail.com>2020-08-09 19:45:17 +0200
committerAriadna Vigo <arivigodr@gmail.com>2020-08-09 19:45:17 +0200
commitea391d8c8f0a8dcc2ff2220ee24c6cba4884c5aa (patch)
tree27b58b26812f886a2a275d77cde76244d255e65f
parenta72a5c9685b0c45d8b45c94bb4ec069ecc22f9a5 (diff)
Split up new file and appending new tasks into -n, -a
-rw-r--r--README.md8
-rw-r--r--cras.111
-rw-r--r--cras.c36
3 files changed, 38 insertions, 17 deletions
diff --git a/README.md b/README.md
index b62c23c..ad7c61e 100644
--- a/README.md
+++ b/README.md
@@ -37,12 +37,14 @@ Tasks due for: Sat Jun 20 15:57:28 2020
To set a task list, pass the -n option and the name of the file that will hold
the list. The tasks will be read from standard input, each line being a new
-task. Cras stops reading when it reaches EOF. The same -n flag allows for
-adding new tasks if the file already exists.
-
+task. Cras stops reading when it reaches EOF.
+
The -t and -T, followed by the task number, mark the task as done or pending,
respectively.
+Adding new tasks to an already existing file is possible by using the -a
+option.
+
For further usage information, please refer to the cras(1) manual page.
## Install
diff --git a/cras.1 b/cras.1
index a12ca60..f3940cb 100644
--- a/cras.1
+++ b/cras.1
@@ -5,7 +5,7 @@ Cras - The Anti-Procrastination Tool
.SH SYNOPSIS
.PP
.B cras
-.RB [ \-nov ]
+.RB [ \-anov ]
.RB [ \-tT
.IR num ]
.I file
@@ -17,10 +17,15 @@ default) and doesn't allow you to edit the task list after set up, except for
marking a task as done.
.SH OPTIONS
.TP
+.B \-a
+Append new tasks to
+.I file.
+(Be aware that this doesn't update the expiration date.)
+.TP
.B \-n
-Add new tasks to
+Create new
.I file,
-creating it if it doesn't exist.
+overwriting previous contents if it already exists.
.TP
.B \-o
Switch to short-form output.
diff --git a/cras.c b/cras.c
index 1c7e4c6..7e63212 100644
--- a/cras.c
+++ b/cras.c
@@ -22,6 +22,7 @@ enum {
enum {
DEF_MODE,
+ APP_MODE,
NEW_MODE,
OUT_MODE,
MARK_MODE
@@ -37,7 +38,7 @@ static void write_crasfile(const char *crasfile, TaskLst list);
static int store_input(TaskLst *list, FILE *fp);
static void usage(void);
-static void new_mode(const char *crasfile);
+static void input_mode(const char *crasfile, int append);
static void output_mode(const char *crasfile, int mode);
static void mark_list_mode(const char *crasfile, const char *id, int value);
@@ -172,7 +173,8 @@ store_input(TaskLst *list, FILE *fp)
char linebuf[TASK_LST_DESC_MAX_SIZE];
while (feof(fp) == 0) {
- fgets(linebuf, TASK_LST_DESC_MAX_SIZE, fp);
+ if (fgets(linebuf, TASK_LST_DESC_MAX_SIZE, fp) == NULL)
+ break;
linebuf[strlen(linebuf) - 1] = '\0'; /* Chomp '\n' */
if (task_lst_add_task(list, TASK_TODO, linebuf) < 0)
@@ -185,23 +187,27 @@ store_input(TaskLst *list, FILE *fp)
static void
usage(void)
{
- die("usage: cras [-nov] [-tT num] file");
+ die("usage: cras [-anov] [-tT num] file");
}
static void
-new_mode(const char *crasfile)
+input_mode(const char *crasfile, int append)
{
- int file_exists;
TaskLst list;
task_lst_init(&list);
- file_exists = read_crasfile(&list, crasfile);
+ if (append > 0)
+ read_crasfile(&list, crasfile);
- if (store_input(&list, stdin) < 0)
- fprintf(stderr, "Warning: Task file already full.\n");
-
- if (file_exists < 0) /* Only set if this is a new file */
+ if (store_input(&list, stdin) < 0) {
+ task_lst_cleanup(&list);
+ die("Internal memory error.");
+ }
+
+ /* Only set a new expiration date if creating a new file */
+ if (append == 0)
task_lst_set_expiration(&list, crasfile_expiry);
+
write_crasfile(crasfile, list);
task_lst_cleanup(&list);
@@ -269,6 +275,11 @@ main(int argc, char *argv[])
mode = DEF_MODE;
ARGBEGIN {
+ case 'a':
+ if (mode != DEF_MODE)
+ usage();
+ mode = APP_MODE;
+ break;
case 'n':
if (mode != DEF_MODE)
usage();
@@ -305,8 +316,11 @@ main(int argc, char *argv[])
usage();
switch (mode) {
+ case APP_MODE:
+ input_mode(argv[0], 1);
+ return 0;
case NEW_MODE:
- new_mode(argv[0]);
+ input_mode(argv[0], 0);
return 0;
case OUT_MODE:
output_mode(argv[0], SHORT_OUTPUT);