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
|
/*
* Mini-Commander Applet
* Copyright (C) 1998, 1999 Oliver Maruhn <oliver@maruhn.com>
*
* Author: Oliver Maruhn <oliver@maruhn.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
If you expect tons of C code for command completion then you will
probably be astonished...
These routines have probably to be rewritten in future. But they
should work on every system with a bash-alike shell.
*/
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <mate-panel-applet.h>
#include <sys/stat.h>
#include <dirent.h>
#include "cmd_completion.h"
#include "preferences.h"
#include "macro.h"
static GList* cmdc( char* );
static void process_dir( const gchar* );
static void cleanup( void );
static gint g_list_str_cmp( gconstpointer, gconstpointer );
/* global declaration so g_atexit() can reference it to free it.
* (not strictly necessary).
*/
static GList *path_elements = NULL;
void
mc_cmd_completion (MCData *mc,
char *cmd)
{
char buffer[MC_MAX_COMMAND_LENGTH] = "";
char largest_possible_completion[MC_MAX_COMMAND_LENGTH] = "";
int num_whitespaces, i, pos;
GList *possible_completions_list = NULL;
GList *completion_element;
if(strlen(cmd) == 0)
{
return;
}
num_whitespaces = mc_macro_prefix_len_wspace (mc, cmd) - mc_macro_prefix_len (mc, cmd);
possible_completions_list = cmdc(cmd + mc_macro_prefix_len_wspace (mc, cmd));
/* get first possible completion */
completion_element = g_list_first(possible_completions_list);
if(completion_element)
strcpy(largest_possible_completion, (char *) completion_element->data);
else
strcpy(largest_possible_completion, "");
/* get the rest */
while((completion_element = g_list_next(completion_element)))
{
strcpy(buffer, (char *) completion_element->data);
pos = 0;
while(largest_possible_completion[pos] != '\000'
&& buffer[pos] != '\000'
&& strncmp(largest_possible_completion, buffer, pos + 1) == 0)
pos++;
strncpy(largest_possible_completion, buffer, pos);
/* strncpy does not add \000 to the end */
largest_possible_completion[pos] = '\000';
}
if(strlen(largest_possible_completion) > 0)
{
if(mc_macro_get_prefix(mc, cmd) != NULL)
strcpy(cmd, mc_macro_get_prefix(mc, cmd));
else
strcpy(cmd, "");
/* fill up the whitespaces */
for(i = 0; i < num_whitespaces; i++)
strcat(cmd, " ");
strcat(cmd, largest_possible_completion);
}
}
/*
* cmdc() -- command completion function.
*
* cmdc takes a char* and returns a GList* of possible completions.
*
* Initial version by Travis Hume <travishume@acm.org>.
*
*/
static GList *
cmdc( char *s )
{
GCompletion *completion = NULL;
GList *ret_list = NULL;
static GHashTable *path_hash = NULL;
static char *path = NULL;
gchar *path_elem;
struct stat buf;
static gboolean inited = FALSE;
gpointer hash_key = NULL;
/*
* Only want to build the GCompletion once. At some point I'd like to add
* code to refresh the GCompletion, either at a regular interval, or when
* there is a completion failure, ...
*
*/
if(!inited)
{
/* Make a local copy of the path variable. Otherwise the path
environment variable would be modified. */
path = (char *) malloc(sizeof(char) * (strlen(getenv("PATH")) + 1));
strcpy(path, getenv("PATH"));
path_hash = g_hash_table_new( g_str_hash, g_str_equal );
for( path_elem = strtok( path, ":" ); path_elem;
path_elem = strtok( NULL, ":" ))
{
if( stat( path_elem, &buf ))
continue;
if( buf.st_mode & S_IFDIR )
{
/* keep a hash of processed paths, to avoid reprocessing
* dupped path entries.
*/
hash_key = g_hash_table_lookup( path_hash, path_elem );
if( hash_key )
continue; /* duplicate $PATH entry */
else
{
g_hash_table_insert(
path_hash, (gpointer)path_elem, (gpointer)path_elem );
process_dir( path_elem );
}
}
}
/* atexit() we want to free the completion. */
g_atexit( cleanup );
inited = TRUE;
}
completion = g_completion_new( NULL );
g_completion_add_items( completion, path_elements );
ret_list = g_list_copy( g_completion_complete( completion, s, NULL ));
g_completion_free( completion );
return g_list_sort( ret_list, (GCompareFunc)g_list_str_cmp );
}
/*
* Compare function to return a sorted completion.
*/
static gint
g_list_str_cmp( gconstpointer a, gconstpointer b )
{
return( strcmp( (char *)a, (char *)b ));
}
/*
* Reads directory entries and adds non-dir, executable files
* to the GCompletion.
* Initial version by Travis Hume <travishume@acm.org>.
*/
static void
process_dir( const char *d )
{
DIR *dir;
struct dirent *de;
struct stat buf;
gpointer data;
gchar *path_str;
if( (dir = opendir( d )) == NULL )
return;
while( (de = readdir( dir )) != NULL )
{
if( strcmp( de->d_name, "." ) == 0 || strcmp( de->d_name, "..") == 0)
continue;
path_str = (gchar *)g_malloc( strlen( d ) + 1 + strlen( de->d_name ) + 1);
strcpy( path_str, d );
strcat( path_str, "/" );
strcat( path_str, de->d_name );
if( stat( path_str, &buf ) != 0)
continue;
g_free( (gpointer)path_str );
if( S_ISDIR( buf.st_mode ) )
continue;
data = g_malloc( strlen( (gchar *)de->d_name ) + 1 );
strcpy( (gchar *)data, (gchar *)(de->d_name) );
if( buf.st_mode & S_IXUSR )
path_elements = g_list_append( path_elements, data );
}
closedir( dir );
}
/*
* cleanup() -- free the memory used by the GCompletion.
*/
static void
cleanup( void )
{
g_list_free( path_elements );
}
|