1 #define _POSIX_C_SOURCE 200809L
13 fprintf(stderr, "zpm-note [-I] [-s <status> ...] [-S <status>] [package]\n");
16 void show_note(struct zpm_note *n) {
17 printf("Note %" PRId64 " %s\n", n->id, n->ts);
19 printf("Package: %s\n", n->pkgid);
22 printf("Hash: %s\n", n->file);
25 printf("%s\n", n->note);
28 void jstring(char *field, char *value, int indent, int final) {
32 printf("\"%s\": ", field);
34 printf("\"%s\": ", value);
44 void show_json(struct zpm_note *n) {
48 len = strcspn(n->note, "\n\r");
51 printf("{\n\t\"id\": %" PRId64 ",\n", n->id);
52 jstring("package", n->pkgid, 1, 0);
53 jstring("hash", n->file, 1, 0);
56 printf("\t\"subject\": \"%.*s\",\n", (int)len, n->note);
58 printf("\t\"subject\": null,\n");
60 jstring("note", n->note, 1, 1);
64 void print_note(struct zpm_note *n) {
70 space -= strlen(n->pkgid);
79 len = strcspn(n->note, "\n\r");
84 printf("%" PRId64 " %.10s %s %.*s\n", n->id, n->ts,
85 n->pkgid ? n->pkgid : "-", (int)len, n->note);
88 int main(int ac, char **av){
93 int64_t start = 0, end = INT64_MAX;
98 struct zpm_note n = { 0 };
104 dbfile = getenv("ZPMDB");
106 dbfile = "/var/lib/zpm/local.db";
115 * -m note message, otherwise EDITOR/vi to edit a text file
116 * -e echo note after creating
117 * zpm note -p pkgid -P path -H hash -m 'foo'
122 int list = 0, delete = 0, ack = 0, show = 0, json = 0;
124 while ((opt = getopt(ac, av, "f:p:P:H:m:n:lDAauesjF:")) != -1) {
126 case 'f': dbfile = optarg; break;
127 case 'p': n.pkgid = optarg; break;
128 case 'P': n.path = optarg; break;
129 case 'H': n.hash = optarg; break;
130 case 'm': n.note = optarg; break;
131 case 'F': notefrom = optarg; break;
132 case 'n': range = optarg; break;
133 case 'l': list = 1; break;
134 case 'j': json = 1; break;
135 case 's': show = 1; break;
136 case 'e': echo = 1; break;
137 case 'D': delete = 1; break;
138 case 'A': ack = 1; break;
139 case 'a': select |= 2; break;
140 case 'u': select |= 6; break;
149 * 0x2 = include acked
150 * 0x4 = suppress unack, implies 0x2
156 end = strtoll(range+1, NULL, 10);
157 } else if (isdigit(*range)) {
159 start = strtoll(range, &next, 10);
160 if (next && *next == '-') {
163 if (isdigit(*next)) {
164 end = strtoll(next, NULL, 10);
173 fprintf(stderr, "must specify db\n");
177 /* given a package name, get the packages */
178 /* no package name, get all */
185 if (strcmp(notefrom, "-")) {
186 input = fopen(notefrom, "r");
191 perror("can't read input file");
194 cread = getdelim(&filenote, &in, 0, input);
196 perror("error reading file");
202 if (zpm_open(&pkg, dbfile)) {
203 if (list || show || delete || ack) {
204 n.id = start ? start - 1 : start;
205 while (zpm_note(&pkg, &n, select)) {
210 zpm_note_del(&pkg, n.id);
212 zpm_note_ack(&pkg, n.id);
226 n.id = zpm_note_add(&pkg, n.pkgid, n.path, n.hash, "%s", n.note);
227 zpm_note(&pkg, &n, 2);
232 printf("%" PRId64 "\n", n.id);
235 fprintf(stderr, "unable to create note\n");
240 fprintf(stderr, "unable to open %s\n", dbfile);
245 return fail ? EXIT_FAILURE : EXIT_SUCCESS;