Permalink
Browse files

SERVER-5246 Move linenoise out of third_party

Remove unused files, move linenoise source files
into src/mongo/shell.
  • Loading branch information...
1 parent 6999e38 commit 3126c21d8acb267a4d9fca4169927ccd0f7a29e8 @tadmarshall tadmarshall committed May 4, 2012
View
6 src/mongo/SConscript
@@ -419,9 +419,9 @@ coreShellFiles = [ "shell/dbshell.cpp",
"shell/shell_utils_extended.cpp",
"shell/shell_utils_launcher.cpp",
"shell/mongo-server.cpp",
- "../third_party/linenoise/linenoise.cpp",
- "../third_party/linenoise/linenoise_utf8.cpp",
- "../third_party/linenoise/mk_wcwidth.cpp"]
+ "shell/linenoise.cpp",
+ "shell/linenoise_utf8.cpp",
+ "shell/mk_wcwidth.cpp"]
if shellEnv is not None:
mongo_shell = shellEnv.Program(
View
2 src/mongo/shell/dbshell.cpp
@@ -26,6 +26,7 @@
#include <boost/filesystem/operations.hpp>
+#include "linenoise.h"
#include "mongo/client/dbclientinterface.h"
#include "mongo/db/cmdline.h"
#include "mongo/db/repl/rs_member.h"
@@ -37,7 +38,6 @@
#include "mongo/util/startup_test.h"
#include "mongo/util/util.h"
#include "mongo/util/version.h"
-#include "third_party/linenoise/linenoise.h"
using namespace std;
using namespace mongo;
View
0 src/third_party/linenoise/linenoise.cpp → src/mongo/shell/linenoise.cpp
File renamed without changes.
View
0 src/third_party/linenoise/linenoise.h → src/mongo/shell/linenoise.h
File renamed without changes.
View
0 src/third_party/linenoise/linenoise_utf8.cpp → src/mongo/shell/linenoise_utf8.cpp
File renamed without changes.
View
0 src/third_party/linenoise/linenoise_utf8.h → src/mongo/shell/linenoise_utf8.h
File renamed without changes.
View
0 src/third_party/linenoise/mk_wcwidth.cpp → src/mongo/shell/mk_wcwidth.cpp
File renamed without changes.
View
0 src/third_party/linenoise/mk_wcwidth.h → src/mongo/shell/mk_wcwidth.h
File renamed without changes.
View
12 src/mongo/shell/mongo.vcxproj
@@ -1148,7 +1148,7 @@ cscript //Nologo createCPPfromJavaScriptFiles.js "$(ProjectDir).."</Command>
<DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Win2008PlusDebug|x64'">4355;4800;4267;4244;4334</DisableSpecificWarnings>
<DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Win2008PlusRelease|x64'">4355;4800;4267;4244;4334</DisableSpecificWarnings>
</ClCompile>
- <ClCompile Include="..\..\third_party\linenoise\linenoise_utf8.cpp">
+ <ClCompile Include="linenoise_utf8.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
@@ -1158,7 +1158,7 @@ cscript //Nologo createCPPfromJavaScriptFiles.js "$(ProjectDir).."</Command>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win2008PlusRelease|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win2008PlusRelease|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
- <ClCompile Include="..\..\third_party\linenoise\mk_wcwidth.cpp">
+ <ClCompile Include="mk_wcwidth.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
@@ -2063,7 +2063,7 @@ cscript //Nologo createCPPfromJavaScriptFiles.js "$(ProjectDir).."</Command>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Win2008PlusRelease|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\scripting\utils.cpp" />
- <ClCompile Include="..\..\third_party\linenoise\linenoise.cpp">
+ <ClCompile Include="linenoise.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win2008PlusDebug|Win32'">NotUsing</PrecompiledHeader>
@@ -2222,8 +2222,8 @@ cscript //Nologo createCPPfromJavaScriptFiles.js "$(ProjectDir).."</Command>
<ClInclude Include="..\..\third_party\js-1.7\jsxml.h" />
<ClInclude Include="..\..\third_party\js-1.7\prmjtime.h" />
<ClInclude Include="..\..\third_party\js-1.7\resource.h" />
- <ClInclude Include="..\..\third_party\linenoise\linenoise_utf8.h" />
- <ClInclude Include="..\..\third_party\linenoise\mk_wcwidth.h" />
+ <ClInclude Include="linenoise_utf8.h" />
+ <ClInclude Include="mk_wcwidth.h" />
<ClInclude Include="..\..\third_party\pcre-8.30\config.h" />
<ClInclude Include="..\..\third_party\pcre-8.30\pcre.h" />
<ClInclude Include="..\..\third_party\pcre-8.30\pcrecpp.h" />
@@ -2284,7 +2284,7 @@ cscript //Nologo createCPPfromJavaScriptFiles.js "$(ProjectDir).."</Command>
<ClInclude Include="..\db\json.h" />
<ClInclude Include="..\db\key.h" />
<ClInclude Include="..\db\lasterror.h" />
- <ClInclude Include="..\..\third_party\linenoise\linenoise.h" />
+ <ClInclude Include="linenoise.h" />
<ClInclude Include="..\db\matcher.h" />
<ClInclude Include="..\db\mongommf.h" />
<ClInclude Include="..\db\mongomutex.h" />
View
41 src/mongo/shell/mongo.vcxproj.filters
@@ -35,9 +35,6 @@
<Filter Include="third_party">
<UniqueIdentifier>{b68efe41-5e2c-4bef-b538-5265a839fc7a}</UniqueIdentifier>
</Filter>
- <Filter Include="third_party\linenoise">
- <UniqueIdentifier>{2faba381-6961-4e3d-ad8b-086a141bd35b}</UniqueIdentifier>
- </Filter>
<Filter Include="third_party\pcre">
<UniqueIdentifier>{291e0d72-13ca-42d7-b0fd-2e7b5f89639f}</UniqueIdentifier>
</Filter>
@@ -92,12 +89,6 @@
<Filter Include="util\Source Files">
<UniqueIdentifier>{6603da1a-0530-4d55-aff7-61c46a010198}</UniqueIdentifier>
</Filter>
- <Filter Include="third_party\linenoise\Source Files">
- <UniqueIdentifier>{8ef72b9e-4e59-4b7f-a795-e4cd882e64aa}</UniqueIdentifier>
- </Filter>
- <Filter Include="third_party\linenoise\Header Files">
- <UniqueIdentifier>{26507f26-0762-4559-9e59-c053daab9e33}</UniqueIdentifier>
- </Filter>
<Filter Include="db\Header Files">
<UniqueIdentifier>{5d0b1ae0-ec8a-4c09-9cac-6f7b0de60de9}</UniqueIdentifier>
</Filter>
@@ -503,17 +494,17 @@
<ClCompile Include="shell_utils_launcher.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="..\..\third_party\linenoise\linenoise.cpp">
- <Filter>third_party\linenoise\Source Files</Filter>
+ <ClCompile Include="..\util\signal_handlers.cpp">
+ <Filter>util\Source Files</Filter>
</ClCompile>
- <ClCompile Include="..\..\third_party\linenoise\linenoise_utf8.cpp">
- <Filter>third_party\linenoise\Source Files</Filter>
+ <ClCompile Include="linenoise.cpp">
+ <Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="..\..\third_party\linenoise\mk_wcwidth.cpp">
- <Filter>third_party\linenoise\Source Files</Filter>
+ <ClCompile Include="linenoise_utf8.cpp">
+ <Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="..\util\signal_handlers.cpp">
- <Filter>util\Source Files</Filter>
+ <ClCompile Include="mk_wcwidth.cpp">
+ <Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
@@ -1160,17 +1151,17 @@
<ClInclude Include="shell_utils_extended.h">
<Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="..\..\third_party\linenoise\linenoise.h">
- <Filter>third_party\linenoise\Header Files</Filter>
+ <ClInclude Include="..\util\signal_handlers.h">
+ <Filter>util\Header Files</Filter>
</ClInclude>
- <ClInclude Include="..\..\third_party\linenoise\mk_wcwidth.h">
- <Filter>third_party\linenoise\Header Files</Filter>
+ <ClInclude Include="linenoise.h">
+ <Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="..\..\third_party\linenoise\linenoise_utf8.h">
- <Filter>third_party\linenoise\Header Files</Filter>
+ <ClInclude Include="linenoise_utf8.h">
+ <Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="..\util\signal_handlers.h">
- <Filter>util\Header Files</Filter>
+ <ClInclude Include="mk_wcwidth.h">
+ <Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
View
6 src/third_party/README
@@ -1,6 +0,0 @@
-
-linenoise
- when making changes here, also publish to
- http://github.com/erh/linenoise
- 6cdc775807e57b2c3fd64bd207814f8ee1fe35f3
-
View
7 src/third_party/linenoise/Makefile
@@ -1,7 +0,0 @@
-linenoise_example: linenoise.h linenoise.cpp
-
-linenoise_example: linenoise.cpp example.c
- $(CXX) -Wall -W -Os -g -o linenoise_example linenoise.cpp example.c
-
-clean:
- rm -f linenoise_example
View
47 src/third_party/linenoise/README.markdown
@@ -1,47 +0,0 @@
-# Linenoise
-
-A minimal, zero-config, BSD licensed, readline replacement.
-
-News: linenoise now includes minimal completion support, thanks to Pieter Noordhuis (@pnoordhuis).
-
-News: linenoise is now part of [Android](http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=liblinenoise;h=56450eaed7f783760e5e6a5993ef75cde2e29dea;hb=HEAD Android)!
-
-## Can a line editing library be 20k lines of code?
-
-Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing?
-
-So what usually happens is either:
-
- * Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (Readl world example of this problem: Tclsh).
- * Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance).
-
-The result is a pollution of binaries without line editing support.
-
-So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporing line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to linenoise if not.
-
-## Terminals, in 2010.
-
-Apparently almost every terminal you can happen to use today has some kind of support for VT100 alike escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it.
-
-Since it's so young I guess there are a few bugs, or the lib may not compile or work with some operating system, but it's a matter of a few weeks and eventually we'll get it right, and there will be no excuses for not shipping command line tools without built-in line editing support.
-
-The library is currently less than 400 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software.
-
-## Tested with...
-
- * Linux text only console ($TERM = linux)
- * Linux KDE terminal application ($TERM = xterm)
- * Linux xterm ($TERM = xterm)
- * Mac OS X iTerm ($TERM = xterm)
- * Mac OS X default Terminal.app ($TERM = xterm)
- * OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
- * IBM AIX 6.1
- * FreeBSD xterm ($TERM = xterm)
-
-Please test it everywhere you can and report back!
-
-## Let's push this forward!
-
-Please fork it and add something interesting and send me a pull request. What's especially interesting are fixes, new key bindings, completion.
-
-Send feedbacks to antirez at gmail
View
27 src/third_party/linenoise/example.c
@@ -1,27 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include "linenoise.h"
-
-
-void completion(const char *buf, linenoiseCompletions *lc) {
- if (buf[0] == 'h') {
- linenoiseAddCompletion(lc,"hello");
- linenoiseAddCompletion(lc,"hello there");
- }
-}
-
-int main(void) {
- char *line;
-
- linenoiseSetCompletionCallback(completion);
- linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
- while((line = linenoise("hello> ")) != NULL) {
- if (line[0] != '\0') {
- printf("echo: '%s'\n", line);
- linenoiseHistoryAdd(line);
- linenoiseHistorySave("history.txt"); /* Save every new entry */
- }
- free(line);
- }
- return 0;
-}
View
3 src/third_party/linenoise/history.txt
@@ -1,3 +0,0 @@
-hi
-this is fun
-hel
View
442 src/third_party/linenoise/linenoise_win32.cpp
@@ -1,442 +0,0 @@
-/* linenoise_win32.c -- Linenoise win32 port.
- *
- * Modifications copyright 2010, Jon Griffiths <jon_p_griffiths at yahoo dot com>.
- * All rights reserved.
- * Based on linenoise, copyright 2010, Salvatore Sanfilippo <antirez at gmail dot com>.
- * The original linenoise can be found at: http://github.com/antirez/linenoise
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of Redis nor the names of its contributors may be used
- * to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * Todo list:
- * Actually switch to/from raw mode so emacs key combos work.
- * Set a console handler to clean up onn exit.
- */
-#include <conio.h>
-#include <windows.h>
-#include <stdio.h>
-
-/* If ALT_KEYS is defined, emacs key combos using ALT instead of CTRL are
- * available. At this time, you don't get key repeats when enabled though. */
-/* #define ALT_KEYS */
-
-static HANDLE console_in, console_out;
-
-#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
-#define LINENOISE_MAX_LINE 4096
-
-static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
-static int history_len = 0;
-char** history = NULL;
-
-int linenoiseHistoryAdd(const char* line);
-
-static int enableRawMode()
-{
- if (!console_in)
- {
- console_in = GetStdHandle(STD_INPUT_HANDLE);
- console_out = GetStdHandle(STD_OUTPUT_HANDLE);
- }
- return 0;
-}
-
-static void disableRawMode()
-{
- /* Nothing to do yet */
-}
-
-static void output(const char* str,
- size_t len,
- int x,
- int y)
-{
- COORD pos = { (SHORT)x, (SHORT)y };
- DWORD count = 0;
- WriteConsoleOutputCharacterA(console_out, str, len, pos, &count);
-}
-
-static void refreshLine(const char* prompt,
- char* buf,
- size_t len,
- size_t pos,
- size_t cols)
-{
- size_t plen = strlen(prompt);
-
- while ((plen + pos) >= cols)
- {
- buf++;
- len--;
- pos--;
- }
- while (plen + len > cols)
- {
- len--;
- }
-
- CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
- GetConsoleScreenBufferInfo(console_out, &inf);
- size_t prompt_len = strlen(prompt);
- output(prompt, prompt_len, 0, inf.dwCursorPosition.Y);
- output(buf, len, prompt_len, inf.dwCursorPosition.Y);
- if (prompt_len + len < (size_t)inf.dwSize.X)
- {
- /* Blank to EOL */
- char* tmp = (char*)malloc(inf.dwSize.X - (prompt_len + len));
- memset(tmp, ' ', inf.dwSize.X - (prompt_len + len));
- output(tmp, inf.dwSize.X - (prompt_len + len), len + prompt_len, inf.dwCursorPosition.Y);
- free(tmp);
- }
- inf.dwCursorPosition.X = (SHORT)(pos + prompt_len);
- SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
-}
-
-static int linenoisePrompt(char* buf,
- size_t buflen,
- const char* prompt)
-{
- size_t plen = strlen(prompt);
- size_t pos = 0;
- size_t len = 0;
- int history_index = 0;
-#ifdef ALT_KEYS
- unsigned char last_down = 0;
-#endif
- buf[0] = '\0';
- buflen--; /* Make sure there is always space for the nulterm */
-
- /* The latest history entry is always our current buffer, that
- * initially is just an empty string. */
- linenoiseHistoryAdd("");
-
- CONSOLE_SCREEN_BUFFER_INFO inf = { 0 };
- GetConsoleScreenBufferInfo(console_out, &inf);
- size_t cols = inf.dwSize.X;
- output(prompt, plen, 0, inf.dwCursorPosition.Y);
- inf.dwCursorPosition.X = (SHORT)plen;
- SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
-
- for ( ; ; )
- {
- INPUT_RECORD rec;
- DWORD count;
- ReadConsoleInputA(console_in, &rec, 1, &count);
- if (rec.EventType != KEY_EVENT)
- continue;
-#ifdef ALT_KEYS
- if (rec.Event.KeyEvent.bKeyDown)
- {
- last_down = rec.Event.KeyEvent.uChar.AsciiChar;
- continue;
- }
-#else
- if (!rec.Event.KeyEvent.bKeyDown)
- {
- continue;
- }
-#endif
- switch (rec.Event.KeyEvent.wVirtualKeyCode)
- {
- case VK_RETURN: /* enter */
- history_len--;
- free(history[history_len]);
- return (int)len;
- case VK_BACK: /* backspace */
-#ifdef ALT_KEYS
- backspace:
-#endif
- if (pos > 0 && len > 0)
- {
- memmove(buf + pos - 1, buf + pos, len - pos);
- pos--;
- len--;
- buf[len] = '\0';
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case VK_LEFT:
-#ifdef ALT_KEYS
- left_arrow:
-#endif
- /* left arrow */
- if (pos > 0)
- {
- pos--;
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case VK_RIGHT:
-#ifdef ALT_KEYS
- right_arrow:
-#endif
- /* right arrow */
- if (pos != len)
- {
- pos++;
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case VK_UP:
- case VK_DOWN:
-#ifdef ALT_KEYS
- up_down_arrow:
-#endif
- /* up and down arrow: history */
- if (history_len > 1)
- {
- /* Update the current history entry before to
- * overwrite it with tne next one. */
- free(history[history_len - 1 - history_index]);
- history[history_len - 1 - history_index] = _strdup(buf);
- /* Show the new entry */
- history_index += (rec.Event.KeyEvent.wVirtualKeyCode == VK_UP) ? 1 : -1;
- if (history_index < 0)
- {
- history_index = 0;
- break;
- }
- else if (history_index >= history_len)
- {
- history_index = history_len - 1;
- break;
- }
- strncpy(buf, history[history_len - 1 - history_index], buflen);
- buf[buflen] = '\0';
- len = pos = strlen(buf);
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case VK_DELETE:
- /* delete */
- if (len > 0 && pos < len)
- {
- memmove(buf + pos, buf + pos + 1, len - pos - 1);
- len--;
- buf[len] = '\0';
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case VK_HOME: /* Ctrl+a, go to the start of the line */
-#ifdef ALT_KEYS
- home:
-#endif
- pos = 0;
- refreshLine(prompt, buf, len, pos, cols);
- break;
- case VK_END: /* ctrl+e, go to the end of the line */
-#ifdef ALT_KEYS
- end:
-#endif
- pos = len;
- refreshLine(prompt, buf, len, pos, cols);
- break;
- default:
-#ifdef ALT_KEYS
- /* Use alt instead of CTRL since windows eats CTRL+char combos */
- if (rec.Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
- {
- switch (last_down)
- {
- case 'a': /* ctrl-t */
- goto home;
- case 'e': /* ctrl-t */
- goto end;
- case 't': /* ctrl-t */
- if (pos > 0 && pos < len)
- {
- int aux = buf[pos - 1];
- buf[pos - 1] = buf[pos];
- buf[pos] = aux;
- if (pos != len - 1)
- pos++;
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- case 'h': /* ctrl-h */
- goto backspace;
- case 'b': /* ctrl-b */
- goto left_arrow;
- case 'f': /* ctrl-f */
- goto right_arrow;
- case 'p': /* ctrl-p */
- rec.Event.KeyEvent.wVirtualKeyCode = VK_UP;
- goto up_down_arrow;
- case 'n': /* ctrl-n */
- rec.Event.KeyEvent.wVirtualKeyCode = VK_DOWN;
- goto up_down_arrow;
- case 'u': /* Ctrl+u, delete the whole line. */
- buf[0] = '\0';
- pos = len = 0;
- refreshLine(prompt, buf, len, pos, cols);
- break;
- case 'k': /* Ctrl+k, delete from current to end of line. */
- buf[pos] = '\0';
- len = pos;
- refreshLine(prompt, buf, len, pos, cols);
- break;
- }
- continue;
- }
-#endif /* ALT_KEYS */
- if (rec.Event.KeyEvent.uChar.AsciiChar < ' ' ||
- rec.Event.KeyEvent.uChar.AsciiChar > '~')
- continue;
-
- if (len < buflen)
- {
- if (len != pos)
- memmove(buf + pos + 1, buf + pos, len - pos);
- buf[pos] = rec.Event.KeyEvent.uChar.AsciiChar;
- len++;
- pos++;
- buf[len] = '\0';
- refreshLine(prompt, buf, len, pos, cols);
- }
- break;
- }
- }
-}
-
-static int linenoiseRaw(char* buf,
- size_t buflen,
- const char* prompt)
-{
- int count = -1;
-
- if (buflen != 0)
- {
- if (enableRawMode() == -1)
- return -1;
- count = linenoisePrompt(buf, buflen, prompt);
- disableRawMode();
- printf("\n");
- }
- return count;
-}
-
-char* linenoise(const char* prompt)
-{
- char buf[LINENOISE_MAX_LINE];
- int count = linenoiseRaw(buf, LINENOISE_MAX_LINE, prompt);
- if (count == -1)
- return NULL;
- return _strdup(buf);
-}
-
-/* Using a circular buffer is smarter, but a bit more complex to handle. */
-int linenoiseHistoryAdd(const char* line)
-{
- char* linecopy;
-
- if (history_max_len == 0)
- return 0;
- if (history == NULL)
- {
- history = (char**)malloc(sizeof(char*) * history_max_len);
- if (history == NULL)
- return 0;
- memset(history, 0, (sizeof(char*) * history_max_len));
- }
- linecopy = _strdup(line);
- if (!linecopy)
- return 0;
- if (history_len == history_max_len)
- {
- free(history[0]);
- memmove(history, history + 1, sizeof(char*) * (history_max_len - 1));
- history_len--;
- }
- history[history_len] = linecopy;
- history_len++;
- return 1;
-}
-
-int linenoiseHistorySetMaxLen(int len)
-{
- char** new_history;
-
- if (len < 1)
- return 0;
- if (history)
- {
- int tocopy = history_len;
-
- new_history = (char**)malloc(sizeof(char*) * len);
- if (new_history == NULL)
- return 0;
- if (len < tocopy)
- tocopy = len;
- memcpy(new_history, history + (history_max_len - tocopy), sizeof(char*) * tocopy);
- free(history);
- history = new_history;
- }
- history_max_len = len;
- if (history_len > history_max_len)
- history_len = history_max_len;
- return 1;
-}
-
-/* Save the history in the specified file. On success 0 is returned
- * otherwise -1 is returned. */
-int linenoiseHistorySave(const char* filename)
-{
- FILE* fp = fopen(filename, "w");
- int j;
-
- if (fp == NULL)
- return -1;
- for (j = 0; j < history_len; j++)
- fprintf(fp, "%s\n", history[j]);
- fclose(fp);
- return 0;
-}
-
-/* Load the history from the specified file. If the file does not exist
- * zero is returned and no operation is performed.
- *
- * If the file exists and the operation succeeded 0 is returned, otherwise
- * on error -1 is returned. */
-int linenoiseHistoryLoad(const char* filename)
-{
- FILE* fp = fopen(filename, "r");
- char buf[LINENOISE_MAX_LINE];
-
- if (fp == NULL)
- return -1;
-
- while (fgets(buf, LINENOISE_MAX_LINE, fp) != NULL)
- {
- char* p;
-
- p = strchr(buf, '\r');
- if (!p)
- p = strchr(buf, '\n');
- if (p)
- *p = '\0';
- linenoiseHistoryAdd(buf);
- }
- fclose(fp);
- return 0;
-}

0 comments on commit 3126c21

Please sign in to comment.