aboutsummaryrefslogtreecommitdiff
path: root/pkg/cl/lists.c
blob: d42b59239f680659b1858fcf858fd3daec3e197e (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
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#define import_spp
#define import_libc
#define import_stdio
#include <iraf.h>

#include "config.h"
#include "mem.h"
#include "operand.h"
#include "param.h"
#include "task.h"
#include "errs.h"
#include "proto.h"


/*
 * LISTS -- Access lists for list-structured parameters.
 */

extern	char *eofstr;
extern	char *nullstr;
extern	int	cldebug;


/* READLIST -- Read next value from list-structured parameter *pp and return
 * an operand.  Operand will be UNDEF if there was no file or cannot open the
 * named file (this will generate a query for the param) or eofstr if eof.
 * As a special case, check for the value of the param being the string "stdin"
 *   and read from the current standard input if it is.
 * Call error() if get ferror while reading or can't open list file.
 */
struct operand
readlist (
  struct param *pp
)
{
	struct	operand result;
	int	bastype;
	char	*line;

	result.o_type = OT_INT;		/* in case we make an undef op	*/
	line = pp->p_listval;

	if ((pp->p_valo.o_type & OT_UNDEF) || *pp->p_val.v_s == '\0') {
	    /* no list file name. */
	    pp->p_flags &= ~P_LEOF;
	    setopundef (&result);
	    return (result);
	}

	if (pp->p_listfp == NULL && !(pp->p_flags & P_LEOF)) {
	    char *filename = pp->p_val.v_s;
	    if (!strcmp (filename, "STDIN") || !strcmp (filename, "stdin"))
		pp->p_listfp = currentask->t_stdin;
	    else if ((pp->p_listfp = fopen (filename, "r")) == NULL) {
		/* should we tell user what's happening?
		cl_error (E_UERR|E_P, "can not open list file `%s'",
		    pp->p_val.v_s);
		 */
		setopundef (&result);
		return (result);
	    }
	}

	bastype = pp->p_type & OT_BASIC;

	if (pp->p_listfp != NULL) {
again:	    fgets (line, SZ_LINE, pp->p_listfp);
	    if (ferror (pp->p_listfp)) {
		closelist (pp);
		/* Don't just let it go as undefined if get an actual error. */
		cl_error (E_UERR|E_P, "list file read err");

	    } else if (feof (pp->p_listfp)) {
		closelist (pp);
		pp->p_flags |= P_LEOF;
		result = makeop (eofstr, OT_STRING);

	    } else {
		char *index(), *nlp, *ip;

		nlp = index (line, '\n');
		if (nlp != NULL)
		    *nlp = '\0';

		/* If not simple list structured struct type parameter (used
		 * to get raw lines from a text file), ignore blank lines and
		 * comments lines in the list.
		 */
		if (bastype != OT_STRING ||
		    pp->p_type & (PT_FILNAM|PT_GCUR|PT_IMCUR|PT_UKEY)) {

		    for (ip=line;  *ip && (*ip == ' ' || *ip == '\t');  ip++)
			;
		    if (*ip == EOS || *ip == '#')
			goto again;
		}
			
		result = makeop (line, bastype);
	    }

	} else
	    result = makeop (eofstr, OT_STRING);

	return (result);
}


/* CLOSELIST -- Close the list file in list-structured param pp.
 * We assume (pp->p_type & PT_LIST) but do check that the file is not
 * already closed and that we're not closing the real stdin.
 */
void
closelist (
  register struct param *pp
)
{
	if (pp->p_listfp != NULL) {
	    if (pp->p_listfp != stdin)
		fclose (pp->p_listfp);
	    pp->p_listfp = NULL;
	}
}