aboutsummaryrefslogtreecommitdiff
path: root/pkg/system/cmdstr.x
blob: be4e965ed0ad1c817f01c7f079134399ffb5324c (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<ctype.h>

define	SZ_CMDSTR	4096

# CMDSTR -- Read the output of LPARAM and format a command string giving
# the values of all parameters.

procedure t_cmdstr()

bool	hidden, hparam
pointer	sp, ltask, pname, ibuf, obuf, ip, op, pp, nl, last
int	getline(), gstrcpy(), strncmp()
bool	clgetb()

begin
	call smark (sp)
	call salloc (ltask, SZ_FNAME, TY_CHAR)
	call salloc (pname, SZ_FNAME, TY_CHAR)
	call salloc (ibuf, SZ_LINE, TY_CHAR)
	call salloc (obuf, SZ_CMDSTR, TY_CHAR)

	# Get the task name and whether to print hidden parameters.
	call clgstr ("task", Memc[ltask], SZ_FNAME)
	hidden = clgetb ("hidden")

	op = obuf + gstrcpy (Memc[ltask], Memc[obuf], SZ_CMDSTR)
	Memc[op] = ' ';  op = op + 1
	Memc[op] = '(';  op = op + 1

	last = NULL
	nl   = NULL

	while (getline (STDIN, Memc[ibuf]) != EOF) {
	    ip = ibuf

	    # Skip white space.
	    while (IS_WHITE (Memc[ip]))
		ip = ip + 1

	    # Check if the parameter is hidden and skip it if desired.
	    if (Memc[ip] == '(') {
		if (!hidden)
		    next
		hparam = true
		ip = ip + 1
	    } else
		hparam = false

	    # Check if parameter name is "mode" and skip it.
	    if (strncmp (Memc[ip], "mode =", 6) == 0)
		next

	    # Copy or skip parameter name.
	    pp = pname
	    while (!IS_WHITE (Memc[ip])) {
		if (hparam) {
		    Memc[op] = Memc[ip]
		    op = op + 1
	        }
		Memc[pp] = Memc[ip]
		pp = pp + 1
		ip = ip + 1
	    }
	    Memc[pp] = EOS

	    # Copy or skip = and skip whitespace.
	    if (hparam) {
		Memc[op] = '='
		op = op + 1
	    }
	    ip = ip + 3

	    # Copy parameter value.  It is an error if there is no value.
	    if (IS_WHITE (Memc[ip]) || (Memc[ip] == ')' && Memc[ip+1] != '_')) {
		call sprintf (Memc[obuf], SZ_CMDSTR,
		    "Undefined parameter value (%s.%s)")
		    call pargstr (Memc[ltask])
		    call pargstr (Memc[pname])
		call error (1, Memc[obuf])
	    }

	    # If the parameter is a quoted string copy until the closing quote,
	    # otherwise copy until whitespace or ).

	    if (Memc[ip] == '"') {
		Memc[op] = Memc[ip]
		ip = ip + 1
		op = op + 1
		while (Memc[ip] != '"') {
		    Memc[op] = Memc[ip]
		    ip = ip + 1
		    op = op + 1
		}
		Memc[op] = Memc[ip]
		ip = ip + 1
		op = op + 1

	    } else if (Memc[ip] == ')' && Memc[ip+1] == '_') {
		# If the value is a redirection, e.g. ")_.foo", add quotes
		# around the value and copy as a special case.
		Memc[op] = '"'
		op = op + 1

		# Copy the opening paren.
		Memc[op] = Memc[ip]
		op = op + 1
		ip = ip + 1

		# Copy the rest of the string.
		while (!IS_WHITE(Memc[ip]) && (Memc[ip] != ')')) {
		    Memc[op] = Memc[ip]
		    ip = ip + 1
		    op = op + 1
		}

		# Add the closing quote.
		Memc[op] = '"'
		op = op + 1

	    } else {
		while (!IS_WHITE(Memc[ip]) && (Memc[ip] != ')')) {
		    Memc[op] = Memc[ip]
		    ip = ip + 1
		    op = op + 1
		}
	    }

	    # Add a comma and a space.
	    Memc[op] = ','
	    op = op + 1
	    Memc[op] = ' '
	    op = op + 1

	    # Replace the last break with a new line if the line exceeds max.
	    if ((op - nl > 80) && (last > 0)) {
		Memc[last] = '\n'
		nl = last
	    }
	    last = op - 1
	}

	# Replace last comma and space by a parenthesis and EOS.
	if (Memc[op-2] == ',')
	    op = op - 2

	Memc[op] = ')'
	op = op + 1
	Memc[op] = EOS

	# Print the command string and finish up.
	call putline (STDOUT, Memc[obuf])
	call putci   (STDOUT, '\n')

	call sfree (sp)
end