/* Go through command tree and discover all file dependencies. */ graphnode_t create_dependency_graph(command_t c){ graphnode_t subshell_graph = NULL; graphnode_t temp_node = NULL; graphnode_t dep_graph = NULL; if(c->type == AND_COMMAND || c->type == OR_COMMAND || c->type == PIPE_COMMAND || c->type == SEQUENCE_COMMAND) { dep_graph = create_dependency_graph(c->u.command[0]); if(dep_graph != NULL){ temp_node = dep_graph; while(temp_node->next_node != NULL){ temp_node = temp_node->next_node; } temp_node->next_node = create_dependency_graph(c->u.command[1]); }else dep_graph = create_dependency_graph(c->u.command[1]); } else if(c->type == SUBSHELL_COMMAND || c->type == SIMPLE_COMMAND){ //We want to make a dependency graph for the subshell first if(c->type == SUBSHELL_COMMAND) subshell_graph = create_dependency_graph(c->u.subshell_command); if(c->input != NULL){ temp_node = malloc(sizeof(struct graphnode)); if(!temp_node){ error(1,0, "Error allocating memory in create_dependency_graph.\n"); } temp_node->filename = c->input; temp_node->next_node = NULL; dep_graph = temp_node; } if(c->output != NULL){ temp_node = malloc(sizeof(struct graphnode)); if(!temp_node){ error(1,0,"Error allocating memory in create_dependency_graph.\n"); } temp_node->filename = c->output; temp_node->next_node = NULL; if(dep_graph != NULL) dep_graph->next_node = temp_node; else dep_graph = temp_node; } if(subshell_graph != NULL){ //This means that the subshell graph was created temp_node = dep_graph; dep_graph = subshell_graph; dep_graph->next_node = temp_node; } } else{ error(7,0,"Cannot determine command type in create_dependency_graph.\n"); } return dep_graph; }
command_stream_t make_forest (struct token_node_list *list) { struct command_stream* current_tree = NULL; struct command_stream* previous_tree = NULL; struct command_stream* head_tree = NULL; struct command_stream* tail_tree = NULL; while (list != NULL && list->head->next_node != NULL && 1) { struct token_node* current = list->head->next_node; //Take the token stream and convert it to a tree. command_t command_node = create_tree(current); current_tree = malloc(sizeof(struct command_stream)); current_tree->depend = create_dependency_graph(command_node); if (current_tree == NULL){ fprintf(stderr, "Was not able to allocate memory to standard error"); } current_tree->command = command_node; //The first iteration, so set both head and previous tree to current_tree (the first one) if (!head_tree) { head_tree = current_tree; previous_tree = head_tree; //head_tree = current_tree; //A later iteration, so increment the previous_tree while keeping head_tree constant. } else { tail_tree = head_tree; previous_tree->next = current_tree; tail_tree = NULL; previous_tree = current_tree; tail_tree = previous_tree->next; } list = list->next; tail_tree = NULL; } //Return the head_tree which should be the first tree linked to the rest of the trees. return head_tree; }
/** \fn int main(int argc, char * argv[]) * \brief Main program function. * \param argc Argument count. * \param argv Pointer Pointer to Argument vector * \return system int */ int main(int argc, char * argv[]) { /* Variables for parsing directories */ int lastd; int i; /* Error value */ int rc; /* Variable to read in command line input */ char inputfile[1000]; char directory[1000]; char filename[1000]; char templatename[1000]; char templatedirectory[1000]; /* For reading input files */ input_file * current_input_file; /* Structure to hold model data */ model_data * modeldata; /* Hold modeldata data */ xmachine * xmachines; xmachine_message * xmessage; variable * envvar; env_func * envfunc; variable * envdefine; variable * allvars; variable * constant_filter_vars; f_code * it_end_code; layer * layers; flame_communication * communications; model_datatype * datatypes; time_data * time_units; input_file * temp_input_file; /* Allocate memory for modeldata */ modeldata = (model_data *)malloc(sizeof(model_data)); /* 0=serial(default) 1=parallel 2=grid */ modeldata->code_type = 0; /* 0=dgraph.dot 1=stategraph.dot */ modeldata->depends_style = 0; modeldata->debug_mode = 1; inputfile[1]='\0'; /* Initialise pointers */ modeldata->name = NULL; modeldata->p_xmachines = &xmachines; xmachines = NULL; modeldata->p_xmessages = &xmessage; xmessage = NULL; modeldata->p_envvars = &envvar; envvar = NULL; modeldata->p_envfuncs = &envfunc; envfunc = NULL; modeldata->p_envdefines = &envdefine; envdefine = NULL; modeldata->p_allvars = &allvars; allvars = NULL; modeldata->p_it_end_code = &it_end_code; it_end_code = NULL; modeldata->p_layers = &layers; layers = NULL; modeldata->p_communications = &communications; communications = NULL; modeldata->p_datatypes = &datatypes; datatypes = NULL; modeldata->p_time_units = &time_units; time_units = NULL; modeldata->p_files = &temp_input_file; temp_input_file = NULL; modeldata->p_constant_filter_vars = &constant_filter_vars; constant_filter_vars = NULL; printf("xparser (Version %d.%d.%d)\n", VERSIONMAJOR, VERSIONMINOR, VERSIONMICRO); /* Must be at least the input file name */ if(argc < 2) { printf("Usage: xparser [XMML file] [-s | -p] [-f]\n"); printf("\t-s\tSerial mode\n"); printf("\t-p\tParallel mode\n"); printf("\t-f\tFinal production mode\n"); free_modeldata(modeldata); return 0; } /* Copy location of xparser */ strcpy(templatedirectory,argv[0]); /* parser command line */ while(argc >1){ if(argv[1][0] == '-') { switch (argv[1][1]){ case 's': modeldata->code_type = 0; break; case 'p': modeldata->code_type = 1; break; case 'f' : modeldata->debug_mode = 0; break; default: printf("xparser: Error - unknown option %s\n",argv[1]); free_modeldata(modeldata); return 0; } } else { strcpy(inputfile,argv[1]); /* printf("XMML input file : %s\n",argv[1]); */ } argc--; argv++; } if(inputfile[1] == '\0') { printf("xparser: Error - XMML must be specified\n"); free_modeldata(modeldata); return 0; } /* Print what type of code writing */ printf("\n"); printf("Code type : "); if(modeldata->code_type == 0) printf("Serial"); if(modeldata->code_type == 1) printf("Parallel"); if(modeldata->debug_mode == 1) printf(" (DEBUG)"); printf("\n"); /* Calculate directory to write files to */ i = 0; lastd = 0; while(inputfile[i] != '\0') { /* For windows directories */ if(inputfile[i] == '\\') lastd=i; /* For unix directories */ if(inputfile[i] == '/') lastd=i; i++; } /* If a directory is in the path */ if(lastd != 0) { strcpy(directory, inputfile); directory[lastd+1] = '\0'; } else directory[0] = '\0'; /* Calculate directory where xparser and template files are */ i = 0; lastd = 0; while(templatedirectory[i] != '\0') { /* For windows directories */ if(templatedirectory[i] == '\\') lastd=i; /* For unix directories */ if(templatedirectory[i] == '/') lastd=i; i++; } /* If a directory is in the path */ if(lastd != 0) { templatedirectory[lastd+1] = '\0'; } else templatedirectory[0] = '\0'; /* Appending templates subdir */ strcat(templatedirectory, "templates/"); printf("Input XMML file : %s\n", inputfile); printf("Model root dir : %s\n", directory); printf("Template dir : %s\n", templatedirectory); printf("\n"); current_input_file = add_input_file(modeldata->p_files); current_input_file->fullfilepath = copystr(inputfile); current_input_file->fulldirectory = copystr(directory); current_input_file->localdirectory = copystr(""); /* Read model from model xml file */ current_input_file = * modeldata->p_files; while(current_input_file) { if(current_input_file->enabled == 1) readModel(current_input_file, directory, modeldata); current_input_file = current_input_file->next; } rc = checkmodel(modeldata); if(rc == -1) { free_modeldata(modeldata); return 0; } /* Calculate dependency graph for model functions */ rc = create_dependency_graph(directory, modeldata); if(rc == -1) { free_modeldata(modeldata); return 0; } /* Looping through Template Directory searching for *.tmpl files */ DIR *dir = opendir (templatedirectory); struct dirent *dp; /* returned from readdir() */ if (dir == NULL) { printf("Error: Opening %s directory failed.\n", templatedirectory); return 0; /* opendir() failed */ } printf("Processing %s Directory.\n",templatedirectory); /* Loop through the directory. */ while ((dp = readdir (dir)) != NULL) { int len = (int)strlen(dp->d_name); if(dp->d_name[len-1] == 'l' && dp->d_name[len-2] == 'p' && dp->d_name[len-3] == 'm' && dp->d_name[len-4] == 't' && dp->d_name[len-5] == '.') /* if file ends with *.tmpl */ { strcpy(templatename, templatedirectory); strcat(templatename, dp->d_name); strcpy(filename, directory); strcat(filename, dp->d_name); filename[strlen(filename)-5] = '\0'; /* Parse templates to output files */ parseTemplate(filename, templatename, modeldata); } } parseAgentHeaderTemplate(directory, modeldata); /*parseUnittest(directory, modeldata);*/ /*parser0dtd(directory, modeldata);*/ /*parser0xsd(directory, modeldata);*/ free_modeldata(modeldata); printf("\n--- xparser finished ---\n\n"); printf("To compile and run the generated code, you will need:\n"); printf(" * libmboard (version %s or newer)\n", LIBMBOARD_MINVER_STR); /* Exit successfully by returning zero to Operating System */ return 0; }