Files
csse2310-a3/hq.h
2025-03-21 10:54:32 +10:00

208 lines
5.1 KiB
C

#ifndef HQ_H
#define HQ_H
#include <csse2310a3.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "job.h"
// Program exit codes
enum ExitCodes {
EXIT_OK = 0,
EXIT_CHLD_ERR = 99
};
/* get_valid_number()
* ------------------
* Gets a number from the given string
*
* Example:
* "1" -> 1
* "a" -> -1
* "1a" -> -1
* "-2" -> -1
*
* rawNumber: the raw number to convert to an integer
*
* Returns: the integer representation of the raw number, if the raw number
* is not a number, returns -1
*/
int get_valid_number(char *rawNumber);
/* get_job_from_raw_id()
* ---------------------
* Similar to get_kob_from_id, however also checks if the given raw id is a
* valid number
*
* rawId: the raw id for the job
*
* Returns: the job if the job id exists, otherwise NULL
*/
struct Job *get_job_from_raw_id(char *rawId);
/* prepare_args_for_exec()
* -----------------------
* NULL terminates the list of arguments so it can be used for execvp
*
* arguments: the list of arguments
* numberArgs: the number of arguments passed
*
* Returns: the null terminated array
*/
char **prepare_args_for_exec(char **arguments, int numberArgs);
/* handle_child_fds()
* ------------------
* Handles opening, duplicating and closing fds for use in child fork
*
* job: the job in which the fds are stored
*/
void handle_child_fds(struct Job *job);
/* execute_command()
* -----------------
* Executes the given command with the given arguments, adds a null terminator
* to the list of arguments before executing
*
* arguments: the list of arguments
* numberArgs: the number of arguments passed
*
* Returns: the return status of the command
*/
int execute_command(char *command, char **arguments, int numberArgs);
/* handle_spawn()
* --------------
* Handles the spawn command
*
* arguments: the arguments to give to the command
* number_args: the number of arguments passed
*/
void handle_spawn(char **arguments, int numberArgs);
/* report_job()
* ------------
* Prints the job information to stdout
*
* job: the job to report on
*/
void report_job(struct Job *job);
/* handle_report()
* ---------------
* Handles the report command
* Prints a brief description of the status of each job
*
* arguments: should only be one argument containing the job id
* number_args: the number of arguments passed
*/
void handle_report(char **arguments, int numberArgs);
/* handle_signal()
* ---------------
* Handles the signal command
* Sends a signal to a job given a job id and signal number
*
* arguments: the array of arguments for the command
* numberArgs: the number of arguments passed
*/
void handle_signal(char **arguments, int numberArgs);
/* handle_sleep()
* --------------
* Handles the sleep command
* Puts this program to sleep for the specified seconds
*
* arguments: the array of arguments for the command
* numberArgs: the number of arguments passed
*/
void handle_sleep(char **arguments, int numberArgs);
/* handle_send()
* -------------
* Handles the send command
* Sends the given message to the given job id's stdin
*
* arguments: the array of arguments for the command
* numberArgs: the number of arguments passed
*/
void handle_send(char **arguments, int numberArgs);
/* handle_recieve()
* -------------
* Handles the rcv command
* Reads next line in the given job id's stdout
*
* arguments: the array of arguments for the command
* numberArgs: the number of arguments passed
*/
void handle_recieve(char **arguments, int numberArgs);
/* handle_eof()
* -------------
* Handles the eof command
* Closes the write end of the programs pipe
*
* arguments: the array of arguments for the command
* numberArgs: the number of arguments passed
*/
void handle_eof(char **arguments, int numberArgs);
/* handle_cleanup()
* ----------------
* Handles the cleanup commands
* Sends a SIGKILL to all running jobs
*/
void handle_cleanup(void);
/* handle_commands()
* -----------------
* Function to coordinate what each command does
*
* command: the command string
* arguments: the arguments for the given command
* number_args: the amount of arguments passed
*/
void handle_commands(char *command, char **arguments, int numberArgs);
/* build_status()
* --------------
* Builds the status for a job and outputs it to the dest
* Builds in the form of "<prefix>(<state>)"
*
* dest: the character array to output to
* prefix: the prefix for the status
* state: the exit number / signal number of the status
*/
void build_status(char **dest, char *prefix, int state);
/* update_status()
* ---------------
* Updates all job status' if they have changed, called for handling of
* SIGCHLD
*
* signal: unused but required for a signal handler
*/
void update_status(int signal);
/* setup_signal_handlers()
* -----------------------
* Sets up the required signal handlers
* Ignores SIGINT, handles SIGCHLD with update_status
*/
void setup_signal_handlers(void);
/* process_input()
* ---------------
* Processes the input from the interactive cli
*
* input: the input from the interactive cli
*/
void process_input(char *input);
#endif