aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/vendor/linenoise/linenoise.c
blob: a5b8ac5f68005ef718719d18f3157684a4577967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "linenoise.h"

#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#define LINENOISE_MAX_LINE 4096
#define LINENOISE_MAX_PROMPT 128

typedef struct linenoiseHistory {
    char **items;
    int len;
    int cap;
    int maxLen;
} linenoiseHistory;

static linenoiseCompletionCallback *g_completionCallback = NULL;
static volatile LONG g_stopRequested = 0;
static linenoiseHistory g_history = { NULL, 0, 0, 128 };
/* Guards redraw/log interleaving so prompt and log lines do not overlap. */
static CRITICAL_SECTION g_ioLock;
static volatile LONG g_ioLockState = 0; /* 0=not init, 1=init in progress, 2=ready */
/* Snapshot of current editor line used to restore prompt after external output. */
static volatile LONG g_editorActive = 0;
static char g_editorPrompt[LINENOISE_MAX_PROMPT] = { 0 };
static char g_editorBuf[LINENOISE_MAX_LINE] = { 0 };
static int g_editorLen = 0;
static int g_editorPos = 0;
static int g_editorPrevLen = 0;

/**
 * Lazily initialize the console I/O critical section.
 * This avoids static init order issues and keeps startup cost minimal.
 */
static void linenoiseEnsureIoLockInit(void)
{
    LONG state = InterlockedCompareExchange(&g_ioLockState, 0, 0);
    if (state == 2)
        return;

    if (state == 0 && InterlockedCompareExchange(&g_ioLockState, 1, 0) == 0)
    {
        InitializeCriticalSection(&g_ioLock);
        InterlockedExchange(&g_ioLockState, 2);
        return;
    }

    while (InterlockedCompareExchange(&g_ioLockState, 0, 0) != 2)
    {
        Sleep(0);
    }
}

static void linenoiseLockIo(void)
{
    linenoiseEnsureIoLockInit();
    EnterCriticalSection(&g_ioLock);
}

static void linenoiseUnlockIo(void)
{
    LeaveCriticalSection(&g_ioLock);
}

/**
 * Save current prompt/buffer/cursor state for later redraw.
 * Called after each redraw while editor is active.
 */
static void linenoiseUpdateEditorState(const char *prompt, const char *buf, int len, int pos, int prevLen)
{
    if (prompt == NULL)
        prompt = "";
    if (buf == NULL)
        buf = "";

    strncpy_s(g_editorPrompt, sizeof(g_editorPrompt), prompt, _TRUNCATE);
    strncpy_s(g_editorBuf, sizeof(g_editorBuf), buf, _TRUNCATE);
    g_editorLen = len;
    g_editorPos = pos;
    g_editorPrevLen = prevLen;
    InterlockedExchange(&g_editorActive, 1);
}

static void linenoiseDeactivateEditorState(void)
{
    InterlockedExchange(&g_editorActive, 0);
    g_editorPrompt[0] = 0;
    g_editorBuf[0] = 0;
    g_editorLen = 0;
    g_editorPos = 0;
    g_editorPrevLen = 0;
}

static char *linenoiseStrdup(const char *src)
{
    size_t n = strlen(src) + 1;
    char *out = (char *)malloc(n);
    if (out == NULL)
        return NULL;
    memcpy(out, src, n);
    return out;
}

static void linenoiseEnsureHistoryCapacity(int wanted)
{
    if (wanted <= g_history.cap)
        return;

    int newCap = g_history.cap == 0 ? 32 : g_history.cap;
    while (newCap < wanted)
        newCap *= 2;

    char **newItems = (char **)realloc(g_history.items, sizeof(char *) * (size_t)newCap);
    if (newItems == NULL)
        return;

    g_history.items = newItems;
    g_history.cap = newCap;
}

static void linenoiseClearCompletions(linenoiseCompletions *lc)
{
    size_t i = 0;
    for (i = 0; i < lc->len; ++i)
    {
        free(lc->cvec[i]);
    }
    free(lc->cvec);
    lc->cvec = NULL;
    lc->len = 0;
}

void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str)
{
    char **newVec = (char **)realloc(lc->cvec, sizeof(char *) * (lc->len + 1));
    if (newVec == NULL)
        return;

    lc->cvec = newVec;
    lc->cvec[lc->len] = linenoiseStrdup(str);
    if (lc->cvec[lc->len] == NULL)
        return;

    lc->len += 1;
}

void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn)
{
    g_completionCallback = fn;
}

void linenoiseFree(void *ptr)
{
    free(ptr);
}

int linenoiseHistorySetMaxLen(int len)
{
    if (len <= 0)
        return 0;

    g_history.maxLen = len;
    while (g_history.len > g_history.maxLen)
    {
        free(g_history.items[0]);
        memmove(g_history.items, g_history.items + 1, sizeof(char *) * (size_t)(g_history.len - 1));
        g_history.len -= 1;
    }

    return 1;
}

int linenoiseHistoryAdd(const char *line)
{
    if (line == NULL || line[0] == 0)
        return 0;

    if (g_history.len > 0)
    {
        const char *last = g_history.items[g_history.len - 1];
        if (last != NULL && strcmp(last, line) == 0)
            return 1;
        }

    linenoiseEnsureHistoryCapacity(g_history.len + 1);
    if (g_history.cap <= g_history.len)
        return 0;

    g_history.items[g_history.len] = linenoiseStrdup(line);
    if (g_history.items[g_history.len] == NULL)
        return 0;

    g_history.len += 1;

    while (g_history.len > g_history.maxLen)
    {
        free(g_history.items[0]);
        memmove(g_history.items, g_history.items + 1, sizeof(char *) * (size_t)(g_history.len - 1));
        g_history.len -= 1;
    }

    return 1;
}

void linenoiseRequestStop(void)
{
    InterlockedExchange(&g_stopRequested, 1);
}

void linenoiseResetStop(void)
{
    InterlockedExchange(&g_stopRequested, 0);
}

static int linenoiseIsStopRequested(void)
{
    return InterlockedCompareExchange(&g_stopRequested, 0, 0) != 0;
}

static void linenoiseWriteHint(const char *hint, size_t hintLen)
{
    HANDLE stdoutHandle;
    CONSOLE_SCREEN_BUFFER_INFO originalInfo;
    int hasColorConsole = 0;

    if (hint == NULL || hintLen == 0)
    {
        return;
    }

    stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    if (stdoutHandle != INVALID_HANDLE_VALUE && stdoutHandle != NULL)
    {
        if (GetConsoleScreenBufferInfo(stdoutHandle, &originalInfo))
        {
            hasColorConsole = 1;
            /* Draw predictive tail in dim gray, then restore original console colors. */
            SetConsoleTextAttribute(stdoutHandle, FOREGROUND_INTENSITY);
        }
    }

    fwrite(hint, 1, hintLen, stdout);

    if (hasColorConsole)
    {
        SetConsoleTextAttribute(stdoutHandle, originalInfo.wAttributes);
    }
}

static int linenoiseStartsWithIgnoreCase(const char *full, const char *prefix)
{
    while (*prefix != 0)
    {
        if (tolower((unsigned char)*full) != tolower((unsigned char)*prefix))
        {
            return 0;
        }
        ++full;
        ++prefix;
    }
    return 1;
}

static size_t linenoiseBuildHint(const char *buf, char *hint, size_t hintSize)
{
    linenoiseCompletions lc;
    size_t inputLen = 0;
    size_t i = 0;

    if (hint == NULL || hintSize == 0)
    {
        return 0;
    }
    hint[0] = 0;

    if (buf == NULL || buf[0] == 0 || g_completionCallback == NULL)
    {
        return 0;
    }

    lc.len = 0;
    lc.cvec = NULL;
    /* Reuse the completion callback and derive a "ghost text" suffix from the first extending match. */
    g_completionCallback(buf, &lc);

    inputLen = strlen(buf);
    for (i = 0; i < lc.len; ++i)
    {
        const char *candidate = lc.cvec[i];
        if (candidate == NULL)
        {
            continue;
        }
        if (strlen(candidate) <= inputLen)
        {
            continue;
        }
        if (!linenoiseStartsWithIgnoreCase(candidate, buf))
        {
            continue;
        }

        /* Keep only the part not yet typed by the user (rendered as hint text). */
        strncpy_s(hint, hintSize, candidate + inputLen, _TRUNCATE);
        break;
    }

    linenoiseClearCompletions(&lc);
    return strlen(hint);
}

static void linenoiseRedrawUnsafe(const char *prompt, const char *buf, int len, int pos, int *prevLen)
{
    int i;
    char hint[LINENOISE_MAX_LINE] = {0};
    int renderedLen = len;
    /* Hint length contributes to the rendered width so stale tail characters can be cleared correctly. */
    int hintLen = (int)linenoiseBuildHint(buf, hint, sizeof(hint));
    if (hintLen > 0)
    {
        renderedLen += hintLen;
    }

    fputc('\r', stdout);
    fputs(prompt, stdout);
    if (len > 0)
    {
        fwrite(buf, 1, (size_t)len, stdout);
    }
    if (hintLen > 0)
    {
        linenoiseWriteHint(hint, (size_t)hintLen);
    }

    if (*prevLen > renderedLen)
    {
        for (i = renderedLen; i < *prevLen; ++i)
        {
            fputc(' ', stdout);
        }
    }

    fputc('\r', stdout);
    fputs(prompt, stdout);
    if (pos > 0)
    {
        /* Cursor positioning reflects only real user input, not the ghost hint suffix. */
        fwrite(buf, 1, (size_t)pos, stdout);
    }

    fflush(stdout);
    *prevLen = renderedLen;
    linenoiseUpdateEditorState(prompt, buf, len, pos, *prevLen);
}

static void linenoiseRedraw(const char *prompt, const char *buf, int len, int pos, int *prevLen)
{
    linenoiseLockIo();
    linenoiseRedrawUnsafe(prompt, buf, len, pos, prevLen);
    linenoiseUnlockIo();
}

static int linenoiseStartsWith(const char *full, const char *prefix)
{
    while (*prefix != 0)
    {
        if (*full != *prefix)
            return 0;
        ++full;
        ++prefix;
    }
    return 1;
}

static int linenoiseComputeCommonPrefix(const linenoiseCompletions *lc, const char *seed, char *out, size_t outSize)
{
    size_t commonLen = 0;
    size_t i;

    if (lc->len == 0 || outSize == 0)
        return 0;

    strncpy_s(out, outSize, lc->cvec[0], _TRUNCATE);
    commonLen = strlen(out);

    for (i = 1; i < lc->len; ++i)
    {
        const char *candidate = lc->cvec[i];
        size_t j = 0;

        while (j < commonLen && out[j] != 0 && candidate[j] != 0 && out[j] == candidate[j])
            ++j;

        commonLen = j;
        out[commonLen] = 0;

        if (commonLen == 0)
            break;
    }

    if (strlen(out) <= strlen(seed))
        return 0;

    return linenoiseStartsWith(out, seed);
}

static void linenoiseApplyCompletion(const char *prompt, char *buf, int *len, int *pos, int *prevLen)
{
    linenoiseCompletions lc;
    int i;

    if (g_completionCallback == NULL)
    {
        Beep(750, 15);
        return;
    }

    lc.len = 0;
    lc.cvec = NULL;
    g_completionCallback(buf, &lc);

    if (lc.len == 0)
    {
        Beep(750, 15);
        linenoiseClearCompletions(&lc);
        return;
    }

    if (lc.len == 1)
    {
        strncpy_s(buf, LINENOISE_MAX_LINE, lc.cvec[0], _TRUNCATE);
        *len = (int)strlen(buf);
        *pos = *len;
        linenoiseRedraw(prompt, buf, *len, *pos, prevLen);
        linenoiseClearCompletions(&lc);
        return;
    }

    {
        char common[LINENOISE_MAX_LINE] = { 0 };
        if (linenoiseComputeCommonPrefix(&lc, buf, common, sizeof(common)))
        {
            strncpy_s(buf, LINENOISE_MAX_LINE, common, _TRUNCATE);
            *len = (int)strlen(buf);
            *pos = *len;
            linenoiseRedraw(prompt, buf, *len, *pos, prevLen);
        }
    }

    linenoiseLockIo();
    fputc('\n', stdout);
    for (i = 0; i < (int)lc.len; ++i)
    {
        fputs(lc.cvec[i], stdout);
        fputs("  ", stdout);
    }
    fputc('\n', stdout);
    linenoiseRedrawUnsafe(prompt, buf, *len, *pos, prevLen);
    linenoiseUnlockIo();
    linenoiseClearCompletions(&lc);
}

char *linenoise(const char *prompt)
{
    char buf[LINENOISE_MAX_LINE];
    int len = 0;
    int pos = 0;
    int prevLen = 0;
    int historyIndex = g_history.len;

    if (prompt == NULL)
        prompt = "";

    buf[0] = 0;
    linenoiseLockIo();
    linenoiseUpdateEditorState(prompt, buf, len, pos, prevLen);
    fputs(prompt, stdout);
    fflush(stdout);
    linenoiseUnlockIo();

    while (!linenoiseIsStopRequested())
    {
        if (!_kbhit())
        {
            Sleep(10);
            continue;
        }

        {
            int c = _getwch();

            if (c == 0 || c == 224)
            {
                int ext = _getwch();
                if (ext == 72)
                {
                    if (g_history.len > 0 && historyIndex > 0)
                    {
                        historyIndex -= 1;
                        strncpy_s(buf, sizeof(buf), g_history.items[historyIndex], _TRUNCATE);
                        len = (int)strlen(buf);
                        pos = len;
                        linenoiseRedraw(prompt, buf, len, pos, &prevLen);
                    }
                }
                else if (ext == 80)
                {
                    if (g_history.len > 0 && historyIndex < g_history.len)
                    {
                        historyIndex += 1;
                        if (historyIndex == g_history.len)
                            buf[0] = 0;
                        else
                            strncpy_s(buf, sizeof(buf), g_history.items[historyIndex], _TRUNCATE);

                        len = (int)strlen(buf);
                        pos = len;
                        linenoiseRedraw(prompt, buf, len, pos, &prevLen);
                    }
                }
                else if (ext == 75)
                {
                    if (pos > 0)
                    {
                        pos -= 1;
                        linenoiseRedraw(prompt, buf, len, pos, &prevLen);
                    }
                }
                else if (ext == 77)
                {
                    if (pos < len)
                    {
                        pos += 1;
                        linenoiseRedraw(prompt, buf, len, pos, &prevLen);
                    }
                }
                continue;
            }

            if (c == 3)
            {
                linenoiseLockIo();
                linenoiseDeactivateEditorState();
                fputc('\n', stdout);
                fflush(stdout);
                linenoiseUnlockIo();
                return NULL;
            }

            if (c == '\r' || c == '\n')
            {
                char *out;
                linenoiseLockIo();
                linenoiseDeactivateEditorState();
                fputc('\n', stdout);
                fflush(stdout);
                linenoiseUnlockIo();

                out = linenoiseStrdup(buf);
                return out;
            }

            if (c == '\t')
            {
                linenoiseApplyCompletion(prompt, buf, &len, &pos, &prevLen);
                continue;
            }

            if (c == 8)
            {
                if (pos > 0 && len > 0)
                {
                    memmove(buf + pos - 1, buf + pos, (size_t)(len - pos + 1));
                    pos -= 1;
                    len -= 1;
                    linenoiseRedraw(prompt, buf, len, pos, &prevLen);
                }
                continue;
            }

            if (isprint((unsigned char)c) && len < LINENOISE_MAX_LINE - 1)
            {
                if (pos == len)
                {
                    buf[pos++] = (char)c;
                    len += 1;
                    buf[len] = 0;
                }
                else
                {
                    memmove(buf + pos + 1, buf + pos, (size_t)(len - pos + 1));
                    buf[pos] = (char)c;
                    pos += 1;
                    len += 1;
                }
                linenoiseRedraw(prompt, buf, len, pos, &prevLen);
            }
        }
    }

    linenoiseLockIo();
    linenoiseDeactivateEditorState();
    fputc('\n', stdout);
    fflush(stdout);
    linenoiseUnlockIo();
    return NULL;
}

void linenoiseExternalWriteBegin(void)
{
    int i;
    int totalChars = 0;

    /* Lock shared console state and clear current prompt area before external output. */
    linenoiseLockIo();
    if (InterlockedCompareExchange(&g_editorActive, 0, 0) == 0)
    {
        return;
    }

    totalChars = (int)strlen(g_editorPrompt) + g_editorPrevLen;
    if (totalChars < 0)
    {
        totalChars = 0;
    }

    fputc('\r', stdout);
    for (i = 0; i < totalChars; ++i)
    {
        fputc(' ', stdout);
    }
    fputc('\r', stdout);
    fflush(stdout);
}

void linenoiseExternalWriteEnd(void)
{
    /* Restore prompt line after external output has been printed. */
    if (InterlockedCompareExchange(&g_editorActive, 0, 0) != 0)
    {
        int prevLen = g_editorPrevLen;
        linenoiseRedrawUnsafe(g_editorPrompt, g_editorBuf, g_editorLen, g_editorPos, &prevLen);
        g_editorPrevLen = prevLen;
    }
    linenoiseUnlockIo();
}