⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cl386.c

📁 一个c compiler的source code
💻 C
📖 第 1 页 / 共 2 页
字号:
    return name;
}


char *FileExt ( Item *item ) {
	_splitpath( item->data, disk, path, name, ext );
	return ext;
}


void ListDelete( List *list ) {
	Item *p, *q;

	chkNULL( list, "Pointer is NULL !" );

	for( p = list->head; p; p = q ) {
		q = p->next;
		free(p);
	}
	list->head = list->tail = NULL;
	list->count = 0;
}


void ListAdd( List *list, char *file, int tmp ) {
	Item *item;

	chkNULL( list, "Pointer is NULL !" );

	item = ItemCreate( file, tmp );

	chkNULL( item, "Out of memory !" );

	if ( list->count == 0 )
		list->head = list->tail = item;
	else {
		(list->tail)->next = item;
		list->tail = item;
	}
	list->count++;
}


void ListIter( List *list, void (*iter)( Item *) ) {
	Item *p;

	chkNULL( list, "Pointer is NULL !" );

	if ( list->count && iter ) {
		for ( p = list->head; p; p = p->next )
			(*iter)(p);
	}
}


static char *need;
static int found = 0;


static void find( Item *item ) {
	if ( strcmp( item->data, need ) == 0 )
		found = 1;
}


int  ListFind( List *list, char *data ) {
	found = 0;
	need = data;
	ListIter( list, find );
	return found;
}


char findpath[ _MAX_PATH ];
char findname[ _MAX_PATH ];


void get_source( char *filename ) {
	_splitpath( filename, disk, path, name, ext );

	if ( strchr( filename, '?' ) || strchr( filename, '*' ) ) {  /* Check for '*' and '?' */
		_makepath( findpath, disk, path, NULL, NULL );
		_makepath( findname, NULL, NULL, name, ext );
		find_source( findpath, findname );
	}
	else
		get_one_source( filename );
}

#ifdef __TURBOC__

void find_source( char *fpath, char *fname ) {
	int fpath_len = strlen( fpath );
	struct ffblk fb;

	if ( !findfirst( strcat( fpath, fname), &fb, FA_ARCH) )
	do {
		fpath[ fpath_len ] = '\0';
		get_one_source( strcat( fpath, fb.ff_name) );
	} while (! findnext(&fb) );
}


#else


void find_source( char *fpath, char *fname ) {
	int fpath_len = strlen( fpath );
	struct find_t fb;

	if (!_dos_findfirst( strcat( fpath, fname), _A_NORMAL, &fb ) )
	do {
		fpath[ fpath_len ] = '\0';
		get_one_source( strcat( fpath, fb.name) );
	} while( ! _dos_findnext( &fb ) );
}

#endif

void get_one_source( char *srcfile ) {
    int temp = FALSE;
    static char file[ _MAX_PATH ];
    static char disk[ _MAX_DRIVE ];
    static char path[ _MAX_DIR ];
    static char name[ _MAX_FNAME ];
	static char ext [ _MAX_EXT ];

	strncpy( file, srcfile, _MAX_PATH );
    _splitpath( strupr(file) , disk, path, name, ext );
//  _makepath( file, disk, path, name, ext );

    if ( strcmp( ext, _ASM ) != 0 && \
         strcmp( ext, _OBJ ) != 0 && strcmp( ext, _LIB ) != 0 ) {
        if ( ListFind( &f_cpp, file ) )
            fprintf( stderr, "Warning! File already exist : %s\n", file );
        else
            FileAdd( &f_cpp, file, temp++ );
#ifdef TO_CURR_DIR
		_makepath( file, disk, NULL, name, strcpy( ext, _ASM ) );
#else
        _makepath( file, disk, path, name, strcpy( ext, _ASM ) );
#endif
    }

    if ( strcmp( ext, _ASM ) == 0 ) {
		if ( ListFind( &f_asm, file ) )
			fprintf( stderr, "Warning! File already exist : %s\n", file );
		else
            FileAdd( &f_asm, file, temp++ );
        _makepath( file, disk, path, name, strcpy( ext, _OBJ) );
    }

    if ( strcmp( ext, _OBJ ) == 0 ) {
        if ( ListFind( &f_obj, file ) )
            fprintf( stderr, "Warning! File already exist : %s\n", file );
        else
            FileAdd( &f_obj, file, temp++ );
    }

    if ( strcmp( ext, _LIB ) == 0 ) {
        if ( ListFind( &f_lib, file ) )
            fprintf( stderr, "Warning! File already exist : %s\n", file );
        else
            FileAdd( &f_lib, file, 0 );
    }
}


void DeleteList( void ) {  // delete all list from memory
	ListDelete( &f_cpp );
	ListDelete( &f_asm );
	ListDelete( &f_obj );
	ListDelete( &f_lib );
	ListDelete( &l_def );
	ListDelete( &l_inc );
//    ListDelete( &l_lib );
	ListDelete( &l_cc386 );
}

