OSDN Git Service

bug bug bug...
[fast-forth/master.git] / config / scite / AS_MSP430 / upload_include.ttl
1 ;
2 ;    tera term mecrisp-stellaris forth upload helper
3 ;    Copyright (C) 2015  Jean Jonethal
4 ;
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.
9 ;
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.
14 ;
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/>.
17
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
21
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
27
28 ; example project.txt :
29 ; include dump.txt
30 ; include ..\common\disassembler-m3.txt
31 ; include demo2.fth
32
33 ; TODO:
34 ; "require" - support for single inclusion
35 ; error handling for MAXLEVEL
36 ; path handling relative to root file
37 ; test path handling
38
39 ; global definitions
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
55
56
57 ; check for macro parameter
58 if paramcnt = 2 then                          ; if there is a macro parameter use it as input file name
59         fname = param2
60 endif
61 strlen fname 
62 if result = 0 then                            ; if there is no valid parameter open file dialog for selection 
63         filenamebox "select file" 0
64         fname = inputstr
65   messagebox fname "opened"
66 endif
67
68 dirname prjdir fname                          ; dirname <strvar> <path>     setup current directory to fname
69 setdir prjdir                                 ; setdir <dir>    set macrodir to directory containing file fname 
70
71 call uploading
72 goto ende
73
74 :uploading
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
83       if result = 0 then
84         call processLine
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 
89           break
90         endif
91       endif
92     endwhile
93   endif
94 return
95
96 :levelback
97 ; close file and switch to previous level
98
99   fileclose infilehandle
100   level = level - 1
101   if level > 0 then                           ; back to previous level
102     infilehandle = FileHandleStack[level]
103     fname        = FileNameStack[level]
104   else
105     infilehandle = -1
106     FileHandleStack[level]   = -1
107   endif
108 return
109
110 :processLine                                    
111 ; scan every line for include filename or send to target and wait for "ok."
112 ; line contains current line to be scanned
113
114   strmatch line INCLUDE_PATTERN               ; scan the line for include pattern
115   if result > 0 then
116     fname = groupmatchstr1                    ; found new include line
117     call openNewFile
118   else
119     call skipComment
120     send line #10                           ; send line + LF
121     wait "ok."
122   endif
123 return
124
125 :openNewFile
126 ; start a new include level when open successfull
127 ; fname contains new include filename
128 ; notify user if failed to open included file
129
130   fileopen newfileHandle fname 0 1
131   if newfileHandle <> -1 then                 ; new include file opened
132     level        = level + 1
133     infilehandle = newfileHandle
134     FileNameStack[level]   = fname
135     FileHandleStack[level]   = infilehandle
136   else
137     messagebox fname "open failed"            ; notify user about failed file
138   endif
139 return
140
141 :skipComment
142 ; dont transfer comments
143   strmatch line SOURCE_PATTERN              ; skip comments
144   if result > 0 then
145     line = groupmatchstr1
146   else
147     line = ""
148   endif
149 return
150
151
152 :ende