summaryrefslogtreecommitdiff
path: root/src/output
diff options
context:
space:
mode:
Diffstat (limited to 'src/output')
-rw-r--r--src/output/amgc_output_compound.c112
-rw-r--r--src/output/amgc_output_entities.c18
-rw-r--r--src/output/amgc_output_enum.c83
-rw-r--r--src/output/amgc_output_pad_symbol.c17
-rw-r--r--src/output/amgc_output_typedef.c44
5 files changed, 145 insertions, 129 deletions
diff --git a/src/output/amgc_output_compound.c b/src/output/amgc_output_compound.c
index 312d475..1736a06 100644
--- a/src/output/amgc_output_compound.c
+++ b/src/output/amgc_output_compound.c
@@ -16,14 +16,14 @@
#include "apimagic_driver_impl.h"
static int output_string(
- FILE * fout,
+ int * fdout,
const char * fmt,
const char * string,
const char * brace,
int * len)
{
- int ret = fout
- ? fprintf(fout,fmt,string,brace)
+ int ret = fdout
+ ? amgc_dprintf(*fdout,fmt,string,brace)
: snprintf(0,0,fmt,string,brace);
if (len && (ret > 0))
@@ -36,7 +36,7 @@ static int output_compound(
union entity_t const * entity,
int depth,
const struct amgc_layout * layout,
- FILE * fout)
+ int * fdout)
{
struct compound_t const * compound;
type_qualifiers_t tquals;
@@ -104,14 +104,14 @@ static int output_compound(
}
- if (depth && fout && (fputc('\n',fout) < 0))
+ if (depth && fdout && (amgc_dprintf(*fdout,"\n") < 0))
return -1;
for (i=0; i<depth; i++)
- if (output_string(fout,"\t","","",0) < 0)
+ if (output_string(fdout,"\t","","",0) < 0)
return -1;
- if (output_string(fout,fmt,name,brace,&len) < 0)
+ if (output_string(fdout,fmt,name,brace,&len) < 0)
return -1;
@@ -156,7 +156,7 @@ static int output_compound(
if (output_compound(
entity,depth,
- layout,fout) < 0)
+ layout,fdout) < 0)
return -1;
}
@@ -171,8 +171,10 @@ static int output_compound(
fmt = "";
name = "";
- if (fout)
- fprintf(fout,"UNHANDLED TYPE! %d ",
+ if (fdout)
+ amgc_dprintf(
+ *fdout,
+ "UNHANDLED TYPE! %d ",
type->kind);
break;
@@ -180,14 +182,14 @@ static int output_compound(
if (ftabs)
for (i=0; i<depth; i++)
- if (output_string(fout,"\t","","",0) < 0)
+ if (output_string(fdout,"\t","","",0) < 0)
return -1;
if (tquals & TYPE_QUALIFIER_CONST)
- if (output_string(fout,"const ","","",&len) < 0)
+ if (output_string(fdout,"const ","","",&len) < 0)
return -1;
- if (output_string(fout,fmt,name,"",&len) < 0)
+ if (output_string(fdout,fmt,name,"",&len) < 0)
return -1;
for (fspace=ptrdepth; ptrdepth; ptrdepth--) {
@@ -201,18 +203,18 @@ static int output_compound(
if (type->base.qualifiers & TYPE_QUALIFIER_CONST)
if (type->kind == TYPE_POINTER)
- if (output_string(fout," const ",
+ if (output_string(fdout," const ",
"","",&len) < 0)
return -1;
- if (output_string(fout,"*","","",&len) < 0)
+ if (output_string(fdout,"*","","",&len) < 0)
return -1;
}
if (fspace)
len++;
- if (fout) {
+ if (fdout) {
symwidth = layout->symwidth;
symwidth += layout->tabwidth;
@@ -221,7 +223,7 @@ static int output_compound(
len &= (~(layout->tabwidth-1));
while (len < symwidth) {
- if (fputc('\t',fout) < 0)
+ if (amgc_dprintf(*fdout,"\t") < 0)
return -1;
else
len += layout->tabwidth;
@@ -229,15 +231,16 @@ static int output_compound(
} else if (len > width)
width = len;
- if (output_string(fout,"%s",
+ if (output_string(fdout,"%s",
entity->base.symbol->string,
"",0) < 0)
return -1;
type = entity->declaration.type;
- while (fout && type->kind == TYPE_ARRAY) {
- if (fprintf(fout,
+ while (fdout && type->kind == TYPE_ARRAY) {
+ if (amgc_dprintf(
+ *fdout,
type->array.size ? "[%zu]" : "[]",
type->array.size) < 0)
return -1;
@@ -245,26 +248,26 @@ static int output_compound(
type = type->array.element_type;
}
- if (fout && fputs(";\n",fout) < 0)
+ if (fdout && amgc_dprintf(*fdout,";\n") < 0)
return -1;
- if (!ftabs && fout && entity->base.next)
- if (fputc('\n',fout) < 0)
+ if (!ftabs && fdout && entity->base.next)
+ if (amgc_dprintf(*fdout,"\n") < 0)
return -1;
}
- if (!fout)
+ if (!fdout)
return width;
if (--depth) {
for (i=0; i<depth; i++)
- if (output_string(fout,"\t","","",0) < 0)
+ if (output_string(fdout,"\t","","",0) < 0)
return -1;
- if (output_string(fout,"} ","","",0) < 0)
+ if (output_string(fdout,"} ","","",0) < 0)
return -1;
} else {
- if (output_string(fout,"};\n","","",0) < 0)
+ if (output_string(fdout,"};\n","","",0) < 0)
return -1;
}
@@ -272,17 +275,18 @@ static int output_compound(
}
static int output_compound_entity(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_entity * aentity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
struct amgc_layout elayout;
+ int fdout = amgc_driver_fdout(dctx);
(void)uctx;
if (layout && layout->symwidth)
- return output_compound(aentity->entity,0,layout,fout);
+ return output_compound(aentity->entity,0,layout,&fdout);
if (layout)
memcpy(&elayout,layout,sizeof(elayout));
@@ -295,83 +299,81 @@ static int output_compound_entity(
if (elayout.tabwidth == 0)
elayout.tabwidth = AMGC_TAB_WIDTH;
- if (output_compound(aentity->entity,0,&elayout,fout) < 0)
+ if (output_compound(aentity->entity,0,&elayout,&fdout) < 0)
return -1;
return 0;
}
int amgc_output_compound(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_entity * aentity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
union entity_t const * entity;
entity = aentity->entity;
- if ((entity->kind == ENTITY_STRUCT) || (entity->kind == ENTITY_UNION))
- return output_compound_entity(uctx,aentity,layout,fout);
- else
- return -1;
+ return ((entity->kind == ENTITY_STRUCT) || (entity->kind == ENTITY_UNION))
+ ? output_compound_entity(dctx,uctx,aentity,layout)
+ : -1;
}
int amgc_output_struct(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_entity * aentity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
union entity_t const * entity;
entity = aentity->entity;
- if (entity->kind == ENTITY_STRUCT)
- return output_compound_entity(uctx,aentity,layout,fout);
- else
- return -1;
+ return (entity->kind == ENTITY_STRUCT)
+ ? output_compound_entity(dctx,uctx,aentity,layout)
+ : -1;
}
int amgc_output_union(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_entity * aentity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
union entity_t const * entity;
entity = aentity->entity;
- if (entity->kind == ENTITY_UNION)
- return output_compound_entity(uctx,aentity,layout,fout);
- else
- return -1;
+ return (entity->kind == ENTITY_UNION)
+ ? output_compound_entity(dctx,uctx,aentity,layout)
+ : -1;
+
}
int amgc_output_unit_structs(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
const struct amgc_entity * aentity;
for (aentity=uctx->entities->structs; aentity->entity; aentity++)
- if (output_compound_entity(uctx,aentity,layout,fout))
+ if (output_compound_entity(dctx,uctx,aentity,layout) < 0)
return -1;
return 0;
}
int amgc_output_unit_unions(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
const struct amgc_entity * aentity;
for (aentity=uctx->entities->unions; aentity->entity; aentity++)
- if (output_compound_entity(uctx,aentity,layout,fout))
+ if (output_compound_entity(dctx,uctx,aentity,layout) < 0)
return -1;
return 0;
diff --git a/src/output/amgc_output_entities.c b/src/output/amgc_output_entities.c
index c1618c2..c239249 100644
--- a/src/output/amgc_output_entities.c
+++ b/src/output/amgc_output_entities.c
@@ -13,39 +13,39 @@
#include "apimagic_driver_impl.h"
static int output_typedefs(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
int subset,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
struct amgc_entity * aentity;
for (aentity=uctx->entities->typedefs; aentity->entity; aentity++)
if (aentity->reftype->kind == subset)
- if (amgc_output_typedef(uctx,aentity,layout,fout) < 0)
+ if (amgc_output_typedef(dctx,uctx,aentity,layout) < 0)
return -1;
return 0;
}
int amgc_output_unit_entities(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
int kind,
int subset,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
switch (kind) {
case ENTITY_ENUM:
- return amgc_output_unit_enums(uctx,layout,fout);
+ return amgc_output_unit_enums(dctx,uctx,layout);
case ENTITY_TYPEDEF:
- return output_typedefs(uctx,subset,layout,fout);
+ return output_typedefs(dctx,uctx,subset,layout);
case ENTITY_STRUCT:
- return amgc_output_unit_structs(uctx,layout,fout);
+ return amgc_output_unit_structs(dctx,uctx,layout);
case ENTITY_UNION:
- return amgc_output_unit_unions(uctx,layout,fout);
+ return amgc_output_unit_unions(dctx,uctx,layout);
}
return -1;
diff --git a/src/output/amgc_output_enum.c b/src/output/amgc_output_enum.c
index 9498774..764ecb1 100644
--- a/src/output/amgc_output_enum.c
+++ b/src/output/amgc_output_enum.c
@@ -14,14 +14,17 @@
#include "apimagic_driver_impl.h"
static int output_enum(
+ const struct amgc_driver_ctx * dctx,
const char * symbol,
- const struct amgc_entity enumvals[],
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_entity * enumvals,
+ const struct amgc_layout * layout)
{
const struct amgc_entity * enumval;
struct amgc_layout elayout;
size_t len;
+ int fdout;
+
+ fdout = amgc_driver_fdout(dctx);
if (!layout || !layout->symwidth) {
if (!layout)
@@ -45,10 +48,11 @@ static int output_enum(
}
}
- if (layout->header && (fputs(layout->header,fout) < 0))
- return -1;
+ if (layout->header)
+ if (amgc_dprintf(fdout,layout->header) < 0)
+ return -1;
- if (fprintf(fout,"enum %s {\n",symbol) < 0)
+ if (amgc_dprintf(fdout,"enum %s {\n",symbol) < 0)
return -1;
for (enumval=enumvals; enumval->entity || enumval->altname; enumval++) {
@@ -56,69 +60,71 @@ static int output_enum(
? enumval->altname
: enumval->entity->base.symbol->string;
- if (fprintf(fout,"\t%s",symbol) < 0)
+ if (amgc_dprintf(fdout,"\t%s",symbol) < 0)
return -1;
- if (amgc_output_pad_symbol(symbol,layout,fout) < 0)
+ if (amgc_output_pad_symbol(dctx,symbol,layout) < 0)
return -1;
if ((enumval->enumval < 0) && (enumval->enumval > -128))
- fprintf(fout,"= (%d)",enumval->enumval);
+ amgc_dprintf(fdout,"= (%d)",enumval->enumval);
else if ((enumval->enumval >= 0) && (enumval->enumval < 2048))
- fprintf(fout,"= %d",enumval->enumval);
+ amgc_dprintf(fdout,"= %d",enumval->enumval);
else
- fprintf(fout,"= 0x%08x",(unsigned)enumval->enumval);
+ amgc_dprintf(fdout,"= 0x%08x",(unsigned)enumval->enumval);
- if (fputs(",\n",fout) < 0)
+ if (amgc_dprintf(fdout,",\n") < 0)
return -1;
}
- if (fputs("};\n",fout) < 0)
+ if (amgc_dprintf(fdout,"};\n") < 0)
return -1;
- if (layout->footer && (fputs(layout->footer,fout) < 0))
- return -1;
+ if (layout->footer)
+ if (amgc_dprintf(fdout,layout->footer) < 0)
+ return -1;
return 0;
}
int amgc_output_unit_enum(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const union entity_t * entity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
- struct amgc_entity * enumvals;
- const char * symbol;
int ret;
+ const char * symbol;
+ struct amgc_entity * enumvals;
if (entity->base.kind != ENTITY_ENUM)
return -1;
+
else if (amgc_get_enum_members(uctx,entity,&enumvals))
return -1;
- if (entity->base.symbol)
- symbol = entity->base.symbol->string;
- else
- symbol = "";
+ symbol = (entity->base.symbol)
+ ? entity->base.symbol->string
+ : "";
- ret = output_enum(symbol,enumvals,layout,fout);
+ ret = output_enum(dctx,symbol,enumvals,layout);
amgc_free_enum_members(enumvals);
return ret;
}
int amgc_output_custom_enum(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_entity * penum,
const struct amgc_entity enumvals[],
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
const struct amgc_entity * aentity;
const char * symbol;
if (penum->entity && penum->entity->base.kind != ENTITY_ENUM)
return -1;
+
else if (!penum->entity && !penum->altname)
return -1;
@@ -133,42 +139,45 @@ int amgc_output_custom_enum(
else
symbol = "";
- return output_enum(symbol,enumvals,layout,fout);
+ return output_enum(dctx,symbol,enumvals,layout);
}
int amgc_output_unit_enums(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
const struct amgc_entity * aentity;
for (aentity=uctx->entities->enums; aentity->entity; aentity++)
- if (amgc_output_unit_enum(uctx,aentity->entity,layout,fout))
+ if (amgc_output_unit_enum(dctx,uctx,aentity->entity,layout))
return -1;
return 0;
}
int amgc_list_unit_enums(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
const struct amgc_entity * aentity;
+ int fdout = amgc_driver_fdout(dctx);
- if (layout && layout->header && (fputs(layout->header,fout) < 0))
- return -1;
+ if (layout && layout->header)
+ if (amgc_dprintf(fdout,layout->header,fdout) < 0)
+ return -1;
for (aentity=uctx->entities->enums; aentity->entity; aentity++)
- if ((fprintf(fout,"enum %s;\n",
+ if ((amgc_dprintf(fdout,"enum %s;\n",
aentity->entity && aentity->entity->base.symbol
? aentity->entity->base.symbol->string
: aentity->altname) < 0))
return -1;
- if (layout && layout->footer && (fputs(layout->footer,fout) < 0))
- return -1;
+ if (layout && layout->footer)
+ if (amgc_dprintf(fdout,layout->footer) < 0)
+ return -1;
return 0;
}
diff --git a/src/output/amgc_output_pad_symbol.c b/src/output/amgc_output_pad_symbol.c
index 6c1d34a..12015c9 100644
--- a/src/output/amgc_output_pad_symbol.c
+++ b/src/output/amgc_output_pad_symbol.c
@@ -11,18 +11,21 @@
#include "apimagic_driver_impl.h"
int amgc_output_pad_symbol(
+ const struct amgc_driver_ctx * dctx,
const char * symbol,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
- int len = (int)(strlen(symbol));
+ int len = (int)(strlen(symbol));
+ int fdout = amgc_driver_fdout(dctx);
int symwidth = layout->symwidth;
if (layout->symwidth < 1)
return -1;
if (layout->tabwidth == 0)
- return fprintf(fout,"%*c",layout->symwidth-len,' ');
+ return amgc_dprintf(
+ fdout,"%*c",
+ layout->symwidth-len,' ');
symwidth += layout->tabwidth;
symwidth &= (~(layout->tabwidth-1));
@@ -30,10 +33,10 @@ int amgc_output_pad_symbol(
len &= (~(layout->tabwidth-1));
while (len < symwidth) {
- if (fputc('\t',fout) < 0)
+ if (amgc_dprintf(fdout,"\t") < 0)
return -1;
- else
- len += layout->tabwidth;
+
+ len += layout->tabwidth;
}
return 0;
diff --git a/src/output/amgc_output_typedef.c b/src/output/amgc_output_typedef.c
index f7b39d4..a579c18 100644
--- a/src/output/amgc_output_typedef.c
+++ b/src/output/amgc_output_typedef.c
@@ -14,58 +14,60 @@
#include <apimagic/apimagic.h>
#include "apimagic_driver_impl.h"
-static int output_atomic_typedef(
- const struct amgc_entity * aentity,
- FILE * fout)
+static int output_atomic_typedef(int fdout, const struct amgc_entity * aentity)
{
int i;
const char * reftype;
- (void)fout;
-
reftype = get_atomic_kind_name(aentity->reftype->atomic.akind);
- if (fprintf(stdout,"typedef %s ",reftype) < 0)
+ if (amgc_dprintf(fdout,"typedef %s ",reftype) < 0)
return -1;
for (i=0; (i < aentity->ptrdepth); i++)
- if (fputc('*',stdout) < 0)
+ if (amgc_dprintf(fdout,"*") < 0)
return -1;
- if (fprintf(stdout," %s;\n",aentity->entity->base.symbol->string) < 0)
+ if (amgc_dprintf(
+ fdout," %s;\n",
+ aentity->entity->base.symbol->string) < 0)
return -1;
return 0;
}
int amgc_output_typedef(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_entity * aentity,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
- int ret = 0;
+ int ret = 0;
+ int fdout = amgc_driver_fdout(dctx);
(void)uctx;
- if (layout && layout->header && (fputs(layout->header,fout) < 0))
- return -1;
-
switch (aentity->reftype->kind) {
case TYPE_ATOMIC:
- ret = output_atomic_typedef(aentity,fout);
break;
default:
- ret = -1;
+ return -1;
}
- if (ret)
- return ret;
+ if (layout && layout->header)
+ if (amgc_dprintf(fdout,layout->header) < 0)
+ return -1;
- if (layout && layout->footer && (fputs(layout->footer,fout) < 0))
- return -1;
+ if (aentity->reftype->kind == TYPE_ATOMIC)
+ ret = output_atomic_typedef(fdout,aentity);
+
+ if (ret < 0)
+ return ret;
+ if (layout && layout->footer)
+ if (amgc_dprintf(fdout,layout->footer) < 0)
+ return -1;
- return ret;
+ return 0;
}