Exemple #1
0
int parseOpt(parseHandle * handle, char **param)
{
	int cmdIndex = 0, cmdLen = 0;
	char *startPos;

	if (!handle)		/* invalid handle  */
		return (parseFree(handle));
	/* skip spaces     */
	for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
	if (!*(handle->bufPos))
		return (parseFree(handle));	/* end of data     */

	startPos = handle->bufPos;	/* store cmd start */
	for (; handle->cmdPos[cmdIndex][cmdLen] && *(handle->bufPos); handle->bufPos++) {	/* no string end?  */
		for (;;) {
			if (*(handle->bufPos) == handle->cmdPos[cmdIndex][cmdLen])
				break;	/* char matches ?  */
			else if (memcmp(startPos, (char *) (handle->cmdPos[++cmdIndex]), cmdLen))
				return (parseFree(handle));	/* unknown command */

			if (cmdIndex >= handle->cmdNum)
				return (parseFree(handle));	/* unknown command */
		}

		cmdLen++;	/* next char       */
	}

	/* Get param. First skip all blanks, then insert zero after param  */

	for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
	*param = handle->bufPos;

	for (; *(handle->bufPos) && *(handle->bufPos) != ' '; handle->bufPos++);
	*(handle->bufPos++) = 0;

	return (cmdIndex);
}
Exemple #2
0
/* Free context */
void tcFree(tcCtx g) {
    tcprivCtx h = g->ctx.tcpriv;

    /* Free modules */
    sindexFree(g);
    encodingFree(g);
    charsetFree(g);
    parseFree(g);
    csFree(g);
    recodeFree(g);
#if TC_SUBR_SUPPORT
    subrFree(g);
#endif /* TC_SUBR_SUPPORT */
    fdselectFree(g);
    t13Free(g);

    freeFonts(g);

    dnaFREE(h->set);
    MEM_FREE(g, h);
    g->cb.free(g->cb.ctx, g); /* Free context */
}
struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* string, size_t length) {
	if (!string || length < 1) {
		return 0;
	}

	struct CLIDebugVector dvTemp = { .type = CLIDV_INT_TYPE };

	struct LexVector lv = { .next = 0 };
	size_t adjusted = lexExpression(&lv, string, length);
	if (adjusted > length) {
		dvTemp.type = CLIDV_ERROR_TYPE;
		lexFree(lv.next);
	}

	struct ParseTree tree;
	parseLexedExpression(&tree, &lv);
	if (tree.token.type == TOKEN_ERROR_TYPE) {
		dvTemp.type = CLIDV_ERROR_TYPE;
	} else {
		dvTemp.intValue = _evaluateParseTree(&debugger->d, &tree, &dvTemp);
	}

	parseFree(tree.lhs);
	parseFree(tree.rhs);

	length -= adjusted;
	string += adjusted;

	struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
	if (dvTemp.type == CLIDV_ERROR_TYPE) {
		dv->type = CLIDV_ERROR_TYPE;
		dv->next = 0;
	} else {
		*dv = dvTemp;
		if (string[0] == ' ') {
			dv->next = CLIDVParse(debugger, string + 1, length - 1);
			if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
				dv->type = CLIDV_ERROR_TYPE;
			}
		}
	}
	return dv;
}

struct CLIDebugVector* CLIDVStringParse(struct CLIDebugger* debugger, const char* string, size_t length) {
	if (!string || length < 1) {
		return 0;
	}

	struct CLIDebugVector dvTemp = { .type = CLIDV_CHAR_TYPE };

	size_t adjusted;
	const char* next = strchr(string, ' ');
	if (next) {
		adjusted = next - string;
	} else {
		adjusted = length;
	}
	dvTemp.charValue = malloc(adjusted);
	strncpy(dvTemp.charValue, string, adjusted);

	length -= adjusted;
	string += adjusted;

	struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
	*dv = dvTemp;
	if (string[0] == ' ') {
		dv->next = CLIDVStringParse(debugger, string + 1, length - 1);
		if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
			dv->type = CLIDV_ERROR_TYPE;
		}
	}
	return dv;
}

static void _DVFree(struct CLIDebugVector* dv) {
	struct CLIDebugVector* next;
	while (dv) {
		next = dv->next;
		if (dv->type == CLIDV_CHAR_TYPE) {
			free(dv->charValue);
		}
		free(dv);
		dv = next;
	}
}

static int _tryCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, const char* command, size_t commandLen, const char* args, size_t argsLen) {
	struct CLIDebugVector* dv = 0;
	int i;
	const char* name;
	for (i = 0; (name = commands[i].name); ++i) {
		if (strlen(name) != commandLen) {
			continue;
		}
		if (strncasecmp(name, command, commandLen) == 0) {
			if (commands[i].parser) {
				if (args) {
					dv = commands[i].parser(debugger, args, argsLen);
					if (dv && dv->type == CLIDV_ERROR_TYPE) {
						printf("Parse error\n");
						_DVFree(dv);
						return false;
					}
				}
			} else if (args) {
				printf("Wrong number of arguments\n");
				return false;
			}
			commands[i].command(debugger, dv);
			_DVFree(dv);
			return true;
		}
	}
	return -1;
}

static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) {
	const char* firstSpace = strchr(line, ' ');
	size_t cmdLength;
	if (firstSpace) {
		cmdLength = firstSpace - line;
	} else {
		cmdLength = count;
	}

	const char* args = 0;
	if (firstSpace) {
		args = firstSpace + 1;
	}
	int result = _tryCommands(debugger, _debuggerCommands, line, cmdLength, args, count - cmdLength - 1);
	if (result < 0 && debugger->system) {
		result = _tryCommands(debugger, debugger->system->commands, line, cmdLength, args, count - cmdLength - 1);
	}
	if (result < 0) {
		printf("Command not found\n");
	}
	return false;
}

static char* _prompt(EditLine* el) {
	UNUSED(el);
	return "> ";
}