Skip to content

Commit 9c9c605

Browse files
major redesign
1 parent 4ba9501 commit 9c9c605

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pyoverflow-cli
22
A Command Line Tool for Unix Based Operating Systems to instantly open Stack Overflow Solution for corresponding error
33

4-
gcc -Wall -I/usr/include/python3.12 $(python3-config --embed --cflags) main.c $(python3-config --embed --ldflags) -lpython3.12 -o my_program
4+
55

66
gcc -Wall -I/usr/include/python3.12 main.c -lpython3.12 -o pyoverflow-cli

headers/commands.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ int (*builtin_func[]) (char **) = {
4040
&pyoverflow_leave, //exit command
4141
};
4242

43-
int ghzsh_num_builtins() {
43+
int pyoverflow_num_builtins() {
4444
return sizeof(builtin_str) / sizeof(char *);
4545
}
4646

4747
/*
4848
***********************************Builtin function implementations*************************************
4949
*/
5050

51-
//***************************************chdir*********************************************** */
51+
//***************************************sofsearch*********************************************** */
52+
//function for calling the python files for searching
5253
int pyoverflow_sofsearch(char **args)
5354
{
5455

@@ -57,11 +58,20 @@ int pyoverflow_sofsearch(char **args)
5758
argc++;
5859
}
5960

61+
// Initialize the Python interpreter.
6062
Py_Initialize();
63+
FILE *fp = fopen("search.py", "r");
64+
if (fp == NULL) {
65+
perror("PyOverFlowFileNotFoundError: Have you deleted search.py by any chance?");
66+
return 1;
67+
}
68+
PyRun_SimpleFile(fp, "search.py");
69+
fclose(fp);
6170

62-
Py_Main(argc, args);
71+
// Finalize the Python interpreter.
6372
Py_Finalize();
64-
return 0;
73+
74+
return 1;
6575
}
6676

6777
//***************************************tell*************************************************
@@ -71,12 +81,12 @@ int pyoverflow_sofsearch(char **args)
7181
int pyoverflow_help(char **args)
7282
{
7383
int i;
74-
printf("Pyoverflow-cli\n");
75-
printf("Version 1.0\n");
84+
printf("--------------------------------PyOverflow-CLI-----------------------------\n");
85+
printf("---------------------------------Version 1.0----------------------------\n");
7686
printf("Type program names and arguments, and hit enter.\n");
7787
printf("The following are built in:\n");
7888

79-
for (i = 0; i < ghzsh_num_builtins(); i++) {
89+
for (i = 0; i < pyoverflow_num_builtins(); i++) {
8090
printf(" %s\n", builtin_str[i]);
8191
}
8292

headers/shellops.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@
2525
//avoiding double execution
2626
#ifndef SHELLOPS_H
2727
#define SHELLOPS_H
28-
#define GHZ_RL_BUFFERSIZE 1024 //buffer size
28+
#define PYOF_RL_BUFFERSIZE 1024 //buffer size
2929
#define EXIT_FAILURE 1 //exit error code
30-
#define GHZ_TOK_BUFFERSIZE 64
31-
#define GHZ_TOK_DELIM " \t\n\r\a"
30+
#define PYOF_TOK_BUFFERSIZE 64
31+
#define PYOF_TOK_DELIM " \t\n\r\a"
3232
// function for reading input
33-
char *ghzsh_readLine();
34-
char **ghzsh_splitLine();
35-
int ghzsh_execute(char **args);
33+
char *pyoverflow_readLine();
34+
char **pyoverflow_splitLine();
35+
int pyoverflow_execute(char **args);
3636

3737
//loop for shell
38-
void ghzsh_loop(void){
38+
void pyoverflow_loop(void){
3939
char *line;
4040
char **args;
4141
int status;
4242

4343
do{
44-
printf("> ");
44+
printf("PyOverflow-CLI> ");
4545
//reading and executing input
46-
line= ghzsh_readLine();
47-
args = ghzsh_splitLine();
48-
status = ghzsh_execute(args);
46+
line= pyoverflow_readLine();
47+
args = pyoverflow_splitLine();
48+
status = pyoverflow_execute(args);
4949

5050
//freeing allocated memory
5151
free(line);
@@ -55,9 +55,9 @@ void ghzsh_loop(void){
5555

5656
// function for reading input
5757

58-
char *ghzsh_readLine(void){
58+
char *pyoverflow_readLine(void){
5959
//setting buffer size
60-
int bufsize = GHZ_RL_BUFFERSIZE;
60+
int bufsize = PYOF_RL_BUFFERSIZE;
6161
//setting buffer position
6262
int position = 0;
6363
//allocating memory for buffer
@@ -66,7 +66,7 @@ char *ghzsh_readLine(void){
6666

6767
//handling buffer memory allocation error
6868
if(!buffer){
69-
fprintf(stderr,"ghz-sh: Buffer Allocation Failed");
69+
fprintf(stderr,"pyoverflow-cli: Buffer Allocation Failed");
7070
exit(EXIT_FAILURE);
7171
}
7272

@@ -85,11 +85,11 @@ char *ghzsh_readLine(void){
8585

8686
//handling event where buffer limit is exceeded
8787
if(position>=bufsize){
88-
bufsize+=GHZ_RL_BUFFERSIZE;
88+
bufsize+=PYOF_RL_BUFFERSIZE;
8989
//realloc the buffer
9090
buffer = (char*)realloc(buffer,bufsize);
9191
if(!buffer){
92-
fprintf(stderr,"ghz-sh: Buffer Allocation Error");
92+
fprintf(stderr,"pyoverflow-cli: Buffer Allocation Error");
9393
exit(EXIT_FAILURE);
9494
}
9595
}
@@ -100,27 +100,27 @@ char *ghzsh_readLine(void){
100100
}
101101

102102
//split line
103-
char **ghzsh_splitLine(){
103+
char **pyoverflow_splitLine(){
104104
//defining buffersize and position
105-
int bufsize = GHZ_TOK_BUFFERSIZE, position =0;
105+
int bufsize = PYOF_TOK_BUFFERSIZE, position =0;
106106
//allocating mem for tokens
107107
char **tokens = (char**)malloc(bufsize*sizeof(char*));
108108
char *token;
109109
char *line;
110110

111111
//handling mem allocation failure
112112
if(!tokens){
113-
fprintf(stderr,"ghz-sh: Buffer Allocation Failed");
113+
fprintf(stderr,"pyoverflow-cli: Buffer Allocation Failed");
114114
exit(EXIT_FAILURE);
115115
}
116-
token = strtok(line,GHZ_TOK_DELIM);
116+
token = strtok(line,PYOF_TOK_DELIM);
117117
while(token!=NULL){
118118
tokens[position]=token;
119119
position++;
120120

121121
//if buffer size exceeds
122122
if(position>=bufsize){
123-
bufsize+=GHZ_TOK_BUFFERSIZE;
123+
bufsize+=PYOF_TOK_BUFFERSIZE;
124124
//realloc bufsize
125125
tokens = realloc(tokens, bufsize * sizeof(char*));
126126
if (!tokens) {
@@ -129,14 +129,14 @@ char **ghzsh_splitLine(){
129129
}
130130

131131
}
132-
token = strtok(NULL,GHZ_TOK_DELIM);
132+
token = strtok(NULL,PYOF_TOK_DELIM);
133133
}
134134
tokens[position] = NULL;
135135
return tokens;
136136
}
137137

138138
//launch function
139-
int ghzsh_launch(char **args)
139+
int pyoverflow_launch(char **args)
140140
{
141141
pid_t pid, wpid;
142142
int status;
@@ -146,12 +146,12 @@ int ghzsh_launch(char **args)
146146
if (pid == 0) {
147147
// Child process
148148
if (execvp(args[0], args) == -1) {
149-
perror("ghz-sh");
149+
perror("pyoverflow-cli");
150150
}
151151
exit(EXIT_FAILURE);
152152
} else if (pid < 0) {
153153
// Error forking
154-
perror("ghz-sh");
154+
perror("pyoverflow-cli");
155155
} else {
156156
// Parent process
157157
do {
@@ -162,7 +162,7 @@ int ghzsh_launch(char **args)
162162
return 1;
163163
}
164164

165-
int ghzsh_execute(char **args)
165+
int pyoverflow_execute(char **args)
166166
{
167167
int i;
168168

@@ -171,13 +171,13 @@ int ghzsh_execute(char **args)
171171
return 1;
172172
}
173173

174-
for (i = 0; i < ghzsh_num_builtins(); i++) {
174+
for (i = 0; i < pyoverflow_num_builtins(); i++) {
175175
if (strcmp(args[0], builtin_str[i]) == 0) {
176176
return (*builtin_func[i])(args);
177177
}
178178
}
179179

180-
return ghzsh_launch(args);
180+
return pyoverflow_launch(args);
181181
}
182182

183183

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int argc, char **argv){
2626
//area for config files
2727

2828
//call the loop
29-
ghzsh_loop();
29+
pyoverflow_loop();
3030

3131
//space for cleanup
3232

pyoverflow-cli

136 Bytes
Binary file not shown.

search.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
print("hello")

0 commit comments

Comments
 (0)