Example #1
0
void newgame(struct gameinfo *gi, unsigned int r_size) {
	unsigned int i, l;
	struct gamestate *prevstate;
	while (gi->gs != 0) {//free previous states' memory
		prevstate = gi->gs->prev;
		for (i = 0; i<gi->r_size; i++) free(gi->gs->board[i]);
		free(gi->gs->board);
		free(gi->gs);
		gi->gs = prevstate;
	}
	gi->gamestarted = 1;
	gi->r_size = r_size;
	//initialize gamestate struct
	gi->gs = (struct gamestate*)malloc_verify(sizeof(struct gamestate));
	gi->gs->whitecount = 2;
	gi->gs->blackcount = 2;
	gi->gs->nowplaying = 'B';
	strcpy(gi->gs->lastmove, "-"); //no last move
	gi->gs->prev = 0;
	gi->gs->board = (char**)malloc_verify(r_size*sizeof(char*)); //allocate board
	for (i = 0; i<r_size; i++) {
		gi->gs->board[i] = (char*)malloc_verify(r_size*sizeof(char));
		for (l = 0; l<r_size; l++) gi->gs->board[i][l] = ' '; //initialize board
	}
	gi->gs->board[r_size/2-1][r_size/2-1] = 'W';
	gi->gs->board[r_size/2-1][r_size/2] = 'B';
	gi->gs->board[r_size/2][r_size/2-1] = 'B';
	gi->gs->board[r_size/2][r_size/2] = 'W';
	build_eval_board(gi); //build board with values for each position
}
/******************************************************************************
* This file is Copyright 1992 by Philip G. Richards.  All Rights Reserved.
* See the file README that came with this distribution for permissions on
* code usage, copying, and distribution.  It comes with absolutely no warranty.
******************************************************************************/

#include "client.h"
#include "main.h"
#include "parse.h"
#include <ctype.h>

extern char **environ;

#define ISMAGIC 0x80
#define ISPROT  0x40

void
#ifndef ANSI_PROTOTYPES
freemyargs(argc, argv)
    int argc;
    char *argv[];
