home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / me / spawn.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  7KB  |  256 lines

  1. /*
  2.  *
  3.  *    SPAWN.C MODULE
  4.  * Routines to create or run subjobs in the system command interpreter.
  5.  */
  6.  
  7. /* Copyright 1990, 1991, 1992 Craig Durland
  8.  *   Distributed under the terms of the GNU General Public License.
  9.  *   Distributed "as is", without warranties of any kind, but comments,
  10.  *     suggestions and bug reports are welcome.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include "me2.h"
  15.  
  16. #if MSDOZ
  17. #include <process.h>
  18. #endif
  19.  
  20. extern char *getenv();
  21.  
  22. /* Fork a shell so the user can mess around outside of ME and return to
  23.  *   their edit session later.
  24.  * When the command interpreter exits, mark the screen as garbage so that
  25.  *   you do a full repaint.
  26.  * Input:
  27.  *   f :  An implementation defined f.  1 => do the "normal" thing (fork a
  28.  *      shell).
  29.  *     For Unix, f != 1 => use job control and put ME into the background.
  30.  * Output:
  31.  *   TRUE!!!???
  32.  */
  33.  
  34. int dont_spawn = FALSE;        /* So a driver, etc can disable spawning */
  35.  
  36. spawn_shell(f)
  37. {
  38. #if MSDOZ
  39.   register char *ptr;
  40.  
  41.   if (dont_spawn) return FALSE;
  42.  
  43.   close_display(TRUE);    /* restore terminal for normal user interface */
  44.  
  45.   if ((ptr = getenv("SHELL")) != NULL && *ptr != '\0')
  46.     spawnlp(P_WAIT,ptr,"ME",(char *)NULL);
  47.   else spawnlp(P_WAIT,"command.com", "ME", (char *)NULL);
  48.  
  49. #if 0        /* old Lattice C */
  50.   if ((ptr = getenv("SHELL")) != NULL && *ptr != '\0')
  51.     forklp(ptr,"ME",(char *)NULL);
  52.   else forklp("command.com", "ME", (char *)NULL);
  53. #endif
  54.  
  55.   open_display();            /* set up terminal for editor */
  56.   return TRUE;
  57. #endif /* MSDOZ */
  58.  
  59.  
  60.  
  61. #if UX_OS
  62.   register char *ptr;
  63.  
  64.   if (dont_spawn) return FALSE;
  65.  
  66.   if (f != 1) sig_pause(0);        /* try and use job control */
  67.   else
  68.   {
  69.     close_display(TRUE);      /* restore terminal for normal user interface */
  70.  
  71.     if ((ptr = getenv("SHELL")) != NULL && *ptr != '\0')
  72. #if 1
  73.     system(ptr);            /* system("/bin/ksh"); */
  74. #else    /* ??? */
  75.     {
  76.       char buf[300], *strcpy(), *strcat();
  77.  
  78.       system(strcat(strcpy(buf,"exec "), ptr));    /* system("exec /bin/ksh"); */
  79.     }
  80. #endif
  81.     else system("exec sh");        /* default shell */
  82.     open_display();        /* set up terminal for editor */
  83.   }
  84.   return TRUE;
  85. #endif /* UX_OS */
  86.  
  87.  
  88.  
  89. #if ATARI
  90.   register char *ptr;
  91.  
  92.   if (dont_spawn) return FALSE;
  93.  
  94. #ifdef __MINT__
  95.   if (f != 1) sig_pause(0);        /* try and use job control */
  96.   else
  97. #endif /* __MINT__ */
  98.   {
  99.     close_display(TRUE);      /* restore terminal for normal user interface */
  100.     if ((ptr = getenv("SHELL")) != NULL && *ptr != '\0')
  101.     system(ptr);            /* system("/bin/ksh"); */
  102.     open_display();        /* set up terminal for editor */
  103.   }
  104.   return TRUE;
  105. #endif    /* ATARI */
  106. }
  107.  
  108.  
  109. /*
  110.  * Have the shell (or OS) run a command (such as LS).
  111.  * When the command returns and we should wait, print a prompt and wait for
  112.  *   a single character to be typed and mark the screen as garbage so a full
  113.  *   repaint is done.
  114.  * Input:
  115.  *   command : command to give the shell
  116.  *   prompt  : If not NULL, what to ask the user (if ask-pgm).
  117.  * Output:
  118.  *   TRUE: Everything went as expected.
  119.  *   FALSE: Command failed, for whatever reason.
  120.  * Notes:
  121.  *   Only asks the user if (ask-user) and there is a prompt.
  122.  *   The command might garbage the screen and its only marked as garbage if
  123.  *     we have to prompt so its up to the caller to take care of this
  124.  *     possibility (if it wants to).
  125.  */
  126. shell_command(command, prompt) char *command, *prompt;
  127. {
  128.   extern int MMask_pgm;        /* in mm.c */
  129.  
  130.   int s, ask;
  131.  
  132.   ask = (!MMask_pgm && prompt);
  133.  
  134.   if (ask)        /* restore terminal for normal user interface */
  135.     close_display(FALSE);
  136.  
  137. #if UX_OS || MSDOZ || ATARI
  138.   if (ask) t_putchar('\n');  /* so the cmd doesn't overwrite the minibuffer */
  139.   s = system(command);        /* !!!??? returns -1 or command return code */
  140. #endif /* UX_OS || MSDOZ || ATARI */
  141.  
  142.   if (ask)
  143.   {
  144.     t_puts(prompt); t_flush();
  145.     while ((s = t_getchar()) != '\r' && s != '\n') ;
  146.     open_display();    /* takeover terminal AFTER resuming editing! */
  147.   }
  148.  
  149.   return !s;
  150. }
  151.  
  152. /* Filter some ME stuff through the OS (usually the shell).  
  153.  * Output from the filter:
  154.  *   Zip:  the filters output (stdout) is thrown away.
  155.  *   bag:  the filter output is stored in a bag.  The old bag contents is
  156.  *     lost.
  157.  *   Might have inserted the output of the filter at dot.
  158.  * Input to os_filter:
  159.  *   filter_name:  name of the filter to invoke.
  160.  *   in_bag: If !NULL, contains stuff to use as stdin.
  161.  *   out_bag: If !NULL, store stdout in there.
  162.  *   insert_output_at_dot: If TRUE, insert stdout at dot.
  163.  * Returns:  TRUE if everything worked, FALSE if problems.
  164.  * Notes:
  165.  *   If !out_bag and !insert_output_at_dot then don't need to "> stdout"
  166.  *     (except to keep stdout from writing all over the screen).  Could use
  167.  *     /dev/null for stdout that probably only works on UNIX.
  168.  *   The filter might garbage the screen - its up to the caller to take care
  169.  *     of this possibility (if it wants to).
  170.  *   If there is no input bag, I'd like to create an empty file as std
  171.  *     input.  This would keep a filter from waiting for user input if it
  172.  *     really wants input.  However, this won't work for pipes:  "foo | bar"
  173.  *     because the filter would be "foo | bar < x > y" which would cause bar
  174.  *     to ignore the output of foo.  So, I have to use "foo | bar > y" and
  175.  *     to keep things from locking up (can't send bar a EOF), reset the
  176.  *     terminal.  This also means that the user can type to the filter (but
  177.  *     won't see the filters output).  To avoid this, send in an empty bag
  178.  *     to used as stdin.
  179.  */
  180. #define CREATE_STDIN    FALSE
  181. os_filter(filter_name,in_bag,out_bag, insert_output_at_dot)
  182.   char *filter_name;
  183.   Bag *in_bag, *out_bag;
  184.   int insert_output_at_dot;
  185. {
  186.   extern char *strcpy(), *strcat(), *unique_file_name();
  187.  
  188.   char filter[NLINE+300], fin[150], fout[150];
  189.   FILE *fptr;
  190.   int s;
  191.  
  192.   if (in_bag)
  193.   {
  194.     unique_file_name(fin);
  195.     if (!bag_to_file(in_bag,fin)) return FALSE;
  196.   }
  197.   else        /* no stdin */
  198.   {
  199. #if CREATE_STDIN
  200.     unique_file_name(fin);
  201.         /* create an empty file for input */
  202.     if (!(fptr = fopen(fin,"w")))        /* open filters' stdin */
  203.       { mlwrite("Can't open %s as stdin",fin); return FALSE; }
  204.     fclose(fptr);
  205. #else
  206.     close_display(FALSE);
  207. #endif
  208.   }
  209.  
  210.   unique_file_name(fout);
  211.  
  212.     /* create the command line for the OS.
  213.      * "filter >stdout" or "filter >stdout <stdin"
  214.      */
  215. #if CREATE_STDIN
  216.   strcat(strcat(
  217.     strcat(strcat( strcpy(filter,filter_name), " <"),fin),
  218.     " >"),fout);
  219. #else
  220.   strcat(strcat( strcpy(filter,filter_name), " >"),fout);
  221.   if (in_bag) strcat(strcat(filter, " <"),fin);
  222. #endif
  223.   if (system(filter))    /* !!!??? return return code? */
  224.   {
  225.     mlwrite("Filter failed.");
  226.     s = FALSE;
  227.     goto done;
  228.   }
  229.  
  230.     /* mess with output of the filter */
  231.   if (insert_output_at_dot)    /* insert the filters output at dot */
  232.   {
  233.     int n, error_code;
  234.  
  235.     if (!(s = insert_file(fout, &n, &error_code)))
  236.       mlwrite("Insert failed!");
  237.   }
  238.  
  239. done:
  240.  
  241.   if (out_bag)        /* copy the filters output to a bag */
  242.     { clear_bag(out_bag); s = file_to_bag(fout,out_bag); }
  243.  
  244.   /* if they want to ignore the output, ignore it */
  245.  
  246.   unlink(fout);
  247. #if CREATE_STDIN
  248.   unlink(fin);
  249. #else
  250.   if (in_bag) unlink(fin);
  251.   else          open_display();
  252. #endif
  253.  
  254.   return s;
  255. }
  256.