-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdSubr.c
327 lines (309 loc) · 9.2 KB
/
cmdSubr.c
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
/* $Id$
* Author: Roger A. Cole
* Date: 10-24-90
*
* Experimental Physics and Industrial Control System (EPICS)
*
* Copyright 1991-92, the Regents of the University of California,
* and the University of Chicago Board of Governors.
*
* This software was produced under U.S. Government contracts:
* (W-7405-ENG-36) at the Los Alamos National Laboratory,
* and (W-31-109-ENG-38) at Argonne National Laboratory.
*
* Initial development by:
* The Controls and Automation Group (AT-8)
* Ground Test Accelerator
* Accelerator Technology Division
* Los Alamos National Laboratory
*
* Co-developed with
* The Controls and Computing Group
* Accelerator Systems Division
* Advanced Photon Source
* Argonne National Laboratory
*
* Modification Log:
* -----------------
* .00 10-24-90 rac initial version
* .01 06-18-91 rac installed in SCCS
* .02 12-05-91 rac abandon use of lightweight process library;
* cmdRead ignores blank lines and comment
* lines; added cmdInitContext
* .03 09-14-92 rac discontinue use of special malloc routines
*
* make options
* -DvxWorks makes a version for VxWorks
* -DNDEBUG don't compile assert() checking
* -DDEBUG compile various debug code
*/
/*+/mod***********************************************************************
* TITLE cmdSubr - routines for implementing keyboard command processing
*
* DESCRIPTION
*
* long cmdBgCheck( pCxCmd )
* void cmdCloseContext( ppCxCmd )
* void cmdInitContext( ppCxCmd, prompt )
* char *cmdRead( ppCxCmd )
* void cmdSource( ppCxCmd )
*
* BUGS
* o if changes in the command context (e.g., re-directing output) are
* made at other than root context level, such changes aren't preserved
* when closing out the level and moving to the previous level.
*
*-***************************************************************************/
#ifdef vxWorks
/*----------------------------------------------------------------------------
* includes and defines for VxWorks compile
*---------------------------------------------------------------------------*/
# include <vxWorks.h>
# include <stdioLib.h>
# include <ctype.h>
#else
/*----------------------------------------------------------------------------
* includes and defines for Sun compile
*---------------------------------------------------------------------------*/
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <sys/types.h> /* for 'select' operations */
#ifndef _WIN32
# include <sys/time.h> /* for 'select' operations */
# include <sys/resource.h>
#endif
# include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#endif
#endif
#include <genDefs.h>
#include <cmdDefs.h>
#include <nextFieldSubrDefs.h>
/*+/subr**********************************************************************
* NAME cmdBgCheck - validate a ``bg'' command
*
* DESCRIPTION
* Check to see if a bg command can be honored. This depends on
* whether the command was in a source'd file (can't have bg there),
* and on whether running under VxWorks (bg is OK) or UNIX (not OK).
*
* If the command shouldn't be honored, this routine prints a message.
*
* RETURNS
* OK, or
* ERROR if the bg command should not be honored
*
*-*/
long
cmdBgCheck(pCxCmd)
CX_CMD *pCxCmd; /* IO pointer to command context */
{
HELP_TOPIC *pBgTopic;
if (pCxCmd != pCxCmd->pCxCmdRoot) {
(void)printf("can't use bg command in source file\n");
return ERROR;
}
#ifdef vxWorks
return OK;
#else
pBgTopic = helpTopicFind(&pCxCmd->helpList, "bg");
if (pBgTopic != NULL)
helpTopicPrint(stdout, pBgTopic);
return ERROR;
#endif
}
/*+/subr**********************************************************************
* NAME cmdRead - read the next input line
*
* DESCRIPTION
* If a prompt hasn't previously been printed, prints a prompt (except
* if input is from a file). Then a check is made to see if input is
* available. If not, NULL is returned. If input is available, it is
* read into the buffer in the command context.
*
* If input is from a source'd file, no prompt is printed, and the
* input line is printed. At EOF on the file, the "source level"
* in the command context is closed, changing to the previous
* source level; the line buffer will contain a zero length line.
*
* Under VxWorks, this routine always waits until input is available,
* rather than doing a check and early NULL return if none is ready.
*
* RETURNS
* char * pointer to input, or
* NULL if no input was obtained
*
* BUGS
* o under VxWorks, with stdout redirected to a socket, a prompt which
* doesn't end with '\n' doesn't get sent
*
* SEE ALSO
* cmdSource()
*
*-*/
char *
cmdRead(ppCxCmd)
CX_CMD **ppCxCmd; /* I pointer to pointer to command context */
{
#ifndef vxWorks
#ifndef _WIN32
fd_set fdSet; /* set of fd's to watch with select */
int fdSetWidth; /* width of select bit mask */
struct timeval fdSetTimeout;/* timeout interval for select */
struct rlimit rlp;
#endif
#endif
CX_CMD *pCxCmd=*ppCxCmd;/* pointer to command context */
int i;
/*-----------------------------------------------------------------------------
* for keyboard and socket input, check to see if input is available
*----------------------------------------------------------------------------*/
if (pCxCmd->inputName == NULL) {
if (pCxCmd->prompt != NULL && pCxCmd->promptFlag) {
(void)printf("%s", pCxCmd->prompt);
(void)fflush(stdout);
pCxCmd->promptFlag = 0;
}
#ifndef vxWorks
#ifndef _WIN32
/* MDA - use getrlimit since getdtablesize() is obsolete
fdSetWidth = getdtablesize();
*/
getrlimit(RLIMIT_NOFILE,&rlp);
fdSetWidth = rlp.rlim_cur;
fdSetTimeout.tv_sec = 0;
fdSetTimeout.tv_usec = 0;
FD_ZERO(&fdSet);
FD_SET(fileno(stdin), &fdSet);
if (select(fdSetWidth, &fdSet, NULL, NULL, &fdSetTimeout) == 0)
return NULL;
#endif
#endif
pCxCmd->promptFlag = 1;
}
if (fgets(pCxCmd->line, 80, pCxCmd->input) == NULL) {
if (pCxCmd->inputName != NULL) {
(void)printf("EOF on source'd file: %s\n", pCxCmd->inputName);
}
else {
(void)printf("^D\n");
pCxCmd->inputEOF = 1;
}
clearerr(pCxCmd->input);
cmdCloseContext(ppCxCmd);
pCxCmd = *ppCxCmd;
}
else if (pCxCmd->inputName != NULL)
(void)printf("%s", pCxCmd->line);
pCxCmd->pLine = pCxCmd->line;
if ((i=nextANField(&pCxCmd->pLine, &pCxCmd->pCommand, &pCxCmd->delim)) < 1)
return NULL;
if (i == 1 && pCxCmd->delim == '#')
return NULL;
return pCxCmd->pLine;
}
/*+/subr**********************************************************************
* NAME cmdCloseContext - closes a command context
*
* DESCRIPTION
* Closes a command context. The action depends on source of input:
*
* keyboard (or socket):
* o store "quit\n" in the line buffer
*
* source'd file:
* o close out this "source level"
* o move to the previous "source level"
* o store '\0' in the line buffer
*
* RETURNS
* void
*
*-*/
void
cmdCloseContext(ppCxCmd)
CX_CMD **ppCxCmd; /* IO ptr to pointer to command context */
{
CX_CMD *pCxCmd; /* temp pointer */
assert(ppCxCmd != NULL);
if ((*ppCxCmd)->inputName == NULL)
strcpy((*ppCxCmd)->line, "quit\n");
else {
(void)fclose((*ppCxCmd)->input);
assert((*ppCxCmd)->pPrev != NULL);
pCxCmd = (*ppCxCmd)->pPrev;
free((char *)(*ppCxCmd));
*ppCxCmd = pCxCmd;
(*ppCxCmd)->line[0] = '\0';
}
}
/*+/subr**********************************************************************
* NAME cmdInitContext - closes a command context
*
* DESCRIPTION
* Initializes a command context.
*
* RETURNS
* void
*
*-*/
void
cmdInitContext(pCxCmd, prompt)
CX_CMD *pCxCmd; /* I pointer to command context */
char *prompt; /* I pointer to static prompt string */
{
assert(pCxCmd != NULL);
pCxCmd->promptFlag = 1;
pCxCmd->input = stdin;
pCxCmd->inputEOF = 0;
pCxCmd->inputName = NULL;
pCxCmd->dataOut = stdout;
pCxCmd->dataOutRedir = 0;
pCxCmd->prompt = prompt;
pCxCmd->pPrev = NULL;
pCxCmd->pCxCmdRoot = pCxCmd;
}
/*+/subr**********************************************************************
* NAME cmdSource - process a ``source'' command
*
* DESCRIPTION
* Processes a "source fileName" command. A new command context is
* built and set up for reading from the named file. If an error
* is detected, the present command context is preserved.
*
* RETURNS
* void
*
*-*/
void
cmdSource(ppCxCmd)
CX_CMD **ppCxCmd; /* IO ptr to pointer to command context. The
pCommand item is assumed to point to the
source command. This routine scans for
the file name. */
{
CX_CMD *pCxCmdNew; /* new command context structure */
CX_CMD *pCxCmd; /* present command context structure */
pCxCmd = *ppCxCmd;
pCxCmd->fldLen =
nextNonSpaceField(&pCxCmd->pLine, &pCxCmd->pField, &pCxCmd->delim);
if (pCxCmd->fldLen <= 1) {
(void)printf("you must specify a file name\n");
return;
}
if ((pCxCmdNew = (CX_CMD *)malloc(sizeof(CX_CMD))) == NULL) {
(void)printf("couldn't malloc command structure\n");
return;
}
*pCxCmdNew = *pCxCmd; /* inherit useful info from present context */
if ((pCxCmdNew->input = fopen(pCxCmd->pField, "r")) == NULL) {
(void)printf("couldn't open file\n");
free((char *)pCxCmdNew);
return;
}
pCxCmdNew->pPrev = pCxCmd;
pCxCmdNew->inputName = pCxCmd->pField;
*ppCxCmd = pCxCmdNew;
}