#include <ebs.c>
#include <sys/mman.h>
#include <error.h>
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define S0(x) ((x) ? (x) : "")
int main (int argc, char ** argv) {
int r = 0;
struct ebs ebs;
ebs_init(&ebs);
if (argc != 1+1)
error_at_line(1, 0, __FILE__, __LINE__, "%s program.ebs", S0(argv[0]));
int pfd = open(argv[1], O_RDONLY | O_CLOEXEC);
if (pfd == -1)
error_at_line(2, errno, __FILE__, __LINE__, "open(%s)", argv[1]);
struct stat statbuf;
if (fstat(pfd, &statbuf) == -1) {
error_at_line(0, errno, __FILE__, __LINE__, "fstat(pfd, &statbuf)");
r = 3;
goto r;
}
if (!(ebs.pm_velikost = statbuf.st_size)) {
error_at_line(0, errno, __FILE__, __LINE__, "!(ebs.pm_velikost = statbuf.st_size)");
r = 4;
goto r;
}
if (!(ebs.pm = mmap(NULL, ebs.pm_velikost, PROT_READ, MAP_SHARED, pfd, 0))) {
error_at_line(0, errno, __FILE__, __LINE__, "!mmap(NULL, ebs.pm_velikost, PROT_READ, MAP_SHARED, pfd, 0)");
r = 5;
goto r;
}
for (unsigned i = 0; i <= 0xffff; i++) {
struct inštrukcija š = inštrukcija(&ebs, i);
if (!š.dobesedno)
break;
printf("0x%04x\t%s\t0x%02x\t0x%02x\t0x%02x%02x\n", i, operacija_str[š.operacija], š.vir, š.destinacija, š.dobesedno[0], š.dobesedno[1]);
}
r:
if (ebs.pm && munmap(ebs.pm, ebs.pm_velikost) == -1)
error_at_line(0, errno, __FILE__, __LINE__, "munmap(ebs.pm, ebs.pm_velikost)");
close(pfd);
pfd = -1;
return r;
}