summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-08-05 00:48:15 -0400
committermidipix <writeonce@midipix.org>2018-08-05 03:22:21 -0400
commitb8225b67f6e452b4751150e52b3dfee54744f4bf (patch)
tree748f2bac1ad7fb7041507ff7251ac0b9b4bac754 /src
parentabf92311681c10ed589ac05f0f2b451e01fbc63e (diff)
downloadapimagic-b8225b67f6e452b4751150e52b3dfee54744f4bf.tar.bz2
apimagic-b8225b67f6e452b4751150e52b3dfee54744f4bf.tar.xz
output interfaces: revised API and implementation to use fdctx and pure fdio.
Diffstat (limited to 'src')
-rw-r--r--src/driver/amgc_amain.c8
-rw-r--r--src/driver/amgc_unit_action.c16
-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
7 files changed, 158 insertions, 140 deletions
diff --git a/src/driver/amgc_amain.c b/src/driver/amgc_amain.c
index 096b960..9325a4f 100644
--- a/src/driver/amgc_amain.c
+++ b/src/driver/amgc_amain.c
@@ -47,12 +47,14 @@ static ssize_t amgc_version(struct amgc_driver_ctx * dctx, int fdout)
verclr[4],verinfo->commit,verclr[5]);
}
-static void amgc_perform_unit_actions(struct amgc_unit_ctx * uctx)
+static void amgc_perform_unit_actions(
+ const struct amgc_driver_ctx * dctx,
+ struct amgc_unit_ctx * uctx)
{
const struct amgc_action * action;
for (action=uctx->cctx->actions; action->type; action++)
- amgc_perform_unit_action(uctx,action,0,stdout);
+ amgc_perform_unit_action(dctx,uctx,action,0);
}
static int amgc_exit(struct amgc_driver_ctx * dctx, int ret)
@@ -85,7 +87,7 @@ int amgc_main(int argc, char ** argv, char ** envp, const struct amgc_fd_ctx * f
for (unit=dctx->units; *unit && !dctx->errv[0]; unit++) {
if (!(amgc_get_unit_ctx(dctx,*unit,&uctx))) {
- amgc_perform_unit_actions(uctx);
+ amgc_perform_unit_actions(dctx,uctx);
amgc_free_unit_ctx(uctx);
}
}
diff --git a/src/driver/amgc_unit_action.c b/src/driver/amgc_unit_action.c
index 29bc446..6189481 100644
--- a/src/driver/amgc_unit_action.c
+++ b/src/driver/amgc_unit_action.c
@@ -11,34 +11,34 @@
#include "apimagic_driver_impl.h"
int amgc_perform_unit_action(
+ const struct amgc_driver_ctx * dctx,
const struct amgc_unit_ctx * uctx,
const struct amgc_action * action,
- const struct amgc_layout * layout,
- FILE * fout)
+ const struct amgc_layout * layout)
{
switch (action->action) {
case AMGC_OUTPUT_ENUM:
if (action->symbol)
return -1;
else
- return amgc_output_unit_enums(uctx,0,stdout);
+ return amgc_output_unit_enums(dctx,uctx,0);
case AMGC_LIST_ENUM:
if (action->symbol)
return -1;
else
- return amgc_list_unit_enums(uctx,0,stdout);
+ return amgc_list_unit_enums(dctx,uctx,0);
case AMGC_OUTPUT_TYPEDEF:
return amgc_output_unit_entities(
- uctx,ENTITY_TYPEDEF,action->subset,
- layout,fout);
+ dctx,uctx,ENTITY_TYPEDEF,
+ action->subset,layout);
case AMGC_OUTPUT_STRUCT:
- return amgc_output_unit_structs(uctx,layout,fout);
+ return amgc_output_unit_structs(dctx,uctx,layout);
case AMGC_OUTPUT_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_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;
}