Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
options.c File Reference

Go to the source code of this file.

Macros

#define CHECK(PARAM1, PARAM2, BODY)
 check parameter and execute code if it matches More...
 

Functions

void init_options (int argc, char *argv[])
 
void dump_statistics ()
 dump statistics More...
 
void finit_options ()
 finalizes the Options module More...
 
void output_compiler_name (int show_help)
 outputs the name of the compier More...
 
void set_compiler_filename (int argc, char *argv[])
 sets the full name of the compiler More...
 
void set_options (int argc, char *argv[])
 sets options from command-line parameters More...
 
voidalloc (int size)
 allocates dynamic memory More...
 
voidreallocate (void *mem, int size)
 
voidxalloc (int size)
 
void dealloc (void *ptr)
 deallocates dynamic memory More...
 

Variables

int option_turned_on [OPTIONS_COUNT]
 Values for options. More...
 
atom_t option_texts [4][OPTIONS_COUNT]
 Texts for short options [0]-short [1]-long [2]-alternative [3]-description. More...
 
char * option_source_filename
 ASCII name of current source file. More...
 
chars_t option_source_filename_chars
 name of current source file More...
 
char * option_compiler_filename
 ASCII name of current compiler. More...
 
chars_t option_compiler_filename_chars
 name of current compiler More...
 
int standalone
 indicate using of standalone compiler (i.e. -x option) More...
 
atom_t standalone_arguments
 list of command-line arguments More...
 
char * id_names [MAX_ID]
 array of names for each atom type More...
 
int stats_free
 statistics for free atoms (of any type) More...
 
int stats_allocs
 statistics for usage of ALLOC and DEALLOC More...
 
stats_t stats [MAX_ID]
 statistics for each type of atoms More...
 

Macro Definition Documentation

#define CHECK (   PARAM1,
  PARAM2,
  BODY 
)
Value:
if( strcmp(*argv,PARAM1)==0 || strcmp(*argv,PARAM2)==0 ) \
BODY;

Definition at line 80 of file options.c.

Function Documentation

void init_options ( int  argc,
char *  argv[] 
)

Definition at line 122 of file options.c.

140  {
141  option_turned_on [i] = 0;
142  int j;
143  for( j=0; j<4; j++ )
144  {
145  #ifdef SAFEMODE
146  assert( IS_NOT_EMPTY(t) ); // too few words in TR_OPTIONS
147  #endif
148  option_texts[j][i] = USE(CAR(t));
149  t=CDR(t);
150  }
151  }
152 
153  #ifdef SAFEMODE
154  assert( IS_EMPTY(t) ); // too many words in TR_OPTIONS
155  #endif
156 
157  DEUSE( names );
158  DEUSE( tokens );
159 }
160 
161 
void dump_statistics ( )

Dumps statistics of memory usage

Definition at line 173 of file options.c.

179  {
180  stats_used += stats[i].allocs-stats[i].deallocs;
181  if( stats[i].allocs )
182  {
183  PRINT(" %10s %8d %8d %8d %8d\n",
184  id_names[i],
185  stats[i].max,
186  stats[i].allocs,
187  stats[i].deallocs,
188  stats[i].allocs-stats[i].deallocs );
189  }
190  }
191  PRINT(" %39s\n","----------------");
192  PRINT(" %30s %8d\n","Used",stats_used);
193  PRINT(" %30s %8d\n","Free",stats_free);
194  PRINT(" %39s\n","----------------");
195  PRINT(" %30s %8d\n","OS alloc balance",stats_allocs);
196 }
197 #endif
198 
void finit_options ( )

Dumps statistics if -Zm option was switched on

Definition at line 207 of file options.c.

209  {
210  int j;
211  for( j = 0; j<4; j++ )
212  {
213  DEUSE(option_texts[j][i]);
214  }
215  }
217 
220 
222  {
224  }
225 
226 #ifdef ADVANCED
227  int stats_used=0;
229  {
230  dump_statistics();
231  }
232 
234  {
235  for( i=MIN_ID; i<MAX_ID; i++ )
236  stats_used += stats[i].allocs-stats[i].deallocs;
237  PRINT("{MEM#%d:%d}\n",stats_used,stats_allocs);
238  }
239 #endif //ADVANCED
240 }
241 
242 
void output_compiler_name ( int  show_help)
Parameters
show_helpif set also output commandline options

Outputs the name of the compiler, its language, its platform and build date. If show_help is not zero, output a list of command-line options

Definition at line 255 of file options.c.

264  {
265  dump( CAR(t) ); t = CDR(t); // Usage
266 
267  int i;
268  for( i=0; i<OPTIONS_COUNT; i++ )
269  {
270  int len;
271 
272  if( i==FIRST_ADVANCED_OPTION )
273  {
274  #ifndef ADVANCED
275  t = CDR(t);
276  #endif
277  dumpln( CAR(t) ); t = CDR(t); // options
278  }
279 
280  // show short version
281  len = LENGTH(option_texts[0][i]);
282  outter( TEXT(" "), -1 );
283  dump( option_texts[0][i] );
284  for( ; len<6; len++ ) outter( TEXT(" "), -1 );
285 
286  // show long version
287  len = LENGTH(option_texts[2][i]);
288  if( len )
289  {
290  dump( option_texts[2][i] );
291  }
292  else
293  {
294  len = LENGTH(option_texts[1][i]);
295  dump( option_texts[1][i] );
296  }
297  for( ; len<30; len++ ) outter( TEXT(" "), -1 );
298 
299  // show description
300  dumpln( option_texts[3][i] );
301  }
302  }
303 
304  DEUSE( texts );
305  DEUSE( help_texts );
306 }
307 
308 
void set_compiler_filename ( int  argc,
char *  argv[] 
)
Parameters
argcargc parameter from the main function
argvargv parameter from the main function

