work/bootstrap/gec17.c
void GE_get_argcargv(EIF_NATIVE_CHAR* cmd, int* argc, EIF_NATIVE_CHAR*** argvp)
{
	int quoted = 0; /* parsing inside a quoted string? */
	int nbs; /* number of backspaces */
	int i;
	EIF_NATIVE_CHAR *p = NULL, *pe = NULL; /* pointers in `cmd' */
	EIF_NATIVE_CHAR *qb = NULL, *q = NULL; /* pointers in arguments */

	*argc = 0;
	/* Remove leading and trailing white spaces */
	for (p = cmd; *p == L' ' || *p == L'\t'; p++)
		; /* empty */
	for (pe = p + GE_nstrlen(p) - 1; pe >= p && (*pe == L' ' || *pe == L'\t'); pe--)
		; /* empty */
	if (p <= pe) {
		*argc = *argc + 1; /* at least one argument */
		qb = q = malloc((pe - p + 2) * sizeof(EIF_NATIVE_CHAR));
		if (!qb) {
			return;
		}
		do {
			switch(*p) {
				case L' ':
				case L'\t':
					if (quoted) {
						do {
							*q++ = *p++;
						} while(*p == L' ' || *p == L'\t');
					} else {
						do {
							p++;
						} while(*p == L' ' || *p == L'\t');
						*q++ = L'\0';
						*argc = *argc + 1;
					}
					break;
				case L'\"':
					quoted = ! quoted;
					p++;
					break;
				case L'\\':
					for (nbs = 0; *p == L'\\'; nbs++)
						*q++ = *p++;
					if (*p == L'\"') {
						if (nbs % 2) { /* odd number of backslashes */
							q -= (nbs + 1) / 2;
							*q++ = *p++;
						} else { /* even number of backslashes */
							quoted = ! quoted;
							q -= nbs / 2;
							p++;
						}
					}
					break;
				default:
					*q++ = *p++;
			}
		} while (p <= pe);
		*q++ = L'\0';
	}

	if (!argvp) {
		free(qb);
		return;
	}

	*argvp = (EIF_NATIVE_CHAR**)malloc((*argc+1)*sizeof(EIF_NATIVE_CHAR*));
	if (!(*argvp)) {
		free(qb);
		return;
	}

	for (i = 0; i < *argc; i++) {
		(*argvp)[i] = qb;
		qb += GE_nstrlen(qb) + 1;
	}
	(*argvp)[i] = (EIF_NATIVE_CHAR *)0;
}
/*
	Break the shell command held in 'cmd', putting each shell word
	in a separate array entry, hence building an argument
	suitable for the 'main'. Note that 'cmd' will be altered
	and 'argvp' will point to some chunks of it.
*/
Break the shell command held in 'cmd', putting each shell word
in a separate array entry, hence building an argument
suitable for the 'main'. Note that 'cmd' will be altered
and 'argvp' will point to some chunks of it.
Break the shell command held in ' cmd ' putting each shell word in a separate array entry hence building an argument suitable for the ' main ' . Note that ' cmd ' will be altered and ' argvp ' will point to some chunks of it .
void GE_get_argcargv(EIF_NATIVE_CHAR* cmd, int* argc, EIF_NATIVE_CHAR*** argvp)
{
	int quoted = 0; 
	int nbs; 
	int i;
	EIF_NATIVE_CHAR *p = NULL, *pe = NULL; 
	EIF_NATIVE_CHAR *qb = NULL, *q = NULL; 
	*argc = 0;
	for (p = cmd; *p == L' ' || *p == L'\t'; p++)
		; 
	for (pe = p + GE_nstrlen(p) - 1; pe >= p && (*pe == L' ' || *pe == L'\t'); pe--)
		; 
	if (p <= pe) {
		*argc = *argc + 1; 
		qb = q = malloc((pe - p + 2) * sizeof(EIF_NATIVE_CHAR));
		if (!qb) {
			return;
		}
		do {
			switch(*p) {
				case L' ':
				case L'\t':
					if (quoted) {
						do {
							*q++ = *p++;
						} while(*p == L' ' || *p == L'\t');
					} else {
						do {
							p++;
						} while(*p == L' ' || *p == L'\t');
						*q++ = L'\0';
						*argc = *argc + 1;
					}
					break;
				case L'\"':
					quoted = ! quoted;
					p++;
					break;
				case L'\\':
					for (nbs = 0; *p == L'\\'; nbs++)
						*q++ = *p++;
					if (*p == L'\"') {
						if (nbs % 2) { 
							q -= (nbs + 1) / 2;
							*q++ = *p++;
						} else { 
							quoted = ! quoted;
							q -= nbs / 2;
							p++;
						}
					}
					break;
				default:
					*q++ = *p++;
			}
		} while (p <= pe);
		*q++ = L'\0';
	}
	if (!argvp) {
		free(qb);
		return;
	}
	*argvp = (EIF_NATIVE_CHAR**)malloc((*argc+1)*sizeof(EIF_NATIVE_CHAR*));
	if (!(*argvp)) {
		free(qb);
		return;
	}
	for (i = 0; i < *argc; i++) {
		(*argvp)[i] = qb;
		qb += GE_nstrlen(qb) + 1;
	}
	(*argvp)[i] = (EIF_NATIVE_CHAR *)0;
}
void GE_get_argcargv ( EIF_NATIVE_CHAR * cmd , int * argc , EIF_NATIVE_CHAR * * * argvp ) { int quoted = 0 ; int nbs ; int i ; EIF_NATIVE_CHAR * p = NULL , * pe = NULL ; EIF_NATIVE_CHAR * qb = NULL , * q = NULL ; * argc = 0 ; for ( p = cmd ; * p == L' ' || * p == L' \t ' ; p ++ ) ; for ( pe = p + GE_nstrlen ( p ) - 1 ; pe >= p && ( * pe == L' ' || * pe == L' \t ' ) ; pe -- ) ; if ( p <= pe ) { * argc = * argc + 1 ; qb = q = malloc ( ( pe - p + 2 ) * sizeof ( EIF_NATIVE_CHAR ) ) ; if ( ! qb ) { return ; } do { switch ( * p ) { case L' ' : case L' \t ' : if ( quoted ) { do { * q ++ = * p ++ ; } while ( * p == L' ' || * p == L' \t ' ) ; } else { do { p ++ ; } while ( * p == L' ' || * p == L' \t ' ) ; * q ++ = L' \0 ' ; * argc = * argc + 1 ; } break ; case L' \" ' : quoted = ! quoted ; p ++ ; break ; case L' \\ ' : for ( nbs = 0 ; * p == L' \\ ' ; nbs ++ ) * q ++ = * p ++ ; if ( * p == L' \" ' ) { if ( nbs % 2 ) { q -= ( nbs + 1 ) / 2 ; * q ++ = * p ++ ; } else { quoted = ! quoted ; q -= nbs / 2 ; p ++ ; } } break ; default : * q ++ = * p ++ ; } } while ( p <= pe ) ; * q ++ = L' \0 ' ; } if ( ! argvp ) { free ( qb ) ; return ; } * argvp = ( EIF_NATIVE_CHAR * * ) malloc ( ( * argc + 1 ) * sizeof ( EIF_NATIVE_CHAR * ) ) ; if ( ! ( * argvp ) ) { free ( qb ) ; return ; } for ( i = 0 ; i < * argc ; i ++ ) { ( * argvp ) [ i ] = qb ; qb += GE_nstrlen ( qb ) + 1 ; } ( * argvp ) [ i ] = ( EIF_NATIVE_CHAR * ) 0 ; }
Break the shell command held in 'cmd', putting each shell word
in a separate array entry, hence building an argument
suitable for the 'main'.
Break the shell command held in ' cmd ' putting each shell word in a separate array entry hence building an argument suitable for the ' main ' .
/* parsing inside a quoted string? */ /* number of backspaces */ /* pointers in `cmd' */ /* pointers in arguments */ /* Remove leading and trailing white spaces */ /* empty */ /* empty */ /* at least one argument */ /* odd number of backslashes */ /* even number of backslashes */