Commit 86573c3b 86573c3becc2b38809caf14cd278bf2d7cedaec7 by Wojciech Polak

(rfc2047_decode_wrapper): Fixed reading a language environment variable.

(util_getcols): Imported function from mail/util.c.
(action): Try to print only one line per message.
1 parent 81b946f5
Showing 1 changed file with 94 additions and 30 deletions
...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
25 #include <string.h> 25 #include <string.h>
26 #include <unistd.h> 26 #include <unistd.h>
27 #include <sys/types.h> 27 #include <sys/types.h>
28 #ifdef HAVE_TERMIOS_H
29 # include <termios.h>
30 #endif
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
28 33
29 #include <mailutils/address.h> 34 #include <mailutils/address.h>
30 #include <mailutils/argp.h> 35 #include <mailutils/argp.h>
...@@ -254,13 +259,21 @@ static const char *frm_argp_capa[] = { ...@@ -254,13 +259,21 @@ static const char *frm_argp_capa[] = {
254 NULL 259 NULL
255 }; 260 };
256 261
257 static void 262 /*
258 frm_rfc2047_decode (char *personal, size_t buflen) 263 FIXME: Generalize this function and move it
264 to `mailbox/locale.c'. Do the same with the one
265 from `from/from.c' and `mail/util.c'...
266 */
267 static char *
268 rfc2047_decode_wrapper (char *buf, size_t buflen)
259 { 269 {
270 char locale[32];
260 char *charset = NULL; 271 char *charset = NULL;
261 char *tmp; 272 char *tmp;
262 int rc; 273 int rc;
263 274
275 memset (locale, 0, sizeof (locale));
276
264 /* Try to deduce the charset from LC_ALL or LANG variables */ 277 /* Try to deduce the charset from LC_ALL or LANG variables */
265 278
266 tmp = getenv ("LC_ALL"); 279 tmp = getenv ("LC_ALL");
...@@ -269,11 +282,13 @@ frm_rfc2047_decode (char *personal, size_t buflen) ...@@ -269,11 +282,13 @@ frm_rfc2047_decode (char *personal, size_t buflen)
269 282
270 if (tmp) 283 if (tmp)
271 { 284 {
272 char *sp; 285 char *sp = NULL;
273 char *lang; 286 char *lang;
274 char *terr; 287 char *terr;
275 288
276 lang = strtok_r (tmp, "_", &sp); 289 strncpy (locale, tmp, sizeof (locale) - 1);
290
291 lang = strtok_r (locale, "_", &sp);
277 terr = strtok_r (NULL, ".", &sp); 292 terr = strtok_r (NULL, ".", &sp);
278 charset = strtok_r (NULL, "@", &sp); 293 charset = strtok_r (NULL, "@", &sp);
279 294
...@@ -282,20 +297,18 @@ frm_rfc2047_decode (char *personal, size_t buflen) ...@@ -282,20 +297,18 @@ frm_rfc2047_decode (char *personal, size_t buflen)
282 } 297 }
283 298
284 if (!charset) 299 if (!charset)
285 return; 300 return strdup (buf);
286 301
287 rc = rfc2047_decode (charset, personal, &tmp); 302 rc = rfc2047_decode (charset, buf, &tmp);
288 if (rc) 303 if (rc)
289 { 304 {
290 if (dbug) 305 if (dbug)
291 mu_error (_("Can't decode line `%s': %s"), 306 mu_error (_("Can't decode line `%s': %s"),
292 personal, mu_strerror (rc)); 307 buf, mu_strerror (rc));
293 } 308 return strdup (buf);
294 else
295 {
296 strncpy (personal, tmp, buflen - 1);
297 free (tmp);
298 } 309 }
310
311 return tmp;
299 } 312 }
300 313
301 /* Retrieve the Personal Name from the header To: or From: */ 314 /* Retrieve the Personal Name from the header To: or From: */
...@@ -314,9 +327,10 @@ get_personal (header_t hdr, const char *field, char *personal, size_t buflen) ...@@ -314,9 +327,10 @@ get_personal (header_t hdr, const char *field, char *personal, size_t buflen)
314 address_t address = NULL; 327 address_t address = NULL;
315 size_t len = 0; 328 size_t len = 0;
316 329
317 frm_rfc2047_decode (hfield, sizeof (hfield)); 330 char *s = rfc2047_decode_wrapper (hfield, strlen (hfield));
318 331 address_create (&address, s);
319 address_create (&address, hfield); 332 free (s);
333
320 address_get_personal (address, 1, personal, buflen, &len); 334 address_get_personal (address, 1, personal, buflen, &len);
321 address_destroy (&address); 335 address_destroy (&address);
322 336
...@@ -333,12 +347,35 @@ static struct { ...@@ -333,12 +347,35 @@ static struct {
333 size_t unread; 347 size_t unread;
334 } counter; 348 } counter;
335 349
350 /*
351 * Get the number of columns on the screen
352 * First try an ioctl() call not all shells set the COLUMNS environ.
353 * This function was taken from mail/util.c.
354 */
355 int
356 util_getcols (void)
357 {
358 struct winsize ws;
359
360 ws.ws_col = ws.ws_row = 0;
361 if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_row == 0)
362 {
363 const char *columns = getenv ("COLUMNS");
364 if (columns)
365 ws.ws_col = strtol (columns, NULL, 10);
366 }
367
368 /* FIXME: Should we exit()/abort() if col <= 0 ? */
369 return ws.ws_col;
370 }
371
336 /* Observable action is being called on discovery of each message. */ 372 /* Observable action is being called on discovery of each message. */
337 /* FIXME: The format of the display is poorly done, please correct. */ 373 /* FIXME: The format of the display is poorly done, please correct. */
338 static int 374 static int
339 action (observer_t o, size_t type) 375 action (observer_t o, size_t type)
340 { 376 {
341 int status; 377 int status;
378 int col_cnt = 0;
342 379
343 switch (type) 380 switch (type)
344 { 381 {
...@@ -375,9 +412,12 @@ action (observer_t o, size_t type) ...@@ -375,9 +412,12 @@ action (observer_t o, size_t type)
375 break; 412 break;
376 413
377 if (show_number) 414 if (show_number)
378 printf ("%4lu: ", (u_long) counter.index); 415 {
416 printf ("%4lu: ", (u_long) counter.index);
417 col_cnt += 6;
418 }
379 419
380 if (show_field) 420 if (show_field) /* FIXME: This should be also rfc2047_decode. */
381 { 421 {
382 char hfield[256]; 422 char hfield[256];
383 status = header_get_value_unfold (hdr, show_field, hfield, 423 status = header_get_value_unfold (hdr, show_field, hfield,
...@@ -392,35 +432,59 @@ action (observer_t o, size_t type) ...@@ -392,35 +432,59 @@ action (observer_t o, size_t type)
392 status = get_personal (hdr, MU_HEADER_TO, hto, sizeof (hto)); 432 status = get_personal (hdr, MU_HEADER_TO, hto, sizeof (hto));
393 433
394 if (status == 0) 434 if (status == 0)
395 printf ("(%s) ", hto); 435 {
436 printf ("(%s) ", hto);
437 col_cnt += strlen (hto) + 3;
438 }
396 else 439 else
397 printf ("(------)"); 440 {
441 printf ("(-----) ");
442 col_cnt += 8;
443 }
398 } 444 }
399 445
400 if (show_from) 446 if (show_from)
401 { 447 {
402 char hfrom[32]; 448 char hfrom[32];
403 status = get_personal (hdr, MU_HEADER_FROM, hfrom, 449 status = get_personal (hdr, MU_HEADER_FROM, hfrom, sizeof (hfrom));
404 sizeof (hfrom));
405 if (status == 0) 450 if (status == 0)
406 printf ("%s\t", hfrom); 451 {
452 printf ("%s\t", hfrom);
453 col_cnt += strlen (hfrom) + 4; // tab=4, sigh.
454 }
407 else 455 else
408 printf ("-----\t"); 456 {
457 printf ("-----\t");
458 col_cnt += 9;
459 }
409 } 460 }
410 461
462 /*
463 A temporary fix for (correct) displaying:
464 util_getcols() - col_cnt - ~1. It's ugly, I know.
465 */
466
411 if (show_subject) 467 if (show_subject)
412 { 468 {
413 char hsubject[64]; 469 char *hsubject;
414 status = header_get_value_unfold (hdr, MU_HEADER_SUBJECT, 470 status = header_aget_value_unfold (hdr, MU_HEADER_SUBJECT,
415 hsubject, 471 &hsubject);
416 sizeof (hsubject), NULL);
417 if (status == 0) 472 if (status == 0)
418 { 473 {
419 frm_rfc2047_decode (hsubject, sizeof (hsubject)); 474 char *s = rfc2047_decode_wrapper (hsubject, strlen (hsubject));
420 printf ("%s", hsubject); 475 char out[80];
476 int fspace = util_getcols () - col_cnt - 1;
477
478 fspace = (fspace > (sizeof (out) - 1))
479 ? (sizeof (out) - 1) : fspace;
480
481 memset (out, 0, sizeof (out));
482 strncpy (out, s, fspace);
483 printf ("%s", out);
484 free (s);
421 } 485 }
422 } 486 }
423 printf ("\n"); 487 putchar ('\n');
424 break; 488 break;
425 } 489 }
426 490
......