Hello everybody, I have been working on an UNIX shell and I have started with implementing |, >, < and have implemented them in my parse_cmd() and tokenizer functions.
I would like to here your feedback.
Thank you for your time!
parse_cmd: https://github.com/cdunku/oyster/blob/main/src/tokenizer.c#L330
tokenizer; https://github.com/cdunku/oyster/blob/main/src/tokenizer.c#L239
#Feedback needed for my UNIX shell parser and tokenizer
11 messages · Page 1 of 1 (latest)
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.
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
So you're telling me that I should allocate memory for each token for the parse_cmds during the tokenization or?
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
Some are operators like pipes and redirect symbols.
I allocate a new cmd only when i encounter an operator
One question though
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