aboutsummaryrefslogtreecommitdiff
path: root/mpiosh/mpiosh.c
blob: fa2f75fe5aab0f9d67120b923d3188a6d6c7b617 (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
/* -*- linux-c -*- */

/* 
 *
 * $Id: mpiosh.c,v 1.16 2002/09/19 22:23:01 crunchy Exp $
 *
 * Author: Andreas Büsching  <crunchy@tzi.de>
 *
 * mpiosh - user interface of the mpio library, providing access to
 * 	    some model of the MPIO mp3 players.
 * 
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * */

#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include <readline/readline.h>
#include <readline/history.h>

#include "libmpio/debug.h"
#include "libmpio/mpio.h"

#include "callback.h"
#include "mpiosh.h"

/* structure containing current state */
mpiosh_t mpiosh;

/* flag indicating a user-interrupt of the current command */
int mpiosh_cancel = 0;

/* prompt strings */
const char *PROMPT_INT = "\033[;1mmpio <i>\033[m ";
const char *PROMPT_EXT = "\033[;1mmpio <e>\033[m ";

mpiosh_cmd_t commands[] = {
  { "debug", "[level|file|on|off] <value>",
    "modify debugging options",
    mpiosh_cmd_debug },
  { "ver", NULL,
    "version of mpio package",
    mpiosh_cmd_version },
  { "help", "[<command>]",
    "show information about known commands or just about <command>",
    mpiosh_cmd_help },
  { "dir", NULL,
    "list content of current memory card",
    mpiosh_cmd_dir },
  { "info", NULL,
    "show information about MPIO player",
    mpiosh_cmd_info },
  { "mem", "[i|e]",
    "set current memory card. 'i' selects the internal and 'e' "
    "selects the external memory card (smart media card)",
    mpiosh_cmd_mem },
  { "open", NULL,
    "open connect to MPIO player",
    mpiosh_cmd_open },
  { "close", NULL,
    "close connect to MPIO player",
    mpiosh_cmd_close },
  { "quit", " or exit",
    "exit mpiosh and close the device",
    mpiosh_cmd_quit },
  { "exit", NULL, NULL, mpiosh_cmd_quit },
  { "get", "<filename>",
    "read <filename> from memory card",
    mpiosh_cmd_get },
  { "mget", "<regexp>",
    "read all files matching the regular expression "
    "from the selected memory card",
    mpiosh_cmd_mget },
  { "put", "<filename>",
    "write <filename> to memory card",
    mpiosh_cmd_put },
  { "mput", "<regexp>",
    "write all local files matching the regular expression "
    "to the selected memory card",
    mpiosh_cmd_mput },
  { "del", "<filename>",
    "deletes <filename> from memory card",
    mpiosh_cmd_del },
  { "mdel", "<regexp>",
    "deletes all files matching the regular expression "
    "from the selected memory card",
    mpiosh_cmd_mdel },
  { "dump", NULL,
    "get all files of current memory card",
    mpiosh_cmd_dump },
  { "free", NULL,
    "display amount of available bytes of current memory card",
    mpiosh_cmd_free },
  { "format", NULL,
    "format current memory card",
    mpiosh_cmd_format },
/*   { "switch", "<file1> <file2>", */
/*     "switches the order of two files", */
/*     mpiosh_cmd_switch }, */
  { "ldir", NULL,
    "list local directory",
    mpiosh_cmd_ldir },
  { "lpwd", NULL,
    "print current working directory",
    mpiosh_cmd_lpwd },
  { "lcd", NULL,
    "change the current working directory",
    mpiosh_cmd_lcd },
  { "lmkdir", NULL,
    "create a local directory",
    mpiosh_cmd_lmkdir },
  { "debug_memory", NULL,
    "dump the first 0x100 of the selected memory card",
    mpiosh_cmd_debug_mem },
  { NULL, NULL, NULL, NULL }
};

/* readline extensaions */
void
mpiosh_readline_init(void)
{
  rl_readline_name = "mpio";
  rl_catch_signals = 0;
  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_completion(const char *text, int start, int end)
{
  char **matches = (char**)NULL;

  UNUSED(end);

  if (start == 0)
    matches = rl_completion_matches(text, mpiosh_readline_comp_cmd);
  
  return matches;
}

/* helper functions */
void
mpiosh_init(void)
{
  mpiosh.dev = NULL;
  mpiosh.card = MPIO_INTERNAL_MEM;
  mpiosh.prompt = PROMPT_INT;
}

char **
mpiosh_command_split_line(char *line)
{
  char **cmds, *cmd;
  int count = 1;
  char *help, *copy = strdup(line);
  
  help = copy;
  
  while (*help)
    if ((*help++ == ';') && (*(help) != '\0')) count++;
  cmds = malloc(sizeof(char *) * (count + 1));

  cmd = help = copy, count = 0;
  while (*cmd == ' ') cmd++;
  while (help) {
    if (*help == '"') {
      help++;
      while (*help != '\0' && *help != '"') help++;
    }
  
    if (*help == '\0') break;
    if (*help == ';') {
      *help++ = '\0';
      if (*help == '\0') break;
      cmds[count++] = strdup(cmd);
      while (*help == ' ') help++;
      cmd = help;
    }
    help++;
  }
  if (cmd != '\0') {
    cmds[count++] = strdup(cmd);
  }

  cmds[count] = NULL;
  free(copy);
  
  return cmds;
}

mpiosh_cmd_t *
mpiosh_command_find(char *line)
{
  mpiosh_cmd_t *cmd = commands;
 
  while (cmd->cmd) {
    if (strstr(line, cmd->cmd) == line) {
      if (line[strlen(cmd->cmd)] == ' ' ||
	  line[strlen(cmd->cmd)] == '\0') 
	return cmd;
    }
    cmd++;
  }

  return NULL;
}

void
mpiosh_command_regex_fix(char *argv[])
{
  char **walk = argv;
  char *new_pos, *help;
  char buffer[512];
  
  while (*walk) {
    memset(buffer, '\0', 512);
    help = *walk;
    new_pos = buffer;
    *new_pos++ = '^';
    while (*help != '\0') {
      if (*help == '*' && ((help == *walk) || (*(help - 1) != '.'))) {
	*new_pos++ = '.';
	*new_pos = *help;
      } else if ((*help == '.') && (*(help + 1) != '*')) {
	*new_pos++ = '\\';
	*new_pos = *help;
      } else if (*help == '?' && ((help == *walk) || (*(help - 1) != '\\'))) {
	*new_pos = '.';
      } else {
	*new_pos = *help;
      }
      
      help++, new_pos++;
    }
    *new_pos = '$';
    free(*walk);
    *walk = strdup(buffer);
    
    walk++;
  }
}

char **
mpiosh_command_get_args(char *line)
{
  char **args;
  char *arg_start, *copy, *help, *prev;
  int count = 0, i = 0, go = 1, in_quote = 0;
  
  copy = strdup(line);
  arg_start = strchr(copy, ' ');

  if (arg_start == NULL) {
    args = malloc(sizeof(char *));
    args[0] = NULL;
    return args;
  }
  
  while (*arg_start == ' ') arg_start++;
  
  help = arg_start;
  while (help <= (copy + strlen(copy))) {
    if (*help == '"') {
      help++;count++;
      
      while (*help != '\0' && *help != '"') 
	help++;
      help++;
      while (*help == ' ') help++;
      if (*help == '\0') break;
      in_quote = 1;
    } else if (((help > arg_start) && (*help == '\0')) ||
	(*help == ' ' && (*(help + 1) != '\0') && (*(help + 1) != ' '))) {
      count++;
      in_quote = 0;
      help++;
    } else
      help++;
  }
  
  args = malloc(sizeof(char *) * (count + 1));
  
  help = prev = arg_start;
  in_quote = 0;
  while (go) {
    if (*help == '"') in_quote = !in_quote, help++;
    if (((*help == ' ') && !in_quote) || (in_quote && *help == '"') ||
	(*help == '\0')) {
      if (*help == '\0') {
	go = 0;
	if (*prev == '\0') break;
      }
      
      if (*prev == '"') {
	if (*(help - 1) == '"')
	  *(help - 1) = '\0';
	else
	  *help = '\0';
	
	args[i++] = strdup(prev + 1);
      } else {
	*help = '\0';
	args[i++] = strdup(prev);
      }
      
      if (go) {
	help++;
	if (in_quote) {
	  while (*help != '"') help++;
	  help++;
	  in_quote = 0;
	} else
	  while (*help == ' ') help++;
	prev = help;
      }
    } else
      help++;
  }
  args[i] = NULL;

  free(copy);
  
  return args;
}

void
mpiosh_command_free_args(char **args)
{
  char **arg = args;
  
  while (*arg) free(*arg++);

  free(args);
}

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

void
mpiosh_signal_handler(int signal)
{
  mpiosh_cancel = 1;
}

void
mpiosh_noredisplay(void)
{
}

int
main(int argc, char *argv[]) {
  char *		line;
  char **		cmds, **walk;
  mpiosh_cmd_t *	cmd;
  struct sigaction	sigc;
  int			interactive = 1;
  
  UNUSED(argc);
  UNUSED(argv);
  
  setenv("mpio_debug", "", 0);
  setenv("mpio_color", "", 0);

  /* no unwanted interruption anymore */
  sigc.sa_handler = mpiosh_signal_handler;
  sigc.sa_flags = SA_NOMASK;
  
  sigaction(SIGINT, &sigc, NULL);
  
  /* init readline and history */
  mpiosh_readline_init();
  using_history();
  
  debug_init();
  
  mpiosh_init();
  mpiosh.dev = mpio_init(mpiosh_callback_init);
  printf("\n");

  if (mpiosh.card == MPIO_INTERNAL_MEM)
    mpiosh.prompt = PROMPT_INT;
  else
    mpiosh.prompt = PROMPT_EXT;

  if (!isatty(fileno(stdin))) {
    interactive = 0;
    mpiosh.prompt = NULL;
    rl_redisplay_function = mpiosh_noredisplay;
    rl_event_hook = NULL;
  }
  
  if (!mpiosh.dev && interactive) {
    printf("could not find MPIO player.\n");
  }

  while ((line = readline(mpiosh.prompt))) {
    if ((*line == '\0') || mpiosh_cancel) {
      rl_clear_pending_input ();
      mpiosh_cancel = 0;
      continue;
    }

    cmds = mpiosh_command_split_line(line);

    if (cmds[0][0] == '\0') {
      free(cmds);
      continue;
    }
    
    walk = cmds;
    while (*walk) {      
      cmd = mpiosh_command_find(*walk);

      if (cmd) {
	char ** help, **args = mpiosh_command_get_args(*walk);
	help = args;
      
	if (!interactive) debug("running... '%s'\n", *walk);
	cmd->func(args);
	mpiosh_command_free_args(args);
      } else
	printf("unknown command: '%s'\n", *walk);

/*       if ((idx = history_search(line, -1)) != -1) */
/* 	history_set_pos(idx); */
/*       else */
      add_history(line);
      free(*walk);
      walk++;
    }
    free(cmds);

    /* reset abort state */
    mpiosh_cancel = 0;    
  }

  mpiosh_cmd_quit(NULL);

  return 0;
}