#ifdef	_WINDOWS
#line 3 "osdep/termout.gen"
#endif

/*
 * Generic tty output routines...
 */

/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 0 args

  Args:  x -- column position on the screen
         y -- row position on the screen
         line -- line of text to output

 Result: text is output
         cursor position is update
  ----*/
void
PutLine0(x, y, line)
    int            x,y;
    register char *line;
{
    MoveCursor(x,y);
    Write_to_screen(line);
}



/*----------------------------------------------------------------------
  Output line of length len to the display observing embedded attributes

 Args:  x      -- column position on the screen
        y      -- column position on the screen
        line   -- text to be output
        length -- length of text to be output

 Result: text is output
         cursor position is updated
  ----------------------------------------------------------------------*/
void
PutLine0n8b(x, y, line, length, handles)
    int            x, y, length;
    register char *line;
    HANDLE_S	  *handles;
{
    unsigned char c;
#ifdef	_WINDOWS
    int hkey = 0;
#endif

    MoveCursor(x,y);
    while(length-- && (c = (unsigned char)*line++)){

	if(c == (unsigned char)TAG_EMBED && length){
	    length--;
	    switch(*line++){
	      case TAG_INVON :
		StartInverse();
		break;
	      case TAG_INVOFF :
		EndInverse();
		break;
	      case TAG_BOLDON :
		StartBold();
		break;
	      case TAG_BOLDOFF :
		EndBold();
		break;
	      case TAG_ULINEON :
		StartUnderline();
		break;
	      case TAG_ULINEOFF :
		EndUnderline();
		break;
	      case TAG_HANDLE :
		length -= *line + 1;	/* key length plus length tag */
		if(handles){
		    int  key, n;

		    for(key = 0, n = *line++; n; n--) /* forget Horner? */
		      key = (key * 10) + (*line++ - '0');

#if	_WINDOWS
		    hkey = key;
#endif

		    if(key == handles->key){
			if(pico_usingcolor() &&
			   ps_global->VAR_SLCTBL_FORE_COLOR &&
			   ps_global->VAR_SLCTBL_BACK_COLOR){
			    pico_set_normal_color();
			}
			else
			  EndBold();

			StartInverse();
		    }
		}
		else{
		    /* BUG: complain? */
		    line += *line + 1;
		}

		break;
	      case TAG_FGCOLOR :
		if(length < RGBLEN){
		    Writechar(TAG_EMBED, 0);
		    Writechar(*(line-1), 0);
		    break;
		}

		(void)pico_set_fg_color(line);
		length -= RGBLEN;
		line += RGBLEN;
		break;
	      case TAG_BGCOLOR :
		if(length < RGBLEN){
		    Writechar(TAG_EMBED, 0);
		    Writechar(*(line-1), 0);
		    break;
		}

		(void)pico_set_bg_color(line);
		length -= RGBLEN;
		line += RGBLEN;
		break;
	      default :		/* literal "embed" char? */
		Writechar(TAG_EMBED, 0);
		Writechar(*(line-1), 0);
		break;
	    }					/* tag with handle, skip it */
	}	
	else if(c == '\033')			/* check for iso-2022 escape */
	  Writechar(c, match_escapes(line));
	else
	  Writechar(c, 0);
    }


#if	_WINDOWS_X
    if(hkey) {
	char *tmp_file = NULL, ext[32], mtype[128];
	HANDLE_S *h;
	extern HANDLE_S *get_handle (HANDLE_S *, int);

	if((h = get_handle(handles, hkey)) && h->type == Attach){
	    ext[0] = '\0';
	    strcpy(mtype, body_type_names(h->h.attach->body->type));
	    if (h->h.attach->body->subtype) {
		strcat (mtype, "/");
		strcat (mtype, h->h.attach->body->subtype);
	    }

	    if(!set_mime_extension_by_type(ext, mtype)){
		char *namep, *dotp, *p;

		if(namep = rfc2231_get_param(h->h.attach->body->parameter,
					     "name", NULL, NULL)){
		    for(dotp = NULL, p = namep; *p; p++)
		      if(*p == '.')
			dotp = p + 1;

		    if(dotp && strlen(dotp) < sizeof(ext) - 1)
		      strcpy(ext, dotp);

		    fs_give((void **) &namep);
		}
	    }

	    if(ext[0] && (tmp_file = temp_nam_ext(NULL, "im", ext))){
		FILE *f = fopen(tmp_file, "w");

		mswin_registericon(x, h->key, tmp_file);

		fclose(f);
		unlink(tmp_file);
		fs_give((void **)&tmp_file);
	    }
	}
    }
#endif
}


/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 1 arg

 Input:  position on the screen
         line of text to output

 Result: text is output
         cursor position is update
  ----------------------------------------------------------------------*/
void
/*VARARGS2*/
PutLine1(x, y, line, arg1)
    int   x, y;
    char *line;
    void *arg1;
{
    char buffer[PUTLINE_BUFLEN];

    sprintf(buffer, line, arg1);
    PutLine0(x, y, buffer);
}


/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 2 args

 Input:  position on the screen
         line of text to output

 Result: text is output
         cursor position is update
  ----------------------------------------------------------------------*/
void
/*VARARGS3*/
PutLine2(x, y, line, arg1, arg2)
    int   x, y;
    char *line;
    void *arg1, *arg2;
{
    char buffer[PUTLINE_BUFLEN];

    sprintf(buffer, line, arg1, arg2);
    PutLine0(x, y, buffer);
}


/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 3 args

 Input:  position on the screen
         line of text to output

 Result: text is output
         cursor position is update
  ----------------------------------------------------------------------*/
void
/*VARARGS4*/
PutLine3(x, y, line, arg1, arg2, arg3)
    int   x, y;
    char *line;
    void *arg1, *arg2, *arg3;
{
    char buffer[PUTLINE_BUFLEN];

    sprintf(buffer, line, arg1, arg2, arg3);
    PutLine0(x, y, buffer);
}


/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 4 args

 Args:  x -- column position on the screen
        y -- column position on the screen
        line -- printf style line of text to output

 Result: text is output
         cursor position is update
  ----------------------------------------------------------------------*/
void
/*VARARGS5*/
PutLine4(x, y, line, arg1, arg2, arg3, arg4)
     int   x, y;
     char *line;
     void *arg1, *arg2, *arg3, *arg4;
{
    char buffer[PUTLINE_BUFLEN];

    sprintf(buffer, line, arg1, arg2, arg3, arg4);
    PutLine0(x, y, buffer);
}



/*----------------------------------------------------------------------
      Printf style output line to the screen at given position, 5 args

 Args:  x -- column position on the screen
        y -- column position on the screen
        line -- printf style line of text to output

 Result: text is output
         cursor position is update
  ----------------------------------------------------------------------*/
void
/*VARARGS6*/
PutLine5(x, y, line, arg1, arg2, arg3, arg4, arg5)
     int   x, y;
     char *line;
     void *arg1, *arg2, *arg3, *arg4, *arg5;
{
    char buffer[PUTLINE_BUFLEN];

    sprintf(buffer, line, arg1, arg2, arg3, arg4, arg5);
    PutLine0(x, y, buffer);
}



/*----------------------------------------------------------------------
       Output a line to the screen, centered

  Input:  Line number to print on, string to output
  
 Result:  String is output to screen
          Returns column number line is output on
  ----------------------------------------------------------------------*/
int
Centerline(line, string)
    int   line;
    char *string;
{
    register int length, col;

    length = strlen(string);

    if (length > ps_global->ttyo->screen_cols)
      col = 0;
    else
      col = (ps_global->ttyo->screen_cols - length) / 2;

    PutLine0(line, col, string);
    return(col);
}



/*----------------------------------------------------------------------
     Clear specified line on the screen

 Result: The line is blanked and the cursor is left at column 0.

  ----*/
void
ClearLine(n)
    int n;
{
    if(ps_global->in_init_seq)
      return;

    MoveCursor(n, 0);
    CleartoEOLN();
}



/*----------------------------------------------------------------------
     Clear specified lines on the screen

 Result: The lines starting at 'x' and ending at 'y' are blanked
	 and the cursor is left at row 'x', column 0

  ----*/
void
ClearLines(x, y)
    int x, y;
{
    int i;

    for(i = x; i <= y; i++)
      ClearLine(i);

    MoveCursor(x, 0);
}



/*----------------------------------------------------------------------
    Indicate to the screen painting here that the position of the cursor
 has been disturbed and isn't where these functions might think.
 ----*/
void
clear_cursor_pos()
{
    _line = FARAWAY;
    _col  = FARAWAY;
}


