Exemplo n.º 1
0
void Minilog::logTerm(LogLevel lvl, const char* msg, int len)
{
	if (lvl >= termLvl)
	{
		if (msg != 0 && msg[0] != '\0')
		{
		    const char* hdr = getHeader(lvl);
			if(len < 0) len = strlen(msg);
#ifdef _MSC_VER
			CONSOLE_SCREEN_BUFFER_INFO csbi = {sizeof(csbi)};
			GetConsoleScreenBufferInfo(
				stdOut, &csbi);
			WORD wAtt = csbi.wAttributes;
			setTermColor(lvl);
			DWORD dwReal;
			WriteConsoleA(stdOut, hdr, strlen(hdr), &dwReal, NULL);
			WriteConsoleA(stdOut, msg, nLen, &dwReal, NULL);
			SetConsoleTextAttribute(stdOut, wAtt);
#else
			setTermColor(lvl);
			printf("%s", hdr);
			printf("%s", msg);
			printf("\033[0m");
#endif
		}
	}
}
Exemplo n.º 2
0
int executeBuiltinCommand(char* cmd, int argc, char** argv) {
  int status = SUCCESS;
  if (strcmp(cmd, "cd") == 0) {
    char* dest = (char*)malloc(MAX_LENGTH);
    if (argc == 2) {
      dest = argv[1];
      fixPath(dest);
    } else if (argc > 2) {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "cd: too many arguments\n");
      setTermColor(stderr, oldColor);
      status = ERROR;
    } else {
      sprintf(dest, "%s", getenv("HOME"));
    }
    int val = chdir(dest);
  } else if (strcmp(cmd, "alias") == 0) {
    if (argc == 3) {
      char* name = argv[1];
      char* word = argv[2];
      mapAlias(name, word);
    } else if (argc == 1) {
      printAliasTable();
    } else {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "alias: need 3 arguments, got %d\n", argc);
      setTermColor(stderr, oldColor);
      status = ERROR;
    }
  } else if (strcmp(cmd, "unalias") == 0) {
    if (argc == 2) {
      char* name = argv[1];
      unmapAlias(name);
    } else {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "unalias: need 2 arguments, got %d\n", argc);
      setTermColor(stderr, oldColor);
      status = ERROR;
    }
  } else if (strcmp(cmd, "printenv") == 0) {
    if (argc < 2) {
      printEnv();
    } else {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "printenv: too many arguments");
      setTermColor(stderr, oldColor);
      status = ERROR;
    }
  } else if (strcmp(cmd, "setenv") == 0) {
    if (argc == 3) {
      char* variable = argv[1];
      char* word = argv[2];
      setEnv(variable, word);
    } else {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "setenv: need 3 arguments, got %d\n", argc);
      setTermColor(stderr, oldColor);
      status = ERROR;
    }
  } else if (strcmp(cmd, "unsetenv") == 0) {
    if (argc == 2) {
      char* variable = argv[1];
      unsetEnv(variable);
    } else {
      const char* oldColor = setTermColor(stderr, KRED);
      fprintf(stderr, "unsetenv: need 2 arguments, got %d\n", argc);
      setTermColor(stderr, oldColor);
      status = ERROR;
    }
  } else if (strcmp(cmd, "bye") == 0) {
    exit(EXIT_SUCCESS);
  } else {
    status = ERROR;
  }
  return status;
}