aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/tools/amforth-upload.py
blob: 18069dc150f9c7c67e15fa6738b77d63fd72fd03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
# amforth-upload.py
#
#   uploads text files containing forth commands to a microcontroller running amforth (v1.3)
#   monitors the output from the microcontroller and uses the character echo to implement a kind of software flow control
#   also waits for a prompt after each line to allow the compiler a chance to run
#
#   in theory it should never overrun the input buffer on the controller, 
#   but it will easily get stuck if the output from the controller is unusual
#
#   you have to kill any other processes that are accessing the serial port before running this,
#   otherwise it will miss characters that are stolen by the other process and get stuck
#
#   perhaps a better way to implement this would have been as a program that could be plugged into a terminal program
#   like x/y/zmodem for minicom (except for the difficulty of killing it when it gets confused). oh well.
#
#   mailto:pix@test.at
#   http://pix.test.at/

# change 26.11.2007
# mt: state machine extended to handle ok prompt later on the line
# mt: defined include syntax for files:
#         #include filename 
#     e.g. #include lib/ans94/marker.frt
# mt: an envar AMFORTH_LIB is checked as the list of directories 
#     to searched. This list is appended to "." (current work directory)
#
import sys
import getopt
import os
import re
import time
import fnmatch

global verbose, debug, mcudef

def merge(seq):
    merged = []
    for s in seq:
        for x in s:
            merged.append(x)
    return merged

def search_and_open_file(filename):
    directorylist=["","."]
    filedirs={}
    basefilename=os.path.basename(filename)
    if os.environ.has_key("AMFORTH_LIB"):
      directorylist = merge([directorylist, os.environ["AMFORTH_LIB"].split(":")])
    for p in directorylist:
      for root, dirs, files in os.walk(p):
        for f in files:
          if os.path.join(root, f).endswith("/"+basefilename): # better than fnmatch since it can deal with directiories
            fpath=os.path.realpath(os.path.join(root, f))
            if filedirs.has_key(f):
              for d in filedirs[f]:
                if d==fpath:
                  fpath=None
              if fpath: filedirs[f].append(fpath)
            else:
              filedirs[f]=[fpath]
    if len(filedirs[basefilename])==1:
       print "\nusing ", filename," from", filedirs[filename][0]
       filehandle = file(filedirs[filename][0])
       return filehandle
    else:
      # oops, too many files or no one at all no file found?
      print "\n", filename , " was found in ", len(filedirs[basefilename]), " directories"
      print "\t\n".join(filedirs[basefilename])
      print >>sys.stderr, "Sorry, giving up. You should check the controller!"
      sys.exit(2)

def write_line_flow(string,dest):
	# strip comments
	# these probably will strip comment-like structures out of ." .." strings as well.

	if debug:
		print >>sys.stderr, "line before comment stripping: "+string

	if usemcudef:
		for regname in MCUREGS:
		    string = re.sub('(^|\s)+'+regname+'(\s|$)+', " " + MCUREGS[regname] + " ", string)
		if string[-1] != "\n":
		    string = string + "\n"
	string = re.sub("(^| )\( .*?\)"," ",string)
	string = re.sub("(^| )\( [^\)]*$"," \n",string)
        #string = re.sub("(^| )\\\\ .*","",string)

#	if re.match("^\s*$",string):
#		if verbose:
#			print >>sys.stderr, "skipping empty line"
#		return	

	if debug:
		print >>sys.stderr, "line after comment stripping: "+string

	if re.match("#include ", string):
		filename=re.match("#include (\S+)",string).group(1)
		nested_file = search_and_open_file(filename)
		write_file_flow(nested_file, dest)
		nested_file.close()
		return


	if verbose: 
		print >>sys.stderr, "sending line: "+string
	for o in list(string):
		dest.write(o)
		if o == "\t":
			o = " "
		
		while True:
			i = dest.read(1)
			#print "<"+i+"]",
			#print >>sys.stderr, "["+i+"]"
			sys.stdout.write(i)
			sys.stdout.flush()
			if i == o:
				#print "|",
				break	
	#dest.write("\n")
	if verbose:
		print >>sys.stderr, "waiting for prompt"

	start, nl, space, o, gt = range(5)

	state = start

	while True:
		#print >>sys.stderr, "{"+str(state)+"}"
		#dest.write("")
		i = dest.read(1)
		if i=="":
			continue
		#print "<"+i+"]",
		#print >>sys.stderr, "i=["+i+"] state="+str(state)
		sys.stdout.write(i)
		sys.stdout.flush()
		if i==" ":
			state = space
		elif state == start:
			if i == "\r":
				state = nl
			elif i == " ":
				state = space
			continue
		elif state == nl:
			if i == ">":
				state = gt
			else:
				state = start
			continue
		elif state == gt:
			if i == " ":
				if debug:
					print >>sys.stderr, "<matched '^> '>"
				break
			else:
				state = start
			continue
		elif state == space:
			if i == "o":
				state = o
			else:
				state = start
			continue
		elif state == o:
			if i == "k":
				if debug:
					print >>sys.stderr, "<matched ' ok'>"
				break
			else:
				state = start
	
		elif state == space:
			if i == "?":
				state = question
			else:
				state = start
		elif state == question:
			if i == "?":
				if debug: 
					print >>sys.stderr, "<matched ' ??'>"
				break
			else:
				state = start

def write_file_flow(in_file,dest):
	while(True):
		line = in_file.readline()
		if len(line)>0:
			write_line_flow(line,dest)
		else:
			break		

tty_dev_name = "/dev/ttyS0"
force = False
verbose = False
debug = False
usemcudef = False
starttime = time.time()

try:
	opts, args = getopt.getopt(sys.argv[1:],"ht:vfdc:")
except getopt.GetoptError:
	print >>sys.stderr, "unknown option. try -h"
	sys.exit(1)
for opt, arg in opts:
	if opt == "-h":
		print >>sys.stderr, "usage: amforth-upload [-h] [-v] [-f] [-t tty] [file1] [file2] [...]"
		print >>sys.stderr, "   default tty is "+tty_dev_name
		print >>sys.stderr, "   if no files are specified, input is read from the the terminal"
		print >>sys.stderr, "   -f will run without checking for other processes accessing the tty"
		print >>sys.stderr, "   -v will print extra information during execution"
		print >>sys.stderr, "   -t selects the serial device. Default is " + tty_dev_name
		print >>sys.stderr, "   -c partname-directory tries to load the device.py file from the partname directory."
		sys.exit(1)
	elif opt == "-t":
		tty_dev_name = arg
	elif opt == "-v":
		verbose = True
	elif opt == "-f":
		force = True
	elif opt == "-d":
		debug = True
	elif opt == "-c":
		mcudef = arg
                if mcudef.startswith("msp"):
                    sys.path.append("msp430/devices/"+mcudef)
                else:
                    sys.path.append("avr8/devices/"+mcudef)
		try:
			from device import *
			usemcudef = True
			print "using device.py from " + mcudef
		except:
			print "failed using device.py from " + mcudef + " .. continuing"

if not force:	
	if not os.system("which lsof > /dev/null 2>&1"):
		if not os.system("lsof " + tty_dev_name):
			print >>sys.stderr, "the above process is accessing "+tty_dev_name+"."
			print >>sys.stderr, "please stop the process and try again."
			print >>sys.stderr, "run with the -f option to force execution anyway"	
			sys.exit(1)
	elif not os.system("which fuser >/dev/null 2>&1"):
		if not os.system("fuser -u "+tty_dev_name):
			print >>sys.stderr, "the above process is accessing "+tty_dev_name+"."
			print >>sys.stderr, "please stop the process and try again."
			print >>sys.stderr, "run with the -f option to force execution anyway"	
			sys.exit(1)
	else:
		print >>sys.stderr, "couldn't find fuser. so i can't check if "+tty_dev_name+" is in use."
	tty_dev = file(tty_dev_name,"r+",0)
	# get a prompt, clean up the input buffer
	write_line_flow("\n", tty_dev)
	if len(args)<1:
		if verbose:
			print >>sys.stderr, "processing stdin"
		write_file_flow(sys.stdin,tty_dev)
	else:
		for filename in args:
		    in_file = search_and_open_file(filename)
		    write_file_flow(in_file,tty_dev)
		    in_file.close()
endtime = time.time()
runtime = endtime - starttime
print "\ntime: ", runtime, " seconds"