#include <rmx.h>
#include <udi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

typedef struct
    {
    U16     jobs;
    RMXOBJ  job[1];
    } OFFSPRING_LIST;

RMXOBJ  this_job, root_job;
RMXRET  status;
int     blanks;
U8      buff[50];

void display_jobtree( RMXOBJ job, int level )
    {
    RMXOBJ          list_tkn;
    OFFSPRING_LIST  *list;
    int             index;

    blanks = level << 2;

    for ( index = 0; index < blanks; index++ )
        putchar( ' ' );

    printf( "%04hX%s\n", job, ( job == this_job )? " (jobtree's job)" : "" );

    if ( list_tkn = RQOFFSPRING( job, &status ) )
        {
        list = buildptr( list_tkn, 0 );
        
        for ( index = 0; index < list->jobs; index++ )
            display_jobtree( list->job[index], level + 1 );

        RQDELETESEGMENT( list_tkn, &status );
        }
    }

int main( int argc, char *argv[] )
    {
    root_job = RQGETTASKTOKENS( ROOT_JOB, &status );
    this_job = RQGETTASKTOKENS( JOB_TOKEN, &status );

    dqgetsystemid( buff, &status );
    buff[buff[0]+1] = '\0';
    printf( "\n%s Job Tree\n\n", &buff[1] );

    display_jobtree( root_job, 0 );
    putchar( '\n' );
    return( 0 );
    }
