1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <cstdlib>
21 #include <iostream>
22 
23 #include <thrift/transport/TBufferTransports.h>
24 #include <thrift/transport/TFDTransport.h>
25 #include <thrift/protocol/TBinaryProtocol.h>
26 #include <thrift/protocol/TDebugProtocol.h>
27 #include <thrift/protocol/TProtocolTap.h>
28 
29 using namespace std;
30 using boost::shared_ptr;
31 using namespace apache::thrift::transport;
32 using namespace apache::thrift::protocol;
33 
usage()34 void usage() {
35   fprintf(stderr,
36       "usage: thrift_dump {-b|-f|-s} < input > ouput\n"
37       "  -b TBufferedTransport messages\n"
38       "  -f TFramedTransport messages\n"
39       "  -s Raw structures\n");
40   exit(EXIT_FAILURE);
41 }
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[]) {
44   if (argc != 2) {
45     usage();
46   }
47 
48   shared_ptr<TTransport> stdin_trans(new TFDTransport(STDIN_FILENO));
49   shared_ptr<TTransport> itrans;
50 
51   if (argv[1] == std::string("-b") || argv[1] == std::string("-s")) {
52     itrans.reset(new TBufferedTransport(stdin_trans));
53   } else if (argv[1] == std::string("-f")) {
54     itrans.reset(new TFramedTransport(stdin_trans));
55   } else {
56     usage();
57   }
58 
59   shared_ptr<TProtocol> iprot(new TBinaryProtocol(itrans));
60   shared_ptr<TProtocol> oprot(
61       new TDebugProtocol(
62         shared_ptr<TTransport>(new TBufferedTransport(
63           shared_ptr<TTransport>(new TFDTransport(STDOUT_FILENO))))));
64 
65   TProtocolTap tap(iprot, oprot);
66 
67   try {
68     if (argv[1] == std::string("-s")) {
69       for (;;) {
70         tap.skip(T_STRUCT);
71       }
72     } else {
73       std::string name;
74       TMessageType messageType;
75       int32_t seqid;
76       for (;;) {
77         tap.readMessageBegin(name, messageType, seqid);
78         tap.skip(T_STRUCT);
79         tap.readMessageEnd();
80       }
81     }
82   } catch (TProtocolException exn) {
83     cout << "Protocol Exception: " << exn.what() << endl;
84   } catch (...) {
85     oprot->getTransport()->flush();
86   }
87 
88   cout << endl;
89 
90   return 0;
91 }
92