Renamed the "As" class to "Asm"

This commit is contained in:
Pierre Pronchery 2011-04-27 23:54:47 +00:00
parent e7c3dc36b2
commit 7bbe4797be
5 changed files with 109 additions and 109 deletions

View File

@ -15,43 +15,43 @@
#ifndef DEVEL_ASM_AS_H #ifndef DEVEL_ASM_ASM_H
# define DEVEL_ASM_AS_H # define DEVEL_ASM_ASM_H
# include <stdio.h> # include <stdio.h>
# include "arch.h" # include "arch.h"
/* As */ /* Asm */
/* types */ /* 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 */ /* functions */
As * as_new(char const * arch, char const * format); Asm * asm_new(char const * arch, char const * format);
void as_delete(As * as); void asm_delete(Asm * a);
/* accessors */ /* accessors */
char const * as_get_arch_name(As * as); char const * asm_get_arch_name(Asm * a);
char const * as_get_format_name(As * as); char const * asm_get_format_name(Asm * a);
/* useful */ /* useful */
int as_decode(As * as, char const * buffer, size_t size); int asm_decode(Asm * a, char const * buffer, size_t size);
int as_decode_file(As * as, char const * filename, FILE * fp); int asm_decode_file(Asm * a, char const * filename, FILE * fp);
int as_parse(As * as, char const * infile, char const * outfile); int asm_parse(Asm * a, char const * infile, char const * outfile);
int as_open(As * as, char const * outfile); int asm_open(Asm * a, char const * outfile);
int as_close(As * as); int asm_close(Asm * a);
int as_section(As * as, char const * name); int asm_section(Asm * a, char const * name);
int as_function(As * as, char const * name); int asm_function(Asm * a, char const * name);
int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...); int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...);
/* plugins helpers */ /* plugins helpers */
int as_plugin_list(AsPluginType type); int asm_plugin_list(AsmPluginType type);
#endif /* !DEVEL_ASM_AS_H */ #endif /* !DEVEL_ASM_AS_H */

132
src/asm.c
View File

