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("Path: %s\n", n->pkgid);
25 printf("Hash: %s\n", n->file);
28 printf("%s\n", n->note);
31 void jstring(char *field, char *value, int indent, int final) {
35 printf("\"%s\": ", field);
38 if (strchr("\"\\\b\f\n\r\t", *value)) {
52 void show_json(struct zpm_note *n) {
56 len = strcspn(n->note, "\n\r");
59 printf("{\n\t\"id\": %" PRId64 ",\n", n->id);
60 jstring("package", n->pkgid, 1, 0);
61 jstring("hash", n->file, 1, 0);
62 jstring("path", n->path, 1, 0);
65 printf("\t\"subject\": \"%.*s\",\n", (int)len, n->note);
67 printf("\t\"subject\": null,\n");
69 jstring("note", n->note, 1, 1);
73 void print_note(struct zpm_note *n) {
79 space -= strlen(n->pkgid);
88 len = strcspn(n->note, "\n\r");
93 printf("%" PRId64 " %.10s %s %.*s\n", n->id, n->ts,
94 n->pkgid ? n->pkgid : "-", (int)len, n->note);
97 int main(int ac, char **av){
102 int64_t start = 0, end = INT64_MAX;
107 struct zpm_note n = { 0 };
113 dbfile = getenv("ZPMDB");
115 dbfile = "/var/lib/zpm/local.db";
124 * -m note message, otherwise EDITOR/vi to edit a text file
125 * -e echo note after creating
126 * zpm note -p pkgid -P path -H hash -m 'foo'
131 int list = 0, delete = 0, ack = 0, show = 0, json = 0;
133 while ((opt = getopt(ac, av, "f:p:P:H:m:n:lDAauesjF:")) != -1) {
135 case 'f': dbfile = optarg; break;
136 case 'p': n.pkgid = optarg; break;
137 case 'P': n.path = optarg; break;
138 case 'H': n.hash = optarg; break;
139 case 'm': n.note = optarg; break;
140 case 'F': notefrom = optarg; break;
141 case 'n': range = optarg; break;
142 case 'l': list = 1; break;
143 case 'j': json = 1; break;
144 case 's': show = 1; break;
145 case 'e': echo = 1; break;
146 case 'D': delete = 1; break;
147 case 'A': ack = 1; break;
148 case 'a': select |= 2; break;
149 case 'u': select |= 6; break;
158 * 0x2 = include acked
159 * 0x4 = suppress unack, implies 0x2
165 end = strtoll(range+1, NULL, 10);
166 } else if (isdigit(*range)) {
168 start = strtoll(range, &next, 10);
169 if (next && *next == '-') {
172 if (isdigit(*next)) {
173 end = strtoll(next, NULL, 10);
182 fprintf(stderr, "must specify db\n");
186 /* given a package name, get the packages */
187 /* no package name, get all */
194 if (strcmp(notefrom, "-")) {
195 input = fopen(notefrom, "r");
200 perror("can't read input file");
203 cread = getdelim(&filenote, &in, 0, input);
205 perror("error reading file");
211 if (zpm_open(&pkg, dbfile)) {
212 if (list || show || delete || ack) {
213 n.id = start ? start - 1 : start;
214 while (zpm_note(&pkg, &n, select)) {
219 zpm_note_del(&pkg, n.id);
221 zpm_note_ack(&pkg, n.id);
235 n.id = zpm_note_add(&pkg, n.pkgid, n.path, n.hash, "%s", n.note);
236 zpm_note(&pkg, &n, 2);
241 printf("%" PRId64 "\n", n.id);
244 fprintf(stderr, "unable to create note\n");
249 fprintf(stderr, "unable to open %s\n", dbfile);
254 return fail ? EXIT_FAILURE : EXIT_SUCCESS;