23

I am currently experimenting with DOS and am trying to run the command prompt on the serial terminal through QEMU. This is the command I have been using.

command >COM1 2>COM1 <COM1

This should in theory connect command.com's stdin, stdout, and stderr to the terminal at COM1. However, it only seems to redirect stdin and stdout. How can I redirect stderr properly so that error messages appear on COM1?

user3840170
  • 23,072
  • 4
  • 91
  • 150
Lennon McLean
  • 333
  • 2
  • 5

3 Answers3

48

That's because MS/PC-DOS can not redirect STDERR.

This feature was only added later with the shells of OS/2 and Windows NT

Having said that, what you want to do isn't redirecting the streams, but putting the console on another device. That's what CTTY is all about. In this case

  ctty com1

will do the trick. CTTY is available starting with DOS 2.0. It takes any device name, even CLOCK and LPT - which of course may not make much sense :))

Raffzahn
  • 222,541
  • 22
  • 631
  • 918
  • 10
    ... and NUL if you want to shoot yourself in the foot! – Stephen Kitt Dec 22 '21 at 17:25
  • 2
    ahh thanks, works great! so it's basically getty but for dos! – Lennon McLean Dec 22 '21 at 19:11
  • 17
    I thought I knew MS-DOS pretty well but I don't remember CTTY at all! Cool ... – davidbak Dec 23 '21 at 00:35
  • 3
    @davidbak I do remember it because it was "that command that would make your computer useless until you rebooted". – hobbs Dec 24 '21 at 01:57
  • 1
    @davidbak I remember it as the command you would have to manually type in to install Interlnk and Intersvr, which would allow you to connect computers via a null modem cable. It was essential in setting up the computer lab where a lot of the floppy drives didn't work. – trlkly Dec 25 '21 at 02:32
  • 1
    @LennonMcLean It's not really similar to getty, more like chvt. ctty just tells DOS to use a given device as the console. It doesn't stay around handling all the user interaction like getty would. – TooTea Dec 25 '21 at 22:51
  • 1
    Also, don't expect to be able to redirect TUI programs to a serial terminal like you could do on unix systems - DOS TUI programs usually worked in a very different way to ncurses-style unix programs. Even if ansi.sys was available to emulate some basic terminal functionality, DOS TUIs often directly hooked into the video card memory.... – rackandboneman Feb 21 '23 at 01:06
  • what setup will be taken for this? (i assume 9600 8N1?) is there a way to configure this? found it: "MODE COMn[:]baud[,][parity][,][databits][,][stopbits][,][retry]" – Tommylee2k Nov 03 '23 at 09:17
24

As Raffzahn says, you can’t redirect standard error under DOS, at least not with the standard COMMAND.COM. With 4DOS, both output streams can be redirected with >&. That would allow you to redirect to a file as well as a device.

Instead of trying to redirect the input and output streams to use a serial connection, under DOS you’d change the TTY:

ctty com1

This will work with any character device (with varying results if the character device isn’t appropriate).

Note that many DOS programs don’t use the standard input and output streams, but read from the keyboard and write to the screen without using DOS services; such programs won’t honour redirection or CTTY and won’t work over a serial connection.

(Amusingly enough, DOS has five standard device handles: standard input, standard output, standard error, standard auxiliary and standard list. The first three are attached to CON by default; the fourth to AUX, and the fifth to PRN.)

Stephen Kitt
  • 121,835
  • 17
  • 505
  • 462
  • 1
    Interesting bit about 4DOS, as the NT syntax uses this as additional parameters with the stream numbers command >com 1>&2. – Raffzahn Dec 22 '21 at 17:20
  • 4
    @Raffzahn: The 1>&2 syntax was adopted from UNIX, which had it at least as far back as V7 (it's in the manual). (Or maybe from some common ancestor.) The shorthand >& is also from Unix but I guess later. – Nate Eldredge Dec 22 '21 at 18:42
  • 1
    @NateEldredge Sure. After all, MS-DOS .0 was designed with a clear goal to bridge from CP/M (which 1.x essentially was) over to Xenix, which MS considered the future. Except they never added that part. It doesn't make that much sense in a single user single tasking system. So it was up to NT&OS/2 to introduce the use case to catch stderr when running in background. – Raffzahn Dec 23 '21 at 13:52
5

As it happens, when launching a new copy of COMMAND.COM, you can simply pass the name of the terminal device as a command-line parameter, as in:

command com1

This is only available specifically when launching COMMAND.COM (whether from another command prompt or from the SHELL= directive in CONFIG.SYS); it is not a generic redirection syntax for an arbitrary command.

user3840170
  • 23,072
  • 4
  • 91
  • 150