From 4449304aeed23df6b742d988d68f95c7a1b18867 Mon Sep 17 00:00:00 2001
From: "B. Watson" <urchlay@slackware.uk>
Date: Mon, 29 Apr 2024 19:51:33 -0400
Subject: xex1to2: added.

---
 Makefile    |  4 +--
 xex1to2.1   | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xex1to2.c   | 60 ++++++++++++++++++++++++++++++++++++++
 xex1to2.rst | 48 ++++++++++++++++++++++++++++++
 4 files changed, 207 insertions(+), 2 deletions(-)
 create mode 100644 xex1to2.1
 create mode 100644 xex1to2.c
 create mode 100644 xex1to2.rst

diff --git a/Makefile b/Makefile
index 19bb1a0..76d7361 100644
--- a/Makefile
+++ b/Makefile
@@ -15,9 +15,9 @@ CC=gcc
 CFLAGS=-Wall $(COPT) -ansi -D_GNU_SOURCE -DVERSION=\"$(VERSION)\"
 
 # BINS and SCRIPTS go in $BINDIR, DOCS go in $DOCDIR
-BINS=a8eol xfd2atr atr2xfd blob2c cart2xex fenders xexsplit xexcat atrsize rom2cart unmac65 axe blob2xex xexamine
+BINS=a8eol xfd2atr atr2xfd blob2c cart2xex fenders xexsplit xexcat atrsize rom2cart unmac65 axe blob2xex xexamine xex1to2
 SCRIPTS=dasm2atasm a8utf8
-MANS=a8eol.1 xfd2atr.1 atr2xfd.1 blob2c.1 cart2xex.1 fenders.1 xexsplit.1 xexcat.1 atrsize.1 rom2cart.1 unmac65.1 axe.1 dasm2atasm.1 a8utf8.1 blob2xex.1 xexamine.1
+MANS=a8eol.1 xfd2atr.1 atr2xfd.1 blob2c.1 cart2xex.1 fenders.1 xexsplit.1 xexcat.1 atrsize.1 rom2cart.1 unmac65.1 axe.1 dasm2atasm.1 a8utf8.1 blob2xex.1 xexamine.1 xex1to2.1
 MAN5S=xex.5
 DOCS=README equates.inc *.dasm
 
