File: gettab.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (831 lines) | stat: -rw-r--r-- 20,602 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
/* File gettab.c
 * January 10, 2007
 * By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 1999-2007
   Smithsonian Astrophysical Observatory, Cambridge, MA USA

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include "libwcs/wcs.h"
#include "libwcs/fitsfile.h"
#include "libwcs/wcscat.h"

#define MAXCOL 500
#define MAXCOND 10
#define MAXFILES 1000

static void usage();
static void PrintValues();
static int maxncond = 100;

static char *RevMsg = "GETTAB WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";

static int verbose = 0;		/* verbose/debugging flag */
static int nfile = 0;
static int ndec = -9;
static int maxlfn = 0;
static int listall = 0;
static int listpath = 0;
static int tabout = 0;
static int printhead = 0;
static int assign = 0;
static int version = 0;		/* If 1, print only program name and version */
static int nkeep = 0;
static int *keeplines;		/* List of lines to keep if nkeep > 0 */
static int ncond=0;		/* Number of keyword conditions to check */
static int condand=1;		/* If 1, AND comparisons, else OR */
static char **cond;		/* Conditions to check */
static char **ccond;		/* Comparison characters */
static char *tcond;		/* Condition character */
struct TabTable *tabtable;

int
main (ac, av)
int ac;
char **av;
{
    char *str;
    char *kwd[MAXCOL];
    char *alias[MAXCOL];
    int ifile;
    int nkwd = 0;
    char *fn[MAXFILES];
    int readlist = 0;
    int lfn;
    char *lastchar;
    char filename[128];
    char *name = NULL;
    char ctemp;
    FILE *flist;
    char *listfile = NULL;
    int ikwd, lkwd, i;
    int lfield = 0;
    char *kw1;
    char *cstr, *nstr;
    int icond;
    char *calias;
    char *ranges = NULL;
    char *temp;
    int nldef = 1;
    int lstr;
    double dnum;
    struct Range *range; /* Range of sequence numbers to list */

    /* Check for help or version command first */
    str = *(av+1);
    if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
	usage();
    if (!strcmp (str, "version") || !strcmp (str, "-version")) {
	version = 1;
	usage();
	}

    cond = (char **)calloc (maxncond, sizeof(char *));
    ccond = (char **)calloc (maxncond, sizeof(char *));
    tcond = (char *)calloc (maxncond, sizeof(char));

    nkwd = 0;
    ncond = 0;
    nfile = 0;

    /* crack arguments */
    for (av++; --ac > 0; av++) {
	if (*(str = *av)=='-') {
	    char c;
	    while ((c = *++str))
	    switch (c) {

		case 'a':	/* list file even if no keywords are found */
		    listall++;
		    break;

		case 'e':	/* output assignments */
		    assign++;
		    break;

		case 'v':	/* more verbosity */
		    verbose++;
		    break;

		case 'h':	/* output column headings */
		    printhead++;
		    break;

		case 'n':	/* number of decimal places in output */
		    if (ac < 2)
			usage();
		    ndec = (int) (atof (*++av));
		    ac--;
		    break;

		case 'o': /* OR conditions insted of ANDing them */
		    condand = 0;
		    break;

		case 'p':	/* output column headings */
		    listpath++;
		    break;

		case 't':	/* output tab table */
		    tabout++;
		    break;

		default:
		    usage();
		    break;
		}
	    continue;
	    }

	/* Set range and make a list of line numbers from it */
	else if (isrange (*av)) {
	    if (ranges) {
		temp = ranges;
		ranges = (char *) calloc (strlen(ranges) + strlen(*av) + 2, 1);
		strcpy (ranges, temp);
		strcat (ranges, ",");
		strcat (ranges, *av);
		free (temp);
		}
	    else {
		ranges = (char *) calloc (strlen(*av) + 1, 1);
		strcpy (ranges, *av);
		}
	    continue;
	    }

	/* If numeric argument, set line to be read */
	else if (isnum (str)) {
	    if (ranges) {
		temp = ranges;
		ranges = (char *)calloc (strlen(ranges)+strlen(*av)+2, 1);
		strcpy (ranges, temp);
		strcat (ranges, ",");
		strcat (ranges, *av);
		free (temp);
		}
	    else {
		ranges = (char *) calloc (strlen(*av) + 1, 1);
		strcpy (ranges, *av);
		}
	    continue;
	    }

	else if (*av[0] == '@') {
	    readlist++;
	    listfile = *av + 1;
	    nfile = 2;
	    continue;
	    }
	else if (!strcmp (*av, "stdin")) {
	    fn[nfile] = *av;
	    name = fn[nfile];
	    lfn = strlen (name);
	    if (lfn > maxlfn)
		maxlfn = lfn;
	    nfile++;
	    continue;
	    }

	/* Record condition
	else if (strchr (*av, '=') || strchr (*av, '<') || strchr (*av, '>')) {
	    cond[ncond] = *av;
	    if (ncond < MAXCOND)
		ncond++;
	    continue;
	    } */

	/* Record column name with output alias */
	else if (strchr (*av, '@')) {
	    kwd[nkwd] = *av;
	    calias = strchr (*av, '@');
	    alias[nkwd] = calias + 1;
	    *calias = (char) 0;
	    if (nkwd < MAXCOL)
		nkwd++;
	    continue;
	    }

	/* Record tab table file name */
	else if (istab (*av)) {
	    fn[nfile] = *av;

	    if (listpath || (name = strrchr (fn[nfile],'/')) == NULL)
		name = fn[nfile];
	    else
		name = name + 1;
	    lfn = strlen (name);
	    if (lfn > maxlfn)
		maxlfn = lfn;
	    if (nfile < MAXFILES)
		nfile++;
	    continue;
	    }

	/* Condition */
	else if (strchr (*av, '=') != NULL || strchr (*av, '#') != NULL ||
		 strchr (*av, '>') != NULL || strchr (*av, '<') != NULL ) {
	    if (ncond >= maxncond) {
		maxncond = maxncond * 2;
		cond = (char **)realloc((void *)cond,maxncond*sizeof(void *));
		ccond = (char **)realloc((void *)ccond,maxncond*sizeof(void *));
		tcond = (char *)realloc((void *)tcond,maxncond*sizeof(char));
		}
	    cond[ncond] = *av;
	    cstr = strchr (*av, '=');
	    if (cstr != NULL)
		tcond[ncond] = '=';
	    else {
		cstr = strchr (*av, '#');
		if (cstr != NULL)
		    tcond[ncond] = '#';
		else {
		    cstr = strchr (*av, '>');
		    if (cstr != NULL)
			tcond[ncond] = '>';
		    else {
			cstr = strchr (*av, '<');
			if (cstr != NULL)
			    tcond[ncond] = '<';
			else
			    tcond[ncond] = (char) 0;
			}
		    }
		}
	    if (tcond[ncond]) {
		cstr[0] = (char) 0;
		cstr++;
		strfix (cstr, 0, 1);
		if (strchr (cstr, ':')) {
		    dnum = str2dec (cstr);
		    nstr = (char *) calloc (32, sizeof(char));
		    num2str (nstr, dnum, 0, 7);
		    ccond[ncond] = nstr;
		    }
		else {
		    lstr = strlen (cstr);
		    ccond[ncond] = (char *) calloc (lstr+2, sizeof(char));
		    strcpy (ccond[ncond], cstr);
		    }
		ncond++;
		}
	    }

	/* Record column name */
	else {
	    kwd[nkwd] = *av;
	    alias[nkwd] = NULL;
	    if (nkwd < MAXCOL)
		nkwd++;
	    continue;
	    }
	}

    /* Read from standard input if no file is specified */
    if (nfile == 0 && (nkwd > 0 || ncond > 0)) {
	name = malloc (8);
	strcpy (name, "stdin");
	fn[nfile] = name;
	lfn = strlen (name);
	if (lfn > maxlfn)
	    maxlfn = lfn;
	nfile++;
	}

    /* Decode ranges */
    if (ranges != NULL) {
	range = RangeInit (ranges, nldef);
	nkeep = rgetn (range);
	keeplines = (int *) calloc (1, nkeep);
	for (i = 0; i < nkeep; i++)
	    keeplines[i] = rgeti4 (range);
	}

    if (nkwd > 0) {

	/* Print column headings if tab table or headings requested */
	if (printhead || tabout) {

	    /* Open the input tab table */
	    if ((tabtable = tabopen (fn[0], 0)) == NULL) {
		fprintf (stderr,"%s\n", gettaberr());
		return (1);
		}

	    /* Print the name of the table, if it has one */
	    if (tabtable->tabname != NULL)
		printf ("%s\n", tabtable->tabname);

	    /* For tab table output, keep input header information */
	    if (tabout) {
		if (tabtable->tabheader != tabtable->tabhead) {
		    ctemp = *(tabtable->tabhead-1);
		    *(tabtable->tabhead-1) = (char) 0;
		    printf ("%s\n", tabtable->tabheader);
		    *(tabtable->tabhead-1) = ctemp;
		    }
		}

	    /* Print conditions in header */
	    for (icond = 0; icond < ncond; icond++) {
		if (verbose) {
		    if (condand || icond == 0)
			printf ("%s\n",cond[icond]);
		    else
			printf (" or %s\n",cond[icond]);
		    }
		else if (tabout) {
		    if (condand || icond == 0)
			printf ("condition	%s %c %s\n",
				cond[icond], tcond[icond], ccond[icond]);
		    else
			printf ("condition	or %s %c %s\n",
				cond[icond], tcond[icond], ccond[icond]);
		    }
		}

	    /* If multiple files, add filename at start of output line */
	    if (nfile > 1) {
		printf ("filename");
		if (maxlfn > 8) {
		    for (i = 8; i < maxlfn; i++)
			printf (" ");
		    }
		if (tabout)
	    	    printf ("	");
		else
		    printf (" ");
		}

	    /* Print column names */
	    for (ikwd = 0; ikwd < nkwd; ikwd++) {
		if (alias[ikwd]) {
		    kw1 = alias[ikwd];
		    lkwd = strlen (alias[ikwd]);
		    }
		else {
		    kw1 = kwd[ikwd];
		    lkwd = strlen (kwd[ikwd]);
		    }
		printf ("%s",kw1);
		if ((i = tabcol (tabtable, kwd[ikwd])) > 0) {
		    lfield = tabtable->lcfld[i-1];
		    if (lfield > 32)
			lfield = 32;
		    }
		if (tabout && lfield > lkwd) {
		    for (i = lkwd; i < lfield; i++)
			printf (" ");
		    }
		if (verbose || ikwd == nkwd - 1)
	    	    printf ("\n");
		else if (tabout)
	    	    printf ("	");
		else
		    printf (" ");
		}

	    /* Print field-defining hyphens if tab table output requested */
	    if (tabout) {
		for (ikwd = 0; ikwd < nkwd; ikwd++) {
		    if ((i = tabcol (tabtable, kwd[ikwd])) > 0) {
			lfield = tabtable->lcfld[i-1];
			for (i = 0; i < lfield; i++)
			    printf ("-");
			if (ikwd == nkwd - 1)
			    printf ("\n");
			else
			    printf ("	");
			}
		    }
		}
	    }

    /* Get table values one at a time */

	/* Read through tables in listfile */
	if (readlist) {
	    if ((flist = fopen (listfile, "r")) == NULL) {
		fprintf (stderr,"GETTAB: List file %s cannot be read\n",
		     listfile);
		usage ();
		}
	    while (fgets (filename, 128, flist) != NULL) {
		lastchar = filename + strlen (filename) - 1;
		if (*lastchar < 32) *lastchar = 0;
		PrintValues (filename,nkwd,kwd,alias);
		if (verbose)
		    printf ("\n");
		}
	    fclose (flist);
	    }

	/* Read tables from command line list */
	else {
	    for (ifile = 0; ifile < nfile; ifile++)
	    	PrintValues (fn[ifile],nkwd,kwd,alias);
	    }
	}

    else {
	if (printhead || tabout) {

	    /* Open the input tab table */
	    if (strcasecmp (name, "stdin")) {
		if ((tabtable = tabopen (fn[0], 0)) == NULL) {
		    fprintf (stderr,"%s\n", gettaberr());
		    return (1);
		    }
		}

	    /* Print the name of the table, if it has one */
	    if (tabtable->tabname != NULL)
		printf ("%s\n", tabtable->tabname);

	    /* For tab table output, keep input header information */
	    if (tabout) {
		if (tabtable->tabheader != tabtable->tabhead) {
		    ctemp = *(tabtable->tabhead-1);
		    *(tabtable->tabhead-1) = (char) 0;
		    printf ("%s\n", tabtable->tabheader);
		    *(tabtable->tabhead-1) = ctemp;
		    }
		}

	    /* Print conditions in header */
	    for (icond = 0; icond < ncond; icond++) {
		if (verbose) {
		    if (condand || icond == 0)
			printf ("%s\n",cond[icond]);
		    else
			printf (" or %s\n",cond[icond]);
		    }
		else if (tabout) {
		    if (condand || icond == 0)
			printf ("condition	%s %c %s\n",
				cond[icond], tcond[icond], ccond[icond]);
		    else
			printf ("condition	or %s %c %s\n",
				cond[icond], tcond[icond], ccond[icond]);
		    }
		}

	    /* If multiple files, add filename at start of output line */
	    if (nfile > 1) {
		printf ("filename");
		if (maxlfn > 8) {
		    for (i = 8; i < maxlfn; i++)
			printf (" ");
		    }
		if (tabout)
	    	    printf ("	");
		else
		    printf (" ");
		}

	    /* Print column headers */
	    ctemp = *(tabtable->tabdata-1);
	    *(tabtable->tabdata-1) = (char) 0;
	    printf ("%s\n", tabtable->tabhead);
	    *(tabtable->tabdata-1) = ctemp;
	    }
	for (ifile = 0; ifile < nfile; ifile++)
	    PrintValues (fn[ifile],nkwd,kwd,alias);
	}

    if (ccond != NULL) {
	for (i = 0; i < ncond; i++)
	    free (ccond[i]);
	free (ccond);
	}
    if (cond != NULL)
	free (cond);
    if (tcond != NULL)
	free (tcond);
    return (0);
}

static void
usage ()
{
    fprintf (stderr,"%s\n",RevMsg);
    if (version)
	exit (-1);
    fprintf (stderr,"Print FITS or IRAF header keyword values\n");
    fprintf(stderr,"usage: gettab [-ahoptv][-n num] file1.tab ... filen.tab kw1 kw2 ... kwn\n");
    fprintf(stderr,"       gettab [-ahoptv][-n num] @filelist kw1 kw2 ... kwn\n");
    fprintf(stderr,"       gettab [-ahoptv][-n num] <file1.tab kw1 kw2 ... kwn\n");
    fprintf(stderr,"  -a: List file even if keywords are not found\n");
    fprintf(stderr,"  -e: Print keyword=value list\n");
    fprintf(stderr,"  -h: Print column headings\n");
    fprintf(stderr,"  -n: Number of decimal places in numeric output\n");
    fprintf(stderr,"  -o: OR conditions instead of ANDing them\n");
    fprintf(stderr,"  -p: Print full pathnames of files\n");
    fprintf(stderr,"  -t: Output in tab-separated table format\n");
    fprintf(stderr,"  -v: Verbose\n");
    exit (1);
}


static void
PrintValues (name, nkwd, kwd, alias)

char	*name;	  /* Name of FITS or IRAF image file */
int	nkwd;	  /* Number of keywords for which to print values */
char	*kwd[];	  /* Names of keywords for which to print values */
char	*alias[]; /* Output names of keywords if different from input */

{
    char *str;
    char *cstr, *cval, cvalue[64];
    int pass = 0;
    int drop;
    int jval, jcond, icond;
    double dval, dcond, dnum;
    char fnform[8];
    char string[80];
    char *filename;
    char outline[1000];
    char *line, *last;
    char *endline;
    char newline = 10;
    char numstr1[32];
    int ikwd, nfound;
    int iline;
    struct Tokens tokens;
    int *col = NULL;
    int *ccol = NULL;
    int ntok;
    int *nextkeep = NULL;
    int lastkeep = 0;

    if (nkeep > 0) {
	nextkeep = keeplines;
	lastkeep = keeplines[nkeep - 1];
	}

    /* Figure out conditions first, separating out keywords to check */

    /* Read tab table and set up data structure */
    if (tabtable == NULL) {
	if ((tabtable = tabopen (name, 0)) == NULL)
	    return;
	}

    if (verbose) {
	fprintf (stderr,"%s\n",RevMsg);
	fprintf (stderr,"Print table Values from tab table file %s\n", name);
	}

    /* Find file name */
    if (listpath || (filename = strrchr (name,'/')) == NULL)
	filename = name;
    else
	filename = filename + 1;

    if (nfile > 1) {
	if (tabout)
	    sprintf (fnform, "%%-%ds	", maxlfn);
	else
	    sprintf (fnform, "%%-%ds ", maxlfn);
	sprintf (outline, fnform, filename);
	}

    nfound = 0;
    line = tabtable->tabdata;
    last = line + strlen (tabtable->tabdata);

    /* Find column numbers for condition columns to speed up comparisons */
    if (ncond > 0) {
	ccol = (int *) calloc (ncond, sizeof (int));
	ccol[0] = 0;
	for (icond = 0; icond < ncond; icond++)
            ccol[icond] = tabcol (tabtable, cond[icond]);
	}

    /* Find column numbers for data columns to speed up extraction */
    col = (int *) calloc (nkwd, sizeof (int));
    col[0] = 0;
    for (ikwd = 0; ikwd < nkwd; ikwd++)
	col[ikwd] = tabcol (tabtable, kwd[ikwd]);

    /* Read through lines in file */
    iline = 0;
    while (line != NULL && line < last) {
	if (*line == (char)12)
	    break;
	if (*line != newline) {
	    outline[0] = (char) 0;

	    /* Check line number if extracting specific lines */
	    iline++;
	    drop = 0;
	    if (nkeep > 0) {
		if (iline != *nextkeep)
		    pass = 0;
		else if (*nextkeep < lastkeep) {
		    pass = 1;
		    nextkeep++;
		    }
		}
	    else
		pass = 0;

	    /* Check conditions */
	    ntok = setoken (&tokens, line, "tab");
	    if (ncond > 0) {
		for (icond = 0; icond < ncond; icond++) {
		    if (condand)
			pass = 0;

		/* Extract test value from comparison string */
		cstr = ccond[icond];

		/* Read comparison value from tab table */
		if (tabgetc (&tokens, ccol[icond], cvalue, 64))
		    continue;
		cval = cvalue;
		if (strchr (cval, ':')) {
		    dnum = str2dec (cval);
		    num2str (numstr1, dnum, 0, 7);
		    cval = numstr1;
		    }
		strfix (cval, 0, 1);

		/* Compare floating point numbers */
		if (isnum (cstr) == 2 && isnum (cval)) {
		    dcond = atof (cstr);
		    dval = atof (cval);
		    if (tcond[icond] == '=' && dval == dcond)
			pass = 1;
		    else if (tcond[icond] == '#' && dval != dcond)
			pass = 1;
		    else if (tcond[icond] == '>' && dval > dcond)
			pass = 1;
		    else if (tcond[icond] == '<' && dval < dcond)
			pass = 1;
		    }

		/* Compare integers */
		else if (isnum (cstr) == 1 && isnum (cval)) {
		    jcond = atoi (cstr);
		    jval = atoi (cval);
		    if (tcond[icond] == '=' && jval == jcond)
			pass = 1;
		    else if (tcond[icond] == '#' && jval != jcond)
			pass = 1;
		    else if (tcond[icond] == '>' && jval > jcond)
			pass = 1;
		    else if (tcond[icond] == '<' && jval < jcond)
			pass = 1;
		    }

		/* Compare strings (only equal or not equal */
		else {
		    if (tcond[icond] == '=' && !strcmp (cstr, cval))
			pass = 1;
		    else if (tcond[icond] == '#' && strcmp (cstr, cval))
			pass = 1;
		    }
		if (condand && !pass)
		    break;
		}
	    if (!pass) {
		line = strchr (line+1, newline);
		if (line == NULL)
		    break;
		if (strlen (line) < 1)
		    break;
		if (*++line == (char) 0)
		    break;
		continue;
		}
	    }

	/* Extract desired columns */
	if (nkwd == 0) {
	    endline = strchr (line+1, newline);
	    if (endline == NULL)
		break;
	    else {
		*endline = 0;
		printf ("%s\n", line);
		*endline = newline;
		}
	    }
	else {
	    ntok = setoken (&tokens, line, "tab");
	    for (ikwd = 0; ikwd < nkwd; ikwd++) {
		if (!tabgetc (&tokens, col[ikwd], string, 80)) {
		    strfix (string, 0, 0);
		    str = string;
		    if (ndec > -9 && isnum (str) && strchr (str, '.'))
			num2str (str, atof(str), 0, ndec);
		    if (verbose) {
			if (alias[ikwd])
			    printf ("%s/%s = %s",kwd[ikwd],alias[ikwd],str);
			else
			    printf ("%s = %s", kwd[ikwd], str);
			}
		    else if (assign) {
			if (alias[ikwd])
			    strcat (outline, alias[ikwd]);
			else
			    strcat (outline, kwd[ikwd]);
			strcat (outline, "=");
			strcat (outline, str);
			 }
		    else
			strcat (outline, str);
		    nfound++;
		    }
		else if (verbose)
		    printf ("%s not found", kwd[ikwd]);
		else
		    strcat (outline, "___");

		if (verbose)
		    printf ("\n");
		else if (ikwd < nkwd-1) {
		    if (tabout)
			strcat (outline, "	");
		    else
			strcat (outline, " ");
		    }
		}

	    if (!verbose && (nfile < 2 || nfound > 0 || listall))
		printf ("%s\n", outline);
	    }
	    }

	line = strchr (line+1, newline);
	if (line == NULL)
	    break;
	if (strlen (line) < 1)
	    break;
	if (*++line == (char) 0)
	    break;
	if (nkeep > 0 && *nextkeep == lastkeep)
	    break;
	}

    tabclose (tabtable);
    tabtable = NULL;
    return;
}

