ack/util/misc/convert.c

109 lines
2.1 KiB
C
Raw Permalink Normal View History

1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1987-02-05 14:47:47 +00:00
#ifndef NORCSID
1994-06-24 11:31:16 +00:00
static char rcsid[] = "$Id$";
1987-02-05 14:47:47 +00:00
#endif
/* This program converts either human-readable or compact EM
2019-05-10 17:15:51 +00:00
assembly code to calls of the procedure-interface.
It must be linked with two libraries:
- a library to read EM code, according to read_em(3)
- a library implementing the em_code(3) interface.
Thus, this program could serve as an EM_encoder, an
EM_decoder, or some code generator, depending on how it is
linked.
*/
1987-02-05 14:47:47 +00:00
#include <stdarg.h>
#include <stdlib.h>
#include "print.h"
#include "em_pseu.h"
#include "em_mnem.h"
#include "em_spec.h"
#include "em_flag.h"
#include "em_ptyp.h"
#include "em.h"
#include "em_comp.h"
1987-02-05 14:47:47 +00:00
2019-05-10 17:15:51 +00:00
char *filename; /* Name of input file */
int errors; /* Number of errors */
1990-04-23 13:43:05 +00:00
extern char *C_error;
2019-05-10 17:15:51 +00:00
extern int C_out(register struct e_instr *);
1987-02-05 14:47:47 +00:00
void error(const char *, ...);
void fatal(const char *, ...);
2019-05-10 17:15:51 +00:00
int main(int argc, char **argv)
1987-02-05 14:47:47 +00:00
{
struct e_instr buf;
register struct e_instr *p = &buf;
1987-02-05 14:47:47 +00:00
2019-05-10 17:15:51 +00:00
if (argc >= 2)
{
1987-02-05 14:47:47 +00:00
filename = argv[1];
}
2019-05-10 17:15:51 +00:00
else
filename = 0;
if (!EM_open(filename))
{
1987-02-05 14:47:47 +00:00
fatal(EM_error);
}
EM_getinstr(p);
1987-02-05 14:47:47 +00:00
C_init((arith) EM_wordsize, (arith) EM_pointersize);
2019-05-10 17:15:51 +00:00
if (argc >= 3)
{
if (!C_open(argv[2]))
{
1987-02-05 14:47:47 +00:00
fatal("C_open failed");
}
}
2019-05-10 17:15:51 +00:00
else if (!C_open((char *) 0))
fatal("C_open failed");
1987-02-05 14:47:47 +00:00
C_magic();
2019-05-10 17:15:51 +00:00
while (p->em_type != EM_EOF)
{
if (p->em_type == EM_FATAL)
{
1987-03-09 12:52:10 +00:00
fatal("%s", EM_error);
1987-02-05 14:47:47 +00:00
}
2019-05-10 17:15:51 +00:00
if (EM_error)
{
1987-03-09 12:52:10 +00:00
error("%s", EM_error);
1987-02-05 14:47:47 +00:00
}
2019-05-10 17:15:51 +00:00
if (p->em_type != EM_ERROR && !C_out(p))
{
1990-04-23 13:43:05 +00:00
error("%s", C_error);
1987-02-05 14:47:47 +00:00
}
EM_getinstr(p);
1987-02-05 14:47:47 +00:00
}
C_close();
EM_close();
2019-05-10 17:15:51 +00:00
exit(errors ? EXIT_FAILURE : EXIT_SUCCESS);
1987-02-05 14:47:47 +00:00
}
/* VARARGS */
2019-05-10 17:15:51 +00:00
void error(const char *s, ...)
1987-02-05 14:47:47 +00:00
{
va_list ap;
va_start(ap, s);
2019-05-10 17:15:51 +00:00
fprint(STDERR, "%s, line %d: ", filename ? filename : "standard input",
EM_lineno);
doprnt(STDERR, s, ap);
1987-03-29 13:00:40 +00:00
fprint(STDERR, "\n");
1987-02-05 14:47:47 +00:00
errors++;
va_end(ap);
1987-02-05 14:47:47 +00:00
}
/* VARARGS */
2019-05-10 17:15:51 +00:00
void fatal(const char *s, ...)
1987-02-05 14:47:47 +00:00
{
va_list ap;
va_start(ap, s);
2019-05-10 17:15:51 +00:00
if (C_busy())
C_close();
error(s, ap);
2019-05-10 17:15:51 +00:00
exit(EXIT_FAILURE);
1987-02-05 14:47:47 +00:00
}