Example #1
0
/**
 * Run the /shell-init script.
 */
void shell_init_script(void)
{
  rtems_status_code sc;
  printf("Running /shell-init....\n\n");
  sc = rtems_shell_script("fstst", 60 * 1024, 160, "/shell-init", "stdout",
                           0, 1, 1);
  if (sc != RTEMS_SUCCESSFUL)
    printf("error: running shell script: %s (%d)\n",
             rtems_status_text (sc), sc);
}
int rtems_shell_main_joel(
  int    argc,
  char **argv
)
{
  unsigned long        tmp;
  int                  option;
  int                  sc;
  int                  verbose = 0;
  char                *taskName = "JOEL";
  uint32_t             stackSize = RTEMS_MINIMUM_STACK_SIZE * 10;
  rtems_task_priority  taskPriority = 20;
  char                *outputFile = "stdout";
  rtems_status_code    result;
  char                 scriptFile[PATH_MAX];
  struct getopt_data   getopt_reent;

  memset(&getopt_reent, 0, sizeof(getopt_data));
  while ( (option = getopt_r( argc, argv, "o:p:s:t:v", &getopt_reent)) != -1 ) {
    switch ((char)option) {
      case 'o':
        outputFile = getopt_reent.optarg;
        break;
      case 'p': {
        const char *s = getopt_reent.optarg;

	if ( rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
	  printf( "Task Priority argument (%s) is not a number\n", s );
	  return -1;
	}
	taskPriority = (rtems_task_priority) tmp;
        break;
      }
      case 's': {
        const char *s = getopt_reent.optarg;

	if ( rtems_string_to_unsigned_long( s, &tmp, NULL, 0) ) {
	  printf( "Stack size argument (%s) is not a number\n", s );
	  return -1;
	}
        stackSize = (uint32_t) tmp;
        break;
      }
      case 't':
        taskName = getopt_reent.optarg;
        break;
      case 'v':
        verbose = 1;
        break;
      case '?':
      default:
        rtems_shell_joel_usage();
        return -1;
    }
  }

  if ( verbose ) {
    fprintf( stderr,
      "outputFile: %s\n"
      "taskPriority: %" PRId32 "\n"
      "stackSize: %" PRId32 "\n"
      "taskName: %s\n",
      outputFile,
      taskPriority,
      stackSize,
      taskName
   );
  }

  /*
   *  Verify there is a script name past the end of the arguments.
   *  Preincrement to skip program name.
   */
  if ( getopt_reent.optind >= argc ) {
    fprintf( stderr, "Shell: No script to execute\n" );
    return -1;
  }

  /*
   *  Find script on the path.
   *
   *  NOTE: It is terrible that this is done twice but it
   *        seems to be the most expedient thing.
   */
  sc = findOnPATH( argv[getopt_reent.optind], scriptFile );
  if ( sc ) {
    fprintf( stderr, "%s: command not found\n", argv[0] );
    return -1;
  }

  /* fprintf( stderr, "SCRIPT: -%s-\n", scriptFile ); */

  /*
   *  I assume that argv[optind...] will have the arguments to
   *  the shell script.  But that remains to be implemented.
   */

  /*
   * Run the script
   */
  result = rtems_shell_script(
    taskName,        /* the name of the task */
    stackSize,       /* stack size */
    taskPriority,    /* task priority */
    scriptFile,      /* the script file */
    outputFile,      /* where to redirect the script */
    0,               /* run once and exit */
    1,               /* we will wait */
    verbose          /* do we echo */
  );
  if (result)
    return -1;
  return 0;
}
Example #3
0
/* --------------------------------------------------------------------------------------
    Name: OS_ShellOutputToFile
    
    Purpose: Takes a shell command in and writes the output of that command to the specified file
    
    Returns: OS_FS_ERROR if the command was not executed properly
             OS_FS_ERR_INVALID_FD if the file descriptor passed in is invalid
             OS_SUCCESS if success
 ---------------------------------------------------------------------------------------*/
