Renamed the "As" class to "Asm"
This commit is contained in:
parent
e7c3dc36b2
commit
7bbe4797be
@ -15,43 +15,43 @@
|
||||
|
||||
|
||||
|
||||
#ifndef DEVEL_ASM_AS_H
|
||||
# define DEVEL_ASM_AS_H
|
||||
#ifndef DEVEL_ASM_ASM_H
|
||||
# define DEVEL_ASM_ASM_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include "arch.h"
|
||||
|
||||
|
||||
/* As */
|
||||
/* Asm */
|
||||
/* types */
|
||||
typedef struct _As As;
|
||||
typedef struct _Asm Asm;
|
||||
|
||||
typedef enum _AsPluginType { ASPT_ARCH = 0, ASPT_FORMAT } AsPluginType;
|
||||
typedef enum _AsmPluginType { APT_ARCH = 0, APT_FORMAT } AsmPluginType;
|
||||
|
||||
|
||||
/* functions */
|
||||
As * as_new(char const * arch, char const * format);
|
||||
void as_delete(As * as);
|
||||
Asm * asm_new(char const * arch, char const * format);
|
||||
void asm_delete(Asm * a);
|
||||
|
||||
|
||||
/* accessors */
|
||||
char const * as_get_arch_name(As * as);
|
||||
char const * as_get_format_name(As * as);
|
||||
char const * asm_get_arch_name(Asm * a);
|
||||
char const * asm_get_format_name(Asm * a);
|
||||
|
||||
|
||||
/* useful */
|
||||
int as_decode(As * as, char const * buffer, size_t size);
|
||||
int as_decode_file(As * as, char const * filename, FILE * fp);
|
||||
int as_parse(As * as, char const * infile, char const * outfile);
|
||||
int asm_decode(Asm * a, char const * buffer, size_t size);
|
||||
int asm_decode_file(Asm * a, char const * filename, FILE * fp);
|
||||
int asm_parse(Asm * a, char const * infile, char const * outfile);
|
||||
|
||||
int as_open(As * as, char const * outfile);
|
||||
int as_close(As * as);
|
||||
int as_section(As * as, char const * name);
|
||||
int as_function(As * as, char const * name);
|
||||
int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...);
|
||||
int asm_open(Asm * a, char const * outfile);
|
||||
int asm_close(Asm * a);
|
||||
int asm_section(Asm * a, char const * name);
|
||||
int asm_function(Asm * a, char const * name);
|
||||
int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...);
|
||||
|
||||
|
||||
/* plugins helpers */
|
||||
int as_plugin_list(AsPluginType type);
|
||||
int asm_plugin_list(AsmPluginType type);
|
||||
|
||||
#endif /* !DEVEL_ASM_AS_H */
|
||||
|
132
src/asm.c
132
src/asm.c
@ -2,7 +2,7 @@
|
||||
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS Devel asm */
|
||||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* it under the terms of the GNU General Public License a published by
|
||||
* the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
@ -30,28 +30,28 @@
|
||||
#include "../config.h"
|
||||
|
||||
|
||||
/* as */
|
||||
/* Asm */
|
||||
/* private */
|
||||
/* types */
|
||||
struct _As
|
||||
struct _Asm
|
||||
{
|
||||
Code * code;
|
||||
};
|
||||
|
||||
typedef struct _AsPluginDescription
|
||||
typedef struct _AsmPluginDescription
|
||||
{
|
||||
char const * name;
|
||||
char const * description;
|
||||
} AsPluginDescription;
|
||||
} AsmPluginDescription;
|
||||
|
||||
|
||||
/* constants */
|
||||
#define ASPT_LAST ASPT_FORMAT
|
||||
#define ASPT_COUNT (ASPT_LAST + 1)
|
||||
#define APT_LAST APT_FORMAT
|
||||
#define APT_COUNT (APT_LAST + 1)
|
||||
|
||||
|
||||
/* variables */
|
||||
static const AsPluginDescription _as_plugin_description[ASPT_COUNT] =
|
||||
static const AsmPluginDescription _asm_plugin_description[APT_COUNT] =
|
||||
{
|
||||
{ "arch", "architecture" },
|
||||
{ "format", "file format" }
|
||||
@ -59,12 +59,12 @@ static const AsPluginDescription _as_plugin_description[ASPT_COUNT] =
|
||||
|
||||
|
||||
/* prototypes */
|
||||
static char const * _as_guess_arch(void);
|
||||
static char const * _asm_guess_arch(void);
|
||||
|
||||
|
||||
/* functions */
|
||||
/* as_guess_arch */
|
||||
static char const * _as_guess_arch(void)
|
||||
/* asm_guess_arch */
|
||||
static char const * _asm_guess_arch(void)
|
||||
{
|
||||
static struct utsname uts;
|
||||
static int cached = 0;
|
||||
@ -84,129 +84,129 @@ static char const * _as_guess_arch(void)
|
||||
|
||||
/* public */
|
||||
/* functions */
|
||||
/* as_new */
|
||||
As * as_new(char const * arch, char const * format)
|
||||
/* asm_new */
|
||||
Asm * asm_new(char const * arch, char const * format)
|
||||
{
|
||||
As * as;
|
||||
Asm * a;
|
||||
|
||||
if((as = object_new(sizeof(*as))) == NULL)
|
||||
if((a = object_new(sizeof(*a))) == NULL)
|
||||
return NULL;
|
||||
if(arch == NULL)
|
||||
arch = _as_guess_arch();
|
||||
if((as->code = code_new(arch, format)) == NULL)
|
||||
arch = _asm_guess_arch();
|
||||
if((a->code = code_new(arch, format)) == NULL)
|
||||
{
|
||||
object_delete(as);
|
||||
object_delete(a);
|
||||
return NULL;
|
||||
}
|
||||
return as;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
/* as_delete */
|
||||
void as_delete(As * as)
|
||||
/* asm_delete */
|
||||
void asm_delete(Asm * a)
|
||||
{
|
||||
code_delete(as->code);
|
||||
object_delete(as);
|
||||
code_delete(a->code);
|
||||
object_delete(a);
|
||||
}
|
||||
|
||||
|
||||
/* accessors */
|
||||
/* as_get_arch */
|
||||
Arch * as_get_arch(As * as)
|
||||
/* asm_get_arch */
|
||||
Arch * asm_get_arch(Asm * a)
|
||||
{
|
||||
return code_get_arch(as->code);
|
||||
return code_get_arch(a->code);
|
||||
}
|
||||
|
||||
|
||||
/* as_get_arch_name */
|
||||
char const * as_get_arch_name(As * as)
|
||||
/* asm_get_arch_name */
|
||||
char const * asm_get_arch_name(Asm * a)
|
||||
{
|
||||
return code_get_arch_name(as->code);
|
||||
return code_get_arch_name(a->code);
|
||||
}
|
||||
|
||||
|
||||
/* as_get_format */
|
||||
Format * as_get_format(As * as)
|
||||
/* asm_get_format */
|
||||
Format * asm_get_format(Asm * a)
|
||||
{
|
||||
return code_get_format(as->code);
|
||||
return code_get_format(a->code);
|
||||
}
|
||||
|
||||
|
||||
/* as_get_format_name */
|
||||
char const * as_get_format_name(As * as)
|
||||
/* asm_get_format_name */
|
||||
char const * asm_get_format_name(Asm * a)
|
||||
{
|
||||
return code_get_format_name(as->code);
|
||||
return code_get_format_name(a->code);
|
||||
}
|
||||
|
||||
|
||||
/* useful */
|
||||
/* as_close */
|
||||
int as_close(As * as)
|
||||
/* asm_close */
|
||||
int asm_close(Asm * a)
|
||||
{
|
||||
return code_close(as->code);
|
||||
return code_close(a->code);
|
||||
}
|
||||
|
||||
|
||||
/* as_decode */
|
||||
int as_decode(As * as, char const * buffer, size_t size)
|
||||
/* asm_decode */
|
||||
int asm_decode(Asm * a, char const * buffer, size_t size)
|
||||
{
|
||||
return code_decode(as->code, buffer, size);
|
||||
return code_decode(a->code, buffer, size);
|
||||
}
|
||||
|
||||
|
||||
/* as_decode_file */
|
||||
int as_decode_file(As * as, char const * filename, FILE * fp)
|
||||
/* asm_decode_file */
|
||||
int asm_decode_file(Asm * a, char const * filename, FILE * fp)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if(fp != NULL)
|
||||
return code_decode_file(as->code, filename, fp);
|
||||
return code_decode_file(a->code, filename, fp);
|
||||
if((fp = fopen(filename, "r")) == NULL)
|
||||
return -error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||
ret = code_decode_file(as->code, filename, fp);
|
||||
ret = code_decode_file(a->code, filename, fp);
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* as_parse */
|
||||
int as_parse(As * as, char const * infile, char const * outfile)
|
||||
/* asm_parse */
|
||||
int asm_parse(Asm * a, char const * infile, char const * outfile)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if(as_open(as, outfile) != 0)
|
||||
if(asm_open(a, outfile) != 0)
|
||||
return -1;
|
||||
ret = parser(as->code, infile);
|
||||
ret = parser(a->code, infile);
|
||||
if(ret != 0 && unlink(outfile) != 0)
|
||||
ret |= error_set_code(3, "%s: %s", outfile, strerror(errno));
|
||||
ret |= as_close(as);
|
||||
ret |= asm_close(a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* as_open */
|
||||
int as_open(As * as, char const * outfile)
|
||||
/* asm_open */
|
||||
int asm_open(Asm * a, char const * outfile)
|
||||
{
|
||||
return code_open(as->code, outfile);
|
||||
return code_open(a->code, outfile);
|
||||
}
|
||||
|
||||
|
||||
/* as_section */
|
||||
int as_section(As * as, char const * name)
|
||||
/* asm_section */
|
||||
int asm_section(Asm * a, char const * name)
|
||||
{
|
||||
return code_section(as->code, name);
|
||||
return code_section(a->code, name);
|
||||
}
|
||||
|
||||
|
||||
/* as_function */
|
||||
int as_function(As * as, char const * name)
|
||||
/* asm_function */
|
||||
int asm_function(Asm * a, char const * name)
|
||||
{
|
||||
return code_function(as->code, name);
|
||||
return code_function(a->code, name);
|
||||
}
|
||||
|
||||
|
||||
/* as_instruction */
|
||||
int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...)
|
||||
/* asm_instruction */
|
||||
int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...)
|
||||
{
|
||||
ArchInstructionCall call;
|
||||
va_list ap;
|
||||
@ -225,21 +225,21 @@ int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...)
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
return code_instruction(as->code, &call);
|
||||
return code_instruction(a->code, &call);
|
||||
}
|
||||
|
||||
|
||||
/* as_plugin_list */
|
||||
int as_plugin_list(AsPluginType type)
|
||||
/* asm_plugin_list */
|
||||
int asm_plugin_list(AsmPluginType type)
|
||||
{
|
||||
AsPluginDescription const * aspd;
|
||||
AsmPluginDescription const * aspd;
|
||||
char * path;
|
||||
DIR * dir;
|
||||
struct dirent * de;
|
||||
size_t len;
|
||||
char const * sep = "";
|
||||
|
||||
aspd = &_as_plugin_description[type];
|
||||
aspd = &_asm_plugin_description[type];
|
||||
fprintf(stderr, "%s%s%s", "Available ", aspd->description,
|
||||
" plug-ins:\n");
|
||||
len = strlen(LIBDIR) + 1 + strlen(PACKAGE) + 1 + strlen(aspd->name) + 1;
|
||||
|
@ -25,7 +25,7 @@
|
||||
|
||||
/* functions */
|
||||
/* accessors */
|
||||
Arch * as_get_arch(As * as);
|
||||
Format * as_get_format(As * as);
|
||||
Arch * asm_get_arch(Asm * a);
|
||||
Format * asm_get_format(Asm * a);
|
||||
|
||||
#endif /* !ASM_ASM_H */
|
||||
|
22
src/deasm.c
22
src/deasm.c
@ -194,7 +194,7 @@ static int _deasm_do(Deasm * deasm)
|
||||
static int _deasm_do_callback(Deasm * deasm, FormatPlugin * format)
|
||||
{
|
||||
int ret;
|
||||
As * as;
|
||||
Asm * a;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, format->name);
|
||||
@ -207,12 +207,12 @@ static int _deasm_do_callback(Deasm * deasm, FormatPlugin * format)
|
||||
if((deasm->arch = format->detect(format)) == NULL)
|
||||
return -1;
|
||||
}
|
||||
if((as = as_new(deasm->arch, format->name)) == NULL)
|
||||
if((a = asm_new(deasm->arch, format->name)) == NULL)
|
||||
return -error_print("deasm");
|
||||
printf("\n%s: %s-%s\n", deasm->filename, format->name, as_get_arch_name(
|
||||
as));
|
||||
ret = as_decode_file(as, deasm->filename, deasm->fp);
|
||||
as_delete(as);
|
||||
printf("\n%s: %s-%s\n", deasm->filename, format->name,
|
||||
asm_get_arch_name(a));
|
||||
ret = asm_decode_file(a, deasm->filename, deasm->fp);
|
||||
asm_delete(a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -221,16 +221,16 @@ static int _deasm_do_callback(Deasm * deasm, FormatPlugin * format)
|
||||
static int _deasm_buffer(char const * arch, char const * format,
|
||||
char const * buffer, size_t size)
|
||||
{
|
||||
As * as;
|
||||
Asm * a;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||
#endif
|
||||
if((as = as_new(arch, format)) == NULL)
|
||||
if((a = asm_new(arch, format)) == NULL)
|
||||
return -1;
|
||||
if(as_decode(as, buffer, size) != 0)
|
||||
if(asm_decode(a, buffer, size) != 0)
|
||||
error_print("deasm");
|
||||
as_delete(as);
|
||||
asm_delete(a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -313,7 +313,7 @@ static int _deasm_list(void)
|
||||
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||
#endif
|
||||
memset(&deasm, 0, sizeof(deasm));
|
||||
as_plugin_list(ASPT_ARCH);
|
||||
asm_plugin_list(APT_ARCH);
|
||||
_deasm_format_open_all(&deasm);
|
||||
fputs("\nAvailable format plug-ins:\n", stderr);
|
||||
for(i = 0; i < deasm.format_cnt; i++)
|
||||
|
24
src/main.c
24
src/main.c
@ -26,22 +26,22 @@
|
||||
/* as */
|
||||
/* private */
|
||||
/* constants */
|
||||
# define AS_FILENAME_DEFAULT "a.out"
|
||||
# define ASM_FILENAME_DEFAULT "a.out"
|
||||
|
||||
|
||||
/* functions */
|
||||
/* as */
|
||||
static int _as(char const * arch, char const * format, char const * infile,
|
||||
/* asm */
|
||||
static int _asm(char const * arch, char const * format, char const * infile,
|
||||
char const * outfile)
|
||||
{
|
||||
int ret = 0;
|
||||
As * as;
|
||||
Asm * a;
|
||||
|
||||
if((as = as_new(arch, format)) == NULL)
|
||||
if((a = asm_new(arch, format)) == NULL)
|
||||
return error_print(PACKAGE);
|
||||
if(as_parse(as, infile, outfile) != 0)
|
||||
if(asm_parse(a, infile, outfile) != 0)
|
||||
ret = error_print(PACKAGE);
|
||||
as_delete(as);
|
||||
asm_delete(a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ static unsigned int _usage(void)
|
||||
" as -l\n"
|
||||
" -a target architecture\n"
|
||||
" -f target file format\n"
|
||||
" -o filename to use for output (default: " AS_FILENAME_DEFAULT ")\n"
|
||||
" -o filename to use for output (default: " ASM_FILENAME_DEFAULT ")\n"
|
||||
" -l list available architectures and formats\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
@ -64,7 +64,7 @@ static unsigned int _usage(void)
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int o;
|
||||
char * outfile = AS_FILENAME_DEFAULT;
|
||||
char * outfile = ASM_FILENAME_DEFAULT;
|
||||
char const * arch = NULL;
|
||||
char const * format = NULL;
|
||||
|
||||
@ -83,11 +83,11 @@ int main(int argc, char * argv[])
|
||||
break;
|
||||
case 'l':
|
||||
o = 0;
|
||||
if(as_plugin_list(ASPT_ARCH) != 0)
|
||||
if(asm_plugin_list(APT_ARCH) != 0)
|
||||
o = error_print(PACKAGE);
|
||||
else
|
||||
putchar('\n');
|
||||
if(as_plugin_list(ASPT_FORMAT) != 0)
|
||||
if(asm_plugin_list(APT_FORMAT) != 0)
|
||||
o = error_print(PACKAGE);
|
||||
return (o == 0) ? 0 : 2;
|
||||
default:
|
||||
@ -96,5 +96,5 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
if(optind + 1 != argc)
|
||||
return _usage();
|
||||
return (_as(arch, format, argv[optind], outfile) == 0) ? 0 : 2;
|
||||
return (_asm(arch, format, argv[optind], outfile) == 0) ? 0 : 2;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user