This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
terminal.h
executable file
·165 lines (149 loc) · 4.31 KB
/
terminal.h
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
float my_pow(float base, int exp)//mickmicking the pow function
{
float temp;
if( exp == 0)
return 1;
temp = my_pow(base, exp/2);
if (exp%2 == 0)
return temp*temp;
else
{
if(exp > 0)
return base*temp*temp;
else
return (temp*temp)/base;
}
}
//micking atoi function (returns integer from string)
static int my_atoi(const char* snum)
{
int idx, str_idx = 0, accum = 0, num_is_neg = 0;
const unsigned int NUMLEN = (int)strlen(snum);
/* Check if negative number and flag it. */
if(snum[0] == 0x2d)
num_is_neg = 1;
for(idx = NUMLEN - 1; idx >= 0; idx--)
{
/* Only process numbers from 0 through 9. */
if(snum[str_idx] >= 0x30 && snum[str_idx] <= 0x39)
accum += (snum[str_idx] - 0x30) * my_pow(10, idx);
str_idx++;
}
/* Check flag to see if originally passed -ve number and convert result if so. */
if(!num_is_neg)
return accum;
else
return accum * -1;
}
//my library for single character input without echoing, **the conio.h is needed for windows systems.
char get_char_necho()
{
#if defined(__linux__)
/*prevent echoing of characters from the buffer*/
getche();
#elif defined(_WIN32)
#include<conio.h>
getch();
#endif
}
/*prevent echoing of characters from the buffer*/
extern int getche(void)//works with linux
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO);//shell out to kill echo
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
system("stty -echo");//shell out to kill echo
ch = getchar();
system("stty echo");
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/*Clears a the number of characters passed to it as parameter n*/
extern void letter_clear(int n)
{
int i;
for(i=1;i<=n;i++)
printf("\b \b");//implementing a destructive backspace
}
unsigned int is_integral(char number[], int argc_cmd)
{
//printf("%s\n", number);
int i = 0;
if(number==NULL)
{
fprintf(stderr, "%s\n", "Fatal Error: Null argument");
exit(EXIT_FAILURE);
}
/* if(my_atoi(number)==0)
{
fprintf(stderr, "%s\n", "Fatal Error: Lesson number cannot be zero");
exit(EXIT_FAILURE);
}*/
//checking for negative numbers
if (number[0] == '-')
{
fprintf(stderr, "%s\n", "Fatal error: Negative argument");
exit(EXIT_FAILURE);
}
if(argc_cmd==4)
{
if(strlen(number)<2||my_atoi(number)<10||my_atoi(number)>900)
{
fprintf(stderr, "%s\n", "Fatal Error: Either argument is not in range <45-900> OR argument is invalid");
exit(EXIT_FAILURE);
}
}
else
if(strlen(number)>2||my_atoi(number)>50||my_atoi(number)<1)
{
fprintf(stderr, "%s\n", "Fatal Error: Argument not in range <1-50> OR invalid");
exit(EXIT_FAILURE);
}
return my_atoi(number);//returns the unsigned integral string entered as argument at terminal
}
int range_verifier(int n)
{
if(n<0||n>15)
{
fprintf(stderr, "%s\n", "Lesson number does not exist");
exit(EXIT_FAILURE);
}
return n;
}
//windows virtual command line; produces command line environment while in the program.
//char v_cmd[81][81];//2d virtual array to hold the arguments as they are entered.
//added an argument, to be removed if does not work and line 152 modified
extern int win_cmd_arg(char **v_cmd, char *temp)
{
printf("%s","%%noslac$>>");
char win_str[300];
int i=0,argc=0,j=0,u=0;
fgets(win_str,250,stdin);//reading arguments, uses \n to flush
if(strlen(win_str)>177||u>=15)//ensures the arguments are not more than 15 and size does not exceed 177
{
fprintf(stderr,"%s\n", "Invalid number of arguments");
exit(EXIT_FAILURE);
}
do
{
temp[j]=win_str[i];
if(temp[j]=='\n'||temp[j]==' ')
{
temp[j]='\0';
j=-1;
v_cmd[u]=temp;
// printf("u=%d, j=%d, i=%d v_cmd=%s\n",u,j,i,v_cmd[u]);
//getchar();
if(temp[j]=='\n')
break;
u++;
}
//printf("temp j=%d\n",j);
j++;
i++;
}while(win_str[i]!=EOF);
return u-1;//first element has index 0
}