diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-02 20:14:30 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-02 20:19:14 +0100 |
commit | 84bec65807220f69d1d439981bac044515b292b5 (patch) | |
tree | e5276de68365190f568cda095e3e899d5448da81 | |
parent | mds-kbdc: files with syntax errors are not empty (diff) | |
download | mds-84bec65807220f69d1d439981bac044515b292b5.tar.gz mds-84bec65807220f69d1d439981bac044515b292b5.tar.bz2 mds-84bec65807220f69d1d439981bac044515b292b5.tar.xz |
mds-kbdc: use absolute and relative paths
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/mds-kbdc/make-tree.c | 21 | ||||
-rw-r--r-- | src/mds-kbdc/parse-error.c | 6 | ||||
-rw-r--r-- | src/mds-kbdc/paths.c | 168 | ||||
-rw-r--r-- | src/mds-kbdc/paths.h | 49 |
5 files changed, 227 insertions, 19 deletions
@@ -33,7 +33,7 @@ OBJ_mds-registry_ = mds-registry util globals reexec registry signals \ OBJ_mds-kbdc_ = mds-kbdc globals raw-data functions string tree \ make-tree parse-error simplify-tree parsed \ process-includes validate-tree eliminate-dead-code \ - include-stack + paths include-stack 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) diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c index a8f20ee..989cd2b 100644 --- a/src/mds-kbdc/make-tree.c +++ b/src/mds-kbdc/make-tree.c @@ -17,6 +17,8 @@ */ #include "make-tree.h" +#include "paths.h" + #include <limits.h> #include <stdlib.h> #include <libgen.h> @@ -711,32 +713,18 @@ static mds_kbdc_parse_error_t* error; */ static int get_pathname(const char* restrict filename, state_t* restrict state) { - size_t cwd_size = 4096 >> 1; char* cwd = NULL; - char* old = NULL; int saved_errno; /* Get a non-relative pathname for the file, relative filenames * can be misleading as the program can have changed working * directory to be able to resolve filenames. */ - parsing_result->pathname = realpath(filename, NULL); /* XXX use absolute path */ + parsing_result->pathname = abspath(filename); if (parsing_result->pathname == NULL) { fail_if (errno != ENOENT); saved_errno = errno; - - /* Get the current working directory. */ - /* glibc offers ways to do this in just one function call, - * but we will not assume that glibc is used here. */ - for (;;) - { - fail_if (xxrealloc(old, cwd, cwd_size <<= 1, char)); - if (getcwd(cwd, cwd_size)) - break; - else - fail_if (errno != ERANGE); - } - + fail_if ((cwd = curpath(), cwd == NULL)); parsing_result->pathname = strdup(filename); fail_if (parsing_result->pathname == NULL); NEW_ERROR_(parsing_result, ERROR, 0, 0, 0, 0, 1, "no such file or directory in ‘%s’", cwd); @@ -758,7 +746,6 @@ static int get_pathname(const char* restrict filename, state_t* restrict state) pfail: saved_errno = errno; free(cwd); - free(old); return errno = saved_errno, -1; } diff --git a/src/mds-kbdc/parse-error.c b/src/mds-kbdc/parse-error.c index 92e5ab0..f7db9ad 100644 --- a/src/mds-kbdc/parse-error.c +++ b/src/mds-kbdc/parse-error.c @@ -17,6 +17,8 @@ */ #include "parse-error.h" +#include "paths.h" + #include <stdlib.h> #include <alloca.h> #include <string.h> @@ -34,6 +36,7 @@ static void print(const mds_kbdc_parse_error_t* restrict this, FILE* restrict ou { size_t i, n, start = 0, end = 0; const char* restrict code = this->code; + char* restrict path = relpath(this->pathname, NULL); /* Convert bytes count to character count for the code position. */ for (i = 0, n = this->start; i < n; i++) @@ -45,7 +48,8 @@ static void print(const mds_kbdc_parse_error_t* restrict this, FILE* restrict ou end += start; /* Print error information. */ - fprintf(output, "\033[01m%s\033[21m:", this->pathname); /* TODO should be relative to the current dir */ + fprintf(output, "\033[01m%s\033[21m:", path ? path : this->pathname); + free(path); if (this->error_is_in_file) fprintf(output, "%zu:%zu–%zu:", this->line + 1, start, end); switch (this->severity) diff --git a/src/mds-kbdc/paths.c b/src/mds-kbdc/paths.c new file mode 100644 index 0000000..d66ae99 --- /dev/null +++ b/src/mds-kbdc/paths.c @@ -0,0 +1,168 @@ +/** + * 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 <http://www.gnu.org/licenses/>. + */ +#include "paths.h" + +#include <libmdsserver/macros.h> + +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + + +/** + * Get the current working directory + * + * @return The current working directory + */ +char* curpath(void) +{ + static size_t cwd_size = 4096 >> 1; + char* cwd = NULL; + char* old = NULL; + int saved_errno; + + /* glibc offers ways to do this in just one function call, + * but we will not assume that glibc is used here. */ + for (;;) + { + fail_if (xxrealloc(old, cwd, (cwd_size <<= 1) + 1, char)); + if (getcwd(cwd, cwd_size)) + break; + else + fail_if (errno != ERANGE); + } + + return cwd; + pfail: + saved_errno = errno; + free(old); + free(cwd); + errno = saved_errno; + return NULL; +} + + +/** + * Get the absolute path of a file + * + * @param path The filename of the file + * @return The file's absolute path, `NULL` on error + */ +char* abspath(const char* path) +{ + char* cwd = NULL; + char* buf = NULL; + int saved_errno, slash = 1; + size_t size, p; + + if (*path == '/') + return strdup(path); + + fail_if ((cwd = curpath(), cwd == NULL)); + size = (p = strlen(cwd)) + strlen(path) + 2; + fail_if (xmalloc(buf, size + 1, char)); + memcpy(buf, cwd, (p + 1) * sizeof(char)); + if (buf[p - 1] != '/') + buf[p++] = '/'; + + while (*path) + if (slash && (path[0] == '/')) + path += 1; + else if (slash && (path[0] == '.') && (path[1] == '/')) + path += 2; + else if (slash && (path[0] == '.') && (path[1] == '.') && (path[2] == '/')) + { + path += 3; + p--; + while (p && (buf[--p] != '/')); + p++; + } + else + { + buf[p++] = *path; + slash = (*path++ == '/'); + } + + buf[p] = '\0'; + + free(cwd); + return buf; + pfail: + saved_errno = errno; + free(cwd); + errno = saved_errno; + return NULL; +} + + +/** + * Get a relative path of a file + * + * @param path The filename of the file + * @param base The pathname of the base directory, + * `NULL` for the current working directroy + * @return The file's relative path, `NULL` on error + */ +char* relpath(const char* path, const char* base) +{ + char* abs = abspath(path); + char* absbase = NULL; + char* buf = NULL; + char* old = NULL; + int saved_errno; + size_t p, slash = 1, back = 0; + + fail_if (abs == NULL); + absbase = base ? abspath(base) : curpath(); + fail_if (absbase == NULL); + + if (absbase[strlen(absbase) - 1] != '/') + /* Both `abspath` and `curpath` (and `relpath`) allocates one extra slot. */ + absbase[strlen(absbase) + 1] = '\0', absbase[strlen(absbase)] = '/'; + + fprintf(stderr, "abs: %s\n", abs); + fprintf(stderr, "absbase: %s\n\n", absbase); + + for (p = 1; abs[p] && absbase[p] && (abs[p] == absbase[p]); p++) + if (abs[p] == '/') + slash = p + 1; + + for (p = slash; absbase[p]; p++) + if (absbase[p] == '/') + back++; + + fail_if (xmalloc(buf, back * 3 + strlen(abs + slash) + 2, char)); + + for (p = 0; back--;) + buf[p++] = '.', buf[p++] = '.', buf[p++] = '/'; + memcpy(buf + p, abs + slash, (strlen(abs + slash) + 1) * sizeof(char)); + + free(abs); + free(absbase); + return buf; + pfail: + saved_errno = errno; + free(abs); + free(absbase); + free(old); + errno = saved_errno; + return NULL; +} + diff --git a/src/mds-kbdc/paths.h b/src/mds-kbdc/paths.h new file mode 100644 index 0000000..a045334 --- /dev/null +++ b/src/mds-kbdc/paths.h @@ -0,0 +1,49 @@ +/** + * 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 <http://www.gnu.org/licenses/>. + */ +#ifndef MDS_MDS_KBDC_PATHS_H +#define MDS_MDS_KBDC_PATHS_H + + +/** + * Get the current working directory + * + * @return The current working directory + */ +char* curpath(void); + +/** + * Get the absolute path of a file + * + * @param path The filename of the file + * @return The file's absolute path, `NULL` on error + */ +char* abspath(const char* path); + +/** + * Get a relative path of a file + * + * @param path The filename of the file + * @param base The pathname of the base directory, + * `NULL` for the current working directroy + * @return The file's relative path, `NULL` on error + */ +char* relpath(const char* path, const char* base); + + +#endif + |