From a36fde37f2adf1579a13707cd64a369683e35ca6 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 28 Sep 2014 01:40:09 +0200 Subject: mds-kbdc will need multiple .c files + .h files are not required during linking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 17 ++-- mk/build.mk | 23 ++--- src/mds-kbdc.c | 253 ------------------------------------------------ src/mds-kbdc.h | 23 ----- src/mds-kbdc/mds-kbdc.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++ src/mds-kbdc/mds-kbdc.h | 23 +++++ 6 files changed, 299 insertions(+), 293 deletions(-) delete mode 100644 src/mds-kbdc.c delete mode 100644 src/mds-kbdc.h create mode 100644 src/mds-kbdc/mds-kbdc.c create mode 100644 src/mds-kbdc/mds-kbdc.h diff --git a/Makefile b/Makefile index f599801..ea85be6 100644 --- a/Makefile +++ b/Makefile @@ -22,17 +22,22 @@ TOOLS = mds-kbdc SETUID_SERVERS = mds mds-kkbd mds-vt - -OBJ_mds-server_ = mds-server interception-condition client multicast \ - queued-interception globals signals interceptors \ - sending slavery reexec receiving +# Object files for multi-object file binaries. +OBJ_mds-server_ = mds-server interception-condition client multicast \ + queued-interception globals signals interceptors \ + sending slavery reexec receiving OBJ_mds-registry_ = mds-registry util globals reexec registry signals \ slave -OBJ_mds-server = $(foreach O,$(OBJ_mds-server_),obj/mds-server/$(O).o) -OBJ_mds-registry = $(foreach O,$(OBJ_mds-registry_),obj/mds-registry/$(O).o) +OBJ_mds-kbdc_ = mds-kbdc + +OBJ_mds-server = $(foreach O,$(OBJ_mds-server_),obj/mds-server/$(O).o) +OBJ_mds-registry = $(foreach O,$(OBJ_mds-registry_),obj/mds-registry/$(O).o) +OBJ_mds-kbdc = $(foreach O,$(OBJ_mds-kbdc_),obj/mds-kbdc/$(O).o) + +# sed:ed .h-source file. ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) SEDED = src/libmdsserver/config.h else diff --git a/mk/build.mk b/mk/build.mk index 8e3ef62..d513a73 100644 --- a/mk/build.mk +++ b/mk/build.mk @@ -17,9 +17,9 @@ tools: $(foreach T,$(TOOLS),bin/$(T)) # Link large servers. ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) -bin/mds-server: $(OBJ_mds-server) obj/mds-base.o src/mds-server/*.h bin/libmdsserver.so $(SEDED) +bin/mds-server: $(OBJ_mds-server) obj/mds-base.o bin/libmdsserver.so else -bin/mds-server: $(OBJ_mds-server) obj/mds-base.o src/mds-server/*.h $(SEDED) +bin/mds-server: $(OBJ_mds-server) obj/mds-base.o endif mkdir -p $(shell dirname $@) $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_mds-server) $(OBJ_mds-server) obj/mds-base.o @@ -30,9 +30,9 @@ endif ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) -bin/mds-registry: $(OBJ_mds-registry) obj/mds-base.o src/mds-registry/*.h bin/libmdsserver.so $(SEDED) +bin/mds-registry: $(OBJ_mds-registry) obj/mds-base.o bin/libmdsserver.so else -bin/mds-registry: $(OBJ_mds-registry) obj/mds-base.o src/mds-registry/*.h $(SEDED) +bin/mds-registry: $(OBJ_mds-registry) obj/mds-base.o endif mkdir -p $(shell dirname $@) $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_mds-registry) $(OBJ_mds-registry) obj/mds-base.o @@ -41,9 +41,9 @@ endif # Link small servers. ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) -bin/%: obj/%.o obj/mds-base.o bin/libmdsserver.so $(SEDED) +bin/%: obj/%.o obj/mds-base.o bin/libmdsserver.so else -bin/%: obj/%.o obj/mds-base.o $(SEDED) +bin/%: obj/%.o obj/mds-base.o endif mkdir -p $(shell dirname $@) $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_$*) $< obj/mds-base.o @@ -52,9 +52,9 @@ endif # Link kernel. ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) -bin/mds: obj/mds.o bin/libmdsserver.so $(SEDED) +bin/mds: obj/mds.o bin/libmdsserver.so else -bin/mds: obj/mds.o $(SEDED) +bin/mds: obj/mds.o endif mkdir -p $(shell dirname $@) $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_mds) $< @@ -62,13 +62,14 @@ endif # Link utilies that do not use mds-base. + ifneq ($(LIBMDSSERVER_IS_INSTALLED),y) -bin/mds-kbdc: obj/mds-kbdc.o bin/libmdsserver.so $(SEDED) +bin/mds-kbdc: $(OBJ_mds-kbdc) bin/libmdsserver.so else -bin/mds-kbdc: obj/mds-kbdc.o $(SEDED) +bin/mds-kbdc: $(OBJ_mds-kbdc) endif mkdir -p $(shell dirname $@) - $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_mds) $< + $(CC) $(C_FLAGS) -o $@ $(LDS) $(LDS_mds-kbdc) $(OBJ_mds-kbdc) # Build object files for kernel/servers. diff --git a/src/mds-kbdc.c b/src/mds-kbdc.c deleted file mode 100644 index 27c9cca..0000000 --- a/src/mds-kbdc.c +++ /dev/null @@ -1,253 +0,0 @@ -/** - * mds — A micro-display server - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include "mds-kbdc.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - - - -/** - * The number of elements in `argv` - */ -static int argc; - -/** - * The command line arguments - */ -static char** argv; - - - -/** - * Read the content of a file, ignoring interruptions - * - * @param pathname The file to read - * @param size Output parameter for the size of the read content, in char:s - * @return The read content, `NULL` on error - */ -static char* read_file(const char* restrict pathname, size_t* restrict size) -{ - size_t buf_size = 8096; - size_t buf_ptr = 0; - char* restrict content = NULL; - char* restrict old = NULL; - int fd = -1; - ssize_t got; - - /* Allocate buffer for the file's content. */ - fail_if (xmalloc(content, buf_size, char)); - /* Open the file to compile. */ - fail_if ((fd = open(pathname, O_RDONLY)) < 0); - - /* Read the file to compile. */ - for (;;) - { - /* Make sure the buffer is not small. */ - if (buf_size - buf_ptr < 2048) - { - fail_if (xxrealloc(old, content, buf_size <<= 1, char)); - } - /* Read a chunk of the file. */ - got = read(fd, content + buf_ptr, (buf_size - buf_ptr) * sizeof(char)); - if ((got < 0) && (errno == EINTR)) continue; - else if (got < 0) goto pfail; - else if (got == 0) break; - buf_ptr += (size_t)got; - } - - /* Shrink the buffer so it is not excessively large. */ - fail_if (xxrealloc(old, content, buf_ptr, char)); - - /* Close file decriptor for the file. */ - close(fd); - - *size = buf_ptr; - return content; - - pfail: - xperror(*argv); - free(old); - free(content); - if (fd >= 0) - close(fd); - return NULL; -} - - -/** - * Remove comments from the content - * - * @param content The code to shrink - * @param size The size of `content`, in char:s - * @return The new size of `content`, in char:s; this function cannot fail - */ -static size_t remove_comments(char* restrict content, size_t size) -{ -#define t content[n_ptr++] = c - - size_t n_ptr = 0, o_ptr = 0; - int comment = 0, quote = 0, escape = 0; - - while (o_ptr < size) - { - char c = content[o_ptr++]; - /* Remove comment */ - if (comment) - { - if (c == '\n') t, comment = 0; - } - /* Quotes can contain comment symbols, quotes by also contain escapes. */ - else if (escape) t, escape = 0; - else if (quote) - { - t; - if (c == '\\') escape = 1; - else if (c == '"') quote = 0; - } - /* # is the comment symbol. */ - else if (c == '#') comment = 1; - /* " is the quote symbol. */ - else if (c == '"') t, quote = 1; - /* Code and whitespace. */ - else t; - } - - return n_ptr; - -#undef t -} - - -/** - * Create an array of each line in a text - * - * @param content The text to split, it must end with an LF. - * LF:s are treated as line endings rather than - * new lines, this means that the final LF will - * not create a new line in the returned array. - * Each LF will be replaced by a NUL-character. - * @param length The length of `content`. - * @return An array of each line in `content`. This - * array will be `NULL`-terminated. It will also - * reuse the allocate of `content`. This means - * that each element must not be free:d, rather - * you should simply free this returned allocation - * and the allocation of `content`. On error - * `NULL` is returned, and `content` will not - * have been modified. - */ -static char** line_split(char* content, size_t length) -{ - char** restrict lines = NULL; - size_t count = 0; - size_t i, j; - int new_line = 1; - - for (i = 0; i < length; i++) - if (content[i] == '\n') - count++; - - fail_if (xmalloc(lines, count + 1, char)); - lines[count] = NULL; - - for (i = j = 0; i < length; i++) - { - if (new_line) - new_line = 0, lines[j++] = content + i; - if (content[i] == '\n') - { - new_line = 1; - content[i] = '\0'; - } - } - - return lines; - - pfail: - xperror(*argv); - return NULL; -} - - -/** - * Compile a keyboard layout file - * - * @param argc_ The number of elements in `argv_` - * @param argv_ The command line arguments - * @return Zero on and only one success - */ -int main(int argc_, char** argv_) -{ - const char* pathname = argv_[1]; - char* content = NULL; - char* real_content = NULL; - char* old = NULL; - size_t content_size; - size_t real_content_size; - char** lines = NULL; - char** real_lines = NULL; - int rc = 0; - - argc = argc_; - argv = argv_; - - /* Read the file. */ - content = read_file(pathname, &content_size); - fail_if (content == NULL); - - /* Make sure the content ends with a new line. */ - if (!content_size || (content[content_size - 1] != '\n')) - { - fail_if (xxrealloc(old, content, content_size + 1, char)); - content[content_size++] = '\n'; - } - - /* Simplify file. */ - fail_if (xmalloc(real_content, content_size, char)); - memcpy(real_content, content, content_size * sizeof(char)); - real_content_size = content_size; - content_size = remove_comments(content, content_size); - fail_if (xxrealloc(old, content, content_size, char)); - - /* Split by line. */ - fail_if ((lines = line_split(content, content_size)) == NULL); - fail_if ((real_lines = line_split(real_content, real_content_size)) == NULL); - - done: - free(old); - free(content); - free(real_content); - free(lines); - free(real_lines); - return rc; - - pfail: - xperror(*argv); - rc = 1; - goto done; -} - diff --git a/src/mds-kbdc.h b/src/mds-kbdc.h deleted file mode 100644 index 333a869..0000000 --- a/src/mds-kbdc.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * mds — A micro-display server - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#ifndef MDS_MDS_KBDC_H -#define MDS_MDS_KBDC_H - - -#endif - diff --git a/src/mds-kbdc/mds-kbdc.c b/src/mds-kbdc/mds-kbdc.c new file mode 100644 index 0000000..27c9cca --- /dev/null +++ b/src/mds-kbdc/mds-kbdc.c @@ -0,0 +1,253 @@ +/** + * mds — A micro-display server + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "mds-kbdc.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + + +/** + * The number of elements in `argv` + */ +static int argc; + +/** + * The command line arguments + */ +static char** argv; + + + +/** + * Read the content of a file, ignoring interruptions + * + * @param pathname The file to read + * @param size Output parameter for the size of the read content, in char:s + * @return The read content, `NULL` on error + */ +static char* read_file(const char* restrict pathname, size_t* restrict size) +{ + size_t buf_size = 8096; + size_t buf_ptr = 0; + char* restrict content = NULL; + char* restrict old = NULL; + int fd = -1; + ssize_t got; + + /* Allocate buffer for the file's content. */ + fail_if (xmalloc(content, buf_size, char)); + /* Open the file to compile. */ + fail_if ((fd = open(pathname, O_RDONLY)) < 0); + + /* Read the file to compile. */ + for (;;) + { + /* Make sure the buffer is not small. */ + if (buf_size - buf_ptr < 2048) + { + fail_if (xxrealloc(old, content, buf_size <<= 1, char)); + } + /* Read a chunk of the file. */ + got = read(fd, content + buf_ptr, (buf_size - buf_ptr) * sizeof(char)); + if ((got < 0) && (errno == EINTR)) continue; + else if (got < 0) goto pfail; + else if (got == 0) break; + buf_ptr += (size_t)got; + } + + /* Shrink the buffer so it is not excessively large. */ + fail_if (xxrealloc(old, content, buf_ptr, char)); + + /* Close file decriptor for the file. */ + close(fd); + + *size = buf_ptr; + return content; + + pfail: + xperror(*argv); + free(old); + free(content); + if (fd >= 0) + close(fd); + return NULL; +} + + +/** + * Remove comments from the content + * + * @param content The code to shrink + * @param size The size of `content`, in char:s + * @return The new size of `content`, in char:s; this function cannot fail + */ +static size_t remove_comments(char* restrict content, size_t size) +{ +#define t content[n_ptr++] = c + + size_t n_ptr = 0, o_ptr = 0; + int comment = 0, quote = 0, escape = 0; + + while (o_ptr < size) + { + char c = content[o_ptr++]; + /* Remove comment */ + if (comment) + { + if (c == '\n') t, comment = 0; + } + /* Quotes can contain comment symbols, quotes by also contain escapes. */ + else if (escape) t, escape = 0; + else if (quote) + { + t; + if (c == '\\') escape = 1; + else if (c == '"') quote = 0; + } + /* # is the comment symbol. */ + else if (c == '#') comment = 1; + /* " is the quote symbol. */ + else if (c == '"') t, quote = 1; + /* Code and whitespace. */ + else t; + } + + return n_ptr; + +#undef t +} + + +/** + * Create an array of each line in a text + * + * @param content The text to split, it must end with an LF. + * LF:s are treated as line endings rather than + * new lines, this means that the final LF will + * not create a new line in the returned array. + * Each LF will be replaced by a NUL-character. + * @param length The length of `content`. + * @return An array of each line in `content`. This + * array will be `NULL`-terminated. It will also + * reuse the allocate of `content`. This means + * that each element must not be free:d, rather + * you should simply free this returned allocation + * and the allocation of `content`. On error + * `NULL` is returned, and `content` will not + * have been modified. + */ +static char** line_split(char* content, size_t length) +{ + char** restrict lines = NULL; + size_t count = 0; + size_t i, j; + int new_line = 1; + + for (i = 0; i < length; i++) + if (content[i] == '\n') + count++; + + fail_if (xmalloc(lines, count + 1, char)); + lines[count] = NULL; + + for (i = j = 0; i < length; i++) + { + if (new_line) + new_line = 0, lines[j++] = content + i; + if (content[i] == '\n') + { + new_line = 1; + content[i] = '\0'; + } + } + + return lines; + + pfail: + xperror(*argv); + return NULL; +} + + +/** + * Compile a keyboard layout file + * + * @param argc_ The number of elements in `argv_` + * @param argv_ The command line arguments + * @return Zero on and only one success + */ +int main(int argc_, char** argv_) +{ + const char* pathname = argv_[1]; + char* content = NULL; + char* real_content = NULL; + char* old = NULL; + size_t content_size; + size_t real_content_size; + char** lines = NULL; + char** real_lines = NULL; + int rc = 0; + + argc = argc_; + argv = argv_; + + /* Read the file. */ + content = read_file(pathname, &content_size); + fail_if (content == NULL); + + /* Make sure the content ends with a new line. */ + if (!content_size || (content[content_size - 1] != '\n')) + { + fail_if (xxrealloc(old, content, content_size + 1, char)); + content[content_size++] = '\n'; + } + + /* Simplify file. */ + fail_if (xmalloc(real_content, content_size, char)); + memcpy(real_content, content, content_size * sizeof(char)); + real_content_size = content_size; + content_size = remove_comments(content, content_size); + fail_if (xxrealloc(old, content, content_size, char)); + + /* Split by line. */ + fail_if ((lines = line_split(content, content_size)) == NULL); + fail_if ((real_lines = line_split(real_content, real_content_size)) == NULL); + + done: + free(old); + free(content); + free(real_content); + free(lines); + free(real_lines); + return rc; + + pfail: + xperror(*argv); + rc = 1; + goto done; +} + diff --git a/src/mds-kbdc/mds-kbdc.h b/src/mds-kbdc/mds-kbdc.h new file mode 100644 index 0000000..333a869 --- /dev/null +++ b/src/mds-kbdc/mds-kbdc.h @@ -0,0 +1,23 @@ +/** + * mds — A micro-display server + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef MDS_MDS_KBDC_H +#define MDS_MDS_KBDC_H + + +#endif + -- cgit v1.2.3-70-g09d2