-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstar.c
More file actions
159 lines (137 loc) · 3.41 KB
/
star.c
File metadata and controls
159 lines (137 loc) · 3.41 KB
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
#include "star.h"
/*
* RTEMS Coding Style:
* - 2 spaces indentation
* - No tabs
* - Function names: lower_case_with_underscores
* - control statements: space after keyword (if, while, for)
*/
static void sigint_handler( int sig )
{
(void) sig;
printf( "\n" );
}
static void sigtstp_handler( int sig )
{
(void) sig;
/* Ignore Ctrl+Z in shell itself */
}
void setup_signal_handlers( void )
{
signal( SIGINT, sigint_handler );
signal( SIGTSTP, sigtstp_handler );
signal( SIGCHLD, SIG_IGN );
}
static void get_cwd( char *buf, size_t size )
{
if ( NULL == getcwd( buf, size ) ) {
perror( "getcwd failed" );
}
}
char *read_line( void )
{
char *buf = NULL;
size_t bufsize = 0;
char cwd[PATH_MAX];
get_cwd( cwd, sizeof( cwd ) );
printf( BLUE " %s " RST WHITE "*> " RST, cwd );
if ( getline( &buf, &bufsize, stdin ) == -1 ) {
free( buf );
if ( feof( stdin ) ) {
printf( RED "[EOF]\n" RST );
exit( EXIT_SUCCESS );
}
perror( RED "Getline failed" RST );
exit( EXIT_FAILURE );
}
return buf;
}
/*
* Custom tokenizer to handle quotes and whitespace
*/
char **tokenize_input( char *line )
{
int bufsize = BUFSIZ;
int position = 0;
char **tokens = Malloc( bufsize * sizeof( char* ) );
char *token;
char *cursor = line;
if ( !tokens ) {
fprintf( stderr, "start: allocation error\n" );
exit( EXIT_FAILURE );
}
while ( *cursor != '\0' ) {
/* Skip whitespace */
while ( *cursor == ' ' || *cursor == '\t' || *cursor == '\n' || *cursor == '\r' ) {
*cursor = '\0';
cursor++;
}
if ( *cursor == '\0' ) {
break;
}
token = cursor;
if ( *cursor == '"' || *cursor == '\'' ) {
char quote = *cursor;
cursor++;
token = cursor; /* Start of actual token content */
while ( *cursor != '\0' && *cursor != quote ) {
cursor++;
}
if ( *cursor == '\0' ) {
fprintf( stderr, "Error: Unclosed quote\n" );
free( tokens );
return NULL;
}
*cursor = '\0'; /* Terminate token at closing quote */
cursor++;
} else if ( *cursor == '|' || *cursor == '&' ) {
/* Special single-character tokens */
cursor++;
if ( *cursor != '\0' && *cursor != ' ' && *cursor != '\t' && *cursor != '\n' && *cursor != '\r' ) {
*cursor = '\0';
cursor++;
}
} else {
while ( *cursor != '\0' && *cursor != ' ' && *cursor != '\t' && *cursor != '\n' && *cursor != '\r' && *cursor != '|' && *cursor != '&' ) {
cursor++;
}
}
tokens[position] = token;
position++;
if ( position >= bufsize ) {
bufsize += BUFSIZ;
tokens = Realloc( tokens, bufsize * sizeof( char* ) );
if ( !tokens ) {
fprintf( stderr, "star: allocation error\n" );
exit( EXIT_FAILURE );
}
}
}
tokens[position] = NULL;
return tokens;
}
int main( void )
{
char *line;
char **args;
init_jobs();
setup_signal_handlers();
while ( ( line = read_line() ) ) {
/* Update job status before prompt */
update_job_status();
/* Skip empty lines check is implicitly handled by tokenizer returning empty list or loops */
if ( line[0] == '\n' && line[1] == '\0' ) {
free( line );
continue;
}
args = tokenize_input( line );
if ( args != NULL ) {
if ( args[0] != NULL ) {
cmd_exec( args );
}
free( args );
}
free( line );
}
return EXIT_SUCCESS;
}