init.c 43 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 1996-8 Michael R. Elkins <[email protected]>
 * 
 *     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
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */ 

#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"
Thomas Roessler's avatar
Thomas Roessler committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45


#ifdef _PGPPATH
#include "pgp.h"
#endif



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

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

46 47 48 49 50 51 52
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
53 54 55

void set_quadoption (int opt, int flag)
{
56 57 58 59 60
  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
61 62 63 64
}

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

  return (QuadOptions[n] >> b) & 0x3;
Thomas Roessler's avatar
Thomas Roessler committed
69 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
}

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++)
97
    if (mutt_strcmp (s, MuttVars[i].option) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      return (MuttVars[i].type == DT_SYN ?  mutt_option_index ((char *) MuttVars[i].data) : i);
  return (-1);
}

static void add_char (BUFFER *buf, char ch)
{
  size_t offset;

  if (buf->dptr >= buf->data + buf->dsize)
  {
    offset = buf->dptr - buf->data;
    buf->dsize += 4;
    safe_realloc ((void **) &buf->data, buf->dsize);
    buf->dptr = buf->data + offset;
  }
  *buf->dptr++ = ch;
}

static void add_str (BUFFER *buf, const char *s)
{
118
  size_t slen = mutt_strlen (s);
Thomas Roessler's avatar
Thomas Roessler committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  size_t offset;

  if (buf->dptr + slen > buf->data + buf->dsize)
  {
    offset = buf->dptr - buf->data;
    buf->dsize += slen;
    safe_realloc ((void **) &buf->data, buf->dsize);
    buf->dptr = buf->data + offset;
  }
  memcpy (buf->dptr, s, slen);
  buf->dptr += slen;
}

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 != '\'')
    {
      switch (ch = *tok->dptr++)
      {
	case 'c':
	case 'C':
	  add_char (dest, (toupper (*tok->dptr) - '@') & 0x7f);
	  tok->dptr++;
	  break;
	case 'r':
	  add_char (dest, '\r');
	  break;
	case 'n':
	  add_char (dest, '\n');
	  break;
	case 't':
	  add_char (dest, '\t');
	  break;
	case 'f':
	  add_char (dest, '\f');
	  break;
	case 'e':
	  add_char (dest, '\033');
	  break;
	default:
Thomas Roessler's avatar
Thomas Roessler committed
185 186 187
	  if (isdigit ((unsigned char) ch) &&
	      isdigit ((unsigned char) *tok->dptr) &&
	      isdigit ((unsigned char) *(tok->dptr + 1)))
Thomas Roessler's avatar
Thomas Roessler committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	  {

	    add_char (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
	    tok->dptr += 2;
	  }
	  else
	    add_char (dest, ch);
      }
    }
    else if (ch == '^' && (flags & M_TOKEN_CONDENSE))
    {
      ch = *tok->dptr++;
      if (ch == '^')
	add_char (dest, ch);
      else if (ch == '[')
	add_char (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
204
      else if (isalpha ((unsigned char) ch))
Thomas Roessler's avatar
Thomas Roessler committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	add_char (dest, toupper (ch) - '@');
      else
      {
	add_char (dest, '^');
	add_char (dest, ch);
      }
    }
    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));
	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 */
      if (expn.data)
      {
255 256
	expnlen = mutt_strlen (expn.data);
	tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
Thomas Roessler's avatar
Thomas Roessler committed
257 258 259 260 261 262 263 264 265 266 267 268
	ptr = safe_malloc (tok->dsize);
	memcpy (ptr, expn.data, expnlen);
	strcpy (ptr + expnlen, tok->dptr);
	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
269
    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
Thomas Roessler's avatar
Thomas Roessler committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    {
      char *env, *var;

      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
284
	for (pc = tok->dptr; isalpha ((unsigned char) *pc) || *pc == '_'; pc++)
Thomas Roessler's avatar
Thomas Roessler committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	  ;
	var = mutt_substrdup (tok->dptr, pc);
	tok->dptr = pc;
      }
      if ((env = getenv (var)))
	add_str (dest, env);
      FREE (&var);
    }
    else
      add_char (dest, ch);
  }
  add_char (dest, 0); /* terminate the string */
  SKIPWS (tok->dptr);
  return 0;
}

