Author | Jamozed <[email protected]> |
Date | 2021-03-31 12:54:21 |
Commit | d601a0a1790e94b672f2fc4156e35af7698ea798 |
Parent | 72d349955dd7a76f549677076d0e6496d21b58cc |
Improve builtin compilation
Diffstat
M | CMakeLists.txt | | | 3 | ++- |
M | src/bltn.c | | | 6 | +++--- |
M | src/bltns/cd.c | | | 48 | +++++++++++++++++++++++++++++++++++++++++------- |
M | src/bltns/pwd.c | | | 5 | ++--- |
4 files changed, 48 insertions, 14 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a42d286..48834a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,5 +6,6 @@ ADD_COMPILE_DEFINITIONS(PROJECT_VERSION="${PROJECT_VERSION}") FILE(GLOB LIBSRC ${CMAKE_SOURCE_DIR}/src/lib/*) FILE(GLOB SOURCES ${CMAKE_SOURCE_DIR}/src/*) +FILE(GLOB BLTNSRC ${CMAKE_SOURCE_DIR}/src/bltns/*) -ADD_EXECUTABLE(osh ${SOURCES} ${LIBSRC}) +ADD_EXECUTABLE(osh ${SOURCES} ${LIBSRC} ${BLTNSRC}) diff --git a/src/bltn.c b/src/bltn.c index 7ff7449..4d4f2e4 100644 --- a/src/bltn.c +++ b/src/bltn.c @@ -30,9 +30,6 @@ 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 WITH THE SOFTWARE. */ -#include "bltns/cd.c" -#include "bltns/pwd.c" - #include "alias.h" #include "bltn.h" #include "exec.h" @@ -48,6 +45,9 @@ static int bltn_false(char *av[]) { (void)(av); return 1; } static int bltn_help(char *av[]); static int bltn_true(char *av[]) { (void)(av); return 0; } +extern int bltn_cd(char *av[]); +extern int bltn_pwd(char *av[]); + static int getret(char *av[]) { printf("%d\n", _ret); return 0; } struct bltn bltns[] = { diff --git a/src/bltns/cd.c b/src/bltns/cd.c index edc9dd2..1b84444 100644 --- a/src/bltns/cd.c +++ b/src/bltns/cd.c @@ -1,4 +1,4 @@ -// cd.c, version 0.1.0 +// cd.c, version 0.1.1 // cd builtin source file for OSH // Copyright (C) 2020, Jakob Wakeling // All rights reserved. @@ -39,21 +39,55 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <stdlib.h> #include <string.h> -static int bltn_cd(char *av[]) { - struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = NULL; +#define VERSION "0.1.1" + +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + +static int mode = 0; + +static void hlp(void); +static void ver(void); + +int bltn_cd(char *av[]) { + struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops; for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { - case 'L': { break; } - case 'P': { break; } + case 'L': { mode = 0; break; } + case 'P': { mode = 1; break; } + case 256: { hlp(); return 0; } + case 257: { ver(); return 0; } default: { return 1; } } - char *path = av[1] ? av[1] : getenv("HOME"); + char *path = av[opt.ind] ? av[opt.ind] : getenv("HOME"); if (chdir(path)) { fprintf(stderr, "%s: %s: %s\n", av[0], path, strerror(errno)); - return 1; + errno = 0; return 1; } path = getcwd(NULL, 0); setenv("PWD", path, 1); free(path); return 0; } + +static void hlp(void) { + puts("cd - change the working directory\n"); + puts("usage: cd [-L|-P] [directory]\n"); + puts("options:"); + puts(" -L Handle the operand dot-dot logically"); + puts(" -P Handle the operand dot-dot physically"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static void ver(void) { + puts("OSH cd, version " VERSION); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +} diff --git a/src/bltns/pwd.c b/src/bltns/pwd.c index 02c0a03..ba48713 100644 --- a/src/bltns/pwd.c +++ b/src/bltns/pwd.c @@ -30,7 +30,6 @@ 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 WITH THE SOFTWARE. */ -#include "../lib/error.h" #include "../lib/optget.h" #include <unistd.h> @@ -51,7 +50,7 @@ static int mode = 0; static void hlp(void); static void ver(void); -static int bltn_pwd(char *av[]) { A0 = av[0]; +int bltn_pwd(char *av[]) { struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops; for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { case 'L': { mode = 0; break; } @@ -64,7 +63,7 @@ static int bltn_pwd(char *av[]) { A0 = av[0]; pwd:; char *cwd = mode ? getcwd(NULL, 0) : getenv("PWD"); if (!cwd && !mode) { mode = 1; goto pwd; } - else if (!cwd) { error(1, "%s", av[0]); } + else if (!cwd) { fprintf(stderr, "ERROR\n"); return 1; } fputs(cwd, stdout); fputc('\n', stdout);