2 ; tera term mecrisp-stellaris forth upload helper
3 ; Copyright (C) 2015 Jean Jonethal
5 ; This program is free software: you can redistribute it and/or modify
6 ; it under the terms of the GNU General Public License as published by
7 ; the Free Software Foundation, either version 3 of the License, or
8 ; (at your option) any later version.
10 ; This program is distributed in the hope that it will be useful,
11 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ; GNU General Public License for more details.
15 ; You should have received a copy of the GNU General Public License
16 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ; small upload macro for uploading forth source via tera term tested with version 4.88
19 ; Tera Term website http://ttssh2.osdn.jp/index.html.en
20 ; tera term supports call stack up to 10 levels on my laptop so using iterative approach here
22 ; upload source file to target line by line and waits for "ok." after transmission of line.
23 ; supports nesting include with pathname
24 ; line containing include directive is not sent to target
25 ; max source line length is 511
26 ; include path\filename.ext
28 ; example project.txt :
30 ; include ..\common\disassembler-m3.txt
34 ; "require" - support for single inclusion
35 ; error handling for MAXLEVEL
36 ; path handling relative to root file
40 MAXLEVEL = 20 ; max nesting level up to 65536 levels might be supported by tera term
41 MAX_LINE_LENGTH = 511 ; max string/line length supported by tera term
42 INCLUDE_PATTERN = "include\s+([\.\w/:\\-]+)" ; this pattern marks our include file
43 SOURCE_PATTERN = "^([^\\]+)" ; skip line comments
44 timeout = 5 ; you might tune this for longer response times
45 mtimeout = 500 ; wait 500 ms for "ok." response
46 infilehandle = -1 ; current file handle
47 fname = "" ; current filename
48 newfileHandle = -1 ; store the new file handle
49 ; parameter stack ; stack rised upwards
50 level = 1 ; current include level
51 intdim FileHandleStack MAXLEVEL ; file handle stack
52 intdim clp MAXLEVEL ; line position stack - unused at the moment
53 strdim LineStack MAXLEVEL ; line stack - unused at the moment
54 strdim FileNameStack MAXLEVEL ; filename stack
57 ; check for macro parameter
58 if paramcnt = 2 then ; if there is a macro parameter use it as input file name
62 if result = 0 then ; if there is no valid parameter open file dialog for selection
63 filenamebox "select file" 0
65 messagebox fname "opened"
68 dirname prjdir fname ; dirname <strvar> <path> setup current directory to fname
69 setdir prjdir ; setdir <dir> set macrodir to directory containing file fname
75 fileopen infilehandle fname 0 1 ; fileopen <file handle> <filename> <append flag> [<readonly flag>]
76 if infilehandle <> -1 then ; if file open successful
77 level = 1 ; we start at level 1
78 FileNameStack[level] = fname ;
79 FileHandleStack[level] = infilehandle
80 while 1 ; infinite loop !
81 infilehandle = FileHandleStack[level] ; update current file handle
82 filereadln infilehandle line ; filereadln <file handle> <strvar> get next line from this file
85 else ; end of file (it is the last line of file)
86 call levelback ; close file , back one nesting level
87 if level < 1 then ; upper most file ended
88 messagebox fname "Finished" ; notify user
97 ; close file and switch to previous level
99 fileclose infilehandle
101 if level > 0 then ; back to previous level
102 infilehandle = FileHandleStack[level]
103 fname = FileNameStack[level]
106 FileHandleStack[level] = -1
111 ; scan every line for include filename or send to target and wait for "ok."
112 ; line contains current line to be scanned
114 strmatch line INCLUDE_PATTERN ; scan the line for include pattern
116 fname = groupmatchstr1 ; found new include line
120 send line #10 ; send line + LF
126 ; start a new include level when open successfull
127 ; fname contains new include filename
128 ; notify user if failed to open included file
130 fileopen newfileHandle fname 0 1
131 if newfileHandle <> -1 then ; new include file opened
133 infilehandle = newfileHandle
134 FileNameStack[level] = fname
135 FileHandleStack[level] = infilehandle
137 messagebox fname "open failed" ; notify user about failed file
142 ; dont transfer comments
143 strmatch line SOURCE_PATTERN ; skip comments
145 line = groupmatchstr1