/* $Id: tools.c,v 1.23 2005/01/21 09:01:49 mah Exp $ $Log: tools.c,v $ Revision 1.23 2005/01/21 09:01:49 mah Added an initial man page; generate usage information (--help) with groff and perl Revision 1.22 2005/01/19 10:24:21 mah New command-line option --cvs-status to add `cvs status' information. This information was always enabled before but this was too annoying if there is no cvs information available. Revision 1.21 2002/02/25 23:34:15 dirkl - connections of bus_protocol get now connected to the top-level module(very uggly implementation) Revision 1.20 2002/02/05 18:02:35 kroening Minor fixes for compiler warnings Revision 1.19 2001/08/25 17:59:25 cj *** empty log message *** Revision 1.18 2001/07/28 11:15:56 cb added --parse-only option Revision 1.17 2001/06/12 14:56:45 sbeyer * added reasonable error message for record type overflow * increased supported record size from 200 to 1000 fields Revision 1.16 2001/05/17 12:12:20 dirkl - added command-line option --no-translation Revision 1.15 2001/02/05 11:13:58 sbeyer extended command line for register name Revision 1.14 2001/01/25 15:06:52 sbeyer added output file name support Revision 1.13 2001/01/25 10:44:20 sbeyer finally wrote real 'safe' functions for allocating and deallocation nodes and strings; allocated node is now initialized with zeroes Revision 1.12 2001/01/23 15:59:01 sbeyer added wert_double support for knoten Revision 1.11 2001/01/21 17:22:58 sbeyer added additional option '--hdl-comments' Revision 1.10 2001/01/16 17:27:28 sbeyer better allocation/deallocation treatment Revision 1.9 2001/01/15 15:11:09 sbeyer cosmetic changes (like const qualifier) Revision 1.8 2001/01/12 15:22:07 sbeyer added const qualifier to char* for gdl_ausgabe Revision 1.7 2001/01/11 16:23:06 sbeyer added pvs_path command line option to help Revision 1.6 2001/01/11 11:47:28 sbeyer renamed subknoten to sub in struct knoten Revision 1.5 2001/01/10 18:10:31 sbeyer extended 'knoten' by source_line, source_file Revision 1.4 2001/01/09 16:27:33 sbeyer *** empty log message *** */ #include "tools.h" struct knoten *alloc_knoten() { struct knoten *tmp; tmp=(struct knoten*) malloc(sizeof(struct knoten)); allokierter_speicher+=sizeof(struct knoten); memset(tmp,0,sizeof(struct knoten)); return tmp; } char *alloc_str(const char *src) { char *tmp; tmp=(char *) malloc(strlen(src)+1); allokierter_speicher+=strlen(src)+1; strcpy(tmp,src); return tmp; } void free_str(char *str) { if (str) { freigegebener_speicher+=strlen(str)+1; free(str); str=0; } } void free_knot(struct knoten *knot) { if (knot) { free_str(knot->wert_str); freigegebener_speicher+=sizeof(struct knoten); free(knot); knot=0; } } void print_help() { printf( "Usage:\n" #include "help_usage.c" "Options:\n" #include "help_options.c" ); } void gdl_knoten(FILE *gdlfile,const struct knoten *k,int hor_pos,int is_root); void gdl_ausgabe(const char *filename,const struct knoten *wurzelknoten) { FILE *gdlfile; gdlfile=fopen(filename, "w"); fprintf (gdlfile,"graph: { layoutalgorithm:tree \n"); gdl_knoten(gdlfile,wurzelknoten,0,1); fprintf (gdlfile,"}\n"); fclose(gdlfile); } void knoten_speicherfreigeben(const struct knoten *k) { int i; for (i=0;isubanz;i++) { knoten_speicherfreigeben(k->sub[i]); } free_knot(k); } // kopiert einen Syntaxbaum und liefert einen Zeiger auf den Wurzelknoten der Kopie zurueck struct knoten *copysyntaxbaum(const struct knoten *const originalwurzel) { struct knoten *temp=NULL; const struct knoten *tempori=originalwurzel; int i; // Speicher fuer den neuen Knoten reservieren temp=alloc_knoten(); // Werte uebernehmen temp->typ=tempori->typ; temp->subtyp=tempori->subtyp; temp->subanz=tempori->subanz; temp->source_line=tempori->source_line; temp->source_file=tempori->source_file; if (tempori->wert_str) temp->wert_str=alloc_str(tempori->wert_str); temp->wert_int=tempori->wert_int; temp->wert_double=tempori->wert_double; // nun die Sub bearbeiten for (i=0;isubanz;i++) { temp->sub[i]=copysyntaxbaum(tempori->sub[i]); } return temp; } void gdl_knoten(FILE *gdlfile,const struct knoten *k,int hor_pos,int is_root) { int i; /// SVEN,DIRK: LOOK HERE if(!k) return; // neu; pvs2hdl stuetzt bei --vebrose sonst mit sigseg ab if (k->subtyp!=no_subtyp) { fprintf (gdlfile,"node: { title:\"%d\" label:\"", k); if (is_root) fprintf(gdlfile,"File: %s, ",k->source_file); fprintf(gdlfile,"Line %d, %s -> %s\" horizontal_order:%d }\n", k->source_line,token_string[k->typ], subtyp_string[k->subtyp],hor_pos); } else { fprintf (gdlfile,"node: { title:\"%d\" label:\"Line %d, %s : Wert_str='%s' Wert_int=%d\" horizontal_order:%d }\n", k,k->source_line,token_string[k->typ],k->wert_str,k->wert_int,hor_pos); } for (i=0;isubanz;i++) { gdl_knoten(gdlfile,k->sub[i],i,0); } for (i=0;isubanz;i++) { fprintf (gdlfile,"edge:{ sourcename: \"%d\" targetname: \"%d\" }\n",k,k->sub[i]); } } void knoten_ausgeben(const struct knoten *k) { int i; printf ("\tAddresse : %d\n",k); printf ("\tTyp : %s\n",token_string[k->typ]); printf ("\tSubtyp : %s\n",subtyp_string[k->subtyp]); printf ("\tWert (String) : %s\n",k->wert_str); printf ("\tQuellcodedatei : %s\n",k->source_file); printf ("\tQuellcodezeile : %d\n",k->source_line); printf ("\tAnzahl der Sub : %d\n",k->subanz); for (i=0;isubanz;i++) { printf ("\tSub %d : %d\n",i+1,k->sub[i]); } printf ("\n"); for (i=0;isubanz;i++) { knoten_ausgeben(k->sub[i]); } } void parse_tree_ausgeben(const struct knoten *knot) { printf ("Parse-Tree\n"); printf ("==========\n\n"); knoten_ausgeben(knot); } struct knoten* id_knoten(const char *name) { struct knoten *temp; temp=alloc_knoten(); temp->typ=ID_token; temp->subtyp=no_subtyp; temp->wert_str=alloc_str(name); temp->source_line=zeilennummer; temp->source_file=inputfile; temp->subanz=0; return temp; } struct knoten* idat_knoten(const char *name) { struct knoten *temp; temp=alloc_knoten(); temp->typ=ID_token; temp->subtyp=no_subtyp; temp->wert_str=alloc_str(name); temp->subanz=0; temp->source_line=zeilennummer; temp->source_file=inputfile; return temp; } struct knoten *number_knoten(const char *wert) { struct knoten *temp; temp=alloc_knoten(); temp->typ=NUMBER_token; temp->subtyp=no_subtyp; temp->wert_int=atoi(wert); temp->wert_double=atoi(wert); temp->source_line=zeilennummer; temp->source_file=inputfile; temp->subanz=0; return temp; } struct knoten *number_knoten_int(int wert) { struct knoten *temp; temp=alloc_knoten(); temp->typ=NUMBER_token; temp->subtyp=no_subtyp; temp->wert_int=wert; temp->wert_double=wert; temp->source_line=zeilennummer; temp->source_file=inputfile; temp->subanz=0; return temp; } struct knoten* string_knoten(const char *s) { struct knoten *temp; temp=alloc_knoten(); temp->typ=STRING_token; temp->subtyp=no_subtyp; temp->wert_str=alloc_str(s); temp->source_line=zeilennummer; temp->source_file=inputfile; temp->subanz=0; return temp; }