Exemplo n.º 1
0
void
beginProgram (int argumentCount, char **argumentVector) {
#if defined(GRUB_RUNTIME)

#else /* at exit */
  atexit(endProgram);
#endif /* at exit */

  initializeSystemObject();
  prepareLocale();

  if ((programPath = getProgramPath())) {
    registerProgramMemory("program-path", &programPath);
  } else {
    programPath = argumentVector[0];
  }

  if (!isExplicitPath(programPath)) {
    char *path = findProgram(programPath);
    if (!path) path = testProgram(".", programPath);
    if (path) programPath = path;
  }

  if (isExplicitPath(programPath)) {
#if defined(HAVE_REALPATH) && defined(PATH_MAX)
    if (!isAbsolutePath(programPath)) {
      char buffer[PATH_MAX];
      char *path = realpath(programPath, buffer);

      if (path) {
        char *realPath = strdup(path);

        if (realPath) {
          programPath = realPath;
        } else {
          logMallocError();
        }
      } else {
        logSystemError("realpath");
      }
    }
#endif /* defined(HAVE_REALPATH) && defined(PATH_MAX) */

    if (!isAbsolutePath(programPath)) {
      char *directory;

      if ((directory = getWorkingDirectory())) {
        char *path;
        if ((path = makePath(directory, programPath))) programPath = path;
        free(directory);
      }
    }
  }

  programName = locatePathName(programPath);
  pushLogPrefix(programName);
}
Exemplo n.º 2
0
const char *
getOverrideDirectory (void) {
  static const char *directory = NULL;

  if (!directory) {
    static const char subdirectory[] = "." PACKAGE_NAME;

    {
      char *homeDirectory = getHomeDirectory();

      if (homeDirectory) {
        directory = makePath(homeDirectory, subdirectory);
        free(homeDirectory);
        if (directory) goto gotDirectory;
      }
    }

    {
      char *workingDirectory = getWorkingDirectory();

      if (workingDirectory) {
        directory = makePath(workingDirectory, subdirectory);
        free(workingDirectory);
        if (directory) goto gotDirectory;
      }
    }

    directory = "";
    logMessage(LOG_WARNING, "no override directory");
    goto ready;

  gotDirectory:
    logMessage(LOG_INFO, "Override Directory: %s", directory);
    registerProgramMemory("override-directory", &directory);
  }

ready:
  return *directory? directory: NULL;
}
Exemplo n.º 3
0
int
fixInstallPath (char **path) {
  static const char *programDirectory = NULL;

  if (!programDirectory) {
    if ((programDirectory = getPathDirectory(programPath))) {
      registerProgramMemory("program-directory", &programDirectory);
    } else {
      logMessage(LOG_WARNING, gettext("cannot determine program directory"));
      programDirectory = ".";
    }

    logMessage(LOG_DEBUG, "program directory: %s", programDirectory);
  }

  {
    const char *problem = strtext("cannot fix install path");
    char *newPath = makePath(programDirectory, *path);

    if (newPath) {
      if (changeStringSetting(path, newPath)) {
        if (isAbsolutePath(*path)) {
          problem = NULL;
        } else {
          problem = strtext("install path not absolute");
        }
      }

      free(newPath);
    }

    if (problem) {
      logMessage(LOG_WARNING, "%s: %s", gettext(problem), *path);
      return 0;
    }
  }

  return 1;
}
Exemplo n.º 4
0
const CommandEntry *
getCommandEntry (int code) {
  static const CommandEntry **commandEntries = NULL;
  static int commandCount;

  if (!commandEntries) {
    {
      const CommandEntry *cmd = commandTable;

      while (cmd->name) cmd += 1;
      commandCount = cmd - commandTable;
    }

    {
      const CommandEntry **entries = malloc(ARRAY_SIZE(entries, commandCount));

      if (!entries) {
        logMallocError();
        return NULL;
      }

      {
        const CommandEntry *cmd = commandTable;
        const CommandEntry **entry = entries;

        while (cmd->name) *entry++ = cmd++;
      }

      qsort(entries, commandCount, sizeof(*entries), compareCommandCodes);
      commandEntries = entries;
    }

    registerProgramMemory("sorted-command-table", &commandEntries);
  }

  code &= BRL_MSK_CMD;
  {
    int first = 0;
    int last = commandCount - 1;

    while (first <= last) {
      int current = (first + last) / 2;
      const CommandEntry *cmd = commandEntries[current];

      if (code < cmd->code) {
        last = current - 1;
      } else {
        first = current + 1;
      }
    }

    if (last >= 0) {
      const CommandEntry *cmd = commandEntries[last];
      int blk = cmd->code & BRL_MSK_BLK;
      int arg = cmd->code & BRL_MSK_ARG;

      if (blk == (code & BRL_MSK_BLK)) {
        if (arg == (code & BRL_MSK_ARG)) return cmd;

        if (blk) {
          return cmd;
          int next = last + 1;

          if (next < commandCount)
            if (blk != (commandEntries[next]->code & BRL_MSK_BLK))
              return cmd;
        }
      }
    }
  }

  return NULL;
}