-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshelp.c
More file actions
91 lines (86 loc) · 2.97 KB
/
shelp.c
File metadata and controls
91 lines (86 loc) · 2.97 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
#include "chatgpt.h"
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
#define SHELSYSTEM "zsh/macOs"
int main(void)
{
const char *api_key = getenv("OPENAI_API_KEY");
char task[BUFFER_SIZE];
char prompt[BUFFER_SIZE * 2];
char *commands;
char need_explanation[2];
if (!api_key)
{
fprintf(stderr,
"Error: The environment variable OPENAI_API_KEY is not set.\n");
fprintf(stderr, "Please set it before running this program.\n");
return (1);
}
curl_global_init(CURL_GLOBAL_ALL);
printf("Enter your task: ");
if (!fgets(task, BUFFER_SIZE, stdin))
{
fprintf(stderr, "Error reading input.\n");
return (1);
}
char *template = "You are a %s shell command assistant."
"Generate shell commands for this task using bash-compatible syntax. "
"Output only the commands. If inappropriate, output only: "
"'Sorry, cannot create commands for this task.' "
"\n\nTask: %s";
snprintf(prompt, sizeof(prompt), template, SHELSYSTEM, task);
commands = chatgpt_query(api_key, prompt);
if (!commands)
{
fprintf(stderr, "\nError producing command(s).\n");
return (1);
}
printf("\n\n=== Disclaimer ===\n\n\n");
printf("This program uses generative AI and can make mistakes. ");
printf("\n\nThe commands below can be unhelpful, or even harmful. ");
printf("\n\nYou are responsible for any damage or loss that occurs ");
printf("as a result of running these commands; use caution. \n");
printf("\n\n=== Suggested Command(s) ====\n\n%s\n", commands);
if (!strncmp(commands, "Sorry", 5))
{
return (0);
}
while (1)
{
printf("\nDo you need explanation? (y/n)\n");
fgets(need_explanation, 2, stdin);
if (!strcmp(need_explanation, "y") || !strcmp(need_explanation, "Y"))
{
char *explanation_template = "Explain in simple terms what the following shell command(s) do. "
"If the command is relatively simple with only a few arguments, "
"keep the explanation brief (1-2 sentences). "
"If the command is complex, provide an overall summary first, "
"then break it down step by step using bullet points for: "
"pipelines, loops, conditionals, substitutions, and individual commands. "
"If the command contains syntax errors or potential issues, "
"mention them clearly. "
"If you cannot provide an explanation because the command is "
"inappropriate or potentially harmful, respond only with: "
"'Sorry, I cannot provide an explanation for this command.' "
"\n\nCommand(s): '''%s'''";
snprintf(prompt, sizeof(prompt), explanation_template, commands);
commands = chatgpt_query(api_key, prompt);
if (!commands)
{
fprintf(stderr, "\nError producing explanation(s).\n");
return (1);
}
printf("\n\n=== Explanation ====\n\n%s\n\n", commands);
return (0);
}
else if (!strcmp(need_explanation, "n") || !strcmp(need_explanation, "N"))
{
break;
}
}
curl_global_cleanup();
return (0);
}