summaryrefslogtreecommitdiff
path: root/sfeed_plain.c
blob: ff54df5e24e53f2a25e500183d3696ddb985aa83 (plain)
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
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>

#include "util.h"

static time_t comparetime;
static char *line = NULL;
static size_t size = 0;

/* print `len' columns of characters. If string is shorter pad the rest
 * with characters `pad`. */
static void
printutf8pad(FILE *fp, const char *s, size_t len, int pad)
{
	wchar_t w;
	size_t n = 0, i;
	int r;

	for (i = 0; *s && n < len; i++, s++) {
		/* skip control characters */
		if (iscntrl(*s))
			continue;
		if (ISUTF8(*s)) {
			if ((r = mbtowc(&w, s, 4)) == -1)
				break;
			if ((r = wcwidth(w)) == -1)
				r = 1;
			n += (size_t)r;
		}
		putc(*s, fp);
	}
	for (; n < len; n++)
		putc(pad, fp);
}

static void
printfeed(FILE *fp, const char *feedname)
{
	char *fields[FieldLast];
	time_t parsedtime;

	while (parseline(&line, &size, fields, FieldLast, '\t', fp) > 0) {
		if (strtotime(fields[FieldUnixTimestamp], &parsedtime) != -1 &&
		   parsedtime >= comparetime)
			fputs(" N ", stdout);
		else
			fputs("   ", stdout);

		if (feedname[0])
			printf("%-15.15s ", feedname);
		printf(" %-30.30s  ", fields[FieldTimeFormatted]);
		printutf8pad(stdout, fields[FieldTitle], 70, ' ');
		printf("  %s\n", fields[FieldLink]);
	}
}

int
main(int argc, char *argv[])
{
	FILE *fp;
	char *name;
	int i;

	if ((comparetime = time(NULL)) == -1)
		err(1, "time");
	/* 1 day is old news */
	comparetime -= 86400;

	if (argc == 1) {
		printfeed(stdin, "");
	} else {
		for (i = 1; i < argc; i++) {
			if (!(fp = fopen(argv[i], "r")))
				err(1, "fopen: %s", argv[i]);
			name = xbasename(argv[i]);
			printfeed(fp, name);
			free(name);
			if (ferror(fp))
				err(1, "ferror: %s", argv[i]);
			fclose(fp);
		}
	}
	return 0;
}