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

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

View file

@ -3,10 +3,10 @@
.SH NAME .SH NAME
em_decode, em_encode \- compact to readable EM and v.v. em_decode, em_encode \- compact to readable EM and v.v.
.SH SYNOPSIS .SH SYNOPSIS
.B ~em/lib.bin/em_decode .B em_decode
[ inputfile [ outputfile ] ] [ inputfile [ outputfile ] ]
.br .br
.B ~em/lib.bin/em_encode .B em_encode
[ inputfile [ outputfile ] ] [ inputfile [ outputfile ] ]
.SH DESCRIPTION .SH DESCRIPTION
Most programs involved with the EM project only produce and accept 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]. 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 To inspect the code produced by compilers or to patch them for one reason
or another, human readable assembly code is needed. 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 .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. unoptimized form.
.PP .PP
Em_encode produces the compact form em_encode produces the compact form
out of these human readable assembly code. out of these human readable assembly code.
.PP .PP
The first argument is the input file. The first argument is the input file.

View file

@ -33,36 +33,50 @@ long szdata;
long ptr7; long ptr7;
long ptr8; long ptr8;
main(argc, argv) /* Forward declarations. */
int argc; static void esize(char *);
char *argv[]; 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"); printf("TPFCRE uref vers w/p text nproc szdata\n");
if (argc == 1) { if (argc == 1)
{
esize("e.out"); esize("e.out");
} }
else { else
while (argc > 1) { {
while (argc > 1)
{
esize(argv[1]); esize(argv[1]);
argc--, argv++; argc--, argv++;
} }
} }
exit(0); exit(EXIT_SUCCESS);
} }
esize(fname) static void esize(char *fname)
char *fname;
{ {
eof = 0; eof = 0;
if (!rd_open(fname)) { if (!rd_open(fname))
{
printf("%s: cannot open\n", fname); printf("%s: cannot open\n", fname);
} }
else { else
if (!rd_header()) { {
if (!rd_header())
{
printf("%s: not EM object format\n", fname); printf("%s: not EM object format\n", fname);
} }
else { else
{
printf("%c", flags & 0001 ? 'T' : '-'); printf("%c", flags & 0001 ? 'T' : '-');
printf("%c", flags & 0002 ? 'P' : '-'); printf("%c", flags & 0002 ? 'P' : '-');
printf("%c", flags & 0004 ? 'F' : '-'); printf("%c", flags & 0004 ? 'F' : '-');
@ -91,15 +105,12 @@ esize(fname)
#define btol(a) ((long)(((long) (a)) & 0xFF)) #define btol(a) ((long)(((long) (a)) & 0xFF))
int static int rd_open(char* load_file)
rd_open(load_file)
char *load_file;
{ {
return (load_fp = fopen(load_file, "r")) != NULL; return (load_fp = fopen(load_file, "rb")) != NULL;
} }
int static int rd_byte(void)
rd_byte()
{ {
int i; int i;
@ -108,9 +119,7 @@ rd_byte()
return (i); return (i);
} }
long static long rd_int(long n)
rd_int(n)
long n;
{ {
long l; long l;
register int i; register int i;
@ -121,10 +130,10 @@ rd_int(n)
return (l); 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); magic = rd_int(2L);
if (magic != MAGIC || eof) if (magic != MAGIC || eof)
@ -150,7 +159,7 @@ rd_header()
return !eof; return !eof;
} }
rd_close() static void rd_close(void)
{ {
fclose(load_fp); fclose(load_fp);
} }