FreeBSD/usr.sbin/vidcontrol/vidcontrol.c
/*
 * Load a font from file and set it.
 */
Load a font from file and set it.
Load a font from file and set it .
static void
load_font(const char *type, const char *filename)
{
	FILE	*fd;
	int	h, i, size, w;
	unsigned long io = 0;	
	char	*name, *fontmap, size_sufx[6];
	const char	*a[] = {"", FONT_PATH, NULL};
	const char	*vt4a[] = {"", VT_FONT_PATH, NULL};
	const char	*b[] = {filename, NULL};
	const char	*c[] = {"", size_sufx, NULL};
	const char	*d[] = {"", ".fnt", NULL};
	vid_info_t _info;
	struct sizeinfo {
		int w;
		int h;
		unsigned long io;
	} sizes[] = {{8, 16, PIO_FONT8x16},
		     {8, 14, PIO_FONT8x14},
		     {8,  8,  PIO_FONT8x8},
		     {0,  0,            0}};
	if (vt4_mode) {
		size_sufx[0] = '\0';
	} else {
		_info.size = sizeof(_info);
		if (ioctl(0, CONS_GETINFO, &_info) == -1) {
			revert();
			warn("failed to obtain current video mode parameters");
			return;
		}
		snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size);
	}
	fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
	if (fd == NULL) {
		revert();
		errx(1, "%s: can't load font file", filename);
	}
	if (vt4_mode) {
		if(load_vt4font(fd))
			warn("failed to load font \"%s\"", filename);
		fclose(fd);
		return;
	}
	if (type != NULL) {
		size = 0;
		if (sscanf(type, "%dx%d", &w, &h) == 2) {
			for (i = 0; sizes[i].w != 0; i++) {
				if (sizes[i].w == w && sizes[i].h == h) {
					size = DATASIZE(sizes[i]);
					io = sizes[i].io;
					font_height = sizes[i].h;
				}
			}
		}
		if (size == 0) {
			fclose(fd);
			revert();
			errx(1, "%s: bad font size specification", type);
		}
	} else {
		int j;
		int dsize[2];
		size = DATASIZE(sizes[0]);
		fontmap = (char*) malloc(size);
		dsize[0] = decode(fd, fontmap, size);
		dsize[1] = fsize(fd);
		free(fontmap);
		size = 0;
		for (j = 0; j < 2; j++) {
			for (i = 0; sizes[i].w != 0; i++) {
				if (DATASIZE(sizes[i]) == dsize[j]) {
					size = dsize[j];
					io = sizes[i].io;
					font_height = sizes[i].h;
					j = 2;	
					break;
				}
			}
		}
		if (size == 0) {
			fclose(fd);
			revert();
			errx(1, "%s: can't guess font size", filename);
		}
		rewind(fd);
	}
	fontmap = (char*) malloc(size);
	if (decode(fd, fontmap, size) != size) {
		rewind(fd);
		if (fsize(fd) != size ||
		    fread(fontmap, 1, size, fd) != (size_t)size) {
			warnx("%s: bad font file", filename);
			fclose(fd);
			free(fontmap);
			revert();
			errx(1, "%s: bad font file", filename);
		}
	}
	if (ioctl(0, io, fontmap) == -1) {
		revert();
		errc(1, errno, "loading font");
	}
	fclose(fd);
	free(fontmap);
}
static void load_font ( const char * type , const char * filename ) { FILE * fd ; int h , i , size , w ; unsigned long io = 0 ; char * name , * fontmap , size_sufx [ 6 ] ; const char * a [ ] = { " " , FONT_PATH , NULL } ; const char * vt4a [ ] = { " " , VT_FONT_PATH , NULL } ; const char * b [ ] = { filename , NULL } ; const char * c [ ] = { " " , size_sufx , NULL } ; const char * d [ ] = { " " , " " , NULL } ; vid_info_t _info ; struct sizeinfo { int w ; int h ; unsigned long io ; } sizes [ ] = { { 8 , 16 , PIO_FONT8x16 } , { 8 , 14 , PIO_FONT8x14 } , { 8 , 8 , PIO_FONT8x8 } , { 0 , 0 , 0 } } ; if ( vt4_mode ) { size_sufx [ 0 ] = ' \0 ' ; } else { _info . size = sizeof ( _info ) ; if ( ioctl ( 0 , CONS_GETINFO , & _info ) == -1 ) { revert ( ) ; warn ( " " ) ; return ; } snprintf ( size_sufx , sizeof ( size_sufx ) , " " , _info . font_size ) ; } fd = openguess ( ( vt4_mode == 0 ) ? a : vt4a , b , c , d , & name ) ; if ( fd == NULL ) { revert ( ) ; errx ( 1 , " " , filename ) ; } if ( vt4_mode ) { if ( load_vt4font ( fd ) ) warn ( " \" \" " , filename ) ; fclose ( fd ) ; return ; } if ( type != NULL ) { size = 0 ; if ( sscanf ( type , " " , & w , & h ) == 2 ) { for ( i = 0 ; sizes [ i ] . w != 0 ; i ++ ) { if ( sizes [ i ] . w == w && sizes [ i ] . h == h ) { size = DATASIZE ( sizes [ i ] ) ; io = sizes [ i ] . io ; font_height = sizes [ i ] . h ; } } } if ( size == 0 ) { fclose ( fd ) ; revert ( ) ; errx ( 1 , " " , type ) ; } } else { int j ; int dsize [ 2 ] ; size = DATASIZE ( sizes [ 0 ] ) ; fontmap = ( char * ) malloc ( size ) ; dsize [ 0 ] = decode ( fd , fontmap , size ) ; dsize [ 1 ] = fsize ( fd ) ; free ( fontmap ) ; size = 0 ; for ( j = 0 ; j < 2 ; j ++ ) { for ( i = 0 ; sizes [ i ] . w != 0 ; i ++ ) { if ( DATASIZE ( sizes [ i ] ) == dsize [ j ] ) { size = dsize [ j ] ; io = sizes [ i ] . io ; font_height = sizes [ i ] . h ; j = 2 ; break ; } } } if ( size == 0 ) { fclose ( fd ) ; revert ( ) ; errx ( 1 , " " , filename ) ; } rewind ( fd ) ; } fontmap = ( char * ) malloc ( size ) ; if ( decode ( fd , fontmap , size ) != size ) { rewind ( fd ) ; if ( fsize ( fd ) != size || fread ( fontmap , 1 , size , fd ) != ( size_t ) size ) { warnx ( " " , filename ) ; fclose ( fd ) ; free ( fontmap ) ; revert ( ) ; errx ( 1 , " " , filename ) ; } } if ( ioctl ( 0 , io , fontmap ) == -1 ) { revert ( ) ; errc ( 1 , errno , " " ) ; } fclose ( fd ) ; free ( fontmap ) ; }
Load a font from file and set it.
Load a font from file and set it .
/* silence stupid gcc(1) in the Wall mode */ /* Apply heuristics */ /* XXX */