示例#1
0
文件: curs_lib.c 项目: darnir/neomutt
/**
 * mutt_query_exit - Ask the user if they want to leave NeoMutt
 *
 * This function is called when the user presses the abort key.
 */
void mutt_query_exit(void)
{
  mutt_flushinp();
  curs_set(1);
  if (C_Timeout)
    mutt_getch_timeout(-1); /* restore blocking operation */
  if (mutt_yesorno(_("Exit NeoMutt?"), MUTT_YES) == MUTT_YES)
  {
    mutt_exit(1);
  }
  mutt_clear_error();
  mutt_curs_set(-1);
  SigInt = 0;
}
/* this function is called when the user presses the abort key */
void mutt_query_exit (void)
{
  mutt_flushinp ();
  curs_set (1);
  if (Timeout)
    timeout (-1); /* restore blocking operation */
  if (mutt_yesorno (_("Exit Mutt?"), 1) == 1)
  {
    endwin ();
    exit (0);
  }
  mutt_clear_error();
  mutt_curs_set (-1);
  SigInt = 0;
}
示例#3
0
文件: enter.c 项目: 0xAX/muttx
int _mutt_enter_string(char *buf, size_t buflen, int y, int x,
			int flags, int multiple, char ***files, int *numfiles,
			struct enter_state *state)
{
	int width = COLS - x - 1;
	int redraw;
	int pass =(flags & M_PASS);
	int first = 1;
	int ch, w, r;
	size_t i;
	wchar_t *tempbuf = 0;
	size_t templen = 0;
	history_class_t hclass;
	wchar_t wc;
	mbstate_t mbstate;

	int rv = 0;
	memset(&mbstate, 0, sizeof(mbstate));

	if (state->wbuf)
	{
		/* Coming back after return 1 */
		redraw = M_REDRAW_LINE;
		first = 0;
	}
	else
	{
		/* Initialise wbuf from buf */
		state->wbuflen = 0;
		state->lastchar = my_mbstowcs(&state->wbuf, &state->wbuflen, 0, buf);
		redraw = M_REDRAW_INIT;
	}

	if (flags & M_FILE)
		hclass = HC_FILE;
	else if (flags & M_EFILE)
		hclass = HC_MBOX;
	else if (flags & M_CMD)
		hclass = HC_CMD;
	else if (flags & M_ALIAS)
		hclass = HC_ALIAS;
	else if (flags & M_COMMAND)
		hclass = HC_COMMAND;
	else if (flags & M_PATTERN)
		hclass = HC_PATTERN;
	else
		hclass = HC_OTHER;

