Beispiel #1
0
void cmd_walk_and_expand (queue_t *qb)
{
    queue_t *q;
    queue_t newq;
    ui_token_t *t;
    int alias_check = TRUE;
    int insquote = FALSE;
    char *envstr;

    q_init(&newq);

    while ((t = (ui_token_t *) q_deqnext(qb))) {
	if (t->token == '\'') {
	    alias_check = FALSE;
	    insquote = !insquote;
	    /* Check to see if we should try to expand this token */
	    }
	else if (!insquote) {
	    if (alias_check && !strchr(tokenbreaks,t->token) && 
		(envstr = env_getenv(&(t->token)))) {
		/* Aliases: stick into token stream if no environment found */
		cmd_append_tokens(&newq,envstr);
		KFREE(t);
		t = NULL;
		}
	    else if (t->token == '$') {
		/* non-aliases: remove from token stream if no env found */
		envstr = env_getenv(&(t->token)+1);
		if (envstr) cmd_append_tokens(&newq,envstr);
		KFREE(t);
		t = NULL;
		}
	    else {
		/* Drop down below, keep this token as-is and append */
		}
	    }

	/*
	 * If token was not removed, add it to the new queue
	 */

	if (t) {
	    q_enqueue(&newq,&(t->qb));
	    alias_check = is_command_separator(t);
	    }

	}

    /*
     * Put everything back on the original list. 
     */

    while ((q = q_deqnext(&newq))) {
	q_enqueue(qb,q);
	}

}
Beispiel #2
0
void cmd_build_cmdline(queue_t *head, ui_cmdline_t *cmd)
{
    ui_token_t *t;
    ui_token_t *next;

    memset(cmd, 0, sizeof(ui_cmdline_t));

    t = (ui_token_t *) q_deqnext(head);

    while (t != NULL) {
	if (is_white_space(t)) {
	    /* do nothing */
	    } 
	else if (t->token != '-') {
	    if(cmd->argc < MAX_TOKENS){
		cmd->argv[cmd->argc] = cmd_eat_quoted_arg(head,t);
		cmd->argc++;
		}
	    /* Token is a switch */
	    }
	else {
	    if (cmd->swc < MAX_SWITCHES) {
		cmd->swv[cmd->swc].swname = lib_strdup(&(t->token));

		if (t->qb.q_next != head) {			/* more tokens */
		    next = (ui_token_t *) t->qb.q_next;
		    if (next->token == '=') {			/* switch has value */
			KFREE(t);				/* Free switch name */
			t = (ui_token_t *) q_deqnext(head);	/* eat equal sign */
			KFREE(t);				/* and free it */
			t = (ui_token_t *) q_deqnext(head);	/* now have value */
			if (t != NULL) {
			    cmd->swv[cmd->swc].swvalue = cmd_eat_quoted_arg(head,t);
			    }
			}
		    else {					/* no value */
			cmd->swv[cmd->swc].swvalue = NULL;
			}
		    }
		/* 
		 * swidx is the index of the argument that this
		 * switch precedes.  So, if you have "foo -d bar",
		 * swidx for "-d" would be 1.
		 */
		cmd->swv[cmd->swc].swidx = cmd->argc;
		cmd->swc++;	
		}
	    }
	KFREE(t);
	t = (ui_token_t *) q_deqnext(head);
	}


}
Beispiel #3
0
void cmd_free_tokens(queue_t *list)
{
    queue_t *q;

    while ((q = q_deqnext(list))) {
	KFREE(q);
	}
}
Beispiel #4
0
ui_command_t *cmd_readcommand(queue_t *head)
{
    char *ptr;
    int insquote = FALSE;
    int indquote = FALSE;
    ui_command_t *cmd;
    int term = CMD_TERM_EOL;
    ui_token_t *t;

    cmd_eat_leading_white(head);

    if (q_isempty(head)) return NULL;

    cmd = (ui_command_t *) KMALLOC(sizeof(ui_command_t),0);
    q_init(&(cmd->head));

    while ((t = (ui_token_t *) q_deqnext(head))) {

	ptr = &(t->token);

	if (!insquote && !indquote) {
	    if ((*ptr == ';') || (*ptr == '\n')) {
		term = CMD_TERM_SEMI;
		break;
		}	
	    if ((*ptr == '&') && (*(ptr+1) == '&')) {
		term = CMD_TERM_AND;
		break;
		}
	    if ((*ptr == '|') && (*(ptr+1) == '|')) {
		term = CMD_TERM_OR;
		break;
		}
	    }

	if (*ptr == '\'') {
	    insquote = !insquote;
	    }

	if (!insquote) {
	    if (*ptr == '"') {
		indquote = !indquote;
		}
	    }

	q_enqueue(&(cmd->head),&(t->qb));
		
	}

    cmd->term = term;

    /* If we got out by finding a command separator, eat the separator */
    if (term != CMD_TERM_EOL) {
	KFREE(t);
	}

    return cmd;
}
Beispiel #5
0
static void cmd_append_tokens(queue_t *qb,char *str)
{
    queue_t *qq;
    queue_t explist;

    cmd_build_list(&explist,str);

    while ((qq = q_deqnext(&explist))) {
	q_enqueue(qb,qq);
	}
}
Beispiel #6
0
static void console_save(unsigned char *buffer,int length)
{
    msgqueue_t *msg;

    /*
     * Get a pointer to the last message in the queue.  If 
     * it's full, preprare to allocate a new one
     */

    msg = (msgqueue_t *) console_msgq.q_prev;
    if (q_isempty(&(console_msgq)) || (msg->len == MSGQUEUESIZE)) {
	msg = NULL;
	}

    /*
     * Stuff characters into message chunks till we're done
     */

    while (length) {

	/*
	 * New chunk 
	 */
	if (msg == NULL) {

	    msg = (msgqueue_t *) KMALLOC(sizeof(msgqueue_t),0);
	    if (msg == NULL) return;
	    msg->len = 0;
	    q_enqueue(&console_msgq,(queue_t *) msg);

	    /*
	     * Remove chunks to prevent chewing too much memory
	     */

	    while (q_count(&console_msgq) > MSGQUEUEMAX) {
		msgqueue_t *dropmsg;
		dropmsg = (msgqueue_t *) q_deqnext(&console_msgq);
		if (dropmsg) KFREE(dropmsg);
		}
	    }

	/*
	 * Save text.  If we run off the end of the buffer, prepare
	 * to allocate a new one
	 */
	msg->data[msg->len++] = *buffer++;
	length--;
	if (msg->len == MSGQUEUESIZE) msg = NULL;
	}
}
Beispiel #7
0
static void console_flushbuffer(void)
{
    msgqueue_t *msg;
    char *buffer;
    int length;
    int res;

    /*
     * Remove console messages from the queue 
     */

    while ((msg = (msgqueue_t *) q_deqnext(&console_msgq))) {

	buffer = msg->data;
	length = msg->len;
	res = 0;

	/*
	 * Write each message to the console 
	 */

	for (;;) {
	    res = cfe_write(console_handle,buffer,length);
	    if (res < 0) break;
	    buffer += res;
	    length -= res;
	    if (length == 0) break;
	    }

	/*
	 * Free the storage
	 */

	KFREE(msg);
	}
}
Beispiel #8
0
static char *cmd_eat_quoted_arg(queue_t *head,ui_token_t *t)
{
    int dquote = 0;
    int squote = 0;
    queue_t qlist;
    queue_t *q;
    char *dest;
    int maxlen = 0;

    /*
     * If it's not a quoted string, just return this token.
     */

    if (!myisquote(t->token)) {
	dest = lib_strdup(&(t->token));
	/* Note: caller deletes original token */
	return dest;
	}

    /*
     * Otherwise, eat tokens in the quotes.
     */

    q_init(&qlist);

    if (t->token == '"') dquote = 1;
    else squote = 1;			/* must be one or the other */

    t = (ui_token_t *) q_deqnext(head);
    while (t != NULL) {
	/* A single quote can only be terminated by another single quote */
	if (squote && (t->token == '\'')) {
	    KFREE(t);
	    break;
	    }
	/* A double quote is only honored if not in a single quote */
	if (dquote && !squote && (t->token == '\"')) {
	    KFREE(t);
	    break;
	    }
	/* Otherwise, keep this token. */
	q_enqueue(&qlist,(queue_t *) t);
	t = (ui_token_t *) q_deqnext(head);
	}

    /*
     * Go back through what we collected and figure out the string length.
     */

    for (q = qlist.q_next; q != &qlist; q = q->q_next) {
	maxlen += strlen(&(((ui_token_t *) q)->token));
	}

    dest = KMALLOC(maxlen+1,0);
    if (!dest) return NULL;

    *dest = '\0';

    while ((t = (ui_token_t *) q_deqnext(&qlist))) {
	strcat(dest,&(t->token));
	KFREE(t);
	}

    return dest;
}