Tuesday, August 21, 2018

New article series: code reviews

I've been thinking of ways to get new developers interested in working on FreeDOS, or at least contributing to FreeDOS in some way. I recognize that DOS is an old concept for some, and as a result, the way you construct programs for DOS can differ to how you'd construct programs for other operating systems such as Linux.

Sure, programs like Choice and Type and Find and other everyday command-line utilities are pretty straightforward. You'd probably write a DOS Type command in the same way you'd write a Unix ‘cat’ command (using streams). But other programs require different methods.

So I thought some kind of “code review” would be helpful, as a way to demonstrate certain DOS programming methods. I'll plan to examine a FreeDOS program and pull apart a specific function or method or programming trick to show how you can use these methods in your own programs.

It will be easiest for me to do a code review on my own programs, and I may start there. But I will try to explore different FreeDOS programs written by different people, so you can see a variety of methods. Every programmer is different, and this will highlight different ways to implement features or optimize execution or minimize footprint when programming in DOS.

Do you have a program you'd like to highlight? I welcome any guest posts in this series. If you have something you'd like to show off, please email me or leave a comment below. I'd be happy to include your article as a guest post here. (If you submit a guest post, it will be easiest if you can share your contribution under the Creative Commons Sharealike [cc-by] public license.)

What programs or methods would you like to see? If you have a suggestion for a “deep dive” into a FreeDOS utility, let me know! My expertise is C, so I am more likely to do code reviews for programs written in C.

Thursday, August 2, 2018

Expanding the FreeDOS COMMAND.COM

The FreeDOS COMMAND.COM (also known as "FreeCOM") is a compatible equivalent to MS-DOS COMMAND.COM. FreeCOM doesn't do anything special; it just creates a command prompt that lets you run programs.

FreeCOM also supports the standard MS-DOS batch programming syntax. Any batch script that runs in MS-DOS should run in FreeCOM. And any batch script you write for FreeCOM will run in MS-DOS COMMAND.COM.

From a recent discussion on the email list, I'd like to suggest an extension to FreeCOM. Maybe this would be better implemented as part of a new alternative shell, but it would be really interesting to see it added as a feature to FreeCOM.

Here's my idea: create a BASIC-like programming language that also supports MS-DOS batch scripts.

DOS batch scripts are simple affairs. Really, the idea behind the MS-DOS batch was to "batch up" a bunch of simple commands. DOS batch programming supports minimal flow control. You can query the exit status (ERRORLEVEL) of the previous command, and you can jump around the script (GOTO) but DOS batch programming doesn't support much else.

I'd love to see an extension to DOS batch programming. Something like a BASIC interpreter would be interesting, if it executed commands in the PATH if the instruction wasn't a keyword.

I wonder if someone is interested in writing a BASIC-like "shell"? Something that uses a combination of BASIC and COMMAND.COM syntax would make the interpreter a "superset" of COMMAND.COM, and an interesting new alternative shell.

Some suggestions for how to extend BASIC and DOS batch programming include:

Variables


Variables can store either numerical or string data. Variable names can be of any reasonable length, and can be referenced in uppercase or lowercase.

Use SET to assign values to variables, and BASIC's LET to assign values based on arithmetic. LET becomes a superset of SET:
SET A=4
SET DIR=C:\FDOS\BIN

And surround a variable with % to reference its value:
SET A=4
LET A=%A%​ ​+​ ​1
ECHO %A%​

Possibly allow %% as a prefix to reference the value from a variable, interchangeably: (sloppy, but see later)
SET A=4
LET A=%%A + 1
ECHO %%A

Output


Use ECHO to display output instead of PRINT:
ECHO Hello world

Tests


Support the standard DOS batch programming tests, such as == to test equality, and NOT to negate a test. Add extra constructs to test inequality, greater-than and less-than. One possible solution is to support simple DOS batch programming IF constructs and use Unix-style brackets for the extra constructs:
IF ERRORLEVEL 0 ECHO Success
IF NOT ERRORLEVEL 0 ECHO Fail
IF EXIST FILE.TXT ECHO Exists

And:
IF %VALUE%==1 ECHO Equal
IF NOT %VALUE%==1 ECHO Not equal

And:
IF [ %VALUE% EQ 1 ] ECHO Equal
IF [ %VALUE% NE 1 ] ECHO Not equal
IF [ %VALUE% GT 1 ] ECHO Greater
IF [ %VALUE% GE 1 ] ECHO Greater or equal
IF [ %STR% LT "ABC" ] ECHO Less

Looping


The FOR statement would need to be extended as a for-next loop:
FOR N IN 1 TO 10
ECHO %N%
NEXT

Or as the traditional DOS for-do one-line shortcut:
FOR N IN 1 TO 10 DO ECHO %N%

And add a special IN (…) construct to create an iteration over a set:
FOR %%N IN (1 2 3 4 5 6 7 8 9 10) DO ECHO %%N
FOR %%FILE IN (*.TXT) DO ECHO %%F

​(If the language allows %A% and %%A then DOS batch programs would probably work seamlessly. But it's sloppy coding. Other ideas?)

Branching


Similar to both BASIC and DOS batch programming, GOTO could use : to mark labels:
SET N=1
:LOOP
ECHO %N%
LET N=%N% + 1
IF %N% LT 10 GOTO LOOP

Comments


And of course, comments remain the same from BASIC and DOS batch programming:
REM This is a comment

Execution


If you called the BASIC-like "shell" using a special parameter (like /P) then it would be nice for anything e​lse you specify that isn't recognized as a shell statement (assuming other syntax is correct) get treated like an external command. That would make this more like a shell:
IF EXIST FILE.TXT EDIT FILE.TXT

Also other internal constructs for CALL and ERRORLEVEL and SHIFT and PATH, but you get the idea.

If you can preserve COMMAND.COM syntax and add a BASIC-like programming language behind it, that would make this an interesting extended shell. Maybe an idea for an interested developer?
Note: Tom points out that Microsoft similarly extended the COMMAND.COM batch programming language when creating CMD.EXE. For example, for /L %%N in (1,1,10) do echo %%N (iterative loops) and SET /A A=%%A + 1 (arithmetic). So this hybrid BASIC-like batch programming language idea is not new.