home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DOS_HELP / ENVCAT12.ZIP / ENVIRCAT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-27  |  6.4 KB  |  283 lines

  1. /* envircat.c
  2. |
  3. | $Header:   E:/mark/envircat/envircat.c_v   1.2   27 Nov 1990 20:48:06   Mark_Gardner  $
  4. |
  5. | environment string concatenation
  6. | c1990 Mark Gardner, EEmergent Consulting, Acton, CA 805-269-1433
  7. |
  8. */
  9.  
  10. /*
  11. |
  12. | compiled and assembled with Turbo C++ and Turbo Assembler:
  13. |
  14. | ... tcc -ml envircat env_adrs.asm
  15. |
  16. */
  17.  
  18. /*
  19. |  inspired by 'LONGEST', a program downloaded from CompuServe,
  20. |  by McLean, but which did not work on my computer (Scott Robert
  21. |  Ladd says, in the article 'Locating The Master Environment'
  22. |  [MS-DOS System Programming, c1990 R&D Publications, Lawrence, KS],
  23. |  that later versions of DOS have an extra level of indirection;
  24. |  this may be what has fooled 'LONGEST').
  25. |
  26. |  The program will find trigger words in the environment that
  27. |  represent continuations of the previous defined string in the
  28. |  environment.  The trigger word must start with a + sign, have
  29. |  the same name as the previous string to be extended, and the
  30. |  name must be followed by any character(s) to distinguish the
  31. |  name from all previous names.
  32. |
  33. |  For example, if the PATH string was to be extended, the following
  34. |  sequence of commands should be executed:
  35. |
  36. |     PATH=C:\UTILS;C:\DOS;C:\;
  37. |    SET +PATH1=D:\;D:\TEST;D:\TC\BIN
  38. |    SET +PATH2=;D:\TD;D:\TP
  39. |
  40. |  (Note, as for 'LONGEST' operating on a path, the preceding line must
  41. |  anticipate the concatenation by terminating with a semicolon, or the
  42. |  continuation line must, by beginning with a semicolon.  The base rule
  43. |  is that ENVIRCAT will not add any characters, only link strings.
  44. |
  45. |  When executed, ENVIRCAT will remove the trigger tokens from the
  46. |  enviroment and leave the master token assigned to the concatenated
  47. |  string.  The remainder of the environment will be moved up to occupy
  48. |  the space that was vacated by the trigger tokens.
  49. |
  50. |  This removes what seemed to be a constraint on the operation of
  51. |  'LONGEST'.  This program need not be used as soon as the triggered
  52. |  string is put in the environment, and it can be used while there are
  53. |  multiple different trigger sets waiting for concatenation.
  54. |
  55. |  Note, in the spirit of my personal bias, I have named this routine
  56. |  with a long semi-mnemonic.  For those that don't like such things,
  57. |  the use of a batch file, EC.BAT, is recommended.
  58. |
  59. */
  60.  
  61.  
  62. /* structured program design
  63.  
  64. ; signon
  65.  
  66. ; find the environment (call to assembly routine)
  67.  
  68. ; if the first element in the environment is not 'COMSPEC'
  69. ;   abort with error level 1
  70. ; else
  71. ;   report found
  72. ; end_if
  73.  
  74. ; for each element
  75. ;   if the next element is a trigger word
  76. ;     count the occurrence
  77. ;     remove the trigger word
  78. ;     move the remainder of the environment down
  79. ;     reset to subject element
  80. ;   end_if
  81. ; end_for
  82.  
  83. ; report occurrences
  84.  
  85. */
  86.  
  87. #include <formdefs.h>
  88. #include <string.h>
  89.  
  90. #define TRUE 1
  91. #define FALSE 0
  92. #define NO_ERROR 0
  93. #define ERRLEVEL1 1
  94.  
  95. char far *findmenv (void) ;
  96. #define SEARCHLIM 2
  97.  
  98. char far *environment ;
  99. char far *position ;
  100. char far *mid_position ;
  101.  
  102. char element[40] ;
  103. char master_element[40] ;
  104.  
  105. main ()
  106. {
  107.  
  108.     char c ;
  109.     int i ;
  110.     int j ;
  111.     int success ;
  112.     int pluscount = { 0 } ;
  113.  
  114.     printf ( "environment string concatenation : ENVIRCAT v1.2\n" ) ;
  115.     printf ( "c1990 Mark Gardner (placed in public domain)\n\n" ) ;
  116.  
  117.     environment = findmenv() ;
  118.  
  119.     i = 0 ;
  120.  
  121.     while ( i < SEARCHLIM )
  122.         if ( check_comspec() )
  123.             break ;
  124.         else {
  125.             i++ ;
  126.             environment = environment + 16 ;
  127.         end_if }
  128.     end_while
  129.  
  130.     if ( i == SEARCHLIM ) {
  131.         printf ( "\nenvircat can't find environment\n" ) ;
  132.         return(ERRLEVEL1) ;
  133.     end_if }
  134.  
  135.     printf ( "envircat found environment at %p\n", environment ) ;
  136.  
  137.     position = environment ;
  138.  
  139.     while ( find_element ( position ) )
  140.         if (element[0] != '+') {
  141.             strcpy ( master_element, element ) ;
  142.             mid_position = position ;
  143.         }
  144.         else {
  145.             pluscount++ ;
  146.             success = TRUE ;
  147.             for ( i=0,j=1 ; i < strlen ( master_element ) ; i++,j++ )
  148.                 if ( master_element[i] != element[j] ) {
  149.                     success = FALSE ;
  150.                     break ;
  151.                 end_if }
  152.             end_for
  153.  
  154.             if ( success == TRUE ) {
  155.  
  156.                 position = mid_position-- ;
  157.  
  158.                 i = 0 ;
  159.                 while ( position[i++] != '=' )
  160.                     ;
  161.                 end_while
  162.  
  163.                 position = &position[i] ;
  164.                 i = j = 0 ;
  165.                 while ( j<2 ) {
  166.                     mid_position[i] = c = position[i] ;
  167.                     i++ ;
  168.                     if ( c == 0 )
  169.                         j = j + 1 ;
  170.                     else
  171.                         j = 0 ;
  172.                     end_if
  173.                 end_while }
  174.  
  175.                 position = environment ;
  176.  
  177.             end_if }
  178.         end_if }
  179.     end_while
  180.  
  181.     printf ( "envircat found %d continuations\n", pluscount ) ;
  182.  
  183.     return(NO_ERROR) ;
  184. }
  185.  
  186.  
  187.  
  188. int function find_element ( char far *p )
  189. {
  190.  
  191. /*
  192. |  stores element at current position in 'element', adjusts position to
  193. |  point to beginning of next element, returns 0 if can't find element
  194. |  at current position (end of environment).
  195. */
  196.  
  197.     int i = { 0 } ;
  198.     int j = { 0 } ;
  199.  
  200.     if ( p[0]==0 )
  201.         return(FALSE) ;
  202.     end_if
  203.  
  204.     while ( p[i] != '=' )
  205.         element[j++] = p[i++] ;
  206.     end_while
  207.  
  208.     element[j] = 0 ;
  209.  
  210.     while ( p[i++] != 0 )
  211.         ;
  212.     end_while
  213.  
  214.     position = &p[i] ;
  215.  
  216.     return(TRUE) ;
  217.  
  218. }
  219.  
  220. int function check_comspec ( void )
  221. {
  222.  
  223. /*
  224. |  verifies that the environment has been correctly located
  225. |  by checking that the contents are 'reasonable'.  The original
  226. |  check was that the first characters spell "COMSPEC", but this
  227. |  was not adequate, since some people redefine it, and it moves
  228. |  away from the environment top.  Hence, this routine searches
  229. |  the forward string and assumes the environment is correctly
  230. |  located if at least two equal signs precede the double nul.
  231. |  Any other constraint is not too reasonable, as it is possible
  232. |  to put any PC graphic character into the environment.
  233. |
  234. */
  235.  
  236. /*
  237. |  determine max search to closing double nul in environment...
  238. |   few indeed who have environments this big, I suppose and hope.
  239. */
  240.  
  241. #define MAX_EXPECTED 2048
  242.  
  243.     int nul_count, eq_count, i ;
  244.  
  245.     i =
  246.     eq_count =
  247.     nul_count = 0 ;
  248.  
  249.     while (
  250.     ___          ( nul_count < 2 )
  251.     ___       && ( i < MAX_EXPECTED )
  252.     ___   ) {
  253.  
  254.         switch ( environment[i++] ) {
  255.  
  256.             case 0 :
  257.                 nul_count++ ;
  258.                 break ;
  259.             end_case
  260.  
  261.             case '=' :
  262.                 eq_count++ ;
  263.             con_case
  264.  
  265.             default :
  266.                 nul_count = 0 ;
  267.             end_case
  268.  
  269.         end_switch }
  270.  
  271.     end_while }
  272.  
  273.     if  (
  274.     ___        ( eq_count > 1 )
  275.     ___    &&  ( nul_count == 2 )
  276.     ___    )
  277.         return ( TRUE ) ;
  278.     else
  279.         return ( FALSE ) ;
  280.     end_if
  281. }
  282.  
  283.