-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmsh.cpp
994 lines (921 loc) · 28.4 KB
/
dmsh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/**************************************************
* *
* { DMSH } *
* *
* A minimal shell built using the api *
* provided by the standard linux kernel *
* *
* AUTHORS: GROUP - 23 *
* *
* 1. DEBAJYOTI DASGUPTA (18CS30051) *
* *
* 2. SHUBHAM MISHRA (18CS10066) *
* *
* LANGUAGE: C++17 *
* COURSE: OPERATING SYSTEMS LAB *
* YEAR: 2020-2021 SPRING *
* *
* DISCLAIMER *
* ----------- *
* Please note the following shell has been *
* built solely for the purpose of education. *
* Any misuse of any part of the code is *
* strictly prohibited. The following code is *
* Open Sourced and can be modified and *
* redistributed solely for the purpose of *
* education. *
* *
**************************************************/
#include <map>
#include <regex>
#include <cstdio>
#include <vector>
#include <string>
#include <csignal>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/fcntl.h>
/**
* Structure of commands:
*
* 1. COMMAND consists of BLOCKs separated by &&
*
* 2. BLOCK consists of ATOMs separated by '|' and
* may terminate with '&'
*
* 3. ATOM consists of ([RUNTIME_ENV_VARS], PROGRAM,
* [ARGs], < INPUT_STREAM, >/>> OUTPUT_STREAM)
*
* >>> Example:
* >>> cmd abc && A=1 ./a.out < b.txt | VARI='abc' execute cde > out.txt &
* >>> |--ATOM--| |-------ATOM-------| |-------------ATOM---------------|
* >>> |-BLOCK--| |------------------BLOCK--------------------------------|
* >>> |---------------------------COMMAND------------------------------------|
*
* 4. The COMMANDS be taken as a std::string and
* will be utilizing the parsing utilities for
* the further processing of the string
*
* 5. The final splitted block will be stored in
* Command structure
*/
/************************************
* *
* CONTAINER STRUCTURES *
* *
************************************/
/**
* Following is the most basic unit of the
* shell program. Atoms consist of the
* following components:
*
* * [RUNTIME_ENV_VARS]
* * PROGRAM
* * [ARGs]
* * < INPUT_STREAM
* * >/>> OUTPUT_STREAM
*
* Atoms dont contain | or &&
*
* Output modes:
* -------------
* 0 -> Append
* 1 -> Write
*/
struct Atom
{
std::map<std::string, std::string> RuntimeVars;
std::string Program;
std::vector<std::string> Args;
std::string InputStream;
std::string OutputStream;
bool OutputMode;
};
/**
* Following structure contains block
* of code that are executed in one
* pass. That is the block will contain
* no &&
*
* IsBackgroundProcess -> boolean
* tells whether the following
* block code is running the
* background or not
*/
struct Block
{
std::vector<Atom *> Atoms;
bool IsBackgroundProcess;
};
/**
* Structure for containing the fully parsed
* command. The structure command is storing
* a collection of the block statement and
* is the top level structure in the parsed
* hierarchy of commands.
*/
struct Command
{
std::vector<Block *> Blocks;
};
/**********************************************
* GLOBAL TABLES *
* --------------- *
* global_envp = global environment varables *
* running_jobs = all jobs that are currently *
* running *
**********************************************/
std::vector<char *> global_envp;
std::vector<pid_t> running_jobs;
std::vector<Command *> queue;
/************************************
* *
* NAMESPACES *
* AND *
* FUNCTION DECLARATIONS *
* *
************************************/
/**
* The following namespace contains the definations
* of the utility fuinctions that are utilized for
* helping in the execution of the commands and for
* signal handling.
*/
namespace utility
{
std::string getPrompt();
void signal_callback_handler(int);
char **strToChrArr(std::string, const std::vector<std::string> &);
char **constructEnvArr(std::map<std::string, std::string>);
} // namespace utility
/**
* The following namespace contains the
* commands to execute the kernel bulitin
* commands
*/
namespace builtin
{
int cd(std::vector<std::string> &);
int info(std::vector<std::string> &);
int help(std::vector<std::string> &);
int exit(std::vector<std::string> &);
int history(std::vector<std::string> &);
int exportEnv(std::vector<std::string> &);
std::map<std::string, int (*)(std::vector<std::string> &)> builtin_commands = {
{"cd", &cd},
{"exit", &exit},
{"info", &info},
{"help", &help},
{"export", &exportEnv},
{"history", &history}};
} // namespace builtin
/**
* The following namespace contains the
* functions that are required for the
* execution of the commands. Each of the
* functions have different tasks w.r.t
* the hierarchy of commands where they
* appear
*/
namespace executors
{
int execSingleCmd(Atom *, bool);
int execute_atom(Atom *, bool);
int execute_block(Block *);
int execute(Command *);
} // namespace executors
/**
* The following namespace contains the function
* that are utilized for parsing the commands.
* The namespace also contains commands that are
* utilized for the splitting of the strings and
* trimming space related characters.
*
* A TOP DOWN parser has been implemented for the
* parsing of the program. That is the parse tree
* will be contstructed from the root down to the
* leaves.
*/
namespace parser
{
std::string trim(const std::string &trimStr);
std::vector<std::string> splitString(const std::string &, const std::string &);
Atom *getAtom(std::string &);
Block *getBlock(std::string &);
Command *Parse(const std::string &);
} // namespace parser
/**
* ----------------
* MAIN EVENT LOOP
* ----------------
*
* Following function has the main event loop
* that the terminals follows. the following
* states are processed in the event loop.
*
* PROCESS LIFE CYCLE
* <-------
* IDLE ----> READY RUN -------> TERMINATED
* \ ------> /
* \ /
* \ /
* WAIT
*
* 1. We start by setting up the global environment
* variables. Followed by we initialize the event
* handler for SIGINT interrupt.
*
* 2. Set up the prompt that willl appear showin the
* current directory we are present
*
* 3. within an infinite loop we take in command from
* the user in the form of std::string and send it
* to the parser for further processing
*
* 4. The parser will return a command object. This
* command is then send for execution to the function
* provided in the namespace executors for the handling
* of the execution of this command object.
*
* 5. Once the process if finished execution the loop repeats
*/
int main(int argc, char *argv[], char *envp[])
{
std::string cmd;
int i = 0;
while (envp[i] != NULL)
global_envp.push_back(envp[i++]);
signal(SIGINT, utility::signal_callback_handler);
setenv("PS1", "$ ", 0);
// 0 --> Don't replace already existing value
while (true)
{
std::cout << utility::getPrompt() << std::flush;
std::getline(std::cin, cmd);
Command *command = parser::Parse(cmd);
if (cmd.find("history") == std::string::npos)
queue.push_back(command);
executors::execute(command);
}
}
/******************************
* *
* FUNCTION DEFINATIONS *
* *
******************************/
/**
* Following function is a signal handler for CTRL+C
* signal which raises a SIGINT interrupt. When this
* interrupt is raised all the running process must
* be stopped and the normal execution of the shell
* must be resumed.
*
* For the we send a SIGTERM signal that is terminate
* the running program signal along with the pid of the
* job that needs to be killed.
*
* Once all the running jobs are killed return to the
* normal execution of the shell.
*/
void utility::signal_callback_handler(int signum)
{
int status;
pid_t cpid;
for (auto &proc : running_jobs)
{
kill(proc, SIGTERM);
cpid = waitpid(proc, &status, 0);
}
std::cout << std::endl
<< "Stopped all processes!!" << std::endl;
return;
}
/**
* The following function is used to bring
* up the display prompt for the user, so
* that the user is aware of the space where
* the command is to be entered.
*
* The prompt string will be concatenation of
* the value of evironment variable PS1 and
* the current working directory
*/
std::string utility::getPrompt()
{
std::string ps1(getenv("PS1"));
std::string user(getenv("USER"));
std::string dir(getcwd(nullptr, 0));
return "\033[1;32m" + user + ":" + "\033[1;31m" + dir + " " + ps1 + "\033[0m";
}
/**
* The following function converts a vector
* of string into a array of character
* pointers. this is particularly useful for
* the functions provided by the kernel like
* execvpe that requre the arguments in the
* for of traditional char**
*/
char **utility::strToChrArr(std::string prog, const std::vector<std::string> &args)
{
char **Args = (char **)malloc((args.size() + 2) * sizeof(char *));
Args[0] = (char *)prog.c_str();
for (int i = 0; i < args.size(); ++i)
{
Args[i + 1] = (char *)args[i].c_str();
}
Args[args.size() + 1] = NULL;
return Args;
}
/**
* The following funtion converts the
* map of string, string that contains
* the mapping so the environment
* variables into the array of char
* pointers so that it can be passed as
* an argument for the kernel functions
* that take environment variables in
* the traditional char** format like
* >>> execvpe(_ , _ , char** envs)
*/
char **utility::constructEnvArr(std::map<std::string, std::string> env)
{
char **envarr = new char *[global_envp.size() + env.size() + 1];
int i = 0;
for (auto s : global_envp)
{
envarr[i] = new char[strlen(s) + 1];
sprintf(envarr[i], "%s", s);
i++;
}
for (auto it : env)
{
if (it.first.length() < 1)
continue;
envarr[i] = new char[it.first.length() + it.second.length() + 2];
sprintf(envarr[i++], "%s=%s", it.first.c_str(), it.second.c_str());
}
envarr[i] = NULL;
return envarr;
}
/**
* The following function handles the
* execution of the builtin change dir
* function using the `chdir` api
* provided by the kernel.
*
* If no argument is provided it is
* assumed that the directry is to be
* changed to the `HOME` directory
* of the user denoted by '~'.
*
* Otherwise change to the directory
* provided in the argument.
*/
int builtin::cd(std::vector<std::string> &args)
{
int exec_status;
if (args.empty())
{
std::string new_dir = "/home/" + std::string(getenv("USER"));
exec_status = chdir(new_dir.c_str());
}
else if (args[0][0] == '~')
{
std::string new_dir = "/home/" + std::string(getenv("USER"));
new_dir = new_dir + args[0].substr(1);
exec_status = chdir(new_dir.c_str());
}
else
{
exec_status = chdir(args[0].c_str());
if (exec_status)
perror("cd");
}
return exec_status;
}
int builtin::help(std::vector<std::string> &args)
{
std::cout << "\
BUILTIN COMMANDS\n\
----------------\n\
cd [OPTIONAL Path] : change directory to the given path\n\
exit : Exit from the shell. Stops all running processes\n\
info : Info about the authors\n\
export [CLAUSE] [OPTIONAL] : Export environment variables\n\
history [NUMBER] : Execute N th from the last command"
<< std::endl;
return 0;
}
/**
* The following function is
* used to execute the [N]th
* command from the last. It
* pulls out the command from
* the command queue and then
* executes the program using
* the command helper function
*/
int builtin::history(std::vector<std::string> &args)
{
int n = atoi(args[0].c_str());
if (n > queue.size())
return 1;
executors::execute(queue[queue.size() - n]);
return 0;
}
/**
* The following funtion handles the
* execution of exit command using the
* exit api provided by processes module
*
* Before exiting from the dshell all the
* currently running process, both
* backgropund and the foreground needs
* to be terminated. Following that exit
* from the terminal and the exit status
* will be the error status (if any) while
* terminating the running tasks
*/
int builtin::exit(std::vector<std::string> &args)
{
int status;
pid_t cpid;
for (auto &proc : running_jobs)
{
kill(proc, SIGTERM);
cpid = waitpid(proc, &status, 0);
}
std::exit(status);
return status;
}
/**
* The following function handles
* the export environment variables
* functionality of the shell. This
* function adds the variable that
* have been exported and adds it
* to the global variable list
*/
int builtin::exportEnv(std::vector<std::string> &args)
{
try
{
std::string buff = args[0];
for (int i = 1; i < args.size(); i++)
{
buff += args[i];
}
global_envp.push_back((char *)buff.c_str());
std::cout << buff << std::endl;
}
catch (...)
{
perror("Invalid Command");
}
return 0;
}
/**
* The following function is built just to
* provide information about the projects
* and its authors
*/
int builtin::info(std::vector<std::string> &)
{
std::cout << " **************************************************\n\
* *\n\
* \033[0;33m{ DMSH }\033[0m *\n\
* *\n\
* A minimal shell built using the api *\n\
* provided by the standard linux kernel *\n\
* *\n\
* \033[1;35mAUTHORS:\033[1;36m GROUP - 23\033[0m *\n\
* *\n\
* \033[1;32m1. DEBAJYOTI DASGUPTA (18CS30051) *\n\
* [email protected]\033[0m *\n\
* *\n\
* \033[1;32m2. SHUBHAM MISHRA (18CS10066) *\n\
* [email protected]\033[0m *\n\
* *\n\
* \033[1;35mLANGUAGE: \033[0;33mC++17 \033[0m *\n\
* \033[1;35mCOURSE: \033[0;33mOPERATING SYSTEMS LAB\033[0m *\n\
* \033[1;35mYEAR: \033[0;33m2020-2021 SPRING \033[0m *\n\
* *\n\
* \033[1;31mDISCLAIMER\033[0m *\n\
* ----------- *\n\
* Please note the following shell has been *\n\
* built solely for the purpose of education. *\n\
* Any misuse of any part of the code is *\n\
* strictly prohibited. The following code is *\n\
* Open Sourced and can be modified and *\n\
* redistributed solely for the purpose of *\n\
* education. *\n\
* *\n\
**************************************************"
<< std::endl;
return 0;
}
/**
* The following function handles
* the execution of a single line
* function that is stored a Atom
* object.
*
* 1. Convert the args to char**
* 2. Convert the env Vars to char**
* 3. Fork to create a child process
*
* On succesful creation of the child
* process we get either 0 or the pid
* of the child process so created.
* Wait teh child process to finish
* execution.
*/
int executors::execSingleCmd(Atom *a, bool bg)
{
char **Args = utility::strToChrArr(a->Program, a->Args);
char **Envs = utility::constructEnvArr(a->RuntimeVars);
if (strlen(Args[0]) == 0)
return 0;
int pid = fork(), status;
if (pid == 0)
{
running_jobs.push_back(getpid());
if (bg)
setpgid(0, 0);
status = execvpe(Args[0], Args, Envs);
if (status == -1)
{
perror(strerror(errno));
}
return errno;
}
else
{
waitpid(pid, &status, 0);
}
return status;
}
/**
* Following function is the main handler
* of any execution related to statement
* stored as an atom, that is the most
* basic command in the shell.
*
* For this we find whether the program
* is part of any of the predefined builtin
* functions. If so then send the program to
* the builtin function handler for further
* execution.
*
* Otherwise make use of the single command
* handler utility provided in the executor
* namespace defined.
*
* Boolean type bg menstions whether the
* program should be sent into the
* background for running
*/
int executors::execute_atom(Atom *a, bool bg)
{
int exec_val;
std::string cmd = a->Program;
transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
if (builtin::builtin_commands.find(cmd) != builtin::builtin_commands.end())
{
return builtin::builtin_commands[cmd](a->Args);
}
else
{
exec_val = execSingleCmd(a, bg);
}
return exec_val;
}
/**
* Following function is the main function
* for handling the execution of the block
* level statements. Block level statements
* may contain pipes, so pipes are handled
* as a part of this code.
*
* all the atom commands that are contained
* the current block are executed sequentially
* in a loop. In the loop the pipes are handled
* where output from prvious iteration is sent
* to the next iteration atom command execution.
*
* For the last element of the loop no further
* piping is required hence the input and out-
* -put streams are set manually.
*
* The execute_atom function handles the execution
* of the atom commands. Finally restore the stdin
* file descriptor and the stdout file descriptor.
*/
int executors::execute_block(Block *b)
{
int stdin_copy = dup(STDIN_FILENO);
int stdout_copy = dup(STDOUT_FILENO);
int fdin;
// Redirections are only taken from the last command of the block
if (!b->Atoms[b->Atoms.size() - 1]->InputStream.empty())
{
fdin = open(b->Atoms[b->Atoms.size() - 1]->InputStream.c_str(), O_RDONLY);
}
else
{
fdin = dup(stdin_copy);
}
int fdout;
for (int i = 0; i < b->Atoms.size(); i++)
{
dup2(fdin, STDIN_FILENO);
close(fdin);
if (i == b->Atoms.size() - 1)
{
if (b->Atoms[i]->OutputStream.length() > 0)
{
FILE *out;
if (b->Atoms[i]->OutputMode == 1)
{
out = fopen(b->Atoms[i]->OutputStream.c_str(), "w");
}
else
{
out = fopen(b->Atoms[i]->OutputStream.c_str(), "a");
}
fdout = fileno(out);
}
else
{
fdout = dup(stdout_copy);
}
}
else
{
int fdes[2];
int res = pipe(fdes);
if (res == -1)
{
perror("pipe");
exit(-1);
}
fdout = fdes[1];
fdin = fdes[0];
}
dup2(fdout, STDOUT_FILENO);
close(fdout);
execute_atom(b->Atoms[i], b->IsBackgroundProcess);
}
int status = 0;
if (!b->IsBackgroundProcess)
wait(&status);
else
std::cerr << "Command sent to background" << std::endl;
dup2(stdin_copy, STDIN_FILENO);
dup2(stdout_copy, STDOUT_FILENO);
close(stdin_copy);
close(stdout_copy);
return status;
}
/**
* Following function performs the task of
* handling the execution of each of the
* command block, which the largest unit in
* the parse tree. The function makes use of
* the execute_block function for handling
* the execution of each block stored in it.
*/
int executors::execute(Command *c)
{
int exec_val;
for (const auto &blk : c->Blocks)
{
exec_val = execute_block(blk);
}
return exec_val;
}
/**
* The following function helps in pre processing
* the string. This function is a utility function
* specifically for the purpose of parsing . This
* function removes any white/space character from
* the beginning and the end of the string thus
* cleaning up the string for further processing
*/
std::string parser::trim(const std::string &trimStr)
{
std::string str(trimStr);
// trim spaces from the front
std::reverse(str.begin(), str.end());
while (isspace(str.back()))
str.pop_back();
// trim spaces from the end
std::reverse(str.begin(), str.end());
while (isspace(str.back()))
str.pop_back();
return str;
}
/**
* Following funcion is a utility function
* specifically for the purpose of parsing
* The function is used to split the string
* as per the delimeter passed as the arg
* regexPattern. This function makes use of
* the in-built regex library.
*/
std::vector<std::string> parser::splitString(const std::string &stringToSplit, const std::string ®exPattern)
{
std::vector<std::string> result;
const std::regex rgx(regexPattern);
// -1 refers to capture all the substrings generated after splitting
std::sregex_token_iterator iter(stringToSplit.begin(), stringToSplit.end(), rgx, -1);
for (std::sregex_token_iterator end; iter != end; ++iter)
{
result.push_back(trim(iter->str()));
}
return result;
}
/**
* The following function is the main function
* responsible for parsing the atom command
* statement that is sent as a std::string
* parameter to the function. This function
* makes use of regex expressions for capturing
* the different parts of the Atom object.
*
* the regex experession captures the
* following groups :-
* 0 => Full string
* 1 => Runtime vars
* 2 => Program
* 3 => Args
* 4 => Redirects
*
* With the help of the redirects the
* function sets the values for the
* Input Stream and the Output Stream
*
* If redirect string length is 2,
* then as per the regex used the
* Output Mode will be set to 1
* which is the append mode.
*
* The arguments and the redirect
* strings that were captured are
* further passed though the argRgx
* and the redirRgx for capturing the
* values. Finally using the values
* that were extracted using the regex
* expressions are used to set the
* valus of the fields of atom object.
*/
Atom *parser::getAtom(std::string &cmd)
{
Atom *atom = new Atom();
atom->OutputStream = "";
atom->InputStream = "";
atom->OutputMode = 0;
std::smatch matches;
std::regex rgx("((?:[a-zA-Z0-9-_]+=(?:(?:\"[^\"]*\")|(?:\'[^\']*\')|(?:[^ \'\"]*)) )*)([a-zA-Z0-9-_./]+)( [^><]*)? *((?:<|>>|>).*)?");
if (regex_search(cmd, matches, rgx))
{
atom->Program = trim(matches[2].str());
atom->InputStream = "";
atom->OutputStream = "";
std::string vars = trim(matches[1].str());
std::regex varRgx("(([a-zA-Z0-9-_]+)=((?:\"[^\"]*\")|(?:\'[^\']*\')|(?:[^ \'\"]*)))");
std::smatch varMatch;
while (std::regex_search(vars, varMatch, varRgx))
{
atom->RuntimeVars[varMatch[2].str()] = varMatch[3].str();
vars = varMatch.suffix().str();
}
std::string args = matches[3].str();
std::regex argRgx("((?:\"[^\"]*\")|(?:\'[^\']*\')|(?:[^ \'\"]+))");
std::smatch argMatches;
while (std::regex_search(args, argMatches, argRgx))
{
std::string s = argMatches[0].str();
if (s.length() > 0 && (s[0] == '"' || s[0] == '\''))
{
s.erase(s.begin());
s.erase(s.end() - 1);
}
atom->Args.push_back(s);
args = argMatches.suffix().str();
}
std::string redirs = trim(matches[4].str());
std::regex redirRgx("((?:<|>>|>) *[^ ><]*)");
std::smatch redirMatches;
while (std::regex_search(redirs, redirMatches, redirRgx))
{
std::string redirStr = redirMatches[0].str();
if (redirStr[0] == '<')
{
redirStr.erase(redirStr.begin());
redirStr = trim(redirStr);
if (redirStr.length() > 0)
atom->InputStream = redirStr;
}
else
{
if (redirStr.length() >= 2)
{
if (redirStr[1] == '>')
{
redirStr.erase(redirStr.begin());
atom->OutputMode = 0;
}
else
{
atom->OutputMode = 1;
}
redirStr.erase(redirStr.begin());
redirStr = trim(redirStr);
if (redirStr.length() > 0)
atom->OutputStream = redirStr;
}
}
redirs = redirMatches.suffix().str();
// Multiple matches will overwrite
}
}
return atom;
}
/**
* The following function is the main
* function that handles the parsing
* of the block statements that are
* passed as a std::string parameter.
*
* getBlock make use of the getAtom
* utility function in the parser
* namespace that for further parsing
* and creating of the Atom objects
*
* The IsBackgroundProcess is a boolean
* field which keeps track of whether the
* code is to be run in the background or
* not.
*
* The split string command is used
* to split the block string into
* atom string. The atom string do
* not contain any "|" hence we use
* "|" as the delimiter for the
* splitting of string. "|" -> PIPE
*/
Block *parser::getBlock(std::string &cmd)
{
Block *blk = new Block();
if (*(cmd.end() - 1) == '&')
{
// Background process
blk->IsBackgroundProcess = true;
cmd.erase(cmd.end() - 1);
}
else
{
blk->IsBackgroundProcess = false;
}
cmd = trim(cmd);
auto singles = splitString(cmd, "\\|");
for (auto s : singles)
{
blk->Atoms.push_back(getAtom(s));
}
return blk;
}
/**
* The following function is the main
* function that handles the parsing
* of the command statements that are
* passed as a std::string parameter.
*
* Parse make use of the getBlock
* utility function in the parser
* namespace that for further parsing
* and creating of the block objects
*
* The split string command is used
* to split the command string into
* block string. The block string do
* not contain any "&&" hence we use
* "&&" as the delimiter for the
* splitting of string.
*/
Command *parser::Parse(const std::string &cmd)
{
auto singles = splitString(cmd, "&&");
Command *cmds = new Command();
for (auto &s : singles)
{
cmds->Blocks.push_back(getBlock(s));
}
return cmds;
}