-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds1.c
1060 lines (910 loc) · 24.6 KB
/
cmds1.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
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
#include <X11/Xlib.h>
#include <xview/frame.h>
#include <xview/canvas.h>
#include <xview/xv_xrect.h>
#include <xview/panel.h>
#include <xview/xview.h>
#include <can.h>
#include <cmds1.h>
#include <config.h>
#include <drawwin.h>
#include <event.h>
#include <filtersm.h>
#include <global.h>
#include <look_funcs.h>
#include <messages.h>
#include <notices.h>
#include <rs_fric_tool.h>
#include <special.h>
#include <strcmd.h>
/* cjm 14.5.07; to solve problem with doit files: increased the size of path names. 1024's used to be 80, 512 used to be 32 */
char pathname[10][1024];
char default_path[1024];
char metapath[1024];
char data_file[512];
char new_file[512];
char headline[512];
FILE *data, *new;
int doit_des, doit_f_open, meta_fd;
int plot_error;
int read_flag = 0;
int qi_flag = 0;
extern char plot_cmd[256], trig_cmd[256];
extern int active_window;
extern int old_active_window;
extern int total_windows;
extern int action;
extern char msg[MSG_LENGTH];
extern Frame main_frame;
extern Panel_item cmd_panel_item;
void new_win_proc()
{
total_windows++;
create_canvas();
sprintf(msg, "Total windows = %d\n", total_windows+1);
print_msg(msg);
}
void qi_win_proc()
{
if (read_flag != 0) {
if (qi_flag == 1)
update_params();
else {
create_qi_canvas();
qi_flag = 1;
}
}
else {
sprintf(msg, "File has not been read yet. File must be read first.\n");
print_msg(msg);
set_left_footer("Type the data file to read");
action = READ;
set_cmd_prompt("Filename: ");
}
}
void set_active_window(win_num)
int win_num;
{
if (wininfo.windows[win_num] != 1)
{
sprintf(msg, "Window #%d does not exist.\n", win_num+1);
print_msg(msg);
action = MAIN;
top();
return;
}
if (active_window != win_num)
{
if (win_num == old_active_window)
{
old_active_window = active_window;
active_window = win_num;
}
else
{
old_active_window = active_window;
active_window = win_num;
}
}
display_active_window(active_window+1);
display_active_plot(wininfo.canvases[active_window]->active_plot+1);
display_active_file(1);
}
void set_active_plot(i)
int i;
{
canvasinfo *can_info;
Canvas canvas;
Xv_Window canvas_xv_window;
can_info = wininfo.canvases[active_window];
canvas = can_info->canvas;
canvas_xv_window = canvas_paint_window(canvas);
can_info->active_plot = i;
redraw_proc(can_info->canvas, can_info->xvwin, (Display *)xv_get(can_info->canvas, XV_DISPLAY), can_info->win, NULL);
if(i < 0) /*no active plot, no plots*/
{
/*set a default footer*/
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME), FRAME_LEFT_FOOTER, "Normal Mode: left & middle buttons pick row numbers, right button gives x-y position", NULL);
display_active_plot(can_info->active_plot);
}
else if (can_info->alive_plots[i] == 0)
{
sprintf(msg, "Plot does not exist, it can't be set active, error!\n");
print_msg(msg);
top();
action = MAIN;
}
else
{
switch(can_info->plots[i]->mouse)
{
case 0:
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME),
FRAME_LEFT_FOOTER, "Normal Mode: left & middle buttons pick row numbers, right button gives x-y position", NULL);
break;
case 1:
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME),
FRAME_LEFT_FOOTER, "Draw Line Mode: left button picks 1st point, middle picks 2nd, right button quits mode", NULL);
break;
case 2:
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME),
FRAME_LEFT_FOOTER, "Vertical Line Mode: left & middle buttons draw vertical line, right button quits mode", NULL);
break;
case 3:
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME),
FRAME_LEFT_FOOTER, "Distance Mode: left button picks 1st point, middle picks 2nd, right button quits mode", NULL);
break;
case 4:
xv_set(xv_get(canvas, XV_KEY_DATA, GRAF_FRAME),
FRAME_LEFT_FOOTER, "Zoom Mode: left button picks 1st point, middle picks 2nd, right button commits zoom", NULL);
break;
}
display_active_plot(can_info->active_plot+1);
}
}
void del_plot_proc(pn)
int pn;
{
canvasinfo *can_info;
int i, j, hole_index[MAX_PLOTS];
pn--; /*internal number starts at zero, instead of 1*/
can_info = wininfo.canvases[active_window];
if (can_info->alive_plots[pn] == 0 || can_info->active_plot == -1 )
{
sprintf(msg, "Plot does not exist or there's no active plot (error!).\n");
print_msg(msg);
top();
action = MAIN;
return;
}
can_info->alive_plots[pn] = 0; /*make it dead*/
free(can_info->plots[pn]); /*free space */
/*if killed plot was active, or if active plot was the last one
in the list, reset active plot*/
if( (pn == can_info->active_plot || can_info->active_plot == can_info->total_plots -1)
&& can_info->active_plot)
can_info->active_plot--;
/*reshuffle the list, so that, say, removing plot #2 from a list of 4
results in #3 going to #2 and #4 going to #3. This will require repointing the
plot data pointers etc.*/
for(j=i=0;i<MAX_PLOTS;i++) /*first set up an index for holes (actually non-holes)*/
{
hole_index[i]=0; /*default*/
if (can_info->alive_plots[i]) /*if alive, numbers will be the same */
hole_index[j++] = i;
}
can_info->total_plots--; /*now decrement this counter*/
for(j=i=0;i<can_info->total_plots;i++) /*shuffle list*/
{
can_info->alive_plots[i] = 1; /*set it live*/
can_info->plots[i] = can_info->plots[hole_index[i]]; /*repoint pointer to data*/
}
for(i=can_info->total_plots;i< MAX_PLOTS;i++) /* all the rest are dead*/
can_info->alive_plots[i] = 0;
clear_canvas_proc(can_info->canvas);
if(can_info->total_plots ==0)
can_info->active_plot = -1;
else
{
j=can_info->active_plot; /*save active plot*/
for (i=0; i<MAX_PLOTS; i++) /*re-draw */
{
if (can_info->alive_plots[i] == 1)
{
can_info->active_plot = i;
redraw_proc(can_info->canvas, can_info->xvwin,
(Display *)xv_get(can_info->canvas, XV_DISPLAY), can_info->win, NULL);
}
}
if(can_info->alive_plots[j] == 0) /*active plot got axed, use default*/
can_info->active_plot = can_info->total_plots-1;
else
can_info->active_plot = j;
}
set_active_plot(can_info->active_plot);
action = MAIN;
top();
}
void kill_win_proc(wn)
int wn;
{
int i;
Frame graf_frame;
canvasinfo *can_info;
wn--;
if (wn < 0 || wininfo.windows[wn] == 0)
{
sprintf(msg, "Window #%d does not exist.\n", wn+1);
print_msg(msg);
action = MAIN;
top();
return;
}
sprintf(msg, "destroying window #%d\n", wn+1);
print_msg(msg);
can_info = wininfo.canvases[wn];
/* free all the plots in this window */
for (i=0; i<MAX_PLOTS; i++)
{
if(can_info->alive_plots[i])
free(can_info->plots[i]);
}
/* kill the xwindow */
graf_frame = xv_get(can_info->canvas, XV_KEY_DATA, GRAF_FRAME);
xv_destroy_safe(graf_frame);
if (active_window == wn)
{
active_window = old_active_window;
old_active_window = get_old_active_window(active_window);
}
if (old_active_window == wn)
{
old_active_window = get_old_active_window(old_active_window);
}
total_windows--;
if (total_windows == -1)
{
active_window = -1;
old_active_window = -1;
}
display_active_window(active_window+1);
/* if no window is opened, active_plot = NONE */
if (active_window != -1)
display_active_plot(wininfo.canvases[active_window]->active_plot+1);
else
display_active_plot(-1);
wininfo.windows[wn] = 0;
sprintf(msg, "Total windows = %d\n", total_windows+1);
print_msg(msg);
/* free the canvasinfo for this window */
free(can_info);
action = MAIN;
top();
}
int get_old_active_window(wn)
int wn;
{
int i;
if (wn > 0)
{
for (i=wn-1; i>=0; i--)
{
if (wininfo.windows[i] == 1)
return i;
}
}
for (i=wn+1; i<10; i++)
{
if (wininfo.windows[i] == 1)
return i;
}
return -1;
}
/* 19/3/96 cjm: this function should renumber after clearing a plot*/
int get_new_plot_num(alive_plots)
int alive_plots[10];
{
int i;
for (i = 0; i < 10; i++)
{
if (alive_plots[i] == 0)
return i;
}
/* reached plots limit.. need to delete one or more plots from window */
return(-1);
}
void plotting_error(pn)
int pn;
{
canvasinfo *can_info;
int i;
can_info = wininfo.canvases[active_window];
can_info->alive_plots[pn] = 0;
can_info->total_plots--;
free(can_info->plots[pn]);
for(i=0; i<MAX_PLOTS; i++)
{
if (can_info->alive_plots[i] == 1)
{
can_info->active_plot = i;
return;
}
}
can_info->active_plot = -1;
}
void do_plot(cmd)
char cmd[256];
{
char desire[256];
int i, j;
canvasinfo *can_info;
plotarray *data;
int begin, end;
int xaxis, yaxis;
int oldplot;
double xmax, xmin, ymax, ymin;
int plot_num;
Canvas canvas;
Xv_Window canvas_xv_window;
Window win;
plot_error = 0;
nocom(cmd);
if (total_windows == -1) new_win_proc();
can_info = wininfo.canvases[active_window];
if( (strncmp(plot_cmd, "plotover", 8) == 0 ||
(strncmp(plot_cmd, "plotsr", 6) == 0) ||
(strncmp(plot_cmd, "plotsame", 8) == 0)))
{
if(can_info->total_plots == 0)
{
sprintf(msg, "No previous plot. You can't use \"plotover\" etc. since they plot at scale of previous plot.\n");
print_msg(msg);
top();
action = MAIN;
return;
}
else
{
oldplot = can_info->active_plot;
}
}
if (can_info->total_plots >= MAX_PLOTS)
{
sprintf(msg, "Reached limit of %d plots per window! Command Ignored\n", MAX_PLOTS);
print_msg(msg);
top();
action = MAIN;
return;
}
can_info->total_plots += 1;
/* printf("(plot proc)total_plots: %d\n", can_info->total_plots+1); */
data = (plotarray *) malloc(sizeof(plotarray));
if (data == NULL)
{
can_info->total_plots -= 1;
sprintf(msg,"Memory allocation error. Plot cannot be drawn.\n");
print_msg(msg);
top();
action = MAIN;
return;
}
plot_num = get_new_plot_num(can_info->alive_plots); /*get a new number, next in list*/
can_info->alive_plots[plot_num] = 1; /*set this one alive*/
can_info->active_plot = plot_num; /*make it the active plot*/
can_info->plots[plot_num] = data; /*data is a pointer to plotarray struct*/
/* set the label type */
if (strcmp(plot_cmd, "pa") == 0 || strcmp(plot_cmd, "plotall") == 0)
data->label_type = 1; /* msp-style label */
else
data->label_type = 0; /* regular label */
/*plotover: Save rows and plot scale. Plot new cols at the same scale as the active plot.*/
if (strcmp(plot_cmd, "plotover") == 0)
{
if (sscanf(cmd, "%s %d %d", desire, &xaxis, &yaxis) != 3)
{
nea();
plotting_error(plot_num);
top();
action = MAIN;
return;
}
else
{
/* copy values from previous plot */
xmax = can_info->plots[oldplot]->xmax;
xmin = can_info->plots[oldplot]->xmin;
ymax = can_info->plots[oldplot]->ymax;
ymin = can_info->plots[oldplot]->ymin;
begin = can_info->plots[oldplot]->begin;
end = can_info->plots[oldplot]->end;
if (xaxis >= max_col || yaxis >= max_col)
{
sprintf(msg, "Out of range. Col not allocated.\n");
print_msg(msg);
plotting_error(plot_num);
top();
action = MAIN;
return;
}
}
}
else
{
if (strcmp(plot_cmd, "plotall") == 0)
{
if (sscanf(cmd, "%s %d %d", desire, &xaxis, &yaxis) != 3)
{
nea();
plotting_error(plot_num);
top();
action = MAIN;
return;
}
else
{
begin = 0;
/* 28/3/96: note that this could be a problem, should use [col]nelem and
should inform user if x and x nelem are not the same...*/
end = head.nrec - 1;
}
}
else if (strcmp(plot_cmd, "plotsr") == 0)
{
/*plotsr: plot the same row numbers but different cols, autoscale*/
if (sscanf(cmd, "%s %d %d", desire, &xaxis, &yaxis) != 3)
{
nea();
plotting_error(plot_num);
top();
action = MAIN;
return;
}
begin = can_info->plots[oldplot]->begin;
end = can_info->plots[oldplot]->end;
}
else if (strcmp(plot_cmd, "plotscale") == 0)
{
if (sscanf(cmd, "%s %d %d %d %d %lf %lf %lf %lf", desire,
&xaxis, &yaxis, &begin, &end,
&xmax, &xmin,
&ymax, &ymin) != 9)
{
nea();
plotting_error(plot_num);
top();
action = MAIN;
return;
}
}
/* plotsame: plot at the same scale but allow different rows and cols*/
else /* plotauto, plotlog, plotsame, pa */
{
if (sscanf(cmd, "%s %d %d %d %d", desire, &xaxis, &yaxis, &begin, &end) != 5)
{
nea();
plotting_error(plot_num);
top();
action = MAIN;
return;
}
}
if (xaxis >= max_col || yaxis >= max_col)
{
sprintf(msg, "Out of range. Col not allocated.\n");
print_msg(msg);
plotting_error(plot_num);
top();
action = MAIN;
return;
}
if (strcmp(plot_cmd, "plotscale") == 0 || strcmp(plot_cmd, "plotauto") == 0 || strcmp(plot_cmd, "plotlog") == 0 || strcmp(plot_cmd, "plotsame") == 0 || strcmp(plot_cmd, "pa") == 0)
{
if (end > head.nrec-1)
{
sprintf(msg, "nrec = %d, truncating interval to [%d, %d]\n", head.nrec, begin, head.nrec-1);
print_msg(msg);
end = head.nrec - 1;
}
if (check_row(&begin, &end, xaxis) != 0)
{
plot_error = 1;
begin = 0;
}
}
/* end reading arguments */
if (strncmp(desire, "plotauto", 8) == 0 || strncmp(desire, "plotall", 7) == 0 || strncmp(desire, "plotsr", 6) == 0 || strncmp(desire, "pa", 2) == 0)
{
stats(&xaxis, &begin, &end);
xmax = col_stat.max;
xmin = col_stat.min;
stats(&yaxis, &begin, &end);
ymax = col_stat.max;
ymin = col_stat.min;
}
else if (strncmp(desire, "plotlog", 7) == 0)
{
stats(&xaxis, &begin, &end);
if (col_stat.min <= 0)
{
sprintf(msg, "Negative numbers within range. Can't do log_plot.\n");
print_msg(msg);
plot_error = 1;
plotting_error(plot_num);
top();
action = MAIN;
return;
}
else
{
xmax = ceil(log10((double)col_stat.max));
xmin = floor(log10((double)col_stat.min));
}
stats(&yaxis, &begin, &end);
if (col_stat.min <= 0)
{
sprintf(msg, "Negative numbers within range. Can't do log_plot.\n");
print_msg(msg);
plot_error = 1;
plotting_error(plot_num);
top();
action = MAIN;
return;
}
else
{
ymax = ceil(log10((double)col_stat.max));
ymin = floor(log10((double)col_stat.min));
}
}
else if (strncmp(desire, "plotsame", 8) == 0)
{
xmax = can_info->plots[oldplot]->xmax;
xmin = can_info->plots[oldplot]->xmin;
ymax = can_info->plots[oldplot]->ymax;
ymin = can_info->plots[oldplot]->ymin;
}
if ((end < begin) || (xmax <= xmin) || (ymax <= ymin))
{
/* printf("begin:%d end:%d max.xaxis:%.3f min.xaxis:%.3f max.yaxis:%.3f min.yaxis:%.3f\n", begin, end, xmax, xmin, ymax, ymin); */
sprintf(msg, "Limits problem: MAX <= MIN ???\n");
print_msg(msg);
plotting_error(plot_num);
plot_error = 1;
top();
action = MAIN;
return;
}
}
/* sets all the variables in plot_array data structure */
data->begin = begin;
data->end = end;
data->col_x = xaxis;
data->col_y = yaxis;
data->xmax = xmax;
data->xmin = xmin;
data->ymax = ymax;
data->ymin = ymin;
data->nrows_x = end - begin +1;
data->nrows_y = end - begin +1;
data->mouse = 0;
data->x1 = 0;
data->x2 = 0;
data->y1 = 0;
data->y2 = 0;
data->p1 = 0;
data->p2 = 0;
data->zp1 = 0;
data->zp2 = 0;
data->xarray = (double *)malloc(sizeof(double)*data->nrows_x);
data->yarray = (double *)malloc(sizeof(double)*data->nrows_x);
/* copy data from darray to xarray and yarray (plot data arrays) */
j = 0;
if (strncmp(desire, "plotlog", 7) == 0)
{
for (i = begin; i <= end; ++i)
{
data->xarray[j] = log10((double)darray[xaxis][i]);
data->yarray[j] = log10((double)darray[yaxis][i]);
j++;
}
}
else
{
for (i = begin; i <= end; ++i)
{
data->xarray[j] = darray[xaxis][i];
data->yarray[j] = darray[yaxis][i];
j++;
}
}
/*
sprintf(msg, "Active window: %d Canvas num: %d Plot num: %d\n", active_window+1, can_info->canvas_num+1, can_info->active_plot+1);
print_msg(msg);
*/
canvas = can_info->canvas;
canvas_xv_window = can_info->xvwin;
win = can_info->win;
/* draw the plot */
/*
redraw_proc(canvas, canvas_xv_window, (Display *)xv_get(canvas, XV_DISPLAY),
(Window)xv_get(canvas_xv_window, XV_XID), NULL);
*/
/* 28.3.08 cjm, took out. I think this is the line I put in ~ 5/07 to get plots to appear when the plot window first opened
but, since then I've found that the problem with blank plots was due to the xv_set command and these calls:
WIN_CONSUME_EVENTS, WIN_NO_EVENTS, WIN_MOUSE_BUTTONS
so
*/
redraw_all_proc(canvas, can_info->xvwin, (Display *)xv_get(canvas, XV_DISPLAY), can_info->win, NULL);
top();
action = MAIN;
}
void write_proc(arg)
char arg[256];
{
strcpy(new_file, arg);
if ((new = fopen(new_file, "a")) == NULL)
{
sprintf(msg, "Can't open file: %s. fopen in write_proc() failed. See cmds1.c\n", new_file);
print_msg(msg);
action =MAIN;
top();
return;
}
else
{
/* file already exist */
if ((int)ftell(new) > 0)
{
/* ask if want to overwrite old file */
if (write_show_warning() != 1)
{
/* don't overwrite */
sprintf(msg, "Write aborted!\n");
print_msg(msg);
action =MAIN;
top();
return;
}
else
{
if ((new = fopen(new_file, "w")) == NULL)
{
sprintf(msg, "Can't open file: %s.\n", new_file);
print_msg(msg);
action =MAIN;
top();
return;
}
else
{
sprintf(msg, "Overwriting %s.\n", new_file);
print_msg(msg);
}
}
}
if (act_col() > head.nchan)
{
sprintf(msg, "Active columns = %d\n", act_col());
print_msg(msg);
sprintf(msg, "Please compact data array to use columns 1 through head.nchan[%d]\n", head.nchan);
print_msg(msg);
sprintf(msg, "Reallocation is not necessary.\n");
print_msg(msg);
action =MAIN;
top();
return;
}
rite_lookfile(new);
fclose(new);
sprintf(msg, "File %s written. \n", new_file);
print_msg(msg);
action =MAIN;
top();
}
}
void read_proc(cmd)
char cmd[256];
{
char dummy[256];
sscanf(cmd, "%s %s", dummy, data_file);
if ((data = fopen(data_file, "r")) == NULL)
{
sprintf(msg, "Can't open data file: %s. fopen in read_proc() failed. Check filename, see cmds1.c\n", data_file);
print_msg(msg);
action = MAIN;
top();
return;
}
else
{
sprintf(msg, "Reading %s...\n", data_file);
print_msg(msg);
/* reed is in filtersm.c */
if (reed(data, ((strncmp(dummy, "append", 6) == 0) ? TRUE : FALSE)) != 1)
{
display_active_file(0);
fclose(data);
action = MAIN;
top();
return;
}
display_active_file(1);
fclose(data);
sprintf(msg, "Reading %s done.\n", data_file);
print_msg(msg);
++read_flag; /* file successfully opened */
action = MAIN;
top();
}
}
void doit_proc(arg)
char arg[256];
{
/* reads a doit file and passes the commands to command_handler() directly */
char *fgets();
char cmd[256];
int i, j, error;
if ((doit_f_open+1) > 9)
{
sprintf(msg, "Sorry but you can only nest up to 10 doit files.\n");
print_msg(msg);
action=MAIN;
top();
return;
}
/* need to set action to MAIN here so that the commands in doit file can use action */
action = MAIN;
strcpy(pathname[doit_f_open+1], default_path); /*set up file path name*/
strcat(pathname[doit_f_open+1], arg);
/*strcpy(data_file, arg);
strcat(pathname[doit_f_open+1], data_file);*/
/* open file, do some initial checking, then add to array of open doit files*/
if ( (temp_com_file = fopen(pathname[doit_f_open+1], "r")) == NULL)
{
sprintf(msg, "Open failed- can't open data file: \"%s\"\n", pathname[doit_f_open+1]);
print_msg(msg);
action = MAIN;
top();
return;
}
else
{
if (fscanf(temp_com_file, "%s", cmd) != 1)
{
sprintf(msg,"Cannot read file.\n");
print_msg(msg);
fclose(temp_com_file);
action = MAIN;
top();
return;
}
if (strncmp(cmd, "begin", 5) != 0)
{
sprintf(msg, "doit file must begin with the string: begin\n");
print_msg(msg);
fclose(temp_com_file);
action = MAIN;
top();
return;
}
/* loop thru the file until EOF; read the file line by line, put the string (whole line) in cmd
pass cmd to command_handler() */
doit_f_open++; /*add to list of open doit files*/
com_file[doit_f_open] = temp_com_file;
/*clear first line (should be "begin" at this point, report errors*/
i=1; /*use to count lines*/
if( fgets(cmd, 256, com_file[doit_f_open]) == NULL)
{
sprintf(msg, "Error from doit file \"%s\", unexpected EOF on line %d, see cmds1.c \n", pathname[doit_f_open+1], i);
print_msg(msg);
fclose(com_file[doit_f_open]);
if (--doit_f_open < 0)
doit_f_open = 0;
action = MAIN;
top();
return;
}
/* the global_error flag is set in messages.c when the error functions are called*/
while( fgets(cmd, 256, com_file[doit_f_open]) != NULL && global_error == FALSE)
{
/*read a line from doit file*/
i++; /*increment line counter*/
if( cmd == NULL)
{
sprintf(msg, "Error from doit file \"%s\", unexpected EOF on line %d, see cmds1.c \n", pathname[doit_f_open+1], i);
print_msg(msg);
fclose(com_file[doit_f_open]);
if (--doit_f_open < 0)
doit_f_open = 0;
action = MAIN;
top();
return;
}
/*make sure line returned by fgets was "whole" --i.e. contained a NEWLINE, otherwise */
/* lines may be longer than 256 (too long)*/
j=0; error=TRUE;
while(j < 256)
{
if(cmd[j] == '\n') /*find end of lines and replace with nulls*/
{
cmd[j] = '\0';
error=FALSE;
break;
}
j++;
}
if(error)
{
sprintf(msg, "Error from doit file \"%s\", line %d too long. Lines must be < 256 chars. But this should be easy to fix, see cmds1.c \n", pathname[doit_f_open+1], i);
print_msg(msg);
fclose(com_file[doit_f_open]);
if (--doit_f_open < 0)
doit_f_open = 0;
action = MAIN;
top();
return;
}
if (strncmp(cmd, "#", 1) != 0)
{
sprintf(msg, "Command: %s\n", cmd);
print_msg(msg);
}
if (strncmp(cmd, "end", 3) == 0)
{
sprintf(msg, "Closing doit file %s.\n",pathname[doit_f_open]);
print_msg(msg);
fclose(com_file[doit_f_open]);
if (--doit_f_open < 0)
doit_f_open = 0;
break;
}
else if (strncmp(cmd, "#", 1) == 0)
{
continue;
}
else
{
command_handler(cmd);
}
}
cmd[0] = '\0'; /*empty cmd buffer*/
action = MAIN;
top();
}
}
void set_path_proc(arg)
char arg[256];
{
/* change: if path is inaccessible, set path to current path instead of default path */
/* printf("path: %s def_path: %s\n", arg, default_path); */
if (access(arg, 4) != 0)