VirtualBox

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

Last change on this file since 67132 was 66602, checked in by vboxsync, 8 years ago

IPRT/vfs-chains: Pass around an pErrInfo buffer too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: RTCat.cpp 66602 2017-04-18 15:27:30Z 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/file.h>
35#include <iprt/getopt.h>
36#include <iprt/initterm.h>
37#include <iprt/message.h>
38#include <iprt/param.h>
39#include <iprt/path.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47/**
48 * CAT command options.
49 */
50typedef struct RTCMDCATOPTS
51{
52 bool fShowEnds; /**< -E */
53 bool fShowNonPrinting; /**< -v */
54 bool fShowTabs; /**< -T */
55 bool fSqueezeBlankLines; /**< -s */
56 bool fNumberLines; /**< -n */
57 bool fNumberNonBlankLines; /**< -b */
58 bool fAdvisoryOutputLock; /**< -l */
59 bool fUnbufferedOutput; /**< -u */
60} RTCMDCATOPTS;
61/** Pointer to const CAT options. */
62typedef RTCMDCATOPTS const *PCRTCMDCATOPTS;
63
64
65
66/**
67 * Outputs the source raw.
68 *
69 * @returns Command exit, error messages written using RTMsg*.
70 * @param hVfsOutput The output I/O stream.
71 * @param hVfsSrc The input I/O stream.
72 * @param pszSrc The input name.
73 */
74static RTEXITCODE rtCmdCatShowRaw(RTVFSIOSTREAM hVfsOutput, RTVFSIOSTREAM hVfsSrc, const char *pszSrc)
75{
76 int rc = RTVfsUtilPumpIoStreams(hVfsSrc, hVfsOutput, 0 /*cbBufHint*/);
77 if (RT_SUCCESS(rc))
78 return RTEXITCODE_SUCCESS;
79 return RTMsgErrorExitFailure("Error catting '%s': %Rrc", pszSrc, rc);
80}
81
82
83/**
84 * Outputs the source with complicated formatting.
85 *
86 * @returns Command exit, error messages written using RTMsg*.
87 * @param hVfsOutput The output I/O stream.
88 * @param hVfsSrc The input I/O stream.
89 * @param pszSrc The input name.
90 */
91static RTEXITCODE rtCmdCatShowComplicated(RTVFSIOSTREAM hVfsOutput, RTVFSIOSTREAM hVfsSrc, const char *pszSrc,
92 PCRTCMDCATOPTS pOpts)
93{
94 if (pOpts->fShowEnds)
95 RTMsgWarning("--show-ends is not implemented\n");
96 if (pOpts->fShowTabs)
97 RTMsgWarning("--show-tabs is not implemented\n");
98 if (pOpts->fShowNonPrinting)
99 RTMsgWarning("--show-nonprinting is not implemented\n");
100 if (pOpts->fSqueezeBlankLines)
101 RTMsgWarning("--squeeze-blank is not implemented\n");
102 if (pOpts->fNumberLines)
103 RTMsgWarning("--number is not implemented\n");
104 if (pOpts->fNumberNonBlankLines)
105 RTMsgWarning("--number-nonblank is not implemented\n");
106 return rtCmdCatShowRaw(hVfsOutput, hVfsSrc, pszSrc);
107}
108
109
110/**
111 * Opens the input file.
112 *
113 * @returns Command exit, error messages written using RTMsg*.
114 *
115 * @param pszFile The input filename.
116 * @param phVfsIos Where to return the input stream handle.
117 */
118static RTEXITCODE rtCmdCatOpenInput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
119{
120 int rc;
121
122 if (!strcmp(pszFile, "-"))
123 {
124 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
125 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
126 true /*fLeaveOpen*/,
127 phVfsIos);
128 if (RT_FAILURE(rc))
129 return RTMsgErrorExitFailure("Error opening standard input: %Rrc", rc);
130 }
131 else
132 {
133 uint32_t offError = 0;
134 RTERRINFOSTATIC ErrInfo;
135 rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
136 phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
137 if (RT_FAILURE(rc))
138 return RTVfsChainMsgErrorExitFailure("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
139 }
140
141 return RTEXITCODE_SUCCESS;
142
143}
144
145
146/**
147 * A /bin/cat clone.
148 *
149 * @returns Program exit code.
150 *
151 * @param cArgs The number of arguments.
152 * @param papszArgs The argument vector. (Note that this may be
153 * reordered, so the memory must be writable.)
154 */
155RTEXITCODE RTCmdCat(unsigned cArgs, char **papszArgs)
156{
157
158 /*
159 * Parse the command line.
160 */
161 static const RTGETOPTDEF s_aOptions[] =
162 {
163 { "--show-all", 'A', RTGETOPT_REQ_NOTHING },
164 { "--number-nonblanks", 'b', RTGETOPT_REQ_NOTHING },
165 { "--show-ends-and-nonprinting", 'e', RTGETOPT_REQ_NOTHING },
166 { "--show-ends", 'E', RTGETOPT_REQ_NOTHING },
167 { "--advisory-output-lock", 'l', RTGETOPT_REQ_NOTHING },
168 { "--number", 'n', RTGETOPT_REQ_NOTHING },
169 { "--squeeze-blank", 's', RTGETOPT_REQ_NOTHING },
170 { "--show-tabs-and-nonprinting", 't', RTGETOPT_REQ_NOTHING },
171 { "--show-tabs", 'T', RTGETOPT_REQ_NOTHING },
172 { "--unbuffered-output", 'u', RTGETOPT_REQ_NOTHING },
173 { "--show-nonprinting", 'v', RTGETOPT_REQ_NOTHING },
174 };
175
176 RTCMDCATOPTS Opts;
177 Opts.fShowEnds = false;
178 Opts.fShowNonPrinting = false;
179 Opts.fShowTabs = false;
180 Opts.fSqueezeBlankLines = false;
181 Opts.fNumberLines = false;
182 Opts.fNumberNonBlankLines = false;
183 Opts.fAdvisoryOutputLock = false;
184 Opts.fUnbufferedOutput = false;
185
186 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
187 unsigned cProcessed = 0;
188 RTVFSIOSTREAM hVfsOutput = NIL_RTVFSIOSTREAM;
189 int rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT, RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
190 true /*fLeaveOpen*/, &hVfsOutput);
191 if (RT_FAILURE(rc))
192 return RTMsgErrorExitFailure("RTVfsIoStrmFromStdHandle: %Rrc", rc);
193
194 RTGETOPTSTATE GetState;
195 rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
196 RTGETOPTINIT_FLAGS_OPTS_FIRST);
197 if (RT_FAILURE(rc))
198 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
199
200 for (;;)
201 {
202 RTGETOPTUNION ValueUnion;
203 int chOpt = RTGetOpt(&GetState, &ValueUnion);
204 switch (chOpt)
205 {
206 case 0:
207 /*
208 * If we've processed any files we're done. Otherwise take
209 * input from stdin and write the output to stdout.
210 */
211 if (cProcessed > 0)
212 return rcExit;
213 ValueUnion.psz = "-";
214 /* Fall thru. */
215 case VINF_GETOPT_NOT_OPTION:
216 {
217 RTVFSIOSTREAM hVfsSrc;
218 RTEXITCODE rcExit2 = rtCmdCatOpenInput(ValueUnion.psz, &hVfsSrc);
219 if (rcExit2 == RTEXITCODE_SUCCESS)
220 {
221 if ( Opts.fShowEnds
222 || Opts.fShowTabs
223 || Opts.fShowNonPrinting
224 || Opts.fSqueezeBlankLines
225 || Opts.fNumberLines
226 || Opts.fNumberNonBlankLines)
227 rcExit2 = rtCmdCatShowComplicated(hVfsOutput, hVfsSrc, ValueUnion.psz, &Opts);
228 else
229 rcExit2 = rtCmdCatShowRaw(hVfsOutput, hVfsSrc, ValueUnion.psz);
230 RTVfsIoStrmRelease(hVfsSrc);
231 }
232 if (rcExit2 != RTEXITCODE_SUCCESS)
233 rcExit = rcExit2;
234 cProcessed++;
235 break;
236 }
237
238 case 'A':
239 Opts.fShowNonPrinting = true;
240 Opts.fShowEnds = true;
241 Opts.fShowTabs = true;
242 break;
243
244 case 'b':
245 Opts.fNumberNonBlankLines = true;
246 break;
247
248 case 'e':
249 Opts.fShowNonPrinting = true;
250 /* fall thru */
251 case 'E':
252 Opts.fShowEnds = true;
253 break;
254
255 case 'l':
256 Opts.fAdvisoryOutputLock = true;
257 break;
258
259 case 'n':
260 Opts.fNumberLines = true;
261 Opts.fNumberNonBlankLines = false;
262 break;
263
264 case 's':
265 Opts.fSqueezeBlankLines = true;
266 break;
267
268 case 't':
269 Opts.fShowNonPrinting = true;
270 /* fall thru */
271 case 'T':
272 Opts.fShowTabs = true;
273 break;
274
275 case 'u': /* currently ignored */
276 Opts.fUnbufferedOutput = true;
277 break;
278
279 case 'v':
280 Opts.fShowNonPrinting = true;
281 break;
282
283 case 'h':
284 RTPrintf("Usage: to be written\nOption dump:\n");
285 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
286 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
287 return RTEXITCODE_SUCCESS;
288
289 case 'V':
290 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
291 return RTEXITCODE_SUCCESS;
292
293 default:
294 return RTGetOptPrintError(chOpt, &ValueUnion);
295 }
296 }
297}
298
299
300int main(int argc, char **argv)
301{
302 int rc = RTR3InitExe(argc, &argv, 0);
303 if (RT_FAILURE(rc))
304 return RTMsgInitFailure(rc);
305 return RTCmdCat(argc, argv);
306}
307
308
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