aboutsummaryrefslogtreecommitdiff
path: root/mpiosh/readline.c
blob: 0b8731dd1ae74652e9abcaed7d374caff7ad42f3 (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
/* readline.c
 *
 * Author: Andreas Büsching  <crunchy@tzi.de>
 *
 * $Id: readline.c,v 1.1 2002/10/12 18:31:45 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_cmd(const char *text, int state)
{
  static mpiosh_cmd_t *cmd = NULL;
  char *cmd_text = NULL;
  
  if (state == 0) {
    cmd = commands;
  }
  
  while (cmd->cmd) {
    if ((*text == '\0') || (strstr(cmd->cmd, text) == cmd->cmd)) {
      cmd_text = strdup(cmd->cmd);
      cmd++;
      break;
    }
    cmd++;
  }

  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;
  char fname[100];
  WORD year;  
  DWORD fsize;  

  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);

    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_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 {
    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 */