301
static void add_to_list (LIST **list, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
302 303 304
{
  LIST *t, *last = NULL;

305 306
  /* 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
307
  {
308
    if (mutt_strcasecmp (str, last->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
309
    {
310 311 312
      /* already on the list, so just ignore it */
      last = NULL;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
313
    }
314 315 316
    if (!last->next)
      break;
  }
Thomas Roessler's avatar
Thomas Roessler committed
317

318 319 320 321 322
  if (!*list || last)
  {
    t = (LIST *) safe_calloc (1, sizeof (LIST));
    t->data = safe_strdup (str);
    if (last)
Thomas Roessler's avatar
Thomas Roessler committed
323
    {
324 325
      last->next = t;
      last = last->next;
Thomas Roessler's avatar
Thomas Roessler committed
326
    }
327 328
    else
      *list = last = t;
Thomas Roessler's avatar
Thomas Roessler committed
329 330 331
  }
}

332
static void remove_from_list (LIST **l, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
333 334 335
{
  LIST *p, *last = NULL;

336 337 338
  if (mutt_strcmp ("*", str) == 0)
    mutt_free_list (l);    /* ``unCMD *'' means delete all current entries */
  else
Thomas Roessler's avatar
Thomas Roessler committed
339
  {
340 341 342
    p = *l;
    last = NULL;
    while (p)
Thomas Roessler's avatar
Thomas Roessler committed
343
    {
344
      if (mutt_strcasecmp (str, p->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
345
      {
346 347 348
	safe_free ((void **) &p->data);
	if (last)
	  last->next = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
349
	else
350 351 352 353 354 355 356
	  (*l) = p->next;
	safe_free ((void **) &p);
      }
      else
      {
	last = p;
	p = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
357 358 359 360 361 362 363
      }
    }
  }
}

static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
364 365 366 367 368 369 370 371
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list (&UnIgnore, buf->data);
    remove_from_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
372 373 374 375 376
  return 0;
}

static int parse_ignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
377 378 379 380 381 382 383 384
  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
385 386 387 388 389
  return 0;
}

static int parse_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
390 391 392 393 394 395 396
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
397 398 399 400 401
  return 0;
}

static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
402 403 404 405 406 407 408
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

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

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

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
439 440 441 442 443 444 445 446 447 448 449
static int parse_unalias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp, *last = NULL;

  do
  {
    mutt_extract_token (buf, s, 0);

    tmp = Aliases;
    for (tmp = Aliases; tmp; tmp = tmp->next)
    {
450
      if (mutt_strcasecmp (buf->data, tmp->name) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
      {
	if (last)
	  last->next = tmp->next;
	else
	  Aliases = tmp->next;
	tmp->next = NULL;
	mutt_free_alias (&tmp);
	break;
      }
      last = tmp;
    }
  }
  while (MoreArgs (s));
  return 0;
}

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

  if ((p = strpbrk (s->dptr, " \t")) == NULL)
  {
476
    strfcpy (err->data, _("alias: no address"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
477 478 479 480 481 482 483 484
    return (-1);
  }

  len = p - s->dptr;

  /* check to see if an alias with this name already exists */
  for (; tmp; tmp = tmp->next)
  {
485
    if (!mutt_strncasecmp (tmp->name, s->dptr, len) && *(tmp->name + len) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      break;
    last = tmp;
  }

  if (!tmp)
  {
    /* create a new alias */
    tmp = (ALIAS *) safe_calloc (1, sizeof (ALIAS));
    tmp->name = safe_malloc (len + 1);
    memcpy (tmp->name, s->dptr, len);
    tmp->name[len] = 0;
  }
  else
  {
    /* override the previous value */
    rfc822_free_address (&tmp->addr);
  }
  s->dptr = p;

505
  mutt_extract_token (buf, s, M_TOKEN_QUOTE | M_TOKEN_SPACE | M_TOKEN_SEMICOLON);
Thomas Roessler's avatar
Thomas Roessler committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  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);
525
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
526 527 528 529 530 531
      mutt_free_list (&UserHeader);
    else
    {
      tmp = UserHeader;
      last = NULL;

532
      l = mutt_strlen (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
533 534 535 536 537
      if (buf->data[l - 1] == ':')
	l--;

      while (tmp)
      {
538
	if (mutt_strncasecmp (buf->data, tmp->data, l) == 0 && tmp->data[l] == ':')
Thomas Roessler's avatar
Thomas Roessler committed
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
	{
	  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 != ':')
  {
570
    strfcpy (err->data, _("invalid header field"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
571 572 573 574 575 576 577
    return (-1);
  }
  keylen = p - buf->data + 1;
  p++;
  SKIPWS (p);
  if (!*p)
  {
578
    snprintf (err->data, err->dsize, _("ignoring empty header field: %s"), buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
579 580 581 582 583 584 585 586
    return (-1);
  }

  if (UserHeader)
  {
    for (tmp = UserHeader; ; tmp = tmp->next)
    {
      /* see if there is already a field by this name */
587
      if (mutt_strncasecmp (buf->data, tmp->data, keylen) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
      {
	/* replace the old value */
	safe_free ((void **) &tmp->data);
	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;

616
  if (mutt_strncmp ("reverse-", s, 8) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
617 618 619 620 621
  {
    s += 8;
    flags = SORT_REVERSE;
  }
  
622
  if (mutt_strncmp ("last-", s, 5) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
623 624 625 626 627 628 629
  {
    s += 5;
    flags |= SORT_LAST;
  }

  if ((i = mutt_getvaluebyname (s, map)) == -1)
  {
630
    snprintf (err->data, err->dsize, _("%s: unknown sorting method"), s);
Thomas Roessler's avatar
Thomas Roessler committed
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
    return (-1);
  }

  *val = i | flags;

  return 0;
}

static void mutt_restore_default (struct option_t *p)
{
  switch (p->type & DT_MASK)
  {
    case DT_STR:
      if (p->init)
      {
	FREE (p->data);
	*((char **) p->data) = safe_strdup ((char *) p->init);
      }
      break;
    case DT_PATH:
      if (p->init)
      {
	char path[_POSIX_PATH_MAX];

	FREE (p->data);
	strfcpy (path, (char *) p->init, sizeof (path));
	mutt_expand_path (path, sizeof (path));
	*((char **) p->data) = safe_strdup (path);
      }
      break;
661 662 663 664 665 666 667
    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
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    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
685 686
	int flags = 0;

Thomas Roessler's avatar
Thomas Roessler committed
687
	FREE (&pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
688 689 690 691 692
	if (pp->rx)
	{
	  regfree (pp->rx);
	  FREE (&pp->rx);
	}
Thomas Roessler's avatar
Thomas Roessler committed
693 694
	if (p->init)
	{
Thomas Roessler's avatar
Thomas Roessler committed
695 696 697
	  char *s = (char *) p->init;

	  pp->rx = safe_calloc (1, sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
698
	  pp->pattern = safe_strdup ((char *) p->init);
699
	  if (mutt_strcmp (p->option, "alternates") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
700
	    flags |= REG_ICASE;
701
	  else if (mutt_strcmp (p->option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
702
	    flags |= mutt_which_case ((const char *) p->init);
703
	  if (mutt_strcmp (p->option, "mask") == 0 && *s == '!')
Thomas Roessler's avatar
Thomas Roessler committed
704 705 706 707 708
	  {
	    s++;
	    pp->not = 1;
	  }
	  if (REGCOMP (pp->rx, s, flags) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
709
	  {
710
	    fprintf (stderr, _("mutt_restore_default: error in regexp: %s\n"),
Thomas Roessler's avatar
Thomas Roessler committed
711
		     pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
712 713 714
	    FREE (&pp->pattern);
	    regfree (pp->rx);
	    FREE (&pp->rx);
Thomas Roessler's avatar
Thomas Roessler committed
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
	  }
	}
      }
      break;
  }
  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);
}

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++;
    }
748
    else if (mutt_strncmp ("no", s->dptr, 2) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
749 750 751 752
    {
      s->dptr += 2;
      unset = !unset;
    }
753
    else if (mutt_strncmp ("inv", s->dptr, 3) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767
    {
      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 &&
768
	!(reset && !mutt_strcmp ("all", tmp->data)))
Thomas Roessler's avatar
Thomas Roessler committed
769
    {
770
      snprintf (err->data, err->dsize, _("%s: unknown variable"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
771 772 773 774 775 776 777 778
      return (-1);
    }
    SKIPWS (s->dptr);

    if (reset)
    {
      if (query || unset || inv)
      {
779
	snprintf (err->data, err->dsize, _("prefix is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
780 781 782 783 784
	return (-1);
      }

      if (s && *s->dptr == '=')
      {
785
	snprintf (err->data, err->dsize, _("value is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
786 787 788
	return (-1);
      }
     
789
      if (!mutt_strcmp ("all", tmp->data))
Thomas Roessler's avatar
Thomas Roessler committed
790 791 792 793 794 795 796 797 798 799 800 801
      {
	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
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	if (unset || inv || query)
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}

	s->dptr++;
	mutt_extract_token (tmp, s, 0);
	if (mutt_strcasecmp ("yes", tmp->data) == 0)
	  unset = inv = 0;
	else if (mutt_strcasecmp ("no", tmp->data) == 0)
	  unset = 1;
	else
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}
Thomas Roessler's avatar
Thomas Roessler committed
819 820 821 822
      }

      if (query)
      {
823 824
	snprintf (err->data, err->dsize, option (MuttVars[idx].data)
			? _("%s is set") : _("%s is unset"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
825 826 827 828 829 830 831 832 833 834 835
	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 ||
836 837
	     DTYPE (MuttVars[idx].type) == DT_PATH ||
	     DTYPE (MuttVars[idx].type) == DT_ADDR)
Thomas Roessler's avatar
Thomas Roessler committed
838 839 840
    {
      if (query || *s->dptr != '=')
      {
841 842 843 844 845 846 847 848 849 850 851 852
	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
853 854
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
855
		  NONULL (val));
Thomas Roessler's avatar
Thomas Roessler committed
856 857 858 859 860 861
	break;
      }

      s->dptr++;

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

Thomas Roessler's avatar
Thomas Roessler committed
867
      mutt_extract_token (tmp, s, 0);
868
      if (DTYPE (MuttVars[idx].type) == DT_PATH)
Thomas Roessler's avatar
Thomas Roessler committed
869 870 871 872 873
      {
	strfcpy (scratch, tmp->data, sizeof (scratch));
	mutt_expand_path (scratch, sizeof (scratch));
	*((char **) MuttVars[idx].data) = safe_strdup (scratch);
      }
874
      else if (DTYPE (MuttVars[idx].type) == DT_STR)
Thomas Roessler's avatar
Thomas Roessler committed
875 876 877
      {
	*((char **) MuttVars[idx].data) = safe_strdup (tmp->data);
      }
878 879 880 881
      else
      {
	*((ADDRESS **) MuttVars[idx].data) = rfc822_parse_adrlist (NULL, tmp->data);
      }
Thomas Roessler's avatar
Thomas Roessler committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    }
    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;
      }

897 898
      if (option(OPTATTACHMSG) && (!mutt_strcmp(MuttVars[idx].option, "alternates")
				   || !mutt_strcmp(MuttVars[idx].option, "reply_regexp")))
899 900 901 902 903 904
      {
	snprintf (err->data, err->dsize, "Operation not permitted when in attach-message mode.");
	r = -1;
	break;
      }
      
Thomas Roessler's avatar
Thomas Roessler committed
905 906 907 908 909
      s->dptr++;

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

910
      if (!ptr->pattern || mutt_strcmp (ptr->pattern, tmp->data) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
911
      {
Thomas Roessler's avatar
Thomas Roessler committed
912 913
	int not = 0;

Thomas Roessler's avatar
Thomas Roessler committed
914 915
	/* $alternates is case-insensitive,
	   $mask is case-sensitive */
916
	if (mutt_strcmp (MuttVars[idx].option, "alternates") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
917
	  flags |= REG_ICASE;
918
	else if (mutt_strcmp (MuttVars[idx].option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
919
	  flags |= mutt_which_case (tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
920 921

	p = tmp->data;
922
	if (mutt_strcmp (MuttVars[idx].option, "mask") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
923 924 925 926 927 928 929 930
	{
	  if (*p == '!')
	  {
	    not = 1;
	    p++;
	  }
	}
	  
Thomas Roessler's avatar
Thomas Roessler committed
931
	rx = (regex_t *) safe_malloc (sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
932
	if ((e = REGCOMP (rx, p, flags)) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
	{
	  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
950
	ptr->not = not;
Thomas Roessler's avatar
Thomas Roessler committed
951

952 953
	/* $reply_regexp and $alterantes require special treatment */
	
Thomas Roessler's avatar
Thomas Roessler committed
954
	if (Context && Context->msgcount &&
955
	    mutt_strcmp (MuttVars[idx].option, "reply_regexp") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	{
	  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
	}
973 974
	
	if(Context && Context->msgcount &&
975
	   mutt_strcmp(MuttVars[idx].option, "alternates") == 0)
976 977 978 979 980 981
	{
	  int i;
	  
	  for(i = 0; i < Context->msgcount; i++)
	    Context->hdrs[i]->recip_valid = 0;
	}
Thomas Roessler's avatar
Thomas Roessler committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995
      }
    }
    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;
996 997 998
	  case M_KENDRA:
	    p = "KENDRA";
	    break;
Thomas Roessler's avatar
Thomas Roessler committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	  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))
      {
1019
	snprintf (err->data, err->dsize, _("%s: invalid mailbox type"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	r = -1;
	break;
      }
    }
    else if (DTYPE(MuttVars[idx].type) == DT_NUM)
    {
      short *ptr = (short *) MuttVars[idx].data;

      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);
      *ptr = (short) atoi (tmp->data);

      /* these ones need a sanity check */
1041
      if (mutt_strcmp (MuttVars[idx].option, "history") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1042 1043 1044 1045 1046
      {
	if (*ptr < 0)
	  *ptr = 0;
	mutt_init_history ();
      }
1047
      else if (mutt_strcmp (MuttVars[idx].option, "pager_index_lines") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
      {
	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);
1068
	if (mutt_strcasecmp ("yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1069
	  set_quadoption (MuttVars[idx].data, M_YES);
1070
	else if (mutt_strcasecmp ("no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1071
	  set_quadoption (MuttVars[idx].data, M_NO);
1072
	else if (mutt_strcasecmp ("ask-yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1073
	  set_quadoption (MuttVars[idx].data, M_ASKYES);
1074
	else if (mutt_strcasecmp ("ask-no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1075 1076 1077
	  set_quadoption (MuttVars[idx].data, M_ASKNO);
	else
	{
1078
	  snprintf (err->data, err->dsize, _("%s: invalid value"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	  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
1105 1106 1107 1108 1109
#ifdef _PGPPATH
	case DT_SORT_KEYS:
	  map = SortKeyMethods;
	  break;
#endif
Thomas Roessler's avatar
Thomas Roessler committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	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)
	break;
    }
    else
    {
1133
      snprintf (err->data, err->dsize, _("%s: unknown type"), MuttVars[idx].option);
Thomas Roessler's avatar
Thomas Roessler committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
      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);
  }
  return (r);
}

/* 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)
    {
1172
      mutt_error (_("Error in %s, line %d: %s"), rcfile, line, err->data);
Thomas Roessler's avatar
Thomas Roessler committed
1173 1174 1175 1176 1177 1178 1179 1180 1181
      rc = -1;
    }
  }
  FREE (&token.data);
  safe_free ((void **) &linebuf);
  fclose (f);
  if (pid != -1)
    mutt_wait_filter (pid);
  if (rc)
1182 1183
    /* the muttrc source keyword */
    snprintf (err->data, err->dsize, _("source: errors in %s"), rcfile);
Thomas Roessler's avatar
Thomas Roessler committed
1184 1185 1186 1187 1188 1189 1190 1191 1192
  return (rc);
}

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)
  {
1193
    snprintf (err->data, err->dsize, _("source: error at %s"), s->dptr);
Thomas Roessler's avatar
Thomas Roessler committed
1194 1195 1196 1197
    return (-1);
  }
  if (MoreArgs (s))
  {
1198
    strfcpy (err->data, _("source: too many arguments"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    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;
1223
  expn.dsize = mutt_strlen (line);
Thomas Roessler's avatar
Thomas Roessler committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

  *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++)
    {
1240
      if (!mutt_strcmp (token->data, Commands[i].name))
Thomas Roessler's avatar
Thomas Roessler committed
1241 1242 1243 1244 1245 1246 1247 1248
      {
	if (Commands[i].func (token, &expn, Commands[i].data, err) != 0)
	  goto finish;
        break;
      }
    }
    if (!Commands[i].name)
    {
1249
      snprintf (err->data, err->dsize, _("%s: unknown command"), NONULL (token->data));
Thomas Roessler's avatar
Thomas Roessler committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
      goto finish;
    }
  }
  r = 0;
finish:
  if (expn.destroy)
    FREE (&expn.data);
  return (r);
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
#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 */
1271

Thomas Roessler's avatar
Thomas Roessler committed
1272 1273 1274 1275 1276 1277 1278
/* 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.
*/
1279
static void candidate (char *dest, char *try, char *src, int len)
Thomas Roessler's avatar
Thomas Roessler committed
1280
{
1281 1282
  int l;

Thomas Roessler's avatar
Thomas Roessler committed
1283 1284
  if (strstr (src, try) == src)
  {
1285 1286 1287 1288 1289 1290 1291 1292
    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
1293 1294 1295
  }
}

1296
int mutt_command_complete (char *buffer, size_t len, int pos, int numtabs)
Thomas Roessler's avatar
Thomas Roessler committed
1297
{
1298
  char *pt = buffer;
1299
  int num;
1300
  int spaces; /* keep track of the number of leading spaces on the line */
1301 1302

  SKIPWS (buffer);
1303
  spaces = buffer - pt;
1304

1305
  pt = buffer + pos - spaces;
Thomas Roessler's avatar
Thomas Roessler committed
1306
  while ((pt > buffer) && !isspace ((unsigned char) *pt))
Thomas Roessler's avatar
Thomas Roessler committed
1307
    pt--;
1308

Thomas Roessler's avatar
Thomas Roessler committed
1309 1310
  if (pt == buffer) /* complete cmd */
  {
1311 1312
    /* first TAB. Collect all the matches */
    if (numtabs == 1)
1313
    {
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
      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;
1326
    }
1327

1328 1329 1330
    if (Completed[0] == 0 && User_typed[0])
      return 0;

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
     /* 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 */
1341
    strncpy (buffer, Completed, len - spaces);
Thomas Roessler's avatar
Thomas Roessler committed
1342
  }
1343 1344 1345 1346
  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
1347
  { 		/* complete variables */
1348
    char *prefixes[] = { "no", "inv", "?", "&", 0 };
Thomas Roessler's avatar
Thomas Roessler committed
1349
    
Thomas Roessler's avatar
Thomas Roessler committed
1350
    pt++;
1351
    /* loop through all the possible prefixes (no, inv, ...) */
1352
    if (!mutt_strncmp (buffer, "set", 3))
1353 1354 1355
    {
      for (num = 0; prefixes[num]; num++)
      {
1356
	if (!mutt_strncmp (pt, prefixes[num], mutt_strlen (prefixes[num])))
1357
	{
1358
	  pt += mutt_strlen (prefixes[num]);
1359 1360 1361 1362 1363
	  break;
	}
      }
    }
    
1364 1365
    /* first TAB. Collect all the matches */
    if (numtabs == 1)
1366
    {
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
      Num_matched = 0;
      strfcpy (User_typed, pt, sizeof (User_typed));
      memset (Matches, 0, sizeof (Matches));
      memset (Completed, 0, sizeof (Completed));
      for (num = 0; MuttVars[num].option; num++)
	candidate (Completed, User_typed, MuttVars[num].option, 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;
1379
    }
1380

1381 1382 1383
    if (Completed[0] == 0 && User_typed[0])
      return 0;

1384 1385 1386 1387 1388 1389 1390 1391 1392
    /* 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]);

1393
    strncpy (pt, Completed, buffer + len - pt - spaces);
Thomas Roessler's avatar
Thomas Roessler committed
1394
  }
1395
  else if (!mutt_strncmp (buffer, "exec", 4))
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
  {
    struct binding_t *menu = km_get_table (CurrentMenu);

    if (!menu && CurrentMenu != MENU_PAGER)
      menu = OpGeneric;
    
    pt++;
    /* first TAB. Collect all the matches */
    if (numtabs == 1)
    {
      Num_matched = 0;
      strfcpy (User_typed, pt, sizeof (User_typed));
      memset (Matches, 0, sizeof (Matches));
      memset (Completed, 0, sizeof (Completed));
      for (num = 0; menu[num].name; num++)
	candidate (Completed, User_typed, menu[num].name, sizeof (Completed));
      /* try the generic menu */
      if (Completed[0] == 0 && CurrentMenu != MENU_PAGER) 
      {
	menu = OpGeneric;
	for (num = 0; menu[num].name; num++)
	  candidate (Completed, User_typed, menu[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;
    }

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

    /* 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]);

    strncpy (pt, Completed, buffer + len - pt - spaces);
  }
Thomas Roessler's avatar
Thomas Roessler committed
1441 1442
  else
    return 0;
1443

Thomas Roessler's avatar
Thomas Roessler committed
1444 1445 1446
  return 1;
}

1447
int mutt_var_value_complete (char *buffer, size_t len, int pos)
1448
{
1449 1450
  char var[STRING], *pt = buffer;
  int spaces;
1451 1452 1453 1454
  
  if (buffer[0] == 0)
    return 0;

1455
  SKIPWS (buffer);
1456
  spaces = buffer - pt;
1457

1458
  pt = buffer + pos - spaces;
1459 1460 1461 1462 1463 1464
  while ((pt > buffer) && !isspace ((unsigned char) *pt))
    pt--;
  pt++; /* move past the space */
  if (*pt == '=') /* abort if no var before the '=' */
    return 0;

1465
  if (mutt_strncmp (buffer, "set", 3) == 0)
1466
  {
1467
    int idx;
1468 1469
    strfcpy (var, pt, sizeof (var));
    /* ignore the trailing '=' when comparing */
1470
    var[mutt_strlen (var) - 1] = 0;
1471 1472 1473
    if ((idx = mutt_option_index (var)) == -1) 
      return 0; /* no such variable. */
    else
1474
    {
1475 1476
      char tmp [LONG_STRING], tmp2[LONG_STRING];
      char *s, *d;
1477 1478
      size_t dlen = buffer + len - pt - spaces;
      char *vals[] = { "no", "yes", "ask-no", "ask-yes" };
1479 1480 1481

      tmp[0] = '\0';

1482 1483 1484
      if ((DTYPE(MuttVars[idx].type) == DT_STR) || 
	  (DTYPE(MuttVars[idx].type) == DT_PATH) ||
	  (DTYPE(MuttVars[idx].type) == DT_RX))
1485 1486 1487 1488 1489
      {
	strfcpy (tmp, NONULL (*((char **) MuttVars[idx].data)), sizeof (tmp));
	if (DTYPE (MuttVars[idx].type) == DT_PATH)
	  mutt_pretty_mailbox (tmp);
      }
1490 1491
      else if (DTYPE (MuttVars[idx].type) == DT_ADDR)
      {
1492
	rfc822_write_address (tmp, sizeof (tmp), *((ADDRESS **) MuttVars[idx].data));
1493
      }
1494
      else if (DTYPE (MuttVars[idx].type) == DT_QUAD)
1495
	strfcpy (tmp, vals[quadoption (MuttVars[idx].data)], sizeof (tmp));
1496
      else if (DTYPE (MuttVars[idx].type) == DT_NUM)
1497
	snprintf (tmp, sizeof (tmp), "%d", (*((short *) MuttVars[idx].data)));
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
      else if (DTYPE (MuttVars[idx].type) == DT_SORT)
      {
	const struct mapping_t *map;
	char *p;

	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
1511 1512 1513 1514 1515
#ifdef _PGPPATH
	  case DT_SORT_KEYS:
	    map = SortKeyMethods;
	    break;
#endif
1516 1517 1518 1519 1520
	  default:
	    map = SortMethods;
	    break;
	}
	p = mutt_getnamebyvalue (*((short *) MuttVars[idx].data) & SORT_MASK, map);
1521
	snprintf (tmp, sizeof (tmp), "%s%s%s",
1522 1523 1524 1525
		  (*((short *) MuttVars[idx].data) & SORT_REVERSE) ? "reverse-" : "",
		  (*((short *) MuttVars[idx].data) & SORT_LAST) ? "last-" : "",
		  p);
      }
Thomas Roessler's avatar
Thomas Roessler committed
1526
      else if (DTYPE (MuttVars[idx].type) == DT_BOOL)
1527
	strfcpy (tmp, option (MuttVars[idx].data) ? "yes" : "no", sizeof (tmp));
1528 1529
      else
	return 0;
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
      
      for (s = tmp, d = tmp2; *s && (d - tmp2) < sizeof (tmp2) - 2;)
      {
	if (*s == '\\' || *s == '"')
	  *d++ = '\\';
	*d++ = *s++;
      }
      *d = '\0';
      
      strfcpy (tmp, pt, sizeof (tmp));
      snprintf (pt, dlen, "%s\"%s\"", tmp, tmp2);
	  
1542
      return 1;
1543
    }
1544 1545 1546 1547
  }
  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
char *mutt_getnamebyvalue (int val, const struct mapping_t *map)
{
  int i;

  for (i=0; map[i].name; i++)
    if (map[i].value == val)
      return (map[i].name);
  return NULL;
}

int mutt_getvaluebyname (const char *name, const struct mapping_t *map)
{
  int i;

  for (i = 0; map[i].name; i++)
1563
    if (mutt_strcasecmp (map[i].name, name) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
      return (map[i].value);
  return (-1);
}

#ifdef DEBUG
static void start_debug (void)
{
  time_t t;
  int i;
  char buf[_POSIX_PATH_MAX];
  char buf2[_POSIX_PATH_MAX];

  /* rotate the old debug logs */
  for (i=3; i>=0; i--)
  {
Thomas Roessler's avatar
Thomas Roessler committed
1579 1580
    snprintf (buf, sizeof(buf), "%s/.muttdebug%d", NONULL(Homedir), i);
    snprintf (buf2, sizeof(buf2), "%s/.muttdebug%d", NONULL(Homedir), i+1);
Thomas Roessler's avatar
Thomas Roessler committed
1581 1582 1583 1584 1585
    rename (buf, buf2);
  }
  if ((debugfile = safe_fopen(buf, "w")) != NULL)
  {
    t = time (0);
1586
    setbuf (debugfile, NULL); /* don't buffer the debugging output! */
Thomas Roessler's avatar
Thomas Roessler committed
1587
    fprintf (debugfile, "Mutt %s started at %s.\nDebugging at level %d.\n\n",
1588
	     MUTT_VERSION, asctime (localtime (&t)), debuglevel);
Thomas Roessler's avatar
Thomas Roessler committed
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  }
}
#endif

static int mutt_execute_commands (LIST *p)
{
  BUFFER err, token;
  char errstr[SHORT_STRING];

  memset (&err, 0, sizeof (err));
  err.data = errstr;
  err.dsize = sizeof (errstr);
  memset (&token, 0, sizeof (token));
  for (; p; p = p->next)
  {
    if (mutt_parse_rc_line (p->data, &token, &err) != 0)
    {
1606
      fprintf (stderr, _("Error in command line: %s\n"), err.data);
Thomas Roessler's avatar
Thomas Roessler committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
      FREE (&token.data);
      return (-1);
    }
  }
  FREE (&token.data);
  return 0;
}

void mutt_init (int skip_sys_rc, LIST *commands)
{
  struct passwd *pw;
  struct utsname utsname;
  char *p, buffer[STRING], error[STRING];
  int i, default_rc = 0, need_pause = 0;
  BUFFER err;

  memset (&err, 0, sizeof (err));
  err.data = error;
  err.dsize = sizeof (error);

  /* on one of the systems I use, getcwd() does not return the same prefix
     as is listed in the passwd file */
  if ((p = getenv ("HOME")))
    Homedir = safe_strdup (p);

  /* Get some information about the user */
  if ((pw = getpwuid (getuid ())))
  {
    Username = safe_strdup (pw->pw_name);
    if (!Homedir)
      Homedir = safe_strdup (pw->pw_dir);
    if ((p = strchr (pw->pw_gecos, ',')))
      Realname = mutt_substrdup (pw->pw_gecos, p);
    else
      Realname = safe_strdup (pw->pw_gecos);
    Shell = safe_strdup (pw->pw_shell);
  }
  else 
  {
    if (!Homedir)
    {
      mutt_endwin (NULL);
1649
      fputs (_("unable to determine home directory"), stderr);
Thomas Roessler's avatar
Thomas Roessler committed
1650 1651 1652 1653 1654 1655 1656
      exit (1);
    }
    if ((p = getenv ("USER")))
      Username = safe_strdup (p);
    else
    {
      mutt_endwin (NULL);
1657
      fputs (_("unable to determine username"), stderr);
Thomas Roessler's avatar
Thomas Roessler committed
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673