From 06a2c5a19bf1030919bda572f1ac583c535bf421 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Tue, 30 Jan 2018 16:54:13 -0800 Subject: [PATCH] accessor: Move accessor generators to one file Signed-off-by: Bjorn Andersson --- Makefile | 2 +- qmi_message.c => accessor.c | 137 ++++++++++++++++++++++++++++++++++-- qmi_struct.c | 101 -------------------------- qmic.c | 57 +++++++-------- qmic.h | 17 +++-- 5 files changed, 172 insertions(+), 142 deletions(-) rename qmi_message.c => accessor.c (61%) delete mode 100644 qmi_struct.c diff --git a/Makefile b/Makefile index 4211663..2713e1c 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS := -Wall -g -O2 LDFLAGS := prefix := /usr/local -SRCS := parser.c qmic.c qmi_message.c qmi_struct.c +SRCS := accessor.c parser.c qmic.c OBJS := $(SRCS:.c=.o) $(OUT): $(OBJS) diff --git a/qmi_message.c b/accessor.c similarity index 61% rename from qmi_message.c rename to accessor.c index edcb50f..d9436d8 100644 --- a/qmi_message.c +++ b/accessor.c @@ -1,11 +1,104 @@ #include #include -#include -#include -#include "list.h" #include "qmic.h" +static void qmi_struct_header(FILE *fp, const char *package) +{ + struct qmi_struct_member *qsm; + struct qmi_struct *qs; + + list_for_each_entry(qs, &qmi_structs, node) { + fprintf(fp, "struct %s_%s {\n", + package, qs->name); + list_for_each_entry(qsm, &qs->members, node) { + fprintf(fp, "\t%s %s;\n", + sz_simple_types[qsm->type], qsm->name); + } + fprintf(fp, "};\n" + "\n"); + } +} + +static void qmi_struct_emit_prototype(FILE *fp, + const char *package, + const char *message, + const char *member, + unsigned array_size, + struct qmi_struct *qs) +{ + if (array_size) { + fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count);\n", + package, message, member, qs->name); + + fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count);\n\n", + package, message, member, qs->name); + } else { + fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val);\n", + package, message, member, qs->name); + + fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s);\n\n", + package, message, member, qs->name); + } +} + +static void qmi_struct_emit_accessors(FILE *fp, + const char *package, + const char *message, + const char *member, + int member_id, + unsigned array_size, + struct qmi_struct *qs) +{ + if (array_size) { + fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count)\n" + "{\n" + " return qmi_tlv_set_array((struct qmi_tlv*)%2$s, %5$d, %6$d, val, count, sizeof(struct %1$s_%4$s));\n" + "}\n\n", + package, message, member, qs->name, member_id, array_size); + + fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count)\n" + "{\n" + " size_t size;\n" + " size_t len;\n" + " void *ptr;\n" + "\n" + " ptr = qmi_tlv_get_array((struct qmi_tlv*)%2$s, %5$d, %6$d, &len, &size);\n" + " if (!ptr)\n" + " return NULL;\n" + "\n" + " if (size != sizeof(struct %1$s_%4$s))\n" + " return NULL;\n" + "\n" + " *count = len;\n" + " return ptr;\n" + "}\n\n", + package, message, member, qs->name, member_id, array_size); + } else { + fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val)\n" + "{\n" + " return qmi_tlv_set((struct qmi_tlv*)%2$s, %5$d, val, sizeof(struct %1$s_%4$s));\n" + "}\n\n", + package, message, member, qs->name, member_id); + + fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s)\n" + "{\n" + " size_t len;\n" + " void *ptr;\n" + "\n" + " ptr = qmi_tlv_get((struct qmi_tlv*)%2$s, %5$d, &len);\n" + " if (!ptr)\n" + " return NULL;\n" + "\n" + " if (len != sizeof(struct %1$s_%4$s))\n" + " return NULL;\n" + "\n" + " return ptr;\n" + "}\n\n", + package, message, member, qs->name, member_id); + } +} + static void qmi_message_emit_message_type(FILE *fp, const char *package, const char *message) @@ -188,7 +281,7 @@ static void qmi_message_emit_string_accessors(FILE *fp, } -void qmi_message_source(FILE *fp, const char *package) +static void qmi_message_source(FILE *fp, const char *package) { struct qmi_message_member *qmm; struct qmi_message *qm; @@ -215,7 +308,7 @@ void qmi_message_source(FILE *fp, const char *package) } } -void qmi_message_header(FILE *fp, const char *package) +static void qmi_message_header(FILE *fp, const char *package) { struct qmi_message_member *qmm; struct qmi_message *qm; @@ -246,3 +339,37 @@ void qmi_message_header(FILE *fp, const char *package) } } } + +static void emit_header_file_header(FILE *fp) +{ + fprintf(fp, "#include \n" + "#include \n\n"); + fprintf(fp, "struct qmi_tlv;\n" + "\n" + "struct qmi_tlv *qmi_tlv_init(unsigned txn, unsigned msg_id, unsigned type);\n" + "struct qmi_tlv *qmi_tlv_decode(void *buf, size_t len, unsigned *txn, unsigned type);\n" + "void *qmi_tlv_encode(struct qmi_tlv *tlv, size_t *len);\n" + "void qmi_tlv_free(struct qmi_tlv *tlv);\n" + "\n" + "void *qmi_tlv_get(struct qmi_tlv *tlv, unsigned id, size_t *len);\n" + "void *qmi_tlv_get_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, size_t *len, size_t *size);\n" + "int qmi_tlv_set(struct qmi_tlv *tlv, unsigned id, void *buf, size_t len);\n" + "int qmi_tlv_set_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, void *buf, size_t len, size_t size);\n" + "\n"); +} + +void accessor_emit_c(FILE *fp, const char *package) +{ + emit_source_includes(fp, package); + qmi_message_source(fp, package); +} + +void accessor_emit_h(FILE *fp, const char *package) +{ + guard_header(fp, qmi_package); + emit_header_file_header(fp); + qmi_const_header(fp); + qmi_struct_header(fp, qmi_package); + qmi_message_header(fp, qmi_package); + guard_footer(fp); +} diff --git a/qmi_struct.c b/qmi_struct.c deleted file mode 100644 index de3b58f..0000000 --- a/qmi_struct.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include - -#include "list.h" -#include "qmic.h" - -void qmi_struct_header(FILE *fp, const char *package) -{ - struct qmi_struct_member *qsm; - struct qmi_struct *qs; - - list_for_each_entry(qs, &qmi_structs, node) { - fprintf(fp, "struct %s_%s {\n", - package, qs->name); - list_for_each_entry(qsm, &qs->members, node) { - fprintf(fp, "\t%s %s;\n", - sz_simple_types[qsm->type], qsm->name); - } - fprintf(fp, "};\n" - "\n"); - } -} - -void qmi_struct_emit_prototype(FILE *fp, - const char *package, - const char *message, - const char *member, - unsigned array_size, - struct qmi_struct *qs) -{ - if (array_size) { - fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count);\n", - package, message, member, qs->name); - - fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count);\n\n", - package, message, member, qs->name); - } else { - fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val);\n", - package, message, member, qs->name); - - fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s);\n\n", - package, message, member, qs->name); - } -} - -void qmi_struct_emit_accessors(FILE *fp, - const char *package, - const char *message, - const char *member, - int member_id, - unsigned array_size, - struct qmi_struct *qs) -{ - if (array_size) { - fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val, size_t count)\n" - "{\n" - " return qmi_tlv_set_array((struct qmi_tlv*)%2$s, %5$d, %6$d, val, count, sizeof(struct %1$s_%4$s));\n" - "}\n\n", - package, message, member, qs->name, member_id, array_size); - - fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s, size_t *count)\n" - "{\n" - " size_t size;\n" - " size_t len;\n" - " void *ptr;\n" - "\n" - " ptr = qmi_tlv_get_array((struct qmi_tlv*)%2$s, %5$d, %6$d, &len, &size);\n" - " if (!ptr)\n" - " return NULL;\n" - "\n" - " if (size != sizeof(struct %1$s_%4$s))\n" - " return NULL;\n" - "\n" - " *count = len;\n" - " return ptr;\n" - "}\n\n", - package, message, member, qs->name, member_id, array_size); - } else { - fprintf(fp, "int %1$s_%2$s_set_%3$s(struct %1$s_%2$s *%2$s, struct %1$s_%4$s *val)\n" - "{\n" - " return qmi_tlv_set((struct qmi_tlv*)%2$s, %5$d, val, sizeof(struct %1$s_%4$s));\n" - "}\n\n", - package, message, member, qs->name, member_id); - - fprintf(fp, "struct %1$s_%4$s *%1$s_%2$s_get_%3$s(struct %1$s_%2$s *%2$s)\n" - "{\n" - " size_t len;\n" - " void *ptr;\n" - "\n" - " ptr = qmi_tlv_get((struct qmi_tlv*)%2$s, %5$d, &len);\n" - " if (!ptr)\n" - " return NULL;\n" - "\n" - " if (len != sizeof(struct %1$s_%4$s))\n" - " return NULL;\n" - "\n" - " return ptr;\n" - "}\n\n", - package, message, member, qs->name, member_id); - } -} diff --git a/qmic.c b/qmic.c index b8b9faa..bcbc22f 100644 --- a/qmic.c +++ b/qmic.c @@ -20,7 +20,7 @@ const char *sz_simple_types[] = { [TYPE_STRING] = "char *", }; -static void qmi_const_header(FILE *fp) +void qmi_const_header(FILE *fp) { struct qmi_const *qc; @@ -33,7 +33,7 @@ static void qmi_const_header(FILE *fp) fprintf(fp, "\n"); } -static void emit_source_header(FILE *fp, const char *package) +void emit_source_includes(FILE *fp, const char *package) { fprintf(fp, "#include \n" "#include \n" @@ -41,7 +41,7 @@ static void emit_source_header(FILE *fp, const char *package) package); } -static void guard_header(FILE *fp, const char *package) +void guard_header(FILE *fp, const char *package) { char *upper; char *p; @@ -55,32 +55,41 @@ static void guard_header(FILE *fp, const char *package) fprintf(fp, "#ifndef __QMI_%s_H__\n", upper); fprintf(fp, "#define __QMI_%s_H__\n", upper); fprintf(fp, "\n"); - fprintf(fp, "#include \n" - "#include \n\n"); - fprintf(fp, "struct qmi_tlv;\n" - "\n" - "struct qmi_tlv *qmi_tlv_init(unsigned txn, unsigned msg_id, unsigned type);\n" - "struct qmi_tlv *qmi_tlv_decode(void *buf, size_t len, unsigned *txn, unsigned type);\n" - "void *qmi_tlv_encode(struct qmi_tlv *tlv, size_t *len);\n" - "void qmi_tlv_free(struct qmi_tlv *tlv);\n" - "\n" - "void *qmi_tlv_get(struct qmi_tlv *tlv, unsigned id, size_t *len);\n" - "void *qmi_tlv_get_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, size_t *len, size_t *size);\n" - "int qmi_tlv_set(struct qmi_tlv *tlv, unsigned id, void *buf, size_t len);\n" - "int qmi_tlv_set_array(struct qmi_tlv *tlv, unsigned id, unsigned len_size, void *buf, size_t len, size_t size);\n" - "\n"); } -static void guard_footer(FILE *fp) +void guard_footer(FILE *fp) { fprintf(fp, "#endif\n"); } +static void usage(void) +{ + extern const char *__progname; + + fprintf(stderr, "Usage: %s -a\n", __progname); + exit(1); +} + int main(int argc, char **argv) { char fname[256]; FILE *hfp; FILE *sfp; + int method = 0; + int opt; + + while ((opt = getopt(argc, argv, "a")) != -1) { + switch (opt) { + case 'a': + method = 1; + break; + default: + usage(); + } + } + + if (!method) + usage(); qmi_parse(); @@ -94,16 +103,8 @@ int main(int argc, char **argv) if (!hfp) err(1, "failed to open %s", fname); - /* Source output */ - emit_source_header(sfp, qmi_package); - qmi_message_source(sfp, qmi_package); - - /* Header output */ - guard_header(hfp, qmi_package); - qmi_const_header(hfp); - qmi_struct_header(hfp, qmi_package); - qmi_message_header(hfp, qmi_package); - guard_footer(hfp); + accessor_emit_c(sfp, qmi_package); + accessor_emit_h(hfp, qmi_package); fclose(hfp); fclose(sfp); diff --git a/qmic.h b/qmic.h index fed369f..8b7c991 100644 --- a/qmic.h +++ b/qmic.h @@ -3,6 +3,8 @@ #include +#include "list.h" + #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) enum { @@ -71,13 +73,14 @@ extern struct list_head qmi_consts; extern struct list_head qmi_messages; extern struct list_head qmi_structs; -void qmi_message_source(FILE *fp, const char *package); -void qmi_message_header(FILE *fp, const char *package); - -void qmi_struct_header(FILE *fp, const char *package); -void qmi_struct_emit_prototype(FILE *fp, const char *package, const char *message, const char *member, unsigned array_size, struct qmi_struct *qs); -void qmi_struct_emit_accessors(FILE *fp, const char *package, const char *message, const char *member, int member_id, unsigned array_size, struct qmi_struct *qs); - void qmi_parse(void); +void emit_source_includes(FILE *fp, const char *package); +void guard_header(FILE *fp, const char *package); +void guard_footer(FILE *fp); +void qmi_const_header(FILE *fp); + +void accessor_emit_c(FILE *fp, const char *package); +void accessor_emit_h(FILE *fp, const char *package); + #endif