Skip to content
Snippets Groups Projects
command.h 2.12 KiB
Newer Older
Sébastien Gendre's avatar
Sébastien Gendre committed
// Command representation and manipulation
// Sébastien Gendre <seb@k-7.ch>

#ifndef COMMAND_H
#define COMMAND_H

#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "limits.h"
#include "builtins.h"
#include "jobs.h"
Sébastien Gendre's avatar
Sébastien Gendre committed

// Chars used to separate atoms of a command
#define ARGS_DELIMITERS " \t"
#define COMMAND_PIPE_DELIMITER "|"
// Command types
enum command_type {BUILTIN, JOB, PIZZA};

// A full user command
typedef struct cmd cmd_t;
struct cmd {
  enum command_type type;  // Command type
  char** argv;             // The command arguments
  int argc;                // Number of command arguments
  bool foreground;         // Should the command be run in foreground or background (might not be relevant for your TP depending on TP version)
  cmd_t *next;             // Pointer to the next command, in case of pipe
/* Parse a string to get one command
*  - str: A string containing the full command. IT WILL BE MODIFIED BY THE FUNCTION.
*  - cmd: Command updated from user input string (i.e. command input). MUST BE DISPOSED SEE FUNCTION dispose_command
* 
*  Return: 
*   A pointer to the new command, NULL in case of error
cmd_t *command_from_str(char *str);

/* Parse a string to get a pipeline of commands
*  
*  Parameters
*  - user_input: A string containing the full command. IT WILL BE MODIFIED BY THE FUNCTION.
* 
*  Return: 
*  The pointer to the first command, NULL in case of error
*/
cmd_t *command_pipeline_from_str(char *str);
Sébastien Gendre's avatar
Sébastien Gendre committed


/* Dispose of the command, which free some allocated memory
*  A COMMAND MUST BE DISPOSED BEFORE IT IS USED FOR A NEW COMMAND (i.e. before calling parse again)
*
*  Parameters:
*  - cmd: The command to dispose of
*/
void command_dispose(cmd_t *cmd);
Sébastien Gendre's avatar
Sébastien Gendre committed

/* Fee a pipeline of command from the given command 
 * 
 *  Parameters:
 *  - cmd: The command start command of the pipeline to free
*/
void command_free_pipeline(cmd_t *cmd);

/* Execute a given command as job or as builtin
 * 
 * Parameters:
 * - cmd: The command to execute
 * 
 * Return:
 * - The command return code, 0 is success
 */
int command_pipeline_exec(cmd_t *cmd);
Sébastien Gendre's avatar
Sébastien Gendre committed

#endif /* COMMAND_H */