void DeleteAll( void ) {   // for atexit()
	DeleteList();
	if ( !_keep_rsp ) {
		remove( COMPILER_RSP );
		remove( ASSEMBLER_RSP );
		remove( LINKER_RSP );
		remove( DELETE_RSP );
	}
}

void exec( char *prg, char *rsp ) {
	static char *args[3];
	int err;

	args[0] = prg;
	args[1] = rsp;
	args[2] = NULL;

	err = spawnvp( P_WAIT, prg, args );

	if ( err <  0 )
		error( ERR_RUNTIME, prg );
	else if ( err > 0 )
		error( err, prg );
}


FILE *frsp;


static void compile_def  ( Item *p ) { fprintf( frsp, " /D%s", p->data ); }
static void compile_inc  ( Item *p ) { fprintf( frsp, "%s;", p->data ); }
static void compile_opt  ( Item *p ) { fprintf( frsp, " %s", p->data ); }
static void compile_files( Item *p ) { fprintf( frsp, "\n%s", p->data ); }

static void compile_rsp( char *file, char *def_opt ) {
	frsp = fopen( file, "wt" );
	if ( frsp == NULL )
		error( ERR_RUNTIME, file );
	fprintf( frsp, " %s", def_opt );
	ListIter( &l_cc386, compile_opt );
	fprintf( frsp, " %ci", ( _dump_cpp ? '+' : '-' ) );
	fprintf( frsp, " %ce", ( _dump_err ? '+' : '-' ) );
	fprintf( frsp, " %cl", ( _dump_lst ? '+' : '-' ) );
	fprintf( frsp, " %cA", (     _ansi ? '+' : '-' ) );
	ListIter( &l_def, compile_def );
	if ( l_inc.count > 0 ) {
		fprintf( frsp, " /I" );
		ListIter( &l_inc, compile_inc );
	}
	ListIter( &f_cpp, compile_files );
	fprintf( frsp, "\n" );
	fclose( frsp );
}

void compile( void ) {
	if ( _compile /* && f_cpp.count > 0 */ )
	{
//		compile_rsp( COMPILER_RSP, COMPILER_OPT );
		exec( COMPILER, "/f" COMPILER_RSP );
		if ( !_keep_rsp )
			remove( COMPILER_RSP );
//		ListDelete( &f_cpp );
//		ListDelete( &l_cc386 );
	}
}

static void delete_temp( Item *p ) { if ( p->temp ) remove( p->data ); }

static void assemble_def( Item *p ) { fprintf( frsp, " /d%s", p->data ); }
static void assemble_inc( Item *p ) { fprintf( frsp, " /i%s", p->data ); }

char *asm_opt;

static void gen_for_asm( Item *p ) {
	fprintf( frsp, " %s", asm_opt );
	fprintf( frsp, " /z%c", ( _debug ? 'i' : 'n' ) );
	ListIter( &l_inc, assemble_inc );
	ListIter( &l_def, assemble_def );
	if ( _dump_lst )
		fprintf( frsp, " %s,%s%s%s.OBJ,%s%s%s.LSA;\n", p->data,
			FileDisk(p), FilePath(p), FileName(p),
			FileDisk(p), FilePath(p), FileName(p) );
	else
		fprintf( frsp, " %s,%s%s%s.OBJ,NUL;\n", p->data,
			FileDisk(p), FilePath(p), FileName(p) );
}

void assemble_rsp( char *file, char *def_opt ) {
	frsp = fopen( file, "wt" );
	if ( frsp == NULL )
		error( ERR_RUNTIME, file );
	asm_opt = def_opt;
	ListIter( &f_asm, gen_for_asm );
	fclose( frsp );
}

void assemble(void) {
	if ( _assemble /* && f_asm.count > 0 */ )
	{
//		assemble_rsp( ASSEMBLER_RSP, ASSEMBLER_OPT );
		exec( ASSEMBLER, "@" ASSEMBLER_RSP );
		if ( !_keep_rsp )
			remove( ASSEMBLER_RSP );
//		if ( !_keep_gen )
//			ListIter( &f_asm, delete_temp );
//		ListDelete( &f_asm );
//		ListDelete( &l_def );
//		ListDelete( &l_inc );
	}
}


char *first( void ) {
	if ( strlen( _exename ) > 0 )
		return _exename;
	else if ( f_obj.count > 0 )
		return FileName( f_obj.head );
	else if ( f_lib.count > 0 )
		return FileName( f_lib.head );
	else
		return NULL;
}

char *libpath( char *lib ) {
	static char library[ _MAX_PATH ];
	strcpy( library, _libpath );
	if ( library[ strlen( library) - 1 ] != '\\' )
		strcat( library, "\\" );
	return strcat( library, lib );
}

static void out_obj( Item *p ) { fprintf( frsp, " %s", p->data ); }

void tlink_rsp( char *file ) {
	char *name = first();

	frsp = fopen( file, "wt" );
	if ( frsp == NULL )
		error( ERR_RUNTIME, file );
	fprintf( frsp, "/3/c/d" );
	if ( _map_file )
		fprintf( frsp, "/m/l/s" );
	else
		fprintf( frsp, "/x" );
	if ( _debug )
		fprintf( frsp, "/v" );
	if ( !_nodef_lib ) {
		if ( _output == 'p' )
			fprintf( frsp, " %s", \
				libpath( ( _debug ? "C0DOSD.OBJ" : "C0DOS.OBJ" ) )  );
		else
			fprintf( frsp, " %s", \
				libpath( ( _debug ? "C0DOSLD.OBJ" : "C0DOSL.OBJ" ) )  );
	}
	ListIter( &f_obj, out_obj );
	fprintf( frsp, ",%s,%s,", name, name );
	if ( !_nodef_lib )
		fprintf( frsp, " %s", libpath( "CLDOS.LIB" )  );
	ListIter( &f_lib, out_obj );
	fprintf( frsp, "\n" );
	fclose( frsp );
}

void tlink(void) {
//	tlink_rsp( LINKER_RSP );
	exec( TLINK, "@" LINKER_RSP );
}

static void wout_obj( Item *p) { fprintf( frsp, "file %s\n", p->data ); }
static void wout_lib( Item *p) { fprintf( frsp, "library %s\n", p->data ); }

void wlink_rsp( char *file ) {
	char *name = first();

	frsp = fopen( file, "wt" );
	if ( frsp == NULL )
		error( ERR_RUNTIME, file );
	fprintf( frsp, "# Generate from CL386.EXE\n" );
	fprintf( frsp, "format os2 le\n" );
	fprintf( frsp, "option nod\n" );
	if ( _map_file )
		fprintf( frsp, "option map\n" );
	if ( _debug )
		fprintf( frsp, "option symf\n" );
	if ( _output == 'w' )
		fprintf( frsp, "option osname='CC386+PMODE/W'\n"
					   "option stub=%s\n", libpath( "PMODEW.EXE" ) );
	else
		fprintf( frsp, "option osname='CC386+DOS/4GW'\n"
					   "option stub=%s\n", libpath( "D4GWSTUB.EXE" ) );
	fprintf( frsp, "name %s\n", name );

	if ( !_nodef_lib )
		fprintf( frsp, "file %s\n",
			libpath( ( _debug ? "C0DOSWD.OBJ" : "C0DOSW.OBJ" ) )  );

	ListIter( &f_obj, wout_obj );

	if ( !_nodef_lib )
		fprintf( frsp, "library %s", libpath( "CLDOS.LIB" )  );

	ListIter( &f_lib, wout_lib );
	fclose( frsp );
}

void wlink(void) {
//	wlink_rsp( LINKER_RSP );
	exec( WLINK, "@" LINKER_RSP );
}

void link(void) {
	if ( _link /* && ( f_obj.count > 0 || f_lib.count > 0 ) */ )
	{
		if ( _output == 'p' || _output == 'l' )
			tlink();
		else if ( _output == 'w' || _output == 'd' )
			wlink();
		else
			printf( "Unknow link!\n" );
		if ( !_keep_rsp )
			remove( LINKER_RSP );
//		if ( !_keep_gen )
//			ListIter( &f_obj, delete_temp );
//		ListDelete( &f_obj );
//		ListDelete( &f_lib );
	}
}

static void delete_file(Item *p) {
	if (p->temp)
		fprintf( frsp, "%s\n", p->data );
}

void delete_rsp( char *file ) {
	frsp = fopen( file, "wt" );
	if ( frsp == NULL )
		error( ERR_RUNTIME, file );
	ListIter( &f_asm, delete_file );
	ListIter( &f_obj, delete_file );
	fclose( frsp );
}

void response( void ) {
	if ( _compile && f_cpp.count > 0 ) {
		compile_rsp( COMPILER_RSP, COMPILER_OPT );
	}
	if ( _assemble && f_asm.count > 0 )	{
		assemble_rsp( ASSEMBLER_RSP, ASSEMBLER_OPT );
	}
	if ( _link && ( f_obj.count > 0 || f_lib.count > 0 ) ) {
		if ( _output == 'p' || _output == 'l' )
			tlink_rsp( LINKER_RSP );
		else if ( _output == 'w' || _output == 'd' )
			wlink_rsp( LINKER_RSP );
	}
	delete_rsp( DELETE_RSP );
}

void remove_temp( void ) {
	int len;
	char s[ _MAX_PATH ];

	if ( !_keep_gen ) {

		frsp = fopen( DELETE_RSP, "rt" );
		if ( frsp == NULL )
			error( ERR_RUNTIME, DELETE_RSP );

		while( fgets( s, _MAX_PATH, frsp ) != NULL ) {
			len = strlen( s );
			if ( len > 0 && s[ len - 1] == '\n' )
				s[ len - 1 ] = '\0';
			remove( s );
		}
		fclose( frsp );
	}
	if ( !_keep_rsp )
		remove( DELETE_RSP );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -