File: command.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (712 lines) | stat: -rw-r--r-- 15,966 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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>

#include "gis.h"
#include "graph.h"
#include "driverlib.h"
#include "driver.h"
#include "pad.h"
#include "utils.h"
#include "glocale.h"

#define REC(a,b)    if ((eof=rec((a),(b)))) break
#define RECTEXT(x,s)  if ((eof=rectext(&x,&s))) break
#define SEND(a,b)   _send((a),(b))
#define SENDTEXT(x)  sendtext((x))

static int rec(void *,int);
static int rectext(char **,int *);
static int _send(void *,int);
static int sendtext(char *);
static int RESULT(int);

static int _wfd;
static int _rfd;

static char inbuf[4096];
static int n_read;
static int atbuf;

static char current_command;

static int eof;

static PAD *curpad;       /* current selected pad */

static unsigned char *blua;
static unsigned char *grna;
static unsigned char *reda;
static unsigned char *nula;
static int blu_alloc;
static int grn_alloc;
static int red_alloc;
static int nul_alloc;

static int *xarray;
static int *yarray;
static int n_xarray;
static int n_yarray;

static char lc;

void
command_init(int rfd, int wfd)
{
    _rfd = rfd;
    _wfd = wfd;

    atbuf = n_read = 0;
    current_command = 0;

    eof = 0;
}

int
command_get_input(void)
{
    return _rfd;
}

