-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlex.c
2279 lines (2034 loc) · 60 KB
/
lex.c
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <stddef.h>
#include <stdio.h>
#include <string.h> /* for strchr() */
#include <ctype.h> /* for toupper */
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <readline/readline.h> /* for command line editing */
#include <readline/history.h>
#include "rlgetc.h"
#include "db.h"
#include "token.h"
#include "xwin.h"
#include "eprintf.h"
#include "readfont.h"
#include "equate.h"
#include "path.h"
#include "rubber.h"
#include "ev.h"
#include "version.h"
#include "postscript.h"
#include "readshpfont.h"
#define UNUSED(x) (void)(x)
int readin();
int aborted; // used to abort drawing
/* The names of functions that actually do the manipulation. */
int com_add(), com_archive(), com_area(), com_background();
int com_bye(), com_change(), com_copy(), com_date(), com_define();
int com_delete(), com_display(), com_distance(), com_dump();
int com_echo(), com_edit(), com_equate(), com_eval(), com_exit(), com_files();
int com_fsize(), com_grid(), com_group(), com_help();
int com_identify(), com_input(), com_interrupt(), com_layer();
int com_level(), com_list(), com_lock(), com_macro(), com_menu();
int com_move(), com_plot(), com_point(), com_process();
int com_purge(), com_redo(), com_retrieve(), com_save(), com_search();
int com_set(), com_shell(), com_show(), com_smash();
int com_split(), com_step(), com_stretch(), com_time(), com_trace();
int com_tslant(), com_undo(), com_units(), com_version(), com_window();
int com_wrap();
typedef int (*funcptr)(); // pointer to a function returning an int
typedef struct {
char *name; /* User printable name of the function. */
funcptr func; /* Function to call to do the job. */
char *doc; /* Description of function. */
char *usage; /* Usage for function. */
} COMMAND;
COMMAND commands[] =
{
{"ADD", com_add, "add a component to the current device",
"ADD A<layer> [.<cnam>][@<snam>][:W<wid>][:R<res>] <xy1> <xy2> <xy3>... \n\
ADD C<layer> [.<cnam>][@<snam>][:W<wid>][:R<res>][:Y<yxratio>] <xy1> <xy2>... \n\
ADD <device> [.<cnam>][@<snam>][:M<mir>][:R<ang>][:X<x>][:Y<ratio>][:Z<slant>] <xy>...\n\
ADD L<layer> [.<cnam>][@<snam>][:W<wid>] <xy1> <xy2> [<xy3> ...]\n\
ADD N<layer> \n [.<cnam>][@<snam>][:F<size>][:J<just>][:M<mir>][:R<ang>][:Y<ratio>][:Z<slant>]\"string\" <xy>\n\
ADD O<layer> [.<cnam>][@<snam>][:W<wid>][:R<res>] <xy1> <xy2> <xy3>...\n\
ADD P<layer> [.<cnam>][@<snam>][:W<wid>] <xy1> <xy2> <xy3> [<xy4>...]\n\
ADD R<layer> [.<cnam>][@<snam>][:W<wid>] <xy1> <xy2>\n\
ADD T<layer> \n [.<cnam>][@<snam>][:F<size>][:J<just>][:M<mir>][:R<ang>][:Y<ratio>][:Z<slant>]\"string\" <xy>" },
{"ARCHIVE", com_archive, "create an archive file of the specified device",
"ARC <EOC>"},
{"AREA", com_area, "calculate and display the area of selected component",
"ARE [<component>[<layer>]] xysel [xysel ...] <EOC>"},
{"BACKGROUND", com_background, "specified device for background overlay",
"unimplemented"},
{"BYE", com_bye, "terminate edit session",
"BYE <EOC>"},
{"CHANGE", com_change, "change characteristics of selected components",
"CHA [<component>[<layer>]] {xysel [<comp_options>|\"string\"|:L<newlayer>]}... <EOC>}"},
{"COPY", com_copy, "copy a component from one location to another",
"COP [<component>[<layer>]] xysel xyref {xynewref ...} <EOC>" },
{"DATE", com_date, "print the current date and time to the console",
"DATE <EOC>"},
{"DEFINE", com_define, "define a macro",
"unimplemented"},
{"DELETE", com_delete, "delete a component from the current device",
"DEL [<component>[<layer>]] xy1... <EOC>"},
{"DISTANCE", com_distance, "measure the distance between two points",
"DIS {<xy1> <xy2>} ... EOC" },
{"DISPLAY", com_display, "turn the display on or off",
"DISP [ON|OFF]"},
{"DUMP", com_dump, "dump graphics window to file or printer",
"DUM [:F][:R][:T{bmp|gif|jpg|png|pgm|ppm|xbm}] <plotname> <EOC>"},
{"ECHO", com_echo, "print a shell variable",
"ECHO <arguments> ... <EOC>"},
{"EDIT", com_edit, "begin edit of an old or new device",
"EDI <device>"},
{"EQUATE", com_equate, "define characteristics of a mask layer",
"EQU [:C<color>] [:P<pen>] [:M{S|D|B|[0-6]}] [:O] [:B|:D|:S|:I] [<label>] <layer>"},
{"EXIT", com_exit, "leave an EDIT, PROCESS, or SEARCH subsystem",
"EXI <EOC>"},
{"FILES", com_files, "purge named files",
"$FILES <cellname1> <cellname2> .... <EOC>"},
{"FSIZE", com_fsize, "Set the default font size for text and notes",
"FSI [<fontsize>]"},
{"GRID", com_grid, "set grid spacing or turn grid on/off",
"GRI [ON|OFF] [:C<color>] <delta> <skip> [<xorig> <yorig>]]\n\
GRI [ON|OFF] [:C<color>] <xdelta> <ydelta> <xskip> <yskip> <xorig> <yorig>"},
{"GROUP", com_group, "create a device from existing components",
"unimplemented"},
{"HELP", com_help, "print syntax diagram for the specified command",
"HELP [<commandname>] EOC"},
{"?", com_help, "Synonym for HELP",
"? [<commandname>] EOC"},
{"IDENTIFY", com_identify, "identify named instances or components",
"IDE [[:P] <xypnt>] | [:R <xy1> <xy2>] ... EOC"},
{"INPUT", com_input, "take command input from a file",
"INP <filename> <EOC>"},
{"INTERRUPT", com_interrupt, "interrupt an ADD to issue another command",
"unimplemented"},
{"LAYER", com_layer, "set a default layer number",
"LAYER [<layer_number>] <EOC>"},
{"LEVEL", com_level, "set the logical level of the current device",
"LEV <logical_level> <EOC>"},
{"LIST", com_list, "list information about the current environment",
"LIS <EOC>"},
{"LOCK", com_lock, "set or print the default lock angle",
"LOCK [<angle>]"},
{"MACRO", com_macro, "enter the MACRO subsystem",
"unimplemented"},
{"MENU", com_menu, "change or save the current menu",
"unimplemented"},
{"MOVE", com_move, "move a component from one location to another",
"MOV [<component>[<layer>]] { [[:P] <xysel>] | [:R <xy1> <xy2>] xyref xynewref } ... <EOC>"},
{"PLOT", com_plot, "make a postscript plot of the current device",
"PLO [:F][:B][:G] [:L<linewidth>][:Tautoplot|:Tdxf|:Tgerber|:Tpostscript|:Tsvg|:Tweb]:P<pagesize><plotname><EOC>"},
{"POINT", com_point, "display the specified point on the screen",
"POI {<xy1>...} <EOC>" },
{"PROCESS", com_process, "enter the PROCESS subsystem",
"PRO <EOC>"},
{"PURGE", com_purge, "remove device from memory and disk",
"PUR <cellname>"},
{"QUIT", com_bye, "terminate edit session",
"QUI <EOC>"},
{"REDO", com_redo, "redo the last command",
"REDo <EOC>"},
{"RETRIEVE", com_retrieve, "read commands from an ARCHIVE file",
"RET <archivefile>"},
{"SAVE", com_save, "save the current file or device to disk",
"SAV [<newname>]"},
{"SEARCH", com_search, "modify the search path",
"unimplemented"},
{"SET", com_set, "set environment variables",
"SET VAR VALUE"},
{"SHELL", com_shell, "run a program from within the editor",
"called implicitly, simply type the name of executable in $PATH"},
{"SHOW", com_show, "define which kinds of things to display",
"SHOW {+|-|#}{EACILN0PRT}<layer>"},
{"SMASH", com_smash, "replace an instance with its components",
"SMAsh [<device_name>] {<coord>} <EOC>"},
{"SPLIT", com_split, "cut a component into two halves",
"unimplemented"},
{"STEP", com_step, "copy a component in an array fashion",
"unimplemented"},
{"STRETCH", com_stretch, "make a component larger or smaller",
"STR [[<comp>[<layer>]] [:P] <xysel> <xyref> <xynewref> ...\n\
STR [[<comp>[<layer>]] :R <xyll> <xyur> <xyref> <xynewref> ..." },
{"TIME", com_time, "print the system clock time",
"TIM <EOC>"},
{"TRACE", com_trace, "highlight named signals",
"unimplemented"},
{"TSLANT", com_tslant, "get or set the default font slant for italic text and notes",
"TSL [<fontslantangle>]"},
{"UNDO", com_undo, "undo the last command",
"UNDo <EOC>"},
{"UNITS", com_units, "set editor resolution and user unit type",
"unimplemented"},
{"VERSION", com_version, "identify the version number of program",
"VER <EOC>"},
{"WINDOW", com_window, "change the current window parameters",
"WINdow :X[<scale>] [:N<physical>] [:F] [:O] [<xy1> [<xy2>]] <EOC>"},
{"WRAP", com_wrap, "create a new device using existing components",
"WRAP [<component>[<layer>]] [<devicename>] <xyorig> <xy1> <xy2> <EOC>"},
{(char *) NULL, (funcptr) NULL, (char *) NULL, (char *) NULL}
};
static int def_layer=0; // default layer for ADD commands
static int def_units=1000; // default maximum division of the grid
#include <signal.h>
#define MAXSIGNAL 31 /* biggest signal under linux */
void sighandler(); /* catch signal */
int main(int argc, char **argv)
{
UNUSED(argc);
LEXER *lp; /* lexer struct for main cmd loop */
int err=0;
char buf[128];
char buf2[128];
char *pig_path;
FILE *fp;
int i;
/* make stdin unbuffered - without this cut/paste fails */
setvbuf(stdin, NULL, _IONBF, 0);
/* set program name for eprintf() error report package */
setprogname(argv[0]);
/* set up to catch all signal */
/* for (i=1; i<=MAXSIGNAL; i++) { */
err+=(signal(SIGINT, &sighandler) == SIG_ERR);
err+=(signal(SIGQUIT, &sighandler) == SIG_ERR);
err+=(signal(SIGTERM, &sighandler) == SIG_ERR);
err+=(signal(SIGTSTP, &sighandler) == SIG_ERR);
err+=(signal(SIGPIPE, &sighandler) == SIG_ERR);
if (err) {
printf("main() had difficulty setting sighandler\n");
return(err);
}
if (!EVinit()) {
printf("can't initialize environment\n");
exit(6);
}
/* set default environment variables */
EVset("PIG_PATH", PIG_PATH); /* where piglet finds its files */
EVset("PIG_GRID_COLOR", "3"); /* grid color */
EVset("PIG_GRID", "10 10 1 1 0 0"); /* grid spec */
EVset("PIG_NOTEDATA_FILE", "NOTEDATA.F"); /* note font file */
EVset("PIG_MENUDATA_FILE", "MENUDATA_V"); /* note font file */
EVset("PIG_TEXTDATA_FILE", "TEXTDATA.F"); /* text font file */
EVset("PIG_PROCDATA_FILE", "PROCDATA.P"); /* text font file */
EVset("PIG_SPLASH_REP", "piglogo"); /* startup logo file */
EVset("PIG_FONT_SLANT", "0.0"); /* default for TSLANT command */
EVset("PIG_FONT_SIZE", "10.0"); /* default for FSIZE command */
EVset("PIG_PAPER_SIZE", "8.5x11"); /* postscript papersize */
EVset("PIG_RC", "pigrc"); /* default piglet startup file */
EVset("PIG_X11MENU_FONT", "10x20"); /* default X11 menu font */
EVset("PIG_HTML_PREFIX", "http://130.27.50.124/legion/doku.php?id="); /* header for svg links */
EVset("PIG_HTML_SUFFIX", ""); /* trailer for svg links */
license(); /* print GPL notice */
pig_path=EVget("PIG_PATH"); // use default pig path
// NOTE: must read pigrc before xinit() is called
// to allow PIG_GEOMETRY to be set before window is created...
// However, if we do readin() at this point, we will get a crash if
// there is an edit command in the file since xwin is not initialized..
// We handle this by doing readin in PROCESS mode and making
// com_edit ignore edit commands in PROCESS mode.
//
// after xinit, we will read pigrc again to handle possible edit
// commands.
findfile(pig_path, EVget("PIG_RC"), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find any pigrc file\n");
} else {
fp=fopen(buf, "r"); // FIXME: check for ret code and print error
printf("reading %s\n",buf);
readin(buf,0,PRO);
}
initX(); /* create window, load MENUDATA */
pig_path=EVget("PIG_PATH"); // read path again in case pigrc reset it
findfile(pig_path, EVget("PIG_NOTEDATA_FILE"), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find NOTEDATA file: %s\n", EVget("PIG_NOTEDATA_FILE"));
printf("PIG_PATH=\"%s\"\n", pig_path);
exit(5);
} else {
loadfont(buf,0); /* load NOTE, TEXT definitions */
}
findfile(pig_path, EVget("PIG_TEXTDATA_FILE"), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find TEXTDATA file: %s\n", EVget("PIG_TEXTDATA_FILE"));
printf("PIG_PATH=\"%s\"\n", pig_path);
exit(5);
} else {
loadfont(buf,1); /* load NOTE, TEXT definitions */
}
shp_fontinit();
for (i=0; i<=255; i++) {
sprintf(buf, "PIG_SHPFONT%d", i);
if (EVget(buf) != NULL) {
findfile(pig_path, EVget(buf), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find font file: %s\n", EVget(buf));
printf("PIG_PATH=\"%s\"\n", pig_path);
// exit(5);
} else {
shp_loadfont(buf,i); /* load FONT definition */
// printf("loaded font %s at position %d\n", buf, i);
}
}
}
initialize_equates();
findfile(pig_path, EVget("PIG_PROCDATA_FILE"), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not PROCDATA file: %s\n", EVget("PIG_PROCDATA_FILE"));
printf("PIG_PATH=\"%s\"\n", pig_path);
exit(5);
} else {
readin(buf,0,PRO); /* load PROCESS FILE definitions */
}
strcpy(buf2, EVget("PIG_SPLASH_REP"));
strcat(buf2, ".d");
findfile(pig_path, buf2, buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find splash screen: %s\n", buf2);
printf("PIG_PATH=\"%s\"\n", pig_path);
exit(5);
} else {
currep = db_install(EVget("PIG_SPLASH_REP")); /* create blank stub */
readin(buf,1,EDI);
currep->modified = 0;
show_init(currep);
currep = NULL;
}
// initialize_readline();
initialize_readline();
rl_pending_input='\n';
rl_setprompt("");
lp = token_stream_open(stdin,"STDIN");
// now read pig_rc again to handle any edi commands
findfile(pig_path, EVget("PIG_RC"), buf, R_OK);
if (buf[0] == '\0') {
printf("Could not find any pigrc file\n");
} else {
fp=fopen(buf, "r"); // FIXME: check for ret code and print error
printf("reading %s\n",buf);
rl_readin_file(fp);
}
parse(lp);
return(1);
}
void parse(LEXER *lp)
{
int debug=0;
TOKEN token;
char *word;
char *s;
char buf[128];
char title[128];
char *path;
// int retcode;
COMMAND *command;
COMMAND * find_command();
int state = 0;
double x1, y1;
while((token=token_get(lp, &word)) != EOF) {
if (debug) printf("%s, line %d: IN MAIN: got %s: %s\n",
lp->name, lp->line, tok2str(token), word);
aborted=0;
switch (lp->mode) {
case MAIN:
rl_setprompt("MAIN> ");
xwin_set_title("");
break;
case PRO:
rl_setprompt("PROCESS> ");
break;
case SEA:
rl_setprompt("SEARCH> ");
break;
case MAC:
rl_setprompt("MACRO> ");
break;
case EDI:
if (currep != NULL) {
if (debug) {
sprintf(buf, "EDIT %s (%d,%d)> ", currep->name,
stack_depth(&(currep->undo))-1, stack_depth(&(currep->redo)));
} else {
sprintf(buf, "EDIT %s> ", currep->name);
sprintf(title, "PD_piglet %s> ", currep->name);
}
rl_setprompt(buf);
xwin_set_title(title);
db_checkpoint(lp);
} else {
rl_setprompt("EDIT> ");
}
break;
default:
rl_setprompt("> ");
break;
}
switch(state) {
case 0:
switch(token) {
case CMD: /* find and call the command */
command = find_command(word);
if (command == NULL) {
printf(" bad command\n");
token_flush_EOL(lp);
} else {
if (debug) printf("MAIN: found command\n");
rl_saveprompt();
sprintf(buf, "%s> ", command->name);
rl_setprompt(buf);
// retcode = ((*(command->func)) (lp, ""));
((*(command->func)) (lp, "")); /* call command */
rl_restoreprompt();
}
break;
case EOL:
case END:
case COMMA:
break;
case EOC:
break;
case NUMBER:
if(sscanf(word, "%lg", &x1) != 1) {
weprintf("bad number: %s\n", word);
}
state=1;
break;
case IDENT:
path=EVget("PATH");
if (word[0] == '/' ||
findfile(path, word, buf, X_OK)) { // Unix command
token_unget(lp, token, word);
com_shell(lp, NULL); // give to SHELL builtin
} else if ((s=Macroget(lp->word)) != NULL) { // MACRO expansion:
token_unget(lp, token, word);
com_eval(lp, NULL); // give to EVAL builtin
} else {
printf("MAIN: expected COMMAND, got %s: %s\n",
tok2str(token), word);
token_flush_EOL(lp);
}
break;
default:
printf("MAIN: expected COMMAND, got %s: %s\n",
tok2str(token), word);
token_flush_EOL(lp);
break;
}
break;
case 1:
if (token == COMMA) {
state=2;
} else {
token_unget(lp, token, word);
state=0;
}
break;
case 2:
if (token == NUMBER) {
if(sscanf(word, "%lg", &y1) != 1) {
weprintf("bad number: %s\n", word);
}
/* pan(x1,y1); */
state=0;
} else {
token_unget(lp, token, word);
state=0;
}
break;
default:
printf("bad case in lex()\n");
state=0;
break;
}
}
}
void sighandler(int x)
{
static int last=-1;
printf("caught %d: %s. Use QUIT command to end program",x, strsignal(x));
fflush(stdout);
aborted++;
if (x == 3) {
if (last==x) {
exit(0);
} else {
printf(": do it again and I'll die!");
}
}
last = x;
printf("\n");
}
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
COMMAND * find_command(char *name)
{
register int i, size;
int debug=0;
size = strlen(name);
size = size > 2 ? size : 3;
for (i = 0; commands[i].name; i++)
if (strncasecmp(name, commands[i].name, size) == 0) {
if (debug) {
;
}
return (&commands[i]);
}
return ((COMMAND *) NULL);
}
/* returns 1 if name found, 0 if not */
int lookup_command(char *name)
{
register int i, size;
size = strlen(name);
size = size > 2 ? size : 3;
for (i = 0; commands[i].name; i++)
if (strncasecmp(name, commands[i].name, size) == 0)
return (1);
return (0);
}
/* **************************************************************** */
/* */
/* Built-in Commands */
/* */
/* **************************************************************** */
int is_comp(char c)
{
switch(toupper((unsigned char)c)) {
case 'A':
return(ARC);
break;
case 'C':
return(CIRC);
break;
case 'I':
return(INST);
break;
case 'L':
return(LINE);
break;
case 'N':
return(NOTE);
break;
case 'O':
return(OVAL);
break;
case 'P':
return(POLY);
break;
case 'R':
return(RECT);
break;
case 'T':
return(TEXT);
break;
default:
return(0);
break;
}
}
/* now in com_add.c ...
com_add(LEXER *lp, char *arg)
*/
/* now in geom_arc.c...
int add_arc(LEXER *lp, int *layer)
*/
int add_oval(LEXER *lp, int *layer)
{
UNUSED(layer);
printf("in add_oval (unimplemented)\n");
token_flush_EOL(lp);
return(1);
}
/* ARCHIVE [{+|-}cmpnt[msk]] [{:N|:L}lvl] [:P] [:R] [:H] [:S] devicename [filename] */
int com_archive(LEXER *lp, char *arg) /* create archive file of currep */
{
UNUSED(arg);
TOKEN token;
int done=0;
char *word;
int smash = 0;
int process = 0;
XFORM *xp;
need_redraw++;
/* FIXME: check for :P option to write PROCDATA info */
/* make sure and purge PROCDATA on read in */
while(!done && (token=token_get(lp, &word)) != EOF) {
switch(token) {
case OPT: /* option */
if (strncasecmp(word, ":S", 2) == 0) { /* smash archive */
smash++; // FIXME: parsed, but not implemented
} else if (strncasecmp(word, ":P", 2) == 0) { /* include process file */
process++; /* FIXME: parsed, but not currently used */
} else {
weprintf("bad option to ARCHIVE: %s\n", word);
return(-1);
}
break;
case CMD: /* command */
token_unget(lp, token, word);
done++; // could fall through but compiler complains
break;
case EOC: /* end of command */
done++;
break;
case NUMBER: /* number */
case IDENT: /* identifier */
case QUOTE: /* quoted string */
case END: /* end of file */
case EOL: /* newline or carriage return */
case COMMA: /* comma */
break;
default:
eprintf("bad case in com_archive");
break;
}
}
if (currep != NULL) {
if (!smash) {
if (db_def_archive(currep, smash, process)) {
printf("unable to archive %s\n", currep->name);
return(-1);
};
printf(" archived %s\n", currep->name);
currep->modified = 0;
} else {
xp = (XFORM *) emalloc(sizeof(XFORM));
xp->r11 = 1.0; xp->r12 = 0.0; xp->r21 = 0.0; xp->r22 = 1.0;
xp->dx = 0.0; xp->dy = 0.0;
// FIXME: not implemented
db_arc_smash(currep, xp, 1);
printf(" smash archive not implemented: %s\n", currep->name);
free(xp);
}
} else {
printf("error: not currently editing a cell\n");
}
return (0);
}
/* now in com_area.c */
/* com_area(LEXER *lp, char *arg) */ /* display area of selected component */
int com_background(LEXER *lp, char *arg) /* use device for background overlay */
{
UNUSED(arg);
TOKEN token;
int done=0;
char buf[128];
char *word;
int nnums=0;
DB_TAB *ed_rep;
buf[0]='\0';
while(!done && (token=token_get(lp, &word)) != EOF) {
switch(token) {
case IDENT: /* identifier */
strncpy(buf, word, 128);
nnums++;
break;
case CMD: /* command */
token_unget(lp, token, word);
done++;
break;
case EOC: /* end of command */
done++;
break;
case EOL: /* newline or carriage return */
break; /* ignore */
case NUMBER: /* number */
case COMMA: /* comma */
case QUOTE: /* quoted string */
case OPT: /* option */
case END: /* end of file */
default:
printf("BACKGROUND: expected IDENT, got: %s\n", tok2str(token));
return(-1);
break;
}
}
if (currep == NULL) {
printf(" BACKGROUND: not currently editing any cell, can't set background\n");
return(-1);
} else if (nnums==1) {
if ((ed_rep = db_lookup(buf)) == NULL) { /* not in memory */
if (loadrep(buf)) { /* valid load */
currep->background = strsave(buf);
}
} else { /* already in mem */
currep->background = strsave(buf);
}
} else if (nnums==0) {
if (currep->background != NULL) {
free(currep->background);
currep->background = NULL;
}
} else {
printf("BACKGROUND: wrong number of arguments\n");
return(-1);
}
need_redraw++;
return (0);
}
int com_bye(LEXER *lp, char *arg) /* terminate edit session */
{
UNUSED(arg);
/* The user wishes to quit using this program */
/* two consecutive BYE requests will force an exit */
/* Just set quit_now non-zero. */
static int linenumber; /* remember last request */
if ( (lp->line != linenumber+1) && db_list_unsaved() ) {
printf(" you have one or more unsaved instances!\n");
printf(" typing either \"QUIT\" or \"BYE\" twice will force exit, discarding all unsaved changes\n");
} else {
db_remove_autosavefiles();
quit_now++;
exit(0); /* for now just bail */
}
linenumber = lp->line;
return (0);
}
/* now in com_change.c */
/* com_change(LEXER *lp, char *arg) */ /* change properties of selected components */
/* now in com_copy.c */
/* com_copy(LEXER *lp, char *arg) */ /* copy component */
/* now in com_shell.c */
/* int com_define(lp, arg) */ /* define a macro */
int com_date(LEXER *lp, char *arg) /* print date and time to console */
{
UNUSED(lp);
UNUSED(arg);
char buf[MAXFILENAME];
time_t time_now;
time_now = time(NULL);
strftime(buf, MAXFILENAME, "%m/%d/%Y %H:%M:%S", localtime(&time_now));
printf(" %s\n",buf);
return (0);
}
/* now in com_delete.c */
/*com_delete(lp, arg) */ /* delete component from currep */
int com_display(LEXER *lp, char *arg) /* turn the display on or off */
{
UNUSED(arg);
TOKEN token;
int done=0;
// char buf[128];
char *word;
DISPLAYSTATE display_state = D_TOGGLE;
// buf[0]='\0';
while(!done && (token=token_get(lp, &word)) != EOF) {
switch(token) {
case IDENT: /* identifier */
if (strncasecmp(word, "ON", 2) == 0) {
display_state=D_ON;
} else if (strncasecmp(word, "OFF", 2) == 0) {
display_state=D_OFF;
} else {
printf("bad argument to DISP: %s\n", word);
}
break;
case CMD: /* command */
token_unget(lp, token, word);
done++;
break;
case EOC: /* end of command */
done++;
break;
case EOL: /* newline or carriage return */
break;
case COMMA: /* comma */
case QUOTE: /* quoted string */
case OPT: /* option */
case END: /* end of file */
case NUMBER: /* number */
default:
printf("DISP: expected ON/OFF, got %s\n", tok2str(token));
return(-1);
break;
}
}
xwin_display_set_state(display_state);
if (strcmp(lp->name, "STDIN") == 0) {
switch (xwin_display_state()) {
case D_ON:
printf("display now ON\n");
break;
case D_OFF:
printf("display now OFF\n");
break;
default:
printf("display state UNKNOWN\n");
break;
}
}
return (0);
}
/* now in com_distance.c */
/* com_distance(lp, arg) */ /* measure the distance between two points */
int com_dump(LEXER *lp, char *arg) /* dump graphics window to file or printer */
{
UNUSED(arg);
TOKEN token;
int done=0;
char cmd[300];
char name[256]="";
char *word;
int debug=0;
char *s = NULL;
char *suffix=".png";
char *conv="pnmtopng";
int i;
int fit=0; /* fit the device to the window before plotting */
int rev=0; /* reverse video flag */
extern void do_win(); /* found in com_window */
if (currep == NULL || currep->name == NULL) {
printf("not editing a file, nothing here to dump\n");
return(2);
}
// some possible options:
// :F to do a fit before plotting, otherwise only plot current window view
// :T<dumptype> gif,tif,pnm,jpg...
// :R (reverse video - swap black and white pixels)
strcpy(name,currep->name);
while(!done && (token=token_get(lp, &word)) != EOF) {
if (debug) printf("COM_DUMP: got %s: %s\n", tok2str(token), word);
switch(token) {
case CMD: /* command */
token_unget(lp, token, word);
done++;
break;
case OPT: /* option */
if (strncasecmp(word, ":F", 2) == 0) { /* fit window */
fit++;
} else if (strncasecmp(word, ":R", 2) == 0) { /* reverse video */
rev++;
} else if (strncasecmp(word, ":T", 2) == 0) { /* specify dumptype */
if (strncasecmp(word+2, "gif", 3) == 0) {
suffix=".gif"; conv="ppmtogif";
} else if (strncasecmp(word+2, "bmp", 3) == 0) {
suffix=".bmp"; conv="ppmtobmp";
} else if (strncasecmp(word+2, "xpm", 3) == 0) {
suffix=".xpm"; conv="ppmtoxpm";
} else if (strncasecmp(word+2, "jpg", 3) == 0) {
suffix=".jpg"; conv="ppmtojpeg";
} else if (strncasecmp(word+2, "pgm", 3) == 0) {
suffix=".pgm"; conv="ppmtopgm";
} else if (strncasecmp(word+2, "png", 3) == 0) {
suffix=".png"; conv="pnmtopng";
} else if (strncasecmp(word+2, "ppm", 3) == 0) {
suffix=".ppm"; conv="cat";
} else {
printf("unrecognized graphic type: %s\n", word);
printf("defaulting to native PPM\n");
suffix=".ppm"; conv="cat";
}
if (findfile(EVget("PATH"), conv, NULL, R_OK)==0) {
printf("couldn't find conversion program in PATH: %s\n", conv);
printf("defaulting to PPM\n");
suffix=".ppm"; conv="cat";
}
} else {
weprintf("bad option to DUMP: %s\n", word);
return(-1);
}
break;
case EOL: /* newline or carriage return */
break; /* ignore */
case IDENT: /* identifier */
strcpy(name,word);
break;
case EOC: /* end of command */
done++;
if (fit) { /* fit the device */
token_unget(lp, EOC, ";");
token_unget(lp, OPT, ":F");
com_window(lp, NULL);
}
xwin_raise_window();
/* FIXME: horrible kludge, we have to wait until the display is properly */
/* updated. How do you know when everything has been properly sloshed */
/* through the server? This is simply an empirical hack that works */
/* on my system. There has to be a better way. */
for (i=0; i<=20; i++) {
xwin_doXevent(&s);
}
sprintf(cmd, "%s %s > %s%s",
rev?"ppmchange black white white black |":"",
conv, name, suffix);
if (debug) printf("doing %s\n", cmd);
if (xwin_dump_graphics(cmd) == -1) {
sprintf(cmd, "rm -f %s%s", currep->name, suffix);
system(cmd);
}
xwin_doXevent(&s);
if (fit) { /* revert to old window params */
token_unget(lp, EOC, ";");
token_unget(lp, OPT, ":Z");
com_window(lp, NULL);
}
break;
case NUMBER: /* number */
case COMMA: /* comma */
case QUOTE: /* quoted string */
case END: /* end of file */
default:
printf("DUMP: expected EOC, got: %s\n", tok2str(token));
return(-1);
break;
}
}
return (0);
}
/* now in com_edit.c */