fmt glue.c

This commit is contained in:
Alex Orlenko 2019-09-30 22:34:47 +01:00
parent 11e22d1cba
commit 2e5762f6e5
1 changed files with 129 additions and 107 deletions

View File

@ -20,13 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <lua.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <lua.h>
#include <lualib.h> #include <lualib.h>
// Macros taken from https://gcc.gnu.org/onlinedocs/cpp/Stringification.html // Macros taken from https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
@ -35,68 +35,83 @@
typedef struct rs_item { typedef struct rs_item {
int type; int type;
const char* name; const char *name;
union { union {
int int_val; int int_val;
const char* str_val; const char *str_val;
LUA_INTEGER lua_int_val; LUA_INTEGER lua_int_val;
}; };
} rs_item; } rs_item;
#define TY_INT 0 #define TY_INT 0
#define TY_LUAINT 1 #define TY_LUAINT 1
#define TY_STR 2 #define TY_STR 2
#define TY_TYPE 3 #define TY_TYPE 3
#define TY_COMMENT 4 #define TY_COMMENT 4
#define TY_RAW 5 #define TY_RAW 5
#define RS_INT(name, val) {TY_INT, name, .int_val=val} #define RS_INT(name, val) \
#define RS_LUAINT(name, val) {TY_LUAINT, name, .lua_int_val=val} { TY_INT, name, .int_val = val }
#define RS_STR(name, val) {TY_STR, name, .str_val=val} #define RS_LUAINT(name, val) \
#define RS_TYPE(name, val) {TY_TYPE, name, .str_val=val} { TY_LUAINT, name, .lua_int_val = val }
#define RS_COMMENT(val) {TY_COMMENT, NULL, .str_val=val} #define RS_STR(name, val) \
#define RS_RAW(val) {TY_RAW, NULL, .str_val=val} { TY_STR, name, .str_val = val }
#define RS_TYPE(name, val) \
{ TY_TYPE, name, .str_val = val }
#define RS_COMMENT(val) \
{ TY_COMMENT, NULL, .str_val = val }
#define RS_RAW(val) \
{ TY_RAW, NULL, .str_val = val }
const char* rs_int_type(int width) { const char *rs_int_type(int width) {
switch (width) { switch (width) {
default: default:
case 2: return "i16"; case 2:
case 4: return "i32"; return "i16";
case 8: return "i64"; case 4:
return "i32";
case 8:
return "i64";
} }
} }
const char* rs_uint_type(int width) { const char *rs_uint_type(int width) {
switch (width) { switch (width) {
default: default:
case 2: return "u16"; case 2:
case 4: return "u32"; return "u16";
case 8: return "u64"; case 4:
return "u32";
case 8:
return "u64";
} }
} }
int try_write(char** str, char c, size_t n, size_t* written, size_t szstr) { int try_write(char **str, char c, size_t n, size_t *written, size_t szstr) {
if (szstr - *written < n) { if (szstr - *written < n) {
return 0; return 0;
} }
for (; n; n--, *written++) *(*str)++ = c; for (; n; n--, *written++)
*(*str)++ = c;
return 1; return 1;
} }
// converts \ in a string to \\ so that it can be used as a rust string literal // converts \ in a string to \\ so that it can be used as a rust string literal
// ensures that `out` will always have a null terminating character // ensures that `out` will always have a null terminating character
size_t escape(const char* in, char* out, size_t szout) { size_t escape(const char *in, char *out, size_t szout) {
size_t written = 0; size_t written = 0;
char cur; char cur;
while (cur = *in++) { while (cur = *in++) {
switch (cur) { switch (cur) {
case '\\': case '\\':
if (!try_write(&out, cur, 2, &written, szout)) goto finalize; if (!try_write(&out, cur, 2, &written, szout))
break; goto finalize;
default: break;
if (!try_write(&out, cur, 1, &written, szout)) goto finalize; default:
break; if (!try_write(&out, cur, 1, &written, szout))
goto finalize;
break;
} }
} }
@ -108,18 +123,19 @@ finalize:
return written; return written;
} }
int write_int_item(FILE* f, const char* name, int value) { int write_int_item(FILE *f, const char *name, int value) {
return fprintf(f, "pub const %s: c_int = %d;\n", name, value); return fprintf(f, "pub const %s: c_int = %d;\n", name, value);
} }
int write_lua_int_item(FILE* f, const char* name, LUA_INTEGER value) { int write_lua_int_item(FILE *f, const char *name, LUA_INTEGER value) {
return fprintf(f, "pub const %s: LUA_INTEGER = "LUA_INTEGER_FMT";\n", name, value); return fprintf(f, "pub const %s: LUA_INTEGER = " LUA_INTEGER_FMT ";\n", name,
value);
} }
int write_str_item(FILE* f, const char* name, const char* value) { int write_str_item(FILE *f, const char *name, const char *value) {
size_t len = strlen(value); size_t len = strlen(value);
size_t bufsz = len * 2 + 1; size_t bufsz = len * 2 + 1;
char* buf = malloc(bufsz); char *buf = malloc(bufsz);
int ret; int ret;
escape(value, buf, bufsz); escape(value, buf, bufsz);
ret = fprintf(f, "pub const %s: &'static str = \"%s\";\n", name, buf); ret = fprintf(f, "pub const %s: &'static str = \"%s\";\n", name, buf);
@ -127,49 +143,55 @@ int write_str_item(FILE* f, const char* name, const char* value) {
return ret; return ret;
} }
int write_type(FILE* f, const char* name, const char* value) { int write_type(FILE *f, const char *name, const char *value) {
return fprintf(f, "pub type %s = %s;\n", name, value); return fprintf(f, "pub type %s = %s;\n", name, value);
} }
int write_comment(FILE* f, const char* value) { int write_comment(FILE *f, const char *value) {
return fprintf(f, "/* %s */\n", value); return fprintf(f, "/* %s */\n", value);
} }
int write_raw(FILE* f, const char* value) { int write_raw(FILE *f, const char *value) { return fputs(value, f) >= 0; }
return fputs(value, f) >= 0;
}
int write_item(FILE* f, const rs_item* c) { int write_item(FILE *f, const rs_item *c) {
switch (c->type) { switch (c->type) {
case TY_INT: return write_int_item(f, c->name, c->int_val); case TY_INT:
case TY_LUAINT: return write_lua_int_item(f, c->name, c->lua_int_val); return write_int_item(f, c->name, c->int_val);
case TY_STR: return write_str_item(f, c->name, c->str_val); case TY_LUAINT:
case TY_TYPE: return write_type(f, c->name, c->str_val); return write_lua_int_item(f, c->name, c->lua_int_val);
case TY_COMMENT: return write_comment(f, c->str_val); case TY_STR:
case TY_RAW: return write_raw(f, c->str_val); return write_str_item(f, c->name, c->str_val);
default: return 0; case TY_TYPE:
return write_type(f, c->name, c->str_val);
case TY_COMMENT:
return write_comment(f, c->str_val);
case TY_RAW:
return write_raw(f, c->str_val);
default:
return 0;
} }
} }
int write_items_(FILE* f, const rs_item items[], size_t num) { int write_items_(FILE *f, const rs_item items[], size_t num) {
size_t i; size_t i;
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (!write_item(f, &items[i])) return 0; if (!write_item(f, &items[i]))
return 0;
} }
return 1; return 1;
} }
#define write_items(f, cs) write_items_(f, cs, sizeof(cs)/sizeof(cs[0])) #define write_items(f, cs) write_items_(f, cs, sizeof(cs) / sizeof(cs[0]))
int main(int argc, const char** argv) { int main(int argc, const char **argv) {
if (argc <= 1) { if (argc <= 1) {
printf("usage: %s <filename>\n", argv[0]); printf("usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
const char* filename = argv[1]; const char *filename = argv[1];
FILE* f = fopen(filename, "w"); FILE *f = fopen(filename, "w");
if (!f) { if (!f) {
printf("could not open file: errno = %d\n", errno); printf("could not open file: errno = %d\n", errno);
@ -177,63 +199,63 @@ int main(int argc, const char** argv) {
} }
const rs_item glue_entries[] = { const rs_item glue_entries[] = {
RS_COMMENT("this file was generated by glue.c; do not modify it by hand"), RS_COMMENT("this file was generated by glue.c; do not modify it by hand"),
RS_RAW("use libc::*;\n"), RS_RAW("use libc::*;\n"),
// == luaconf.h =========================================================== // == luaconf.h ==========================================================
RS_COMMENT("luaconf.h"), RS_COMMENT("luaconf.h"),
RS_STR("LUA_VDIR", LUA_VDIR), RS_STR("LUA_VDIR", LUA_VDIR),
RS_STR("LUA_PATH_DEFAULT", LUA_PATH_DEFAULT), RS_STR("LUA_PATH_DEFAULT", LUA_PATH_DEFAULT),
RS_STR("LUA_CPATH_DEFAULT", LUA_CPATH_DEFAULT), RS_STR("LUA_CPATH_DEFAULT", LUA_CPATH_DEFAULT),
RS_STR("LUA_DIRSEP", LUA_DIRSEP), RS_STR("LUA_DIRSEP", LUA_DIRSEP),
RS_INT("LUA_EXTRASPACE", LUA_EXTRASPACE), RS_INT("LUA_EXTRASPACE", LUA_EXTRASPACE),
RS_INT("LUA_IDSIZE", LUA_IDSIZE), RS_INT("LUA_IDSIZE", LUA_IDSIZE),
// RS_INT("LUAI_MAXSHORTLEN", LUAI_MAXSHORTLEN), // RS_INT("LUAI_MAXSHORTLEN", LUAI_MAXSHORTLEN),
RS_TYPE("LUA_KCONTEXT", xstr(LUA_KCONTEXT)), RS_TYPE("LUA_KCONTEXT", xstr(LUA_KCONTEXT)),
RS_INT("LUAI_BITSINT", LUAI_BITSINT), RS_INT("LUAI_BITSINT", LUAI_BITSINT),
// LUA_INT32? LUAI_UMEM? LUAI_MEM? // LUA_INT32? LUAI_UMEM? LUAI_MEM?
RS_INT("LUAI_MAXSTACK", LUAI_MAXSTACK), RS_INT("LUAI_MAXSTACK", LUAI_MAXSTACK),
RS_INT("LUAL_BUFFERSIZE", LUAL_BUFFERSIZE), RS_INT("LUAL_BUFFERSIZE", LUAL_BUFFERSIZE),
RS_TYPE("LUA_NUMBER", RS_TYPE("LUA_NUMBER",
sizeof(LUA_NUMBER) > sizeof(float) ? "c_double" : "c_float"), sizeof(LUA_NUMBER) > sizeof(float) ? "c_double" : "c_float"),
RS_TYPE("LUA_UNSIGNED", rs_uint_type(sizeof(LUA_UNSIGNED))), RS_TYPE("LUA_UNSIGNED", rs_uint_type(sizeof(LUA_UNSIGNED))),
RS_TYPE("LUA_INTEGER", rs_int_type(sizeof(LUA_INTEGER))), RS_TYPE("LUA_INTEGER", rs_int_type(sizeof(LUA_INTEGER))),
RS_LUAINT("LUA_MAXINTEGER", LUA_MAXINTEGER), RS_LUAINT("LUA_MAXINTEGER", LUA_MAXINTEGER),
RS_LUAINT("LUA_MININTEGER", LUA_MININTEGER), RS_LUAINT("LUA_MININTEGER", LUA_MININTEGER),
// == lua.h =============================================================== // == lua.h ==============================================================
RS_COMMENT("lua.h"), RS_COMMENT("lua.h"),
RS_STR("LUA_VERSION_MAJOR", LUA_VERSION_MAJOR), RS_STR("LUA_VERSION_MAJOR", LUA_VERSION_MAJOR),
RS_STR("LUA_VERSION_MINOR", LUA_VERSION_MINOR), RS_STR("LUA_VERSION_MINOR", LUA_VERSION_MINOR),
RS_INT("LUA_VERSION_NUM", LUA_VERSION_NUM), RS_INT("LUA_VERSION_NUM", LUA_VERSION_NUM),
RS_STR("LUA_VERSION_RELEASE", LUA_VERSION_RELEASE), RS_STR("LUA_VERSION_RELEASE", LUA_VERSION_RELEASE),
RS_STR("LUA_VERSION", LUA_VERSION), RS_STR("LUA_VERSION", LUA_VERSION),
RS_STR("LUA_RELEASE", LUA_RELEASE), RS_STR("LUA_RELEASE", LUA_RELEASE),
RS_STR("LUA_COPYRIGHT", LUA_COPYRIGHT), RS_STR("LUA_COPYRIGHT", LUA_COPYRIGHT),
RS_STR("LUA_AUTHORS", LUA_AUTHORS), RS_STR("LUA_AUTHORS", LUA_AUTHORS),
RS_INT("LUA_REGISTRYINDEX", LUA_REGISTRYINDEX), RS_INT("LUA_REGISTRYINDEX", LUA_REGISTRYINDEX),
// == lauxlib.h =========================================================== // == lauxlib.h ==========================================================
RS_COMMENT("lauxlib.h"), RS_COMMENT("lauxlib.h"),
RS_INT("LUAL_NUMSIZES", LUAL_NUMSIZES), RS_INT("LUAL_NUMSIZES", LUAL_NUMSIZES),
RS_STR("LUA_FILEHANDLE", LUA_FILEHANDLE), RS_STR("LUA_FILEHANDLE", LUA_FILEHANDLE),
// == lualib.h ============================================================ // == lualib.h ===========================================================
RS_COMMENT("lualib.h"), RS_COMMENT("lualib.h"),
RS_STR("LUA_COLIBNAME", LUA_COLIBNAME), RS_STR("LUA_COLIBNAME", LUA_COLIBNAME),
RS_STR("LUA_TABLIBNAME", LUA_TABLIBNAME), RS_STR("LUA_TABLIBNAME", LUA_TABLIBNAME),
RS_STR("LUA_IOLIBNAME", LUA_IOLIBNAME), RS_STR("LUA_IOLIBNAME", LUA_IOLIBNAME),
RS_STR("LUA_OSLIBNAME", LUA_OSLIBNAME), RS_STR("LUA_OSLIBNAME", LUA_OSLIBNAME),
RS_STR("LUA_STRLIBNAME", LUA_STRLIBNAME), RS_STR("LUA_STRLIBNAME", LUA_STRLIBNAME),
RS_STR("LUA_UTF8LIBNAME", LUA_UTF8LIBNAME), RS_STR("LUA_UTF8LIBNAME", LUA_UTF8LIBNAME),
RS_STR("LUA_BITLIBNAME", LUA_BITLIBNAME), RS_STR("LUA_BITLIBNAME", LUA_BITLIBNAME),
RS_STR("LUA_MATHLIBNAME", LUA_MATHLIBNAME), RS_STR("LUA_MATHLIBNAME", LUA_MATHLIBNAME),
RS_STR("LUA_DBLIBNAME", LUA_DBLIBNAME), RS_STR("LUA_DBLIBNAME", LUA_DBLIBNAME),
RS_STR("LUA_LOADLIBNAME", LUA_LOADLIBNAME), RS_STR("LUA_LOADLIBNAME", LUA_LOADLIBNAME),
}; };
if (!write_items(f, glue_entries)) { if (!write_items(f, glue_entries)) {