Commit 0c7777fa 0c7777faa66c17c850315cdd137d5a22361a3979 by Sergey Poznyakoff

Support message annotation. Improved error reporting after launching external programs.

1 parent c444358a
...@@ -217,12 +217,34 @@ display_file (const char *name) ...@@ -217,12 +217,34 @@ display_file (const char *name)
217 } 217 }
218 218
219 static int 219 static int
220 check_exit_status (char *progname, int status)
221 {
222 if (WIFEXITED (status))
223 {
224 if (WEXITSTATUS (status))
225 {
226 mh_error (_("Command `%s' exited with status %d"),
227 progname, WEXITSTATUS(status));
228 return 1;
229 }
230 return 0;
231 }
232 else if (WIFSIGNALED (status))
233 mh_error (_("Command `%s' terminated on signal %d"),
234 progname, WTERMSIG (status));
235 else
236 mh_error (_("Command `%s' terminated abnormally"), progname);
237 return 1;
238 }
239
240 static int
220 invoke (char *compname, char *defval, int argc, char **argv, 241 invoke (char *compname, char *defval, int argc, char **argv,
221 char *extra0, char *extra1, int *status) 242 char *extra0, char *extra1)
222 { 243 {
223 int i, rc; 244 int i, rc;
224 char **xargv; 245 char **xargv;
225 char *progname; 246 char *progname;
247 int status;
226 248
227 progname = mh_global_profile_get (compname, defval); 249 progname = mh_global_profile_get (compname, defval);
228 if (!progname) 250 if (!progname)
...@@ -243,9 +265,63 @@ invoke (char *compname, char *defval, int argc, char **argv, ...@@ -243,9 +265,63 @@ invoke (char *compname, char *defval, int argc, char **argv,
243 if (extra1) 265 if (extra1)
244 xargv[i++] = extra1; 266 xargv[i++] = extra1;
245 xargv[i++] = NULL; 267 xargv[i++] = NULL;
246 rc = mu_spawnvp (xargv[0], (const char **) xargv, status); 268 rc = mu_spawnvp (xargv[0], (const char **) xargv, &status);
247 free (xargv); 269 free (xargv);
248 return rc; 270 return rc ? rc : check_exit_status (progname, status);
271 }
272
273 struct anno_data
274 {
275 char *field;
276 char *value;
277 int date;
278 };
279
280 static int
281 anno (void *item, void *data)
282 {
283 struct anno_data *d = item;
284 mh_annotate (item, d->field, d->value, d->date);
285 return 0;
286 }
287
288 static void
289 annotate (struct mh_whatnow_env *wh)
290 {
291 mu_message_t msg;
292 mu_address_t addr = NULL;
293 size_t i, count;
294
295 if (!wh->anno_field || !wh->anno_list)
296 return;
297
298 msg = mh_file_to_message (NULL, wh->file);
299 if (!msg)
300 return;
301
302 mh_expand_aliases (msg, &addr, NULL, NULL);
303 mu_address_get_count (addr, &count);
304 for (i = 1; i <= count; i++)
305 {
306 mu_address_t subaddr;
307
308 if (mu_address_get_nth (addr, i, &subaddr) == 0)
309 {
310 size_t size;
311 struct anno_data d;
312
313 mu_address_to_string (subaddr, NULL, 0, &size);
314 d.value = xmalloc (size + 1);
315 d.field = wh->anno_field;
316 d.date = i == 1;
317 mu_address_to_string (subaddr, d.value, size + 1, NULL);
318 mu_list_do (wh->anno_list, anno, &d);
319 free (d.value);
320 mu_address_destroy (&subaddr);
321 }
322 }
323 mu_address_destroy (&addr);
324 mu_message_destroy (&msg, NULL);
249 } 325 }
250 326
251 327
...@@ -306,10 +382,9 @@ static int ...@@ -306,10 +382,9 @@ static int
306 edit (struct mh_whatnow_env *wh, int argc, char **argv, int *status) 382 edit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
307 { 383 {
308 char *name; 384 char *name;
309 int rc;
310 385
311 asprintf (&name, "%s-next", wh->editor); 386 asprintf (&name, "%s-next", wh->editor);
312 invoke (name, wh->editor, argc, argv, wh->file, NULL, &rc); 387 invoke (name, wh->editor, argc, argv, wh->file, NULL);
313 free (name); 388 free (name);
314 389
315 return 0; 390 return 0;
...@@ -330,8 +405,8 @@ list (struct mh_whatnow_env *wh, int argc, char **argv, int *status) ...@@ -330,8 +405,8 @@ list (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
330 static int 405 static int
331 push (struct mh_whatnow_env *wh, int argc, char **argv, int *status) 406 push (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
332 { 407 {
333 int rc; 408 if (invoke ("sendproc", MHBINDIR "/send", argc, argv, "-push", wh->file) == 0)
334 invoke ("sendproc", MHBINDIR "/send", argc, argv, "-push", wh->file, &rc); 409 annotate (wh);
335 return 0; 410 return 0;
336 } 411 }
337 412
...@@ -359,8 +434,7 @@ quit (struct mh_whatnow_env *wh, int argc, char **argv, int *status) ...@@ -359,8 +434,7 @@ quit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
359 static int 434 static int
360 refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status) 435 refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
361 { 436 {
362 int rc; 437 invoke ("fileproc", MHBINDIR "/refile", argc, argv, "-file", wh->file);
363 invoke ("fileproc", MHBINDIR "/refile", argc, argv, "-file", wh->file, &rc);
364 return 0; 438 return 0;
365 } 439 }
366 440
...@@ -368,8 +442,8 @@ refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status) ...@@ -368,8 +442,8 @@ refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
368 static int 442 static int
369 send (struct mh_whatnow_env *wh, int argc, char **argv, int *status) 443 send (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
370 { 444 {
371 int rc; 445 if (invoke ("sendproc", MHBINDIR "/send", argc, argv, wh->file, NULL) == 0)
372 invoke ("sendproc", MHBINDIR "/send", argc, argv, wh->file, NULL, &rc); 446 annotate (wh);
373 return 0; 447 return 0;
374 } 448 }
375 449
......