File: he_main.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (1150 lines) | stat: -rw-r--r-- 28,437 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/******************************************************************************
 * he - HDF editor
 *
 * This program has a long history, starting with its creation as a tool to
 * help HDF developers work at a low-level on HDF files.  It has evolved into
 * user-level program, though it is still designed for small editing tasks on
 * HDF files.
 *
 * he allows sophisticated HDF users to manipulate the elements in an HDF file.
 * These manipulations include selecting groups and showing information about
 * them, dumping them to the output, writing them to new files, deleting them,
 * inserting them, replacing, say, the palette of an r8 group, and editing the
 * text labels and descriptions of any element.
 *
 * he will NOT allow the user to *arbitrarily* modify binary data in the file
 * or any element, though it allows modification of tag and reference numbers
 * within strict constraints.  The user should not attempt to alter individual
 * bytes.  It is acceptable, however, to replace an element with another of
 * the same type.
 *
 * he can be used both interactively or in "batch" mode.  Here is a sample
 * batch program:
 *
 *      #!/bin/csh -f
 *      set file=$1
 *      shift
 *      he -batch $file -nobackup << EOF
 *      info -all -group $*
 *      close
 *      quit
 *      EOF
 *
 * This makes use of C-shell variable substitution to pass a filename to he,
 * invokes he, and then lists out on separate lines the commands to give once
 * he is running.  The $* trailing the info command is also C-shell variable
 * substitution.
 *
 * List of commands:       annotate   dump    if      open    putr8    unalias
 *                         close      getr8   info    prev    revert   wait
 *                         delete     help    next    put     select   write
 *                         display
 *
 * Predicates are of the form TAG = 3 IMAGE_SIZE < 1000  LABEL = "abc"
 * Type <command> -help for usage of <command>.  DO NOT type the command
 * without arguments and expect help.  Some commands delete objects and do not
 * need any arguments.  If you are learning to use he, try it on an expendable
 * file.
 *****************************************************************************/
/* ------ he.c ------- main() main HDF interfacing routines */

#include "hdf_priv.h"
#include "he.h"

#include <stdio.h>
#ifdef H4_HAVE_UNISTD_H
#include <unistd.h>
#endif

#if defined H4_HAVE_WIN32_API && !defined __MINGW32__
typedef int                               pid_t;
#endif

/* the return status of last command executed */
int he_status = HE_OK;

/* is this on the console or remote terminals?
   this should eventually be detected automatically */
int he_remote = YES;

/* is this batch mode or interactive? */
extern int he_batch;

int       he_currDesc;
int       he_numDesc;
int       he_numGrp;
int       he_backup;
char     *he_file = NULL;
DFdesc   *he_desc = NULL;
HE_GROUP *he_grp  = NULL;

int
main(int argc, char *argv[])
{
    int   backup   = YES; /* Backup files when opening? */
    char *fileName = NULL;

    for (int i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (findOpt(argv[i] + 1)) {
                case HE_HELP:
                    printf("he [<file>] [-nobackup] [-batch]\n");
                    help();
                    quit(0);
                    break;
                case HE_BATCH:
                    he_batch = YES;
                    break;
                case HE_REMOTE:
                    he_remote = YES;
                    break;
                case HE_NOBACKUP:
                    backup = NO;
                    break;
                case HE_BACKUP:
                    backup = YES;
                    break;
                case HE_NOTFOUND:
                    unkOpt(argv[i]);
                    quit(1); /* does not return */
                    break;
                case HE_AMBIG:
                    ambigOpt(argv[i]);
                    quit(1);
                    break;
                default:
                    irrOpt(argv[i]);
                    quit(1); /* does not return */
                    break;
            }
        }
        else {
            /* Must be a filename */
            if (!fileName)
                fileName = argv[i];
            else
                fprintf(stderr, "Single file only. %s not open.\n", argv[i]);
        }
    }

    if (NULL == (he_desc = (DFdesc *)calloc(HE_DESC_SZ, sizeof(DFdesc))))
        goto error;
    if (NULL == (he_grp = (HE_GROUP *)calloc(HE_DESC_SZ, sizeof(HE_GROUP))))
        goto error;

    /* if there is a file name given in the command line, open it */

    if (fileName)
        he_status = openFile(fileName, backup);

    /* read, execute loop */
    cmdLoop();

    if (fileOpen())
        closeFile(YES); /* close with keep */
    quit(EXIT_SUCCESS);
    return EXIT_SUCCESS;

