-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellTUI.c
137 lines (107 loc) · 3.45 KB
/
shellTUI.c
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
/*
* Original Author: Jonathan Hyry
* Date Originally Written: 1/30/2012
*/
#include "shellTUIHdr.h"
/*
* Function cursorWait is the actual shell prompt
* and command parsing engine.
*/
pid_t cursorWait ( void ) {
// Here is the shell prompt
printf("\n%s", SHELL_PROMPT);
fgets(commandArgs[EXECL_SH_CMD],
INPUT_ARG_SIZE,
stdin);
#ifdef DEBUG
/*Normal output*/
printf("Command string: %s", commandArgs[EXECL_SH_CMD]);
#endif
// If user entered built-in command exit...
if ( strstr ( commandArgs[EXECL_SH_CMD], SHELL_EXIT ) != NULL )
return -1;
// If the input command is a shell script
else if ( commandArgs[EXECL_SH_CMD][0] == '.' ) {
runShellacCommandBatch ( strtok ( commandArgs[EXECL_SH_CMD], "\n" ) );
printf("Batch run finished for file %s.", commandArgs[EXECL_SH_CMD]);
return -1;
}
// If the user entered cd < path >...
else if ( memcmp ( commandArgs[EXECL_SH_CMD],
&SHELL_CWDIR,
CD_CMD_LEN ) == (int)NULL ) {
// Allocate space for the directory path substring
dirStr =
(char *)malloc(INPUT_ARG_SIZE*sizeof(char));
// Clean up the newly allocated memory
memset (dirStr,
(int)NULL,
strlen(commandArgs[EXECL_SH_CMD])*sizeof(char));
#ifdef DEBUG
// DEBUG Normal output
printf("\nCommand string: %s", commandArgs[EXECL_SH_CMD]);
#endif
if ( strstr ( commandArgs[EXECL_SH_CMD], " " ) == NULL )
pathLength = 0;
else {
// The length of the actual path string in the substring
pathLength =
strlen ( strstr ( commandArgs[EXECL_SH_CMD], " " ) ) - 2;
}
if ( pathLength == 0)
strcpy(dirStr,
getenv("HOME"));
else {
#ifdef DEBUG
// DEBUG Detected string path length
printf("Detected path length: %d\n", pathLength);
#endif
// Address to the first char in the substring
subStrAddress =
strstr(commandArgs[EXECL_SH_CMD], " ");
// Copy the path string into dirStr
if(*++subStrAddress == '~' && pathLength > 2) {
getTildeHomePath ( subStrAddress, /* Parses this string into a readable
* home folder subdirectory path */
dirStr, /* Returns the resulting path here */
pathLength - 1 /* Length of subdirectory path */ );
}
else if (*subStrAddress == '~')
strcpy(dirStr,
getenv("HOME"));
else
strncpy(dirStr,
subStrAddress,
pathLength);
#ifdef DEBUG
// DEBUG Computed substring address
printf("SubStr address: 0x%x\n", (unsigned int)subStrAddress);
// DEBUG A lot better... still not really there yet.*/
printf("Passing the following to chdir(): %s\n", dirStr);
#endif
}
// Try to change the working directory
if ( chdir(dirStr) < 0) {
char * chDirErr = getError();
printf("Error %d: %s", errno, chDirErr);
free(chDirErr);
}
else {
// Get and print the Present working directory
pwdir = get_current_dir_name();
printf("\n%s\n", pwdir);
// Memory management
free(pwdir);
}
#ifdef DEBUG
// DEBUG Value before deallocation*/
printf("\nDeallocating: %s\n", dirStr);
// Deallocate directory path string
free(dirStr);
#endif
return -1;
}
// If the arguments entered are not a built-in command,
// fork this process.
return fork();
}