From 724bca0eeca05a68900e3054e118904b6618dc7c Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Tue, 13 Jan 2015 21:25:17 -0500 Subject: [PATCH] Remove fs.c ansi.c and PORTING guide --- Makefile | 6 ++--- PORTING | 4 --- ansi.c | 57 ---------------------------------------- fs.c | 67 ----------------------------------------------- gmqcc.h | 27 ++++++++----------- main.c | 2 +- opts.c | 2 +- platform.h | 37 -------------------------- test.c | 8 +++--- util.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 10 files changed, 95 insertions(+), 192 deletions(-) delete mode 100644 PORTING delete mode 100644 ansi.c delete mode 100644 fs.c delete mode 100644 platform.h diff --git a/Makefile b/Makefile index 9b02469..fdb173e 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ CC ?= clang CFLAGS = -MD -std=gnu99 -Wall -Wextra -pedantic-errors -g3 LDFLAGS = -lm -CSRCS = ansi.c ast.c code.c conout.c fold.c fs.c ftepp.c hash.c intrin.c ir.c lexer.c main.c opts.c parser.c stat.c utf8.c util.c -TSRCS = ansi.c conout.c fs.c hash.c opts.c stat.c test.c util.c -VSRCS = ansi.c exec.c fs.c hash.c stat.c util.c +CSRCS = ast.c code.c conout.c fold.c ftepp.c hash.c intrin.c ir.c lexer.c main.c opts.c parser.c stat.c utf8.c util.c +TSRCS = conout.c hash.c opts.c stat.c test.c util.c +VSRCS = exec.c hash.c stat.c util.c COBJS = $(CSRCS:.c=.o) TOBJS = $(TSRCS:.c=.o) diff --git a/PORTING b/PORTING deleted file mode 100644 index bbdb274..0000000 --- a/PORTING +++ /dev/null @@ -1,4 +0,0 @@ -Porting gmqcc to a new platform is farily trivial, in most cases ansi.c -will be sufficent enough to get it to run on your favorite platform. If -however it isn't you can duplicate ansi.c and change it accordingly. -Changes to platform.h may also be required. diff --git a/ansi.c b/ansi.c deleted file mode 100644 index 02db1b9..0000000 --- a/ansi.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2012, 2013, 2014, 2015 - * Dale Weiler - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include -#include - -#include "platform.h" -#include "gmqcc.h" - -int platform_vasprintf(char **dat, const char *fmt, va_list args) { - int ret; - int len; - char *tmp = NULL; - char buf[128]; - va_list cpy; - - va_copy(cpy, args); - len = vsnprintf(buf, sizeof(buf), fmt, cpy); - va_end (cpy); - - if (len < 0) - return len; - - if (len < (int)sizeof(buf)) { - *dat = util_strdup(buf); - return len; - } - - tmp = (char*)mem_a(len + 1); - if ((ret = vsnprintf(tmp, len + 1, fmt, args)) != len) { - mem_d(tmp); - *dat = NULL; - return -1; - } - - *dat = tmp; - return len; -} diff --git a/fs.c b/fs.c deleted file mode 100644 index 4c7a178..0000000 --- a/fs.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2012, 2013, 2014, 2015 - * Dale Weiler - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "gmqcc.h" - -int fs_file_getline(char **lineptr, size_t *n, FILE *stream) { - int chr; - int ret; - char *pos; - - if (!lineptr || !n || !stream) - return -1; - if (!*lineptr) { - if (!(*lineptr = (char*)mem_a((*n=64)))) - return -1; - } - - chr = *n; - pos = *lineptr; - - for (;;) { - int c = getc(stream); - - if (chr < 2) { - *n += (*n > 16) ? *n : 64; - chr = *n + *lineptr - pos; - if (!(*lineptr = (char*)mem_r(*lineptr,*n))) - return -1; - pos = *n - chr + *lineptr; - } - - if (ferror(stream)) - return -1; - if (c == EOF) { - if (pos == *lineptr) - return -1; - else - break; - } - - *pos++ = c; - chr--; - if (c == '\n') - break; - } - *pos = '\0'; - return (ret = pos - *lineptr); -} diff --git a/gmqcc.h b/gmqcc.h index 235059d..1bdce34 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -310,22 +310,17 @@ typedef struct hash_table_s { * util_htdel(foo); */ hash_table_t *util_htnew (size_t size); -void util_htrem (hash_table_t *ht, void (*callback)(void *data)); -void util_htset (hash_table_t *ht, const char *key, void *value); -void util_htdel (hash_table_t *ht); -size_t util_hthash(hash_table_t *ht, const char *key); -void util_htseth(hash_table_t *ht, const char *key, size_t hash, void *value); -void util_htrmh (hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)); -void util_htrm (hash_table_t *ht, const char *key, void (*cb)(void*)); - -void *util_htget (hash_table_t *ht, const char *key); -void *util_htgeth(hash_table_t *ht, const char *key, size_t hash); - -int util_snprintf(char *str, size_t, const char *fmt, ...); - - -/* fs.c */ -int fs_file_getline(char **, size_t *, FILE *); +void util_htrem(hash_table_t *ht, void (*callback)(void *data)); +void util_htset(hash_table_t *ht, const char *key, void *value); +void util_htdel(hash_table_t *ht); +size_t util_hthash(hash_table_t *ht, const char *key); +void util_htseth(hash_table_t *ht, const char *key, size_t hash, void *value); +void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)); +void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)); +void *util_htget(hash_table_t *ht, const char *key); +void *util_htgeth(hash_table_t *ht, const char *key, size_t hash); +int util_snprintf(char *str, size_t, const char *fmt, ...); +int util_getline(char **, size_t *, FILE *); /* code.c */ diff --git a/main.c b/main.c index 9f64baf..3f6f6ef 100644 --- a/main.c +++ b/main.c @@ -531,7 +531,7 @@ static bool progs_nextline(char **out, size_t *alen, FILE *src) { char *end; line = *out; - len = fs_file_getline(&line, alen, src); + len = util_getline(&line, alen, src); if (len == -1) return false; diff --git a/opts.c b/opts.c index 116792f..272d3f7 100644 --- a/opts.c +++ b/opts.c @@ -234,7 +234,7 @@ static size_t opts_ini_parse ( char *read_name; char *read_value; - while (fs_file_getline(&line, &linesize, filehandle) != EOF) { + while (util_getline(&line, &linesize, filehandle) != EOF) { parse_beg = line; /* handle BOM */ diff --git a/platform.h b/platform.h deleted file mode 100644 index f4bd9ef..0000000 --- a/platform.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2012, 2013, 2014, 2015 - * Dale Weiler - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef GMQCC_PLATFORM_HDR -#define GMQCC_PLATFORM_HDR - -#include -#include -#include - -#include -#include -#include - -int platform_vasprintf(char **dat, const char *fmt, va_list args); - -#endif diff --git a/test.c b/test.c index f15d1fb..5e41be3 100644 --- a/test.c +++ b/test.c @@ -309,7 +309,7 @@ static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *f return false; /* top down parsing */ - while (fs_file_getline(&back, &size, fp) != EOF) { + while (util_getline(&back, &size, fp) != EOF) { /* skip whitespace */ data = back; if (*data && (*data == ' ' || *data == '\t')) @@ -932,7 +932,7 @@ static bool task_trymatch(size_t i, char ***line) { size_t size = 0; size_t compare = 0; - while (fs_file_getline(&data, &size, execute) != EOF) { + while (util_getline(&data, &size, execute) != EOF) { if (!strcmp(data, "No main function found\n")) { con_err("test failure: `%s` (No main function found) [%s]\n", tmpl->description, @@ -1066,7 +1066,7 @@ static size_t task_schedualize(size_t *pad) { * Read data from stdout first and pipe that stuff into a log file * then we do the same for stderr. */ - while (fs_file_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) { + while (util_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) { fputs(data, task_tasks[i].stdoutlog); if (strstr(data, "failed to open file")) { @@ -1074,7 +1074,7 @@ static size_t task_schedualize(size_t *pad) { execute = false; } } - while (fs_file_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) { + while (util_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) { /* * If a string contains an error we just dissalow execution * of it in the vm. diff --git a/util.c b/util.c index 0e468c0..10fd9f4 100644 --- a/util.c +++ b/util.c @@ -24,7 +24,6 @@ #include #include #include "gmqcc.h" -#include "platform.h" /* * Initially this was handled with a table in the gmqcc.h header, but @@ -584,6 +583,36 @@ size_t util_optimizationtostr(const char *in, char *out, size_t outsz) { return util_strtransform(in, out, outsz, "_ ", 'a'-'A'); } +static int util_vasprintf(char **dat, const char *fmt, va_list args) { + int ret; + int len; + char *tmp = NULL; + char buf[128]; + va_list cpy; + + va_copy(cpy, args); + len = vsnprintf(buf, sizeof(buf), fmt, cpy); + va_end (cpy); + + if (len < 0) + return len; + + if (len < (int)sizeof(buf)) { + *dat = util_strdup(buf); + return len; + } + + tmp = (char*)mem_a(len + 1); + if ((ret = vsnprintf(tmp, len + 1, fmt, args)) != len) { + mem_d(tmp); + *dat = NULL; + return -1; + } + + *dat = tmp; + return len; +} + int util_snprintf(char *str, size_t size, const char *fmt, ...) { va_list arg; int ret; @@ -597,7 +626,7 @@ int util_asprintf(char **ret, const char *fmt, ...) { va_list args; int read; va_start(args, fmt); - read = platform_vasprintf(ret, fmt, args); + read = util_vasprintf(ret, fmt, args); va_end(args); return read; } @@ -635,6 +664,50 @@ const char *util_ctime(const time_t *timer) { return ctime(timer); } +int util_getline(char **lineptr, size_t *n, FILE *stream) { + int chr; + int ret; + char *pos; + + if (!lineptr || !n || !stream) + return -1; + if (!*lineptr) { + if (!(*lineptr = (char*)mem_a((*n=64)))) + return -1; + } + + chr = *n; + pos = *lineptr; + + for (;;) { + int c = getc(stream); + + if (chr < 2) { + *n += (*n > 16) ? *n : 64; + chr = *n + *lineptr - pos; + if (!(*lineptr = (char*)mem_r(*lineptr,*n))) + return -1; + pos = *n - chr + *lineptr; + } + + if (ferror(stream)) + return -1; + if (c == EOF) { + if (pos == *lineptr) + return -1; + else + break; + } + + *pos++ = c; + chr--; + if (c == '\n') + break; + } + *pos = '\0'; + return (ret = pos - *lineptr); +} + #ifndef _WIN32 #include bool util_isatty(FILE *file) { -- 2.39.2