mirror of
https://github.com/open-simh/simtools.git
synced 2026-04-29 13:12:09 +00:00
New tape manipulation tools
rawtap Allows extract, create and append operations on .tap files. cpytap Copies a .tap file to a new .tap file while allowing file level edits; skip file, replace file, append files and insert files. Any files copied from the original source .tap will have their internal record structure maintained. cosy COSY is the compressed format used by the CDC1700. This program allows for extraction of all files from an archive and the creation of a new archive. It assumes that you would have used raw tap about to have extracted the COSY file from a tape. dbtap Utility to read, write and list .tap containers written in the DOS/BATCH-11 format. It understands ascii and binary modes and can be used to transfer files in and out of most PDP-11 operating systems (not sure about RSTS/E), early VMS and early TOPS-10 systems.
This commit is contained in:
committed by
Mark Pizzolato
parent
94d94fe695
commit
b08ebe8eb0
21
extracters/dbtap/Makefile
Normal file
21
extracters/dbtap/Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
# all of these can be over-ridden on the "make" command line if don't suit
|
||||
# your environment
|
||||
TOOL=dbtap
|
||||
CFLAGS=-O2 -Wall -Wshadow -Wextra -pedantic -Woverflow -Wstrict-overflow
|
||||
BIN=/usr/local/bin
|
||||
INSTALL=install
|
||||
CC=gcc
|
||||
|
||||
$(TOOL): $(TOOL).c tapeio.c dos11.c tapeio.h dos11.h tap.h defs.h
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $(TOOL) $(TOOL).c tapeio.c dos11.c $(LDLIBS)
|
||||
|
||||
.PHONY: clean install uninstall
|
||||
|
||||
clean:
|
||||
rm -f $(TOOL)
|
||||
|
||||
install: $(TOOL)
|
||||
$(INSTALL) -p -m u=rx,g=rx,o=rx $(TOOL) $(BIN)
|
||||
|
||||
uninstall:
|
||||
rm -f $(BIN)/$(TOOL)
|
||||
318
extracters/dbtap/dbtap.c
Normal file
318
extracters/dbtap/dbtap.c
Normal file
@@ -0,0 +1,318 @@
|
||||
/* dbtap.c: process DOS/BATCH-11 tape files within a SIMH .tap container
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "dos11.h"
|
||||
#include "tapeio.h"
|
||||
char *ascii = "";
|
||||
int extra = 0, strict = 0, reclen = 512;
|
||||
uint8 prog = 1, proj = 1;
|
||||
|
||||
int list = 0, create = 0, append = 0, extract = 0;
|
||||
|
||||
/*++
|
||||
* usage
|
||||
*
|
||||
* Display a usage message on stderr and exit.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
void usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage: dbtap -lcae [-r len -A <list> -E -S -P pg,pj] container [...]\n\n");
|
||||
fprintf(stderr,
|
||||
" List contents of container file(s):\n");
|
||||
fprintf(stderr,
|
||||
" dbtap -l container ...\n\n");
|
||||
fprintf(stderr,
|
||||
" Create new container and append file(s):\n");
|
||||
fprintf(stderr,
|
||||
" dbtap -c [-r len -A <list> -S -P pg,pj] container file ...\n\n");
|
||||
fprintf(stderr,
|
||||
" Append file(s) to an existing container:\n");
|
||||
fprintf(stderr,
|
||||
" dbtap -a [-r len -A <list> -S -P pg,pj] container file ...\n\n");
|
||||
fprintf(stderr,
|
||||
" Extract files from container(s):\n");
|
||||
fprintf(stderr,
|
||||
" dbtap -e [-A <list> -E] container ...\n\n");
|
||||
fprintf(stderr, "\nSwitches:\n\n");
|
||||
fprintf(stderr,
|
||||
"-r len - Specifiy max tape record size (512 <= len <= 65536)\n");
|
||||
fprintf(stderr,
|
||||
"-A <list> - Specify filename extension for ASCII tranefer mode\n");
|
||||
fprintf(stderr,
|
||||
" e.g. \".MAC,.BAT\" or \".MAC:.BAT\"\n");
|
||||
fprintf(stderr,
|
||||
"-E - Include prog,proj in filename when extracting\n");
|
||||
fprintf(stderr,
|
||||
" e.g. FILE_ggg_jjj.EXT\n");
|
||||
fprintf(stderr,
|
||||
"-S - Use DOS/BATCH-11 6+3 filenames (rather than 9+3)\n");
|
||||
fprintf(stderr,
|
||||
"-P pg,pj - Specify prog,proj number when writing to tape\n");
|
||||
fprintf(stderr,
|
||||
" Numbers are in octal. Default is [1,1].\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*++
|
||||
* main
|
||||
*
|
||||
* Entry point for the dbtap program.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* argc - # of supplied arguments
|
||||
* argv - array of argument strings
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* Exit status for dbtap
|
||||
*
|
||||
--*/
|
||||
int main(
|
||||
int argc,
|
||||
char *argv[]
|
||||
)
|
||||
{
|
||||
int ch;
|
||||
uint8 myprog, myproj;
|
||||
|
||||
while ((ch = getopt(argc, argv, "lcaer:A:EP:S")) != -1) {
|
||||
switch (ch) {
|
||||
case 'l':
|
||||
list = 1;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
create = 1;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
append = 1;
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
extract = 1;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
reclen = strtoul(optarg, NULL, 0);
|
||||
if (reclen < 512)
|
||||
reclen = 512;
|
||||
if (reclen > MAXRCLNT)
|
||||
reclen = MAXRCLNT;
|
||||
break;
|
||||
|
||||
case 'A':
|
||||
ascii = optarg;
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
extra = 1;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
if (sscanf(optarg, "%hho,%hho", &myprog, &myproj) != 2)
|
||||
usage();
|
||||
|
||||
prog = myprog;
|
||||
proj = myproj;
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
strict = 1;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
/*
|
||||
* Only one of -l, -c, -a, -e can be specified.
|
||||
*/
|
||||
if ((list + create + append + extract) != 1)
|
||||
usage();
|
||||
|
||||
if (list != 0) {
|
||||
/*
|
||||
* List directories on one or more container files.
|
||||
*/
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
while (argc >= 1) {
|
||||
switch (OpenTapeForRead(argv[0])) {
|
||||
case TIO_SUCCESS:
|
||||
case TIO_ERROR:
|
||||
printf("%s:\n\n", argv[0]);
|
||||
listDirectory();
|
||||
printf("\n");
|
||||
CloseTape();
|
||||
break;
|
||||
|
||||
case TIO_CORRUPT:
|
||||
fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
|
||||
exit(2);
|
||||
|
||||
case TIO_OPENFAIL:
|
||||
fprintf(stderr, "%s open failed\n", argv[0]);
|
||||
exit(3);
|
||||
}
|
||||
argc--, argv++;
|
||||
}
|
||||
} else if (create != 0) {
|
||||
/*
|
||||
* Create a new container file and append 0 or more files to the tape.
|
||||
*/
|
||||
if (argc <= 1)
|
||||
usage();
|
||||
|
||||
switch (OpenTapeForWrite(argv[0])) {
|
||||
case TIO_SUCCESS:
|
||||
argc--, argv++;
|
||||
|
||||
while (argc >= 1) {
|
||||
switch (appendFile(argv[0], ascii, prog, proj, reclen, strict)) {
|
||||
case -1:
|
||||
fprintf(stderr, "Failed to append %s to tape\n", argv[0]);
|
||||
fprintf(stderr, "Container file is probably corrupt\n");
|
||||
exit(6);
|
||||
|
||||
case 1:
|
||||
fprintf(stderr, "Failed to open file %s - skipping\n", argv[0]);
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
argc--, argv++;
|
||||
}
|
||||
CloseTape();
|
||||
break;
|
||||
|
||||
case TIO_IOERROR:
|
||||
fprintf(stderr, "Error writing to container file - %s\n", argv[0]);
|
||||
exit(5);
|
||||
|
||||
case TIO_CREATEFAIL:
|
||||
fprintf(stderr, "Failed to create container file - %s\n", argv[0]);
|
||||
exit(3);
|
||||
}
|
||||
} else if (append != 0) {
|
||||
/*
|
||||
* Open an existing container file and append 0 or more files to the tape.
|
||||
*/
|
||||
if (argc <= 1)
|
||||
usage();
|
||||
|
||||
switch (OpenTapeForAppend(argv[0])) {
|
||||
case TIO_SUCCESS:
|
||||
case TIO_ERROR:
|
||||
argc--, argv++;
|
||||
|
||||
while (argc >= 1) {
|
||||
switch (appendFile(argv[0], ascii, prog, proj, reclen, strict)) {
|
||||
case -1:
|
||||
fprintf(stderr, "Failed to append %s to tape\n", argv[0]);
|
||||
fprintf(stderr, "Container file is probably corrupt\n");
|
||||
exit(6);
|
||||
|
||||
case 1:
|
||||
fprintf(stderr, "Failed to open file %s - skipping\n", argv[0]);
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
argc--, argv++;
|
||||
}
|
||||
CloseTape();
|
||||
break;
|
||||
|
||||
case TIO_CORRUPT:
|
||||
fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
|
||||
exit(2);
|
||||
|
||||
case TIO_OPENFAIL:
|
||||
fprintf(stderr, "%s open failed\n", argv[0]);
|
||||
exit(3);
|
||||
}
|
||||
} else if (extract != 0) {
|
||||
/*
|
||||
* Extract files from one or more container files.
|
||||
*/
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
while (argc >= 1) {
|
||||
switch (OpenTapeForRead(argv[0])) {
|
||||
case TIO_SUCCESS:
|
||||
case TIO_ERROR:
|
||||
printf("%s:\n\n", argv[0]);
|
||||
extractFiles(ascii, extra);
|
||||
printf("\n");
|
||||
CloseTape();
|
||||
break;
|
||||
|
||||
case TIO_CORRUPT:
|
||||
fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
|
||||
exit(2);
|
||||
|
||||
case TIO_OPENFAIL:
|
||||
fprintf(stderr, "%s open failed\n", argv[0]);
|
||||
exit(3);
|
||||
}
|
||||
argc--, argv++;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
43
extracters/dbtap/dbtap.txt
Normal file
43
extracters/dbtap/dbtap.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
dbtap manipulates .tap magtape container files used by SIMH and written in
|
||||
DOS/BATCH-11 file format. dbtap is invoked by:
|
||||
|
||||
dbtap -lcae [-r len -A <list> -E -S -P pg,pj] container [...]
|
||||
|
||||
To list the contents of one or more container files:
|
||||
|
||||
dbtap -l container ...
|
||||
|
||||
To create a new container file and append file(s):
|
||||
|
||||
dbtap -c [-r len -A <list> -S -P pg,pj] container file ...
|
||||
|
||||
To append file(s) to an existing container:
|
||||
|
||||
dbtap -a [-r len -A <list> -S -P pg,pj] container file ...
|
||||
|
||||
To extract file(s) from container(s):
|
||||
|
||||
dbtap -e [-A <list> -E] container ...
|
||||
|
||||
Switches:
|
||||
|
||||
-r len Specify max tape record size when writing.
|
||||
(512 <= len <= 65536).
|
||||
|
||||
-A <list> By default, files are transferred in Binary mode. This switch
|
||||
is used to specify file extensions which should be transferred
|
||||
in Ascii mode (e.g. ".MAC:.BAT" or ".MAC,.BAT").
|
||||
|
||||
-E Include prog,proj in filename when extracting.
|
||||
(e.g. FILE_ggg_jjj.EXT").
|
||||
|
||||
-S DOS/BATCH-11 uses 6+3 (name+extension) for filenames including
|
||||
on magtape. Over time, the magtape definition changed to
|
||||
allow 9+3 filenames by adding an extra word to the filename
|
||||
record. By default, this program uses the 9+3 format but
|
||||
the -S switch may be used to force 6+3 filenames when writing
|
||||
to tape.
|
||||
|
||||
-P pg,pj Specify prog,proj number when writing to tape. Numbers are in
|
||||
octal. Default is [1,1].
|
||||
|
||||
40
extracters/dbtap/defs.h
Normal file
40
extracters/dbtap/defs.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* defs.h: Common definitions
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __DEFS_H__
|
||||
#define __DEFS_H__
|
||||
#if defined(VMS)
|
||||
#include <ints.h>
|
||||
#else
|
||||
typedef signed char int8;
|
||||
typedef signed short int16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned char uint8;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned int uint32;
|
||||
#endif
|
||||
#endif
|
||||
661
extracters/dbtap/dos11.c
Normal file
661
extracters/dbtap/dos11.c
Normal file
@@ -0,0 +1,661 @@
|
||||
/* dos11.c: Handle DOS/BATCH-11 tape file structure operations
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "dos11.h"
|
||||
#include "tapeio.h"
|
||||
/*
|
||||
* Record buffer.
|
||||
*/
|
||||
char record[MAXRCLNT];
|
||||
|
||||
FILE *file = NULL;
|
||||
|
||||
int CRpending = 0;
|
||||
|
||||
char rad50[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789";
|
||||
|
||||
/*
|
||||
* Table of days/month for both normal and leap years.
|
||||
*/
|
||||
unsigned short mnth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
unsigned short mnthl[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
char *month[12] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
/*++
|
||||
* r50Asc
|
||||
*
|
||||
* Convert 1 16-bit rad50 value into 3 ASCII characters.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* value - rad50 value to be converted
|
||||
* outbuf - pointer to buffer to receive the characters
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
static void r50Asc(
|
||||
uint16 value,
|
||||
char *outbuf
|
||||
)
|
||||
{
|
||||
outbuf[2] = rad50[value % 050];
|
||||
value /= 050;
|
||||
outbuf[1] = rad50[value % 050];
|
||||
outbuf[0] = rad50[value / 050];
|
||||
}
|
||||
|
||||
/*++
|
||||
* r50AscNoSpace
|
||||
*
|
||||
* Convert 1 16-bit value into up to 3 ASCII characters, spaces are dropped
|
||||
* from the conversion.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* value - rad50 value to be converted
|
||||
* outbuf - pointer to the buffer to receive the characters
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* # of none space characters converted
|
||||
*
|
||||
--*/
|
||||
static int r50AscNoSpace(
|
||||
uint16 value,
|
||||
char *outbuf
|
||||
)
|
||||
{
|
||||
int count = 0;
|
||||
int value2 = value / 050;
|
||||
|
||||
/*
|
||||
* The rad50 representation of ' ' is zero.
|
||||
*/
|
||||
if ((value2 / 050) != 0)
|
||||
outbuf[count++] = rad50[value2 / 050];
|
||||
if ((value2 % 050) != 0)
|
||||
outbuf[count++] = rad50[value2 % 050];
|
||||
if ((value % 050) != 0)
|
||||
outbuf[count++] = rad50[value % 050];
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*++
|
||||
* AscR50
|
||||
*
|
||||
* Converts 3 characters into a single 16-bit rad50 value. If an input
|
||||
* character is not in the rad50 character set it is converted to '%'.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* inbuf - pointer to the buffer with the 3 characters
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* rad50 value for the 3 characters
|
||||
*
|
||||
--*/
|
||||
static uint16 AscR50(
|
||||
char *inbuf
|
||||
)
|
||||
{
|
||||
uint16 value;
|
||||
char *ptr;
|
||||
|
||||
if ((ptr = strchr(rad50, toupper(*inbuf++))) == NULL)
|
||||
ptr = strchr(rad50, '%');
|
||||
|
||||
value = (ptr - rad50) * 03100;
|
||||
|
||||
if ((ptr = strchr(rad50, toupper(*inbuf++))) == NULL)
|
||||
ptr = strchr(rad50, '%');
|
||||
|
||||
value += (ptr - rad50) * 050;
|
||||
|
||||
if ((ptr = strchr(rad50, toupper(*inbuf))) == NULL)
|
||||
ptr = strchr(rad50, '%');
|
||||
|
||||
value += ptr - rad50;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*++
|
||||
* dos11Date
|
||||
*
|
||||
* Convert a DOS/BATCH-11 date value into an ASCII string.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* value - DOS/BATCH-11 date value
|
||||
* buf - buffer to receive the string (requires 12 bytes)
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
static void dos11Date(
|
||||
uint16 value,
|
||||
char *buf
|
||||
)
|
||||
{
|
||||
unsigned short year, doyr, leapyr;
|
||||
unsigned short *table;
|
||||
|
||||
/*
|
||||
* The DOS/BATCH-11 date format covers a range of 1970 - 2035.
|
||||
*/
|
||||
year = 1970 + value / 1000;
|
||||
doyr = value % 1000;
|
||||
leapyr = ((year % 4) == 0) && (year != 2000);
|
||||
|
||||
table = leapyr ? mnthl : mnth;
|
||||
|
||||
/*
|
||||
* Check for valid day of year.
|
||||
*/
|
||||
if (doyr < (leapyr ? 366 : 365)) {
|
||||
int i = 0;
|
||||
|
||||
while (doyr > table[i])
|
||||
doyr -= table[i++];
|
||||
|
||||
sprintf(buf, "%2d-%s-%4d", doyr, month[i], year);
|
||||
} else strcpy(buf, "xx-yyy-xxxx");
|
||||
}
|
||||
|
||||
/*++
|
||||
* initAscii
|
||||
*
|
||||
* Initialize variables for ASCII mode transfers (translates CRLF -> LF).
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
static void initAscii(void)
|
||||
{
|
||||
CRpending = 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* xferAscii
|
||||
*
|
||||
* Write data to the current output file while performing CRLF translation.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* ch - the next character to be output to the file
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* # of errors returned by fwrite
|
||||
*
|
||||
--*/
|
||||
static char cr = '\r';
|
||||
|
||||
static int xferAscii(
|
||||
char ch
|
||||
)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
if (ch == '\r') {
|
||||
if (CRpending != 0)
|
||||
if (fwrite(&cr, sizeof(char), 1, file) != 1)
|
||||
count++;
|
||||
CRpending = 1;
|
||||
return count;
|
||||
}
|
||||
|
||||
if (CRpending != 0) {
|
||||
CRpending = 0;
|
||||
if (ch != '\n')
|
||||
if (fwrite(&cr, sizeof(char), 1, file) != 1)
|
||||
count++;
|
||||
}
|
||||
if (fwrite(&ch, sizeof(ch), 1, file) != 1)
|
||||
count++;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*++
|
||||
* flushAscii
|
||||
*
|
||||
* Flush any pending CR to the output file
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* # of errors returned by fwrite (0 or 1)
|
||||
*
|
||||
--*/
|
||||
static int flushAscii(void)
|
||||
{
|
||||
if (CRpending != 0)
|
||||
if (fwrite(&cr, sizeof(char), 1, file) != 1)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* appendFile
|
||||
*
|
||||
* Append a file to the current open tape.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* name - pointer to the filename string to append
|
||||
* ascii - pointer to string containing extensions whose files
|
||||
* are to be appended in ASCII mode.
|
||||
* e.g. ".MAC:.BAT" or ".MAC.BAT"
|
||||
* prog - programmer number for the directory entry
|
||||
* proj - project number for the directory entry
|
||||
* reclen - record length to use on the 'tape'
|
||||
* strict - if 1, use strict DOS/BATCH-11 format file headers
|
||||
* i.e. dos11hdr1
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* 0 if file successfully appended,
|
||||
* 1 if failed to open the input file
|
||||
* -1 if some data may have been written to the tape which is now corrupt
|
||||
*
|
||||
--*/
|
||||
int appendFile(
|
||||
char *name,
|
||||
char *ascii,
|
||||
uint8 prog,
|
||||
uint8 proj,
|
||||
int reclen,
|
||||
int strict
|
||||
)
|
||||
{
|
||||
size_t hdrSz = strict ? sizeof(struct dos11hdr1) : sizeof(struct dos11hdr2);
|
||||
struct dos11hdr2 hdr;
|
||||
char *fname, *ext, *useAscii;
|
||||
time_t now;
|
||||
struct tm *tm;
|
||||
|
||||
memset(&hdr, 0, sizeof(hdr));
|
||||
|
||||
if ((file = fopen(name, "r")) != NULL) {
|
||||
char filename[16], extension[8], exten[8];
|
||||
int i, len;
|
||||
size_t datalen;
|
||||
|
||||
/*
|
||||
* We now need to convert the supplied filename into something that
|
||||
* fits into the DOS/BATCH-11 file header structure(s).
|
||||
*/
|
||||
if ((fname = strrchr(name, '/')) == NULL)
|
||||
fname = name;
|
||||
|
||||
ext = strrchr(fname, '.');
|
||||
|
||||
/*
|
||||
* Fill int the first 'n' bytes of the filename and extension with the
|
||||
* remaining bytes set to ' '.
|
||||
*/
|
||||
memset(&filename, ' ', sizeof(filename));
|
||||
memset(&extension, ' ', sizeof(extension));
|
||||
memset(&exten, 0, sizeof(exten));
|
||||
|
||||
if (ext != NULL)
|
||||
len = ext - fname;
|
||||
else len = strlen(fname);
|
||||
|
||||
if (len > (strict ? 6 : 9))
|
||||
len = strict ? 6 : 9;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
filename[i] = toupper(fname[i]);
|
||||
|
||||
if (ext != NULL) {
|
||||
len = strlen(&ext[1]);
|
||||
if (len > 3)
|
||||
len = 3;
|
||||
|
||||
for (i = 0; i < len + 1; i++) {
|
||||
extension[i] = toupper(ext[i]);
|
||||
exten[i] = toupper(ext[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Construct the directory entry
|
||||
*/
|
||||
hdr.fname[0] = AscR50(&filename[0]);
|
||||
hdr.fname[1] = AscR50(&filename[3]);
|
||||
if (!strict)
|
||||
hdr.fname3 = AscR50(&filename[6]);
|
||||
if (ext != NULL)
|
||||
hdr.ext = AscR50(&extension[1]);
|
||||
|
||||
hdr.proj = proj;
|
||||
hdr.prog = prog;
|
||||
hdr.prot = 0233;
|
||||
|
||||
now = time(NULL);
|
||||
tm = localtime(&now);
|
||||
|
||||
hdr.date = ((tm->tm_year - 70) * 1000) + tm->tm_yday + 1;
|
||||
|
||||
useAscii = strstr(ascii, exten);
|
||||
|
||||
if (WriteTapeRecord(&hdr, hdrSz) == 0) {
|
||||
initTapeBuffering(reclen);
|
||||
while ((datalen = fread(record, sizeof(char), reclen, file)) != 0) {
|
||||
if (ferror(file))
|
||||
goto failed;
|
||||
|
||||
if (useAscii != NULL) {
|
||||
size_t j;
|
||||
|
||||
for (j = 0; j < datalen; i++) {
|
||||
if (record[j] == '\n')
|
||||
if (writeTapeBuffering('\r') != 0)
|
||||
goto failed;
|
||||
if (writeTapeBuffering(record[j]) != 0)
|
||||
goto failed;
|
||||
}
|
||||
} else {
|
||||
if (WriteTapeRecord(record, datalen) != 0)
|
||||
goto failed;
|
||||
}
|
||||
}
|
||||
if (flushTapeBuffering() != 0)
|
||||
goto failed;
|
||||
|
||||
if ((WriteTapeMark(0) == 0) && (WriteTapeMark(1) == 0)) {
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
failed:
|
||||
fclose(file);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*++
|
||||
* extractFiles
|
||||
*
|
||||
* Extract files from the current open tape to the current working
|
||||
* directory.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* ascii - pointer to string containing extensions whose files
|
||||
* are to be extracted in ASCII mode.
|
||||
* e.g. ".MAC:.BAT" or ".MAC.BAT"
|
||||
* ppn - if non-zero, append proj, prog numbers to the name:
|
||||
* filename_prog_proj.ext
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
void extractFiles(
|
||||
char *ascii,
|
||||
int ppn
|
||||
)
|
||||
{
|
||||
unsigned int status;
|
||||
|
||||
do {
|
||||
switch (status = ReadTapeRecord(record, sizeof(record))) {
|
||||
case ST_EOM:
|
||||
break;
|
||||
|
||||
case ST_TM:
|
||||
/* Second tape mark in a row - treat as end of medium */
|
||||
status = ST_EOM;
|
||||
break;
|
||||
|
||||
default:
|
||||
if ((status & ST_ERROR) == 0) {
|
||||
status &= ST_LENGTH;
|
||||
|
||||
if ((status == sizeof(struct dos11hdr1)) ||
|
||||
(status == sizeof(struct dos11hdr2))) {
|
||||
char filename[32];
|
||||
struct dos11hdr2 *hdr = (struct dos11hdr2 *)record;
|
||||
int offset = 0, extension;
|
||||
char *useAscii;
|
||||
uint32 errorCount = 0, length, i;
|
||||
|
||||
/*
|
||||
* Construct the output filename
|
||||
*/
|
||||
offset += r50AscNoSpace(hdr->fname[0], &filename[offset]);
|
||||
offset += r50AscNoSpace(hdr->fname[1], &filename[offset]);
|
||||
if (status == sizeof(struct dos11hdr2))
|
||||
offset += r50AscNoSpace(hdr->fname3, &filename[offset]);
|
||||
if (ppn) {
|
||||
sprintf(&filename[offset], "_%o_%o", hdr->prog, hdr->proj);
|
||||
offset = strlen(filename);
|
||||
}
|
||||
extension = offset;
|
||||
filename[offset++] = '.';
|
||||
offset += r50AscNoSpace(hdr->ext, &filename[offset]);
|
||||
filename[offset++] = '\0';
|
||||
|
||||
useAscii = strstr(ascii, &filename[extension]);
|
||||
|
||||
printf(" Extracting: %s ", filename);
|
||||
|
||||
initAscii();
|
||||
|
||||
if ((file = fopen(filename, "w")) != NULL) {
|
||||
do {
|
||||
switch (status = ReadTapeRecord(record, sizeof(record))) {
|
||||
case ST_EOM:
|
||||
case ST_TM:
|
||||
break;
|
||||
|
||||
default:
|
||||
if ((status & ST_ERROR) != 0)
|
||||
errorCount++;
|
||||
length = status & ST_LENGTH;
|
||||
|
||||
if (useAscii != NULL) {
|
||||
/*
|
||||
* Process each character separately in order to map
|
||||
* CRLF -> LF.
|
||||
*/
|
||||
for (i = 0; i < length; i++)
|
||||
xferAscii(record[i]);
|
||||
} else {
|
||||
if (fwrite(record, sizeof(char), length, file) != length)
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
} while ((status != ST_EOM) && (status != ST_TM));
|
||||
flushAscii();
|
||||
printf("%s \n", errorCount ? "Errors detected" : "Done");
|
||||
fclose(file);
|
||||
break;
|
||||
} else printf("*** File open failure\n");
|
||||
} else fprintf(stderr, " *** Unexpected record size\n");
|
||||
} else fprintf(stderr, " *** Directory entry contains error\n");
|
||||
|
||||
/*
|
||||
* Scan forward to the end of this file
|
||||
*/
|
||||
do {
|
||||
status = ReadTapeRecordLength();
|
||||
} while ((status != ST_EOM) && (status != ST_TM));
|
||||
}
|
||||
} while (status != ST_EOM);
|
||||
}
|
||||
|
||||
/*++
|
||||
* listDirectory
|
||||
*
|
||||
* List the directory from the current open tape to stdout.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
void listDirectory(void)
|
||||
{
|
||||
unsigned int status;
|
||||
|
||||
do {
|
||||
switch (status = ReadTapeRecord(record, sizeof(record))) {
|
||||
case ST_EOM:
|
||||
break;
|
||||
|
||||
case ST_TM:
|
||||
/* Second tape mark in a row - treat as end of medium */
|
||||
status = ST_EOM;
|
||||
break;
|
||||
|
||||
default:
|
||||
if ((status & ST_ERROR) == 0) {
|
||||
status &= ST_LENGTH;
|
||||
|
||||
if ((status == sizeof(struct dos11hdr1)) ||
|
||||
(status == sizeof(struct dos11hdr2))) {
|
||||
char filename[14], sdate[12];
|
||||
struct dos11hdr2 *hdr = (struct dos11hdr2 *)record;
|
||||
uint32 errorCount = 0, length = 0;
|
||||
|
||||
memset(filename, ' ', sizeof(filename));
|
||||
filename[13] = '\0';
|
||||
|
||||
r50Asc(hdr->fname[0], &filename[0]);
|
||||
r50Asc(hdr->fname[1], &filename[3]);
|
||||
if (status == sizeof(struct dos11hdr2))
|
||||
r50Asc(hdr->fname3, &filename[6]);
|
||||
filename[9] = '.';
|
||||
r50Asc(hdr->ext, &filename[10]);
|
||||
|
||||
dos11Date(hdr->date, sdate);
|
||||
|
||||
printf("%s %s <%03o> [%3o,%3o] ",
|
||||
filename, sdate, hdr->prot, hdr->prog, hdr->proj);
|
||||
|
||||
do {
|
||||
switch (status = ReadTapeRecordLength()) {
|
||||
case ST_EOM:
|
||||
case ST_TM:
|
||||
printf("%u bytes%s\n", length, errorCount ? "(E)" : "");
|
||||
break;
|
||||
|
||||
default:
|
||||
if ((status & ST_ERROR) != 0)
|
||||
errorCount++;
|
||||
length += status & ST_LENGTH;
|
||||
break;
|
||||
}
|
||||
} while ((status != ST_EOM) && (status != ST_TM));
|
||||
break;
|
||||
} else fprintf(stderr, " *** Unexpected record size\n");
|
||||
} else fprintf(stderr, " *** Directory entry contains error\n");
|
||||
|
||||
/*
|
||||
* Scan forward to the end of this file
|
||||
*/
|
||||
do {
|
||||
status = ReadTapeRecordLength();
|
||||
} while ((status != ST_EOM) && (status != ST_TM));
|
||||
}
|
||||
} while (status != ST_EOM);
|
||||
}
|
||||
60
extracters/dbtap/dos11.h
Normal file
60
extracters/dbtap/dos11.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/* dos11.h: DOS/BATCH-11 tape file format definitions
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#include "defs.h"
|
||||
|
||||
/*
|
||||
* Actual file header as used in DOS/BATCH-11.
|
||||
*/
|
||||
struct dos11hdr1 {
|
||||
uint16 fname[2]; /* first 6 chars of filename (RAD50) */
|
||||
uint16 ext; /* 3 letter file extension (RAD50) */
|
||||
uint8 proj; /* project # (octal) */
|
||||
uint8 prog; /* programmer # (octal) */
|
||||
uint16 prot; /* protection code (octal) */
|
||||
uint16 date; /* (year-1970) * 1000 + day of year */
|
||||
};
|
||||
|
||||
/*
|
||||
* File header as seen on many archive tapes.
|
||||
*/
|
||||
struct dos11hdr2 {
|
||||
uint16 fname[2]; /* first 6 chars of filename (RAD50) */
|
||||
uint16 ext; /* 3 letter file extension (RAD50) */
|
||||
uint8 proj; /* project # (octal) */
|
||||
uint8 prog; /* programmer # (octal) */
|
||||
uint16 prot; /* protection code (octal) */
|
||||
uint16 date; /* (year-1970) * 1000 + day of year */
|
||||
uint16 fname3; /* optional, letters 7 - 9 of name */
|
||||
};
|
||||
|
||||
/*
|
||||
* DOS/BATCH-11 processing functions
|
||||
*/
|
||||
int appendFile(char *, char *, uint8, uint8, int, int);
|
||||
void extractFiles(char *, int);
|
||||
void listDirectory(void);
|
||||
50
extracters/dbtap/tap.h
Normal file
50
extracters/dbtap/tap.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/* tap.h: simh tape representation definitions
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
* Metadata markers
|
||||
*/
|
||||
#define ST_EOM 0xFFFFFFFF /* end of medium */
|
||||
#define ST_GAP 0xFFFFFFFE /* erase gap */
|
||||
#define ST_TM 0x00000000 /* tape mark */
|
||||
|
||||
/*
|
||||
* Record length field layout
|
||||
*/
|
||||
#define ST_ERROR 0x80000000 /* record contains an error */
|
||||
#define ST_MBZ 0x7F000000 /* must be zero */
|
||||
#define ST_LENGTH 0x00FFFFFF /* record length */
|
||||
|
||||
/*
|
||||
* Data in the .tap containe file is rounded up to an even number of bytes
|
||||
*/
|
||||
#define RECLEN(c) (((c) + 1) & ~1)
|
||||
|
||||
/*
|
||||
* The maximum record length supported by this code is 64K.
|
||||
*/
|
||||
#define MAXRCLNT 65536
|
||||
614
extracters/dbtap/tapeio.c
Normal file
614
extracters/dbtap/tapeio.c
Normal file
@@ -0,0 +1,614 @@
|
||||
/* tapeio.c: Tape I/O routines
|
||||
|
||||
Copyright (c) 2017, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include "tapeio.h"
|
||||
|
||||
FILE *tfile = NULL;
|
||||
|
||||
char buffer[MAXRCLNT];
|
||||
int rLength, occupied;
|
||||
|
||||
static int verifyFormat(void);
|
||||
|
||||
/*++
|
||||
* OpenTapeForRead
|
||||
*
|
||||
* Open an existing SIMH .tap format file for read access. If the file is
|
||||
* successfully opened, scan the file to determine if it is a valid .tap
|
||||
* format file and whether there are error records present.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* name - name of the file
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* TIO_SUCCESS - file successfully opened, format is valid
|
||||
* TIO_ERROR - file successfully opened, error records present
|
||||
* TIO_CORRUPT - file successfully opened, format is invalid
|
||||
* TIO_OPENFAIL - file open failed
|
||||
*
|
||||
* Note the file remains open if the return status is TIO_SUCCESS or
|
||||
* TIO_ERROR
|
||||
*
|
||||
--*/
|
||||
int OpenTapeForRead(
|
||||
char *name
|
||||
)
|
||||
{
|
||||
if ((tfile = fopen(name, "r")) != NULL) {
|
||||
int status;
|
||||
|
||||
status = verifyFormat();
|
||||
rewind(tfile);
|
||||
if ((status != TIO_SUCCESS) && (status != TIO_ERROR))
|
||||
CloseTape();
|
||||
return status;
|
||||
}
|
||||
return TIO_OPENFAIL;
|
||||
}
|
||||
|
||||
/*++
|
||||
* OpenTapeForWrite
|
||||
*
|
||||
* Create a new SIMH .tap format fiole for write access. Two tape marks are
|
||||
* written to the file and the file handle is rewound to the beginning of the
|
||||
* file. If the file already exists, and error (TIO_CREATEFAIL) is returned.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* name - name of the file
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* TIO_SUCCESS - file successfully created
|
||||
* TIO_IOERROR - I/O error writing the initial file contents
|
||||
* TIO_CREATEFAIL - file create failed
|
||||
*
|
||||
--*/
|
||||
int OpenTapeForWrite(
|
||||
char *name
|
||||
)
|
||||
{
|
||||
/*
|
||||
* Fail if the file exists
|
||||
*/
|
||||
if (access(name, F_OK) == 0)
|
||||
return TIO_CREATEFAIL;
|
||||
|
||||
if ((tfile = fopen(name, "w+")) != NULL) {
|
||||
uint32 tm = 0;
|
||||
int status = TIO_SUCCESS;
|
||||
|
||||
/*
|
||||
* Write 2 tape marks
|
||||
*/
|
||||
if (fwrite(&tm, sizeof(tm), 2, tfile) != 2)
|
||||
status = TIO_IOERROR;
|
||||
|
||||
if (status == TIO_SUCCESS) {
|
||||
rewind(tfile);
|
||||
return TIO_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Failed to write the 2 tape marks. Try to delete the file before
|
||||
* returning an error.
|
||||
*/
|
||||
CloseTape();
|
||||
unlink(name);
|
||||
return TIO_IOERROR;
|
||||
}
|
||||
return TIO_CREATEFAIL;
|
||||
}
|
||||
|
||||
/*++
|
||||
* OpenTapeForAppend
|
||||
*
|
||||
* Open an existing SIMH .tap format file for write access, leaving the
|
||||
* file handle positioned just before the final tape mark. If the file is
|
||||
* successfully opened, scan the file to determine if it is a valid .tap
|
||||
* format file and whether there are error records present.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* name - name of the file
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* TIO_SUCCESS - file successfully opened, format is valid
|
||||
* TIO_ERROR - file successfully opened, error records present
|
||||
* TIO_CORRUPT - file successfully opened, format is invalid
|
||||
* TIO_OPENFAIL - file open failed
|
||||
*
|
||||
* Note the file remains open if the return status is TIO_SUCCESS or
|
||||
* TIO_ERROR
|
||||
*
|
||||
--*/
|
||||
int OpenTapeForAppend(
|
||||
char *name
|
||||
)
|
||||
{
|
||||
if ((tfile =fopen(name, "r+")) != NULL) {
|
||||
int status;
|
||||
|
||||
status = verifyFormat();
|
||||
if ((status != TIO_SUCCESS) && (status != TIO_ERROR))
|
||||
CloseTape();
|
||||
return status;
|
||||
}
|
||||
return TIO_OPENFAIL;
|
||||
}
|
||||
|
||||
/*++
|
||||
* CloseTape
|
||||
*
|
||||
* If the tape is open, close it.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
void CloseTape(void)
|
||||
{
|
||||
if (tfile != NULL)
|
||||
fclose(tfile);
|
||||
tfile = NULL;
|
||||
}
|
||||
|
||||
/*++
|
||||
* verifyFormat
|
||||
*
|
||||
* Verify the format of the SIMH .tap file. If the format is valid, leave
|
||||
* the file handle positioned right before the last tape mark in the file.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* TIO_SUCCESS - file format is correct
|
||||
* TIO_ERROR - file format is correct, error records detected
|
||||
* TIO_CORRUPT - file format is invalid
|
||||
* TIO_IOERROR - I/O errror while processing file
|
||||
*
|
||||
--*/
|
||||
static int verifyFormat(void)
|
||||
{
|
||||
int errorCount = 0, tmSeen = 0;
|
||||
uint8 meta[4];
|
||||
uint32 header, bc;
|
||||
off_t position;
|
||||
struct stat stat;
|
||||
|
||||
/*
|
||||
* Determine the size of the file.
|
||||
*/
|
||||
fstat(fileno(tfile), &stat);
|
||||
|
||||
for (;;) {
|
||||
position = ftello(tfile);
|
||||
|
||||
/*
|
||||
* If we are position at the end of file, there is a tape mark missing.
|
||||
* Treat it as though there is one present.
|
||||
*/
|
||||
if (position == stat.st_size)
|
||||
return TIO_SUCCESS;
|
||||
|
||||
if (fread(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return TIO_CORRUPT;
|
||||
|
||||
bc = (((unsigned int)meta[3]) << 24) |
|
||||
(((unsigned int)meta[2]) << 16) |
|
||||
(((unsigned int)meta[1]) << 8) |
|
||||
(unsigned int)meta[0];
|
||||
|
||||
switch (bc) {
|
||||
case ST_TM:
|
||||
if (++tmSeen <= 1)
|
||||
break;
|
||||
/* Treat second TM in a row as end of medium */
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case ST_EOM:
|
||||
if (fseek(tfile, -sizeof(meta), SEEK_CUR) != 0)
|
||||
return TIO_IOERROR;
|
||||
return errorCount ? TIO_ERROR : TIO_SUCCESS;
|
||||
|
||||
case ST_GAP:
|
||||
break;
|
||||
|
||||
default:
|
||||
/*
|
||||
* Record descriptor
|
||||
*/
|
||||
tmSeen = 0;
|
||||
|
||||
header = bc;
|
||||
if ((bc & ST_ERROR) != 0)
|
||||
errorCount++;
|
||||
if ((bc & ST_MBZ) != 0)
|
||||
return TIO_CORRUPT;
|
||||
|
||||
bc = RECLEN(bc & ST_LENGTH);
|
||||
|
||||
/*
|
||||
* Check if we are seeking outside of the file. If so, this is not
|
||||
* a .tap container file.
|
||||
*/
|
||||
if ((position + bc + (2 * sizeof(meta))) > (unsigned long long)stat.st_size)
|
||||
return TIO_CORRUPT;
|
||||
|
||||
if (fseek(tfile, bc, SEEK_CUR) != 0)
|
||||
return TIO_CORRUPT;
|
||||
if (fread(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return TIO_CORRUPT;
|
||||
|
||||
bc = (((unsigned int)meta[3]) << 24) |
|
||||
(((unsigned int)meta[2]) << 16) |
|
||||
(((unsigned int)meta[1]) << 8) |
|
||||
(unsigned int)meta[0];
|
||||
if (header != bc)
|
||||
return TIO_CORRUPT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*++
|
||||
* ReadTapeRecord
|
||||
*
|
||||
* Read the next record from the tape into the specified buffer. If the
|
||||
* buffer is smaller than the record, the entire record will be consumed.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* buf - pointer to the buffer to receive the data
|
||||
* len - length of the buffer
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* ST_EOM - end of medium detected
|
||||
* ST_TM - tape mark detected
|
||||
* Other - record length (including error flag)
|
||||
* if the buffer is smaller than the record, the
|
||||
* length returned will be that of the buffer
|
||||
*
|
||||
--*/
|
||||
uint32 ReadTapeRecord(
|
||||
void *buf,
|
||||
int len
|
||||
)
|
||||
{
|
||||
long pos = ftell(tfile);
|
||||
uint8 meta[4];
|
||||
uint32 bc, erflag, length;
|
||||
|
||||
/*
|
||||
* Note: any I/O errors are treated as "end of medium" detection.
|
||||
*/
|
||||
if (fread(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return ST_EOM;
|
||||
|
||||
bc = (((unsigned int)meta[3]) << 24) |
|
||||
(((unsigned int)meta[2]) << 16) |
|
||||
(((unsigned int)meta[1]) << 8) |
|
||||
(unsigned int)meta[0];
|
||||
|
||||
switch (bc) {
|
||||
case ST_EOM:
|
||||
case ST_TM:
|
||||
return bc;
|
||||
|
||||
default:
|
||||
erflag = bc & ST_ERROR;
|
||||
bc &= ST_LENGTH;
|
||||
|
||||
length = (uint32)len;
|
||||
if (bc < length)
|
||||
length = bc;
|
||||
|
||||
if (fread(buf, sizeof(uint8), length, tfile) != length)
|
||||
return ST_EOM;
|
||||
|
||||
/*
|
||||
* Now position the file after this record.
|
||||
*/
|
||||
pos += RECLEN(bc) + (2 * sizeof(meta));
|
||||
if (fseek(tfile, pos, SEEK_SET) != 0)
|
||||
return ST_EOM;
|
||||
|
||||
return erflag | length;
|
||||
}
|
||||
return ST_EOM;
|
||||
}
|
||||
|
||||
/*++
|
||||
* ReadTapeRecordLength
|
||||
*
|
||||
* Get the length of the next record on the tape without actually reading
|
||||
* the data.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* ST_EOM - end of medium detected
|
||||
* ST_TM - tape mark detected
|
||||
* Other - record length (including error flag)
|
||||
* if the buffer is smaller than the record, the
|
||||
* length returned will be that of the buffer
|
||||
*
|
||||
--*/
|
||||
uint32 ReadTapeRecordLength(void)
|
||||
{
|
||||
long pos = ftell(tfile);
|
||||
uint8 meta[4];
|
||||
uint32 bc, erflag;
|
||||
|
||||
/*
|
||||
* Note: any I/O errors are treated as "end of medium" detection.
|
||||
*/
|
||||
if (fread(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return ST_EOM;
|
||||
|
||||
bc = (((unsigned int)meta[3]) << 24) |
|
||||
(((unsigned int)meta[2]) << 16) |
|
||||
(((unsigned int)meta[1]) << 8) |
|
||||
(unsigned int)meta[0];
|
||||
|
||||
switch (bc) {
|
||||
case ST_EOM:
|
||||
case ST_TM:
|
||||
return bc;
|
||||
|
||||
default:
|
||||
erflag = bc & ST_ERROR;
|
||||
bc &= ST_LENGTH;
|
||||
|
||||
/*
|
||||
* Now position the file after this record.
|
||||
*/
|
||||
pos += RECLEN(bc) + (2 * sizeof(meta));
|
||||
if (fseek(tfile, pos, SEEK_SET) != 0)
|
||||
return ST_EOM;
|
||||
|
||||
return erflag | bc;
|
||||
}
|
||||
return ST_EOM;
|
||||
}
|
||||
|
||||
/*++
|
||||
* WriteTapeRecord
|
||||
*
|
||||
* Write a record to the tape at it's current position.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* buf - pointer to the buffer to be written
|
||||
* len - length of the buffer
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* 0 if record was written successfully, -1 if write failed
|
||||
*
|
||||
--*/
|
||||
int WriteTapeRecord(
|
||||
void *buf,
|
||||
int len
|
||||
)
|
||||
{
|
||||
uint8 meta[4];
|
||||
int datalen;
|
||||
|
||||
meta[0] = len & 0xFF;
|
||||
meta[1] = (len >> 8) & 0xFF;
|
||||
meta[2] = (len >> 16) & 0xFF;
|
||||
meta[3] = (len >> 24) & 0xFF;
|
||||
|
||||
datalen = (len + 1) & ~1;
|
||||
|
||||
if (fwrite(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return -1;
|
||||
|
||||
if (fwrite(buf, datalen, 1, tfile) != 1)
|
||||
return -1;
|
||||
|
||||
if (fwrite(meta, sizeof(meta), 1, tfile) != 1)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* WriteTapeMark
|
||||
*
|
||||
* Write a tape mark to the tape at it's current position and, optionally,
|
||||
* backup to before the tape mark.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* backup - if 1, reposition the tape to before the tape mark
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* 0 if tape mark was written successfully, -1 if write failed
|
||||
*
|
||||
--*/
|
||||
int WriteTapeMark(
|
||||
int backup
|
||||
)
|
||||
{
|
||||
uint32 tm = 0;
|
||||
|
||||
if (fwrite(&tm, sizeof(tm), 1, tfile) != 1)
|
||||
return -1;
|
||||
|
||||
if (backup)
|
||||
if (fseek(tfile, -sizeof(tm), SEEK_CUR) != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* initTapeBuffering
|
||||
*
|
||||
* Initialize variables for writes to tape for ASCII mode transfers
|
||||
* (translates LF -> CRLF).
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* reclen - size of the tape record buffer to use
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* None
|
||||
*
|
||||
--*/
|
||||
void initTapeBuffering(
|
||||
int reclen
|
||||
)
|
||||
{
|
||||
rLength = reclen;
|
||||
occupied = 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* flushTapeBuffering
|
||||
*
|
||||
* Flush any pending data out to the current tape.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* 0 if data was successfully flushed, -1 if write failed
|
||||
*
|
||||
--*/
|
||||
int flushTapeBuffering(void)
|
||||
{
|
||||
uint32 count = occupied;
|
||||
|
||||
occupied = 0;
|
||||
|
||||
if (count != 0)
|
||||
return WriteTapeRecord(buffer, count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*++
|
||||
* writeTapeBuffering
|
||||
*
|
||||
* Write a character to the current tape, buffering the data into records.
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* ch - the character to be output
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* None
|
||||
*
|
||||
* Returns:
|
||||
*
|
||||
* 0 if character was successfully buffered or written to tape, -1 if
|
||||
* write failed
|
||||
*
|
||||
--*/
|
||||
int writeTapeBuffering(
|
||||
char ch
|
||||
)
|
||||
{
|
||||
buffer[occupied++] = ch;
|
||||
|
||||
if (occupied == rLength) {
|
||||
occupied = 0;
|
||||
return WriteTapeRecord(buffer, rLength);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
62
extracters/dbtap/tapeio.h
Normal file
62
extracters/dbtap/tapeio.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/* tapeio.h: Tape I/O definitions
|
||||
|
||||
Copyright (c) 2015, John Forecast
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the name of John Forecast shall not
|
||||
be used in advertising or otherwise to promote the sale, use or other dealings
|
||||
in this Software without prior written authorization from John Forecast.
|
||||
|
||||
*/
|
||||
|
||||
#include "tap.h"
|
||||
#include "defs.h"
|
||||
|
||||
/*
|
||||
* Tape open status return codes
|
||||
*/
|
||||
#define TIO_SUCCESS 0 /* operation successful */
|
||||
#define TIO_ERROR -1 /* error record seen */
|
||||
#define TIO_CORRUPT -2 /* tape format is corrupt */
|
||||
#define TIO_OPENFAIL -3 /* open operation failed */
|
||||
#define TIO_CREATEFAIL -4 /* create operation failed */
|
||||
#define TIO_IOERROR -5 /* I/O error */
|
||||
|
||||
/*
|
||||
* Tape open/close routines.
|
||||
*/
|
||||
extern int OpenTapeForRead(char *);
|
||||
extern int OpenTapeForWrite(char *);
|
||||
extern int OpenTapeForAppend(char *);
|
||||
extern void CloseTape(void);
|
||||
|
||||
/*
|
||||
* Tape I/O routines.
|
||||
*/
|
||||
uint32 ReadTapeRecord(void *, int);
|
||||
uint32 ReadTapeRecordLength(void);
|
||||
int WriteTapeRecord(void *, int);
|
||||
int WriteTapeMark(int);
|
||||
|
||||
/*
|
||||
* Buffered I/O routines
|
||||
*/
|
||||
void initTapeBuffering(int);
|
||||
int flushTapeBuffering(void);
|
||||
int writeTapeBuffering(char);
|
||||
Reference in New Issue
Block a user