#else /* ANSI_PROTOTYPES */
freemyargs(int argc, char **argv)
#endif /* ANSI_PROTOTYPES */
{
    int i;

    if (argv == (char**)0)
	return;

#ifdef MALLOCDEBUG
  {
    int oldlvl = malloc_debug(0);
#endif

    for (i = 0; i < argc && argv[i]; i++)
	(void)free(argv[i]);

#ifdef MALLOCDEBUG
    (void)malloc_debug(oldlvl);
    if (!malloc_verify())
    {
        ffprintf(STDERR, "??freemyargs() has screwed up malloc()\n");
	abort();
    }
  }
#endif
}
Example #3
0
void *operator new(size_t l)
{
//    ASSERT(l != 0);

    if (l == 0)
    {
	printf ("void *operator new(size_t): Allocating 0 bytes.\n"
		"                            Allocate 1 byte instead.\n");
	l = 1;
    }

#if defined(MALLOC_DEBUG) && !defined(__PURIFY__)
#ifdef sun4
    malloc_verify();
#endif
#endif

    void *r = (void *)MALLOC(l);

    if (!r) {
	printf("Can't allocate %ld bytes...aborting!\n",l);
	fflush(stdout);
	abort();
    }

    return r;
}
Example #4
0
main(int argc, char **argv)
{
  DCM_OBJECT
	* object;
    CONDITION
	cond;
		char *
	   fileInput;
    CTNBOOLEAN
	verbose = FALSE,
	exitFlag = FALSE,
	formatFlag = FALSE;
    unsigned long
        options = DCM_ORDERLITTLEENDIAN;
    long vmLimit = 0;
    LST_HEAD* fileNames = 0;
    UTL_FILEITEM* p = NULL;

		if (argc < 2)
			usageerror();
		else {
			argv++;
			fileInput = *argv;
		}

    THR_Init();
    DCM_Debug(verbose);

    cond = DCM_OpenFile(fileInput, options, &object);
    if (cond != DCM_NORMAL && ((options & DCM_PART10FILE) == 0)) {
	    COND_DumpConditions();
	    (void) DCM_CloseObject(&object);
	    (void) COND_PopCondition(TRUE);
	    fprintf(stderr, "Could not open %s as expected.  Trying Part 10 format.\n", p->path);
	    cond = DCM_OpenFile(p->path, options | DCM_PART10FILE, &object);
    }
    if (cond == DCM_NORMAL) {
			printf("<?xml version=\"1.0\" ?>\n");
			printf("<Structured_Report>\n");
	    iterateThroughElements(&object, 1);
			printf("</Structured_Report>\n");
    }
    COND_DumpConditions();
    (void) DCM_CloseObject(&object);
    (void) COND_PopCondition(TRUE);

    if (cond != DCM_NORMAL && exitFlag) {
	    THR_Shutdown();
	    exit(1);
    }

#ifdef MALLOC_DEBUG
    malloc_verify(0);
    malloc_shutdown();
#endif
    THR_Shutdown();
    return 0;
}
Example #5
0
int play(struct gameinfo *gi, char *cell) {
	unsigned int x, y, i, l;
	char color = gi->gs->nowplaying;
	struct gamestate *newstate;
	x = cell[0]-'a'; //get x from letter (eg 'c'->3)
	if (isnum(cell+1)) {
		y = atoi(cell+1)-1; //get y
		if (x >= 0 && x<gi->r_size && y >= 0 && y<gi->r_size && gi->gs->board[x][y] == '*') {//if a legal move
			newstate = (struct gamestate*)malloc_verify(sizeof(struct gamestate)); //make a new state (for undo)
			newstate->board = (char**)malloc_verify(gi->r_size*sizeof(char*));
			for (i = 0; i<gi->r_size; i++) {
				newstate->board[i] = (char*)malloc_verify(gi->r_size*sizeof(char));
				memcpy(newstate->board[i], gi->gs->board[i], gi->r_size*sizeof(char)); //copy board to new state
				for (l = 0; l<gi->r_size; l++)
					if (newstate->board[i][l] == '*') newstate->board[i][l] = ' '; //legal moves aren't legal anymore
			}
			newstate->board[x][y] = color; //add current move
			newstate->blackcount = gi->gs->blackcount;
			newstate->whitecount = gi->gs->whitecount;
			if (color == 'B') newstate->blackcount++; //copy counts and increase the appropriate count
			else newstate->whitecount++;
			strcpy(newstate->lastmove, cell); //save last move (for showstate)
			newstate->prev = gi->gs; //current state is now last state
			gi->gs = newstate; //updated state is now current state
			updatecolors(gi, x, y); //change opposing pieces between player's old pieces and new piece
			if (gi->gs->prev->nowplaying == 'B') gi->gs->nowplaying = 'W'; //change current player
			else gi->gs->nowplaying = 'B';
			if (findlegal(gi) == 0) {//if there are no legal moves
				//printf("Player %s (%s) has no available moves!\n\n", gi->gs->nowplaying == 'W'?"White":"Black", gi->gs->nowplaying == gi->playercolor?"human":"computer");
				if (gi->gs->nowplaying == 'B') gi->gs->nowplaying = 'W'; //change player
				else gi->gs->nowplaying = 'B';
				if (findlegal(gi) == 0)//if the other player doesn't have legal moves either
					gi->gamestarted = 2; //gamestarted = 2  =  neither player has moves
			}
			return 1;
		}
		else printf("Invalid move\n");
	}
	else printf("Invalid move\n");
	return 0;
}
Example #6
0
char *
strealloc(char *p, char *s)
{
  int len = strlen(s) + 1;

  p = (char *) xrealloc((voidp) p, len);

  strcpy(p, s);
#ifdef DEBUG_MEM
  malloc_verify();
#endif /* DEBUG_MEM */
  return p;
}
Example #7
0
File: autil.c Project: 0mp/freebsd
/*
 * Copy s into p, reallocating p if necessary
 */
