File: quiet.c

package info (click to toggle)
linuxtrade 3.65-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,880 kB
  • ctags: 1,969
  • sloc: ansic: 30,091; sh: 2,278; perl: 566; makefile: 126
file content (538 lines) | stat: -rw-r--r-- 10,062 bytes parent folder | download | duplicates (2)
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
/*b
 * Copyright (C) 2001,2002  Rick Richardson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Rick Richardson <rickr@mn.rr.com>
b*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ncurses.h>
#include <panel.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <ctype.h>
#include <pthread.h>
#include "error.h"
#include "rc.h"
#include "streamer.h"
#include "curse.h"
#include "linuxtrade.h"
#include "minihtml.h"
#include "quiet.h"

/*
 * This is very hacked, do not use as a prototype.  Try splits.c instead.
 */
static WINDOW	*Win;
static WINDOW	*Subwin;
static PANEL	*Panel;

static int	Cursor;		// Article line number at top of display
static int	MaxCursor;	// Maximum value for above
static int	NumLines;	// Number of lines in the article
static time_t	LastLivePoll;	// Last time we grabbed the live quotes
static STREAMER	Sr = NULL;
static int	ValLivePoll;

#define	PADLINES	700

typedef struct
{
	char	sym[SYMLEN+1];
	int	x;
} QUOTELOC;

static QUOTELOC	Live[PADLINES];

static void
get_live_quotes(void)
{
	int	y;
	int	cnt = 0;

	for (y = Cursor; y < Cursor+getmaxy(Win)-2; ++y)
		if (Live[y].sym[0])
		{
			(Sr->send_livequote)(Sr, Live[y].sym);
			++cnt;
		}
	if (cnt)
		(Sr->send_livequote_end)(Sr);

	time(&LastLivePoll);
}

void
quiet_livequote(LIVEQUOTE *lq)
{
	int	y;

	if (!Win)
		return;

	for (y = Cursor; y < Cursor+getmaxy(Win)-2; ++y)
	{
		attr_t	attr;

		if (strcmp(lq->sym, Live[y].sym))
			continue;

		if (lq->last > lq->close)
			attr = COLOR_PAIR(1) | A_NORMAL;
		else if (lq->last < lq->close)
			attr = COLOR_PAIR(2) | A_NORMAL;
		else
			attr = A_NORMAL;

		wattrset(Subwin, attr);
		mvwprintw(Subwin, y, Live[y].x,
			"%7.2f %+7.2f", lq->last, lq->last - lq->close);
		wattrset(Subwin, A_NORMAL);
	}

	// TODO: optimize this to do 1 line and move into loop
	copywin(Subwin, Win,
			Cursor, 0,
			1, 1,
			getmaxy(Win)-2, getmaxx(Win)-2,
			FALSE);
	move(LINES-1, CursorX);
	update_panels();
	doupdate();
}

//
// TODO: make this asynchronous
//

static void
display_more(void)
{
	mvwaddch(Win, 1, getmaxx(Win)-1,
		Cursor ? ACS_UARROW : ACS_VLINE);

	mvwaddch(Win, getmaxy(Win)-2, getmaxx(Win)-1,
		(Cursor < MaxCursor) ? ACS_DARROW : ACS_VLINE);
}

static int
fixdate(char *buf)
{
	int	rc;
	int	mm, dd, yy;

	rc = sscanf(buf, "%d/%d/%d", &mm, &dd, &yy);
	if (rc == 3)
		sprintf(buf, "%02d/%02d/%02d", mm, dd, yy);

	return strlen(buf);
}

static TBLCOL	Col[] =
{
	{  0, 99999},
	{  0, 8 },	//Quiet Period Ends
	{ 10, 17 },	//Trading
	{ 19, 45},	//Company
	{ 47, 52 },	//Symbol (pseudo column created by parser)
	{ 54, 62 },	//Offering Price
	{ 64, 63 },	//Closing Price
	{ 64, 78 },
	{ 0, 99999 }
};
#define	NUMCOL	asizeof(Col)

static int	Nstock0;
static int	Paren;

