-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdLn.c
More file actions
executable file
·41 lines (34 loc) · 1.34 KB
/
cmdLn.c
File metadata and controls
executable file
·41 lines (34 loc) · 1.34 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
#include"cmdLn.h"
void tokenizeCmdLn( const char *const line, struct cmln *cls ) {
if ( cls == NULL ) { return; }
cls->original = (char *)calloc( strlen( line ) + 1, sizeof(char) );
strcpy( cls->original, line );
char *tmp = strrchr( cls->original, '\n' );
if ( tmp ) { *tmp = (char)0; }
char *working = (char *)calloc( strlen( line ) + 1, sizeof(char) );
strcpy( working, line );
// for now ignore separating out > < | ` ' " ;
// and count tokens
cls->tokc = 0;
char *c = working;
while( *c ) { // until \0
if ( !isspace( *c ) ) { // have a token
++cls->tokc;
++c;
while( !isspace( *c ) ) { ++c; }
}
// have a space
++c;
}
cls->tokv = (char **)calloc( cls->tokc + 1, sizeof(char*) ); // unchecked allocation
int cur = 0;
char *saveptr;
c = strtok_r( working, " \t\n\r", &saveptr );
while( c ) {
cls->tokv[cur] = (char *)calloc( strlen( c ) + 1, sizeof(char) );
strcpy( cls->tokv[cur], c );
++cur;
c = strtok_r( NULL, " \t\n\r", &saveptr );
}
free( working );
}