Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active April 10, 2023 18:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/48183217173cdaef580b2cb1797a052c to your computer and use it in GitHub Desktop.
Save tresf/48183217173cdaef580b2cb1797a052c to your computer and use it in GitHub Desktop.
Printing to a raw printer from DOSBox

Printing to a raw printer from DOSBox

Summary

  • Although DOSBox offers printing support, the documentation is sparse and most articles refer to printing on a Windows host.
  • Using DOSBox 0.7.4-3 I was unable to get printing working, so I'm using DOSBox-X instead.
  • Using Ubuntu 20.04 and a fork of DOSBox called DOSBox-X, printing can be configured as follows.

Prerequisites

This tutorial assumes you already have a raw printer setup and set as the default printer. For assistance setting up a default raw printer in Linux see here

Contents

  1. Install DOSBox-X
  2. Configure DOSBox-X to write to a physical file
  3. Write a script which listens on the physical file

Steps

Install DOSBox-X

Installing DOSBox-X on Linux is as simple as installing the snap: https://snapcraft.io/dosbox-x. This can be launched with Ubuntu Software center. If you're not on Ubutunt, see if DOXBox-X can be installed through other means. For Windows, MacOS or RPM users, DOXBox-X can be downloaded directly from the official dosbox-x site.

Configure DOSBox-X to write to a physical file

Although the recommendation for most tutorials is to configure DOSBox-X to print to a physical printer, I was unable to get this working on Ubuntu 20.04. Instead, I had to manually configure DOSBox-X to print to a file.

  1. Start DOSBox-X for the first time from Applications (or dosbox-x from Terminal)
  2. Save a configuration file (the default location is your $HOME directory, this is fine)
    • Click: Main, Configuration Gui
    • Click: Parallel
      For parallel1, type:
      file append:prnout.txt
    • Click: Configuration, Save (This is important!)
    • Click: OK

Write a script which listens on the physical file

Finally, write a bash script which listens on this prnout.txt file.

  1. Create a file called printlistener.sh

    gedit printlistener.sh &

    Paste the following

    #!/bin/bash
    
    # File to look for
    TOPRINT=~/prnout.txt
    
    printf "Waiting for content at $TOPRINT\n"
    
    while [ true ]; do
      printf "."
      sleep 1 # one second
      if test -f "$TOPRINT"; then
        printf "\n[$(date)] Sending $(wc -c "$TOPRINT" | awk '{print $1}') bytes\n"
        lpr -o raw "$TOPRINT" && rm "$TOPRINT"
      fi
    done
  2. Make it executable:

    chmod +x printlistener.sh
  3. Start the listener

    ./printlistener.sh

Last, print from the DOS application and it will send to your printer. Note, this will send content in a text format. This may be undesirable for certain printers.

Final Thoughts

DOSBox-X should have printer support which should be preferred, but due to the inability to get it to work, this workaround was created. It may be worthwhile to file this bug against the DOXBox-X code repository. The workaround uses a flat file and there are some grave limitations to printing using a flat file. Specifically, the above example may contain race conditions for when the file is partially written. Also, it relies on a relative file path, which may differ between environments. Please take this into consideration before using for critical services. Feel free to comment, fork or reuse this information in part or whole under public domain.

@tresf
Copy link
Author

tresf commented Jun 18, 2020

Apparently the incron tool can serve as the file watchdog in place of the more well-knowncrontab/printlistener script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment