boxcutter / windows

Virtual machine templates for Windows written in legacy JSON and Batch Scripting/JScript
Apache License 2.0
753 stars 266 forks source link

Missing labels for "eof" in 00-run-all-scripts.cmd and zzz-debug-log.cmd #259

Closed arizvisa closed 3 years ago

arizvisa commented 3 years ago

As per the post by @arizvisa (me) in https://github.com/boxcutter/windows/issues/258#issuecomment-751147487, double-checking all the labels used by the batch files under floppy/ identified that the two scripts, "floppy/00-run-all-scripts.cmd" and "floppy/zzz-debug-log.cmd", are branching to an undefined label, ":eof".

This looks like a minor fix, but I don't recall if the logic for a non-existent label is to terminate the script, or to just emit an error and continue execution. Hence the decision to remove these labels or fix them is up-in-the air until I confirm that. Since it's the holidays I won't be able to get to actually testing any fixes for this until sometime after, or unless someone else confirms the logic.

This issue is just a placeholder for a PR.

arizvisa commented 3 years ago

Okay, finally...

So...from testing, when a branch is made to a nonexistent label, the script will terminate with an error message. More importantly, the ERRORLEVEL seems to be the same. If you're calling a label, and the execution terminates due to an invalid branch, execution will resume where the call would've returned. Hopefully this isn't different when running scripts with command.com.

This means that the ":find_tee" function (in both files) is depending on the "goto :eof" to fail so that the function can be exited properly. The function calls in both files actually seem to use these semantics, even. I'm not sure why the original committer opted for this methodology, but I believe this should be replaced with a proper call to "exit".

arizvisa commented 3 years ago

I untagged "BUG" and "HELPWANTED" in favor of "ENHANCEMENT" because despite the label name being nonexistent, the invalid label results in the intended consequences. Nonetheless, I'm going to create a PR for it so that it can be tested and schedule the PR at the very least priority.

arizvisa commented 3 years ago

Okay. Correction...I'm dropping this. The documentation for "GOTO" reads as follows:

Directs cmd.exe to a labeled line in a batch program.

GOTO label

  label   Specifies a text string used in the batch program as a label.

You type a label on a line by itself, beginning with a colon.

If Command Extensions are enabled GOTO changes as follows:

GOTO command now accepts a target label of :EOF which transfers control
to the end of the current batch script file.  This is an easy way to
exit a batch script file without defining a label.  Type CALL /?  for a
description of extensions to the CALL command that make this feature
useful.

So, "GOTO :EOF" is actually the proper way to exit a function in batch files. Dan Gookin of "Advanced MS-DOS Batch File Programming" is likely turning in his grave?

Re-tagged this issue as "WONTFIX", and closing it.