Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/sp_utils.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.4 $
3 *---------------------------------------------------------------------
4 * Copyright (c) 1992, Visual Edge Software Ltd.
5 *
6 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
7 * distribute this software and its documentation for any purpose
8 * and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Visual Edge Software not be
12 * used in advertising or publicity pertaining to distribution of
13 * the software without specific, written prior permission. The year
14 * included in the notice is the year of the creation of the work.
15
16 .VERSION
17 090813 last modif KB
18
19 *---------------------------------------------------------------------
20 * File_Description_Section
21 *--------------------------------------------------------------------*/
22
23 /*--- include files ---*/
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "uxproto.h"
28 #include "version.h"
29 #include "subproc.h"
30 #include "sp_spmd.h"
31 #include "sp_pty.h"
32
33
34 /*--- file global variables ---*/
35 M_FILE_VERSION("$Header")
36
37 /*--- forward declaration of static functions ---*/
38 static void InitializeSubprocMgr UXPROTO((void));
39
40
41 /*----------------------------------------------------------------------
42 * NAME: <static void InitializeSubprocMgr()>
43 * DESCRIPTION: <Initialize struct SubprocMgr_t for functions to run
44 according to type of subproc: streams or ptys >
45 * PARAMETERS: NONE
46 * RETURN: void
47 * EXT REFS: globals used
48 * EXT EFFECTS: globals or other things altered
49 * ASSUMPTIONS: the fields of the structure are accessed by #define
50 * REVISIONS: 04/01/93 fix3810 creation
51 dd/mm/yy fix# fix_description
52 *--------------------------------------------------------------------*/
53 static void InitializeSubprocMgr()
54
55 {
56 #ifdef _PTY_DRIVER
57 UX_PREPARE_COMMAND = UxPtyPrepareCommand;
58 UX_SET_PARENT_SIDE = UxGetPtty;
59 UX_SETUP_PARENT_COMMUNICATION = UxSetPseudo;
60 UX_START_CHILD = UxPtyStartChild;
61 UX_SET_CHILD_SIDE = UxSetTty;
62 UX_SETUP_CHILD_COMMUNICATION = UxPtyChildCommSetup;
63 UX_SPMD_EXEC_SUBPROCESS = UxPtyExecSubprocess;
64 UX_CLOSE_COMMUNICATION = UxPtyExitHandler;
65 #endif
66
67 #ifdef _STREAM_DRIVER
68 UX_PREPARE_COMMAND = UxStreamPrepareCommand;
69 UX_SET_PARENT_SIDE = UxGetStream;
70 UX_SETUP_PARENT_COMMUNICATION = NULL;
71 UX_START_CHILD = UxStreamStartChild;
72 UX_SET_CHILD_SIDE = UxSetStream;
73 UX_SETUP_CHILD_COMMUNICATION = UxStreamChildCommSetup;
74 UX_EXEC_SUBPROCESS = UxStreamExecSubprocess;
75 UX_CLOSE_COMMUNICATION = UxStreamExitHandler;
76 #endif
77 }
78
79
80 /*----------------------------------------------------------------------
81 * NAME: <int UxGetSp()>
82 * DESCRIPTION: <initialization of a newly created object>
83 * PARAMETERS: NONE
84 * RETURN: int - index of new process in processes array
85 * EXT REFS: UxSpmd, UxSpArray
86 * EXT EFFECTS: UxSpArray
87 * ASSUMPTIONS:
88 * REVISIONS: 05/01/93 fix3810 clean-up
89 dd/mm/yy fix# fix_description
90 *--------------------------------------------------------------------*/
91 int UxGetSp()
92
93 {
94 /* declare register as int because of PS/2 compiler */
95 /* where the default register declarations are of type const */
96 register int i;
97
98 InitializeSubprocMgr();
99
100 /* Start the Sub-Process Manager Daemon
101 * if it hasn't already been started.
102 */
103 if ((UxSpmd.spmd_pid <= 0) && (UxSpmdCreateDaemon() == ERROR)) {
104 error_msg("Subprocess Manager Daemon cannot be started");
105 return(ERROR);
106 }
107
108
109 /* look for a free slot in the array */
110 for(i = 0; i < MAX_SUBPROC; i++)
111 {
112 if( UxSpArray[i] == (SubprocInfo_t *)0)
113 break;
114 }
115 if(i == MAX_SUBPROC)
116 {
117 char *msg, *fmt;
118
119 fmt = CGETS( MS_MISC_MAX_PROCS, DS_MS_MISC_MAX_PROCS);
120 msg = UxMalloc(strlen(fmt) + 16 + 1);
121 sprintf(msg,fmt,MAX_SUBPROC);
122 error_msg(msg);
123 UxFree(msg);
124 return(ERROR);
125 }
126 else
127 {
128 if(! (UxSpArray[i] = (SubprocInfo_t *)UxCalloc(1, sizeof(SubprocInfo_t))) )
129 {
130 UxInternalError(__FILE__, __LINE__,
131 CGETS( MS_MISC_CALLOC_ERR,DS_MS_MISC_CALLOC_ERR));
132 return ERROR;
133 }
134 else
135 {
136 UxSpArray[i]->handle = -1;
137 UxSpArray[i]->pid = -1;
138 UxSpArray[i]->subprocStatus = UNDEFINED;
139 return i;
140 }
141 }
142 }
143
144
145 /*----------------------------------------------------------------------
146 * NAME: <int UxCheckIndex(index)>
147 * DESCRIPTION: <returns ERROR if invalid handle or entry not set
148 and NO_ERROR if valid>
149 * PARAMETERS: handle index - index to subproc struct
150 * RETURN: ERROR/NO_ERROR
151 * EXT REFS: UxSpArray
152 * EXT EFFECTS: globals or other things altered
153 * ASSUMPTIONS:
154 * REVISIONS: 05/01/93 fix3810 clean-up
155 11/03/93 fix3989 tested for MAX_SUBPROC - 1
156 dd/mm/yy fix# fix_description
157 *--------------------------------------------------------------------*/
158 int UxCheckIndex(index)
159 handle index;
160
161 {
162 if ((index < 0) || (index > (MAX_SUBPROC - 1))) {
163 UxError( CGETS( MS_MISC_INV_HNDL, DS_MS_MISC_INV_HNDL));
164 return(ERROR);
165 }
166
167 if( !(UxSpArray[index]) ) {
168 UxError( CGETS( MS_MISC_NOT_HNDL, DS_MS_MISC_NOT_HNDL));
169 return(ERROR);
170 }
171
172 return(NO_ERROR);
173 }
174
175
176 /*----------------------------------------------------------------------
177 * NAME: <void UxSyserr(msg1, msg2)>
178 * DESCRIPTION: <Generate an error message including the system errno.>
179 * PARAMETERS: char *msg1, *msg2; message to print.
180 * RETURN: void
181 * EXT REFS: globals_used
182 * EXT EFFECTS: globals or other things altered
183 * ASSUMPTIONS:
184 * REVISIONS: 05/01/93 fix3810 cleanup
185 * dd/mm/yy fix# fix_description
186 *--------------------------------------------------------------------*/
187 void UxSyserr(msg1, msg2)
188 char *msg1;
189 char *msg2;
190
191 {
192 int error;
193
194 extern int errno;
195
196 char buffer[BUFSIZ];
197
198 *buffer = '\0';
199 error = errno;
200
201 if (msg1 && *msg1) {
202 (void) strcat(buffer, msg1);
203 (void) strcat(buffer, ": ");
204 if (msg2 && *msg2) {
205 (void) strcat(buffer, msg2);
206 (void) strcat(buffer, ": ");
207 }
208 }
209 (void) strcat(buffer, strerror(errno));
210 (void) strcat(buffer, "\n");
211 UxStandardError(buffer);
212 }
213
214
215 /*----------------------------------------------------------------------
216 * NAME: <void UxOnExitRunExitCb(pid, status)>
217 * DESCRIPTION: <For the specified process, run the exit callback function
218 and then remove the process>
219 * PARAMETERS: int pid - process id
220 int status - status of the process
221 * RETURN: void
222 * EXT REFS: UxSpArray
223 * EXT EFFECTS: UxSpArray
224 * ASSUMPTIONS:
225 * REVISIONS: 05/01/93 fix3810 clean-up
226 02/04/93 fix4074 set subprocStatus flag
227 dd/mm/yy fix# fix_description
228 *--------------------------------------------------------------------*/
229 void UxOnExitRunExitCb(pid, status)
230 int pid;
231 int status;
232
233 {
234 int i;
235
236 if (pid != -1)
237 {
238 for (i=0; i<MAX_SUBPROC; i++)
239 {
240 if (!UxSpArray[i])
241 continue;
242 if (UxSpArray[i]->pid == pid)
243 {
244 UxSpArray[i]->subprocStatus = EXITING;
245 if (UxSpArray[i]->exit_cb) {
246 (*UxSpArray[i]->exit_cb)(pid, status, i);
247 }
248 UxSpExit(UxSpArray[i]);
249 if (UxSpArray[i]->subprocStatus!=DELAYED_EXIT) {
250 UxSpArray[i]->pid = -1;
251 UxSpArray[i]->subprocStatus = UNDEFINED;
252 }
253 break;
254 }
255 }
256 }
257 }
258
259
260 /*----------------------------------------------------------------------
261 * NAME: <char **UxBuildArgs(process, command_line)>
262 * DESCRIPTION: <Small parser of the argument list. It supports the
263 following:
264 - '\' quotes the next character.
265 - all text inside of "" is quoted (with
266 the exception of '\' quoting).
267 - all text inside of '' is quoted (with
268 the exception of '\' quoting).
269 - arguments are separated by non quoted spaces
270 or tabs.
271 - there is no attempt to remove special characters
272 since they may mean something to the application
273 which is running the command (such as:
274 sh -c "filter </etc/passwd").
275 - dynamically creates the arg list.
276 - dynamically creates storage for breaking up
277 command line. >
278 * PARAMETERS: char *process, *command_line -- args
279 * RETURN: char **args - returns pointer to the argument array
280 * EXT REFS: globals_used
281 * EXT EFFECTS: globals or other things altered
282 * ASSUMPTIONS:
283 * REVISIONS: 05/01/93 fix3810 clean-up
284 dd/mm/yy fix# fix_description
285 *--------------------------------------------------------------------*/
286 char **UxBuildArgs(process, command_line)
287 char *process, *command_line;
288
289 {
290 static char *Buffer = NULL;
291 static char **Argv = (char **) 0;
292 register char *From;
293 register char *To;
294 register char **ArgvPtr;
295 register int Quote;
296 int i;
297
298 if (! command_line)
299 command_line = "";
300
301 /* allocate storage for string to be mucked with and copy it over... */
302 if (Buffer)
303 Buffer = (char*)UxRealloc(Buffer, strlen(command_line) + 1);
304 else
305 Buffer = UxMalloc(strlen(command_line) + 1);
306 (void) strcpy(Buffer, command_line);
307
308 /* Allocate storage for Argv. We don't need to be exact. We just don't
309 * want to underestimate the number of arguments (or we trash memory),
310 * and we don't want to grossly overestimate the amount of memory. If
311 * we count all groups of spaces as separators we will be close -- we
312 * will overcount any that are quoted.
313 */
314 /* The first one is for the command being executed... */
315 for (i = 1, From = Buffer; *From; ) {
316 /* count this as a potential argument... */
317 (void) i++;
318
319 /* skip over characters... */
320 while (*From && ((*From != ' ') && (*From != '\t')))
321 (void) From++;
322
323 /* skip over white space... */
324 while (*From && ((*From == ' ') || (*From == '\t')))
325 (void) From++;
326 }
327
328 /* leave an extra one for null terminating the array... */
329 if (Argv)
330 Argv = (char **) UxRealloc((char *) Argv, (i + 1) * sizeof(char *));
331 else
332 Argv = (char **) UxMalloc((i + 1) * sizeof(char *));
333
334 /* initialize the world... */
335 ArgvPtr = Argv;
336 From = Buffer;
337 To = Buffer;
338 Quote = 0;
339
340 *ArgvPtr++ = process;
341 *ArgvPtr = To;
342
343 /*
344 ** skip over leading blanks
345 */
346
347 while (*From && ((*From == ' ') || (*From == '\t')))
348 (void) From++;
349
350 if (*From == '\0') {
351 *ArgvPtr = (char *) 0;
352 return (Argv);
353 }
354
355 while (*From) {
356 #ifdef DEBUGPARSE
357 printf ("char='%c' ", *From);
358 #endif /* DEBUGPARSE */
359
360 switch (*From) {
361 case '\'' :
362 case '"' :
363 /*
364 ** if we are not quoting, start doing so
365 */
366
367 if (Quote == 0) {
368 #ifdef DEBUGPARSE
369 printf ("quoting on\n");
370 #endif /* DEBUGPARSE */
371
372 Quote = *From++;
373 break;
374 }
375
376 /*
377 ** if we are done quoting, do so
378 */
379
380 if (Quote == *From) {
381 #ifdef DEBUGPARSE
382 printf ("quoting off\n");
383 #endif /* DEBUGPARSE */
384
385 Quote = 0;
386 From++;
387 break;
388 }
389
390 /*
391 ** else, it is a valid character
392 */
393
394 #ifdef DEBUGPARSE
395 printf ("copied\n");
396 #endif /* DEBUGPARSE */
397
398 *To++ = *From++;
399 break;
400
401 case ' ' :
402 case '\t' :
403 if (Quote) {
404 #ifdef DEBUGPARSE
405 printf ("quoted tab/space\n");
406 #endif /* DEBUGPARSE */
407
408 *To++ = *From++;
409 break;
410 }
411
412 /*
413 ** put in a null and advance ArgvPtr
414 */
415
416 #ifdef DEBUGPARSE
417 printf ("seperator\n");
418 #endif /* DEBUGPARSE */
419
420 while ((*From == ' ') || (*From == '\t'))
421 (void) From++;
422
423 *To++ = '\0';
424
425 /* if we are done, return the string. This takes care
426 * of the problem of command lines terminated by spaces.
427 */
428 if (*From == '\0') {
429 *(++ArgvPtr) = (char *) 0;
430 return (Argv);
431 }
432
433 *(++ArgvPtr) = To;
434 break;
435
436 case '\\' :
437 /*
438 ** quote the next character directly
439 */
440
441 (void) From++;
442 if (*From) {
443 #ifdef DEBUGPARSE
444 printf ("quoting '%c'\n", *From);
445 #endif /* DEBUGPARSE */
446
447 *To++ = *From++;
448 }
449 break;
450
451 default :
452 #ifdef DEBUGPARSE
453 printf ("copied\n");
454 #endif /* DEBUGPARSE */
455
456 *To++ = *From++;
457 break;
458 }
459 }
460
461 *To = '\0';
462 *(++ArgvPtr) = (char *) 0;
463 return (Argv);
464 }
465
466 #ifdef DEBUG
467 /*----------------------------------------------------------------------
468 * NAME: <int UxPrintArgs(tmp)>
469 * DESCRIPTION: <debug fnt - print the argument list>
470 * PARAMETERS: char **tmp - arg list
471 * RETURN: NONE
472 * EXT REFS: globals_used
473 * EXT EFFECTS: globals or other things altered
474 * ASSUMPTIONS:
475 * REVISIONS: 05/01/93 fix3810 clean-up
476 dd/mm/yy fix# fix_description
477 *--------------------------------------------------------------------*/
478 int UxPrintArgs(tmp)
479 char **tmp;
480 {
481 int i;
482
483 printf("Argument list\n");
484 for(i = 0; tmp[i]; i++)
485 printf("Arg %d: %s\n", i, tmp[i]);
486 printf("\n");
487 }
488 #endif /* DEBUG */
489
490
491 /*----------------------------------------------------------------------
492 * NAME: <void UxSpExit (sp)>
493 * DESCRIPTION: <Closes the pty/tty pair or the stream (if open),
494 removes the input source for X, and resets the 'input_id'.>
495 * PARAMETERS: subproc *sp - process that exits
496 * RETURN: void
497 * EXT REFS: globals_used
498 * EXT EFFECTS: UxSpArray via sp
499 * ASSUMPTIONS:
500 * REVISIONS: 13/06/89 creation
501 05/01/93 fix3810 clean-up
502 dd/mm/yy fix# fix_description
503 *--------------------------------------------------------------------*/
504
505 void UxSpExit (sp)
506 SubprocInfo_t *sp;
507
508 {
509 if(sp->handle != -1)
510 {
511 (void)close(sp->handle);
512 sp->handle = -1;
513 }
514 }
515
516
517 /*----------------------------------------------------------------------
518 * NAME: <void UxSpCheck(sp)>
519 * DESCRIPTION: <check if the subprocess is still alive>
520 * PARAMETERS: subproc *sp -- subprocess object
521 * RETURN: void
522 * EXT REFS: globals_used
523 * EXT EFFECTS: UxSpArray via sp
524 * ASSUMPTIONS:
525 * REVISIONS: 19/12/91 fix3288
526 05/01/93 fix3810 clean-up
527 dd/mm/yy fix# fix_description
528 *--------------------------------------------------------------------*/
529 void UxSpCheck(sp)
530 SubprocInfo_t *sp;
531
532 {
533 if(!sp)
534 return;
535
536 if ((kill(sp->pid, 0) == -1) && (errno != EPERM))
537 {
538 sp->pid = -1;
539 UxSpExit (sp);
540 }
541 }
542
543
544 /*----------------------------------------------------------------------
545 * NAME: <void UxOutputHandler(sp)>
546 * DESCRIPTION: <Low level output function - reads the output
547 of the subprocess into a temp. buffer and executes
548 the user function passing a pointer to the buffer.
549 NOTE: This might be changed to just passing the fd
550 and a pointer to user supplied data. >
551 * PARAMETERS: subproc *sp - subproc object
552 * RETURN: void
553 * EXT REFS: globals_used
554 * EXT EFFECTS: globals or other things altered
555 * ASSUMPTIONS:
556 * REVISIONS: 05/01/93 fix3810 cleanup
557 * dd/mm/yy fix# fix_description
558 *--------------------------------------------------------------------*/
559 void UxOutputHandler(sp)
560 SubprocInfo_t *sp;
561
562 {
563 if(!sp)
564 {
565 error_msg(CGETS( MS_MISC_INV_SPRC_STRUCT,
566 DS_MS_MISC_INV_SPRC_STRUCT));
567 return;
568 }
569
570 if(! sp->output_fnt )
571 {
572 error_msg(CGETS( MS_MISC_INV_OUT_FNCTN,
573 DS_MS_MISC_INV_OUT_FNCTN));
574 return;
575 }
576
577 (*sp->output_fnt) (sp->handle, sp->user_data);
578 }
579
580 /*----------------------------------------------------------------------
581 * NAME: <int UxKillAllSubprocs()>
582 * DESCRIPTION: <kills any remaining subprocess>
583 * PARAMETERS: none
584 * RETURN: nothing
585 * EXT REFS: UxSpArray
586 * EXT EFFECTS: UxSpArray
587 * ASSUMPTIONS:
588 * REVISIONS: 23/04/93 fix4181 handle Ctrl-C in design time
589 dd/mm/yy fix# fix_description
590 *--------------------------------------------------------------------*/
591 void UxKillAllSubprocs()
592
593 {
594 int i;
595
596 for(i = 0; i < MAX_SUBPROC; i++) {
597 if( (UxSpArray[i] != (SubprocInfo_t *)0) &&
598 (UxSpArray[i]->subprocStatus != EXITING)) {
599 UxDeleteSubproc (i);
600 }
601 }
602 }