예제 #1
0
static int test_item8(void)
{
    int rc = TC_PASS;
    static TYPE_VALUE target_addr[MAX_BUFFER_SIZE * 2];
    static TYPE_VALUE source_addr[MAX_BUFFER_SIZE * 2];
    TYPE_VALUE source_value = 0;
    TYPE_VALUE expect_value = 0;
    int num_proc = 0;
    int my_proc = 0;
    long* pSyncMult = NULL;
    TYPE_VALUE* pWrkMult = NULL;
    int pSyncNum = 2;
    int pWrkNum = 2;

    num_proc = _num_pes();
    my_proc = _my_pe();

    pSyncMult = shmalloc(sizeof(*pSyncMult) * pSyncNum * _SHMEM_REDUCE_SYNC_SIZE);
    if (pSyncMult)
    {
        TYPE_VALUE value = DEFAULT_VALUE;
        int i = 0;
        int j = 0;
        long cur_buf_size = 0;

        for ( j = 0; j < pSyncNum * _SHMEM_REDUCE_SYNC_SIZE; j++ )
        {
            pSyncMult[j] = _SHMEM_SYNC_VALUE;
        }

        /* Give some time to all PE for setting their values */
        shmem_barrier_all();

        pWrkMult = shmalloc(sizeof(*pWrkMult) * pWrkNum * sys_max(MAX_BUFFER_SIZE, _SHMEM_REDUCE_MIN_WRKDATA_SIZE));
        if (pWrkMult)
        {
            value = DEFAULT_VALUE;
            source_value = (TYPE_VALUE)my_proc;
            fill_buffer((void *)source_addr, MAX_BUFFER_SIZE * 2, (void *)&source_value, sizeof(source_value));
            fill_buffer((void *)target_addr, MAX_BUFFER_SIZE * 2, (void *)&value, sizeof(value));
            shmem_barrier_all();
            for (i = 0; (i < __cycle_count) && (rc == TC_PASS); i++)
            {
                cur_buf_size = sys_max(1, (i + 1) * MAX_BUFFER_SIZE / __cycle_count);
                /* Set initial target value */
                value = DEFAULT_VALUE;

                /* Set my value */
                source_value = (TYPE_VALUE)my_proc;

                /* Define expected value */
                expect_value = 0;
                if (my_proc % 2)    expect_value = DEFAULT_VALUE;
                else
                {
                    int k = num_proc;
                    while (k--) expect_value += (k % 2 ? 0 : k);
                }

                /* Put value to peer */
                FUNC_VALUE(target_addr + (i % 2) * MAX_BUFFER_SIZE, source_addr + (i % 2) * MAX_BUFFER_SIZE, cur_buf_size, 0, 1, ((num_proc / 2) + (num_proc % 2)), pWrkMult + (i % pWrkNum) * sys_max(MAX_BUFFER_SIZE, _SHMEM_REDUCE_MIN_WRKDATA_SIZE),  pSyncMult + (i % pSyncNum) * _SHMEM_REDUCE_SYNC_SIZE);
                rc = (!compare_buffer_with_const(target_addr + (i % 2) * MAX_BUFFER_SIZE, cur_buf_size, &expect_value, sizeof(expect_value)) ? TC_PASS : TC_FAIL);

                log_debug(OSH_TC, "my#%d source = %lld expected = %lld actual = %lld buffer size = %lld\n",
                          my_proc, (INT64_TYPE)source_value, (INT64_TYPE)expect_value, (INT64_TYPE)value, (INT64_TYPE)cur_buf_size);

                if (rc)
                {
                    TYPE_VALUE* check_addr = target_addr + (i % 2) * MAX_BUFFER_SIZE;
                    int odd_index = compare_buffer_with_const(check_addr, cur_buf_size, &expect_value, sizeof(expect_value));
                    int show_index = (odd_index > 1 ? odd_index - 2 : 0);
                    int show_size = sizeof(*check_addr) * sys_min(3, cur_buf_size - odd_index - 1);

                    log_debug(OSH_TC, "index of incorrect value: 0x%08X (%d)\n", odd_index - 1, odd_index - 1);
                    log_debug(OSH_TC, "buffer interval: 0x%08X - 0x%08X\n", show_index, show_index + show_size);
                    show_buffer(check_addr + show_index, show_size);
                }
                fill_buffer((void *)(source_addr + (i % 2) * MAX_BUFFER_SIZE), cur_buf_size, (void *)&source_value, sizeof(source_value));
                fill_buffer((void *)(target_addr + (i % 2) * MAX_BUFFER_SIZE ), cur_buf_size, (void *)&value, sizeof(value));
            }
            shfree(pWrkMult);
        } else {
            rc = TC_SETUP_FAIL;
        }
        shfree(pSyncMult);
    } else {
        rc = TC_SETUP_FAIL;
    }

    return rc;
}
예제 #2
0
static int nxsnake_handle_event(GR_EVENT *event)
{
  switch(event->type)
    {
    case GR_EVENT_TYPE_KEY_DOWN:

      /* Allow m to quit no matter where we are*/
      
      if (event->keystroke.ch == 'm')
	    {
	      //GrClose();
          fin = 1;
          pz_close_window(offscreen);
          pz_close_window(swindow);
          GrDestroyTimer(nxsnake_timer);
	      //exit(0);
	    }
      
      switch(game_state)
	{
	case SNAKE_START:
        if (event->keystroke.ch == 'd') 
            {
			game_state = SNAKE_START;
            draw_string((char *) instructions, 1);
            show_buffer();
            break;
            }
	case SNAKE_INSTRUCTIONS:
	case SNAKE_DONE:
	  
	  switch(event->keystroke.ch)
	    {

	    default:
	      start_game();
	      break;
	    }

	  break;

	case SNAKE_PAUSED:
	  if ((event->keystroke.ch == '\r') ||
          (event->keystroke.ch == 'd'))
	    {
	      draw_score();
	      do_frame(1);
	      game_state = SNAKE_PLAYING;
	    }
	  
	  break;

	case SNAKE_NEXTLEVEL:

	  if (current_level >= LEVELCOUNT)
	    current_level = 0;
	  
	  start_level(current_level);
	  draw_score();
	  do_frame(1);   /* and show the first frame */

	  break;

	case SNAKE_PLAYING:
      if (event->keystroke.ch == 'd') 
            {
			game_state = SNAKE_PAUSED;
            draw_string((char *) instructions, 1);
            show_buffer();
            break;
            }
    
	  if (event->keystroke.ch == '\r')
	    	{
			game_state = SNAKE_PAUSED;
          	draw_string((char *) gamepaused, 2);
	  	  	show_buffer();
	      	break;
	    	}
	  
      if ((event->keystroke.ch == 'w') || (event->keystroke.ch == 'f'))
      		{
      		if (redirect_snake(event->keystroke)) 
	    		do_snake_advance();
        	}
        
      if ((event->keystroke.ch == 'l') || (event->keystroke.ch == 'r'))
      	{
        if(last_keystroke == event->keystroke.ch)
      		{
        	if (count_wheel > 7)
            	{
                if (redirect_snake(event->keystroke)) 
	    			do_snake_advance();
                count_wheel = 0;
                }
        	else
 				count_wheel ++;
        	}
      	else
      		count_wheel = 0;
        }
      
        
	  last_keystroke = event->keystroke.ch;
      
	  break;
	}

      break;

    case GR_EVENT_TYPE_EXPOSURE:
      show_buffer();
      break;
    }
 return(1);
}
예제 #3
0
static int test_item7(void)
{
    int rc = TC_PASS;
    TYPE_VALUE* target_addr = NULL;
    TYPE_VALUE* source_addr = NULL;
    TYPE_VALUE source_value = 0;
    TYPE_VALUE expect_value = 0;
    int num_proc = 0;
    int my_proc = 0;

    num_proc = _num_pes();
    my_proc = _my_pe();

    target_addr = (TYPE_VALUE*)shmalloc(sizeof(*target_addr) * __max_buffer_size);
    source_addr = (TYPE_VALUE*)shmalloc(sizeof(*source_addr) * __max_buffer_size);
    if (target_addr && source_addr)
    {
        TYPE_VALUE value = DEFAULT_VALUE;
        int i = 0;
        int j = 0;
        long cur_buf_size = 0;

        for (i = 0; (i < __cycle_count) && (rc == TC_PASS); i++)
        {
            cur_buf_size = sys_max(1, (i + 1) * __max_buffer_size / __cycle_count);
            pWrk = shmalloc(sizeof(*pWrk) * sys_max(cur_buf_size/2 + 1, _SHMEM_REDUCE_MIN_WRKDATA_SIZE));
            if (pWrk)
            {
                /* Set initial target value */
                value = DEFAULT_VALUE;
                fill_buffer((void *)target_addr, cur_buf_size, (void *)&value, sizeof(value));

                /* Give some time to all PE for setting their values */
                shmem_barrier_all();

                /* Set my value */
                source_value = (TYPE_VALUE)my_proc;
                fill_buffer((void *)source_addr, cur_buf_size, (void *)&source_value, sizeof(source_value));

                /* Define expected value */
                expect_value = 0;
                if (my_proc % 2)    expect_value = DEFAULT_VALUE;
                else
                {
                    int k = num_proc;
                    while (k--) expect_value += (k % 2 ? 0 : k);
                }

                /* This guarantees that PE set initial value before peer change one */
                for ( j = 0; j < _SHMEM_REDUCE_SYNC_SIZE; j++ )
                {
                    pSync[j] = _SHMEM_SYNC_VALUE;
                }
                shmem_barrier_all();

                /* Put value to peer */
                FUNC_VALUE(target_addr, source_addr, cur_buf_size, 0, 1, ((num_proc / 2) + (num_proc % 2)), pWrk, pSync);

                /* Get value put by peer:
                 * These routines start the remote transfer and may return before the data
                 * is delivered to the remote PE
                 */
                shmem_barrier_all();
                {
                    int wait = WAIT_COUNT;

                    while (wait--)
                    {
                        value = *target_addr;
                        if (sys_fcompare(expect_value, value)) break;
                        sleep(1);
                    }
                }

                rc = (!compare_buffer_with_const(target_addr, cur_buf_size, &expect_value, sizeof(expect_value)) ? TC_PASS : TC_FAIL);

                log_debug(OSH_TC, "my#%d source = %lld expected = %lld actual = %lld buffer size = %lld\n",
                          my_proc, (INT64_TYPE)source_value, (INT64_TYPE)expect_value, (INT64_TYPE)value, (INT64_TYPE)cur_buf_size);

                if (rc)
                {
                    TYPE_VALUE* check_addr = target_addr;
                    int odd_index = compare_buffer_with_const(check_addr, cur_buf_size, &expect_value, sizeof(expect_value));
                    int show_index = (odd_index > 1 ? odd_index - 2 : 0);
                    int show_size = sizeof(*check_addr) * sys_min(3, cur_buf_size - odd_index - 1);

                    log_debug(OSH_TC, "index of incorrect value: 0x%08X (%d)\n", odd_index - 1, odd_index - 1);
                    log_debug(OSH_TC, "buffer interval: 0x%08X - 0x%08X\n", show_index, show_index + show_size);
                    show_buffer(check_addr + show_index, show_size);
                }

                shfree(pWrk);
            } else {
                rc = TC_SETUP_FAIL;
            }
        }
    }
    else
    {
        rc = TC_SETUP_FAIL;
    }

    if (source_addr)
    {
        shfree(source_addr);
    }

    if (target_addr)
    {
        shfree(target_addr);
    }

    return rc;
}
예제 #4
0
파일: client.c 프로젝트: slayer/rt-n56u
void do_client (void)
{
	char obuffer[1024] = {0}, ibuffer[1024] = {0};
	int obuffer_offset = 0, stop = 0;

	fflush (stdout);  /* get anything out of the buffer */
	conio_init();
	sprintf (ibuffer, "*** connected to %s", net_getpeer (conn));
	write_to_window (ibuffer, 9);
	show_buffer (obuffer, obuffer_offset);

	do {

		if (net_query_rdm (conn)) {
			int col = 10;
			int x = net_receive_rdm (conn, ibuffer, 1024);

			if (x<0)
				strcpy (ibuffer, "!!! (local) error reading packet");
			else
				ibuffer[x] = 0;

			switch (ibuffer[0]) {
				case '*': col = 9; break;
				case '+':
				case '-':
				case '=': col = 11; break;
				case '!': col = 12; break;
			}

			write_to_window (ibuffer, col);
			show_buffer (obuffer, obuffer_offset);

			if (!strcmp (ibuffer, "*** go away")) stop = 1;
			if (!strcmp (ibuffer, "*** server shutting down")) stop = 1;
		}

		if (conio_kbhit()) {
			char ch = conio_getch();
			switch (ch) {
			    	case 7:
				case 8:
					if (obuffer_offset) obuffer[--obuffer_offset] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
				case 13:
					net_send_rdm (conn, obuffer, strlen (obuffer));
					obuffer[obuffer_offset = 0] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
				default:
					obuffer[obuffer_offset] = ch;
					obuffer[++obuffer_offset] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
			}
		}

	} while (!stop);

	erase_buffer();
	conio_exit();
}
예제 #5
0
static int test_item4(void)
{
    int rc = TC_PASS;
    TYPE_VALUE* shmem_addr = NULL;
    TYPE_VALUE* recv_addr = NULL;
    TYPE_VALUE my_value = 0;
    TYPE_VALUE peer_value = 0;
    TYPE_VALUE expect_value = 0;
    int num_proc = 0;
    int my_proc = 0;
    int peer_proc = 0;

    num_proc = _num_pes();
    my_proc = _my_pe();

    shmem_addr = (TYPE_VALUE*)shmalloc(sizeof(*shmem_addr) * __max_buffer_size);
    recv_addr = (TYPE_VALUE*)sys_malloc(sizeof(*recv_addr) * __max_buffer_size);
    if (shmem_addr && recv_addr)
    {
        INT64_TYPE i = 0;
        long cur_buf_size = 0;

        my_value = 0;
        for (i = 0; (i < __cycle_count) && (rc == TC_PASS); i++)
        {
            /* Set my value */
            my_value = (my_proc % 2 ? 1 : -1) * (i * (MAX_VALUE / __cycle_count));
            cur_buf_size = sys_max(1, (i + 1) * __max_buffer_size / __cycle_count);
            fill_buffer((void *)shmem_addr, cur_buf_size, (void *)&my_value, sizeof(my_value));

            /* Give some time to all PE for setting their values */
            shmem_barrier_all();

            /* Define peer and it value */
            peer_proc = (my_proc + 1) % num_proc;
            peer_value = (peer_proc % 2 ? 1 : -1) * (i * (MAX_VALUE / __cycle_count));

            /* Define expected value */
            expect_value = peer_value;

            /* Get value from peer */
            FUNC_VALUE(recv_addr, shmem_addr, cur_buf_size, peer_proc);

            rc = (!compare_buffer_with_const(recv_addr, cur_buf_size, &expect_value, sizeof(expect_value)) ? TC_PASS : TC_FAIL);

            log_debug(OSH_TC, "my(#%d:%lld) peer(#%d:%lld) expected = %lld buffer size = %lld\n",
                               my_proc, (INT64_TYPE)my_value, peer_proc, (INT64_TYPE)peer_value, (INT64_TYPE)expect_value, (INT64_TYPE)cur_buf_size);

            if (rc)
            {
                TYPE_VALUE* check_addr = recv_addr;
                int odd_index = compare_buffer_with_const(check_addr, cur_buf_size, &expect_value, sizeof(expect_value));
                int show_index = (odd_index > 1 ? odd_index - 2 : 0);
                int show_size = sizeof(*check_addr) * sys_min(3, cur_buf_size - odd_index - 1);

                log_debug(OSH_TC, "index of incorrect value: 0x%08X (%d)\n", odd_index - 1, odd_index - 1);
                log_debug(OSH_TC, "buffer interval: 0x%08X - 0x%08X\n", show_index, show_index + show_size);
                show_buffer(check_addr + show_index, show_size);
            }

            shmem_barrier_all();
        }
    }
    else
    {
        rc = TC_SETUP_FAIL;
    }

    if (recv_addr)
    {
        sys_free(recv_addr);
    }

    if (shmem_addr)
    {
        shfree(shmem_addr);
    }

    return rc;
}
예제 #6
0
static int test_item7(void)
{
    int rc = TC_PASS;
    static TYPE_VALUE shmem_addr[MAX_BUFFER_SIZE * 2];
    static TYPE_VALUE send_addr[MAX_BUFFER_SIZE * 2];
    TYPE_VALUE my_value = 0;
    TYPE_VALUE peer_value = 0;
    TYPE_VALUE expect_value = 0;
    int num_proc = 0;
    int my_proc = 0;
    int root_proc = 0;
    long* pSyncMult = NULL;
    int pSyncNum = 2;

    num_proc = _num_pes();
    my_proc = _my_pe();

    pSyncMult = shmalloc(sizeof(*pSyncMult) * pSyncNum * _SHMEM_COLLECT_SYNC_SIZE);
    if (!pSyncMult)
    {
        rc = TC_SETUP_FAIL;
    }

    if (rc == TC_PASS)
    {
        int i = 0;
        int j = 0;

        for ( j = 0; j < pSyncNum * _SHMEM_COLLECT_SYNC_SIZE; j++ )
        {
            pSyncMult[j] = _SHMEM_SYNC_VALUE;
        }

        /* Give some time to all PE for setting their values */
        shmem_barrier_all();

        /* Set root */
        root_proc = 0;

        my_value = DEFAULT_VALUE;
        peer_value = MAX_VALUE;
        expect_value = (my_proc == root_proc ? DEFAULT_VALUE : peer_value);

        fill_buffer((void *)send_addr, MAX_BUFFER_SIZE * 2, (void *)&peer_value, sizeof(peer_value));
        fill_buffer((void *)shmem_addr, MAX_BUFFER_SIZE * 2, (void *)&my_value, sizeof(my_value));
        shmem_barrier_all();
        for (i = 0; (i < __cycle_count) && (rc == TC_PASS); i++)
        {
            /* Put value to peer */
            FUNC_VALUE(shmem_addr + (i % 2) * MAX_BUFFER_SIZE, send_addr + (i % 2) * MAX_BUFFER_SIZE, MAX_BUFFER_SIZE, root_proc, 0, 0, num_proc, pSyncMult + (i % pSyncNum) * _SHMEM_COLLECT_SYNC_SIZE);
            rc = (!compare_buffer_with_const(shmem_addr + (i % 2) * MAX_BUFFER_SIZE, MAX_BUFFER_SIZE, &expect_value, sizeof(expect_value)) ? TC_PASS : TC_FAIL);

            log_debug(OSH_TC, "my#%d root(#%d:%lld) expected = %lld actual = %lld buffer size = %lld\n",
                               my_proc, root_proc, (INT64_TYPE)peer_value, (INT64_TYPE)expect_value, (INT64_TYPE)(*shmem_addr), (INT64_TYPE)MAX_BUFFER_SIZE);

            if (rc)
            {
                TYPE_VALUE* check_addr = shmem_addr + (i % 2) * MAX_BUFFER_SIZE;
                int odd_index = compare_buffer_with_const(check_addr, MAX_BUFFER_SIZE, &expect_value, sizeof(expect_value));
                int show_index = (odd_index > 1 ? odd_index - 2 : 0);
                int show_size = sizeof(*check_addr) * sys_min(3, MAX_BUFFER_SIZE - show_index);

                log_debug(OSH_TC, "index of incorrect value: 0x%08X (%d)\n", odd_index - 1, odd_index - 1);
                log_debug(OSH_TC, "buffer interval: 0x%08X - 0x%08X\n", show_index, show_index + show_size);
                show_buffer(check_addr + show_index, show_size);
            }
                fill_buffer((void *)(send_addr + (i % 2) * MAX_BUFFER_SIZE), MAX_BUFFER_SIZE, (void *)&peer_value, sizeof(peer_value));
                fill_buffer((void *)(shmem_addr + (i % 2) * MAX_BUFFER_SIZE ), MAX_BUFFER_SIZE, (void *)&my_value, sizeof(my_value));
        }
    }

    if (pSyncMult)
    {
        shfree(pSyncMult);
    }

    return rc;
}
예제 #7
0
static int test_item6(void)
{
    int rc = TC_PASS;
    static TYPE_VALUE shmem_addr[MAX_BUFFER_SIZE];
    static TYPE_VALUE send_addr[MAX_BUFFER_SIZE];
    TYPE_VALUE my_value = 0;
    TYPE_VALUE peer_value = 0;
    TYPE_VALUE expect_value = 0;
    int num_proc = 0;
    int my_proc = 0;
    int root_proc = 0;

    num_proc = _num_pes();
    my_proc = _my_pe();

    {
        TYPE_VALUE value = DEFAULT_VALUE;
        int i = 0;
        int j = 0;
        long cur_buf_size = 0;

        for (i = 0; (i < __cycle_count) && (rc == TC_PASS); i++)
        {
            cur_buf_size = sys_max(1, (i + 1) * MAX_BUFFER_SIZE / __cycle_count);

            /* Set my value */
            my_value = DEFAULT_VALUE;
            fill_buffer((void *)shmem_addr, cur_buf_size, (void *)&my_value, sizeof(my_value));

            /* Give some time to all PE for setting their values */
            shmem_barrier_all();

            /* Define peer and it value */
            peer_value = (i * (MAX_VALUE / __cycle_count));
            fill_buffer((void *)send_addr, cur_buf_size, (void *)&peer_value, sizeof(peer_value));

            /* Set root */
            root_proc = 0;

            /* Define expected value */
            expect_value = (((my_proc % 2) == 0) && (my_proc != root_proc) ? peer_value : DEFAULT_VALUE);

            /* This guarantees that PE set initial value before peer change one */
            for ( j = 0; j < _SHMEM_COLLECT_SYNC_SIZE; j++ )
            {
                pSync[j] = _SHMEM_SYNC_VALUE;
            }
            shmem_barrier_all();

            /* Put value to peer */
            if ((my_proc % 2) == 0)
            {
                FUNC_VALUE(shmem_addr, send_addr, cur_buf_size, root_proc, 0, 1, ((num_proc / 2) + (num_proc % 2)), pSync);
            }

            /* Get value put by peer:
             * These routines start the remote transfer and may return before the data
             * is delivered to the remote PE
             */
            shmem_barrier_all();
            {
                int wait = WAIT_COUNT;

                while (wait--)
                {
                    value = *shmem_addr;
                    if (expect_value == value) break;
                    sleep(1);
                }
            }

            rc = (!compare_buffer_with_const(shmem_addr, cur_buf_size, &expect_value, sizeof(expect_value)) ? TC_PASS : TC_FAIL);

            log_debug(OSH_TC, "my#%d root(#%d:%lld) expected = %lld actual = %lld buffer size = %lld\n",
                               my_proc, root_proc, (INT64_TYPE)peer_value, (INT64_TYPE)expect_value, (INT64_TYPE)value, (INT64_TYPE)cur_buf_size);

            if (rc)
            {
                TYPE_VALUE* check_addr = shmem_addr;
                int odd_index = compare_buffer_with_const(check_addr, cur_buf_size, &expect_value, sizeof(expect_value));
                int show_index = (odd_index > 1 ? odd_index - 2 : 0);
                int show_size = sizeof(*check_addr) * sys_min(3, cur_buf_size - show_index);

                log_debug(OSH_TC, "index of incorrect value: 0x%08X (%d)\n", odd_index - 1, odd_index - 1);
                log_debug(OSH_TC, "buffer interval: 0x%08X - 0x%08X\n", show_index, show_index + show_size);
                show_buffer(check_addr + show_index, show_size);
            }
        }
    }

    return rc;
}
예제 #8
0
void preParse(char **body, char **header, char **label)

