root / Whitix / branches / hybrid / user / burn / main.c

Revision 328, 2.8 kB (checked in by mwhitworth, 7 months ago)

Fix issue with too-long commands.

Line 
1/* Burn - the Whitix shell */
2
3#include <syscalls.h>
4#include <stdio.h>
5#include <ctype.h>
6#include <errno.h>
7
8#include "builtins.h"
9
10#define BUFFER_SIZE 32768
11char buffer[BUFFER_SIZE];
12
13char currPath[PATH_MAX];
14
15#define MAJOR_BURN_VERSION              0
16#define MINOR_BURN_VERSION              3
17
18int ProcessLine(char* buffer)
19{
20        /* Strtok isn't flexible enough */
21        char* array[100];
22        int i=0;
23        int j=0;
24
25        int length=strlen(buffer);
26
27        memset(array,0,100*sizeof(char*));
28
29        while (i < length)
30        {
31                /* Skip any whitespace */
32                while (isspace(buffer[i]))
33                        ++i;
34
35                if (buffer[i] != '\0')
36                        array[j++]=&buffer[i];
37                else{
38                        /* End of buffer */
39                        array[j]=NULL; /* Just null-terminate the array for safety */
40                        break;
41                }
42
43                while (buffer[i] != '\0' && !isspace(buffer[i]))
44                        ++i;
45
46                /* Terminate the token string */
47                if (buffer[i] != '\0' && i < length)
48                {
49                        buffer[i]='\0';
50                        ++i;
51                }
52        }
53
54        /* First token should be command */
55        if (!array[0])
56                return 0; /* Nothing to evaluate */
57
58        struct CmdTable* curr=btTable;
59
60        while (curr->command)
61        {
62                if (!strcmp(curr->command,array[0]))
63                {
64                        curr->function(array);
65                        break;
66                }
67
68                curr++;
69        }
70
71        if (!curr->command)
72        {
73                /* Try to execute */
74                if (BtInExec(array))
75                        printf("Command %s not found\n",array[0]);
76        }
77
78#if 0
79        }else{
80                /* Ready for I/O redirection */
81                int fds[]={0,1,2};
82                int fd;
83               
84                /* Search for the > symbol */
85                for (i=0; i<j; i++)
86                {
87                        if (!strcmp(array[i],">"))
88                                break;
89                }
90
91                /* I/O redirection is required */
92                if (i != j)
93                {
94                        if (!array[i+1])
95                        {
96                                printf("burn: I/O redirection requires a filename\n");
97                                return 0;
98                        }
99
100                        /* Either way, it'll still overwrite the stuff at the beginning of the file. Use fopen? */
101                        fd=SysOpen(array[i+1],FILE_CREATE_OPEN | FILE_READ | FILE_WRITE,0);
102                        if (fd < 0)
103                        {
104                                printf("Failed to open %s\n",array[i+1]);
105                                return 0;
106                        }
107
108                        SysTruncate(fd,0);
109
110                        fds[0]=fd;
111                        array[i]=NULL;
112                        array[i+1]=NULL;
113                }
114
115                int pid=SysCreateProcess(array[0],fds,&array[1]);
116                if (pid < 0)
117                        printf("Command %s not found\n",array[0]);
118                else
119                        SysWaitForProcessFinish(pid);
120
121                if (i != j)
122                        SysClose(fd);
123        }
124#endif
125
126        return 0;
127}
128
129int gets(char* buffer,int length)
130{
131        int c,i=0;
132
133        while (i<length)
134        {
135                c=getchar();
136
137                /* Don't add extended characters */
138                if (c >= 0x80)
139                        continue;
140
141                if (c == EOF || c == '\n')
142                        break;
143               
144                if (c == '\b')
145                {
146                        if (i > 0)
147                        {
148                                buffer[i]=' ';
149                                --i;
150                                putchar('\b');
151                        }
152                }else{
153                        buffer[i++]=(char)c;
154                }
155        }
156
157        buffer[i]='\0';
158
159        return 0;
160}
161
162int main(int argc,char* argv[])
163{
164        printf("Burn shell V%d.0%d\n---------------\nPrint help for a list of commands\n", MAJOR_BURN_VERSION, MINOR_BURN_VERSION);
165
166        /* Get the current directory now, and only update when cd is called */
167        SysGetCurrDir(currPath,PATH_MAX);
168
169        while (1)
170    {   
171                printf("%s>",currPath);
172                gets(buffer,BUFFER_SIZE-1);
173
174                ProcessLine(buffer);
175        }
176
177        return 0;
178}
Note: See TracBrowser for help on using the browser.