00001
00002
00003
00004
00005
00006
00007
00008 #include "wireshark_generator.h"
00009 #include "be_extern.h"
00010
00011 #include "utl_identifier.h"
00012 #include "utl_labellist.h"
00013
00014 using namespace AstTypeClassification;
00015
00016 void wireshark_generator::write_common (UTL_ScopedName *name,
00017 const char *kind,
00018 const char *repoid)
00019 {
00020 const char *ident = name->last_component()->get_string();
00021 be_global->ws_config_ << "\n[" << scoped_helper(name, "/") << "]\n";
00022 be_global->ws_config_ << ident << ".kind = \"" << kind << "\"\n";
00023 be_global->ws_config_ << ident << ".repoid = \"" << repoid << "\"\n";
00024 }
00025
00026 bool wireshark_generator::gen_enum(AST_Enum*, UTL_ScopedName* name,
00027 const std::vector<AST_EnumVal*>& contents, const char* repoid)
00028 {
00029 if (!be_global->generate_wireshark())
00030 return true;
00031
00032 write_common (name, "enum", repoid);
00033 be_global->ws_config_ << name->last_component()->get_string()
00034 << ".order = \"";
00035
00036 for (size_t i = 0; i < contents.size(); ++i) {
00037 be_global->ws_config_ << contents[i]->local_name()->get_string();
00038 if ((i+1) < contents.size())
00039 be_global->ws_config_<< " ";
00040 }
00041 be_global->ws_config_ << "\"\n";
00042 return true;
00043 }
00044
00045 void wireshark_generator::gen_array(UTL_ScopedName* name, AST_Array* arr)
00046 {
00047 std::string elem = scoped(arr->base_type()->name());
00048 const char *ident = name->last_component()->get_string();
00049
00050 be_global->ws_config_ << ident << ".element = \"" << elem << "\"\n";
00051 ACE_CDR::ULong dims = arr->n_dims();
00052 if (dims > 1)
00053 {
00054 be_global->ws_config_ << ident << ".dimensions = " << dims << "\n";
00055 be_global->ws_config_ << ident << ".sizes = \"";
00056 }
00057 else
00058 {
00059 be_global->ws_config_ << ident << ".size = ";
00060 }
00061
00062 for (size_t i = 0; i < dims; ++i) {
00063 be_global->ws_config_ << arr->dims()[i]->ev()->u.ulval;
00064 if ((i+1) < dims)
00065 be_global->ws_config_ << " ";
00066 }
00067
00068 if (dims > 1)
00069 {
00070 be_global->ws_config_ << "\"";
00071 }
00072 be_global->ws_config_ << "\n";
00073
00074 }
00075
00076
00077 bool wireshark_generator::gen_typedef(AST_Typedef*, UTL_ScopedName* name, AST_Type* base,
00078 const char* repoid)
00079 {
00080 if (!be_global->generate_wireshark())
00081 return true;
00082 switch (base->node_type()) {
00083 case AST_Decl::NT_sequence:
00084 {
00085 AST_Sequence *seq = AST_Sequence::narrow_from_decl(base);
00086 write_common (name, "sequence", repoid);
00087 be_global->ws_config_ << name->last_component()->get_string()
00088 << ".element = \""
00089 << scoped(seq->base_type()->name())
00090 << "\"\n";
00091 break;
00092 }
00093 case AST_Decl::NT_array:
00094 {
00095 write_common (name, "array", repoid);
00096 gen_array(name, AST_Array::narrow_from_decl(base));
00097 break;
00098 }
00099 default:
00100 {
00101 write_common (name, "alias", repoid);
00102 be_global->ws_config_ << name->last_component()->get_string()
00103 << ".base = \""
00104 << scoped(base->name())
00105 << "\"\n";
00106
00107 return true;
00108 }
00109 }
00110 return true;
00111 }
00112
00113 namespace {
00114 static std::stringstream orderstrm;
00115
00116 void write_field (AST_Field *field)
00117 {
00118 orderstrm << field->local_name()->get_string() << " ";
00119 be_global->ws_config_ << field->local_name()->get_string() << " = \"";
00120 be_global->ws_config_ << scoped(field->field_type()->name()) << "\"\n";
00121 }
00122
00123 void write_branch (AST_UnionBranch *branch)
00124 {
00125 const char *label_name = 0;
00126 unsigned long count = branch->label_list_length();
00127 for (unsigned long i = 0; i < count; i++)
00128 {
00129 AST_UnionLabel *label = branch->label(i);
00130 if (label->label_kind() == AST_UnionLabel::UL_default)
00131 {
00132 label_name = "default";
00133 break;
00134 }
00135 label_name = label->label_val()->n()->last_component()->get_string();
00136 orderstrm << label_name << " ";
00137 }
00138
00139 be_global->ws_config_ << label_name << ".type = \""
00140 << scoped(branch->field_type()->name()) << "\"\n";
00141 be_global->ws_config_ << label_name << ".name = \""
00142 << branch->local_name()->get_string() << "\"\n";
00143 }
00144
00145 }
00146
00147
00148 bool wireshark_generator::gen_struct(AST_Structure*, UTL_ScopedName* name,
00149 const std::vector<AST_Field*>& fields,
00150 AST_Type::SIZE_TYPE, const char* repoid)
00151 {
00152 if (!be_global->generate_wireshark())
00153 return true;
00154
00155 write_common (name, "struct", repoid);
00156
00157 std::for_each (fields.begin(), fields.end(), write_field);
00158
00159 be_global->ws_config_ << name->last_component()->get_string()
00160 << ".order = \""
00161 << orderstrm.str() << "\"\n";
00162 orderstrm.str("");
00163 orderstrm.clear();
00164
00165 return true;
00166 }
00167
00168
00169 bool wireshark_generator::gen_union(AST_Union*, UTL_ScopedName* name,
00170 const std::vector<AST_UnionBranch*>& cases,
00171 AST_Type* _d,
00172 const char* repoid)
00173 {
00174 if (!be_global->generate_wireshark())
00175 return true;
00176
00177 write_common (name, "union", repoid);
00178 const char *ident = name->last_component()->get_string();
00179
00180 be_global->ws_config_ << ident << ".discriminator = \""
00181 << scoped(_d->name()) << "\"\n";
00182
00183 std::for_each (cases.begin(), cases.end(), write_branch);
00184
00185 be_global->ws_config_ << ident << ".order = \""
00186 << orderstrm.str() << "\"\n";
00187 orderstrm.str("");
00188 orderstrm.clear();
00189
00190 return true;
00191 }