File: splits.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 (456 lines) | stat: -rw-r--r-- 8,472 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
/*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 "splits.h"

/*
 * TODO. Oh yeah, there's bugs in here.
 */
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
splits_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 TBLCOL	Col[] =
{
	{  0, 99999},
	{  0, 11 },	//Company
	{ 13, 18 },	//Symbol
	{ 20, 24 },	//Ratio
	{ 26, 32 },	//Payable
	{ 34, 40 },	//Ex-Date
	{ 42, 50 },	//Announced
	{ 52, 54 },	//Optionable
	{ 56, 78 },
	{ 0, 99999 }
};
#define	NUMCOL	asizeof(Col)

static int	Nstock0;

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

	// Add live quotes and add it to stocklist 0
	if (dolive && (wlen-0) < SYMLEN && *tblcolp == 2)
	{
		x = getcurx(Subwin);
		y = getcury(Subwin);

		if (strcmp(wbuf, "Symbol"))
		{
			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, "Splits");
			++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 (*tblcolp == 3 && wlen < COMLEN && strcmp(wbuf, "Ratio"))
	{
		if (List0Comments[0])
			strcat(List0Comments, "\n");
		strcat(List0Comments, wbuf);
		strcat(List0Comments, " ");
	}
	if (*tblcolp == 4 && wlen < COMLEN && strcmp(wbuf, "Payable"))
	{
		strcat(List0Comments, wbuf);
	}
}

static void
get_splits(void)
{
	FILE	*fp;
	int	y;
	char	buf[BUFSIZ];
	char	*url = get_rc_value(RcFile, "splits_URL");

	sprintf(buf, "%s \"%s\"", SUCKURL, url);

	fp = popen(buf, "r");
	if (!fp)
	{
		mvwprintw(Subwin, getmaxy(Subwin)/2, 20,
				"Can't access splits");
		touchwin(Win);
		return;
	}

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

	/*
	 * Parse HTML
	 */
	Nstock0 = 0;
	List0[0] = 0;
	List0Comments[0] = 0;
	for (y = 0; y < PADLINES; ++y)
		Live[y].sym[0] = 0;

	minihtml_parse(Subwin, fp, Col, NUMCOL, MHP_DEFAULT,
			NULL, postwordhook, NULL, NULL);

	pclose(fp);

	if (List0Comments[0])
		strcat(List0Comments, "\n");

	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
splits_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 splits window\n");

	cols = getmaxx(Win);

	wbkgd(Win, Reverse ? A_REVERSE : A_NORMAL);

	box(Win, 0, 0);
	mvwcenter(Win, 0, "Briefing.com Splits 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 splits 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;

	// Paint empty frame to give user some feedback
	clearok(Win, TRUE);
	update_panels(); doupdate();

	Sr = sr;
	get_splits();
}

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

int
splits_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 'g':
		all_charts_from_list("-i", List0);
		break;
	case 'G':
		all_charts_from_list("", List0);
		break;

	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);
}