VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTCat.cpp@ 76346

Last change on this file since 76346 was 76346, checked in by vboxsync, 6 years ago

*: Preparing for iprt/string.h, iprt/json.h and iprt/serialport.h no longer including iprt/err.h and string.h no longer including latin1.h (it needs err.h). bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: RTCat.cpp 76346 2018-12-22 00:51:28Z vboxsync $ */
2/** @file
3 * IPRT - cat like utility.
4 */
5
6/*
7 * Copyright (C) 2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/vfs.h>
32
33#include <iprt/buildconfig.h>
34#include <iprt/err.h>
35#include <iprt/file.h>
36#include <iprt/getopt.h>
37#include <iprt/initterm.h>
38#include <iprt/message.h>
39#include <iprt/param.h>
40#include <iprt/path.h>
41#include <iprt/stream.h>
42#include <iprt/string.h>
43
44
45/*********************************************************************************************************************************
46* Structures and Typedefs *
47*********************************************************************************************************************************/
48/**
49 * CAT command options.
50 */
51typedef struct RTCMDCATOPTS
52{
53 bool fShowEnds; /**< -E */
54 bool fShowNonPrinting; /**< -v */
55 bool fShowTabs; /**< -T */
56 bool fSqueezeBlankLines; /**< -s */
57 bool fNumberLines; /**< -n */
58 bool fNumberNonBlankLines; /**< -b */
59 bool fAdvisoryOutputLock; /**< -l */
60 bool fUnbufferedOutput; /**< -u */
61} RTCMDCATOPTS;
62/** Pointer to const CAT options. */
63typedef RTCMDCATOPTS const *PCRTCMDCATOPTS;
64
65
66
67/**
68 * Outputs the source raw.
69 *
70 * @returns Command exit, error messages written using RTMsg*.
71 * @param hVfsOutput The output I/O stream.
72 * @param hVfsSrc The input I/O stream.
73 * @param pszSrc The input name.
74 */
75static RTEXITCODE rtCmdCatShowRaw(RTVFSIOSTREAM hVfsOutput, RTVFSIOSTREAM hVfsSrc, const char *pszSrc)
76{
77 int rc = RTVfsUtilPumpIoStreams(hVfsSrc, hVfsOutput, 0 /*cbBufHint*/);
78 if (RT_SUCCESS(rc))
79 return RTEXITCODE_SUCCESS;
80 return RTMsgErrorExitFailure("Error catting '%s': %Rrc", pszSrc, rc);
81}
82
83
84/**
85 * Outputs the source with complicated formatting.
86 *
87 * @returns Command exit, error messages written using RTMsg*.
88 * @param hVfsOutput The output I/O stream.
89 * @param hVfsSrc The input I/O stream.
90 * @param pszSrc The input name.
91 */
92static RTEXITCODE rtCmdCatShowComplicated(RTVFSIOSTREAM hVfsOutput, RTVFSIOSTREAM hVfsSrc, const char *pszSrc,
93 PCRTCMDCATOPTS pOpts)
94{
95 if (pOpts->fShowEnds)
96 RTMsgWarning("--show-ends is not implemented\n");
97 if (pOpts->fShowTabs)
98 RTMsgWarning("--show-tabs is not implemented\n");
99 if (pOpts->fShowNonPrinting)
100 RTMsgWarning("--show-nonprinting is not implemented\n");
101 if (pOpts->fSqueezeBlankLines)
102 RTMsgWarning("--squeeze-blank is not implemented\n");
103 if (pOpts->fNumberLines)
104 RTMsgWarning("--number is not implemented\n");
105 if (pOpts->fNumberNonBlankLines)
106 RTMsgWarning("--number-nonblank is not implemented\n");
107 return rtCmdCatShowRaw(hVfsOutput, hVfsSrc, pszSrc);
108}
109
110
111/**
112 * Opens the input file.
113 *
114 * @returns Command exit, error messages written using RTMsg*.
115 *
116 * @param pszFile The input filename.
117 * @param phVfsIos Where to return the input stream handle.
118 */
119static RTEXITCODE rtCmdCatOpenInput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
120{
121 int rc;
122
123 if (!strcmp(pszFile, "-"))
124 {
125 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
126 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
127 true /*fLeaveOpen*/,
128 phVfsIos);
129 if (RT_FAILURE(rc))
130 return RTMsgErrorExitFailure("Error opening standard input: %Rrc", rc);
131 }
132 else
133 {
134 uint32_t offError = 0;
135 RTERRINFOSTATIC ErrInfo;
136 rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
137 phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
138 if (RT_FAILURE(rc))
139 return RTVfsChainMsgErrorExitFailure("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
140 }
141
142 return RTEXITCODE_SUCCESS;
143
144}
145
146
147/**
148 * A /bin/cat clone.
149 *
150 * @returns Program exit code.
151 *
152 * @param cArgs The number of arguments.
153 * @param papszArgs The argument vector. (Note that this may be
154 * reordered, so the memory must be writable.)
155 */
156RTEXITCODE RTCmdCat(unsigned cArgs, char **papszArgs)
157{
158
159 /*
160 * Parse the command line.
161 */
162 static const RTGETOPTDEF s_aOptions[] =
163 {
164 { "--show-all", 'A', RTGETOPT_REQ_NOTHING },
165 { "--number-nonblanks", 'b', RTGETOPT_REQ_NOTHING },
166 { "--show-ends-and-nonprinting", 'e', RTGETOPT_REQ_NOTHING },
167 { "--show-ends", 'E', RTGETOPT_REQ_NOTHING },
168 { "--advisory-output-lock", 'l', RTGETOPT_REQ_NOTHING },
169 { "--number", 'n', RTGETOPT_REQ_NOTHING },
170 { "--squeeze-blank", 's', RTGETOPT_REQ_NOTHING },
171 { "--show-tabs-and-nonprinting", 't', RTGETOPT_REQ_NOTHING },
172 { "--show-tabs", 'T', RTGETOPT_REQ_NOTHING },
173 { "--unbuffered-output", 'u', RTGETOPT_REQ_NOTHING },
174 { "--show-nonprinting", 'v', RTGETOPT_REQ_NOTHING },
175 };
176
177 RTCMDCATOPTS Opts;
178 Opts.fShowEnds = false;
179 Opts.fShowNonPrinting = false;
180 Opts.fShowTabs = false;
181 Opts.fSqueezeBlankLines = false;
182 Opts.fNumberLines = false;
183 Opts.fNumberNonBlankLines = false;
184 Opts.fAdvisoryOutputLock = false;
185 Opts.fUnbufferedOutput = false;
186
187 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
188 unsigned cProcessed = 0;
189 RTVFSIOSTREAM hVfsOutput = NIL_RTVFSIOSTREAM;
190 int rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT, RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
191 true /*fLeaveOpen*/, &hVfsOutput);
192 if (RT_FAILURE(rc))
193 return RTMsgErrorExitFailure("RTVfsIoStrmFromStdHandle: %Rrc", rc);
194
195 RTGETOPTSTATE GetState;
196 rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
197 RTGETOPTINIT_FLAGS_OPTS_FIRST);
198 if (RT_SUCCESS(rc))
199 {
200 bool fContinue = true;
201 do
202 {
203 RTGETOPTUNION ValueUnion;
204 int chOpt = RTGetOpt(&GetState, &ValueUnion);
205 switch (chOpt)
206 {
207 case 0:
208 /*
209 * If we've processed any files we're done. Otherwise take
210 * input from stdin and write the output to stdout.
211 */
212 if (cProcessed > 0)
213 {
214 fContinue = false;
215 break;
216 }
217 ValueUnion.psz = "-";
218 RT_FALL_THRU();
219 case VINF_GETOPT_NOT_OPTION:
220 {
221 RTVFSIOSTREAM hVfsSrc;
222 RTEXITCODE rcExit2 = rtCmdCatOpenInput(ValueUnion.psz, &hVfsSrc);
223 if (rcExit2 == RTEXITCODE_SUCCESS)
224 {
225 if ( Opts.fShowEnds
226 || Opts.fShowTabs
227 || Opts.fShowNonPrinting
228 || Opts.fSqueezeBlankLines
229 || Opts.fNumberLines
230 || Opts.fNumberNonBlankLines)
231 rcExit2 = rtCmdCatShowComplicated(hVfsOutput, hVfsSrc, ValueUnion.psz, &Opts);
232 else
233 rcExit2 = rtCmdCatShowRaw(hVfsOutput, hVfsSrc, ValueUnion.psz);
234 RTVfsIoStrmRelease(hVfsSrc);
235 }
236 if (rcExit2 != RTEXITCODE_SUCCESS)
237 rcExit = rcExit2;
238 cProcessed++;
239 break;
240 }
241
242 case 'A':
243 Opts.fShowNonPrinting = true;
244 Opts.fShowEnds = true;
245 Opts.fShowTabs = true;
246 break;
247
248 case 'b':
249 Opts.fNumberNonBlankLines = true;
250 break;
251
252 case 'e':
253 Opts.fShowNonPrinting = true;
254 RT_FALL_THRU();
255 case 'E':
256 Opts.fShowEnds = true;
257 break;
258
259 case 'l':
260 Opts.fAdvisoryOutputLock = true;
261 break;
262
263 case 'n':
264 Opts.fNumberLines = true;
265 Opts.fNumberNonBlankLines = false;
266 break;
267
268 case 's':
269 Opts.fSqueezeBlankLines = true;
270 break;
271
272 case 't':
273 Opts.fShowNonPrinting = true;
274 RT_FALL_THRU();
275 case 'T':
276 Opts.fShowTabs = true;
277 break;
278
279 case 'u': /* currently ignored */
280 Opts.fUnbufferedOutput = true;
281 break;
282
283 case 'v':
284 Opts.fShowNonPrinting = true;
285 break;
286
287 case 'h':
288 RTPrintf("Usage: to be written\nOption dump:\n");
289 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
290 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
291 fContinue = false;
292 break;
293
294 case 'V':
295 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
296 fContinue = false;
297 break;
298
299 default:
300 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
301 fContinue = false;
302 break;
303 }
304 } while (fContinue);
305 }
306 else
307 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
308 RTVfsIoStrmRelease(hVfsOutput);
309 return rcExit;
310}
311
312
313int main(int argc, char **argv)
314{
315 int rc = RTR3InitExe(argc, &argv, 0);
316 if (RT_FAILURE(rc))
317 return RTMsgInitFailure(rc);
318 return RTCmdCat(argc, argv);
319}
320
Note: See TracBrowser for help on using the repository browser.

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