home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / GLOBEN.ZIP / GLOB2SET.C next >
Text File  |  1987-10-12  |  8KB  |  161 lines

  1. /*=========================================================================*/
  2. /*                                                                         */
  3. /*              GLOB2SET : Get Global Names as Shell SET Commands          */
  4. /*                                                                         */
  5. /*                      glob2set [xyz]*                                    */
  6. /*                                                                         */
  7. /* The Global Environment for OS/2 is a store of named values that is      */
  8. /* maintained by dynalink code defined in GLOBENV.ASM/.OBJ/.DLL/.LIB.      */
  9. /* There's no communication between the Global Environment and the command */
  10. /* environment maintained by the command shell.  Moreover, there's no way  */
  11. /* by which a program that is called as a command may make permanent       */
  12. /* insertions in the environment it inherits.  Therefore we cannot write   */
  13. /* a program to copy strings from the global environment to the local one. */
  14. /*                                                                         */
  15. /* What this program does is to copy a selected set of global variables    */
  16. /* to stdout in the format of a series of SET commands.  If the output     */
  17. /* is captured in a command file and executed, the transfer from global    */
  18. /* to local environments will have been made, e.g. do:                     */
  19. /*                                                                         */
  20. /*      glob2set * >setall.cmd && setall                                   */
  21. /*                                                                         */
  22. /* The first parameter is a prefix string which may end in an asterisk     */
  23. /* (similar to the GLOBAL command, q.v.).  Only variables whose names      */
  24. /* start with that prefix will be copied.  An asterisk alone copies all    */
  25. /* the variables.                                                          */
  26. /*                                                                         */
  27. /* Two problems remain: the names often copied into the local environment  */
  28. /* have heterogenous names that don't lend themselves to being copied in a */
  29. /* group (e.g. LIB, PATH).  And the Global environment will likely be used */
  30. /* for other items that might have conflicting names.  We solve these at a */
  31. /* stroke with the following KLUDGE RULE:                                 */
  32. /*   If the search prefix string ends in underscore, the entire prefix     */
  33. /* will be stripped from the output names.  Thus the names ZZ_path, ZZ_lib */
  34. /* and ZZ_dpath may be copied out as path, lib and dpath with one command, */
  35. /*      glob2set ZZ_* >set3.cmd                                            */
  36. /*                                                                         */
  37. /* Revision History:                                                       */
  38. /*   10/08/87 -- first version completed                                   */
  39. /*   10/12/87 -- commentary, explain()                                     */
  40. /*                                                                         */
  41. /* Copyright (C) 1987 David E. Cortesi                                     */
  42. /*=========================================================================*/
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. /*=================================================================*/
  47. /* The following functions are defined in the GlobEnv dynalink.    */
  48. /*=================================================================*/
  49. extern int far pascal GLBQUERY(char far * thename
  50.                               ,int far * thesize);
  51. extern int far pascal GLBFETCH( char far * thename
  52.                               , void far * valbuff
  53.                               , int far * thesize);
  54. extern int far pascal GLBNEXT ( char far * thename
  55.                               , int strmax
  56.                               , char far * valbuff
  57.                               , int far * thesize);
  58.  
  59. /*=================================================================*/
  60. /* Explain function called on any parameter error.  Exits.         */
  61. /*=================================================================*/
  62. static void explain()
  63. {
  64.         printf(
  65. "\t\tglob2set  [pfx]*\n"
  66.         ); printf(
  67. "Copies Global Environment variables to stdout as SET commands.\n"
  68.         ); printf(
  69. "To copy variables from Global to command-line environment, redirect\n"
  70.         ); printf(
  71. "output to a .cmd file and then execute, e.g.\n"
  72.         ); printf(
  73. "\tglob2set loc_* >tempset.cmd && tempset\n"
  74.         ); printf(
  75. "Names beginning with prefix 'pfx' are copied; use * alone to copy all.\n"
  76.         ); printf(
  77. "Kludge rule: if 'pfx' ends in an underscore as in example, it is\n"
  78.         ); printf(
  79. " stripped from each set-name, allowing subsetting of global vars.\n"
  80.         );
  81. }
  82.  
  83. /*=================================================================*/
  84. /* The maximum command line is 127 bytes (plus a nul).  Of that we */
  85. /* need 5 for SET, space, and the equals sign, leaving 122.  Since */
  86. /* a name has to be at least 1 byte and a value ditto, the max     */
  87. /* size for either is 121, to leave room for the other.            */
  88. /*=================================================================*/
  89. #define MAXSTR 121+1
  90. static char xname[MAXSTR];
  91. #define MAXVAL 121+1
  92. static char xvalue[MAXVAL];
  93. static int valsize;     /* value size word parameter space */
  94. static char outline[128];
  95.  
  96. /*=================================================================*/
  97. /* getlist(int strip) gets the value of xname and writes name and  */
  98. /* value as a set command to stdout.  The parameter strip is the   */
  99. /* number of bytes to strip from the name under the Kludge Rule.   */
  100. /*=================================================================*/
  101. static void getlist(int strip)
  102. {
  103.         if (valsize > MAXVAL)
  104.         {
  105.                 fprintf(stderr,"value of %s is %d bytes, only %d copied\n"
  106.                                      ,xname ,valsize      ,MAXVAL);
  107.                 valsize = MAXVAL;
  108.         }
  109.         GLBFETCH(xname,xvalue,&valsize);
  110.         /* ensure a string delimiter on a value of max length */
  111.         if (valsize == MAXVAL)
  112.                 xvalue[MAXVAL-1] = '\0';
  113.         /* convert a null value to a single space, otherwise the */
  114.         /* shell will take "name=" as a delete request. */
  115.         if (valsize == 0)
  116.                 {xvalue[0] = ' '; xvalue[1] = '\0'; }
  117.         printf("SET %s=%s\n",xname+strip,xvalue);
  118. }
  119.  
  120. /*=================================================================*/
  121. /* listall(char *pfx) does the search for matching names.          */
  122. /*=================================================================*/
  123. static void listall(char *pfx)
  124. {
  125. int len, strip;
  126. char x, yname[MAXSTR];
  127.  
  128.         strcpy(xname,pfx);
  129.         len = strlen(xname);
  130.         if (xname[len-1]=='*')  /* if a trailing star, zot it */
  131.                 xname[--len] = '\0';
  132.         strcpy(yname,xname);  /* save prefix for compares */
  133.         strip = (yname[len-1]=='_')?len:0;
  134.         /*=========================================================*/
  135.         /* GLBNEXT only returns the "next," so if there's a match  */
  136.         /* to the given name we have to get it first.  For an      */
  137.         /* exact match it's obviously wrong to strip a prefix.     */
  138.         /*=========================================================*/
  139.         if (0==GLBQUERY(xname,&valsize))
  140.                 getlist(0);
  141.         /*=========================================================*/
  142.         /* Now we can spin through all the "nexts" there may be.   */
  143.         /*=========================================================*/
  144.         while (0==GLBNEXT(xname,MAXSTR,xname,&valsize))
  145.         {
  146.                 x = xname[len];         /* temporarily truncate to */
  147.                 xname[len] = '\0';      /* length of prefix */
  148.                 if (0 != strcmp(xname,yname)) break;
  149.                 xname[len] = x;         /* still equals prefix, so */
  150.                 getlist(strip);         /* output it.       */
  151.         }
  152. }
  153.  
  154. int  main(argc,argv)
  155. int  argc;
  156. char *argv[];
  157. {
  158.         if ((argc < 2)||(argc > 3)) explain();
  159.         else listall(argv[1]);
  160. }
  161.