int32 OS_ShellOutputToFile(char* Cmd, int32 OS_fd)
{
    int32              cmdFd;
    int32              tmpFd;
    rtems_status_code  rtemsRc;;
    int32              ReturnCode = OS_SUCCESS;
    int32              fileStatus;
    int32              bytesRead;
    int32              bytesWritten;
    char               readBuffer[256];
    char               outputFileName[OS_MAX_PATH_LEN + OS_SHELL_TMP_FILE_EXT_LEN];
    char               localCmd[OS_MAX_CMD_LEN]; 

    /* Make sure the file descriptor is legit before using it */
    if (OS_fd < 0 || OS_fd >= OS_MAX_NUM_OPEN_FILES || OS_FDTable[OS_fd].IsValid == FALSE)
    {
        ReturnCode = OS_FS_ERR_INVALID_FD;
    }
    else
    {
        /* 
        ** Create a file to write the command to (or write over the old one) 
        */
        cmdFd = OS_creat(OS_SHELL_CMD_INPUT_FILE_NAME,OS_READ_WRITE);
        if (cmdFd < OS_FS_SUCCESS)
        {
            ReturnCode = OS_FS_ERROR;
        }
        else
        {
            /*
            ** Write the command to the buffer
            */
            strncpy(localCmd,Cmd,OS_MAX_CMD_LEN);
            strncat(localCmd,"\n",1);

            /*
            ** This function passes in an open file descriptor to write the shell
            **  command output. The RTEMS shell script API expects a filename, not
            **  a file descriptor. So in addition to the temporary file for the shell input,
            **  we need to create a temporary file for the command output, then it has
            **  to be copied to the open file.  
            */
            strncpy(outputFileName,OS_SHELL_CMD_INPUT_FILE_NAME,OS_MAX_PATH_LEN);           
            strncat(outputFileName,OS_SHELL_TMP_FILE_EXT,OS_SHELL_TMP_FILE_EXT_LEN);

            /* 
            ** copy the shell command buffer to the file 
            */
            fileStatus = OS_write(cmdFd, localCmd, strlen(localCmd));
            if ( fileStatus == strlen(localCmd) )
            {
               /*
               ** Close the file
               */
               OS_close(cmdFd);	

               /*
               ** Spawn a task to execute the shell command
               */
               rtemsRc =  rtems_shell_script ( 
                         "RSHL", 
                         OS_SHELL_CMD_TASK_STACK_SIZE, 
                         OS_SHELL_CMD_TASK_PRIORITY, 
                         OS_SHELL_CMD_INPUT_FILE_NAME,
                         outputFileName,
                         FALSE,       /* Do not append output to file */
                         TRUE,        /* Wait for shell task to complete */
                         FALSE        /* Echo output */ 
                        );
                
               /*
               ** Now we have the temporary file with the output 
               */
               if((tmpFd = OS_open(outputFileName, OS_READ_ONLY,0)) == OS_FS_ERROR)
               {
                  printf("OSAL:Could not open %s for reading\n",outputFileName);
                  ReturnCode = OS_FS_ERROR; 
               }
               else
               { 
                  while((bytesRead = OS_read(tmpFd, readBuffer, 256)) > 0)
                  {
                     bytesWritten = OS_write(OS_fd, readBuffer, bytesRead);
                  }
                  OS_close(tmpFd);
                  /*
                  ** Remove the temporary output file
                  */
                  fileStatus = OS_remove(outputFileName); 
               } 
            }
            else
            {
                 ReturnCode = OS_FS_ERROR;
            }
        } 
        /*
        ** Remove the temporary shell input file
        */
        fileStatus = OS_remove(OS_SHELL_CMD_INPUT_FILE_NAME); 
    }
 
    return ReturnCode;

}/* end OS_ShellOutputToFile */