char *
strealloc(char *p, char *s)
{
  size_t len = strlen(s) + 1;

  p = (char *) xrealloc((voidp) p, len);

  xstrlcpy(p, s, len);
#ifdef DEBUG_MEM
# if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_VERIFY)
  malloc_verify();
# endif /* not defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_VERIFY) */
#endif /* DEBUG_MEM */
  return p;
}
Example #8
0
void operator delete(void *p)
{
//    ASSERT(p != NULL);

#if defined(MALLOC_DEBUG) && !defined(__PURIFY__)
#ifdef sun4
    malloc_verify();
#endif
#endif

    if (p == NULL)
    {
#if defined(MALLOC_DEBUG)
	printf("void operator delete(void*): Freeing NULL pointer\n");
#endif
    }
    else
	FREE(p);
}
Example #9
0
static void
checkup_mem(void)
{
  struct mallinfo mi = mallinfo();
  u_long uordbytes = mi.uordblks * 4096;

  if (mem_bytes != uordbytes) {
    if (orig_mem_bytes == 0)
      mem_bytes = orig_mem_bytes = uordbytes;
    else {
      fprintf(logfp, "%s[%ld]: ", am_get_progname(), (long) am_mypid);
      if (mem_bytes < uordbytes) {
	fprintf(logfp, "ALLOC: %ld bytes", uordbytes - mem_bytes);
      } else {
	fprintf(logfp, "FREE: %ld bytes", mem_bytes - uordbytes);
      }
      mem_bytes = uordbytes;
      fprintf(logfp, ", making %d missing\n", mem_bytes - orig_mem_bytes);
    }
  }
  malloc_verify();
}
Example #10
0
int main( int argc, char ** argv ) {
    int c;
    int rc;
    char * cp;
    int no_warnings = 1;
    int resolve = 1;
    int result;

    bool buffer_messages = false;
    Express model;

    EXPRESSprogram_name = argv[0];
    ERRORusage_function = 0;

    EXPRESSinit_init();

    EXPRESSinitialize();

    if( EXPRESSinit_args ) {
        ( *EXPRESSinit_args )( argc, argv );
    }

    sc_optind = 1;
    while( ( c = sc_getopt( argc, argv, EXPRESSgetopt_options ) ) != -1 ) {
        switch( c ) {
            case 'd':
                ERRORdebugging = 1;
                switch( atoi( sc_optarg ) ) {
                    case 0:
                        fprintf( stderr, "\ndebug codes:\n" );
                        fprintf( stderr, "  0 - this help\n" );
                        fprintf( stderr, "  1 - basic debugging\n" );
#ifdef debugging
                        fprintf( stderr, "  4 - light malloc debugging\n" );
                        fprintf( stderr, "  5 - heavy malloc debugging\n" );
                        fprintf( stderr, "  6 - heavy malloc debugging while resolving\n" );
#endif /* debugging*/
#ifdef YYDEBUG
                        fprintf( stderr, "  8 - set YYDEBUG\n" );
#endif /*YYDEBUG*/
                        break;
                    case 1:
                        debug = 1;
                        break;
#ifdef debugging
                    case 4:
                        malloc_debug( 1 );
                        break;
                    case 5:
                        malloc_debug( 2 );
                        break;
                    case 6:
                        malloc_debug_resolve = 1;
                        break;
#endif /*debugging*/
#ifdef YYDEBUG
                    case 8:
                        exp_yydebug = 1;
                        break;
#endif /* YYDEBUG */
                }
                break;
            case 'B':
                buffer_messages = true;
                break;
            case 'b':
                buffer_messages = false;
                break;
            case 'e':
                input_filename = sc_optarg;
                break;
            case 'r':
                resolve = 0;
                break;
            case 'i':
            case 'w':
                no_warnings = 0;
                ERRORset_warning( sc_optarg, c == 'w' );
                break;
            case 'p':
                for( cp = sc_optarg; *cp; cp++ ) {
                    if( *cp == '#' ) {
                        print_objects_while_running |= OBJ_PASS_BITS;
                    } else if( *cp == 'E' ) {
                        print_objects_while_running = OBJ_ANYTHING_BITS;
                    } else {
                        print_objects_while_running |= OBJget_bits( *cp );
                    }
                }
                break;
            case 'v':
                print_fedex_version();
                no_need_to_work = 1;
                break;
            default:
                rc = 1;
                if( EXPRESSgetopt ) {
                    rc = ( *EXPRESSgetopt )( c, sc_optarg );
                }
                if( rc == 1 ) {
                    if( ERRORusage_function ) {
                        ( *ERRORusage_function )();
                    } else {
                        EXPRESSusage(1);
                    }
                }
                break;
        }
    }
    if( !input_filename ) {
        input_filename = argv[sc_optind];
        if( !input_filename ) {
            EXPRESScleanup();
            if( no_need_to_work ) {
                return( 0 );
            } else {
                ( *ERRORusage_function )();
            }
        }
    }

    if( no_warnings ) {
        ERRORset_all_warnings( 1 );
    }
    ERRORbuffer_messages( buffer_messages );

    if( EXPRESSinit_parse ) {
        ( *EXPRESSinit_parse )();
    }

    model = EXPRESScreate();
    EXPRESSparse( model, ( FILE * )0, input_filename );
    if( ERRORoccurred ) {
        result = EXPRESS_fail( model );
        EXPRESScleanup();
        EXPRESSdestroy( model );
        return result;
    }

#ifdef debugging
    if( malloc_debug_resolve ) {
        malloc_verify();
        malloc_debug( 2 );
    }
#endif /*debugging*/

    if( resolve ) {
        EXPRESSresolve( model );
        if( ERRORoccurred ) {
            result = EXPRESS_fail( model );
            EXPRESScleanup();
            EXPRESSdestroy( model );
            return result;
        }
    }

    if( EXPRESSbackend ) {
        ( *EXPRESSbackend )( model );
    }

    if( ERRORoccurred ) {
        result = EXPRESS_fail( model );
        EXPRESScleanup();
        EXPRESSdestroy( model );
        return result;
    }

    result = EXPRESS_succeed( model );
    EXPRESScleanup();
    EXPRESSdestroy( model );
    return result;
}
Example #11
0
struct netconfig *
getnetconfig(void *handlep)
{
    struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
    char *stringp;		/* tmp string pointer */
    struct netconfig_list	*list;
    struct netconfig *np;
    struct netconfig *result;

    /*
     * Verify that handle is valid
     */
    mutex_lock(&nc_file_lock);
    if (ncp == NULL || nc_file == NULL) {
	nc_error = NC_NOTINIT;
	mutex_unlock(&nc_file_lock);
	return (NULL);
    }
    mutex_unlock(&nc_file_lock);

    switch (ncp->valid) {
    case NC_VALID:
	/*
	 * If entry has already been read into the list,
	 * we return the entry in the linked list.
	 * If this is the first time call, check if there are any entries in
	 * linked list.  If no entries, we need to read the netconfig db.
	 * If we have been here and the next entry is there, we just return
	 * it.
	 */
	if (ncp->flag == 0) {	/* first time */
	    ncp->flag = 1;
	    mutex_lock(&ni_lock);
	    ncp->nc_configs = ni.head;
	    mutex_unlock(&ni_lock);
	    if (ncp->nc_configs != NULL)	/* entry already exist */
		return(ncp->nc_configs->ncp);
	}
	else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
	    ncp->nc_configs = ncp->nc_configs->next;
	    return(ncp->nc_configs->ncp);
	}

	/*
	 * If we cannot find the entry in the list and is end of file,
	 * we give up.
	 */
	mutex_lock(&ni_lock);
	if (ni.eof == 1) {
		mutex_unlock(&ni_lock);
		return(NULL);
	}
	mutex_unlock(&ni_lock);

	break;
    default:
	nc_error = NC_NOTINIT;
	return (NULL);
    }

    stringp = (char *) malloc(MAXNETCONFIGLINE);
    if (stringp == NULL)
    	return (NULL);