/**************************************************************************
    purpose: obtain the next section of the latex file
    
    This is now a preparsing routine that breaks a file up into sections.  
    Macro expansion happens here as well.  \input and \include are also
    handled here.  The reason for this routine is allow \labels to refer
    to sections.  
    
    This routine reads text until a new section heading is found.  The text 
    is returned in body and the *next* header is returned in header.  If 
    no header follows then NULL is returned.
    
**************************************************************************/
{
    int any_possible_match, found;
    char cNext, cThis, *s, *text, *next_header, *str, *p;
    int i;
    int possible_match[43];
    char *command[43] = { "",   /* 0 entry is for user definitions */
        "",                     /* 1 entry is for user environments */
        "\\begin{verbatim}", 
        "\\begin{figure}",      "\\begin{figure*}", 
        "\\begin{equation}",    "\\begin{equation*}",
        "\\begin{eqnarray}",    "\\begin{eqnarray*}",
        "\\begin{table}",       "\\begin{table*}",
        "\\begin{description}", "\\begin{comment}",
        "\\end{verbatim}", 
        "\\end{figure}",        "\\end{figure*}", 
        "\\end{equation}",      "\\end{equation*}",
        "\\end{eqnarray}",      "\\end{eqnarray*}",
        "\\end{table}",         "\\end{table*}",
        "\\end{description}",   "\\end{comment}",
        "\\part", "\\chapter",  "\\section", "\\subsection", "\\subsubsection",
        "\\section*", "\\subsection*", "\\subsubsection*",
        "\\label", "\\input", "\\include", "\\verb", "\\url", "\\nolinkurl",
        "\\newcommand", "\\def", "\\renewcommand", "\\endinput", "\\end{document}",
    };

    int ncommands = 43;

    const int b_verbatim_item = 2;
    const int b_figure_item = 3;
    const int b_figure_item2 = 4;
    const int b_equation_item = 5;
    const int b_equation_item2 = 6;
    const int b_eqnarray_item = 7;
    const int b_eqnarray_item2 = 8;
    const int b_table_item = 9;
    const int b_table_item2 = 10;
    const int b_description_item = 11;
    const int b_comment_item = 12;
    const int e_verbatim_item = 13;
    const int e_figure_item = 14;
    const int e_figure_item2 = 15;
    const int e_equation_item = 16;
    const int e_equation_item2 = 17;
    const int e_eqnarray_item = 18;
    const int e_eqnarray_item2 = 19;
    const int e_table_item = 20;
    const int e_table_item2 = 21;
    
    const int e_description_item = 22;
    const int e_comment_item = 23;

    const int label_item = 32;
    const int input_item = 33;
    const int include_item = 34;
    
    const int verb_item = 35;
    const int url_item = 36;
    const int nolinkurl_item = 37;
    const int new_item = 38;
    const int def_item = 39;
    const int renew_item = 40;
    const int endinput_item = 41;
    const int e_document_item = 42;

    int bs_count = 0;          /* number of backslashes encountered in a row */
    size_t cmd_pos = 0;        /* position of start of command relative to end of buffer */
    int label_depth = 0;
    int i_match = 0;
    
    text = NULL;
    next_header = NULL;         /* typically becomes \subsection{Cows eat grass} */
    *body = NULL;
    *header = NULL;
    *label = NULL;

    PushTrackLineNumber(FALSE);
    reset_buffer();
    while (1) {

        cThis = getRawTexChar();
        while (cThis == '\0' && getParserDepth() > 0) {
            PopSource();
            diagnostics(4,"parser depth is now %d", getParserDepth());
            cThis = getRawTexChar();
        }

        if (cThis == '\0')
            diagnostics(6, "[%ld] xchar=000 '\\0' (backslash count=%d)", section_buffer_end, bs_count);
        else if (cThis == '\n')
            diagnostics(6, "[%ld] xchar=012 '\\n' (backslash count=%d)", section_buffer_end, bs_count);
        else
            diagnostics(6, "[%ld] xchar=%03d '%c' (backslash count=%d)", section_buffer_end, (int) cThis, cThis, bs_count);

		cThis = skipBOM(cThis);

        add_chr_to_buffer(cThis);

        if (cThis == '\0') break;

        if (cThis == '%' && even(bs_count)) {   
            int n = 1;  /* remove initial % */
            do {
                cNext = getRawTexChar();
                add_chr_to_buffer(cNext);
                n++;
            } while (cNext != '\n' && cNext != '\0');        
            move_end_of_buffer(-n);
            if (0) show_buffer("percent");
            continue;
        }

        /* cmd_pos > 0 means that we have encountered a '\' and some
           fraction of a command, e.g., '\sec', therefore the first
           time that a backslash is found then cmd_pos becomes 1,      */
        if (cThis == '\\') {
            bs_count++;
            if (odd(bs_count)) {    /* avoid "\\section" and "\\\\section" */
                for (i = 0; i < ncommands; i++)
                    possible_match[i] = TRUE;
                cmd_pos = 1;
                continue;
            }
        } else
            bs_count = 0;

        if (cmd_pos == 0) continue;

        /* replace "\ " and "\\n" with a space */
        if (cmd_pos == 1 &&  (cThis == '\n' || cThis == ' ') ) {
            move_end_of_buffer(-1);
            add_chr_to_buffer(' ');
            cmd_pos = 0;
            continue;
        }
        
        /* at this point we are have encountered a command and may have to do something.
           if it is a user definition, then the user definition is expanded here
           if it is a user environment, then expansion also happens here
           it it is something else then we may or may not have to mess with it. */

        /* hack to convert '\begin   {' --> '\begin{' */
        if (cThis==' ' && (matches_buffer_tail("\\begin") || matches_buffer_tail("\\end")) ) {
            diagnostics(5, "matched '\\begin ' or '\\end '");
            do {cThis = getRawTexChar();} while (cThis == ' ');
        }
        
        /* hack to convert '\begin{   ' --> '\begin{' */
        if (cThis==' ' && (matches_buffer_tail("\\begin{") || matches_buffer_tail("\\end{")) ) {
            diagnostics(5, "matched '\\begin{ ' or '\\end{ '");
            do {cThis = getRawTexChar();} while (cThis == ' ');
        }
        
        any_possible_match = FALSE;
        found = FALSE;

        /* is is possibly a user defined command? */
        if (possible_match[0]) {
            possible_match[0] = maybeDefinition(section_buffer + section_buffer_end - cmd_pos + 1, cmd_pos - 1);
            
            if (possible_match[0]) {        /* test to make sure \userdef is complete */
                any_possible_match = TRUE;
                cNext = getRawTexChar();    /* wrong when cNext == '%' */
                ungetTexChar(cNext);
                
                if (!isalpha((int) cNext)) {   /* is macro name complete? */
    
                    *(section_buffer + section_buffer_end + 1) = '\0';
                    i = existsDefinition(section_buffer + section_buffer_end - cmd_pos + 1);
                    if (i > -1) {
                        diagnostics(4, "matched <%s> ", section_buffer + section_buffer_end - cmd_pos);
                        if (cNext == ' ') {
                            cNext = getNonSpace();
                            ungetTexChar(cNext);
                        }
    
                        move_end_of_buffer(-cmd_pos-1);  /* remove \userdef */
                        
                        str = expandDefinition(i);
                        PushSource(NULL, str);
                        free(str);
                        cmd_pos = 0;
                        bs_count = 0;
                        continue;
                    }
                }
            }
        }

        /* is it a user defined environment? */
        if (possible_match[1]) {
            char *pp = section_buffer + section_buffer_end - cmd_pos;
            possible_match[1] = maybeEnvironment(pp, cmd_pos);
            
            if (possible_match[1] == TRUE) {
    
                any_possible_match = TRUE;
                cNext = getRawTexChar();    /* wrong when cNext == '%' */
    
                /* \begin{name} or \end{name} will end with '}' */
                if (cNext == '}') {
                    char *ss = NULL;
                    
                    *(pp + cmd_pos + 1) = '\0';
                    if (*(pp + 1) == 'e') {  
                        i = existsEnvironment(pp + strlen("\\end{"));
                        ss = expandEnvironment(i, CMD_END);
                    } else { 
                        i = existsEnvironment(pp + strlen("\\begin{"));
                        ss = expandEnvironment(i, CMD_BEGIN);
                    }
    
                    if (ss) {          /* found */
                        diagnostics(5, "matched <%s}>", pp);
                        diagnostics(5, "expanded to <%s>", ss);
    
                        PushSource(NULL, ss);
                        move_end_of_buffer(-cmd_pos-1); /* remove \begin{userenvironment} */
                        
                        free(ss);
                        cmd_pos = 0;
                        continue;
                    }
                }
                
                ungetTexChar(cNext);    /* put the character back */
            }
        }

        /* is it one of the commands listed above? */
        for (i = 2; i < ncommands; i++) {
            if (possible_match[i]) {
                if (cThis == command[i][cmd_pos])
                    any_possible_match = TRUE;
                else
                    possible_match[i] = FALSE;

                diagnostics(6,"cmd_pos = %d, char = %c, possible match %s, size=%d, possible=%d", \
                    cmd_pos,cThis,command[i],strlen(command[i]),possible_match[i]);
            }
        }



        i_match = -1;
        for (i = 2; i < ncommands; i++) {   /* discover any exact matches */
            if (possible_match[i]) {
                diagnostics(6, "testing for <%s>", command[i]);
                
                /* right length? */
                
                if (cmd_pos+1 == strlen(command[i])) {

                    if (i<=e_comment_item || i==e_comment_item) { 
                        /* these entries are complete matches */ 
                        found = TRUE;
                    } else {
                        /* these entries we need to be a bit more careful and check next character */
                        cNext = getRawTexChar();
                        ungetTexChar(cNext);
                        
                        /* this test for the end of commands may still need tweaking */
                        if (!isalpha(cNext) && cNext != '*') {
                            found = TRUE;
                        }
                    }
                }
            }
            
            if (found == TRUE) {
                diagnostics(6,"preparse matched '%s'",command[i]);
                i_match = i;
                break;
            }
        }


        if (any_possible_match)
            cmd_pos++;
        else
            cmd_pos = 0;    /* no possible matches, reset and wait for next '\\' */
        

        if (!found)
            continue;

        if (i_match == endinput_item) {
            diagnostics(6, "\\endinput");
            move_end_of_buffer(-9);         /* remove \endinput */
            PopSource();
            cmd_pos = 0;          /* keep looking */
            continue;
        }

        /* \end{document} reached! Stop processing */
        if (i_match == e_document_item) {
            diagnostics(6, "\\end{document}");
            move_end_of_buffer(-strlen(command[e_document_item]));
            add_chr_to_buffer('\0');
            safe_free(*header);
            *header = strdup(command[e_document_item]);
            p = section_buffer;
            while (*p==' ' || *p == '\n') p++;
            *body = strdup(p);
            PopTrackLineNumber();
            diagnostics(6, "body = %s", section_buffer);
            diagnostics(6, "next header = '%s'", command[e_document_item]);
            return;
        }

        if (i_match == verb_item) {  /* slurp \verb#text# */
            char cc;
            
            cNext = getRawTexChar();
            add_chr_to_buffer(cNext);
            diagnostics(6, "verb char = %c", cNext);
 
            do {
                cc = getRawTexChar();
                add_chr_to_buffer(cc);
            } while (cc != cNext && cc != '\0');
              
            cmd_pos = 0;          /* reset the command position */
            continue;
        }

        /* cannot ignore this because it may contain unescaped '%' */
        if (i_match == url_item || i_match == nolinkurl_item) {  
            char cc;
 
            do {
                cc = getRawTexChar();
                add_chr_to_buffer(cc);
            } while (cc != '\0' && cc != '}');

            cmd_pos = 0;          /* reset the command position */
            continue;
        }

        if (i_match == include_item) {
            CmdInclude(0);
            move_end_of_buffer(-strlen(command[i]));    
            cmd_pos = 0;                 /* reset the command position */
            continue;
        }

        if (i_match == input_item) {
            CmdInclude(1);
            move_end_of_buffer(-strlen(command[i]));    
            cmd_pos = 0;                 /* reset the command position */
            continue;
        }

        if (i_match == label_item) {
            char *tag;
            tag = getBraceParam();

            /* append \label{tag} to the buffer */
            add_chr_to_buffer('{');
            add_str_to_buffer(tag);
            add_chr_to_buffer('}');

            if (!(*label) && strlen(tag) && label_depth == 0)
                *label = strdup_nobadchars(tag);

            free(tag);
            cmd_pos = 0;          /* keep looking */
            continue;
        }

        /* process any new definitions */
        if (i_match == def_item || i_match == new_item || i_match == renew_item) {
        
            cNext = getRawTexChar();    /* wrong when cNext == '%' */
            ungetTexChar(cNext);
            
            if (isalpha((int) cNext))   /* is macro name complete? */
                continue;

            move_end_of_buffer(-strlen(command[i]));    /* do not include in buffer */

            if (i_match == def_item)
                CmdNewDef(DEF_DEF);
            else if (i_match == new_item)
                CmdNewDef(DEF_NEW);
            else
                CmdNewDef(DEF_RENEW);

            cmd_pos = 0;          /* keep looking */
            continue;
        }

        if (i_match == b_figure_item   || i_match == b_figure_item2   || 
            i_match == b_equation_item || i_match == b_equation_item2 || 
            i_match == b_eqnarray_item || i_match == b_eqnarray_item2 ||
            i_match == b_table_item    || i_match == b_table_item2    ||
            i_match == b_description_item) {
            label_depth++;      /* labels now will not be the section label */
            cmd_pos = 0;
            continue;
        }

        if (i_match == e_figure_item   || i_match == e_figure_item2   || 
            i_match == e_equation_item || i_match == e_equation_item2 || 
            i_match == e_eqnarray_item || i_match == e_eqnarray_item2 ||
            i_match == e_table_item    || i_match == e_table_item2    ||
            i_match == e_description_item)  {
            label_depth--;      /* labels may now be the section label */
            cmd_pos = 0;
            continue;
        }

        if (i_match == b_verbatim_item) { /* slurp environment ... toxic contents! */
            s = getTexUntil(command[e_verbatim_item], TRUE);
            add_str_to_buffer(s);
            free(s);
            add_str_to_buffer(command[e_verbatim_item]);
            diagnostics(4,"matched \\end{verbatim}");
            cmd_pos = 0;          /* keep looking */
            continue;
        }

        if (i_match == b_comment_item) {  /* slurp environment ... toxic contents! */
            s = getTexUntil(command[e_comment_item], TRUE);
            add_str_to_buffer(s);
            free(s);
            add_str_to_buffer(command[e_comment_item]);
            cmd_pos = 0;          /* keep looking */
            continue;
        }

        diagnostics(4, "possible end of section");
        diagnostics(4, "label_depth = %d", label_depth);

        if (label_depth > 0)    /* still in a \begin{xxx} environment? */
            continue;

        /* actually found command to end the section */
        diagnostics(4, "preParse() found command to end section");
        s = getBraceParam();
        next_header = strdup_together4(command[i], "{", s, "}");
        free(s);

        move_end_of_buffer(-strlen(command[i]));
        add_chr_to_buffer('\0');
        break;
    }
    
    /*eliminate white space at beginning of buffer */
    p = section_buffer;
    while (*p==' ' || *p == '\n') p++;
    
    *body = strdup(p);
    safe_free(*header);
    *header = next_header;
    PopTrackLineNumber();
}