tiny-shell 0.2
A mini shell project aiming to gain knowledge about Win32 and Linux API
Loading...
Searching...
No Matches
args.h File Reference
#include <stdbool.h>
#include "../os/type.h"
Include dependency graph for args.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  args
 Result after parsing an arbitrary string. More...
 

Functions

bool args_init_from_str (struct args *obj, const os_char *input)
 Build args from an input string.
 
void args_destroy (struct args *obj)
 
struct argsargs_deep_copy (const struct args *obj)
 Deep copy the object, including arguments string.
 
void args_deep_copy_init (struct args *obj, const struct args *source)
 Initialize the object by deep copying source
 
bool is_whitespace (os_char c)
 !
 

Function Documentation

◆ args_deep_copy()

struct args * args_deep_copy ( const struct args * obj)

Deep copy the object, including arguments string.

Definition at line 145 of file args.c.

145 {
146 if(!obj) {
147 return NULL;
148 }
149 struct args *res = malloc(sizeof(struct args));
150 res->argc = obj->argc;
151 res->background = obj->background;
152 res->argv = (os_char **)malloc((obj->argc + 1) * sizeof(os_char *));
153 for(int i = 0; i < obj->argc; ++i) {
154 const unsigned int len = strlen(obj->argv[i]);
155 res->argv[i] = malloc((len + 1) * sizeof(os_char));
156 memcpy(res->argv[i], obj->argv[i], len * sizeof(os_char));
157 res->argv[i][len] = '\0';
158 }
159 res->argv[obj->argc] = NULL;
160 return res;
161}
Result after parsing an arbitrary string.
Definition args.h:10
os_char ** argv
Definition args.h:11
unsigned int argc
Definition args.h:12
bool background
Definition args.h:13

References args::argc, args::argv, and args::background.

◆ args_deep_copy_init()

void args_deep_copy_init ( struct args * obj,
const struct args * source )

Initialize the object by deep copying source

Definition at line 163 of file args.c.

163 {
164 assert(source != NULL && "Cannot deep copy null");
165 obj->argc = source->argc;
166 obj->background = source->background;
167 obj->argv = (os_char **)malloc((source->argc + 1) * sizeof(os_char *));
168 for(int i = 0; i < source->argc; ++i) {
169 const unsigned int len = strlen(source->argv[i]);
170 obj->argv[i] = malloc((len + 1) * sizeof(os_char));
171 memcpy(obj->argv[i], source->argv[i], len * sizeof(os_char));
172 obj->argv[i][len] = '\0';
173 }
174 obj->argv[obj->argc] = NULL;
175}

References args::argc, args::argv, and args::background.

Referenced by cmd_init_from_str().

Here is the caller graph for this function:

◆ args_destroy()

void args_destroy ( struct args * obj)

Definition at line 135 of file args.c.

135 {
136 if(!obj) {
137 return;
138 }
139 for(int i = 0; i < obj->argc; ++i) {
140 free(obj->argv[i]);
141 }
142 free((void *)obj->argv);
143}

References args::argc, and args::argv.

Referenced by cmd_destroy(), and cmd_init_from_str().

Here is the caller graph for this function:

◆ args_init_from_str()

bool args_init_from_str ( struct args * obj,
const os_char * input )

Build args from an input string.

Returns
true if initialization succeeded, false otherwise

Definition at line 117 of file args.c.

117 {
118 assert(obj && "NULL input");
119 obj->argc = 0;
120 obj->argv = NULL;
121 obj->background = false;
122 os_char *quote_transformed = transform_quotes(input);
123 if(!quote_transformed) {
124 return false;
125 }
126 split_by_whitespaces(quote_transformed, obj);
127 free(quote_transformed);
128 for(int i = 0; i < obj->argc; ++i) {
129 re_transform_arg(obj->argv[i]);
130 }
132 return true;
133}
void verify_background(struct args *args)
Definition args.c:87
void split_by_whitespaces(const os_char *str, struct args *buffer)
Definition args.c:52
os_char * transform_quotes(const os_char *str)
Transform whitespaces inside quotes into MAGIC_TOKEN
Definition args.c:16
void re_transform_arg(os_char *arg)
Transform MAGIC_TOKEN inside arg into space.
Definition args.c:108

References args::argc, args::argv, args::background, re_transform_arg(), split_by_whitespaces(), transform_quotes(), and verify_background().

Referenced by cmd_init_from_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_whitespace()

bool is_whitespace ( os_char c)

!

Helper function

Definition at line 177 of file args.c.

177 {
178 return c == ' ' || c == '\t' || c == '\n';
179}

Referenced by split_by_whitespaces(), and transform_quotes().

Here is the caller graph for this function: