aboutsummaryrefslogtreecommitdiff
path: root/src/readline.c
blob: afc78c02b691f253b4d1af64f59c06e1479f4097 (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
/* readline.c
 *
 * Author: Andreas Büsching  <crunchy@tzi.de>
 *
 * $Id: readline.c,v 1.7 2003/04/23 08:34:16 crunchy Exp $
 *
 * Copyright (C) 2001 Andreas Büsching <crunchy@tzi.de>
 *
 * 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, 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.
 */

#include "readline.h"

#include "command.h"
#include "mpiosh.h"

/* readline extensions */
void
mpiosh_readline_init(void)
{
  rl_readline_name = "mpio";
  rl_catch_signals = 0;
  rl_completer_quote_characters = "\"\'";
  rl_filename_quote_characters = " \"\' ";
  rl_attempted_completion_function = mpiosh_readline_completion;
  rl_event_hook = mpiosh_readline_cancel;
}

char *
mpiosh_readline_comp_onoff(const char *text, int state)
{
  static const char * states[] = { "on", "off", NULL };
  static const char ** st = states;
  const char * ret = NULL;

  if (state == 0) {
    st = states;
  }
  
  while (*st) {
    if (!strncmp(text, *st, strlen(text))) {
      ret = *st;
      st++;
      break;
    }
    st++;
  }
  
  return (ret ? strdup(ret) : NULL);
}

char *
mpiosh_readline_comp_cmd(const char *text, int state)
{
  static struct mpiosh_cmd_t	*cmd = NULL;
  static char			**alias = NULL;
  char				*cmd_text = NULL;
  
  if (state == 0) {
    cmd = commands;
  }
  
  while (cmd->cmd) {
    if (!alias) {
      if ((*text == '\0') || (strstr(cmd->cmd, text) == cmd->cmd)) {
	cmd_text = strdup(cmd->cmd);
	if (cmd->aliases) alias = cmd->aliases;
	else cmd++;
	break;
      }
      if (cmd->aliases) alias = cmd->aliases;
      else cmd++;
    } else {
      int break_it = 0;

      while (*alias) {
	if (strstr(*alias, text) == *alias) {
	  cmd_text = strdup(*alias);
	  alias++;
	  break_it = 1;
	  break;
	}
	alias++;
      }
      if (break_it) break;
      if (*alias == NULL) cmd++, alias = NULL;
    }
  }

  return cmd_text;
}

char *
mpiosh_readline_comp_mpio_file(const char *text, int state)
{
  static BYTE *p;
  char *arg = NULL;
  BYTE month, day, hour, minute, type;
  char fname[100];
  WORD year;  
  DWORD fsize;  

  if (mpiosh.dev == NULL) {
    rl_attempted_completion_over = 1;
    return NULL;
  }
  
  if (state == 0) p = mpio_directory_open(mpiosh.dev, mpiosh.card);

  while ((p != NULL) && (arg == NULL)) {
    memset(fname, '\0', 100);
    
    mpio_dentry_get(mpiosh.dev, mpiosh.card, p,
		    fname, 100,
		    &year, &month, &day,
		    &hour, &minute, &fsize, &type);

    if (strstr(fname, text) == fname) {
      arg = strdup(fname);
      if (strchr(arg, ' ')) {
	rl_filename_completion_desired = 1;
	rl_filename_quoting_desired = 1;
      }
    }

    p = mpio_dentry_next(mpiosh.dev, mpiosh.card, p);
  }
  
  
  return arg;
}

char *
mpiosh_readline_comp_config(const char *text, int state)
{
  static char *args[] = { "put", "write", "show", NULL };
  static char **arg = NULL;
  char *res = NULL;
  
  if (state == 0) arg = args;  
  if (*arg && (strstr(*arg, text) == *arg)) {
    res = strdup(*arg);
    arg++;
  }

  return res;
}


char **
mpiosh_readline_completion(const char *text, int start, int end)
{
  char **matches = (char**)NULL;

  UNUSED(end);

  if (start == 0) /* command completion */
    matches = rl_completion_matches(text, mpiosh_readline_comp_cmd);
  else {
    struct mpiosh_cmd_t	*scmd;
    char		*cmd, *help= rl_line_buffer;

    while (*help != ' ') help++;
    cmd = malloc(help - rl_line_buffer + 1);
    strncpy(cmd, rl_line_buffer, help - rl_line_buffer + 1);
    cmd[help - rl_line_buffer] = '\0';
    
    if ((scmd = mpiosh_command_find(cmd))) {
      if (scmd->comp_func) {
	matches = rl_completion_matches(text, scmd->comp_func);
      }
    }
    
    free(cmd);
  }
  
  return matches;
}

int
mpiosh_readline_cancel(void)
{
  if (mpiosh_cancel) rl_done = 1;
  
  return 0;
}

void
mpiosh_readline_noredisplay(void)
{
}

void
mpiosh_readline_pipe(void)
{
  rl_redisplay_function = mpiosh_readline_noredisplay;
  rl_event_hook = NULL;
}

/* end of readline.c */