completion.c - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/completion.c
blob: 2eb304d0b40af2bf78b925cd7cf413da20ff6a48 (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

#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline.h>
#include <history.h>
#include <lua.h>
#include "completion.h"
#include "gsl-shell.h"
static char *my_generator (const char *text, int state);
void initialize_readline()
{
 rl_completion_entry_function = my_generator;
 rl_basic_word_break_characters = " \t\n\"'~><=*+-/;,|[{(";
}
#define WORDS_BUFFER_SIZE 256
#define NODE_LIST_SIZE 8
char *my_generator (const char *text, int state)
{
 static int list_index, len;
 static const char *text_term;
 static char words_buffer[WORDS_BUFFER_SIZE];
 static char *node_list[NODE_LIST_SIZE];
 static int word_number;
 static char *base_word;
 char *word;
 lua_State *L = globalL;
 int k;
 if (!state)
 {
 const char *p, *curr = text;
 int node_counter = 0, words_index = 0;
 list_index = 0;
 word_number = 0;
 if (strlen (text) >= WORDS_BUFFER_SIZE)
 return NULL;
 for (p = strchr (curr, '.'); p; )
 {
 char *new_word = words_buffer + words_index;
 int wlen = p - curr;
 if (node_counter >= NODE_LIST_SIZE)
 return NULL;
 memcpy (new_word, curr, wlen);
 new_word[wlen] = 0;
 words_index += wlen+1;
 node_list[node_counter] = new_word;
 node_counter ++;
 curr = p+1;
 p = strchr (curr, '.');
 word_number++;
 }
 text_term = curr;
 len = strlen (text_term);
 {
 int wlen = curr - text;
 base_word = words_buffer + words_index;
 if (words_index + wlen + 1 >= WORDS_BUFFER_SIZE)
 return NULL;
 memcpy (base_word, text, wlen);
 base_word[wlen] = 0;
 }
 }
 lua_pushvalue (L, LUA_GLOBALSINDEX);
 word = words_buffer;
 for (k = 0; k < word_number; k++)
 {
 if (!lua_istable(L, -1))
 goto pop_exit;
 lua_getfield(L, -1, word);
 lua_remove(L, -2);
 word = strchr (word, '0円') + 1;
 }
 if (!lua_istable(L, -1))
 goto pop_exit;
 lua_pushnil (L);
 for (k = 0; k < list_index; k++)
 {
 if (lua_next (L, -2) == 0)
 goto pop_exit;
 lua_pop (L, 1);
 }
 while (lua_next(L, -2) != 0)
 {
 const char *key = lua_tostring (L, -2);
 list_index ++;
 if (key)
 {
 if (strncmp (key, text_term, len) == 0)
 {
 char *new_word;
 if (lua_istable (L, -1))
 rl_completion_append_character = '.';
 else
 rl_completion_suppress_append = 1;
 if (asprintf (&new_word, "%s%s", base_word, key) < 0)
 {
 lua_pop (L, 2);
 goto pop_exit;
 }
 lua_pop (L, 3);
 return new_word;
 }
 }
 lua_pop (L, 1);
 }
pop_exit:
 lua_pop (L, 1);
 return NULL;
}
generated by cgit v1.2.3 (git 2.25.1) at 2025年09月11日 06:21:38 +0000

AltStyle によって変換されたページ (->オリジナル) /