1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)system.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <stdio.h>
13 
14 #include "../general/general.h"
15 #include "../ctlr/api.h"
16 #include "spint.h"
17 
18 #include "../general/globals.h"
19 
20 
21 static Spint spinted;
22 static char command[256];
23 static int need_to_start = 0;
24 
25 /*
26  * shell_continue() actually runs the command, and looks for API
27  * requests coming back in.
28  *
29  * We are called from the main loop in telnet.c.
30  */
31 
32 int
33 shell_continue()
34 {
35     /*
36      * spint_start() returns when either the command has finished, or when
37      * the required interrupt comes in.  In the latter case, the appropriate
38      * thing to do is to process the interrupt, and then return to
39      * the interrupt issuer by calling spint_continue().
40      */
41     if (need_to_start) {
42 	need_to_start = 0;
43 	spint_start(command, &spinted);
44     }
45 
46     if (spinted.done == 0) {
47 	/* Process request */
48 	handle_api(&spinted.regs, &spinted.sregs);
49 	spint_continue(&spinted);
50     } else {
51 	char inputbuffer[100];
52 
53 	if (spinted.rc != 0) {
54 	    fprintf(stderr, "Process generated a return code of 0x%x.\n",
55 								spinted.rc);
56 	}
57 	printf("[Hit return to continue]");
58 	fflush(stdout);
59 	(void) gets(inputbuffer);
60 	shell_active = 0;
61 	setconnmode();
62 	ConnectScreen();
63     }
64     return shell_active;
65 }
66 
67 
68 /*
69  * Called from telnet.c to fork a lower command.com.  We
70  * use the spint... routines so that we can pick up
71  * interrupts generated by application programs.
72  */
73 
74 
75 int
76 shell(argc,argv)
77 int	argc;
78 char	*argv[];
79 {
80 
81     ClearElement(spinted);
82     spinted.int_no = API_INTERRUPT_NUMBER;
83     if (argc == 1) {
84 	command[0] = 0;
85     } else {
86 	char *cmdptr;
87 	int length;
88 
89 	argc--;
90 	argv++;
91 	strcpy(command, " /c");
92 	cmdptr = command+strlen(command);
93 	while (argc) {
94 	    if ((cmdptr+strlen(*argv)) >= (command+sizeof command)) {
95 		fprintf(stderr, "Argument list too long at argument *%s*.\n",
96 			    *argv);
97 		return 0;
98 	    }
99 	    *cmdptr++ = ' ';		/* Blank separators */
100 	    strcpy(cmdptr, *argv);
101 	    cmdptr += strlen(cmdptr);
102 	    argc--;
103 	    argv++;
104 	}
105 	length = strlen(command)-1;
106 	if (length < 0) {
107 	    length = 0;
108 	}
109 	command[0] = length;
110     }
111     need_to_start = 1;
112     shell_active = 1;
113     return 1;			/* Go back to main loop */
114 }
115