forked from petercorke/toolbox-common-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runscript.m
289 lines (245 loc) · 8.13 KB
/
runscript.m
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
%RUNSCRIPT Run an M-file in interactive fashion
%
% RUNSCRIPT(SCRIPT, OPTIONS) runs the M-file SCRIPT and pauses after every
% executable line in the file until a key is pressed. Comment lines are shown
% without any delay between lines.
%
% Options::
% 'delay',D Don't wait for keypress, just delay of D seconds (default 0)
% 'cdelay',D Pause of D seconds after each comment line (default 0)
% 'begin' Start executing the file after the comment line %%begin (default false)
% 'dock' Cause the figures to be docked when created
% 'path',P Look for the file SCRIPT in the folder P (default .)
% 'dock' Dock figures within GUI
% 'nocolor' Don't use cprintf to print lines in color (comments black, code blue)
%
% Notes::
% - If no file extension is given in SCRIPT, .m is assumed.
% - A copyright text block will be skipped and not displayed.
% - If cprintf exists and 'nocolor' is not given then lines are displayed
% in color.
% - Leading comment characters are not displayed.
% - If the executable statement has comments immediately afterward (no blank lines)
% then the pause occurs after those comments are displayed.
% - A simple '-' prompt indicates when the script is paused, hit enter.
% - If the function cprintf() is in your path, the display is more
% colorful. You can get this file from MATLAB File Exchange.
% - If the file has a lot of boilerplate, you can skip over and not display
% it by giving the 'begin' option which searchers for the first line
% starting with %%begin and commences execution at the line after that.
%
% See also eval.
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
function runscript(fname, varargin)
opt.path = [];
opt.delay = [];
opt.begin = false;
opt.cdelay = 0;
opt.dock = false;
opt.color = true;
opt = tb_optparse(opt, varargin);
if ~exist('cprintf')
opt.color = false;
end
close all
curDir = pwd();
prevDockStatus = get(0,'DefaultFigureWindowStyle');
if opt.dock
set(0,'DefaultFigureWindowStyle','docked');
else
set(0,'DefaultFigureWindowStyle','normal');
end
if ~isempty(opt.path)
fname = fullfile(opt.path, [fname '.m']);
else
fname = [fname '.m'];
end
fp = fopen(fname, 'r');
clc
fprintf('--- runscript <-- %s\n', fname);
running = false;
shouldPause = false;
savedText = [];
if ~opt.begin
running = true;
end
lineNum = 1;
skipping = false;
% stashMode
% 0 normal
% 1 loop
% 2 continuation
continMode = false;
compoundDepth = 0;
while 1
% get the next line from the file, bail if EOF
line = fgetl(fp);
if line == -1
break
end
lineNum = lineNum+1;
if startswith(line, '% Copyright')
skipping = true;
continue;
end
% logic to skip lines until we see one beginning with %%begin
if ~running
if strcmp(line, '%%begin')
running = true;
else
continue;
end
end;
if length(strtrim(line)) == 0
% blank line
if skipping
skipping = false;
end
fprintf('\n');
if shouldPause
scriptwait(opt);
shouldPause = false;
end
continue
elseif skipping
continue;
elseif startswith(strtrim(line), '%')
% line was a comment
disp( strtrim(line(2:end)) )
pause(opt.cdelay) % optional comment delay
continue;
else
if shouldPause
scriptwait(opt);
shouldPause = false;
end
end
% if the start of a loop, stash the text for now
if startswith(line, 'for') || startswith(line, 'while') || startswith(line, 'if')
% found a compound block, don't eval it until we get to the end
compoundDepth = compoundDepth + 1;
end
% if the statement has a continuation
if endswith(line, '...') && compoundDepth == 0
% found a compound statement, don't eval it until we get to the end
continMode = true;
end
if compoundDepth == 0 && ~continMode
prompt = '>> ';
else
prompt = '';
end
% display the line with a pretend MATLAB prompt
if opt.color
cprintf('blue', '%s%s', prompt, line)
else
fprintf('%s', prompt); disp(line)
end
if compoundDepth > 0 || continMode
% we're in stashing mode
savedText = strcat(savedText, '\n', line);
end
if compoundDepth > 0 && startswith(line, 'end')
% the compound block is fully unnested
compoundDepth = compoundDepth - 1;
if compoundDepth == 0
evalSavedText(savedText, lineNum, opt);
savedText = '';
shouldPause = true;
end
continue
elseif continMode && ~endswith(line, '...')
% no longer in continuation mode
evalSavedText(savedText, lineNum, opt);
savedText = '';
continMode = false;
shouldPause = true;
continue
end
if compoundDepth == 0 && ~continMode
% it's a simple executable statement, execute it
fprintf(' \n');
try
evalSavedText(line, lineNum, opt);
catch
break
end
shouldPause = true;
end
end
fprintf('------ done --------\n');
% restore the docking mode if we set it
set(0,'DefaultFigureWindowStyle', prevDockStatus)
cd(curDir)
end
function evalSavedText(text, lineNum, opt)
if length(strtrim(text)) == 0
return
end
text = sprintf(text);
try
if opt.color
text = strrep(text, '''', ''''''); % fix single quotes
t = evalin('base', strcat('evalc(''', text, ''')') );
cprintf('blue', '%s', t);
else
evalin('base', text);
end
catch m
fprintf('error in script %s at line %d', fname, lineNum);
m.rethrow();
end
fprintf('\n');
end
% delay or prompt according to passed options
function scriptwait(opt)
if isempty(opt.delay)
%a = input('-', 's');
prompt = 'continue?';
bs = repmat('\b', [1 length(prompt)]);
if opt.color
cprintf('red', prompt); pause;
cprintf('text', bs);
else
fprintf(prompt); pause;
fprintf(bs);
end
else
pause(opt.delay);
end
end
% test if s2 is at the start of s1 (ignoring leading spaces)
function res = startswith(s1, s2)
s1 = strtrim(s1); % trim leading white space
r = strfind(s1, s2);
res = false;
if ~isempty(r) && (r(1) == 1)
res = true;
end
end
% test if s2 is at the end of s1
function res = endswith(s1, s2)
if length(s1) < length(s2)
res = false;
else
n2 = length(s2)-1;
res = strcmp(s1(end-n2:end), s2);
end
end