#ifdef MEM_CHK
    if (malloc_verify() == 0) {
	fprintf(stderr, "memory heap corrupted in getnetconfig\n");
	exit(1);
    }
#endif

    /*
     * Read a line from netconfig file.
     */
    mutex_lock(&nc_file_lock);
    do {
	if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
	    free(stringp);
	    mutex_lock(&ni_lock);
	    ni.eof = 1;
	    mutex_unlock(&ni_lock);
	    mutex_unlock(&nc_file_lock);
	    return (NULL);
        }
    } while (*stringp == '#');
    mutex_unlock(&nc_file_lock);

    list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
    if (list == NULL) {
    	free(stringp);
    	return(NULL);
    }
    np = (struct netconfig *) malloc(sizeof (struct netconfig));
    if (np == NULL) {
    	free(stringp);
	free(list);
    	return(NULL);
    }
    list->ncp = np;
    list->next = NULL;
    list->ncp->nc_lookups = NULL;
    list->linep = stringp;
    if (parse_ncp(stringp, list->ncp) == -1) {
	free(stringp);
	free(np);
	free(list);
	return (NULL);
    }
    else {
	/*
	 * If this is the first entry that's been read, it is the head of
	 * the list.  If not, put the entry at the end of the list.
	 * Reposition the current pointer of the handle to the last entry
	 * in the list.
	 */
	mutex_lock(&ni_lock);
	if (ni.head == NULL) {	/* first entry */
	    ni.head = ni.tail = list;
	}
    	else {
    	    ni.tail->next = list;
    	    ni.tail = ni.tail->next;
    	}
	ncp->nc_configs = ni.tail;
	result = ni.tail->ncp;
	mutex_unlock(&ni_lock);
	return(result);
    }
}
Example #12
0
/************************************************************************
 *                                                                       *
 *  get_name                                                              *
 *									*/
