summaryrefslogtreecommitdiff
path: root/alpine/osdep/termout.gen.c
blob: ef50aa5a8cf98b24ab71348fcc7663fd7157aa4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: termout.gen.c 1012 2008-03-26 00:44:22Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2008 University of Washington
 * Copyright 2013-2020 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */
#include <system.h>
#include <general.h>

#include "../../c-client/mail.h"	/* for MAILSTREAM and friends */
#include "../../c-client/osdep.h"
#include "../../c-client/rfc822.h"	/* for soutr_t and such */
#include "../../c-client/misc.h"	/* for cpystr proto */
#include "../../c-client/utf8.h"	/* for CHARSET and such*/
#include "../../c-client/imap4r1.h"

#include "../../pith/osdep/color.h"
#include "../../pith/charconv/filesys.h"

#include "../../pith/debug.h"
#include "../../pith/newmail.h"
#include "../../pith/filter.h"
#include "../../pith/handle.h"
#include "../../pith/conf.h"
#include "../../pith/mimedesc.h"

#include "../../pico/estruct.h"
#include "../../pico/pico.h"
#include "../../pico/keydefs.h"
#include "../../pico/osdep/color.h"

#include "../status.h"
#include "../radio.h"
#include "../folder.h"
#include "../keymenu.h"
#include "../send.h"
#include "../mailindx.h"

int   _line  = FARAWAY;
int   _col   = FARAWAY;

#include "termout.gen.h"


#define	PUTLINE_BUFLEN	256



/*
 * 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(int x, int 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(int x, int y, register char *line, int length, HANDLE_S *handles)
{
    unsigned char c;
    int is_inv = 0, is_bold = 0, is_uline = 0, is_fg = 0, is_bg = 0;
#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();
		is_inv = 1;
		break;

	      case TAG_INVOFF :
		EndInverse();
		is_inv = 0;
		break;

	      case TAG_BOLDON :
		StartBold();
		is_bold = 1;
		break;

	      case TAG_BOLDOFF :
		EndBold();
		is_bold = 0;
		break;

	      case TAG_ITALICON : /* express italic as uline in terminal */
	      case TAG_ULINEON :
		StartUnderline();
		is_uline = 1;
		break;

	      case TAG_ITALICOFF : /* express italic as uline in terminal */
	      case TAG_ULINEOFF :
		EndUnderline();
		is_uline = 0;
		break;

	      case TAG_HANDLE :
		length -= *line + 1;	/* key length plus length tag */
		if(handles){
		    int  key, n, current_key = 0;

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

#if	_WINDOWS
		    hkey = key;
#endif

		    if(handles->using_is_used){
			HANDLE_S *h;

			for(h = handles; h; h = h->next)
			  if(h->is_used)
			    break;
			
			if(h)
			  current_key = h->key;
		    }
		    else
		      current_key = handles->key;

		    if(key == current_key){
			COLOR_PAIR *curcolor = NULL;
			COLOR_PAIR *revcolor = NULL;

			if(handles->color_unseen
			   && (curcolor = pico_get_cur_color())
			   && (colorcmp(curcolor->fg, ps_global->VAR_NORM_FORE_COLOR)
			       || colorcmp(curcolor->bg, ps_global->VAR_NORM_BACK_COLOR))
			   && (revcolor = apply_rev_color(curcolor,
							  ps_global->index_color_style)))
			  (void) pico_set_colorp(revcolor, PSC_NONE);
			else{

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

			    StartInverse();
			    is_inv = 1;
			}

			if(curcolor)
			  free_color_pair(&curcolor);

			if(revcolor)
			  free_color_pair(&revcolor);
		    }
		}
		else{
		    /* BUG: complain? */
		    line += *line + 1;
		}

		break;

	      case TAG_FGCOLOR :
		if(length < RGBLEN){
		    dprint((9,
			       "FGCOLOR not proper length, ignoring\n"));
		    length = 0;
		    break;
		}

		(void)pico_set_fg_color(line);
		is_fg = 1;
		length -= RGBLEN;
		line += RGBLEN;
		break;

	      case TAG_BGCOLOR :
		if(length < RGBLEN){
		    dprint((9,
			       "BGCOLOR not proper length, ignoring\n"));
		    length = 0;
		    break;
		}

		(void)pico_set_bg_color(line);
		is_bg = 1;
		length -= RGBLEN;
		line += RGBLEN;
		break;

	      case TAG_EMBED:			/* literal "embed" char */
		Writechar(TAG_EMBED, 0);
		break;

	      case TAG_STRIKEON :		/* unsupported text markup */
	      case TAG_STRIKEOFF :
	      case TAG_BIGON :
	      case TAG_BIGOFF :
	      case TAG_SMALLON :
	      case TAG_SMALLOFF :
	      default :				/* Eat unrecognized tag - TAG_BIGON, etc */
		break;
	    }					/* tag with handle, skip it */
	}	
	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';
	    strncpy(mtype, body_type_names(h->h.attach->body->type), sizeof(mtype));
	    mtype[sizeof(mtype)-1] = '\0';
	    if (h->h.attach->body->subtype) {
		strncat (mtype, "/", sizeof(mtype)-strlen(mtype)-1);
		mtype[sizeof(mtype)-1] = '\0';
		strncat (mtype, h->h.attach->body->subtype, sizeof(mtype)-strlen(mtype)-1);
		mtype[sizeof(mtype)-1] = '\0';
	    }

	    if(!set_mime_extension_by_type(ext, mtype)){
		char *p, *extp = NULL;

		if((p = get_filename_parameter(NULL, 0, h->h.attach->body, &extp)) != NULL){
		    if(extp){
			strncpy(ext, extp, sizeof(ext));
			ext[sizeof(ext)-1] = '\0';
		    }

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

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

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

		fclose(f);
		our_unlink(tmp_file);
		fs_give((void **)&tmp_file);
	    }
	}
    }