/* Jan 22 1999	New program
 * Jan 25 1999	Keep header information
 * Mar  9 1999	Add range of lines; rework command line decoding logic
 * Oct 22 1999	Drop unused variables after lint
 *
 * Jan  3 2000	Use isrange() to check for ranges
 * Jan  4 2000	If no keywords are specified, print entire line if tests met
 * Feb 16 2000	Always open tab table if printing headers OR putting out tab
 * May 26 2000	Handle multiple tables in a single file
 * May 26 2000	Print full header if all columns asked for
 *
 * Jun 11 2001	Add buffer size argument to tabopen()
 * Jun 18 2001	Use token parsing to speed column extraction
 * Jun 29 2001	Open stdin only once
 * Oct 10 2001	Fix bugs in condition handling
 *
 * Feb 21 2002	Improve line range implementation
 * Apr 10 2002	Fix bug dealing with ranges
 *
 * Jan 22 2004	Increase maximum number of columns from 100 to 200
 * Apr 15 2004	Avoid removing trailing zeroes from exponents
 *
 * Sep 28 2005	Increase columns from 200 to 500, lines from 1000 t0 5000
 *
 * Jun 21 2006	Drop unused; initialize uninitialized variables
 * Jun 29 2006	Rename strclean() strfix() and move to hget.c
 *
 * Jan 10 2007	Declare RevMsg static, not const
 * Jan 10 2007	Drop unused variable numstr
 *
 * Mar 30 2011	Fix str/string bug so values are returned correctly
 */