static char Sccsid
[] = "aplcvt.c @(#)aplcvt.c 1.2 10/1/82 Berkeley ";
* aplcvt - convert APL workspace to/from VAX format
#define PDPMAGIC 0100554 /* PDP-11 magic number */
#define VAXMAGIC 0100556 /* VAX magic number */
#define DA 1 /* data type */
#define NF 8 /* niladic function type */
#define MF 9 /* monadic function type */
#define DF 10 /* dyadic function type */
#define MRANK 8 /* maximum rank */
* The following define the internal data structures for APL
* on both the PDP-11 and the VAX. Two short integers are
* used instead of a long integer for the VAX definitions so
* that the program can be compiled and run on either machine
* without changes. (Otherwise, the reversal of long integers
* between the two machines would cause problems.)
#define PTSIZE 14 /* its real size, not the sizeof */
short pi_datap
; /* really a 16-bit pointer */
short vi_datap
[2]; /* really a 32-bit pointer */
short vi_dim
[MRANK
][2]; /* array of 32-bit integers */
#define eperror(x,y) {eprintf(x); perror(y);}
char *base(), *strcpy(), *strcmp();
int makevax
= 1; /* by default, convert to VAX format */
int makevax
= 0; /* by default, convert to PDP format */
char *pname
; /* holds argv[0] */
char *ifname
; /* points to input file name */
char ofname
[128]; /* contains output file name */
register FILE *ifp
, *ofp
;
/* Parse the arguments */
if (argc
> 1 && *argv
[1] == '-'){
makevax
= (argv
[1][1] == 'v');
eprintf("unknown flag \"%s\"\n", argv
[1]);
/* If there are no filename arguments, convert standard
* input to standard output. However, if one of these is
* a tty, just exit with a syntax error message (it is highly
* unlikely that the user wanted input or output from/to his
* If there are filenames, convert each one.
if(isatty(0) || isatty(1)){
fprintf(stderr
, "Syntax: \"%s [-v|-p] filename ...\"\n",
strcpy(ofname
, "<stdout>");
if (makevax
? tovax(stdin
,stdout
) : topdp(stdin
,stdout
)){
eprintf("don't trust the output file!\n");
if ((ifp
=fopen(ifname
, "r")) == NULL
){
eperror("can't open ", ifname
);
strcat(strcpy(ofname
,base(ifname
)),
makevax
? ".vax" : ".pdp");
if ((ofp
=fopen(ofname
, "w")) == NULL
){
eperror("can't create ", ofname
);
if (makevax
? tovax(ifp
,ofp
) : topdp(ifp
,ofp
))
eperror("unlink ", ofname
);
static char basename
[128];
/* Strip off a trailing ".pdp" or ".vax" (depending upon the
* direction of conversion.
for(p
=basename
; *p
= *s
; p
++,s
++)
if (*s
== '.' && !strcmp(s
+1, makevax
? "pdp" : "vax")){
/* Look for proper magic number */
if (fread(&magic
, sizeof magic
, 1, ifp
) != 1){
eperror("read error on ", ifname
);
if ((magic
|1) != (VAXMAGIC
|1)){
eprintf("%s is not a VAX APL workspace\n", ifname
);
if (fread(&magic
, sizeof magic
, 1, ifp
) != 1){
eperror("read error on ", ifname
);
eprintf("warning: %s may be corrupted\n", ifname
);
eprintf("attempting to continue\n");
magic
= (magic
&1) | PDPMAGIC
;
if (fwrite(&magic
, sizeof magic
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
/* Convert the "thread" structure */
if (fread(&vthread
, sizeof vthread
, 1, ifp
) != 1){
eperror("read error on ", ifname
);
pthread
.pt_fuzz
= vthread
.vt_fuzz
;
pthread
.pt_iorg
= vthread
.vt_iorg
[0];
pthread
.pt_rl
= vthread
.vt_rl
[0];
pthread
.pt_digits
= vthread
.vt_digits
[0];
pthread
.pt_width
= vthread
.vt_width
[0];
if (fwrite(&pthread
, PTSIZE
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
/* Convert each data item or function */
if ((j
=fread(&iz
, sizeof(long), 1, ifp
)) != 1)
eperror("read error on ", ifname
);
if (fwrite(&iz
, sizeof(short), 1, ofp
) != 1){
eperror("write error on ", ofname
);
if (fread(name
, sizeof(char), (unsigned)iz
.cv
[1], ifp
) != iz
.cv
[1]){
eperror("read error on ", ifname
);
if (fwrite(name
, sizeof(char), (unsigned)iz
.cv
[1], ofp
) != iz
.cv
[1]){
eperror("write error on ", ofname
);
eprintf("unknown item, type = %d\n", iz
.cv
[0]);
eprintf("conversion aborted\n");
if ((c
=getc(ifp
)) == EOF
){
eperror("getc error on ", ifname
);
if (fread(&iz
, sizeof(long), 1, ifp
) != 1){
eperror("read error on ", ifname
);
if (iz
.cv
[2] | iz
.cv
[3]){
eprintf("item %s too large -- aborting\n", name
);
if (fread(&vitem
, sizeof vitem
- MRANK
*sizeof(long),
eperror("read error on ", ifname
);
if (fread(vitem
.vi_dim
, sizeof(long), vitem
.vi_rank
, ifp
)
eperror("read error on ", ifname
);
pitem
.pi_rank
= vitem
.vi_rank
;
pitem
.pi_type
= vitem
.vi_type
;
pitem
.pi_size
= vitem
.vi_size
[0];
for(j
=0; j
<vitem
.vi_rank
; j
++)
pitem
.pi_dim
[j
] = vitem
.vi_dim
[j
][0];
nsz
= sizeof pitem
- (MRANK
-pitem
.pi_rank
)*sizeof(short)
- sizeof vitem
+ (MRANK
-vitem
.vi_rank
)*sizeof(long)
if (fwrite(&nsz
, sizeof nsz
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
j
= sizeof pitem
- (MRANK
-pitem
.pi_rank
)*sizeof(short);
if (fwrite(&pitem
, j
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
j
= sizeof vitem
- (MRANK
-vitem
.vi_rank
)*sizeof(long);
if (copy(ifp
, ofp
, iz
.s
-j
))
goto loop
; /* should be while(1) */
/* Look for proper magic number. */
if (fread(&magic
, sizeof magic
, 1, ifp
) != 1){
eperror("read error on ", ifname
);
if ((magic
|1) != (PDPMAGIC
|1)){
eprintf("%s is not a PDP-11 APL workspace\n", ifname
);
magic
= (magic
&1) | VAXMAGIC
;
if (fwrite(&magic
, sizeof magic
, 1, ofp
) != 1
|| fwrite(&zero
, sizeof zero
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
/* Convert the "thread" structure. */
if (fread(&pthread
, PTSIZE
, 1, ifp
) != 1){
eperror("read error on ", ifname
);
vthread
.vt_fuzz
= pthread
.pt_fuzz
;
vthread
.vt_iorg
[0] = pthread
.pt_iorg
;
vthread
.vt_rl
[0] = pthread
.pt_rl
;
vthread
.vt_digits
[0] = pthread
.pt_digits
;
vthread
.vt_digits
[1] = 0;
vthread
.vt_width
[0] = pthread
.pt_width
;
if (fwrite(&vthread
, sizeof vthread
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
/* Convert each data item or function. */
if ((j
=fread(&iz
, sizeof(short), 1, ifp
)) != 1)
eperror("read error on ", ifname
);
if (fwrite(&iz
, sizeof(long), 1, ofp
) != 1){
eperror("write error on ", ofname
);
if (fread(name
, sizeof(char), (unsigned)iz
.cv
[1], ifp
) != iz
.cv
[1]){
eperror("read error on ", ifname
);
if (fwrite(name
, sizeof(char), (unsigned)iz
.cv
[1], ofp
) != iz
.cv
[1]){
eperror("write error on ", ofname
);
eprintf("unknown item, type = %d\n", iz
.cv
[0]);
eprintf("conversion aborted\n");
if ((c
=getc(ifp
)) == EOF
){
eperror("getc error on ", ifname
);
if (fread(&iz
, sizeof(short), 1, ifp
) != 1){
eperror("read error on ", ifname
);
if (fread(&pitem
, sizeof pitem
- MRANK
*sizeof(short),
eperror("read error on ", ifname
);
if (fread(pitem
.pi_dim
, sizeof(short), pitem
.pi_rank
, ifp
)
eperror("read error on ", ifname
);
vitem
.vi_rank
= pitem
.pi_rank
;
vitem
.vi_type
= pitem
.pi_type
;
vitem
.vi_size
[0] = pitem
.pi_size
;
for(j
=0; j
<pitem
.pi_rank
; j
++){
vitem
.vi_dim
[j
][0] = pitem
.pi_dim
[j
];
nsz
= sizeof vitem
- (MRANK
-vitem
.vi_rank
)*sizeof(long)
- sizeof pitem
+ (MRANK
-pitem
.pi_rank
)*sizeof(short)
if (fwrite(&nsz
, sizeof nsz
, 1, ofp
) != 1
|| fwrite(&zero
, sizeof zero
, 1, ofp
) != 1){
perror("write error on ", ofname
);
j
= sizeof vitem
- (MRANK
-vitem
.vi_rank
)*sizeof(long);
if (fwrite(&vitem
, j
, 1, ofp
) != 1){
eperror("write error on ", ofname
);
j
= sizeof pitem
- (MRANK
-pitem
.pi_rank
)*sizeof(short);
if (copy(ifp
, ofp
, iz
.s
-j
))
goto loop
; /* should be while(1) */
if ((c
=getc(ifp
)) == EOF
){
eperror("getc error on ", ifname
);
eprintf(a
, b
, c
, d
, e
, f
, g
, h
, i
, j
){
fprintf(stderr
, "%s: ", pname
);
fprintf(stderr
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
);