int cmd_remove(const char* arg) { char* ptr, *argv; struct ncds_ds_list* ds; struct model_list* model; if (strlen(arg) < 7) { cmd_remove_help(); return 1; } argv = strdupa(arg + strlen("remove ")); ptr = strtok(argv, " "); ds = find_datastore(ptr); if (ds == NULL) { model = find_model(ptr); if (model == NULL) { nc_verb_error("No datastore or model \"%s\" found", ptr); return 1; } else { ncds_ds_model_free(model->model); } } else { ncds_free(ds->datastore); } remove_hint(ptr); return 0; }
int cmd_print(const char* arg) { char* ptr, *argv; xmlDocPtr doc; struct ncds_ds_list* ds; struct model_list* model; argv = strdupa(arg); strtok(argv, " "); ptr = strtok(NULL, " "); if (ptr == NULL) { ds = ncds.datastores; model = models_list; printf("Datastores:\n"); if (ds == NULL) { printf("\tnone\n"); } for (; ds != NULL; ds = ds->next) { printf("\t%s\n", ds->datastore->data_model->name); } printf("Models:\n"); if (model == NULL) { printf("\tnone\n"); } for (; model != NULL; model = model->next) { printf("\t%s\n", model->model->name); } } else { char* buf = NULL; int buf_len = 0; ds = find_datastore(ptr); if (ds == NULL) { model = find_model(ptr); if (model == NULL) { nc_verb_error("No datastore or model \"%s\" found", ptr); return 1; } else { doc = model->model->xml; } } else { doc = ds->datastore->ext_model; } xmlDocDumpFormatMemory(doc, (xmlChar**)&buf, &buf_len, 1); fwrite(buf, 1, buf_len, stdout); free(buf); } return 0; }
int cmd_feature(const char* arg) { char* argv, *ptr, *state_str, *saveptr; int i, ret = 0; struct model_list* list; struct data_model* model; if (strlen(arg) < 8) { cmd_feature_help(); return 1; } argv = strdupa(arg + strlen("feature ")); ptr = strtok_r(argv, " ", &saveptr); if (ptr == NULL) { cmd_feature_help(); return 1; } list = find_model(ptr); if (list == NULL) { nc_verb_error("No model \"%s\" found", ptr); return 1; } model = list->model; ptr = strtok_r(NULL, " ", &saveptr); /* we are done, no more arguments */ if (ptr == NULL) { printf("Features:\n"); if (model->features == NULL) { printf("\tnone\n"); return 0; } for (i = 0; model->features[i] != NULL; ++i) { printf("\t%s %s\n", model->features[i]->name, (model->features[i]->enabled ? "ON" : "OFF")); } return 0; } do { state_str = strtok_r(NULL, " ", &saveptr); } while (state_str != NULL && strcmp(state_str, "on") != 0 && strcmp(state_str, "off") != 0); /* there was no "yes" or "no" at the end */ if (state_str == NULL) { cmd_feature_help(); return 1; } if (model->features == NULL) { nc_verb_error("Model does not have any features"); return 1; } /* all features */ if (strcmp(ptr, "*") == 0) { for (i = 0; model->features[i] != NULL; ++i) { if (strcmp(state_str, "on") == 0) { model->features[i]->enabled = 1; } else { model->features[i]->enabled = 0; } } } else { /* one or more features */ ptr = argv + strlen(argv)+1; while (ptr != state_str) { for (i = 0; model->features[i] != NULL; ++i) { if (strcmp(model->features[i]->name, ptr) == 0) { if ((model->features[i]->enabled && strcmp(state_str, "on") == 0) || (!model->features[i]->enabled && strcmp(state_str, "off") == 0)) { nc_verb_verbose("Feature \"%s\" is already %s", ptr, state_str); } else if (strcmp(state_str, "on") == 0) { model->features[i]->enabled = 1; } else { model->features[i]->enabled = 0; } break; } } if (model->features[i] == NULL) { nc_verb_error("Model does not have the feature \"%s\"", ptr); ret = 1; } ptr = ptr + strlen(ptr)+1; } } return ret; }
int cmd_print(const char* arg) { int fd; char* ptr, *argv; xmlDocPtr doc; struct ncds_ds_list* ds; struct model_list* model; argv = strdupa(arg); strtok(argv, " "); ptr = strtok(NULL, " "); if (ptr == NULL) { ds = ncds.datastores; model = models_list; printf("Datastores:\n"); if (ds == NULL) { printf("\tnone\n"); } for (; ds != NULL; ds = ds->next) { printf("\t%s\n", ds->datastore->data_model->name); } printf("Models:\n"); if (model == NULL) { printf("\tnone\n"); } for (; model != NULL; model = model->next) { printf("\t%s\n", model->model->name); } } else { char* buf = NULL; int buf_len = 0; ds = find_datastore(ptr); if (ds == NULL) { model = find_model(ptr); if (model == NULL) { nc_verb_error("No datastore or model \"%s\" found", ptr); return 1; } else { doc = model->model->xml; } } else { doc = ds->datastore->ext_model; } xmlDocDumpFormatMemory(doc, (xmlChar**)&buf, &buf_len, 1); ptr = strtok(NULL, " "); if (ptr == NULL) { fwrite(buf, 1, buf_len, stdout); } else { fd = creat(ptr, 00660); if (fd == -1) { nc_verb_error("Failed to open file \"%s\" (%s)", ptr, strerror(errno)); free(buf); return 1; } if (write(fd, buf, buf_len) < buf_len) { nc_verb_error("Failed to write into file (%s)", strerror(errno)); free(buf); close(fd); return 1; } close(fd); } free(buf); } return 0; }
elemptr symmodel::get_model_widx( const vector<string>& parsed, const vector<size_t>& idx, const vector<elemptr>& trace ) { //I start with it already parsed. //If parsed.size() == 0, I simply return this (with an index?) //The empty string indicates "this" model? No, when I parse it, I need to sep it, so an empty, will return in a zero-parse, of size zero, or nothing? //Either way, would result in same, so return ;) if( parsed.size() == 0 || parsed[0].compare("") == 0 ) { fprintf(stdout, "FOUND MODEL! [%s]\n", buildpath().c_str() ); elemptr t = elemptr( shared_from_this(), idx ); return t; } else { fprintf(stdout, "Model [%s], attempting to find model name [%s] widx (note, trace size is [%lu])\n", buildpath().c_str(), CAT(parsed, "/").c_str(), trace.size()); } //This is the next model I will go into string submodel = parsed[0]; vector<string> remainder( parsed.begin()+1, parsed.end() ); //same as remainder, remainder.erase(0); //REV: This is where I should iterate up the tree! This is the issue. vector<size_t> mlocs = find_model( submodel ); vector<size_t> hlocs = find_hole( submodel ); //At this point, we are finding the hole etc. normally. if( mlocs.size() >= 1 ) { if( mlocs.size() > 1 ) { fprintf(stderr, "WTF found more than one in getmodelwidx\n"); exit(1); } size_t mloc = mlocs[0]; //add model to trace? I guess? It is a submodel, so it is not necessary I guess? But it helps it find submodels I guess? Could this cause a problem? fprintf(stdout, "Model [%s], going through submodel [%s] to find [%s]\n", buildpath().c_str(), models[mloc]->localname.c_str(), CAT(remainder, "/").c_str() ); std::shared_ptr<symmodel> nextmodel = models[mloc]; //Don't add to trace because if same model, parent will cause infinite loop in combin with trace. //However //Problem is if I go through a hole, and the hole is the same model, that is the main problem vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); } else if( hlocs.size() >= 1 ) { if( hlocs.size() > 1) { fprintf(stderr, "WTF more than one HOLE found in getmodelwidx\n"); exit(1); } size_t hloc = hlocs[0]; fprintf(stdout, "Model [%s], going through hole [%s] to find [%s]\n", buildpath().c_str(), holes[hloc].name.c_str(), CAT(remainder, "/").c_str()); if( holes[ hloc ].members.size() != 1 ) { fprintf(stderr, "ERROR in get_model_widx, getting [%s] from HOLE, but hole [%s] has size [%lu], but it should be 1\n", submodel.c_str(), holes[hloc].name.c_str(), holes[hloc].members.size() ); exit(1); } //REV: so in the case it does not exist yet, we have a problem? std::shared_ptr<symmodel> nextmodel = holes[hloc].members[0]; if( check_same_toplevel_model( nextmodel ) ) { //Dont add to trace because its same model so infinite loop with going to parent.x vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); } else //not same toplevel model { //I NEED TO GO THROUGH A CORRESPONDENCE fprintf(stdout, "REV: about to go through a corresp to get a non-same model thing through a hole...\n"); //std::shared_ptr<corresp> mycorresp; auto mycorresp = getcorresp( nextmodel ); if( !mycorresp ) { fprintf(stderr, "REV: getcorresp in get_model_widx, failed, no such corresp exists between [%s] and [%s]\n", buildpath().c_str(), nextmodel->buildpath().c_str()); exit(1); } //REV; SANITY, if corresp not allocated yet, just return 0. //size_t idx_in_submodel = 0; vector<size_t> idx_in_submodel(1,0); //REV; Don't check this here, check this in the corresp struct? I.e. return dummy data if it is not existing yet (or exit?) if(mycorresp->isinit()) { fprintf(stdout, "Corresp is INIT!!!! Will attempt a GETALL...\n"); //REV: TODO HERE, just return it directly, with new IDX_IN_SUBMODEL ;0 //REV: this is it!!! This is where I vector<size_t> sanity = mycorresp->getall( idx ); fprintf(stdout, "Attempted to GETALL from the corresp!\n"); /*if( sanity.size() != 1 ) { fprintf(stderr, "SANITY check for corresp during access failed! Expected corresp for idx [%lu] of model [%s] to have only 1 corresponding element in model [%s], but it had [%lu]\n", idx, buildpath().c_str(), nextmodel->buildpath().c_str(), sanity.size() ); exit(1); } size_t idx_in_submodel = sanity[0]; //no change, b/c submodel. */ idx_in_submodel = sanity; } vector<elemptr> newtrace = trace; newtrace.push_back( elemptr( shared_from_this(), idx ) ); fprintf(stdout, "About to get next model with idx...\n"); auto toret = nextmodel->get_model_widx( remainder, idx_in_submodel, newtrace ); fprintf(stdout, "FINISHED About to get next model with idx...\n"); return toret; } } //end if not found in HOLES (or submodels) else { fprintf(stdout, "Model [%s], walking up to parent [%s] to find [%s]\n", buildpath().c_str(), parent->localname.c_str(), CAT(parsed, "/").c_str()); //Else, try to bubble up to ROOT. if( parent && (parent->parent) ) { std::shared_ptr<symmodel> nextmodel = parent; vector<elemptr> newtrace = trace; //size_t idx_in_submodel = idx; //no change, b/c submodel. vector<size_t> idx_in_submodel = idx; //no change, b/c submodel. //newtrace.push_back( elemptr( shared_from_this(), idx ) ); return nextmodel->get_model_widx( parsed, idx_in_submodel, newtrace ); } else if( parent && !(parent->parent) ) { //Couldn't find it! Return empty elemptr...bad. elemptr ep; return ep; } else { fprintf(stderr, "REV; this should never happen weird, Neither parent nor parent->parent? In searching for model with idx. Exit\n"); if( parent ) { fprintf( stderr, "Parent of me [%s] exists and is [%s]\n", buildpath().c_str(), parent->buildpath().c_str() ); } else { fprintf( stderr, "Parent does not exist... (note current model is [%s])!\n", buildpath().c_str() ); } exit(1); } } //couldn't find in "else" (i.e. not in this model, so try bubbling up parents) if(trace.size() == 0) { fprintf(stderr, "Trace size zero. This should never happen (should have been caught above)\n"); exit(1); } //REV: Did I mess something up? First it should check through all guys directly to see if it is same model? I.e. if target model matches b/c we can use that idx. fprintf(stdout, "Couldn't find model [%s] in previous model trace [%s], so moving to next! (trace size is [%lu])\n", CAT(parsed,"/").c_str(), buildpath().c_str(), trace.size() ); //Move back model and try again? vector<elemptr> newtrace = trace; //size_t idx_in_submodel = newtrace[ newtrace.size() - 1].idx; //end of trace. vector<size_t> idx_in_submodel = newtrace[ newtrace.size() - 1].idx; //end of trace. std::shared_ptr<symmodel> nextmodel = newtrace[ newtrace.size() - 1].model; newtrace.pop_back(); fprintf(stdout, "Will now try to run with new trace size [%lu]\n", newtrace.size() ); return nextmodel->get_model_widx( parsed, idx_in_submodel, newtrace ); } //end get_model_widx