	for(;;)
	{
		if (redraw && !pass)
		{
			if (redraw == M_REDRAW_INIT)
			{
				/* Go to end of line */
				state->curpos = state->lastchar;
				state->begin = width_ceiling(state->wbuf, state->lastchar, my_wcswidth(state->wbuf, state->lastchar) - width + 1);
			}
			if (state->curpos < state->begin ||
			    my_wcswidth(state->wbuf + state->begin, state->curpos - state->begin) >= width)
				state->begin = width_ceiling(state->wbuf, state->lastchar, my_wcswidth(state->wbuf, state->curpos) - width / 2);
			move(y, x);
			w = 0;
			for(i = state->begin; i < state->lastchar; i++)
			{
				w += my_wcwidth(state->wbuf[i]);
				if (w > width)
					break;
				my_addwch(state->wbuf[i]);
			}
			clrtoeol();
			move(y, x + my_wcswidth(state->wbuf + state->begin, state->curpos - state->begin));
		}
		mutt_refresh();

		if ((ch = km_dokey(MENU_EDITOR)) == -1)
		{
			rv = -1;
			goto bye;
		}

		if (ch != OP_NULL)
		{
			first = 0;
			if (ch != OP_EDITOR_COMPLETE && ch != OP_EDITOR_COMPLETE_QUERY)
				state->tabs = 0;
			redraw = M_REDRAW_LINE;
			switch(ch)
			{
			case OP_EDITOR_HISTORY_UP:
				state->curpos = state->lastchar;
				if (mutt_history_at_scratch(hclass))
				{
					my_wcstombs(buf, buflen, state->wbuf, state->curpos);
					mutt_history_save_scratch(hclass, buf);
				}
				replace_part(state, 0, mutt_history_prev(hclass));
				redraw = M_REDRAW_INIT;
				break;

			case OP_EDITOR_HISTORY_DOWN:
				state->curpos = state->lastchar;
				if (mutt_history_at_scratch(hclass))
				{
					my_wcstombs(buf, buflen, state->wbuf, state->curpos);
					mutt_history_save_scratch(hclass, buf);
				}
				replace_part(state, 0, mutt_history_next(hclass));
				redraw = M_REDRAW_INIT;
				break;

			case OP_EDITOR_BACKSPACE:
				if (state->curpos == 0)
					BEEP();
				else
				{
					i = state->curpos;
					while(i && COMB_CHAR(state->wbuf[i - 1]))
						--i;
					if (i)
						--i;
					memmove(state->wbuf + i, state->wbuf + state->curpos,(state->lastchar - state->curpos) * sizeof(wchar_t));
					state->lastchar -= state->curpos - i;
					state->curpos = i;
				}
				break;

			case OP_EDITOR_BOL:
				state->curpos = 0;
				break;

			case OP_EDITOR_EOL:
				redraw= M_REDRAW_INIT;
				break;

			case OP_EDITOR_KILL_LINE:
				state->curpos = state->lastchar = 0;
				break;

			case OP_EDITOR_KILL_EOL:
				state->lastchar = state->curpos;
				break;

			case OP_EDITOR_BACKWARD_CHAR:
				if (state->curpos == 0)
					BEEP();
				else
				{
					while(state->curpos && COMB_CHAR(state->wbuf[state->curpos - 1]))
						state->curpos--;
					if (state->curpos)
						state->curpos--;
				}
				break;

			case OP_EDITOR_FORWARD_CHAR:
				if (state->curpos == state->lastchar)
					BEEP();
				else
				{
					++state->curpos;
					while(state->curpos < state->lastchar && COMB_CHAR(state->wbuf[state->curpos]))
						++state->curpos;
				}
				break;

			case OP_EDITOR_BACKWARD_WORD:
				if (state->curpos == 0)
					BEEP();
				else
				{
					while(state->curpos && iswspace(state->wbuf[state->curpos - 1]))
						--state->curpos;
					while(state->curpos && !iswspace(state->wbuf[state->curpos - 1]))
						--state->curpos;
				}
				break;

			case OP_EDITOR_FORWARD_WORD:
				if (state->curpos == state->lastchar)
					BEEP();
				else
				{
					while(state->curpos < state->lastchar && iswspace(state->wbuf[state->curpos]))
						++state->curpos;
					while(state->curpos < state->lastchar && !iswspace(state->wbuf[state->curpos]))
						++state->curpos;
				}
				break;

			case OP_EDITOR_CAPITALIZE_WORD:
			case OP_EDITOR_UPCASE_WORD:
			case OP_EDITOR_DOWNCASE_WORD:
				if (state->curpos == state->lastchar)
				{
					BEEP();
					break;
				}
				while(state->curpos && !iswspace(state->wbuf[state->curpos]))
					--state->curpos;
				while(state->curpos < state->lastchar && iswspace(state->wbuf[state->curpos]))
					++state->curpos;
				while(state->curpos < state->lastchar && !iswspace(state->wbuf[state->curpos]))
				{
					if (ch == OP_EDITOR_DOWNCASE_WORD)
						state->wbuf[state->curpos] = towlower(state->wbuf[state->curpos]);
					else
					{
						state->wbuf[state->curpos] = towupper(state->wbuf[state->curpos]);
						if (ch == OP_EDITOR_CAPITALIZE_WORD)
							ch = OP_EDITOR_DOWNCASE_WORD;
					}
					state->curpos++;
				}
				break;

			case OP_EDITOR_DELETE_CHAR:
				if (state->curpos == state->lastchar)
					BEEP();
				else
				{
					i = state->curpos;
					while(i < state->lastchar && COMB_CHAR(state->wbuf[i]))
						++i;
					if (i < state->lastchar)
						++i;
					while(i < state->lastchar && COMB_CHAR(state->wbuf[i]))
						++i;
					memmove(state->wbuf + state->curpos, state->wbuf + i,(state->lastchar - i) * sizeof(wchar_t));
					state->lastchar -= i - state->curpos;
				}
				break;

			case OP_EDITOR_KILL_WORD:
				/* delete to beginning of word */
				if (state->curpos != 0)
				{
					i = state->curpos;
					while(i && iswspace(state->wbuf[i - 1]))
						--i;
					if (i)
					{
						if (iswalnum(state->wbuf[i - 1]))
						{
							for(--i; i && iswalnum(state->wbuf[i - 1]); i--)
								;
						}
						else
							--i;
					}
					memmove(state->wbuf + i, state->wbuf + state->curpos,
						(state->lastchar - state->curpos) * sizeof(wchar_t));
					state->lastchar += i - state->curpos;
					state->curpos = i;
				}
				break;

			case OP_EDITOR_KILL_EOW:
				/* delete to end of word */

				/* first skip over whitespace */
				for(i = state->curpos;
				     i < state->lastchar && iswspace(state->wbuf[i]); i++)
					;

				/* if there are any characters left.. */
				if (i < state->lastchar)
				{
					/* if the current character is alphanumeric.. */
					if (iswalnum(state->wbuf[i]))
					{
						/* skip over the rest of the word consistent of only alphanumerics */
						for(; i < state->lastchar && iswalnum(state->wbuf[i]); i++)
							;
					}
					else
					{
						/* skip over one non-alphanumeric character */
						++i;
					}
				}

				memmove(state->wbuf + state->curpos, state->wbuf + i,
					(state->lastchar - i) * sizeof(wchar_t));
				state->lastchar += state->curpos - i;
				break;

			case OP_EDITOR_BUFFY_CYCLE:
				if (flags & M_EFILE)
				{
					first = 1; /* clear input if user types a real key later */
					my_wcstombs(buf, buflen, state->wbuf, state->curpos);
					mutt_buffy(buf, buflen);
					state->curpos = state->lastchar = my_mbstowcs(&state->wbuf, &state->wbuflen, 0, buf);
					break;
				}
				else if (!(flags & M_FILE))
					goto self_insert;
				/* fall through to completion routine(M_FILE) */

			case OP_EDITOR_COMPLETE:
			case OP_EDITOR_COMPLETE_QUERY:
				state->tabs++;
				if (flags & M_CMD)
				{
					for(i = state->curpos; i && !is_shell_char(state->wbuf[i-1]); i--)
						;
					my_wcstombs(buf, buflen, state->wbuf + i, state->curpos - i);
					if (tempbuf && templen == state->lastchar - i &&
					    !memcmp(tempbuf, state->wbuf + i,(state->lastchar - i) * sizeof(wchar_t)))
					{
						mutt_select_file(buf, buflen,(flags & M_EFILE) ? M_SEL_FOLDER : 0);
						set_bit(options, OPTNEEDREDRAW);
						if (*buf)
							replace_part(state, i, buf);
						rv = 1;
						goto bye;
					}
					if (!mutt_complete(buf, buflen))
					{
						templen = state->lastchar - i;
						safe_realloc(&tempbuf, templen * sizeof(wchar_t));
					}
					else
						BEEP();

					replace_part(state, i, buf);
				}
				else if (flags & M_ALIAS && ch == OP_EDITOR_COMPLETE)
				{
					/* invoke the alias-menu to get more addresses */
					for(i = state->curpos; i && state->wbuf[i-1] != ',' &&
						     state->wbuf[i-1] != ':'; i--)
						;
					for(; i < state->lastchar && state->wbuf[i] == ' '; i++)
						;
					my_wcstombs(buf, buflen, state->wbuf + i, state->curpos - i);
					r = mutt_alias_complete(buf, buflen);
					replace_part(state, i, buf);
					if (!r)
					{
						rv = 1;
						goto bye;
					}
					break;
				}
				else if (flags & M_ALIAS && ch == OP_EDITOR_COMPLETE_QUERY)
				{
					/* invoke the query-menu to get more addresses */
					if ((i = state->curpos))
					{
						for(; i && state->wbuf[i - 1] != ','; i--)
							;
						for(; i < state->curpos && state->wbuf[i] == ' '; i++)
							;
					}

					my_wcstombs(buf, buflen, state->wbuf + i, state->curpos - i);
					mutt_query_complete(buf, buflen);
					replace_part(state, i, buf);

					rv = 1;
					goto bye;
				}
				else if (flags & M_COMMAND)
				{
					my_wcstombs(buf, buflen, state->wbuf, state->curpos);
					i = strlen(buf);
					if (i && buf[i - 1] == '=' &&
					    mutt_var_value_complete(buf, buflen, i))
						state->tabs = 0;
					else if (!mutt_command_complete(buf, buflen, i, state->tabs))
						BEEP();
					replace_part(state, 0, buf);
				}
				else if (flags &(M_FILE | M_EFILE))
				{
					my_wcstombs(buf, buflen, state->wbuf, state->curpos);

					/* see if the path has changed from the last time */
					if ((!tempbuf && !state->lastchar) ||(tempbuf && templen == state->lastchar &&
									       !memcmp(tempbuf, state->wbuf, state->lastchar * sizeof(wchar_t))))
					{
						_mutt_select_file(buf, buflen,
								  ((flags & M_EFILE) ? M_SEL_FOLDER : 0) |(multiple ? M_SEL_MULTI : 0),
								   files, numfiles);
						set_bit(options, OPTNEEDREDRAW);
						if (*buf)
						{
							mutt_pretty_mailbox(buf, buflen);
							if (!pass)
								mutt_history_add(hclass, buf, 1);
							rv = 0;
							goto bye;
						}

						/* file selection cancelled */
						rv = 1;
						goto bye;
					}

					if (!mutt_complete(buf, buflen))
					{
						templen = state->lastchar;
						safe_realloc(&tempbuf, templen * sizeof(wchar_t));
						memcpy(tempbuf, state->wbuf, templen * sizeof(wchar_t));
					}
					else
						BEEP(); /* let the user know that nothing matched */
					replace_part(state, 0, buf);
				}
				else
					goto self_insert;
				break;

			case OP_EDITOR_QUOTE_CHAR:
			{
				struct event event;
				/*ADDCH(LastKey);*/
				event = mutt_getch();
				if (event.ch >= 0)
				{
					LastKey = event.ch;
					goto self_insert;
				}
			}

			case OP_EDITOR_TRANSPOSE_CHARS:
				if (state->lastchar < 2)
					BEEP();
				else
				{
					wchar_t t;

					if (state->curpos == 0)
						state->curpos = 2;
					else if (state->curpos < state->lastchar)
						++state->curpos;

					t = state->wbuf[state->curpos - 2];
					state->wbuf[state->curpos - 2] = state->wbuf[state->curpos - 1];
					state->wbuf[state->curpos - 1] = t;
				}
				break;

			default:
				BEEP();
			}
		}
		else
		{

		self_insert:

			state->tabs = 0;
			/* use the raw keypress */
			ch = LastKey;

#if KEY_ENTER
			/* treat ENTER the same as RETURN */
			if (ch == KEY_ENTER)
				ch = '\r';
#endif

			/* quietly ignore all other function keys */
			if (ch & ~0xff)
				continue;

			/* gather the octets into a wide character */
			{
				char c;
				size_t k;

				c = ch;
				k = mbrtowc(&wc, &c, 1, &mbstate);
				if (k ==(size_t)(-2))
					continue;
				else if (k && k != 1)
				{
					memset(&mbstate, 0, sizeof(mbstate));
					continue;
				}
			}

			if (first &&(flags & M_CLEAR))
			{
				first = 0;
				if (IsWPrint(wc)) /* why? */
					state->curpos = state->lastchar = 0;
			}

			if (wc == '\r' || wc == '\n')
			{
				/* Convert from wide characters */
				my_wcstombs(buf, buflen, state->wbuf, state->lastchar);
				if (!pass)
					mutt_history_add(hclass, buf, 1);

				if (multiple)
				{
					char **tfiles;
					*numfiles = 1;
					tfiles = safe_calloc(*numfiles, sizeof(char *));
					mutt_expand_path(buf, buflen);
					tfiles[0] = safe_strdup(buf);
					*files = tfiles;
				}
				rv = 0;
				goto bye;
			}
			else if (wc &&(wc < ' ' || IsWPrint(wc))) /* why? */
			{
				if (state->lastchar >= state->wbuflen)
				{
					state->wbuflen = state->lastchar + 20;
					safe_realloc(&state->wbuf, state->wbuflen * sizeof(wchar_t));
				}
				memmove(state->wbuf + state->curpos + 1, state->wbuf + state->curpos,(state->lastchar - state->curpos) * sizeof(wchar_t));
				state->wbuf[state->curpos++] = wc;
				state->lastchar++;
			}
			else
			{
				mutt_flushinp();
				BEEP();
			}
		}
	}

bye:

	mutt_reset_history_state(hclass);
	safe_free(&tempbuf);
	return rv;
}
示例#4
0
文件: main.c 项目: 0xAX/muttx
int main(int argc, char **argv)
{
	char folder[_POSIX_PATH_MAX] = "";
	int flags = 0;

	/* set default locale */
	setlocale(LC_ALL, "");
	/* initialization of main output routines with default values */
	mutt_error = mutt_message = mutt_nocurses_error;

	/* parse command line options */
        parse_argv(argc, argv);

        if (arg_version == 1)
        {
                show_version();
		exit(RETURN_SUCCESS);
        } else if (arg_version == 2)
        {
                show_version_verbose();
                exit(RETURN_SUCCESS);
        }

        if (arg_execute)
        {
                init(commands);
                is_mutt_init = true;
        }

        if (arg_expand_alias)
        {
                struct list_t *alias;

                if (!is_mutt_init)
                        init(commands);

                for(alias = aliases; alias; alias = alias->next)
                {
                        struct address *a = mutt_lookup_alias(alias->data);

                        if (a)
                        {
                                printf("alias->data %s\n", alias->data);
                                mutt_write_address_list(a, stdout, 0, 0);
                        }
                }
                exit(RETURN_SUCCESS);
        }

        if (arg_query_conf)
        {
                if (!is_mutt_init)
                        init(commands);

                if (queries)
                        return mutt_query_variables(queries);

                exit(RETURN_SUCCESS);
        }

        if (arg_emulate_mailx) {
                sendflags |= SENDMAILX;
        }

        if (arg_mailbox)
                strfcpy(folder, arg_mailbox, sizeof(folder));

        if (arg_mailbox_type)
                mx_set_magic(arg_mailbox_type);

        if (arg_resume_postponed)
                sendflags |= SENDPOSTPONED;

        /* no-curses for non-terminal session */
	if (!isatty(STDIN_FILENO))
	{
		set_bit(options, OPTNOCURSES);
		sendflags = SENDBATCH;
	}
	else
	{
		/*
		 * This must come before mutt_init() because curses needs to be started
		 * before calling the init_pair() function to set the color scheme.
		 */
		start_curses();
		/* check whether terminal status is supported */
		term_status = mutt_ts_capability();
	}

	/* Initialize crypto backends. */
	crypt_init();

	if (!is_mutt_init)
		init(commands);

	if (!bit_val(options, OPTNOCURSES))
	{
		SETCOLOR(MT_COLOR_NORMAL);
		clear();
		mutt_error = mutt_curses_error;
		mutt_message = mutt_curses_message;
	}

	/* Create the Maildir directory if it doesn't exist */
	if (!bit_val(options, OPTNOCURSES) && Maildir)
	{
		struct stat sb;
		char fpath[_POSIX_PATH_MAX];
		char msg[STRING];

		strfcpy(fpath, Maildir, sizeof(fpath));
		mutt_expand_path(fpath, sizeof(fpath));
#if USE_IMAP
		/* we're not connected yet - skip mail folder creation */
		if (!mx_is_imap(fpath))
#endif
			if (stat(fpath, &sb) == -1 && errno == ENOENT)
			{
				snprintf(msg, sizeof(msg), ("%s does not exist. Create it?"), Maildir);
				if (mutt_yesorno(msg, M_YES) == M_YES)
					if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
						mutt_error( ("Can't create %s: %s."), Maildir, strerror(errno));
			}
	}

	if (sendflags & SENDPOSTPONED)
	{
		if (!bit_val(options, OPTNOCURSES))
			mutt_flushinp();
		ci_send_message(SENDPOSTPONED, NULL, NULL, NULL, NULL);
		mutt_endwin(NULL);
	}
	else if (arg_subject || msg || sendflags || arg_include || attach)
	{
		FILE *fin = NULL;
		char buf[LONG_STRING];
		char *tempfile = NULL, *infile = NULL;
		char *bodytext = NULL;
		int rv = 0;

		if (!bit_val(options, OPTNOCURSES))
			mutt_flushinp();

		if (!msg)
			msg = mutt_new_header();
		if (!msg->env)
			msg->env = mutt_new_envelope();

		for(; addr_to; addr_to = addr_to->next)
		{
			if (url_check_scheme(addr_to->data) == U_MAILTO)
			{
				if (url_parse_mailto(msg->env, &bodytext, addr_to->data) < 0)
				{
					if (!bit_val(options, OPTNOCURSES))
						mutt_endwin(NULL);
					fputs(("Failed to parse mailto: link\n"), stderr);
					exit(RETURN_WRONG_ADDR);
				}
			}
			else
				msg->env->to = rfc822_parse_adrlist(msg->env->to, addr_to->data);
		}

		if (bit_val(options, OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
		{
			if (!bit_val(options, OPTNOCURSES))
				mutt_endwin(NULL);
			fputs(("No recipients specified.\n"), stderr);
			exit(RETURN_ERR_ARG);
		}

		if (arg_subject)
			msg->env->subject = safe_strdup(arg_subject);

                if (arg_include)
                        infile = arg_include;

		if (infile || bodytext)
		{
			if (infile)
			{
				if (mutt_strcmp("-", infile) == 0)
					fin = stdin;
				else
				{
					char path[_POSIX_PATH_MAX];

					strfcpy(path, infile, sizeof(path));
					mutt_expand_path(path, sizeof(path));
					if ((fin = fopen(path, "r")) == NULL)
					{
						if (!bit_val(options, OPTNOCURSES))
							mutt_endwin(NULL);
						perror(path);
						exit(RETURN_ERR_ARG);
					}
				}
			}

			mutt_mktemp(buf, sizeof(buf));
			tempfile = safe_strdup(buf);

			/* TODO: is the following if still needed? */
			if (tempfile)
			{
				FILE *fout;

				if ((fout = safe_fopen(tempfile, "w")) == NULL)
				{
					if (!bit_val(options, OPTNOCURSES))
						mutt_endwin(NULL);
					perror(tempfile);
					safe_fclose(&fin);
					safe_free(&tempfile);
					exit(RETURN_ERR_ARG);
				}
				if (fin)
					mutt_copy_stream(fin, fout);
				else if (bodytext)
					fputs(bodytext, fout);
				safe_fclose(&fout);
			}

			if (fin && fin != stdin)
				safe_fclose(&fin);
		}

		safe_free(&bodytext);

		if (attach)
		{
			struct list_t *t = attach;
			struct body *a = NULL;

			while(t)
			{
				if (a)
				{
					a->next = mutt_make_file_attach(t->data);
					a = a->next;
				}
				else
					msg->content = a = mutt_make_file_attach(t->data);
				if (!a)
				{
					if (!bit_val(options, OPTNOCURSES))
						mutt_endwin(NULL);
					fprintf(stderr, ("%s: unable to attach file.\n"), t->data);
					mutt_free_list(&attach);
					exit(RETURN_ERR_ARG);
				}
				t = t->next;
			}
			mutt_free_list(&attach);
		}

		rv = ci_send_message(sendflags, msg, tempfile, NULL, NULL);

		if (!bit_val(options, OPTNOCURSES))
			mutt_endwin(NULL);

		if (rv)
			exit(RETURN_ERR_ARG);
	}
	else
	{

		if (!folder[0])
			strfcpy(folder, NONULL(Spoolfile), sizeof(folder));
		mutt_expand_path(folder, sizeof(folder));

		mutt_str_replace(&CurrentFolder, folder);
		mutt_str_replace(&LastFolder, folder);
		mutt_folder_hook(folder);

		if ((Context = mx_open_mailbox(folder, flags, NULL)) || !arg_mailbox)
		{
			mutt_index_menu();
			if (Context)
				safe_free(&Context);
		}
#if USE_IMAP
		imap_logout_all();
#endif

#if USE_SASL
		mutt_sasl_done();
#endif
		mutt_free_opts();
		mutt_endwin(Errorbuf);
	}
	exit(RETURN_SUCCESS);
}