Skip to content

Commit 4ba9501

Browse files
works a little
1 parent fc7b2fb commit 4ba9501

File tree

8 files changed

+336
-0
lines changed

8 files changed

+336
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a.out

.vscode/c_cpp_properties.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"/usr/include/python3.12"
8+
],
9+
"defines": [],
10+
"compilerPath": "/usr/bin/gcc",
11+
"cStandard": "c17",
12+
"cppStandard": "gnu++17",
13+
"intelliSenseMode": "linux-gcc-x64"
14+
}
15+
],
16+
"version": 4
17+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# pyoverflow-cli
22
A Command Line Tool for Unix Based Operating Systems to instantly open Stack Overflow Solution for corresponding error
3+
4+
gcc -Wall -I/usr/include/python3.12 $(python3-config --embed --cflags) main.c $(python3-config --embed --ldflags) -lpython3.12 -o my_program
5+
6+
gcc -Wall -I/usr/include/python3.12 main.c -lpython3.12 -o pyoverflow-cli

headers/commands.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/****************************************************
2+
*
3+
* @headerfile commands.h
4+
*
5+
* @author Aryan Karamtoth (SpaciousCoder78)
6+
*
7+
* @date Sunday, March 16 2024
8+
*
9+
* @brief Gigahertz Shell's Command Functions
10+
*
11+
****************************************************/
12+
13+
14+
#include <Python.h>
15+
16+
#ifndef COMMANDS_H
17+
#define COMMANDS_H
18+
19+
#include<stdio.h>
20+
#include<unistd.h>
21+
/*
22+
Function Declarations for builtin shell commands:
23+
*/
24+
int pyoverflow_sofsearch(char **args); //change directory
25+
int pyoverflow_help(char **args); //help command
26+
int pyoverflow_leave(char **args); //exit command
27+
28+
/*
29+
List of builtin commands, followed by their corresponding functions.
30+
*/
31+
char *builtin_str[] = {
32+
"sofsearch", //change directory
33+
"help", //help command
34+
"leave", //exit command
35+
};
36+
37+
int (*builtin_func[]) (char **) = {
38+
&pyoverflow_sofsearch, //change directory
39+
&pyoverflow_help, //help command
40+
&pyoverflow_leave, //exit command
41+
};
42+
43+
int ghzsh_num_builtins() {
44+
return sizeof(builtin_str) / sizeof(char *);
45+
}
46+
47+
/*
48+
***********************************Builtin function implementations*************************************
49+
*/
50+
51+
//***************************************chdir*********************************************** */
52+
int pyoverflow_sofsearch(char **args)
53+
{
54+
55+
int argc = 0;
56+
while (args[argc] != NULL) {
57+
argc++;
58+
}
59+
60+
Py_Initialize();
61+
62+
Py_Main(argc, args);
63+
Py_Finalize();
64+
return 0;
65+
}
66+
67+
//***************************************tell*************************************************
68+
69+
70+
//*****************************************sos*********************************************** */
71+
int pyoverflow_help(char **args)
72+
{
73+
int i;
74+
printf("Pyoverflow-cli\n");
75+
printf("Version 1.0\n");
76+
printf("Type program names and arguments, and hit enter.\n");
77+
printf("The following are built in:\n");
78+
79+
for (i = 0; i < ghzsh_num_builtins(); i++) {
80+
printf(" %s\n", builtin_str[i]);
81+
}
82+
83+
return 1;
84+
}
85+
86+
87+
//*****************************************leave*********************************************** */
88+
int pyoverflow_leave(char **args)
89+
{
90+
return 0;
91+
}
92+
93+
94+
#endif

headers/shellops.h

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/***********************************************************
2+
*
3+
* @headerfile shellops.h
4+
*
5+
* @author Aryan Karamtoth (SpaciousCoder78)
6+
*
7+
* @date Thursday, August 15 2024
8+
*
9+
* @brief Gigahertz Shell's Operation Functions
10+
*
11+
*
12+
*
13+
*
14+
**********************************************************/
15+
16+
17+
18+
//header file for handling shell operations
19+
#include<stdio.h>
20+
#include<stdlib.h>
21+
#include<string.h>
22+
#include<unistd.h>
23+
#include<sys/wait.h>
24+
#include "commands.h"
25+
//avoiding double execution
26+
#ifndef SHELLOPS_H
27+
#define SHELLOPS_H
28+
#define GHZ_RL_BUFFERSIZE 1024 //buffer size
29+
#define EXIT_FAILURE 1 //exit error code
30+
#define GHZ_TOK_BUFFERSIZE 64
31+
#define GHZ_TOK_DELIM " \t\n\r\a"
32+
// function for reading input
33+
char *ghzsh_readLine();
34+
char **ghzsh_splitLine();
35+
int ghzsh_execute(char **args);
36+
37+
//loop for shell
38+
void ghzsh_loop(void){
39+
char *line;
40+
char **args;
41+
int status;
42+
43+
do{
44+
printf("> ");
45+
//reading and executing input
46+
line= ghzsh_readLine();
47+
args = ghzsh_splitLine();
48+
status = ghzsh_execute(args);
49+
50+
//freeing allocated memory
51+
free(line);
52+
free(args);
53+
}while(status);
54+
}
55+
56+
// function for reading input
57+
58+
char *ghzsh_readLine(void){
59+
//setting buffer size
60+
int bufsize = GHZ_RL_BUFFERSIZE;
61+
//setting buffer position
62+
int position = 0;
63+
//allocating memory for buffer
64+
char *buffer = (char*)malloc(sizeof(char)*bufsize);
65+
int c;
66+
67+
//handling buffer memory allocation error
68+
if(!buffer){
69+
fprintf(stderr,"ghz-sh: Buffer Allocation Failed");
70+
exit(EXIT_FAILURE);
71+
}
72+
73+
while(1){
74+
//reading input
75+
c=getchar();
76+
77+
//handling null or EOF event
78+
if(c==EOF || c=='\n'){
79+
buffer[position]='\0';
80+
return buffer;
81+
}else{
82+
buffer[position]=c;
83+
}
84+
position++;
85+
86+
//handling event where buffer limit is exceeded
87+
if(position>=bufsize){
88+
bufsize+=GHZ_RL_BUFFERSIZE;
89+
//realloc the buffer
90+
buffer = (char*)realloc(buffer,bufsize);
91+
if(!buffer){
92+
fprintf(stderr,"ghz-sh: Buffer Allocation Error");
93+
exit(EXIT_FAILURE);
94+
}
95+
}
96+
97+
98+
}
99+
100+
}
101+
102+
//split line
103+
char **ghzsh_splitLine(){
104+
//defining buffersize and position
105+
int bufsize = GHZ_TOK_BUFFERSIZE, position =0;
106+
//allocating mem for tokens
107+
char **tokens = (char**)malloc(bufsize*sizeof(char*));
108+
char *token;
109+
char *line;
110+
111+
//handling mem allocation failure
112+
if(!tokens){
113+
fprintf(stderr,"ghz-sh: Buffer Allocation Failed");
114+
exit(EXIT_FAILURE);
115+
}
116+
token = strtok(line,GHZ_TOK_DELIM);
117+
while(token!=NULL){
118+
tokens[position]=token;
119+
position++;
120+
121+
//if buffer size exceeds
122+
if(position>=bufsize){
123+
bufsize+=GHZ_TOK_BUFFERSIZE;
124+
//realloc bufsize
125+
tokens = realloc(tokens, bufsize * sizeof(char*));
126+
if (!tokens) {
127+
fprintf(stderr, "ghz-sh: allocation error\n");
128+
exit(EXIT_FAILURE);
129+
}
130+
131+
}
132+
token = strtok(NULL,GHZ_TOK_DELIM);
133+
}
134+
tokens[position] = NULL;
135+
return tokens;
136+
}
137+
138+
//launch function
139+
int ghzsh_launch(char **args)
140+
{
141+
pid_t pid, wpid;
142+
int status;
143+
144+
//using for sys call
145+
pid = fork();
146+
if (pid == 0) {
147+
// Child process
148+
if (execvp(args[0], args) == -1) {
149+
perror("ghz-sh");
150+
}
151+
exit(EXIT_FAILURE);
152+
} else if (pid < 0) {
153+
// Error forking
154+
perror("ghz-sh");
155+
} else {
156+
// Parent process
157+
do {
158+
wpid = waitpid(pid, &status, WUNTRACED);
159+
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
160+
}
161+
162+
return 1;
163+
}
164+
165+
int ghzsh_execute(char **args)
166+
{
167+
int i;
168+
169+
if (args[0] == NULL) {
170+
// An empty command was entered.
171+
return 1;
172+
}
173+
174+
for (i = 0; i < ghzsh_num_builtins(); i++) {
175+
if (strcmp(args[0], builtin_str[i]) == 0) {
176+
return (*builtin_func[i])(args);
177+
}
178+
}
179+
180+
return ghzsh_launch(args);
181+
}
182+
183+
184+
185+
#endif

main.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//main file
2+
3+
/************************************************************
4+
*
5+
* @file main.c
6+
*
7+
* @author Aryan Karamtoth(SpaciousCoder78)
8+
*
9+
* @date Thursday, August 15 2024
10+
*
11+
* @brief Gigahertz Shell
12+
*
13+
* @details Main file of Gigahertz Shell
14+
*
15+
*
16+
*
17+
***********************************************************/
18+
19+
20+
21+
#include "headers/shellops.h"
22+
#define EXIT_SUCCESS 0
23+
24+
int main(int argc, char **argv){
25+
26+
//area for config files
27+
28+
//call the loop
29+
ghzsh_loop();
30+
31+
//space for cleanup
32+
33+
return EXIT_SUCCESS;
34+
35+
}

pyoverflow-cli

16.8 KB
Binary file not shown.

requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)