error:
    free(he_desc);
    free(he_grp);
    quit(EXIT_FAILURE);
    return EXIT_FAILURE;
}

/* cmdLoop -- read commands and execute them */
void
cmdLoop(void)
{
    HE_CMD *cmd;

    for (cmd = getCmd(); cmd; cmd = getCmd()) {
        if (cmd->func)
            he_status = (*cmd->func)(cmd);
        else {
            fprintf(stderr, "Unknown command: %s.\n", cmd->argv[0]);
            he_status = HE_FAIL;
        }
        deleteCmd(cmd);
    }
}

int32
getElement(int desc, char **pdata)
{
    int32 length;
    int32 fid;

    length = he_desc[desc].length;

    /* alloc memory to read the element in */
    *pdata = (char *)malloc(length);
    if (*pdata == NULL)
        return FAIL;

    /* read in the element and check for error */
    if ((fid = Hopen(he_file, DFACC_READ, 0)) == -1) {
        HEprint(stderr, 0);
        return FAIL;
    }
    if (Hgetelement(fid, he_desc[desc].tag, he_desc[desc].ref, (unsigned char *)(*pdata)) < 0) {
        free(*pdata);
        fprintf(stderr, "Cannot read element.\n");
        return FAIL;
    }
    Hclose(fid);
    return length;
}

/* the tmp directory, currently set for unix */
#define TDIR "/tmp/"

int
getTmpName(char **pname)
{
    int        length;
    static int count = 0;
    char       s[32];

    int pid = getpid();
    (void)sprintf(s, "%she%d.%d", TDIR, pid, count);
    count++;

    length = (int)strlen(s);
    if (length <= 0)
        return FAIL;

    *pname = (char *)malloc(length + 1);
    strcpy(*pname, s);

    return length;
}

int
writeToFile(char *file, char *data, int32 length)
{
    FILE *fd = NULL;
    int   written;

    fd = fopen(file, "w");
    if (fd == NULL)
        goto error;

    written = (int)fwrite(data, sizeof(char), (size_t)length, fd);
    if (written != length) {
        fprintf(stderr, "Error in write.\n");
        goto error;
    }
    fclose(fd);

    return HE_OK;

error:
    if (fd != NULL)
        fclose(fd);
    return FAIL;
}

int
removeFile(char *file)
{
    return remove((const char *)file);
}

/* is a file currently opened */
int
fileOpen(void)
{
    return (he_file != NULL);
}

char *
backupName(const char *file)
{
    return catStr(file, "$hdfed$");
}

int
backupFile(char *file)
{
    char *back = NULL; /* backup file name */
    int   f    = -1;

    back = backupName(file);
    f    = copyFile(file, back);
    free(back);

    return f;
}

int
copyFile(char *from, char *to)
{
    int   num_read;
    char  buf[HE_BUF_SZ]; /* copying buffer */
    FILE *fp  = NULL;
    FILE *bfp = NULL;

    /* open the hdf file for backing up */
    if ((fp = fopen(from, "r")) == NULL) {
        fprintf(stderr, "Unable to open file: <%s>\n", from);
        goto error;
    }
    if ((bfp = fopen(to, "w")) == NULL) {
        fclose(fp);
        fprintf(stderr, "Unable to open backup file.\n");
        goto error;
    }
    /* copy the contents from hdf file to backup file */
    while ((num_read = (int)fread(buf, 1, HE_BUF_SZ, fp)) > 0)
        fwrite(buf, 1, (size_t)num_read, bfp);

    fclose(fp);
    fclose(bfp);

    return HE_OK;

error:

    if (fp != NULL)
        fclose(fp);
    if (bfp != NULL)
        fclose(bfp);

    return FAIL;
}

