Implemented "-D" to influence the pre-processor
This commit is contained in:
parent
266f4c2682
commit
2e7902b0cb
@ -25,6 +25,12 @@
|
||||
/* types */
|
||||
typedef struct _Asm Asm;
|
||||
|
||||
typedef struct _AsmPrefs
|
||||
{
|
||||
char ** defines;
|
||||
size_t defines_cnt;
|
||||
} AsmPrefs;
|
||||
|
||||
typedef unsigned int AsmId;
|
||||
|
||||
typedef struct _AsmFunction
|
||||
@ -93,7 +99,8 @@ int asm_guess_format(Asm * a);
|
||||
int asm_close(Asm * a);
|
||||
|
||||
/* 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_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...);
|
||||
|
@ -149,13 +149,14 @@ int asm_set_section(Asm * a, char const * name, off_t offset, ssize_t size)
|
||||
|
||||
/* useful */
|
||||
/* 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;
|
||||
|
||||
if(_asm_open(a, outfile) != 0)
|
||||
return -1;
|
||||
ret = parser(a->code, infile);
|
||||
ret = parser(prefs, a->code, infile);
|
||||
if(ret != 0 && unlink(outfile) != 0)
|
||||
ret |= error_set_code(3, "%s: %s", outfile, strerror(errno));
|
||||
ret |= asm_close(a);
|
||||
|
58
src/main.c
58
src/main.c
@ -17,7 +17,10 @@
|
||||
|
||||
#include <System.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "Asm/asm.h"
|
||||
#include "../config.h"
|
||||
|
||||
@ -30,15 +33,15 @@
|
||||
|
||||
/* functions */
|
||||
/* asm */
|
||||
static int _asm(char const * arch, char const * format, char const * infile,
|
||||
char const * outfile)
|
||||
static int _asm(AsmPrefs * prefs, char const * arch, char const * format,
|
||||
char const * infile, char const * outfile)
|
||||
{
|
||||
int ret = 0;
|
||||
Asm * a;
|
||||
|
||||
if((a = asm_new(arch, format)) == NULL)
|
||||
return error_print(PACKAGE);
|
||||
if(asm_assemble(a, infile, outfile) != 0)
|
||||
if(asm_assemble(a, prefs, infile, outfile) != 0)
|
||||
ret = error_print(PACKAGE);
|
||||
asm_delete(a);
|
||||
return ret;
|
||||
@ -48,32 +51,41 @@ static int _asm(char const * arch, char const * format, char const * infile,
|
||||
/* usage */
|
||||
static unsigned int _usage(void)
|
||||
{
|
||||
fputs("Usage: as [-a arch][-f format][-o file] file\n"
|
||||
" as -l\n"
|
||||
" -a target architecture\n"
|
||||
" -f target file format\n"
|
||||
" -o filename to use for output (default: " ASM_FILENAME_DEFAULT ")\n"
|
||||
" -l list available architectures and formats\n", stderr);
|
||||
fputs("Usage: asm [-D name][-a arch][-f format][-o file] file\n"
|
||||
" asm -l\n"
|
||||
" -D Set a variable in the pre-processor\n"
|
||||
" -a Target architecture\n"
|
||||
" -f Target file format\n"
|
||||
" -o Filename to use for output (default: " ASM_FILENAME_DEFAULT ")\n"
|
||||
" -l List available architectures and formats\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* public */
|
||||
/* main */
|
||||
static int _main_add_define(AsmPrefs * prefs, char * define);
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int ret;
|
||||
AsmPrefs prefs;
|
||||
int o;
|
||||
char * outfile = ASM_FILENAME_DEFAULT;
|
||||
char const * arch = 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)
|
||||
{
|
||||
case 'a':
|
||||
arch = optarg;
|
||||
break;
|
||||
case 'D':
|
||||
if(_main_add_define(&prefs, optarg) != 0)
|
||||
return 2;
|
||||
break;
|
||||
case 'f':
|
||||
format = optarg;
|
||||
break;
|
||||
@ -92,8 +104,28 @@ int main(int argc, char * argv[])
|
||||
default:
|
||||
return _usage();
|
||||
}
|
||||
}
|
||||
if(optind + 1 != argc)
|
||||
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;
|
||||
}
|
||||
|
@ -187,10 +187,11 @@ static int _parser_warning(State * state, char const * format, ...)
|
||||
/* protected */
|
||||
/* functions */
|
||||
/* parser */
|
||||
int parser(Code * code, char const * infile)
|
||||
int parser(AsmPrefs * ap, Code * code, char const * infile)
|
||||
{
|
||||
CppPrefs prefs;
|
||||
State state;
|
||||
size_t i;
|
||||
|
||||
memset(&prefs, 0, sizeof(prefs));
|
||||
prefs.filename = infile;
|
||||
@ -199,6 +200,10 @@ int parser(Code * code, char const * infile)
|
||||
state.code = code;
|
||||
if((state.cpp = cpp_new(&prefs)) == NULL)
|
||||
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)
|
||||
return _parser_error(&state, "%s", error_get());
|
||||
if(_program(&state) != 0)
|
||||
|
@ -21,6 +21,6 @@
|
||||
# include "code.h"
|
||||
|
||||
|
||||
int parser(Code * code, char const * infile);
|
||||
int parser(AsmPrefs * prefs, Code * code, char const * infile);
|
||||
|
||||
#endif /* !ASM_PARSER_H */
|
||||
|
Loading…
Reference in New Issue
Block a user