diff --git a/xex1to2.1 b/xex1to2.1
new file mode 100644
index 0000000..96b2c37
--- /dev/null
+++ b/xex1to2.1
@@ -0,0 +1,97 @@
+.\" Man page generated from reStructuredText.
+.
+.
+.nr rst2man-indent-level 0
+.
+.de1 rstReportMargin
+\\$1 \\n[an-margin]
+level \\n[rst2man-indent-level]
+level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
+-
+\\n[rst2man-indent0]
+\\n[rst2man-indent1]
+\\n[rst2man-indent2]
+..
+.de1 INDENT
+.\" .rstReportMargin pre:
+. RS \\$1
+. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
+. nr rst2man-indent-level +1
+.\" .rstReportMargin post:
+..
+.de UNINDENT
+. RE
+.\" indent \\n[an-margin]
+.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
+.nr rst2man-indent-level -1
+.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
+.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
+..
+.TH "XEX1TO2" 1 "2024-04-29" "0.2.1" "Urchlay's Atari 8-bit Tools"
+.SH NAME
+xex1to2 \- Convert an Atari DOS 1.0 executable to a standard Atari executable
+.\" RST source for xex1to2(1) man page. Convert with:
+.
+.\" rst2man.py xex1to2.rst > xex1to2.1
+.
+.SH SYNOPSIS
+.sp
+xex1to2 [\fIinfile\fP] [\fIoutfile\fP]
+.SH DESCRIPTION
+.sp
+\fBxex1to2\fP reads a binary load file created by Atari DOS 1.0 and
+converts it to a standard Atari executable, as used by Atari DOS 2.0S
+and all other Atari DOSes.
+.sp
+DOS 1.0\(aqs executable format is segmented, like the XEX format,
+but the signature bytes are \fB$84\fP, \fB$09\fP rather than \fB$FF\fP,
+\fB$FF\fP\&. Also, the signature bytes can \fIonly\fP occur for the first
+segment (the start of the file). Further segments just have a 4\-byte
+header with the start and end addresses.
+.sp
+All this utility really does is copy \fIinfile\fP to \fIoutfile\fP, replacing
+the first two bytes (which must be \fB$84\fP, \fB$09\fP) with \fB$FF\fP,
+\fB$FF\fP\&.
+.sp
+If \fIoutfile\fP is omitted, output is written to \fBstdout\fP\&. If \fIinfile\fP
+is also omitted, input is read from \fBstdin\fP\&. \fBxex1to2\fP will not
+write to \fBstdout\fP if it\(aqs a terminal.
+.sp
+To convert the other way, from a DOS 2.0 compatible XEX file to a DOS
+1.0 executable, use \fBxexcat \-1\fP\&.
+.SH EXIT STATUS
+.sp
+Exit status is zero for success, non\-zero for failure.
+.SH COPYRIGHT
+.sp
+WTFPL. See \fI\%http://www.wtfpl.net/txt/copying/\fP for details.
+.SH AUTHOR
+.INDENT 0.0
+.IP B. 3
+Watson <\fI\%urchlay@slackware.uk\fP>; Urchlay on irc.libera.chat \fI##atari\fP\&.
+.UNINDENT
+.SH SEE ALSO
+.sp
+\fBa8eol\fP(1),
+\fBa8utf8\fP(1),
+\fBatr2xfd\fP(1),
+\fBatrsize\fP(1),
+\fBaxe\fP(1),
+\fBblob2c\fP(1),
+\fBblob2xex\fP(1),
+\fBcart2xex\fP(1),
+\fBdasm2atasm\fP(1),
+\fBf2toxex\fP(1),
+\fBfenders\fP(1),
+\fBrom2cart\fP(1),
+\fBunmac65\fP(1),
+\fBxexamine\fP(1),
+\fBxexcat\fP(1),
+\fBxexsplit\fP(1),
+\fBxfd2atr\fP(1),
+\fBxex\fP(5).
+.sp
+Any good Atari 8\-bit book: \fIDe Re Atari\fP, \fIThe Atari BASIC  Reference
+Manual\fP,  the  \fIOS Users\(aq Guide\fP, \fIMapping the Atari\fP, etc.
+.\" Generated by docutils manpage writer.
+.
diff --git a/xex1to2.c b/xex1to2.c
new file mode 100644
index 0000000..36a33a2
--- /dev/null
+++ b/xex1to2.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#define SELF "xex1to2"
+
+FILE *open_file(const char *name, const char *mode) {
+	FILE *f = fopen(name, mode);
+	if(f) return f;
+
+	fprintf(stderr, SELF ": %s: %s\n", name, strerror(errno));
+	exit(1);
+}
+
+void invalid(void) {
+	fprintf(stderr, SELF ": input is not a valid Atari DOS 1.0 executable.\n");
+	exit(1);
+}
+
+void read_header(FILE *in) {
+	int c;
+
+	c = getc(in);
+	if(c < 0 || c != 0x84) invalid();
+	c = getc(in);
+	if(c < 0 || c != 0x09) invalid();
+}
+
+int main(int argc, char **argv) {
+	int c;
+	FILE *in = stdin, *out = stdout;
+
+	if(argc > 3) {
+		fprintf(stderr, "Usage: " SELF " [dos1_input.xex] [dos2_output.xex]\n");
+		exit(1);
+	}
+
+	if(argc > 1) in  = open_file(argv[1], "rb");
+	if(argc > 2) out = open_file(argv[2], "wb");
+
+	if(isatty(fileno(out))) {
+		fprintf(stderr,
+				SELF ": Standard output is a terminal; not writing binary data\n");
+		exit(1);
+	}
+
+	read_header(in);
+	fputc(0xff, out);
+	fputc(0xff, out);
+
+	while( (c = fgetc(in)) >= 0 )
+		fputc(c, out);
+
+	fclose(in);
+	fclose(out);
+
+	return 0;
+}
diff --git a/xex1to2.rst b/xex1to2.rst
new file mode 100644
index 0000000..44464f8
--- /dev/null
+++ b/xex1to2.rst
@@ -0,0 +1,48 @@
+.. RST source for xex1to2(1) man page. Convert with:
+..   rst2man.py xex1to2.rst > xex1to2.1
+
+=======
+xex1to2
+=======
+
+------------------------------------------------------------------
+Convert an Atari DOS 1.0 executable to a standard Atari executable
+------------------------------------------------------------------
+
+.. include:: manhdr.rst
+
+SYNOPSIS
+========
+
+xex1to2 [*infile*] [*outfile*]
+
+DESCRIPTION
+===========
+
+**xex1to2** reads a binary load file created by Atari DOS 1.0 and
+converts it to a standard Atari executable, as used by Atari DOS 2.0S
+and all other Atari DOSes.
+
+DOS 1.0's executable format is segmented, like the XEX format,
+but the signature bytes are **$84**, **$09** rather than **$FF**,
+**$FF**. Also, the signature bytes can *only* occur for the first
+segment (the start of the file). Further segments just have a 4-byte
+header with the start and end addresses.
+
+All this utility really does is copy *infile* to *outfile*, replacing
+the first two bytes (which must be **$84**, **$09**) with **$FF**,
+**$FF**.
+
+If *outfile* is omitted, output is written to **stdout**. If *infile*
+is also omitted, input is read from **stdin**. **xex1to2** will not
+write to **stdout** if it's a terminal.
+
+To convert the other way, from a DOS 2.0 compatible XEX file to a DOS
+1.0 executable, use **xexcat -1**.
+
+EXIT STATUS
+===========
+
+Exit status is zero for success, non-zero for failure.
+
+.. include:: manftr.rst
-- 
cgit v1.2.3