Question
In C Programming: I'm working on a small shell and I have to expand more tokens and I'm stuck on this part: Any occurrence of
In C Programming:
I'm working on a small shell and I have to expand more tokens and I'm stuck on this part:
Any occurrence of $? within a word shall be replaced with the exit status of the last foreground command (see waiting).
Any occurrence of $! within a word shall be replaced with the process ID of the most recent background process (see waiting).
If an expanded environment variable is unset, it shall be interpreted as an empty string (). This includes the PS1 variable as described above.
The $? parameter shall default to 0 (0).
The $! parameter shall default to an empty string () if no background process ID is available.
Expansion shall occur in the forward direction in a single pass, and expanded text shall not participate in further expansion token recognition (expansion is not recursive).
Here's what I have so far:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_ARGS 512
#define MAX_LENGTH 2048
static bool reading_input = false;
static bool foreground_only = false;
int status = 0;
void handle_sigint(int signo) {
if (!reading_input) {
signal(SIGINT, SIG_IGN);
return;
}
if (!foreground_only) {
return;
}
if (signo == SIGINT) {
fprintf(stderr, "Terminated by signal %d ", signo);
status = 2;
}
}
void handle_sigstp(int signo) {
char* message = "Entering foreground-only mode (& is now ignored) ";
write(STDOUT_FILENO, message, strlen(message));
fflush(stdout);
}
int main() {
struct sigaction sigint_action = {0};
sigint_action.sa_handler = handle_sigint;
sigaction(SIGINT, &sigint_action, NULL);
struct sigaction sigtstp_action = {0};
sigtstp_action.sa_handler = handle_sigstp;
sigaction(SIGTSTP, &sigtstp_action, NULL);
char *prompt = (getenv("PS1") != NULL) ? getenv("PS1") : "";
char *input = NULL;
size_t input_size = 0;
char *args[MAX_ARGS];
char input_copy[MAX_LENGTH];
int num_args = 0;
pid_t child_pid;
int child_status;
while (1) {
while ((child_pid = waitpid(-1, &child_status, WNOHANG)) > 0) {
if (WIFEXITED(child_status)) {
fprintf(stderr, "Child process %jd done. Exit status %d. ", (intmax_t) child_pid, WEXITSTATUS(child_status));
} else if (WIFSIGNALED(child_status)) {
fprintf(stderr, "Child process %jd done. Signaled %d. ", (intmax_t) child_pid, WTERMSIG(child_status));
} else if (WIFSTOPPED(child_status)) {
kill(child_pid, SIGCONT);
fprintf(stderr, "Child process %jd stopped. Continuing. ", (intmax_t) child_pid);
}
}
printf("%s", prompt);
ssize_t num_chars = getline(&input, &input_size, stdin);
reading_input = true;
if (num_chars == -1 && errno == EINTR) {
clearerr(stdin);
fprintf(stderr, " ");
continue;
}
char *delimiter = (getenv("IFS") != NULL) ? getenv("IFS") : " \t ";
char* word = strtok(input, delimiter);
while(word != NULL && num_args < MAX_ARGS) {
args[num_args++] = word;
word = strtok(NULL, delimiter);
char* home = getenv("HOME");
for (int i = 0; i < num_args; i++) {
char* word = args[i];
char* tilde = strstr(word, "~/");
if (tilde == word) {
sprintf(input_copy, "%s%s", home, word + 1);
args[i] = input_copy;
}
char* pid = strstr(word, "$$");
if (pid != NULL) {
sprintf(input_copy, "%d%s", getpid(), pid + 2);
args[i] = input_copy;
}
}
}
reading_input = false;
}
}
thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started