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
|
include <ctype.h> # defines IS_WHITE
include <tbset.h>
include "tupar.h" # defines TUPAR_EXIT & TUPAR_QUIT
# tu_instr -- execute edit instruction
# Execute one instruction regarding header parameters for a table:
# get, put, delete, replace, type, or list.
# The flag DONE will be set to true if the instruction is 'q' or
# if the user's response to a prompt is EOF. Prompting is turned
# off if the input is redirected.
#
# Phil Hodge, 28-Mar-1988 Subroutine created
# Phil Hodge, 9-Sep-1988 Prompt changed for delete & replace
# Phil Hodge, 9-Mar-1989 Change data type of header parm from char to int.
# Phil Hodge, 23-Aug-1991 Include eq_flag, allowing quit without saving changes
# Phil Hodge, 9-Jul-1993 Set modified=true if header was changed.
# Phil Hodge, 7-Mar-1995 In tu_putpar, also put a comment, if present.
# Phil Hodge, 17-May-1995 In tu_putpar, allow ' as well as " as delimiter.
# Phil Hodge, 22-May-1996 Add "k" instruction.
# Phil Hodge, 5-Jun-1997 In tu_getpar and tu_listpar, also print comments
# Phil Hodge, 2-Jul-1998 In tu_putpar, check for "true" or "t" for a boolean
# parameter; get data type from existing parameter;
# in tu_listpar, print boolean as "yes" or "no".
# Ellyne Kinney, 2-Feb-1999 Testing Automatic updates of IRAFRA under CVS
# 13th try.
procedure tu_instr (tp, linebuf, readonly, prompt, from_stdin,
iredir, save_instr, isbuf, bufsize, ibp,
modified, eq_flag, done, istat)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # o: scratch for input line
bool readonly # i: was table opened readonly?
bool prompt # i: prompt for input?
bool from_stdin # i: get input from STDIN (or from buffer)
bool iredir # i: input redirected?
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
bool modified # io: set to true if the header was modified
int eq_flag # o: exit or quit
bool done # o: set to true if done with current table
int istat # o: > 0 if put or delete but table is readonly
#--
char instr[11] # instruction from user: q, g, p, etc.
int ip # index in linebuf
int clen # length of command (to check for "!")
bool verify # verify before delete or replace?
bool incl_num # include par numbers when listing keywords?
int ctowrd(), tu_gline(), tu_rd_instr()
int strlen()
errchk tu_getpar, tu_putpar, tu_delpar, tu_replpar, tu_ch_name
begin
# default value in case user finishes by giving an EOF
eq_flag = TUPAR_EXIT
istat = 0
done = false
if (from_stdin) {
# Read an instruction from STDIN into linebuf.
if (prompt) {
call eprintf (":")
call flush (STDOUT)
call flush (STDERR)
}
if (tu_gline (STDIN, linebuf) == EOF)
done = true
} else {
# Read an instruction from buffer into linebuf.
if (tu_rd_instr (Memc[isbuf], ibp, linebuf) == EOF)
done = true
}
if ( done )
return
ip = 1
if (ctowrd (linebuf, ip, instr, 11) <= 0) # a blank line
return
if (instr[1] == '#') # a comment line
return
if (instr[1] == 'e') {
eq_flag = TUPAR_EXIT
done = true
} else if (instr[1] == 'q') {
clen = strlen (instr)
if (instr[clen] == '!')
eq_flag = TUPAR_QUIT_NC
else
eq_flag = TUPAR_QUIT
done = true
} else if (instr[1] == 'g') {
call tu_getpar (tp, linebuf, ip, instr,
save_instr, isbuf, bufsize, ibp)
} else if (instr[1] == 'p') {
if (readonly) {
istat = 1
return
}
call tu_putpar (tp, linebuf, ip, instr,
save_instr, isbuf, bufsize, ibp)
modified = true
} else if (instr[1] == 'd') {
# Delete a header parameter specified by name or by number.
if (readonly) {
istat = 1
return
}
verify = ( ! iredir ) && (instr[2] != '!')
call tu_delpar (tp, linebuf, ip, verify,
save_instr, isbuf, bufsize, ibp, modified)
} else if (instr[1] == 'r') {
# Replace a header parameter specified by name or by number.
if (readonly) {
istat = 1
return
}
verify = ( ! iredir ) && (instr[2] != '!')
call tu_replpar (tp, linebuf, ip, prompt, from_stdin,
verify, save_instr, isbuf, bufsize, ibp,
modified, done)
} else if (instr[1] == 'k') {
# Change keyword name.
if (readonly) {
istat = 1
return
}
call tu_ch_name (tp, linebuf, ip,
save_instr, isbuf, bufsize, ibp,
modified)
} else if (instr[1] == 't' || instr[1] == 'l') {
# Type or list parameters; list means include keyword number.
incl_num = instr[1] == 'l' # list rather than type
call tu_listpar (tp, linebuf, ip, incl_num,
save_instr, isbuf, bufsize, ibp)
} else {
call eprintf ("The options are:\n")
call eprintf (
" e, q, g, p, d, r, t, l\n")
call eprintf (
" (exit, quit, get, put, delete, replace, type, list)\n")
call eprintf (
" e exit the task, saving changes\n")
call eprintf (
" q quit the task WITHOUT saving any changes\n")
call eprintf (
" g keyword get parameter with keyword `keyword'\n")
call eprintf (
" p keyword value put parameter `keyword'\n")
call eprintf (
" d keyword delete parameter `keyword'\n")
call eprintf (
" r keyword replace parameter `keyword'\n")
call eprintf (
" k oldkey newkey change keyword name\n")
call eprintf (
" t type the parameters\n")
call eprintf (
" l list parameters and show par numbers\n")
call eprintf (
" see help for further info about these instructions\n")
}
end
# tu_getpar -- get a parameter
# The value of a parameter specified by name (not by number) will be gotten
# and displayed. If the keyword is not found in the header, nothing will
# be displayed (i.e. no error message). If the keyword is HISTORY, COMMENT,
# or a blank, then all keywords of that type will be displayed.
procedure tu_getpar (tp, linebuf, ip, instr,
save_instr, isbuf, bufsize, ibp)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # io: index in linebuf
char instr[ARB] # i: the instruction (needed for data type)
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
#--
char keyword[SZ_KEYWORD] # keyword for parameter
char kwrd[SZ_KEYWORD] # keyword returned by tbhgnp
char text[SZ_PARREC] # buffer for value of parameter
char comment[SZ_PARREC] # buffer for comment, if any
int dtype # data type (TY_CHAR, etc)
double dblval
real realval
int intval
bool boolval
int npar # current number of parameters
int k # loop index
double tbhgtd()
real tbhgtr()
int tbhgti(), tbpsta()
bool tbhgtb()
int ctowrd()
bool streq()
begin
npar = tbpsta (tp, TBL_NPAR)
if (ctowrd (linebuf, ip, keyword, SZ_KEYWORD) <= 0) {
call eprintf ("syntax: g keyword\n")
return # get next instruction
}
call strupr (keyword)
# Get comment, if it exists.
iferr (call tbhgcm (tp, keyword, comment, SZ_PARREC))
comment[1] = EOS
if (instr[2] == 'r') {
ifnoerr (realval = tbhgtr (tp, keyword)) {
call printf ("%s = %g")
call pargstr (keyword)
call pargr (realval)
if (comment[1] == EOS) {
call printf ("\n")
} else {
call printf (" %s\n")
call pargstr (comment)
}
}
} else if (instr[2] == 'd') {
ifnoerr (dblval = tbhgtd (tp, keyword)) {
call printf ("%s = %g")
call pargstr (keyword)
call pargd (dblval)
if (comment[1] == EOS) {
call printf ("\n")
} else {
call printf (" %s\n")
call pargstr (comment)
}
}
} else if (instr[2] == 'i') {
ifnoerr (intval = tbhgti (tp, keyword)) {
call printf ("%s = %d")
call pargstr (keyword)
call pargi (intval)
if (comment[1] == EOS) {
call printf ("\n")
} else {
call printf (" %s\n")
call pargstr (comment)
}
}
} else if (instr[2] == 'b') {
ifnoerr (boolval = tbhgtb (tp, keyword)) {
call printf ("%s = %b")
call pargstr (keyword)
call pargb (boolval)
if (comment[1] == EOS) {
call printf ("\n")
} else {
call printf (" %s\n")
call pargstr (comment)
}
}
} else {
if (streq (keyword, "HISTORY") || streq (keyword, "COMMENT") ||
keyword[1] == EOS) {
# Print all history or comment or blank-keyword records.
do k = 1, npar {
call tbhgnp (tp, k, kwrd, dtype, text)
if (streq (keyword, kwrd)) {
call printf ("%s = %s\n")
call pargstr (keyword)
call pargstr (text)
}
}
} else {
ifnoerr (call tbhgtt (tp, keyword, text, SZ_PARREC)) {
if (comment[1] == EOS) {
call printf ("%s = %s\n")
call pargstr (keyword)
call pargstr (text)
} else {
call printf ("%s = '%s' %s\n")
call pargstr (keyword)
call pargstr (text)
call pargstr (comment)
}
}
}
}
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
end
# tu_putpar -- add or replace a parameter
# A parameter specified by name (not by number) is put into the table.
# If the parameter already exists it will be replaced (and the data type
# may be changed); otherwise, it will be added.
procedure tu_putpar (tp, linebuf, ip, instr,
save_instr, isbuf, bufsize, ibp)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # io: index in linebuf
char instr[ARB] # i: the instruction (needed for data type)
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
#--
pointer sp
pointer value # scratch for a string value
char keyword[SZ_KEYWORD] # keyword for parameter
int dtype # data type code for parameter
bool found # parameter already in header? (ignored)
double dblval
real realval
int intval
bool boolval
int npar # current number of parameters
int tbpsta()
int nchar, ctowrd(), ctod(), ctoi(), nscan(), strlen()
bool streq()
begin
npar = tbpsta (tp, TBL_NPAR)
if (ctowrd (linebuf, ip, keyword, SZ_KEYWORD) <= 0) {
call eprintf ("syntax: p keyword value\n")
return
}
call smark (sp)
call salloc (value, SZ_FNAME, TY_CHAR)
# Set the data type. If the user specified a type, use that.
# If none was specified, find out whether the parameter is
# already in the header, and if so, use the existing type.
if (instr[2] == 'r')
dtype = TY_REAL
else if (instr[2] == 'd')
dtype = TY_DOUBLE
else if (instr[2] == 'i')
dtype = TY_INT
else if (instr[2] == 'b')
dtype = TY_BOOL
else if (instr[2] == 't')
dtype = TY_CHAR
else
call tu_partype (tp, keyword, dtype, found)
# In each section, we use ctowrd or ctod (or something), not only
# to extract the value but also to skip past it so we can check for
# a comment.
if (dtype == TY_REAL) {
if (ctod (linebuf, ip, dblval) > 0)
realval = dblval
else
realval = INDEFR
call tbhadr (tp, keyword, realval)
} else if (dtype == TY_DOUBLE) {
if (ctod (linebuf, ip, dblval) < 1)
dblval = INDEFD
call tbhadd (tp, keyword, dblval)
} else if (dtype == TY_INT) {
if (ctoi (linebuf, ip, intval) < 1)
intval = INDEFI
call tbhadi (tp, keyword, intval)
} else if (dtype == TY_BOOL) {
nchar = ctowrd (linebuf, ip, Memc[value], SZ_FNAME)
call strlwr (Memc[value])
call sscan (Memc[value])
call gargb (boolval)
if (nscan() < 1) {
if (streq (Memc[value], "true") || streq (Memc[value], "t") ||
streq (Memc[value], "1"))
boolval = true
else
boolval = false
}
call tbhadb (tp, keyword, boolval)
} else {
while (IS_WHITE(linebuf[ip]))
ip = ip + 1
if (linebuf[ip] == '"' || linebuf[ip] == '\'') {
nchar = ctowrd (linebuf, ip, Memc[value], SZ_FNAME)
call tbhadt (tp, keyword, Memc[value])
} else {
call tbhadt (tp, keyword, linebuf[ip])
# Set ip to point to end of line because there's no comment.
ip = strlen (linebuf) + 1
}
}
# Check for a comment.
while (IS_WHITE(linebuf[ip]))
ip = ip + 1
if (linebuf[ip] != EOS)
call tbhpcm (tp, keyword, linebuf[ip])
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
call sfree (sp)
end
# tu_delpar -- delete a parameter
# A parameter is to be deleted. The user will be prompted for confirmation
# if the input is not redirected and the instruction was not 'q!'.
procedure tu_delpar (tp, linebuf, ip, verify,
save_instr, isbuf, bufsize, ibp, modified)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # i: index in linebuf
bool verify # i: ask for verification before deleting?
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
bool modified # io: set to true if parameter was deleted
#--
char keyword[SZ_KEYWORD] # keyword for parameter
char text[SZ_PARREC] # buffer for value of parameter
char char_type # data type as a letter
int dtype # data type
int par1, par2 # range of par numbers to delete
int i # loop index
int parnum # keyword number
int ctowrd()
bool clgetb()
begin
i = ip
if (ctowrd (linebuf, i, keyword, SZ_KEYWORD) <= 0) {
call eprintf ("syntax: d keyword (or d parnum)\n")
return
}
# Get the parameter numbers; = 0,-1 if not found.
call tu_parnum (tp, linebuf[ip], par1, par2)
# Delete in increasing numerical order. That means that if we
# delete every one, it will be the same par number each time.
# Whenever we don't delete one, we increment parnum.
parnum = par1
do i = par1, par2 {
# Get parameter by number.
call tbhgnp (tp, parnum, keyword, dtype, text)
if (dtype == 0)
call error (1, "tu_delpar: keyword miscount")
if ( verify ) {
# Change data type to a char.
switch (dtype) {
case TY_REAL:
char_type = 'r'
case TY_INT:
char_type = 'i'
case TY_DOUBLE:
char_type = 'd'
case TY_BOOL:
char_type = 'b'
default:
char_type = 't'
}
# Ask for confirmation before deleting.
call clputb ("go_ahead", clgetb ("delete_default"))
call eprintf (
"The following parameter is to be deleted:\n")
call eprintf ("%-8s %c %s\n")
call pargstr (keyword)
call pargc (char_type)
call pargstr (text)
call eprintf (" ... OK to delete")
call flush (STDERR)
if (clgetb ("go_ahead")) {
call tbhdel (tp, parnum) # delete it
modified = true
} else {
parnum = parnum + 1 # point to next parameter
}
} else {
# Delete without asking for confirmation.
call tbhdel (tp, parnum)
modified = true
}
}
# Note that we may save this instruction even if the parameter
# was not found in the current table.
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
end
# tu_replpar -- replace a parameter
# Replace an existing parameter, specified either by name or by number.
# The instruction and the replacement string will be saved in the instruction
# buffer if appropriate. Neither will be saved, however, if the keyword is
# not found in the first table. (This is in contrast to the behavior of the
# delete instruction.) The user will be prompted for confirmation if the
# input is not redirected and the instruction was not 'r!'.
procedure tu_replpar (tp, linebuf, ip, prompt, from_stdin,
verify, save_instr, isbuf, bufsize, ibp,
modified, done)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # i: index in linebuf
bool prompt # i: prompt for input?
bool from_stdin # i: get instructions from STDIN?
bool verify # i: ask for verification before deleting?
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
bool modified # io: set to true if parameter was replaced
bool done # io: set to false if done with current table
#--
char keyword[SZ_KEYWORD] # keyword for parameter
char text[SZ_PARREC] # buffer for value of parameter
char rtext[SZ_PARREC] # replacement value for a parameter
char char_type # data type as a letter
int dtype # data type (TY_CHAR, etc)
int par1, par2 # range of keywords to replace
int i # loop index for keyword number
int ctowrd(), tu_gline(), tu_rd_instr()
bool clgetb()
begin
i = ip
if (ctowrd (linebuf, i, keyword, SZ_KEYWORD) <= 0) {
call eprintf ("syntax: r keyword (or r parnum)\n")
return
}
# Save the instruction; the replacement value(s) will be
# saved within the loop over keyword number.
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
# Get the parameter numbers, 0,-1 if not found.
call tu_parnum (tp, linebuf[ip], par1, par2)
do i = par1, par2 {
# Get parameter by number.
call tbhgnp (tp, i, keyword, dtype, text)
if (dtype == 0)
call error (1, "tu_replpar: keyword miscount")
# Change data type to a char.
switch (dtype) {
case TY_REAL:
char_type = 'r'
case TY_INT:
char_type = 'i'
case TY_DOUBLE:
char_type = 'd'
case TY_BOOL:
char_type = 'b'
default:
char_type = 't'
}
if (prompt) {
# Display current value.
call eprintf (
"keyword %s, type %c; give replacement value:\n")
call pargstr (keyword)
call pargc (char_type)
call eprintf ("%s\n")
call pargstr (text)
}
# Read replacement text, either from STDIN or from instr buffer.
if (from_stdin) {
if (tu_gline (STDIN, rtext) == EOF) {
done = true
return
}
} else {
if (tu_rd_instr (Memc[isbuf], ibp, rtext) == EOF) {
done = true
return
}
}
# Tab is saved in the instruction buffer to mean that the
# value should not be changed. This allows replacing a value
# with blanks.
if (rtext[1] == EOS) {
call eprintf ("no action taken\n")
call strcpy ("\t", rtext, SZ_PARREC)
} else if (rtext[1] == '\t') {
;
} else if (verify) {
# Prompt for confirmation.
call clputb ("go_ahead", clgetb ("delete_default"))
call eprintf ("Current parameter and its replacement are:\n")
call eprintf ("%-8s %c %s\n")
call pargstr (keyword)
call pargc (char_type)
call pargstr (text)
call eprintf ("%-8s %c %s\n")
call pargstr (keyword)
call pargc (char_type)
call pargstr (rtext)
call eprintf (" ... OK to replace")
call flush (STDERR)
if (clgetb ("go_ahead")) {
call tbhpnp (tp, i, keyword, dtype, rtext) # replace it
modified = true
} else {
call eprintf ("not replaced\n")
}
} else {
# Replace the value without prompting.
call tbhpnp (tp, i, keyword, dtype, rtext)
modified = true
}
# Save the replacement value.
if (save_instr)
call tu_save_instr (rtext, isbuf, bufsize, ibp)
}
end
# tu_ch_name -- change keyword name
# Replace the name of an existing keyword without changing either the
# value or comment.
# The instruction and the replacement string will be saved in the instruction
# buffer if appropriate. Neither will be saved, however, if the keyword is
# not found in the first table. The user will be prompted for confirmation
# if the input is not redirected and the instruction was not 'k!'.
procedure tu_ch_name (tp, linebuf, ip,
save_instr, isbuf, bufsize, ibp,
modified)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # i: index in linebuf
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
bool modified # io: set to true if parameter was replaced
#--
char oldkey[SZ_KEYWORD] # current keyword
char newkey[SZ_KEYWORD+1] # new keyword; extra space for testing length
int i
int parnum # parameter specified by number (zero)
bool insufficient_input # true if not enough input was given
int ctowrd(), strlen()
errchk tbhckn
begin
i = ip
insufficient_input = false # initial value
if (ctowrd (linebuf, i, oldkey, SZ_KEYWORD) <= 0)
insufficient_input = true
if (ctowrd (linebuf, i, newkey, SZ_KEYWORD+1) <= 0)
insufficient_input = true
if (insufficient_input) {
call eprintf ("syntax: k oldkey newkey\n")
return
}
if (strlen (newkey) > SZ_KEYWORD) {
call eprintf ("new keyword name is too long; limit is %d\n")
call pargi (SZ_KEYWORD)
return
}
# Save the instruction.
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
# Replace the keyword name.
parnum = 0
call tbhckn (tp, oldkey, parnum, newkey)
modified = true
end
# tu_listpar -- list parameters
# Either all parameters or a range of parameters specified by number
# may be displayed. The parameter numbers may optionally be displayed.
procedure tu_listpar (tp, linebuf, ip, incl_num,
save_instr, isbuf, bufsize, ibp)
pointer tp # i: pointer to table descriptor
char linebuf[ARB] # i: input line
int ip # io: index in linebuf
bool incl_num # i: include number when listing parameters?
bool save_instr # i: save instruction?
pointer isbuf # io: pointer to instruction buffer
int bufsize # io: current size of instruction buffer
int ibp # io: current index in instruction buffer
#--
char keyword[SZ_KEYWORD] # keyword for parameter
char text[SZ_PARREC] # buffer for value of parameter
char comment[SZ_PARREC] # buffer for comment, if any
char char_type # data type as a letter
int dtype # data type (a character constant)
int j1, j2 # loop bounds: first & last par numbers
int npar # current number of parameters
int k # loop index
int tbpsta()
int ctoi()
begin
npar = tbpsta (tp, TBL_NPAR)
# Get the range of keywords to list.
if (ctoi (linebuf, ip, j1) <= 0) {
j1 = 1
j2 = npar
} else if (ctoi (linebuf, ip, j2) <= 0) {
j2 = j1
}
if (j2 < j1) {
k = j1 # swap j1, j2
j1 = j2
j2 = k
}
if (j1 > npar || j2 < 1) {
call eprintf ("out of range; max is %d\n")
call pargi (npar)
j1 = 1 # so loop will not be executed
j2 = 0
}
j1 = max (j1, 1)
j2 = min (j2, npar)
do k = j1, j2 {
call tbhgnp (tp, k, keyword, dtype, text)
call tbhgcm (tp, keyword, comment, SZ_PARREC)
# Change data type to a char.
switch (dtype) {
case TY_REAL:
char_type = 'r'
case TY_INT:
char_type = 'i'
case TY_DOUBLE:
char_type = 'd'
case TY_BOOL:
char_type = 'b'
default:
char_type = 't'
}
if (incl_num) { # include keyword number
call printf ("%2d ")
call pargi (k)
}
call printf ("%-8s %c")
call pargstr (keyword)
call pargc (char_type)
if (comment[1] == EOS) {
if (dtype == TY_BOOL && text[1] == '1') {
call printf (" yes\n")
} else if (dtype == TY_BOOL && text[1] == '0') {
call printf (" no\n")
} else {
call printf (" %s\n")
call pargstr (text)
}
} else { # also print comment
if (char_type == 't') {
call printf (" '%s'") # enclose text in quotes
call pargstr (text)
} else if (dtype == TY_BOOL) {
if (text[1] == '1') {
call printf (" yes")
} else if (text[1] == '0') {
call printf (" no")
} else {
call printf (" %s")
call pargstr (text)
}
} else {
call printf (" %s") # no quotes needed
call pargstr (text)
}
call printf (" %s\n")
call pargstr (comment)
}
}
if (save_instr)
call tu_save_instr (linebuf, isbuf, bufsize, ibp)
end
# tu_gline -- getline without newline
# Read a line using getline, and replace the newline character with EOS.
# Either EOF or the number of char read before the newline will be returned.
int procedure tu_gline (fd, linebuf)
int fd # i: identifies input file
char linebuf[ARB] # o: output buffer for text that was read
#--
int istat
int k
int getline()
begin
istat = getline (fd, linebuf)
if (istat == EOF)
return (istat)
k = 1
while (linebuf[k] != EOS) {
if (linebuf[k] == '\n') {
linebuf[k] = EOS
break
}
k = k + 1
}
return (k-1)
end
# tu_parnum -- get parameter number
# Either one or a pair of keywords may be given as input in the string
# 'keyword', and each may be specified either by name or by number.
# This routine reads the numbers and/or names and converts keyword
# names to numbers. If the parameter (or either of two) is not found, or
# if the number is larger than the number of header keywords, par1
# will be set to 0 and par2 to -1. If only one keyword is given, par2
# will be set equal to par1.
procedure tu_parnum (tp, keyword, par1, par2)
pointer tp # i: pointer to table descriptor
char keyword[ARB] # i: keyword name or number
int par1 # o: number of first parameter to delete
int par2 # o: number of last parameter to delete
#--
char key1[SZ_KEYWORD], key2[SZ_KEYWORD]
int ip # counter within keyword
int ipi # counter in key1 or key2
int temp # for swapping par1 & par2
int npar # total number of header parameters
int nchar
int ctowrd(), ctoi(), tbpsta()
begin
npar = tbpsta (tp, TBL_NPAR)
# Default values so a loop from par1 to par2 will not execute.
par1 = 0
par2 = -1
# Extract the first (and possibly only) word.
ip = 1
nchar = ctowrd (keyword, ip, key1, SZ_KEYWORD)
if (nchar < 1)
return # nothing given
# Interpret the first word as a number or name. First try to
# read it as an integer. If it's not an integer, or if there's
# something after an integer part (e.g. key1 = "37test"), then
# treat it as a keyword name.
ipi = 1
nchar = ctoi (key1, ipi, par1)
if ( (nchar <= 0) || (key1[ipi] != EOS) )
call tbhfkw (tp, key1, par1) # get the par number
if (par1 < 1) {
call eprintf ("warning: keyword `%s' not found\n")
call pargstr (key1)
return
}
nchar = ctowrd (keyword, ip, key2, SZ_KEYWORD) # read second word
if (nchar < 1) {
par2 = par1 # there was only one word
} else {
ipi = 1
nchar = ctoi (key2, ipi, par2)
if ( (nchar <= 0) || (key2[ipi] != EOS) )
call tbhfkw (tp, key2, par2)
if (par2 < 1) {
call eprintf ("warning: keyword `%s' not found\n")
call pargstr (key2)
return
}
if (par1 > par2) {
temp = par2
par2 = par1
par1 = temp
}
}
if (par1 > npar || par2 > npar) {
call eprintf (
"there are only %d header parameters; no action taken\n")
call pargi (npar)
par1 = 0
par2 = -1
}
end
# tu_partype -- get data type of parameter
# This routine looks for the given keyword in the header. If it is found,
# the data type (integer code) is returned as dtype. If not, dtype is set
# to the default TY_CHAR, and found is set to false.
procedure tu_partype (tp, keyword, dtype, found)
pointer tp # i: pointer to table descriptor
char keyword[ARB] # i: keyword name
int dtype # o: data type (TY_INT, TY_CHAR, ...)
bool found # o: true if keyword was found in header.
#--
char kwrd[SZ_KEYWORD] # keyword returned by tbhgnp
char value[SZ_PARREC] # buffer for value of parameter
int parnum
begin
# Get the keyword number, or zero if it isn't in the header.
call tbhfkw (tp, keyword, parnum)
# Get the data type, ignoring the value.
if (parnum > 0) {
call tbhgnp (tp, parnum, kwrd, dtype, value)
found = true
} else {
dtype = TY_CHAR # default
found = false
}
end
|