Ejemplo n.º 1
0
void uclear() {
    /*
    *+
    *  Name:
    *     uclear
    *
    *  Purpose:
    *     Clear the list of unoutput strings.
    *
    *  Description:
    *     This routine reclaims the memory used by the list of unoutput
    *     strings and initialises the values of the start and end pointers.
    *
    *  Authors:
    *     MBT: Mark Taylor (STARLINK)
    *
    *  History:
    *     10-DEC-1999 (MBT):
    *        Initial revision.
    *-
    */
    ELEMENT *i, *j;

    /* Reclaim memory. */
    i = ufirst->next;
    while ( i != NULL ) {
        j = i->next;
        if ( i->text != NULL )
            free( i->text );
        free( i );
        i = j;
    }

    /* Reset pointers. */
    unew();
}
Ejemplo n.º 2
0
int main( int argc, char **argv ) {
    /*
    *+
    *  Name:
    *     main
    *
    *  Purpose:
    *     Harness routine for calling the yacc-generated parser.
    *
    *  Flags:
    *     -s
    *        If present, this enforces strict parsing; any parse errors lead
    *        to program termination.  By default, parse errors are silently
    *        tolerated.
    *     -d
    *        Debugging.  It may be followed immediately by the letters 'l',
    *        'y' or both.  This turns on the debugging messages in the lex
    *        part and/or the yacc part of the processor respectively.
    *        This will only work if lex and yacc have been compiled to
    *        enable debugging reports.
    *
    *  Authors:
    *     MBT: Mark Taylor (STARLINK)
    *
    *  History:
    *     25-NOV-1999 (MBT):
    *        Original version.
    *-
    */

    /* Declare external function. */
    int yyparse();

    /* Declare local variables. */
    int retval;
    char *name, *usagef, *text;
    char c;

    /* Get name of program etc. */
    name = *(argv++);
    argc--;
    usagef = "Usage: %s [-d[l][y]] [-s] [ in [ out ] ]\n";

    /* Work through any command line flags. */
    yy_flex_debug = 0;
    yydebug = 0;
    strict = 0;
    while ( argc > 0 && **argv == '-' ) {
        switch( *(++(*argv)) ) {

        /* Debugging output flag. */
        case 'd':
            while ( c = *(++(*argv)) )
                switch( c ) {
                case 'l':
                    yy_flex_debug = 1;
                    break;
                case 'y':
                    yydebug = 1;
                    break;
                };
            break;

        /* Strict error handling flag. */
        case 's':
            strict = 1;
            break;

        /* Reply to any other flag (including -h) with a usage message. */
        default:
            printf( usagef, name );
            exit( 1 );
        }
        argv++;
        argc--;
    }

    /* Open standard input and output appropriately according to command line
       arguments, in the normal filter-type way. */
    switch( argc ) {
    case 2:
        if ( freopen( argv[ 1 ], "w", stdout ) == NULL ) {
            perror( argv[ 1 ] );
            exit( 1 );
        }
    case 1:
        if ( freopen( argv[ 0 ], "r", stdin ) == NULL ) {
            perror( argv[ 0 ] );
            exit( 1 );
        }
    case 0:
        break;
    default:
        printf( usagef, name );
        exit( 1 );
    }

    /* Some initialisation. */
    unew();
    preleng = 0;
    prealloc = 0;
    preval = "";

    /* Call the parser. */
    retval = yyparse();

    /* Some tidying. */
    text = ucontent();
    printf( "%s", text );
    free( text );
    uclear();
    tagwrap();

    /* Return. */
    return( retval );
}
Ejemplo n.º 3
0
static int riva_load(module_t *Module, const char *FileName) {
	riva_t *Riva = unew(riva_t); // This really should be new...
	module_setup(Module, Riva, (module_importer)riva_import);

	gzFile File = gzopen(FileName, "rb");
	char *LoadPath;
	for (int I = strlen(FileName) - 1; I >= 0; --I) {
		if (FileName[I] == PATHCHR) {
			strncpy(LoadPath = (char *)GC_malloc_atomic(I + 2), FileName, I + 1);
			break;
		};
	};
	module_set_path(Module, LoadPath);

	uint32_t Magic; gzread(File, &Magic, 4);
	if (Magic != 0x41564952) {
		log_errorf("Error: %s is not a valid riva module\n", FileName);
		return 0;
	};

	uint32_t NoOfSections; gzread(File, &NoOfSections, 4);
	uint32_t NoOfExports; gzread(File, &NoOfExports, 4);
	uint32_t NoOfRequires; gzread(File, &NoOfRequires, 4);

	jmp_buf OnError[1];

	if (setjmp(OnError)) return 0;
	section_t **Sections = (Riva->Sections = (section_t **)GC_malloc(NoOfSections * sizeof(section_t *)));
	for (int I = 0; I < NoOfSections; ++I) Sections[I] = new(section_t);
	for (int I = 0; I < NoOfSections; ++I) {
		section_t *Section = Sections[I];
		uint8_t Type; gzread(File, &Type, 1);
		switch (Type) {
		case SECT_CODE: {
			Section->Fixup = fixup_code_section;
			gzread(File, &Section->Flags, 1);
			uint32_t Length; gzread(File, &Length, 4);
			uint32_t NoOfRelocs; gzread(File, &NoOfRelocs, 4);
			Section->NoOfRelocs = NoOfRelocs;
			reloc_t *Relocs = (Section->Relocs = (reloc_t *)GC_malloc_uncollectable(NoOfRelocs * sizeof(reloc_t)));
			if (Section->Flags & FLAG_GC) {
				Section->Data = GC_malloc_uncollectable(Length);
			} else {
				Section->Data = GC_malloc_atomic_uncollectable(Length);
			};
			gzread(File, Section->Data, Length);
			for (int J = 0; J < NoOfRelocs; ++J) {
				reloc_t *Reloc = &Relocs[J];
				gzread(File, &Reloc->Size, 1);
				gzread(File, &Reloc->Flags, 1);
				gzread(File, &Reloc->Position, 4);
				uint32_t Index; gzread(File, &Index, 4);
				Reloc->Section = Sections[Index];
			};
		break;};
		case SECT_LIBRARY: {
			Section->Fixup = fixup_library_section;
			gzread(File, &Section->Flags, 1);
			uint32_t Length; gzread(File, &Length, 4);
			gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length);
			Section->Name[Length] = 0;
			if (Section->Flags == LIBRARY_ABS) {
				Section->Path = 0;
			} else if (Section->Flags == LIBRARY_REL) {
				Section->Path = LoadPath;
			};
			for (char *P = Section->Name; *P; ++P) if (*P == '/') *P = PATHCHR;
		break;};
		case SECT_IMPORT: {
			Section->Fixup = fixup_import_section;
			gzread(File, &Section->Flags, 1);
			uint32_t Index; gzread(File, &Index, 4);
			Section->Library = Sections[Index];
			uint32_t Length; gzread(File, &Length, 4);
			gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length);
			Section->Name[Length] = 0;
		break;};
		case SECT_BSS: {
			Section->Fixup = fixup_bss_section;
			gzread(File, &Section->Flags, 1);
			uint32_t Size; gzread(File, &Size, 4);
			Section->Data = (uint8_t *)GC_malloc(Size);
		break;};
		case SECT_SYMBOL: {
			Section->Fixup = fixup_symbol_section;
			gzread(File, &Section->Flags, 1);
			uint32_t Length; gzread(File, &Length, 4);
			gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length);
			Section->Name[Length] = 0;
		break;};
		};
	};
	for (int I = 0; I < NoOfExports; ++I) {
		export_t *Export = new(export_t);
		gzread(File, &Export->Flags, 1);
		uint32_t Index; gzread(File, &Index, 4);
		Export->Section = Sections[Index];
		gzread(File, &Export->Offset, 4);
		uint32_t Length; gzread(File, &Length, 4);
		char *Name = (char *)GC_malloc_atomic(Length + 1);
		gzread(File, Name, Length);
		Name[Length] = 0;
		stringtable_put(Riva->Exports, Name, Export);
	};
	for (int I = 0; I < NoOfRequires; ++I) {
		uint8_t Flags; gzread(File, &Flags, 1);
		uint32_t Length; gzread(File, &Length, 4);
		char *Name = (char *)GC_malloc_atomic(Length + 1);
		char *Path = 0;
		gzread(File, Name, Length);
		Name[Length] = 0;
		if (Flags == LIBRARY_REL) Path = LoadPath;
		for (char *P = Name; *P; ++P) if (*P == '/') *P = PATHCHR;
		module_load(Path, Name);
	};
	gzclose(File);

	void (*__init)(module_t *) = check_import(Riva, "__init", OnError);
 	if (__init) __init(Module);
 	void *Methods = check_import(Riva, "__methods", OnError);
 	if (Methods) add_methods(Methods);
	return 1;
};