コード例 #1
0
ファイル: main.c プロジェクト: andrii-zymohliad/zash
// Main function
int main(int argc, char const *argv[]) {
  char input[MAX_LINE]; // input string
  char **cmd_argv;      // parsed arguments
  int cmd_argc;         // arguments number
  bool cmd_found;       // flag to indicate if command is found

  // Print initial help message
  cmd_help.execute(1,&(cmd_help.cmd));
  update_current_dir();

  // Main superloop
  while(true) {
    // Read command from input stream
    printf("[%s]%c ", current_dir, INVITE_CHAR);
    gets(input);

    // Parse input stream
    cmd_argv = parse(input, ARG_SEPARATOR, &cmd_argc);
    remove_empty_or_whitespace(&cmd_argv, &cmd_argc);
    if (cmd_argc == 0) continue;

    // Search for appropriate built-in command and execute it
    cmd_found = false;
    for (int i = 0; i < commands_count; ++i) {
      if (strcmp(cmd_argv[0],commands[i]->cmd) == 0) {
        cmd_found = true;
        commands[i]->execute(cmd_argc, cmd_argv);
        break;
      }
    }

    // Search for appropriate executable file and execute it
    if (!cmd_found) {
      if (is_exe_file(cmd_argv[0])) {
        cmd_found = true;
        execute_file(cmd_argc, cmd_argv);
      }
    }

    // Error message, if command not found
    if (!cmd_found) {
      fprintf(stderr, "Error: \"%s\" command not found!\n", cmd_argv[0]);
    }

    // Release resources
    free_strs(cmd_argv, cmd_argc);
  }
  return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: display.c プロジェクト: cbmeeks/sd2iec
void display_service(void) {
  if (menustate == MENU_NONE) {
    /* No menu active, upload+run system menu */
    uint8_t i;

    /* Dummy read to reset the interrupt line */
    i2c_read_register(DISPLAY_I2C_ADDR, DISPLAY_MENU_GETSELECTION);

    display_menu_reset();
    i = 0;
    while (pgm_read_byte(systemmenu+i)) {
      ustrcpy_P(displaybuffer, systemmenu+i);
      display_menu_add(displaybuffer);
      i += ustrlen(displaybuffer)+1;
    }
    display_menu_show(0);
    menustate = MENU_SYSTEM;

  } else if (menustate == MENU_SYSTEM) {
    /* Selection on the system menu */
    uint8_t sel = i2c_read_register(DISPLAY_I2C_ADDR, DISPLAY_MENU_GETSELECTION);

    switch (sel) {
    case SYSMENU_CHDIR:
      menu_chdir();
      break;

    case SYSMENU_CHADDR:
      menu_chaddr();
      break;

    case SYSMENU_STORE:
      menustate = MENU_NONE;
      write_configuration();
      break;

    case SYSMENU_CANCEL:
      menustate = MENU_NONE;
      return;
    }
  } else if (menustate == MENU_CHADDR) {
    /* New address selected */
    uint8_t sel = i2c_read_register(DISPLAY_I2C_ADDR, DISPLAY_MENU_GETSELECTION);

    device_address = sel+4;
    menustate = MENU_NONE;
    display_address(device_address);
  } else if (menustate == MENU_CHDIR) {
    /* New directory selected */
    uint8_t sel = i2c_read_register(DISPLAY_I2C_ADDR, DISPLAY_MENU_GETSELECTION);
    path_t path;
    cbmdirent_t dent;

    menustate = MENU_NONE;
    if (sel == 0)
      /* Cancel */
      return;

    /* Read directory name into displaybuffer */
    path.part = current_part;
    path.dir  = partition[current_part].current_dir;

    if (sel == 1) {
      /* Previous directory */
      dent.name[0] = '_';
      dent.name[1] = 0;
    } else {
      i2c_read_registers(DISPLAY_I2C_ADDR, DISPLAY_MENU_GETENTRY, sizeof(displaybuffer), displaybuffer);

      if (first_match(&path, displaybuffer, FLAG_HIDDEN, &dent))
        return;
    }

    chdir(&path, &dent);
    update_current_dir(&path);
  }
}