| 1 |
/* |
| 2 |
* Copyright (C) 1994-1998 T. Teranishi |
| 3 |
* (C) 2006- TeraTerm Project |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. The name of the author may not be used to endorse or promote products |
| 16 |
* derived from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 19 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
/* TERATERM.EXE, Clipboard routines */ |
| 31 |
#include "teraterm.h" |
| 32 |
#include "tttypes.h" |
| 33 |
#include <string.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
#define _CRTDBG_MAP_ALLOC |
| 37 |
#include <crtdbg.h> |
| 38 |
#include <wchar.h> |
| 39 |
|
| 40 |
#include "ttwinman.h" |
| 41 |
#include "ttlib.h" |
| 42 |
#include "codeconv.h" |
| 43 |
|
| 44 |
#include "clipboar.h" |
| 45 |
#include "fileread.h" |
| 46 |
#include "sendmem.h" |
| 47 |
#include "clipboarddlg.h" |
| 48 |
|
| 49 |
static const wchar_t BracketStartW[] = L"\033[200~"; |
| 50 |
static const wchar_t BracketEndW[] = L"\033[201~"; |
| 51 |
|
| 52 |
static void TrimTrailingNLW(wchar_t *src) |
| 53 |
{ |
| 54 |
wchar_t *tail = src + wcslen(src) - 1; |
| 55 |
while(tail >= src) { |
| 56 |
if (*tail != L'\r' && *tail != L'\n') { |
| 57 |
break; |
| 58 |
} |
| 59 |
*tail = L'\0'; |
| 60 |
tail--; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* �t�@�C�������`���������������Atext���������������������B |
| 66 |
* ���������� TRUE������ |
| 67 |
*/ |
| 68 |
static BOOL search_dictW(const wchar_t *filename, const wchar_t *text) |
| 69 |
{ |
| 70 |
BOOL result = FALSE; |
| 71 |
const wchar_t *buf_top = LoadFileWW(filename, NULL); |
| 72 |
const wchar_t *buf = buf_top; |
| 73 |
if (buf == NULL) { |
| 74 |
return FALSE; |
| 75 |
} |
| 76 |
|
| 77 |
for(;;) { |
| 78 |
const wchar_t *line_end; |
| 79 |
size_t len; |
| 80 |
wchar_t *search_str; |
| 81 |
if (*buf == 0) { |
| 82 |
break; |
| 83 |
} |
| 84 |
if (*buf == '\r' || *buf == '\n') { |
| 85 |
buf++; |
| 86 |
continue; |
| 87 |
} |
| 88 |
line_end = wcspbrk(buf, L"\r\n"); |
| 89 |
if (line_end == NULL) { |
| 90 |
// ���s������ |
| 91 |
len = wcslen(buf); |
| 92 |
if (len == 0) { |
| 93 |
// �I�� |
| 94 |
break; |
| 95 |
} |
| 96 |
} else { |
| 97 |
len = line_end - buf; |
| 98 |
} |
| 99 |
search_str = (wchar_t *)malloc(sizeof(wchar_t) * (len+1)); |
| 100 |
if (search_str == NULL) |
| 101 |
continue; |
| 102 |
memcpy(search_str, buf, sizeof(wchar_t) * len); |
| 103 |
search_str[len] = 0; |
| 104 |
buf += len; |
| 105 |
result = (wcsstr(text, search_str) != NULL); |
| 106 |
free(search_str); |
| 107 |
if (result) { |
| 108 |
result = TRUE; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
free((void *)buf_top); |
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B |
| 118 |
* |
| 119 |
* �����l: |
| 120 |
* TRUE -> ���������A�\���t�������{ |
| 121 |
* FALSE -> �\���t�����~ |
| 122 |
*/ |
| 123 |
static BOOL CheckClipboardContentW(HWND HWin, const wchar_t *str_w, BOOL AddCR, wchar_t **out_str_w) |
| 124 |
{ |
| 125 |
INT_PTR ret; |
| 126 |
BOOL confirm = FALSE; |
| 127 |
|
| 128 |
*out_str_w = NULL; |
| 129 |
if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) { |
| 130 |
return TRUE; |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* ConfirmChangePasteCR ���������� |
| 135 |
* �����������������������B |
| 136 |
* |
| 137 |
* ChangePasteCR !ChangePasteCR |
| 138 |
* AddCR !AddCR AddCR !AddCR |
| 139 |
* ���s���� o o x(2) o |
| 140 |
* ���s���� o(1) x x x |
| 141 |
* |
| 142 |
* ChangePasteCR �� AddCR �������m�F���s����(1���m�F����)���������������A |
| 143 |
* !ChangePasteCR �������l�������AAddCR ���������� CR ���������������� |
| 144 |
* �m�F���s������������ 2 �����������m�F���s�p���������v�\�������������B |
| 145 |
* 2 ����������������������? |
| 146 |
*/ |
| 147 |
if (AddCR) { |
| 148 |
if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) { |
| 149 |
confirm = TRUE; |
| 150 |
} |
| 151 |
} |
| 152 |
else { |
| 153 |
size_t pos = wcscspn(str_w, L"\r\n"); // ���s���������������� |
| 154 |
if (str_w[pos] != '\0') { |
| 155 |
confirm = TRUE; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// �������T�[�`���� |
| 160 |
if (ts.ConfirmChangePasteStringFile[0] != '\0') { |
| 161 |
wchar_t *fname_rel =ToWcharA(ts.ConfirmChangePasteStringFile); |
| 162 |
wchar_t *fnameW = GetFullPathW(ts.HomeDirW, fname_rel); |
| 163 |
if (!confirm && search_dictW(fnameW, str_w)) { |
| 164 |
confirm = TRUE; |
| 165 |
} |
| 166 |
free(fnameW); |
| 167 |
free(fname_rel); |
| 168 |
} |
| 169 |
|
| 170 |
ret = IDOK; |
| 171 |
if (confirm) { |
| 172 |
clipboarddlgdata dlg_data; |
| 173 |
dlg_data.strW_ptr = str_w; |
| 174 |
dlg_data.UILanguageFileW = ts.UILanguageFileW; |
| 175 |
dlg_data.PasteDialogSize = ts.PasteDialogSize; |
| 176 |
ret = clipboarddlg(hInst, HWin, &dlg_data); |
| 177 |
ts.PasteDialogSize = dlg_data.PasteDialogSize; |
| 178 |
*out_str_w = dlg_data.strW_edited_ptr; |
| 179 |
} |
| 180 |
|
| 181 |
if (ret == IDOK) { |
| 182 |
return TRUE; |
| 183 |
} |
| 184 |
else { |
| 185 |
return FALSE; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* �N���b�v�{�[�h�p�e�L�X�g���M���� |
| 191 |
* |
| 192 |
* @param str_w �����������|�C���^ |
| 193 |
* malloc()�������o�b�t�@�A���M��������������free()������ |
| 194 |
*/ |
| 195 |
static void CBSendStart(wchar_t *str_w) |
| 196 |
{ |
| 197 |
SendMem *sm = SendMemTextW(str_w, 0); |
| 198 |
if (sm == NULL) { |
| 199 |
free(str_w); |
| 200 |
return; |
| 201 |
} |
| 202 |
if (ts.PasteDelayPerLine == 0) { |
| 203 |
SendMemInitDelay(sm, SENDMEM_DELAYTYPE_NO_DELAY, 0, 0); |
| 204 |
} |
| 205 |
else { |
| 206 |
SendMemInitDelay(sm, SENDMEM_DELAYTYPE_PER_LINE, ts.PasteDelayPerLine, 0); |
| 207 |
} |
| 208 |
#if 0 |
| 209 |
SendMemInitDialog(sm, hInst, HVTWin, ts.UILanguageFile); |
| 210 |
SendMemInitDialogCaption(sm, L"from clipboard"); |
| 211 |
SendMemInitDialogFilename(sm, L"Clipboard"); |
| 212 |
#endif |
| 213 |
SendMemStart(sm); |
| 214 |
} |
| 215 |
|
| 216 |
void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed) |
| 217 |
{ |
| 218 |
wchar_t *str_w; |
| 219 |
wchar_t *str_w_edited; |
| 220 |
|
| 221 |
if (! cv.Ready) { |
| 222 |
return; |
| 223 |
} |
| 224 |
if (TalkStatus!=IdTalkKeyb) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
str_w = GetClipboardTextW(HWin, FALSE); |
| 229 |
if (str_w == NULL || !IsTextW(str_w, 0)) { |
| 230 |
// �N���b�v�{�[�h���������������������������� |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
if (ts.PasteFlag & CPF_TRIM_TRAILING_NL) { |
| 235 |
// �o�b�t�@���������s������ |
| 236 |
TrimTrailingNLW(str_w); |
| 237 |
} |
| 238 |
|
| 239 |
{ |
| 240 |
// ���s�� CR+LF �����K���A�_�C�A���O�����s���������\���������� |
| 241 |
wchar_t *dest = NormalizeLineBreakCRLF(str_w); |
| 242 |
free(str_w); |
| 243 |
str_w = dest; |
| 244 |
} |
| 245 |
|
| 246 |
if (!CheckClipboardContentW(HWin, str_w, AddCR, &str_w_edited)) { |
| 247 |
free(str_w); |
| 248 |
return; |
| 249 |
} |
| 250 |
if (str_w_edited != NULL) { |
| 251 |
// �_�C�A���O�����W������ |
| 252 |
free(str_w); |
| 253 |
str_w = str_w_edited; |
| 254 |
} |
| 255 |
|
| 256 |
if (AddCR) { |
| 257 |
size_t str_len = wcslen(str_w) + 2; |
| 258 |
str_w = realloc(str_w, sizeof(wchar_t) * str_len); |
| 259 |
str_w[str_len-2] = L'\r'; |
| 260 |
str_w[str_len-1] = 0; |
| 261 |
} |
| 262 |
|
| 263 |
{ |
| 264 |
// ���s�� CR ���������K�� |
| 265 |
wchar_t *dest = NormalizeLineBreakCR(str_w, 0); |
| 266 |
free(str_w); |
| 267 |
str_w = dest; |
| 268 |
} |
| 269 |
|
| 270 |
if (Bracketed) { |
| 271 |
const size_t BracketStartLenW = _countof(BracketStartW) - 1; |
| 272 |
const size_t BracketEndLenW = _countof(BracketEndW) - 1; |
| 273 |
size_t str_len = wcslen(str_w); |
| 274 |
size_t dest_len = str_len + BracketStartLenW + BracketEndLenW; |
| 275 |
wchar_t *dest = malloc(sizeof(wchar_t) * (dest_len+1)); |
| 276 |
size_t i = 0; |
| 277 |
wmemcpy(&dest[i], BracketStartW, BracketStartLenW); |
| 278 |
i += BracketStartLenW; |
| 279 |
wmemcpy(&dest[i], str_w, str_len); |
| 280 |
i += str_len; |
| 281 |
wmemcpy(&dest[i], BracketEndW, BracketEndLenW); |
| 282 |
i += BracketEndLenW; |
| 283 |
dest[i] = 0; |
| 284 |
free(str_w); |
| 285 |
str_w = dest; |
| 286 |
} |
| 287 |
|
| 288 |
CBSendStart(str_w); |
| 289 |
} |
| 290 |
|
| 291 |
void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer) |
| 292 |
{ |
| 293 |
size_t mb_len, b64_len, header_len = 0, footer_len = 0; |
| 294 |
wchar_t *str_w = NULL; |
| 295 |
char *str_mb = NULL; |
| 296 |
char *str_b64 = NULL; |
| 297 |
|
| 298 |
if (! cv.Ready) { |
| 299 |
return; |
| 300 |
} |
| 301 |
if (TalkStatus!=IdTalkKeyb) { |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
str_w = GetClipboardTextW(HWin, FALSE); |
| 306 |
if (str_w == NULL || !IsTextW(str_w, 0)) { |
| 307 |
// �N���b�v�{�[�h���������������������������� |
| 308 |
goto error; |
| 309 |
} |
| 310 |
|
| 311 |
if (ts.Language == IdUtf8 || ts.KanjiCodeSend == IdUTF8) { |
| 312 |
str_mb = ToU8W(str_w); |
| 313 |
} |
| 314 |
else { |
| 315 |
str_mb = ToCharW(str_w); |
| 316 |
} |
| 317 |
|
| 318 |
if (str_mb == NULL) { |
| 319 |
goto error; |
| 320 |
} |
| 321 |
|
| 322 |
if (header != NULL) { |
| 323 |
header_len = strlen(header); |
| 324 |
} |
| 325 |
if (footer != NULL) { |
| 326 |
footer_len = strlen(footer); |
| 327 |
} |
| 328 |
|
| 329 |
mb_len = strlen(str_mb); |
| 330 |
b64_len = (mb_len + 2) / 3 * 4 + header_len + footer_len + 1; |
| 331 |
|
| 332 |
if ((str_b64 = malloc(b64_len)) == NULL) {; |
| 333 |
goto error; |
| 334 |
} |
| 335 |
|
| 336 |
if (header_len > 0) { |
| 337 |
strncpy_s(str_b64, b64_len, header, _TRUNCATE); |
| 338 |
} |
| 339 |
|
| 340 |
b64encode(str_b64 + header_len, b64_len - header_len, str_mb, mb_len); |
| 341 |
|
| 342 |
if (footer_len > 0) { |
| 343 |
strncat_s(str_b64, b64_len, footer, _TRUNCATE); |
| 344 |
} |
| 345 |
|
| 346 |
free(str_w); |
| 347 |
if ((str_w = ToWcharA(str_b64)) == NULL) { |
| 348 |
goto error; |
| 349 |
} |
| 350 |
|
| 351 |
free(str_mb); |
| 352 |
free(str_b64); |
| 353 |
|
| 354 |
// �\���t�����������������o���� |
| 355 |
CBSendStart(str_w); |
| 356 |
|
| 357 |
return; |
| 358 |
|
| 359 |
error: |
| 360 |
free(str_w); |
| 361 |
free(str_mb); |
| 362 |
free(str_b64); |
| 363 |
return; |
| 364 |
} |