int
process_command(int c)
{
    static char *name;
    static int name_size;
    static char *text;
    static int text_size;

    int t, b, l, r, ret;
    int x, y;
    unsigned char red, grn, blu;
    int min, max;
    int number;
    int index;
    int button;
    float wx;
    ITEM *item;
    LIST *list;
    PAD *pad;
    unsigned char ch;

    switch(c)
    {
    case BEGIN:
	ch = 0;
	for (index = -10; index < BEGIN_SYNC_COUNT; index++)
	    SEND(&ch, 1);
	ch = COMMAND_ESC;
	SEND(&ch, 1);
	break;
    case RESPOND:
	Respond();
	SEND(&ch, 1);
	break;
    case GET_NUM_COLORS:
	Number_of_colors(&index);
	SEND(&index, sizeof index);
	break;
    case STANDARD_COLOR:
	REC(&index, sizeof index);
	Standard_color(index);
	break;
    case COLOR:
	REC(&index, sizeof index);
	Color(index);
	break;
    case RGB_COLOR:
	REC(&red, sizeof red);
	REC(&grn, sizeof grn);
	REC(&blu, sizeof blu);
	RGB_color(red, grn, blu);
	break;
    case COLOR_TABLE_FIXED:
	x = Color_table_fixed();
	SEND(&x, sizeof x);
	break;
    case COLOR_TABLE_FLOAT:
	x = Color_table_float();
	SEND(&x, sizeof x);
	break;
    case COLOR_OFFSET:
	REC(&index, sizeof index);
	Color_offset(index);
	break;
    case COLOR_PRINT:
	break;
    case CONT_ABS:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Cont_abs(x, y);
	break;
    case CONT_REL:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Cont_rel(x, y);
	break;
    case BOX_ABS:
	REC(&l, sizeof l);
	REC(&t, sizeof t);
	REC(&r, sizeof r);
	REC(&b, sizeof b);
	Box_abs(l, t, r, b);
	break;
    case BOX_REL:
	REC(&l, sizeof l);
	REC(&t, sizeof t);
	Box_rel(l, t);
	break;
    case ERASE:
	Erase();
	break;
    case GET_LOCATION_WITH_BOX:
	REC(&t, sizeof t);
	REC(&b, sizeof b);
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Get_location_with_box2(t, b, &x, &y, &button, 1); /* start */
        while ( 1 ) {
	    /* Check monitor and send result back */
	    ret = Get_location_with_box2(t, b, &x, &y, &button, 2);
	    if ( ret == 0 ) button = 0;
	    /* we send back current position but button set to 0 */
	    SEND(&x, sizeof x);
	    SEND(&y, sizeof y);
	    SEND(&button, sizeof button);
	    if ( ret == 1 ) break;
	    /* Should we continue (0) or break (1) */
	    REC(&ret, sizeof ret );
	    if ( ret == 1 ) { /* break */
	        ret = Get_location_with_box2(t, b, &x, &y, &button, 3);
            	break;
	    }
	}	
	break;
    case GET_LOCATION_WITH_LINE:
	REC(&t, sizeof t);
	REC(&b, sizeof b);
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Get_location_with_line2(t, b, &x, &y, &button, 1); /* start */
        while ( 1 ) {
	    /* Check monitor and send result back */
	    ret = Get_location_with_line2(t, b, &x, &y, &button, 2);
	    if ( ret == 0 ) button = 0;
	    /* we send back current position but button set to 0 */
	    SEND(&x, sizeof x);
	    SEND(&y, sizeof y);
	    SEND(&button, sizeof button);
	    if ( ret == 1 ) break;
	    /* Should we continue (0) or break (1) */
	    REC(&ret, sizeof ret );
	    if ( ret == 1 ) { /* break */
	        ret = Get_location_with_line2(t, b, &x, &y, &button, 3);
            	break;
	    }
	}	
	break;
    case GET_LOCATION_WITH_POINTER:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	REC(&button, sizeof button);
	Get_location_with_pointer2(&x, &y, &button, 1); /* start */
        while ( 1 ) {
	    /* Check monitor and send result back */
	    ret = Get_location_with_pointer2(&x, &y, &button, 2);
	    if ( ret == 0 ) button = 0;
	    /* we send back current position but button set to 0 */
	    SEND(&x, sizeof x);
	    SEND(&y, sizeof y);
	    SEND(&button, sizeof button);
	    if ( ret == 1 ) break;
	    /* Should we continue (0) or break (1) */
	    REC(&ret, sizeof ret );
	    if ( ret == 1 ) { /* break */
	        ret = Get_location_with_pointer2(&x, &y, &button, 3);
            	break;
	    }
	}	
	break;
    case GRAPH_CLOSE:
	Graph_Close();
	exit(0);
    case LINEMOD:
	REC(&index, sizeof index);
	Linemod(&index);
	break;
    case MOVE_ABS:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Move_abs(x, y);
	break;
    case MOVE_REL:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Move_rel(x, y);
	break;
    case RASTER_CHAR:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	REC(&index, sizeof index);
	blua = (unsigned char *) xalloc(blua, &blu_alloc, x, sizeof(*blua));
	REC(blua, x * sizeof(char));
	if (index != 0) index = 1;
	Raster_char(x, y, blua, index, 1);
	break;
    case RASTER_INT:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	REC(&index, sizeof index);
	xarray = (int *) xalloc(xarray, &n_xarray, x, sizeof(*xarray));
	REC(xarray, x * sizeof(*xarray));
	if (index != 0) index = 1;
	Raster_int(x, y, xarray, index, 1);
	break;
    case RGB_RASTER:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	reda = (unsigned char *) xalloc(reda, &red_alloc, x, sizeof(*reda));
	grna = (unsigned char *) xalloc(grna, &grn_alloc, x, sizeof(*grna));
	blua = (unsigned char *) xalloc(blua, &blu_alloc, x, sizeof(*blua));
	nula = (unsigned char *) xalloc(nula, &nul_alloc, x, sizeof(*nula));
	REC(reda, x * sizeof(char));
	REC(grna, x * sizeof(char));
	REC(blua, x * sizeof(char));
	REC(nula, x * sizeof(char));
	REC(&t, sizeof t);
	RGB_raster(x, y, reda, grna, blua, t ? nula : NULL);
	break;
    case RGB_COLORS:
	reda = (unsigned char *) xalloc(reda, &red_alloc, 256, sizeof(*reda));
	grna = (unsigned char *) xalloc(grna, &grn_alloc, 256, sizeof(*grna));
	blua = (unsigned char *) xalloc(blua, &blu_alloc, 256, sizeof(*blua));
	REC(reda, 256);
	REC(grna, 256);
	REC(blua, 256);
	Set_RGB_color(reda, grna, blua);
	break;
    case POLYGON_ABS:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polygon_abs(xarray, yarray, number);
	break;
    case POLYGON_REL:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polygon_rel(xarray, yarray, number);
	break;
    case POLYLINE_ABS:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polyline_abs(xarray, yarray, number);
	break;
    case POLYLINE_REL:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polyline_rel(xarray, yarray, number);
	break;
    case POLYDOTS_ABS:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polydots_abs(xarray, yarray, number);
	break;
    case POLYDOTS_REL:
	REC(&number, sizeof number);
	xarray = (int *) xalloc(xarray, &n_xarray, number, sizeof(*xarray));
	yarray = (int *) xalloc(yarray, &n_yarray, number, sizeof(*yarray));
	REC(xarray, number * sizeof(xarray[0]));
	REC(yarray, number * sizeof(yarray[0]));
	Polydots_rel(xarray, yarray, number);
	break;
    case RESET_COLORS:
	REC(&min, sizeof min);
	REC(&max, sizeof max);
	number = max - min + 1;
	reda = (unsigned char *) xalloc(reda, &red_alloc, number, sizeof(*reda));
	grna = (unsigned char *) xalloc(grna, &grn_alloc, number, sizeof(*grna));
	blua = (unsigned char *) xalloc(blua, &blu_alloc, number, sizeof(*blua));
	REC(reda, number * sizeof(char));
	REC(grna, number * sizeof(char));
	REC(blua, number * sizeof(char));
	Reset_colors(min, max, reda, grna, blua);
	break;
    case RESET_COLOR:
	REC(&red, sizeof red);
	REC(&grn, sizeof grn);
	REC(&blu, sizeof blu);
	REC(&number, sizeof number);
	Reset_color(red, grn, blu, number);
	break;
    case SCREEN_LEFT:
	Screen_left(&index);
	SEND(&index, sizeof index);
	break;
    case SCREEN_RITE:
	Screen_rite(&index);
	SEND(&index, sizeof index);
	break;
    case SCREEN_BOT:
	Screen_bot(&index);
	SEND(&index, sizeof index);
	break;
    case SCREEN_TOP:
	Screen_top(&index);
	SEND(&index, sizeof index);
	break;
    case SET_WINDOW:
	REC(&t, sizeof t);
	REC(&b, sizeof b);
	REC(&l, sizeof l);
	REC(&r, sizeof r);
	Set_window(t, b, l, r);
	break;
    case GET_TEXT_BOX:
	RECTEXT(text, text_size);
	Get_text_box(text, &t, &b, &l, &r);
	SEND(&t, sizeof t);
	SEND(&b, sizeof b);
	SEND(&l, sizeof l);
	SEND(&r, sizeof r);
	break;
    case FONT:
	RECTEXT(text, text_size);
	x = Font_get(text);
	SEND(&x, sizeof x);
	break;
    case FONT_FREETYPE:
	RECTEXT(text, text_size);
	x = Font_freetype_get(text);
	SEND(&x, sizeof x);
	break;
    case FONT_FREETYPE_RELEASE:
	x = Font_freetype_release();
	SEND(&x, sizeof x);
	break;
    case CHARSET:
	RECTEXT(text, text_size);
	x = init_font_charset(text);
	SEND(&x, sizeof x);
	break;
    case TEXT:
	RECTEXT(text, text_size);
	Text(text);
	break;
    case TEXT_SIZE:
	REC(&x, sizeof x);
	REC(&y, sizeof y);
	Text_size(x, y);
	break;
    case TEXT_ROTATION:
	REC(&wx, sizeof wx);
	Text_rotation(wx);
	break;
    case PANEL_SAVE:
	RECTEXT(text, text_size);
	REC(&t, sizeof t);
	REC(&b, sizeof b);
	REC(&l, sizeof l);
	REC(&r, sizeof r);
	Panel_save(text, t, b, l, r);
	break;
    case PANEL_RESTORE:
	RECTEXT(text, text_size);
	Panel_restore(text);
	break;
    case PANEL_DELETE:
	RECTEXT(text, text_size);
	Panel_delete(text);
	break;
    case PAD_CREATE:
	RECTEXT(text, text_size);
	if (*text == 0) /* this is scratch pad */
	    RESULT(OK);
	else if (find_pad(text) != NULL)
	    RESULT(DUPLICATE); /* duplicate pad */
	else if (create_pad(text))
	    RESULT(OK);
	else
	    RESULT(NO_MEMORY);
	break;

    case PAD_CURRENT:
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    SENDTEXT("");
	}
	else
	{
	    RESULT(OK);
	    SENDTEXT(curpad->name);
	}
	break;

    case PAD_DELETE:
	if (curpad == NULL)
	    RESULT(NO_CUR_PAD);
	else if (*curpad->name == 0)
	    RESULT(ILLEGAL);
	else
	{
	    delete_pad(curpad);
	    curpad = NULL;
	    RESULT(OK);
	}
	break;

    case PAD_INVENT:
	invent_pad(text);
	SENDTEXT(text);
	break;

    case PAD_LIST:
	for (pad = pad_list(); pad != NULL; pad = pad->next)
	    if (*pad->name)
		SENDTEXT(pad->name);
	SENDTEXT("");
	break;

    case PAD_SELECT:
	RECTEXT(text, text_size); /* pad name */
	curpad = find_pad(text);
	if (curpad == NULL)
	    RESULT(NO_PAD);
	else
	    RESULT(OK);
	break;

    case PAD_GET_ITEM:
	RECTEXT(text, text_size); /* item name */
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    break;
	}
	item = find_item(curpad, text);
	if (item == NULL)
	{
	    RESULT(NO_ITEM);
	    break;
	}
	RESULT(OK);
	for (list = item->list; list != NULL; list = list->next)
	    if (*list->value)
		SENDTEXT(list->value);
	SENDTEXT("");
	break;

    case PAD_SET_ITEM:
	RECTEXT(name, name_size); /* item name */
	RECTEXT(text, text_size); /* item value */
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    break;
	}
	delete_item(curpad, name);
	if (append_item(curpad, name, text, 0))
	    RESULT(OK);
	else
	    RESULT(NO_MEMORY);
	break;

    case PAD_APPEND_ITEM:
	RECTEXT(name, name_size); /* item name */
	RECTEXT(text, text_size); /* item value */
	REC(&index, sizeof index); /* replace flag */
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    break;
	}
	if (append_item(curpad, name, text, index))
	    RESULT(OK);
	else
	    RESULT(NO_MEMORY);
	break;

    case PAD_DELETE_ITEM:
	RECTEXT(text, text_size); /* item name */
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    break;
	}
	delete_item(curpad, text);
	RESULT(OK);
	break;

    case PAD_LIST_ITEMS:
	if (curpad == NULL)
	{
	    RESULT(NO_CUR_PAD);
	    break;
	}
	RESULT(OK);
	for (item = curpad->items; item != NULL; item = item->next)
	    if (*item->name)
		SENDTEXT(item->name);
	SENDTEXT("");
	break;

    default:
	fprintf(stderr, _("\nUnknown command: %d last: %d\n"), c, lc);
	break;
    }
    lc = c;

    return eof;
}

static int read1(char *c)
{
    if (atbuf == n_read)
    {
        atbuf = 0;
        n_read = read(_rfd, inbuf, sizeof inbuf);
	if (n_read < 0)
	    perror("Monitor: read1: Error reading input");
        if (n_read <= 0)
            return 1;           /* EOF */
    }
    *c = inbuf[atbuf++];
    return 0;
}

int get_command(char *c)
{
    /* is there a command char pending? */
    if ((*c = current_command)) {
        current_command = 0;
        return 0;
    }

    /*
     * look for 1 (or more) COMMAND_ESC chars
     * followed by a non-zero comamnd token char
     */
    while (read1(c) == 0)   /* while !EOF */
    {
        if (*c != COMMAND_ESC)
            continue;
        while (*c == COMMAND_ESC)
            if (read1(c) != 0)
	    {
		fprintf(stderr, _("Monitor: get_command: Premature EOF\n"));
                return 1;               /* EOF */
	    }
        if (*c)
            return 0;           /* got the command token */
    }
    return 1;   /* EOF */
}

static int get1(char *c)
{
    if (read1(c) != 0)
        return 1;       /* EOF */
    if (*c != COMMAND_ESC)
        return 0;       /* OK */
    if (read1(c) != 0)
        return 1;       /* EOF */
    if (*c)
    {
        current_command = *c;
        return -1;      /* Got command within data */
    }
    *c = COMMAND_ESC;   /* sequence COMMAND_ESC,0 becomes data COMMAND_ESC */
    return 0;           /* OK */
}

static int rec(void *buf, int n)
{
    char *cbuf = buf;
    int stat;
    while (n-- > 0) {
        if ((stat=get1(cbuf++)) != 0)
	    return stat; /* EOF or COMMAND_ESC */
    }
    return 0;
}

static int rectext(char **buff_p, int *size_p)
{
    char *buff = *buff_p;
    int size = *size_p;
    int i, stat;
    for (i = 0; ; i++)
    {
	char c;

	stat = get1(&c);
	if (stat != 0)
	    return stat; /* EOF or COMMAND_ESC */

	if (i >= size)
	{
	    *size_p = size = size ? size * 2 : 1000;
	    *buff_p = buff = G_realloc(buff, size);
	}

	buff[i] = c;

	if (!c)
	    return 0;
    }
}

static int _send(void *buf, int n)
{
    int r = write(_wfd, (char *)buf, n);
    if (r < 0)
    {
	perror("Monitor: _send: write");
	return 1;
    }
    if (r < n)
    {
	fprintf(stderr,
		_("Monitor: _send: write returned short count: %d of %d\n"),
		r, n);
	return 1;
    }
    return 0;
}

static int sendtext(char *s)
{
    SEND(s, strlen(s) + 1);
    return 0;
}

static int RESULT(int n)
{
    unsigned char c;

    c = n;
    SEND(&c, 1);

    return 0;
}