• R/O
  • SSH
  • HTTPS

ttssh2: コミット


コミットメタ情報

リビジョン8624 (tree)
日時2020-03-27 00:39:14
作者zmatsuo

ログメッセージ

r7696 (19/05/20) から移植

- そのまま移植開始

変更サマリ

差分

--- branches/unicode_macro/teraterm/ttpmacro/ttl.c (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/ttl.c (nonexistent)
@@ -1,6615 +0,0 @@
1-/*
2- * Copyright (C) 1994-1998 T. Teranishi
3- * (C) 2005-2019 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-/* TTMACRO.EXE, Tera Term Language interpreter */
31-
32-#include "teraterm.h"
33-#include <stdlib.h>
34-#include <stdio.h>
35-#include <string.h>
36-#include <mbstring.h>
37-#include <time.h>
38-#include <errno.h>
39-#include "tt-version.h"
40-#include "ttmdlg.h"
41-#include "ttmbuff.h"
42-#include "ttmparse.h"
43-#include "ttmdde.h"
44-#include "ttmlib.h"
45-#include "ttlib.h"
46-#include "ttmenc.h"
47-#include "tttypes.h"
48-#include "ttmonig.h"
49-#include <shellapi.h>
50-#include <sys/stat.h>
51-#include <io.h>
52-#include <fcntl.h>
53-#include <sys/types.h>
54-#include <sys/stat.h>
55-#include <share.h>
56-
57-// for _findXXXX() functions
58-#include <io.h>
59-
60-// for _ismbblead
61-#include <mbctype.h>
62-
63-#include "ttl.h"
64-#include "SFMT.h"
65-
66-#include <winsock2.h>
67-#include <ws2tcpip.h>
68-#include <iptypes.h>
69-#include <iphlpapi.h>
70-#include "win16api.h"
71-
72-#define TTERMCOMMAND "TTERMPRO /D="
73-#define CYGTERMCOMMAND "cyglaunch -o /D="
74-
75-// for 'ExecCmnd' command
76-static BOOL ParseAgain;
77-static int IfNest;
78-static int ElseFlag;
79-static int EndIfFlag;
80-// Window handle of the main window
81-static HWND HMainWin;
82-// Timeout
83-static DWORD TimeLimit;
84-static DWORD TimeStart;
85-// for 'WaitEvent' command
86-static int WakeupCondition;
87-
88-// exit code of TTMACRO
89-int ExitCode = 0;
90-
91-// for "FindXXXX" commands
92-#define NumDirHandle 8
93-static intptr_t DirHandle[NumDirHandle] = {-1,-1, -1, -1, -1, -1, -1, -1};
94-/* for "FileMarkPtr" and "FileSeekBack" commands */
95-#define NumFHandle 16
96-static HANDLE FHandle[NumFHandle];
97-static long FPointer[NumFHandle];
98-
99-// forward declaration
100-int ExecCmnd();
101-
102-static void HandleInit()
103-{
104- int i;
105- for (i=0; i<_countof(FHandle); i++) {
106- FHandle[i] = INVALID_HANDLE_VALUE;
107- }
108-}
109-
110-/**
111- * @retval ファイルハンドルインデックス(0〜)
112- * -1のときエラー
113- */
114-static int HandlePut(HANDLE FH)
115-{
116- int i;
117- if (FH == INVALID_HANDLE_VALUE) {
118- return -1;
119- }
120- for (i=0; i<_countof(FHandle); i++) {
121- if (FHandle[i] == INVALID_HANDLE_VALUE) {
122- FHandle[i] = FH;
123- FPointer[i] = 0;
124- return i;
125- }
126- }
127- return -1;
128-}
129-
130-static HANDLE HandleGet(int fhi)
131-{
132- if (fhi < 0 || _countof(FHandle) < fhi) {
133- return INVALID_HANDLE_VALUE;
134- }
135- return FHandle[fhi];
136-}
137-
138-static void HandleFree(int fhi)
139-{
140- FHandle[fhi] = INVALID_HANDLE_VALUE;
141-}
142-
143-BOOL InitTTL(HWND HWin)
144-{
145- int i;
146- TStrVal Dir;
147- TVarId ParamsVarId;
148- char tmpname[10];
149- WORD Err;
150-
151- HMainWin = HWin;
152-
153- if (! InitVar()) return FALSE;
154- LockVar();
155-
156- // System variables
157- NewIntVar("result",0);
158- NewIntVar("timeout",0);
159- NewIntVar("mtimeout",0); // ミリ秒単位のタイムアウト用 (2009.1.23 maya)
160- NewStrVar("inputstr","");
161- NewStrVar("matchstr",""); // for 'waitregex' command (2005.10.7 yutaka)
162- NewStrVar("groupmatchstr1",""); // for 'waitregex' command (2005.10.15 yutaka)
163- NewStrVar("groupmatchstr2",""); // for 'waitregex' command (2005.10.15 yutaka)
164- NewStrVar("groupmatchstr3",""); // for 'waitregex' command (2005.10.15 yutaka)
165- NewStrVar("groupmatchstr4",""); // for 'waitregex' command (2005.10.15 yutaka)
166- NewStrVar("groupmatchstr5",""); // for 'waitregex' command (2005.10.15 yutaka)
167- NewStrVar("groupmatchstr6",""); // for 'waitregex' command (2005.10.15 yutaka)
168- NewStrVar("groupmatchstr7",""); // for 'waitregex' command (2005.10.15 yutaka)
169- NewStrVar("groupmatchstr8",""); // for 'waitregex' command (2005.10.15 yutaka)
170- NewStrVar("groupmatchstr9",""); // for 'waitregex' command (2005.10.15 yutaka)
171-
172- if (ParamCnt == 0) {
173- ParamCnt++;
174- }
175- NewIntVar("paramcnt",ParamCnt); // ファイル名も含む引数の個数 (2012.4.10 yutaka)
176-
177- // 旧形式のパラメータ設定 (param1 〜 param9)
178- NewStrVar("param1", ShortName);
179- if (Params) {
180- for (i=2; i<=9; i++) {
181- _snprintf_s(tmpname, sizeof(tmpname), _TRUNCATE, "param%d", i);
182- if (ParamCnt >= i && Params[i] != NULL) {
183- NewStrVar(tmpname, Params[i]);
184- }
185- else {
186- NewStrVar(tmpname, "");
187- }
188- }
189- }
190-
191- // 新形式のパラメータ設定 (params[1〜ParamCnt])
192- if (NewStrAryVar("params", ParamCnt+1) == 0) {
193- Err = 0;
194- GetStrAryVarByName(&ParamsVarId, "params", &Err);
195- if (Err == 0) {
196- if (ShortName[0] != 0) {
197- SetStrValInArray(ParamsVarId, 1, ShortName, &Err);
198- }
199- if (Params) {
200- for (i=0; i<=ParamCnt; i++) {
201- if (i == 1) {
202- continue;
203- }
204- if (Params[i]) {
205- SetStrValInArray(ParamsVarId, i, Params[i], &Err);
206- free(Params[i]);
207- }
208- }
209- free(Params);
210- Params = NULL;
211- }
212- }
213- }
214-
215- ParseAgain = FALSE;
216- IfNest = 0;
217- ElseFlag = 0;
218- EndIfFlag = 0;
219-
220- for (i=0; i<NumDirHandle; i++)
221- DirHandle[i] = -1L;
222- HandleInit();
223-
224- if (! InitBuff(FileName))
225- {
226- TTLStatus = IdTTLEnd;
227- return FALSE;
228- }
229-
230- UnlockVar();
231-
232- ExtractDirName(FileName,Dir);
233- TTMSetDir(Dir);
234-
235- if (SleepFlag)
236- { // synchronization for startup macro
237- // sleep until Tera Term is ready
238- WakeupCondition = IdWakeupInit;
239- // WakeupCondition = IdWakeupConnect | IdWakeupDisconn | IdWakeupUnlink;
240- TTLStatus = IdTTLSleep;
241- }
242- else
243- TTLStatus = IdTTLRun;
244-
245- return TRUE;
246-}
247-
248-void EndTTL()
249-{
250- int i;
251-
252- CloseStatDlg();
253-
254- for (i=0; i<NumDirHandle; i++)
255- {
256- if (DirHandle[i]!=-1L)
257- _findclose(DirHandle[i]);
258- DirHandle[i] = -1L;
259- }
260-
261- UnlockVar();
262- if (TTLStatus==IdTTLWait)
263- KillTimer(HMainWin,IdTimeOutTimer);
264- CloseBuff(0);
265- EndVar();
266-}
267-
268-long int CalcTime()
269-{
270- time_t time1;
271- struct tm *ptm;
272-
273- time1 = time(NULL);
274- ptm = localtime(&time1);
275- return ((long int)ptm->tm_hour * 3600 +
276- (long int)ptm->tm_min * 60 +
277- (long int)ptm->tm_sec
278- );
279-}
280-
281-//////////////// Beginning of TTL commands //////////////
282-WORD TTLCommCmd(char Cmd, int Wait)
283-{
284- if (GetFirstChar()!=0)
285- return ErrSyntax;
286- else if (! Linked)
287- return ErrLinkFirst;
288- else
289- return SendCmnd(Cmd,Wait);
290-}
291-
292-WORD TTLCommCmdFile(char Cmd, int Wait)
293-{
294- TStrVal Str;
295- WORD Err;
296-
297- Err = 0;
298- GetStrVal(Str,&Err);
299-
300- if ((Err==0) &&
301- ((strlen(Str)==0) || (GetFirstChar()!=0)))
302- Err = ErrSyntax;
303- if ((Err==0) && (! Linked))
304- Err = ErrLinkFirst;
305- if (Err==0)
306- {
307- SetFile(Str);
308- Err = SendCmnd(Cmd,Wait);
309- }
310- return Err;
311-}
312-
313-WORD TTLCommCmdBin(char Cmd, int Wait)
314-{
315- int Val;
316- WORD Err;
317-
318- Err = 0;
319- GetIntVal(&Val,&Err);
320-
321- if ((Err==0) &&
322- (GetFirstChar()!=0))
323- Err = ErrSyntax;
324- if ((Err==0) && (! Linked))
325- Err = ErrLinkFirst;
326- if (Err==0)
327- {
328- SetBinary(Val);
329- Err = SendCmnd(Cmd,Wait);
330- }
331- return Err;
332-}
333-
334-
335-WORD TTLCommCmdDeb()
336-{
337- int Val;
338- WORD Err;
339-
340- Err = 0;
341- GetIntVal(&Val,&Err);
342-
343- if ((Err==0) &&
344- (GetFirstChar()!=0))
345- Err = ErrSyntax;
346- if ((Err==0) && (! Linked))
347- Err = ErrLinkFirst;
348- if (Err==0)
349- {
350- SetDebug(Val);
351- }
352- return Err;
353-}
354-
355-WORD TTLCommCmdInt(char Cmd, int Wait)
356-{
357- int Val;
358- char Str[21];
359- WORD Err;
360-
361- Err = 0;
362- GetIntVal(&Val,&Err);
363-
364- if ((Err==0) &&
365- (GetFirstChar()!=0))
366- Err = ErrSyntax;
367- if ((Err==0) && (! Linked))
368- Err = ErrLinkFirst;
369- if (Err==0)
370- {
371- _snprintf_s(Str,sizeof(Str),_TRUNCATE,"%d",Val);
372- SetFile(Str);
373- Err = SendCmnd(Cmd,Wait);
374- }
375- return Err;
376-}
377-
378-WORD TTLBeep()
379-{
380- int val = 0;
381- WORD Err = 0;
382- UINT type = MB_OK;
383-
384- if (CheckParameterGiven()) {
385- GetIntVal(&val, &Err);
386- if (Err!=0) return Err;
387-
388- switch (val) {
389- case 0:
390- type = -1;
391- break;
392- case 1:
393- type = MB_ICONASTERISK;
394- break;
395- case 2:
396- type = MB_ICONEXCLAMATION;
397- break;
398- case 3:
399- type = MB_ICONHAND;
400- break;
401- case 4:
402- type = MB_ICONQUESTION;
403- break;
404- case 5:
405- type = MB_OK;
406- break;
407- default:
408- return ErrSyntax;
409- break;
410- }
411- }
412-
413- MessageBeep(type);
414-
415- return 0;
416-}
417-
418-WORD TTLBreak(WORD WId) {
419- if (GetFirstChar()!=0)
420- return ErrSyntax;
421-
422- return BreakLoop(WId);
423-}
424-
425-WORD TTLBringupBox()
426-{
427- if (GetFirstChar()!=0)
428- return ErrSyntax;
429- BringupStatDlg();
430- return 0;
431-}
432-
433-WORD TTLCall()
434-{
435- TName LabName;
436- WORD Err, VarType;
437- TVarId VarId;
438-
439- if (GetLabelName(LabName) && (GetFirstChar()==0)) {
440- if (CheckVar(LabName, &VarType, &VarId) && (VarType==TypLabel))
441- Err = CallToLabel(VarId);
442- else
443- Err = ErrLabelReq;
444- }
445- else
446- Err = ErrSyntax;
447-
448- return Err;
449-}
450-
451-// add 'clipb2var' (2006.9.17 maya)
452-WORD TTLClipb2Var()
453-{
454- WORD Err;
455- TVarId VarId;
456- HANDLE hText;
457- PTSTR clipbText;
458- char buf[MaxStrLen];
459- int Num = 0;
460- char *newbuff;
461- static char *cbbuff;
462- static int cbbuffsize, cblen;
463- HANDLE wide_hText;
464- LPWSTR wide_buf;
465- int mb_len;
466- UINT Cf;
467-
468- Err = 0;
469- GetStrVar(&VarId, &Err);
470- if (Err!=0) return Err;
471-
472- // get 2nd arg(optional) if given
473- if (CheckParameterGiven()) {
474- GetIntVal(&Num, &Err);
475- }
476-
477- if ((Err==0) && (GetFirstChar()!=0))
478- Err = ErrSyntax;
479- if (Err!=0) return Err;
480-
481- if (Num == 0) {
482- if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
483- Cf = CF_UNICODETEXT;
484- }
485- else if (IsClipboardFormatAvailable(CF_TEXT)) {
486- Cf = CF_TEXT;
487- }
488- else {
489- cblen = 0;
490- SetResult(0);
491- return Err;
492- }
493- if (OpenClipboard(NULL) == 0) {
494- cblen = 0;
495- SetResult(0);
496- return Err;
497- }
498-
499- if (Cf == CF_UNICODETEXT) {
500- wide_hText = GetClipboardData(CF_UNICODETEXT);
501- if (wide_hText != NULL) {
502- wide_buf = GlobalLock(wide_hText);
503- mb_len = WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, NULL, 0, NULL, NULL);
504- hText = GlobalAlloc(GMEM_MOVEABLE, sizeof(CHAR) * mb_len);
505- clipbText = GlobalLock(hText);
506- if (hText != NULL) {
507- WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, clipbText, mb_len, NULL, NULL);
508-
509- cblen = strlen(clipbText);
510- if (cbbuffsize <= cblen) {
511- if ((newbuff = realloc(cbbuff, cblen + 1)) == NULL) {
512- // realloc failed. fall back to old mode.
513- cblen = 0;
514- strncpy_s(buf,sizeof(buf),clipbText,_TRUNCATE);
515- GlobalUnlock(hText);
516- CloseClipboard();
517- SetStrVal(VarId, buf);
518- SetResult(3);
519- return Err;
520- }
521- cbbuff = newbuff;
522- cbbuffsize = cblen + 1;
523- }
524- strncpy_s(cbbuff, cbbuffsize, clipbText, _TRUNCATE);
525-
526- GlobalUnlock(hText);
527- GlobalFree(hText);
528- }
529- GlobalUnlock(wide_hText);
530- }
531- else {
532- cblen = 0;
533- }
534- }
535- else if (Cf == CF_TEXT) {
536- hText = GetClipboardData(CF_TEXT);
537- if (hText != NULL) {
538- clipbText = GlobalLock(hText);
539- cblen = strlen(clipbText);
540- if (cbbuffsize <= cblen) {
541- if ((newbuff = realloc(cbbuff, cblen + 1)) == NULL) {
542- // realloc failed. fall back to old mode.
543- cblen = 0;
544- strncpy_s(buf,sizeof(buf),clipbText,_TRUNCATE);
545- GlobalUnlock(hText);
546- CloseClipboard();
547- SetStrVal(VarId, buf);
548- SetResult(3);
549- return Err;
550- }
551- cbbuff = newbuff;
552- cbbuffsize = cblen + 1;
553- }
554- strncpy_s(cbbuff, cbbuffsize, clipbText, _TRUNCATE);
555- GlobalUnlock(hText);
556- }
557- else {
558- cblen = 0;
559- }
560- }
561- CloseClipboard();
562- }
563-
564- if (cbbuff != NULL && Num >= 0 && Num * (MaxStrLen - 1) < cblen) {
565- if (strncpy_s(buf ,sizeof(buf), cbbuff + Num * (MaxStrLen-1), _TRUNCATE) == STRUNCATE)
566- SetResult(2); // Copied string is truncated.
567- else {
568- SetResult(1);
569- }
570- SetStrVal(VarId, buf);
571- }
572- else {
573- SetResult(0);
574- }
575-
576- return Err;
577-}
578-
579-// add 'var2clipb' (2006.9.17 maya)
580-WORD TTLVar2Clipb()
581-{
582- WORD Err;
583- TStrVal Str;
584- HGLOBAL hText;
585- PTSTR clipbText;
586- int wide_len;
587- HGLOBAL wide_hText;
588- LPWSTR wide_buf;
589-
590- Err = 0;
591- GetStrVal(Str,&Err);
592- if (Err!=0) return Err;
593-
594- hText = GlobalAlloc(GHND, sizeof(Str));
595- clipbText = GlobalLock(hText);
596- strncpy_s(clipbText, sizeof(Str), Str, _TRUNCATE);
597- GlobalUnlock(hText);
598-
599- wide_len = MultiByteToWideChar(CP_ACP, 0, clipbText, -1, NULL, 0);
600- wide_hText = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len);
601- if (wide_hText) {
602- wide_buf = (LPWSTR)GlobalLock(wide_hText);
603- MultiByteToWideChar(CP_ACP, 0, clipbText, -1, wide_buf, wide_len);
604- GlobalUnlock(wide_hText);
605- }
606-
607- if (OpenClipboard(NULL) == 0) {
608- SetResult(0);
609- }
610- else {
611- EmptyClipboard();
612- SetClipboardData(CF_TEXT, hText);
613-
614- if (wide_buf) {
615- SetClipboardData(CF_UNICODETEXT, wide_hText);
616- }
617-
618- CloseClipboard();
619- SetResult(1);
620- }
621-
622- return Err;
623-}
624-
625-WORD TTLCloseSBox()
626-{
627- if (GetFirstChar()!=0)
628- return ErrSyntax;
629- CloseStatDlg();
630- return 0;
631-}
632-
633-WORD TTLCloseTT()
634-{
635- if (GetFirstChar()!=0)
636- return ErrSyntax;
637-
638- if (! Linked)
639- return ErrLinkFirst;
640- else {
641- // Close Tera Term
642- SendCmnd(CmdCloseWin,IdTTLWaitCmndEnd);
643- EndDDE();
644- return 0;
645- }
646-}
647-
648-WORD TTLCode2Str()
649-{
650- TVarId VarId;
651- WORD Err;
652- int Num, Len, c, i;
653- BYTE d;
654- TStrVal Str;
655-
656- Err = 0;
657- GetStrVar(&VarId, &Err);
658-
659- GetIntVal(&Num,&Err);
660- if ((Err==0) && (GetFirstChar()!=0))
661- Err = ErrSyntax;
662- if (Err!=0) return Err;
663-
664- Len = sizeof(Num);
665- i = 0;
666- for (c=0; c<=Len-1; c++)
667- {
668- d = (Num >> ((Len-1-c)*8)) & 0xff;
669- if ((i>0) || (d!=0))
670- {
671- Str[i] = d;
672- i++;
673- }
674- }
675- Str[i] = 0;
676- SetStrVal(VarId, Str);
677- return Err;
678-}
679-
680-WORD TTLConnect(WORD mode)
681-{
682- TStrVal Cmnd, Str;
683- WORD Err;
684- WORD w;
685-
686- Str[0] = 0;
687- Err = 0;
688-
689- if (mode == RsvConnect || CheckParameterGiven()) {
690- GetStrVal(Str,&Err);
691- if (Err!=0) return Err;
692- }
693-
694- if (GetFirstChar()!=0)
695- return ErrSyntax;
696-
697- if (Linked)
698- {
699- if (ComReady!=0)
700- {
701- SetResult(2);
702- return Err;
703- }
704-
705- if (mode == RsvConnect) {
706- // new connection
707- SetFile(Str);
708- SendCmnd(CmdConnect,0);
709-
710- WakeupCondition = IdWakeupInit;
711- TTLStatus = IdTTLSleep;
712- return Err;
713- }
714- else { // cygwin connection
715- TTLCloseTT();
716- }
717- }
718-
719- SetResult(0);
720- // link to Tera Term
721- if (strlen(TopicName)==0)
722- {
723- switch (mode) {
724- case RsvConnect:
725- strncpy_s(Cmnd, sizeof(Cmnd),TTERMCOMMAND, _TRUNCATE);
726- break;
727- case RsvCygConnect:
728- strncpy_s(Cmnd, sizeof(Cmnd),CYGTERMCOMMAND, _TRUNCATE);
729- break;
730- }
731- w = HIWORD(HMainWin);
732- Word2HexStr(w,TopicName);
733- w = LOWORD(HMainWin);
734- Word2HexStr(w,&(TopicName[4]));
735- strncat_s(Cmnd,sizeof(Cmnd),TopicName,_TRUNCATE);
736- strncat_s(Cmnd,sizeof(Cmnd)," ",_TRUNCATE);
737- strncat_s(Cmnd,sizeof(Cmnd),Str,_TRUNCATE);
738- if (WinExec(Cmnd,SW_SHOW)<32)
739- return ErrCantConnect;
740- TTLStatus = IdTTLInitDDE;
741- }
742- return Err;
743-}
744-
745-
746-/*
747- * cf. http://oku.edu.mie-u.ac.jp/~okumura/algo/
748- */
749-
750-enum checksum_type {
751- CHECKSUM8,
752- CHECKSUM16,
753- CHECKSUM32,
754- CRC16,
755- CRC32
756-};
757-
758-static unsigned int checksum32(int n, unsigned char c[])
759-{
760- unsigned long value = 0;
761- int i;
762-
763- for (i = 0; i < n; i++) {
764- value += (c[i] & 0xFF);
765- }
766- return (value & 0xFFFFFFFF);
767-}
768-
769-static unsigned int checksum16(int n, unsigned char c[])
770-{
771- unsigned long value = 0;
772- int i;
773-
774- for (i = 0; i < n; i++) {
775- value += (c[i] & 0xFF);
776- }
777- return (value & 0xFFFF);
778-}
779-
780-static unsigned int checksum8(int n, unsigned char c[])
781-{
782- unsigned long value = 0;
783- int i;
784-
785- for (i = 0; i < n; i++) {
786- value += (c[i] & 0xFF);
787- }
788- return (value & 0xFF);
789-}
790-
791-// CRC-16-CCITT
792-static unsigned int crc16(int n, unsigned char c[])
793-{
794-#define CRC16POLY1 0x1021U /* x^{16}+x^{12}+x^5+1 */
795-#define CRC16POLY2 0x8408U /* 左右逆転 */
796-
797- int i, j;
798- unsigned long r;
799-
800- r = 0xFFFFU;
801- for (i = 0; i < n; i++) {
802- r ^= c[i];
803- for (j = 0; j < CHAR_BIT; j++)
804- if (r & 1) r = (r >> 1) ^ CRC16POLY2;
805- else r >>= 1;
806- }
807- return r ^ 0xFFFFU;
808-}
809-
810-static unsigned long crc32(int n, unsigned char c[])
811-{
812-#define CRC32POLY1 0x04C11DB7UL
813- /* x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11]+
814- x^{10}+x^8+x^7+x^5+x^4+x^2+x^1+1 */
815-#define CRC32POLY2 0xEDB88320UL /* 左右逆転 */
816- int i, j;
817- unsigned long r;
818-
819- r = 0xFFFFFFFFUL;
820- for (i = 0; i < n; i++) {
821- r ^= c[i];
822- for (j = 0; j < CHAR_BIT; j++)
823- if (r & 1) r = (r >> 1) ^ CRC32POLY2;
824- else r >>= 1;
825- }
826- return r ^ 0xFFFFFFFFUL;
827-}
828-
829-// チェックサムアルゴリズム・共通ルーチン
830-WORD TTLDoChecksum(enum checksum_type type)
831-{
832- TStrVal Str;
833- WORD Err;
834- TVarId VarId;
835- DWORD cksum;
836-
837- Err = 0;
838- GetIntVar(&VarId, &Err);
839- GetStrVal(Str,&Err);
840- if ((Err==0) && (GetFirstChar()!=0))
841- Err = ErrSyntax;
842- if (Err!=0) return Err;
843- if (Str[0]==0) return Err;
844-
845- switch (type) {
846- case CHECKSUM8:
847- cksum = checksum8(strlen(Str), Str);
848- break;
849- case CHECKSUM16:
850- cksum = checksum16(strlen(Str), Str);
851- break;
852- case CHECKSUM32:
853- cksum = checksum32(strlen(Str), Str);
854- break;
855- case CRC16:
856- cksum = crc16(strlen(Str), Str);
857- break;
858- case CRC32:
859- cksum = crc32(strlen(Str), Str);
860- break;
861- default:
862- cksum = 0;
863- break;
864- }
865-
866- SetIntVal(VarId, cksum);
867-
868- return Err;
869-}
870-
871-WORD TTLDoChecksumFile(enum checksum_type type)
872-{
873- TStrVal Str;
874- int result = 0;
875- WORD Err;
876- TVarId VarId;
877- HANDLE fh = INVALID_HANDLE_VALUE, hMap = NULL;
878- LPBYTE lpBuf = NULL;
879- DWORD fsize;
880- DWORD cksum;
881-
882- Err = 0;
883- GetIntVar(&VarId, &Err);
884- GetStrVal(Str,&Err);
885- if ((Err==0) && (GetFirstChar()!=0))
886- Err = ErrSyntax;
887- if (Err!=0) return Err;
888- if (Str[0]==0) return Err;
889-
890- fh = CreateFile(Str,GENERIC_READ,0,NULL,OPEN_EXISTING,
891- FILE_ATTRIBUTE_NORMAL,NULL); /* ファイルオープン */
892- if (fh == INVALID_HANDLE_VALUE) {
893- result = -1;
894- goto error;
895- }
896- /* ファイルマッピングオブジェクト作成 */
897- hMap = CreateFileMapping(fh,NULL,PAGE_READONLY,0,0,NULL);
898- if (hMap == NULL) {
899- result = -1;
900- goto error;
901- }
902-
903- /* ファイルをマップし、先頭アドレスをlpBufに取得 */
904- lpBuf = (LPBYTE)MapViewOfFile(hMap,FILE_MAP_READ,0,0,0);
905- if (lpBuf == NULL) {
906- result = -1;
907- goto error;
908- }
909-
910- fsize = GetFileSize(fh,NULL);
911-
912- switch (type) {
913- case CHECKSUM8:
914- cksum = checksum8(fsize, lpBuf);
915- break;
916- case CHECKSUM16:
917- cksum = checksum16(fsize, lpBuf);
918- break;
919- case CHECKSUM32:
920- cksum = checksum32(fsize, lpBuf);
921- break;
922- case CRC16:
923- cksum = crc16(fsize, lpBuf);
924- break;
925- case CRC32:
926- cksum = crc32(fsize, lpBuf);
927- break;
928- default:
929- cksum = 0;
930- break;
931- }
932-
933- SetIntVal(VarId, cksum);
934-
935-error:
936- if (lpBuf != NULL) {
937- UnmapViewOfFile(lpBuf);
938- }
939-
940- if (hMap != NULL) {
941- CloseHandle(hMap);
942- }
943- if (fh != INVALID_HANDLE_VALUE) {
944- CloseHandle(fh);
945- }
946-
947- SetResult(result);
948-
949- return Err;
950-}
951-
952-WORD TTLDelPassword()
953-{
954- TStrVal Str, Str2;
955- WORD Err;
956-
957- Err = 0;
958- GetStrVal(Str,&Err);
959- GetStrVal(Str2,&Err);
960- if ((Err==0) && (GetFirstChar()!=0))
961- Err = ErrSyntax;
962- if (Err!=0) return Err;
963- if (Str[0]==0) return Err;
964-
965- GetAbsPath(Str,sizeof(Str));
966- if (! DoesFileExist(Str)) return Err;
967- if (Str2[0]==0) // delete all password
968- WritePrivateProfileString("Password",NULL,NULL,Str);
969- else // delete password specified by Str2
970- WritePrivateProfileString("Password",Str2,NULL,Str);
971- return Err;
972-}
973-
974-WORD TTLDim(WORD type)
975-{
976- WORD Err, WordId, VarType;
977- TName Name;
978- TVarId VarId;
979- int size;
980-
981- Err = 0;
982-
983- if (! GetIdentifier(Name)) return ErrSyntax;
984- if (CheckReservedWord(Name, &WordId)) return ErrSyntax;
985- if (CheckVar(Name, &VarType, &VarId)) return ErrSyntax;
986-
987- GetIntVal(&size, &Err);
988- if (Err!=0) return Err;
989-
990- if (type == RsvIntDim) {
991- Err = NewIntAryVar(Name, size);
992- }
993- else { // type == RsvStrDim
994- Err = NewStrAryVar(Name, size);
995- }
996- return Err;
997-}
998-
999-WORD TTLDisconnect()
1000-{
1001- WORD Err;
1002- int Val = 1;
1003- char Str[21];
1004-
1005- Err = 0;
1006- // get 1rd arg(optional) if given
1007- if (CheckParameterGiven()) {
1008- GetIntVal(&Val, &Err);
1009- }
1010-
1011- if ((Err==0) &&
1012- (GetFirstChar()!=0))
1013- Err = ErrSyntax;
1014- if ((Err==0) && (! Linked))
1015- Err = ErrLinkFirst;
1016- if (Err==0)
1017- {
1018- _snprintf_s(Str,sizeof(Str),_TRUNCATE,"%d",Val);
1019- SetFile(Str);
1020- Err = SendCmnd(CmdDisconnect,0);
1021- }
1022- return Err;
1023-}
1024-
1025-WORD TTLDispStr()
1026-{
1027- TStrVal Str, buff;
1028- WORD Err, ValType;
1029- int Val;
1030-
1031- if (! Linked)
1032- return ErrLinkFirst;
1033-
1034- buff[0] = 0;
1035-
1036- while (TRUE) {
1037- if (GetString(Str, &Err)) {
1038- if (Err) return Err;
1039- strncat_s(buff, MaxStrLen, Str, _TRUNCATE);
1040- }
1041- else if (GetExpression(&ValType, &Val, &Err)) {
1042- if (Err!=0) return Err;
1043- switch (ValType) {
1044- case TypInteger:
1045- Str[0] = LOBYTE(Val);
1046- Str[1] = 0;
1047- strncat_s(buff, MaxStrLen, Str, _TRUNCATE);
1048- /* Falls through. */
1049- case TypString:
1050- strncat_s(buff, MaxStrLen, StrVarPtr((TVarId)Val), _TRUNCATE);
1051- break;
1052- default:
1053- return ErrTypeMismatch;
1054- }
1055- }
1056- else {
1057- break;
1058- }
1059- }
1060- SetFile(buff);
1061- return SendCmnd(CmdDispStr, 0);
1062-}
1063-
1064-WORD TTLDo()
1065-{
1066- WORD WId, Err;
1067- int Val = 1;
1068-
1069- Err = 0;
1070- if (CheckParameterGiven()) {
1071- if (GetReservedWord(&WId)) {
1072- switch (WId) {
1073- case RsvWhile:
1074- GetIntVal(&Val,&Err);
1075- break;
1076- case RsvUntil:
1077- GetIntVal(&Val,&Err);
1078- Val = Val == 0;
1079- break;
1080- default:
1081- Err = ErrSyntax;
1082- }
1083- if ((Err==0) && (GetFirstChar()!=0))
1084- Err = ErrSyntax;
1085- }
1086- else {
1087- Err = ErrSyntax;
1088- }
1089- }
1090-
1091- if (Err!=0) return Err;
1092-
1093- if (Val!=0)
1094- return SetWhileLoop();
1095- else
1096- EndWhileLoop();
1097- return Err;
1098-}
1099-
1100-WORD TTLElse()
1101-{
1102- if (GetFirstChar()!=0)
1103- return ErrSyntax;
1104-
1105- if (IfNest<1)
1106- return ErrInvalidCtl;
1107-
1108- // Skip until 'EndIf'
1109- IfNest--;
1110- EndIfFlag = 1;
1111- return 0;
1112-}
1113-
1114-int CheckElseIf(LPWORD Err)
1115-{
1116- int Val;
1117- WORD WId;
1118-
1119- *Err = 0;
1120- GetIntVal(&Val,Err);
1121- if (*Err!=0) return 0;
1122- if (! GetReservedWord(&WId) ||
1123- (WId!=RsvThen) ||
1124- (GetFirstChar()!=0))
1125- *Err = ErrSyntax;
1126- return Val;
1127-}
1128-
1129-WORD TTLElseIf()
1130-{
1131- WORD Err;
1132- int Val;
1133-
1134- Val = CheckElseIf(&Err);
1135- if (Err!=0) return Err;
1136-
1137- if (IfNest<1)
1138- return ErrInvalidCtl;
1139-
1140- // Skip until 'EndIf'
1141- IfNest--;
1142- EndIfFlag = 1;
1143- return Err;
1144-}
1145-
1146-WORD TTLEnd()
1147-{
1148- if (GetFirstChar()==0)
1149- {
1150- TTLStatus = IdTTLEnd;
1151- return 0;
1152- }
1153- else
1154- return ErrSyntax;
1155-}
1156-
1157-WORD TTLEndIf()
1158-{
1159- if (GetFirstChar()!=0)
1160- return ErrSyntax;
1161-
1162- if (IfNest<1)
1163- return ErrInvalidCtl;
1164-
1165- IfNest--;
1166- return 0;
1167-}
1168-
1169-WORD TTLEndWhile(BOOL mode)
1170-{
1171- WORD Err;
1172- int Val = mode;
1173-
1174- Err = 0;
1175- if (CheckParameterGiven()) {
1176- GetIntVal(&Val,&Err);
1177- }
1178-
1179- if ((Err==0) && (GetFirstChar()!=0))
1180- Err = ErrSyntax;
1181- if (Err!=0) return Err;
1182-
1183- return BackToWhile((Val!=0) == mode);
1184-}
1185-
1186-WORD TTLExec()
1187-{
1188- TStrVal Str,Str2, CurDir;
1189- int mode = SW_SHOW;
1190- int wait = 0, ret;
1191- WORD Err;
1192- STARTUPINFO sui;
1193- PROCESS_INFORMATION pi;
1194- BOOL bRet;
1195-
1196- memset(CurDir, 0, sizeof(CurDir));
1197-
1198- Err = 0;
1199- GetStrVal(Str,&Err);
1200-
1201- if (CheckParameterGiven()) {
1202- GetStrVal(Str2, &Err);
1203- if (Err!=0) return Err;
1204-
1205- if (_stricmp(Str2, "hide") == 0)
1206- mode = SW_HIDE;
1207- else if (_stricmp(Str2, "minimize") == 0)
1208- mode = SW_MINIMIZE;
1209- else if (_stricmp(Str2, "maximize") == 0)
1210- mode = SW_MAXIMIZE;
1211- else if (_stricmp(Str2, "show") == 0)
1212- mode = SW_SHOW;
1213- else
1214- Err = ErrSyntax;
1215-
1216- // get 3nd arg(optional) if given
1217- if (CheckParameterGiven()) {
1218- GetIntVal(&wait, &Err);
1219- if (Err!=0) return Err;
1220-
1221- // get 4th arg(optional) if given
1222- if (CheckParameterGiven()) {
1223- GetStrVal(CurDir, &Err);
1224- if (Err!=0) return Err;
1225- }
1226- }
1227- }
1228-
1229- if ((Err==0) &&
1230- ((strlen(Str)==0) || (GetFirstChar()!=0)))
1231- Err = ErrSyntax;
1232-
1233- if (Err!=0) return Err;
1234-
1235- memset(&sui, 0, sizeof(STARTUPINFO));
1236- sui.cb = sizeof(STARTUPINFO);
1237- sui.wShowWindow = mode;
1238- sui.dwFlags = STARTF_USESHOWWINDOW;
1239- if (CurDir[0] == 0)
1240- bRet = CreateProcess(NULL, Str, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &sui, &pi);
1241- else
1242- bRet = CreateProcess(NULL, Str, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, CurDir, &sui, &pi);
1243- if (bRet == FALSE) {
1244- // 実行できなかった場合、resultに-1を返す
1245- SetResult(-1);
1246-#if 0
1247- // エラーになる
1248- Err = ErrCantExec;
1249-#endif
1250- } else {
1251- if (wait) {
1252- // 実行 & wait指定
1253- WaitForSingleObject(pi.hProcess, INFINITE);
1254- GetExitCodeProcess(pi.hProcess, &ret);
1255- SetResult(ret);
1256- } else {
1257- SetResult(0);
1258- }
1259- CloseHandle(pi.hThread);
1260- CloseHandle(pi.hProcess);
1261- }
1262- return Err;
1263-}
1264-
1265-WORD TTLExecCmnd()
1266-{
1267- WORD Err;
1268- TStrVal NextLine;
1269- BYTE b;
1270-
1271- Err = 0;
1272- GetStrVal(NextLine,&Err);
1273- if (Err!=0) return Err;
1274- if (GetFirstChar()!=0)
1275- return ErrSyntax;
1276-
1277- strncpy_s(LineBuff, sizeof(LineBuff),NextLine, _TRUNCATE);
1278- LineLen = (WORD)strlen(LineBuff);
1279- LinePtr = 0;
1280- b = GetFirstChar();
1281- LinePtr--;
1282- ParseAgain = (b!=0) && (b!=':') && (b!=';');
1283- return Err;
1284-}
1285-
1286-WORD TTLExit()
1287-{
1288- if (GetFirstChar()==0)
1289- {
1290- if (! ExitBuffer())
1291- TTLStatus = IdTTLEnd;
1292- return 0;
1293- }
1294- else
1295- return ErrSyntax;
1296-}
1297-
1298-WORD TTLExpandEnv()
1299-{
1300- WORD Err;
1301- TVarId VarId;
1302- TStrVal deststr, srcptr;
1303-
1304- Err = 0;
1305- GetStrVar(&VarId, &Err);
1306- if (Err!=0) return Err;
1307-
1308- if (CheckParameterGiven()) { // expandenv strvar strval
1309- GetStrVal(srcptr,&Err);
1310- if ((Err==0) && (GetFirstChar()!=0))
1311- Err = ErrSyntax;
1312- if (Err!=0) {
1313- return Err;
1314- }
1315-
1316- // ファイルパスに環境変数が含まれているならば、展開する。
1317- ExpandEnvironmentStrings(srcptr, deststr, MaxStrLen);
1318- SetStrVal(VarId, deststr);
1319- }
1320- else { // expandenv strvar
1321- // ファイルパスに環境変数が含まれているならば、展開する。
1322- ExpandEnvironmentStrings(StrVarPtr(VarId), deststr, MaxStrLen);
1323- SetStrVal(VarId, deststr);
1324- }
1325-
1326- return Err;
1327-}
1328-
1329-WORD TTLFileClose()
1330-{
1331- WORD Err;
1332- int fhi; // handle index
1333- HANDLE FH;
1334-
1335- Err = 0;
1336- GetIntVal(&fhi,&Err);
1337- FH = HandleGet(fhi);
1338- if ((Err==0) && (GetFirstChar()!=0))
1339- Err = ErrSyntax;
1340- if (Err!=0) return Err;
1341- _lclose(FH);
1342- HandleFree(fhi);
1343- return Err;
1344-}
1345-
1346-WORD TTLFileConcat()
1347-{
1348- WORD Err;
1349- HANDLE FH1, FH2;
1350- int c;
1351- TStrVal FName1, FName2;
1352- BYTE buf[1024];
1353-
1354- Err = 0;
1355- GetStrVal(FName1,&Err);
1356- GetStrVal(FName2,&Err);
1357- if ((Err==0) &&
1358- ((strlen(FName1)==0) ||
1359- (strlen(FName2)==0) ||
1360- (GetFirstChar()!=0)))
1361- Err = ErrSyntax;
1362- if (Err!=0) {
1363- SetResult(1);
1364- return Err;
1365- }
1366-
1367- if (!GetAbsPath(FName1,sizeof(FName1))) {
1368- SetResult(-1);
1369- return Err;
1370- }
1371- if (!GetAbsPath(FName2,sizeof(FName2))) {
1372- SetResult(-2);
1373- return Err;
1374- }
1375- if (_stricmp(FName1,FName2)==0) {
1376- SetResult(2);
1377- return Err;
1378- }
1379-
1380- FH1 = _lopen(FName1,OF_WRITE);
1381- if (FH1 == INVALID_HANDLE_VALUE)
1382- FH1 = _lcreat(FName1,0);
1383- if (FH1 == INVALID_HANDLE_VALUE) {
1384- SetResult(3);
1385- return Err;
1386- }
1387- _llseek(FH1,0,2);
1388-
1389- FH2 = _lopen(FName2,OF_READ);
1390- if (FH2 != INVALID_HANDLE_VALUE)
1391- {
1392- do {
1393- c = _lread(FH2,&(buf[0]),sizeof(buf));
1394- if (c>0)
1395- _lwrite(FH1,&(buf[0]),c);
1396- } while (c >= sizeof(buf));
1397- _lclose(FH2);
1398- }
1399- _lclose(FH1);
1400-
1401- SetResult(0);
1402- return Err;
1403-}
1404-
1405-WORD TTLFileCopy()
1406-{
1407- WORD Err;
1408- TStrVal FName1, FName2;
1409- BOOL ret;
1410-
1411- Err = 0;
1412- GetStrVal(FName1,&Err);
1413- GetStrVal(FName2,&Err);
1414- if ((Err==0) &&
1415- ((strlen(FName1)==0) ||
1416- (strlen(FName2)==0) ||
1417- (GetFirstChar()!=0)))
1418- Err = ErrSyntax;
1419- if (Err!=0) {
1420- SetResult(1);
1421- return Err;
1422- }
1423-
1424- if (!GetAbsPath(FName1,sizeof(FName1))) {
1425- SetResult(-1);
1426- return Err;
1427- }
1428- if (!GetAbsPath(FName2,sizeof(FName2))) {
1429- SetResult(-2);
1430- return Err;
1431- }
1432- if (_stricmp(FName1, FName2) == 0) {
1433- SetResult(-3);
1434- return Err;
1435- }
1436-
1437- ret = CopyFile(FName1, FName2, FALSE);
1438- if (ret == 0) {
1439- SetResult(-4);
1440- return Err;
1441- }
1442-
1443- SetResult(0);
1444- return Err;
1445-}
1446-
1447-WORD TTLFileCreate()
1448-{
1449- WORD Err;
1450- TVarId VarId;
1451- HANDLE FH;
1452- int fhi;
1453- TStrVal FName;
1454-
1455- Err = 0;
1456- GetIntVar(&VarId, &Err);
1457- GetStrVal(FName, &Err);
1458- if ((Err==0) &&
1459- ((strlen(FName)==0) || (GetFirstChar()!=0)))
1460- Err = ErrSyntax;
1461- if (Err!=0) {
1462- SetResult(1);
1463- return Err;
1464- }
1465-
1466- if (!GetAbsPath(FName,sizeof(FName))) {
1467- SetIntVal(VarId, -1);
1468- SetResult(-1);
1469- return Err;
1470- }
1471- FH = _lcreat(FName,0);
1472- if (FH == INVALID_HANDLE_VALUE) {
1473- SetResult(2);
1474- }
1475- else {
1476- SetResult(0);
1477- }
1478- fhi = HandlePut(FH);
1479- SetIntVal(VarId, fhi);
1480- if (fhi == -1) {
1481- _lclose(FH);
1482- }
1483- return Err;
1484-}
1485-
1486-WORD TTLFileDelete()
1487-{
1488- WORD Err;
1489- TStrVal FName;
1490-
1491- Err = 0;
1492- GetStrVal(FName,&Err);
1493- if ((Err==0) &&
1494- ((strlen(FName)==0) || (GetFirstChar()!=0)))
1495- Err = ErrSyntax;
1496- if (Err!=0) {
1497- SetResult(1);
1498- return Err;
1499- }
1500-
1501- if (!GetAbsPath(FName,sizeof(FName))) {
1502- SetResult(-1);
1503- return Err;
1504- }
1505-
1506- if (remove(FName) != 0) {
1507- SetResult(-1);
1508- }
1509- else {
1510- SetResult(0);
1511- }
1512-
1513- return Err;
1514-}
1515-
1516-WORD TTLFileMarkPtr()
1517-{
1518- WORD Err;
1519- int fhi;
1520- HANDLE FH;
1521- LONG pos;
1522-
1523- Err = 0;
1524- GetIntVal(&fhi,&Err);
1525- FH = HandleGet(fhi);
1526- if ((Err==0) && (GetFirstChar()!=0))
1527- Err = ErrSyntax;
1528- if (Err!=0) return Err;
1529- pos = _llseek(FH,0,1); /* mark current pos */
1530- if (pos == INVALID_SET_FILE_POINTER) {
1531- pos = 0; // ?
1532- }
1533- FPointer[fhi] = pos;
1534- return Err;
1535-}
1536-
1537-// add 'filenamebox' (2007.9.13 maya)
1538-WORD TTLFilenameBox()
1539-{
1540- TStrVal Str1;
1541- WORD Err, ValType;
1542- TVarId VarId;
1543- OPENFILENAME ofn;
1544- char uimsg[MAX_UIMSG];
1545- BOOL SaveFlag = FALSE;
1546- TStrVal InitDir = "";
1547- BOOL ret;
1548-
1549- Err = 0;
1550- GetStrVal(Str1,&Err);
1551- if (Err!=0) return Err;
1552-
1553- // get 2nd arg(optional) if given
1554- if (CheckParameterGiven()) { // dialogtype
1555- GetIntVal(&SaveFlag, &Err);
1556- if (Err!=0) return Err;
1557-
1558- // get 3rd arg(optional) if given
1559- if (CheckParameterGiven()) { // initdir
1560- GetStrVal(InitDir, &Err);
1561- if (Err!=0) return Err;
1562- }
1563- }
1564-
1565- if ((Err==0) && (GetFirstChar()!=0))
1566- Err = ErrSyntax;
1567- if (Err!=0) return Err;
1568-
1569- SetInputStr("");
1570- if (CheckVar("inputstr", &ValType, &VarId) &&
1571- (ValType==TypString)) {
1572- memset(&ofn, 0, sizeof(OPENFILENAME));
1573- ofn.lStructSize = get_OPENFILENAME_SIZE();
1574- ofn.hwndOwner = HMainWin;
1575- ofn.lpstrTitle = Str1;
1576- ofn.lpstrFile = StrVarPtr(VarId);
1577- ofn.nMaxFile = MaxStrLen;
1578- get_lang_msg("FILEDLG_ALL_FILTER", uimsg, sizeof(uimsg), "All(*.*)\\0*.*\\0\\0", UILanguageFile);
1579- ofn.lpstrFilter = uimsg;
1580- ofn.lpstrInitialDir = NULL;
1581- if (strlen(InitDir) > 0) {
1582- ofn.lpstrInitialDir = InitDir;
1583- }
1584- BringupWindow(HMainWin);
1585- if (SaveFlag) {
1586- ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1587- ret = GetSaveFileName(&ofn);
1588- }
1589- else {
1590- ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
1591- ret = GetOpenFileName(&ofn);
1592- }
1593- SetResult(ret);
1594- }
1595- return Err;
1596-}
1597-
1598-WORD TTLFileOpen()
1599-{
1600- WORD Err;
1601- TVarId VarId;
1602- int fhi;
1603- HANDLE FH;
1604- int Append, ReadonlyFlag=0;
1605- TStrVal FName;
1606-
1607- Err = 0;
1608- GetIntVar(&VarId, &Err);
1609- GetStrVal(FName, &Err);
1610- GetIntVal(&Append, &Err);
1611- // get 4nd arg(optional) if given
1612- if (CheckParameterGiven()) { // readonly
1613- GetIntVal(&ReadonlyFlag, &Err);
1614- }
1615- if ((Err==0) &&
1616- ((strlen(FName)==0) || (GetFirstChar()!=0)))
1617- Err = ErrSyntax;
1618- if (Err!=0) return Err;
1619-
1620- if (!GetAbsPath(FName,sizeof(FName))) {
1621- SetIntVal(VarId, -1);
1622- return Err;
1623- }
1624-
1625- if (ReadonlyFlag) {
1626- FH = _lopen(FName,OF_READ);
1627- }
1628- else {
1629- FH = _lopen(FName,OF_READWRITE);
1630- if (FH == INVALID_HANDLE_VALUE)
1631- FH = _lcreat(FName,0);
1632- }
1633- if (FH == INVALID_HANDLE_VALUE) {
1634- SetIntVal(VarId, -1);
1635- return Err;
1636- }
1637- fhi = HandlePut(FH);
1638- if (fhi == -1) {
1639- SetIntVal(VarId, -1);
1640- _lclose(FH);
1641- return Err;
1642- }
1643- SetIntVal(VarId, fhi);
1644- if (Append!=0) {
1645- _llseek(FH, 0, 2/*FILE_END*/);
1646- }
1647- return 0; // no error
1648-}
1649-
1650-// Format: filelock <file handle> [<timeout>]
1651-// (2012.4.19 yutaka)
1652-WORD TTLFileLock()
1653-{
1654- WORD Err;
1655- HANDLE FH;
1656- int fhi;
1657- DWORD timeout;
1658- int result;
1659- BOOL ret;
1660- DWORD dwStart;
1661-
1662- Err = 0;
1663- GetIntVal(&fhi,&Err);
1664- FH = HandleGet(fhi);
1665- if (Err!=0) return Err;
1666-
1667- timeout = -1; // 無限大
1668- if (CheckParameterGiven()) {
1669- GetIntVal(&timeout, &Err);
1670- if (Err!=0) return Err;
1671- }
1672-
1673- result = 1; // error
1674- dwStart = GetTickCount();
1675- do {
1676- ret = LockFile(FH, 0, 0, (DWORD)-1, (DWORD)-1);
1677- if (ret != 0) { // ロック成功
1678- result = 0; // success
1679- break;
1680- }
1681- Sleep(1);
1682- } while (GetTickCount() - dwStart < (timeout*1000));
1683-
1684- SetResult(result);
1685- return Err;
1686-}
1687-
1688-// Format: fileunlock <file handle>
1689-// (2012.4.19 yutaka)
1690-WORD TTLFileUnLock()
1691-{
1692- WORD Err;
1693- HANDLE FH;
1694- int fhi;
1695- BOOL ret;
1696-
1697- Err = 0;
1698- GetIntVal(&fhi,&Err);
1699- FH = HandleGet(fhi);
1700- if ((Err==0) && (GetFirstChar()!=0))
1701- Err = ErrSyntax;
1702- if (Err!=0) return Err;
1703-
1704- ret = UnlockFile(FH, 0, 0, (DWORD)-1, (DWORD)-1);
1705- if (ret != 0) { // アンロック成功
1706- SetResult(0);
1707- } else {
1708- SetResult(1);
1709- }
1710-
1711- return Err;
1712-}
1713-
1714-WORD TTLFileReadln()
1715-{
1716- WORD Err;
1717- TVarId VarId;
1718- int fhi;
1719- HANDLE FH;
1720- int i, c;
1721- TStrVal Str;
1722- BOOL EndFile, EndLine;
1723- BYTE b;
1724-
1725- Err = 0;
1726- GetIntVal(&fhi, &Err);
1727- FH = HandleGet(fhi);
1728- GetStrVar(&VarId, &Err);
1729- if ((Err==0) && (GetFirstChar()!=0))
1730- Err = ErrSyntax;
1731- if (Err!=0) return Err;
1732-
1733- i = 0;
1734- EndLine = FALSE;
1735- EndFile = TRUE;
1736- do {
1737- c = _lread(FH, &b, 1);
1738- if (c>0) EndFile = FALSE;
1739- if (c==1) {
1740- switch (b) {
1741- case 0x0d:
1742- c = _lread(FH, &b, 1);
1743- if ((c==1) && (b!=0x0a))
1744- _llseek(FH, -1, 1);
1745- EndLine = TRUE;
1746- break;
1747- case 0x0a: EndLine = TRUE; break;
1748- default:
1749- if (i<MaxStrLen-1)
1750- {
1751- Str[i] = b;
1752- i++;
1753- }
1754- }
1755- }
1756- } while ((c>=1) && (! EndLine));
1757-
1758- if (EndFile)
1759- SetResult(1);
1760- else
1761- SetResult(0);
1762-
1763- Str[i] = 0;
1764- SetStrVal(VarId, Str);
1765- return Err;
1766-}
1767-
1768-
1769-// Format: fileread <file handle> <read byte> <strvar>
1770-// 指定したバイト数だけファイルから読み込む。
1771-// ただし、<read byte>は 1〜255 まで。
1772-// (2006.11.1 yutaka)
1773-WORD TTLFileRead()
1774-{
1775- WORD Err;
1776- TVarId VarId;
1777- int fhi;
1778- HANDLE FH;
1779- int i, c;
1780- int ReadByte; // 読み込むバイト数
1781- TStrVal Str;
1782- BOOL EndFile, EndLine;
1783- BYTE b;
1784-
1785- Err = 0;
1786- GetIntVal(&fhi,&Err);
1787- FH = HandleGet(fhi);
1788- GetIntVal(&ReadByte,&Err);
1789- GetStrVar(&VarId,&Err);
1790- if ((Err==0) && (GetFirstChar()!=0))
1791- Err = ErrSyntax;
1792- if ((Err==0) && (ReadByte < 1 || ReadByte > MaxStrLen-1)) // 範囲チェック
1793- Err = ErrSyntax;
1794- if (Err!=0) return Err;
1795-
1796- EndLine = FALSE;
1797- EndFile = FALSE;
1798- for (i = 0 ; i < ReadByte ; i++) {
1799- c = _lread(FH,&b,1);
1800- if (c <= 0) { // EOF
1801- EndFile = TRUE;
1802- break;
1803- }
1804- if (i<MaxStrLen-1)
1805- {
1806- Str[i] = b;
1807- }
1808- }
1809-
1810- if (EndFile)
1811- SetResult(1);
1812- else
1813- SetResult(0);
1814-
1815- Str[i] = 0;
1816- SetStrVal(VarId,Str);
1817- return Err;
1818-}
1819-
1820-
1821-WORD TTLFileRename()
1822-{
1823- WORD Err;
1824- TStrVal FName1, FName2;
1825-
1826- Err = 0;
1827- GetStrVal(FName1,&Err);
1828- GetStrVal(FName2,&Err);
1829- if ((Err==0) &&
1830- ((strlen(FName1)==0) ||
1831- (strlen(FName2)==0) ||
1832- (GetFirstChar()!=0)))
1833- Err = ErrSyntax;
1834- if (Err!=0) {
1835- SetResult(1);
1836- return Err;
1837- }
1838- if (_stricmp(FName1,FName2)==0) {
1839- SetResult(2);
1840- return Err;
1841- }
1842-
1843- if (!GetAbsPath(FName1,sizeof(FName1))) {
1844- SetResult(-1);
1845- return Err;
1846- }
1847- if (!GetAbsPath(FName2,sizeof(FName2))) {
1848- SetResult(-2);
1849- return Err;
1850- }
1851- if (rename(FName1,FName2) != 0) {
1852- // リネームに失敗したら、エラーで返す。
1853- SetResult(-3);
1854- return Err;
1855- }
1856-
1857- SetResult(0);
1858- return Err;
1859-}
1860-
1861-WORD TTLFileSearch()
1862-{
1863- WORD Err;
1864- TStrVal FName;
1865-
1866- Err = 0;
1867- GetStrVal(FName,&Err);
1868- if ((Err==0) &&
1869- ((strlen(FName)==0) || (GetFirstChar()!=0)))
1870- Err = ErrSyntax;
1871- if (Err!=0) return Err;
1872-
1873- GetAbsPath(FName,sizeof(FName));
1874- if (DoesFileExist(FName))
1875- SetResult(1);
1876- else
1877- SetResult(0);
1878- return Err;
1879-}
1880-
1881-WORD TTLFileSeek()
1882-{
1883- WORD Err;
1884- int fhi;
1885- HANDLE FH;
1886- int i, j;
1887-
1888- Err = 0;
1889- GetIntVal(&fhi,&Err);
1890- FH = HandleGet(fhi);
1891- GetIntVal(&i,&Err);
1892- GetIntVal(&j,&Err);
1893- if ((Err==0) && (GetFirstChar()!=0))
1894- Err = ErrSyntax;
1895- if (Err!=0) return Err;
1896- _llseek(FH,i,j);
1897- return Err;
1898-}
1899-
1900-WORD TTLFileSeekBack()
1901-{
1902- WORD Err;
1903- int fhi;
1904- HANDLE FH;
1905-
1906- Err = 0;
1907- GetIntVal(&fhi,&Err);
1908- FH = HandleGet(fhi);
1909- if ((Err==0) && (GetFirstChar()!=0))
1910- Err = ErrSyntax;
1911- if (Err!=0) return Err;
1912- /* move back to the marked pos */
1913- _llseek(FH,FPointer[fhi],0);
1914- return Err;
1915-}
1916-
1917-WORD TTLFileStat()
1918-{
1919- WORD Err;
1920- TVarId SizeVarId, TimeVarId, DrvVarId;
1921- TStrVal FName, TimeStr, DrvStr;
1922- struct _stat st;
1923- int ret;
1924- int result = -1;
1925- struct tm *tmp;
1926-
1927- Err = 0;
1928- GetStrVal(FName,&Err);
1929- if ((Err==0) &&
1930- (strlen(FName)==0))
1931- Err = ErrSyntax;
1932- if (Err!=0) return Err;
1933-
1934- if (!GetAbsPath(FName,sizeof(FName))) {
1935- goto end;
1936- }
1937-
1938- ret = _stat(FName, &st);
1939- if (ret != 0) {
1940- goto end;
1941- }
1942-
1943- if (CheckParameterGiven()) {
1944- GetIntVar(&SizeVarId,&Err);
1945- if (Err!=0) return Err;
1946- SetIntVal(SizeVarId, st.st_size);
1947- }
1948-
1949- if (CheckParameterGiven()) {
1950- GetStrVar(&TimeVarId,&Err);
1951- if (Err!=0) return Err;
1952- tmp = localtime(&st.st_mtime);
1953- strftime(TimeStr, sizeof(TimeStr), "%Y-%m-%d %H:%M:%S", tmp);
1954- SetStrVal(TimeVarId, TimeStr);
1955- }
1956-
1957- if (CheckParameterGiven()) {
1958- GetStrVar(&DrvVarId,&Err);
1959- if (Err!=0) return Err;
1960- _snprintf_s(DrvStr, sizeof(DrvStr), _TRUNCATE, "%c", st.st_dev + 'A');
1961- SetStrVal(DrvVarId, DrvStr);
1962- }
1963-
1964- result = 0;
1965-
1966-end:
1967- SetResult(result);
1968-
1969- return Err;
1970-}
1971-
1972-WORD TTLFileStrSeek()
1973-{
1974- WORD Err;
1975- int fhi;
1976- HANDLE FH;
1977- int Len, i, c;
1978- TStrVal Str;
1979- BYTE b;
1980- long int pos;
1981-
1982- Err = 0;
1983- GetIntVal(&fhi,&Err);
1984- FH = HandleGet(fhi);
1985- GetStrVal(Str,&Err);
1986- if ((Err==0) &&
1987- ((strlen(Str)==0) || (GetFirstChar()!=0)))
1988- Err = ErrSyntax;
1989- if (Err!=0) return Err;
1990- pos = _llseek(FH,0,1);
1991- if (pos == INVALID_SET_FILE_POINTER) return Err;
1992-
1993- Len = strlen(Str);
1994- i = 0;
1995- do {
1996- c = _lread(FH,&b,1);
1997- if (c==1)
1998- {
1999- if (b==(BYTE)Str[i])
2000- i++;
2001- else if (i>0) {
2002- i = 0;
2003- if (b==(BYTE)Str[0])
2004- i = 1;
2005- }
2006- }
2007- } while ((c>=1) && (i!=Len));
2008- if (i==Len)
2009- SetResult(1);
2010- else {
2011- SetResult(0);
2012- _llseek(FH,pos,0);
2013- }
2014- return Err;
2015-}
2016-
2017-WORD TTLFileStrSeek2()
2018-{
2019- WORD Err;
2020- int fhi;
2021- HANDLE FH;
2022- int Len, i, c;
2023- TStrVal Str;
2024- BYTE b;
2025- long int pos, pos2;
2026- BOOL Last;
2027-
2028- Err = 0;
2029- GetIntVal(&fhi,&Err);
2030- FH = HandleGet(fhi);
2031- GetStrVal(Str,&Err);
2032- if ((Err==0) &&
2033- ((strlen(Str)==0) || (GetFirstChar()!=0)))
2034- Err = ErrSyntax;
2035- if (Err!=0) return Err;
2036- pos = _llseek(FH,0,1);
2037- if (pos == INVALID_SET_FILE_POINTER) return Err;
2038-
2039- Len = strlen(Str);
2040- i = 0;
2041- pos2 = pos;
2042- do {
2043- Last = (pos2<=0);
2044- c = _lread(FH,&b,1);
2045- pos2 = _llseek(FH,-2,1);
2046- if (c==1)
2047- {
2048- if (b==(BYTE)Str[Len-1-i])
2049- i++;
2050- else if (i>0) {
2051- i = 0;
2052- if (b==(BYTE)Str[Len-1])
2053- i = 1;
2054- }
2055- }
2056- } while (!Last && (i!=Len));
2057- if (i==Len) {
2058- // ファイルの1バイト目がヒットすると、ファイルポインタが突き破って
2059- // INVALID_SET_FILE_POINTER になるので、
2060- // ゼロオフセットになるように調整する。(2008.10.10 yutaka)
2061- if (pos2 == INVALID_SET_FILE_POINTER)
2062- _llseek(FH, 0, 0);
2063- SetResult(1);
2064- } else {
2065- SetResult(0);
2066- _llseek(FH,pos,0);
2067- }
2068- return Err;
2069-}
2070-
2071-WORD TTLFileTruncate()
2072-{
2073- WORD Err;
2074- TStrVal FName;
2075- int result = -1;
2076- int TruncByte;
2077- int fh = -1;
2078- int ret;
2079-
2080- Err = 0;
2081- GetStrVal(FName,&Err);
2082- if ((Err==0) &&
2083- (strlen(FName)==0))
2084- Err = ErrSyntax;
2085- if (Err!=0) return Err;
2086-
2087- if (!GetAbsPath(FName,sizeof(FName))) {
2088- goto end;
2089- }
2090-
2091- if (CheckParameterGiven()) {
2092- GetIntVal(&TruncByte,&Err);
2093- if (Err!=0) return Err;
2094- } else {
2095- Err = ErrSyntax;
2096- goto end;
2097- }
2098-
2099- // ファイルを指定したサイズで切り詰める。
2100- ret = _sopen_s( &fh, FName, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE );
2101- if (ret != 0) {
2102- goto end;
2103- }
2104- ret = _chsize_s(fh, TruncByte);
2105- if (ret != 0) {
2106- goto end;
2107- }
2108-
2109- result = 0;
2110- Err = 0;
2111-
2112-end:
2113- SetResult(result);
2114-
2115- if (fh != -1)
2116- _close(fh);
2117-
2118- return Err;
2119-}
2120-
2121-WORD TTLFileWrite(BOOL addCRLF)
2122-{
2123- WORD Err, P;
2124- int fhi;
2125- HANDLE FH;
2126- int Val;
2127- TStrVal Str;
2128-
2129- Err = 0;
2130- GetIntVal(&fhi, &Err);
2131- FH = HandleGet(fhi);
2132- if (Err) return Err;
2133-
2134- P = LinePtr;
2135- GetStrVal(Str, &Err);
2136- if (!Err) {
2137- if (GetFirstChar())
2138- return ErrSyntax;
2139-
2140- _lwrite(FH, Str, strlen(Str));
2141- }
2142- else if (Err == ErrTypeMismatch) {
2143- Err = 0;
2144- LinePtr = P;
2145- GetIntVal(&Val, &Err);
2146- if (Err) return Err;
2147- if (GetFirstChar())
2148- return ErrSyntax;
2149-
2150- Str[0] = Val & 0xff;
2151- _lwrite(FH, Str, 1);
2152- }
2153- else {
2154- return Err;
2155- }
2156-
2157- if (addCRLF) {
2158- _lwrite(FH,"\015\012",2);
2159- }
2160- return 0;
2161-}
2162-
2163-WORD TTLFindClose()
2164-{
2165- WORD Err;
2166- int DH;
2167-
2168- Err = 0;
2169- GetIntVal(&DH,&Err);
2170- if ((Err==0) && (GetFirstChar()!=0))
2171- Err = ErrSyntax;
2172- if (Err!=0) return Err;
2173-
2174- if ((DH>=0) && (DH<NumDirHandle) &&
2175- (DirHandle[DH]!=-1L))
2176- {
2177- _findclose(DirHandle[DH]);
2178- DirHandle[DH] = -1L;
2179- }
2180- return Err;
2181-}
2182-
2183-WORD TTLFindFirst()
2184-{
2185- TVarId DH, Name;
2186- WORD Err;
2187- TStrVal Dir;
2188- int i;
2189- struct _finddata_t data;
2190-
2191- Err = 0;
2192- GetIntVar(&DH,&Err);
2193- GetStrVal(Dir,&Err);
2194- GetStrVar(&Name,&Err);
2195- if ((Err==0) && (GetFirstChar()!=0))
2196- Err = ErrSyntax;
2197- if (Err!=0) return Err;
2198-
2199- if (Dir[0]==0) strncpy_s(Dir, sizeof(Dir),"*.*", _TRUNCATE);
2200- GetAbsPath(Dir,sizeof(Dir));
2201- i = 0;
2202- while ((i<NumDirHandle) &&
2203- (DirHandle[i]!=-1L))
2204- i++;
2205- if (i<NumDirHandle)
2206- {
2207- DirHandle[i] = _findfirst(Dir,&data);
2208- if (DirHandle[i]!=-1L)
2209- SetStrVal(Name,data.name);
2210- else
2211- i = -1;
2212- }
2213- else
2214- i = -1;
2215-
2216- SetIntVal(DH,i);
2217- if (i<0)
2218- {
2219- SetResult(0);
2220- SetStrVal(Name,"");
2221- }
2222- else
2223- SetResult(1);
2224- return Err;
2225-}
2226-
2227-WORD TTLFindNext()
2228-{
2229- TVarId Name;
2230- WORD Err;
2231- int DH;
2232- struct _finddata_t data;
2233-
2234- Err = 0;
2235- GetIntVal(&DH,&Err);
2236- GetStrVar(&Name,&Err);
2237- if ((Err==0) && (GetFirstChar()!=0))
2238- Err = ErrSyntax;
2239- if (Err!=0) return Err;
2240-
2241- if ((DH>=0) && (DH<NumDirHandle) &&
2242- (DirHandle[DH]!=-1L) &&
2243- (_findnext(DirHandle[DH],&data)==0))
2244- {
2245- SetStrVal(Name,data.name);
2246- SetResult(1);
2247- }
2248- else {
2249- SetStrVal(Name,"");
2250- SetResult(0);
2251- }
2252- return Err;
2253-}
2254-
2255-WORD TTLFlushRecv()
2256-{
2257- if (GetFirstChar()!=0)
2258- return ErrSyntax;
2259- FlushRecv();
2260- return 0;
2261-}
2262-
2263-WORD TTLFolderCreate()
2264-{
2265- WORD Err;
2266- TStrVal FName;
2267-
2268- Err = 0;
2269- GetStrVal(FName,&Err);
2270- if ((Err==0) &&
2271- ((strlen(FName)==0) || (GetFirstChar()!=0)))
2272- Err = ErrSyntax;
2273- if (Err!=0) {
2274- SetResult(1);
2275- return Err;
2276- }
2277-
2278- if (!GetAbsPath(FName,sizeof(FName))) {
2279- SetResult(-1);
2280- return Err;
2281- }
2282-
2283- if (CreateDirectory(FName, NULL) == 0) {
2284- SetResult(2);
2285- }
2286- else {
2287- SetResult(0);
2288- }
2289- return Err;
2290-}
2291-
2292-WORD TTLFolderDelete()
2293-{
2294- WORD Err;
2295- TStrVal FName;
2296-
2297- Err = 0;
2298- GetStrVal(FName,&Err);
2299- if ((Err==0) &&
2300- ((strlen(FName)==0) || (GetFirstChar()!=0)))
2301- Err = ErrSyntax;
2302- if (Err!=0) {
2303- SetResult(1);
2304- return Err;
2305- }
2306-
2307- if (!GetAbsPath(FName,sizeof(FName))) {
2308- SetResult(-1);
2309- return Err;
2310- }
2311-
2312- if (RemoveDirectory(FName) == 0) {
2313- SetResult(2);
2314- }
2315- else {
2316- SetResult(0);
2317- }
2318- return Err;
2319-}
2320-
2321-WORD TTLFolderSearch()
2322-{
2323- WORD Err;
2324- TStrVal FName;
2325-
2326- Err = 0;
2327- GetStrVal(FName,&Err);
2328- if ((Err==0) &&
2329- ((strlen(FName)==0) || (GetFirstChar()!=0)))
2330- Err = ErrSyntax;
2331- if (Err!=0) return Err;
2332-
2333- GetAbsPath(FName,sizeof(FName));
2334- if (DoesFolderExist(FName)) {
2335- SetResult(1);
2336- }
2337- else {
2338- SetResult(0);
2339- }
2340- return Err;
2341-}
2342-
2343-WORD TTLFor()
2344-{
2345- WORD Err;
2346- TVarId VarId;
2347- int ValStart, ValEnd, i;
2348-
2349- Err = 0;
2350- GetIntVar(&VarId,&Err);
2351- GetIntVal(&ValStart,&Err);
2352- GetIntVal(&ValEnd,&Err);
2353- if ((Err==0) && (GetFirstChar()!=0))
2354- Err = ErrSyntax;
2355- if (Err!=0) return Err;
2356-
2357- if (! CheckNext())
2358- { // first time
2359- Err = SetForLoop();
2360- if (Err==0)
2361- {
2362- SetIntVal(VarId,ValStart);
2363- i = CopyIntVal(VarId);
2364- if (i==ValEnd)
2365- LastForLoop();
2366- }
2367- }
2368- else { // return from 'next'
2369- i = CopyIntVal(VarId);
2370- if (i<ValEnd)
2371- i++;
2372- else if (i>ValEnd)
2373- i--;
2374- SetIntVal(VarId,i);
2375- if (i==ValEnd)
2376- LastForLoop();
2377- }
2378- return Err;
2379-}
2380-
2381-WORD TTLGetDir()
2382-{
2383- WORD Err;
2384- TVarId VarId;
2385- TStrVal Str;
2386-
2387- Err = 0;
2388- GetStrVar(&VarId,&Err);
2389- if ((Err==0) && (GetFirstChar()!=0))
2390- Err = ErrSyntax;
2391- if (Err!=0) return Err;
2392-
2393- TTMGetDir(Str, sizeof(Str));
2394- SetStrVal(VarId,Str);
2395- return Err;
2396-}
2397-
2398-WORD TTLGetEnv()
2399-{
2400- WORD Err;
2401- TVarId VarId;
2402- TStrVal Str;
2403- PCHAR Str2;
2404-
2405- Err = 0;
2406- GetStrVal(Str,&Err);
2407- GetStrVar(&VarId,&Err);
2408- if ((Err==0) && (GetFirstChar()!=0))
2409- Err = ErrSyntax;
2410- if (Err!=0) return Err;
2411-
2412- Str2 = getenv(Str);
2413-
2414- if (Str2!=NULL)
2415- SetStrVal(VarId,Str2);
2416- else
2417- SetStrVal(VarId,"");
2418- return Err;
2419-}
2420-
2421-WORD TTLGetFileAttr()
2422-{
2423- WORD Err;
2424- TStrVal Filename;
2425-
2426- Err = 0;
2427- GetStrVal(Filename,&Err);
2428- if ((Err==0) && (GetFirstChar()!=0))
2429- Err = ErrSyntax;
2430- if (Err!=0) return Err;
2431-
2432- GetAbsPath(Filename, sizeof(Filename));
2433- SetResult(GetFileAttributes(Filename));
2434-
2435- return Err;
2436-}
2437-
2438-WORD TTLGetHostname()
2439-{
2440- WORD Err;
2441- TVarId VarId;
2442- char Str[MaxStrLen];
2443-
2444- Err = 0;
2445- GetStrVar(&VarId,&Err);
2446- if ((Err==0) && (GetFirstChar()!=0))
2447- Err = ErrSyntax;
2448- if ((Err==0) && (! Linked))
2449- Err = ErrLinkFirst;
2450- if (Err!=0) return Err;
2451-
2452- Err = GetTTParam(CmdGetHostname,Str,sizeof(Str));
2453- if (Err==0)
2454- SetStrVal(VarId,Str);
2455- return Err;
2456-}
2457-
2458-#define MAX_IPADDR 30
2459-/*
2460- strdim ipaddr 10
2461- getipv4addr ipaddr num
2462- */
2463-WORD TTLGetIPv4Addr()
2464-{
2465- WORD Err;
2466- TVarId VarId, VarId2, id;
2467- WSADATA ws;
2468- INTERFACE_INFO info[MAX_IPADDR];
2469- SOCKET sock;
2470- DWORD socknum;
2471- int num, result, arysize;
2472- int i, n;
2473- IN_ADDR addr;
2474-
2475- Err = 0;
2476- GetStrAryVar(&VarId,&Err);
2477- GetIntVar(&VarId2, &Err);
2478- if ((Err==0) && (GetFirstChar()!=0))
2479- Err = ErrSyntax;
2480- if (Err!=0) return Err;
2481-
2482- // 自分自身の全IPv4アドレスを取得する。
2483- if (WSAStartup(MAKEWORD(2,2), &ws) != 0) {
2484- SetResult(-1);
2485- SetIntVal(VarId2, 0);
2486- return Err;
2487- }
2488- arysize = GetStrAryVarSize(VarId);
2489- num = 0;
2490- result = 1;
2491- sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2492- if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, info, sizeof(info), &socknum, NULL, NULL) != SOCKET_ERROR) {
2493- n = socknum / sizeof(info[0]);
2494- for (i = 0 ; i < n ; i++) {
2495- if ((info[i].iiFlags & IFF_UP) == 0)
2496- continue;
2497- if ((info[i].iiFlags & IFF_LOOPBACK) != 0)
2498- continue;
2499- addr = info[i].iiAddress.AddressIn.sin_addr;
2500-
2501- if (num < arysize) {
2502- id = GetStrVarFromArray(VarId, num, &Err);
2503- SetStrVal(id, inet_ntoa(addr));
2504- }
2505- else {
2506- result = 0;
2507- }
2508- num++;
2509- }
2510- }
2511- closesocket(sock);
2512- WSACleanup();
2513-
2514- SetResult(result);
2515- SetIntVal(VarId2, num);
2516-
2517- return Err;
2518-}
2519-
2520-
2521-// IPv6アドレスを文字列に変換する。
2522-static void myInetNtop(int Family, char *pAddr, char *pStringBuf, size_t StringBufSize)
2523-{
2524- int i;
2525- char s[16];
2526- unsigned int val;
2527-
2528- pStringBuf[0] = '\0';
2529- for (i = 0 ; i < 16 ; i++) {
2530- val = (pAddr[i]) & 0xFF;
2531- _snprintf_s(s, sizeof(s), _TRUNCATE, "%02x", val);
2532- strncat_s(pStringBuf, StringBufSize, s, _TRUNCATE);
2533- if (i != 15 && (i & 1))
2534- strncat_s(pStringBuf, StringBufSize, ":", _TRUNCATE);
2535- }
2536-}
2537-
2538-
2539-WORD TTLGetIPv6Addr()
2540-{
2541- WORD Err;
2542- TVarId VarId, VarId2, id;
2543- int num, result, arysize;
2544- DWORD ret;
2545- IP_ADAPTER_ADDRESSES addr[256];/* XXX */
2546- ULONG len = sizeof(addr);
2547- char ipv6str[64];
2548-
2549- Err = 0;
2550- GetStrAryVar(&VarId,&Err);
2551- GetIntVar(&VarId2, &Err);
2552- if ((Err==0) && (GetFirstChar()!=0))
2553- Err = ErrSyntax;
2554- if (Err!=0) return Err;
2555-
2556- // GetAdaptersAddresses がサポートされていない OS はここで return
2557- if (!HasGetAdaptersAddresses()) {
2558- SetResult(-1);
2559- SetIntVal(VarId2, 0);
2560- return Err;
2561- }
2562-
2563- // 自分自身の全IPv6アドレスを取得する。
2564- arysize = GetStrAryVarSize(VarId);
2565- num = 0;
2566- result = 1;
2567- ret = GetAdaptersAddresses(AF_INET6, 0, NULL, addr, &len);
2568- if (ret == ERROR_SUCCESS) {
2569- IP_ADAPTER_ADDRESSES *padap = &addr[0];
2570-
2571- do {
2572- IP_ADAPTER_UNICAST_ADDRESS *uni = padap->FirstUnicastAddress;
2573-
2574- if (!uni) {
2575- continue;
2576- }
2577- do {
2578- SOCKET_ADDRESS addr = uni->Address;
2579- struct sockaddr_in6 *sa;
2580-
2581- if (!(uni->Flags & IP_ADAPTER_ADDRESS_DNS_ELIGIBLE)) {
2582- continue;
2583- }
2584- sa = (struct sockaddr_in6*)addr.lpSockaddr;
2585- myInetNtop(AF_INET6, (char*)&sa->sin6_addr, ipv6str, sizeof(ipv6str));
2586-
2587- if (num < arysize) {
2588- id = GetStrVarFromArray(VarId, num, &Err);
2589- SetStrVal(id, ipv6str);
2590- }
2591- else {
2592- result = 0;
2593- }
2594- num++;
2595-
2596- } while ((uni = uni->Next));
2597- } while ((padap = padap->Next));
2598- }
2599-
2600- SetResult(result);
2601- SetIntVal(VarId2, num);
2602-
2603- return Err;
2604-}
2605-
2606-
2607-WORD TTLGetPassword()
2608-{
2609- TStrVal Str, Str2, Temp2;
2610- char Temp[512];
2611- WORD Err;
2612- TVarId VarId;
2613- int result = 0; /* failure */
2614-
2615- Err = 0;
2616- GetStrVal(Str,&Err);
2617- GetStrVal(Str2,&Err);
2618- GetStrVar(&VarId,&Err);
2619- if ((Err==0) && (GetFirstChar()!=0))
2620- Err = ErrSyntax;
2621- if (Err!=0) return Err;
2622- SetStrVal(VarId,"");
2623- if (Str[0]==0) return Err;
2624- if (Str2[0]==0) return Err;
2625-
2626- GetAbsPath(Str,sizeof(Str));
2627-
2628- GetPrivateProfileString("Password",Str2,"",
2629- Temp,sizeof(Temp), Str);
2630- if (Temp[0]==0) // password not exist
2631- {
2632- OpenInpDlg(Temp2, Str2, "Enter password", "", TRUE);
2633- if (Temp2[0]!=0) {
2634- Encrypt(Temp2,Temp);
2635- if (WritePrivateProfileString("Password",Str2,Temp, Str) != 0) {
2636- result = 1; /* success */
2637- }
2638- }
2639- }
2640- else {// password exist
2641- Decrypt(Temp,Temp2);
2642- result = 1; /* success */
2643- }
2644-
2645- SetStrVal(VarId,Temp2);
2646-
2647- SetResult(result); // 成功可否を設定する。
2648- return Err;
2649-}
2650-
2651-// setpassword 'password.dat' 'mypassword' passowrd
2652-WORD TTLSetPassword()
2653-{
2654- TStrVal FileNameStr, KeyStr;
2655- char Temp[512];
2656- WORD Err;
2657- TVarId VarId;
2658- PCHAR VarStr;
2659- int result = 0; /* failure */
2660-
2661- Err = 0;
2662- GetStrVal(FileNameStr, &Err); // ファイル名
2663- GetStrVal(KeyStr, &Err); // キー名
2664- GetStrVar(&VarId, &Err);
2665- VarStr = StrVarPtr(VarId); // 変数へのポインタ
2666- if ((Err==0) && (GetFirstChar()!=0))
2667- Err = ErrSyntax;
2668- if (Err!=0) return Err;
2669-
2670- // 文字列が空の場合はエラーとする。
2671- if (FileNameStr[0]==0 ||
2672- KeyStr[0]==0 ||
2673- VarStr[0]==0) // "getpassword"同様、空パスワードも許可しない。
2674- Err = ErrSyntax;
2675- if (Err!=0) return Err;
2676-
2677- GetAbsPath(FileNameStr, sizeof(FileNameStr));
2678-
2679- // パスワードを暗号化する。
2680- Encrypt(VarStr, Temp);
2681-
2682- if (WritePrivateProfileString("Password", KeyStr, Temp, FileNameStr) != 0)
2683- result = 1; /* success */
2684-
2685- SetResult(result); // 成功可否を設定する。
2686- return Err;
2687-}
2688-
2689-// ispassword 'password.dat' 'username' ; result: 0=false; 1=true
2690-WORD TTLIsPassword()
2691-{
2692- TStrVal FileNameStr, KeyStr;
2693- char Temp[512];
2694- WORD Err;
2695- int result = 0;
2696-
2697- Err = 0;
2698- GetStrVal(FileNameStr, &Err); // ファイル名
2699- GetStrVal(KeyStr, &Err); // キー名
2700- if ((Err==0) && (GetFirstChar()!=0))
2701- Err = ErrSyntax;
2702- if (Err!=0) return Err;
2703-
2704- // 文字列が空の場合はエラーとする。
2705- if (FileNameStr[0]==0 ||
2706- KeyStr[0]==0)
2707- Err = ErrSyntax;
2708- if (Err!=0) return Err;
2709-
2710- GetAbsPath(FileNameStr, sizeof(FileNameStr));
2711-
2712- Temp[0] = 0;
2713- GetPrivateProfileString("Password", KeyStr,"",
2714- Temp, sizeof(Temp), FileNameStr);
2715- if (Temp[0] == 0) { // password not exist
2716- result = 0;
2717- } else {
2718- result = 1;
2719- }
2720-
2721- SetResult(result); // 成功可否を設定する。
2722- return Err;
2723-}
2724-
2725-WORD TTLGetSpecialFolder()
2726-{
2727- WORD Err;
2728- TVarId VarId;
2729- TStrVal type;
2730- int result;
2731-
2732- Err = 0;
2733- GetStrVar(&VarId,&Err);
2734- if (Err!=0) return Err;
2735-
2736- GetStrVal(type,&Err);
2737- if ((Err==0) && (GetFirstChar()!=0))
2738- Err = ErrSyntax;
2739- if (Err!=0) {
2740- return Err;
2741- }
2742-
2743- result = GetSpecialFolder(StrVarPtr(VarId), sizeof(TStrVal), type);
2744- SetResult(result);
2745-
2746- return Err;
2747-}
2748-
2749-WORD TTLGetTime(WORD mode)
2750-{
2751- WORD Err;
2752- TVarId VarId;
2753- TStrVal Str1, Str2, tzStr;
2754- time_t time1;
2755- struct tm *ptm;
2756- char *format;
2757- BOOL set_result;
2758- const char *tz = NULL;
2759- char tz_copy[128];
2760-
2761- // Save timezone
2762- tz = getenv("TZ");
2763- tz_copy[0] = 0;
2764- if (tz)
2765- strncpy_s(tz_copy, sizeof(tz_copy), tz, _TRUNCATE);
2766-
2767- Err = 0;
2768- GetStrVar(&VarId,&Err);
2769-
2770- if (CheckParameterGiven()) {
2771- GetStrVal(Str1, &Err);
2772- format = Str1;
2773-
2774- if (isInvalidStrftimeChar(Str1)) {
2775- SetResult(2);
2776- return 0;
2777-// return ErrSyntax;
2778- }
2779- set_result = TRUE;
2780-
2781- // タイムゾーンの指定があれば、localtime()に影響させる。(2012.5.2 yutaka)
2782- if (CheckParameterGiven()) {
2783- GetStrVal(tzStr, &Err);
2784- if (Err!=0) return Err;
2785- _putenv_s("TZ", tzStr);
2786- _tzset();
2787- }
2788-
2789- }
2790- else {
2791- switch (mode) {
2792- case RsvGetDate:
2793- format = "%Y-%m-%d";
2794- break;
2795- case RsvGetTime:
2796- format = "%H:%M:%S";
2797- break;
2798- }
2799- set_result = FALSE;
2800- }
2801-
2802- if ((Err==0) && (GetFirstChar()!=0))
2803- Err = ErrSyntax;
2804- if (Err!=0) return Err;
2805-
2806- // get current date & time
2807- time1 = time(NULL);
2808- ptm = localtime(&time1);
2809-
2810- if (strftime(Str2, sizeof(Str2), format, ptm)) {
2811- SetStrVal(VarId,Str2);
2812- if (set_result) SetResult(0);
2813- }
2814- else {
2815- if (set_result) SetResult(1);
2816- }
2817-
2818- // Restore timezone
2819- _putenv_s("TZ", tz_copy);
2820- _tzset();
2821-
2822- return Err;
2823-}
2824-
2825-WORD TTLGetTitle()
2826-{
2827- TVarId VarId;
2828- WORD Err;
2829- char Str[TitleBuffSize*2];
2830-
2831- Err = 0;
2832- GetStrVar(&VarId,&Err);
2833- if ((Err==0) && (GetFirstChar()!=0))
2834- Err = ErrSyntax;
2835- if ((Err==0) && (! Linked))
2836- Err = ErrLinkFirst;
2837- if (Err!=0) return Err;
2838-
2839- Err = GetTTParam(CmdGetTitle,Str,sizeof(Str));
2840- if (Err==0)
2841- SetStrVal(VarId,Str);
2842- return Err;
2843-}
2844-
2845-WORD TTLGetTTDir()
2846-{
2847- TVarId VarId;
2848- WORD Err;
2849- char Temp[MAX_PATH],HomeDir[MAX_PATH];
2850-
2851- Err = 0;
2852- GetStrVar(&VarId,&Err);
2853- if ((Err==0) && (GetFirstChar()!=0))
2854- Err = ErrSyntax;
2855- if (Err!=0) return Err;
2856-
2857- if (GetModuleFileName(NULL, Temp, sizeof(Temp)) == 0) {
2858- SetStrVal(VarId,"");
2859- SetResult(0);
2860- return Err;
2861- }
2862- ExtractDirName(Temp, HomeDir);
2863- SetStrVal(VarId,HomeDir);
2864- SetResult(1);
2865-
2866- return Err;
2867-}
2868-
2869-// COMポートからレジスタ値を読む。
2870-// (2015.1.8 yutaka)
2871-WORD TTLGetModemStatus()
2872-{
2873- TVarId VarId;
2874- WORD Err;
2875- char Str[MaxStrLen];
2876- int Num;
2877-
2878- Err = 0;
2879- GetIntVar(&VarId, &Err);
2880- if ((Err == 0) && (GetFirstChar() != 0))
2881- Err = ErrSyntax;
2882- if ((Err == 0) && (!Linked))
2883- Err = ErrLinkFirst;
2884- if (Err != 0) return Err;
2885-
2886- memset(Str, 0, sizeof(Str));
2887- Err = GetTTParam(CmdGetModemStatus, Str, sizeof(Str));
2888- if (Err == 0) {
2889- Num = atoi(Str);
2890- SetIntVal(VarId, Num);
2891- SetResult(0);
2892- }
2893- else {
2894- SetResult(1);
2895- }
2896-
2897- return Err;
2898-}
2899-
2900-//
2901-// Tera Term のバージョン取得 & 比較
2902-// バージョン番号はコンパイル時に決定する。
2903-// (現在は実行ファイルのバージョン情報は参照しない)
2904-//
2905-WORD TTLGetVer()
2906-{
2907- TVarId VarId;
2908- WORD Err;
2909- TStrVal Str1, Str2;
2910- int cur_major = TT_VERSION_MAJOR;
2911- int cur_minor = TT_VERSION_MINOR;
2912- int compare = 0;
2913- int comp_major, comp_minor, ret;
2914- int cur_ver, comp_ver;
2915-
2916- Err = 0;
2917- GetStrVar(&VarId, &Err);
2918-
2919- if (CheckParameterGiven()) {
2920- GetStrVal(Str1, &Err);
2921-
2922- ret = sscanf_s(Str1, "%d.%d", &comp_major, &comp_minor);
2923- if (ret != 2) {
2924- SetResult(-2);
2925- return 0;
2926- //Err = ErrSyntax;
2927- }
2928- compare = 1;
2929-
2930- } else {
2931- compare = 0;
2932-
2933- }
2934-
2935- if ((Err==0) && (GetFirstChar()!=0))
2936- Err = ErrSyntax;
2937- if (Err!=0) return Err;
2938-
2939- _snprintf_s(Str2, sizeof(Str2), _TRUNCATE, "%d.%d", cur_major, cur_minor);
2940- SetStrVal(VarId, Str2);
2941-
2942- if (compare == 1) {
2943- cur_ver = cur_major * 10000 + cur_minor;
2944- comp_ver = comp_major * 10000 + comp_minor;
2945-
2946- if (cur_ver < comp_ver) {
2947- SetResult(-1);
2948- } else if (cur_ver == comp_ver) {
2949- SetResult(0);
2950- } else {
2951- SetResult(1);
2952- }
2953- }
2954-
2955- return Err;
2956-}
2957-
2958-WORD TTLGoto()
2959-{
2960- TName LabName;
2961- WORD Err, VarType;
2962- TVarId VarId;
2963-
2964- if (GetLabelName(LabName) && (GetFirstChar()==0))
2965- {
2966- if (CheckVar(LabName,&VarType,&VarId) && (VarType==TypLabel))
2967- {
2968- JumpToLabel(VarId);
2969- Err = 0;
2970- }
2971- else
2972- Err = ErrLabelReq;
2973- }
2974- else
2975- Err = ErrSyntax;
2976-
2977- return Err;
2978-}
2979-
2980-// add 'ifdefined' (2006.9.23 maya)
2981-WORD TTLIfDefined()
2982-{
2983- WORD VarType, Err;
2984- int Val;
2985-
2986- GetVarType(&VarType,&Val,&Err);
2987-
2988- SetResult(VarType);
2989-
2990- return Err;
2991-}
2992-
2993-BOOL CheckThen(LPWORD Err)
2994-{
2995- BYTE b;
2996- TName Temp;
2997-
2998- do {
2999-
3000- do {
3001- b = GetFirstChar();
3002- if (b==0) return FALSE;
3003- } while (((b<'A') || (b>'Z')) &&
3004- (b!='_') &&
3005- ((b<'a') || (b>'z')));
3006- LinePtr--;
3007- if (! GetIdentifier(Temp)) return FALSE;
3008-
3009- } while (_stricmp(Temp,"then")!=0);
3010-
3011- if (GetFirstChar()!=0)
3012- *Err = ErrSyntax;
3013- return TRUE;
3014-}
3015-
3016-WORD TTLIf()
3017-{
3018- WORD Err, ValType, Tmp, WId;
3019- int Val;
3020-
3021- if (! GetExpression(&ValType,&Val,&Err))
3022- return ErrSyntax;
3023-
3024- if (Err!=0) return Err;
3025-
3026- if (ValType!=TypInteger)
3027- return ErrTypeMismatch;
3028-
3029- Tmp = LinePtr;
3030- if (GetReservedWord(&WId) &&
3031- (WId==RsvThen))
3032- { // If then ... EndIf structure
3033- if (GetFirstChar()!=0)
3034- return ErrSyntax;
3035- IfNest++;
3036- if (Val==0)
3037- ElseFlag = 1; // Skip until 'Else' or 'EndIf'
3038- }
3039- else { // single line If command
3040- LinePtr = Tmp;
3041- if (!CheckParameterGiven())
3042- return ErrSyntax;
3043- if (Val==0)
3044- return 0;
3045- return ExecCmnd();
3046- }
3047- return Err;
3048-}
3049-
3050-WORD TTLInclude()
3051-{
3052- WORD Err;
3053- TStrVal Str;
3054-
3055- Err = 0;
3056- GetStrVal(Str,&Err);
3057- if (!GetAbsPath(Str,sizeof(Str))) {
3058- Err = ErrCantOpen;
3059- return Err;
3060- }
3061- if (! BuffInclude(Str)) {
3062- Err = ErrCantOpen;
3063- return Err;
3064- }
3065-
3066- return Err;
3067-}
3068-
3069-WORD TTLInputBox(BOOL Paswd)
3070-{
3071- TStrVal Str1, Str2, Str3;
3072- WORD Err, ValType, P;
3073- TVarId VarId;
3074- int sp = 0;
3075-
3076- Err = 0;
3077- GetStrVal(Str1,&Err);
3078- GetStrVal(Str2,&Err);
3079- if (Err!=0) return Err;
3080-
3081- if (!Paswd && CheckParameterGiven()) {
3082- // get 3rd arg(optional)
3083- P = LinePtr;
3084- GetStrVal(Str3,&Err);
3085- if (Err == ErrTypeMismatch) {
3086- strncpy_s(Str3,sizeof(Str3),"",_TRUNCATE);
3087- LinePtr = P;
3088- Err = 0;
3089- }
3090- }
3091- else {
3092- strncpy_s(Str3,sizeof(Str3),"",_TRUNCATE);
3093- }
3094-
3095- // get 4th(3rd) arg(optional) if given
3096- if (CheckParameterGiven()) {
3097- GetIntVal(&sp, &Err);
3098- }
3099-
3100- if ((Err==0) && (GetFirstChar()!=0))
3101- Err = ErrSyntax;
3102- if (Err!=0) return Err;
3103-
3104- if (sp) {
3105- RestoreNewLine(Str1);
3106- }
3107-
3108- SetInputStr("");
3109- if (CheckVar("inputstr",&ValType,&VarId) &&
3110- (ValType==TypString))
3111- OpenInpDlg(StrVarPtr(VarId),Str1,Str2,Str3,Paswd);
3112- return Err;
3113-}
3114-
3115-WORD TTLInt2Str()
3116-{
3117- TVarId VarId;
3118- WORD Err;
3119- int Num;
3120- TStrVal Str2;
3121-
3122- Err = 0;
3123- GetStrVar(&VarId,&Err);
3124-
3125- GetIntVal(&Num,&Err);
3126- if ((Err==0) && (GetFirstChar()!=0))
3127- Err = ErrSyntax;
3128- if (Err!=0) return Err;
3129-
3130- _snprintf_s(Str2,sizeof(Str2),_TRUNCATE,"%d",Num);
3131-
3132- SetStrVal(VarId,Str2);
3133- return Err;
3134-}
3135-
3136-//
3137-// logrotate size value
3138-// logrotate rotate num
3139-// logrotate halt
3140-//
3141-WORD TTLLogRotate()
3142-{
3143- WORD Err;
3144- char Str[MaxStrLen];
3145- char Str2[MaxStrLen];
3146- char buf[MaxStrLen*2];
3147- int size, num, len;
3148-
3149- Err = 0;
3150- GetStrVal(Str, &Err);
3151- if ((Err==0) && (! Linked))
3152- Err = ErrLinkFirst;
3153- if (Err!=0) return Err;
3154-
3155- Err = ErrSyntax;
3156- if (strcmp(Str, "size") == 0) { // ローテートサイズ
3157- if (CheckParameterGiven()) {
3158- Err = 0;
3159- size = 0;
3160- GetStrVal(Str2, &Err);
3161- if (Err == 0) {
3162- len = strlen(Str2);
3163- if (isdigit(Str2[len-1])) {
3164- num = 1;
3165- } else if (Str2[len-1] == 'K') {
3166- Str2[len-1] = 0;
3167- num = 1024;
3168- } else if (Str2[len-1] == 'M') {
3169- Str2[len-1] = 0;
3170- num = 1024*1024;
3171- } else {
3172- Err = ErrSyntax;
3173- }
3174- size = atoi(Str2) * num;
3175- }
3176-
3177- if (size < 128)
3178- Err = ErrSyntax;
3179- if (Err == 0)
3180- _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s %u", Str, size);
3181- }
3182-
3183- } else if (strcmp(Str, "rotate") == 0) { // ローテートの世代数
3184- if (CheckParameterGiven()) {
3185- Err = 0;
3186- num = 0;
3187- GetIntVal(&num, &Err);
3188- if (num <= 0)
3189- Err = ErrSyntax;
3190- if (Err == 0)
3191- _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s %u", Str, num);
3192- }
3193-
3194- } else if (strcmp(Str, "halt") == 0) {
3195- Err = 0;
3196- _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s", Str);
3197- }
3198- if (Err!=0) return Err;
3199-
3200- SetFile(buf);
3201- Err = SendCmnd(CmdLogRotate, 0);
3202-
3203- return Err;
3204-}
3205-
3206-WORD TTLLogInfo()
3207-{
3208- WORD Err;
3209- TVarId VarId;
3210- char Str[MaxStrLen];
3211-
3212- Err = 0;
3213- GetStrVar(&VarId, &Err);
3214- if ((Err==0) && (GetFirstChar()!=0))
3215- Err = ErrSyntax;
3216- if ((Err==0) && (! Linked))
3217- Err = ErrLinkFirst;
3218- if (Err!=0) return Err;
3219-
3220- Err = GetTTParam(CmdLogInfo, Str, sizeof(Str));
3221- if (Err==0) {
3222- SetResult(Str[0] - '0');
3223- SetStrVal(VarId, Str+1);
3224- }
3225- return Err;
3226-}
3227-
3228-WORD TTLLogOpen()
3229-{
3230- TStrVal Str;
3231- WORD Err;
3232- int LogFlags[LogOptMax+1] = { 0 };
3233- int TmpFlag;
3234- char ret[2];
3235-
3236- Err = 0;
3237- GetStrVal(Str, &Err);
3238- GetIntVal(&LogFlags[LogOptBinary], &Err);
3239- GetIntVal(&LogFlags[LogOptAppend], &Err);
3240-
3241- // plain text
3242- if (!CheckParameterGiven()) goto EndLogOptions;
3243- GetIntVal(&LogFlags[LogOptPlainText], &Err);
3244- if (Err!=0) return Err;
3245-
3246- // timestamp
3247- if (!CheckParameterGiven()) goto EndLogOptions;
3248- GetIntVal(&LogFlags[LogOptTimestamp], &Err);
3249- if (Err!=0) return Err;
3250-
3251- // hide file transfer dialog
3252- if (!CheckParameterGiven()) goto EndLogOptions;
3253- GetIntVal(&LogFlags[LogOptHideDialog], &Err);
3254- if (Err!=0) return Err;
3255-
3256- // Include screen buffer
3257- if (!CheckParameterGiven()) goto EndLogOptions;
3258- GetIntVal(&LogFlags[LogOptIncScrBuff], &Err);
3259- if (Err!=0) return Err;
3260-
3261- // Timestamp Type
3262- if (!CheckParameterGiven()) goto EndLogOptions;
3263- GetIntVal(&TmpFlag, &Err);
3264- if (Err!=0) return Err;
3265- if (TmpFlag < 0 || TmpFlag > 3)
3266- return ErrSyntax;
3267- LogFlags[LogOptTimestampType] = TmpFlag;
3268-
3269-EndLogOptions:
3270- if (strlen(Str) == 0 || GetFirstChar() != 0)
3271- return ErrSyntax;
3272-
3273- SetFile(Str);
3274- SetLogOption(LogFlags);
3275-
3276- memset(ret, 0, sizeof(ret));
3277- Err = GetTTParam(CmdLogOpen,ret,sizeof(ret));
3278- if (Err==0) {
3279- if (ret[0] == 0x31) {
3280- SetResult(0);
3281- }
3282- else {
3283- SetResult(1);
3284- }
3285- }
3286- return Err;
3287-}
3288-
3289-WORD TTLLoop()
3290-{
3291- WORD WId, Err;
3292- int Val = 1;
3293-
3294- Err = 0;
3295- if (CheckParameterGiven()) {
3296- if (GetReservedWord(&WId)) {
3297- switch (WId) {
3298- case RsvWhile:
3299- GetIntVal(&Val,&Err);
3300- break;
3301- case RsvUntil:
3302- GetIntVal(&Val,&Err);
3303- Val = Val == 0;
3304- break;
3305- default:
3306- Err = ErrSyntax;
3307- }
3308- if ((Err==0) && (GetFirstChar()!=0))
3309- Err = ErrSyntax;
3310- }
3311- else {
3312- Err = ErrSyntax;
3313- }
3314- }
3315-
3316- if (Err!=0) return Err;
3317-
3318- return BackToWhile(Val!=0);
3319-}
3320-
3321-WORD TTLMakePath()
3322-{
3323- TVarId VarId;
3324- WORD Err;
3325- TStrVal Dir, Name;
3326-
3327- Err = 0;
3328- GetStrVar(&VarId,&Err);
3329- GetStrVal(Dir,&Err);
3330- GetStrVal(Name,&Err);
3331- if ((Err==0) &&
3332- (GetFirstChar()!=0))
3333- Err = ErrSyntax;
3334-
3335- if (Err!=0) return Err;
3336-
3337- AppendSlash(Dir,sizeof(Dir));
3338- strncat_s(Dir,sizeof(Dir),Name,_TRUNCATE);
3339- SetStrVal(VarId,Dir);
3340-
3341- return Err;
3342-}
3343-
3344-static void basedirname(char *fullpath, char *dest_base, int base_len, char *dest_dir, int dir_len) {
3345- char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
3346- char dirname[MAX_PATH];
3347- char basename[MAX_PATH];
3348-
3349- _splitpath_s(fullpath, drive, sizeof(drive), dir, sizeof(dir), fname, sizeof(fname), ext, sizeof(ext));
3350- strncpy_s(dirname, sizeof(dirname), drive, _TRUNCATE);
3351- strncat_s(dirname, sizeof(dirname), dir, _TRUNCATE);
3352- DeleteSlash(dirname); // 末尾の \ を取り除く
3353- if (strlen(fname) == 0 && strlen(ext) == 0) {
3354- _splitpath_s(dirname, drive, sizeof(drive), dir, sizeof(dir), fname, sizeof(fname), ext, sizeof(ext));
3355- strncpy_s(dirname, sizeof(dirname), drive, _TRUNCATE);
3356- strncat_s(dirname, sizeof(dirname), dir, _TRUNCATE);
3357- DeleteSlash(dirname); // 末尾の \ を取り除く
3358- strncpy_s(basename, sizeof(basename), fname, _TRUNCATE);
3359- strncat_s(basename, sizeof(basename), ext, _TRUNCATE);
3360- }
3361- else {
3362- strncpy_s(basename, sizeof(basename), fname, _TRUNCATE);
3363- strncat_s(basename, sizeof(basename), ext, _TRUNCATE);
3364- }
3365-
3366- if (dest_dir != NULL) {
3367- strncpy_s(dest_dir, dir_len, dirname, _TRUNCATE);
3368- }
3369- if (dest_base != NULL) {
3370- strncpy_s(dest_base, base_len, basename, _TRUNCATE);
3371- }
3372-}
3373-
3374-static void basename(char *fullpath, char *dest, int len) {
3375- basedirname(fullpath, dest, len, NULL, 0);
3376-}
3377-
3378-static void dirname(char *fullpath, char *dest, int len) {
3379- basedirname(fullpath, NULL, 0, dest, len);
3380-}
3381-
3382-WORD TTLBasename()
3383-{
3384- TVarId VarId;
3385- WORD Err;
3386- TStrVal Src, Name;
3387-
3388- Err = 0;
3389- GetStrVar(&VarId,&Err);
3390- GetStrVal(Src,&Err);
3391- if ((Err==0) &&
3392- (GetFirstChar()!=0))
3393- Err = ErrSyntax;
3394-
3395- if (Err!=0) return Err;
3396-
3397- basename(Src, Name, sizeof(Name));
3398- SetStrVal(VarId, Name);
3399-
3400- return Err;
3401-}
3402-
3403-WORD TTLDirname()
3404-{
3405- TVarId VarId;
3406- WORD Err;
3407- TStrVal Src, Dir;
3408-
3409- Err = 0;
3410- GetStrVar(&VarId,&Err);
3411- GetStrVal(Src,&Err);
3412- if ((Err==0) &&
3413- (GetFirstChar()!=0))
3414- Err = ErrSyntax;
3415-
3416- if (Err!=0) return Err;
3417-
3418- dirname(Src, Dir, sizeof(Dir));
3419- SetStrVal(VarId, Dir);
3420-
3421- return Err;
3422-}
3423-
3424-WORD TTLDirnameBox()
3425-{
3426- TStrVal Title;
3427- WORD Err, ValType;
3428- TVarId VarId;
3429- char buf[MAX_PATH];
3430- TStrVal InitDir = "";
3431- BOOL ret;
3432-
3433- Err = 0;
3434- GetStrVal(Title, &Err);
3435- if (Err != 0) return Err;
3436-
3437- // get 2nd arg(optional) if given
3438- if (CheckParameterGiven()) { // initdir
3439- GetStrVal(InitDir, &Err);
3440- if (Err != 0) return Err;
3441- }
3442-
3443- if ((Err == 0) && (GetFirstChar() != 0))
3444- Err = ErrSyntax;
3445- if (Err != 0) return Err;
3446-
3447- SetInputStr("");
3448- if (CheckVar("inputstr", &ValType, &VarId) &&
3449- (ValType == TypString)) {
3450- BringupWindow(HMainWin);
3451- if (doSelectFolder(HMainWin, buf, sizeof(buf), InitDir, Title)) {
3452- SetInputStr(buf);
3453- ret = 1;
3454- }
3455- else {
3456- ret = 0;
3457- }
3458- SetResult(ret);
3459- }
3460- return Err;
3461-}
3462-
3463-#define IdMsgBox 1
3464-#define IdYesNoBox 2
3465-#define IdStatusBox 3
3466-#define IdListBox 4
3467-#define LISTBOX_ITEM_NUM 10
3468-
3469-int MessageCommand(int BoxId, LPWORD Err)
3470-{
3471- TStrVal Str1, Str2;
3472- int sp = 0;
3473- int ret;
3474- char **s;
3475- int i, ary_size;
3476- int sel = 0;
3477- TVarId VarId, VarId2;
3478-
3479- *Err = 0;
3480- GetStrVal2(Str1, Err, TRUE);
3481- GetStrVal2(Str2, Err, TRUE);
3482- if (*Err!=0) return 0;
3483-
3484- if (BoxId != IdListBox) {
3485- // get 3rd arg(optional) if given
3486- if (CheckParameterGiven()) {
3487- GetIntVal(&sp, Err);
3488- }
3489- if ((*Err==0) && (GetFirstChar()!=0))
3490- *Err = ErrSyntax;
3491- if (*Err!=0) return 0;
3492- }
3493-
3494- if (sp) {
3495- RestoreNewLine(Str1);
3496- }
3497-
3498- if (BoxId==IdMsgBox) {
3499- ret = OpenMsgDlg(Str1,Str2,FALSE);
3500- // メッセージボックスをキャンセルすると、マクロの終了とする。
3501- // (2008.8.5 yutaka)
3502- if (ret == IDCANCEL) {
3503- TTLStatus = IdTTLEnd;
3504- }
3505- } else if (BoxId==IdYesNoBox) {
3506- ret = OpenMsgDlg(Str1,Str2,TRUE);
3507- // メッセージボックスをキャンセルすると、マクロの終了とする。
3508- // (2008.8.6 yutaka)
3509- if (ret == IDCLOSE) {
3510- TTLStatus = IdTTLEnd;
3511- }
3512- return (ret);
3513- }
3514- else if (BoxId==IdStatusBox) {
3515- OpenStatDlg(Str1,Str2);
3516-
3517- } else if (BoxId==IdListBox) {
3518- // リストボックスの選択肢を取得する。
3519- GetStrAryVar(&VarId, Err);
3520-
3521- if (CheckParameterGiven()) {
3522- GetIntVal(&sel, Err);
3523- }
3524- if (*Err==0 && GetFirstChar()!=0)
3525- *Err = ErrSyntax;
3526- if (*Err!=0) return 0;
3527-
3528- ary_size = GetStrAryVarSize(VarId);
3529- if (sel < 0 || sel >= ary_size) {
3530- sel = 0;
3531- }
3532-
3533- s = (char **)calloc(ary_size + 1, sizeof(char *));
3534- if (s == NULL) {
3535- *Err = ErrFewMemory;
3536- return -1;
3537- }
3538- for (i = 0 ; i < ary_size ; i++) {
3539- VarId2 = GetStrVarFromArray(VarId, i, Err);
3540- if (*Err!=0) return -1;
3541- s[i] = _strdup(StrVarPtr(VarId2));
3542- }
3543- if (s[0] == NULL) {
3544- *Err = ErrSyntax;
3545- return -1;
3546- }
3547-
3548- // return
3549- // 0以上: 選択項目
3550- // -1: キャンセル
3551- // -2: close
3552- ret = OpenListDlg(Str1, Str2, s, sel);
3553-
3554- for (i = 0 ; i < ary_size ; i++) {
3555- free(s[i]);
3556- }
3557- free(s);
3558-
3559- // リストボックスの閉じるボタン(&確認ダイアログ)で、マクロの終了とする。
3560- if (ret == -2) {
3561- TTLStatus = IdTTLEnd;
3562- }
3563- return (ret);
3564-
3565- }
3566- return 0;
3567-}
3568-
3569-// リストボックス
3570-// (2013.3.13 yutaka)
3571-WORD TTLListBox()
3572-{
3573- WORD Err;
3574- int ret;
3575-
3576- ret = MessageCommand(IdListBox, &Err);
3577- SetResult(ret);
3578- return Err;
3579-}
3580-
3581-WORD TTLMessageBox()
3582-{
3583- WORD Err;
3584-
3585- MessageCommand(IdMsgBox, &Err);
3586- return Err;
3587-}
3588-
3589-WORD TTLNext()
3590-{
3591- if (GetFirstChar()!=0)
3592- return ErrSyntax;
3593- return NextLoop();
3594-}
3595-
3596-WORD TTLPause()
3597-{
3598- int TimeOut;
3599- WORD Err;
3600-
3601- Err = 0;
3602- GetIntVal(&TimeOut,&Err);
3603- if ((Err==0) && (GetFirstChar()!=0))
3604- Err = ErrSyntax;
3605- if (Err!=0) return Err;
3606-
3607- if (TimeOut>0)
3608- {
3609- TTLStatus = IdTTLPause;
3610- TimeLimit = (DWORD)(TimeOut*1000);
3611- TimeStart = GetTickCount();
3612- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
3613- }
3614- return Err;
3615-}
3616-
3617-// add 'mpause' command
3618-// SYNOPSIS: mpause millisecoand
3619-// DESCRIPTION: This command would sleep Tera Term macro program by specified millisecond time.
3620-// (2006.2.10 yutaka)
3621-WORD TTLMilliPause()
3622-{
3623- int TimeOut;
3624- WORD Err;
3625-
3626- Err = 0;
3627- GetIntVal(&TimeOut,&Err);
3628- if ((Err==0) && (GetFirstChar()!=0))
3629- Err = ErrSyntax;
3630- if (Err!=0) return Err;
3631-
3632- if (TimeOut > 0) {
3633- Sleep(TimeOut);
3634- }
3635-
3636- return Err;
3637-}
3638-
3639-
3640-// add 'random' command
3641-// SYNOPSIS: random <intvar> <value>
3642-// DESCRIPTION:
3643-// This command generates the random value from 0 to <value> and
3644-// stores the value to <intvar>.
3645-// (2006.2.11 yutaka)
3646-WORD TTLRandom()
3647-{
3648- static int init_done = 0;
3649- static sfmt_t sfmt;
3650- TVarId VarId;
3651- WORD Err;
3652- int MaxNum;
3653- unsigned int Num, RandMin;
3654-
3655- Err = 0;
3656- GetIntVar(&VarId,&Err);
3657- GetIntVal(&MaxNum,&Err);
3658-
3659- if ( ((Err==0) && (GetFirstChar()!=0)) || MaxNum <= 0)
3660- Err = ErrSyntax;
3661- if (Err!=0) return Err;
3662-
3663- MaxNum++;
3664-
3665- /* 2**32 % x == (2**32 - x) % x */
3666- RandMin = (unsigned int) -MaxNum % MaxNum;
3667-
3668- if (!init_done) {
3669- init_done = 1;
3670- sfmt_init_gen_rand(&sfmt, (unsigned int)time(NULL));
3671- }
3672-
3673- do {
3674- Num = sfmt_genrand_uint32(&sfmt);
3675- } while (Num < RandMin);
3676-
3677- SetIntVal(VarId, (int)(Num % MaxNum));
3678-
3679- return Err;
3680-}
3681-
3682-WORD TTLRecvLn()
3683-{
3684- TStrVal Str;
3685- WORD ValType;
3686- TVarId VarId;
3687- int TimeOut;
3688-
3689- if (GetFirstChar()!=0)
3690- return ErrSyntax;
3691- if (! Linked)
3692- return ErrLinkFirst;
3693-
3694- ClearWait();
3695-
3696- Str[0] = 0x0a;
3697- Str[1] = 0;
3698- SetWait(1,Str);
3699- SetInputStr("");
3700- SetResult(1);
3701- TTLStatus = IdTTLWaitNL;
3702- TimeOut = 0;
3703- if (CheckVar("timeout",&ValType,&VarId) && (ValType==TypInteger)) {
3704- TimeOut = CopyIntVal(VarId) * 1000;
3705- }
3706- if (CheckVar("mtimeout",&ValType,&VarId) && (ValType==TypInteger)) {
3707- TimeOut += CopyIntVal(VarId);
3708- }
3709-
3710- if (TimeOut>0)
3711- {
3712- TimeLimit = (DWORD)TimeOut;
3713- TimeStart = GetTickCount();
3714- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
3715- }
3716-
3717- return 0;
3718-}
3719-
3720-WORD TTLRegexOption()
3721-{
3722- TStrVal Str;
3723- WORD Err;
3724- int opt_none_flag = 0;
3725- OnigOptionType new_opt = ONIG_OPTION_NONE;
3726- OnigEncoding new_enc = ONIG_ENCODING_UNDEF;
3727- OnigSyntaxType *new_syntax = NULL;
3728-
3729- Err = 0;
3730-
3731- while (CheckParameterGiven()) {
3732- GetStrVal(Str, &Err);
3733- if (Err)
3734- return Err;
3735-
3736- // Encoding
3737- if (_stricmp(Str, "ENCODING_ASCII")==0 || _stricmp(Str, "ASCII")==0) {
3738- if (new_enc != ONIG_ENCODING_UNDEF)
3739- return ErrSyntax;
3740- new_enc = ONIG_ENCODING_ASCII;
3741- }
3742- else if (_stricmp(Str, "ENCODING_ISO_8859_1")==0 || _stricmp(Str, "ISO_8859_1")==0) {
3743- if (new_enc != ONIG_ENCODING_UNDEF)
3744- return ErrSyntax;
3745- new_enc = ONIG_ENCODING_ISO_8859_1;
3746- }
3747- else if (_stricmp(Str, "ENCODING_ISO_8859_2")==0 || _stricmp(Str, "ISO_8859_2")==0) {
3748- if (new_enc != ONIG_ENCODING_UNDEF)
3749- return ErrSyntax;
3750- new_enc = ONIG_ENCODING_ISO_8859_2;
3751- }
3752- else if (_stricmp(Str, "ENCODING_ISO_8859_3")==0 || _stricmp(Str, "ISO_8859_3")==0) {
3753- if (new_enc != ONIG_ENCODING_UNDEF)
3754- return ErrSyntax;
3755- new_enc = ONIG_ENCODING_ISO_8859_3;
3756- }
3757- else if (_stricmp(Str, "ENCODING_ISO_8859_4")==0 || _stricmp(Str, "ISO_8859_4")==0) {
3758- if (new_enc != ONIG_ENCODING_UNDEF)
3759- return ErrSyntax;
3760- new_enc = ONIG_ENCODING_ISO_8859_4;
3761- }
3762- else if (_stricmp(Str, "ENCODING_ISO_8859_5")==0 || _stricmp(Str, "ISO_8859_5")==0) {
3763- if (new_enc != ONIG_ENCODING_UNDEF)
3764- return ErrSyntax;
3765- new_enc = ONIG_ENCODING_ISO_8859_5;
3766- }
3767- else if (_stricmp(Str, "ENCODING_ISO_8859_6")==0 || _stricmp(Str, "ISO_8859_6")==0) {
3768- if (new_enc != ONIG_ENCODING_UNDEF)
3769- return ErrSyntax;
3770- new_enc = ONIG_ENCODING_ISO_8859_6;
3771- }
3772- else if (_stricmp(Str, "ENCODING_ISO_8859_7")==0 || _stricmp(Str, "ISO_8859_7")==0) {
3773- if (new_enc != ONIG_ENCODING_UNDEF)
3774- return ErrSyntax;
3775- new_enc = ONIG_ENCODING_ISO_8859_7;
3776- }
3777- else if (_stricmp(Str, "ENCODING_ISO_8859_8")==0 || _stricmp(Str, "ISO_8859_8")==0) {
3778- if (new_enc != ONIG_ENCODING_UNDEF)
3779- return ErrSyntax;
3780- new_enc = ONIG_ENCODING_ISO_8859_8;
3781- }
3782- else if (_stricmp(Str, "ENCODING_ISO_8859_9")==0 || _stricmp(Str, "ISO_8859_9")==0) {
3783- if (new_enc != ONIG_ENCODING_UNDEF)
3784- return ErrSyntax;
3785- new_enc = ONIG_ENCODING_ISO_8859_9;
3786- }
3787- else if (_stricmp(Str, "ENCODING_ISO_8859_10")==0 || _stricmp(Str, "ISO_8859_10")==0) {
3788- if (new_enc != ONIG_ENCODING_UNDEF)
3789- return ErrSyntax;
3790- new_enc = ONIG_ENCODING_ISO_8859_10;
3791- }
3792- else if (_stricmp(Str, "ENCODING_ISO_8859_11")==0 || _stricmp(Str, "ISO_8859_11")==0) {
3793- if (new_enc != ONIG_ENCODING_UNDEF)
3794- return ErrSyntax;
3795- new_enc = ONIG_ENCODING_ISO_8859_11;
3796- }
3797- else if (_stricmp(Str, "ENCODING_ISO_8859_13")==0 || _stricmp(Str, "ISO_8859_13")==0) {
3798- if (new_enc != ONIG_ENCODING_UNDEF)
3799- return ErrSyntax;
3800- new_enc = ONIG_ENCODING_ISO_8859_13;
3801- }
3802- else if (_stricmp(Str, "ENCODING_ISO_8859_14")==0 || _stricmp(Str, "ISO_8859_14")==0) {
3803- if (new_enc != ONIG_ENCODING_UNDEF)
3804- return ErrSyntax;
3805- new_enc = ONIG_ENCODING_ISO_8859_14;
3806- }
3807- else if (_stricmp(Str, "ENCODING_ISO_8859_15")==0 || _stricmp(Str, "ISO_8859_15")==0) {
3808- if (new_enc != ONIG_ENCODING_UNDEF)
3809- return ErrSyntax;
3810- new_enc = ONIG_ENCODING_ISO_8859_15;
3811- }
3812- else if (_stricmp(Str, "ENCODING_ISO_8859_16")==0 || _stricmp(Str, "ISO_8859_16")==0) {
3813- if (new_enc != ONIG_ENCODING_UNDEF)
3814- return ErrSyntax;
3815- new_enc = ONIG_ENCODING_ISO_8859_16;
3816- }
3817- else if (_stricmp(Str, "ENCODING_UTF8")==0 || _stricmp(Str, "UTF8")==0) {
3818- if (new_enc != ONIG_ENCODING_UNDEF)
3819- return ErrSyntax;
3820- new_enc = ONIG_ENCODING_UTF8;
3821- }
3822- else if (_stricmp(Str, "ENCODING_UTF16_BE")==0 || _stricmp(Str, "UTF16_BE")==0) {
3823- if (new_enc != ONIG_ENCODING_UNDEF)
3824- return ErrSyntax;
3825- new_enc = ONIG_ENCODING_UTF16_BE;
3826- }
3827- else if (_stricmp(Str, "ENCODING_UTF16_LE")==0 || _stricmp(Str, "UTF16_LE")==0) {
3828- if (new_enc != ONIG_ENCODING_UNDEF)
3829- return ErrSyntax;
3830- new_enc = ONIG_ENCODING_UTF16_LE;
3831- }
3832- else if (_stricmp(Str, "ENCODING_UTF32_BE")==0 || _stricmp(Str, "UTF32_BE")==0) {
3833- if (new_enc != ONIG_ENCODING_UNDEF)
3834- return ErrSyntax;
3835- new_enc = ONIG_ENCODING_UTF32_BE;
3836- }
3837- else if (_stricmp(Str, "ENCODING_UTF32_LE")==0 || _stricmp(Str, "UTF32_LE")==0) {
3838- if (new_enc != ONIG_ENCODING_UNDEF)
3839- return ErrSyntax;
3840- new_enc = ONIG_ENCODING_UTF32_LE;
3841- }
3842- else if (_stricmp(Str, "ENCODING_EUC_JP")==0 || _stricmp(Str, "EUC_JP")==0) {
3843- if (new_enc != ONIG_ENCODING_UNDEF)
3844- return ErrSyntax;
3845- new_enc = ONIG_ENCODING_EUC_JP;
3846- }
3847- else if (_stricmp(Str, "ENCODING_EUC_TW")==0 || _stricmp(Str, "EUC_TW")==0) {
3848- if (new_enc != ONIG_ENCODING_UNDEF)
3849- return ErrSyntax;
3850- new_enc = ONIG_ENCODING_EUC_TW;
3851- }
3852- else if (_stricmp(Str, "ENCODING_EUC_KR")==0 || _stricmp(Str, "EUC_KR")==0) {
3853- if (new_enc != ONIG_ENCODING_UNDEF)
3854- return ErrSyntax;
3855- new_enc = ONIG_ENCODING_EUC_KR;
3856- }
3857- else if (_stricmp(Str, "ENCODING_EUC_CN")==0 || _stricmp(Str, "EUC_CN")==0) {
3858- if (new_enc != ONIG_ENCODING_UNDEF)
3859- return ErrSyntax;
3860- new_enc = ONIG_ENCODING_EUC_CN;
3861- }
3862- else if (_stricmp(Str, "ENCODING_SJIS")==0 || _stricmp(Str, "SJIS")==0
3863- || _stricmp(Str, "ENCODING_CP932")==0 || _stricmp(Str, "CP932")==0) {
3864- if (new_enc != ONIG_ENCODING_UNDEF)
3865- return ErrSyntax;
3866- new_enc = ONIG_ENCODING_SJIS;
3867- }
3868- else if (_stricmp(Str, "ENCODING_KOI8_R")==0 || _stricmp(Str, "KOI8_R")==0) {
3869- if (new_enc != ONIG_ENCODING_UNDEF)
3870- return ErrSyntax;
3871- new_enc = ONIG_ENCODING_KOI8_R;
3872- }
3873- else if (_stricmp(Str, "ENCODING_CP1251")==0 || _stricmp(Str, "CP1251")==0) {
3874- if (new_enc != ONIG_ENCODING_UNDEF)
3875- return ErrSyntax;
3876- new_enc = ONIG_ENCODING_CP1251;
3877- }
3878- else if (_stricmp(Str, "ENCODING_BIG5")==0 || _stricmp(Str, "BIG5")==0) {
3879- if (new_enc != ONIG_ENCODING_UNDEF)
3880- return ErrSyntax;
3881- new_enc = ONIG_ENCODING_BIG5;
3882- }
3883- else if (_stricmp(Str, "ENCODING_GB18030")==0 || _stricmp(Str, "GB18030")==0) {
3884- if (new_enc != ONIG_ENCODING_UNDEF)
3885- return ErrSyntax;
3886- new_enc = ONIG_ENCODING_GB18030;
3887- }
3888-
3889- // Syntax
3890- else if (_stricmp(Str, "SYNTAX_DEFAULT")==0) {
3891- if (new_syntax != NULL)
3892- return ErrSyntax;
3893- new_syntax = ONIG_SYNTAX_DEFAULT;
3894- }
3895- else if (_stricmp(Str, "SYNTAX_ASIS")==0 || _stricmp(Str, "ASIS")==0) {
3896- if (new_syntax != NULL)
3897- return ErrSyntax;
3898- new_syntax = ONIG_SYNTAX_ASIS;
3899- }
3900- else if (_stricmp(Str, "SYNTAX_POSIX_BASIC")==0 || _stricmp(Str, "POSIX_BASIC")==0) {
3901- if (new_syntax != NULL)
3902- return ErrSyntax;
3903- new_syntax = ONIG_SYNTAX_POSIX_BASIC;
3904- }
3905- else if (_stricmp(Str, "SYNTAX_POSIX_EXTENDED")==0 || _stricmp(Str, "POSIX_EXTENDED")==0) {
3906- if (new_syntax != NULL)
3907- return ErrSyntax;
3908- new_syntax = ONIG_SYNTAX_POSIX_EXTENDED;
3909- }
3910- else if (_stricmp(Str, "SYNTAX_EMACS")==0 || _stricmp(Str, "EMACS")==0) {
3911- if (new_syntax != NULL)
3912- return ErrSyntax;
3913- new_syntax = ONIG_SYNTAX_EMACS;
3914- }
3915- else if (_stricmp(Str, "SYNTAX_GREP")==0 || _stricmp(Str, "GREP")==0) {
3916- if (new_syntax != NULL)
3917- return ErrSyntax;
3918- new_syntax = ONIG_SYNTAX_GREP;
3919- }
3920- else if (_stricmp(Str, "SYNTAX_GNU_REGEX")==0 || _stricmp(Str, "GNU_REGEX")==0) {
3921- if (new_syntax != NULL)
3922- return ErrSyntax;
3923- new_syntax = ONIG_SYNTAX_GNU_REGEX;
3924- }
3925- else if (_stricmp(Str, "SYNTAX_JAVA")==0 || _stricmp(Str, "JAVA")==0) {
3926- if (new_syntax != NULL)
3927- return ErrSyntax;
3928- new_syntax = ONIG_SYNTAX_JAVA;
3929- }
3930- else if (_stricmp(Str, "SYNTAX_PERL")==0 || _stricmp(Str, "PERL")==0) {
3931- if (new_syntax != NULL)
3932- return ErrSyntax;
3933- new_syntax = ONIG_SYNTAX_PERL;
3934- }
3935- else if (_stricmp(Str, "SYNTAX_PERL_NG")==0 || _stricmp(Str, "PERL_NG")==0) {
3936- if (new_syntax != NULL)
3937- return ErrSyntax;
3938- new_syntax = ONIG_SYNTAX_PERL_NG;
3939- }
3940- else if (_stricmp(Str, "SYNTAX_RUBY")==0 || _stricmp(Str, "RUBY")==0) {
3941- if (new_syntax != NULL)
3942- return ErrSyntax;
3943- new_syntax = ONIG_SYNTAX_RUBY;
3944- }
3945-
3946- // Option
3947- else if (_stricmp(Str, "OPTION_NONE")==0) {
3948- if (new_opt != ONIG_OPTION_NONE || opt_none_flag != 0)
3949- return ErrSyntax;
3950- new_opt = ONIG_OPTION_NONE;
3951- opt_none_flag = 1;
3952- }
3953- else if (_stricmp(Str, "OPTION_SINGLELINE")==0 || _stricmp(Str, "SINGLELINE")==0) {
3954- new_opt |= ONIG_OPTION_SINGLELINE;
3955- }
3956- else if (_stricmp(Str, "OPTION_MULTILINE")==0 || _stricmp(Str, "MULTILINE")==0) {
3957- new_opt |= ONIG_OPTION_MULTILINE;
3958- }
3959- else if (_stricmp(Str, "OPTION_IGNORECASE")==0 || _stricmp(Str, "IGNORECASE")==0) {
3960- new_opt |= ONIG_OPTION_IGNORECASE;
3961- }
3962- else if (_stricmp(Str, "OPTION_EXTEND")==0 || _stricmp(Str, "EXTEND")==0) {
3963- new_opt |= ONIG_OPTION_EXTEND;
3964- }
3965- else if (_stricmp(Str, "OPTION_FIND_LONGEST")==0 || _stricmp(Str, "FIND_LONGEST")==0) {
3966- new_opt |= ONIG_OPTION_FIND_LONGEST;
3967- }
3968- else if (_stricmp(Str, "OPTION_FIND_NOT_EMPTY")==0 || _stricmp(Str, "FIND_NOT_EMPTY")==0) {
3969- new_opt |= ONIG_OPTION_FIND_NOT_EMPTY;
3970- }
3971- else if (_stricmp(Str, "OPTION_NEGATE_SINGLELINE")==0 || _stricmp(Str, "NEGATE_SINGLELINE")==0) {
3972- new_opt |= ONIG_OPTION_NEGATE_SINGLELINE;
3973- }
3974- else if (_stricmp(Str, "OPTION_DONT_CAPTURE_GROUP")==0 || _stricmp(Str, "DONT_CAPTURE_GROUP")==0) {
3975- new_opt |= ONIG_OPTION_DONT_CAPTURE_GROUP;
3976- }
3977- else if (_stricmp(Str, "OPTION_CAPTURE_GROUP")==0 || _stricmp(Str, "CAPTURE_GROUP")==0) {
3978- new_opt |= ONIG_OPTION_CAPTURE_GROUP;
3979- }
3980-
3981- else {
3982- return ErrSyntax;
3983- }
3984- }
3985-
3986-
3987- if (new_enc != ONIG_ENCODING_UNDEF) {
3988- RegexEnc = new_enc;
3989- }
3990- if (new_syntax != NULL) {
3991- RegexSyntax = new_syntax;
3992- }
3993- if (new_opt != ONIG_OPTION_NONE || opt_none_flag != 0) {
3994- RegexOpt = new_opt;
3995- }
3996-
3997- return 0;
3998-}
3999-
4000-WORD TTLReturn()
4001-{
4002- if (GetFirstChar()==0)
4003- return ReturnFromSub();
4004- else
4005- return ErrSyntax;
4006-}
4007-
4008-// add 'rotateleft' and 'rotateright' (2007.8.19 maya)
4009-#define ROTATE_DIR_LEFT 0
4010-#define ROTATE_DIR_RIGHT 1
4011-WORD BitRotate(int direction)
4012-{
4013- TVarId VarId;
4014- WORD Err;
4015- int x, n;
4016-
4017- Err = 0;
4018- GetIntVar(&VarId, &Err);
4019- GetIntVal(&x, &Err);
4020- GetIntVal(&n, &Err);
4021- if ((Err==0) && (GetFirstChar()!=0))
4022- Err = ErrSyntax;
4023- if (Err!=0) return Err;
4024-
4025- if (direction == ROTATE_DIR_RIGHT)
4026- n = -n;
4027-
4028- n %= INT_BIT;
4029-
4030- if (n < 0)
4031- n += INT_BIT;
4032-
4033- if (n == 0) {
4034- SetIntVal(VarId, x);
4035- } else {
4036- SetIntVal(VarId, (x << n) | ((unsigned int)x >> (INT_BIT-n)));
4037- }
4038- return Err;
4039-}
4040-
4041-WORD TTLRotateLeft()
4042-{
4043- return BitRotate(ROTATE_DIR_LEFT);
4044-}
4045-
4046-WORD TTLRotateRight()
4047-{
4048- return BitRotate(ROTATE_DIR_RIGHT);
4049-}
4050-
4051-WORD TTLSend()
4052-{
4053- TStrVal Str;
4054- WORD Err, ValType;
4055- int Val;
4056- BOOL EndOfLine;
4057-
4058- if (! Linked)
4059- return ErrLinkFirst;
4060-
4061- EndOfLine = FALSE;
4062- do {
4063- if (GetString(Str,&Err))
4064- {
4065- if (Err!=0) return Err;
4066- DDEOut(Str);
4067- }
4068- else if (GetExpression(&ValType,&Val,&Err))
4069- {
4070- if (Err!=0) return Err;
4071- switch (ValType) {
4072- case TypInteger: DDEOut1Byte(LOBYTE(Val)); break;
4073- case TypString: DDEOut(StrVarPtr((TVarId)Val)); break;
4074- default:
4075- return ErrTypeMismatch;
4076- }
4077- }
4078- else
4079- EndOfLine = TRUE;
4080- } while (! EndOfLine);
4081- return 0;
4082-}
4083-
4084-/*
4085- * src に含まれる 0x01 を 0x01 0x02 に置き換えて dst にコピーする。
4086- * TStrVal には 0x00 が含まれる事が無い(終端と区別できない)ので 0x00 は考慮する必要なし。
4087- */
4088-static void AddBroadcastString(char *dst, int dstlen, char *src)
4089-{
4090- int i;
4091-
4092- i = strlen(dst);
4093- dst += i;
4094- dstlen -= i;
4095-
4096- while (*src != 0 && dstlen > 1) {
4097- if (*src == 0x01) {
4098- // 0x01 を格納するには 0x01 0x02 の2バイト + NUL 終端用の1バイトが必要
4099- if (dstlen < 3) {
4100- break;
4101- }
4102- *dst++ = *src++;
4103- *dst++ = 0x02;
4104- dstlen -= 2;
4105- }
4106- else {
4107- *dst++ = *src++;
4108- dstlen--;
4109- }
4110- }
4111-
4112- *dst = 0;
4113-}
4114-
4115-/*
4116- * TTLSendBroadcast / TTLSendMulticast の下請け
4117- *
4118- * 各パラメータを連結した文字列を buff に格納して返す。
4119- * crlf が TRUE の時は各パラメータの間に "\n" を挟む。(要検討)
4120- *
4121- * パラメータが String の場合はそのまま、Integer の場合は ASCII コードとみなしてその文字を送る。
4122- * Tera Term 側では send 等と共通のルーチンが使われる為、DDE 通信の為のエンコードを行う必要有り。
4123- * 0x00 -> 0x01 0x01
4124- * 0x01 -> 0x01 0x02
4125- */
4126-static WORD GetBroadcastString(char *buff, int bufflen, BOOL crlf)
4127-{
4128- TStrVal Str;
4129- WORD Err, ValType;
4130- int Val;
4131- char tmp[3];
4132-
4133- buff[0] = '\0';
4134-
4135- while (1) {
4136- if (GetString(Str, &Err)) {
4137- if (Err!=0) return Err;
4138- AddBroadcastString(buff, bufflen, Str);
4139- }
4140- else if (GetExpression(&ValType, &Val, &Err)) {
4141- if (Err!=0) return Err;
4142- switch (ValType) {
4143- case TypInteger:
4144- Val = LOBYTE(Val);
4145- if (Val == 0 || Val == 1) {
4146- tmp[0] = 1;
4147- tmp[1] = Val + 1;
4148- tmp[2] = 0;
4149- }
4150- else {
4151- tmp[0] = Val;
4152- tmp[1] = 0;
4153- }
4154- strncat_s(buff, bufflen, tmp, _TRUNCATE);
4155- break;
4156- case TypString:
4157- AddBroadcastString(buff, bufflen, StrVarPtr((TVarId)Val));
4158- break;
4159- default:
4160- return ErrTypeMismatch;
4161- }
4162- }
4163- else {
4164- break;
4165- }
4166- }
4167- if (crlf) {
4168- strncat_s(buff, bufflen, "\r\n", _TRUNCATE);
4169- }
4170- return 0;
4171-}
4172-
4173-// sendbroadcast / sendlnbroadcast の二つから利用 (crlfの値で動作を変える)
4174-static WORD TTLSendBroadcast(BOOL crlf)
4175-{
4176- TStrVal buf;
4177- WORD Err;
4178-
4179- if (! Linked)
4180- return ErrLinkFirst;
4181-
4182- if ((Err = GetBroadcastString(buf, MaxStrLen, crlf)) != 0)
4183- return Err;
4184-
4185- SetFile(buf);
4186- return SendCmnd(CmdSendBroadcast, 0);
4187-}
4188-
4189-WORD TTLSetMulticastName()
4190-{
4191- TStrVal Str;
4192- WORD Err;
4193-
4194- Err = 0;
4195- GetStrVal(Str,&Err);
4196- if (Err!=0) return Err;
4197-
4198- SetFile(Str);
4199- return SendCmnd(CmdSetMulticastName, 0);
4200-}
4201-
4202-// sendmulticast / sendlnmulticast の二つから利用 (crlfの値で動作を変える)
4203-WORD TTLSendMulticast(BOOL crlf)
4204-{
4205- TStrVal buf, Str;
4206- WORD Err;
4207-
4208- if (! Linked)
4209- return ErrLinkFirst;
4210-
4211- // マルチキャスト識別用の名前を取得する。
4212- Err = 0;
4213- GetStrVal(Str,&Err);
4214- if (Err!=0) return Err;
4215- SetFile(Str);
4216-
4217- if ((Err = GetBroadcastString(buf, MaxStrLen, crlf)) != 0)
4218- return Err;
4219-
4220- SetSecondFile(buf);
4221- return SendCmnd(CmdSendMulticast, 0);
4222-}
4223-
4224-WORD TTLSendFile()
4225-{
4226- TStrVal Str;
4227- WORD Err;
4228- int BinFlag;
4229-
4230- Err = 0;
4231- GetStrVal(Str,&Err);
4232- GetIntVal(&BinFlag,&Err);
4233- if ((Err==0) &&
4234- ((strlen(Str)==0) || (GetFirstChar()!=0)))
4235- Err = ErrSyntax;
4236-
4237- if (Err!=0) return Err;
4238-
4239- SetFile(Str);
4240- SetBinary(BinFlag);
4241- return SendCmnd(CmdSendFile,IdTTLWaitCmndEnd);
4242-}
4243-
4244-WORD TTLSendKCode()
4245-{
4246- TStrVal Str;
4247- WORD Err;
4248- int KCode, Count;
4249-
4250- Err = 0;
4251- GetIntVal(&KCode,&Err);
4252- GetIntVal(&Count,&Err);
4253- if ((Err==0) && (GetFirstChar()!=0))
4254- Err = ErrSyntax;
4255- if (Err!=0) return Err;
4256-
4257- Word2HexStr((WORD)KCode,Str);
4258- Word2HexStr((WORD)Count,&Str[4]);
4259- SetFile(Str);
4260- return SendCmnd(CmdSendKCode,0);
4261-}
4262-
4263-WORD TTLSendLn()
4264-{
4265- WORD Err;
4266- char Str[3];
4267-
4268- Err = TTLSend();
4269- if (Err==0)
4270- {
4271- Str[0] = 0x0D;
4272- Str[1] = 0x0A;
4273- Str[2] = 0;
4274- if (Linked)
4275- DDEOut(Str);
4276- else
4277- Err = ErrLinkFirst;
4278- }
4279- return Err;
4280-}
4281-
4282-WORD TTLSetDate()
4283-{
4284- WORD Err;
4285- TStrVal Str;
4286- int y, m, d;
4287- SYSTEMTIME Time;
4288-
4289- Err = 0;
4290- GetStrVal(Str,&Err);
4291- if ((Err==0) && (GetFirstChar()!=0))
4292- Err = ErrSyntax;
4293- if (Err!=0) return Err;
4294-
4295- Str[4] = 0;
4296- if (sscanf(Str,"%u",&y)!=1) return 0;
4297- Str[7] = 0;
4298- if (sscanf(&(Str[5]),"%u",&m)!=1) return 0;
4299- Str[10] = 0;
4300- if (sscanf(&(Str[8]),"%u",&d)!=1) return 0;
4301- GetLocalTime(&Time);
4302- Time.wYear = y;
4303- Time.wMonth = m;
4304- Time.wDay = d;
4305- SetLocalTime(&Time);
4306- return Err;
4307-}
4308-
4309-WORD TTLSetDir()
4310-{
4311- WORD Err;
4312- TStrVal Str;
4313-
4314- Err = 0;
4315- GetStrVal(Str,&Err);
4316- if ((Err==0) && (GetFirstChar()!=0))
4317- Err = ErrSyntax;
4318- if (Err!=0) return Err;
4319-
4320- TTMSetDir(Str);
4321-
4322- return Err;
4323-}
4324-
4325-WORD TTLSetDlgPos()
4326-{
4327- WORD Err = 0;
4328-
4329- if (CheckParameterGiven()) {
4330- // パラメータがあれば、x y の2つのパラメータがある
4331- int x, y;
4332- GetIntVal(&x,&Err);
4333- GetIntVal(&y,&Err);
4334- if ((Err==0) && (GetFirstChar()!=0))
4335- Err = ErrSyntax;
4336- if (Err!=0) return Err;
4337- SetDlgPos(x,y);
4338- }
4339- else {
4340- // パラメータがなければデフォルト位置に戻す
4341- SetDlgPos(CW_USEDEFAULT, CW_USEDEFAULT);
4342- }
4343- return Err;
4344-}
4345-
4346-// reactivate 'setenv' (2007.8.31 maya)
4347-WORD TTLSetEnv()
4348-{
4349- WORD Err;
4350- TStrVal Str, Str2;
4351-
4352- Err = 0;
4353- GetStrVal(Str,&Err);
4354- GetStrVal(Str2,&Err);
4355- if ((Err==0) && (GetFirstChar()!=0))
4356- Err = ErrSyntax;
4357- if (Err!=0) return Err;
4358-
4359- _putenv_s(Str,Str2);
4360- return Err;
4361-}
4362-
4363-WORD TTLSetExitCode()
4364-{
4365- WORD Err;
4366- int Val;
4367-
4368- Err = 0;
4369- GetIntVal(&Val,&Err);
4370- if ((Err==0) && (GetFirstChar()!=0))
4371- Err = ErrSyntax;
4372- if (Err!=0) return Err;
4373- ExitCode = Val;
4374- return Err;
4375-}
4376-
4377-WORD TTLSetFileAttr()
4378-{
4379- WORD Err;
4380- TStrVal Filename;
4381- int attributes;
4382-
4383- Err = 0;
4384- GetStrVal(Filename,&Err);
4385- GetIntVal(&attributes,&Err);
4386- if ((Err==0) && (GetFirstChar()!=0))
4387- Err = ErrSyntax;
4388- if (Err!=0) return Err;
4389-
4390- if (SetFileAttributes(Filename, attributes) == 0) {
4391- SetResult(0);
4392- }
4393- else {
4394- SetResult(1);
4395- }
4396-
4397- return Err;
4398-}
4399-
4400-WORD TTLSetSync()
4401-{
4402- WORD Err;
4403- int Val;
4404-
4405- Err = 0;
4406- GetIntVal(&Val,&Err);
4407- if ((Err==0) && (GetFirstChar()!=0))
4408- Err = ErrSyntax;
4409- if ((Err==0) && (! Linked))
4410- Err = ErrLinkFirst;
4411- if (Err!=0) return Err;
4412-
4413- if (Val==0)
4414- SetSync(FALSE);
4415- else
4416- SetSync(TRUE);
4417-
4418- return 0;
4419-}
4420-
4421-WORD TTLSetTime()
4422-{
4423- WORD Err;
4424- TStrVal Str;
4425- int h, m, s;
4426- SYSTEMTIME Time;
4427-
4428- Err = 0;
4429- GetStrVal(Str,&Err);
4430- if ((Err==0) && (GetFirstChar()!=0))
4431- Err = ErrSyntax;
4432- if (Err!=0) return Err;
4433-
4434- Str[2] = 0;
4435- if (sscanf(Str,"%u",&h)!=1) return 0;
4436- Str[5] = 0;
4437- if (sscanf(&(Str[3]),"%u",&m)!=1) return 0;
4438- Str[8] = 0;
4439- if (sscanf(&(Str[6]),"%u",&s)!=1) return 0;
4440-
4441- GetLocalTime(&Time);
4442- Time.wHour = h;
4443- Time.wMinute = m;
4444- Time.wSecond = s;
4445- SetLocalTime(&Time);
4446-
4447- return Err;
4448-}
4449-
4450-WORD TTLShow()
4451-{
4452- WORD Err;
4453- int Val;
4454-
4455- Err = 0;
4456- GetIntVal(&Val,&Err);
4457- if ((Err==0) && (GetFirstChar()!=0))
4458- Err = ErrSyntax;
4459- if (Err!=0) return Err;
4460- if (Val==0)
4461- ShowWindow(HMainWin,SW_MINIMIZE);
4462- else if (Val>0) {
4463- ShowWindow(HMainWin,SW_RESTORE);
4464- PostMessage(HMainWin, WM_USER_MACROBRINGUP, 0, 0);
4465- }
4466- else
4467- ShowWindow(HMainWin,SW_HIDE);
4468- return Err;
4469-}
4470-
4471-// 'sprintf': Format a string in the style of sprintf
4472-//
4473-// (2007.5.1 yutaka)
4474-// (2007.5.3 maya)
4475-WORD TTLSprintf(int getvar)
4476-{
4477- TStrVal Fmt;
4478- int Num, NumWidth, NumPrecision;
4479- TStrVal Str;
4480- WORD Err = 0, TmpErr;
4481- TVarId VarId;
4482- char buf[MaxStrLen];
4483- char *p, subFmt[MaxStrLen], buf2[MaxStrLen];
4484- int width_asterisk, precision_asterisk, reg_beg, reg_end, reg_len, i;
4485- char *match_str;
4486-
4487- enum arg_type {
4488- INTEGER,
4489- DOUBLE,
4490- STRING,
4491- NONE
4492- };
4493- enum arg_type type;
4494-
4495- int r;
4496- unsigned char *start, *range, *end;
4497- regex_t* reg;
4498- OnigErrorInfo einfo;
4499- OnigRegion *region;
4500- UChar* pattern, * str;
4501-
4502- if (getvar) {
4503- GetStrVar(&VarId, &Err);
4504- if (Err!=0) {
4505- SetResult(4);
4506- return Err;
4507- }
4508- }
4509-
4510-// pattern = (UChar* )"^%[-+0 #]*(?:[1-9][0-9]*)?(?:\\.[0-9]*)?$";
4511- pattern = (UChar* )"^%[-+0 #]*([1-9][0-9]*|\\*)?(?:\\.([0-9]*|\\*))?$";
4512- // flags--------
4513- // width------------------
4514- // precision--------------------
4515-
4516- r = onig_new(&reg, pattern, pattern + strlen(pattern),
4517- ONIG_OPTION_NONE, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT,
4518- &einfo);
4519- if (r != ONIG_NORMAL) {
4520- char s[ONIG_MAX_ERROR_MESSAGE_LEN];
4521- onig_error_code_to_str(s, r, &einfo);
4522- fprintf(stderr, "ERROR: %s\n", s);
4523- SetResult(-1);
4524- goto exit2;
4525- }
4526-
4527- region = onig_region_new();
4528-
4529- GetStrVal(Fmt, &Err);
4530- if (Err!=0) {
4531- SetResult(1);
4532- goto exit2;
4533- }
4534-
4535- p = Fmt;
4536- memset(buf, 0, sizeof(buf));
4537- memset(subFmt, 0, sizeof(subFmt));
4538- while(*p != '\0') {
4539- if (strlen(subFmt)>0) {
4540- type = NONE;
4541- switch (*p) {
4542- case '%':
4543- if (strlen(subFmt) == 1) { // "%%" -> "%"
4544- strncat_s(buf, sizeof(buf), "%", _TRUNCATE);
4545- memset(subFmt, 0, sizeof(subFmt));
4546- }
4547- else {
4548- // 一つ手前までをそのまま buf に格納
4549- strncat_s(buf, sizeof(buf), subFmt, _TRUNCATE);
4550- // 仕切り直し
4551- memset(subFmt, 0, sizeof(subFmt));
4552- strncat_s(subFmt, sizeof(subFmt), p, 1);
4553- }
4554- break;
4555-
4556- case 'c':
4557- case 'd':
4558- case 'i':
4559- case 'o':
4560- case 'u':
4561- case 'x':
4562- case 'X':
4563- type = INTEGER;
4564- /* Falls through. */
4565-
4566- case 'e':
4567- case 'E':
4568- case 'f':
4569- case 'g':
4570- case 'G':
4571- case 'a':
4572- case 'A':
4573- if (type == NONE) {
4574- type = DOUBLE;
4575- }
4576- /* Falls through. */
4577-
4578- case 's':
4579- if (type == NONE) {
4580- type = STRING;
4581- }
4582-
4583- // "%" と *p の間が正しいかチェック
4584- str = (UChar* )subFmt;
4585- end = str + strlen(subFmt);
4586- start = str;
4587- range = end;
4588- r = onig_search(reg, str, end, start, range, region,
4589- ONIG_OPTION_NONE);
4590- if (r != 0) {
4591- SetResult(2);
4592- Err = ErrSyntax;
4593- goto exit1;
4594- }
4595-
4596- strncat_s(subFmt, sizeof(subFmt), p, 1);
4597-
4598- // width, precision が * かどうかチェック
4599- width_asterisk = precision_asterisk = 0;
4600- if (region->num_regs != 3) {
4601- SetResult(-1);
4602- goto exit2;
4603- }
4604- reg_beg = region->beg[1];
4605- reg_end = region->end[1];
4606- reg_len = reg_end - reg_beg;
4607- match_str = (char*)calloc(reg_len + 1, sizeof(char));
4608- for (i = 0; i < reg_len; i++) {
4609- match_str[i] = str[reg_beg + i];
4610- }
4611- if (strcmp(match_str, "*") == 0) {
4612- width_asterisk = 1;
4613- }
4614- free(match_str);
4615- reg_beg = region->beg[2];
4616- reg_end = region->end[2];
4617- reg_len = reg_end - reg_beg;
4618- match_str = (char*)calloc(reg_len + 1, sizeof(char));
4619- for (i = 0; i < reg_len; i++) {
4620- match_str[i] = str[reg_beg + i];
4621- }
4622- if (strcmp(match_str, "*") == 0) {
4623- precision_asterisk = 1;
4624- }
4625- free(match_str);
4626-
4627- // * に対応する引数を取得
4628- if (width_asterisk) {
4629- TmpErr = 0;
4630- GetIntVal(&NumWidth, &TmpErr);
4631- if (TmpErr != 0) {
4632- SetResult(3);
4633- Err = TmpErr;
4634- goto exit1;
4635- }
4636- }
4637- if (precision_asterisk) {
4638- TmpErr = 0;
4639- GetIntVal(&NumPrecision, &TmpErr);
4640- if (TmpErr != 0) {
4641- SetResult(3);
4642- Err = TmpErr;
4643- goto exit1;
4644- }
4645- }
4646-
4647- if (type == STRING || type == DOUBLE) {
4648- // 文字列として読めるかトライ
4649- TmpErr = 0;
4650- GetStrVal(Str, &TmpErr);
4651- if (TmpErr == 0) {
4652- if (type == STRING) {
4653- if (!width_asterisk && !precision_asterisk) {
4654- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, Str);
4655- }
4656- else if (width_asterisk && !precision_asterisk) {
4657- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, Str);
4658- }
4659- else if (!width_asterisk && precision_asterisk) {
4660- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumPrecision, Str);
4661- }
4662- else { // width_asterisk && precision_asterisk
4663- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, NumPrecision, Str);
4664- }
4665- }
4666- else { // DOUBLE
4667- if (!width_asterisk && !precision_asterisk) {
4668- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, atof(Str));
4669- }
4670- else if (width_asterisk && !precision_asterisk) {
4671- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, atof(Str));
4672- }
4673- else if (!width_asterisk && precision_asterisk) {
4674- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumPrecision, atof(Str));
4675- }
4676- else { // width_asterisk && precision_asterisk
4677- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, NumPrecision, atof(Str));
4678- }
4679- }
4680- }
4681- else {
4682- SetResult(3);
4683- Err = TmpErr;
4684- goto exit1;
4685- }
4686- }
4687- else {
4688- // 数値として読めるかトライ
4689- TmpErr = 0;
4690- GetIntVal(&Num, &TmpErr);
4691- if (TmpErr == 0) {
4692- if (!width_asterisk && !precision_asterisk) {
4693- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, Num);
4694- }
4695- else if (width_asterisk && !precision_asterisk) {
4696- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, Num);
4697- }
4698- else if (!width_asterisk && precision_asterisk) {
4699- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumPrecision, Num);
4700- }
4701- else { // width_asterisk && precision_asterisk
4702- _snprintf_s(buf2, sizeof(buf2), _TRUNCATE, subFmt, NumWidth, NumPrecision, Num);
4703- }
4704- }
4705- else {
4706- SetResult(3);
4707- Err = TmpErr;
4708- goto exit1;
4709- }
4710- }
4711-
4712- strncat_s(buf, sizeof(buf), buf2, _TRUNCATE);
4713- memset(subFmt, 0, sizeof(subFmt));
4714- onig_region_free(region, 0);
4715- break;
4716-
4717- default:
4718- strncat_s(subFmt, sizeof(subFmt), p, 1);
4719- }
4720- }
4721- else if (*p == '%') {
4722- strncat_s(subFmt, sizeof(subFmt), p, 1);
4723- }
4724- else if (strlen(buf) < sizeof(buf)-1) {
4725- strncat_s(buf, sizeof(buf), p, 1);
4726- }
4727- else {
4728- break;
4729- }
4730- p++;
4731- }
4732- if (strlen(subFmt) > 0) {
4733- strncat_s(buf, sizeof(buf), subFmt, _TRUNCATE);
4734- }
4735-
4736- if (getvar) {
4737- SetStrVal(VarId, buf);
4738- }
4739- else {
4740- // マッチした行を inputstr へ格納する
4741- SetInputStr(buf); // ここでバッファがクリアされる
4742- }
4743- SetResult(0);
4744-
4745-exit1:
4746- onig_region_free(region, 1);
4747-exit2:
4748- onig_free(reg);
4749- onig_end();
4750-
4751- return Err;
4752-}
4753-
4754-WORD TTLStatusBox()
4755-{
4756- WORD Err;
4757-
4758- MessageCommand(IdStatusBox, &Err);
4759- return Err;
4760-}
4761-
4762-WORD TTLStr2Code()
4763-{
4764- TVarId VarId;
4765- WORD Err;
4766- TStrVal Str;
4767- int Len, c, i;
4768- unsigned int Num;
4769-
4770- Err = 0;
4771- GetIntVar(&VarId,&Err);
4772- GetStrVal(Str,&Err);
4773- if ((Err==0) && (GetFirstChar()!=0))
4774- Err = ErrSyntax;
4775-
4776- if (Err!=0) return Err;
4777-
4778- Len = strlen(Str);
4779- if (Len > sizeof(Num))
4780- c = sizeof(Num);
4781- else
4782- c = Len;
4783- Num = 0;
4784- for (i=c; i>=1; i--)
4785- Num = Num*256 + (BYTE)Str[Len-i];
4786- SetIntVal(VarId,Num);
4787-
4788- return Err;
4789-}
4790-
4791-WORD TTLStr2Int()
4792-{
4793- TVarId VarId;
4794- WORD Err;
4795- TStrVal Str;
4796- int Num;
4797-
4798- Err = 0;
4799- GetIntVar(&VarId,&Err);
4800- GetStrVal(Str,&Err);
4801- if ((Err==0) && (GetFirstChar()!=0))
4802- Err = ErrSyntax;
4803-
4804- if (Err!=0) return Err;
4805-
4806- // C言語では16進は0xで始まるが、TTL仕様では $ で始まるため、後者もサポートする。
4807- if (Str[0] == '$') {
4808- memmove_s(Str + 2, sizeof(Str) - 2, Str + 1, strlen(Str));
4809- Str[0] = '0';
4810- Str[1] = 'x';
4811- }
4812-
4813- // '%d'から'%i'へ変更により、10進以外の数値を変換できるようにする。 (2007.5.1 yutaka)
4814- // 下位互換性のため10進と16進のみのサポートとする。(2007.5.2 yutaka)
4815- // 10 : decimal
4816- // 0x10, $10: hex
4817- if (Str[0] == '0' && tolower(Str[1]) == 'x') {
4818- if (sscanf(Str,"%i",&Num)!=1)
4819- {
4820- Num = 0;
4821- SetResult(0);
4822- }
4823- else {
4824- SetResult(1);
4825- }
4826- } else {
4827- if (sscanf(Str,"%d",&Num)!=1)
4828- {
4829- Num = 0;
4830- SetResult(0);
4831- }
4832- else {
4833- SetResult(1);
4834- }
4835- }
4836- SetIntVal(VarId,Num);
4837-
4838- return Err;
4839-}
4840-
4841-WORD TTLStrCompare()
4842-{
4843- TStrVal Str1, Str2;
4844- WORD Err;
4845- int i;
4846-
4847- Err = 0;
4848- GetStrVal(Str1,&Err);
4849- GetStrVal(Str2,&Err);
4850- if ((Err==0) && (GetFirstChar()!=0))
4851- Err = ErrSyntax;
4852- if (Err!=0) return Err;
4853-
4854- i = strcmp(Str1,Str2);
4855- if (i<0)
4856- i = -1;
4857- else if (i>0)
4858- i = 1;
4859- SetResult(i);
4860- return Err;
4861-}
4862-
4863-WORD TTLStrConcat()
4864-{
4865- TVarId VarId;
4866- WORD Err;
4867- TStrVal Str;
4868-
4869- Err = 0;
4870- GetStrVar(&VarId,&Err);
4871- GetStrVal(Str,&Err);
4872- if ((Err==0) && (GetFirstChar()!=0))
4873- Err = ErrSyntax;
4874- if (Err!=0) return Err;
4875-
4876- strncat_s(StrVarPtr(VarId),MaxStrLen,Str,_TRUNCATE);
4877- return Err;
4878-}
4879-
4880-WORD TTLStrCopy()
4881-{
4882- WORD Err;
4883- TVarId VarId;
4884- int From, Len, SrcLen;
4885- TStrVal Str;
4886-
4887- Err = 0;
4888- GetStrVal(Str,&Err);
4889- GetIntVal(&From,&Err);
4890- GetIntVal(&Len,&Err);
4891- GetStrVar(&VarId,&Err);
4892- if ((Err==0) && (GetFirstChar()!=0))
4893- Err = ErrSyntax;
4894- if (Err!=0) return Err;
4895-
4896- if (From<1) From = 1;
4897- SrcLen = strlen(Str)-From+1;
4898- if (Len > SrcLen) Len = SrcLen;
4899- if (Len < 0) Len = 0;
4900- memcpy(StrVarPtr(VarId),&(Str[From-1]),Len);
4901- StrVarPtr(VarId)[Len] = 0;
4902- return Err;
4903-}
4904-
4905-WORD TTLStrLen()
4906-{
4907- WORD Err;
4908- TStrVal Str;
4909-
4910- Err = 0;
4911- GetStrVal(Str,&Err);
4912- if ((Err==0) && (GetFirstChar()!=0))
4913- Err = ErrSyntax;
4914- if (Err!=0) return Err;
4915- SetResult(strlen(Str));
4916- return Err;
4917-}
4918-
4919-/*
4920- 書式: strmatch <文字列> <正規表現>
4921- <文字列>に<正規表現>がマッチするか調べるコマンド(awkのmatch関数相当)。
4922- resultには、マッチした位置をセット(マッチしない場合は0)。
4923- マッチした場合は、waitregexと同様にmatchstr,groupmatchstr1-9をセット。
4924- */
4925-WORD TTLStrMatch()
4926-{
4927- WORD Err;
4928- TStrVal Str1, Str2;
4929- int ret, result;
4930-
4931- Err = 0;
4932- GetStrVal(Str1,&Err); // target string
4933- GetStrVal(Str2,&Err); // regex pattern
4934- if ((Err==0) && (GetFirstChar()!=0))
4935- Err = ErrSyntax;
4936- if (Err!=0) return Err;
4937-
4938- ret = FindRegexStringOne(Str2, strlen(Str2), Str1, strlen(Str1));
4939- if (ret > 0) { // matched
4940- result = ret;
4941- } else {
4942- result = 0;
4943- }
4944-
4945- // FindRegexStringOneの中でUnlockVar()されてしまうので、LockVar()しなおす。
4946- LockVar();
4947-
4948- SetResult(result);
4949-
4950- return Err;
4951-}
4952-
4953-WORD TTLStrScan()
4954-{
4955- WORD Err;
4956- TStrVal Str1, Str2;
4957- char *p;
4958-
4959- Err = 0;
4960- GetStrVal(Str1,&Err);
4961- GetStrVal(Str2,&Err);
4962- if ((Err==0) && (GetFirstChar()!=0))
4963- Err = ErrSyntax;
4964- if (Err!=0) return Err;
4965-
4966- if ((Str1[0] == 0) || (Str2[0] == 0)) {
4967- SetResult(0);
4968- return Err;
4969- }
4970-
4971- if ((p = _mbsstr(Str1, Str2)) != NULL) {
4972- SetResult(p - Str1 + 1);
4973- }
4974- else {
4975- SetResult(0);
4976- }
4977- return Err;
4978-}
4979-
4980-static void insert_string(char *str, int index, char *addstr)
4981-{
4982- char *np;
4983- int srclen;
4984- int addlen;
4985-
4986- srclen = strlen(str);
4987- addlen = strlen(addstr);
4988-
4989- // まずは挿入される箇所以降のデータを、後ろに移動する。
4990- np = str + (index - 1);
4991- memmove_s(np + addlen, MaxStrLen, np, srclen - (index - 1));
4992-
4993- // 文字列を挿入する
4994- memcpy(np, addstr, addlen);
4995-
4996- // null-terminate
4997- str[srclen + addlen] = '\0';
4998-}
4999-
5000-WORD TTLStrInsert()
5001-{
5002- WORD Err;
5003- TVarId VarId;
5004- int Index;
5005- TStrVal Str;
5006- int srclen, addlen;
5007- char *srcptr;
5008-
5009- Err = 0;
5010- GetStrVar(&VarId,&Err);
5011- GetIntVal(&Index,&Err);
5012- GetStrVal(Str,&Err);
5013- if ((Err==0) && (GetFirstChar()!=0))
5014- Err = ErrSyntax;
5015- if (Err!=0) return Err;
5016-
5017- srcptr = StrVarPtr(VarId);
5018- srclen = strlen(srcptr);
5019- if (Index <= 0 || Index > srclen+1) {
5020- Err = ErrSyntax;
5021- }
5022- addlen = strlen(Str);
5023- if (srclen + addlen + 1 > MaxStrLen) {
5024- Err = ErrSyntax;
5025- }
5026- if (Err!=0) return Err;
5027-
5028- insert_string(srcptr, Index, Str);
5029-
5030- return Err;
5031-}
5032-
5033-// 文字列 str の index 文字目(1オリジン)から len 文字削除する
5034-static void remove_string(char *str, int index, int len)
5035-{
5036- char *np;
5037- int srclen, copylen;
5038-
5039- srclen = strlen(str);
5040-
5041- if (len <=0 || index <= 0 || (index-1 + len) > srclen) {
5042- return;
5043- }
5044-
5045- /*
5046- remove_string(str, 6, 4);
5047-
5048- <------------>srclen
5049- <-->len
5050- XXXXXX****YYY
5051- ^index(np)
5052- ^np+len
5053- <-->srclen - len - index
5054- ↓
5055- XXXXXXYYY
5056- */
5057-
5058- np = str + (index - 1);
5059- copylen = srclen - len - (index - 1);
5060- if (copylen > 0)
5061- memmove_s(np, MaxStrLen, np + len, copylen);
5062-
5063- // null-terminate
5064- str[srclen - len] = '\0';
5065-}
5066-
5067-WORD TTLStrRemove()
5068-{
5069- WORD Err;
5070- TVarId VarId;
5071- int Index, Len;
5072- int srclen;
5073- char *srcptr;
5074-
5075- Err = 0;
5076- GetStrVar(&VarId,&Err);
5077- GetIntVal(&Index,&Err);
5078- GetIntVal(&Len,&Err);
5079- if ((Err==0) && (GetFirstChar()!=0))
5080- Err = ErrSyntax;
5081- if (Err!=0) return Err;
5082-
5083- srcptr = StrVarPtr(VarId);
5084- srclen = strlen(srcptr);
5085- if (Len <=0 || Index <= 0 || (Index-1 + Len) > srclen) {
5086- Err = ErrSyntax;
5087- }
5088- if (Err!=0) return Err;
5089-
5090- remove_string(srcptr, Index, Len);
5091-
5092- return Err;
5093-}
5094-
5095-WORD TTLStrReplace()
5096-{
5097- WORD Err, VarType;
5098- TVarId VarId;
5099- TStrVal oldstr;
5100- TStrVal newstr;
5101- TStrVal tmpstr;
5102- char *srcptr, *matchptr;
5103- char *p;
5104- int srclen, oldlen, matchlen;
5105- int pos, ret;
5106- int result = 0;
5107-
5108- Err = 0;
5109- GetStrVar(&VarId,&Err);
5110- GetIntVal(&pos,&Err);
5111- GetStrVal(oldstr,&Err);
5112- GetStrVal(newstr,&Err);
5113- if ((Err==0) && (GetFirstChar()!=0))
5114- Err = ErrSyntax;
5115- if (Err!=0) return Err;
5116-
5117- srcptr = StrVarPtr(VarId);
5118- srclen = strlen(srcptr);
5119-
5120- if (pos > srclen || pos <= 0) {
5121- result = 0;
5122- goto error;
5123- }
5124- pos--;
5125-
5126- strncpy_s(tmpstr, MaxStrLen, srcptr, _TRUNCATE);
5127-
5128- oldlen = strlen(oldstr);
5129-
5130- // strptr文字列の pos 文字目以降において、oldstr を探す。
5131- p = tmpstr + pos;
5132- ret = FindRegexStringOne(oldstr, oldlen, p, strlen(p));
5133- // FindRegexStringOneの中でUnlockVar()されてしまうので、LockVar()しなおす。
5134- LockVar();
5135- if (ret == 0) {
5136- // 見つからなかった場合は、"0"で戻る。
5137- result = 0;
5138- goto error;
5139- }
5140- else if (ret < 0) {
5141- // 正しくない正規表現等でエラーの場合は -1 を返す
5142- result = -1;
5143- goto error;
5144- }
5145- ret--;
5146-
5147- if (CheckVar("matchstr",&VarType,&VarId) &&
5148- (VarType==TypString)) {
5149- matchptr = StrVarPtr(VarId);
5150- matchlen = strlen(matchptr);
5151- } else {
5152- result = 0;
5153- goto error;
5154- }
5155-
5156- strncpy_s(srcptr, MaxStrLen, tmpstr, pos + ret);
5157- strncat_s(srcptr, MaxStrLen, newstr, _TRUNCATE);
5158- strncat_s(srcptr, MaxStrLen, tmpstr + pos + ret + matchlen, _TRUNCATE);
5159-
5160- result = 1;
5161-
5162-error:
5163- SetResult(result);
5164- return Err;
5165-}
5166-
5167-WORD TTLStrSpecial()
5168-{
5169- WORD Err;
5170- TVarId VarId;
5171- TStrVal srcstr;
5172-
5173- Err = 0;
5174- GetStrVar(&VarId,&Err);
5175- if (Err!=0) return Err;
5176-
5177- if (CheckParameterGiven()) { // strspecial strvar strval
5178- GetStrVal(srcstr,&Err);
5179- if ((Err==0) && (GetFirstChar()!=0))
5180- Err = ErrSyntax;
5181- if (Err!=0) {
5182- return Err;
5183- }
5184-
5185- RestoreNewLine(srcstr);
5186- SetStrVal(VarId, srcstr);
5187- }
5188- else { // strspecial strvar
5189- RestoreNewLine(StrVarPtr(VarId));
5190- }
5191-
5192- return Err;
5193-}
5194-
5195-WORD TTLStrTrim()
5196-{
5197- TStrVal trimchars;
5198- WORD Err;
5199- TVarId VarId;
5200- int srclen;
5201- int i, start, end;
5202- char *srcptr, *p;
5203- char table[256];
5204-
5205- Err = 0;
5206- GetStrVar(&VarId,&Err);
5207- GetStrVal(trimchars,&Err);
5208-
5209- if ((Err==0) && (GetFirstChar()!=0))
5210- Err = ErrSyntax;
5211- if (Err!=0) return Err;
5212-
5213- srcptr = StrVarPtr(VarId);
5214- srclen = strlen(srcptr);
5215-
5216- // 削除する文字のテーブルを作る。
5217- memset(table, 0, sizeof(table));
5218- for (p = trimchars; *p ; p++) {
5219- table[*p] = 1;
5220- }
5221-
5222- // 文字列の先頭から検索する
5223- for (i = 0 ; i < srclen ; i++) {
5224- if (table[srcptr[i]] == 0)
5225- break;
5226- }
5227- // 削除されない有効な文字列の始まり。
5228- // すべて削除対象となる場合は、start == srclen 。
5229- start = i;
5230-
5231- // 文字列の末尾から検索する
5232- for (i = srclen - 1 ; i >= 0 ; i--) {
5233- if (table[srcptr[i]] == 0)
5234- break;
5235- }
5236- // 削除されない有効な文字列の終わり。
5237- // すべて削除対象となる場合は、end == -1 。
5238- end = i;
5239-
5240- // 末尾を削る
5241- srcptr[end + 1] = '\0';
5242-
5243- // 次に、先頭から削る。
5244- remove_string(srcptr, 1, start);
5245-
5246- return Err;
5247-}
5248-
5249-WORD TTLStrSplit()
5250-{
5251-#define MAXVARNUM 9
5252- TStrVal src, delimchars, buf;
5253- WORD Err;
5254- int maxvar;
5255- int srclen, len;
5256- int i, count;
5257- BOOL ary = FALSE, omit = FALSE;
5258- char *p;
5259- char /* *last, */ *tok[MAXVARNUM];
5260-
5261- Err = 0;
5262- GetStrVal(src,&Err);
5263- GetStrVal(delimchars,&Err);
5264- // 3rd arg (optional)
5265- if (CheckParameterGiven()) {
5266- GetIntVal(&maxvar,&Err);
5267- if (Err!=0) {
5268- // TODO array
5269-#if 0
5270- Err = 0;
5271- // Parameter から array を受け取る
5272- if (Err==0) {
5273- ary = TRUE;
5274- }
5275-#endif
5276- }
5277- }
5278- else {
5279- maxvar = 9;
5280- omit = TRUE;
5281- }
5282-
5283- if ((Err==0) && (GetFirstChar()!=0))
5284- Err = ErrSyntax;
5285- if (Err!=0) return Err;
5286-
5287- if (!ary && (maxvar < 1 || maxvar > MAXVARNUM) )
5288- return ErrSyntax;
5289-
5290- // デリミタは1文字のみとする。
5291- len = strlen(delimchars);
5292- if (len != 1)
5293- return ErrSyntax;
5294-
5295- srclen = strlen(src);
5296- strcpy_s(buf, MaxStrLen, src); /* 破壊されてもいいように、コピーバッファを使う。*/
5297-
5298-#if 0
5299- // トークンの切り出しを行う。
5300- memset(tok, 0, sizeof(tok));
5301-#if 0
5302- tok[0] = strtok_s(srcptr, delimchars, &last);
5303- for (i = 1 ; i < MAXVARNUM ; i++) {
5304- tok[i] = strtok_s(NULL, delimchars, &last);
5305- if (tok[i] == NULL)
5306- break;
5307- }
5308-#else
5309- /* strtokを使うと、連続した区切りが1つに丸められるため、自前でポインタを
5310- * たどる。ただし、区切り文字は1つのみとする。
5311- */
5312- i = 0;
5313- for (p = buf; *p == delimchars[0] ; p++) {
5314- tok[i++] = NULL;
5315- if (i >= maxvar)
5316- goto end;
5317- }
5318-
5319- for (p = strtok_s(p, delimchars, &last); p != NULL ; p = strtok_s(NULL, delimchars, &last) ) {
5320- tok[i++] = p;
5321- if (i >= maxvar)
5322- goto end;
5323- for (p += strlen(p) + 1 ; *p == delimchars[0] ; p++) {
5324- tok[i++] = NULL;
5325- if (i >= maxvar)
5326- goto end;
5327- }
5328- }
5329-#endif
5330-#else
5331- if (ary) {
5332- // TODO array
5333- }
5334- else {
5335- p = buf;
5336- count = 1;
5337- tok[count-1] = p;
5338- for (i=0; i < srclen && count < maxvar + omit; i++) { // count 省略時には、超過分を捨てるため 1 つ余分に進める
5339- if (*p == *delimchars) {
5340- *p = '\0';
5341- count++;
5342- if (count <= MAXVARNUM) { // tok の要素数を超えて代入しないようにする(count 省略時のため)
5343- tok[count-1] = p+1;
5344- }
5345- }
5346- p++;
5347- }
5348- }
5349-#endif
5350-
5351-//end:
5352- // 結果の格納
5353- for (i = 1 ; i <= count ; i++) {
5354- SetGroupMatchStr(i, tok[i-1]);
5355- }
5356- for (i = count+1 ; i <= MAXVARNUM ; i++) {
5357- SetGroupMatchStr(i, "");
5358- }
5359- SetResult(count);
5360- return Err;
5361-#undef MAXVARNUM
5362-}
5363-
5364-WORD TTLStrJoin()
5365-{
5366-#define MAXVARNUM 9
5367- TStrVal delimchars, buf;
5368- WORD Err;
5369- TVarId VarId;
5370- WORD VarType;
5371- int maxvar;
5372- int srclen;
5373- int i;
5374- BOOL ary = FALSE;
5375- char *srcptr, *p;
5376-
5377- Err = 0;
5378- GetStrVar(&VarId,&Err);
5379- GetStrVal(delimchars,&Err);
5380- // 3rd arg (optional)
5381- if (CheckParameterGiven()) {
5382- GetIntVal(&maxvar,&Err);
5383- if (Err!=0) {
5384- // TODO array
5385-#if 0
5386- Err = 0;
5387- // Parameter から array を受け取る
5388- if (Err==0) {
5389- ary = TRUE;
5390- }
5391-#endif
5392- }
5393- }
5394- else {
5395- maxvar = 9;
5396- }
5397-
5398- if ((Err==0) && (GetFirstChar()!=0))
5399- Err = ErrSyntax;
5400- if (Err!=0) return Err;
5401-
5402- if (!ary && (maxvar < 1 || maxvar > MAXVARNUM) )
5403- return ErrSyntax;
5404-
5405- srcptr = StrVarPtr(VarId);
5406- srclen = strlen(srcptr);
5407-
5408- srcptr[0] = '\0';
5409- if (ary) {
5410- // TODO array
5411- }
5412- else {
5413- for (i = 0 ; i < maxvar ; i++) {
5414- _snprintf_s(buf, sizeof(buf), _TRUNCATE, "groupmatchstr%d", i + 1);
5415- if (CheckVar(buf,&VarType,&VarId)) {
5416- if (VarType!=TypString)
5417- return ErrSyntax;
5418- p = StrVarPtr(VarId);
5419- strncat_s(srcptr, MaxStrLen, p, _TRUNCATE);
5420- if (i < maxvar-1) {
5421- strncat_s(srcptr, MaxStrLen, delimchars, _TRUNCATE);
5422- }
5423- }
5424- }
5425- }
5426-
5427- return Err;
5428-#undef MAXVARNUM
5429-}
5430-
5431-WORD TTLTestLink()
5432-{
5433- if (GetFirstChar()!=0)
5434- return ErrSyntax;
5435-
5436- if (! Linked)
5437- SetResult(0);
5438- else if (ComReady==0)
5439- SetResult(1);
5440- else
5441- SetResult(2);
5442-
5443- return 0;
5444-}
5445-
5446-// added (2007.7.12 maya)
5447-WORD TTLToLower()
5448-{
5449- WORD Err;
5450- TVarId VarId;
5451- TStrVal Str;
5452- int i=0;
5453-
5454- Err = 0;
5455- GetStrVar(&VarId,&Err);
5456- GetStrVal(Str,&Err);
5457- if ((Err==0) && (GetFirstChar()!=0))
5458- Err = ErrSyntax;
5459- if (Err!=0) return Err;
5460-
5461- while (Str[i] != 0) {
5462- if(_ismbblead(Str[i])) {
5463- i = i + 2;
5464- continue;
5465- }
5466- if (Str[i] >= 'A' && Str[i] <= 'Z') {
5467- Str[i] = Str[i] + 0x20;
5468- }
5469- i++;
5470- }
5471-
5472- strncpy_s(StrVarPtr(VarId), MaxStrLen, Str, _TRUNCATE);
5473- return Err;
5474-}
5475-
5476-// added (2007.7.12 maya)
5477-WORD TTLToUpper()
5478-{
5479- WORD Err;
5480- TVarId VarId;
5481- TStrVal Str;
5482- int i=0;
5483-
5484- Err = 0;
5485- GetStrVar(&VarId,&Err);
5486- GetStrVal(Str,&Err);
5487- if ((Err==0) && (GetFirstChar()!=0))
5488- Err = ErrSyntax;
5489- if (Err!=0) return Err;
5490-
5491- while (Str[i] != 0) {
5492- if(_ismbblead(Str[i])) {
5493- i = i + 2;
5494- continue;
5495- }
5496- if (Str[i] >= 'a' && Str[i] <= 'z') {
5497- Str[i] = Str[i] - 0x20;
5498- }
5499- i++;
5500- }
5501-
5502- strncpy_s(StrVarPtr(VarId), MaxStrLen, Str, _TRUNCATE);
5503- return Err;
5504-}
5505-
5506-WORD TTLUnlink()
5507-{
5508- if (GetFirstChar()!=0)
5509- return ErrSyntax;
5510-
5511- if (Linked)
5512- {
5513- EndDDE();
5514- }
5515- return 0;
5516-}
5517-
5518-
5519-WORD TTLUptime()
5520-{
5521- WORD Err;
5522- TVarId VarId;
5523- DWORD tick;
5524-
5525- Err = 0;
5526- GetIntVar(&VarId,&Err);
5527- if ((Err==0) && (GetFirstChar()!=0))
5528- Err = ErrSyntax;
5529- if (Err!=0) return Err;
5530-
5531- // Windows OSが起動してからの経過時間(ミリ秒)を取得する。ただし、49日を経過すると、0に戻る。
5532- // GetTickCount64() API(Vista以降)を使うと、オーバーフローしなくなるが、そもそもTera Termでは
5533- // 64bit変数をサポートしていないので、意味がない。
5534- tick = GetTickCount();
5535-
5536- SetIntVal(VarId, tick);
5537-
5538- return Err;
5539-}
5540-
5541-
5542-
5543-WORD TTLWait(BOOL Ln)
5544-{
5545- TStrVal Str;
5546- WORD Err, ValType;
5547- TVarId VarId;
5548- int i, Val;
5549- int TimeOut;
5550-
5551- ClearWait();
5552-
5553- for (i=0; i<10; i++) {
5554- Err = 0;
5555- if (GetString(Str, &Err)) {
5556- SetWait(i+1, Str);
5557- }
5558- else if (GetExpression(&ValType, &Val, &Err) && Err == 0) {
5559- if (ValType == TypString)
5560- SetWait(i+1, StrVarPtr((TVarId)Val));
5561- else
5562- Err = ErrTypeMismatch;
5563- }
5564- else
5565- break;
5566-
5567- if (Err)
5568- break;
5569- }
5570-
5571- if ((Err==0) && (GetFirstChar()!=0))
5572- Err = ErrSyntax;
5573-
5574- if (! Linked)
5575- Err = ErrLinkFirst;
5576-
5577- if ((Err==0) && (i>0))
5578- {
5579- if (Ln)
5580- TTLStatus = IdTTLWaitLn;
5581- else
5582- TTLStatus = IdTTLWait;
5583- TimeOut = 0;
5584- if (CheckVar("timeout",&ValType,&VarId) && (ValType==TypInteger)) {
5585- TimeOut = CopyIntVal(VarId) * 1000;
5586- }
5587- if (CheckVar("mtimeout",&ValType,&VarId) && (ValType==TypInteger)) {
5588- TimeOut += CopyIntVal(VarId);
5589- }
5590-
5591- if (TimeOut>0)
5592- {
5593- TimeLimit = (DWORD)TimeOut;
5594- TimeStart = GetTickCount();
5595- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
5596- }
5597- }
5598- else
5599- ClearWait();
5600-
5601- return Err;
5602-}
5603-
5604-
5605-WORD TTLWait4all(BOOL Ln)
5606-{
5607- WORD Err = 0;
5608-
5609- Err = TTLWait(Ln);
5610- TTLStatus = IdTTLWait4all;
5611-
5612- Wait4allGotIndex = FALSE;
5613- Wait4allFoundNum = 0;
5614-
5615- return Err;
5616-}
5617-
5618-
5619-// 'waitregex'(wait regular expression): wait command with regular expression
5620-//
5621-// This command has almost same function of 'wait' command. Additionally 'waitregex' can search
5622-// the keyword with regular expression. Tera Term uses a regex library that is called 'Oniguruma'.
5623-// cf. http://www.geocities.jp/kosako3/oniguruma/
5624-//
5625-// (2005.10.5 yutaka)
5626-WORD TTLWaitRegex(BOOL Ln)
5627-{
5628- WORD ret;
5629-
5630- ret = TTLWait(Ln);
5631-
5632- RegexActionType = REGEX_WAIT; // regex enabled
5633-
5634- return (ret);
5635-}
5636-
5637-
5638-
5639-WORD TTLWaitEvent()
5640-{
5641- WORD Err, ValType;
5642- TVarId VarId;
5643- int TimeOut;
5644-
5645- Err = 0;
5646- GetIntVal(&WakeupCondition,&Err);
5647- if ((Err==0) && (GetFirstChar()!=0))
5648- Err = ErrSyntax;
5649- if (Err!=0) return Err;
5650-
5651- WakeupCondition &= 15;
5652- TimeOut = 0;
5653- if (CheckVar("timeout",&ValType,&VarId) && (ValType==TypInteger)) {
5654- TimeOut = CopyIntVal(VarId) * 1000;
5655- }
5656- if (CheckVar("mtimeout",&ValType,&VarId) && (ValType==TypInteger)) {
5657- TimeOut += CopyIntVal(VarId);
5658- }
5659-
5660- if (TimeOut>0)
5661- {
5662- TimeLimit = (DWORD)TimeOut;
5663- TimeStart = GetTickCount();
5664- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
5665- }
5666-
5667- TTLStatus = IdTTLSleep;
5668- return Err;
5669-}
5670-
5671-
5672-WORD TTLWaitN()
5673-{
5674- WORD Err, ValType;
5675- TVarId VarId;
5676- int TimeOut, WaitBytes;
5677-
5678- ClearWaitN();
5679-
5680- Err = 0;
5681- GetIntVal(&WaitBytes,&Err);
5682- if ((Err==0) && (GetFirstChar()!=0))
5683- Err = ErrSyntax;
5684- if (Err!=0) return Err;
5685-
5686- SetWaitN(WaitBytes);
5687-
5688- if (! Linked)
5689- Err = ErrLinkFirst;
5690- if (Err!=0) return Err;
5691-
5692- TTLStatus = IdTTLWaitN;
5693- TimeOut = 0;
5694- if (CheckVar("timeout",&ValType,&VarId) && (ValType==TypInteger)) {
5695- TimeOut = CopyIntVal(VarId) * 1000;
5696- }
5697- if (CheckVar("mtimeout",&ValType,&VarId) && (ValType==TypInteger)) {
5698- TimeOut += CopyIntVal(VarId);
5699- }
5700-
5701- if (TimeOut>0)
5702- {
5703- TimeLimit = (DWORD)TimeOut;
5704- TimeStart = GetTickCount();
5705- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
5706- }
5707-
5708- return Err;
5709-}
5710-
5711-
5712-WORD TTLWaitRecv()
5713-{
5714- TStrVal Str;
5715- WORD Err;
5716- int Pos, Len, TimeOut;
5717- WORD VarType;
5718- TVarId VarId;
5719-
5720- Err = 0;
5721- GetStrVal(Str,&Err);
5722- GetIntVal(&Len,&Err);
5723- GetIntVal(&Pos,&Err);
5724- if ((Err==0) && (GetFirstChar()!=0))
5725- Err = ErrSyntax;
5726- if ((Err==0) && (! Linked))
5727- Err = ErrLinkFirst;
5728- if (Err!=0) return Err;
5729- SetInputStr("");
5730- SetWait2(Str,Len,Pos);
5731-
5732- TTLStatus = IdTTLWait2;
5733- TimeOut = 0;
5734- if (CheckVar("timeout",&VarType,&VarId) && (VarType==TypInteger)) {
5735- TimeOut = CopyIntVal(VarId) * 1000;
5736- }
5737- if (CheckVar("mtimeout",&VarType,&VarId) && (VarType==TypInteger)) {
5738- TimeOut += CopyIntVal(VarId);
5739- }
5740-
5741- if (TimeOut>0)
5742- {
5743- TimeLimit = (DWORD)TimeOut;
5744- TimeStart = GetTickCount();
5745- SetTimer(HMainWin, IdTimeOutTimer, TIMEOUT_TIMER_MS, NULL);
5746- }
5747- return Err;
5748-}
5749-
5750-WORD TTLWhile(BOOL mode)
5751-{
5752- WORD Err;
5753- int Val = mode;
5754-
5755- Err = 0;
5756- if (CheckParameterGiven()) {
5757- GetIntVal(&Val,&Err);
5758- }
5759-
5760- if ((Err==0) && (GetFirstChar()!=0))
5761- Err = ErrSyntax;
5762- if (Err!=0) return Err;
5763-
5764- if ((Val!=0) == mode)
5765- return SetWhileLoop();
5766- else
5767- EndWhileLoop();
5768- return Err;
5769-}
5770-
5771-WORD TTLXmodemRecv()
5772-{
5773- TStrVal Str;
5774- WORD Err;
5775- int BinFlag, XOption;
5776-
5777- Err = 0;
5778- GetStrVal(Str,&Err);
5779- GetIntVal(&BinFlag,&Err);
5780- GetIntVal(&XOption,&Err);
5781- if ((Err==0) &&
5782- ((strlen(Str)==0) || (GetFirstChar()!=0)))
5783- Err = ErrSyntax;
5784-
5785- if (Err!=0) return Err;
5786-
5787- switch (XOption) {
5788- case XoptCRC:
5789- // NOP
5790- break;
5791- case Xopt1kCRC:
5792- // for compatibility
5793- XOption = XoptCRC;
5794- break;
5795- default:
5796- XOption = XoptCheck;
5797- }
5798-
5799- SetFile(Str);
5800- SetBinary(BinFlag);
5801- SetXOption(XOption);
5802- return SendCmnd(CmdXmodemRecv,IdTTLWaitCmndResult);
5803-}
5804-
5805-WORD TTLXmodemSend()
5806-{
5807- TStrVal Str;
5808- WORD Err;
5809- int XOption;
5810-
5811- Err = 0;
5812- GetStrVal(Str,&Err);
5813- GetIntVal(&XOption,&Err);
5814- if ((Err==0) &&
5815- ((strlen(Str)==0) || (GetFirstChar()!=0)))
5816- Err = ErrSyntax;
5817- if (Err!=0) return Err;
5818-
5819- switch (XOption) {
5820- case Xopt1kCRC:
5821- // NOP
5822- break;
5823- default:
5824- XOption = XoptCRC;
5825- }
5826-
5827- SetFile(Str);
5828- SetXOption(XOption);
5829- return SendCmnd(CmdXmodemSend,IdTTLWaitCmndResult);
5830-}
5831-
5832-WORD TTLYesNoBox()
5833-{
5834- WORD Err;
5835- int YesNo;
5836-
5837- YesNo = MessageCommand(IdYesNoBox, &Err);
5838- if (Err!=0) return Err;
5839- if (YesNo==IDOK)
5840- YesNo = 1; // Yes
5841- else
5842- YesNo = 0; // No
5843- SetResult(YesNo);
5844- return Err;
5845-}
5846-
5847-WORD TTLZmodemSend()
5848-{
5849- TStrVal Str;
5850- WORD Err;
5851- int BinFlag;
5852-
5853- Err = 0;
5854- GetStrVal(Str,&Err);
5855- GetIntVal(&BinFlag,&Err);
5856- if ((Err==0) &&
5857- ((strlen(Str)==0) || (GetFirstChar()!=0)))
5858- Err = ErrSyntax;
5859- if (Err!=0) return Err;
5860-
5861- SetFile(Str);
5862- SetBinary(BinFlag);
5863- return SendCmnd(CmdZmodemSend,IdTTLWaitCmndResult);
5864-}
5865-
5866-WORD TTLYmodemSend()
5867-{
5868- TStrVal Str;
5869- WORD Err;
5870-// int BinFlag;
5871-
5872- Err = 0;
5873- GetStrVal(Str,&Err);
5874-// GetIntVal(&BinFlag,&Err);
5875- if ((Err==0) &&
5876- ((strlen(Str)==0) || (GetFirstChar()!=0)))
5877- Err = ErrSyntax;
5878- if (Err!=0) return Err;
5879-
5880- SetFile(Str);
5881-// SetBinary(BinFlag);
5882- return SendCmnd(CmdYmodemSend,IdTTLWaitCmndResult);
5883-}
5884-
5885-// SYNOPSIS:
5886-// scpsend "c:\usr\sample.chm" "doc/sample.chm"
5887-// scpsend "c:\usr\sample.chm"
5888-WORD TTLScpSend()
5889-{
5890- TStrVal Str;
5891- TStrVal Str2;
5892- WORD Err;
5893-
5894- Err = 0;
5895- GetStrVal(Str,&Err);
5896-
5897- if ((Err==0) &&
5898- ((strlen(Str)==0)))
5899- Err = ErrSyntax;
5900- if (Err!=0) return Err;
5901-
5902- GetStrVal(Str2,&Err);
5903- if (Err) {
5904- Str2[0] = '\0';
5905- Err = 0;
5906- }
5907-
5908- if (GetFirstChar() != 0)
5909- Err = ErrSyntax;
5910- if (Err!=0) return Err;
5911-
5912- SetFile(Str);
5913- SetSecondFile(Str2);
5914- return SendCmnd(CmdScpSend, 0);
5915-}
5916-
5917-// SYNOPSIS:
5918-// scprecv "foo.txt"
5919-// scprecv "src/foo.txt" "c:\foo.txt"
5920-WORD TTLScpRecv()
5921-{
5922- TStrVal Str;
5923- TStrVal Str2;
5924- WORD Err;
5925-
5926- Err = 0;
5927- GetStrVal(Str,&Err);
5928-
5929- if ((Err==0) &&
5930- ((strlen(Str)==0)))
5931- Err = ErrSyntax;
5932- if (Err!=0) return Err;
5933-
5934- GetStrVal(Str2,&Err);
5935- if (Err) {
5936- Str2[0] = '\0';
5937- Err = 0;
5938- }
5939-
5940- if (GetFirstChar() != 0)
5941- Err = ErrSyntax;
5942- if (Err!=0) return Err;
5943-
5944- SetFile(Str);
5945- SetSecondFile(Str2);
5946- return SendCmnd(CmdScpRcv, 0);
5947-}
5948-
5949-int ExecCmnd()
5950-{
5951- WORD WId, Err;
5952- BOOL StrConst, E, WithIndex, Result;
5953- TStrVal Str;
5954- TName Cmnd;
5955- WORD ValType, VarType;
5956- TVarId VarId;
5957- int Val, Index;
5958-
5959- Err = 0;
5960-
5961- Result = GetReservedWord(&WId);
5962-
5963- if (EndWhileFlag>0) {
5964- if (Result) {
5965- switch (WId) {
5966- case RsvWhile:
5967- case RsvUntil:
5968- case RsvDo:
5969- EndWhileFlag++; break;
5970-
5971- case RsvEndWhile:
5972- case RsvEndUntil:
5973- case RsvLoop:
5974- EndWhileFlag--; break;
5975- }
5976- }
5977- return 0;
5978- }
5979-
5980- if (BreakFlag>0) {
5981- if (Result) {
5982- switch (WId) {
5983- case RsvIf:
5984- if (CheckThen(&Err))
5985- IfNest++;
5986- break;
5987-
5988- case RsvEndIf:
5989- if (IfNest<1)
5990- Err = ErrInvalidCtl;
5991- else
5992- IfNest--;
5993- break;
5994-
5995- case RsvFor:
5996- case RsvWhile:
5997- case RsvUntil:
5998- case RsvDo:
5999- BreakFlag++; break;
6000-
6001- case RsvNext:
6002- case RsvEndWhile:
6003- case RsvEndUntil:
6004- case RsvLoop:
6005- BreakFlag--; break;
6006- }
6007- }
6008- if (BreakFlag>0 || !ContinueFlag)
6009- return Err;
6010- ContinueFlag = FALSE;
6011- }
6012-
6013- if (EndIfFlag>0) {
6014- if (! Result)
6015- ;
6016- else if ((WId==RsvIf) && CheckThen(&Err))
6017- EndIfFlag++;
6018- else if (WId==RsvEndIf)
6019- EndIfFlag--;
6020- return Err;
6021- }
6022-
6023- if (ElseFlag>0) {
6024- if (! Result)
6025- ;
6026- else if ((WId==RsvIf) && CheckThen(&Err))
6027- EndIfFlag++;
6028- else if (WId==RsvElse)
6029- ElseFlag--;
6030- else if (WId==RsvElseIf)
6031- {
6032- if (CheckElseIf(&Err)!=0)
6033- ElseFlag--;
6034- }
6035- else if (WId==RsvEndIf)
6036- {
6037- ElseFlag--;
6038- if (ElseFlag==0)
6039- IfNest--;
6040- }
6041- return Err;
6042- }
6043-
6044- if (Result)
6045- switch (WId) {
6046- case RsvBasename:
6047- Err = TTLBasename(); break;
6048- case RsvBeep:
6049- Err = TTLBeep(); break;
6050- case RsvBPlusRecv:
6051- Err = TTLCommCmd(CmdBPlusRecv,IdTTLWaitCmndResult); break;
6052- case RsvBPlusSend:
6053- Err = TTLCommCmdFile(CmdBPlusSend,IdTTLWaitCmndResult); break;
6054- case RsvBreak:
6055- case RsvContinue:
6056- Err = TTLBreak(WId); break;
6057- case RsvBringupBox:
6058- Err = TTLBringupBox(); break;
6059- case RsvCall:
6060- Err = TTLCall(); break;
6061- case RsvCallMenu:
6062- Err = TTLCommCmdInt(CmdCallMenu,0); break;
6063- case RsvChangeDir:
6064- Err = TTLCommCmdFile(CmdChangeDir,0); break;
6065- case RsvChecksum8:
6066- Err = TTLDoChecksum(CHECKSUM8); break;
6067- case RsvChecksum8File:
6068- Err = TTLDoChecksumFile(CHECKSUM8); break;
6069- case RsvChecksum16:
6070- Err = TTLDoChecksum(CHECKSUM16); break;
6071- case RsvChecksum16File:
6072- Err = TTLDoChecksumFile(CHECKSUM16); break;
6073- case RsvChecksum32:
6074- Err = TTLDoChecksum(CHECKSUM32); break;
6075- case RsvChecksum32File:
6076- Err = TTLDoChecksumFile(CHECKSUM32); break;
6077- case RsvClearScreen:
6078- Err = TTLCommCmdInt(CmdClearScreen,0); break;
6079- case RsvClipb2Var:
6080- Err = TTLClipb2Var(); break; // add 'clipb2var' (2006.9.17 maya)
6081- case RsvCloseSBox:
6082- Err = TTLCloseSBox(); break;
6083- case RsvCloseTT:
6084- Err = TTLCloseTT(); break;
6085- case RsvCode2Str:
6086- Err = TTLCode2Str(); break;
6087- case RsvConnect:
6088- case RsvCygConnect:
6089- Err = TTLConnect(WId); break;
6090- case RsvCrc16:
6091- Err = TTLDoChecksum(CRC16); break;
6092- case RsvCrc16File:
6093- Err = TTLDoChecksumFile(CRC16); break;
6094- case RsvCrc32:
6095- Err = TTLDoChecksum(CRC32); break;
6096- case RsvCrc32File:
6097- Err = TTLDoChecksumFile(CRC32); break;
6098- case RsvDelPassword:
6099- Err = TTLDelPassword(); break;
6100- case RsvDirname:
6101- Err = TTLDirname(); break;
6102- case RsvDirnameBox:
6103- Err = TTLDirnameBox(); break;
6104- case RsvDisconnect:
6105- Err = TTLDisconnect(); break;
6106- case RsvDispStr:
6107- Err = TTLDispStr(); break;
6108- case RsvDo:
6109- Err = TTLDo(); break;
6110- case RsvElse:
6111- Err = TTLElse(); break;
6112- case RsvElseIf:
6113- Err = TTLElseIf(); break;
6114- case RsvEnableKeyb:
6115- Err = TTLCommCmdBin(CmdEnableKeyb,0); break;
6116- case RsvEnd:
6117- Err = TTLEnd(); break;
6118- case RsvEndIf:
6119- Err = TTLEndIf(); break;
6120- case RsvEndUntil:
6121- Err = TTLEndWhile(FALSE); break;
6122- case RsvEndWhile:
6123- Err = TTLEndWhile(TRUE); break;
6124- case RsvExec:
6125- Err = TTLExec(); break;
6126- case RsvExecCmnd:
6127- Err = TTLExecCmnd(); break;
6128- case RsvExit:
6129- Err = TTLExit(); break;
6130- case RsvExpandEnv:
6131- Err = TTLExpandEnv(); break;
6132- case RsvFileClose:
6133- Err = TTLFileClose(); break;
6134- case RsvFileConcat:
6135- Err = TTLFileConcat(); break;
6136- case RsvFileCopy:
6137- Err = TTLFileCopy(); break;
6138- case RsvFileCreate:
6139- Err = TTLFileCreate(); break;
6140- case RsvFileDelete:
6141- Err = TTLFileDelete(); break;
6142- case RsvFileMarkPtr:
6143- Err = TTLFileMarkPtr(); break;
6144- case RsvFilenameBox:
6145- Err = TTLFilenameBox(); break; // add 'filenamebox' (2007.9.13 maya)
6146- case RsvFileOpen:
6147- Err = TTLFileOpen(); break;
6148- case RsvFileLock:
6149- Err = TTLFileLock(); break;
6150- case RsvFileUnLock:
6151- Err = TTLFileUnLock(); break;
6152- case RsvFileReadln:
6153- Err = TTLFileReadln(); break;
6154- case RsvFileRead:
6155- Err = TTLFileRead(); break; // add 'fileread'
6156- case RsvFileRename:
6157- Err = TTLFileRename(); break;
6158- case RsvFileSearch:
6159- Err = TTLFileSearch(); break;
6160- case RsvFileSeek:
6161- Err = TTLFileSeek(); break;
6162- case RsvFileSeekBack:
6163- Err = TTLFileSeekBack(); break;
6164- case RsvFileStat:
6165- Err = TTLFileStat(); break;
6166- case RsvFileStrSeek:
6167- Err = TTLFileStrSeek(); break;
6168- case RsvFileStrSeek2:
6169- Err = TTLFileStrSeek2(); break;
6170- case RsvFileTruncate:
6171- Err = TTLFileTruncate(); break;
6172- case RsvFileWrite:
6173- Err = TTLFileWrite(FALSE); break;
6174- case RsvFileWriteLn:
6175- Err = TTLFileWrite(TRUE); break;
6176- case RsvFindClose:
6177- Err = TTLFindClose(); break;
6178- case RsvFindFirst:
6179- Err = TTLFindFirst(); break;
6180- case RsvFindNext:
6181- Err = TTLFindNext(); break;
6182- case RsvFlushRecv:
6183- Err = TTLFlushRecv(); break;
6184- case RsvFolderCreate:
6185- Err = TTLFolderCreate(); break;
6186- case RsvFolderDelete:
6187- Err = TTLFolderDelete(); break;
6188- case RsvFolderSearch:
6189- Err = TTLFolderSearch(); break;
6190- case RsvFor:
6191- Err = TTLFor(); break;
6192- case RsvGetDate:
6193- case RsvGetTime:
6194- Err = TTLGetTime(WId); break;
6195- case RsvGetDir:
6196- Err = TTLGetDir(); break;
6197- case RsvGetEnv:
6198- Err = TTLGetEnv(); break;
6199- case RsvGetFileAttr:
6200- Err = TTLGetFileAttr(); break;
6201- case RsvGetHostname:
6202- Err = TTLGetHostname(); break;
6203- case RsvGetIPv4Addr:
6204- Err = TTLGetIPv4Addr(); break;
6205- case RsvGetIPv6Addr:
6206- Err = TTLGetIPv6Addr(); break;
6207- case RsvGetModemStatus:
6208- Err = TTLGetModemStatus(); break;
6209- case RsvGetPassword:
6210- Err = TTLGetPassword(); break;
6211- case RsvSetPassword:
6212- Err = TTLSetPassword(); break;
6213- case RsvIsPassword:
6214- Err = TTLIsPassword(); break;
6215- case RsvGetSpecialFolder:
6216- Err = TTLGetSpecialFolder(); break;
6217- case RsvGetTitle:
6218- Err = TTLGetTitle(); break;
6219- case RsvGetTTDir:
6220- Err = TTLGetTTDir(); break;
6221- case RsvGetVer:
6222- Err = TTLGetVer(); break;
6223- case RsvGoto:
6224- Err = TTLGoto(); break;
6225- case RsvIfDefined:
6226- Err = TTLIfDefined(); break;
6227- case RsvIf:
6228- Err = TTLIf(); break;
6229- case RsvInclude:
6230- Err = TTLInclude(); break;
6231- case RsvInputBox:
6232- Err = TTLInputBox(FALSE); break;
6233- case RsvInt2Str:
6234- Err = TTLInt2Str(); break;
6235- case RsvIntDim:
6236- case RsvStrDim:
6237- Err = TTLDim(WId); break;
6238- case RsvKmtFinish:
6239- Err = TTLCommCmd(CmdKmtFinish,IdTTLWaitCmndResult); break;
6240- case RsvKmtGet:
6241- Err = TTLCommCmdFile(CmdKmtGet,IdTTLWaitCmndResult); break;
6242- case RsvKmtRecv:
6243- Err = TTLCommCmd(CmdKmtRecv,IdTTLWaitCmndResult); break;
6244- case RsvKmtSend:
6245- Err = TTLCommCmdFile(CmdKmtSend,IdTTLWaitCmndResult); break;
6246- case RsvListBox:
6247- Err = TTLListBox(); break;
6248- case RsvLoadKeyMap:
6249- Err = TTLCommCmdFile(CmdLoadKeyMap,0); break;
6250- case RsvLogAutoClose:
6251- Err = TTLCommCmdBin(CmdLogAutoClose, 0); break;
6252- case RsvLogClose:
6253- Err = TTLCommCmd(CmdLogClose,0); break;
6254- case RsvLogInfo:
6255- Err = TTLLogInfo(); break;
6256- case RsvLogOpen:
6257- Err = TTLLogOpen(); break;
6258- case RsvLogPause:
6259- Err = TTLCommCmd(CmdLogPause,0); break;
6260- case RsvLogRotate:
6261- Err = TTLLogRotate(); break;
6262- case RsvLogStart:
6263- Err = TTLCommCmd(CmdLogStart,0); break;
6264- case RsvLogWrite:
6265- Err = TTLCommCmdFile(CmdLogWrite,0); break;
6266- case RsvLoop:
6267- Err = TTLLoop(); break;
6268- case RsvMakePath:
6269- Err = TTLMakePath(); break;
6270- case RsvMessageBox:
6271- Err = TTLMessageBox(); break;
6272- case RsvNext:
6273- Err = TTLNext(); break;
6274- case RsvPasswordBox:
6275- Err = TTLInputBox(TRUE); break;
6276- case RsvPause:
6277- Err = TTLPause(); break;
6278- case RsvMilliPause:
6279- Err = TTLMilliPause(); break; // add 'mpause'
6280- case RsvQuickVANRecv:
6281- Err = TTLCommCmd(CmdQVRecv,IdTTLWaitCmndResult); break;
6282- case RsvQuickVANSend:
6283- Err = TTLCommCmdFile(CmdQVSend,IdTTLWaitCmndResult); break;
6284- case RsvRandom:
6285- Err = TTLRandom(); break;
6286- case RsvRecvLn:
6287- Err = TTLRecvLn(); break;
6288- case RsvRegexOption:
6289- Err = TTLRegexOption(); break;
6290- case RsvRestoreSetup:
6291- Err = TTLCommCmdFile(CmdRestoreSetup,0); break;
6292- case RsvReturn:
6293- Err = TTLReturn(); break;
6294- case RsvRotateL:
6295- Err = TTLRotateLeft(); break; // add 'rotateleft' (2007.8.19 maya)
6296- case RsvRotateR:
6297- Err = TTLRotateRight(); break; // add 'rotateright' (2007.8.19 maya)
6298- case RsvScpSend:
6299- Err = TTLScpSend(); break; // add 'scpsend' (2008.1.1 yutaka)
6300- case RsvScpRecv:
6301- Err = TTLScpRecv(); break; // add 'scprecv' (2008.1.4 yutaka)
6302- case RsvSend:
6303- Err = TTLSend(); break;
6304- case RsvSendBreak:
6305- Err = TTLCommCmd(CmdSendBreak,0); break;
6306- case RsvSendBroadcast:
6307- Err = TTLSendBroadcast(FALSE); break;
6308- case RsvSendlnBroadcast:
6309- Err = TTLSendBroadcast(TRUE); break;
6310- case RsvSendlnMulticast:
6311- Err = TTLSendMulticast(TRUE); break;
6312- case RsvSendMulticast:
6313- Err = TTLSendMulticast(FALSE); break;
6314- case RsvSetMulticastName:
6315- Err = TTLSetMulticastName(); break;
6316- case RsvSendFile:
6317- Err = TTLSendFile(); break;
6318- case RsvSendKCode:
6319- Err = TTLSendKCode(); break;
6320- case RsvSendLn:
6321- Err = TTLSendLn(); break;
6322- case RsvSetBaud:
6323- Err = TTLCommCmdInt(CmdSetBaud,0); break;
6324- case RsvSetDate:
6325- Err = TTLSetDate(); break;
6326- case RsvSetDebug:
6327- Err = TTLCommCmdDeb(); break;
6328- case RsvSetDir:
6329- Err = TTLSetDir(); break;
6330- case RsvSetDlgPos:
6331- Err = TTLSetDlgPos(); break;
6332- case RsvSetDtr:
6333- Err = TTLCommCmdInt(CmdSetDtr,0); break;
6334- case RsvSetEcho:
6335- Err = TTLCommCmdBin(CmdSetEcho,0); break;
6336- case RsvSetEnv:
6337- Err = TTLSetEnv(); break;
6338- case RsvSetExitCode:
6339- Err = TTLSetExitCode(); break;
6340- case RsvSetFileAttr:
6341- Err = TTLSetFileAttr(); break;
6342- case RsvSetFlowCtrl:
6343- Err = TTLCommCmdInt(CmdSetFlowCtrl,0); break;
6344- case RsvSetRts:
6345- Err = TTLCommCmdInt(CmdSetRts,0); break;
6346- case RsvSetSync:
6347- Err = TTLSetSync(); break;
6348- case RsvSetTime:
6349- Err = TTLSetTime(); break;
6350- case RsvSetTitle:
6351- Err = TTLCommCmdFile(CmdSetTitle,0); break;
6352- case RsvShow:
6353- Err = TTLShow(); break;
6354- case RsvShowTT:
6355- Err = TTLCommCmdInt(CmdShowTT,0); break;
6356- case RsvSprintf:
6357- Err = TTLSprintf(0); break;
6358- case RsvSprintf2:
6359- Err = TTLSprintf(1); break;
6360- case RsvStatusBox:
6361- Err = TTLStatusBox(); break;
6362- case RsvStr2Code:
6363- Err = TTLStr2Code(); break;
6364- case RsvStr2Int:
6365- Err = TTLStr2Int(); break;
6366- case RsvStrCompare:
6367- Err = TTLStrCompare(); break;
6368- case RsvStrConcat:
6369- Err = TTLStrConcat(); break;
6370- case RsvStrCopy:
6371- Err = TTLStrCopy(); break;
6372- case RsvStrInsert:
6373- Err = TTLStrInsert(); break;
6374- case RsvStrJoin:
6375- Err = TTLStrJoin(); break;
6376- case RsvStrLen:
6377- Err = TTLStrLen(); break;
6378- case RsvStrMatch:
6379- Err = TTLStrMatch(); break;
6380- case RsvStrRemove:
6381- Err = TTLStrRemove(); break;
6382- case RsvStrReplace:
6383- Err = TTLStrReplace(); break;
6384- case RsvStrScan:
6385- Err = TTLStrScan(); break;
6386- case RsvStrSpecial:
6387- Err = TTLStrSpecial(); break;
6388- case RsvStrSplit:
6389- Err = TTLStrSplit(); break;
6390- case RsvStrTrim:
6391- Err = TTLStrTrim(); break;
6392- case RsvTestLink:
6393- Err = TTLTestLink(); break;
6394- case RsvToLower:
6395- Err = TTLToLower(); break; // add 'tolower' (2007.7.12 maya)
6396- case RsvToUpper:
6397- Err = TTLToUpper(); break; // add 'toupper' (2007.7.12 maya)
6398- case RsvUnlink:
6399- Err = TTLUnlink(); break;
6400- case RsvUntil:
6401- Err = TTLWhile(FALSE); break;
6402- case RsvUptime:
6403- Err = TTLUptime(); break;
6404- case RsvVar2Clipb:
6405- Err = TTLVar2Clipb(); break; // add 'var2clipb' (2006.9.17 maya)
6406- case RsvWaitRegex:
6407- Err = TTLWaitRegex(FALSE); break; // add 'waitregex' (2005.10.5 yutaka)
6408- case RsvWait:
6409- Err = TTLWait(FALSE); break;
6410- case RsvWait4all:
6411- Err = TTLWait4all(FALSE); break;
6412- case RsvWaitEvent:
6413- Err = TTLWaitEvent(); break;
6414- case RsvWaitLn:
6415- Err = TTLWait(TRUE); break;
6416- case RsvWaitN:
6417- Err = TTLWaitN(); break;
6418- case RsvWaitRecv:
6419- Err = TTLWaitRecv(); break;
6420- case RsvWhile:
6421- Err = TTLWhile(TRUE); break;
6422- case RsvXmodemRecv:
6423- Err = TTLXmodemRecv(); break;
6424- case RsvXmodemSend:
6425- Err = TTLXmodemSend(); break;
6426- case RsvYesNoBox:
6427- Err = TTLYesNoBox(); break;
6428- case RsvZmodemRecv:
6429- Err = TTLCommCmd(CmdZmodemRecv,IdTTLWaitCmndResult); break;
6430- case RsvZmodemSend:
6431- Err = TTLZmodemSend(); break;
6432- case RsvYmodemRecv:
6433- Err = TTLCommCmd(CmdYmodemRecv,IdTTLWaitCmndResult); break;
6434- case RsvYmodemSend:
6435- Err = TTLYmodemSend(); break;
6436- default:
6437- Err = ErrSyntax;
6438- }
6439- else if (GetIdentifier(Cmnd)) {
6440- if (GetIndex(&Index, &Err)) {
6441- WithIndex = TRUE;
6442- }
6443- else {
6444- WithIndex = FALSE;
6445- }
6446-
6447- if (!Err && GetFirstChar() == '=') {
6448- StrConst = GetString(Str,&Err);
6449- if (StrConst)
6450- ValType = TypString;
6451- else
6452- if (! GetExpression(&ValType,&Val,&Err))
6453- Err = ErrSyntax;
6454-
6455- if (!Err) {
6456- if (CheckVar(Cmnd,&VarType,&VarId)) {
6457- if (WithIndex) {
6458- switch (VarType) {
6459- case TypIntArray:
6460- VarId = GetIntVarFromArray(VarId, Index, &Err);
6461- if (!Err) VarType = TypInteger;
6462- break;
6463- case TypStrArray:
6464- VarId = GetStrVarFromArray(VarId, Index, &Err);
6465- if (!Err) VarType = TypString;
6466- break;
6467- default:
6468- Err = ErrSyntax;
6469- }
6470- }
6471- if (Err) return Err;
6472- if (VarType==ValType) {
6473- switch (ValType) {
6474- case TypInteger: SetIntVal(VarId,Val); break;
6475- case TypString:
6476- if (StrConst)
6477- SetStrVal(VarId,Str);
6478- else
6479- // StrVarPtr の返り値が TStrVal のポインタであることを期待してサイズを固定
6480- // (2007.6.23 maya)
6481- strncpy_s(StrVarPtr(VarId),MaxStrLen,StrVarPtr((TVarId)Val),_TRUNCATE);
6482- break;
6483- default:
6484- Err = ErrSyntax;
6485- }
6486- }
6487- else {
6488- Err = ErrTypeMismatch;
6489- }
6490- }
6491- else if (WithIndex) {
6492- Err = ErrSyntax;
6493- }
6494- else {
6495- switch (ValType) {
6496- case TypInteger: E = NewIntVar(Cmnd,Val); break;
6497- case TypString:
6498- if (StrConst)
6499- E = NewStrVar(Cmnd,Str);
6500- else
6501- E = NewStrVar(Cmnd,StrVarPtr((TVarId)Val));
6502- break;
6503- default:
6504- E = FALSE;
6505- }
6506- if (! E) Err = ErrTooManyVar;
6507- }
6508- if (!Err && (GetFirstChar()!=0))
6509- Err = ErrSyntax;
6510- }
6511- }
6512- else Err = ErrNotSupported;
6513- }
6514- else
6515- Err = ErrSyntax;
6516-
6517- return Err;
6518-}
6519-
6520-void Exec()
6521-{
6522- WORD Err;
6523-
6524- // ParseAgain is set by 'ExecCmnd'
6525- if (! ParseAgain &&
6526- ! GetNewLine())
6527- {
6528- TTLStatus = IdTTLEnd;
6529- return;
6530- }
6531- ParseAgain = FALSE;
6532-
6533- LockVar();
6534- Err = ExecCmnd();
6535- if (Err>0) DispErr(Err);
6536- UnlockVar();
6537-}
6538-
6539-// 正規表現でマッチした文字列を記録する
6540-// (2005.10.7 yutaka)
6541-void SetMatchStr(PCHAR Str)
6542-{
6543- WORD VarType;
6544- TVarId VarId;
6545-
6546- if (CheckVar("matchstr",&VarType,&VarId) &&
6547- (VarType==TypString))
6548- SetStrVal(VarId,Str);
6549-}
6550-
6551-// 正規表現でグループマッチした文字列を記録する
6552-// (2005.10.15 yutaka)
6553-void SetGroupMatchStr(int no, PCHAR Str)
6554-{
6555- WORD VarType;
6556- TVarId VarId;
6557- char buf[128];
6558- char *p;
6559-
6560- if (Str == NULL)
6561- p = "";
6562- else
6563- p = Str;
6564-
6565- _snprintf_s(buf, sizeof(buf), _TRUNCATE, "groupmatchstr%d", no);
6566-
6567- if (CheckVar(buf,&VarType,&VarId) &&
6568- (VarType==TypString))
6569- SetStrVal(VarId,p);
6570-}
6571-
6572-void SetInputStr(const char *Str)
6573-{
6574- WORD VarType;
6575- TVarId VarId;
6576-
6577- if (CheckVar("inputstr",&VarType,&VarId) &&
6578- (VarType==TypString))
6579- SetStrVal(VarId,Str);
6580-}
6581-
6582-void SetResult(int ResultCode)
6583-{
6584- WORD VarType;
6585- TVarId VarId;
6586-
6587- if (CheckVar("result",&VarType,&VarId) &&
6588- (VarType==TypInteger))
6589- SetIntVal(VarId,ResultCode);
6590-}
6591-
6592-BOOL CheckTimeout()
6593-{
6594- BOOL ret;
6595- DWORD TimeUp = (TimeStart+TimeLimit);
6596-
6597- if (TimeUp > TimeStart) {
6598- ret = (GetTickCount() > TimeUp);
6599- }
6600- else { // for DWORD overflow (49.7 days)
6601- DWORD TimeTmp = GetTickCount();
6602- ret = (TimeUp < TimeTmp && TimeTmp >= TimeStart);
6603- }
6604- return ret;
6605-}
6606-
6607-BOOL TestWakeup(int Wakeup)
6608-{
6609- return ((Wakeup & WakeupCondition) != 0);
6610-}
6611-
6612-void SetWakeup(int Wakeup)
6613-{
6614- WakeupCondition = Wakeup;
6615-}
--- branches/unicode_macro/teraterm/ttpmacro/CMakeLists.txt (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/CMakeLists.txt (revision 8624)
@@ -19,8 +19,10 @@
1919 statdlg.cpp
2020 statdlg.h
2121 macrodlgbase.h
22- ttl.c
22+ ttl.cpp
2323 ttl.h
24+ ttl_gui.cpp
25+ ttl_gui.h
2426 ttm_res.h
2527 ttmacro.cpp
2628 ttmacro.h
--- branches/unicode_macro/teraterm/ttpmacro/ListDlg.cpp (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/ListDlg.cpp (revision 8624)
@@ -29,6 +29,7 @@
2929 // ListDlg.cpp : 実装ファイル
3030 //
3131
32+#include <tchar.h>
3233 #include "tmfc.h"
3334 #include "teraterm.h"
3435 #include "ttlib.h"
@@ -37,12 +38,13 @@
3738 #include "tttypes.h"
3839 #include "dlglib.h"
3940 #include "ttmdlg.h"
41+#include "ttmacro.h"
4042
4143 #include "ListDlg.h"
4244
4345 // CListDlg ダイアログ
4446
45-CListDlg::CListDlg(const PCHAR Text, const PCHAR Caption, const CHAR **Lists, int Selected, int x, int y)
47+CListDlg::CListDlg(const TCHAR *Text, const TCHAR *Caption, TCHAR **Lists, int Selected, int x, int y)
4648 {
4749 m_Text = Text;
4850 m_Caption = Caption;
@@ -59,7 +61,7 @@
5961
6062 void CListDlg::InitList(HWND HList)
6163 {
62- const char **p;
64+ TCHAR **p;
6365 int ListMaxWidth = 0;
6466 int ListCount = 0;
6567 HDC DC = ::GetDC(HList);
--- branches/unicode_macro/teraterm/ttpmacro/ListDlg.h (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/ListDlg.h (revision 8624)
@@ -34,15 +34,15 @@
3434 class CListDlg : public CMacroDlgBase
3535 {
3636 public:
37- CListDlg(PCHAR Text, PCHAR Caption, const CHAR **Lists, int Selected, int x, int y);
37+ CListDlg(const TCHAR *Text, const TCHAR *Caption, TCHAR **Lists, int Selected, int x, int y);
3838 INT_PTR DoModal(HINSTANCE hInst, HWND hWndParent);
3939 int m_SelectItem;
4040
4141 private:
4242 enum { IDD = IDD_LISTDLG };
43- PCHAR m_Text;
44- PCHAR m_Caption;
45- const CHAR **m_Lists;
43+ const TCHAR *m_Text;
44+ const TCHAR *m_Caption;
45+ TCHAR **m_Lists;
4646 int m_Selected;
4747 int init_WW, TW, TH, BH, BW, LW, LH;
4848 SIZE s;
--- branches/unicode_macro/teraterm/ttpmacro/errdlg.cpp (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/errdlg.cpp (revision 8624)
@@ -31,7 +31,7 @@
3131
3232 #include <windowsx.h>
3333 #include <stdio.h>
34-
34+#include <tchar.h>
3535 #include "tmfc.h"
3636 #include "teraterm.h"
3737 #include "ttlib.h"
@@ -43,21 +43,23 @@
4343 #include "ttmparse.h"
4444 #include "htmlhelp.h"
4545 #include "dlglib.h"
46+#include "ttmacro.h"
47+#include "codeconv.h"
4648
4749 #include "errdlg.h"
4850
4951 // CErrDlg dialog
5052
51-CErrDlg::CErrDlg(const char *Msg, PCHAR Line, int x, int y, int lineno, int start, int end, PCHAR FileName)
53+CErrDlg::CErrDlg(const char *Msg, const char *Line, int x, int y, int lineno, int start, int end, const char *FileName)
5254 {
53- MsgStr = Msg;
54- LineStr = Line;
55+ MsgStr = _tcsdup((tc)Msg);
56+ LineStr = _tcsdup((tc)Line);
5557 PosX = x;
5658 PosY = y;
5759 LineNo = lineno;
5860 StartPos = start;
5961 EndPos = end;
60- MacroFileName = FileName;
62+ MacroFileName = _tcsdup((tc)FileName);
6163 }
6264
6365 INT_PTR CErrDlg::DoModal(HINSTANCE hInst, HWND hWndParent)
--- branches/unicode_macro/teraterm/ttpmacro/errdlg.h (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/errdlg.h (revision 8624)
@@ -36,17 +36,17 @@
3636 class CErrDlg : public CMacroDlgBase
3737 {
3838 public:
39- CErrDlg(const char *Msg, PCHAR Line, int x, int y, int lineno, int start, int end, PCHAR FileName);
39+ CErrDlg(const char *Msg, const char *Line, int x, int y, int lineno, int start, int end, const char *FileName);
4040 INT_PTR DoModal(HINSTANCE hInst, HWND hWndParent);
4141
4242 private:
4343 enum { IDD = IDD_ERRDLG };
4444
45- const char* MsgStr;
46- PCHAR LineStr;
45+ const TCHAR *MsgStr;
46+ const TCHAR *LineStr;
4747 int LineNo;
4848 int StartPos, EndPos;
49- PCHAR MacroFileName;
49+ const TCHAR *MacroFileName;
5050
5151 virtual BOOL OnInitDialog();
5252 BOOL OnCommand(WPARAM wp, LPARAM lp);
--- branches/unicode_macro/teraterm/ttpmacro/inpdlg.cpp (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/inpdlg.cpp (revision 8624)
@@ -40,8 +40,8 @@
4040 #include "inpdlg.h"
4141
4242 // CInpDlg dialog
43-CInpDlg::CInpDlg(PCHAR Input, PCHAR Text, PCHAR Title,
44- PCHAR Default, BOOL Paswd,
43+CInpDlg::CInpDlg(TCHAR *Input, const TCHAR *Text, const TCHAR *Title,
44+ const TCHAR *Default, BOOL Paswd,
4545 int x, int y)
4646 {
4747 InputStr = Input;
--- branches/unicode_macro/teraterm/ttpmacro/inpdlg.h (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/inpdlg.h (revision 8624)
@@ -37,8 +37,8 @@
3737 class CInpDlg : public CMacroDlgBase
3838 {
3939 public:
40- CInpDlg(PCHAR Input, PCHAR Text, PCHAR Title,
41- PCHAR Default, BOOL Paswd,
40+ CInpDlg(TCHAR *Input, const TCHAR *Text, const TCHAR *Title,
41+ const TCHAR *Default, BOOL Paswd,
4242 int x, int y);
4343 INT_PTR DoModal(HINSTANCE hInst, HWND hWndParent);
4444
@@ -45,7 +45,10 @@
4545 private:
4646 enum { IDD = IDD_INPDLG };
4747
48- PCHAR InputStr, TextStr, TitleStr, DefaultStr;
48+ TCHAR *InputStr;
49+ const TCHAR *TextStr;
50+ const TCHAR *TitleStr;
51+ const TCHAR *DefaultStr;
4952 BOOL PaswdFlag;
5053 int init_WW, TW, TH, BH, BW, EW, EH;
5154 SIZE s;
--- branches/unicode_macro/teraterm/ttpmacro/statdlg.cpp (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/statdlg.cpp (revision 8624)
@@ -37,12 +37,13 @@
3737 #include "ttmlib.h"
3838 #include "tmfc.h"
3939 #include "tttypes.h"
40+#include "ttmacro.h"
4041
4142 #include "statdlg.h"
4243
4344 // CStatDlg dialog
4445
45-BOOL CStatDlg::Create(HINSTANCE hInst, PCHAR Text, PCHAR Title, int x, int y)
46+BOOL CStatDlg::Create(HINSTANCE hInst, const TCHAR *Text, const TCHAR *Title, int x, int y)
4647 {
4748 TextStr = Text;
4849 TitleStr = Title;
@@ -51,7 +52,7 @@
5152 return TTCDialog::Create(hInst, NULL, CStatDlg::IDD);
5253 }
5354
54-void CStatDlg::Update(PCHAR Text, PCHAR Title, int x, int y)
55+void CStatDlg::Update(const TCHAR *Text, const TCHAR *Title, int x, int y)
5556 {
5657 RECT R;
5758
@@ -213,6 +214,12 @@
213214 BringupWindow(m_hWnd);
214215 }
215216
217+/**
218+ * MFCのCWndの隠れメンバ関数
219+ * この関数がFALSEを返すと
220+ * CDialog::OnInitDialog()後に
221+ * CWnd::CenterWindow() が呼び出されない
222+ */
216223 BOOL CStatDlg::CheckAutoCenter()
217224 {
218225 // CenterWindow() is called when x=0 && y=0
--- branches/unicode_macro/teraterm/ttpmacro/statdlg.h (revision 8623)
+++ branches/unicode_macro/teraterm/ttpmacro/statdlg.h (revision 8624)
@@ -34,12 +34,13 @@
3434 class CStatDlg : public CMacroDlgBase
3535 {
3636 public:
37- BOOL Create(HINSTANCE hInst, PCHAR Text, PCHAR Title, int x, int y);
38- void Update(PCHAR Text, PCHAR Title, int x, int y);
37+ BOOL Create(HINSTANCE hInst, const TCHAR *Text, const TCHAR *Title, int x, int y);
38+ void Update(const TCHAR *Text, const TCHAR *Title, int x, int y);
3939 void Bringup();
4040 enum { IDD = IDD_STATDLG };
4141 private:
42- PCHAR TextStr, TitleStr;
42+ const TCHAR *TextStr;
43+ const TCHAR *TitleStr;
4344 int init_WW, TW, TH;
4445 SIZE s;
4546
--- branches/unicode_macro/teraterm/ttpmacro/ttl.cpp (nonexistent)
+++ branches/unicode_macro/teraterm/ttpmacro/ttl.cpp (revision 8624)
@@ -0,0 +1,6644 @@
1+/*
2+ * Copyright (C) 1994-1998 T. Teranishi
3+ * (C) 2005-2020 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+/* TTMACRO.EXE, Tera Term Language interpreter */
31+
32+#include "teraterm.h"
33+#include <stdlib.h>
34+#include <stdio.h>
35+#include <string.h>
36+#include <mbstring.h>
37+#include <time.h>
38+#include <errno.h>
39+#include "tt-version.h"
40+#include "ttmdlg.h"
41+#include "ttmbuff.h"
42+#include "ttmparse.h"
43+#include "ttmdde.h"
44+#include "ttmlib.h"
45+#include "ttlib.h"
46+#include "ttmenc.h"
47+#include "tttypes.h"
48+#include "ttmonig.h"
49+#include <shellapi.h>
50+#include <sys/stat.h>
51+#include <io.h>
52+#include <fcntl.h>
53+#include <sys/types.h>
54+#include <sys/stat.h>
55+#include <share.h>
56+
57+// for _findXXXX() functions
58+#include <io.h>
59+
60+// for _ismbblead
61+#include <mbctype.h>
62+
63+#include "ttl.h"
64+#include "SFMT.h"
65+
66+#include <winsock2.h>
67+#include <ws2tcpip.h>
68+#include <iptypes.h>
69+#include <iphlpapi.h>
70+#include "win16api.h"
71+#include "ttl_gui.h"
72+#include "codeconv.h"
73+
74+#define TTERMCOMMAND "TTERMPRO /D="
75+#define CYGTERMCOMMAND "cyglaunch -o /D="
76+
77+// for 'ExecCmnd' command
78+static BOOL ParseAgain;
79+static int IfNest;
80+static int ElseFlag;
81+static int EndIfFlag;
82+// Window handle of the main window
83+static HWND HMainWin;
84+// Timeout
85+static DWORD TimeLimit;
86+static DWORD TimeStart;
87+// for 'WaitEvent' command
88+static int WakeupCondition;
89+
90+// exit code of TTMACRO
91+int ExitCode = 0;
92+
93+// for "FindXXXX" commands
94+#define NumDirHandle 8
95+static intptr_t DirHandle[NumDirHandle] = {-1,-1, -1, -1, -1, -1, -1, -1};
96+/* for "FileMarkPtr" and "FileSeekBack" commands */
97+#define NumFHandle 16
98+static HANDLE FHandle[NumFHandle];
99+static long FPointer[NumFHandle];
100+
101+// forward declaration
102+int ExecCmnd();
103+
104+static void HandleInit()
105+{
106+ int i;
107+ for (i=0; i<_countof(FHandle); i++) {
108+ FHandle[i] = INVALID_HANDLE_VALUE;
109+ }
110+}
111+
112+/**
113+ * @retval ファイルハンドルインデックス(0〜)
114+ * -1のときエラー
115+ */
116+static int HandlePut(HANDLE FH)
117+{
118+ int i;
119+ if (FH == INVALID_HANDLE_VALUE) {
120+ return -1;
121+ }
122+ for (i=0; i<_countof(FHandle); i++) {
123+ if (FHandle[i] == INVALID_HANDLE_VALUE) {
124+ FHandle[i] = FH;
125+ FPointer[i] = 0;
126+ return i;
127+ }
128+ }
129+ return -1;
130+}
131+
132+static HANDLE HandleGet(int fhi)
133+{
134+ if (fhi < 0 || _countof(FHandle) < fhi) {
135+ return INVALID_HANDLE_VALUE;
136+ }
137+ return FHandle[fhi];
138+}
139+
140+static void HandleFree(int fhi)
141+{
142+ FHandle[fhi] = INVALID_HANDLE_VALUE;
143+}
144+
145+BOOL InitTTL(HWND HWin)
146+{
147+ int i;
148+ TStrVal Dir;
149+ TVarId ParamsVarId;
150+ char tmpname[10];
151+ WORD Err;
152+
153+ HMainWin = HWin;
154+
155+ if (! InitVar()) return FALSE;
156+ LockVar();
157+
158+ // System variables
159+ NewIntVar("result",0);
160+ NewIntVar("timeout",0);
161+ NewIntVar("mtimeout",0); // ミリ秒単位のタイムアウト用 (2009.1.23 maya)
162+ NewStrVar("inputstr","");
163+ NewStrVar("matchstr",""); // for 'waitregex' command (2005.10.7 yutaka)
164+ NewStrVar("groupmatchstr1",""); // for 'waitregex' command (2005.10.15 yutaka)
165+ NewStrVar("groupmatchstr2",""); // for 'waitregex' command (2005.10.15 yutaka)
166+ NewStrVar("groupmatchstr3",""); // for 'waitregex' command (2005.10.15 yutaka)
167+ NewStrVar("groupmatchstr4",""); // for 'waitregex' command (2005.10.15 yutaka)
168+ NewStrVar("groupmatchstr5",""); // for 'waitregex' command (2005.10.15 yutaka)
169+ NewStrVar("groupmatchstr6",""); // for 'waitregex' command (2005.10.15 yutaka)
170+ NewStrVar("groupmatchstr7",""); // for 'waitregex' command (2005.10.15 yutaka)
171+ NewStrVar("groupmatchstr8",""); // for 'waitregex' command (2005.10.15 yutaka)
172+ NewStrVar("groupmatchstr9",""); // for 'waitregex' command (2005.10.15 yutaka)
173+
174+ if (ParamCnt == 0) {
175+ ParamCnt++;
176+ }
177+ NewIntVar("paramcnt",ParamCnt); // ファイル名も含む引数の個数 (2012.4.10 yutaka)
178+
179+ // 旧形式のパラメータ設定 (param1 〜 param9)
180+ NewStrVar("param1", ShortName);
181+ if (Params) {
182+ for (i=2; i<=9; i++) {
183+ _snprintf_s(tmpname, sizeof(tmpname), _TRUNCATE, "param%d", i);
184+ if (ParamCnt >= i && Params[i] != NULL) {
185+ NewStrVar(tmpname, Params[i]);
186+ }
187+ else {
188+ NewStrVar(tmpname, "");
189+ }
190+ }
191+ }
192+
193+ // 新形式のパラメータ設定 (params[1〜ParamCnt])
194+ if (NewStrAryVar("params", ParamCnt+1) == 0) {
195+ Err = 0;
196+ GetStrAryVarByName(&ParamsVarId, "params", &Err);
197+ if (Err == 0) {
198+ if (ShortName[0] != 0) {
199+ SetStrValInArray(ParamsVarId, 1, ShortName, &Err);
200+ }
201+ if (Params) {
202+ for (i=0; i<=ParamCnt; i++) {
203+ if (i == 1) {
204+ continue;
205+ }
206+ if (Params[i]) {
207+ SetStrValInArray(ParamsVarId, i, Params[i], &Err);
208+ free(Params[i]);
209+ }
210+ }
211+ free(Params);
212+ Params = NULL;
213+ }
214+ }
215+ }
216+
217+ ParseAgain = FALSE;
218+ IfNest = 0;
219+ ElseFlag = 0;
220+ EndIfFlag = 0;
221+
222+ for (i=0; i<NumDirHandle; i++)
223+ DirHandle[i] = -1L;
224+ HandleInit();
225+
226+ if (! InitBuff(FileName))
227+ {
228+ TTLStatus = IdTTLEnd;
229+ return FALSE;
230+ }
231+
232+ UnlockVar();
233+
234+ ExtractDirName(FileName,Dir);
235+ TTMSetDir(Dir);
236+
237+ if (SleepFlag)
238+ { // synchronization for startup macro
239+ // sleep until Tera Term is ready
240+ WakeupCondition = IdWakeupInit;
241+ // WakeupCondition = IdWakeupConnect | IdWakeupDisconn | IdWakeupUnlink;
242+ TTLStatus = IdTTLSleep;
243+ }
244+ else
245+ TTLStatus = IdTTLRun;
246+
247+ return TRUE;
248+}
249+
250+void EndTTL()
251+{
252+ int i;
253+
254+ CloseStatDlg();
255+
256+ for (i=0; i<NumDirHandle; i++)
257+ {
258+ if (DirHandle[i]!=-1L)
259+ _findclose(DirHandle[i]);
260+ DirHandle[i] = -1L;
261+ }
262+
263+ UnlockVar();
264+ if (TTLStatus==IdTTLWait)
265+ KillTimer(HMainWin,IdTimeOutTimer);
266+ CloseBuff(0);
267+ EndVar();
268+}
269+
270+long int CalcTime()
271+{
272+ time_t time1;
273+ struct tm *ptm;
274+
275+ time1 = time(NULL);
276+ ptm = localtime(&time1);
277+ return ((long int)ptm->tm_hour * 3600 +
278+ (long int)ptm->tm_min * 60 +
279+ (long int)ptm->tm_sec
280+ );
281+}
282+
283+//////////////// Beginning of TTL commands //////////////
284+WORD TTLCommCmd(char Cmd, int Wait)
285+{
286+ if (GetFirstChar()!=0)
287+ return ErrSyntax;
288+ else if (! Linked)
289+ return ErrLinkFirst;
290+ else
291+ return SendCmnd(Cmd,Wait);
292+}
293+
294+WORD TTLCommCmdFile(char Cmd, int Wait)
295+{
296+ TStrVal Str;
297+ WORD Err;
298+
299+ Err = 0;
300+ GetStrVal(Str,&Err);
301+
302+ if ((Err==0) &&
303+ ((strlen(Str)==0) || (GetFirstChar()!=0)))
304+ Err = ErrSyntax;
305+ if ((Err==0) && (! Linked))
306+ Err = ErrLinkFirst;
307+ if (Err==0)
308+ {
309+ SetFile(Str);
310+ Err = SendCmnd(Cmd,Wait);
311+ }
312+ return Err;
313+}
314+
315+WORD TTLCommCmdBin(char Cmd, int Wait)
316+{
317+ int Val;
318+ WORD Err;
319+
320+ Err = 0;
321+ GetIntVal(&Val,&Err);
322+
323+ if ((Err==0) &&
324+ (GetFirstChar()!=0))
325+ Err = ErrSyntax;
326+ if ((Err==0) && (! Linked))
327+ Err = ErrLinkFirst;
328+ if (Err==0)
329+ {
330+ SetBinary(Val);
331+ Err = SendCmnd(Cmd,Wait);
332+ }
333+ return Err;
334+}
335+
336+
337+WORD TTLCommCmdDeb()
338+{
339+ int Val;
340+ WORD Err;
341+
342+ Err = 0;
343+ GetIntVal(&Val,&Err);
344+
345+ if ((Err==0) &&
346+ (GetFirstChar()!=0))
347+ Err = ErrSyntax;
348+ if ((Err==0) && (! Linked))
349+ Err = ErrLinkFirst;
350+ if (Err==0)
351+ {
352+ SetDebug(Val);
353+ }
354+ return Err;
355+}
356+
357+WORD TTLCommCmdInt(char Cmd, int Wait)
358+{
359+ int Val;
360+ char Str[21];
361+ WORD Err;
362+
363+ Err = 0;
364+ GetIntVal(&Val,&Err);
365+
366+ if ((Err==0) &&
367+ (GetFirstChar()!=0))
368+ Err = ErrSyntax;
369+ if ((Err==0) && (! Linked))
370+ Err = ErrLinkFirst;
371+ if (Err==0)
372+ {
373+ _snprintf_s(Str,sizeof(Str),_TRUNCATE,"%d",Val);
374+ SetFile(Str);
375+ Err = SendCmnd(Cmd,Wait);
376+ }
377+ return Err;
378+}
379+
380+WORD TTLBeep()
381+{
382+ int val = 0;
383+ WORD Err = 0;
384+ UINT type = MB_OK;
385+
386+ if (CheckParameterGiven()) {
387+ GetIntVal(&val, &Err);
388+ if (Err!=0) return Err;
389+
390+ switch (val) {
391+ case 0:
392+ type = -1;
393+ break;
394+ case 1:
395+ type = MB_ICONASTERISK;
396+ break;
397+ case 2:
398+ type = MB_ICONEXCLAMATION;
399+ break;
400+ case 3:
401+ type = MB_ICONHAND;
402+ break;
403+ case 4:
404+ type = MB_ICONQUESTION;
405+ break;
406+ case 5:
407+ type = MB_OK;
408+ break;
409+ default:
410+ return ErrSyntax;
411+ break;
412+ }
413+ }
414+
415+ MessageBeep(type);
416+
417+ return 0;
418+}
419+
420+WORD TTLBreak(WORD WId) {
421+ if (GetFirstChar()!=0)
422+ return ErrSyntax;
423+
424+ return BreakLoop(WId);
425+}
426+
427+WORD TTLBringupBox()
428+{
429+ if (GetFirstChar()!=0)
430+ return ErrSyntax;
431+ BringupStatDlg();
432+ return 0;
433+}
434+
435+WORD TTLCall()
436+{
437+ TName LabName;
438+ WORD Err, VarType;
439+ TVarId VarId;
440+
441+ if (GetLabelName(LabName) && (GetFirstChar()==0)) {
442+ if (CheckVar(LabName, &VarType, &VarId) && (VarType==TypLabel))
443+ Err = CallToLabel(VarId);
444+ else
445+ Err = ErrLabelReq;
446+ }
447+ else
448+ Err = ErrSyntax;
449+
450+ return Err;
451+}
452+
453+#if 0
454+// add 'clipb2var' (2006.9.17 maya)
455+WORD TTLClipb2Var()
456+{
457+ WORD Err;
458+ TVarId VarId;
459+ HANDLE hText;
460+ PTSTR clipbText;
461+ char buf[MaxStrLen];
462+ int Num = 0;
463+ char *newbuff;
464+ static char *cbbuff;
465+ static int cbbuffsize, cblen;
466+ HANDLE wide_hText;
467+ LPWSTR wide_buf;
468+ int mb_len;
469+ UINT Cf;
470+
471+ Err = 0;
472+ GetStrVar(&VarId, &Err);
473+ if (Err!=0) return Err;
474+
475+ // get 2nd arg(optional) if given
476+ if (CheckParameterGiven()) {
477+ GetIntVal(&Num, &Err);
478+ }
479+
480+ if ((Err==0) && (GetFirstChar()!=0))
481+ Err = ErrSyntax;
482+ if (Err!=0) return Err;
483+
484+ if (Num == 0) {
485+ if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
486+ Cf = CF_UNICODETEXT;
487+ }
488+ else if (IsClipboardFormatAvailable(CF_TEXT)) {
489+ Cf = CF_TEXT;
490+ }
491+ else {
492+ cblen = 0;
493+ SetResult(0);
494+ return Err;
495+ }
496+ if (OpenClipboard(NULL) == 0) {
497+ cblen = 0;
498+ SetResult(0);
499+ return Err;
500+ }
501+
502+ if (Cf == CF_UNICODETEXT) {
503+ wide_hText = GetClipboardData(CF_UNICODETEXT);
504+ if (wide_hText != NULL) {
505+ wide_buf = GlobalLock(wide_hText);
506+ mb_len = WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, NULL, 0, NULL, NULL);
507+ hText = GlobalAlloc(GMEM_MOVEABLE, sizeof(CHAR) * mb_len);
508+ clipbText = GlobalLock(hText);
509+ if (hText != NULL) {
510+ WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, clipbText, mb_len, NULL, NULL);
511+
512+ cblen = strlen(clipbText);
513+ if (cbbuffsize <= cblen) {
514+ if ((newbuff = realloc(cbbuff, cblen + 1)) == NULL) {
515+ // realloc failed. fall back to old mode.
516+ cblen = 0;
517+ strncpy_s(buf,sizeof(buf),clipbText,_TRUNCATE);
518+ GlobalUnlock(hText);
519+ CloseClipboard();
520+ SetStrVal(VarId, buf);
521+ SetResult(3);
522+ return Err;
523+ }
524+ cbbuff = newbuff;
525+ cbbuffsize = cblen + 1;
526+ }
527+ strncpy_s(cbbuff, cbbuffsize, clipbText, _TRUNCATE);
528+
529+ GlobalUnlock(hText);
530+ GlobalFree(hText);
531+ }
532+ GlobalUnlock(wide_hText);
533+ }
534+ else {
535+ cblen = 0;
536+ }
537+ }
538+ else if (Cf == CF_TEXT) {
539+ hText = GetClipboardData(CF_TEXT);
540+ if (hText != NULL) {
541+ clipbText = GlobalLock(hText);
542+ cblen = strlen(clipbText);
543+ if (cbbuffsize <= cblen) {
544+ if ((newbuff = realloc(cbbuff, cblen + 1)) == NULL) {
545+ // realloc failed. fall back to old mode.
546+ cblen = 0;
547+ strncpy_s(buf,sizeof(buf),clipbText,_TRUNCATE);
548+ GlobalUnlock(hText);
549+ CloseClipboard();
550+ SetStrVal(VarId, buf);
551+ SetResult(3);
552+ return Err;
553+ }
554+ cbbuff = newbuff;
555+ cbbuffsize = cblen + 1;
556+ }
557+ strncpy_s(cbbuff, cbbuffsize, clipbText, _TRUNCATE);
558+ GlobalUnlock(hText);
559+ }
560+ else {
561+ cblen = 0;
562+ }
563+ }
564+ CloseClipboard();
565+ }
566+
567+ if (cbbuff != NULL && Num >= 0 && Num * (MaxStrLen - 1) < cblen) {
568+ if (strncpy_s(buf ,sizeof(buf), cbbuff + Num * (MaxStrLen-1), _TRUNCATE) == STRUNCATE)
569+ SetResult(2); // Copied string is truncated.
570+ else {
571+ SetResult(1);
572+ }
573+ SetStrVal(VarId, buf);
574+ }
575+ else {
576+ SetResult(0);
577+ }
578+
579+ return Err;
580+}
581+#endif
582+
583+#if 0
584+// add 'var2clipb' (2006.9.17 maya)
585+WORD TTLVar2Clipb()
586+{
587+ WORD Err;
588+ TStrVal Str;
589+ HGLOBAL hText;
590+ PTSTR clipbText;
591+ int wide_len;
592+ HGLOBAL wide_hText;
593+ LPWSTR wide_buf;
594+
595+ Err = 0;
596+ GetStrVal(Str,&Err);
597+ if (Err!=0) return Err;
598+
599+ hText = GlobalAlloc(GHND, sizeof(Str));
600+ clipbText = GlobalLock(hText);
601+ strncpy_s(clipbText, sizeof(Str), Str, _TRUNCATE);
602+ GlobalUnlock(hText);
603+
604+ wide_len = MultiByteToWideChar(CP_ACP, 0, clipbText, -1, NULL, 0);
605+ wide_hText = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len);
606+ if (wide_hText) {
607+ wide_buf = (LPWSTR)GlobalLock(wide_hText);
608+ MultiByteToWideChar(CP_ACP, 0, clipbText, -1, wide_buf, wide_len);
609+ GlobalUnlock(wide_hText);
610+ }
611+
612+ if (OpenClipboard(NULL) == 0) {
613+ SetResult(0);
614+ }
615+ else {
616+ EmptyClipboard();
617+ SetClipboardData(CF_TEXT, hText);
618+
619+ if (wide_buf) {
620+ SetClipboardData(CF_UNICODETEXT, wide_hText);
621+ }
622+
623+ CloseClipboard();
624+ SetResult(1);
625+ }
626+
627+ return Err;
628+}
629+#endif
630+
631+WORD TTLCloseSBox()
632+{
633+ if (GetFirstChar()!=0)
634+ return ErrSyntax;
635+ CloseStatDlg();
636+ return 0;
637+}
638+
639+WORD TTLCloseTT()
640+{
641+ if (GetFirstChar()!=0)
642+ return ErrSyntax;
643+
644+ if (! Linked)
645+ return ErrLinkFirst;
646+ else {
647+ // Close Tera Term
648+ SendCmnd(CmdCloseWin,IdTTLWaitCmndEnd);
649+ EndDDE();
650+ return 0;
651+ }
652+}
653+
654+WORD TTLCode2Str()
655+{
656+ TVarId VarId;
657+ WORD Err;
658+ int Num, Len, c, i;
659+ BYTE d;
660+ TStrVal Str;
661+
662+ Err = 0;
663+ GetStrVar(&VarId, &Err);
664+
665+ GetIntVal(&Num,&Err);
666+ if ((Err==0) && (GetFirstChar()!=0))
667+ Err = ErrSyntax;
668+ if (Err!=0) return Err;
669+
670+ Len = sizeof(Num);
671+ i = 0;
672+ for (c=0; c<=Len-1; c++)
673+ {
674+ d = (Num >> ((Len-1-c)*8)) & 0xff;
675+ if ((i>0) || (d!=0))
676+ {
677+ Str[i] = d;
678+ i++;
679+ }
680+ }
681+ Str[i] = 0;
682+ SetStrVal(VarId, Str);
683+ return Err;
684+}
685+
686+WORD TTLConnect(WORD mode)
687+{
688+ TStrVal Cmnd, Str;
689+ WORD Err;
690+ WORD w;
691+
692+ Str[0] = 0;
693+ Err = 0;
694+
695+ if (mode == RsvConnect || CheckParameterGiven()) {
696+ GetStrVal(Str,&Err);
697+ if (Err!=0) return Err;
698+ }
699+
700+ if (GetFirstChar()!=0)
701+ return ErrSyntax;
702+
703+ if (Linked)
704+ {
705+ if (ComReady!=0)
706+ {
707+ SetResult(2);
708+ return Err;
709+ }
710+
711+ if (mode == RsvConnect) {
712+ // new connection
713+ SetFile(Str);
714+ SendCmnd(CmdConnect,0);
715+
716+ WakeupCondition = IdWakeupInit;
717+ TTLStatus = IdTTLSleep;
718+ return Err;
719+ }
720+ else { // cygwin connection
721+ TTLCloseTT();
722+ }
723+ }
724+
725+ SetResult(0);
726+ // link to Tera Term
727+ if (strlen(TopicName)==0)
728+ {
729+ switch (mode) {
730+ case RsvConnect:
731+ strncpy_s(Cmnd, sizeof(Cmnd),TTERMCOMMAND, _TRUNCATE);
732+ break;
733+ case RsvCygConnect:
734+ strncpy_s(Cmnd, sizeof(Cmnd),CYGTERMCOMMAND, _TRUNCATE);
735+ break;
736+ }
737+ w = HIWORD(HMainWin);
738+ Word2HexStr(w,TopicName);
739+ w = LOWORD(HMainWin);
740+ Word2HexStr(w,&(TopicName[4]));
741+ strncat_s(Cmnd,sizeof(Cmnd),TopicName,_TRUNCATE);
742+ strncat_s(Cmnd,sizeof(Cmnd)," ",_TRUNCATE);
743+ strncat_s(Cmnd,sizeof(Cmnd),Str,_TRUNCATE);
744+ if (WinExec(Cmnd,SW_SHOW)<32)
745+ return ErrCantConnect;
746+ TTLStatus = IdTTLInitDDE;
747+ }
748+ return Err;
749+}
750+
751+
752+/*
753+ * cf. http://oku.edu.mie-u.ac.jp/~okumura/algo/
754+ */
755+
756+enum checksum_type {
757+ CHECKSUM8,
758+ CHECKSUM16,
759+ CHECKSUM32,
760+ CRC16,
761+ CRC32
762+};
763+
764+static unsigned int checksum32(int n, unsigned char c[])
765+{
766+ unsigned long value = 0;
767+ int i;
768+
769+ for (i = 0; i < n; i++) {
770+ value += (c[i] & 0xFF);
771+ }
772+ return (value & 0xFFFFFFFF);
773+}
774+
775+static unsigned int checksum16(int n, unsigned char c[])
776+{
777+ unsigned long value = 0;
778+ int i;
779+
780+ for (i = 0; i < n; i++) {
781+ value += (c[i] & 0xFF);
782+ }
783+ return (value & 0xFFFF);
784+}
785+
786+static unsigned int checksum8(int n, unsigned char c[])
787+{
788+ unsigned long value = 0;
789+ int i;
790+
791+ for (i = 0; i < n; i++) {
792+ value += (c[i] & 0xFF);
793+ }
794+ return (value & 0xFF);
795+}
796+
797+// CRC-16-CCITT
798+static unsigned int crc16(int n, unsigned char c[])
799+{
800+#define CRC16POLY1 0x1021U /* x^{16}+x^{12}+x^5+1 */
801+#define CRC16POLY2 0x8408U /* 左右逆転 */
802+
803+ int i, j;
804+ unsigned long r;
805+
806+ r = 0xFFFFU;
807+ for (i = 0; i < n; i++) {
808+ r ^= c[i];
809+ for (j = 0; j < CHAR_BIT; j++)
810+ if (r & 1) r = (r >> 1) ^ CRC16POLY2;
811+ else r >>= 1;
812+ }
813+ return r ^ 0xFFFFU;
814+}
815+
816+static unsigned long crc32(int n, unsigned char c[])
817+{
818+#define CRC32POLY1 0x04C11DB7UL
819+ /* x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11]+
820+ x^{10}+x^8+x^7+x^5+x^4+x^2+x^1+1 */
821+#define CRC32POLY2 0xEDB88320UL /* 左右逆転 */
822+ int i, j;
823+ unsigned long r;
824+
825+ r = 0xFFFFFFFFUL;
826+ for (i = 0; i < n; i++) {
827+ r ^= c[i];
828+ for (j = 0; j < CHAR_BIT; j++)
829+ if (r & 1) r = (r >> 1) ^ CRC32POLY2;
830+ else r >>= 1;
831+ }
832+ return r ^ 0xFFFFFFFFUL;
833+}
834+
835+// チェックサムアルゴリズム・共通ルーチン
836+WORD TTLDoChecksum(enum checksum_type type)
837+{
838+ TStrVal Str;
839+ WORD Err;
840+ TVarId VarId;
841+ DWORD cksum;
842+ unsigned char *p;
843+
844+ Err = 0;
845+ GetIntVar(&VarId, &Err);
846+ GetStrVal(Str,&Err);
847+ if ((Err==0) && (GetFirstChar()!=0))
848+ Err = ErrSyntax;
849+ if (Err!=0) return Err;
850+ if (Str[0]==0) return Err;
851+ p = (unsigned char *)Str;
852+
853+ switch (type) {
854+ case CHECKSUM8:
855+ cksum = checksum8(strlen(Str), p);
856+ break;
857+ case CHECKSUM16:
858+ cksum = checksum16(strlen(Str), p);
859+ break;
860+ case CHECKSUM32:
861+ cksum = checksum32(strlen(Str), p);
862+ break;
863+ case CRC16:
864+ cksum = crc16(strlen(Str), p);
865+ break;
866+ case CRC32:
867+ cksum = crc32(strlen(Str), p);
868+ break;
869+ default:
870+ cksum = 0;
871+ break;
872+ }
873+
874+ SetIntVal(VarId, cksum);
875+
876+ return Err;
877+}
878+
879+WORD TTLDoChecksumFile(enum checksum_type type)
880+{
881+ TStrVal Str;
882+ int result = 0;
883+ WORD Err;
884+ TVarId VarId;
885+ HANDLE fh = INVALID_HANDLE_VALUE, hMap = NULL;
886+ LPBYTE lpBuf = NULL;
887+ DWORD fsize;
888+ DWORD cksum;
889+
890+ Err = 0;
891+ GetIntVar(&VarId, &Err);
892+ GetStrVal(Str,&Err);
893+ if ((Err==0) && (GetFirstChar()!=0))
894+ Err = ErrSyntax;
895+ if (Err!=0) return Err;
896+ if (Str[0]==0) return Err;
897+
898+ fh = CreateFile(Str,GENERIC_READ,0,NULL,OPEN_EXISTING,
899+ FILE_ATTRIBUTE_NORMAL,NULL); /* ファイルオープン */
900+ if (fh == INVALID_HANDLE_VALUE) {
901+ result = -1;
902+ goto error;
903+ }
904+ /* ファイルマッピングオブジェクト作成 */
905+ hMap = CreateFileMapping(fh,NULL,PAGE_READONLY,0,0,NULL);
906+ if (hMap == NULL) {
907+ result = -1;
908+ goto error;
909+ }
910+
911+ /* ファイルをマップし、先頭アドレスをlpBufに取得 */
912+ lpBuf = (LPBYTE)MapViewOfFile(hMap,FILE_MAP_READ,0,0,0);
913+ if (lpBuf == NULL) {
914+ result = -1;
915+ goto error;
916+ }
917+
918+ fsize = GetFileSize(fh,NULL);
919+
920+ switch (type) {
921+ case CHECKSUM8:
922+ cksum = checksum8(fsize, lpBuf);
923+ break;
924+ case CHECKSUM16:
925+ cksum = checksum16(fsize, lpBuf);
926+ break;
927+ case CHECKSUM32:
928+ cksum = checksum32(fsize, lpBuf);
929+ break;
930+ case CRC16:
931+ cksum = crc16(fsize, lpBuf);
932+ break;
933+ case CRC32:
934+ cksum = crc32(fsize, lpBuf);
935+ break;
936+ default:
937+ cksum = 0;
938+ break;
939+ }
940+
941+ SetIntVal(VarId, cksum);
942+
943+error:
944+ if (lpBuf != NULL) {
945+ UnmapViewOfFile(lpBuf);
946+ }
947+
948+ if (hMap != NULL) {
949+ CloseHandle(hMap);
950+ }
951+ if (fh != INVALID_HANDLE_VALUE) {
952+ CloseHandle(fh);
953+ }
954+
955+ SetResult(result);
956+
957+ return Err;
958+}
959+
960+WORD TTLDelPassword()
961+{
962+ TStrVal Str, Str2;
963+ WORD Err;
964+
965+ Err = 0;
966+ GetStrVal(Str,&Err);
967+ GetStrVal(Str2,&Err);
968+ if ((Err==0) && (GetFirstChar()!=0))
969+ Err = ErrSyntax;
970+ if (Err!=0) return Err;
971+ if (Str[0]==0) return Err;
972+
973+ GetAbsPath(Str,sizeof(Str));
974+ if (! DoesFileExist(Str)) return Err;
975+ if (Str2[0]==0) // delete all password
976+ WritePrivateProfileString("Password",NULL,NULL,Str);
977+ else // delete password specified by Str2
978+ WritePrivateProfileString("Password",Str2,NULL,Str);
979+ return Err;
980+}
981+
982+WORD TTLDim(WORD type)
983+{
984+ WORD Err, WordId, VarType;
985+ TName Name;
986+ TVarId VarId;
987+ int size;
988+
989+ Err = 0;
990+
991+ if (! GetIdentifier(Name)) return ErrSyntax;
992+ if (CheckReservedWord(Name, &WordId)) return ErrSyntax;
993+ if (CheckVar(Name, &VarType, &VarId)) return ErrSyntax;
994+
995+ GetIntVal(&size, &Err);
996+ if (Err!=0) return Err;
997+
998+ if (type == RsvIntDim) {
999+ Err = NewIntAryVar(Name, size);
1000+ }
1001+ else { // type == RsvStrDim
1002+ Err = NewStrAryVar(Name, size);
1003+ }
1004+ return Err;
1005+}
1006+
1007+WORD TTLDisconnect()
1008+{
1009+ WORD Err;
1010+ int Val = 1;
1011+ char Str[21];
1012+
1013+ Err = 0;
1014+ // get 1rd arg(optional) if given
1015+ if (CheckParameterGiven()) {
1016+ GetIntVal(&Val, &Err);
1017+ }
1018+
1019+ if ((Err==0) &&
1020+ (GetFirstChar()!=0))
1021+ Err = ErrSyntax;
1022+ if ((Err==0) && (! Linked))
1023+ Err = ErrLinkFirst;
1024+ if (Err==0)
1025+ {
1026+ _snprintf_s(Str,sizeof(Str),_TRUNCATE,"%d",Val);
1027+ SetFile(Str);
1028+ Err = SendCmnd(CmdDisconnect,0);
1029+ }
1030+ return Err;
1031+}
1032+
1033+WORD TTLDispStr()
1034+{
1035+ TStrVal Str, buff;
1036+ WORD Err, ValType;
1037+ int Val;
1038+
1039+ if (! Linked)
1040+ return ErrLinkFirst;
1041+
1042+ buff[0] = 0;
1043+
1044+ while (TRUE) {
1045+ if (GetString(Str, &Err)) {
1046+ if (Err) return Err;
1047+ strncat_s(buff, MaxStrLen, Str, _TRUNCATE);
1048+ }
1049+ else if (GetExpression(&ValType, &Val, &Err)) {
1050+ if (Err!=0) return Err;
1051+ switch (ValType) {
1052+ case TypInteger:
1053+ Str[0] = LOBYTE(Val);
1054+ Str[1] = 0;
1055+ strncat_s(buff, MaxStrLen, Str, _TRUNCATE);
1056+ /* Falls through. */
1057+ case TypString:
1058+ strncat_s(buff, MaxStrLen, StrVarPtr((TVarId)Val), _TRUNCATE);
1059+ break;
1060+ default:
1061+ return ErrTypeMismatch;
1062+ }
1063+ }
1064+ else {
1065+ break;
1066+ }
1067+ }
1068+ SetFile(buff);
1069+ return SendCmnd(CmdDispStr, 0);
1070+}
1071+
1072+WORD TTLDo()
1073+{
1074+ WORD WId, Err;
1075+ int Val = 1;
1076+
1077+ Err = 0;
1078+ if (CheckParameterGiven()) {
1079+ if (GetReservedWord(&WId)) {
1080+ switch (WId) {
1081+ case RsvWhile:
1082+ GetIntVal(&Val,&Err);
1083+ break;
1084+ case RsvUntil:
1085+ GetIntVal(&Val,&Err);
1086+ Val = Val == 0;
1087+ break;
1088+ default:
1089+ Err = ErrSyntax;
1090+ }
1091+ if ((Err==0) && (GetFirstChar()!=0))
1092+ Err = ErrSyntax;
1093+ }
1094+ else {
1095+ Err = ErrSyntax;
1096+ }
1097+ }
1098+
1099+ if (Err!=0) return Err;
1100+
1101+ if (Val!=0)
1102+ return SetWhileLoop();
1103+ else
1104+ EndWhileLoop();
1105+ return Err;
1106+}
1107+
1108+WORD TTLElse()
1109+{
1110+ if (GetFirstChar()!=0)
1111+ return ErrSyntax;
1112+
1113+ if (IfNest<1)
1114+ return ErrInvalidCtl;
1115+
1116+ // Skip until 'EndIf'
1117+ IfNest--;
1118+ EndIfFlag = 1;
1119+ return 0;
1120+}
1121+
1122+int CheckElseIf(LPWORD Err)
1123+{
1124+ int Val;
1125+ WORD WId;
1126+
1127+ *Err = 0;
1128+ GetIntVal(&Val,Err);
1129+ if (*Err!=0) return 0;
1130+ if (! GetReservedWord(&WId) ||
1131+ (WId!=RsvThen) ||
1132+ (GetFirstChar()!=0))
1133+ *Err = ErrSyntax;
1134+ return Val;
1135+}
1136+
1137+WORD TTLElseIf()
1138+{
1139+ WORD Err;
1140+ int Val;
1141+
1142+ Val = CheckElseIf(&Err);
1143+ if (Err!=0) return Err;
1144+
1145+ if (IfNest<1)
1146+ return ErrInvalidCtl;
1147+
1148+ // Skip until 'EndIf'
1149+ IfNest--;
1150+ EndIfFlag = 1;
1151+ return Err;
1152+}
1153+
1154+WORD TTLEnd()
1155+{
1156+ if (GetFirstChar()==0)
1157+ {
1158+ TTLStatus = IdTTLEnd;
1159+ return 0;
1160+ }
1161+ else
1162+ return ErrSyntax;
1163+}
1164+
1165+WORD TTLEndIf()
1166+{
1167+ if (GetFirstChar()!=0)
1168+ return ErrSyntax;
1169+
1170+ if (IfNest<1)
1171+ return ErrInvalidCtl;
1172+
1173+ IfNest--;
1174+ return 0;
1175+}
1176+
1177+WORD TTLEndWhile(BOOL mode)
1178+{
1179+ WORD Err;
1180+ int Val = mode;
1181+
1182+ Err = 0;
1183+ if (CheckParameterGiven()) {
1184+ GetIntVal(&Val,&Err);
1185+ }
1186+
1187+ if ((Err==0) && (GetFirstChar()!=0))
1188+ Err = ErrSyntax;
1189+ if (Err!=0) return Err;
1190+
1191+ return BackToWhile((Val!=0) == mode);
1192+}
1193+
1194+WORD TTLExec()
1195+{
1196+ TStrVal Str,Str2, CurDir;
1197+ int mode = SW_SHOW;
1198+ int wait = 0;
1199+ DWORD ret;
1200+ WORD Err;
1201+ STARTUPINFO sui;
1202+ PROCESS_INFORMATION pi;
1203+ BOOL bRet;
1204+
1205+ memset(CurDir, 0, sizeof(CurDir));
1206+
1207+ Err = 0;
1208+ GetStrVal(Str,&Err);
1209+
1210+ if (CheckParameterGiven()) {
1211+ GetStrVal(Str2, &Err);
1212+ if (Err!=0) return Err;
1213+
1214+ if (_stricmp(Str2, "hide") == 0)
1215+ mode = SW_HIDE;
1216+ else if (_stricmp(Str2, "minimize") == 0)
1217+ mode = SW_MINIMIZE;
1218+ else if (_stricmp(Str2, "maximize") == 0)
1219+ mode = SW_MAXIMIZE;
1220+ else if (_stricmp(Str2, "show") == 0)
1221+ mode = SW_SHOW;
1222+ else
1223+ Err = ErrSyntax;
1224+
1225+ // get 3nd arg(optional) if given
1226+ if (CheckParameterGiven()) {
1227+ GetIntVal(&wait, &Err);
1228+ if (Err!=0) return Err;
1229+
1230+ // get 4th arg(optional) if given
1231+ if (CheckParameterGiven()) {
1232+ GetStrVal(CurDir, &Err);
1233+ if (Err!=0) return Err;
1234+ }
1235+ }
1236+ }
1237+
1238+ if ((Err==0) &&
1239+ ((strlen(Str)==0) || (GetFirstChar()!=0)))
1240+ Err = ErrSyntax;
1241+
1242+ if (Err!=0) return Err;
1243+
1244+ memset(&sui, 0, sizeof(STARTUPINFO));
1245+ sui.cb = sizeof(STARTUPINFO);
1246+ sui.wShowWindow = mode;
1247+ sui.dwFlags = STARTF_USESHOWWINDOW;
1248+ tc StrT = tc::fromUtf8(Str);
1249+ tc CurDirT = tc::fromUtf8(CurDir);
1250+ LPTSTR StrT_NC = (TCHAR *)(const TCHAR *)StrT;
1251+ if (CurDir[0] == 0)
1252+ bRet = CreateProcess(NULL, StrT_NC, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &sui, &pi);
1253+ else
1254+ bRet = CreateProcess(NULL, StrT_NC, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, CurDirT, &sui, &pi);
1255+ if (bRet == FALSE) {
1256+ // 実行できなかった場合、resultに-1を返す
1257+ SetResult(-1);
1258+#if 0
1259+ // エラーになる
1260+ Err = ErrCantExec;
1261+#endif
1262+ } else {
1263+ if (wait) {
1264+ // 実行 & wait指定
1265+ WaitForSingleObject(pi.hProcess, INFINITE);
1266+ GetExitCodeProcess(pi.hProcess, &ret);
1267+ SetResult(ret);
1268+ } else {
1269+ SetResult(0);
1270+ }
1271+ CloseHandle(pi.hThread);
1272+ CloseHandle(pi.hProcess);
1273+ }
1274+ return Err;
1275+}
1276+
1277+WORD TTLExecCmnd()
1278+{
1279+ WORD Err;
1280+ TStrVal NextLine;
1281+ BYTE b;
1282+
1283+ Err = 0;
1284+ GetStrVal(NextLine,&Err);
1285+ if (Err!=0) return Err;
1286+ if (GetFirstChar()!=0)
1287+ return ErrSyntax;
1288+
1289+ strncpy_s(LineBuff, sizeof(LineBuff),NextLine, _TRUNCATE);
1290+ LineLen = (WORD)strlen(LineBuff);
1291+ LinePtr = 0;
1292+ b = GetFirstChar();
1293+ LinePtr--;
1294+ ParseAgain = (b!=0) && (b!=':') && (b!=';');
1295+ return Err;
1296+}
1297+
1298+WORD TTLExit()
1299+{
1300+ if (GetFirstChar()==0)
1301+ {
1302+ if (! ExitBuffer())
1303+ TTLStatus = IdTTLEnd;
1304+ return 0;
1305+ }
1306+ else
1307+ return ErrSyntax;
1308+}
1309+
1310+WORD TTLExpandEnv()
1311+{
1312+ WORD Err;
1313+ TVarId VarId;
1314+ TStrVal deststr, srcptr;
1315+
1316+ Err = 0;
1317+ GetStrVar(&VarId, &Err);
1318+ if (Err!=0) return Err;
1319+
1320+ if (CheckParameterGiven()) { // expandenv strvar strval
1321+ GetStrVal(srcptr,&Err);
1322+ if ((Err==0) && (GetFirstChar()!=0))
1323+ Err = ErrSyntax;
1324+ if (Err!=0) {
1325+ return Err;
1326+ }
1327+
1328+ // ファイルパスに環境変数が含まれているならば、展開する。
1329+ ExpandEnvironmentStrings(srcptr, deststr, MaxStrLen);
1330+ SetStrVal(VarId, deststr);
1331+ }
1332+ else { // expandenv strvar
1333+ // ファイルパスに環境変数が含まれているならば、展開する。
1334+ ExpandEnvironmentStrings(StrVarPtr(VarId), deststr, MaxStrLen);
1335+ SetStrVal(VarId, deststr);
1336+ }
1337+
1338+ return Err;
1339+}
1340+
1341+WORD TTLFileClose()
1342+{
1343+ WORD Err;
1344+ int fhi; // handle index
1345+ HANDLE FH;
1346+
1347+ Err = 0;
1348+ GetIntVal(&fhi,&Err);
1349+ FH = HandleGet(fhi);
1350+ if ((Err==0) && (GetFirstChar()!=0))
1351+ Err = ErrSyntax;
1352+ if (Err!=0) return Err;
1353+ _lclose(FH);
1354+ HandleFree(fhi);
1355+ return Err;
1356+}
1357+
1358+WORD TTLFileConcat()
1359+{
1360+ WORD Err;
1361+ HANDLE FH1, FH2;
1362+ int c;
1363+ TStrVal FName1, FName2;
1364+ BYTE buf[1024];
1365+
1366+ Err = 0;
1367+ GetStrVal(FName1,&Err);
1368+ GetStrVal(FName2,&Err);
1369+ if ((Err==0) &&
1370+ ((strlen(FName1)==0) ||
1371+ (strlen(FName2)==0) ||
1372+ (GetFirstChar()!=0)))
1373+ Err = ErrSyntax;
1374+ if (Err!=0) {
1375+ SetResult(1);
1376+ return Err;
1377+ }
1378+
1379+ if (!GetAbsPath(FName1,sizeof(FName1))) {
1380+ SetResult(-1);
1381+ return Err;
1382+ }
1383+ if (!GetAbsPath(FName2,sizeof(FName2))) {
1384+ SetResult(-2);
1385+ return Err;
1386+ }
1387+ if (_stricmp(FName1,FName2)==0) {
1388+ SetResult(2);
1389+ return Err;
1390+ }
1391+
1392+ FH1 = _lopen(FName1,OF_WRITE);
1393+ if (FH1 == INVALID_HANDLE_VALUE)
1394+ FH1 = _lcreat(FName1,0);
1395+ if (FH1 == INVALID_HANDLE_VALUE) {
1396+ SetResult(3);
1397+ return Err;
1398+ }
1399+ _llseek(FH1,0,2);
1400+
1401+ FH2 = _lopen(FName2,OF_READ);
1402+ if (FH2 != INVALID_HANDLE_VALUE)
1403+ {
1404+ do {
1405+ c = _lread(FH2,&(buf[0]),sizeof(buf));
1406+ if (c>0)
1407+ _lwrite(FH1,&(buf[0]),c);
1408+ } while (c >= sizeof(buf));
1409+ _lclose(FH2);
1410+ }
1411+ _lclose(FH1);
1412+
1413+ SetResult(0);
1414+ return Err;
1415+}
1416+
1417+WORD TTLFileCopy()
1418+{
1419+ WORD Err;
1420+ TStrVal FName1, FName2;
1421+ BOOL ret;
1422+
1423+ Err = 0;
1424+ GetStrVal(FName1,&Err);
1425+ GetStrVal(FName2,&Err);
1426+ if ((Err==0) &&
1427+ ((strlen(FName1)==0) ||
1428+ (strlen(FName2)==0) ||
1429+ (GetFirstChar()!=0)))
1430+ Err = ErrSyntax;
1431+ if (Err!=0) {
1432+ SetResult(1);
1433+ return Err;
1434+ }
1435+
1436+ if (!GetAbsPath(FName1,sizeof(FName1))) {
1437+ SetResult(-1);
1438+ return Err;
1439+ }
1440+ if (!GetAbsPath(FName2,sizeof(FName2))) {
1441+ SetResult(-2);
1442+ return Err;
1443+ }
1444+ if (_stricmp(FName1, FName2) == 0) {
1445+ SetResult(-3);
1446+ return Err;
1447+ }
1448+
1449+ ret = CopyFile(FName1, FName2, FALSE);
1450+ if (ret == 0) {
1451+ SetResult(-4);
1452+ return Err;
1453+ }
1454+
1455+ SetResult(0);
1456+ return Err;
1457+}
1458+
1459+WORD TTLFileCreate()
1460+{
1461+ WORD Err;
1462+ TVarId VarId;
1463+ HANDLE FH;
1464+ int fhi;
1465+ TStrVal FName;
1466+
1467+ Err = 0;
1468+ GetIntVar(&VarId, &Err);
1469+ GetStrVal(FName, &Err);
1470+ if ((Err==0) &&
1471+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
1472+ Err = ErrSyntax;
1473+ if (Err!=0) {
1474+ SetResult(1);
1475+ return Err;
1476+ }
1477+
1478+ if (!GetAbsPath(FName,sizeof(FName))) {
1479+ SetIntVal(VarId, -1);
1480+ SetResult(-1);
1481+ return Err;
1482+ }
1483+ FH = _lcreat(FName,0);
1484+ if (FH == INVALID_HANDLE_VALUE) {
1485+ SetResult(2);
1486+ }
1487+ else {
1488+ SetResult(0);
1489+ }
1490+ fhi = HandlePut(FH);
1491+ SetIntVal(VarId, fhi);
1492+ if (fhi == -1) {
1493+ _lclose(FH);
1494+ }
1495+ return Err;
1496+}
1497+
1498+WORD TTLFileDelete()
1499+{
1500+ WORD Err;
1501+ TStrVal FName;
1502+
1503+ Err = 0;
1504+ GetStrVal(FName,&Err);
1505+ if ((Err==0) &&
1506+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
1507+ Err = ErrSyntax;
1508+ if (Err!=0) {
1509+ SetResult(1);
1510+ return Err;
1511+ }
1512+
1513+ if (!GetAbsPath(FName,sizeof(FName))) {
1514+ SetResult(-1);
1515+ return Err;
1516+ }
1517+
1518+ if (remove(FName) != 0) {
1519+ SetResult(-1);
1520+ }
1521+ else {
1522+ SetResult(0);
1523+ }
1524+
1525+ return Err;
1526+}
1527+
1528+WORD TTLFileMarkPtr()
1529+{
1530+ WORD Err;
1531+ int fhi;
1532+ HANDLE FH;
1533+ LONG pos;
1534+
1535+ Err = 0;
1536+ GetIntVal(&fhi,&Err);
1537+ FH = HandleGet(fhi);
1538+ if ((Err==0) && (GetFirstChar()!=0))
1539+ Err = ErrSyntax;
1540+ if (Err!=0) return Err;
1541+ pos = _llseek(FH,0,1); /* mark current pos */
1542+ if (pos == INVALID_SET_FILE_POINTER) {
1543+ pos = 0; // ?
1544+ }
1545+ FPointer[fhi] = pos;
1546+ return Err;
1547+}
1548+
1549+#if 0
1550+// add 'filenamebox' (2007.9.13 maya)
1551+WORD TTLFilenameBox()
1552+{
1553+ TStrVal Str1;
1554+ WORD Err, ValType;
1555+ TVarId VarId;
1556+ OPENFILENAME ofn;
1557+ char uimsg[MAX_UIMSG];
1558+ BOOL SaveFlag = FALSE;
1559+ TStrVal InitDir = "";
1560+ BOOL ret;
1561+
1562+ Err = 0;
1563+ GetStrVal(Str1,&Err);
1564+ if (Err!=0) return Err;
1565+
1566+ // get 2nd arg(optional) if given
1567+ if (CheckParameterGiven()) { // dialogtype
1568+ GetIntVal(&SaveFlag, &Err);
1569+ if (Err!=0) return Err;
1570+
1571+ // get 3rd arg(optional) if given
1572+ if (CheckParameterGiven()) { // initdir
1573+ GetStrVal(InitDir, &Err);
1574+ if (Err!=0) return Err;
1575+ }
1576+ }
1577+
1578+ if ((Err==0) && (GetFirstChar()!=0))
1579+ Err = ErrSyntax;
1580+ if (Err!=0) return Err;
1581+
1582+ SetInputStr("");
1583+ if (CheckVar("inputstr", &ValType, &VarId) &&
1584+ (ValType==TypString)) {
1585+ memset(&ofn, 0, sizeof(OPENFILENAME));
1586+ ofn.lStructSize = get_OPENFILENAME_SIZE();
1587+ ofn.hwndOwner = HMainWin;
1588+ ofn.lpstrTitle = Str1;
1589+ ofn.lpstrFile = StrVarPtr(VarId);
1590+ ofn.nMaxFile = MaxStrLen;
1591+ get_lang_msg("FILEDLG_ALL_FILTER", uimsg, sizeof(uimsg), "All(*.*)\\0*.*\\0\\0", UILanguageFile);
1592+ ofn.lpstrFilter = uimsg;
1593+ ofn.lpstrInitialDir = NULL;
1594+ if (strlen(InitDir) > 0) {
1595+ ofn.lpstrInitialDir = InitDir;
1596+ }
1597+ BringupWindow(HMainWin);
1598+ if (SaveFlag) {
1599+ ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1600+ ret = GetSaveFileName(&ofn);
1601+ }
1602+ else {
1603+ ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
1604+ ret = GetOpenFileName(&ofn);
1605+ }
1606+ SetResult(ret);
1607+ }
1608+ return Err;
1609+}
1610+#endif
1611+
1612+WORD TTLFileOpen()
1613+{
1614+ WORD Err;
1615+ TVarId VarId;
1616+ int fhi;
1617+ HANDLE FH;
1618+ int Append, ReadonlyFlag=0;
1619+ TStrVal FName;
1620+
1621+ Err = 0;
1622+ GetIntVar(&VarId, &Err);
1623+ GetStrVal(FName, &Err);
1624+ GetIntVal(&Append, &Err);
1625+ // get 4nd arg(optional) if given
1626+ if (CheckParameterGiven()) { // readonly
1627+ GetIntVal(&ReadonlyFlag, &Err);
1628+ }
1629+ if ((Err==0) &&
1630+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
1631+ Err = ErrSyntax;
1632+ if (Err!=0) return Err;
1633+
1634+ if (!GetAbsPath(FName,sizeof(FName))) {
1635+ SetIntVal(VarId, -1);
1636+ return Err;
1637+ }
1638+
1639+ if (ReadonlyFlag) {
1640+ FH = _lopen(FName,OF_READ);
1641+ }
1642+ else {
1643+ FH = _lopen(FName,OF_READWRITE);
1644+ if (FH == INVALID_HANDLE_VALUE)
1645+ FH = _lcreat(FName,0);
1646+ }
1647+ if (FH == INVALID_HANDLE_VALUE) {
1648+ SetIntVal(VarId, -1);
1649+ return Err;
1650+ }
1651+ fhi = HandlePut(FH);
1652+ if (fhi == -1) {
1653+ SetIntVal(VarId, -1);
1654+ _lclose(FH);
1655+ return Err;
1656+ }
1657+ SetIntVal(VarId, fhi);
1658+ if (Append!=0) {
1659+ _llseek(FH, 0, 2/*FILE_END*/);
1660+ }
1661+ return 0; // no error
1662+}
1663+
1664+// Format: filelock <file handle> [<timeout>]
1665+// (2012.4.19 yutaka)
1666+WORD TTLFileLock()
1667+{
1668+ WORD Err;
1669+ HANDLE FH;
1670+ int fhi;
1671+ int timeoutI;
1672+ DWORD timeout;
1673+ int result;
1674+ BOOL ret;
1675+ DWORD dwStart;
1676+
1677+ Err = 0;
1678+ GetIntVal(&fhi,&Err);
1679+ FH = HandleGet(fhi);
1680+ if (Err!=0) return Err;
1681+
1682+ timeout = -1; // 無限大
1683+ if (CheckParameterGiven()) {
1684+ GetIntVal(&timeoutI, &Err);
1685+ if (Err!=0) return Err;
1686+ }
1687+ timeout = timeoutI * 1000;
1688+
1689+ result = 1; // error
1690+ dwStart = GetTickCount();
1691+ do {
1692+ ret = LockFile(FH, 0, 0, (DWORD)-1, (DWORD)-1);
1693+ if (ret != 0) { // ロック成功
1694+ result = 0; // success
1695+ break;
1696+ }
1697+ Sleep(1);
1698+ } while (GetTickCount() - dwStart < (timeout*1000));
1699+
1700+ SetResult(result);
1701+ return Err;
1702+}
1703+
1704+// Format: fileunlock <file handle>
1705+// (2012.4.19 yutaka)
1706+WORD TTLFileUnLock()
1707+{
1708+ WORD Err;
1709+ HANDLE FH;
1710+ int fhi;
1711+ BOOL ret;
1712+
1713+ Err = 0;
1714+ GetIntVal(&fhi,&Err);
1715+ FH = HandleGet(fhi);
1716+ if ((Err==0) && (GetFirstChar()!=0))
1717+ Err = ErrSyntax;
1718+ if (Err!=0) return Err;
1719+
1720+ ret = UnlockFile(FH, 0, 0, (DWORD)-1, (DWORD)-1);
1721+ if (ret != 0) { // アンロック成功
1722+ SetResult(0);
1723+ } else {
1724+ SetResult(1);
1725+ }
1726+
1727+ return Err;
1728+}
1729+
1730+WORD TTLFileReadln()
1731+{
1732+ WORD Err;
1733+ TVarId VarId;
1734+ int fhi;
1735+ HANDLE FH;
1736+ int i, c;
1737+ TStrVal Str;
1738+ BOOL EndFile, EndLine;
1739+ BYTE b;
1740+
1741+ Err = 0;
1742+ GetIntVal(&fhi, &Err);
1743+ FH = HandleGet(fhi);
1744+ GetStrVar(&VarId, &Err);
1745+ if ((Err==0) && (GetFirstChar()!=0))
1746+ Err = ErrSyntax;
1747+ if (Err!=0) return Err;
1748+
1749+ i = 0;
1750+ EndLine = FALSE;
1751+ EndFile = TRUE;
1752+ do {
1753+ c = _lread(FH, &b, 1);
1754+ if (c>0) EndFile = FALSE;
1755+ if (c==1) {
1756+ switch (b) {
1757+ case 0x0d:
1758+ c = _lread(FH, &b, 1);
1759+ if ((c==1) && (b!=0x0a))
1760+ _llseek(FH, -1, 1);
1761+ EndLine = TRUE;
1762+ break;
1763+ case 0x0a: EndLine = TRUE; break;
1764+ default:
1765+ if (i<MaxStrLen-1)
1766+ {
1767+ Str[i] = b;
1768+ i++;
1769+ }
1770+ }
1771+ }
1772+ } while ((c>=1) && (! EndLine));
1773+
1774+ if (EndFile)
1775+ SetResult(1);
1776+ else
1777+ SetResult(0);
1778+
1779+ Str[i] = 0;
1780+ SetStrVal(VarId, Str);
1781+ return Err;
1782+}
1783+
1784+
1785+// Format: fileread <file handle> <read byte> <strvar>
1786+// 指定したバイト数だけファイルから読み込む。
1787+// ただし、<read byte>は 1〜255 まで。
1788+// (2006.11.1 yutaka)
1789+WORD TTLFileRead()
1790+{
1791+ WORD Err;
1792+ TVarId VarId;
1793+ int fhi;
1794+ HANDLE FH;
1795+ int i, c;
1796+ int ReadByte; // 読み込むバイト数
1797+ TStrVal Str;
1798+ BOOL EndFile, EndLine;
1799+ BYTE b;
1800+
1801+ Err = 0;
1802+ GetIntVal(&fhi,&Err);
1803+ FH = HandleGet(fhi);
1804+ GetIntVal(&ReadByte,&Err);
1805+ GetStrVar(&VarId,&Err);
1806+ if ((Err==0) && (GetFirstChar()!=0))
1807+ Err = ErrSyntax;
1808+ if ((Err==0) && (ReadByte < 1 || ReadByte > MaxStrLen-1)) // 範囲チェック
1809+ Err = ErrSyntax;
1810+ if (Err!=0) return Err;
1811+
1812+ EndLine = FALSE;
1813+ EndFile = FALSE;
1814+ for (i = 0 ; i < ReadByte ; i++) {
1815+ c = _lread(FH,&b,1);
1816+ if (c <= 0) { // EOF
1817+ EndFile = TRUE;
1818+ break;
1819+ }
1820+ if (i<MaxStrLen-1)
1821+ {
1822+ Str[i] = b;
1823+ }
1824+ }
1825+
1826+ if (EndFile)
1827+ SetResult(1);
1828+ else
1829+ SetResult(0);
1830+
1831+ Str[i] = 0;
1832+ SetStrVal(VarId,Str);
1833+ return Err;
1834+}
1835+
1836+
1837+WORD TTLFileRename()
1838+{
1839+ WORD Err;
1840+ TStrVal FName1, FName2;
1841+
1842+ Err = 0;
1843+ GetStrVal(FName1,&Err);
1844+ GetStrVal(FName2,&Err);
1845+ if ((Err==0) &&
1846+ ((strlen(FName1)==0) ||
1847+ (strlen(FName2)==0) ||
1848+ (GetFirstChar()!=0)))
1849+ Err = ErrSyntax;
1850+ if (Err!=0) {
1851+ SetResult(1);
1852+ return Err;
1853+ }
1854+ if (_stricmp(FName1,FName2)==0) {
1855+ SetResult(2);
1856+ return Err;
1857+ }
1858+
1859+ if (!GetAbsPath(FName1,sizeof(FName1))) {
1860+ SetResult(-1);
1861+ return Err;
1862+ }
1863+ if (!GetAbsPath(FName2,sizeof(FName2))) {
1864+ SetResult(-2);
1865+ return Err;
1866+ }
1867+ if (rename(FName1,FName2) != 0) {
1868+ // リネームに失敗したら、エラーで返す。
1869+ SetResult(-3);
1870+ return Err;
1871+ }
1872+
1873+ SetResult(0);
1874+ return Err;
1875+}
1876+
1877+WORD TTLFileSearch()
1878+{
1879+ WORD Err;
1880+ TStrVal FName;
1881+
1882+ Err = 0;
1883+ GetStrVal(FName,&Err);
1884+ if ((Err==0) &&
1885+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
1886+ Err = ErrSyntax;
1887+ if (Err!=0) return Err;
1888+
1889+ GetAbsPath(FName,sizeof(FName));
1890+ if (DoesFileExist(FName))
1891+ SetResult(1);
1892+ else
1893+ SetResult(0);
1894+ return Err;
1895+}
1896+
1897+WORD TTLFileSeek()
1898+{
1899+ WORD Err;
1900+ int fhi;
1901+ HANDLE FH;
1902+ int i, j;
1903+
1904+ Err = 0;
1905+ GetIntVal(&fhi,&Err);
1906+ FH = HandleGet(fhi);
1907+ GetIntVal(&i,&Err);
1908+ GetIntVal(&j,&Err);
1909+ if ((Err==0) && (GetFirstChar()!=0))
1910+ Err = ErrSyntax;
1911+ if (Err!=0) return Err;
1912+ _llseek(FH,i,j);
1913+ return Err;
1914+}
1915+
1916+WORD TTLFileSeekBack()
1917+{
1918+ WORD Err;
1919+ int fhi;
1920+ HANDLE FH;
1921+
1922+ Err = 0;
1923+ GetIntVal(&fhi,&Err);
1924+ FH = HandleGet(fhi);
1925+ if ((Err==0) && (GetFirstChar()!=0))
1926+ Err = ErrSyntax;
1927+ if (Err!=0) return Err;
1928+ /* move back to the marked pos */
1929+ _llseek(FH,FPointer[fhi],0);
1930+ return Err;
1931+}
1932+
1933+WORD TTLFileStat()
1934+{
1935+ WORD Err;
1936+ TVarId SizeVarId, TimeVarId, DrvVarId;
1937+ TStrVal FName, TimeStr, DrvStr;
1938+ struct _stat st;
1939+ int ret;
1940+ int result = -1;
1941+ struct tm *tmp;
1942+
1943+ Err = 0;
1944+ GetStrVal(FName,&Err);
1945+ if ((Err==0) &&
1946+ (strlen(FName)==0))
1947+ Err = ErrSyntax;
1948+ if (Err!=0) return Err;
1949+
1950+ if (!GetAbsPath(FName,sizeof(FName))) {
1951+ goto end;
1952+ }
1953+
1954+ ret = _stat(FName, &st);
1955+ if (ret != 0) {
1956+ goto end;
1957+ }
1958+
1959+ if (CheckParameterGiven()) {
1960+ GetIntVar(&SizeVarId,&Err);
1961+ if (Err!=0) return Err;
1962+ SetIntVal(SizeVarId, st.st_size);
1963+ }
1964+
1965+ if (CheckParameterGiven()) {
1966+ GetStrVar(&TimeVarId,&Err);
1967+ if (Err!=0) return Err;
1968+ tmp = localtime(&st.st_mtime);
1969+ strftime(TimeStr, sizeof(TimeStr), "%Y-%m-%d %H:%M:%S", tmp);
1970+ SetStrVal(TimeVarId, TimeStr);
1971+ }
1972+
1973+ if (CheckParameterGiven()) {
1974+ GetStrVar(&DrvVarId,&Err);
1975+ if (Err!=0) return Err;
1976+ _snprintf_s(DrvStr, sizeof(DrvStr), _TRUNCATE, "%c", st.st_dev + 'A');
1977+ SetStrVal(DrvVarId, DrvStr);
1978+ }
1979+
1980+ result = 0;
1981+
1982+end:
1983+ SetResult(result);
1984+
1985+ return Err;
1986+}
1987+
1988+WORD TTLFileStrSeek()
1989+{
1990+ WORD Err;
1991+ int fhi;
1992+ HANDLE FH;
1993+ int Len, i, c;
1994+ TStrVal Str;
1995+ BYTE b;
1996+ long int pos;
1997+
1998+ Err = 0;
1999+ GetIntVal(&fhi,&Err);
2000+ FH = HandleGet(fhi);
2001+ GetStrVal(Str,&Err);
2002+ if ((Err==0) &&
2003+ ((strlen(Str)==0) || (GetFirstChar()!=0)))
2004+ Err = ErrSyntax;
2005+ if (Err!=0) return Err;
2006+ pos = _llseek(FH,0,1);
2007+ if (pos == INVALID_SET_FILE_POINTER) return Err;
2008+
2009+ Len = strlen(Str);
2010+ i = 0;
2011+ do {
2012+ c = _lread(FH,&b,1);
2013+ if (c==1)
2014+ {
2015+ if (b==(BYTE)Str[i])
2016+ i++;
2017+ else if (i>0) {
2018+ i = 0;
2019+ if (b==(BYTE)Str[0])
2020+ i = 1;
2021+ }
2022+ }
2023+ } while ((c>=1) && (i!=Len));
2024+ if (i==Len)
2025+ SetResult(1);
2026+ else {
2027+ SetResult(0);
2028+ _llseek(FH,pos,0);
2029+ }
2030+ return Err;
2031+}
2032+
2033+WORD TTLFileStrSeek2()
2034+{
2035+ WORD Err;
2036+ int fhi;
2037+ HANDLE FH;
2038+ int Len, i, c;
2039+ TStrVal Str;
2040+ BYTE b;
2041+ long int pos, pos2;
2042+ BOOL Last;
2043+
2044+ Err = 0;
2045+ GetIntVal(&fhi,&Err);
2046+ FH = HandleGet(fhi);
2047+ GetStrVal(Str,&Err);
2048+ if ((Err==0) &&
2049+ ((strlen(Str)==0) || (GetFirstChar()!=0)))
2050+ Err = ErrSyntax;
2051+ if (Err!=0) return Err;
2052+ pos = _llseek(FH,0,1);
2053+ if (pos == INVALID_SET_FILE_POINTER) return Err;
2054+
2055+ Len = strlen(Str);
2056+ i = 0;
2057+ pos2 = pos;
2058+ do {
2059+ Last = (pos2<=0);
2060+ c = _lread(FH,&b,1);
2061+ pos2 = _llseek(FH,-2,1);
2062+ if (c==1)
2063+ {
2064+ if (b==(BYTE)Str[Len-1-i])
2065+ i++;
2066+ else if (i>0) {
2067+ i = 0;
2068+ if (b==(BYTE)Str[Len-1])
2069+ i = 1;
2070+ }
2071+ }
2072+ } while (!Last && (i!=Len));
2073+ if (i==Len) {
2074+ // ファイルの1バイト目がヒットすると、ファイルポインタが突き破って
2075+ // INVALID_SET_FILE_POINTER になるので、
2076+ // ゼロオフセットになるように調整する。(2008.10.10 yutaka)
2077+ if (pos2 == INVALID_SET_FILE_POINTER)
2078+ _llseek(FH, 0, 0);
2079+ SetResult(1);
2080+ } else {
2081+ SetResult(0);
2082+ _llseek(FH,pos,0);
2083+ }
2084+ return Err;
2085+}
2086+
2087+WORD TTLFileTruncate()
2088+{
2089+ WORD Err;
2090+ TStrVal FName;
2091+ int result = -1;
2092+ int TruncByte;
2093+ int fh = -1;
2094+ int ret;
2095+
2096+ Err = 0;
2097+ GetStrVal(FName,&Err);
2098+ if ((Err==0) &&
2099+ (strlen(FName)==0))
2100+ Err = ErrSyntax;
2101+ if (Err!=0) return Err;
2102+
2103+ if (!GetAbsPath(FName,sizeof(FName))) {
2104+ goto end;
2105+ }
2106+
2107+ if (CheckParameterGiven()) {
2108+ GetIntVal(&TruncByte,&Err);
2109+ if (Err!=0) return Err;
2110+ } else {
2111+ Err = ErrSyntax;
2112+ goto end;
2113+ }
2114+
2115+ // ファイルを指定したサイズで切り詰める。
2116+ ret = _sopen_s( &fh, FName, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE );
2117+ if (ret != 0) {
2118+ goto end;
2119+ }
2120+ ret = _chsize_s(fh, TruncByte);
2121+ if (ret != 0) {
2122+ goto end;
2123+ }
2124+
2125+ result = 0;
2126+ Err = 0;
2127+
2128+end:
2129+ SetResult(result);
2130+
2131+ if (fh != -1)
2132+ _close(fh);
2133+
2134+ return Err;
2135+}
2136+
2137+WORD TTLFileWrite(BOOL addCRLF)
2138+{
2139+ WORD Err, P;
2140+ int fhi;
2141+ HANDLE FH;
2142+ int Val;
2143+ TStrVal Str;
2144+
2145+ Err = 0;
2146+ GetIntVal(&fhi, &Err);
2147+ FH = HandleGet(fhi);
2148+ if (Err) return Err;
2149+
2150+ P = LinePtr;
2151+ GetStrVal(Str, &Err);
2152+ if (!Err) {
2153+ if (GetFirstChar())
2154+ return ErrSyntax;
2155+
2156+ _lwrite(FH, Str, strlen(Str));
2157+ }
2158+ else if (Err == ErrTypeMismatch) {
2159+ Err = 0;
2160+ LinePtr = P;
2161+ GetIntVal(&Val, &Err);
2162+ if (Err) return Err;
2163+ if (GetFirstChar())
2164+ return ErrSyntax;
2165+
2166+ Str[0] = Val & 0xff;
2167+ _lwrite(FH, Str, 1);
2168+ }
2169+ else {
2170+ return Err;
2171+ }
2172+
2173+ if (addCRLF) {
2174+ _lwrite(FH,"\015\012",2);
2175+ }
2176+ return 0;
2177+}
2178+
2179+WORD TTLFindClose()
2180+{
2181+ WORD Err;
2182+ int DH;
2183+
2184+ Err = 0;
2185+ GetIntVal(&DH,&Err);
2186+ if ((Err==0) && (GetFirstChar()!=0))
2187+ Err = ErrSyntax;
2188+ if (Err!=0) return Err;
2189+
2190+ if ((DH>=0) && (DH<NumDirHandle) &&
2191+ (DirHandle[DH]!=-1L))
2192+ {
2193+ _findclose(DirHandle[DH]);
2194+ DirHandle[DH] = -1L;
2195+ }
2196+ return Err;
2197+}
2198+
2199+WORD TTLFindFirst()
2200+{
2201+ TVarId DH, Name;
2202+ WORD Err;
2203+ TStrVal Dir;
2204+ int i;
2205+ struct _finddata_t data;
2206+
2207+ Err = 0;
2208+ GetIntVar(&DH,&Err);
2209+ GetStrVal(Dir,&Err);
2210+ GetStrVar(&Name,&Err);
2211+ if ((Err==0) && (GetFirstChar()!=0))
2212+ Err = ErrSyntax;
2213+ if (Err!=0) return Err;
2214+
2215+ if (Dir[0]==0) strncpy_s(Dir, sizeof(Dir),"*.*", _TRUNCATE);
2216+ GetAbsPath(Dir,sizeof(Dir));
2217+ i = 0;
2218+ while ((i<NumDirHandle) &&
2219+ (DirHandle[i]!=-1L))
2220+ i++;
2221+ if (i<NumDirHandle)
2222+ {
2223+ DirHandle[i] = _findfirst(Dir,&data);
2224+ if (DirHandle[i]!=-1L)
2225+ SetStrVal(Name,data.name);
2226+ else
2227+ i = -1;
2228+ }
2229+ else
2230+ i = -1;
2231+
2232+ SetIntVal(DH,i);
2233+ if (i<0)
2234+ {
2235+ SetResult(0);
2236+ SetStrVal(Name,"");
2237+ }
2238+ else
2239+ SetResult(1);
2240+ return Err;
2241+}
2242+
2243+WORD TTLFindNext()
2244+{
2245+ TVarId Name;
2246+ WORD Err;
2247+ int DH;
2248+ struct _finddata_t data;
2249+
2250+ Err = 0;
2251+ GetIntVal(&DH,&Err);
2252+ GetStrVar(&Name,&Err);
2253+ if ((Err==0) && (GetFirstChar()!=0))
2254+ Err = ErrSyntax;
2255+ if (Err!=0) return Err;
2256+
2257+ if ((DH>=0) && (DH<NumDirHandle) &&
2258+ (DirHandle[DH]!=-1L) &&
2259+ (_findnext(DirHandle[DH],&data)==0))
2260+ {
2261+ SetStrVal(Name,data.name);
2262+ SetResult(1);
2263+ }
2264+ else {
2265+ SetStrVal(Name,"");
2266+ SetResult(0);
2267+ }
2268+ return Err;
2269+}
2270+
2271+WORD TTLFlushRecv()
2272+{
2273+ if (GetFirstChar()!=0)
2274+ return ErrSyntax;
2275+ FlushRecv();
2276+ return 0;
2277+}
2278+
2279+WORD TTLFolderCreate()
2280+{
2281+ WORD Err;
2282+ TStrVal FName;
2283+
2284+ Err = 0;
2285+ GetStrVal(FName,&Err);
2286+ if ((Err==0) &&
2287+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
2288+ Err = ErrSyntax;
2289+ if (Err!=0) {
2290+ SetResult(1);
2291+ return Err;
2292+ }
2293+
2294+ if (!GetAbsPath(FName,sizeof(FName))) {
2295+ SetResult(-1);
2296+ return Err;
2297+ }
2298+
2299+ if (CreateDirectory(FName, NULL) == 0) {
2300+ SetResult(2);
2301+ }
2302+ else {
2303+ SetResult(0);
2304+ }
2305+ return Err;
2306+}
2307+
2308+WORD TTLFolderDelete()
2309+{
2310+ WORD Err;
2311+ TStrVal FName;
2312+
2313+ Err = 0;
2314+ GetStrVal(FName,&Err);
2315+ if ((Err==0) &&
2316+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
2317+ Err = ErrSyntax;
2318+ if (Err!=0) {
2319+ SetResult(1);
2320+ return Err;
2321+ }
2322+
2323+ if (!GetAbsPath(FName,sizeof(FName))) {
2324+ SetResult(-1);
2325+ return Err;
2326+ }
2327+
2328+ if (RemoveDirectory(FName) == 0) {
2329+ SetResult(2);
2330+ }
2331+ else {
2332+ SetResult(0);
2333+ }
2334+ return Err;
2335+}
2336+
2337+WORD TTLFolderSearch()
2338+{
2339+ WORD Err;
2340+ TStrVal FName;
2341+
2342+ Err = 0;
2343+ GetStrVal(FName,&Err);
2344+ if ((Err==0) &&
2345+ ((strlen(FName)==0) || (GetFirstChar()!=0)))
2346+ Err = ErrSyntax;
2347+ if (Err!=0) return Err;
2348+
2349+ GetAbsPath(FName,sizeof(FName));
2350+ if (DoesFolderExist(FName)) {
2351+ SetResult(1);
2352+ }
2353+ else {
2354+ SetResult(0);
2355+ }
2356+ return Err;
2357+}
2358+
2359+WORD TTLFor()
2360+{
2361+ WORD Err;
2362+ TVarId VarId;
2363+ int ValStart, ValEnd, i;
2364+
2365+ Err = 0;
2366+ GetIntVar(&VarId,&Err);
2367+ GetIntVal(&ValStart,&Err);
2368+ GetIntVal(&ValEnd,&Err);
2369+ if ((Err==0) && (GetFirstChar()!=0))
2370+ Err = ErrSyntax;
2371+ if (Err!=0) return Err;
2372+
2373+ if (! CheckNext())
2374+ { // first time
2375+ Err = SetForLoop();
2376+ if (Err==0)
2377+ {
2378+ SetIntVal(VarId,ValStart);
2379+ i = CopyIntVal(VarId);
2380+ if (i==ValEnd)
2381+ LastForLoop();
2382+ }
2383+ }
2384+ else { // return from 'next'
2385+ i = CopyIntVal(VarId);
2386+ if (i<ValEnd)
2387+ i++;
2388+ else if (i>ValEnd)
2389+ i--;
2390+ SetIntVal(VarId,i);
2391+ if (i==ValEnd)
2392+ LastForLoop();
2393+ }
2394+ return Err;
2395+}
2396+
2397+WORD TTLGetDir()
2398+{
2399+ WORD Err;
2400+ TVarId VarId;
2401+ TStrVal Str;
2402+
2403+ Err = 0;
2404+ GetStrVar(&VarId,&Err);
2405+ if ((Err==0) && (GetFirstChar()!=0))
2406+ Err = ErrSyntax;
2407+ if (Err!=0) return Err;
2408+
2409+ TTMGetDir(Str, sizeof(Str));
2410+ SetStrVal(VarId,Str);
2411+ return Err;
2412+}
2413+
2414+WORD TTLGetEnv()
2415+{
2416+ WORD Err;
2417+ TVarId VarId;
2418+ TStrVal Str;
2419+ PCHAR Str2;
2420+
2421+ Err = 0;
2422+ GetStrVal(Str,&Err);
2423+ GetStrVar(&VarId,&Err);
2424+ if ((Err==0) && (GetFirstChar()!=0))
2425+ Err = ErrSyntax;
2426+ if (Err!=0) return Err;
2427+
2428+ Str2 = getenv(Str);
2429+
2430+ if (Str2!=NULL)
2431+ SetStrVal(VarId,Str2);
2432+ else
2433+ SetStrVal(VarId,"");
2434+ return Err;
2435+}
2436+
2437+WORD TTLGetFileAttr()
2438+{
2439+ WORD Err;
2440+ TStrVal Filename;
2441+
2442+ Err = 0;
2443+ GetStrVal(Filename,&Err);
2444+ if ((Err==0) && (GetFirstChar()!=0))
2445+ Err = ErrSyntax;
2446+ if (Err!=0) return Err;
2447+
2448+ GetAbsPath(Filename, sizeof(Filename));
2449+ SetResult(GetFileAttributes(Filename));
2450+
2451+ return Err;
2452+}
2453+
2454+WORD TTLGetHostname()
2455+{
2456+ WORD Err;
2457+ TVarId VarId;
2458+ char Str[MaxStrLen];
2459+
2460+ Err = 0;
2461+ GetStrVar(&VarId,&Err);
2462+ if ((Err==0) && (GetFirstChar()!=0))
2463+ Err = ErrSyntax;
2464+ if ((Err==0) && (! Linked))
2465+ Err = ErrLinkFirst;
2466+ if (Err!=0) return Err;
2467+
2468+ Err = GetTTParam(CmdGetHostname,Str,sizeof(Str));
2469+ if (Err==0)
2470+ SetStrVal(VarId,Str);
2471+ return Err;
2472+}
2473+
2474+#define MAX_IPADDR 30
2475+/*
2476+ strdim ipaddr 10
2477+ getipv4addr ipaddr num
2478+ */
2479+WORD TTLGetIPv4Addr()
2480+{
2481+ WORD Err;
2482+ TVarId VarId, VarId2, id;
2483+ WSADATA ws;
2484+ INTERFACE_INFO info[MAX_IPADDR];
2485+ SOCKET sock;
2486+ DWORD socknum;
2487+ int num, result, arysize;
2488+ int i, n;
2489+ IN_ADDR addr;
2490+
2491+ Err = 0;
2492+ GetStrAryVar(&VarId,&Err);
2493+ GetIntVar(&VarId2, &Err);
2494+ if ((Err==0) && (GetFirstChar()!=0))
2495+ Err = ErrSyntax;
2496+ if (Err!=0) return Err;
2497+
2498+ // 自分自身の全IPv4アドレスを取得する。
2499+ if (WSAStartup(MAKEWORD(2,2), &ws) != 0) {
2500+ SetResult(-1);
2501+ SetIntVal(VarId2, 0);
2502+ return Err;
2503+ }
2504+ arysize = GetStrAryVarSize(VarId);
2505+ num = 0;
2506+ result = 1;
2507+ sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
2508+ if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, info, sizeof(info), &socknum, NULL, NULL) != SOCKET_ERROR) {
2509+ n = socknum / sizeof(info[0]);
2510+ for (i = 0 ; i < n ; i++) {
2511+ if ((info[i].iiFlags & IFF_UP) == 0)
2512+ continue;
2513+ if ((info[i].iiFlags & IFF_LOOPBACK) != 0)
2514+ continue;
2515+ addr = info[i].iiAddress.AddressIn.sin_addr;
2516+
2517+ if (num < arysize) {
2518+ id = GetStrVarFromArray(VarId, num, &Err);
2519+ SetStrVal(id, inet_ntoa(addr));
2520+ }
2521+ else {
2522+ result = 0;
2523+ }
2524+ num++;
2525+ }
2526+ }
2527+ closesocket(sock);
2528+ WSACleanup();
2529+
2530+ SetResult(result);
2531+ SetIntVal(VarId2, num);
2532+
2533+ return Err;
2534+}
2535+
2536+
2537+// IPv6アドレスを文字列に変換する。
2538+static void myInetNtop(int Family, char *pAddr, char *pStringBuf, size_t StringBufSize)
2539+{
2540+ int i;
2541+ char s[16];
2542+ unsigned int val;
2543+
2544+ pStringBuf[0] = '\0';
2545+ for (i = 0 ; i < 16 ; i++) {
2546+ val = (pAddr[i]) & 0xFF;
2547+ _snprintf_s(s, sizeof(s), _TRUNCATE, "%02x", val);
2548+ strncat_s(pStringBuf, StringBufSize, s, _TRUNCATE);
2549+ if (i != 15 && (i & 1))
2550+ strncat_s(pStringBuf, StringBufSize, ":", _TRUNCATE);