Changeset 33154 in vbox
- Timestamp:
- Oct 15, 2010 12:05:52 PM (14 years ago)
- Location:
- trunk/src/VBox/Additions/common/VBoxService
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
r33128 r33154 525 525 #endif 526 526 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 527 537 /* 528 538 * Do pre-init of services. … … 535 545 return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc); 536 546 } 537 538 547 #ifdef RT_OS_WINDOWS 539 548 /* … … 558 567 * is running... */ 559 568 } 560 #endif561 562 #ifdef VBOXSERVICE_TOOLBOX563 rc = VBoxServiceToolboxMain(argc, argv);564 if (rc != VERR_NOT_FOUND) /* Internal tool found? Then bail out. */565 return rc;566 569 #endif 567 570 -
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp
r33129 r33154 23 23 24 24 #include <iprt/assert.h> 25 #include <iprt/file.h> 26 #include <iprt/getopt.h> 25 27 #include <iprt/list.h> 26 28 #include <iprt/mem.h> … … 42 44 "\n"); 43 45 } 46 44 47 45 48 /** … … 62 65 } 63 66 67 68 /** 69 * 70 * 71 * @return int 72 * 73 * @param argc 74 * @param argv 75 */ 64 76 int VBoxServiceToolboxCatMain(int argc, char **argv) 65 77 { … … 70 82 } 71 83 if (!usageOK) 72 rc = VBoxServiceToolboxErrorSyntax( 0 /* TODO */, "Incorrect parameters");84 rc = VBoxServiceToolboxErrorSyntax("Incorrect parameters!"); 73 85 return rc; 74 86 } 75 87 76 /** 77 * Main routine for toolbox command line handling. 88 89 /** 90 * 91 * 92 * @return int 93 * 94 * @param hInput 95 * @param hOutput 96 */ 97 int 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 * 78 151 * 79 152 * @return int … … 82 155 * @param argv 83 156 */ 157 int 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 */ 84 228 int VBoxServiceToolboxMain(int argc, char **argv) 85 229 { 86 230 int rc = VERR_NOT_FOUND; 87 88 bool fUsageOK = true;89 231 if (argc >= 1) /* Do we have at least a main command? */ 90 232 { 91 233 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 } 138 236 139 237 if (rc != VERR_NOT_FOUND)
Note:
See TracChangeset
for help on using the changeset viewer.