| 1 |
/* praat_script.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code 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 (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the 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 work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "praatP.h" |
| 20 |
#include "praat_script.h" |
| 21 |
#include "UiPause.h" |
| 22 |
#include "DemoEditor.h" |
| 23 |
|
| 24 |
static integer praat_findObjectFromString (Interpreter interpreter, conststring32 string) { |
| 25 |
try { |
| 26 |
integer IOBJECT; |
| 27 |
while (*string == U' ') |
| 28 |
string ++; |
| 29 |
if (*string >= U'A' && *string <= U'Z') { |
| 30 |
/* |
| 31 |
Find the object by its name. |
| 32 |
*/ |
| 33 |
static MelderString buffer; |
| 34 |
MelderString_copy (& buffer, string); |
| 35 |
char32 *space = str32chr (buffer.string, U' '); |
| 36 |
if (! space) |
| 37 |
Melder_throw (U"Missing space in name."); |
| 38 |
*space = U'\0'; |
| 39 |
char32 *className = & buffer.string [0], *givenName = space + 1; |
| 40 |
WHERE_DOWN (1) { |
| 41 |
Daata object = (Daata) OBJECT; |
| 42 |
if (str32equ (className, Thing_className (OBJECT)) && str32equ (givenName, object -> name.get())) |
| 43 |
return IOBJECT; |
| 44 |
} |
| 45 |
/* |
| 46 |
No object with that name. Perhaps the class name was wrong? |
| 47 |
*/ |
| 48 |
ClassInfo klas = Thing_classFromClassName (className, nullptr); |
| 49 |
WHERE_DOWN (1) { |
| 50 |
Daata object = (Daata) OBJECT; |
| 51 |
if (str32equ (klas -> className, Thing_className (OBJECT)) && str32equ (givenName, object -> name.get())) |
| 52 |
return IOBJECT; |
| 53 |
} |
| 54 |
Melder_throw (U"No object with that name."); |
| 55 |
} else { |
| 56 |
/* |
| 57 |
Find the object by its ID. |
| 58 |
*/ |
| 59 |
double value; |
| 60 |
Interpreter_numericExpression (interpreter, string, & value); |
| 61 |
const integer id = (integer) value; // SMELL: rounding down |
| 62 |
WHERE (ID == id) |
| 63 |
return IOBJECT; |
| 64 |
Melder_throw (U"No object with number ", id, U"."); |
| 65 |
} |
| 66 |
} catch (MelderError) { |
| 67 |
Melder_throw (U"Object \"", string, U"\" does not exist."); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
Editor praat_findEditorFromString (conststring32 string) { |
| 72 |
integer IOBJECT; |
| 73 |
while (*string == U' ') |
| 74 |
string ++; |
| 75 |
if (*string >= U'A' && *string <= U'Z') { |
| 76 |
WHERE_DOWN (1) { |
| 77 |
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 78 |
Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 79 |
if (editor) { |
| 80 |
Melder_assert (editor -> name); |
| 81 |
const char32 *space = str32chr (editor -> name.get(), U' '); // editors tend to be called like "3. Sound kanweg" |
| 82 |
if (space) { // but not all |
| 83 |
conststring32 name = space + 1; |
| 84 |
if (str32equ (name, string)) |
| 85 |
return editor; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} else { |
| 91 |
WHERE_DOWN (1) { |
| 92 |
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 93 |
Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 94 |
if (editor && str32equ (editor -> name.get(), string)) |
| 95 |
return editor; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
Melder_throw (U"Editor \"", string, U"\" does not exist."); |
| 100 |
} |
| 101 |
|
| 102 |
Editor praat_findEditorById (integer id) { |
| 103 |
integer IOBJECT; |
| 104 |
WHERE (1) { |
| 105 |
if (ID == id) { |
| 106 |
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 107 |
Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 108 |
if (editor) |
| 109 |
return editor; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
Melder_throw (U"Editor ", id, U" does not exist."); |
| 114 |
} |
| 115 |
|
| 116 |
static int parseCommaSeparatedArguments (Interpreter interpreter, char32 *arguments, structStackel *args) { |
| 117 |
int narg = 0, depth = 0; |
| 118 |
for (char32 *p = arguments; ; p ++) { |
| 119 |
const bool endOfArguments = ( *p == U'\0' ); |
| 120 |
if (endOfArguments || (*p == U',' && depth == 0)) { |
| 121 |
if (narg == MAXIMUM_NUMBER_OF_FIELDS) |
| 122 |
Melder_throw (U"Cannot have more than ", MAXIMUM_NUMBER_OF_FIELDS, U" arguments"); |
| 123 |
*p = U'\0'; |
| 124 |
Formula_Result result; |
| 125 |
Interpreter_anyExpression (interpreter, arguments, & result); |
| 126 |
narg ++; |
| 127 |
/* |
| 128 |
First remove the old contents. |
| 129 |
*/ |
| 130 |
args [narg]. reset(); |
| 131 |
#if STACKEL_VARIANTS_ARE_PACKED_IN_A_UNION |
| 132 |
memset (& args [narg], 0, sizeof (structStackel)); |
| 133 |
#endif |
| 134 |
/* |
| 135 |
Then copy in the new contents. |
| 136 |
*/ |
| 137 |
switch (result. expressionType) { |
| 138 |
case kFormula_EXPRESSION_TYPE_NUMERIC: { |
| 139 |
args [narg]. which = Stackel_NUMBER; |
| 140 |
args [narg]. number = result. numericResult; |
| 141 |
} break; case kFormula_EXPRESSION_TYPE_STRING: { |
| 142 |
args [narg]. setString (result. stringResult.move()); |
| 143 |
} break; case kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR: { |
| 144 |
args [narg]. which = Stackel_NUMERIC_VECTOR; |
| 145 |
args [narg]. numericVector = result. numericVectorResult; |
| 146 |
args [narg]. owned = result. owned; |
| 147 |
result. owned = false; |
| 148 |
} break; case kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX: { |
| 149 |
args [narg]. which = Stackel_NUMERIC_MATRIX; |
| 150 |
args [narg]. numericMatrix = result. numericMatrixResult; |
| 151 |
args [narg]. owned = result. owned; |
| 152 |
result. owned = false; |
| 153 |
} break; case kFormula_EXPRESSION_TYPE_STRING_ARRAY: { |
| 154 |
args [narg]. which = Stackel_STRING_ARRAY; |
| 155 |
args [narg]. stringArray = result. stringArrayResult; |
| 156 |
args [narg]. owned = result. owned; |
| 157 |
result. owned = false; |
| 158 |
} break; |
| 159 |
} |
| 160 |
arguments = p + 1; |
| 161 |
} else if (*p == U'(' || *p == U'[' || *p == U'{') { |
| 162 |
depth ++; |
| 163 |
} else if (*p == U')' || *p == U']' || *p == U'}') { |
| 164 |
depth --; |
| 165 |
} else if (*p == U'\"') { |
| 166 |
for (;;) { |
| 167 |
p ++; |
| 168 |
if (*p == U'\"') { |
| 169 |
if (p [1] == U'\"') |
| 170 |
p ++; |
| 171 |
else |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
if (endOfArguments) |
| 177 |
break; |
| 178 |
} |
| 179 |
return narg; |
| 180 |
} |
| 181 |
|
| 182 |
bool praat_executeCommand (Interpreter interpreter, char32 *command) { |
| 183 |
if (interpreter) |
| 184 |
interpreter -> returnType = kInterpreter_ReturnType::VOID_; // clear return type to its default |
| 185 |
|
| 186 |
static struct structStackel args [1 + MAXIMUM_NUMBER_OF_FIELDS]; |
| 187 |
//trace (U"praat_executeCommand: ", Melder_pointer (interpreter), U": ", command); |
| 188 |
if (command [0] == U'\0' || command [0] == U'#' || command [0] == U'!' || command [0] == U';') |
| 189 |
/* Skip empty lines and comments. */; |
| 190 |
else if ((command [0] == U'.' || command [0] == U'+' || command [0] == U'-') && Melder_isAsciiUpperCaseLetter (command [1])) { // selection? |
| 191 |
integer IOBJECT = praat_findObjectFromString (interpreter, command + 1); |
| 192 |
if (command [0] == '.') |
| 193 |
praat_deselectAll (); |
| 194 |
if (command [0] == '-') |
| 195 |
praat_deselect (IOBJECT); |
| 196 |
else |
| 197 |
praat_select (IOBJECT); |
| 198 |
praat_show (); |
| 199 |
} else if (Melder_isLetter (command [0]) && ! Melder_isUpperCaseLetter (command [0])) { // all directives start with an ASCII lower-case letter |
| 200 |
if (str32nequ (command, U"select ", 7)) { |
| 201 |
if (str32nequ (command + 7, U"all", 3) && (command [10] == U'\0' || command [10] == U' ' || command [10] == U'\t')) { |
| 202 |
praat_selectAll (); |
| 203 |
praat_show (); |
| 204 |
} else { |
| 205 |
integer IOBJECT = praat_findObjectFromString (interpreter, command + 7); |
| 206 |
praat_deselectAll (); |
| 207 |
praat_select (IOBJECT); |
| 208 |
praat_show (); |
| 209 |
} |
| 210 |
} else if (str32nequ (command, U"plus ", 5)) { |
| 211 |
integer IOBJECT = praat_findObjectFromString (interpreter, command + 5); |
| 212 |
praat_select (IOBJECT); |
| 213 |
praat_show (); |
| 214 |
} else if (str32nequ (command, U"minus ", 6)) { |
| 215 |
integer IOBJECT = praat_findObjectFromString (interpreter, command + 6); |
| 216 |
praat_deselect (IOBJECT); |
| 217 |
praat_show (); |
| 218 |
} else if (str32nequ (command, U"echo ", 5)) { |
| 219 |
MelderInfo_open (); |
| 220 |
MelderInfo_write (command + 5); |
| 221 |
MelderInfo_close (); |
| 222 |
} else if (str32nequ (command, U"clearinfo", 9)) { |
| 223 |
Melder_clearInfo (); |
| 224 |
} else if (str32nequ (command, U"print ", 6)) { |
| 225 |
MelderInfo_write (command + 6); |
| 226 |
MelderInfo_drain (); |
| 227 |
} else if (str32nequ (command, U"printtab", 8)) { |
| 228 |
MelderInfo_write (U"\t"); |
| 229 |
MelderInfo_drain (); |
| 230 |
} else if (str32nequ (command, U"printline", 9)) { |
| 231 |
if (command [9] == ' ') MelderInfo_write (command + 10); |
| 232 |
MelderInfo_write (U"\n"); |
| 233 |
MelderInfo_drain (); |
| 234 |
} else if (str32nequ (command, U"fappendinfo ", 12)) { |
| 235 |
if (theCurrentPraatObjects != & theForegroundPraatObjects) |
| 236 |
Melder_throw (U"The script command “fappendinfo” is not available inside pictures."); |
| 237 |
structMelderFile file { }; |
| 238 |
Melder_relativePathToFile (command + 12, & file); |
| 239 |
MelderFile_appendText_e (& file, Melder_getInfo ()); |
| 240 |
} else if (str32nequ (command, U"unix ", 5)) { |
| 241 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 242 |
U"The script command “unix” is not available inside manuals."); |
| 243 |
try { |
| 244 |
Melder_runSystem (command + 5); |
| 245 |
} catch (MelderError) { |
| 246 |
Melder_throw (U"Unix command “", command + 5, U"” returned error status;\n" |
| 247 |
U"if you want to ignore this, use `unix_nocheck' instead of `unix'."); |
| 248 |
} |
| 249 |
} else if (str32nequ (command, U"unix_nocheck ", 13)) { |
| 250 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 251 |
U"The script command “unix_nocheck” is not available inside manuals."); |
| 252 |
try { |
| 253 |
Melder_runSystem (command + 13); |
| 254 |
} catch (MelderError) { |
| 255 |
Melder_clearError (); |
| 256 |
} |
| 257 |
} else if (str32nequ (command, U"system ", 7)) { |
| 258 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 259 |
U"The script command “system” is not available inside manuals."); |
| 260 |
try { |
| 261 |
Melder_runSystem (command + 7); |
| 262 |
} catch (MelderError) { |
| 263 |
Melder_throw (U"System command “", command + 7, U"” returned error status;\n" |
| 264 |
U"if you want to ignore this, use “system_nocheck” instead of “system”."); |
| 265 |
} |
| 266 |
} else if (str32nequ (command, U"system_nocheck ", 15)) { |
| 267 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 268 |
U"The script command “system_nocheck” is not available inside manuals."); |
| 269 |
try { |
| 270 |
Melder_runSystem (command + 15); |
| 271 |
} catch (MelderError) { |
| 272 |
Melder_clearError (); |
| 273 |
} |
| 274 |
} else if (str32nequ (command, U"nowarn ", 7)) { |
| 275 |
autoMelderWarningOff nowarn; |
| 276 |
return praat_executeCommand (interpreter, command + 7); |
| 277 |
} else if (str32nequ (command, U"noprogress ", 11)) { |
| 278 |
autoMelderProgressOff noprogress; |
| 279 |
return praat_executeCommand (interpreter, command + 11); |
| 280 |
} else if (str32nequ (command, U"nocheck ", 8)) { |
| 281 |
try { |
| 282 |
return praat_executeCommand (interpreter, command + 8); |
| 283 |
} catch (MelderError) { |
| 284 |
Melder_clearError (); |
| 285 |
return false; |
| 286 |
} |
| 287 |
} else if (str32nequ (command, U"demo ", 5)) { |
| 288 |
autoDemoOpen demo; |
| 289 |
return praat_executeCommand (interpreter, command + 5); |
| 290 |
} else if (str32nequ (command, U"asynchronous ", 13)) { |
| 291 |
autoMelderAsynchronous asynchronous; |
| 292 |
return praat_executeCommand (interpreter, command + 13); |
| 293 |
} else if (str32nequ (command, U"pause ", 6) || str32equ (command, U"pause")) { |
| 294 |
if (theCurrentPraatApplication -> batch) |
| 295 |
return true; // in batch we ignore pause statements |
| 296 |
const Editor optionalPauseWindowOwningEditor = interpreter -> optionalDynamicEnvironmentEditor(); |
| 297 |
const GuiWindow parentShell = ( optionalPauseWindowOwningEditor ? optionalPauseWindowOwningEditor -> windowForm : theCurrentPraatApplication -> topShell ); |
| 298 |
UiPause_pauseScript (parentShell, optionalPauseWindowOwningEditor, interpreter, str32equ (command, U"pause") ? U"..." : command + 6); |
| 299 |
} else if (str32nequ (command, U"execute ", 8)) { |
| 300 |
//TRACE |
| 301 |
trace (U"interpreter: ", Melder_pointer (interpreter)); |
| 302 |
if (interpreter) |
| 303 |
trace (U"stack: ", Melder_pointer (interpreter -> owningInterpreterStack)); |
| 304 |
praat_runOldExecuteCommand (interpreter ? interpreter -> owningInterpreterStack : nullptr, command + 8); |
| 305 |
} else if (str32nequ (command, U"editor", 6)) { // deprecated |
| 306 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 307 |
U"The script command “editor” is not available inside manuals."); |
| 308 |
if (command [6] == U' ' && Melder_isLetter (command [7])) { |
| 309 |
interpreter -> setDynamicEditorEnvironmentFromEditor (praat_findEditorFromString (command + 7)); |
| 310 |
} else if (command [6] == U'\0') { |
| 311 |
if (interpreter && interpreter -> wasStartedFromEditorEnvironment()) |
| 312 |
/* |
| 313 |
The script directive |
| 314 |
editor |
| 315 |
(without arguments) should set the interpreter's environment back to the editor that started the script. |
| 316 |
*/ |
| 317 |
interpreter -> setDynamicFromOwningEditorEnvironment(); // TODO: what if the owener has been orphaned? |
| 318 |
else |
| 319 |
Melder_throw (U"The script command “editor” requires an argument when called from outside an editor."); |
| 320 |
} else { |
| 321 |
Interpreter_voidExpression (interpreter, command); |
| 322 |
} |
| 323 |
} else if (str32nequ (command, U"endeditor", 9)) { |
| 324 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 325 |
U"The script command “endeditor” is not available inside manuals."); |
| 326 |
interpreter -> nullifyDynamicEditorEnvironment(); |
| 327 |
} else if (str32nequ (command, U"filedelete ", 11)) { |
| 328 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 329 |
U"The script command “filedelete” is not available inside manuals."); |
| 330 |
const char32 *p = command + 11; |
| 331 |
structMelderFile file { }; |
| 332 |
while (*p == U' ' || *p == U'\t') p ++; |
| 333 |
if (*p == U'\0') |
| 334 |
Melder_throw (U"Missing file name after “filedelete”."); |
| 335 |
Melder_relativePathToFile (p, & file); |
| 336 |
MelderFile_delete (& file); |
| 337 |
} else if (str32nequ (command, U"fileappend ", 11)) { |
| 338 |
Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 339 |
U"The script command “fileappend” is not available inside manuals."); |
| 340 |
const char32 *p = command + 11; |
| 341 |
char32 path [kMelder_MAXPATH+1], *q = & path [0]; |
| 342 |
while (*p == U' ' || *p == U'\t') p ++; |
| 343 |
if (*p == U'\0') |
| 344 |
Melder_throw (U"Missing file name after “fileappend”."); |
| 345 |
if (*p == '\"') { |
| 346 |
for (;;) { |
| 347 |
char32 kar = * ++ p; |
| 348 |
if (kar == U'\"') if (* ++ p == U'\"') *q ++ = U'\"'; else break; |
| 349 |
else if (kar == U'\0') break; |
| 350 |
else *q ++ = kar; |
| 351 |
} |
| 352 |
} else { |
| 353 |
for (;;) { |
| 354 |
char32 kar = * p; |
| 355 |
if (kar == U'\0' || kar == U' ' || kar == U'\t') break; |
| 356 |
*q ++ = kar; |
| 357 |
p ++; |
| 358 |
} |
| 359 |
} |
| 360 |
*q = U'\0'; |
| 361 |
if (*p == U' ' || *p == U'\t') { |
| 362 |
structMelderFile file { }; |
| 363 |
Melder_relativePathToFile (path, & file); |
| 364 |
MelderFile_appendText_e (& file, p + 1); |
| 365 |
} |
| 366 |
} else { |
| 367 |
/* |
| 368 |
This must be a formula command: |
| 369 |
proc (args) |
| 370 |
*/ |
| 371 |
Interpreter_voidExpression (interpreter, command); |
| 372 |
} |
| 373 |
} else { /* Simulate menu choice. */ |
| 374 |
bool hasDots = false, hasColon = false; |
| 375 |
|
| 376 |
/* Parse command line into command and arguments. */ |
| 377 |
/* The separation is formed by the three dots or a colon. */ |
| 378 |
|
| 379 |
char32 *arguments = & command [0]; |
| 380 |
for (arguments = & command [0]; *arguments != U'\0'; arguments ++) { |
| 381 |
if (*arguments == U':') { |
| 382 |
hasColon = true; |
| 383 |
if (arguments [1] == U'\0') { |
| 384 |
arguments = & arguments [1]; // empty string |
| 385 |
} else { |
| 386 |
if (arguments [1] != U' ') |
| 387 |
Melder_throw (U"There should be a space after the colon."); |
| 388 |
arguments [1] = U'\0'; // new end of "command" |
| 389 |
arguments += 2; // the arguments start after the space |
| 390 |
} |
| 391 |
break; |
| 392 |
} |
| 393 |
if (*arguments == U'.' && arguments [1] == U'.' && arguments [2] == U'.') { |
| 394 |
hasDots = true; |
| 395 |
arguments += 3; |
| 396 |
if (*arguments == U'\0') { |
| 397 |
// empty string |
| 398 |
} else { |
| 399 |
if (*arguments != U' ') |
| 400 |
Melder_throw (U"There should be a space after the three dots."); |
| 401 |
*arguments = U'\0'; // new end of "command" |
| 402 |
arguments ++; // the arguments start after the space |
| 403 |
} |
| 404 |
break; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/* See if command exists and is available; ignore separators. */ |
| 409 |
/* First try loose commands, then fixed commands. */ |
| 410 |
|
| 411 |
integer narg; |
| 412 |
char32 command2 [200]; |
| 413 |
if (hasColon) { |
| 414 |
narg = parseCommaSeparatedArguments (interpreter, arguments, args); |
| 415 |
str32cpy (command2, command); |
| 416 |
char32 *colon = str32chr (command2, U':'); |
| 417 |
colon [0] = colon [1] = colon [2] = U'.'; |
| 418 |
colon [3] = U'\0'; |
| 419 |
} |
| 420 |
if (theCurrentPraatObjects == & theForegroundPraatObjects && interpreter && interpreter -> hasDynamicEnvironmentEditor()) { |
| 421 |
if (hasColon) |
| 422 |
Editor_doMenuCommand (interpreter -> optionalDynamicEnvironmentEditor(), command2, narg, args, nullptr, interpreter); |
| 423 |
else |
| 424 |
Editor_doMenuCommand (interpreter -> optionalDynamicEnvironmentEditor(), command, 0, nullptr, arguments, interpreter); |
| 425 |
} else if (! praat_commandsWithExternalSideEffectsAreAllowed () && |
| 426 |
(str32nequ (command, U"Save ", 5) || |
| 427 |
str32nequ (command, U"Write ", 6) || |
| 428 |
str32nequ (command, U"Append to ", 10) || |
| 429 |
str32nequ (command, U"Export ", 7) || |
| 430 |
str32nequ (command, U"Quit", 4))) |
| 431 |
{ |
| 432 |
Melder_throw (U"Commands that write files (including Quit) are not available inside manuals."); |
| 433 |
} else { |
| 434 |
bool theCommandIsAnExistingAction = false; |
| 435 |
try { |
| 436 |
if (hasColon) |
| 437 |
theCommandIsAnExistingAction = praat_doAction (command2, narg, args, interpreter); |
| 438 |
else |
| 439 |
theCommandIsAnExistingAction = praat_doAction (command, arguments, interpreter); |
| 440 |
} catch (MelderError) { |
| 441 |
/* |
| 442 |
We only get here if the command *was* an existing action. |
| 443 |
Anything could have gone wrong in its execution, |
| 444 |
but one invisible problem can be checked here. |
| 445 |
*/ |
| 446 |
if (hasDots && arguments [0] != U'\0' && arguments [Melder_length (arguments) - 1] == U' ') |
| 447 |
Melder_throw (U"It may be helpful to remove the trailing spaces in \"", arguments, U"\"."); |
| 448 |
else |
| 449 |
throw; |
| 450 |
} |
| 451 |
if (! theCommandIsAnExistingAction) { |
| 452 |
bool theCommandIsAnExistingMenuCommand = false; |
| 453 |
try { |
| 454 |
if (hasColon) |
| 455 |
theCommandIsAnExistingMenuCommand = praat_doMenuCommand (command2, narg, args, interpreter); |
| 456 |
else |
| 457 |
theCommandIsAnExistingMenuCommand = praat_doMenuCommand (command, arguments, interpreter); |
| 458 |
} catch (MelderError) { |
| 459 |
/* |
| 460 |
We only get here if the command *was* an existing menu command. |
| 461 |
Anything could have gone wrong in its execution, |
| 462 |
but one invisible problem can be checked here. |
| 463 |
*/ |
| 464 |
if (hasDots && arguments [0] != U'\0' && arguments [Melder_length (arguments) - 1] == U' ') |
| 465 |
Melder_throw (U"It may be helpful to remove the trailing spaces in “", arguments, U"”."); |
| 466 |
else |
| 467 |
throw; |
| 468 |
} |
| 469 |
if (! theCommandIsAnExistingMenuCommand) { |
| 470 |
const integer length = Melder_length (command); |
| 471 |
if (str32nequ (command, U"ARGS ", 5)) |
| 472 |
Melder_throw (U"Command “ARGS” no longer supported. Instead use “form” and “endform”."); |
| 473 |
else if (str32chr (command, U'=')) |
| 474 |
if (command [0] == U'_') |
| 475 |
Melder_throw (U"Command “", command, U"” not recognized.\n" |
| 476 |
U"Probable cause: you are trying to use a variable name that starts with an underscore."); |
| 477 |
else |
| 478 |
Melder_throw (U"Command “", command, U"” not recognized.\n" |
| 479 |
U"Probable cause: you are trying to use a variable name that starts with a capital letter."); |
| 480 |
else if (length >= 1 && Melder_isHorizontalSpace (command [length - 1])) |
| 481 |
Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 482 |
U"It may be helpful to remove the trailing spaces."); |
| 483 |
else if (length >= 2 && Melder_isHorizontalSpace (command [length - 2]) && command [length - 1] == U':') |
| 484 |
Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 485 |
U"It may be helpful to remove the space before the colon."); |
| 486 |
else if (str32nequ (command, U"\"ooTextFile\"", 12)) |
| 487 |
Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 488 |
U"It is possible that this file is not a Praat script but a Praat data file that you can open with “Read from file...”."); |
| 489 |
else { |
| 490 |
if (interpreter && interpreter -> wasStartedFromEditorEnvironment()) |
| 491 |
if (interpreter -> hasDynamicEnvironmentEditor()) |
| 492 |
Melder_throw (U"Command “", command, U"” not available in ", interpreter -> optionalDynamicEditorEnvironmentClassName(), U"."); |
| 493 |
else |
| 494 |
Melder_throw (U"Command “", command, U"” not available for current selection.\n" |
| 495 |
U"Perhaps this is a ", interpreter -> optionalOwningEditorEnvironmentClassName(), U" command?"); |
| 496 |
else |
| 497 |
Melder_throw (U"Command “", command, U"” not available for current selection."); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
praat_updateSelection (); |
| 503 |
} |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
void praat_executeCommandFromStandardInput (conststring32 programName) { |
| 508 |
char command8 [1000]; // can be recursive |
| 509 |
/* |
| 510 |
FIXME: implement for Windows. |
| 511 |
*/ |
| 512 |
for (;;) { |
| 513 |
printf ("%s > ", Melder_peek32to8 (programName)); |
| 514 |
if (! fgets (command8, 999, stdin)) |
| 515 |
Melder_throw (U"Cannot read input."); |
| 516 |
char *newLine = strchr (command8, '\n'); |
| 517 |
if (newLine) |
| 518 |
*newLine = '\0'; |
| 519 |
autostring32 command32 = Melder_8to32_e (command8); |
| 520 |
try { |
| 521 |
(void) praat_executeCommand (nullptr, command32.get()); |
| 522 |
} catch (MelderError) { |
| 523 |
Melder_flushError (programName, U": Command “", Melder_peek8to32_u (command8), U"” not executed."); |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void praat_executeScript_noGUI (MelderFile file, bool fullTrust) { |
| 529 |
/* |
| 530 |
The problem here occurs when plug-in initialization (typically, `setup.praat`) posts a pause window, |
| 531 |
as happens e.g. in the VocalToolkit plug-in if the Praat version is too old. |
| 532 |
This function will return while the pause window is on the screen, and |
| 533 |
Praat will happily initialize the next plug-in. |
| 534 |
Quick fix: allow up to 10 InterpreterStacks. |
| 535 |
Long-term solution TODO: make pause windows in plug-in initializers modal. |
| 536 |
*/ |
| 537 |
constexpr integer NUMBER_OF_STACKS = 10; |
| 538 |
static autoInterpreterStack interpreterStacks [NUMBER_OF_STACKS]; |
| 539 |
static integer stackNumber = 0; |
| 540 |
stackNumber = (stackNumber + 1) % NUMBER_OF_STACKS; |
| 541 |
autoInterpreterStack& interpreterStack = interpreterStacks [stackNumber]; |
| 542 |
try { |
| 543 |
interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 544 |
autostring32 text = MelderFile_readText (file); |
| 545 |
{// scope |
| 546 |
autoMelderFileSetCurrentFolder folder (file); // so that script-relative file names can be used for including include files |
| 547 |
Melder_includeIncludeFiles (& text); |
| 548 |
} // back to the default directory of the caller |
| 549 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 550 |
interpreterStack.get(), // owner always needed |
| 551 |
Editor (nullptr), // never used inside an Editor window |
| 552 |
file // so that script-relative file names can be used inside the script |
| 553 |
); |
| 554 |
interpreterStack -> emptyAll (); |
| 555 |
Interpreter_rememberScript_override (interpreter.get(), file, fullTrust && str32str (text.get(), U"\n# --FULL-TRUST\n")); |
| 556 |
interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 557 |
} catch (MelderError) { |
| 558 |
Melder_throw (U"Script ", file, U" not completed."); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
void praat_runScript (InterpreterStack interpreterStack, conststring32 fileName, integer narg, Stackel args, Editor optionalInterpreterOwningEditor) { |
| 563 |
Melder_assert (interpreterStack); |
| 564 |
//Melder_assert (Melder_backgrounding); |
| 565 |
|
| 566 |
Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 567 |
if (parentInterpreter -> isInSecondPass) { |
| 568 |
interpreterStack -> resumeNextLevelInSecondPass (); |
| 569 |
} else { |
| 570 |
Melder_assert (MelderFolder_equal (Melder_peekWorkingDirectory (), & parentInterpreter -> workingDirectory)); |
| 571 |
//autoMelderSetCurrentFolder folder (& parentInterpreter -> workingDirectory); // TODO: is this superfluous, i.e. does the previous assertion never fire? |
| 572 |
|
| 573 |
structMelderFile file { }; |
| 574 |
try { |
| 575 |
Melder_relativePathToFile (fileName, & file); |
| 576 |
autostring32 text = MelderFile_readText (& file); |
| 577 |
/* |
| 578 |
We switch between default directories no fewer than four times: |
| 579 |
1. runScript() tends to be called from a script that we call the "caller"; |
| 580 |
when we enter runScript(), the default directory is the caller's folder, |
| 581 |
as was appropriate for the use of file names in the caller before runScript(), |
| 582 |
which had to be interpreted relative to the caller's folder. |
| 583 |
2. runScript() will call a script that we call the "callee"; |
| 584 |
include files have to be included from the callee's folder. |
| 585 |
3. For expanding any infile/outfile/folder arguments to runScript(), |
| 586 |
we have to be back in the caller's folder. |
| 587 |
4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 588 |
5. After runScript() finishes, we will have to be back in the caller's folder, |
| 589 |
so that the use of file names in the caller after runScript() |
| 590 |
will be interpreted relative to the caller's folder again. |
| 591 |
*/ |
| 592 |
{// scope |
| 593 |
autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 594 |
Melder_includeIncludeFiles (& text); |
| 595 |
} // back to the default directory of the caller |
| 596 |
autoInterpreter me = Interpreter_createFromEnvironment ( |
| 597 |
interpreterStack, // owner always needed |
| 598 |
optionalInterpreterOwningEditor, |
| 599 |
& file // so that callee-relative file names can be used inside the script |
| 600 |
); |
| 601 |
Interpreter_readParameters (me.get(), text.get()); // TODO: should become a field of structInterpreter |
| 602 |
my text = text.move(); |
| 603 |
Interpreter_getArgumentsFromArgs (me.get(), narg, args); // interpret caller-relative paths for infile/outfile/folder arguments |
| 604 |
Interpreter_rememberScript (me.get(), & file, ! parentInterpreter -> scriptReference || parentInterpreter -> scriptReference -> trusted); |
| 605 |
interpreterStack -> runDown (me.move(), autostring32(), false); // back to the default directory of the caller |
| 606 |
} catch (MelderError) { |
| 607 |
Melder_throw (U"Script ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
void praat_runNotebook (InterpreterStack interpreterStack, conststring32 fileName, integer narg, Stackel args, Editor optionalInterpreterOwningEditor) { |
| 613 |
Melder_assert (interpreterStack); |
| 614 |
//Melder_assert (Melder_backgrounding); |
| 615 |
|
| 616 |
//TRACE |
| 617 |
Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 618 |
if (parentInterpreter -> isInSecondPass) { |
| 619 |
interpreterStack -> resumeNextLevelInSecondPass (); |
| 620 |
} else { |
| 621 |
structMelderFolder workingDirectory; |
| 622 |
Melder_getCurrentFolder (& workingDirectory); |
| 623 |
Melder_assert (MelderFolder_equal (& workingDirectory, & parentInterpreter -> workingDirectory)); |
| 624 |
|
| 625 |
structMelderFile file { }; |
| 626 |
try { |
| 627 |
Melder_relativePathToFile (fileName, & file); |
| 628 |
autostring32 text = MelderFile_readText (& file); |
| 629 |
if (! Melder_startsWith (text.get(), U"\"")) |
| 630 |
Melder_throw (U"File ", & file, U" is not a Praat notebook."); |
| 631 |
/* |
| 632 |
We switch between default directories no fewer than four times: |
| 633 |
1. runScript() tends to be called from a script that we call the "caller"; |
| 634 |
when we enter runScript(), the working directory is the caller's folder |
| 635 |
(set upon Interpreter creation as the folder where the calling script is, |
| 636 |
though perhaps changed later in that script by `setWorkingDirectory`), |
| 637 |
as was appropriate for the use of file names in the caller before runScript(), |
| 638 |
which had to be interpreted relative to the caller's folder. |
| 639 |
2. runScript() will call a script that we call the "callee"; |
| 640 |
include files have to be included from the callee's folder. |
| 641 |
3. For expanding any infile/outfile/folder arguments to runScript(), |
| 642 |
we have to be back in the caller's folder. |
| 643 |
4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 644 |
5. After runScript() finishes, we will have to be back in the caller's folder, |
| 645 |
so that the use of file names in the caller after runScript() |
| 646 |
will be interpreted relative to the caller's folder again. |
| 647 |
*/ |
| 648 |
{// scope |
| 649 |
autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 650 |
Melder_includeIncludeFiles (& text); |
| 651 |
} // back to the default directory of the caller |
| 652 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 653 |
interpreterStack, |
| 654 |
optionalInterpreterOwningEditor, |
| 655 |
& file |
| 656 |
); |
| 657 |
Interpreter_readParameters (interpreter.get(), text.get()); // TODO: should become a field of structInterpreter |
| 658 |
autoMelderReadText readText = MelderReadText_createFromText (text.move()); |
| 659 |
autoManPages manPages = ManPages_createFromText (readText.get(), & file); |
| 660 |
ManPage firstPage = manPages -> pages.at [1]; |
| 661 |
autoManual manual = Manual_create (firstPage -> title.get(), interpreter.get(), manPages.releaseToAmbiguousOwner(), true, true); |
| 662 |
manual.releaseToUser (); |
| 663 |
} catch (MelderError) { |
| 664 |
Melder_throw (U"Notebook ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
void praat_executeScriptFromCommandLine (conststring32 fileName, integer argc, char **argv, bool fullTrust) { |
| 670 |
static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 671 |
structMelderFile file { }; |
| 672 |
Melder_relativePathToFile (fileName, & file); |
| 673 |
try { |
| 674 |
autostring32 text = MelderFile_readText (& file); |
| 675 |
{// scope |
| 676 |
autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used for including include files |
| 677 |
Melder_includeIncludeFiles (& text); |
| 678 |
} // back to the default directory of the caller |
| 679 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 680 |
interpreterStack.get(), |
| 681 |
Editor (nullptr), |
| 682 |
& file |
| 683 |
); |
| 684 |
Interpreter_readParameters (interpreter.get(), text.get()); |
| 685 |
Interpreter_getArgumentsFromCommandLine (interpreter.get(), argc, argv); // interpret caller-relative paths for infile/outfile/folder arguments |
| 686 |
autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used inside the script |
| 687 |
Interpreter_rememberScript (interpreter.get(), & file, fullTrust); |
| 688 |
interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 689 |
} catch (MelderError) { |
| 690 |
Melder_throw (U"Script ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
void praat_executeScriptFromFileNameWithArguments (conststring32 nameAndArguments) { |
| 695 |
structMelderFile file { }; |
| 696 |
try { |
| 697 |
char32 path [256]; |
| 698 |
const char32 *p, *arguments; |
| 699 |
/* |
| 700 |
Split into file name and arguments. |
| 701 |
*/ |
| 702 |
p = nameAndArguments; |
| 703 |
while (*p == U' ' || *p == U'\t') |
| 704 |
p ++; |
| 705 |
if (*p == U'\"') { |
| 706 |
char32 *q = path; |
| 707 |
p ++; // skip quote |
| 708 |
while (*p != U'\"' && *p != U'\0') |
| 709 |
* q ++ = * p ++; |
| 710 |
*q = U'\0'; |
| 711 |
arguments = p; |
| 712 |
if (*arguments == U'\"') |
| 713 |
arguments ++; |
| 714 |
if (*arguments == U' ') |
| 715 |
arguments ++; |
| 716 |
} else { |
| 717 |
char32 *q = path; |
| 718 |
while (*p != U' ' && *p != U'\0') |
| 719 |
* q ++ = * p ++; |
| 720 |
*q = U'\0'; |
| 721 |
arguments = p; |
| 722 |
if (*arguments == U' ') |
| 723 |
arguments ++; |
| 724 |
} |
| 725 |
Melder_relativePathToFile (path, & file); |
| 726 |
|
| 727 |
static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 728 |
autostring32 text = MelderFile_readText (& file); |
| 729 |
{// scope |
| 730 |
autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used for including include files |
| 731 |
Melder_includeIncludeFiles (& text); |
| 732 |
} // back to the default directory of the caller |
| 733 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 734 |
interpreterStack.get(), |
| 735 |
Editor (nullptr), |
| 736 |
& file // so that script-relative file names can be used inside the script |
| 737 |
); |
| 738 |
if (arguments) { |
| 739 |
Interpreter_readParameters (interpreter.get(), text.get()); |
| 740 |
Interpreter_getArgumentsFromString (interpreter.get(), arguments); // interpret caller-relative paths for infile/outfile/folder arguments |
| 741 |
} |
| 742 |
Interpreter_rememberScript (interpreter.get(), & file, false); |
| 743 |
interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 744 |
} catch (MelderError) { |
| 745 |
Melder_throw (U"Script ", & file, U" not completed."); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
void praat_runOldExecuteCommand (InterpreterStack interpreterStack, conststring32 nameAndArguments) { |
| 750 |
Melder_assert (interpreterStack); |
| 751 |
|
| 752 |
char32 path [256]; |
| 753 |
const char32 *p, *arguments; |
| 754 |
structMelderFile file { }; |
| 755 |
/* |
| 756 |
Split into file name and arguments. |
| 757 |
*/ |
| 758 |
p = nameAndArguments; |
| 759 |
while (*p == U' ' || *p == U'\t') |
| 760 |
p ++; |
| 761 |
if (*p == U'\"') { |
| 762 |
char32 *q = path; |
| 763 |
p ++; // skip quote |
| 764 |
while (*p != U'\"' && *p != U'\0') |
| 765 |
* q ++ = * p ++; |
| 766 |
*q = U'\0'; |
| 767 |
arguments = p; |
| 768 |
if (*arguments == U'\"') |
| 769 |
arguments ++; |
| 770 |
if (*arguments == U' ') |
| 771 |
arguments ++; |
| 772 |
} else { |
| 773 |
char32 *q = path; |
| 774 |
while (*p != U' ' && *p != U'\0') |
| 775 |
* q ++ = * p ++; |
| 776 |
*q = U'\0'; |
| 777 |
arguments = p; |
| 778 |
if (*arguments == U' ') |
| 779 |
arguments ++; |
| 780 |
} |
| 781 |
Melder_relativePathToFile (path, & file); |
| 782 |
|
| 783 |
Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 784 |
if (parentInterpreter -> isInSecondPass) { |
| 785 |
interpreterStack -> resumeNextLevelInSecondPass (); |
| 786 |
} else { |
| 787 |
try { |
| 788 |
autostring32 text = MelderFile_readText (& file); |
| 789 |
{// scope |
| 790 |
autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 791 |
Melder_includeIncludeFiles (& text); |
| 792 |
} // back to the default directory of the caller |
| 793 |
autoInterpreter me = Interpreter_createFromEnvironment ( |
| 794 |
interpreterStack, |
| 795 |
Editor (nullptr), |
| 796 |
& file // so that callee-relative file names can be used inside the script |
| 797 |
); |
| 798 |
if (arguments) { |
| 799 |
Interpreter_readParameters (me.get(), text.get()); |
| 800 |
Interpreter_getArgumentsFromString (me.get(), arguments); // interpret caller-relative paths for infile/outfile/folder arguments |
| 801 |
} |
| 802 |
my text = text.move(); |
| 803 |
Interpreter_rememberScript (me.get(), & file, ! parentInterpreter -> scriptReference || parentInterpreter -> scriptReference -> trusted); |
| 804 |
interpreterStack -> runDown (me.move(), autostring32(), false); // callee-relative file names can be used inside the script |
| 805 |
// back to the default directory of the caller |
| 806 |
} catch (MelderError) { |
| 807 |
Melder_throw (U"Script ", & file, U" not completed."); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
extern "C" void praatlib_executeScript (const char *text8) { |
| 813 |
static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); // TODO: never from an editor? |
| 814 |
try { |
| 815 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 816 |
interpreterStack.get(), |
| 817 |
Editor (nullptr), |
| 818 |
MelderFile (nullptr) // TODO: look for a file... |
| 819 |
); |
| 820 |
autostring32 string = Melder_8to32_e (text8); |
| 821 |
interpreterStack -> runDown (interpreter.move(), string.move(), false); |
| 822 |
} catch (MelderError) { |
| 823 |
Melder_throw (U"Script not completed."); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
void praat_executeScriptFromText (conststring32 text) { |
| 828 |
static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); // never from an editor (last checked 2026-02-01) |
| 829 |
try { |
| 830 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 831 |
interpreterStack.get(), |
| 832 |
Editor (nullptr), // never from an editor (last checked 2026-02-01) |
| 833 |
MelderFile (nullptr) |
| 834 |
); |
| 835 |
Melder_getCurrentFolder (& interpreter -> workingDirectory); |
| 836 |
interpreterStack -> emptyAll (); |
| 837 |
//Interpreter_rememberScript (interpreter.get(), & file, true); // will be OR-ed |
| 838 |
interpreterStack -> runDown (interpreter.move(), Melder_dup (text), false); |
| 839 |
} catch (MelderError) { |
| 840 |
Melder_throw (U"Script not completed."); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
static void secondPassThroughScript (UiForm sendingForm, integer /* narg */, Stackel /* args */, |
| 845 |
conststring32 /* sendingString_dummy */, Interpreter /* interpreter_dummy */, |
| 846 |
conststring32 /* invokingButtonTitle */, bool /* modified */, void * /* closure */, Editor optionalInterpreterOwningEditor) |
| 847 |
{ |
| 848 |
static autoInterpreterStack interpreterStack; |
| 849 |
interpreterStack = InterpreterStack_create (optionalInterpreterOwningEditor); // each time anew |
| 850 |
structMelderFile file { }; |
| 851 |
Melder_pathToFile (sendingForm -> scriptFilePath.get(), & file); |
| 852 |
autostring32 text = MelderFile_readText (& file); |
| 853 |
{// scope |
| 854 |
autoMelderFileSetCurrentFolder folder (& file); |
| 855 |
Melder_includeIncludeFiles (& text); |
| 856 |
} |
| 857 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 858 |
interpreterStack.get(), |
| 859 |
optionalInterpreterOwningEditor, |
| 860 |
& file |
| 861 |
); |
| 862 |
Interpreter_readParameters (interpreter.get(), text.get()); |
| 863 |
Interpreter_getArgumentsFromDialog (interpreter.get(), sendingForm); |
| 864 |
autoPraatBackground background; |
| 865 |
interpreterStack -> emptyAll (); |
| 866 |
Interpreter_rememberScript (interpreter.get(), & file, false); // will be OR-ed |
| 867 |
interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 868 |
} |
| 869 |
|
| 870 |
static void firstPassThroughScript (MelderFile file, Editor optionalInterpreterOwningEditor, EditorCommand optionalCommand, bool fullTrust) { |
| 871 |
static autoInterpreterStack interpreterStack; |
| 872 |
//UiPause_cleanUp (); // TODO: needed? |
| 873 |
try { |
| 874 |
autostring32 text = MelderFile_readText (file); |
| 875 |
/* |
| 876 |
We switch between default directories no fewer than four times: |
| 877 |
1. runScript() tends to be called from a script that we call the "caller"; |
| 878 |
when we enter runScript(), the default directory is the caller's folder, |
| 879 |
as was appropriate for the use of file names in the caller before runScript(), |
| 880 |
which had to be interpreted relative to the caller's folder. |
| 881 |
2. runScript() will call a script that we call the "callee"; |
| 882 |
include files have to be included from the callee's folder. |
| 883 |
3. For expanding any infile/outfile/folder arguments to runScript(), |
| 884 |
we have to be back in the caller's folder. |
| 885 |
4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 886 |
5. After runScript() finishes, we will have to be back in the caller's folder, |
| 887 |
so that the use of file names in the caller after runScript() |
| 888 |
will be interpreted relative to the caller's folder again. |
| 889 |
*/ |
| 890 |
{// scope |
| 891 |
autoMelderFileSetCurrentFolder folder (file); // so that callee-relative file names can be used for including include files |
| 892 |
Melder_includeIncludeFiles (& text); |
| 893 |
} // back to the default directory of the caller |
| 894 |
|
| 895 |
interpreterStack = InterpreterStack_create (optionalInterpreterOwningEditor); |
| 896 |
autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 897 |
interpreterStack.get(), |
| 898 |
optionalInterpreterOwningEditor, |
| 899 |
file // so that callee-relative file names can be used inside the script |
| 900 |
); |
| 901 |
Interpreter_rememberScript (interpreter.get(), file, fullTrust); // this is early, but possible here because the name of the script cannot change (and NEEDED for "passing" to second pass) |
| 902 |
|
| 903 |
const integer numberOfParameters = Interpreter_readParameters (interpreter.get(), text.get()); |
| 904 |
if (numberOfParameters > 0) { |
| 905 |
const GuiWindow parentShell = ( optionalInterpreterOwningEditor ? optionalInterpreterOwningEditor -> windowForm : theCurrentPraatApplication -> topShell ); |
| 906 |
autoUiForm form = Interpreter_createForm (interpreter.get(), parentShell, optionalInterpreterOwningEditor, |
| 907 |
MelderFile_peekPath (file), secondPassThroughScript, nullptr, false); |
| 908 |
UiForm_do (form.get(), false); |
| 909 |
if (optionalCommand) { |
| 910 |
optionalCommand -> d_uiform = form.move(); |
| 911 |
} else { |
| 912 |
UiForm_destroyWhenUnmanaged (form.get()); |
| 913 |
form. releaseToUser(); |
| 914 |
} |
| 915 |
} else { |
| 916 |
{// scope |
| 917 |
autoPraatBackground background; |
| 918 |
interpreterStack -> emptyAll (); |
| 919 |
//Interpreter_rememberScript (interpreter.get(), file, false); // superfluous |
| 920 |
interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 921 |
} |
| 922 |
} |
| 923 |
} catch (MelderError) { |
| 924 |
Melder_throw (U"Script ", file, U" not completed."); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
void DO_RunTheScriptFromAnyAddedMenuCommand (UiForm /* sendingForm_dummy */, integer /* narg */, Stackel /* args */, |
| 929 |
conststring32 scriptPath, Interpreter /* interpreter */, |
| 930 |
conststring32 /* invokingButtonTitle */, bool /* modified */, void *, Editor optionalInterpreterOwningEditor) |
| 931 |
{ |
| 932 |
structMelderFile file { }; |
| 933 |
Melder_relativePathToFile (scriptPath, & file); |
| 934 |
firstPassThroughScript (& file, optionalInterpreterOwningEditor, nullptr, false); |
| 935 |
} |
| 936 |
|
| 937 |
void praat_runScriptWithForm (conststring32 fileName, bool fullTrust) { |
| 938 |
structMelderFile file { }; |
| 939 |
Melder_relativePathToFile (fileName, & file); |
| 940 |
firstPassThroughScript (& file, nullptr, nullptr, fullTrust); |
| 941 |
} |
| 942 |
|
| 943 |
void praat_executeScriptFromEditorCommand (Editor interpreterOwningEditor, EditorCommand command, conststring32 scriptPath) { |
| 944 |
Melder_assert (interpreterOwningEditor); |
| 945 |
Melder_assert (command); |
| 946 |
Melder_assert (command -> d_editor == interpreterOwningEditor); |
| 947 |
structMelderFile file { }; |
| 948 |
Melder_relativePathToFile (scriptPath, & file); |
| 949 |
firstPassThroughScript (& file, interpreterOwningEditor, command, false); |
| 950 |
} |
| 951 |
|
| 952 |
/* End of file praat_script.cpp */ |