Implemented "-D" to influence the pre-processor

This commit is contained in:
Pierre Pronchery 2011-05-04 16:18:41 +00:00
parent 266f4c2682
commit 2e7902b0cb
5 changed files with 63 additions and 18 deletions

View File

@ -25,6 +25,12 @@
/* types */ /* types */
typedef struct _Asm Asm; typedef struct _Asm Asm;
typedef struct _AsmPrefs
{
char ** defines;
size_t defines_cnt;
} AsmPrefs;
typedef unsigned int AsmId; typedef unsigned int AsmId;
typedef struct _AsmFunction typedef struct _AsmFunction
@ -93,7 +99,8 @@ int asm_guess_format(Asm * a);
int asm_close(Asm * a); int asm_close(Asm * a);
/* assemble */ /* assemble */
int asm_assemble(Asm * a, char const * infile, char const * outfile); int asm_assemble(Asm * a, AsmPrefs * prefs, char const * infile,
char const * outfile);
int asm_open_assemble(Asm * a, char const * outfile); int asm_open_assemble(Asm * a, char const * outfile);
int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...); int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...);

View File

@ -149,13 +149,14 @@ int asm_set_section(Asm * a, char const * name, off_t offset, ssize_t size)
/* useful */ /* useful */
/* asm_assemble */ /* asm_assemble */
int asm_assemble(Asm * a, char const * infile, char const * outfile) int asm_assemble(Asm * a, AsmPrefs * prefs, char const * infile,
char const * outfile)
{ {
int ret; int ret;
if(_asm_open(a, outfile) != 0) if(_asm_open(a, outfile) != 0)
return -1; return -1;
ret = parser(a->code, infile); ret = parser(prefs, 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 |= asm_close(a); ret |= asm_close(a);

View File

@ -17,7 +17,10 @@
#include <System.h> #include <System.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <errno.h>
#include "Asm/asm.h" #include "Asm/asm.h"
#include "../config.h" #include "../config.h"
@ -30,15 +33,15 @@
/* functions */ /* functions */
/* asm */ /* asm */
static int _asm(char const * arch, char const * format, char const * infile, static int _asm(AsmPrefs * prefs, char const * arch, char const * format,
char const * outfile) char const * infile, char const * outfile)
{ {
int ret = 0; int ret = 0;
Asm * a; Asm * a;
if((a = asm_new(arch, format)) == NULL) if((a = asm_new(arch, format)) == NULL)
return error_print(PACKAGE); return error_print(PACKAGE);
if(asm_assemble(a, infile, outfile) != 0) if(asm_assemble(a, prefs, infile, outfile) != 0)
ret = error_print(PACKAGE); ret = error_print(PACKAGE);
asm_delete(a); asm_delete(a);
return ret; return ret;
@ -48,32 +51,41 @@ static int _asm(char const * arch, char const * format, char const * infile,
/* usage */ /* usage */
static unsigned int _usage(void) static unsigned int _usage(void)
{ {
fputs("Usage: as [-a arch][-f format][-o file] file\n" fputs("Usage: asm [-D name][-a arch][-f format][-o file] file\n"
" as -l\n" " asm -l\n"
" -a target architecture\n" " -D Set a variable in the pre-processor\n"
" -f target file format\n" " -a Target architecture\n"
" -o filename to use for output (default: " ASM_FILENAME_DEFAULT ")\n" " -f Target file format\n"
" -l list available architectures and formats\n", stderr); " -o Filename to use for output (default: " ASM_FILENAME_DEFAULT ")\n"
" -l List available architectures and formats\n", stderr);
return 1; return 1;
} }
/* public */ /* public */
/* main */ /* main */
static int _main_add_define(AsmPrefs * prefs, char * define);
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int ret;
AsmPrefs prefs;
int o; int o;
char * outfile = ASM_FILENAME_DEFAULT; char * outfile = ASM_FILENAME_DEFAULT;
char const * arch = NULL; char const * arch = NULL;
char const * format = NULL; char const * format = NULL;
while((o = getopt(argc, argv, "a:f:o:l")) != -1) memset(&prefs, 0, sizeof(prefs));
{ while((o = getopt(argc, argv, "a:D:f:o:l")) != -1)
switch(o) switch(o)
{ {
case 'a': case 'a':
arch = optarg; arch = optarg;
break; break;
case 'D':
if(_main_add_define(&prefs, optarg) != 0)
return 2;
break;
case 'f': case 'f':
format = optarg; format = optarg;
break; break;
@ -92,8 +104,28 @@ int main(int argc, char * argv[])
default: default:
return _usage(); return _usage();
} }
}
if(optind + 1 != argc) if(optind + 1 != argc)
return _usage(); return _usage();
return (_asm(arch, format, argv[optind], outfile) == 0) ? 0 : 2; ret = _asm(&prefs, arch, format, argv[optind], outfile);
free(prefs.defines);
return (ret == 0) ? 0 : 2;
}
static int _main_add_define(AsmPrefs * prefs, char * define)
{
char ** p;
char * value;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, define);
#endif
if(strlen(define) == 0)
return -_usage();
value = strtok(define, "=");
if((p = realloc(prefs->defines, sizeof(*p) * (prefs->defines_cnt + 1)))
== NULL)
return -error_set_print(PACKAGE, 1, "%s", strerror(errno));
prefs->defines = p;
prefs->defines[prefs->defines_cnt++] = define;
return 0;
} }

View File

@ -187,10 +187,11 @@ static int _parser_warning(State * state, char const * format, ...)
/* protected */ /* protected */
/* functions */ /* functions */
/* parser */ /* parser */
int parser(Code * code, char const * infile) int parser(AsmPrefs * ap, Code * code, char const * infile)
{ {
CppPrefs prefs; CppPrefs prefs;
State state; State state;
size_t i;
memset(&prefs, 0, sizeof(prefs)); memset(&prefs, 0, sizeof(prefs));
prefs.filename = infile; prefs.filename = infile;
@ -199,6 +200,10 @@ int parser(Code * code, char const * infile)
state.code = code; state.code = code;
if((state.cpp = cpp_new(&prefs)) == NULL) if((state.cpp = cpp_new(&prefs)) == NULL)
return _parser_error(&state, "%s", error_get()); return _parser_error(&state, "%s", error_get());
if(ap != NULL)
for(i = 0; i < ap->defines_cnt; i++)
/* FIXME check errors */
cpp_define_add(state.cpp, ap->defines[i], NULL);
if(_parser_scan(&state) != 0) if(_parser_scan(&state) != 0)
return _parser_error(&state, "%s", error_get()); return _parser_error(&state, "%s", error_get());
if(_program(&state) != 0) if(_program(&state) != 0)

View File

@ -21,6 +21,6 @@
# include "code.h" # include "code.h"
int parser(Code * code, char const * infile); int parser(AsmPrefs * prefs, Code * code, char const * infile);
#endif /* !ASM_PARSER_H */ #endif /* !ASM_PARSER_H */