#include #include #include #include #include #include #include #include #include #include #include #include #include #define PROMPT "tm>" enum { ENC_NONE, ENC_RAST, ENC_TXT, ENC_RDF_DC, ENC_WTM, ENC_XML, ENC_HTML }; static int parse_only = 0; static int silent = 0; static TMTopicMap topicmap = NULL; /* default encoding is txt */ static int xencoding = ENC_TXT; static int comments_in_output = 1; static char* base_uri = NULL; static char* version_tag = NULL; static char *parse_mode = NULL; static int read_stdin = 0; static char *query = NULL; static int interactive = 0; static int N = 1; static int tm_result_dump = 0; static void print_chunk(const char *chunk, void*cl) { FILE *f = (FILE*)cl; fprintf(f,chunk); } #define MAX_MAPS 256 #define PARSE_BUF_SIZE 4096 static TM tm; static void tk_set_progname(const char*n); static void tk_err_exit(const char *fmt,...); static void qhelp(void); static void prompt(TMTopicMap topicmap); static void print_stats(TMTopicMap tm,FILE *f); #ifndef NDEBUG static char vstr[] = "Version 0.7 (Debug)"; #else static char vstr[] = "Version 0.7"; #endif void usage(void) { printf("Usage: tm [-hsvIPLQd] " "[-q query]" "[-p processing model for processing] [-A TMAs to load] " "[-b base-uri] [-i input-syntax] " #ifndef NDEBUG "[-t tracemask] " #endif "[files...]\n"); } void version(void) { printf("tm: %s\n\n", vstr); } void list_apps(void) { printf("\n\n"); printf("tm: list of avalable Topic Map Applications\n\n"); printf(" \n"); printf(" IS13250\n"); printf(" sam\n"); printf(" sx\n"); printf(" dc\n"); printf(" rss\n"); printf(" \n"); printf(" Please use tmainfo for details about the applications:\n\n"); printf(" tmainfo appname\n\n"); } void list_pms(void) { printf("\n\n"); printf("tm: list of avalable Topic Map Processing Models\n\n"); printf(" \n"); printf(" xtm: a simple PM for XTM (uses TMA 'SAM') \n"); printf(" wtm: a simple PM for WTM\n"); printf(" rdf_common: can handle some of the common RDF statements\n"); printf(" such as Dublin Core and RSS (uses 'DC' semantics)\n"); printf(" iso2788: used for processing ISO2788 thesauri\n"); printf(" iso2788namesonly: processes only descriptors of ISO2788 thesauri\n"); } /* static void _export(TMTopicMap topicmap, FILE *f); static void _index(TMTopicMap topicmap, FILE *f, int format); static void _index_html(TMTopicMap topicmap, FILE *f); static void _rindex(TMTopicMap topicmap, FILE *f, int format); static void _namelist1(TMTopicMap topicmap, FILE *f, int format); static void _iso2788_descriptors(TMTopicMap topicmap, FILE *f, int format); static void _complete_xtm(TMTopicMap topicmap, FILE *f); */ static void _complete(TMTopicMap topicmap, FILE *f, int format); /* static void _complete_xml(TMTopicMap topicmap, FILE *f); */ void help(void) { printf("tm: topic map processing tool\n\n"); usage(); printf("\nOptions:\n"); printf(" -h show this screen\n"); printf(" -F framework the name of the disclosure framework to use (defaults to 'www-df')\n"); printf(" -S storage the name of the storage implementation to use (defaults to 'MemStore')\n"); printf(" -i input-type input is of parse type 'input-type'. This option\n"); printf(" is used to choose which parser is to be used.\n"); printf(" Recognized types are:\n"); #ifdef HAVE_LIBEXPAT printf(" xml (default) (parser is expat)\n"); #endif #ifdef HAVE_LIBRAPTOR printf(" rdfxml (parser is raptor)\n"); printf(" rdfntriples (parser is raptor) (not implemented)\n"); #endif printf(" lines (build in line-oriented parser\n"); printf(" -I read input from stdin\n"); printf(" -p procmodel the name of the processing model to use for processing.\n"); printf(" This will be used for all given maps.\n"); printf(" -a argstring argument to pass to processing model\n"); printf(" -A app-list additional Applications to be loaded into topic map\n"); printf(" app-list is a comma seperated list of application names\n"); printf(" -o format use 'format' for the output\n"); printf(" Recognized formats are:\n"); printf(" txt: ASCII output\n"); printf(" plain: Plain ASCII output (without comments)\n"); printf(" xml: XML (still needs fixes)\n"); printf(" rast: RAST (still needs some fixes)\n"); /* printf(" html: HTML\n"); printf(" rdfdc: RDF using Dublin Core namespace\n"); printf(" wtm: WTM\n"); */ printf(" -b base-uri use base-uri base URI\n"); printf(" -P only process maps, don't construct topicmap\n"); printf(" -Q launch interactive query interface\n"); printf(" -q query specify a query to be issued\n"); printf(" -L loglevel set loglevel to be used ('debug','info','warn','error' or 'critical')\n"); printf(" -v show version information and exit\n"); printf(" -s silent mode (no progression bar)\n"); printf(" -M do not fully merge the topic map\n"); printf(" -d dump topic map\n"); printf(" -D dump result topic map (if query result is one)\n"); printf(" -N n do the processing n times \n"); /* #ifndef NDEBUG printf(" -t mask enable trace output (for debugging) \n"); printf(" Recognized flags are:\n"); printf(" *: full trace (use -t \"*\" !)\n"); printf(" t: topicmap component\n"); printf(" s: storage\n"); printf(" p: syntax parsing\n"); printf(" m: memory pools\n"); printf(" x: extreme traces\n"); printf(" a: traces of *this* application \n"); printf(" o: traces of the omnivore\n"); printf(" v: traces of values, valuetypes\n"); printf(" q: traces of query component (parsing,compiling)\n"); #if 0 printf(" o: omnivore (processing)\n"); printf(" p: syntax processing model\n"); printf(" v: value type functions\n"); printf(" a: user application\n"); printf(" P: other parsing components (e.g. TMADL parser)\n"); #endif #endif */ /* printf(" -m list available Topic Map Processing Models\n"); printf(" -l list available Topic Map Applications\n"); printf(" \n"); printf(" \n"); */ printf(" \n"); printf(" \n"); #if 0 printf("tm is TmTk's commandline tool for working with topic maps.\n"); printf("\n"); printf("Loading data into the topic map:\n"); printf(" tm takes a list of files as commandline arguments and \n"); printf(" will apply the processing model specified with the -p option\n"); printf(" to all the input files. tm -m will list the available\n"); printf(" processing models with a short description\n"); printf(" When given the -I option, tm will read data from STDIN.\n"); printf("\n"); printf(" Depending on how tm has been built, several parsers are\n"); printf(" available and the -i option will tell tm which parser to\n"); printf(" invoke for the input data.\n"); printf("\n"); printf("\n"); printf("Querying the topic map:\n"); printf(" With the -q option, a query can be executed the result of which\n"); printf(" will be in the output type that has been specified with the -e\n"); printf(" option. See above for a list of output types.\n"); printf(" The option -Q will launch tm's interactive query interface.\n"); printf("\n"); printf("Please check http://www.gooseworks.org for extended documentation.\n"); printf("\n"); printf("\n"); printf("tm is a flexible tool for topic map processing. Its primary uses are:\n"); printf(" - doing a quick parse check for a topic map (use the -P option)\n"); printf(" - investigate a topic map quickliy by displaying it in several formats\n"); printf(" - loading a topic map into persistent storage (not until tmtk 1.0)\n"); printf(" - transforming a topic map to another syntax (propably lossy)\n"); printf(" - exracting portions of a topic map in formats that can be used for\n"); printf(" subsequent information retrieval tasks such as query expasnion,\n"); printf(" text mining, etc.\n"); /* printf("\n"); printf("\n"); printf("\n"); printf("\n"); */ printf("\n"); /* printf(" -u uri specify URI of a map to import\n"); printf(" -t type type of input data\n"); printf(" -f file file to open\n"); printf(" http://www.proxy.com:8080 \n"); printf(" -e enable subjectEquivalence callback\n"); printf(" -u uri specify URI of a map to import\n"); printf(" -a enable association callback\n"); printf(" -o export the topicmap as XTM\n"); */ #endif exit(0); } /* static TMError merge_map_handler(Omnivore ov, const char* loc); static TMError handler(Omnivore ov,TMAssertion assertion); static TMError built_in_topic_handler(Omnivore ov, TMSubject subject); static TMError add_property_handler(Omnivore ov, TMSubject subject, const char *model, const char *prop, const void *value); */ int main(int argc, char **args) { char *df_name = NULL; char *storage_name = NULL; char *loglevel = NULL; TMPool pool; char *topicmap_name = NULL; char *maps[MAX_MAPS]; int i,map_cnt = 0; char cwd_buf[256]; int j; /* for N iterations of processing */ int dump = 0; int fully_merge = 1; const char *config_file = NULL; /* TMError e; */ char *pm_arg = NULL; TMList lp,add_model_list = NULL; /* TMModel model; TMProcModel proc_model = NULL; */ char *pm_name = NULL; char option; tm = tm_init(); pool = tm_get_pool(tm); tm_model_dict_put(tm,"http://purl.org/tm/schemas/amod/",PREFIX"/tmtk/www-df/schemas/amod"); tm_model_dict_put(tm,"http://purl.org/tm/schemas/w3/",PREFIX"/tmtk/www-df/schemas/w3"); tm_model_dict_put(tm,"http://purl.org/tm/schemas/naming/",PREFIX"/tmtk/www-df/schemas/naming"); tm_model_dict_put(tm,"http://purl.org/tm/schemas/hry/",PREFIX"/tmtk/www-df/schemas/hry"); tm_model_dict_put(tm,"http://purl.org/tm/schemas/dc/",PREFIX"/tmtk/www-df/schemas/dc"); /* tm_model_dict_put(tm,"http://purl.org/dc/elements/1.1/",PREFIX"/tmtk/www-df/schemas/dc"); */ #if 0 if( strcmp(name,"http://purl.org/tm/sam/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/sam",PREFIX); } else if( strcmp(name,"http://purl.org/tm/amod/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/amod",PREFIX); } else if( strcmp(name,"http://purl.org/tm/hry/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/hry",PREFIX); } else if( strcmp(name,"http://purl.org/tm/DC/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/DC",PREFIX); } else if( strcmp(name,"http://purl.org/tm/UML/Deployment/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/UML/Deployment",PREFIX); } else if( strcmp(name,"http://purl.org/tm/ITIL/CMDB/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/ITIL/CMDB",PREFIX); } else if( strcmp(name,"http://purl.org/tm/UML/Dependencies/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/UML/Dependencies",PREFIX); } else if( strcmp(name,"http://purl.org/tm/UML/Kernel/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/UML/Kernel",PREFIX); } else if( strcmp(name,"http://purl.org/tm/Example/") == 0) { snprintf(filename,sizeof(filename),"%s/tmtk/www-df/schema/Example",PREFIX); } else if( strstr(name,"file://") == name) { snprintf(filename,sizeof(filename),"%s",name+7); } else { tm_set_error(tm,"cannot find model named %s" ,name); return TM_FAIL; } ..... #endif /* TMList list = NULL; list = tm_list_push(pool,list,(void*)1); list = tm_list_push(pool,list,(void*)2); list = tm_list_push(pool,list,(void*)3); list = tm_list_push(pool,list,(void*)4); list = tm_list_sort(list,NULL); for(; list;list=list->next) printf("%d\n",(int)list->content); */ tk_set_progname(args[0]); if( getcwd(cwd_buf,sizeof(cwd_buf)) == NULL) tk_err_exit("unable to get cwd, %s\n",strerror(errno)); opterr = 0; while( (option = getopt(argc,args, "-QIhMsvPdDL:F:S:t:C:a:i:o:a:A:b:p:q:V:N:")) != EOF) { switch(option) { case 'h': help(); exit(0); break; case 'F': df_name = tm_strdup(pool,optarg); break; case 'S': storage_name = tm_strdup(pool,optarg); break; case 'I': read_stdin = 1; break; case 'd': dump = 1; break; case 'D': tm_result_dump = 1; break; case 'P': parse_only = 1; break; case 's': silent = 1; break; case 'L': loglevel = tm_strdup(pool,optarg); break; case 'V': assert(0); version_tag = tm_strdup(pool,optarg); break; case 'v': version(); exit(0); case 't': tm_set_trace_mask(optarg); loglevel = tm_strdup(pool,"debug"); break; case 'b': base_uri = tm_strdup(pool,optarg); break; case 'M': fully_merge = 0; break; case 'q': query = tm_strdup(pool,optarg); break; case 'N': N = atoi(optarg); break; case 'C': assert(0); config_file = tm_strdup(pool,optarg); break; case 'a': pm_arg = tm_strdup(pool,optarg); break; case 'Q': interactive = 1; break; case 'i': parse_mode = tm_strdup(pool,optarg); break; case 'p': pm_name = tm_strdup(pool,optarg); break; case 'A': { char *p; char buf[1024]; const char *sp = optarg; while( (p = tm_strtok(&sp,",",buf,sizeof(buf) )) != NULL) { add_model_list = tm_list_push(pool,add_model_list,tm_strdup(pool,p)); } break; } case 'o': if(strcmp(optarg,"plain") == 0) { comments_in_output = 0; xencoding = ENC_TXT; } else if(strcmp(optarg,"txt") == 0) { comments_in_output = 1; xencoding = ENC_TXT; } else if(strcmp(optarg,"rast") == 0) xencoding = ENC_RAST; else if(strcmp(optarg,"rdfdc") == 0) xencoding = ENC_RDF_DC; else if(strcmp(optarg,"wtm") == 0) xencoding = ENC_WTM; else if(strcmp(optarg,"xml") == 0) xencoding = ENC_XML; else if(strcmp(optarg,"html") == 0) xencoding = ENC_HTML; else tk_err_exit("unknow output encoding %s",optarg); break; case 'g': assert(0); if(topicmap_name) { usage(); exit(1); } if( (topicmap_name = (char*)malloc(strlen(optarg)+1)) == NULL) tk_err_exit("out of memory"); strcpy(topicmap_name,(const char*)optarg); break; case 1: if(map_cnt >= MAX_MAPS) tk_err_exit("maximum number of topic maps " "exceeded\n"); if( tm_uri_is_abs(optarg)) { maps[map_cnt]=(char*)malloc(strlen(optarg)+1); if( ! maps[map_cnt]) tk_err_exit("out of memory"); strcpy(maps[map_cnt],optarg); } else { maps[map_cnt]=(char*)malloc( strlen(cwd_buf)+1+strlen(optarg)+1); if( ! maps[map_cnt]) tk_err_exit("out of memory"); sprintf(maps[map_cnt],"%s/%s",cwd_buf,optarg); } map_cnt++; break; case '?': usage(); exit(1); } } if(config_file) { assert(0); } if(df_name == NULL) { df_name = tm_strdup(pool,"www-df"); } if(tm_set_df(tm,df_name,NULL) != TM_OK) { fprintf(stderr,"ERROR: %s\n",tm_get_error(tm)); /* FIXME */ exit(1); } TM_FREE(pool,df_name); for(lp=add_model_list;lp;lp=lp->next) { TMModel model; if( tm_lookup_model(tm, (const char*)lp->content ,&model) != TM_OK) { tk_err_exit("unable to load model %s, %s", (const char*)lp->content,tm_get_error(tm) ); } TM_FREE(pool,lp->content); lp->content = model; } if( loglevel == NULL ) tm_set_loglevel(tm,TM_LOG_WARNING); else if( strcmp(loglevel,"debug") == 0) tm_set_loglevel(tm,TM_LOG_DEBUG); else if( strcmp(loglevel,"info") == 0) tm_set_loglevel(tm,TM_LOG_INFO); else if( strcmp(loglevel,"warning") == 0) tm_set_loglevel(tm,TM_LOG_WARNING); else if( strcmp(loglevel,"error") == 0) tm_set_loglevel(tm,TM_LOG_ERROR); else if( strcmp(loglevel,"critical") == 0) tm_set_loglevel(tm,TM_LOG_CRITICAL); else tk_err_exit("unknown log level %s" , loglevel); if(loglevel) TM_FREE(pool,loglevel); if(storage_name == NULL) { storage_name = tm_strdup(pool,"MemStore"); } for(j=0;jnext) { if(tm_topicmap_require_model(topicmap,(TMModel)lp->content) != TM_OK) tk_err_exit("unable to load model %s, %s\n", ((TMModel)lp->content)->name, tm_get_error(tm) ); } /* if(do_log) tm_topicmap_set_log_function(topicmap,tm_log); */ } if(!pm_name && (read_stdin || map_cnt > 0) ) { pm_name = tm_strdup(pool,"wtm"); } /* tk_err_exit("cannot process input: no processing model specified\n"); */ if(! read_stdin) { for(i=0;i= (percent_save + percent_inc)) ) { double diff,n; double i; diff = percent_done - (percent_save + percent_inc); n = diff / percent_inc; for(i=0;i0;N--) printf("*"); fflush(stdout); printf("\n%.0f %% done\n",percent_done); fflush(stdout); } Omnivore_delete( &o ); if(! parse_only) { if(fully_merge) { if( tm_topicmap_fully_merge(topicmap) != TM_OK) tk_err_exit("after processing %s: unable to merge topicmap %s\n", maps[i],tm_get_error(tm) ); } tm_topicmap_end_transaction(topicmap); } } /* while more maps */ } /* if not read stdin */ else /* read stdin */ { Omnivore o; int fd; fd = 0; /* stdin, FIXME */ o = Omnivore_new(pool,tm); if(!o) { fprintf(stderr,"cannot read STDIN, %s\n",tm_get_error(tm)); /* FIXME cleanup mem */ exit(1); } if( tm_omnivore_prepare(o,parse_mode ? parse_mode : "xml" ,pm_name,pm_arg,topicmap) != TM_OK) tk_err_exit("cannot prepare omnivore, %s\n", tm_get_error(tm)); if(base_uri) Omnivore_setDocUri(o,base_uri); /* if(do_log) tm_omnivore_set_log_function(o,tm_log); */ if(! parse_only) { /* Omnivore_setAssertionHandler(o,handler); Omnivore_setMergeMapHandler(o,merge_map_handler); Omnivore_setBuiltInNodeHandler(o,built_in_topic_handler); Omnivore_setAddPropertyHandler(o,add_property_handler); */ } if(! parse_only) { tm_topicmap_start_transaction(topicmap,"from-stdin"); } for (;;) { char buf[PARSE_BUF_SIZE]; int len; if( (len = read(fd, buf, sizeof(buf))) < 0) tk_err_exit("error reading %s, %s\n", maps[i], strerror(errno)); if (! Omnivore_parse(o, buf, len, len == 0)) { tk_err_exit("Parse error: %s\n", tm_get_error(tm) ); } if (len == 0) break; } Omnivore_delete( &o ); if(! parse_only) { if(fully_merge) { if( tm_topicmap_fully_merge(topicmap) != TM_OK) tk_err_exit("after processing %s: unable to merge topicmap %s\n", maps[i],tm_get_error(tm) ); } tm_topicmap_end_transaction(topicmap); } } /* if read stdin */ if(! parse_only) { if(interactive) { prompt(topicmap); } if(query) { TMResult result = NULL; int i,j; tm_topicmap_start_transaction(topicmap,"query"); if( tm_topicmap_query(topicmap,query,&result) != TM_OK) { tk_err_exit("unable to process query %s, %s\n", query, tm_get_error(tm) ); } assert(result); switch(result->type) { case TM_RESULT_TOPICMAP: if( tm_result_dump ) { tm_topicmap_start_transaction(result->topicmap,"dump"); tm_topicmap_dump(result->topicmap,stdout); tm_topicmap_end_transaction(result->topicmap); } make_wtm(result->topicmap,print_chunk,stdout,0); break; case TM_RESULT_SUBJECTS: for(i=0;insubjects;i++) { TMSubject sbj = &(result->subjects[i]); printf("#%06d\n",result->topics[i]); TMTRACE(TM_X_TRACE,"subject: %d\n" _ i); for(j=0;jN;j++) { char buf[1024]; TMTRACE(TM_X_TRACE,"prop: %s\n" _ sbj->props[j].prop->name); assert(sbj->props[j].prop->value_type); tm_value_to_string( sbj->props[j].prop->value_type, sbj->props[j].value, buf,1024); printf("%s%s:%s\n", j==0 ? "* " : " ", sbj->props[j].prop->fullname, buf ); } printf("\n"); } break; case TM_RESULT_TABLE: for(j=0;jncols;j++) { TMProperty p = result->prop_heading[j]; printf("| %-30s\t",p->name); } printf("%s\n\n","|"); for(i=0;inrows;i++) { void **row; row = result->vtable[i]; for(j=0;jncols;j++) { TMProperty p = result->prop_heading[j]; printf("| %-30s\t", tm_value_as_string(p->value_type,row[j],NULL,0) ); } printf("%s\n","|"); } break; default: assert(0); } /* end switch */ tm_result_delete(&result); tm_topicmap_end_transaction(topicmap); } if(dump) { tm_topicmap_start_transaction(topicmap,"Dump"); _complete(topicmap,stdout,xencoding); tm_topicmap_end_transaction(topicmap); } tm_topicmap_close(topicmap); tm_topicmap_delete(&topicmap); } /* if not parse only */ } /* iterate N times */ if(storage_name) TM_FREE(pool,storage_name); if(base_uri) TM_FREE(pool,base_uri); if(query) TM_FREE(pool,query); if(pm_arg) TM_FREE(pool,pm_arg); if(parse_mode) TM_FREE(pool,parse_mode); if(pm_name) TM_FREE(pool,pm_name); tm_list_delete(pool,&add_model_list); tm_cleanup(&tm); return 0; } void _complete(TMTopicMap topicmap, FILE *f, int encoding) { switch(encoding) { case ENC_WTM: make_wtm(topicmap,print_chunk,f,0); break; case ENC_XML: /* _complete_xml(topicmap,f); tm_topicmap_xml_export(topicmap,f); */ break; case ENC_TXT: tm_topicmap_dump(topicmap,f); break; default: tk_err_exit("encoding not supported for 'complete'\n"); } } #if 0 void _test(TMTopicMap topicmap, FILE *f, int encoding) { TMTopicScan scan; TMTopic at_basenamed_basename_topic; TMTopic role_basenamed_topic; TMTopic role_basename_topic; TMTopic at_occurring_occurrence_topic; TMTopic role_occurring_topic; TMTopic role_occurrence_topic; TMModel m; TMProperty prop_descriptor; TMProperty prop_asidp; TMProperty prop_sbjadr; TMProperty prop_sbjdata; TMProperty prop_inddata; tmtk_default_model_lookup_function("ISO2788",&m); prop_descriptor = tm_model_get_property(m,"Descriptor"); assert(prop_descriptor); #if 0 prop_sbjadr = tm_model_get_property(m,"SubjectAddress"); prop_sbjdata = tm_model_get_property(m,"SubjectData"); prop_inddata = tm_model_get_property(m,"IndicatorData"); assert(prop_asidp); assert(prop_sbjadr); assert(prop_sbjdata); assert(prop_inddata); if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","AtBaseNamedBaseName", &(at_basenamed_basename_topic)) != TM_OK) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleBaseNamed", &(role_basenamed_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleBaseName", &(role_basename_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","AtOccurringOccurrence", &(at_occurring_occurrence_topic)) != TM_OK) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleOccurring", &(role_occurring_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleOccurrence", &(role_occurrence_topic)) != TM_OK ) { assert(0); } assert(at_basenamed_basename_topic); assert(role_basenamed_topic); assert(role_basename_topic); assert(at_occurring_occurrence_topic); assert(role_occurring_topic); assert(role_occurrence_topic); #endif /* if( tm_topicmap_topic_scan_open_with_single_condition(topicmap, "ISO2788","Descriptor","LIKE","ate",&scan) != TM_OK) { fprintf(stderr,"cannot open scan, %s\n", tm_get_error(tm)); return; } */ if( tm_topicmap_topic_scan_open_with_double_condition(topicmap, "ISO2788","Descriptor","=","soda water", "OR", "ISO2788","Descriptor","=","water", NULL,&scan) != TM_OK) { fprintf(stderr,"cannot open scan, %s\n", tm_get_error(tm)); return; } fprintf(f,"Testoutpt\n\n"); while(! tm_topicmap_topic_scan_finished(topicmap,scan) ) { TMTopic t; TMTopic b_topic; TMTopic t_topic; /* char buf[1024]; */ void *v = NULL,*v2 = NULL, *v3 = NULL; tm_topicmap_topic_scan_fetch(topicmap,scan,&t); tm_topicmap_get_property(topicmap,t,"ISO2788","Descriptor",&v); fprintf(f,"%s\n",v); tm_value_delete(prop_descriptor->value_type,&v); } tm_topicmap_topic_scan_close(topicmap,scan); } void _test2(TMTopicMap topicmap, FILE *f, int encoding) { TMTopicScan scan; TMTopic at_basenamed_basename_topic; TMTopic role_basenamed_topic; TMTopic role_basename_topic; TMTopic at_occurring_occurrence_topic; TMTopic role_occurring_topic; TMTopic role_occurrence_topic; TMModel m; TMProperty prop_descriptor; TMProperty prop_asidp; TMProperty prop_sbjadr; TMProperty prop_sbjdata; TMProperty prop_inddata; tmtk_default_model_lookup_function("ISO2788",&m); prop_descriptor = tm_model_get_property(m,"Descriptor"); assert(prop_descriptor); #if 0 prop_sbjadr = tm_model_get_property(m,"SubjectAddress"); prop_sbjdata = tm_model_get_property(m,"SubjectData"); prop_inddata = tm_model_get_property(m,"IndicatorData"); assert(prop_asidp); assert(prop_sbjadr); assert(prop_sbjdata); assert(prop_inddata); if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","AtBaseNamedBaseName", &(at_basenamed_basename_topic)) != TM_OK) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleBaseNamed", &(role_basenamed_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleBaseName", &(role_basename_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","AtOccurringOccurrence", &(at_occurring_occurrence_topic)) != TM_OK) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleOccurring", &(role_occurring_topic)) != TM_OK ) { assert(0); } if( tm_topicmap_get_topic(topicmap,"IS13250","UniqueName","RoleOccurrence", &(role_occurrence_topic)) != TM_OK ) { assert(0); } assert(at_basenamed_basename_topic); assert(role_basenamed_topic); assert(role_basename_topic); assert(at_occurring_occurrence_topic); assert(role_occurring_topic); assert(role_occurrence_topic); #endif /* if( tm_topicmap_topic_scan_open_with_single_condition(topicmap, "ISO2788","Descriptor","LIKE","ate",&scan) != TM_OK) { fprintf(stderr,"cannot open scan, %s\n", tm_get_error(tm)); return; } */ if( tm_topicmap_topic_scan_open_with_double_condition(topicmap, "ISO2788","Descriptor","=","soda water", "OR", "ISO2788","Descriptor","=","water", NULL,&scan) != TM_OK) { fprintf(stderr,"cannot open scan, %s\n", tm_get_error(tm)); return; } fprintf(f,"Testoutpt\n\n"); while(! tm_topicmap_topic_scan_finished(topicmap,scan) ) { TMTopic t; TMTopic b_topic; TMTopic t_topic; /* char buf[1024]; */ void *v = NULL,*v2 = NULL, *v3 = NULL; tm_topicmap_topic_scan_fetch(topicmap,scan,&t); tm_topicmap_get_property(topicmap,t,"ISO2788","Descriptor",&v); fprintf(f,"%s\n",v); tm_value_delete(prop_descriptor->value_type,&v); } tm_topicmap_topic_scan_close(topicmap,scan); } #endif const char *default_topicmap = "/tmp/.topicmap"; static char progname[256]; void tk_set_progname(const char*n) { assert(n); memset(progname,0,sizeof(progname)); strncpy(progname,n,sizeof(progname)); } void tk_err_exit(const char *fmt,...) { char b[2048], c[2048]; va_list args; assert(fmt); va_start(args,fmt); vsnprintf(b,sizeof(b),fmt,args); va_end(args); snprintf(c,sizeof(c),"\n%s: %s\n", progname,b); fprintf(stderr,c); exit(1); } void list_tmas(TMTopicMap topicmap,FILE *f) { TMList lp; fprintf(f,"these TMAs are loaded in the topic map:\n"); tm_topicmap_get_models(topicmap,&lp); /* FIXME: error check or not??? */ for(;lp;lp=lp->next) { TMModel m = (TMModel)lp->content; fprintf(f,"%s\n",m->name); } } void prompt(TMTopicMap topicmap) { int quit; char qbuf[4096]; char *ptr; setbuf(stdout,NULL); tm_topicmap_start_transaction(topicmap,"interactive"); printf("\n\n"); printf("Welcome to tm's interactive mode!\n"); printf("\n"); printf("Type \\h for help\n"); printf("\n"); printf("\n"); quit = 0; bzero(qbuf,sizeof(qbuf)); ptr = qbuf; while(!quit) { char input[1024]; char *p; printf(PROMPT" "); p = fgets(input,sizeof(input),stdin); if(!p) break; p[strlen(p)-1] = '\0'; /* chomp */ if(*p == '\0') continue; if(*p != '\\') /* not a command */ { strcpy(ptr,p); if(p[strlen(p)-1] != ';') { ptr += strlen(p); continue; } qbuf[strlen(qbuf)-1] = '\0'; printf("_%s_\n",qbuf); if( tm_topicmap_query(topicmap,qbuf,NULL) != TM_OK) { printf("could not execute query '%s', %s\n", qbuf, tm_get_error(tm) ); } bzero(qbuf,sizeof(qbuf)); ptr = qbuf; continue; } bzero(qbuf,sizeof(qbuf)); ptr = qbuf; switch(*++p) { case '\0': continue; case 'q' : quit = 1; break; case 'h' : qhelp(); break; case 'l' : list_tmas(topicmap,stdout); break; case 's' : print_stats(topicmap,stdout); break; case 'd' : _complete(topicmap,stdout,xencoding); break; default: continue; } } tm_topicmap_end_transaction(topicmap); return; } void qhelp(void) { printf("\n\nList of commands:\n"); printf(" \\q Quit\n"); printf(" \\h Show this screen\n"); printf(" \\l list the loaded TMAs\n"); #if 0 printf(" \\X Show the list of all tenplates\n"); printf(" \\N Show the list of all basenames\n"); printf(" \\O Show the list of all occurrences\n"); printf(" \\C Show the list of all classes\n"); printf(" \\I Show the list of all instances\n"); printf(" \\R Show the list of all played roles\n"); printf(" \\S Show the list of all scopes\n"); printf("\n"); printf(" \\a Show the templates and their constraints\n"); printf(" \\n Show a list of subjects and their names\n"); printf(" \\o Show a list of subjects and their occurrences\n"); printf(" \\s Show the basenames by scopes\n"); #endif printf("\n\n\n"); } void print_stats(TMTopicMap tm,FILE *f) { TMList models,lp; fprintf(f,"Topic Map Statistics\n"); fprintf(f,"--------------------\n"); tm_topicmap_get_models(tm,&models); for(lp=models;lp;lp=lp->next) { fprintf(f,"%s\n",((TMModel)lp->content)->name); } } struct load_map { char *map; char *mode; char *pm; char *arg; }; #if 0 /* LoadMap /home/jan/foo xml xtm_simple - */ TMError _read_config(TM tm, const char *filename, TMList *listp) { char buf[1024]; char *p; FILE *file; TMList list = NULL; if( (file = fopen(filename,"r")) == NULL) { tm_set_error(o,"unable to open config file %s, %s", filename, strerror(errno)); return TM_FAIL; } while( (p = fgets(buf,sizeof(buf), file)) != NULL) { if(tm_is_whitespace_string(p)) continue; if(tm_is_comment_string(p,'#')) continue; p = (char*)tm_lstrip(p); if( strstr(p,"LoadMap") == p) { struct load_map *lmp; char *q; TMError e; TM_NEW(lmp); mlp->map = NULL; mlp->mode = NULL; mlp->pm = NULL; mlp->arg = NULL; p += 7; while( *p && isspace(*p)) p++; q = p; while( *p && !isspace(*p)) p++; *p = "\0"; mlp->map = tm_strdup(pool,q); } } *listp = list; } #endif