home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / 7 < prev    next >
Encoding:
Text File  |  1992-12-09  |  6.2 KB  |  146 lines

  1. /*
  2.  *   exec.cf -- extra-ANSI 
  3.  *
  4.  *   Facilities for executing subprocesses.
  5.  *
  6.  *           Copyright (c) 1990, MetaWare Incorporated
  7.  */
  8.  
  9. #ifndef _EXEC_CF
  10. #define _EXEC_CF
  11.  
  12. #include <implemen.cf>
  13. #include <language.cf>
  14. #pragma Global_aliasing_convention(_Private_routine_prefix "%r");
  15. #pragma Calling_convention(PASCAL);
  16.  
  17. /* Progname is a nul-terminated string naming the program to be executed.
  18.  */
  19. int c_exec(char *Progname, void *Commandstring);
  20.  
  21. /* Progname is a nul-terminated string, and the length of
  22.  * the environment must be passed in.
  23.  */
  24. int c_exec_env
  25.    (char *Progname, char *Commandstring, char *Environment, unsigned Envlen);
  26.                                        
  27. #pragma Global_aliasing_convention();
  28. #pragma Calling_convention();
  29.  
  30. #if 0 
  31.                   How to EXEC subprocesses in MetaWare High C.
  32.                   
  33. There are two parts to this documentation:  an explanation of how the
  34. functions work, and an explanation of memory requirements.  Be sure to
  35. read the latter, no matter which function you intend to use.
  36.  
  37. You can use the library functions c_exec, c_exec_env, and system for executing
  38. subprocesses (to completion), and obtaining their return code.  (On DOS this
  39. is the return code from COMMAND.COM; unfortunately it-s only whether or not the
  40. program loaded.)  system is ANSI-standard; the other two are not.  To use the 
  41. first two you MUST include the "exec.cf" header file.  To use system you can 
  42. include stdlib.h to obtain type-checking protection, but you need not include 
  43. anything.
  44.  
  45. system takes an arbitrary string S and passes it to COMMAND.COM for execution.
  46. For example:
  47.  
  48.     system("ls *.p");
  49.     
  50. is the same as if you had typed 
  51.  
  52.     command -C ls *.p
  53.     
  54. at a DOS prompt.  This only works if the COMSPEC environment variable is
  55. properly set to the path for COMMAND.COM.
  56.     
  57. c_exec takes a program name to execute (you must specify the program
  58. name completely; no searching of the environment variable PATH is done) and
  59. a parameter string.
  60.  
  61. The parameter string S is at most 129 bytes long.  The first byte is the 
  62. number of characters following the first byte.  For example:
  63.  
  64.    c_exec("/bin/ls.exe","\5*.asm");
  65.    
  66. c_exec_env is the same as c_exec except it also takes an environment string.
  67. The environment string is of the form
  68.     S* 0               (S* means zero or more Ss)
  69. where each S is a C string (nul-terminated) of the form
  70.     somename=sometext
  71. "somename" is the name of the environment variable; sometext is its value.
  72. See the "set" command in your DOS manual, and the description of the
  73. environment in the DOS technical reference manual.
  74. For example:
  75.  
  76.    #define ENV "VAR1=a b\0VAR2=xyz\0COMSPEC=c:\\command.com\0\0"
  77.    c_exec_env("c:/command.com",
  78.            "\x9/C set\xd ", 
  79.            ENV, sizeof(ENV));
  80.    
  81. executes command.com with the instruction to print out the environment
  82. ("set").  The environment printed will contain just VAR1, VAR2, and COMSPEC
  83. (variables should be in upper case to conform to DOS conventions).
  84. Note also that the length of the enviroment must be passed in since
  85. embedded 0s prevent "strlen" from being used.
  86. Also, if you omit COMSPEC from the environment, COMMAND.COM will hang.
  87. This appears to be a DOS bug.
  88.    
  89.  
  90. MEMORY REQUIREMENTS FOR EXEC-ing PROGRAMS:
  91.  
  92. For EXEC to work in small-data memory models (small and medium) nothing
  93. need be done.  However, note that by default 64K of stack and heap
  94. will be consumed by a small-data-model program, and the rest freed to
  95. DOS for use in EXECing subprograms.  To reduce the 64K to less, you must
  96. use the CPARMAXALLOC parameter to the Microsoft linker when linking the
  97. program.  CPARMAXALLOC is the number of paragraphs you want to use
  98. for both heap and stack in your program.
  99. For example, -CPARMALLOC:100 specifies only 100 paragraphs
  100. (= 1600 bytes) of memory to be made available; the default is FFFF
  101. (= 1 megabyte), so that the maximum memory is allocated.  You can
  102. also change this value at a late date by modifying locations C and D (hex)
  103. of the .exe file; therein is stored CPARMAXLLOC.
  104.  
  105. For EXEC to work in non-small-data memory models (compact, big, large),
  106. you must provide that some memory is given back to DOS.  Again, you
  107. can use CPARMAXLLOC to reduce the memory requirements.  If your program-s
  108. memory requirements don-t vary much, this is an acceptable solution.
  109. However, if your program could effectively use all of memory sometimes,
  110. then you need a solution that allows use of all of memory but takes that
  111. memory only as it needs it, leaving the rest for EXECing programs.
  112. This second solution is obtained by specifying that
  113. the run-time system should 
  114.     (a) allocate a stack of fixed size, 
  115.     (b) free all other memory back to DOS, and finally 
  116.     (c) allocate any heap by calling DOS to get the memory.  
  117. To do this you will need
  118. the Microsoft MASM assembler to re-assemble the initializer, and you
  119. will need to link in an alternative memory allocation routine rather
  120. than the default one supplied in the library.  Do the following to 
  121. the initiliazer source init.asm in the lib/src subdirectory:
  122.  
  123.    a. Set variable STACK_SIZE to whatever size you want the stack.
  124.       (Normally the stack size is dependent only upon available memory
  125.       and the architecture-imposed restriction of 64K max.)
  126.       For example, "STACK_SIZE = 2000" bytes.  
  127.       (For more information on STACK_SIZE, see initializer source.)
  128.    b. Set variable USE_DOS_ALLOC (to anything).  For example,
  129.       "USE_DOS_ALLOC = 1".
  130.       (For more information on USE_DOS_ALLOC, see initializer source.)
  131.    c. Assemble the initializer from the appropriate model subdirectory
  132.       so that the memory model information is set properly.   
  133.    d. Link your program with "dosalloc.obj" included (the Pascal source to
  134.       dosalloc.p is provided, and for the C compiler, a version of
  135.       dosalloc.obj is provided for the compact, big, and large models), 
  136.       and with the newly-assembled initializer.
  137.       
  138. The primary disadvantage of using DOS to allocate memory is that
  139. if your program erroneously writes out of bounds of allocated memory,
  140. you will clobber DOS-s memory links and it will print out
  141. "memory allocation error" and HALT THE OS, requiring a reboot, with
  142. no chance of debugging your program.
  143.  
  144. #endif
  145. #endif /* _EXEC_CF */
  146.