int
updateDesc(void)
{
    int32 fid;
    int32 groupID;
    int32 aid, status;
    int   i;

    if ((fid = Hopen(he_file, DFACC_READ, 0)) == 0) {
        printf("failed opening\n");
        HEprint(stdout, 0);
        return FAIL;
    }

    aid = Hstartread(fid, DFTAG_WILDCARD, DFREF_WILDCARD);
    if (aid == FAIL) {
        HEprint(stderr, 0);
        return FAIL;
    }

    status = SUCCEED;
    for (i = 0; (i < HE_DESC_SZ) && (status != FAIL); i++) {
        Hinquire(aid, NULL, &he_desc[i].tag, &he_desc[i].ref, &he_desc[i].length, &he_desc[i].offset, NULL,
                 (int16 *)NULL, (int16 *)NULL);
        status = Hnextread(aid, DFTAG_WILDCARD, DFREF_WILDCARD, DF_CURRENT);
    }
    he_numDesc = i;

    /* get information about the groups */
    he_numGrp = 0;
    for (i = 0; i < he_numDesc; i++) {
        if (isGrp(he_desc[i].tag)) {
            he_grp[he_numGrp].desc = i;
            /*                he_grp[he_numGrp].size = (int) (he_desc[i].length / sizeof(tag_ref));
                            he_grp[he_numGrp].ddList = (tag_ref_ptr) malloc(he_desc[i].length);
            */
            he_grp[he_numGrp].size   = (int)(he_desc[i].length / 4);
            he_grp[he_numGrp].ddList = (tag_ref_ptr)malloc(he_grp[he_numGrp].size * sizeof(tag_ref));

            if (!he_grp[he_numGrp].ddList) {
                fprintf(stderr, "Out of memory. Closing file.\n");
                closeFile(1); /* keep the backup */
                return FAIL;
            }
            groupID = DFdiread(fid, he_desc[i].tag, he_desc[i].ref);
            if (groupID < 0) {
                HEprint(stderr, 0);
                return FAIL;
            }
            for (int j = 0; j < he_grp[he_numGrp].size; j++)
                DFdiget(groupID, &he_grp[he_numGrp].ddList[j].tag, &he_grp[he_numGrp].ddList[j].ref);

            he_numGrp++;
        }
    }
    Hendaccess(aid);
    Hclose(fid);
    return SUCCEED;
}

int
initFile(char *file)
{
    free(he_file);
    he_file = copyStr(file);

    if (updateDesc() < 0)
        return FAIL;

    /* if there are groups in this file, go to the first group tag */
    /* otherwise, just go to the first element */
    if (he_numGrp > 0)
        he_currDesc = he_grp[0].desc;
    else
        he_currDesc = 0;

    return resetPred();
}

int
closeFile(int keep)
{
    char *back = NULL;

    if (!fileOpen()) {
        fprintf(stderr, "No open file to close.\n");
        return FAIL;
    }
    /* free some dynamic storages */
    if (he_backup && !keep) {
        back = backupName(he_file);
        (void)removeFile(back);
        free(back);
    }
    free(he_file);
    he_file = NULL;
    for (int i = 0; i < he_numGrp; i++)
        free(he_grp[i].ddList);

    return HE_OK;
}

int
getR8(int xdim, int ydim, char *image, char *pal, int compress)
{
    FILE *fp = NULL;
    int32 length;
    char *buf = NULL;

    if (!fileOpen()) {
        noFile();
        goto error;
    }
    if (pal)
        if (setPal(pal) < 0)
            /* Error already signalled by setPal */
            goto error;

    length = xdim * ydim;
    buf    = (char *)malloc(length);

    if ((fp = fopen(image, "r")) == NULL) {
        fprintf(stderr, "Error opening image file: %s.\n", image);
        goto error;
    }
    if (fread(buf, (size_t)xdim, (size_t)ydim, fp) < (size_t)ydim) {
        fprintf(stderr, "Error reading image file: %s.\n", image);
        goto error;
    }

    if (DFR8addimage(he_file, buf, (int32)xdim, (int32)ydim, (uint16)compress) < 0) {
        HEprint(stderr, 0);
        goto error;
    }

    if (updateDesc() < 0)
        goto error;

    fclose(fp);
    free(buf);

    return HE_OK;

error:

    if (fp != NULL)
        fclose(fp);
    free(buf);

    return FAIL;
}

int
setPal(char *pal)
{
    FILE *fp = NULL;
    char  reds[HE_COLOR_SZ], greens[HE_COLOR_SZ], blues[HE_COLOR_SZ];
    char  palette[HE_PALETTE_SZ];
    char *p;

    if ((fp = fopen(pal, "r")) == NULL) {
        fprintf(stderr, "Error opening palette file: %s.\n", pal);
        goto error;
    }
    if (fread(reds, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ || fread(greens, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ ||
        fread(blues, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ) {
        fprintf(stderr, "Error reading palette file: %s.\n", pal);
        goto error;
    }

    /* convert sun palette to hdf palette */
    p = palette;
    for (int i = 0; i < HE_COLOR_SZ; i++) {
        *p++ = reds[i];
        *p++ = greens[i];
        *p++ = blues[i];
    }

    if (DFR8setpalette((uint8 *)palette) < 0) {
        fputs("Error setting palette.\n", stderr);
        goto error;
    }

    fclose(fp);

    return HE_OK;

error:

    if (fp != NULL)
        fclose(fp);
    return FAIL;
}

int
findDesc(tag_ref_ptr dd)
{
    for (int i = 0; i < he_numDesc; i++)
        if ((he_desc[i].tag == dd->tag) && (he_desc[i].ref == dd->ref))
            return i;

    return FAIL;
}

int
desc2Grp(int desc)
{
    for (int i = 0; i < he_numGrp; i++)
        if (he_grp[i].desc == desc)
            return i;

    NOT_REACHED();
    return FAIL;
}

int
hasReference(int desc)
{
    for (int i = 0; i < he_numGrp; i++)
        for (int j = 0; j < he_grp[i].size; j++)
            if (he_grp[i].ddList[j].tag == he_desc[desc].tag && he_grp[i].ddList[j].ref == he_desc[desc].ref)
                return YES;
    return NO;
}

int
deleteDesc(int desc)
{
    int32 fid = -1;

    if ((fid = Hopen(he_file, DFACC_WRITE, 0)) == -1) {
        HEprint(stderr, 0);
        goto error;
    }

    if (Hdeldd(fid, he_desc[desc].tag, he_desc[desc].ref) == FAIL) {
        HEprint(stderr, 0);
        goto error;
    }
    return Hclose(fid);

error:
    if (fid != -1)
        if (FAIL == Hclose(fid))
            HEprint(stderr, 0);
    return FAIL;
}

int
getCurrRig(int32 *pXdim, int32 *pYdim, char **pPalette, char **pRaster)
{
    int ispal;

    goTo(he_currDesc);

    if (DFR8getdims(he_file, pXdim, pYdim, &ispal) < 0) {
        fprintf(stderr, "Error getting image group.\n");
        HEprint(stderr, 0);
        return FAIL;
    }
    if (ispal)
        *pPalette = (char *)malloc(HE_PALETTE_SZ);
    else
        *pPalette = (char *)NULL;
    *pRaster = (char *)malloc((size_t)(*pXdim) * (size_t)(*pYdim));

    if (DFR8getimage(he_file, (unsigned char *)*pRaster, *pXdim, *pYdim, (unsigned char *)*pPalette) ==
        FAIL) {
        fprintf(stderr, "Error getting image group.\n");
        HEprint(stderr, 0);
        return FAIL;
    }

    return HE_OK;
}

int
putWithTempl(char *template, int n1, int n2, int n3, char *data, int length, int verbose)
{
    char *file = NULL;
    int   ret  = FAIL;

    convertTemplate(template, n1, n2, n3, &file);
    if (verbose)
        printf("Writing to file: %s\n", file);
    ret = writeToFile(file, data, length);
    free(file);

    return ret;
}

int
revert(void)
{
    char *back = NULL;

    back = backupName(he_file);
    if (copyFile(back, he_file) < 0)
        return FAIL;
    return initFile(he_file);
}

int
writeElt(char *file, uint16 ref, int elt)
{
    int         ret;
    char       *data;
    int32       eltLength;
    char       *p;
    uint16      rank;
    int         i;
    uint16      ntTag;
    uint16      ntRef;
    tag_ref_ptr ntDesc;
    int         nt;

    eltLength = getElement(elt, &data);
    if (eltLength <= 0) {
        fprintf(stderr, "Cannot get element: tag %d ref %d.\n", he_desc[elt].tag, he_desc[elt].ref);
        return FAIL;
    }
    /* special case */

    if (he_desc[elt].tag == DFTAG_SDD) {

        /* lots of hack here */
        /* assume that the number types are of the same ref as this elt */
        p = data;

        /* get the rank */
        /* NOTE: UINT16READ and UINT16WRITE advances p */
        UINT16DECODE(p, rank);
        /* move to the NT of the data */
        p += (rank * 4);
        UINT16DECODE(p, ntTag);
        UINT16DECODE(p, ntRef);

        /* set up to write the number type element */
        ntDesc      = (tag_ref_ptr)malloc(sizeof(tag_ref));
        ntDesc->tag = ntTag;
        ntDesc->ref = ntRef;
        nt          = findDesc(ntDesc);
        free(ntDesc);
        writeElt(file, ref, nt);

        p -= 2;
        UINT16ENCODE(p, ref);
        /* do the NT of scales */
        for (i = 0; (uint16)i < rank; i++) {
            p += 2;
            UINT16ENCODE(p, ref);
        }
    }

    ret = putElement(file, he_desc[elt].tag, ref, data, eltLength);
    free(data);
    return ret;
}

int
putElement(char *file, uint16 tag, uint16 ref, char *data, int32 len)
{
    int32 ret;
    int32 fid = -1;

    if ((fid = Hopen(file, DFACC_READ | DFACC_WRITE, 0)) == FAIL)
        /* a little tricky here */
        if (HEvalue(0) != DFE_FNF || (fid = Hopen(file, DFACC_ALL, 0)) == FAIL) {
            HEprint(stderr, 0);
            goto error;
        }
    if ((ret = Hputelement(fid, tag, ref, (unsigned char *)data, len)) < 0) {
        HEprint(stderr, 0);
        goto error;
    }
    return Hclose(fid);

error:
    if (fid != -1)
        if (FAIL == Hclose(fid))
            HEprint(stderr, 0);

    return FAIL;
}

int
writeGrp(char *file)
{
    uint16 ref = DFREF_NONE;
    int    grp;
    int    elt;
    int    ret;
    int32  fid;
    int32  gid;

    getNewRef(file, &ref);

    grp = currGrpNo;
    gid = DFdisetup(he_grp[grp].size);
    for (int i = 0; i < he_grp[grp].size; i++) {
        elt = findDesc(he_grp[grp].ddList + i);
        if (elt >= 0)
            writeElt(file, ref, elt);

        /* Update the group dd list */
        DFdiput(gid, he_grp[grp].ddList[i].tag, ref);
    }

    /* Do the group now */

    if ((fid = Hopen(file, DFACC_READ | DFACC_WRITE, 0)) == FAIL) {
        HEprint(stderr, 0);
        return FAIL;
    }
    if ((ret = DFdiwrite(fid, gid, currTag, ref)) < 0) {
        HEprint(stderr, 0);
        return ret;
    }
    return Hclose(fid);
}

int
getNewRef(char *file, uint16 *pRef)
{
    int32 fid;

    if ((fid = Hopen(file, DFACC_READ | DFACC_WRITE, 0)) == FAIL) {
        /* a little tricky here */
        if (HEvalue(0) != DFE_FNF || (fid = Hopen(file, DFACC_ALL, 0)) == FAIL) {
            HEprint(stderr, 0);
            return FAIL;
        }
    }

    *pRef = Hnewref(fid);
    return Hclose(fid);
}

int
writeAnnot(char *file, uint16 tag, uint16 ref)
{
    char  *data;
    int32  eltLength;
    int    tmp;
    char  *p;
    uint16 newRef;

    while (tag == 0) {
        printf("Attach to what tag? (> 0)");
        scanf("%d", &tmp);
        tag = (uint16)tmp;
    }

    while (ref == 0) {
        printf("Attach to what ref? (> 0)");
        scanf("%d", &tmp);
        ref = (uint16)tmp;
    }

    eltLength = getElement(he_currDesc, &data);
    if (eltLength <= 0) {
        fprintf(stderr, "Cannot get element: tag %d ref %d.\n", he_desc[he_currDesc].tag,
                he_desc[he_currDesc].ref);
        return FAIL;
    }

    p = data;

    /*
     *  This is really ugly...
     */
    UINT16ENCODE(p, tag);
    UINT16ENCODE(p, ref);

    if (getNewRef(file, &newRef) < 0) {
        fprintf(stderr, "Error getting new ref number.\n");
        return FAIL;
    }

    return putElement(file, he_desc[he_currDesc].tag, newRef, data, eltLength);
}

int32
getAnn(int ann, uint16 tag, uint16 ref, char **pBuf)
{
    int32 len;

    if (ann == HE_LABEL) {
        len = DFANgetlablen(he_file, tag, ref);
        if (len > 0) {
            *pBuf = (char *)malloc((size_t)(len + 1));
            DFANgetlabel(he_file, tag, ref, *pBuf, len + 1);
        }
        else
            *pBuf = NULL;
    }
    else {
        len = DFANgetdesclen(he_file, tag, ref);
        if (len > 0) {
            *pBuf = (char *)malloc((size_t)len);
            DFANgetdesc(he_file, tag, ref, *pBuf, len);
        }
        else
            *pBuf = NULL;
    }
    return len;
}

int
putAnn(int ann, uint16 tag, uint16 ref, char *buf, int32 len)
{
    int ret;

    if (ann == HE_LABEL)
        ret = DFANputlabel(he_file, tag, ref, buf);
    else
        ret = DFANputdesc(he_file, tag, ref, buf, len);
    if (ret < 0)
        HEprint(stderr, 0);

    return ret;
}

int32
readFromFile(char *file, char **pBuf)
{
    FILE *fp = NULL;
    int32 soFar;
    int32 bufLen;
    int32 num_read;

    fp = fopen(file, "r");
    if (fp == NULL)
        goto error;

    soFar  = 0;
    bufLen = 0;
    for (num_read = HE_BUF_SZ; num_read == HE_BUF_SZ; soFar += num_read) {
        bufLen += HE_BUF_SZ;
        if (bufLen == HE_BUF_SZ)
            *pBuf = (char *)malloc(bufLen);
        else
            *pBuf = (char *)realloc(*pBuf, bufLen);
        if (*pBuf == NULL)
            goto error;

        num_read = (int32)fread((*pBuf) + soFar, 1, HE_BUF_SZ, fp);
    }
    *pBuf          = (char *)realloc(*pBuf, soFar + 1);
    (*pBuf)[soFar] = '\0';
    fclose(fp);
    return soFar;

error:
    if (fp != NULL)
        fclose(fp);
    return FAIL;
}

/* ---- table for operators -------- */
struct {
    const char *str;
    int         key;
} he_optTab[] = {
    {"readonly", HE_RDONLY},
    {"all", HE_ALL},
    {"backup", HE_BACKUP},
    {"batch", HE_BATCH},
    {"help", HE_HELP},
    {"longout", HE_LONGOUT},
    {"nobackup", HE_NOBACKUP},
    {"remote", HE_REMOTE},
    {"verbose", HE_VERBOSE},
    {"position", HE_POSITION},
    {"expansion", HE_EXPANSION},
    {"large", HE_LARGE},
    {"offset", HE_OFFSET},
    {"ascii", HE_ASCII},
    {"octal", HE_OCTAL},
    {"hexadecimal", HE_HEX},
    {"decimal", HE_DECIMAL},
    {"float", HE_FLOAT},
    {"dimensions", HE_DIMS},
    {"image", HE_IMAGE},
    {"palette", HE_PALETTE},
    {"raster", HE_RASTER},
    {"rle", HE_RLE},
    {"compress", HE_RLE},
    {"imcomp", HE_IMCOMP},
    {"group", HE_DOGROUP},
    {"file", HE_FILE},
    {"keep", HE_KEEP},
    {"length", HE_LENGTH},
    {"attachto", HE_ATTACHTO},
    {"label", HE_LABEL},
    {"descriptor", HE_DESCRIPTOR},
    {"editor", HE_EDITOR},
    {"byte", HE_BYTE},
    {"short", HE_SHORT},
    {"double", HE_DOUBLE},
    {"ushort", HE_USHORT},
    {"udecimal", HE_UDECIMAL},
    {"raw", HE_RAW},
};

int
findOpt(char *word)
{
    unsigned len;
    int      found = -1;

    len = strlen(word);

    for (int i = 0; i < sizeof(he_optTab) / sizeof(he_optTab[0]); i++)
        if (!strncmp(he_optTab[i].str, word, len)) {
            /* exact match */
            if (strlen(he_optTab[i].str) == len)
                return he_optTab[i].key;

            if (found < 0)
                found = (int)i;
            else
                return HE_AMBIG;
        }

    if (found < 0)
        return HE_NOTFOUND;

    return he_optTab[found].key;
}

char *
catStr(const char *s, const char *s1)
{
    char *t = NULL;

    t = (char *)malloc(strlen(s) + strlen(s1) + 1);
    strcpy(t, s);
    strcat(t, s1);
    return t;
}

char *
copyStr(char *s)
{
    char *t = NULL;

    t = (char *)malloc(strlen(s) + 1);
    strcpy(t, s);
    return t;
}

int
isGrp(uint16 tag)
{
    switch (tag) {
        case DFTAG_RIG:
        case DFTAG_SDG:
        case DFTAG_NDG:
            /* and other group tags */
            return 1;
        default:
            return 0;
    }
}

int
HEquit(HE_CMD *cmd)
{
    if (cmd->argc > 1) {
        puts("quit");
        puts("\tQuits this application.");
        return HE_OK;
    }
    return quit(0);
}

int
quit(int status)
{
    free(he_desc);
    free(he_grp);

    if (fileOpen()) {
        if (closeFile(0) < 0)
            return HE_FAIL;
    }
    exit(status);
}

int
HEhelp(HE_CMD *dummy)
{
    (void)dummy;

    help();
    return HE_OK;
}

void
help(void)
{
    /* print some help information */
    printf("hdfed allows sophisticated HDF users to manipulate the elements in");
    printf(" an HDF file.\nThese manipulations include selecting groups and ");
    printf("showing information about\nthem, dumping them to the output, ");
    printf("writing them to new files, deleting them,\ninserting them, ");
    printf("replacing, say, the palette of an r8 group, and editing the\n");
    printf("text labels and descriptions of any element.\n\n");

    printf("hdfed will NOT allow the user to *arbitrarily* modify binary data ");
    printf("in the file or\nany element, though it allows modification of tag ");
    printf("and reference numbers within\nstrict constraints.  The user should");
    printf(" not attempt to alter individual bytes.  It\nis acceptable,");
    printf(" however, to replace an element with another of the same type.\n\n");

    printf("hdfed can be used both interactively or in 'batch' mode.  See the ");
    printf("\"HDF Calling\nInterfaces and Utilities\" manual section on hdfed ");
    printf("for an example.\n\n");

    printf("List of commands:");
    printf("\tannotate   dump    if      open    putr8    unalias\n");
    printf("\t\t\tclose      getr8   info    prev    revert   wait\n");
    printf("\t\t\tdelete     help    next    put     select   write\n");

    printf("\nPredicates for 'if' 'select' 'next' and 'prev' are of the form\n");
    printf("\ttag = 3 ref = 2 image_size < 1000 label = \"abc\"\n");
    printf("Type <command> -help for usage of <command>.  DO NOT type the");
    printf(" command without\narguments and expect help.  Some commands delete");
    printf(" objects and do not need any\narguments.  If you are learning to ");
    printf("use hdfed, try it on an expendable file.\n");
}

int
HEwait(HE_CMD *cmd)
{
    int c;

    if (cmd->argv[1][0] == '-' && findOpt(cmd->argv[1] + 1) == HE_HELP) {
        printf("wait [<message>]\n");
        printf("\tPrints message and then wait for user to hit return\n");
        return HE_OK;
    }

    printf("%s\nPress return to continue.", cmd->argv[1]);
    do
        c = getchar();
    while (c != '\n');

    return HE_OK;
}

void
deleteCmd(HE_CMD *cmd)
{
    if (cmd == NULL)
        return;
    if (cmd->next != NULL)
        deleteCmd(cmd->next);
    if (cmd->sub != NULL)
        deleteCmd(cmd->sub);
    for (int i = 0; i < cmd->argc; i++)
        free(cmd->argv[i]);
    free(cmd);
}

/* -------------- routines to manipulate templates --------------------- */

#define TEMPLATE_CHAR1 '#'
#define TEMPLATE_CHAR2 '@'
#define TEMPLATE_CHAR3 '%'

void
convertTemplate(char *template, int n1, int n2, int n3, char **pname)
{
    char  s1[20], s2[20], s3[20];
    char *t;

    sprintf(s1, "%1d", n1);
    sprintf(s2, "%1d", n2);
    sprintf(s3, "%1d", n3);

    *pname = t = (char *)malloc(strlen(template) + 61);

    while (*template)
        switch (*template) {
            case TEMPLATE_CHAR1:
                fillTemplate(&template, &t, s1, TEMPLATE_CHAR1);
                break;
            case TEMPLATE_CHAR2:
                fillTemplate(&template, &t, s2, TEMPLATE_CHAR2);
                break;
            case TEMPLATE_CHAR3:
                fillTemplate(&template, &t, s3, TEMPLATE_CHAR3);
                break;
            default:
                *t++ = *template ++;
        }
    *t = '\0';
}

void
fillTemplate(char **template, char **pout, char *s, char templateChar)
{
    int templateLen, sLen;

    /* count length of template to replace */
    for (templateLen = 0; **template == templateChar; (*template)++, templateLen++)
        ;
    sLen = (int)strlen(s);

    /* fill with zero's if the space reserved in template is
       longer than the length of s */
    for (; templateLen > sLen; templateLen--)
        *(*pout)++ = '0';

    while (*s)
        *(*pout)++ = *s++;
}

/* end of he-main.c */