From 84bec65807220f69d1d439981bac044515b292b5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 2 Dec 2014 20:14:30 +0100 Subject: mds-kbdc: use absolute and relative paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-kbdc/make-tree.c | 21 ++---- src/mds-kbdc/parse-error.c | 6 +- src/mds-kbdc/paths.c | 168 +++++++++++++++++++++++++++++++++++++++++++++ src/mds-kbdc/paths.h | 49 +++++++++++++ 4 files changed, 226 insertions(+), 18 deletions(-) create mode 100644 src/mds-kbdc/paths.c create mode 100644 src/mds-kbdc/paths.h (limited to 'src/mds-kbdc') 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 #include #include @@ -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 #include #include @@ -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 . + */ +#include "paths.h" + +#include + +#include +#include +#include +#include + + + +/** + * 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 . + */ +#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 + -- cgit v1.2.3-70-g09d2