ANSI C conversion.

This commit is contained in:
carl 2019-05-11 01:15:51 +08:00
parent 970e7da896
commit 0301827482
3 changed files with 101 additions and 86 deletions

View file

@ -7,18 +7,17 @@ static char rcsid[] = "$Id$";
#endif
/* This program converts either human-readable or compact EM
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.
*/
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.
*/
#include <stdarg.h>
#include <stdlib.h>
#include "system.h"
#include "print.h"
#include "em_pseu.h"
#include "em_mnem.h"
@ -28,62 +27,69 @@ static char rcsid[] = "$Id$";
#include "em.h"
#include "em_comp.h"
char *filename; /* Name of input file */
int errors; /* Number of errors */
char *filename; /* Name of input file */
int errors; /* Number of errors */
extern char *C_error;
extern int C_out(register struct e_instr *);
void error(const char *, ...);
void fatal(const char *, ...);
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
struct e_instr buf;
register struct e_instr *p = &buf;
if (argc >= 2) {
if (argc >= 2)
{
filename = argv[1];
}
else filename = 0;
if (!EM_open(filename)) {
else
filename = 0;
if (!EM_open(filename))
{
fatal(EM_error);
}
EM_getinstr(p);
C_init((arith) EM_wordsize, (arith) EM_pointersize);
if (argc >= 3) {
if (!C_open(argv[2])) {
if (argc >= 3)
{
if (!C_open(argv[2]))
{
fatal("C_open failed");
}
}
else if (!C_open( (char *) 0)) fatal("C_open failed");
else if (!C_open((char *) 0))
fatal("C_open failed");
C_magic();
while (p->em_type != EM_EOF) {
if (p->em_type == EM_FATAL) {
while (p->em_type != EM_EOF)
{
if (p->em_type == EM_FATAL)
{
fatal("%s", EM_error);
}
if (EM_error) {
if (EM_error)
{
error("%s", EM_error);
}
if (p->em_type != EM_ERROR && !C_out(p)) {
if (p->em_type != EM_ERROR && !C_out(p))
{
error("%s", C_error);
}
EM_getinstr(p);
}
C_close();
EM_close();
exit(errors ? 1 : 0);
exit(errors ? EXIT_FAILURE : EXIT_SUCCESS);
}
/* VARARGS */
void
error(const char *s, ...)
void error(const char *s, ...)
{
va_list ap;
va_start(ap, s);
fprint(STDERR,
"%s, line %d: ",
filename ? filename : "standard input",
EM_lineno);
fprint(STDERR, "%s, line %d: ", filename ? filename : "standard input",
EM_lineno);
doprnt(STDERR, s, ap);
fprint(STDERR, "\n");
errors++;
@ -91,12 +97,12 @@ error(const char *s, ...)
}
/* VARARGS */
void
fatal(const char *s, ...)
void fatal(const char *s, ...)
{
va_list ap;
va_start(ap, s);
if (C_busy()) C_close();
if (C_busy())
C_close();
error(s, ap);
exit(1);
exit(EXIT_FAILURE);
}

View file

@ -3,10 +3,10 @@
.SH NAME
em_decode, em_encode \- compact to readable EM and v.v.
.SH SYNOPSIS
.B ~em/lib.bin/em_decode
.B em_decode
[ inputfile [ outputfile ] ]
.br
.B ~em/lib.bin/em_encode
.B em_encode
[ inputfile [ outputfile ] ]
.SH DESCRIPTION
Most programs involved with the EM project only produce and accept
@ -15,12 +15,12 @@ These files are only machine readable.
A description of this compact form can be found in [1].
To inspect the code produced by compilers or to patch them for one reason
or another, human readable assembly code is needed.
Em_decode produces human readable assembly code from the compact form.
em_decode produces human readable assembly code from the compact form.
.PP
Em_decode accepts the normal compact form in both optimized and
em_decode accepts the normal compact form in both optimized and
unoptimized form.
.PP
Em_encode produces the compact form
em_encode produces the compact form
out of these human readable assembly code.
.PP
The first argument is the input file.

View file

@ -1,5 +1,5 @@
/* esize: prints info from e.out header
*/
*/
#include <stdlib.h>
#include <stdio.h>
@ -12,9 +12,9 @@ FILE *load_fp;
int eof;
/* Much of the code has been borrowed from the EM interpreter
*/
*/
typedef /* unsigned */ long ptr; /* pointer to EM adress */
typedef /* unsigned */long ptr; /* pointer to EM adress */
long magic;
long flags;
@ -33,51 +33,65 @@ long szdata;
long ptr7;
long ptr8;
main(argc, argv)
int argc;
char *argv[];
/* Forward declarations. */
static void esize(char *);
static int rd_open(char*);
static int rd_byte(void);
static long rd_int(long n);
static int rd_header(void);
static void rd_close(void);
#define rd_ptr() ((ptr) rd_int(psize))
int main(int argc, char *argv[])
{
printf("TPFCRE uref vers w/p text nproc szdata\n");
if (argc == 1) {
if (argc == 1)
{
esize("e.out");
}
else {
while (argc > 1) {
else
{
while (argc > 1)
{
esize(argv[1]);
argc--, argv++;
}
}
exit(0);
exit(EXIT_SUCCESS);
}
esize(fname)
char *fname;
static void esize(char *fname)
{
eof = 0;
if (!rd_open(fname)) {
if (!rd_open(fname))
{
printf("%s: cannot open\n", fname);
}
else {
if (!rd_header()) {
else
{
if (!rd_header())
{
printf("%s: not EM object format\n", fname);
}
else {
printf("%c", flags&0001 ? 'T' : '-');
printf("%c", flags&0002 ? 'P' : '-');
printf("%c", flags&0004 ? 'F' : '-');
printf("%c", flags&0010 ? 'C' : '-');
printf("%c", flags&0020 ? 'R' : '-');
printf("%c", flags&0040 ? 'E' : '-');
printf("%c", flags&0100 ? '?' : ' ');
printf("%c", flags&0200 ? '?' : ' ');
else
{
printf("%c", flags & 0001 ? 'T' : '-');
printf("%c", flags & 0002 ? 'P' : '-');
printf("%c", flags & 0004 ? 'F' : '-');
printf("%c", flags & 0010 ? 'C' : '-');
printf("%c", flags & 0020 ? 'R' : '-');
printf("%c", flags & 0040 ? 'E' : '-');
printf("%c", flags & 0100 ? '?' : ' ');
printf("%c", flags & 0200 ? '?' : ' ');
printf("%3ld ", uref);
printf("%3ld ", version);
printf("%1ld/%1ld", wsize, psize);
printf("%c", int7 ? '?' : ' ');
printf("%c", int8 ? '?' : ' ');
printf("%5ld ", ntext);
printf("%5ld ", nproc);
printf("%6ld", szdata);
@ -91,15 +105,12 @@ esize(fname)
#define btol(a) ((long)(((long) (a)) & 0xFF))
int
rd_open(load_file)
char *load_file;
static int rd_open(char* load_file)
{
return (load_fp = fopen(load_file, "r")) != NULL;
return (load_fp = fopen(load_file, "rb")) != NULL;
}
int
rd_byte()
static int rd_byte(void)
{
int i;
@ -108,35 +119,33 @@ rd_byte()
return (i);
}
long
rd_int(n)
long n;
static long rd_int(long n)
{
long l;
register int i;
l = btol(rd_byte());
for (i = 1; i < n; i++)
l = l | (btol(rd_byte()) << (long) (i*8));
l = l | (btol(rd_byte()) << (long) (i * 8));
return (l);
}
#define rd_ptr() ((ptr) rd_int(psize))
int
rd_header()
/* read e.out header information */
static int rd_header(void)
{
magic = rd_int(2L);
if (magic != MAGIC || eof)
return 0;
flags = rd_int(2L);
uref = rd_int(2L);
version = rd_int(2L);
wsize = rd_int(2L);
psize = rd_int(2L);
int7 = rd_int(2L); /* Entry 7 is unused */
int8 = rd_int(2L); /* Entry 8 is unused */
int7 = rd_int(2L); /* Entry 7 is unused */
int8 = rd_int(2L); /* Entry 8 is unused */
ntext = rd_ptr();
ndata = rd_ptr();
@ -144,13 +153,13 @@ rd_header()
entrypoint = rd_ptr();
nline = rd_ptr();
szdata = rd_ptr();
ptr7 = rd_ptr(); /* entry 7 is unused */
ptr8 = rd_ptr(); /* entry 8 is unused */
ptr7 = rd_ptr(); /* entry 7 is unused */
ptr8 = rd_ptr(); /* entry 8 is unused */
return !eof;
}
rd_close()
static void rd_close(void)
{
fclose(load_fp);
}