If the compiler is executed through a search in the PATH, then try to find the full name

Definition at line 321 of file options.c.

350  {
351  //printf( "dir='%s'\n", path );
352  char filename[strlen(path)+1+strlen(option_compiler_filename)+10];
353  strcpy( filename, path );
354  strcpy( filename+strlen(path), PATH_SLASH );
355  strcpy( filename+strlen(path)+1, option_compiler_filename );
356  strcpy( filename+strlen(path)+1+strlen(option_compiler_filename), "\0" );
357 
358  err = stat( filename, &buffer );
359  //printf( "full name(err=%d)='%s'\n", err, filename );
360  if( !err )
361  {
362  // We found where the file is loaded from, remember it
364  option_compiler_filename = (char*)ALLOC( strlen(filename)+1 );
365  strcpy( option_compiler_filename, filename );
366  goto free_and_ready;
367  }
368 
369  path = strtok( NULL, PATH_DELIM );
370  }
371 
372 free_and_ready:
373  free( envpath );
374 
375 #ifdef WINDOWS
376 ready:
377 #endif
379 }
380 
381 //===================================================
void set_options ( int  argc,
char *  argv[] 
)
Parameters
argcargc parameter from the main function
argvargv parameter from the main function

Reads options from the argv array and sets appropriate parameters.

Definition at line 392 of file options.c.

400  {
401  //atom_t error = new_os_error( option_compiler_filename_chars );
402  return;
403  }
404 
405  standalone = (*(int*)(code+ptr-4)==MAGIC_NUMBER);
406  DEALLOC( code );
407 
408  // get options only if not in standalone mode,
409  // otherwise collect them in a list
410  if( standalone )
411  { // standalone mode -- collect arguments in a list
412  for( argc--,argv++; argc--; argv++ )
413  {
414  atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
415  append( word, &standalone_arguments, &last );
416  }
417  }
418  else
419  { // compiler mode -- process options till file name and then collect arguments
420  for( argc--,argv++; argc--; argv++ )
421  {
423  {
424  atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
425  append ( word, &standalone_arguments, &last );
426  }
427  else
428  {
429  int found = 0;
430  chars_t option = UNFILENAME(*argv);
431  int len = STRLEN(option);
432  int i;
433  for( i=0; i<OPTIONS_SUPPORTED_COUNT; i++ )
434  {
435  int j;
436  for( j=0; j<4; j++ )
437  {
438  if( len==LENGTH(option_texts[j][i]) )
439  {
440  if( same_strings(1,option,STRING(option_texts[j][i]),len) )
441  {
442  found = 1;
443  option_turned_on[i] = 1;
444  break;
445  }
446  }
447  }
448  }
449  DEALLOC( option );
450 
451  if( OPTION_HELP )
452  {
454  exit(0);
455  }
456 
457  if( found ) continue;
458 
459  // check for invalid option or second file name
460  if( **argv=='-' )
461  {
462  chars_t name = UNFILENAME(*argv); // has ALLOC inside
463  atom_t word = new_word( name, UNKNOWN );
464  atom_t err = new_error( ERROR_UNKNOWN_OPTION, word );
465  dumpln( err );
466  DEUSE( word );
467  DEALLOC( name );
468  continue;
469  }
470 
471  // set file name
472  option_source_filename = *argv;
474  }
475  } // for
476  }
477 }
478 
479 
void * alloc ( int  size)
Parameters
sizesize of memory (in bytes) to allocate
Returns
pointer to allocated memory

This function is used to allocate memory. Depending on the existence of DEBUG_HEAP symbol it either just allocates memory or allocates and dump on the screen allocation data.

Definition at line 556 of file options.c.

557  { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
558  #ifdef ADVANCED
559  stats_allocs++;
560  #endif
561  return buffer;
562 }
563 
564 void* reallocate(void * mem, int size)
void* reallocate ( void mem,
int  size 
)

Definition at line 566 of file options.c.

570  { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
571  return buffer;
572 }
573 
574 void* xalloc( int size )
void* xalloc ( int  size)

Definition at line 576 of file options.c.

583  { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
584 
585  #ifdef ADVANCED
586  stats_allocs++;
587  #endif
588  return buffer;
589 }
590 void dealloc( void* ptr )
591 {
void dealloc ( void ptr)
Parameters
ptrpointer to the memory to deallocate

This function is used to deallocate memory. Depending on the existence of DEBUG_HEAP symbol it either just deallocates memory or deallocates and dumps on the screen deallocation data.

Definition at line 592 of file options.c.

Variable Documentation

int option_turned_on[OPTIONS_COUNT]

Definition at line 86 of file options.c.

atom_t option_texts[4][OPTIONS_COUNT]

Definition at line 87 of file options.c.

char* option_source_filename

Definition at line 89 of file options.c.

chars_t option_source_filename_chars

Definition at line 90 of file options.c.

char* option_compiler_filename

Definition at line 91 of file options.c.

chars_t option_compiler_filename_chars

Definition at line 92 of file options.c.

int standalone

Definition at line 94 of file options.c.

atom_t standalone_arguments

Definition at line 95 of file options.c.

char* id_names[MAX_ID]
Initial value:
= {
"Integers",
"Floats",
"Lists",
"Words",
"Subwords",
"Errors",
"Var",
"Memory" }

Definition at line 98 of file options.c.

int stats_free

Definition at line 108 of file options.c.

int stats_allocs

Definition at line 109 of file options.c.

stats_t stats[MAX_ID]

Definition at line 110 of file options.c.


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013