aboutsummaryrefslogtreecommitdiff
path: root/src/lsh_split_line.c
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-02-28 23:49:02 -0600
committericeyrazor <iceyrazor@mailfence.com>2026-02-28 23:49:02 -0600
commit1b8967ea87c63342f03c340d44a084ea7f62cae9 (patch)
tree06b7998b94fe9b05115d59c2a584339af8943445 /src/lsh_split_line.c
parentbecbecb016f16c2dea64aacd7077973f45ed0517 (diff)
command typing
now can chain commands with just ;
Diffstat (limited to 'src/lsh_split_line.c')
-rw-r--r--src/lsh_split_line.c181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/lsh_split_line.c b/src/lsh_split_line.c
new file mode 100644
index 0000000..ff4f644
--- /dev/null
+++ b/src/lsh_split_line.c
@@ -0,0 +1,181 @@
+#ifndef split_line
+#define split_line
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include "helper_functions.c"
+
+char *handle_vars(char *token){
+ char *ret_token=(char*)malloce(sizeof(char)*strlen(token)+1);
+ strcpy(ret_token,token);
+
+ if (token[0]=='~'){
+ token++;
+ if(!getenv("HOME")){
+ fprintf(stderr,"lsh: you aint got no home. da hell\n");
+ free(ret_token);
+ ret_token=(char*)malloce(sizeof(char)*strlen(token)+1);
+ strcpy(ret_token,token);
+ return ret_token;
+ }
+ free(ret_token);
+ ret_token=(char*)malloce(sizeof(char)*strlen(token)+strlen(getenv("HOME"))+1);
+ strcpy(ret_token,getenv("HOME"));
+ strcat(ret_token,token);
+ }
+
+ char *tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1);
+ strcpy(tmp_token,ret_token);
+ int i=0;
+ while (tmp_token[i]!='\0'){
+ if(tmp_token[i]=='$'){
+ char sep[2]=" \0";
+ int j=0; while(tmp_token[i+j]){
+ j++;
+ if(tmp_token[i+j]==' ') break;
+ if(tmp_token[i+j]=='$'){
+ sep[0]='$';
+ break;
+ }
+ }
+
+ char *varname=(char*)malloce(sizeof(char)*j+1);
+ varname[j]='\0';
+ strncpy(varname,&tmp_token[i+1],j);
+ varname[j-1]='\0';
+
+ char *env = getenv(varname);
+ if(!env) env="";
+
+ free(ret_token);
+ int tmp_len=strlen(tmp_token);
+ ret_token=(char*)malloce(sizeof(char)*(strlen(tmp_token)-strlen(varname))+strlen(env)+1);
+ tmp_token[i]='\0';
+ tmp_token[i+j]='\0';
+
+ strcpy(ret_token,tmp_token);
+ strcat(ret_token,env);
+ strcat(ret_token,sep);
+ if(i+j+1 < tmp_len){
+ strcat(ret_token,&tmp_token[i+j+1]);
+ }
+
+ free(tmp_token);
+ tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1);
+ strcpy(tmp_token,ret_token);
+
+ free(varname);
+ }
+ i++;
+ }
+ free(tmp_token);
+ return ret_token;
+}
+
+char * strtok_quotes(char *token){
+ static char* parse_args_last_p;
+ if(token != NULL){
+ parse_args_last_p=token;
+ }
+ if(parse_args_last_p==NULL){
+ return NULL;
+ }
+
+ char *return_char=parse_args_last_p;
+ char in_quote=' ';
+
+ if(parse_args_last_p != NULL){
+ int ch_i=0;
+ while(1){
+ if (parse_args_last_p[ch_i] == '\0')
+ break;
+
+ if (in_quote==' ' && (parse_args_last_p[ch_i]=='"' || parse_args_last_p[ch_i]=='\'')){
+ bool pass_check=true;
+ if(ch_i>0 && parse_args_last_p[ch_i-1]=='\\')
+ pass_check=false;
+
+ if(pass_check==true){
+ in_quote=parse_args_last_p[ch_i];
+ if (parse_args_last_p[ch_i+1] == '\0')
+ break;
+ if(ch_i==0){
+ parse_args_last_p[0]='\0';
+ parse_args_last_p=&parse_args_last_p[1];
+ return_char=&return_char[1];
+ } else {
+ pop(return_char,ch_i);
+ }
+ ch_i++;
+ }
+ }
+
+ if(in_quote!=' '){
+ if (parse_args_last_p[ch_i]==in_quote){
+ bool pass_check=true;
+ if(ch_i>0 && parse_args_last_p[ch_i-1]=='\\')
+ pass_check=false;
+
+ if(pass_check==true){
+ in_quote=' ';
+ if(parse_args_last_p[ch_i+1]=='\0'){
+ parse_args_last_p[ch_i]='\0';
+ } else if(parse_args_last_p[ch_i+1]==' '){
+ parse_args_last_p[ch_i]='\0';
+ parse_args_last_p = &parse_args_last_p[ch_i+2];
+ pop(return_char,ch_i);
+ return return_char;
+ }
+ pop(return_char,ch_i);
+ }
+ }
+ } else {
+ if (parse_args_last_p[ch_i] == ' '){
+ parse_args_last_p[ch_i] = '\0';
+ parse_args_last_p = &parse_args_last_p[ch_i+1];
+ return return_char;
+ }
+ }
+ ch_i++;
+ }
+ parse_args_last_p=NULL;
+ }
+ return return_char;
+}
+
+#define LSH_TOK_BUFSIZE 64
+char **lsh_split_line(char *line)
+{
+ int bufsize = LSH_TOK_BUFSIZE, position = 0;
+ char **tokens = malloce(bufsize * sizeof(char*));
+ char *token;
+
+ token = strtok_quotes(line);
+ while (token != NULL) {
+ token = handle_vars(token);
+ tokens[position] = token;
+ position++;
+
+
+ if (position >= bufsize) {
+ bufsize += LSH_TOK_BUFSIZE;
+ tokens = realloc(tokens, bufsize * sizeof(char*));
+ if (!tokens) {
+ fprintf(stderr, "lsh: allocation error\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ token = strtok_quotes(NULL);
+ }
+ tokens[position] = NULL;
+ return tokens;
+}
+
+#endif
+