static int
prewordhook(int *tblcolp, char *wbuf, int wlen)
{
	int	x, y;
	int	tblcol = *tblcolp;
	int	dolive = ValLivePoll > 1;

	if (tblcol == 6 && strcmp(wbuf, " Closing Price ") == 0)
		return 0;
	if (tblcol == 1 || tblcol == 2)
		wlen = fixdate(wbuf);

	if (Paren)
	{
		if (strcmp(wbuf, ")") == 0)
		{
			Paren = 0;
			return 0;
		}
		else if (dolive && (wlen-0) < SYMLEN)
		{
			x = getcurx(Subwin);
			y = getcury(Subwin);

			memcpy(Live[y].sym, wbuf, wlen);
			Live[y].sym[wlen] = 0;
			Live[y].x = getmaxx(Subwin) - 15;

			if (Nstock0)
				strcat(List0, " ");
			strcat(List0, Live[y].sym);
			strcpy(StockList0Name, "Quiet Period");
			++Nstock0;

			if (Nstock0 == 1)
			{
				wattrset(Subwin, A_BOLD);
				mvwprintw(Subwin,
					y-2, getmaxx(Subwin)-15,
					"   Live Quotes ");
				mvwprintw(Subwin,
					y-1, getmaxx(Subwin)-15,
					"  Price  Change");
				wattrset(Subwin, A_NORMAL);
				wmove(Subwin, y, x);
			}
		}
	}

	if (tblcol == 3 && strcmp(wbuf, " (") == 0)
	{
		Paren = 1;
		*tblcolp = ++tblcol;
		while (getcurx(Subwin) < Col[tblcol].sx)
			waddch(Subwin, ' ');
		return 0;
	}

	if (tblcol == 3 && strcmp(wbuf, "Company (Symbol) ") == 0)
	{
		waddstr(Subwin, "Company");
		*tblcolp = ++tblcol;
		while (getcurx(Subwin) < Col[tblcol].sx)
			waddch(Subwin, ' ');
		waddstr(Subwin, "Symbol");
		return 0;
	}

	if (tblcol == 1 && (strcmp(wbuf, "  Any") == 0
		|| strcmp(wbuf, " Companies") == 0
		|| strcmp(wbuf, "Printed") == 0) )
	{
		*tblcolp = tblcol = 0; x = 0;
		wmove(Subwin, getcury(Subwin), x);
	}
	else if (tblcol == 1 && strcmp(wbuf, " Quiet Period ") == 0)
	{
		strcpy(wbuf, "Quiet Ends");
		wattrset(Subwin, A_BOLD);
	}
	else if (tblcol == 1 && strcmp(wbuf, " End Date ") == 0)
		strcpy(wbuf, "");
	else if (tblcol == 1)
	{
		if (wlen < COMLEN)
		{
			strcat(List0Comments, "QP til ");
			strcat(List0Comments, wbuf);
		}
	}
	if (tblcol == 5 && strchr(wbuf, '$') && wlen < COMLEN)
	{
		strcat(List0Comments, wbuf);
		strcat(List0Comments, "\n");
	}

	return wlen;
}

static void
postwordhook(int *tblcolp, char *wbuf,int wlen)
{
	if (*tblcolp == 5 && strcmp(wbuf, " Offering ") == 0)
	{
		wattrset(Subwin, A_NORMAL);
	}
}

static void
get_quiet(void)
{
	int	y;
	FILE	*fp;
	char	buf[BUFSIZ];
	int	i;
	char	*url = get_rc_value(RcFile, "quiet_URL");
	static int	adjust = 1;

	// Paint empty frame to give user some feedback
	mvwcenter(Win, 4, "*** Please wait, fetching data ***");
	clearok(Win, TRUE);
	update_panels(); doupdate();
	mvwcenter(Subwin, 3, "*** No Data Available ***");

	sprintf(buf, "%s \"%s\"", SUCKURL, url);
	if (Debug)
		fprintf(stderr, "URL: <%s>\n", url);

	fp = popen(buf, "r");
	if (!fp)
	{
		mvwcenter(Win, 4, "   *** Can't access quiet period ***   ");
		touchwin(Win);
		return;
	}

	/*
	 * Eat lines until we see the table
	 */
	if (0)
	minihtml_skip_past_line(fp, "<P>Splits announced");

	/*
	 * Fix columns for narrow displays
	 */
	if (COLS < 80 && adjust)
	{
		Col[3].ex -= (80-COLS);
		for (i = 4; i <= 7; ++i)
		{
			Col[i].sx -= (80-COLS);
			Col[i].ex -= (80-COLS);
		}
		adjust = 0;
	}

	/*
	 * Parse HTML
	 */
	Nstock0 = 0;
	List0[0] = 0;
	List0Comments[0] = 0;
	Paren = 0;

	for (y = 0; y < PADLINES; ++y)
		Live[y].sym[0] = 0;

	minihtml_parse(Subwin, fp, Col, NUMCOL, MHP_EATBLANK,
			prewordhook, postwordhook, NULL, NULL);

	pclose(fp);

	Cursor = 0;
	NumLines = getcury(Subwin) + 1;
	MaxCursor = NumLines - (getmaxy(Win) - 2);
	if (MaxCursor < 0)
		MaxCursor = 0;

	display_more();

	copywin(Subwin, Win,
			Cursor, 0,
			1, 1,
			getmaxy(Win)-2, getmaxx(Win)-2,
			FALSE);
	move(LINES-1, CursorX);
	update_panels();
	doupdate();
	get_live_quotes();
}

void
quiet_popup(STREAMER sr)
{
	int	n;
	int	cols;

	// Clear alert, if any
	mvprintw(1, 7, "      ");

	n = LINES - 4 - 2 - NumStock - 12;
	if (n < 24)
		n = 24;
	Win = bestwin(n);
	if (!Win)
		error(1, "Can't create quiet window\n");

	cols = getmaxx(Win);

	wbkgd(Win, Reverse ? A_REVERSE : A_NORMAL);

	box(Win, 0, 0);
	mvwcenter(Win, 0, "Quiet Period Calendar");

	//
	// Turns out, this works best for most news articles and an 80
	// column xterm. We will clip columns 78 and 79, but there are
	// only blanks there in the news articles I've seen.  For smaller
	// displays, we are screwed anyway.
	//
	Subwin = newpad(PADLINES, cols - 2);
	if (!Subwin)
		error(1, "Can't create quiet pad\n");
	wbkgd(Subwin, Reverse ? A_REVERSE : A_NORMAL);

	Panel = new_panel(Win);

	//
	// Lots of stocks on this list, limit poll period
	//
	ValLivePoll = atoi(get_rc_value(RcFile, "live_quotes"));
	if (ValLivePoll && ValLivePoll < 10)
		ValLivePoll = 10;

	Sr = sr;
	get_quiet();
}

static void
popdown(void)
{
	hide_panel(Panel);
	update_panels();
	del_panel(Panel);
	delwin(Subwin);
	delwin(Win);
	Win = 0;
	Subwin = 0;
	Sr = NULL;
}

int
quiet_command(int c, STREAMER sr)
{
	MEVENT	m;

	switch (c)
	{
	case '\f':
		move(LINES-1, CursorX);
		wrefresh(curscr);
		return -1;
	case 'j':
	case KEY_DOWN:
		if (++Cursor > MaxCursor)
		{
			--Cursor;
			beep();
			break;
		}
		break;
	case 'k':
	case KEY_UP:
		if (--Cursor < 0)
		{
			++Cursor;
			beep();
			break;
		}
		break;
	case '-':
	case KEY_PPAGE:
		if (Cursor == 0)
		{
			beep();
			break;
		}
		Cursor -= getmaxy(Win) - 2 - 1;
		if (Cursor < 0)
			Cursor = 0;
		break;
	case '+':
	case ' ':
	case KEY_NPAGE:
		if (Cursor == MaxCursor)
		{
			beep();
			break;
		}
		Cursor += getmaxy(Win) - 2 - 1;
		if (Cursor > MaxCursor)
			Cursor = MaxCursor;
		break;
	case '0':
	case KEY_HOME:
		Cursor = 0;
		break;
	case '$':
	case KEY_END:
		Cursor = MaxCursor;
		break;

	case KEY_F(11):
		print_rect_troff(getbegy(Win), getbegx(Win),
				getmaxy(Win), getmaxx(Win),
				NULL, "screen.tr");
		break;
	case KEY_F(12):
	case CTRL('P'):
	case KEY_PRINT:
		print_window(Subwin, NumLines,
				get_rc_value(RcFile, "print_cmd"));
		break;

		// Change stocklist on main screen
	case '1': case '2': case '3': case '4': case '5':
	case '6': case '7': case '8': case '9':
		return 3;

	case KEY_MOUSE:
		if (getmouse(&m) != OK)
			break;

		// Ignore clicks in our window
		if (m.y >= getbegy(Win)
			&& m.y < getbegy(Win) + getmaxy(Win))
			break;

		// popdown and reprocess clicks in main window
		if (ungetmouse(&m) == OK)
			Ungetch = 1;
		popdown();
		return 2;

		// Quick switches to another popup
	case 'p':
	case 'e':
		popdown();
		return 1;

		// Regular exit
	case 033:
	case 'q':
	case 'x':
		popdown();
		return 2;
	default:
		beep();
		break;
	}

	display_more();
	copywin(Subwin, Win,
			Cursor, 0,
			1, 1,
			getmaxy(Win)-2, getmaxx(Win)-2,
			FALSE);
	move(LINES-1, CursorX);
	update_panels();
	doupdate();
	get_live_quotes();

	return (-1);
}