One can incidentally terminate sushi by pressing
Ctrl+C
(Ctrl+Break, Command+dot) or a similar combination of
keys that sends
SIGNIT to the shell. Function prevent_interruption()
sets up a signal
handler that intercepts SIGINT and displays message
“Type exit to
exit the shell” on stderr. The name of the handler
is
refuse_to_die(), its skeleton and the skeleton
of
prevent_interruption() are provided in sushi.c. Hint:
use system call
sigaction() to set up the handler. As a result, when
you attempt to
interrupt the shell, it will display the message and
continue.
II. (Non-actionable.) File sushi_yyparser.y has been
refactored for
clarity. It also has mechanisms for building the
command line. The
definitions of the supporting data structures:
arglist_t, prog_t, and
redirection_t – have been added to sushi.h.
Familiarize yourself
with the definitions of the rules (“non-terminal
symbols”) exe, args,
and arg.
exe, the command line program, is simply a list of
arguments with some additional information stored in prog_t. An
instance of the structure is created dynamically with malloc (lines
87– 92). It must be freed after the command execution.
args is an array (not a list) of char*, each holding
one argument (lines 94–98). It is reallocated each time a new
argument is discovered (lines 98–103). It must be freed after the
command execution.
arg, a command-line argument, is simply a string
returned by the lexer (lines 105–107). It must be freed after the
command execution.
Once the command line is collected, it is passed to
function int
spawn(prog_t *exe, prog_t *pipe, int bgmode), lines
41–42. You can
disregard the second and the third parameters for
now.
Sushi.c
#include <stdlib.h>
#include <stdio.h>
#include "sushi.h"
#include <stdbool.h>
#include <string.h>
int sushi_exit = 0;
static void refuse_to_die(int sig)
{
// TODO
}
static void prevent_interruption() {
// TODO
}
int main() {
char *file;
char *fileName =
"/sushi.conf";
// read the commands from the file
sushi.conf, located in the $HOME directory.
file =
malloc(strlen(getenv("HOME")) + strlen(fileName) + 1);
strcat(strcpy(file,
getenv("HOME")), fileName);
sushi_read_config(file);
prevent_interruption();
while (sushi_exit == 0) {
// display
the prompt SUSHI_DEFAULT_PROMPT
printf("%s", SUSHI_DEFAULT_PROMPT);
char
*commandLine = sushi_read_line(stdin);
if
(commandLine != NULL){
// Call sushi_parse_command() if sushi_read_line() is successfully
called
if (sushi_parse_command(commandLine) == 0) {
// store the returnValue to history if no syntax error
found.
sushi_store(commandLine);
};
}
}
return EXIT_SUCCESS;
}
Sushi.h
#ifndefSUSHI_H
#defineSUSHI_H
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
#defineSUSHI_MAX_INPUT80/* really modest :) */
#defineSUSHI_HISTORY_LENGTH32
#defineSUSHI_DEFAULT_PROMPT"> "
char *sushi_read_line(FILE *in);
intsushi_read_config(char *fname);
voidsushi_store(char *line);
voidsushi_show_history();
char *sushi_unquote(char * s);
intsushi_parse_command(char*command);
externint sushi_exit; // The global exit flag
// Support for the primitive parse tree
// An array of arguments, eventually NULL-terminated
typedefstruct {
int size;
char **args;
} arglist_t;
// I/O redirection, as in "foobar < foo > bar"
typedefstruct {
char *in, *out1, *out2; // stdin, stdout-write, stdout-append
} redirection_t;
// The program to be executed
typedefstruct prog {
arglist_t args; // Arguments, including the program name
redirection_t redirection; // Optional redirections
struct prog *next; // The next program in the pipeline, if any; NULL otherwise
} prog_t;
// Start a new program
intspawn(prog_t *exe, prog_t *pipe, intbgmode);
// Report unimplemented functions
void__not_implemented__();
// Replace standard malloc and realloc: abort() if malloc/realloc fails
// The likelyhood of the event is low, but the consequences are grave
void *super_malloc(size_t size);
void *super_realloc(void *ptr, size_tsize);
#endif
sushi_yyparser.y
%{
#include "sushi.h"
int yylex();
void yyerror(const char* s);
%}
%union {
char *s;
int i;
arglist_t alist;
prog_t *exe;
redirection_t redir;
}
%token YY_SUSHI_SET
%token YY_SUSHI_HISTORY
%token YY_SUSHI_EXIT
%token YY_SUSHI_JOBS
%token YY_SUSHI_PWD
%token YY_SUSHI_CD
%token YY_SUSHI_UNKNOWN
%token YY_SUSHI_LESS
%token YY_SUSHI_MORE
%token YY_SUSHI_MOREMORE
%token YY_SUSHI_AMP
%token YY_SUSHI_BAR
%token<s> YY_SUSHI_TOK
%type<s> arg
%type<redir> out_redir any_redir in_redir
out1_redir out2_redir inout_redir
%type<alist> args
%type<i> bg_mode
%type<exe> exe out_exe in_exe redir_exe
pipe
%start cmdline
%%
cmdline:
{ /* an empty line is valid, too! Do nothing */ }
| redir_exe bg_mode { spawn($1,
NULL, $2); }
| in_exe pipe bg_mode { spawn($1, $2, $3);
}
| arg YY_SUSHI_SET arg { __not_implemented__(); } /*
TODO */
|
YY_SUSHI_JOBS {
__not_implemented__(); } /* TODO */
|
YY_SUSHI_PWD {
__not_implemented__(); } /* TODO */
| YY_SUSHI_CD arg {
__not_implemented__(); } /* TODO */
| YY_SUSHI_HISTORY {
sushi_show_history(stdout); }
|
YY_SUSHI_EXIT {
sushi_exit = 1; }
pipe:
YY_SUSHI_BAR out_exe {
__not_implemented__(); } /* TODO */
| YY_SUSHI_BAR exe pipe { __not_implemented__(); } /*
TODO */
redir_exe:
exe {
$$ = $1; }
| exe any_redir { $$ = $1; $$->redirection = $2;
__not_implemented__(); }
in_exe:
exe {
$$ = $1; }
| exe in_redir { $$ = $1; $$->redirection =
$2; __not_implemented__(); }
out_exe:
exe {
$$ = $1; }
| exe out_redir { $$ = $1; $$->redirection = $2;
__not_implemented__(); }
inout_redir:
in_redir out_redir { $$.in = $1.in; $$.out1 =
$2.out1; $$.out2 = $2.out2; }
| out_redir in_redir { $$.in = $2.in; $$.out1 =
$1.out1; $$.out2 = $1.out2; }
out_redir:
out1_redir { $$ = $1; }
| out2_redir { $$ = $1; }
any_redir:
in_redir { $$ = $1;
}
| out_redir { $$ = $1; }
| inout_redir { $$ = $1; }
in_redir: YY_SUSHI_LESS
arg { $$.in = $2; $$.out1 = $$.out2 = NULL;
}
out1_redir: YY_SUSHI_MORE arg
{ $$.out1 = $2; $$.in = $$.out2 = NULL; }
out2_redir: YY_SUSHI_MOREMORE arg { $$.out2 = $2;
$$.in = $$.out1 = NULL; }
bg_mode:
{ $$ = 0; }
| YY_SUSHI_AMP { $$ = 1; __not_implemented__();
}
exe:
args {
$$ = malloc(sizeof(prog_t));
$$->args = $1;
$$->redirection.in =
$$->redirection.out1 = $$->redirection.out2 = NULL;
$$->next = NULL; }
args:
arg {
$$.args = malloc(sizeof(char*)); //
New argument array
$$.args[0] = $1; // Its first
element
$$.size = 1; }
| args arg {
$$ = $1;
$$.size++; // Increase argument
array size
$$.args = realloc($$.args,
sizeof(char*) * $$.size); // Add storage
$$.args[$$.size - 1] = $2; } // Add
the last element
arg:
YY_SUSHI_TOK { $$ = $1; }
%%
/* This section is empty */
I am doing an operating shell and this is a part of
it. I need help with the C programming. I. One can incidentally
terminate sushi by pressing Ctrl C (Ctrl Break, Command dot) or a
similar combination of keys that sends SIGNIT to the shell.
Function prevent_interruption() sets up a signal handler that
intercepts SIGINT and displays message “Type exit to exit the
shell” on stderr. The name of the handler is refuse_to_die(), its
skeleton and the skeleton of prevent_interruption() are provided in
sushi.c. Hint: use system call sigaction() to set up the handler.
As a result, when you attempt to interrupt the shell, it will
display the message and continue.
Then, please help me with this II. (Non-actionable.)
File sushi_yyparser.y has been refactored for clarity. It also has
mechanisms for building the command line. The definitions of the
supporting data structures: arglist_t, prog_t, and redirection_t –
have been added to sushi.h. Familiarize yourself with the
definitions of the rules (“non-terminal symbols”) exe, args, and
arg. 1. exe, the command line program, is simply a list of
arguments with some additional information stored in prog_t. An
instance of the structure is created dynamically with malloc (lines
87– 92). It must be freed after the command execution. 2. args is
an array (not a list) of char*, each holding one argument (lines
94–98). It is reallocated each time a new argument is discovered
(lines 98–103). It must be freed after the command execution. 3.
arg, a command-line argument, is simply a string returned by the
lexer (lines 105–107). It must be freed after the command
execution. Once the command line is collected, it is passed to
function int spawn(prog_t *exe, prog_t *pipe, int bgmode), lines
41–42. You can disregard the second and the third parameters for
now.
One can incidentally terminate sushi by pressing Ctrl+C (Ctrl+Break, Command+dot) or a similar combination of keys...
Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd ); pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
need this in c programming and you can edit the code below. also
give me screenshot of the output
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LINE_SIZE 1024
void my_error(char *s)
{
fprintf(stderr, "Error: %s\n", s);
perror("errno");
exit(-1);
}
// This funciton prints lines (in a file) that contain string
s.
// assume all lines has at most (LINE_SIZE - 2) ASCII
characters.
//
// Functions that may be called in this function:
// fopen(), fclose(), fgets(), fputs(),...
Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following program. My program is called Ultimo.exe and it is in (C:\Users\Dell\source\repos\Ultimo\Debug). The file used is a .txt file in the directory of the .exe program. The text file which is address.txt is at the same location as Ultimo.exe (C:\Users\Dell\source\repos\Ultimo\Debug). The program takes information in the address.txt file and sorts it by zip code, from smallest to largest. The program works by using input/output redirection...
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...
C Programming
Take screenshot of compiled output once complete. (which
should look similar to the sample one provided). Will rate
positively.
Problem:
(1) Add PopTailSLL()
as a new member function to the SLL abstract data type. “Unlink”
the logically-last node (tail) from the list *sll, call the
DestructElement function to destruct the object pointed-to by the
node’s element data member, free() the node, and decrement
size. A SLL_UNDERFLOW exception occurs when size = 0.
void PopTailSLL(SLL *sll);
Hint PopTailSLL() is...
Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....
10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual...