aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imutil/src/nhedit.x
blob: 1e9300c1c4ccb47370cc38c4c26da5d13c23b04d (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<evexpr.h>
include	<imset.h>
include	<ctype.h>
include	<lexnum.h>

define	LEN_USERAREA	28800		# allow for the largest possible header
define	SZ_IMAGENAME	63		# max size of an image name
define	SZ_FIELDNAME	31		# max size of a field name
define  HRECLEN         80

define	OP_EDIT		1		# hedit opcodes
define	OP_INIT		2		
define	OP_ADD		3
define	OP_DELETE	4
define  OP_DEFPAR       5
define  OP_RENAME       6
define  BEFORE		1
define  AFTER           2


# NHEDIT -- Edit or view selected fields of an image header or headers.  This
# editor performs a single edit operation upon a relation, e.g., upon a set
# of fields of a set of images.  Templates and expressions may be used to 
# automatically select the images and fields to be edited, and to compute
# the new value of each field.

procedure t_nhedit()

pointer	fields			# template listing fields to be processed
pointer	valexpr			# the value expression (if op=edit|add)

bool	noupdate, quit
int	imlist, nfields, up, min_lenuserarea
pointer	sp, field, comment, sections, im, ip, image, buf
pointer cmd, pkey
int	operation, verify, show, update, fd, baf
int     dp_oper, dp_update, dp_verify, dp_show	

pointer	immap()
bool    streq()
int	imtopenp(), imtgetim(), getline(), nowhite()
int	envfind(), ctoi(), open()

begin
	call smark (sp)
	call salloc (buf,       SZ_FNAME, TY_CHAR)
	call salloc (image,     SZ_FNAME, TY_CHAR)
	call salloc (field,     SZ_FNAME, TY_CHAR)
	call salloc (fields,    SZ_FNAME, TY_CHAR)
	call salloc (pkey,      SZ_FNAME, TY_CHAR)
	call salloc (valexpr,   SZ_LINE,  TY_CHAR)
	call salloc (comment,   SZ_LINE,  TY_CHAR)
	call salloc (sections,  SZ_FNAME, TY_CHAR)
	call salloc (cmd,       SZ_LINE,  TY_CHAR)

	# Get the primary operands.
	imlist = imtopenp ("images")

	# Determine type of operation to be performed (default is edit).

	# Do we have a command file instead of a command line?  Allow either
	# a null string or the string "NULL" to indicate we don't.

        call clgstr ("comfile", Memc[fields], SZ_LINE)
	if (nowhite (Memc[fields], Memc[fields], SZ_LINE) == 0 ||
            streq (Memc[fields], "NULL")) {
	        call he_getpars (operation, fields, valexpr, Memc[comment], 
	            Memc[pkey], baf, update, verify, show)
	        fd = 0
        } else {
	    call he_getpars (dp_oper, NULL, valexpr, Memc[comment], 
	        Memc[pkey], baf, dp_update, dp_verify, dp_show)
	    fd = open(Memc[fields], READ_ONLY, TEXT_FILE)
        }

	# Main processing loop.  An image is processed in each pass through
	# the loop.

	while (imtgetim (imlist, Memc[image], SZ_FNAME) != EOF) {

	    # set the length of the user area
	    if (envfind ("min_lenuserarea", Memc[sections], SZ_FNAME) > 0) {
		up = 1
		if (ctoi (Memc[sections], up, min_lenuserarea) <= 0)
		    min_lenuserarea = LEN_USERAREA
		else
		    min_lenuserarea = max (LEN_USERAREA, min_lenuserarea)
	    } else
		min_lenuserarea = LEN_USERAREA

	    # Open the image.
	    iferr {
		if (update == YES || fd != 0)
		    im = immap (Memc[image], READ_WRITE,  min_lenuserarea)
		else
		    im = immap (Memc[image], READ_ONLY,  min_lenuserarea)
	    } then {
		call erract (EA_WARN)
		next
	    }
            
	    if (fd != 0) {
	        # Open the command file and start processing each line. 
                # rewind file before  proceeding

	        call seek(fd, BOF)
                while (getline(fd, Memc[cmd]) != EOF) {
		    for (ip=cmd;  IS_WHITE(Memc[ip]);  ip=ip+1)
			    ;
		    if (Memc[cmd] == '#' || Memc[ip] == '\n')
		        next

                    call he_getcmdf (Memc[cmd], operation, Memc[fields],
		        Memc[valexpr], Memc[comment], Memc[pkey], baf, 
			update, verify, show)

		    # Set the default parameters for the command file.
                    if (operation < 0) {
			dp_oper = -operation
			if (update != -1)
			    dp_update = update
			if (verify != -1)
			    dp_verify = verify
			if (show != -1)
			    dp_show = show
                        next
		    }

                    # Set the parameters for the current command, the
                    # command parameters take precedence over the defaults.
                    call nh_setpar (operation, dp_oper, dp_update, 
			   dp_verify, dp_show, update, verify, show)

		    iferr (call nh_edit (im, Memc[image], operation,
			Memc[fields], Memc[valexpr], Memc[comment],
			Memc[pkey], baf, update, verify, show, nfields))
			call erract (EA_WARN)

                }

            } else
                iferr (call nh_edit (im, Memc[image], operation, Memc[fields],
	            Memc[valexpr], Memc[comment], Memc[pkey], baf, update,
		    verify, show, nfields))
		    call erract (EA_WARN)

	    # Update the image header and unmap the image.

	    noupdate = false
	    quit = false

	    if (update == YES) {
		if (nfields == 0 && fd == 0)
		    noupdate = true
		else if (verify == YES) {
		    call eprintf ("update %s ? (yes): ")
			call pargstr (Memc[image])
		    call flush (STDERR)

		    if (getline (STDIN, Memc[buf]) == EOF)
			noupdate = true
		    else {
			# Strip leading whitespace and trailing newline.
			for (ip=buf;  IS_WHITE(Memc[ip]);  ip=ip+1)
			    ;
			if (Memc[ip] == 'q') {
			    quit = true
			    noupdate = true
			} else if (! (Memc[ip] == '\n' || Memc[ip] == 'y'))
			    noupdate = true
		    }
		}

		if (noupdate) {
		    call imseti (im, IM_WHEADER, NO)
		    call imunmap (im)
		} else {
		    call imunmap (im)
		    if (show == YES) {
			call printf ("%s updated\n")
			    call pargstr (Memc[image])
		    }
		}
	    } else {
		call imunmap (im)
	    }

	    call flush (STDOUT)
	    if (quit)
		break
	} #end of while

	# Close command file
        if (fd != 0)
            call close(fd)
	call imtclose (imlist)
	call sfree (sp)
end


# NH_EDIT -- Edit the field in the image header. 

procedure nh_edit (im, image, operation, keyws, exprs, comment, pkey, baf,
	                   update, verify, show, nfields)

pointer im		#I image descriptor
char    image[ARB]      #  
int	operation	#I operation code
char    keyws[ARB]      # Memc[fields]
char    exprs[ARB]	# Memc[valexpr]
char    comment[ARB]	# Memc[comment]
char    pkey[ARB]	# 
int	baf
int	update
int	verify
int	show
int	nfields

pointer sp, field
int	imgnfn(), imofnlu()
int	flist

begin

	call smark(sp)
	call salloc (field, SZ_FNAME, TY_CHAR)
	   
	if (operation == OP_INIT || operation == OP_ADD) {
	    # Add a field to the image header.  This cannot be done within
	    # the IMGNFN loop because template expansion on the existing
	    # fields of the image header would discard the new field name
	    # since it does not yet exist.

	    nfields = 1
	    call he_getopsetimage (im, image, keyws)
	    switch (operation) {
	    case OP_INIT:
	        call nh_initfield (im, image, keyws, exprs, comment, 
		    pkey, baf, verify, show, update)
	    case OP_ADD:
	        call nh_addfield (im, image, keyws, exprs, comment,
		    pkey, baf, verify, show, update)
	    }
	} else {
	    # Open list of fields to be processed.
	    flist = imofnlu (im, keyws)
	    nfields = 0
	    while (imgnfn (flist, Memc[field], SZ_FNAME) != EOF) {
		call he_getopsetimage (im, image, Memc[field])

		switch (operation) {
		case OP_EDIT:
		    call nh_editfield (im, image, Memc[field],
			exprs, comment, verify, show, update)
		case OP_RENAME:
		    call nh_renamefield (im, image, Memc[field],
			exprs, verify, show, update)
		case OP_DELETE:
		    call nh_deletefield (im, image, Memc[field],
			exprs, verify, show, update)
		}
		nfields = nfields + 1
	    }

	    call imcfnl (flist)
	}
	call sfree(sp)
end


# NH_EDITFIELD -- Edit the value of the named field of the indicated image.
# The value expression is evaluated, interactively inspected if desired,
# and the resulting value put to the image.

procedure nh_editfield (im, image, field, valexpr, comment, verify, 
    show, update)

pointer	im			# image descriptor of image to be edited
char	image[ARB]		# name of image to be edited
char	field[ARB]		# name of field to be edited
char	valexpr[ARB]		# value expression
char	comment[ARB]		# keyword comment
int	verify			# verify new value interactively
int	show			# print record of edit
int	update			# enable updating of the image

int	goahead, nl
pointer	sp, ip, oldval, newval, defval, o, fcomm, ncomm

bool	streq()
pointer	evexpr()
extern	he_getop()
int	getline(), imaccf(), strldxs(), locpr()
errchk	evexpr, getline, imaccf, he_gval

begin
	call smark (sp)
	call salloc (oldval, SZ_LINE, TY_CHAR)
	call salloc (newval, SZ_LINE, TY_CHAR)
	call salloc (defval, SZ_LINE, TY_CHAR)
	call salloc (fcomm, HRECLEN, TY_CHAR)
	call salloc (ncomm, HRECLEN, TY_CHAR)

	call strcpy (comment, Memc[ncomm], HRECLEN)

	# Verify that the named field exists before going any further.
	if (field[1] != '$')
	    if (imaccf (im, field) == NO) {
		call eprintf ("parameter %s,%s not found\n")
		    call pargstr (image)
		    call pargstr (field)
		call sfree (sp)
		return
	    }

	# Get the old value.
	call he_gval (im, image, field, Memc[oldval], SZ_LINE)

	# Evaluate the expression.  Encode the result operand as a string.
	# If the expression is not parenthesized, assume that is is already
	# a string literal.

	if (valexpr[1] == '(') {
	    o = evexpr (valexpr, locpr (he_getop), 0)
	    call he_encodeop (o, Memc[newval], SZ_LINE)
	    call xev_freeop (o)
	    call mfree (o, TY_STRUCT)
	} else
	    call strcpy (valexpr, Memc[newval], SZ_LINE)

	call imgcom (im, field, Memc[fcomm])
	if (streq (Memc[newval], ".") && streq (comment, ".")) {
	    # Merely print the value of the field.

	    if (Memc[fcomm] == EOS) {
	    	call printf ("%s,%s = %s\n")
		call pargstr (image)
		call pargstr (field)
		call he_pargstr (Memc[oldval])
            } else {
	        call strcpy (Memc[oldval], Memc[newval], SZ_LINE)
		call printf ("%s,%s = %s / %s\n")
		call pargstr (image)
		call pargstr (field)
		call he_pargstr (Memc[oldval])
		call pargstr(Memc[fcomm])
	    }

	} else if (verify == YES) {
	    # Query for new value and edit the field.  If the response is a
	    # blank line, use the default new value.  If the response is "$"
	    # or EOF, do not change the value of the parameter.

	    if (streq (Memc[newval], ".")) {
		call strcpy (Memc[oldval], Memc[newval], SZ_LINE)
	    }
	    if (streq (comment, ".")) 
	        call strcpy (Memc[fcomm], Memc[ncomm], SZ_LINE)
	    call strcpy (Memc[newval], Memc[defval], SZ_LINE)
	    call eprintf ("%s,%s (%s -> %s): ")
		call pargstr (image)
		call pargstr (field)
		call nh_pargstrc (Memc[oldval], Memc[fcomm])
		call nh_pargstrc (Memc[defval], Memc[ncomm]) 
	    call flush (STDERR)

	    if (getline (STDIN, Memc[newval]) != EOF) {
		# Do not skip leading whitespace; may be significant in a
		# string literal.

		ip = newval

		# Do strip trailing newline since it is an artifact of getline.
		nl = strldxs ("\n", Memc[ip]) 
		if (nl > 0)
		    Memc[ip+nl-1] = EOS

		# Decode user response.
		if (Memc[ip] == '\\') {
		    ip = ip + 1
		    goahead = YES
		} else if (streq(Memc[ip],"n") || streq(Memc[ip],"no")) {
		    goahead = NO
		} else if (streq(Memc[ip],"y") || streq(Memc[ip],"yes") ||
		    Memc[ip] == EOS) {
		    call strcpy (Memc[defval], Memc[newval], SZ_LINE)
		    goahead = YES
		} else {
		    if (ip > newval)
			call strcpy (Memc[ip], Memc[newval], SZ_LINE)
		    goahead = YES
		}

		# Edit field if so indicated.
		if (goahead == YES && update == YES)
		    call nh_updatefield (im, image, field, Memc[oldval],
			Memc[newval], Memc[fcomm], Memc[ncomm], show)

		call flush (STDOUT)
	    }

	} else {
	    if (streq (Memc[newval], ".")) {
		call strcpy (Memc[oldval], Memc[newval], SZ_LINE)
	    }
	    if (streq (comment, ".")) 
	        call strcpy (Memc[fcomm], Memc[ncomm], SZ_LINE)
	    if (update == YES) {
		call nh_updatefield (im, image, field, Memc[oldval],
                      Memc[newval], Memc[fcomm], Memc[ncomm], show)
            }
	}
	if (update == NO && show == YES) {
	    call printf ("%s,%s: %s -> %s\n")
		call pargstr (image)
		call pargstr (field)
		call nh_pargstrc (Memc[oldval], Memc[fcomm])
		call nh_pargstrc (Memc[newval], Memc[ncomm])
	}

	call sfree (sp)
end


# NH_RENAMEFIELD -- Rename the named field of the indicated image.
# The value expression is evaluated, interactively inspected if desired,
# and the resulting value put to the image.

procedure nh_renamefield (im, image, field, valexpr, verify, show, update)

pointer	im			# image descriptor of image to be edited
char	image[ARB]		# name of image to be edited
char	field[ARB]		# name of field to be edited
char	valexpr[ARB]		# value expression
int	verify			# verify new value interactively
int	show			# print record of edit
int	update			# enable updating of the image

int	goahead, nl
pointer	sp, ip, oldval, newval, defval, o

bool	streq()
pointer	evexpr()
extern	he_getop()
int	getline(), imaccf(), strldxs(), locpr()
errchk	evexpr, getline, imaccf, he_gval

begin
	call smark (sp)
	call salloc (oldval, SZ_LINE, TY_CHAR)
	call salloc (newval, SZ_LINE, TY_CHAR)
	call salloc (defval, SZ_LINE, TY_CHAR)

	# Verify that the named field exists before going any further.
	if (field[1] != '$')
	    if (imaccf (im, field) == NO) {
		call eprintf ("parameter %s,%s not found\n")
		    call pargstr (image)
		    call pargstr (field)
		call sfree (sp)
		return
	    }

	# Get the old value.
	call he_gval (im, image, field, Memc[oldval], SZ_LINE)

	# Evaluate the expression.  Encode the result operand as a string.
	# If the expression is not parenthesized, assume that is is already
	# a string literal.

	if (valexpr[1] == '(') {
	    o = evexpr (valexpr, locpr (he_getop), 0)
	    call he_encodeop (o, Memc[newval], SZ_LINE)
	    call xev_freeop (o)
	    call mfree (o, TY_STRUCT)
	} else
	    call strcpy (valexpr, Memc[newval], SZ_LINE)
	call strupr (Memc[newval])

	if (verify == YES) {
	    # Query for new value and edit the field.  If the response is a
	    # blank line, use the default new value.  If the response is "$"
	    # or EOF, do not change the value of the parameter.

	    call strcpy (field, Memc[oldval], SZ_LINE)
	    if (streq (Memc[newval], "."))
		call strcpy (Memc[oldval], Memc[newval], SZ_LINE)
	    call strcpy (Memc[newval], Memc[defval], SZ_LINE)
	    call eprintf ("%s,%s (%s -> %s): ")
		call pargstr (image)
		call pargstr (field)
		call pargstr (field)
		call pargstr (Memc[newval])
	    call flush (STDERR)

	    if (getline (STDIN, Memc[newval]) != EOF) {
		# Do not skip leading whitespace; may be significant in a
		# string literal.

		ip = newval

		# Do strip trailing newline since it is an artifact of getline.
		nl = strldxs ("\n", Memc[ip]) 
		if (nl > 0)
		    Memc[ip+nl-1] = EOS

		# Decode user response.
		if (Memc[ip] == '\\') {
		    ip = ip + 1
		    goahead = YES
		} else if (streq(Memc[ip],"n") || streq(Memc[ip],"no")) {
		    goahead = NO
		} else if (streq(Memc[ip],"y") || streq(Memc[ip],"yes") ||
		    Memc[ip] == EOS) {
		    call strcpy (Memc[defval], Memc[newval], SZ_LINE)
		    goahead = YES
		} else {
		    if (ip > newval)
			call strcpy (Memc[ip], Memc[newval], SZ_LINE)
		    goahead = YES
		}

		# Edit field if so indicated.
		if (goahead == YES && update == YES)
		    call nh_updatekey (im, image, field, Memc[newval], show)

		call flush (STDOUT)
	    }

	} else {
	    call strcpy (field, Memc[oldval], SZ_LINE)
	    if (update == YES)
		call nh_updatekey (im, image, field, Memc[newval], show)
	}
	if (update == NO && show == YES) {
	    call printf ("%s,%s: %s -> %s\n")
		call pargstr (image)
		call pargstr (field)
		call pargstr (field)
		call pargstr (Memc[newval])
	}

	call sfree (sp)
end


# NH_INITFIELD -- Add a new field to the indicated image.  If the field already
# existsdo not set its value.  The value expression is evaluated and the
# resulting value used as the initial value in adding the field to the image.

procedure nh_initfield (im, image, field, valexpr, comment, pkey, baf,
       verify, show, update)

pointer	im			# image descriptor of image to be edited
char	image[ARB]		# name of image to be edited
char	field[ARB]		# name of field to be edited
char	valexpr[ARB]		# value expression
char	comment[ARB]		# keyword comment
char	pkey[ARB]		# 
int	baf
int	verify			# verify new value interactively
int	show			# print record of edit
int	update			# enable updating of the image

bool	numeric
int	numlen, ip
pointer	sp, newval, o
pointer	evexpr()
int	imaccf(), locpr(), strlen(), lexnum()
extern	he_getop()
errchk	imaccf, evexpr, imakbc, imastrc, imakic, imakrc

begin
	call smark (sp)
	call salloc (newval, SZ_LINE, TY_CHAR)

	# If the named field already exists, this is really an edit operation
	# rather than an add.  Call editfield so that the usual verification
	# can take place.

	if (imaccf (im, field) == YES) {
	    call eprintf ("parameter %s,%s already exists\n")
	        call pargstr (image)
	        call pargstr (field)
	    call sfree (sp)
	    return
	}

	# If the expression is not parenthesized, assume that is is already
	# a string literal.  If the expression is a string check for a simple
	# numeric field.

	ip = 1
	numeric = (lexnum (valexpr, ip, numlen) != LEX_NONNUM)
	if (numeric)
	    numeric = (numlen == strlen (valexpr))

	if (numeric || valexpr[1] == '(')
	    o = evexpr (valexpr, locpr(he_getop), 0)
	else {
	    call malloc (o, LEN_OPERAND, TY_STRUCT)
	    call xev_initop (o, strlen(valexpr), TY_CHAR)
	    call strcpy (valexpr, O_VALC(o), ARB)
	}

	# Add the field to the image (or update the value).  The datatype of
	# the expression value operand determines the datatype of the new
	# parameter.

	if (update == YES) {
	    switch (O_TYPE(o)) {
	    case TY_BOOL:
	        if (pkey[1] != EOS && baf != 0)
	            call imakbci (im, field, O_VALB(o), comment, pkey, baf)
	        else   
	            call imakbc (im, field, O_VALB(o), comment)
	    case TY_CHAR:
	        if (pkey[1] != EOS && baf != 0)
	            call imastrci (im, field, O_VALC(o), comment, pkey, baf)
	        else
	            call imastrc (im, field, O_VALC(o), comment)
	    case TY_INT:
	        if (pkey[1] != EOS && baf != 0)
	            call imakici (im, field, O_VALI(o), comment, pkey, baf)
	        else
	            call imakic (im, field, O_VALI(o), comment)
	    case TY_REAL:
	        if (pkey[1] != EOS && baf != 0)
	            call imakrci (im, field, O_VALR(o), comment, pkey, baf)
	        else
	            call imakrc (im, field, O_VALR(o), comment)
	    default:
	        call error (1, "unknown expression datatype")
	    }
	}

	if (show == YES) {
	    call he_encodeop (o, Memc[newval], SZ_LINE)
	    call printf ("add %s,%s = %s / %s\n")
		call pargstr (image)
		call pargstr (field)
		call he_pargstr (Memc[newval])
		call pargstr(comment)
	}

	call xev_freeop (o)
	call mfree (o, TY_STRUCT)
	call sfree (sp)
end


# NH_ADDFIELD -- Add a new field to the indicated image.  If the field already
# exists, merely set its value.  The value expression is evaluated and the
# resulting value used as the initial value in adding the field to the image.

procedure nh_addfield (im, image, field, valexpr, comment, pkey, baf,
            verify, show, update)

pointer	im			# image descriptor of image to be edited
char	image[ARB]		# name of image to be edited
char	field[ARB]		# name of field to be edited
char	valexpr[ARB]		# value expression
char	comment[ARB]		# keyword comment
char	pkey[ARB]		# pivot keyword name
int	baf			# either BEFORE or AFTER value
int	verify			# verify new value interactively
int	show			# print record of edit
int	update			# enable updating of the image

bool	numeric
int	numlen, ip
pointer	sp, newval, o
pointer	evexpr()
bool    streq()
int	imaccf(), locpr(), strlen(), lexnum()
extern	he_getop()
errchk	imaccf, evexpr, imakbc, imastrc, imakic, imakrc

begin
	call smark (sp)
	call salloc (newval, SZ_LINE, TY_CHAR)

	# If the named field already exists, this is really an edit operation
	# rather than an add.  Call editfield so that the usual verification
	# can take place.
        if (!streq(field, "comment") && !streq(field, "history")) {
	    if (imaccf (im, field) == YES) {
	        call nh_editfield (im, image, field, valexpr, comment,
	             verify, show, update)
	        call sfree (sp)
	        return
	    }
	}

	# If the expression is not parenthesized, assume that is is already
	# a string literal.  If the expression is a string check for a simple
	# numeric field.

	ip = 1
	numeric = (lexnum (valexpr, ip, numlen) != LEX_NONNUM)
	if (numeric)
	    numeric = (numlen == strlen (valexpr))

	if (numeric || valexpr[1] == '(')
	    o = evexpr (valexpr, locpr(he_getop), 0)
	else {
	    call malloc (o, LEN_OPERAND, TY_STRUCT)
	    call xev_initop (o, max(1,strlen(valexpr)), TY_CHAR)
	    call strcpy (valexpr, O_VALC(o), SZ_LINE)
	}

	# Add the field to the image (or update the value).  The datatype of
	# the expression value operand determines the datatype of the new
	# parameter.
	if (update == YES) {
	    switch (O_TYPE(o)) {
	    case TY_BOOL:
	        if (pkey[1] != EOS && baf != 0)
	            call imakbci (im, field, O_VALB(o), comment, pkey, baf)
	        else
	            call imakbc (im, field, O_VALB(o), comment)
	    case TY_CHAR:
	        if (streq(field, "comment") || 
	            streq(field, "history") ||
	            streq(field, "add_textf") ||
		    streq(field, "add_blank")) {
                        if (streq(field, "add_textf")) {
	                    call imputextf (im, O_VALC(o), pkey, baf) 
		        } else {
	                    call imphis (im, field, O_VALC(o), pkey, baf) 
	                }
	        } else if (pkey[1] != EOS && baf != 0) {
	            call imastrci (im, field, O_VALC(o), comment, pkey, baf)
	        } else {
	            call imastrc (im, field, O_VALC(o), comment)
	        }
	    case TY_INT:
	        if (pkey[1] != EOS && baf != 0)
	            call imakici (im, field, O_VALI(o), comment, pkey, baf)
	        else
	            call imakic (im, field, O_VALI(o), comment)
	    case TY_REAL:
	        if (pkey[1] != EOS && baf != 0)
	            call imakrci (im, field, O_VALR(o), comment, pkey, baf)
	        else
	            call imakrc (im, field, O_VALR(o), comment)
	    default:
	        call error (1, "unknown expression datatype")
	    }
	}

	if (show == YES) {
	    call he_encodeop (o, Memc[newval], SZ_LINE)
	    call printf ("add %s,%s = %s / %s\n")
		call pargstr (image)
		call pargstr (field)
		call he_pargstr (Memc[newval])
		call pargstr(comment)
	}

	call xev_freeop (o)
	call mfree (o, TY_STRUCT)
	call sfree (sp)
end


# NH_DELETEFIELD -- Delete a field from the indicated image.  If the field does
# not exist, print a warning message.

procedure nh_deletefield (im, image, field, valexpr, verify, show, update)

pointer	im			# image descriptor of image to be edited
char	image[ARB]		# name of image to be edited
char	field[ARB]		# name of field to be edited
char	valexpr[ARB]		# not used
int	verify			# verify deletion interactively
int	show			# print record of edit
int	update			# enable updating of the image

pointer	sp, ip, newval
int	getline(), imaccf()

begin
	call smark (sp)
	call salloc (newval, SZ_LINE, TY_CHAR)

	if (imaccf (im, field) == NO) {
	    call eprintf ("nonexistent field %s,%s\n")
		call pargstr (image)
		call pargstr (field)
	    call sfree (sp)
	    return
	}
	    
	if (verify == YES) {
	    # Delete pending verification.

	    call eprintf ("delete %s,%s ? (yes): ")
		call pargstr (image)
		call pargstr (field)
	    call flush (STDERR)

	    if (getline (STDIN, Memc[newval]) != EOF) {
		# Strip leading whitespace and trailing newline.
		for (ip=newval;  IS_WHITE(Memc[ip]);  ip=ip+1)
		    ;
		if (Memc[ip] == '\n' || Memc[ip] == 'y') {
		    call imdelf (im, field)
		    if (show == YES) {
			call printf ("%s,%s deleted\n")
			    call pargstr (image)
			    call pargstr (field)
		    }
		}
	    }
	
	} else {
	    # Delete without verification.

            if (update == YES) {
	        iferr (call imdelf (im, field))
		    call erract (EA_WARN)
	        else if (show == YES) {
		    call printf ("%s,%s deleted\n")
		        call pargstr (image)
		        call pargstr (field)
	    } else if (show == YES)
		call printf ("%s,%s deleted, no update\n")
		    call pargstr (image)
		    call pargstr (field)
	    }
	}

	call sfree (sp)
end


# NH_UPDATEFIELD -- Update the value of an image header field.

procedure nh_updatefield (im, image, field, oldval, newval, oldcomm,
           newcomm, show)

pointer	im			# image descriptor
char	image[ARB]		# image name
char	field[ARB]		# field name
char	oldval[ARB]		# old value, encoded as a string
char	newval[ARB]		# new value, encoded as a string
char	oldcomm[ARB]		# old keyword comment
char	newcomm[ARB]		# new keyword comment
int	show			# print record of update

begin
	iferr (call impstrc (im, field, newval, newcomm)) {
	    call eprintf ("cannot update %s,%s\n")
		call pargstr (image)
		call pargstr (field)
	    return
	}
	if (show == YES) {
	    call printf ("%s,%s: %s -> %s\n")
		call pargstr (image)
		call pargstr (field)
		call nh_pargstrc (oldval, oldcomm)
		call nh_pargstrc (newval, newcomm)

	}
end


# NH_UPDATEKEY -- Update the image header field.

procedure nh_updatekey (im, image, field, newkey, show)

pointer	im			# image descriptor
char	image[ARB]		# image name
char	field[ARB]		# field name
char	newkey[ARB]		# new key
int	show			# print record of update

begin
	iferr (call imrenf (im, field, newkey)) {
	    call eprintf ("cannot update %s,%s\n")
		call pargstr (image)
		call pargstr (field)
	    return
	}
	if (show == YES) {
	    call printf ("%s,%s: %s -> %s\n")
		call pargstr (image)
		call pargstr (field)
		call pargstr (field)
		call pargstr (newkey)

	}
end


# NH_CPSTR -- Copy a string to a header record with optional comment.

procedure nh_cpstr (str, outbuf)

char    str[ARB]                	# string to be printed
char    outbuf[ARB]            		# comment string to be printed
                                                                                
int     ip
bool    quoteit
pointer sp, op, buf
                                                                                
begin
                                                                                
        call smark (sp)
        call salloc (buf, SZ_LINE, TY_CHAR)
                                                                                
        op = buf
        Memc[op] = '"'
        op = op + 1
                                                                                
        # Copy string to scratch buffer, enclosed in quotes.  Check for
        # embedded whitespace.
                                                                                
        quoteit = false
        for (ip=1;  str[ip] != EOS;  ip=ip+1) {
            if (IS_WHITE(str[ip])) {            # detect whitespace
                quoteit = true
                Memc[op] = str[ip]
            } else if (str[ip] == '\n') {       # prettyprint newlines
                Memc[op] = '\\'
                op = op + 1
                Memc[op] = 'n'
            } else                              # normal characters
                Memc[op] = str[ip]
                                                                                
            if (ip < SZ_LINE)
                op = op + 1
        }
                                                                                
        # If whitespace was seen pass the quoted string, otherwise pass the
        # original input string.
                                                                                
        if (quoteit) {
            Memc[op] = '"'
            op = op + 1
            Memc[op] = EOS
            call strcpy (Memc[buf], outbuf, SZ_LINE)
        } else
            call strcpy (str, outbuf, SZ_LINE)
                                                                                
        call sfree (sp)
end


# NH_PARGSTRC -- Pass a string to a printf statement plus the comment string.
										procedure nh_pargstrc (str, comment)

char	str[ARB]		# string to be printed
char	comment[ARB]		# comment string to be printed

pointer	sp, buf

begin

	call smark (sp)
        call salloc (buf, SZ_LINE, TY_CHAR)
                                                                                
        call nh_cpstr (str, Memc[buf])

        if (comment[1] != EOS) {
           call strcat (" / ", Memc[buf], SZ_LINE)
           call strcat (comment, Memc[buf], SZ_LINE)
        }

        call pargstr (Memc[buf])

        call sfree (sp)
end


# HE_GETPARS -- get the cl parameters for this task

procedure he_getpars (operation, fields, valexpr, comment,
	        pivot, baf, update, verify, show)
        
int	operation
pointer	fields			# template listing fields to be processed
pointer	valexpr			# the value expression (if op=edit|add)
char	comment[ARB]
char	pivot[ARB]
int	baf
int	update
int	verify
int	show
bool	clgetb(), streq()

pointer ip
int	btoi()

begin
	# Set switches.
	operation = OP_EDIT
	if (clgetb ("add"))
	    operation = OP_ADD
	else if (clgetb ("addonly"))
	    operation = OP_INIT
	else if (clgetb ("delete"))
	    operation = OP_DELETE
	else if (clgetb ("rename"))
	    operation = OP_RENAME

	# If fields is NULL then this will be done in a command file.
	if (fields != NULL) {

	    # Get list of fields to be edited, added, or deleted.
	    call clgstr ("fields", Memc[fields], SZ_LINE)
	    for (ip=fields;  IS_WHITE (Memc[ip]);  ip=ip+1)
		;
	    call strcpy (Memc[ip], Memc[fields], SZ_LINE)

	    # Set value expression.
	    Memc[valexpr] = EOS
	    if (operation != OP_DELETE) {
		call clgstr ("value", Memc[valexpr], SZ_LINE)
		if (operation != OP_RENAME)
		    call clgstr ("comment", comment, SZ_LINE)
		   
		# Justify value
		for (ip=valexpr;  IS_WHITE (Memc[ip]);  ip=ip+1)
		    ;
		call strcpy (Memc[ip], Memc[valexpr], SZ_LINE)
		ip = valexpr
		while (Memc[ip] != EOS)
		    ip = ip + 1
		while (ip > valexpr && IS_WHITE (Memc[ip-1]))
		    ip = ip - 1
		Memc[ip] = EOS
	    }

	    # If only printing results ignore the RENAME flag.
	    if (operation == OP_RENAME && streq (Memc[valexpr], ".")) {
		operation = OP_EDIT
		call strcpy (".", comment, SZ_LINE)
	    }

	} else {
	    Memc[valexpr] = EOS
	    comment[1] = EOS
	}


	# Get switches.  If the expression value is ".", meaning print value
	# rather than edit, then we do not use the switches.
	
	if (operation == OP_EDIT && streq (Memc[valexpr], ".") &&
	    streq (comment, ".")) {
	    update = NO
	    verify = NO
	    show   = NO
	} else {
	    update = btoi (clgetb ("update"))
	    verify = btoi (clgetb ("verify"))
	    show   = btoi (clgetb ("show"))
            call clgstr ("after", pivot, SZ_LINE)
            if (pivot[1] != EOS)
                baf = AFTER
            if (pivot[1] == EOS) {
                call clgstr ("before", pivot, SZ_LINE)
                if (pivot[1] != EOS)
                   baf = BEFORE
	    }
	}
end


# NH_SETPAR -- Set a parameter.

procedure nh_setpar (operation, dp_oper, dp_update, dp_verify, dp_show, 
                        update, verify, show)
int     operation
int     dp_oper
int     dp_update
int     dp_verify
int     dp_show
int     update
int     verify
int     show

begin
        # If the value is positive then the parameter has been set
        # in the command line.

	if (operation == OP_DEFPAR)
            operation = dp_oper
        if (update == -1)
            update = dp_update
	if (verify == -1)
            verify = dp_verify
        if (show == -1)
            show = dp_show
end