VirtualBox

Changeset 33154 in vbox


Ignore:
Timestamp:
Oct 15, 2010 12:05:52 PM (14 years ago)
Author:
vboxsync
Message:

VBoxService/Toolbox: Update.

Location:
trunk/src/VBox/Additions/common/VBoxService
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r33128 r33154  
    525525#endif
    526526
     527#ifdef VBOXSERVICE_TOOLBOX
     528    /*
     529     * Run toolbox code before all other stuff, especially before checking the global
     530     * mutex because VBoxService might spawn itself to execute some commands.
     531     */
     532    rc = VBoxServiceToolboxMain(argc, argv);
     533    if (rc != VERR_NOT_FOUND) /* Internal tool found? Then bail out. */
     534        return rc;
     535#endif
     536
    527537    /*
    528538     * Do pre-init of services.
     
    535545            return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
    536546    }
    537 
    538547#ifdef RT_OS_WINDOWS
    539548    /*
     
    558567         *        is running... */
    559568    }
    560 #endif
    561 
    562 #ifdef VBOXSERVICE_TOOLBOX
    563     rc = VBoxServiceToolboxMain(argc, argv);
    564     if (rc != VERR_NOT_FOUND) /* Internal tool found? Then bail out. */
    565         return rc;
    566569#endif
    567570
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp

    r33129 r33154  
    2323
    2424#include <iprt/assert.h>
     25#include <iprt/file.h>
     26#include <iprt/getopt.h>
    2527#include <iprt/list.h>
    2628#include <iprt/mem.h>
     
    4244             "\n");
    4345}
     46
    4447
    4548/**
     
    6265}
    6366
     67
     68/**
     69 *
     70 *
     71 * @return  int
     72 *
     73 * @param   argc
     74 * @param   argv
     75 */
    6476int VBoxServiceToolboxCatMain(int argc, char **argv)
    6577{
     
    7082    }
    7183    if (!usageOK)
    72         rc = VBoxServiceToolboxErrorSyntax(0 /* TODO */, "Incorrect parameters");
     84        rc = VBoxServiceToolboxErrorSyntax("Incorrect parameters!");
    7385    return rc;
    7486}
    7587
    76 /**
    77  * Main routine for toolbox command line handling.
     88
     89/**
     90 *
     91 *
     92 * @return  int
     93 *
     94 * @param   hInput
     95 * @param   hOutput
     96 */
     97int VBoxServiceToolboxCatOutput(RTFILE hInput, RTFILE hOutput)
     98{
     99    int rc = VINF_SUCCESS;
     100    if (hInput == NIL_RTFILE)
     101    {
     102#ifdef RT_OS_WINDOWS
     103        rc = RTFileFromNative(&hInput, STD_INPUT_HANDLE);
     104#else
     105        rc = RTFileFromNative(&hInput, 0 /*stdin*/);
     106#endif
     107        if (RT_FAILURE(rc))
     108            VBoxServiceError("Cat: Could not translate input file to native handle, rc=%Rrc\n", rc);
     109    }
     110
     111    if (hOutput == NIL_RTFILE)
     112    {
     113#ifdef RT_OS_WINDOWS
     114        rc = RTFileFromNative(&hOutput, STD_OUTPUT_HANDLE);
     115#else
     116        rc = RTFileFromNative(&hOutput, 1 /*stdout*/);
     117#endif
     118        if (RT_FAILURE(rc))
     119            VBoxServiceError("Cat: Could not translate output file to native handle, rc=%Rrc\n", rc);
     120    }
     121
     122    if (RT_SUCCESS(rc))
     123    {
     124        uint8_t abBuf[_64K];
     125        size_t cbRead;
     126        for (;;)
     127        {
     128            rc = RTFileRead(hInput, abBuf, sizeof(abBuf), &cbRead);
     129            if (RT_SUCCESS(rc) && cbRead)
     130            {
     131                rc = RTFileWrite(hOutput, abBuf, cbRead, NULL /* Try to write all at once! */);
     132                cbRead = 0;
     133            }
     134            else
     135            {
     136                if (   cbRead == 0
     137                    && rc     == VERR_BROKEN_PIPE)
     138                {
     139                    rc = VINF_SUCCESS;
     140                }
     141                break;
     142            }
     143        }
     144    }
     145    return rc;
     146}
     147
     148
     149/**
     150 *
    78151 *
    79152 * @return  int
     
    82155 * @param   argv
    83156 */
     157int VBoxServiceToolboxCat(int argc, char **argv)
     158{
     159     static const RTGETOPTDEF s_aOptions[] =
     160     {
     161         { "--input",     'i', RTGETOPT_REQ_STRING },
     162         { "--output",    'o', RTGETOPT_REQ_STRING }
     163     };
     164
     165     int ch;
     166     RTGETOPTUNION ValueUnion;
     167     RTGETOPTSTATE GetState;
     168     RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
     169
     170     int rc = VINF_SUCCESS;
     171     //bool fSeenInput = false;
     172     RTFILE hInput = NIL_RTFILE;
     173     RTFILE hOutput = NIL_RTFILE;
     174
     175     while (   (ch = RTGetOpt(&GetState, &ValueUnion))
     176            && RT_SUCCESS(rc))
     177     {
     178         /* For options that require an argument, ValueUnion has received the value. */
     179         switch (ch)
     180         {
     181             case 'o':
     182                 rc = RTFileOpen(&hOutput, ValueUnion.psz,
     183                                 RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE);
     184                 if (RT_FAILURE(rc))
     185                     VBoxServiceError("Cat: Could not create output file \"%s\"! rc=%Rrc\n",
     186                                      ValueUnion.psz, rc);
     187                 break;
     188
     189             case 'i':
     190             case VINF_GETOPT_NOT_OPTION:
     191             {
     192                 /*rc = VBoxServiceToolboxCatInput(ValueUnion.psz, hOutput);
     193                 if (RT_SUCCESS(rc))
     194                     fSeenInput = true;*/
     195
     196                 rc = RTFileOpen(&hInput, ValueUnion.psz,
     197                                 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
     198                 if (RT_FAILURE(rc))
     199                     VBoxServiceError("Cat: Could not open input file \"%s\"! rc=%Rrc\n",
     200                                      ValueUnion.psz, rc);
     201                 break;
     202             }
     203
     204             default:
     205                 return RTGetOptPrintError(ch, &ValueUnion);
     206         }
     207     }
     208
     209     if (RT_SUCCESS(rc) /*&& !fSeenInput*/)
     210         rc  = VBoxServiceToolboxCatOutput(hInput, hOutput);
     211
     212     if (hInput != NIL_RTFILE)
     213         RTFileClose(hInput);
     214     if (hOutput != NIL_RTFILE)
     215         RTFileClose(hOutput);
     216     return rc;
     217}
     218
     219
     220/**
     221 * Main routine for toolbox command line handling.
     222 *
     223 * @return  int
     224 *
     225 * @param   argc
     226 * @param   argv
     227 */
    84228int VBoxServiceToolboxMain(int argc, char **argv)
    85229{
    86230    int rc = VERR_NOT_FOUND;
    87 
    88     bool fUsageOK = true;
    89231    if (argc >= 1) /* Do we have at least a main command? */
    90232    {
    91233        if (!strcmp(argv[1], "cat"))
    92         {
    93             /** @todo Move this block into an own "cat main" routine! */
    94             PRTSTREAM pStream;
    95             bool fHaveFile = false;
    96 
    97             /* Do we have a file as second argument? */
    98             if (argc == 2)
    99             {
    100                 /* Use stdin as standard input stream. */
    101                 pStream = g_pStdIn;
    102                 rc = VINF_SUCCESS;
    103             }
    104             else if (argc == 3)
    105             {
    106                 rc = RTStrmOpen(argv[2], /* Filename */
    107                                 "rb",    /* Read binary */
    108                                 &pStream);
    109                 if (RT_SUCCESS(rc))
    110                     fHaveFile = true;
    111             }
    112             else
    113                 fUsageOK = false;
    114 
    115 
    116             if (RT_SUCCESS(rc))
    117             {
    118                 uint8_t cBuf[_64K];
    119                 size_t cbRead;
    120                 do
    121                 {
    122                     rc = RTStrmReadEx(pStream, cBuf, sizeof(cBuf), &cbRead);
    123                     if (RT_SUCCESS(rc))
    124                         rc = RTStrmWrite(g_pStdOut, cBuf, cbRead);
    125                 } while (RT_SUCCESS(rc) && cbRead);
    126                 RTStrmFlush(g_pStdOut);
    127 
    128                 /* Close opened file handle. */
    129                 if (fHaveFile)
    130                     rc = RTStrmClose(pStream);
    131             }
    132         }
    133     }
    134     /* Ignore usage errors when using an unknown command - this might
    135      * be a regular VBoxService startup command (--whatever)! */
    136     if (!fUsageOK)
    137         rc = VBoxServiceToolboxErrorSyntax(0 /* TODO */, "Incorrect parameters");
     234            rc = VBoxServiceToolboxCat(argc - 1, &argv[1]);
     235    }
    138236
    139237    if (rc != VERR_NOT_FOUND)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette