Blame view

mail/mailvar.c 25.1 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include "mail.h"

#define MAILVAR_ALIAS  0x0001
#define MAILVAR_RDONLY 0x0002
#define MAILVAR_HIDDEN 0x0004

23 24
#define MAILVAR_TYPEMASK(type) (1<<(8+(type)))

25 26 27 28 29
struct mailvar_symbol
{
  struct mailvar_variable var;
  int flags;
  char *descr;
30
  void (*handler) (struct mailvar_variable *);
31 32 33 34
};

mu_list_t mailvar_list = NULL;

35 36 37 38 39
static void set_decode_fallback (struct mailvar_variable *);
static void set_replyregex (struct mailvar_variable *);
static void set_screen (struct mailvar_variable *);
static void set_debug (struct mailvar_variable *);
  
40 41
struct mailvar_symbol mailvar_tab[] =
  {
42 43 44
    /* FIXME: */
    { { "allnet", }, MAILVAR_HIDDEN },
    
45 46
    /* For compatibility with other mailx implementations.
       Never used, always true. */
47 48
    { { "append", },
      MAILVAR_TYPEMASK (mailvar_type_boolean) | MAILVAR_RDONLY,
49
      N_("messages saved in mbox are appended to the end rather than prepended") },
50 51
    { { "appenddeadletter", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
52
      N_("append the contents of canceled letter to dead.letter file") },
53 54
    { { "askbcc", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
55
      N_("prompt user for bcc before composing the message") },
56 57
    { { "askcc", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
58
      N_("prompt user for cc before composing the message") },
59 60
    { { "ask", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
61
      N_("prompt user for subject before composing the message") },
62 63 64
    { { "asksub", }, MAILVAR_ALIAS, NULL },
    { { "autoinc", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
65
      N_("automatically incorporate newly arrived messages")},
66 67
    { { "autoprint", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
68
      N_("delete command behaves like dp") },
69 70 71 72
    { { "byname" },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
      N_("record outgoing messages in a file named after the first recipient; "
	 "overrides the `record' variable") },
73 74
    { { "bang", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
75 76
      N_("replace every occurrence of ! in arguments to the shell command"
	 " with the last executed command") },
77 78
    { { "charset", },
      MAILVAR_TYPEMASK (mailvar_type_string),
79
      N_("output character set for decoded header fields") },
80 81
    { { "cmd", },
      MAILVAR_TYPEMASK (mailvar_type_string),
82
      N_("default shell command for pipe") },
83 84
    { { "columns", },
      MAILVAR_TYPEMASK (mailvar_type_number),
85
      N_("number of columns on terminal screen") },
86 87 88
    { { "crt", },
      MAILVAR_TYPEMASK (mailvar_type_number) |
      MAILVAR_TYPEMASK (mailvar_type_boolean),
89 90 91
      N_("if numeric, sets the minimum number of output lines needed "
	 "to engage paging; if boolean, use the height of the terminal "
	 "screen to compute the threshold") },
92 93
    { { "datefield", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
94
      N_("get date from the `Date:' header, instead of the envelope") },
95 96 97 98 99 100 101 102 103 104 105 106
    { { "debug", },
      MAILVAR_TYPEMASK (mailvar_type_string) |
        MAILVAR_TYPEMASK (mailvar_type_boolean),
      N_("set Mailutils debug level"),
      set_debug },
    { { "decode-fallback", },
      MAILVAR_TYPEMASK (mailvar_type_string),
      N_("how to represent characters that cannot be rendered using the "
	 "current character set"),
      set_decode_fallback },
    { { "dot", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
107
      N_("input message is terminated with a dot alone on a line") },
108 109
    { { "editheaders", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
110
      N_("allow to edit message headers while composing") },
111 112
    { { "emptystart", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
113
      N_("start interactive mode if the mailbox is empty") },
114 115
    { { "escape", },
      MAILVAR_TYPEMASK (mailvar_type_string),
116
      N_("character denoting escapes") },
117 118
    { { "flipr", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
119
      N_("swap the meaning of reply and Reply commands") },
120 121
    { { "folder", },
      MAILVAR_TYPEMASK (mailvar_type_string), 
122
      N_("folder directory name") },
123 124
    { { "fromfield", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
125 126
      N_("get sender address from the `From:' header, instead of "
	 "the envelope") },
127 128
    { { "gnu-last-command", },
      MAILVAR_TYPEMASK (mailvar_type_string) | MAILVAR_RDONLY,
129
      N_("last executed command line") },
130 131
    { { "header", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
132
      N_("run the `headers' command after entering interactive mode") },
133 134 135 136
    { { "headline", },
      MAILVAR_TYPEMASK (mailvar_type_string),
      N_("format string to use for the header summary"),
      mail_compile_headline },
137 138
    { { "hold", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
139
      N_("hold the read or saved messages in the system mailbox") },
140 141
    { { "ignore", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
142
      N_("ignore keyboard interrupts when composing messages") },
143 144
    { { "ignoreeof", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
145
      N_("typing the EOF character terminates the letter being composed") },
146 147
    { { "indentprefix", },
      MAILVAR_TYPEMASK (mailvar_type_string),
148
      N_("string used by the ~m escape for indenting quoted messages") },
149 150
    { { "inplacealiases", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
151 152 153 154
      N_("expand aliases in the address header field "
	 "before entering send mode") },
    /* For compatibility with other mailx implementations.
       Never used, always true. */
155 156
    { { "keep", },
      MAILVAR_TYPEMASK (mailvar_type_boolean) | MAILVAR_RDONLY,
157
      N_("truncate the user's system mailbox when it is empty") },
158 159
    { { "keepsave", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
160
      N_("keep saved messages in system mailbox too") },
161 162
    { { "mailx", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
163
      N_("enable mailx compatibility mode") },
164 165
    { { "metamail", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
166 167
      N_("interpret the content of message parts; if set to a string "
	 "specifies the name of the externam metamail command") },
168 169
    { { "metoo", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
170
      N_("do not remove sender addresses from the recipient list") },
171 172
    { { "mimenoask", },
      MAILVAR_TYPEMASK (mailvar_type_string),
173 174
      N_("a comma-separated list of MIME types for which "
	 "no confirmation is needed before running metamail interpreter") },
175 176
    { { "mode", },
      MAILVAR_TYPEMASK (mailvar_type_string) | MAILVAR_RDONLY,
177
      N_("the name of current operation mode") },
178 179
    { { "nullbody", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
180
      N_("accept messages with an empty body") },
181 182
    { { "nullbodymsg", },
      MAILVAR_TYPEMASK (mailvar_type_string),
183
      N_("display this text when sending a message with empty body") },
184 185
    { { "outfolder", },
      MAILVAR_TYPEMASK (mailvar_type_string),
186
      N_("keep created files in this folder") },
187 188
    { { "page", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
189
      N_("pipe command terminates each message with a linefeed") },
190 191
    { { "prompt", },
      MAILVAR_TYPEMASK (mailvar_type_string),
192
      N_("command prompt sequence") },
193 194
    { { "quit", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
195
      N_("keyboard interrupts terminate the program") },
196 197
    { { "rc", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
198
      N_("read the system-wide configuration file upon startup") },
199 200
    { { "readonly", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
201
      N_("mailboxes are opened in readonly mode") },
202 203
    { { "record", },
      MAILVAR_TYPEMASK (mailvar_type_string),
204
      N_("save outgoing messages in this file") },
205 206
    { { "recursivealiases", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
207
      N_("recursively expand aliases") },
208 209
    { { "regex", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
210
      N_("use regular expressions in message specifications") },
211 212
    { { "replyprefix", },
      MAILVAR_TYPEMASK (mailvar_type_string),
213
      N_("prefix for the subject line of a reply message") },
214 215 216 217 218 219
    { { "replyregex", },
      MAILVAR_TYPEMASK (mailvar_type_string),
      N_("regexp for recognizing subject lines of reply messages"),
      set_replyregex },
    { { "save", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
220
      N_("stored aborted messages in the user's dead.file") },
221 222 223 224 225 226
    { { "screen", },
      MAILVAR_TYPEMASK (mailvar_type_number),
      N_("number of lines on terminal screen"),
      set_screen },
    { { "sendmail", },
      MAILVAR_TYPEMASK (mailvar_type_string),
227 228
      N_("URL of the mail transport agent") },
    /* FIXME: Not yet used. */
229 230 231 232
    { { "sendwait", },
      MAILVAR_TYPEMASK (mailvar_type_boolean) | MAILVAR_HIDDEN, NULL },
    { { "sign", },
      MAILVAR_TYPEMASK (mailvar_type_string),
233
      N_("signature for use with the ~a command") },
234 235
    { { "Sign", },
      MAILVAR_TYPEMASK (mailvar_type_string),
236
      N_("signature for use with the ~A command") },
237 238
    { { "showenvelope", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
239
      N_("`print' command includes the SMTP envelope in its output") },
240 241
    { { "showto", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
242 243
      N_("if the message was sent by the user, print its recipient address "
	 "in the header summary") },
244 245
    { { "toplines", },
      MAILVAR_TYPEMASK (mailvar_type_number),
246
      N_("number of lines to be displayed by `top' or `Top'") },
247 248
    { { "variable-pretty-print", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
249
      N_("print variables with short descriptions") },
250 251 252
    { { "varpp", }, MAILVAR_ALIAS },
    { { "variable-strict", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
253
      N_("perform strict checking when setting options") },
254 255 256 257 258 259 260
    { { "varstrict", }, MAILVAR_ALIAS },
    { { "verbose", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
      N_("verbosely trace the process of message delivery") },
    { { "xmailer", },
      MAILVAR_TYPEMASK (mailvar_type_boolean),
      N_("add the `X-Mailer' header to the outgoing messages") },
261 262

    /* These will be implemented later */
263
    { { "onehop", }, MAILVAR_HIDDEN, NULL },
264

265
    { { "quiet", }, MAILVAR_TYPEMASK (mailvar_type_boolean) | MAILVAR_HIDDEN,
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
      "suppresses the printing of the version when first invoked" },
    
    { { NULL }, }
  };

static int mailvar_symbol_count = sizeof (mailvar_tab) / sizeof (mailvar_tab[0]) - 1;

struct mailvar_symbol *
find_mailvar_symbol (const char *var)
{
  struct mailvar_symbol *ep;
  for (ep = mailvar_tab; ep->var.name; ep++)
    if (strcmp (ep->var.name, var) == 0)
      {
	while ((ep->flags & MAILVAR_ALIAS) && ep > mailvar_tab)
	  ep--;
	return ep;
      }
  return NULL;
}

static void
288 289
print_descr (FILE *out, const char *s, int n,
	     int doc_col, int rmargin, char *pfx)
290 291 292 293 294
{
  if (!s)
    return;
  do
    {
295 296
      const char *p;
      const char *space = NULL;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

      if (n == 1 && pfx)
	n += fprintf (out, "%s", pfx);
      
      for (; n < doc_col; n++)
	fputc (' ', out);

      for (p = s; *p && p < s + (rmargin - doc_col); p++)
	if (mu_isspace (*p))
	  space = p;
      
      if (!space || p < s + (rmargin - doc_col))
	{
	  fprintf (out, "%s", s);
	  s += strlen (s);
	}
      else
	{
	  for (; s < space; s++)
	    fputc (*s, out);
	  for (; *s && mu_isspace (*s); s++)
	    ;
	}
      fputc ('\n', out);
      n = 1;
    }
  while (*s);
}

/* Functions for dealing with internal mailvar_list variables */
static int
mailvar_variable_comp (const void *a, const void *b)
{
  const struct mailvar_variable *v1 = a;
  const struct mailvar_variable *v2 = b;

  return strcmp (v1->name, v2->name);
}

static int
mailvar_varptr_comp (const void *a, const void *b)
{
  const struct mailvar_variable * const * v1 = a;
  const struct mailvar_variable * const * v2 = b;

  return strcmp ((*v1)->name, (*v2)->name);
}

/* Find mailvar_list entry VAR. If not found and CREATE is not NULL, then
   create the (unset and untyped) variable */
struct mailvar_variable *
mailvar_find_variable (const char *name, int create)
{
  struct mailvar_symbol *sym;
  struct mailvar_variable *var;
  
  sym = find_mailvar_symbol (name);
  if (sym)
    var = &sym->var;
  else
    {
      struct mailvar_variable entry, *p;

      entry.name = (char*)name;
      /* Store it into the list */
      if (mailvar_list == NULL)
	{
	  mu_list_create (&mailvar_list);
	  mu_list_set_comparator (mailvar_list, mailvar_variable_comp);
	}
  
      if (mu_list_locate (mailvar_list, &entry, (void**)&p))
	{
	  if (!create)
	    return 0;
	  else
	    {
	      p = xmalloc (sizeof *p);
	      p->name = xstrdup (name);
	      mu_list_prepend (mailvar_list, p);
	    }
	}
      var = p;
      var->set = 0;
      var->type = mailvar_type_whatever;
      var->value.number = 0;
    }

  return var;
}


/* Retrieve the value of a specified variable of given type.
   The value is stored in the location pointed to by PTR variable.
   VARIABLE and TYPE specify the variable name and type. If the
   variable is not found and WARN is not null, the warning message
   is issued.

   Return value is 0 if the variable is found, 1 otherwise.
   If PTR is not NULL, it must point to

   int           if TYPE is mailvar_type_number or mailvar_type_boolean
   const char *  if TYPE is mailvar_type_string. 

   Passing PTR=NULL may be used to check whether the variable is set
   without retrieving its value. */
   
int
mailvar_get (void *ptr, const char *variable, enum mailvar_type type, int warn)
{
  struct mailvar_variable *var = mailvar_find_variable (variable, 0);

  if (!var->set || var->type != type)
    {
      if (warn)
	util_error (_("No value set for \"%s\""), variable);
      return 1;
    }
  if (ptr)
    switch (type)
      {
      case mailvar_type_string:
	*(char**)ptr = var->value.string;
	break;

      case mailvar_type_number:
	*(int*)ptr = var->value.number;
	break;

      case mailvar_type_boolean:
	*(int*)ptr = var->value.bool;
	break;

      default:
	break;
      }
  
  return 0;
}

/* Initialize mailvar_list entry: clear set indicator and free any memory
   associated with the data */
void
mailvar_variable_reset (struct mailvar_variable *var)
{
  if (!var->set)
    return;
  
  switch (var->type)
    {
    case mailvar_type_string:
      free (var->value.string);
      var->value.string = NULL;
      break;
	      
    default:
      break;
    }
  var->set = 0;
}

/* Set environement
   The  mailvar_set() function adds to the mailvar_list the VARIABLE
   with the given VALUE, if VARIABLE does not already exist.
   If it does exist in the mailvar_list, then its value is changed
   to VALUE if MOPTF_OVERWRITE bit is set in FLAGS, otherwise the
   value is not changed.

   Unless MOPTF_QUIET bit is set in FLAGS, the function performs
   semantic check, using the builtin options table.
 
   If VALUE is null the VARIABLE is unset. */
int
mailvar_set (const char *variable, void *value, enum mailvar_type type,
	     int flags)
{
  struct mailvar_variable *var;
474 475
  const struct mailvar_symbol *sym = find_mailvar_symbol (variable);
  int unset = flags & MOPTF_UNSET;
476 477 478 479 480 481 482 483 484 485 486 487 488
  
  if (!(flags & MOPTF_QUIET)
      && mailvar_get (NULL, "variable-strict", mailvar_type_boolean, 0) == 0)
    {
      if (!sym)
	mu_diag_output (MU_DIAG_WARNING, _("setting unknown variable %s"),
			variable);
      else if (sym->flags & MAILVAR_RDONLY)
	{
	  mu_error (_("Cannot set read-only variable %s"),
		    variable);
	  return 1;
	}
489 490 491 492 493 494
      else if (!(sym->flags & MAILVAR_TYPEMASK (type))
	       && !unset)
	{
	  mu_error (_("Wrong type for %s"), variable);
	  return 1;
	}
495 496
    }

497
  var = mailvar_find_variable (variable, !unset);
498 499 500 501 502

  if (!var || (var->set && !(flags & MOPTF_OVERWRITE)))
    return 0;

  mailvar_variable_reset (var);
503
  if (!unset)
504
    {
505 506
      var->type = type;
      if (value)
507
	{
508 509 510 511 512 513
	  var->set = 1;
	  switch (type)
	    {
	    case mailvar_type_number:
	      var->value.number = *(int*)value;
	      break;
514
	  
515 516 517
	    case mailvar_type_string:
	      var->value.string = strdup (value);
	      break;
518
	  
519 520 521
	    case mailvar_type_boolean:
	      var->value.bool = *(int*)value;
	      break;
522
		  
523 524 525
	    default:
	      abort();
	    }
526 527 528 529
	}
    }
    
  /* Special handling for some variables */
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  if (sym && sym->flags & MAILVAR_TYPEMASK (type) && sym->handler)
    sym->handler (var);
  
  return 0;
}


static void
set_decode_fallback (struct mailvar_variable *var)
{
  if (mu_set_default_fallback (var->value.string))
    mu_error (_("Incorrect value for decode-fallback"));
}

static void
set_replyregex (struct mailvar_variable *var)
{
  int rc;
  char *err;
  
  if ((rc = mu_unre_set_regex (var->value.string, 0, &err)))
    {
      if (err)
	mu_error ("%s: %s", mu_strerror (rc), err);
      else
	mu_error ("%s", mu_strerror (rc));
    }
}

static void
set_screen (struct mailvar_variable *var)
{
  page_invalidate (1);
}

static void
set_mailbox_debug_level (mu_log_level_t level)
{
  if (mbox)
    {
      mu_debug_t mdbg;
      mu_mailbox_get_debug (mbox, &mdbg);
      mu_debug_set_level (mdbg, level);
    }
}  

#define DEFAULT_DEBUG_LEVEL  \
  (MU_DEBUG_INHERIT | MU_DEBUG_LEVEL_UPTO (MU_DEBUG_TRACE7))

static void
set_debug (struct mailvar_variable *var)
{
  int rc;
  int argc;
  char **argv;
  int i;
  mu_debug_t dbg;

  mu_global_debug_clear_level (NULL);
  set_mailbox_debug_level (0);

  if (var->type == mailvar_type_boolean)
    {
      if (var->set)
594
	{
595 596 597
	  /* FIXME: What to set here? 
	     mu_global_debug_set_level ("*", DEFAULT_DEBUG_LEVEL); */
	  set_mailbox_debug_level (DEFAULT_DEBUG_LEVEL);
598
	}
599
      return;
600
    }
601 602 603 604 605
  
  mu_diag_get_debug (&dbg);
  
  rc = mu_argcv_get (var->value.string, ";", NULL, &argc, &argv);
  if (rc)
606
    {
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
      mu_error (_("Cannot parse string: %s"), mu_strerror (rc));
      return;
    }

  for (i = 0; i < argc; i++)
    {
      char *p;
      mu_log_level_t level = MU_DEBUG_INHERIT;
      char *object_name = argv[i];
      
      for (p = object_name; *p && *p != '='; p++)
	;

      if (*p == '=')
	{
	  *p++ = 0;
	  mu_debug_level_from_string (p, &level, dbg);
	}
      else
	level |= MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT);
      
      if (strcmp (object_name, "mailbox") == 0)
	set_mailbox_debug_level (level);
      mu_global_debug_set_level (object_name, level);
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    }
}


size_t
_mailvar_symbol_count (int set)
{
  if (!set)
    return mailvar_symbol_count;
  else
    {
      struct mailvar_symbol *s;
      size_t count = 0;
      for (s = mailvar_tab; s->var.name; s++)
	if (s->var.set)
	  count++;
      return count;
    }
}

void
_mailvar_symbol_to_array (int set, struct mailvar_variable **vartab)
{
  struct mailvar_symbol *s;
  for (s = mailvar_tab; s->var.name; s++)
    if (!set || s->var.set)
      {
	*vartab = &s->var;
	vartab++;
      }
}

struct mailvar_variable **
mailvar_make_array (int set, size_t *pcount)
{
  struct mailvar_variable **vartab;
  size_t count = 0, symcount;

  symcount = _mailvar_symbol_count (set);
  mu_list_count (mailvar_list, &count);
  vartab = xcalloc (symcount + count + 1, sizeof *vartab);
  mu_list_to_array (mailvar_list, (void**) vartab, count, NULL);
  _mailvar_symbol_to_array (set, vartab + count);
  count += symcount;
  qsort (vartab, count, sizeof *vartab, mailvar_varptr_comp);
  vartab[count] = NULL;
  if (pcount)
    *pcount = count;
  return vartab;
}
  

struct mailvar_iterator
{
  const char *prefix;
  int prefixlen;
  struct mailvar_variable **varptr;
  size_t varcnt;
  size_t pos;
};
  
const char *
mailvar_iterate_next (struct mailvar_iterator *itr)
{
  struct mailvar_variable *vp;
  
  while (itr->pos < itr->varcnt)
    {
      vp = itr->varptr[itr->pos++];
  
      if (strlen (vp->name) >= itr->prefixlen
	  && strncmp (vp->name, itr->prefix, itr->prefixlen) == 0)
	return vp->name;
    }
  return NULL;
}

const char *
mailvar_iterate_first (int set, const char *prefix, struct mailvar_iterator **pitr)
{
  struct mailvar_iterator *itr = xmalloc (sizeof *itr);
  itr->prefix = prefix;
  itr->prefixlen = strlen (prefix);
  itr->varptr = mailvar_make_array (set, &itr->varcnt);
  itr->pos = 0;
  *pitr = itr;
  return mailvar_iterate_next (itr);
}

void
mailvar_iterate_end (struct mailvar_iterator **pitr)
{
  if (pitr && *pitr)
    {
      struct mailvar_iterator *itr = *pitr;
      free (itr->varptr);
      free (itr);
      *pitr = NULL;
    }
}

void
mailvar_print (int set)
{
  struct mailvar_variable **vartab;
  FILE *out = ofile;
  size_t i, count;
  int pagelines = util_get_crt ();
  int width = util_getcols ();
  int prettyprint = mailvar_get (NULL, "variable-pretty-print",
				 mailvar_type_boolean, 0) == 0;
  
  vartab = mailvar_make_array (set, &count);

  if (pagelines && count > pagelines)
    out = popen (getenv ("PAGER"), "w");

  for (i = 0; i < count; i++)
    {
      if (prettyprint)
	{
	  const struct mailvar_symbol *sym =
	    find_mailvar_symbol (vartab[i]->name);

	  if (sym)
	    {
	      if (sym->flags & MAILVAR_HIDDEN)
		continue;
	      if (sym->flags & MAILVAR_RDONLY)
		fprintf (out, "# %s:\n", _("Read-only variable"));
	      print_descr (out, gettext (sym->descr), 1, 3, width - 1, "# ");
	    }
	}
      switch (vartab[i]->type)
	{
	case mailvar_type_number:
	  fprintf (out, "%s=%d", vartab[i]->name, vartab[i]->value.number);
	  break;
	  
	case mailvar_type_string:
	  fprintf (out, "%s=\"%s\"", vartab[i]->name, vartab[i]->value.string);
	  break;
	  
	case mailvar_type_boolean:
	  if (!vartab[i]->value.bool)
	    fprintf (out, "no");
	  fprintf (out, "%s", vartab[i]->name);
	  break;
	  
	case mailvar_type_whatever:
	  fprintf (out, "%s %s", vartab[i]->name, _("oops?"));
	}
      fprintf (out, "\n");
    }
  free (vartab);

  if (out != ofile)
    pclose (out);
}


void
mailvar_variable_format (FILE *fp,
			 const struct mailvar_variable *var,
			 const char *defval)
{
  if (var)
    switch (var->type)
      {
      case mailvar_type_string:
	fprintf (fp, "%s", var->value.string);
	break;

      case mailvar_type_number:
	fprintf (fp, "%d", var->value.number);
	break;

      case mailvar_type_boolean:
	fprintf (fp, "%s", var->set ? "yes" : "no");
	break;

      default:
	if (defval)
	  fprintf (fp, "%s", defval);
	break;
      }
}


static char *typestr[] =
  {
    N_("untyped"),
    N_("numeric"),
    N_("string"),
    N_("boolean")
  };

static void
describe_symbol (FILE *out, int width, const struct mailvar_symbol *sym)
{
  int n;
832
  int i, t;
833 834 835 836 837 838 839 840 841 842 843 844 845
  const struct mailvar_symbol *ali;

  n = fprintf (out, "%s", sym->var.name);
  for (ali = sym + 1; ali->var.name && ali->flags & MAILVAR_ALIAS; ali++)
    {
      size_t len = strlen (ali->var.name) + 2;
      if (n + len > width)
	n = fprintf (out, "\n%s", ali->var.name);
      else
	n = fprintf (out, ", %s", ali->var.name);
    }
  fputc ('\n', out);
  
846 847 848 849 850 851 852 853 854 855 856 857
  fprintf (out, _("Type: "));
  for (i = 0, t = 0; i < sizeof (typestr) / sizeof (typestr[0]); i++)
    if (sym->flags & MAILVAR_TYPEMASK (i))
      {
	if (t++)
	  fprintf (out, " %s ", _("or"));
	fprintf (out, "%s", gettext (typestr[i]));
      }
  if (!t)
    fprintf (out, "%s", gettext (typestr[0]));
  fputc ('\n', out);
  
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
  fprintf (out, "%s", _("Current value: "));
  mailvar_variable_format (out, &sym->var, _("[not set]"));

  if (sym->flags & MAILVAR_RDONLY)
    fprintf (out, " [%s]", _("read-only"));
  fputc ('\n', out);
  
  print_descr (out, gettext (sym->descr ? sym->descr : N_("Not documented")),
	       1, 1, width - 1, NULL);
  fputc ('\n', out);
}

int
mail_variable (int argc, char **argv)
{
  int pagelines = util_get_crt ();
  int width = util_getcols ();
  FILE *out = ofile;

  if (pagelines)
    out = popen (getenv ("PAGER"), "w");
  
  if (argc == 1)
    {
      struct mailvar_symbol *sym;

      for (sym = mailvar_tab; sym->var.name; sym++)
	if (!(sym->flags & (MAILVAR_HIDDEN|MAILVAR_ALIAS)))
	  describe_symbol (out, width, sym);
    }
  else
    {
      int i;

      for (i = 1; i < argc; i++)
	{
	  struct mailvar_symbol *sym = find_mailvar_symbol (argv[i]);
	  if (!sym)
	    fprintf (out, "%s: unknown\n", argv[i]);
	  else
	    describe_symbol (out, width, sym);
	}
    }
  if (out != ofile)
    pclose (out);
  return 0;
}


#ifdef WITH_READLINE
static char *
mailvar_generator (int set, const char *text, int state)
{
  static struct mailvar_iterator *itr;
  const char *p;
  
  if (!state)
    p = mailvar_iterate_first (set, text, &itr);
  else
    p = mailvar_iterate_next (itr);

  if (!p)
    {
      mailvar_iterate_end (&itr);
      return NULL;
    }
  return strdup (p);
}

static char *
mailvar_set_generator (const char *text, int state)
{
  return mailvar_generator (0, text, state);
}

static char *
mailvar_unset_generator (const char *text, int state)
{
  return mailvar_generator (1, text, state);
}

char **
mailvar_set_compl (int argc, char **argv, int ws)
{
  ml_attempted_completion_over ();
  return rl_completion_matches (ws ? "" : argv[argc-1],
				/* Possible values for argv[0] are:
				   set, unset, variable */
				argv[0][0] == 'u' ? mailvar_unset_generator
				                  : mailvar_set_generator);
}

#endif