📄 macedon-conv.c
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in// the documentation and/or other materials provided with the// distribution.// * Neither the names of Duke University nor The University of// California, San Diego, nor the names of its contributors// may be used to endorse or promote products derived from// this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./* macedon-conv.c *//* * For MACEDON code generation * * Adolfo Rodriguez *//* includes */#include <fnmatch.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "macedon-conv.h"#include "macedon-conv-utils.h"extern FILE *yyin, *yyout;/* imports */extern int protocol_done;extern char *message_str, transition_state_str, transition_stimulus_str;extern char *protocol_name;extern char *base_name;extern char *mac_name;extern char *agent_name; // not used (yet) ...Soorajextern FILE *hfile, *hfile2, *cfile, *cfilefuncs, *cfile2, *hfilemacros;extern int API_specified[API_num_types];extern int addressing;extern int tracing;extern char include_buf[1024];extern char *include_ptr;/* exports *//* local */static int done_start_init;static int done_end_init;static int current_line;struct item { char *desc; /* item description */ char *cmd; /* command */ int action; /* action to take */ char *act_str; /* action operation */ int attribute; /* visible/invisible */ struct item *next; /* next member of list */} *item_list, *last_item;node_type *my_node_types=0;message *my_messages=0;transport *my_transports=0;neighbor_type *my_neighbor_types=0;declare *my_periphs=0;declare *my_locals=0;state *my_states=0;timer *my_timers=0;constant *my_constants=0;object_replace *my_replaces=0;api_demux* apidemux=0;msg_demux* msglist=0;int foreachcount=0;int outhdr_num=0;start_protocol(char *name, char *base) { char *hfilename; char *hfile2name; char *cfilename; char *cfilefuncsname; char *cfile2name; char *hfilemacrosname; time_t rawtime; struct tm * timeinfo; protocol_name = (char *) malloc (400); strcpy (protocol_name, name); if (base) { base_name = (char *) malloc (400); strcpy (base_name, base); } else base_name = 0; printf ("Starting protocol %s\n", protocol_name);#define _CREATE_FILENAME_(_name_, extension, comment) \ _name_ = (char *) malloc(400); \ sprintf (_name_, "%s%s", protocol_name, extension); \ printf(comment, _name_); _CREATE_FILENAME_(hfilename, ".h","Header file is %s\n"); _CREATE_FILENAME_(hfile2name, "2.h","Header2 file is %s\n"); _CREATE_FILENAME_(cfilename, ".cc","C++ file is %s\n"); _CREATE_FILENAME_(cfilefuncsname, "-funcs.cc","C++ functions file is %s\n"); _CREATE_FILENAME_(cfile2name, "2.cc","C++ functions2 file is %s\n"); _CREATE_FILENAME_(hfilemacrosname, "-macros.h","Header file for macros is %s\n");#undef _CREATE_FILENAME_#define _OPEN_FILE_(_file_) \ _file_ = fopen(_file_##name,"w"); \ if (_file_ == NULL) \ { \ fprintf(stderr,"cannot open %s\n", _file_##name); \ exit(1); \ } _OPEN_FILE_(hfile); _OPEN_FILE_(hfile2); _OPEN_FILE_(cfile); _OPEN_FILE_(cfilefuncs); _OPEN_FILE_(cfile2); _OPEN_FILE_(hfilemacros);#undef _OPEN_FILE_ time ( & rawtime ); timeinfo = localtime (&rawtime); fprintf(hfile, "/*********************************************\n"); fprintf(hfile, " %s\n", hfilename); fprintf(hfile, " MACEDON generated on:\n"); fprintf(hfile, " %s", asctime(timeinfo)); fprintf(hfile, "*********************************************/\n\n\n"); fprintf(hfile, "#ifndef %s_h\n", protocol_name); fprintf(hfile, "#define %s_h\n", protocol_name); fprintf(hfile, "#include \"config.h\"\n"); fprintf(hfile, "#include \"packet.h\"\n"); fprintf(hfile, "#include \"scheduler.h\"\n"); fprintf(hfile, "#include \"neighbor_set.h\"\n"); fprintf(hfile, "#include \"ext.h\"\n"); if (base_name) fprintf(hfile, "#include \"%s.h\"\n", base_name); fprintf(hfile, "%s", include_buf); fprintf(hfile2, "/*********************************************\n"); fprintf(hfile2, " %s\n", hfile2name); fprintf(hfile2, " MACEDON generated on:\n"); fprintf(hfile2, " %s", asctime(timeinfo)); fprintf(hfile2, "*********************************************/\n\n\n"); fprintf(hfile2, "#ifndef %s2_h\n", protocol_name); fprintf(hfile2, "#define %s2_h\n", protocol_name); fprintf(hfile2, "class MACEDON_Agent;\n"); fprintf(hfile2, "\n"); fprintf(hfile2, "int %s_load_protocol(int protocol_num);\n", protocol_name); fprintf(hfile2, "MACEDON_Agent* %s_init_function();\n", protocol_name); fprintf(hfile2, "\n"); fprintf(cfile, "/*********************************************\n"); fprintf(cfile, " %s \n", cfilename); fprintf(cfile, " MACEDON generated on: \n"); fprintf(cfile, " %s", asctime(timeinfo)); fprintf(cfile, "*********************************************/\n\n\n"); fprintf(cfile, "extern int random_integer( int maximum);\n\n"); fprintf(cfilefuncs, "/*********************************************\n"); fprintf(cfilefuncs, " %s \n", cfilefuncsname); fprintf(cfilefuncs, " MACEDON generated on: \n"); fprintf(cfilefuncs, " %s", asctime(timeinfo)); fprintf(cfilefuncs, "*********************************************/\n\n\n"); fprintf(cfilefuncs, "#include \"macedon.h\"\n"); fprintf(cfilefuncs, "#include \"nsport.h\"\n"); fprintf(cfilefuncs, "#include \"%s.h\"\n", protocol_name); fprintf(cfilefuncs, "#include \"%s\"\n", hfilemacrosname); fprintf(cfilefuncs, "\n"); fprintf(cfilefuncs, "extern int PT_%s;\n", protocol_name); fprintf(cfile2, "/*********************************************\n"); fprintf(cfile2, " %s \n", cfile2name); fprintf(cfile2, " MACEDON generated on: \n"); fprintf(cfile2, " %s", asctime(timeinfo)); fprintf(cfile2, "*********************************************/\n\n\n"); fprintf(cfile2, "extern int random_integer( int maximum);\n"); fprintf(cfile2, "#include \"macedon.h\"\n"); fprintf(cfile2, "#include \"nsport.h\"\n"); fprintf(cfile2, "#include \"%s.h\"\n", protocol_name); fprintf(cfile2, "#include \"%s\"\n", hfilemacrosname); fprintf(cfile2, "\n"); fprintf(cfile2, "extern int PT_%s;\n", protocol_name); fprintf(cfile, "#include \"macedon.h\"\n"); fprintf(cfile, "#include \"nsport.h\"\n"); fprintf(cfile, "#include \"%s.h\"\n", protocol_name); fprintf(cfile, "#include \"%s\"\n", hfilemacrosname); fprintf(cfile, "\n"); fprintf(cfile, "%s_Agent *global%s;\n", protocol_name, protocol_name); fprintf(cfile, "\n"); fprintf(cfile, "int PT_%s;\n", protocol_name); fprintf(cfile, "\n"); fprintf(cfile, "MACEDON_Agent* %s_init_function() {\n", protocol_name); fprintf(cfile, " return new %s_Agent();\n", protocol_name); fprintf(cfile, "}\n"); fprintf(cfile, "\n"); fprintf(cfile, "int %s_load_protocol(int protocol_num) {\n", protocol_name); fprintf(cfile, " PT_%s = protocol_num;\n", protocol_name); fprintf(cfile, " hdr_%s::offset_ = hdr_ip::offset_ + sizeof(hdr_ip);\n", protocol_name); fprintf(cfile, " init_array[protocol_num] = %s_init_function;\n", protocol_name); fprintf(cfile, " return 0;\n"); fprintf(cfile, "}\n"); fprintf(cfile, "\n"); fprintf(hfilemacros, "/*********************************************\n"); fprintf(hfilemacros, " %s \n", hfilemacrosname); fprintf(hfilemacros, " MACEDON generated on: \n"); fprintf(hfilemacros, " %s", asctime(timeinfo)); fprintf(hfilemacros, "*********************************************/\n\n\n"); fprintf(hfilemacros, "#ifndef _%s_MACROS_H\n", protocol_name); fprintf(hfilemacros, "#define _%s_MACROS_H\n", protocol_name); fprintf(hfilemacros, "\n"); fprintf(hfilemacros, "#include \"macedon-macros.h\"\n"); fprintf(hfilemacros, "\n"); if (tracing >= 1) fprintf(hfilemacros, "#define cut_trace() trace_print() \n"); else fprintf(hfilemacros, "#define cut_trace() \n"); fprintf(hfilemacros, "#define timer_resched(x,y) do { timer_##x->stop(); timer_##x->start((double)y);"); if (tracing > 1) { fprintf(hfilemacros, "\\\n debug_macro(\"Timer: Resched timer \"#x\" for time %%f\\n\",(double)y); "); } fprintf(hfilemacros, " } while(0)\n"); fprintf(hfilemacros, "\n"); fprintf(hfilemacros, "\n"); fprintf(hfilemacros, "#define field(x) hdr->%s_header_##x \n",protocol_name); fprintf(hfilemacros, "#define state_change(x) \\\n"); fprintf(hfilemacros, " {fsm = %s_state_##x ; \\\n",protocol_name); fprintf(hfilemacros, " if(fsm_hint != %s_state_hint_##x) { \\\n",protocol_name); fprintf(hfilemacros, " fsm_hint = %s_state_hint_##x; \\\n",protocol_name); fprintf(hfilemacros, " debug_macro(\"FSM hint: state status changed to %%d\\n\", fsm_hint); \\\n"); fprintf(hfilemacros, " upcall_ext(AUTOEXT_STATUS_CHANGE, (void*)fsm_hint); \\\n"); fprintf(hfilemacros, " }\\\n"); fprintf(hfilemacros, " debug_macro(\"FSM: state changed to %%d\\n\", fsm); } \n"); fprintf(hfilemacros, "\n"); #ifdef ALLINONEHDR fprintf(hfilemacros, "#define unpack_pkt() \\\n"); fprintf(hfilemacros, " hdr_ip * iphdr = gethdrip( pkt ); \\\n"); fprintf(hfilemacros, " hdr_%s* hdr = (hdr_%s*)gethdr%s( pkt ); \\\n", protocol_name, protocol_name, protocol_name);#else fprintf(hfilemacros, "#define unpack_pkt(msgtype) \\\n"); fprintf(hfilemacros, " hdr_ip * iphdr = gethdrip( pkt ); \\\n"); fprintf(hfilemacros, " hdr_%s_##msgtype* hdr = (hdr_%s_##msgtype*)gethdr%s( pkt ); \\\n", protocol_name, protocol_name, protocol_name);#endif fprintf(hfilemacros, " hdr_cmn* ch = gethdrcmn( pkt ); \\\n"); fprintf(hfilemacros, " int size = pkt->size_; \\\n"); fprintf(hfilemacros, " char *msg = (char *)pkt->data_; \\\n"); fprintf(hfilemacros, " pkt->data_ = 0; \\\n"); fprintf(hfilemacros, " pkt->size_ = 0; \\\n"); fprintf(hfilemacros, " int dest = hdr->%s_mf_.mh_dest_addr_; \\\n", protocol_name); fprintf(hfilemacros, " int from = hdr->%s_mf_.mh_src_addr_;\n", protocol_name); fprintf(hfilemacros, "\n"); return 0;}start_agent(){ declare *temp; timer *tempt; int i; message_field *field; message *tempm = my_messages; fprintf(hfile, " Packet *%s_prep_delivery(char *msg, int size, int type, int nextkey, macedon_key nextkeyhash, int from_pipe=0);\n", protocol_name); fprintf(hfile, " int %s_my_forward(char *msg, int size, int type, int nextkey, macedon_key nextkeyhash);\n", protocol_name); fprintf(hfile, " void %s_my_deliver(char *msg, int size, int type);\n", protocol_name); fprintf(hfile, " void %s_my_notify(int type, int size, int *neighbors);\n", protocol_name); fprintf(hfile, " int %s_my_upcall_ext(int operation, void *arg);\n", protocol_name); fprintf(hfile, "class %s_Agent : public MACEDON_Agent\n", protocol_name); fprintf(hfile, "{\n"); fprintf(hfile, " public:\n"); fprintf(hfile, " %s_Agent();\n", protocol_name); fprintf(hfile, " Packet *create_pkt(int type, int toaddr);\n"); while(tempm) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -