#Feedback needed for my UNIX shell parser and tokenizer

11 messages · Page 1 of 1 (latest)

jade thorn
flat roostBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

weary forge
#

You might prefer dynamic arrays over a linked list, since you only appending items but never deleting them. With using array list on Tokens you can allocate Command array once on parse_cmds and never need to resize it later

You can change

size_t cmd_count = 1;
Command *cmd = malloc(cmd_count * sizeof(Command)); // then reallocating several times

to

size_t cmd_count = token_list->size;
Command *cmd = malloc(cmd_count * sizeof(Command)); // allocation just for once

And you don't always need to free memory after use if it's freed at process exit, so you can direct move some string pointers instead of strdup


This one is a little bit about code writing style but you can use

char c;
size_t i = 0;
while (c = str[i++]) {
...

instead of

size_t length = strlen(str);
char c;
size_t i = 0;
while (length > i) {
  c = str[i];
...

so you won't need to write i++ before every continue

jade thorn
weary forge
#

No, I meant you can use dynamic array insted of linked list since you only append new items to it and if you use dynamic array you can allocate Command array once and never need to resize it later

#

I'm sorry, I realised that not every token is a command now

jade thorn
#

Some are operators like pipes and redirect symbols.

#

I allocate a new cmd only when i encounter an operator

jade thorn
#

When encountering let’s say < or > should the parser allocate new memory to the command or not?

#

Because for pipes I do need that due to forks