init.c 47.1 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2002 Michael R. Elkins <[email protected]>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 
 *     This program 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 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 this program; if not, write to the Free Software
16
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18 19
 */ 

#include "mutt.h"
Thomas Roessler's avatar
Thomas Roessler committed
20
#include "mapping.h"
Thomas Roessler's avatar
Thomas Roessler committed
21 22
#include "mutt_curses.h"
#include "mutt_regex.h"
Thomas Roessler's avatar
Thomas Roessler committed
23
#include "history.h"
24
#include "keymap.h"
25
#include "mbyte.h"
26
#include "charset.h"
27
#include "mutt_crypt.h"
28

29
#if defined(USE_SSL) || defined(USE_NSS)
30
#include "mutt_ssl.h"
31 32 33
#endif


Thomas Roessler's avatar
Thomas Roessler committed
34 35 36 37 38 39 40 41 42 43 44 45 46

#include "mx.h"
#include "init.h"
#include "mailbox.h"

#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <errno.h>
#include <sys/wait.h>

47 48 49 50 51 52 53
void toggle_quadoption (int opt)
{
  int n = opt/4;
  int b = (opt % 4) * 2;

  QuadOptions[n] ^= (1 << b);
}
Thomas Roessler's avatar
Thomas Roessler committed
54 55 56

void set_quadoption (int opt, int flag)
{
57 58 59 60 61
  int n = opt/4;
  int b = (opt % 4) * 2;

  QuadOptions[n] &= ~(0x3 << b);
  QuadOptions[n] |= (flag & 0x3) << b;
Thomas Roessler's avatar
Thomas Roessler committed
62 63 64 65
}

int quadoption (int opt)
{
66 67 68 69
  int n = opt/4;
  int b = (opt % 4) * 2;

  return (QuadOptions[n] >> b) & 0x3;
Thomas Roessler's avatar
Thomas Roessler committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

int query_quadoption (int opt, const char *prompt)
{
  int v = quadoption (opt);

  switch (v)
  {
    case M_YES:
    case M_NO:
      return (v);

    default:
      v = mutt_yesorno (prompt, (v == M_ASKYES));
      CLEARLINE (LINES - 1);
      return (v);
  }

  /* not reached */
}

/* given the variable ``s'', return the index into the rc_vars array which
   matches, or -1 if the variable is not found.  */
int mutt_option_index (char *s)
{
  int i;

  for (i = 0; MuttVars[i].option; i++)
98
    if (mutt_strcmp (s, MuttVars[i].option) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      return (MuttVars[i].type == DT_SYN ?  mutt_option_index ((char *) MuttVars[i].data) : i);
  return (-1);
}

int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags)
{
  char		ch;
  char		qc = 0; /* quote char */
  char		*pc;

  /* reset the destination pointer to the beginning of the buffer */
  dest->dptr = dest->data;

  SKIPWS (tok->dptr);
  while ((ch = *tok->dptr))
  {
    if (!qc)
    {
      if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
	  (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
	  (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
	  (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
	  ((flags & M_TOKEN_PATTERN) && strchr ("~!|", ch)))
	break;
    }

    tok->dptr++;

    if (ch == qc)
      qc = 0; /* end of quote */
    else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
      qc = ch;
    else if (ch == '\\' && qc != '\'')
    {
133 134
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
135 136 137 138
      switch (ch = *tok->dptr++)
      {
	case 'c':
	case 'C':
139 140
	    if (!*tok->dptr)
		return -1; /* premature end of token */
141
	  mutt_buffer_addch (dest, (toupper (*tok->dptr) - '@') & 0x7f);
Thomas Roessler's avatar
Thomas Roessler committed
142 143 144
	  tok->dptr++;
	  break;
	case 'r':
145
	  mutt_buffer_addch (dest, '\r');
Thomas Roessler's avatar
Thomas Roessler committed
146 147
	  break;
	case 'n':
148
	  mutt_buffer_addch (dest, '\n');
Thomas Roessler's avatar
Thomas Roessler committed
149 150
	  break;
	case 't':
151
	  mutt_buffer_addch (dest, '\t');
Thomas Roessler's avatar
Thomas Roessler committed
152 153
	  break;
	case 'f':
154
	  mutt_buffer_addch (dest, '\f');
Thomas Roessler's avatar
Thomas Roessler committed
155 156
	  break;
	case 'e':
157
	  mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
158 159
	  break;
	default:
Thomas Roessler's avatar
Thomas Roessler committed
160 161 162
	  if (isdigit ((unsigned char) ch) &&
	      isdigit ((unsigned char) *tok->dptr) &&
	      isdigit ((unsigned char) *(tok->dptr + 1)))
Thomas Roessler's avatar
Thomas Roessler committed
163 164
	  {

165
	    mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
Thomas Roessler's avatar
Thomas Roessler committed
166 167 168
	    tok->dptr += 2;
	  }
	  else
169
	    mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
170 171 172 173
      }
    }
    else if (ch == '^' && (flags & M_TOKEN_CONDENSE))
    {
174 175
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
176 177
      ch = *tok->dptr++;
      if (ch == '^')
178
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
179
      else if (ch == '[')
180
	mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
181
      else if (isalpha ((unsigned char) ch))
182
	mutt_buffer_addch (dest, toupper (ch) - '@');
Thomas Roessler's avatar
Thomas Roessler committed
183 184
      else
      {
185 186
	mutt_buffer_addch (dest, '^');
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
      }
    }
    else if (ch == '`' && (!qc || qc == '"'))
    {
      FILE	*fp;
      pid_t	pid;
      char	*cmd, *ptr;
      size_t	expnlen;
      BUFFER	expn;
      int	line = 0;

      pc = tok->dptr;
      do {
	if ((pc = strpbrk (pc, "\\`")))
	{
	  /* skip any quoted chars */
	  if (*pc == '\\')
	    pc += 2;
	}
      } while (pc && *pc != '`');
      if (!pc)
      {
	dprint (1, (debugfile, "mutt_get_token: mismatched backtics\n"));
	return (-1);
      }
      cmd = mutt_substrdup (tok->dptr, pc);
      if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
      {
	dprint (1, (debugfile, "mutt_get_token: unable to fork command: %s", cmd));
216
	FREE (&cmd);
Thomas Roessler's avatar
Thomas Roessler committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
	return (-1);
      }
      FREE (&cmd);

      tok->dptr = pc + 1;

      /* read line */
      memset (&expn, 0, sizeof (expn));
      expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
      fclose (fp);
      mutt_wait_filter (pid);

      /* if we got output, make a new string consiting of the shell ouptput
	 plus whatever else was left on the original line */
231 232 233 234 235 236 237
      /* BUT: If this is inside a quoted string, directly add output to 
       * the token */
      if (expn.data && qc)
      {
	mutt_buffer_addstr (dest, expn.data);
	FREE (&expn.data);
      }
Thomas Roessler's avatar
Thomas Roessler committed
238
      else if (expn.data)
Thomas Roessler's avatar
Thomas Roessler committed
239
      {
240 241
	expnlen = mutt_strlen (expn.data);
	tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
Thomas Roessler's avatar
Thomas Roessler committed
242 243
	ptr = safe_malloc (tok->dsize);
	memcpy (ptr, expn.data, expnlen);
244
	strcpy (ptr + expnlen, tok->dptr);	/* __STRCPY_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
245 246 247 248 249 250 251 252 253
	if (tok->destroy)
	  FREE (&tok->data);
	tok->data = ptr;
	tok->dptr = ptr;
	tok->destroy = 1; /* mark that the caller should destroy this data */
	ptr = NULL;
	FREE (&expn.data);
      }
    }
Thomas Roessler's avatar
Thomas Roessler committed
254
    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
Thomas Roessler's avatar
Thomas Roessler committed
255
    {
256
      char *env = NULL, *var = NULL;
Thomas Roessler's avatar
Thomas Roessler committed
257 258 259 260 261 262 263 264 265 266 267 268

      if (*tok->dptr == '{')
      {
	tok->dptr++;
	if ((pc = strchr (tok->dptr, '}')))
	{
	  var = mutt_substrdup (tok->dptr, pc);
	  tok->dptr = pc + 1;
	}
      }
      else
      {
Thomas Roessler's avatar
Thomas Roessler committed
269
	for (pc = tok->dptr; isalpha ((unsigned char) *pc) || *pc == '_'; pc++)
Thomas Roessler's avatar
Thomas Roessler committed
270 271 272 273
	  ;
	var = mutt_substrdup (tok->dptr, pc);
	tok->dptr = pc;
      }
274
      if (var && (env = getenv (var)))
275
	mutt_buffer_addstr (dest, env);
Thomas Roessler's avatar
Thomas Roessler committed
276 277 278
      FREE (&var);
    }
    else
279
      mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
280
  }
281
  mutt_buffer_addch (dest, 0); /* terminate the string */
Thomas Roessler's avatar
Thomas Roessler committed
282 283 284 285
  SKIPWS (tok->dptr);
  return 0;
}

286
static void add_to_list (LIST **list, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
287 288 289
{
  LIST *t, *last = NULL;

290 291 292 293
  /* don't add a NULL or empty string to the list */
  if (!str || *str == '\0')
    return;

294 295
  /* check to make sure the item is not already on this list */
  for (last = *list; last; last = last->next)
Thomas Roessler's avatar
Thomas Roessler committed
296
  {
297
    if (ascii_strcasecmp (str, last->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
298
    {
299 300 301
      /* already on the list, so just ignore it */
      last = NULL;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
302
    }
303 304 305
    if (!last->next)
      break;
  }
Thomas Roessler's avatar
Thomas Roessler committed
306

307 308 309 310 311
  if (!*list || last)
  {
    t = (LIST *) safe_calloc (1, sizeof (LIST));
    t->data = safe_strdup (str);
    if (last)
Thomas Roessler's avatar
Thomas Roessler committed
312
    {
313 314
      last->next = t;
      last = last->next;
Thomas Roessler's avatar
Thomas Roessler committed
315
    }
316 317
    else
      *list = last = t;
Thomas Roessler's avatar
Thomas Roessler committed
318 319 320
  }
}

321
static void remove_from_list (LIST **l, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
322 323 324
{
  LIST *p, *last = NULL;

325 326 327
  if (mutt_strcmp ("*", str) == 0)
    mutt_free_list (l);    /* ``unCMD *'' means delete all current entries */
  else
Thomas Roessler's avatar
Thomas Roessler committed
328
  {
329 330 331
    p = *l;
    last = NULL;
    while (p)
Thomas Roessler's avatar
Thomas Roessler committed
332
    {
333
      if (ascii_strcasecmp (str, p->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
334
      {
335
	FREE (&p->data);
336 337
	if (last)
	  last->next = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
338
	else
339
	  (*l) = p->next;
340
	FREE (&p);
341 342 343 344 345
      }
      else
      {
	last = p;
	p = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
346 347 348 349 350 351 352
      }
    }
  }
}

static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
353 354 355
  do
  {
    mutt_extract_token (buf, s, 0);
356 357 358 359 360

    /* don't add "*" to the unignore list */
    if (strcmp (buf->data, "*")) 
      add_to_list (&UnIgnore, buf->data);

361 362 363 364
    remove_from_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
365 366 367 368 369
  return 0;
}

static int parse_ignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
370 371 372 373 374 375 376 377
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_list (&UnIgnore, buf->data);
    add_to_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
378 379 380 381 382
  return 0;
}

static int parse_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
383 384 385 386 387 388 389
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
390 391 392 393 394
  return 0;
}

static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
395 396 397
  do
  {
    mutt_extract_token (buf, s, 0);
398 399 400 401 402 403 404 405
    /*
     * Check for deletion of entire list
     */
    if (mutt_strcmp (buf->data, "*") == 0)
    {
      mutt_free_list ((LIST **) data);
      break;
    }
406 407 408 409
    remove_from_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
410 411 412
  return 0;
}

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

static int parse_unlists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_list (&MailLists, buf->data);
    remove_from_list (&SubscribedLists, buf->data);
  }
  while (MoreArgs (s));

  return 0;
}

static int parse_subscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list (&MailLists, buf->data);
    add_to_list (&SubscribedLists, buf->data);
  }
  while (MoreArgs (s));

  return 0;
}
  
Thomas Roessler's avatar
Thomas Roessler committed
440 441 442 443 444 445 446 447
static int parse_unalias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp, *last = NULL;

  do
  {
    mutt_extract_token (buf, s, 0);

448
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
449
    {
450
      if (CurrentMenu == MENU_ALIAS)
Thomas Roessler's avatar
Thomas Roessler committed
451
      {
452
	for (tmp = Aliases; tmp ; tmp = tmp->next)
453
	  tmp->del = 1;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	set_option (OPTFORCEREDRAWINDEX);
      }
      else
	mutt_free_alias (&Aliases);
      break;
    }
    else
      for (tmp = Aliases; tmp; tmp = tmp->next)
      {
	if (mutt_strcasecmp (buf->data, tmp->name) == 0)
	{
	  if (CurrentMenu == MENU_ALIAS)
	  {
	    tmp->del = 1;
	    set_option (OPTFORCEREDRAWINDEX);
	    break;
	  }

	  if (last)
	    last->next = tmp->next;
	  else
	    Aliases = tmp->next;
	  tmp->next = NULL;
	  mutt_free_alias (&tmp);
478 479
	  break;
	}
480
	last = tmp;
Thomas Roessler's avatar
Thomas Roessler committed
481 482 483 484 485 486 487 488 489 490 491
      }
  }
  while (MoreArgs (s));
  return 0;
}

static int parse_alias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp = Aliases;
  ALIAS *last = NULL;

492
  if (!MoreArgs (s))
Thomas Roessler's avatar
Thomas Roessler committed
493
  {
494
    strfcpy (err->data, _("alias: no address"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
495 496 497
    return (-1);
  }

498
  mutt_extract_token (buf, s, 0);
Thomas Roessler's avatar
Thomas Roessler committed
499 500 501 502

  /* check to see if an alias with this name already exists */
  for (; tmp; tmp = tmp->next)
  {
503
    if (!mutt_strcasecmp (tmp->name, buf->data))
Thomas Roessler's avatar
Thomas Roessler committed
504 505 506 507 508 509 510 511
      break;
    last = tmp;
  }

  if (!tmp)
  {
    /* create a new alias */
    tmp = (ALIAS *) safe_calloc (1, sizeof (ALIAS));
Thomas Roessler's avatar
Thomas Roessler committed
512
    tmp->self = tmp;
513
    tmp->name = safe_strdup (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
514 515 516
    /* give the main addressbook code a chance */
    if (CurrentMenu == MENU_ALIAS)
      set_option (OPTMENUCALLER);
Thomas Roessler's avatar
Thomas Roessler committed
517 518 519 520 521
  }
  else
  {
    /* override the previous value */
    rfc822_free_address (&tmp->addr);
Thomas Roessler's avatar
Thomas Roessler committed
522
    if (CurrentMenu == MENU_ALIAS)
523
      set_option (OPTFORCEREDRAWINDEX);
Thomas Roessler's avatar
Thomas Roessler committed
524 525
  }

526
  mutt_extract_token (buf, s, M_TOKEN_QUOTE | M_TOKEN_SPACE | M_TOKEN_SEMICOLON);
Thomas Roessler's avatar
Thomas Roessler committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
  tmp->addr = mutt_parse_adrlist (tmp->addr, buf->data);
  if (last)
    last->next = tmp;
  else
    Aliases = tmp;
  return 0;
}

static int
parse_unmy_hdr (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  LIST *last = NULL;
  LIST *tmp = UserHeader;
  LIST *ptr;
  size_t l;

  do
  {
    mutt_extract_token (buf, s, 0);
546
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
547 548 549 550 551 552
      mutt_free_list (&UserHeader);
    else
    {
      tmp = UserHeader;
      last = NULL;

553
      l = mutt_strlen (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
554 555 556 557 558
      if (buf->data[l - 1] == ':')
	l--;

      while (tmp)
      {
559
	if (ascii_strncasecmp (buf->data, tmp->data, l) == 0 && tmp->data[l] == ':')
Thomas Roessler's avatar
Thomas Roessler committed
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
	{
	  ptr = tmp;
	  if (last)
	    last->next = tmp->next;
	  else
	    UserHeader = tmp->next;
	  tmp = tmp->next;
	  ptr->next = NULL;
	  mutt_free_list (&ptr);
	}
	else
	{
	  last = tmp;
	  tmp = tmp->next;
	}
      }
    }
  }
  while (MoreArgs (s));
  return 0;
}

static int parse_my_hdr (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  LIST *tmp;
  size_t keylen;
  char *p;

  mutt_extract_token (buf, s, M_TOKEN_SPACE | M_TOKEN_QUOTE);
  if ((p = strpbrk (buf->data, ": \t")) == NULL || *p != ':')
  {
591
    strfcpy (err->data, _("invalid header field"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
592 593 594 595 596 597 598 599 600
    return (-1);
  }
  keylen = p - buf->data + 1;

  if (UserHeader)
  {
    for (tmp = UserHeader; ; tmp = tmp->next)
    {
      /* see if there is already a field by this name */
601
      if (ascii_strncasecmp (buf->data, tmp->data, keylen) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
602 603
      {
	/* replace the old value */
604
	FREE (&tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	tmp->data = buf->data;
	memset (buf, 0, sizeof (BUFFER));
	return 0;
      }
      if (!tmp->next)
	break;
    }
    tmp->next = mutt_new_list ();
    tmp = tmp->next;
  }
  else
  {
    tmp = mutt_new_list ();
    UserHeader = tmp;
  }
  tmp->data = buf->data;
  memset (buf, 0, sizeof (BUFFER));
  return 0;
}

static int
parse_sort (short *val, const char *s, const struct mapping_t *map, BUFFER *err)
{
  int i, flags = 0;

630
  if (mutt_strncmp ("reverse-", s, 8) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
631 632 633 634 635
  {
    s += 8;
    flags = SORT_REVERSE;
  }
  
636
  if (mutt_strncmp ("last-", s, 5) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
637 638 639 640 641 642 643
  {
    s += 5;
    flags |= SORT_LAST;
  }

  if ((i = mutt_getvaluebyname (s, map)) == -1)
  {
644
    snprintf (err->data, err->dsize, _("%s: unknown sorting method"), s);
Thomas Roessler's avatar
Thomas Roessler committed
645 646 647 648 649 650 651 652
    return (-1);
  }

  *val = i | flags;

  return 0;
}

653 654 655 656 657
static void mutt_set_default (struct option_t *p)
{
  switch (p->type & DT_MASK)
  {
    case DT_STR:
658
      if (!p->init && *((char **) p->data))
659 660 661
        p->init = (unsigned long) safe_strdup (* ((char **) p->data));
      break;
    case DT_PATH:
662
      if (!p->init && *((char **) p->data))
663 664
      {
	char *cp = safe_strdup (*((char **) p->data));
665
	/* mutt_pretty_mailbox (cp); */
666 667 668 669
        p->init = (unsigned long) cp;
      }
      break;
    case DT_ADDR:
670
      if (!p->init && *((ADDRESS **) p->data))
671 672
      {
	char tmp[HUGE_STRING];
673
	*tmp = '\0';
674 675 676 677 678 679 680
	rfc822_write_address (tmp, sizeof (tmp), *((ADDRESS **) p->data));
	p->init = (unsigned long) safe_strdup (tmp);
      }
      break;
    case DT_RX:
    {
      REGEXP *pp = (REGEXP *) p->data;
681
      if (!p->init && pp->pattern)
682 683 684 685 686 687
	p->init = (unsigned long) safe_strdup (pp->pattern);
      break;
    }
  }
}

Thomas Roessler's avatar
Thomas Roessler committed
688 689 690 691 692 693
static void mutt_restore_default (struct option_t *p)
{
  switch (p->type & DT_MASK)
  {
    case DT_STR:
      if (p->init)
694
	mutt_str_replace ((char **) p->data, (char *) p->init); 
Thomas Roessler's avatar
Thomas Roessler committed
695 696 697 698 699 700 701 702
      break;
    case DT_PATH:
      if (p->init)
      {
	char path[_POSIX_PATH_MAX];

	strfcpy (path, (char *) p->init, sizeof (path));
	mutt_expand_path (path, sizeof (path));
703
	mutt_str_replace ((char **) p->data, path);
Thomas Roessler's avatar
Thomas Roessler committed
704 705
      }
      break;
706 707 708 709 710 711 712
    case DT_ADDR:
      if (p->init)
      {
	rfc822_free_address ((ADDRESS **) p->data);
	*((ADDRESS **) p->data) = rfc822_parse_adrlist (NULL, (char *) p->init);
      }
      break;
Thomas Roessler's avatar
Thomas Roessler committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    case DT_BOOL:
      if (p->init)
	set_option (p->data);
      else
	unset_option (p->data);
      break;
    case DT_QUAD:
      set_quadoption (p->data, p->init);
      break;
    case DT_NUM:
    case DT_SORT:
    case DT_MAGIC:
      *((short *) p->data) = p->init;
      break;
    case DT_RX:
      {
	REGEXP *pp = (REGEXP *) p->data;
Thomas Roessler's avatar
Thomas Roessler committed
730 731
	int flags = 0;

Thomas Roessler's avatar
Thomas Roessler committed
732
	FREE (&pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
733 734 735 736 737
	if (pp->rx)
	{
	  regfree (pp->rx);
	  FREE (&pp->rx);
	}
738

Thomas Roessler's avatar
Thomas Roessler committed
739 740
	if (p->init)
	{
Thomas Roessler's avatar
Thomas Roessler committed
741 742 743
	  char *s = (char *) p->init;

	  pp->rx = safe_calloc (1, sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
744
	  pp->pattern = safe_strdup ((char *) p->init);
745
	  if (mutt_strcmp (p->option, "alternates") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
746
	    flags |= REG_ICASE;
747
	  else if (mutt_strcmp (p->option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
748
	    flags |= mutt_which_case ((const char *) p->init);
749
	  if (mutt_strcmp (p->option, "mask") == 0 && *s == '!')
Thomas Roessler's avatar
Thomas Roessler committed
750 751 752 753 754
	  {
	    s++;
	    pp->not = 1;
	  }
	  if (REGCOMP (pp->rx, s, flags) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
755
	  {
756 757
	    fprintf (stderr, _("mutt_restore_default(%s): error in regexp: %s\n"),
		     p->option, pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
758 759 760
	    FREE (&pp->pattern);
	    regfree (pp->rx);
	    FREE (&pp->rx);
Thomas Roessler's avatar
Thomas Roessler committed
761 762 763 764 765
	  }
	}
      }
      break;
  }
766

Thomas Roessler's avatar
Thomas Roessler committed
767 768 769 770 771 772 773 774
  if (p->flags & R_INDEX)
    set_option (OPTFORCEREDRAWINDEX);
  if (p->flags & R_PAGER)
    set_option (OPTFORCEREDRAWPAGER);
  if (p->flags & R_RESORT_SUB)
    set_option (OPTSORTSUBTHREADS);
  if (p->flags & R_RESORT)
    set_option (OPTNEEDRESORT);
775 776
  if (p->flags & R_RESORT_INIT)
    set_option (OPTRESORTINIT);
777 778
  if (p->flags & R_TREE)
    set_option (OPTREDRAWTREE);
Thomas Roessler's avatar
Thomas Roessler committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
}

static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
{
  int idx, query, unset, inv, reset, r = 0;
  char *p, scratch[_POSIX_PATH_MAX];

  while (MoreArgs (s))
  {
    /* reset state variables */
    query = 0;
    unset = data & M_SET_UNSET;
    inv = data & M_SET_INV;
    reset = data & M_SET_RESET;

    if (*s->dptr == '?')
    {
      query = 1;
      s->dptr++;
    }
799
    else if (mutt_strncmp ("no", s->dptr, 2) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
800 801 802 803
    {
      s->dptr += 2;
      unset = !unset;
    }
804
    else if (mutt_strncmp ("inv", s->dptr, 3) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818
    {
      s->dptr += 3;
      inv = !inv;
    }
    else if (*s->dptr == '&')
    {
      reset = 1;
      s->dptr++;
    }

    /* get the variable name */
    mutt_extract_token (tmp, s, M_TOKEN_EQUAL);

    if ((idx = mutt_option_index (tmp->data)) == -1 &&
819
	!(reset && !mutt_strcmp ("all", tmp->data)))
Thomas Roessler's avatar
Thomas Roessler committed
820
    {
821
      snprintf (err->data, err->dsize, _("%s: unknown variable"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
822 823 824 825 826 827 828 829
      return (-1);
    }
    SKIPWS (s->dptr);

    if (reset)
    {
      if (query || unset || inv)
      {
830
	snprintf (err->data, err->dsize, _("prefix is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
831 832 833 834 835
	return (-1);
      }

      if (s && *s->dptr == '=')
      {
836
	snprintf (err->data, err->dsize, _("value is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
837 838 839
	return (-1);
      }
     
840
      if (!mutt_strcmp ("all", tmp->data))
Thomas Roessler's avatar
Thomas Roessler committed
841 842 843 844 845 846 847 848 849 850 851 852
      {
	for (idx = 0; MuttVars[idx].option; idx++)
	  mutt_restore_default (&MuttVars[idx]);
	return 0;
      }
      else
	mutt_restore_default (&MuttVars[idx]);
    } 
    else if (DTYPE (MuttVars[idx].type) == DT_BOOL)
    { 
      if (s && *s->dptr == '=')
      {
Thomas Roessler's avatar
Thomas Roessler committed
853 854 855 856 857 858 859 860
	if (unset || inv || query)
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}

	s->dptr++;
	mutt_extract_token (tmp, s, 0);
861
	if (ascii_strcasecmp ("yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
862
	  unset = inv = 0;
863
	else if (ascii_strcasecmp ("no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
864 865 866 867 868 869
	  unset = 1;
	else
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}
Thomas Roessler's avatar
Thomas Roessler committed
870 871 872 873
      }

      if (query)
      {
874 875
	snprintf (err->data, err->dsize, option (MuttVars[idx].data)
			? _("%s is set") : _("%s is unset"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
876 877 878 879 880 881 882 883 884 885 886
	return 0;
      }

      if (unset)
	unset_option (MuttVars[idx].data);
      else if (inv)
	toggle_option (MuttVars[idx].data);
      else
	set_option (MuttVars[idx].data);
    }
    else if (DTYPE (MuttVars[idx].type) == DT_STR ||
887 888
	     DTYPE (MuttVars[idx].type) == DT_PATH ||
	     DTYPE (MuttVars[idx].type) == DT_ADDR)
Thomas Roessler's avatar
Thomas Roessler committed
889
    {
890 891 892 893 894
      if (unset)
      {
	if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	  rfc822_free_address ((ADDRESS **) MuttVars[idx].data);
	else
895
	  FREE (MuttVars[idx].data);
896 897
      }
      else if (query || *s->dptr != '=')
Thomas Roessler's avatar
Thomas Roessler committed
898
      {
899 900 901 902 903 904 905 906 907 908 909 910
	char _tmp[STRING];
	char *val = NULL;
	
	if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	{
	  _tmp[0] = '\0';
	  rfc822_write_address (_tmp, sizeof (_tmp), *((ADDRESS **) MuttVars[idx].data));
	  val = _tmp;
	}
	else
	  val = *((char **) MuttVars[idx].data);
	
Thomas Roessler's avatar
Thomas Roessler committed
911 912
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
913
		  NONULL (val));
Thomas Roessler's avatar
Thomas Roessler committed
914 915
	break;
      }
916 917
      else
      {
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
        s->dptr++;

        /* copy the value of the string */
        if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	  rfc822_free_address ((ADDRESS **) MuttVars[idx].data);
        else
	  FREE (MuttVars[idx].data);

        mutt_extract_token (tmp, s, 0);
        if (DTYPE (MuttVars[idx].type) == DT_PATH)
        {
	  strfcpy (scratch, tmp->data, sizeof (scratch));
	  mutt_expand_path (scratch, sizeof (scratch));
	  *((char **) MuttVars[idx].data) = safe_strdup (scratch);
        }
        else if (DTYPE (MuttVars[idx].type) == DT_STR)
        {
	  *((char **) MuttVars[idx].data) = safe_strdup (tmp->data);
936 937
	  if (mutt_strcmp (MuttVars[idx].option, "charset") == 0)
	    mutt_set_charset (Charset);
938 939 940 941 942
        }
        else
        {
	  *((ADDRESS **) MuttVars[idx].data) = rfc822_parse_adrlist (NULL, tmp->data);
        }
943
      }
Thomas Roessler's avatar
Thomas Roessler committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    }
    else if (DTYPE(MuttVars[idx].type) == DT_RX)
    {
      REGEXP *ptr = (REGEXP *) MuttVars[idx].data;
      regex_t *rx;
      int e, flags = 0;

      if (query || *s->dptr != '=')
      {
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
		  NONULL (ptr->pattern));
	break;
      }

959 960
      if (option(OPTATTACHMSG) && (!mutt_strcmp(MuttVars[idx].option, "alternates")
				   || !mutt_strcmp(MuttVars[idx].option, "reply_regexp")))
961 962 963 964 965 966
      {
	snprintf (err->data, err->dsize, "Operation not permitted when in attach-message mode.");
	r = -1;
	break;
      }
      
Thomas Roessler's avatar
Thomas Roessler committed
967 968 969 970 971
      s->dptr++;

      /* copy the value of the string */
      mutt_extract_token (tmp, s, 0);

972
      if (!ptr->pattern || mutt_strcmp (ptr->pattern, tmp->data) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
973
      {
Thomas Roessler's avatar
Thomas Roessler committed
974 975
	int not = 0;

Thomas Roessler's avatar
Thomas Roessler committed
976 977
	/* $alternates is case-insensitive,
	   $mask is case-sensitive */
978
	if (mutt_strcmp (MuttVars[idx].option, "alternates") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
979
	  flags |= REG_ICASE;
980
	else if (mutt_strcmp (MuttVars[idx].option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
981
	  flags |= mutt_which_case (tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
982 983

	p = tmp->data;
984
	if (mutt_strcmp (MuttVars[idx].option, "mask") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
985 986 987 988 989 990 991 992
	{
	  if (*p == '!')
	  {
	    not = 1;
	    p++;
	  }
	}
	  
Thomas Roessler's avatar
Thomas Roessler committed
993
	rx = (regex_t *) safe_malloc (sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
994
	if ((e = REGCOMP (rx, p, flags)) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	{
	  regerror (e, rx, err->data, err->dsize);
	  regfree (rx);
	  FREE (&rx);
	  break;
	}

	/* get here only if everything went smootly */
	if (ptr->pattern)
	{
	  FREE (&ptr->pattern);
	  regfree ((regex_t *) ptr->rx);
	  FREE (&ptr->rx);
	}

	ptr->pattern = safe_strdup (tmp->data);
	ptr->rx = rx;
Thomas Roessler's avatar
Thomas Roessler committed
1012
	ptr->not = not;
Thomas Roessler's avatar
Thomas Roessler committed
1013

1014 1015
	/* $reply_regexp and $alterantes require special treatment */
	
Thomas Roessler's avatar
Thomas Roessler committed
1016
	if (Context && Context->msgcount &&
1017
	    mutt_strcmp (MuttVars[idx].option, "reply_regexp") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	{
	  regmatch_t pmatch[1];
	  int i;
	  
#define CUR_ENV Context->hdrs[i]->env
	  for (i = 0; i < Context->msgcount; i++)
	  {
	    if (CUR_ENV && CUR_ENV->subject)
	    {
	      CUR_ENV->real_subj = (regexec (ReplyRegexp.rx,
				    CUR_ENV->subject, 1, pmatch, 0)) ?
				    CUR_ENV->subject : 
				    CUR_ENV->subject + pmatch[0].rm_eo;
	    }
	  }
#undef CUR_ENV
	}
1035 1036
	
	if(Context && Context->msgcount &&
1037
	   mutt_strcmp(MuttVars[idx].option, "alternates") == 0)
1038 1039 1040 1041 1042 1043
	{
	  int i;
	  
	  for(i = 0; i < Context->msgcount; i++)
	    Context->hdrs[i]->recip_valid = 0;
	}
Thomas Roessler's avatar
Thomas Roessler committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
      }
    }
    else if (DTYPE(MuttVars[idx].type) == DT_MAGIC)
    {
      if (query || *s->dptr != '=')
      {
	switch (DefaultMagic)
	{
	  case M_MBOX:
	    p = "mbox";
	    break;
	  case M_MMDF:
	    p = "MMDF";
	    break;
1058 1059 1060
	  case M_KENDRA:
	    p = "KENDRA";
	    break;
Thomas Roessler's avatar
Thomas Roessler committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	  case M_MH:
	    p = "MH";
	    break;
	  case M_MAILDIR:
	    p = "Maildir";
	    break;
	  default:
	    p = "unknown";
	    break;
	}
	snprintf (err->data, err->dsize, "%s=%s", MuttVars[idx].option, p);
	break;
      }

      s->dptr++;

      /* copy the value of the string */
      mutt_extract_token (tmp, s, 0);
      if (mx_set_magic (tmp->data))
      {
1081
	snprintf (err->data, err->dsize, _("%s: invalid mailbox type"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1082 1083 1084 1085 1086 1087 1088
	r = -1;
	break;
      }
    }
    else if (DTYPE(MuttVars[idx].type) == DT_NUM)
    {
      short *ptr = (short *) MuttVars[idx].data;
1089 1090
      int val;
      char *t;
Thomas Roessler's avatar
Thomas Roessler committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

      if (query || *s->dptr != '=')
      {
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=%d", MuttVars[idx].option, *ptr);
	break;
      }

      s->dptr++;

      mutt_extract_token (tmp, s, 0);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
      val = strtol (tmp->data, &t, 0);

      if (!*tmp->data || *t || (short) val != val)
      {
	snprintf (err->data, err->dsize, _("%s: invalid value"), tmp->data);
	r = -1;
	break;
      }
      else
	*ptr = (short) val;
Thomas Roessler's avatar
Thomas Roessler committed
1112 1113

      /* these ones need a sanity check */
1114
      if (mutt_strcmp (MuttVars[idx].option, "history") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1115 1116 1117 1118 1119
      {
	if (*ptr < 0)
	  *ptr = 0;
	mutt_init_history ();
      }
1120
      else if (mutt_strcmp (MuttVars[idx].option, "pager_index_lines") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
      {
	if (*ptr < 0)
	  *ptr = 0;
      }
    }
    else if (DTYPE (MuttVars[idx].type) == DT_QUAD)
    {
      if (query)
      {
	char *vals[] = { "no", "yes", "ask-no", "ask-yes" };

	snprintf (err->data, err->dsize, "%s=%s", MuttVars[idx].option,
		  vals [ quadoption (MuttVars[idx].data) ]);
	break;
      }

      if (*s->dptr == '=')
      {
	s->dptr++;
	mutt_extract_token (tmp, s, 0);
1141
	if (ascii_strcasecmp ("yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1142
	  set_quadoption (MuttVars[idx].data, M_YES);
1143
	else if (ascii_strcasecmp ("no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1144
	  set_quadoption (MuttVars[idx].data, M_NO);
1145
	else if (ascii_strcasecmp ("ask-yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1146
	  set_quadoption (MuttVars[idx].data, M_ASKYES);
1147
	else if (ascii_strcasecmp ("ask-no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1148 1149 1150
	  set_quadoption (MuttVars[idx].data, M_ASKNO);
	else
	{
1151
	  snprintf (err->data, err->dsize, _("%s: invalid value"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	  r = -1;
	  break;
	}
      }
      else
      {
	if (inv)
	  toggle_quadoption (MuttVars[idx].data);
	else if (unset)
	  set_quadoption (MuttVars[idx].data, M_NO);
	else
	  set_quadoption (MuttVars[idx].data, M_YES);
      }
    }
    else if (DTYPE (MuttVars[idx].type) == DT_SORT)
    {
      const struct mapping_t *map;

      switch (MuttVars[idx].type & DT_SUBTYPE_MASK)
      {
	case DT_SORT_ALIAS:
	  map = SortAliasMethods;
	  break;
	case DT_SORT_BROWSER:
	  map = SortBrowserMethods;
	  break;
Thomas Roessler's avatar
Thomas Roessler committed
1178
	case DT_SORT_KEYS:
1179 1180
          if ((WithCrypto & APPLICATION_PGP))
            map = SortKeyMethods;
Thomas Roessler's avatar
Thomas Roessler committed
1181
	  break;
1182 1183 1184
	case DT_SORT_AUX:
	  map = SortAuxMethods;
	  break;
Thomas Roessler's avatar
Thomas Roessler committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	default:
	  map = SortMethods;
	  break;
      }

      if (query || *s->dptr != '=')
      {
	p = mutt_getnamebyvalue (*((short *) MuttVars[idx].data) & SORT_MASK, map);

	snprintf (err->data, err->dsize, "%s=%s%s%s", MuttVars[idx].option,
		  (*((short *) MuttVars[idx].data) & SORT_REVERSE) ? "reverse-" : "",
		  (*((short *) MuttVars[idx].data) & SORT_LAST) ? "last-" : "",
		  p);
	return 0;
      }
      s->dptr++;
      mutt_extract_token (tmp, s , 0);

      if (parse_sort ((short *) MuttVars[idx].data, tmp->data, map, err) == -1)
Thomas Roessler's avatar
Thomas Roessler committed
1204 1205
      {
	r = -1;
Thomas Roessler's avatar
Thomas Roessler committed
1206
	break;
Thomas Roessler's avatar
Thomas Roessler committed
1207
      }
Thomas Roessler's avatar
Thomas Roessler committed
1208 1209 1210
    }
    else
    {
1211
      snprintf (err->data, err->dsize, _("%s: unknown type"), MuttVars[idx].option);
Thomas Roessler's avatar
Thomas Roessler committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
      r = -1;
      break;
    }

    if (MuttVars[idx].flags & R_INDEX)
      set_option (OPTFORCEREDRAWINDEX);
    if (MuttVars[idx].flags & R_PAGER)
      set_option (OPTFORCEREDRAWPAGER);
    if (MuttVars[idx].flags & R_RESORT_SUB)
      set_option (OPTSORTSUBTHREADS);
    if (MuttVars[idx].flags & R_RESORT)
      set_option (OPTNEEDRESORT);
1224 1225
    if (MuttVars[idx].flags & R_RESORT_INIT)
      set_option (OPTRESORTINIT);
1226 1227
    if (MuttVars[idx].flags & R_TREE)
      set_option (OPTREDRAWTREE);
Thomas Roessler's avatar
Thomas Roessler committed
1228 1229 1230 1231
  }
  return (r);
}

1232 1233
#define MAXERRS 128

Thomas Roessler's avatar
Thomas Roessler committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
/* reads the specified initialization file.  returns -1 if errors were found
   so that we can pause to let the user know...  */
static int source_rc (const char *rcfile, BUFFER *err)
{
  FILE *f;
  int line = 0, rc = 0;
  BUFFER token;
  char *linebuf = NULL;
  size_t buflen;
  pid_t pid;

  if ((f = mutt_open_read (rcfile, &pid)) == NULL)
  {
    snprintf (err->data, err->dsize, "%s: %s", rcfile, strerror (errno));
    return (-1);
  }

  memset (&token, 0, sizeof (token));
  while ((linebuf = mutt_read_line (linebuf, &buflen, f, &line)) != NULL)
  {
    if (mutt_parse_rc_line (linebuf, &token, err) == -1)
    {
1256
      mutt_error (_("Error in %s, line %d: %s"), rcfile, line, err->data);
1257 1258
      if (--rc < -MAXERRS)
        break;
Thomas Roessler's avatar
Thomas Roessler committed
1259
    }
1260 1261 1262
    else
      if (rc < 0)
        rc = -1;
Thomas Roessler's avatar
Thomas Roessler committed
1263 1264
  }
  FREE (&token.data);
1265
  FREE (&linebuf);
Thomas Roessler's avatar
Thomas Roessler committed
1266 1267 1268 1269
  fclose (f);
  if (pid != -1)
    mutt_wait_filter (pid);
  if (rc)
1270
  {
1271
    /* the muttrc source keyword */
1272 1273 1274 1275
    snprintf (err->data, err->dsize, rc >= -MAXERRS ? _("source: errors in %s")
      : _("source: reading aborted due too many errors in %s"), rcfile);
    rc = -1;
  }
Thomas Roessler's avatar
Thomas Roessler committed
1276 1277 1278
  return (rc);
}

1279 1280
#undef MAXERRS

Thomas Roessler's avatar
Thomas Roessler committed
1281 1282 1283 1284 1285 1286
static int parse_source (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
{
  char path[_POSIX_PATH_MAX];

  if (mutt_extract_token (tmp, s, 0) != 0)
  {
1287
    snprintf (err->data, err->dsize, _("source: error at %s"), s->dptr);
Thomas Roessler's avatar
Thomas Roessler committed
1288 1289 1290 1291
    return (-1);
  }
  if (MoreArgs (s))
  {
1292
    strfcpy (err->data, _("source: too many arguments"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
    return (-1);
  }
  strfcpy (path, tmp->data, sizeof (path));
  mutt_expand_path (path, sizeof (path));
  return (source_rc (path, err));
}

/* line		command to execute

   token	scratch buffer to be used by parser.  caller should free
   		token->data when finished.  the reason for this variable is
		to avoid having to allocate and deallocate a lot of memory
		if we are parsing many lines.  the caller can pass in the
		memory to use, which avoids having to create new space for
		every call to this function.

   err		where to write error messages */
int mutt_parse_rc_line (/* const */ char *line, BUFFER *token, BUFFER *err)
{
  int i, r = -1;
  BUFFER expn;

  memset (&expn, 0, sizeof (expn));
  expn.data = expn.dptr = line;
1317
  expn.dsize = mutt_strlen (line);
Thomas Roessler's avatar
Thomas Roessler committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333

  *err->data = 0;

  SKIPWS (expn.dptr);
  while (*expn.dptr)
  {
    if (*expn.dptr == '#')
      break; /* rest of line is a comment */
    if (*expn.dptr == ';')
    {
      expn.dptr++;
      continue;
    }
    mutt_extract_token (token, &expn, 0);
    for (i = 0; Commands[i].name; i++)
    {
1334
      if (!mutt_strcmp (token->data, Commands[i].name))
Thomas Roessler's avatar
Thomas Roessler committed
1335 1336 1337 1338 1339 1340 1341 1342
      {
	if (Commands[i].func (token, &expn, Commands[i].data, err) != 0)
	  goto finish;
        break;
      }
    }
    if (!Commands[i].name)
    {
1343
      snprintf (err->data, err->dsize, _("%s: unknown command"), NONULL (token->data));
Thomas Roessler's avatar
Thomas Roessler committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
      goto finish;
    }
  }
  r = 0;
finish:
  if (expn.destroy)
    FREE (&expn.data);
  return (r);
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363

#define NUMVARS (sizeof (MuttVars)/sizeof (MuttVars[0]))
#define NUMCOMMANDS (sizeof (Commands)/sizeof (Commands[0]))
/* initial string that starts completion. No telling how much crap 
 * the user has typed so far. Allocate LONG_STRING just to be sure! */
char User_typed [LONG_STRING] = {0}; 

int  Num_matched = 0; /* Number of matches for completion */
char Completed [STRING] = {0}; /* completed string (command or variable) */
char *Matches[MAX(NUMVARS,NUMCOMMANDS) + 1]; /* all the matches + User_typed */
1364

Thomas Roessler's avatar
Thomas Roessler committed
1365 1366 1367 1368 1369 1370 1371
/* helper function for completion.  Changes the dest buffer if
   necessary/possible to aid completion.
	dest == completion result gets here.
	src == candidate for completion.
	try == user entered data for completion.
	len == length of dest buffer.
*/
1372
static void candidate (char *dest, char *try, char *src, int len)
Thomas Roessler's avatar
Thomas Roessler committed
1373
{
1374 1375
  int l;

Thomas Roessler's avatar
Thomas Roessler committed
1376 1377
  if (strstr (src, try) == src)
  {
1378 1379 1380 1381 1382 1383 1384 1385
    Matches[Num_matched++] = src;
    if (dest[0] == 0)
      strfcpy (dest, src, len);
    else
    {
      for (l = 0; src[l] && src[l] == dest[l]; l++);
      dest[l] = 0;
    }
Thomas Roessler's avatar
Thomas Roessler committed
1386 1387 1388
  }
}

1389
int mutt_command_complete (char *buffer, size_t len, int pos, int numtabs)
Thomas Roessler's avatar
Thomas Roessler committed
1390
{
1391
  char *pt = buffer;
1392
  int num;
1393
  int spaces; /* keep track of the number of leading spaces on the line */
1394 1395

  SKIPWS (buffer);
1396
  spaces = buffer - pt;
1397

1398
  pt = buffer + pos - spaces;
Thomas Roessler's avatar
Thomas Roessler committed
1399
  while ((pt > buffer) && !isspace ((unsigned char) *pt))
Thomas Roessler's avatar
Thomas Roessler committed
1400
    pt--;
1401

Thomas Roessler's avatar
Thomas Roessler committed
1402 1403
  if (pt == buffer) /* complete cmd */
  {
1404 1405
    /* first TAB. Collect all the matches */
    if (numtabs == 1)
1406
    {
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
      Num_matched = 0;
      strfcpy (User_typed, pt, sizeof (User_typed));
      memset (Matches, 0, sizeof (Matches));
      memset (Completed, 0, sizeof (Completed));
      for (num = 0; Commands[num].name; num++)
	candidate (Completed, User_typed, Commands[num].name, sizeof (Completed));
      Matches[Num_matched++] = User_typed;

      /* All matches are stored. Longest non-ambiguous string is ""
       * i.e. dont change 'buffer'. Fake successful return this time */
      if (User_typed[0] == 0)
	return 1;
1419
    }
1420

1421 1422 1423
    if (Completed[0] == 0 && User_typed[0])
      return 0;

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
     /* Num_matched will _always_ be atleast 1 since the initial
      * user-typed string is always stored */
    if (numtabs == 1 && Num_matched == 2)
      snprintf(Completed, sizeof(Completed),"%s", Matches[0]);
    else if (numtabs > 1 && Num_matched > 2)
      /* cycle thru all the matches */
      snprintf(Completed, sizeof(Completed), "%s", 
	       Matches[(numtabs - 2) % Num_matched]);

    /* return the completed command */
1434
    strncpy (buffer, Completed, len - spaces);
Thomas Roessler's avatar
Thomas Roessler committed
1435
  }
1436 1437 1438 1439
  else if (!mutt_strncmp (buffer, "set", 3)
	   || !mutt_strncmp (buffer, "unset", 5)
	   || !mutt_strncmp (buffer, "reset", 5)
	   || !mutt_strncmp (buffer, "toggle", 6))
Thomas Roessler's avatar
Thomas Roessler committed
1440
  { 		/* complete variables */
1441
    char *prefixes[] = { "no", "inv", "?", "&", 0 };
Thomas Roessler's avatar
Thomas Roessler committed
1442
    
Thomas Roessler's avatar
Thomas Roessler committed
1443
    pt++;
1444
    /* loop through all the possible prefixes (no, inv, ...) */
1445
    if (!mutt_strncmp (buffer, "set", 3))
1446 1447 1448
    {
      for (num = 0; prefixes[num]; num++)
      {