From 423285b855cd89c1bac42ddc41405a079625f595 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 28 Feb 2016 23:38:08 +0100 Subject: [PATCH] Convert sh to C. --- sh/Makefile | 18 ++++++------ sh/{editline.cpp => editline.c} | 6 ++-- sh/{proper-sh.cpp => proper-sh.c} | 7 +++-- sh/{sh.cpp => sh.c} | 48 +++++++++++++++++++------------ sh/{showline.cpp => showline.c} | 3 +- sh/{util.cpp => util.c} | 10 +++++-- sh/util.h | 3 +- 7 files changed, 59 insertions(+), 36 deletions(-) rename sh/{editline.cpp => editline.c} (99%) rename sh/{proper-sh.cpp => proper-sh.c} (96%) rename sh/{sh.cpp => sh.c} (98%) rename sh/{showline.cpp => showline.c} (99%) rename sh/{util.cpp => util.c} (94%) diff --git a/sh/Makefile b/sh/Makefile index bd240c53..471b642c 100644 --- a/sh/Makefile +++ b/sh/Makefile @@ -5,18 +5,18 @@ include ../build-aux/version.mak include ../build-aux/dirs.mak OPTLEVEL?=$(DEFAULT_OPTLEVEL) -CXXFLAGS?=$(OPTLEVEL) +CFLAGS?=$(OPTLEVEL) CPPFLAGS:=$(CPPFLAGS) -DVERSIONSTR=\"$(VERSION)\" -CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti +CFLAGS:=$(CFLAGS) -Wall -Wextra BINARIES:=sh sortix-sh SORTIX_SH_SRCS=\ -editline.cpp \ -sh.cpp \ -showline.cpp \ -util.cpp +editline.c \ +sh.c \ +showline.c \ +util.c all: $(BINARIES) @@ -27,10 +27,10 @@ install: all install $(BINARIES) $(DESTDIR)$(BINDIR) sortix-sh: $(SORTIX_SH_SRCS) *.h - $(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $(SORTIX_SH_SRCS) -o $@ + $(CC) -std=gnu11 $(CFLAGS) $(CPPFLAGS) $(SORTIX_SH_SRCS) -o $@ -sh: proper-sh.cpp - $(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< -o $@ +sh: proper-sh.c + $(CC) -std=gnu11 $(CFLAGS) $(CPPFLAGS) $< -o $@ clean: rm -f $(BINARIES) *.o diff --git a/sh/editline.cpp b/sh/editline.c similarity index 99% rename from sh/editline.cpp rename to sh/editline.c index 14cfca8e..6bd92fce 100644 --- a/sh/editline.cpp +++ b/sh/editline.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - editline.cpp + editline.c Read a line from the terminal. *******************************************************************************/ @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -596,7 +597,8 @@ void edit_line(struct edit_line* edit_state) break; } - if ( int kbkey = KBKEY_DECODE(codepoint) ) + int kbkey; + if ( (kbkey = KBKEY_DECODE(codepoint)) ) edit_line_kbkey(edit_state, kbkey); else edit_line_codepoint(edit_state, (wchar_t) codepoint); diff --git a/sh/proper-sh.cpp b/sh/proper-sh.c similarity index 96% rename from sh/proper-sh.cpp rename to sh/proper-sh.c index 41fb5e59..c71f8cee 100644 --- a/sh/proper-sh.cpp +++ b/sh/proper-sh.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - proper-sh.cpp + proper-sh.c Forward execution to the best shell. *******************************************************************************/ @@ -24,12 +24,13 @@ #include #include +#include #include #include #include #include -const char* getenv_safe(const char* name, const char* def = "") +const char* getenv_safe(const char* name, const char* def) { const char* ret = getenv(name); return ret ? ret : def; @@ -56,7 +57,7 @@ bool is_existing_shell(const char* candidate) return WIFEXITED(status) && WEXITSTATUS(status) == 0; } -char* search_for_proper_shell() +char* search_for_proper_shell(void) { if ( getenv("SORTIX_SH_BACKEND") ) { diff --git a/sh/sh.cpp b/sh/sh.c similarity index 98% rename from sh/sh.cpp rename to sh/sh.c index 8a604ec9..19303e4b 100644 --- a/sh/sh.cpp +++ b/sh/sh.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - sh.cpp + sh.c Command language interpreter. *******************************************************************************/ @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -90,7 +91,7 @@ static bool is_proper_absolute_path(const char* path) return true; } -void update_env() +void update_env(void) { char str[3 * sizeof(size_t)]; struct winsize ws; @@ -349,7 +350,7 @@ bool token_expand_wildcards(void*** out, size_t index = 0; size_t num_escaped_wildcards = 0; // We don't properly support them yet. - stringbuf buf; + struct stringbuf buf; stringbuf_begin(&buf); bool escape = false; @@ -385,9 +386,10 @@ bool token_expand_wildcards(void*** out, if ( token[index] != '*' || num_escaped_wildcards ) { + char* value; free(stringbuf_finish(&buf)); just_return_input: - char* value = strdup(token); + value = strdup(token); if ( !value ) return false; if ( !array_add(out, out_used, out_length, value) ) @@ -485,7 +487,8 @@ bool token_expand_wildcards(void*** out, } } size_t num_inserted = 0; - while ( struct dirent* entry = readdir(dir) ) + struct dirent* entry; + while ( (entry = readdir(dir)) ) { if ( !matches_simple_pattern(entry->d_name, pattern + match_from) ) continue; @@ -867,7 +870,7 @@ struct execute_result bool exited; }; -struct execute_result execute(const char* const* tokens, +struct execute_result execute(char** tokens, size_t tokens_count, bool interactive, int pipein, @@ -1117,7 +1120,7 @@ struct execute_result execute(const char* const* tokens, internal = true; const char* newdir = argv[1]; if ( !newdir ) - newdir = getenv_safe("HOME", "/"); + newdir = getenv_safe_def("HOME", "/"); internal_status = 0; if ( perform_chdir(newdir) < 0 ) { @@ -1308,7 +1311,7 @@ struct execute_result execute(const char* const* tokens, __builtin_unreachable(); } -int run_tokens(const char* const* tokens, +int run_tokens(char** tokens, size_t tokens_count, bool interactive, bool exit_on_error, @@ -1542,12 +1545,13 @@ int run_command(char* command, return result; } -bool does_line_editing_need_another_line(void*, const char* line) +bool does_line_editing_need_another_line(void* ctx, const char* line) { + (void) ctx; return !is_shell_input_ready(line); } -bool is_outermost_shell() +bool is_outermost_shell(void) { const char* shlvl_str = getenv("SHLVL"); if ( !shlvl_str ) @@ -1576,10 +1580,12 @@ bool is_usual_char_for_completion(char c) size_t do_complete(char*** completions_ptr, size_t* used_before_ptr, size_t* used_after_ptr, - void*, + void* ctx, const char* partial, size_t complete_at) { + (void) ctx; + size_t used_before = 0; size_t used_after = 0; @@ -1658,9 +1664,11 @@ size_t do_complete(char*** completions_ptr, char* component; while ( (component = strsep(&path_input, ":")) ) { - if ( DIR* dir = opendir(component) ) + DIR* dir; + if ( (dir = opendir(component)) ) { - while ( struct dirent* entry = readdir(dir) ) + struct dirent* entry; + while ( (entry = readdir(dir)) ) { if ( strncmp(entry->d_name, partial + complete_at - used_before, used_before) != 0 ) continue; @@ -1716,7 +1724,8 @@ size_t do_complete(char*** completions_ptr, free(dirpath_alloc); break; } - while ( struct dirent* entry = readdir(dir) ) + struct dirent* entry; + while ( (entry = readdir(dir)) ) { if ( strncmp(entry->d_name, pattern, pattern_length) != 0 ) continue; @@ -1812,7 +1821,7 @@ void read_command_interactive(struct sh_read_command* sh_read_command) strlcpy(hostname, "(none)", sizeof(hostname)); const char* print_hostname = hostname; const char* print_dir = current_dir ? current_dir : "?"; - const char* home_dir = getenv_safe("HOME", ""); + const char* home_dir = getenv_safe("HOME"); const char* print_dir_1 = print_dir; const char* print_dir_2 = ""; @@ -2064,7 +2073,8 @@ int main(int argc, char* argv[]) // TODO: Canonicalize argv[0] if it contains a slash and isn't absolute? - if ( const char* env_pwd = getenv("PWD") ) + const char* env_pwd; + if ( (env_pwd = getenv("PWD")) ) { if ( !is_proper_absolute_path(env_pwd) ) { @@ -2092,7 +2102,8 @@ int main(int argc, char* argv[]) break; if ( arg[0] == '+' ) { - while ( char c = *++arg ) switch ( c ) + char c; + while ( (c = *++arg) ) switch ( c ) { case 'e': flag_e_exit_on_error = false; break; default: @@ -2103,7 +2114,8 @@ int main(int argc, char* argv[]) } else if ( arg[1] != '-' ) { - while ( char c = *++arg ) switch ( c ) + char c; + while ( (c = *++arg) ) switch ( c ) { case 'c': flag_c_first_operand_is_command = true; break; case 'e': flag_e_exit_on_error = true; break; diff --git a/sh/showline.cpp b/sh/showline.c similarity index 99% rename from sh/showline.cpp rename to sh/showline.c index b37afe99..f7a9d713 100644 --- a/sh/showline.cpp +++ b/sh/showline.c @@ -15,12 +15,13 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - showline.cpp + showline.c Display a line on the terminal. *******************************************************************************/ #include +#include #include #include #include diff --git a/sh/util.cpp b/sh/util.c similarity index 94% rename from sh/util.cpp rename to sh/util.c index ea030bd0..c782c518 100644 --- a/sh/util.cpp +++ b/sh/util.c @@ -15,11 +15,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - util.cpp + util.c Utility functions. *******************************************************************************/ +#include #include #include @@ -30,12 +31,17 @@ char* strdup_safe(const char* string) return string ? strdup(string) : NULL; } -const char* getenv_safe(const char* name, const char* def) +const char* getenv_safe_def(const char* name, const char* def) { const char* ret = getenv(name); return ret ? ret : def; } +const char* getenv_safe(const char* name) +{ + return getenv_safe_def(name, ""); +} + bool array_add(void*** array_ptr, size_t* used_ptr, size_t* length_ptr, diff --git a/sh/util.h b/sh/util.h index 91729240..3a6534f3 100644 --- a/sh/util.h +++ b/sh/util.h @@ -24,7 +24,8 @@ #define UTIL_H char* strdup_safe(const char* string); -const char* getenv_safe(const char* name, const char* def = ""); +const char* getenv_safe_def(const char* name, const char* def); +const char* getenv_safe(const char* name); bool array_add(void*** array_ptr, size_t* used_ptr, size_t* length_ptr,