Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/clipboar.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8919 - (hide annotations) (download) (as text)
Fri Aug 21 15:24:32 2020 UTC (3 years, 7 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 13498 byte(s)
改行変換を ttlib へ移動

- wchar_t *NormalizeLineBreakCR(const wchar_t *src, size_t *len);
- wchar_t *NormalizeLineBreakCRLF(const wchar_t *src_);
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3 zmatsuo 8459 * (C) 2006-2020 TeraTerm Project
4 doda 6806 * All rights reserved.
5     *
6 doda 6841 * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9 doda 6806 *
10 doda 6841 * 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 doda 6806 *
18 doda 6841 * 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 doda 6806 */
29 maya 3227
30     /* TERATERM.EXE, Clipboard routines */
31     #include "teraterm.h"
32     #include "tttypes.h"
33     #include "vtdisp.h"
34 doda 7140 #include "vtterm.h"
35 maya 3227 #include <string.h>
36     #include <stdlib.h>
37     #include <stdio.h>
38 yutakapon 3635 #include <commctrl.h>
39 doda 8445 #define _CRTDBG_MAP_ALLOC
40     #include <crtdbg.h>
41     #include <wchar.h>
42 maya 3227
43     #include "ttwinman.h"
44     #include "ttcommon.h"
45 doda 4769 #include "ttlib.h"
46 zmatsuo 7509 #include "dlglib.h"
47 doda 8445 #include "codeconv.h"
48 maya 3227
49     #include "clipboar.h"
50     #include "tt_res.h"
51 zmatsuo 8586 #include "fileread.h"
52 doda 8445 #include "sendmem.h"
53     #include "clipboarddlg.h"
54 maya 3227
55     // for clipboard paste
56     static HGLOBAL CBMemHandle = NULL;
57     static PCHAR CBMemPtr = NULL;
58     static LONG CBMemPtr2 = 0;
59     static BYTE CBByte;
60     static BOOL CBRetrySend;
61     static BOOL CBRetryEcho;
62     static BOOL CBSendCR;
63 doda 3974 static BOOL CBEchoOnly;
64 doda 5259 static BOOL CBInsertDelay = FALSE;
65 maya 3227
66 doda 8445 static const wchar_t BracketStartW[] = L"\033[200~";
67     static const wchar_t BracketEndW[] = L"\033[201~";
68 doda 6456
69 doda 8445 static void CBEcho(void);
70    
71     /**
72     * ���������M
73     * ���������������g�p����������
74     * - DDE(TTL)
75     * - �u���[�h�L���X�g
76     */
77 doda 6440 void CBStartSend(PCHAR DataPtr, int DataSize, BOOL EchoOnly)
78 maya 3227 {
79 doda 6440 if (! cv.Ready) {
80     return;
81     }
82     if (TalkStatus!=IdTalkKeyb) {
83     return;
84     }
85    
86     CBEchoOnly = EchoOnly;
87    
88 doda 6452 if (CBMemHandle) {
89     GlobalFree(CBMemHandle);
90     }
91 doda 6440 CBMemHandle = NULL;
92     CBMemPtr = NULL;
93     CBMemPtr2 = 0;
94    
95     CBInsertDelay = FALSE;
96    
97     CBRetrySend = FALSE;
98     CBRetryEcho = FALSE;
99     CBSendCR = FALSE;
100    
101 doda 6535 if ((CBMemHandle = GlobalAlloc(GHND, DataSize+1)) != NULL) {
102 doda 6440 if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
103     memcpy(CBMemPtr, DataPtr, DataSize);
104 doda 6535 // WM_COPYDATA ���������������f�[�^�� NUL Terminate ���������������� NUL ���t������
105     CBMemPtr[DataSize] = 0;
106 doda 6440 GlobalUnlock(CBMemHandle);
107     CBMemPtr=NULL;
108     TalkStatus=IdTalkCB;
109     }
110     }
111     if (TalkStatus != IdTalkCB) {
112     CBEndPaste();
113     }
114     }
115    
116 doda 8445 static void TrimTrailingNLW(wchar_t *src)
117     {
118     wchar_t *tail = src + wcslen(src) - 1;
119     while(tail >= src) {
120     if (*tail != L'\r' && *tail != L'\n') {
121     break;
122     }
123     *tail = L'\0';
124     tail--;
125     }
126     }
127    
128 zmatsuo 8737 /**
129 doda 8445 * �t�@�C�������`���������������Atext���������������������B
130     * ���������� TRUE������
131     */
132     static BOOL search_dictW(char *filename, const wchar_t *text)
133     {
134     BOOL result = FALSE;
135     const wchar_t *buf_top = LoadFileWA(filename, NULL);
136     const wchar_t *buf = buf_top;
137     if (buf == NULL) {
138     return FALSE;
139     }
140    
141     for(;;) {
142     const wchar_t *line_end;
143     size_t len;
144     wchar_t *search_str;
145     if (*buf == 0) {
146     break;
147     }
148     if (*buf == '\r' || *buf == '\n') {
149     buf++;
150     continue;
151     }
152     line_end = wcspbrk(buf, L"\r\n");
153     if (line_end == NULL) {
154     // ���s������
155     len = wcslen(buf);
156     if (len == 0) {
157     // �I��
158     break;
159     }
160     } else {
161     len = line_end - buf;
162     }
163     search_str = (wchar_t *)malloc(sizeof(wchar_t) * (len+1));
164     if (search_str == NULL)
165     continue;
166     memcpy(search_str, buf, sizeof(wchar_t) * len);
167     search_str[len] = 0;
168     buf += len;
169     result = (wcsstr(text, search_str) != NULL);
170     free(search_str);
171     if (result) {
172     result = TRUE;
173     break;
174     }
175     }
176     free((void *)buf_top);
177     return result;
178     }
179    
180 doda 6456 /*
181     * �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B
182     *
183     * �����l:
184     * TRUE -> ���������A�\���t�������{
185     * FALSE -> �\���t�����~
186     */
187 zmatsuo 8736 static BOOL CheckClipboardContentW(HWND HWin, const wchar_t *str_w, BOOL AddCR, wchar_t **out_str_w)
188 doda 6456 {
189 zmatsuo 8736 INT_PTR ret;
190 doda 6456 BOOL confirm = FALSE;
191    
192 zmatsuo 8736 *out_str_w = NULL;
193 doda 6594 if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) {
194 doda 6456 return TRUE;
195     }
196    
197     /*
198     * ConfirmChangePasteCR ����������
199     * �����������������������B
200     *
201     * ChangePasteCR !ChangePasteCR
202     * AddCR !AddCR AddCR !AddCR
203     * ���s���� o o x(2) o
204     * ���s���� o(1) x x x
205     *
206     * ChangePasteCR �� AddCR �������m�F���s����(1���m�F����)���������������A
207     * !ChangePasteCR �������l�������AAddCR ���������� CR ����������������
208     * �m�F���s������������ 2 �����������m�F���s�p���������v�\�������������B
209     * 2 ����������������������?
210     */
211     if (AddCR) {
212 doda 6594 if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) {
213 doda 6456 confirm = TRUE;
214     }
215     }
216     else {
217 doda 8445 size_t pos = wcscspn(str_w, L"\r\n"); // ���s����������������
218     if (str_w[pos] != '\0') {
219     confirm = TRUE;
220     }
221     }
222    
223     // �������T�[�`����
224     if (!confirm && search_dictW(ts.ConfirmChangePasteStringFile, str_w)) {
225     confirm = TRUE;
226     }
227    
228     ret = IDOK;
229     if (confirm) {
230     clipboarddlgdata dlg_data;
231     dlg_data.strW_ptr = str_w;
232     dlg_data.UILanguageFile = ts.UILanguageFile;
233     ret = clipboarddlg(hInst, HWin, &dlg_data);
234     *out_str_w = dlg_data.strW_edited_ptr;
235     }
236    
237     if (ret == IDOK) {
238     return TRUE;
239     }
240     else {
241     return FALSE;
242     }
243     }
244    
245     /**
246     * �N���b�v�{�[�h�p�e�L�X�g���M����
247     *
248     * @param str_w �����������|�C���^
249     * malloc()�������o�b�t�@�A���M��������������free()������
250     */
251 zmatsuo 8788 static void CBSendStart(wchar_t *str_w)
252 doda 8445 {
253 zmatsuo 8788 SendMem *sm = SendMemTextW(str_w, 0);
254 doda 8445 if (sm == NULL)
255     return;
256 zmatsuo 8588 if (ts.PasteDelayPerLine == 0) {
257     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_NO_DELAY, 0, 0);
258 doda 8445 }
259 zmatsuo 8588 else {
260     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_PER_LINE, ts.PasteDelayPerLine, 0);
261     }
262 doda 8445 #if 0
263     SendMemInitDialog(sm, hInst, HVTWin, ts.UILanguageFile);
264     SendMemInitDialogCaption(sm, L"from clipboard");
265     SendMemInitDialogFilename(sm, L"Clipboard");
266     #endif
267     SendMemStart(sm);
268     }
269    
270     void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
271     {
272     // unsigned int StrLen = 0;
273     wchar_t *str_w;
274     wchar_t *str_w_edited;
275    
276     if (! cv.Ready) {
277     return;
278     }
279     if (TalkStatus!=IdTalkKeyb) {
280     return;
281     }
282    
283     CBEchoOnly = FALSE;
284    
285     str_w = GetClipboardTextW(HWin, FALSE);
286 zmatsuo 8796 if (str_w == NULL || !IsTextW(str_w, 0)) {
287 doda 8445 // �N���b�v�{�[�h����������������������������
288     CBEndPaste();
289     return;
290     }
291    
292     if (ts.PasteFlag & CPF_TRIM_TRAILING_NL) {
293     // �o�b�t�@���������s������
294     TrimTrailingNLW(str_w);
295     }
296    
297 zmatsuo 8459 {
298 zmatsuo 8919 // ���s�� CR+LF �����K���A�_�C�A���O�����s���������\����������
299     wchar_t *dest = NormalizeLineBreakCRLF(str_w);
300 doda 8445 free(str_w);
301     str_w = dest;
302     }
303    
304     if (!CheckClipboardContentW(HWin, str_w, AddCR, &str_w_edited)) {
305 zmatsuo 8738 free(str_w);
306 doda 8445 CBEndPaste();
307     return;
308     }
309     if (str_w_edited != NULL) {
310     // �_�C�A���O�����W������
311     free(str_w);
312     str_w = str_w_edited;
313     }
314    
315     if (AddCR) {
316     size_t str_len = wcslen(str_w) + 2;
317     str_w = realloc(str_w, sizeof(wchar_t) * str_len);
318     str_w[str_len-2] = L'\r';
319     str_w[str_len-1] = 0;
320     }
321    
322     if (Bracketed) {
323     const size_t BracketStartLenW = _countof(BracketStartW) - 1;
324     const size_t BracketEndLenW = _countof(BracketEndW) - 1;
325     size_t str_len = wcslen(str_w);
326     size_t dest_len = str_len + BracketStartLenW + BracketEndLenW;
327     wchar_t *dest = malloc(sizeof(wchar_t) * (dest_len+1));
328     size_t i = 0;
329     wmemcpy(&dest[i], BracketStartW, BracketStartLenW);
330     i += BracketStartLenW;
331     wmemcpy(&dest[i], str_w, str_len);
332     i += str_len;
333     wmemcpy(&dest[i], BracketEndW, BracketEndLenW);
334     i += BracketEndLenW;
335     dest[i] = 0;
336     free(str_w);
337     str_w = dest;
338     }
339    
340 zmatsuo 8788 CBSendStart(str_w);
341 doda 8445 }
342    
343 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
344     {
345 doda 8445 size_t mb_len, b64_len, header_len = 0, footer_len = 0;
346     wchar_t *str_w = NULL;
347     char *str_mb = NULL;
348     char *str_b64 = NULL;
349    
350     if (! cv.Ready) {
351     return;
352     }
353     if (TalkStatus!=IdTalkKeyb) {
354     return;
355     }
356    
357     CBEchoOnly = FALSE;
358    
359     str_w = GetClipboardTextW(HWin, FALSE);
360 zmatsuo 8796 if (str_w == NULL || !IsTextW(str_w, 0)) {
361 doda 8445 // �N���b�v�{�[�h����������������������������
362     goto error;
363     }
364    
365     if (ts.Language == IdUtf8 || ts.KanjiCodeSend == IdUTF8) {
366     str_mb = ToU8W(str_w);
367     }
368     else {
369     str_mb = ToCharW(str_w);
370     }
371    
372     if (str_mb == NULL) {
373     goto error;
374     }
375    
376     if (header != NULL) {
377     header_len = strlen(header);
378     }
379     if (footer != NULL) {
380     footer_len = strlen(footer);
381     }
382    
383     mb_len = strlen(str_mb);
384     b64_len = (mb_len + 2) / 3 * 4 + header_len + footer_len + 1;
385    
386     if ((str_b64 = malloc(b64_len)) == NULL) {;
387     goto error;
388     }
389    
390     if (header_len > 0) {
391     strncpy_s(str_b64, b64_len, header, _TRUNCATE);
392     }
393    
394     b64encode(str_b64 + header_len, b64_len - header_len, str_mb, mb_len);
395    
396     if (footer_len > 0) {
397     strncat_s(str_b64, b64_len, footer, _TRUNCATE);
398     }
399    
400     free(str_w);
401     if ((str_w = ToWcharA(str_b64)) == NULL) {
402     goto error;
403     }
404    
405     free(str_mb);
406     free(str_b64);
407    
408     // �\���t�����������������o����
409 zmatsuo 8788 CBSendStart(str_w);
410 doda 8445
411     return;
412    
413     error:
414     free(str_w);
415     free(str_mb);
416     free(str_b64);
417     CBEndPaste();
418     return;
419     }
420    
421 maya 3227 // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
422     //
423     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
424     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
425     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
426     // �����������B
427     // (2006.11.6 yutaka)
428     void CBSend()
429     {
430 maya 3393 int c;
431     BOOL EndFlag;
432 doda 3688 static DWORD lastcr;
433     DWORD now;
434 maya 3227
435 maya 3393 if (CBMemHandle==NULL) {
436     return;
437     }
438 maya 3227
439 doda 3974 if (CBEchoOnly) {
440     CBEcho();
441     return;
442     }
443    
444 doda 5259 if (CBInsertDelay) {
445     now = GetTickCount();
446     if (now - lastcr < (DWORD)ts.PasteDelayPerLine) {
447     return;
448     }
449 doda 3688 }
450    
451 maya 3393 if (CBRetrySend) {
452     CBRetryEcho = (ts.LocalEcho>0);
453     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
454     CBRetrySend = (c==0);
455     if (CBRetrySend) {
456     return;
457     }
458     }
459 maya 3227
460 maya 3393 if (CBRetryEcho) {
461     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
462     CBRetryEcho = (c==0);
463     if (CBRetryEcho) {
464     return;
465     }
466     }
467 maya 3227
468 maya 4009 CBMemPtr = GlobalLock(CBMemHandle);
469     if (CBMemPtr==NULL) {
470 maya 3393 return;
471     }
472 maya 3227
473 maya 3393 do {
474     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
475     CBMemPtr2++;
476     // added PasteDelayPerLine (2009.4.12 maya)
477 doda 5259 if (CBInsertDelay) {
478 doda 3690 lastcr = now;
479     CBSendCR = FALSE;
480     SetTimer(HVTWin, IdPasteDelayTimer, ts.PasteDelayPerLine, NULL);
481     break;
482     }
483 maya 3393 }
484    
485     EndFlag = (CBMemPtr[CBMemPtr2]==0);
486 doda 6449 if (! EndFlag) {
487 maya 3393 CBByte = CBMemPtr[CBMemPtr2];
488     CBMemPtr2++;
489 maya 3227 // Decoding characters which are encoded by MACRO
490     // to support NUL character sending
491     //
492     // [encoded character] --> [decoded character]
493     // 01 01 --> 00
494     // 01 02 --> 01
495 maya 3393 if (CBByte==0x01) { /* 0x01 from MACRO */
496     CBByte = CBMemPtr[CBMemPtr2];
497     CBMemPtr2++;
498     CBByte = CBByte - 1; // character just after 0x01
499     }
500     }
501     else {
502     CBEndPaste();
503     return;
504     }
505 maya 3227
506 maya 3393 if (! EndFlag) {
507     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
508     CBSendCR = (CBByte==0x0D);
509     CBRetrySend = (c==0);
510     if ((! CBRetrySend) &&
511     (ts.LocalEcho>0)) {
512     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
513     CBRetryEcho = (c==0);
514     }
515     }
516     else {
517     c=0;
518     }
519     }
520     while (c>0);
521 maya 3227
522 maya 4009 if (CBMemPtr!=NULL) {
523 maya 3393 GlobalUnlock(CBMemHandle);
524     CBMemPtr=NULL;
525     }
526 maya 3227 }
527    
528 doda 8445 static void CBEcho()
529 doda 3974 {
530     if (CBMemHandle==NULL) {
531     return;
532     }
533    
534     if (CBRetryEcho && CommTextEcho(&cv,(PCHAR)&CBByte,1) == 0) {
535     return;
536     }
537    
538     if ((CBMemPtr = GlobalLock(CBMemHandle)) == NULL) {
539     return;
540     }
541    
542     do {
543     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
544     CBMemPtr2++;
545     }
546    
547     if (CBMemPtr[CBMemPtr2] == 0) {
548     CBRetryEcho = FALSE;
549     CBEndPaste();
550     return;
551     }
552    
553     CBByte = CBMemPtr[CBMemPtr2];
554     CBMemPtr2++;
555    
556     // Decoding characters which are encoded by MACRO
557     // to support NUL character sending
558     //
559     // [encoded character] --> [decoded character]
560     // 01 01 --> 00
561     // 01 02 --> 01
562     if (CBByte==0x01) { /* 0x01 from MACRO */
563     CBByte = CBMemPtr[CBMemPtr2];
564     CBMemPtr2++;
565     CBByte = CBByte - 1; // character just after 0x01
566     }
567    
568     CBSendCR = (CBByte==0x0D);
569    
570     } while (CommTextEcho(&cv,(PCHAR)&CBByte,1) > 0);
571    
572     CBRetryEcho = TRUE;
573    
574     if (CBMemHandle != NULL) {
575     GlobalUnlock(CBMemHandle);
576     CBMemPtr=NULL;
577     }
578     }
579    
580 maya 3227 void CBEndPaste()
581     {
582 maya 3393 TalkStatus = IdTalkKeyb;
583 maya 3227
584 maya 3393 if (CBMemHandle!=NULL) {
585     if (CBMemPtr!=NULL) {
586     GlobalUnlock(CBMemHandle);
587     }
588 doda 6449 GlobalFree(CBMemHandle);
589 maya 3393 }
590 maya 3227
591 maya 3393 CBMemHandle = NULL;
592     CBMemPtr = NULL;
593     CBMemPtr2 = 0;
594 doda 3974 CBEchoOnly = FALSE;
595 doda 5259 CBInsertDelay = FALSE;
596 doda 8445 _CrtCheckMemory();
597 maya 3227 }
598    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26