@ -2,7 +2,7 @@
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */ /* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Devel asm */ /* This file is part of DeforaOS Devel asm */
/* This program is free software: you can redistribute it and/or modify /* 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. * the Free Software Foundation, version 3 of the License.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
@ -30,28 +30,28 @@
#include "../config.h" #include "../config.h"
/* as */ /* Asm */
/* private */ /* private */
/* types */ /* types */
struct _As struct _Asm
{ {
Code * code; Code * code;
}; };
typedef struct _AsPluginDescription typedef struct _AsmPluginDescription
{ {
char const * name; char const * name;
char const * description; char const * description;
} AsPluginDescription; } AsmPluginDescription;
/* constants */ /* constants */
#define ASPT_LAST ASPT_FORMAT #define APT_LAST APT_FORMAT
#define ASPT_COUNT (ASPT_LAST + 1) #define APT_COUNT (APT_LAST + 1)
/* variables */ /* variables */
static const AsPluginDescription _as_plugin_description[ASPT_COUNT] = static const AsmPluginDescription _asm_plugin_description[APT_COUNT] =
{ {
{ "arch", "architecture" }, { "arch", "architecture" },
{ "format", "file format" } { "format", "file format" }
@ -59,12 +59,12 @@ static const AsPluginDescription _as_plugin_description[ASPT_COUNT] =
/* prototypes */ /* prototypes */
static char const * _as_guess_arch(void); static char const * _asm_guess_arch(void);
/* functions */ /* functions */
/* as_guess_arch */ /* asm_guess_arch */
static char const * _as_guess_arch(void) static char const * _asm_guess_arch(void)
{ {
static struct utsname uts; static struct utsname uts;
static int cached = 0; static int cached = 0;
@ -84,129 +84,129 @@ static char const * _as_guess_arch(void)
/* public */ /* public */
/* functions */ /* functions */
/* as_new */ /* asm_new */
As * as_new(char const * arch, char const * format) 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; return NULL;
if(arch == NULL) if(arch == NULL)
arch = _as_guess_arch(); arch = _asm_guess_arch();
if((as->code = code_new(arch, format)) == NULL) if((a->code = code_new(arch, format)) == NULL)
{ {
object_delete(as); object_delete(a);
return NULL; return NULL;
} }
return as; return a;
} }
/* as_delete */ /* asm_delete */
void as_delete(As * as) void asm_delete(Asm * a)
{ {
code_delete(as->code); code_delete(a->code);
object_delete(as); object_delete(a);
} }
/* accessors */ /* accessors */
/* as_get_arch */ /* asm_get_arch */
Arch * as_get_arch(As * as) Arch * asm_get_arch(Asm * a)
{ {
return code_get_arch(as->code); return code_get_arch(a->code);
} }
/* as_get_arch_name */ /* asm_get_arch_name */
char const * as_get_arch_name(As * as) 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 */ /* asm_get_format */
Format * as_get_format(As * as) Format * asm_get_format(Asm * a)
{ {
return code_get_format(as->code); return code_get_format(a->code);
} }
/* as_get_format_name */ /* asm_get_format_name */
char const * as_get_format_name(As * as) char const * asm_get_format_name(Asm * a)
{ {
return code_get_format_name(as->code); return code_get_format_name(a->code);
} }
/* useful */ /* useful */
/* as_close */ /* asm_close */
int as_close(As * as) int asm_close(Asm * a)
{ {
return code_close(as->code); return code_close(a->code);
} }
/* as_decode */ /* asm_decode */
int as_decode(As * as, char const * buffer, size_t size) 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 */ /* asm_decode_file */
int as_decode_file(As * as, char const * filename, FILE * fp) int asm_decode_file(Asm * a, char const * filename, FILE * fp)
{ {
int ret; int ret;
if(fp != NULL) 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) if((fp = fopen(filename, "r")) == NULL)
return -error_set_code(1, "%s: %s", filename, strerror(errno)); 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); fclose(fp);
return ret; return ret;
} }
/* as_parse */ /* asm_parse */
int as_parse(As * as, char const * infile, char const * outfile) int asm_parse(Asm * a, char const * infile, char const * outfile)
{ {
int ret; int ret;
if(as_open(as, outfile) != 0) if(asm_open(a, outfile) != 0)
return -1; return -1;
ret = parser(as->code, infile); ret = parser(a->code, infile);
if(ret != 0 && unlink(outfile) != 0) if(ret != 0 && unlink(outfile) != 0)
ret |= error_set_code(3, "%s: %s", outfile, strerror(errno)); ret |= error_set_code(3, "%s: %s", outfile, strerror(errno));
ret |= as_close(as); ret |= asm_close(a);
return ret; return ret;
} }
/* as_open */ /* asm_open */
int as_open(As * as, char const * outfile) int asm_open(Asm * a, char const * outfile)
{ {
return code_open(as->code, outfile); return code_open(a->code, outfile);
} }
/* as_section */ /* asm_section */
int as_section(As * as, char const * name) int asm_section(Asm * a, char const * name)
{ {
return code_section(as->code, name); return code_section(a->code, name);
} }
/* as_function */ /* asm_function */
int as_function(As * as, char const * name) int asm_function(Asm * a, char const * name)
{ {
return code_function(as->code, name); return code_function(a->code, name);
} }
/* as_instruction */ /* asm_instruction */
int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...) int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...)
{ {
ArchInstructionCall call; ArchInstructionCall call;
va_list ap; va_list ap;
@ -225,21 +225,21 @@ int as_instruction(As * as, char const * name, unsigned int operands_cnt, ...)
} }
va_end(ap); va_end(ap);
} }
return code_instruction(as->code, &call); return code_instruction(a->code, &call);
} }
/* as_plugin_list */ /* asm_plugin_list */
int as_plugin_list(AsPluginType type) int asm_plugin_list(AsmPluginType type)
{ {
AsPluginDescription const * aspd; AsmPluginDescription const * aspd;
char * path; char * path;
DIR * dir; DIR * dir;
struct dirent * de; struct dirent * de;
size_t len; size_t len;
char const * sep = ""; char const * sep = "";
aspd = &_as_plugin_description[type]; aspd = &_asm_plugin_description[type];
fprintf(stderr, "%s%s%s", "Available ", aspd->description, fprintf(stderr, "%s%s%s", "Available ", aspd->description,
" plug-ins:\n"); " plug-ins:\n");
len = strlen(LIBDIR) + 1 + strlen(PACKAGE) + 1 + strlen(aspd->name) + 1; len = strlen(LIBDIR) + 1 + strlen(PACKAGE) + 1 + strlen(aspd->name) + 1;

View File

@ -25,7 +25,7 @@
/* functions */ /* functions */
/* accessors */ /* accessors */
Arch * as_get_arch(As * as); Arch * asm_get_arch(Asm * a);
Format * as_get_format(As * as); Format * asm_get_format(Asm * a);
#endif /* !ASM_ASM_H */ #endif /* !ASM_ASM_H */

View File

@ -194,7 +194,7 @@ static int _deasm_do(Deasm * deasm)
static int _deasm_do_callback(Deasm * deasm, FormatPlugin * format) static int _deasm_do_callback(Deasm * deasm, FormatPlugin * format)
{ {
int ret; int ret;
As * as; Asm * a;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, format->name); 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) if((deasm->arch = format->detect(format)) == NULL)
return -1; return -1;
} }
if((as = as_new(deasm->arch, format->name)) == NULL) if((a = asm_new(deasm->arch, format->name)) == NULL)
return -error_print("deasm"); return -error_print("deasm");
printf("\n%s: %s-%s\n", deasm->filename, format->name, as_get_arch_name( printf("\n%s: %s-%s\n", deasm->filename, format->name,
as)); asm_get_arch_name(a));
ret = as_decode_file(as, deasm->filename, deasm->fp); ret = asm_decode_file(a, deasm->filename, deasm->fp);
as_delete(as); asm_delete(a);
return ret; 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, static int _deasm_buffer(char const * arch, char const * format,
char const * buffer, size_t size) char const * buffer, size_t size)
{ {
As * as; Asm * a;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__); fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif #endif
if((as = as_new(arch, format)) == NULL) if((a = asm_new(arch, format)) == NULL)
return -1; return -1;
if(as_decode(as, buffer, size) != 0) if(asm_decode(a, buffer, size) != 0)
error_print("deasm"); error_print("deasm");
as_delete(as); asm_delete(a);
return 0; return 0;
} }
@ -313,7 +313,7 @@ static int _deasm_list(void)
fprintf(stderr, "DEBUG: %s()\n", __func__); fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif #endif
memset(&deasm, 0, sizeof(deasm)); memset(&deasm, 0, sizeof(deasm));
as_plugin_list(ASPT_ARCH); asm_plugin_list(APT_ARCH);
_deasm_format_open_all(&deasm); _deasm_format_open_all(&deasm);
fputs("\nAvailable format plug-ins:\n", stderr); fputs("\nAvailable format plug-ins:\n", stderr);
for(i = 0; i < deasm.format_cnt; i++) for(i = 0; i < deasm.format_cnt; i++)

