diff options
Diffstat (limited to '')
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | README.md | 60 | ||||
| -rw-r--r-- | sdep-0.2/LICENSE | 21 | ||||
| -rw-r--r-- | sdep-0.2/Makefile | 61 | ||||
| -rw-r--r-- | sdep-0.2/sdep.1 | 94 | ||||
| -rw-r--r-- | sdep-0.2/sdep.c | 277 |
6 files changed, 34 insertions, 481 deletions
| @@ -1,2 +1,2 @@ | |||
| 1 | sdep | 1 | sdep |
| 2 | sdep-*.tar.gz | 2 | sdep-* |
| @@ -1,26 +1,30 @@ | |||
| 1 | # sdep | 1 | # sdep |
| 2 | |||
| 2 | A simple "date+event" line parser. | 3 | A simple "date+event" line parser. |
| 3 | 4 | ||
| 4 | sdep follows the UNIX philosphy (do one thing well, use stdin and stdout) and | 5 | sdep follows the UNIX philosphy (do one thing well, use stdin and stdout) |
| 5 | is heavily inspired by [suckless](https://suckless.org) utilities such as | 6 | and is heavily inspired by [suckless](https://suckless.org) utilities |
| 6 | [dmenu](https://tools.suckless.org/dmenu/). | 7 | such as [dmenu](https://tools.suckless.org/dmenu/). |
| 7 | 8 | ||
| 8 | You can wrap it around shell scripts to turn it into a no-nonsense calendar | 9 | You can wrap it around shell scripts to turn it into a no-nonsense |
| 9 | system. | 10 | calendar system. |
| 10 | 11 | ||
| 11 | ## Description | 12 | ## Description |
| 12 | 13 | ||
| 13 | sdep reads lines of the form `date text` from stdin and writes to stdout those | 14 | sdep reads lines of the form `date text` from stdin and writes to stdout |
| 14 | lines such that `date` is between the two dates specified with the `-f` and | 15 | those lines such that `date` is between the two dates specified with the |
| 15 | `-t` options, both of which default to the current minute. | 16 | `-f` and `-t` options, both of which default to the current minute. |
| 16 | 17 | ||
| 17 | The format for `date` can be specified with the same syntax as for date(1). | 18 | The format for `date` can be specified with the same syntax as for |
| 18 | The dates should correspond to a unique minute in time. | 19 | date(1). The dates should correspond to a unique minute in time. |
| 19 | 20 | ||
| 20 | ## Installation | 21 | ## Installation |
| 21 | Edit the Makefile to match your local configuration and type `make install`. | 22 | |
| 23 | Edit the Makefile to match your local configuration and type `make | ||
| 24 | install`. | ||
| 22 | 25 | ||
| 23 | ## Examples | 26 | ## Examples |
| 27 | |||
| 24 | If `events.txt` contains lines formatted as `date text` then | 28 | If `events.txt` contains lines formatted as `date text` then |
| 25 | 29 | ||
| 26 | ``` | 30 | ``` |
| @@ -39,25 +43,26 @@ will print all the lines whose date is in the past, while | |||
| 39 | sdep -t -w "%A" <events.txt | 43 | sdep -t -w "%A" <events.txt |
| 40 | ``` | 44 | ``` |
| 41 | 45 | ||
| 42 | will print all lines whose date is in the future, showing only the day of the | 46 | will print all lines whose date is in the future, showing only the day |
| 43 | week and the text. | 47 | of the week and the text. |
| 44 | 48 | ||
| 45 | ``` | 49 | ``` |
| 46 | sdep -f "1999-01-01 00:00" -t "1999-12-31 23:59" -w "" <events.txt | 50 | sdep -f "1999-01-01 00:00" -t "1999-12-31 23:59" -w "" <events.txt |
| 47 | ``` | 51 | ``` |
| 48 | 52 | ||
| 49 | will show only the `text` of all lines with a date in 1999. You can specify a | 53 | will show only the `text` of all lines with a date in 1999. You can |
| 50 | different format for the dates, for example | 54 | specify a different format for the dates, for example |
| 51 | 55 | ||
| 52 | ``` | 56 | ``` |
| 53 | sdep +"%m/%d/%Y %I:%M%p" -t "12/31/2020 11:59pm" -w "" <events.txt | 57 | sdep +"%m/%d/%Y %I:%M%p" -t "12/31/2020 11:59pm" -w "" <events.txt |
| 54 | ``` | 58 | ``` |
| 55 | 59 | ||
| 56 | will match all dates from December 31st, 2020, one minute before midnight | 60 | will match all dates from December 31st, 2020, one minute before midnight |
| 57 | (included). Note: this only works if your locale has an am/pm format, see | 61 | (included). Note: this only works if your locale has an am/pm format, |
| 58 | date(1). | 62 | see date(1). |
| 59 | 63 | ||
| 60 | ## A stupidly simple calendar app | 64 | ## A stupidly simple calendar app |
| 65 | |||
| 61 | If you keep your events and reminders in a simple plain text file (say | 66 | If you keep your events and reminders in a simple plain text file (say |
| 62 | events.txt), you can run | 67 | events.txt), you can run |
| 63 | 68 | ||
| @@ -65,12 +70,12 @@ events.txt), you can run | |||
| 65 | sdep -w "" -s "" | while read text; do notify-send "$text"; done < events.txt | 70 | sdep -w "" -s "" | while read text; do notify-send "$text"; done < events.txt |
| 66 | ``` | 71 | ``` |
| 67 | 72 | ||
| 68 | every minute, for example using cron(8), to get a notification every time | 73 | every minute, for example using cron(8), to get a notification every |
| 69 | an events is happens. | 74 | time an events is happens. |
| 70 | 75 | ||
| 71 | You can use `sdep -f -t < events.txt` to list all your events, or | 76 | You can use `sdep -f -t < events.txt` to list all your events, or |
| 72 | `sdep -t < events.txt` to list only the future ones. You can specify any date | 77 | `sdep -t < events.txt` |
| 73 | range. Running | 78 | to list only the future ones. You can specify any date range. Running |
| 74 | 79 | ||
| 75 | ``` | 80 | ``` |
| 76 | temp = $(mktemp) | 81 | temp = $(mktemp) |
| @@ -80,18 +85,19 @@ mv "$temp" events.txt | |||
| 80 | 85 | ||
| 81 | will remove all old events from your file. | 86 | will remove all old events from your file. |
| 82 | 87 | ||
| 83 | You can edit your events using any text editor and you can keep them synced | 88 | You can edit your events using any text editor and you can |
| 84 | between multiple devices using something like | 89 | keep them synced between multiple devices using something like |
| 85 | [rsync](https://rsync.samba.org/). | 90 | [rsync](https://rsync.samba.org/). |
| 86 | 91 | ||
| 87 | ## Scripts | 92 | ## Scripts |
| 93 | |||
| 88 | The `scripts` folder contains the few scripts that I use. They are | 94 | The `scripts` folder contains the few scripts that I use. They are |
| 89 | basically just a more elaborate version of the calendar system described | 95 | basically just a more elaborate version of the calendar system described |
| 90 | above, with support for recurring events (e.g. weekly, daily). You | 96 | above, with support for recurring events (e.g. weekly, daily). You |
| 91 | can install them with `sudo SD=/path/to/scripts/folder make scripts`, | 97 | can install them with `sudo SD=/path/to/scripts/folder make scripts`, |
| 92 | where `SD` specifies the path where the directory where you want your sdep | 98 | where `SD` specifies the path where the directory where you want your |
| 93 | files to be saved; for example it can be `/home/username/.sdep`. | 99 | sdep files to be saved; for example it can be `/home/username/.sdep`. |
| 94 | For example check that the folder SDEPDATA in Makefile suits you. | 100 | For example check that the folder SDEPDATA in Makefile suits you. |
| 95 | 101 | ||
| 96 | Most of the scripts rely on the `-d` option of the GNU date utility, so you | 102 | Most of the scripts rely on the `-d` option of the GNU date utility, |
| 97 | should change that too if you are on a BSD system or on MacOS. | 103 | so you should change that too if you are on a BSD system or on MacOS. |
diff --git a/sdep-0.2/LICENSE b/sdep-0.2/LICENSE deleted file mode 100644 index b47cbe9..0000000 --- a/sdep-0.2/LICENSE +++ /dev/null | |||
| @@ -1,21 +0,0 @@ | |||
| 1 | MIT License | ||
| 2 | |||
| 3 | Copyright (c) 2021-2023 Sebastiano Tronto | ||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | of this software and associated documentation files (the "Software"), to deal | ||
| 7 | in the Software without restriction, including without limitation the rights | ||
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | copies of the Software, and to permit persons to whom the Software is | ||
| 10 | furnished to do so, subject to the following conditions: | ||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all | ||
| 13 | copies or substantial portions of the Software. | ||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | SOFTWARE. | ||
diff --git a/sdep-0.2/Makefile b/sdep-0.2/Makefile deleted file mode 100644 index 444c6f2..0000000 --- a/sdep-0.2/Makefile +++ /dev/null | |||
| @@ -1,61 +0,0 @@ | |||
| 1 | # See LICENSE file for copyright and license details. | ||
| 2 | |||
| 3 | VERSION = 0.2 | ||
| 4 | |||
| 5 | PREFIX = /usr/local | ||
| 6 | MANPREFIX = ${PREFIX}/share/man | ||
| 7 | SCRIPTS = sdep-add sdep-checknow sdep-checkpast sdep-clear sdep-edit sdep-list | ||
| 8 | |||
| 9 | CPPFLAGS = -D_XOPEN_SOURCE=700 -DVERSION=\"${VERSION}\" | ||
| 10 | CFLAGS = -pedantic -Wall -Os ${CPPFLAGS} | ||
| 11 | DBGFLAGS = -pedantic -Wall -Wextra \ | ||
| 12 | -fsanitize=address -fsanitize=undefined ${CPPFLAGS} | ||
| 13 | |||
| 14 | CC = cc | ||
| 15 | |||
| 16 | |||
| 17 | all: options sdep | ||
| 18 | |||
| 19 | options: | ||
| 20 | @echo sdep build options: | ||
| 21 | @echo "CFLAGS = ${CFLAGS}" | ||
| 22 | @echo "CC = ${CC}" | ||
| 23 | |||
| 24 | sdep: | ||
| 25 | ${CC} ${CFLAGS} -o sdep sdep.c | ||
| 26 | |||
| 27 | debug: | ||
| 28 | ${CC} ${CFLAGS} -o sdep sdep.c | ||
| 29 | |||
| 30 | clean: | ||
| 31 | rm -rf sdep sdep-${VERSION}.tar.gz | ||
| 32 | |||
| 33 | dist: clean | ||
| 34 | mkdir -p sdep-${VERSION} | ||
| 35 | cp -R LICENSE Makefile README sdep.1 sdep.c sdep-${VERSION} | ||
| 36 | tar -cf sdep-${VERSION}.tar sdep-${VERSION} | ||
| 37 | gzip sdep-${VERSION}.tar | ||
| 38 | rm -rf sdep-${VERSION} | ||
| 39 | |||
| 40 | install: all | ||
| 41 | mkdir -p ${DESTDIR}${PREFIX}/bin | ||
| 42 | cp -f sdep ${DESTDIR}${PREFIX}/bin/sdep | ||
| 43 | chmod 755 ${DESTDIR}${PREFIX}/bin/sdep | ||
| 44 | mkdir -p ${DESTDIR}${MANPREFIX}/man1 | ||
| 45 | sed "s/VERSION/${VERSION}/g" < sdep.1 \ | ||
| 46 | > ${DESTDIR}${MANPREFIX}/man1/sdep.1 | ||
| 47 | chmod 644 ${DESTDIR}${MANPREFIX}/man1/sdep.1 | ||
| 48 | |||
| 49 | scripts: | ||
| 50 | for s in ${SCRIPTS}; do\ | ||
| 51 | sed "s|SDEPDATA|${SD}|g" < scripts/$$s > \ | ||
| 52 | ${DESTDIR}${PREFIX}/bin/$$s ; \ | ||
| 53 | chmod 755 ${DESTDIR}${PREFIX}/bin/$$s ;\ | ||
| 54 | done | ||
| 55 | |||
| 56 | uninstall: | ||
| 57 | rm -rf ${DESTDIR}${PREFIX}/bin/sdep ${DESTDIR}${MANPREFIX}/man1/sdep.1 | ||
| 58 | for s in ${SCRIPTS}; do rm -rf ${DESTDIR}${PREFIX}/bin/$$s; done | ||
| 59 | |||
| 60 | .PHONY: all options debug clean dist install scripts uninstall | ||
| 61 | |||
diff --git a/sdep-0.2/sdep.1 b/sdep-0.2/sdep.1 deleted file mode 100644 index 302c45d..0000000 --- a/sdep-0.2/sdep.1 +++ /dev/null | |||
| @@ -1,94 +0,0 @@ | |||
| 1 | .Dd May 13, 2021 | ||
| 2 | .Dt SDEP 1 | ||
| 3 | .Os | ||
| 4 | .Sh NAME | ||
| 5 | .Nm sdep | ||
| 6 | .Nd a simple "date+event" line parser | ||
| 7 | |||
| 8 | .Sh SYNOPSIS | ||
| 9 | .Nm | ||
| 10 | .Op Fl dv | ||
| 11 | .Op Ar +format | ||
| 12 | .Op Fl f Op Ar date | ||
| 13 | .Op Fl t Op Ar date | ||
| 14 | .Op Fl w Ar format | ||
| 15 | .Op Fl s Ar string | ||
| 16 | |||
| 17 | .Sh DESCRIPTION | ||
| 18 | .Nm | ||
| 19 | reads lines of the form | ||
| 20 | |||
| 21 | .Dl Ar "date text" | ||
| 22 | |||
| 23 | from stdin and writes to stdout those lines such that | ||
| 24 | .Ar date | ||
| 25 | is between the dates specified by the | ||
| 26 | .Fl f | ||
| 27 | and | ||
| 28 | .Fl t | ||
| 29 | options, both defaulting to the current minute. | ||
| 30 | The dates should correspond to a unique minute in time. The format for | ||
| 31 | .Ar date | ||
| 32 | can be specified with the same syntax as for | ||
| 33 | .Xr date 1 . | ||
| 34 | |||
| 35 | The options are as follows: | ||
| 36 | .Bl -tag -width Ds | ||
| 37 | .It Fl d | ||
| 38 | print the default date format and exit. | ||
| 39 | .It Fl f Op Ar date | ||
| 40 | initial date for the range (default: current minute). If | ||
| 41 | .Ar date | ||
| 42 | is not specified there will be no lower bound for the dates. | ||
| 43 | .It Fl s Ar string | ||
| 44 | change the string that separates the date from the text in the output | ||
| 45 | lines (deafult: "\t"). | ||
| 46 | .It Fl t Op Ar date | ||
| 47 | final date for the range (default: current minute). If | ||
| 48 | .Ar date | ||
| 49 | is not specified then there will be no upper bound for the dates. | ||
| 50 | .It Fl v | ||
| 51 | print version information and exit. | ||
| 52 | .It Fl w Ar format | ||
| 53 | change the format in which the date is written in the output lines. | ||
| 54 | |||
| 55 | .Sh EXAMPLES | ||
| 56 | If | ||
| 57 | .Ar events.txt | ||
| 58 | contains lines formatted as | ||
| 59 | .Ar "date text" | ||
| 60 | then | ||
| 61 | |||
| 62 | .Dl sdep -f <events.txt | ||
| 63 | |||
| 64 | will print all the lines whose date is in the past, while | ||
| 65 | |||
| 66 | .Dl sdep -t -w "%A" <events.txt | ||
| 67 | |||
| 68 | will print all lines whose date is in the future, showing only the day of the | ||
| 69 | week and the text. | ||
| 70 | |||
| 71 | .Dl sdep -f "1999-01-01 00:00" -t "1999-12-31 23:59" -w "" <events.txt | ||
| 72 | |||
| 73 | will show only the | ||
| 74 | .Ar text | ||
| 75 | of all lines with a date in 1999. You can specify a different format for the | ||
| 76 | dates, for example | ||
| 77 | |||
| 78 | .Dl sdep +"%m/%d/%Y %I:%M%p" -t "12/31/2020 11:59pm" -w "" <events.txt | ||
| 79 | |||
| 80 | will match all dates from December 31st, 2020, one minute before midnight | ||
| 81 | (included). Note: this only works if your locale has an am/pm format, see | ||
| 82 | .Xr date 1 . | ||
| 83 | |||
| 84 | .Sh AUTHORS | ||
| 85 | .An Sebastiano Tronto Aq Mt sebastiano.tronto@gmail.com | ||
| 86 | |||
| 87 | .Sh SOURCE CODE | ||
| 88 | Source code is available at | ||
| 89 | .Lk https://github.com/sebastianotronto/sdep | ||
| 90 | |||
| 91 | .Sh SEE ALSO | ||
| 92 | .Xr date 1 , | ||
| 93 | .Xr strftime 3 , | ||
| 94 | .Xr strptime 3 | ||
diff --git a/sdep-0.2/sdep.c b/sdep-0.2/sdep.c deleted file mode 100644 index 629387b..0000000 --- a/sdep-0.2/sdep.c +++ /dev/null | |||
| @@ -1,277 +0,0 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <time.h> | ||
| 7 | |||
| 8 | /* | ||
| 9 | * Maximum number of characters in a line. The rest will be truncated. | ||
| 10 | * Change this if you need very long lines. | ||
| 11 | */ | ||
| 12 | #define MAXLEN 10000 | ||
| 13 | |||
| 14 | /* | ||
| 15 | * Default date format. Anything that strftime(3) understands works, but | ||
| 16 | * it should determine a date completely up to the minute. | ||
| 17 | */ | ||
| 18 | static char *default_format = "%Y-%m-%d %H:%M"; | ||
| 19 | |||
| 20 | |||
| 21 | typedef struct Event Event; | ||
| 22 | typedef struct EventList EventList; | ||
| 23 | typedef struct EventNode EventNode; | ||
| 24 | typedef struct Options Options; | ||
| 25 | |||
| 26 | struct Event{ | ||
| 27 | struct tm time; | ||
| 28 | char *text; | ||
| 29 | }; | ||
| 30 | |||
| 31 | struct EventList { | ||
| 32 | EventNode *first; | ||
| 33 | EventNode *last; | ||
| 34 | int count; | ||
| 35 | }; | ||
| 36 | |||
| 37 | struct EventNode { | ||
| 38 | Event ev; | ||
| 39 | EventNode *next; | ||
| 40 | }; | ||
| 41 | |||
| 42 | struct Options { | ||
| 43 | struct tm from; | ||
| 44 | struct tm to; | ||
| 45 | char *format_in; | ||
| 46 | char *format_out; | ||
| 47 | char *separator; | ||
| 48 | }; | ||
| 49 | |||
| 50 | static void add_event(struct tm, char *, EventList *); | ||
| 51 | static int compare_tm(const void *, const void *); | ||
| 52 | static int compare_event(const void *, const void *); | ||
| 53 | static Options default_op(void); | ||
| 54 | static int events_in_range(EventList *, Options, Event *); | ||
| 55 | static char *format_line(Event, Options, char *); | ||
| 56 | static int is_space(char); | ||
| 57 | static void read_input(Options, EventList *); | ||
| 58 | static Options read_op(int, char *[]); | ||
| 59 | static void str_copy(char *, char *, int); | ||
| 60 | static char *str_trim(char *); | ||
| 61 | static void write_output(Options, Event *, int); | ||
| 62 | |||
| 63 | |||
| 64 | static void | ||
| 65 | add_event(struct tm t, char *text, EventList *evlist) | ||
| 66 | { | ||
| 67 | size_t l = strlen(text)+1; | ||
| 68 | EventNode *next = malloc(sizeof(EventNode)); | ||
| 69 | |||
| 70 | next->ev.time = t; | ||
| 71 | next->ev.text = malloc(l); | ||
| 72 | str_copy(next->ev.text, text, l); | ||
| 73 | next->ev.text = str_trim(next->ev.text); | ||
| 74 | next->next = NULL; | ||
| 75 | |||
| 76 | if (++evlist->count == 1) { | ||
| 77 | evlist->first = next; | ||
| 78 | evlist->last = next; | ||
| 79 | } else { | ||
| 80 | evlist->last->next = next; | ||
| 81 | evlist->last = next; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | static int | ||
| 86 | compare_tm(const void *v1, const void *v2) | ||
| 87 | { | ||
| 88 | const struct tm *t1 = v1; | ||
| 89 | const struct tm *t2 = v2; | ||
| 90 | |||
| 91 | if (t1->tm_year != t2->tm_year) | ||
| 92 | return t1->tm_year - t2->tm_year; | ||
| 93 | if (t1->tm_mon != t2->tm_mon) | ||
| 94 | return t1->tm_mon - t2->tm_mon; | ||
| 95 | if (t1->tm_mday != t2->tm_mday) | ||
| 96 | return t1->tm_mday - t2->tm_mday; | ||
| 97 | if (t1->tm_hour != t2->tm_hour) | ||
| 98 | return t1->tm_hour - t2->tm_hour; | ||
| 99 | return t1->tm_min - t2->tm_min; | ||
| 100 | } | ||
| 101 | |||
| 102 | static int | ||
| 103 | compare_event(const void *v1, const void *v2) | ||
| 104 | { | ||
| 105 | const Event *ev1 = v1; | ||
| 106 | const Event *ev2 = v2; | ||
| 107 | |||
| 108 | return compare_tm(&ev1->time, &ev2->time); | ||
| 109 | } | ||
| 110 | |||
| 111 | static Options | ||
| 112 | default_op(void) | ||
| 113 | { | ||
| 114 | Options op; | ||
| 115 | time_t t_now = time(NULL); | ||
| 116 | struct tm *now = localtime(&t_now); | ||
| 117 | |||
| 118 | op.format_in = malloc(MAXLEN); | ||
| 119 | op.format_out = malloc(MAXLEN); | ||
| 120 | op.separator = malloc(MAXLEN); | ||
| 121 | strcpy(op.format_in, default_format); | ||
| 122 | strcpy(op.format_out, default_format); | ||
| 123 | strcpy(op.separator, "\t"); | ||
| 124 | op.from = *now; | ||
| 125 | op.to = *now; | ||
| 126 | |||
| 127 | return op; | ||
| 128 | } | ||
| 129 | |||
| 130 | /* | ||
| 131 | * Saves the events in ev[] that happen between op->from and op->to in sel[] | ||
| 132 | * sorted by date and returns their number. | ||
| 133 | */ | ||
| 134 | static int | ||
| 135 | events_in_range(EventList *evlist, Options op, Event *sel) | ||
| 136 | { | ||
| 137 | EventNode *i; | ||
| 138 | int n = 0; | ||
| 139 | |||
| 140 | for (i = evlist->first; i != NULL; i = i->next) | ||
| 141 | if (compare_tm(&i->ev.time, &op.from) >= 0 && | ||
| 142 | compare_tm(&i->ev.time, &op.to) <= 0) | ||
| 143 | sel[n++] = i->ev; | ||
| 144 | |||
| 145 | qsort(sel, n, sizeof(Event), compare_event); | ||
| 146 | |||
| 147 | return n; | ||
| 148 | } | ||
| 149 | |||
| 150 | static char * | ||
| 151 | format_line(Event ev, Options op, char *out) | ||
| 152 | { | ||
| 153 | size_t l; | ||
| 154 | |||
| 155 | strftime(out, MAXLEN, op.format_out, &ev.time); | ||
| 156 | l = strlen(out); | ||
| 157 | |||
| 158 | str_copy(out+l, op.separator, MAXLEN - l); | ||
| 159 | l = strlen(out); | ||
| 160 | |||
| 161 | str_copy(out+l, ev.text, MAXLEN - l); | ||
| 162 | |||
| 163 | return out; | ||
| 164 | } | ||
| 165 | |||
| 166 | static int | ||
| 167 | is_space(char c) | ||
| 168 | { | ||
| 169 | return c == ' ' || c == '\t'; | ||
| 170 | } | ||
| 171 | |||
| 172 | static void | ||
| 173 | read_input(Options op, EventList *evlist) | ||
| 174 | { | ||
| 175 | struct tm t; | ||
| 176 | char line[MAXLEN], *text_ptr; | ||
| 177 | |||
| 178 | while (fgets(line, MAXLEN, stdin) != NULL) | ||
| 179 | if ((text_ptr = strptime(line, op.format_in, &t)) != NULL) | ||
| 180 | add_event(t, text_ptr, evlist); | ||
| 181 | } | ||
| 182 | |||
| 183 | static Options | ||
| 184 | read_op(int argc, char *argv[]) | ||
| 185 | { | ||
| 186 | Options op = default_op(); | ||
| 187 | int i; | ||
| 188 | |||
| 189 | /* Check for format specification. | ||
| 190 | * This changes the way other options are read */ | ||
| 191 | for (i = 1; i < argc; i++) { | ||
| 192 | if (argv[i][0] == '+') { | ||
| 193 | str_copy(op.format_in, &argv[i][1], MAXLEN); | ||
| 194 | str_copy(op.format_out, &argv[i][1], MAXLEN); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | for (i = 1; i < argc; i++) { | ||
| 199 | if (!strcmp(argv[i], "-v")) { | ||
| 200 | puts("sdep-"VERSION); | ||
| 201 | exit(0); | ||
| 202 | } else if (!strcmp(argv[i], "-d")) { | ||
| 203 | puts(default_format); | ||
| 204 | exit(0); | ||
| 205 | } else if (!strcmp(argv[i], "-s")) { | ||
| 206 | str_copy(op.separator, argv[++i], MAXLEN); | ||
| 207 | } else if (!strcmp(argv[i], "-f")) { | ||
| 208 | if (i+1 >= argc || | ||
| 209 | strptime(argv[i+1], op.format_in, &op.from) == NULL) | ||
| 210 | op.from.tm_year = -1000000000; /* Very large number */ | ||
| 211 | else | ||
| 212 | i++; | ||
| 213 | } else if (!strcmp(argv[i], "-t")) { | ||
| 214 | if (i+1 >= argc || | ||
| 215 | strptime(argv[i+1], op.format_in, &op.to) == NULL) | ||
| 216 | op.to.tm_year = 1000000000; /* Very small number */ | ||
| 217 | else | ||
| 218 | i++; | ||
| 219 | } else if (!strcmp(argv[i], "-w")) { | ||
| 220 | op.format_out = argv[++i]; | ||
| 221 | } else if (argv[i][0] != '+' && strlen(argv[i]) != 0) { | ||
| 222 | fprintf(stderr, "usage: sdep [-dv]"); | ||
| 223 | fprintf(stderr, " [+format] [-f [date]] [-t [date]]"); | ||
| 224 | fprintf(stderr, " [-w format] [-s string]\n"); | ||
| 225 | exit(1); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return op; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* | ||
| 233 | * Copy up to n characters of the string str to dst and append '\0'. | ||
| 234 | * Suggested by NRK. | ||
| 235 | */ | ||
| 236 | static void | ||
| 237 | str_copy(char *dst, char *src, int n) | ||
| 238 | { | ||
| 239 | if (memccpy(dst, src, '\0', n) == NULL) | ||
| 240 | dst[n-1] = '\0'; | ||
| 241 | } | ||
| 242 | |||
| 243 | static char * | ||
| 244 | str_trim(char *t) | ||
| 245 | { | ||
| 246 | char *s; | ||
| 247 | |||
| 248 | for (s = &t[strlen(t)-1]; s != t && is_space(*s); *s = '\0', s--); | ||
| 249 | for (; *t != '\0' && is_space(*t); t++); | ||
| 250 | |||
| 251 | return t; | ||
| 252 | } | ||
| 253 | |||
| 254 | static void | ||
| 255 | write_output(Options op, Event *ev, int n) | ||
| 256 | { | ||
| 257 | char outline[MAXLEN]; | ||
| 258 | int i; | ||
| 259 | |||
| 260 | for (i = 0; i < n; i++) | ||
| 261 | printf("%s\n", format_line(ev[i], op, outline)); | ||
| 262 | } | ||
| 263 | |||
| 264 | int | ||
| 265 | main(int argc, char *argv[]) | ||
| 266 | { | ||
| 267 | Options op; | ||
| 268 | EventList evlist = {0}; | ||
| 269 | Event *selected; | ||
| 270 | |||
| 271 | op = read_op(argc, argv); | ||
| 272 | read_input(op, &evlist); | ||
| 273 | selected = malloc(sizeof(Event) * evlist.count); | ||
| 274 | write_output(op, selected, events_in_range(&evlist, op, selected)); | ||
| 275 | |||
| 276 | return 0; | ||
| 277 | } | ||
