forked from poopgiggle/prudebug
/
cmdinput.c
65 lines (51 loc) · 1.34 KB
/
cmdinput.c
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
/*
*
* PRU Debug Program - command input function
* (c) Copyright 2011, 2013 by Arctica Technologies
* Written by Steven Anderson
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "prudbg.h"
int cmd_input(char *prompt, char *cmd, char *cmdargs, unsigned int *argptrs, unsigned int *numargs)
{
unsigned int i, on_zero;
unsigned int full_len;
// collect command until space or return
char * buf = readline (prompt);
if (!buf)
return -1;
if (strcmp(buf, "") != 0)
add_history(buf);
// replace spaces and return with zeros
full_len = strlen(buf);
for (i=0; i<full_len; i++) if (buf[i] == ' ') buf[i] = 0;
// copy command (first word) to cmd argument and shift to upper case
for (i=0; i<(strlen(buf)+1); i++) cmd[i] = toupper(buf[i]);
// build index array and count number of arguments
for (i=strlen(cmd), on_zero=TRUE, numargs[0]=0; i<full_len; i++) {
if (on_zero) {
if (buf[i] != 0) {
on_zero = FALSE;
argptrs[numargs[0]++] = i;
}
} else {
if (buf[i] == 0) on_zero = TRUE;
}
}
for (i=0; i<full_len+1; i++) cmdargs[i] = buf[i];
free(buf);
return 0;
}