示例#1
0
文件: uitest.c 项目: jens-maus/amissl
static void TestUI(UI *ui)
{
	char buffer[64] = {'!'};

	if (ui)
	{
		char answer1 = '!', answer2 = '!';
		int ok;

		if ((ok = UI_add_info_string(ui, "Info")) < 0)
			printf("Error while adding info string\n");
		else if ((ok = UI_add_input_string(ui, "Prompt2", 0, &buffer[0], 0,
		                                   sizeof(buffer) - 1)) < 0)
			printf("Error while adding input string\n");
		else if ((ok = UI_dup_input_boolean(ui, "Boolean1",
		                                    "\n then hit <enter> or C<enter> to cancel\n",
		                                    "o", "c", UI_INPUT_FLAG_ECHO,
		                                    &answer1)) < 0)
			printf("Error while adding boolean string\n");
		else if ((ok = UI_dup_input_boolean(ui, "Boolean2",
		                                    "\n then hit <enter> or C<enter> to cancel\n",
		                                    "y", "N", UI_INPUT_FLAG_ECHO,
		                                    &answer2)) < 0)
			printf("Error while adding boolean string\n");
		else if ((ok = UI_process(ui)) < 0)
			printf("Error during UI_process\n");

		printf("Prompt result: %s\n", &buffer[0]);

		printf("Answer1: %c\n", answer1);
		printf("Answer2: %c\n", answer2);
	}
}
static int hwcrhk_insert_card(const char *prompt_info,
		      const char *wrong_info,
		      HWCryptoHook_PassphraseContext *ppctx,
		      HWCryptoHook_CallerContext *cactx)
        {
        int ok = -1;
        UI *ui;
	void *callback_data = NULL;
        UI_METHOD *ui_method = NULL;

        if (cactx)
                {
                if (cactx->ui_method)
                        ui_method = cactx->ui_method;
		if (cactx->callback_data)
			callback_data = cactx->callback_data;
                }
	if (ppctx)
		{
                if (ppctx->ui_method)
                        ui_method = ppctx->ui_method;
		if (ppctx->callback_data)
			callback_data = ppctx->callback_data;
		}
	if (ui_method == NULL)
		{
		HWCRHKerr(HWCRHK_F_HWCRHK_INSERT_CARD,
			HWCRHK_R_NO_CALLBACK);
		return -1;
		}

	ui = UI_new_method(ui_method);

	if (ui)
		{
		char answer;
		char buf[BUFSIZ];
		/* Despite what the documentation says wrong_info can be
	 	 * an empty string.
		 */
		if (wrong_info && *wrong_info)
			BIO_snprintf(buf, sizeof(buf)-1,
				"Current card: \"%s\"\n", wrong_info);
		else
			buf[0] = 0;
		ok = UI_dup_info_string(ui, buf);
		if (ok >= 0 && prompt_info)
			{
			BIO_snprintf(buf, sizeof(buf)-1,
				"Insert card \"%s\"", prompt_info);
			ok = UI_dup_input_boolean(ui, buf,
				"\n then hit <enter> or C<enter> to cancel\n",
				"\r\n", "Cc", UI_INPUT_FLAG_ECHO, &answer);
			}
		UI_add_user_data(ui, callback_data);

		if (ok >= 0)
			ok = UI_process(ui);
		UI_free(ui);

		if (ok == -2 || (ok >= 0 && answer == 'C'))
			ok = 1;
		else if (ok < 0)
			ok = -1;
		else
			ok = 0;
		}
	return ok;
	}