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 10983 - (hide annotations) (download) (as text)
Tue Oct 10 14:27:46 2023 UTC (6 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 8906 byte(s)
クリップボードString file指定が相対パスの時、設定ファイルフォルダを参照するよう修正

- Tera Term 5 の設定ファイルフォルダ
  - %APPDATA%\teraterm5
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3 nmaya 9048 * (C) 2006- 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 <string.h>
34     #include <stdlib.h>
35     #include <stdio.h>
36 doda 8445 #define _CRTDBG_MAP_ALLOC
37     #include <crtdbg.h>
38     #include <wchar.h>
39 maya 3227
40     #include "ttwinman.h"
41 doda 4769 #include "ttlib.h"
42 doda 8445 #include "codeconv.h"
43 maya 3227
44     #include "clipboar.h"
45 zmatsuo 8586 #include "fileread.h"
46 doda 8445 #include "sendmem.h"
47     #include "clipboarddlg.h"
48 maya 3227
49 doda 8445 static const wchar_t BracketStartW[] = L"\033[200~";
50     static const wchar_t BracketEndW[] = L"\033[201~";
51 doda 6456
52 doda 8445 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 zmatsuo 8737 /**
65 doda 8445 * �t�@�C�������`���������������Atext���������������������B
66     * ���������� TRUE������
67     */
68 zmatsuo 10983 static BOOL search_dictW(const wchar_t *filename, const wchar_t *text)
69 doda 8445 {
70     BOOL result = FALSE;
71 zmatsuo 10983 const wchar_t *buf_top = LoadFileWW(filename, NULL);
72 doda 8445 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 doda 6456 /*
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 zmatsuo 8736 static BOOL CheckClipboardContentW(HWND HWin, const wchar_t *str_w, BOOL AddCR, wchar_t **out_str_w)
124 doda 6456 {
125 zmatsuo 8736 INT_PTR ret;
126 doda 6456 BOOL confirm = FALSE;
127    
128 zmatsuo 8736 *out_str_w = NULL;
129 doda 6594 if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) {
130 doda 6456 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 doda 6594 if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) {
149 doda 6456 confirm = TRUE;
150     }
151     }
152     else {
153 doda 8445 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 zmatsuo 10983 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 doda 8445 }
169    
170     ret = IDOK;
171     if (confirm) {
172     clipboarddlgdata dlg_data;
173     dlg_data.strW_ptr = str_w;
174 zmatsuo 9359 dlg_data.UILanguageFileW = ts.UILanguageFileW;
175 zmatsuo 9380 dlg_data.PasteDialogSize = ts.PasteDialogSize;
176 doda 8445 ret = clipboarddlg(hInst, HWin, &dlg_data);
177 zmatsuo 9380 ts.PasteDialogSize = dlg_data.PasteDialogSize;
178 doda 8445 *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 zmatsuo 8788 static void CBSendStart(wchar_t *str_w)
196 doda 8445 {
197 zmatsuo 8788 SendMem *sm = SendMemTextW(str_w, 0);
198 zmatsuo 8920 if (sm == NULL) {
199     free(str_w);
200 doda 8445 return;
201 zmatsuo 8920 }
202 zmatsuo 8588 if (ts.PasteDelayPerLine == 0) {
203     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_NO_DELAY, 0, 0);
204 doda 8445 }
205 zmatsuo 8588 else {
206     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_PER_LINE, ts.PasteDelayPerLine, 0);
207     }
208 doda 8445 #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 zmatsuo 8796 if (str_w == NULL || !IsTextW(str_w, 0)) {
230 doda 8445 // �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 zmatsuo 8459 {
240 zmatsuo 8919 // ���s�� CR+LF �����K���A�_�C�A���O�����s���������\����������
241     wchar_t *dest = NormalizeLineBreakCRLF(str_w);
242 doda 8445 free(str_w);
243     str_w = dest;
244     }
245    
246     if (!CheckClipboardContentW(HWin, str_w, AddCR, &str_w_edited)) {
247 zmatsuo 8738 free(str_w);
248 doda 8445 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 zmatsuo 8920 {
264     // ���s�� CR ���������K��
265     wchar_t *dest = NormalizeLineBreakCR(str_w, 0);
266     free(str_w);
267     str_w = dest;
268     }
269    
270 doda 8445 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 zmatsuo 8788 CBSendStart(str_w);
289 doda 8445 }
290    
291 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
292     {
293 doda 8445 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 zmatsuo 8796 if (str_w == NULL || !IsTextW(str_w, 0)) {
307 doda 8445 // �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 zmatsuo 8788 CBSendStart(str_w);
356 doda 8445
357     return;
358    
359     error:
360     free(str_w);
361     free(str_mb);
362     free(str_b64);
363     return;
364     }

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