shithub: apl10

ref: 7b65afc1ad13f3859eca6eadaa2c45d864320304
dir: /as.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dat.h"

static void _Noreturn
usage(void)
{
	fprint(2, "usage: apl/as [-d] [-o outfile] [infile]\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	int disass = 0;
	char *infile = nil;
	char *outfile = nil;

	Biobuf *in, *out;

	ARGBEGIN{
	case 'o':
		outfile = EARGF(usage());
		break;
	case 'd':
		disass = 1;
		break;
	default:
		usage();
	}ARGEND;

	if(argc > 1)
		usage();
	else if(argc == 1)
		infile = *argv;

	if(outfile == nil && infile != nil){
		outfile = strdup(infile);
		char *dot = utfrrune(outfile, '.');
		char *inext = disass ? OC_EXT : BC_EXT;
		char *outext = disass ? BC_EXT : OC_EXT; 
		if(dot && strcmp(dot+1, inext) == 0)
			dot[0] = 0;
		outfile = smprint("%s.%s", outfile, outext);
	}

	in = infile ? Bopen(infile, OREAD) : Bfdopen(0, OREAD);
	out = outfile ? Bopen(outfile, OWRITE|OTRUNC) : Bfdopen(1, OWRITE);
	if(in == nil || out == nil)
		sysfatal("open: %r");

	Module *m = mallocz(sizeof(Module), 1);
	for(int output = 0; output < 2; output++){
		for(int n = 0; n < nelem(objparts); n++){
			ObjpartSpec p = objparts[n];
			if(output)
				p.write(m, out, disass);
			else
				p.read(m, in, !disass);
		}
	}

	Bterm(in);
	Bterm(out);
}