#endif
    if(is_inv){
	dprint((9,
		   "INVERSE left on at end of line, turning off now\n"));
	EndInverse();
    }
    if(is_bold){
	dprint((9,
		   "BOLD left on at end of line, turning off now\n"));
	EndBold();
    }
    if(is_uline){
	dprint((9,
		   "UNDERLINE left on at end of line, turning off now\n"));
	EndUnderline();
    }
    if(is_fg || is_bg)
      pico_set_normal_color();

}


/*----------------------------------------------------------------------
      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(int x, int y, char *line, void *arg1)
{
    char buffer[PUTLINE_BUFLEN];

    snprintf(buffer, sizeof(buffer), line, arg1);
    buffer[sizeof(buffer)-1] = '\0';
    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(int x, int y, char *line, void *arg1, void *arg2)
{
    char buffer[PUTLINE_BUFLEN];

    snprintf(buffer, sizeof(buffer), line, arg1, arg2);
    buffer[sizeof(buffer)-1] = '\0';
    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(int x, int y, char *line, void *arg1, void *arg2, void *arg3)
{
    char buffer[PUTLINE_BUFLEN];

    snprintf(buffer, sizeof(buffer), line, arg1, arg2, arg3);
    buffer[sizeof(buffer)-1] = '\0';
    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(int x, int y, char *line, void *arg1, void *arg2, void *arg3, void *arg4)
{
    char buffer[PUTLINE_BUFLEN];

    snprintf(buffer, sizeof(buffer), line, arg1, arg2, arg3, arg4);
    buffer[sizeof(buffer)-1] = '\0';
    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(int x, int y, char *line, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5)
{
    char buffer[PUTLINE_BUFLEN];

    snprintf(buffer, sizeof(buffer), line, arg1, arg2, arg3, arg4, arg5);
    buffer[sizeof(buffer)-1] = '\0';
    PutLine0(x, y, buffer);
}


/*----------------------------------------------------------------------
       Output a UTF-8 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(int line, char *string)
{
    int width, col;

    width = (int) utf8_width(string);

    if (width > ps_global->ttyo->screen_cols)
      col = 0;
    else
      col = (ps_global->ttyo->screen_cols - width) / 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(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(int x, int 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(void)
{
    _line = FARAWAY;
    _col  = FARAWAY;
}


/*----------------------------------------------------------------------
    Write a character to the screen, keeping track of cursor position

   Args: ch -- character to output. The stream of characters coming to
               this function is expected to be UTF-8. State is kept  between
	       calls in order to collect up the octets needed for a single
	       Unicode character.

 Result: character output
         cursor position variables updated
  ----*/
void
Writechar(unsigned int ch, int new_esc_len)
{
    static   unsigned char  cbuf[6];
    static   unsigned char *cbufp = cbuf;

    if(cbufp < cbuf+sizeof(cbuf)){
	unsigned char *inputp;
	unsigned long remaining_octets;
	UCS ucs;

	*cbufp++ = (unsigned char) ch;
	inputp = cbuf;
	remaining_octets = (cbufp - cbuf) * sizeof(unsigned char);
	ucs = (UCS) utf8_get(&inputp, &remaining_octets);

	switch(ucs){
	  case U8G_ENDSTRG:	/* incomplete character, wait */
	  case U8G_ENDSTRI:	/* incomplete character, wait */
	    break;

	  default:
	    if(ucs & U8G_ERROR || ucs == UBOGON){
		/*
		 * None of these cases is supposed to happen. If it
		 * does happen then the input stream isn't UTF-8
		 * so something is wrong. Treat each character in the
		 * input buffer as a separate error character and
		 * print a '?' for each.
		 */
		for(inputp = cbuf; inputp < cbufp; inputp++){
		    int width = 0;

		    if(_col + width <= ps_global->ttyo->screen_cols){
			Writewchar('?');
			width++;
		    }
		}

		cbufp = cbuf;	/* start over */
	    }
	    else{

		/* got a good character */
		Writewchar(ucs);

		/* update the input buffer */
		if(inputp >= cbufp)	/* this should be the case */
		  cbufp = cbuf;
		else{		/* extra chars for some reason? */
		    unsigned char *q, *newcbufp;

		    newcbufp = (cbufp - inputp) + cbuf;
		    q = cbuf;
		    while(inputp < cbufp)
		      *q++ = *inputp++;

		    cbufp = newcbufp;
		}
	    }

	    break;
	}
    }
    else{			/* error */
	Writewchar('?');
	cbufp = cbuf;		/* start over */
    }
}