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

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

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

extern void u_doprnt();


/*
 * CLPRINTF -- These are just printf's with various implied write files for
 * convenience.  Also here are other assorted printing utilities.
 */

/* EPRINTF -- Printf that always writes to the current pseudo-file t_stderr.
 */
void
eprintf (char *fmt, ...)
{
	va_list	args;
	FILE *eout;

	va_start (args, fmt);
	eout = currentask->t_stderr;
	u_doprnt (fmt, &args, eout);
	va_end (args);
	fflush (eout);
}


/* OPRINTF -- Printf that always writes to the current pseudo-file t_stdout.
 */
void
oprintf (char *fmt, ...)
{
	va_list	args;
	FILE *sout;

	va_start (args, fmt);
	sout = currentask->t_stdout;
	u_doprnt (fmt, &args, sout);
	va_end (args);
	fflush (sout);
}


/* TPRINTF -- Printf that always goes through the pipe out to the currently
 * running task.  Be a bit more careful here in case a pipe is broken or
 * something is going haywire.
 */
void
tprintf (char *fmt, ...)
{
	va_list	args;
	FILE *out;

	out = currentask->t_out;
	if (out == NULL)
	    cl_error (E_IERR, "no t_out for currentask `%s'",
	    currentask->t_ltp->lt_lname);
	else {
	    va_start (args, fmt);
	    u_doprnt (fmt, &args, out);
	    va_end (args);
	    fflush (out);
	    if (ferror (out))
		cl_error (E_UERR|E_P, "pipe write error to `%s'",
		    currentask->t_ltp->lt_lname);
	}
}


/* TWRITE -- Write a binary block of data to the current task.
 *
 * This function is currently not used by anyone.
void
twrite (
  char	*buf,
  int	nbytes
)
{
	FILE *out;

	out = currentask->t_out;
	if (out == NULL) {
	    cl_error (E_IERR, "no t_out for currentask `%s'",
		currentask->t_ltp->lt_lname);
	} else if (nbytes > 0) {
	    fwrite (buf, sizeof(*buf), nbytes, out);
	    fflush (out);
	    if (ferror (out))
		cl_error (E_UERR|E_P, "pipe write error to `%s'",
		    currentask->t_ltp->lt_lname);
	}
}
*/


/* PRPARAMVAL -- Print the value field of param pp on file fp.
 * Give name of file if list, don't do anything if undefinded. 
 * Do not include a trailing \n.
 */
void
prparamval (
  struct param *pp,
  FILE	*fp
)
{
	char	buf[SZ_LINE];

	spparval (buf, pp);
	fputs (buf, fp);
}


/* STRSORT -- Sort a list of pointers to strings.
 */
void
strsort (
  char	*list[],		/* array of string pointers */
  int	nstr			/* number of strings */
)
{
	extern	int qstrcmp();

	qsort ((char *)list, nstr, sizeof(char *), qstrcmp);
}


/* QSTRCMP -- String comparison routine (strcmp interface) for STRSRT.
 */
int
qstrcmp (
  char	*a, 
  char  *b
)
{
	return (strcmp (*(char **)a, *(char **)b));
}


/* STRTABLE -- Given a list of pointers to strings as input, format and print
 * the strings in the form of a nice table on the named output file.  Adjust
 * the number of columns to fill the page (64 cols) as nearly as possible,
 * with at least two spaces between strings.  Excessively long strings
 * are truncated (adapted from "fmtio/strtbl.x").
 */
void
strtable (
  FILE	*fp,			/* output file */
  char	*list[],		/* array of string pointers */
  int	nstr,			/* number of strings */
  int	first_col,		/* where to place table on a line */ 
  int   last_col,
  int	maxch,			/* maximum chars to print from a string */
  int	ncol 			/* desired # of columns (0 to autoscale) */
)
{
	int	row, i, j, nspaces, len, maxlen, colwidth;
	int	numcol, numrow, str;
	char	*p;

	/* Find the maximum string length.  */
	maxlen = 0;
	for (i=1;  i <= nstr;  i++)
	    if ((len = strlen (list[i-1])) > maxlen)
		maxlen = len;

	/* Cannot be longer than "maxch" characters, if given.  */
	if (maxch > 0 && maxch < maxlen)
	    maxlen = maxch;

	/* Compute the optimum number of columns. */
	if ((numcol = (last_col - first_col + 1) / (maxlen + 2)) < 1)
	    numcol = 1;
	if (ncol > 0 && ncol < numcol)
	    numcol = ncol;
	colwidth = (last_col - first_col + 1) / numcol;
	numrow = (nstr + numcol-1) / numcol;

	/* For each row in the table:
	 */
	for (row=1;  row <= numrow;  row=row+1) {
	    for (i=1;  i < first_col;  i=i+1)	/* space to first col */
		putc (' ', fp);
	    /* For each string in the row:
	     */
	    for (i=1;  i <= numcol;  i=i+1) {
		str = row + (i-1) * numrow;
		if (str > nstr)
		    continue;
		p = list[str-1];		/* output string */
		for (j=0;  p[j] != '\0' && j < maxlen;  j=j+1)
		    putc (p[j], fp);
		if (i < numcol) {		/* advance to next col */
		    if ((nspaces = colwidth - j) < 2)
			nspaces = 2;
		    for (j=1;  j <= nspaces;  j=j+1)
			putc (' ', fp);
		}
	    }
	    putc ('\n', fp);			/* end of row of table */
	}
}