View File

@ -26,22 +26,22 @@
/* as */ /* as */
/* private */ /* private */
/* constants */ /* constants */
# define AS_FILENAME_DEFAULT "a.out" # define ASM_FILENAME_DEFAULT "a.out"
/* functions */ /* functions */
/* as */ /* asm */
static int _as(char const * arch, char const * format, char const * infile, static int _asm(char const * arch, char const * format, char const * infile,
char const * outfile) char const * outfile)
{ {
int ret = 0; 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); return error_print(PACKAGE);
if(as_parse(as, infile, outfile) != 0) if(asm_parse(a, infile, outfile) != 0)
ret = error_print(PACKAGE); ret = error_print(PACKAGE);
as_delete(as); asm_delete(a);
return ret; return ret;
} }
@ -53,7 +53,7 @@ static unsigned int _usage(void)
" as -l\n" " as -l\n"
" -a target architecture\n" " -a target architecture\n"
" -f target file format\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); " -l list available architectures and formats\n", stderr);
return 1; return 1;
} }
@ -64,7 +64,7 @@ static unsigned int _usage(void)
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int o; int o;
char * outfile = AS_FILENAME_DEFAULT; char * outfile = ASM_FILENAME_DEFAULT;
char const * arch = NULL; char const * arch = NULL;
char const * format = NULL; char const * format = NULL;
@ -83,11 +83,11 @@ int main(int argc, char * argv[])
break; break;
case 'l': case 'l':
o = 0; o = 0;
if(as_plugin_list(ASPT_ARCH) != 0) if(asm_plugin_list(APT_ARCH) != 0)
o = error_print(PACKAGE); o = error_print(PACKAGE);
else else
putchar('\n'); putchar('\n');
if(as_plugin_list(ASPT_FORMAT) != 0) if(asm_plugin_list(APT_FORMAT) != 0)
o = error_print(PACKAGE); o = error_print(PACKAGE);
return (o == 0) ? 0 : 2; return (o == 0) ? 0 : 2;
default: default:
@ -96,5 +96,5 @@ int main(int argc, char * argv[])
} }
if(optind + 1 != argc) if(optind + 1 != argc)
return _usage(); return _usage();
return (_as(arch, format, argv[optind], outfile) == 0) ? 0 : 2; return (_asm(arch, format, argv[optind], outfile) == 0) ? 0 : 2;
} }