-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcl.cpp
More file actions
172 lines (162 loc) · 4.98 KB
/
cl.cpp
File metadata and controls
172 lines (162 loc) · 4.98 KB
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
/** ************************************************************************************************
* CL - Command Line functions
* Part of the Wilson project (www.iambobot.com)
* Fernando R
*
*
** ************************************************************************************************
**/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
using namespace std;
#include "cl.h"
// File management and manipulation library
// return: argv count
size_t cParser(char* inputline, char a[8][32])
{
int ls= strlen(inputline);
int i=0; // index inputline[i]
size_t j=0; // index argv[j]
int k=0; // index argv[j][k]
//fprintf(stdout, "\ncParser %d %s", ls, inputline); fflush(stdout);
//return ls;
a[j][k]= '\0';
// get i pointing to the first non space char
while(i<ls && inputline[i]!=0 && inputline[i]==' ') i++;
while(i<ls && inputline[i]!=0 && j<8)
{
if(inputline[i]==' ' || inputline[i]=='\n')
{
// END OF WORD
// Remove spaces & CR
while(i<ls && inputline[i]!=0 && (inputline[i]==' ' || inputline[i]=='\n')) i++;
//start new word
if(i<ls && inputline[i]!=0)
{
//fprintf(stdout, "\ncParser j= %d", j); fflush(stdout);
k=0;
j++;
//fprintf(stdout, "\n %lu %d %d %d", (unsigned long) &a[j][k], i, j, k); fflush(stdout);
a[j][k]='\0';
}
}
else
{
//fprintf(stdout, "\n %lu %d %d %d", (unsigned long) &a[j][k], i, j, k); fflush(stdout);
a[j][k++]= inputline[i++];
a[j][k]= '\0';
}
}
// count last word
if(k!=0) j++;
// done
return j;
}
// ------------------------------------------------------------------------------------------------
// -------------------------- ---- ----------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
/* ----------------------------------------------------------------------------------------------------- */
/* ------------------------------------------- KEYBOARD ---------------------------------------------- */
// restore values
struct termios term_flags;
int term_ctrl;
int termios_init()
{
/* get the original state */
tcgetattr(STDIN_FILENO, &term_flags);
term_ctrl = fcntl(STDIN_FILENO, F_GETFL, 0);
return 0;
}
int termios_restore()
{
tcsetattr(STDIN_FILENO, TCSANOW, &term_flags);
fcntl(STDIN_FILENO, F_SETFL, term_ctrl);
return 0;
}
int kbhit(void)
{
struct termios newtio, oldtio;
int oldf;
if (tcgetattr(STDIN_FILENO, &oldtio) < 0) /* get the original state */
return -1;
newtio = oldtio;
/* echo off, canonical mode off */
newtio.c_lflag &= ~(ECHO | ICANON );
tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
int ch = getchar();
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
fcntl(STDIN_FILENO, F_SETFL, oldf);
return 0;
}
/* ----------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ CommandLineBuff ----------------------------------------- */
CommandLineBuff::CommandLineBuff(char *command_buffer, string prompt)
{
this->command_b_read= 0;
this->command_b_write= 0;
this->command_b_length= 0;
this->command_b_last= 0;
this->prompt= prompt;
this->command_buffer= command_buffer;
}
void CommandLineBuff::Up()
{
this->command_b_read --;
if(this->command_b_read<0) this->command_b_read= this->command_b_length - 1;
// return 0;
}
void CommandLineBuff::Down()
{
this->command_b_read ++;
if(this->command_b_read>=COMMNAD_QUEUE_LENGTH ||
(this->command_b_length<COMMNAD_QUEUE_LENGTH && this->command_b_read>=this->command_b_length))
this->command_b_read= 0;
// return 0;
}
int CommandLineBuff::Last()
{
int ic=0;
if(this->command_b_length > 0)
{
// Delete last
ic= this->command_queue[command_b_last].length();
printf("\r");
printf(this->prompt.c_str());
for (int i=0; i<ic; i++) printf(" ");
printf("\r");
// Print prompt
printf(this->prompt.c_str());
// take b_read
strcpy(this->command_buffer, this->command_queue[command_b_read].c_str());
printf("%s", this->command_buffer);
this->command_b_last= this->command_b_read;
ic= this->command_queue[command_b_read].length();
}
return ic;
}
void CommandLineBuff::Store()
{
string command_line(this->command_buffer);
this->command_queue[this->command_b_write]= command_line;
this->command_b_read= this->command_b_write;
this->command_b_last= this->command_b_read;
this->command_b_write = (this->command_b_write + 1) % COMMNAD_QUEUE_LENGTH;
if(this->command_b_length < COMMNAD_QUEUE_LENGTH) this->command_b_length++;
// return 0;
}
// END OF FILE