double
Win_movie::get_name(char *s1, char *s2, int where)
{
    Movie_frame *insert_point, *list_point, *newframe ;
    int zip ;
    double the_vs = 0.0;

#ifdef DMALLOC
    cerr << "v1.1 ";
    malloc_verify();
#endif

    if ((strcmp(s1,"delete")==0) && (strcmp(s2,"delete")==0)){
	if ((where<number_of_frames)&&(number_of_frames>0)){
	    insert_point = movie_head ;
	    for (zip = 0 ; zip < where ; zip++ ){
		insert_point = insert_point->nextframe ;
	    }
	    insert_point->prevframe->nextframe = insert_point->nextframe ; 
	    insert_point->nextframe->prevframe = insert_point->prevframe ; 
	    if (where==0){
		movie_head = insert_point->nextframe ;
	    }
	    delete insert_point ;
	    if (number_of_frames==1){       /* if there was only one, */
		movie_head = NULL ;          /* there are none now. */
	    }
	}
    }else{
	char *filename = new char[strlen(s1) + strlen(s2) + 2];
	strcpy(filename, s1);
	strcat(filename, "/");
	strcat(filename, s2);
    
	if (movie_head == NULL){
	    movie_head = new Movie_frame(filename);
	    delete [] filename;
	    if ((the_vs = get_stuff (movie_head)) == -1.0){
		delete movie_head ;
		movie_head = NULL;
		return (-1.0);
	    }else{
		movie_head->nextframe = movie_head ;
		movie_head->prevframe = movie_head ;
	    }
	}else{
	    insert_point = movie_head ;
	    for (zip = 0 ; zip < where ; zip++ ){
		insert_point = insert_point->nextframe ;
	    }
	    newframe = new Movie_frame(filename);
	    delete [] filename;
	    if ((the_vs = get_stuff (newframe)) == -1.0){  
		delete newframe ;
		newframe = NULL; 
		return (-1.0);
	    }else{ 
		newframe->nextframe = insert_point ;
		newframe->prevframe = insert_point->prevframe;
		insert_point->prevframe->nextframe = newframe ;
		insert_point->prevframe = newframe ;
	    }
	    
	    if (where==0){
		movie_head = newframe;
	    }
	}
    }

#ifdef DMALLOC
    cerr << "v1.2 ";
    malloc_verify();
#endif

    number_of_frames = 0 ;
    list_point = movie_head ;
    if (movie_head != NULL){
	do{
	    list_point = list_point->nextframe ;
	    number_of_frames++ ;
	} while (list_point != movie_head);
    }
    
    frame_number = high_frame = number_of_frames - 1 ;

    // If we have more than 1 frame, show frame slider and start/stop buttons

    int slider_show = number_of_frames > 1 ? TRUE : FALSE;
    xv_set(frame_slider,
	   PANEL_MAX_VALUE, number_of_frames,
	   PANEL_VALUE, number_of_frames,
	   XV_SHOW, slider_show,
	   NULL);
    xv_set(start_stop_item, XV_SHOW, slider_show, NULL);

    if (number_of_frames){
	xv_set(high_frame_item,
	       PANEL_VALUE, number_of_frames,
	       PANEL_MAX_VALUE, number_of_frames,
	       NULL);
	xv_set(low_frame_item, PANEL_MAX_VALUE, number_of_frames, NULL);
	int t = (int)xv_get(low_frame_item, PANEL_VALUE);
	if (t <= 0){
	    xv_set(low_frame_item, PANEL_VALUE, 1, NULL);
	}else if (t > number_of_frames){
	    xv_set(low_frame_item, PANEL_VALUE, number_of_frames, NULL);
	}
    }else{
	xv_set(high_frame_item, PANEL_VALUE, 1, NULL);
	xv_set(low_frame_item, PANEL_VALUE, 1, NULL);
    }

#ifdef DMALLOC
    cerr << "v1.3 ";
    malloc_verify();
#endif
    
    return the_vs;
}
Example #13
0
int main( int argc, char ** argv ) {
    int c;
    int rc;
    char * cp;
    int no_warnings = 1;
    int resolve = 1;

    bool buffer_messages = false;
    char * filename = 0;
    Express model;

    EXPRESSprogram_name = argv[0];
    ERRORusage_function = usage;
#ifdef YYDEBUG
    yydebug = 0;
#endif
    EXPRESSinit_init();

    EXPRESSinitialize();

    if( EXPRESSinit_args ) {
        ( *EXPRESSinit_args )( argc, argv );
    }

    optind = 1;
    while( ( c = getopt( argc, argv, EXPRESSgetopt_options ) ) != -1 )
        switch( c ) {
            case 'd':
                ERRORdebugging = 1;
                switch( atoi( optarg ) ) {
                    case 0:
                        fprintf( stderr, "\ndebug codes:\n" );
                        fprintf( stderr, "  0 - this help\n" );
                        fprintf( stderr, "  1 - basic debugging\n" );
#ifdef debugging
                        fprintf( stderr, "  4 - light malloc debugging\n" );
                        fprintf( stderr, "  5 - heavy malloc debugging\n" );
                        fprintf( stderr, "  6 - heavy malloc debugging while resolving\n" );
#endif /* debugging*/
#ifdef YYDEBUG
                        fprintf( stderr, "  8 - set yydebug\n" );
                        fprintf( stderr, "  9 - set yydebug selectively, must use -u and/or -l. Also increases verbosity for some errors\n" );
                        fprintf( stderr, "-u nnn: upper line limit\n" );
                        fprintf( stderr, "-l nnn: lower line limit\n" );
#endif /*YYDEBUG*/
                        break;
                    case 1:
                        debug = 1;
                        break;
#ifdef debugging
                    case 4:
                        malloc_debug( 1 );
                        break;
                    case 5:
                        malloc_debug( 2 );
                        break;
                    case 6:
                        malloc_debug_resolve = 1;
                        break;
#endif /*debugging*/
#ifdef YYDEBUG
                    case 8:
                        yydebug = 1;
                        break;
                    case 9:
                        yydbg_verbose = true;
                        //yydebug gets set in expscan.l when in the line range set by -l and -u
                        break;
#endif /* YYDEBUG */
                }
                break;
#ifdef YYDEBUG
            case 'u':
                yydbg_upper_limit = atoi( optarg );
                break;
            case 'l':
                yydbg_lower_limit = atoi( optarg );
                break;
#endif /* YYDEBUG */
            case 'B':
                buffer_messages = true;
                break;
            case 'b':
                buffer_messages = false;
                break;
            case 'e':
                filename = optarg;
                break;
            case 'n':
                skip_exp_pause = true;
                break;
            case 'r':
                resolve = 0;
                break;
            case 'i':
            case 'w':
                no_warnings = 0;
                ERRORset_warning( optarg, c == 'w' );
                break;
            case 'p':
                for( cp = optarg; *cp; cp++ ) {
                    if( *cp == '#' ) {
                        print_objects_while_running |= OBJ_PASS_BITS;
                    } else if( *cp == 'E' ) {
                        print_objects_while_running = OBJ_ANYTHING_BITS;
                    } else {
                        print_objects_while_running |= OBJget_bits( *cp );
                    }
                }
                break;
            case 'v':
                print_fedex_version();
                break;
            case 'z': /* to allow user to attach debugger and continue */
                printf( "pid = %d\n", getpid() );
#ifndef __WIN32__
                pause();
#else     //windows
                getchar();
                abort();
#endif
                break;
            default:
                rc = 1;
                if( EXPRESSgetopt ) {
                    rc = ( *EXPRESSgetopt )( c, optarg );
                }
                if( rc == 1 ) {
                    ( *ERRORusage_function )();
                }
                break;
        }

    if( !filename ) {
        filename = argv[optind];
        if( !filename ) {
            if( no_need_to_work ) {
                return( 0 );
            } else {
                ( *ERRORusage_function )();
            }
        }
    }

    if( no_warnings ) {
        ERRORset_all_warnings( 1 );
    }
    ERRORbuffer_messages( buffer_messages );

    if( EXPRESSinit_parse ) {
        ( *EXPRESSinit_parse )();
    }

    model = EXPRESScreate();
    EXPRESSparse( model, ( FILE * )0, filename );
    if( ERRORoccurred ) {
        return( EXPRESS_fail( model ) );
    }

#ifdef debugging
    if( malloc_debug_resolve ) {
        malloc_verify();
        malloc_debug( 2 );
    }
#endif /*debugging*/

    if( resolve ) {
        EXPRESSresolve( model );
        if( ERRORoccurred ) {
            return( EXPRESS_fail( model ) );
        }
    }

    if( EXPRESSbackend ) {
        ( *EXPRESSbackend )( model );
    }

    if( ERRORoccurred ) {
        return( EXPRESS_fail( model ) );
    }

    return( EXPRESS_succeed( model ) );
}
Example #14
0
int
main(int argc, char **argv)
{
    CONDITION
    cond = APP_NORMAL;		/* condition code returned by various
				 * facilities */
    DUL_NETWORKKEY
	* network;		/* The handle to the network */
    DUL_ASSOCIATIONKEY
	* association = NULL;	/* The handle to the Association */
    DUL_ASSOCIATESERVICEPARAMETERS
	service;		/* Presentation parameters */
    int
        pid,			/* process id */
        port;			/* port number on which the server listens */
    char
        node[MAXHOSTNAMELEN + 1] = "";	/* name of node */
    CTNBOOLEAN
	singleUserMode = FALSE;	/* indicates if the server will run in single
				 * user mode i.e. iterative */
    LST_HEAD
	* processList = NULL;	/* maintains a list of children processes */
    CTNBOOLEAN paramsFlag = FALSE;	/* Dump association parameters? */

    (void) gethostname(node, MAXHOSTNAMELEN);
    /* Parse the command line arguments. First process all the switches */
    while (--argc > 0 && *(++argv)[0] == '-') {
	switch ((*argv)[1]) {
	case 'd':		/* option to put a specific facility in debug
				 * mode */
	    argc--;
	    argv++;
	    if (!argc)
		usageError();
	    if (strcmp(*argv, "DCM") == 0)
		verboseDCM = TRUE;
	    else if (strcmp(*argv, "DUL") == 0)
		verboseDUL = TRUE;
	    else if (strcmp(*argv, "SRV") == 0)
		verboseSRV = TRUE;
	    else
		usageError();
	    break;
	case 'f':		/* database selection option */
	    argc--;
	    argv++;
	    if (!argc)
		usageError();
	    controlDatabase = *argv;
	    break;
	case 'i':		/* set the forgive option */
	    forgiveFlag = TRUE;
	    break;
	case 'n':		/* use node as name rather than hostname */
	    if (argc < 1)
		usageError();
	    argc--;
	    argv++;
	    strcpy(node, *argv);
	    break;
	case 'p':
	    paramsFlag = TRUE;	/* Dump the association parameters to stdout */
	    break;
	case 'r':
	    sendBack = TRUE;	/* send optional attributes back in the
				 * response messages */
	    break;
	case 's':		/* set single user mode. Useful for debugging */
	    singleUserMode = TRUE;
	    break;
	case 't':
	    traceFlag = TRUE;	/* tracing ON (non silent operation) */
	    break;
	case 'v':		/* set verbose mode ON */
	    verboseDUL = TRUE;
	    verboseSRV = TRUE;
	    verboseDCM = TRUE;
	    break;
	case 'x':		/* for maintaining a Generalized Queue. This
				 * option is useful for terminals having X
				 * capability */
	    gqueueFlag = TRUE;
#ifdef ASG
	    argc--;
	    argv++;
	    if (sscanf(*argv, "%d", &gqID) != 1)
		usageError();
#endif
	    break;
	default:
	    printf("Unrecognized option: %s\n", *argv);
	    break;
	}
    }

    if (argc < 1)
	usageError();

    if (sscanf(*argv++, "%d", &port) != 1)
	usageError();

#ifdef _MSC_VER
    singleUserMode = TRUE;
#endif

    THR_Init();
    DCM_Debug(verboseDCM);
    DUL_Debug(verboseDUL);
    SRV_Debug(verboseSRV);

    /* Handle interrupts */
    (void) signal(SIGINT, signalHandler);

    /* Initialize a network connection and listen on the specified port */
    cond = DUL_InitializeNetwork(DUL_NETWORK_TCP, DUL_AEBOTH,
		 (void *) &port, DUL_TIMEOUT, DUL_ORDERBIGENDIAN, &network);
    if (cond != DUL_NORMAL) {
	exitApplication(cond);
    }
    appHandles.network = network;	/* set the network key field */
    if (!singleUserMode) {
	/*
	 * initialize a list that will hold information about all the
	 * children processes that are active
	 */
	if ((processList = LST_Create()) == NULL) {
	    cond = COND_PushCondition(APP_ERROR(APP_FAILURE), "LST_Create",
				      "main");
	    exitApplication(cond);
	}
	appHandles.processList = processList;	/* set the process list field */
    }
    /* The server loops forever accepting new requests */
    while (1) {
	/* get the next Association request */
	cond = nextAssociationRequest(node, &network, &service, maxPDU,
				      forgiveFlag, &gqID, &association);
	if (cond == APP_NORMAL) {
	    appHandles.association = association;
	    appHandles.service = &service;
	    if (!singleUserMode) {
		/*
		 * remove all those child processes that have exited so that
		 * zombie processes are not created
		 */
		(void) harvestChildrenProcesses(&processList);
		/*
		 * server continues to accept new requests, whereas the child
		 * services the requests on that association. To create a
		 * child process, we fork
		 */
		pid = fork();
	    } else
		pid = 0;	/* setting pid to 0 in this else clause is
				 * essential due to the manner in which the
				 * following if condition is coded */

	    if (pid < 0) {
		/* fork failed */
		printf("Cannot spawn child process. Request rejected\n");
		clearAssociationKeyAndServiceParameters(APP_FAILURE);
		continue;
	    } else if (pid == 0) {	/* This is the child process unless
					 * the single user mode is ON in
					 * which case this is the parent
					 * process itself. For this part of
					 * the code to be activated when the
					 * singleUserMode is TRUE, we need to
					 * set pid = 0 as done above */
		if (!singleUserMode)
		    printf("Forked child\n");
		cond = DUL_AcknowledgeAssociationRQ(&association, &service);
		if (cond != DUL_NORMAL) {
		    if (!singleUserMode) {
			/* child process exits with a status of -1 */
			exitApplication(cond);
		    } else {
			/*
			 * In the single user mode, the parent just discards
			 * the current association and service parameters and
			 * continues to listen to new requests
			 */
			clearAssociationKeyAndServiceParameters(cond);
			continue;	/* go to next iteration of main loop */
		    }
		}
		/*
		 * open a GQ queue, if the GQ facility is to be used
		 */
		if (gqueueFlag) {
		    if (gqID == -1) {	/* we were unsuccessful retrieving
					 * the GQ ID info from the database.
					 * Hence we decide to ignore the
					 * display */
			fprintf(stderr,
				"Failure to retrieve GQ ID information\n");
			fprintf(stderr,
				"GQ facility ignored\n");
			gqueueFlag = FALSE;
		    } else {
			/* open a GQ */
			cond = openGQ(gqID);
			if (cond != APP_NORMAL) {
			    if (!singleUserMode) {
				/* child process exits with a status of -1 */
				exitApplication(cond);
			    } else {
				clearAssociationKeyAndServiceParameters(cond);
				continue;	/* go to next iteration of
						 * main loop */
			    }
			}
		    }
		}
		if (paramsFlag)
		    DUL_DumpParams(&service);
		cond = serviceRequests(&network, &association, &service);
		if (cond == SRV_PEERREQUESTEDRELEASE)
		    cond = SRV_NORMAL;
		if (CTN_ERROR(cond)) {
		    if (!singleUserMode) {
			fprintf(stderr, "child failed to service request\n");
			COND_DumpConditions();
			exitApplication(cond);
		    } else {
			/*
			 * In the single user mode, the parent just discards
			 * the current association and service parameters and
			 * continues to listen to new requests
			 */
			fprintf(stderr,
			      "Iterative server failed to serve request\n");
			clearAssociationKeyAndServiceParameters(cond);
			continue;	/* go to next iteration of main loop */
		    }
		}
		if (!singleUserMode) {
		    /* graceful exit by the child process (return status 0) */
		    exitApplication(SRV_NORMAL);
		}
	    } else {
		/*
		 * Parent has to drop the Association that was created for
		 * the child when an Association request arrived from the
		 * client. If this is not done, both the parent and the child
		 * are capable of receiving on the same association
		 */
		cond = DUL_DropAssociation(&association);
		if (cond != DUL_NORMAL) {
		    exitApplication(cond);
		}
		/*
		 * Add information of the process to the list maintained by
		 * the parent
		 */
		(void) addChildProcess(&service, pid, &processList);
		/*
		 * Now clear the service parameters so as to accept a new set
		 * of parameters on the association
		 */
		(void) DUL_ClearServiceParameters(&service);
		printf("Parent ready to accept new request\n");
	    }
	} else {
	    /*
	     * something went wrong accepting the next Association request
	     * Parent server issues an error message and continues to serve
	     * the next request
	     */
	    fprintf(stderr, "!!!! Error getting next Request.\n");
	    fprintf(stderr, "Discarding this request\n");
	    COND_DumpConditions();
	}
    }
#ifdef MALLOC_DEBUG
    malloc_verify(0);
    malloc_shutdown();
#endif
    return 0;
}