File: main.c

package info (click to toggle)
alevt 1:1.6.2-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 662
  • sloc: ansic: 6,284; makefile: 119; perl: 104; sh: 15
file content (255 lines) | stat: -rw-r--r-- 5,487 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "vt.h"
#include "misc.h"
#include "fdset.h"
#include "xio.h"
#include "vbi.h"
#include "lang.h"
#include "cache.h"
#include "ui.h"

int debug = 0;

/* current args for next start() */
static char *geometry;
static char *dpy_name;
static char *vbi_name = "/dev/vbi0";
static struct xio *xio;
static struct vbi *vbi;
static int fine_tune = 1; // auto = 999;
static int erc = 1;
static int newbttv = -1;


static void
usage(FILE *fp, int exitval)
{
    fprintf(fp, "\nUsage: %s [options]\n", prgname);
    fprintf(fp,
	    "\n"
	    "  Valid options:\t\tDefault:\n"
	    "    --help\n"
	    "    --version\n"
	    "    -vbi <vbidev>\t\t/dev/vbi0\n"
	    "    -display <dpy>\t\t$DISPLAY\n"
	    "    -geometry <geo>\t\t41x25\n"
	    "    -finetune <-4..4|auto>\t0\n"
	    "    -child ppp[.ss]\n"
	    "    [-parent] ppp[.ss]\t\t900\n"
	    //"    -oldbttv\t\t(for bttv <0.5.20)\n"
	    "    -[no]erc\t\t\tenabled\n"
	    "    -[no]bell\t\t\tenabled\n"
	    "    -charset latin-1/2\t\tlatin-1\n"
	    "\n"
	    "  Order is important!  Each page number\n"
	    "  opens a new window with the previously\n"
	    "  given geometry, device, and display.\n"
	    "\n"
	    "  ppp[.ss] stands for a page number and an\n"
	    "  optional subpage number (ie 123.4).  If\n"
	    "  the subpage number is omitted the first\n"
	    "  transmitted subpage is shown.\n"
	    "\n"
	    "  The -child option requires a parent\n"
	    "  window.  So, it must be preceeded by\n"
	    "  a parent or another child window.\n"
	);
    exit(exitval);
}


static int
arg_pgno(char *p, int *subno)
{
    char *end;
    int pgno;

    *subno = ANY_SUB;
    if (*p)
    {
	pgno = strtol(p, &end, 16);
	if ((*end == ':' || *end == '/' || *end == '.') && end[1])
	    *subno = strtol(end + 1, &end, 16);
	if (*end == 0)
	    if (pgno >= 0x100 && pgno <= 0x999)
		if (*subno == ANY_SUB || (*subno >= 0x00 && *subno <= 0x3f7f))
		    return pgno;
    }
    fatal("%s: invalid page number", p);
}


static struct vtwin *
start(int argc, char **argv, struct vtwin *parent, int pgno, int subno)
{
    if (vbi == 0)
	vbi = vbi_open(vbi_name, cache_open(), fine_tune, newbttv);
    if (vbi == 0)
	fatal("cannot open %s", vbi_name);
    if (vbi->cache)
	vbi->cache->op->mode(vbi->cache, CACHE_MODE_ERC, erc);

    if (xio == 0)
	xio = xio_open_dpy(dpy_name, argc, argv);
    if (xio == 0)
	fatal("cannot open display");

    parent = vtwin_new(xio, vbi, geometry, parent, pgno, subno);
    if (parent == 0)
	fatal("cannot create window");
    return parent;
}


static int
option(int argc, char **argv, int *ind, char **arg)
{
    static struct { char *nam, *altnam; int arg; } opts[] = {
	{ "-vbi", "-dev", 1 },
	{ "-display", "-d", 1 },
	{ "-geometry", "-g", 1 },
	{ "-child", "-c", 1 },
	{ "-editor", "-ed", 0 },
	{ "-parent", "-p", 1 },
	{ "--version", "-v", 0 },
	{ "--help", "-h", 0 },
	{ "-newbttv", "-new", 0 },
	{ "-oldbttv", "-old", 0 },
	{ "-finetune", "-f", 1 },
	{ "-debug", "--debug", 0 },
	{ "-copyright", "-�", 0 },
	{ "-erc", "-e", 0 },
	{ "-noerc", "-ne", 0 },
	{ "-bell", "-b", 0 },
	{ "-nobell", "-nb", 0 },
	{ "-charset", "-latin", 1 },
    };
    int i;

    if (*ind >= argc)
	return 0;

    *arg = argv[(*ind)++];
    for (i = 0; i < NELEM(opts); ++i)
	if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
	{
	    if (opts[i].arg)
		if (*ind < argc)
		    *arg = argv[(*ind)++];
		else
		    fatal("option %s requires an argument", *arg);
	    return i+1;
	}

    if (**arg == '-')
    {
	fatal("%s: invalid option", *arg);
	usage(stderr, 1);
    }

    return -1;
}



int
main(int argc, char **argv)
{
    struct vtwin *parent = 0;
    int pgno, subno;
    int opt, ind;
    char *arg;

    setprgname(argv[0]);

    fdset_init(fds);

    ind = 1;
    while (opt = option(argc, argv, &ind, &arg))
	switch (opt)
	{
	    case 1:	// vbi
		vbi_name = arg;
		vbi = 0;
		parent = 0;
		break;
	    case 2:	// display
		dpy_name = arg;
		xio = 0;
		parent = 0;
		break;
	    case 3:	// geometry
		geometry = arg;
		break;
	    case 4:	// child
		if (parent == 0)
		    fatal("-child requires a parent window");
		pgno = arg_pgno(arg, &subno);
		parent = start(argc, argv, parent, pgno, subno);
		geometry = 0;
		break;
	    case 5:	// editor
		enab_editor = 1;
		break;
	    case 7:	// version
		printf("AleVT Version "VERSION"\n");
		exit(0);
	    case 13:	// copyright
		printf("Copyright 2000 by E. Toernig, froese@gmx.de\n");
		exit(0);
	    case 8:	// help
		usage(stdout, 0);
		break;
	    case 9:	// newbttv
		newbttv = 1;
		break;
	    case 10:	// oldbttv
		newbttv = 0;
		break;
	    case 11:	// finetune
		if (streq(arg, "auto"))
		    fine_tune = 999;
		else
		    fine_tune = strtol(arg, 0, 10);
		break;
	    case 14:	// erc
	    	erc = 1;
		break;
	    case 15:	// noerc
	    	erc = 0;
		break;
	    case 16:	// bell
	    	bell = 1;
		break;
	    case 17:	// nobell
	    	bell = 0;
		break;
	    case 18:	// charset
		if (streq(arg, "latin-1") || streq(arg, "1"))
		    latin1 = 1;
		else if (streq(arg, "latin-2") || streq(arg, "2"))
		    latin1 = 0;
		else
		    fatal("bad charset (not latin-1/2)");
		break;
	    case 12:	// debug
		debug++;
		break;
	    case 6:	// parent
	    case -1:	// non-option arg
		pgno = arg_pgno(arg, &subno);
		parent = start(argc, argv, 0, pgno, subno);
		geometry = 0;
		break;
	}

    if (parent == 0)
	start(argc, argv, 0, 0x900, ANY_SUB);

    xio_event_loop();

    exit(0);
}