This repository has been archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmtags.c
154 lines (132 loc) · 3.14 KB
/
mtags.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "blaze822.h"
#include <notmuch.h>
const char usage[] = "Usage: mtags [-th]\n\
-t \tformat for input to notmuch-tags\n\
-h \tshow this message\n";
char database_path[BUFSIZ];
int
find_database_path ()
{
FILE *fp = popen ("notmuch config get database.path", "r");
if (! fgets (database_path, BUFSIZ, fp )) {
pclose (fp);
return 0;
}
pclose (fp);
size_t l = strnlen (database_path, BUFSIZ);
if (l == 0) {
return 0;
}
database_path[l - 1] = '\0';
return -1;
}
/* Hex encode tags (see notmuch-tags(1) )
* The enc array must be three times the the maximum size of the s array.
*/
void
hex_encode (const char *s, char *enc)
{
for (; (*enc = *s); enc++, s++) {
if ((*s == '"') || (*s == ' ') || (iscntrl (*s))) {
sprintf ( enc, "%%%02X", *s);
enc += 2;
}
}
}
void
print_double_quoted (const char *s)
{
putchar ('"');
for (; *s; s++) {
putchar (*s);
if (*s == '"') {
putchar (*s);
}
}
putchar ('"');
}
char encoded_tag[3 * NOTMUCH_TAG_MAX];
char pathname[4096];
bool format_notmuch_tags = false;
notmuch_database_t *db;
void
mtag (char *file)
{
notmuch_status_t st;
notmuch_message_t *message;
notmuch_tags_t *tags;
const char *tag;
while (*file == ' ') file++;
st = notmuch_database_find_message_by_filename (db, file, &message);
if (st != NOTMUCH_STATUS_SUCCESS || message == NULL) {
fprintf (stderr, "Could not open %s\n", pathname);
exit (EXIT_FAILURE);
}
for (tags = notmuch_message_get_tags (message);
notmuch_tags_valid (tags);
notmuch_tags_move_to_next (tags)) {
tag = notmuch_tags_get (tags);
hex_encode (tag, encoded_tag);
if (format_notmuch_tags) {
printf ("+%s ", encoded_tag);
} else {
printf ("%s ", encoded_tag);
}
}
if (format_notmuch_tags) {
printf ("-- id:");
print_double_quoted (notmuch_message_get_message_id (message));
}
puts ("");
notmuch_message_destroy (message);
}
int
main (int argc, char **argv)
{
notmuch_status_t st;
char c;
while ((c = getopt (argc, argv, "th")) != -1)
switch (c) {
case 't':
format_notmuch_tags = true;
break;
case 'h':
fprintf (stderr, usage);
exit (EXIT_SUCCESS);
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
exit (EXIT_FAILURE);
default:
abort ();
}
errno = 0;
if (! find_database_path ()) {
fprintf (stderr, "Could not find notmuch database: %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
errno = 0;
st = notmuch_database_open (database_path, NOTMUCH_DATABASE_MODE_READ_ONLY, &db);
if (st != NOTMUCH_STATUS_SUCCESS ) {
fprintf (stderr, "Could not open notmuch database: %s\n", strerror (errno));
exit (errno);
}
if (argc == optind && isatty (0))
blaze822_loop1 (":", mtag);
else
blaze822_loop (argc - optind, argv + optind, mtag);
notmuch_database_close (db);
exit (EXIT_SUCCESS);
}