home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / GLOBEN.ZIP / GLOBAL.C < prev    next >
Text File  |  1987-10-13  |  10KB  |  209 lines

  1. /*=========================================================================*/
  2. /*                                                                         */
  3. /*              GLOBAL  :  Command access to Global Environment            */
  4. /*                                                                         */
  5. /* The Global Environment for OS/2 is a store of named values that is      */
  6. /* maintained by dynalink code defined in GLOBENV.ASM/.OBJ/.DLL/.LIB.      */
  7. /* This command makes the contents of the Global Environment accessible    */
  8. /* from the OS/2 protect-mode command line.  See also GLOBLOAD, GLOB2SET.  */
  9. /*                                                                         */
  10. /* Here are the forms and uses of the GLOBAL command:                      */
  11. /*                                                                         */
  12. /*              GLOBAL *                                                   */
  13. /*              GLOBAL xyz*                                                */
  14. /*              GLOBAL name                                                */
  15. /*                                                                         */
  16. /* The first form lists all names and their values.                        */
  17. /* The second lists names and values of variables whose names start "xyz." */
  18. /* The third lists the name and value of variable "name."                  */
  19. /* All three list to stdout, one line per name, in the form:               */
  20. /*              namestring = valuestring                                   */
  21. /* There is no guarantee, of course, that valuestring is printable, but    */
  22. /* it usually will be.  Use redirection to save values in a file, e.g. for */
  23. /* reloading with GLOBLOAD.                                                */
  24. /*                                                                         */
  25. /*              GLOBAL name =                                              */
  26. /*              GLOBAL name = value                                        */
  27. /*                                                                         */
  28. /* These forms define "name" if it isn't defined.  The first assigns a     */
  29. /* null value to name and the second an explicit value.  Use quotes to     */
  30. /* protect spaces and special characters in the value token.               */
  31. /*                                                                         */
  32. /*              GLOBAL name #                                              */
  33. /*                                                                         */
  34. /* This form deletes the variable "name" from the global environment.      */
  35. /*                                                                         */
  36. /* NOTE: if there exists a name longer than MAXSTR, this command can't     */
  37. /* list it, furthermore no name following that name will appear in the     */
  38. /* list produced by "global *"                                             */
  39. /*                                                                         */
  40. /* Revision History:                                                       */
  41. /*   10/06/87 -- first version completed                                   */
  42. /*   10/08/87 small bugs fixed, added # for delete                         */
  43. /*   10/13/87 restriction noted                                            */
  44. /*                                                                         */
  45. /* Copyright (C) 1987 David E. Cortesi                                     */
  46. /*=========================================================================*/
  47. #include <ctype.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. /*=================================================================*/
  51. /* The following functions are defined in the GlobEnv dynalink.    */
  52. /*=================================================================*/
  53. #include "globenv.h"
  54.  
  55. /*=================================================================*/
  56. /* The following statics are used in calling the above functions   */
  57. /*=================================================================*/
  58. #define MAXSTR 128 /* names could get this long on cmd line */
  59. static char xname[MAXSTR];
  60. #define MAXVAL 512      /* arbitrary limit on value size */
  61. static char xvalue[MAXVAL];
  62. static int valsize;     /* value size word parameter space */
  63.  
  64. /*=================================================================*/
  65. /* Explain() function taking usage msg out of line.                */
  66. /*=================================================================*/
  67.  
  68. static void explain()
  69. {
  70.         printf(
  71. "GLOBAL: command line access to the global environment space.\n"
  72.                 ); printf(
  73. "  GLOBAL *                     -- list all names & values   \n"
  74.                 ); printf(
  75. "  GLOBAL xyz*                  -- list names beginning 'xyz'\n"
  76.                 ); printf(
  77. "(redirect output of above commands to make input for GLOBLOAD)\n"
  78.                 ); printf(
  79. "  GLOBAL name                  -- list name, value of 'name'\n"
  80.                 ); printf(
  81. "  GLOBAL name =                -- assign null value to 'name'\n"
  82.                 ); printf(
  83. "  GLOBAL name = 'value string' -- assign value to 'name'\n"
  84.                 ); printf(
  85. "  GLOBAL name #                -- delete the variable 'name'\n"
  86.                 );
  87.         exit(0);
  88. }
  89.  
  90. /*=================================================================*/
  91. /* getlist() gets the value of xname and lists it to stdout.  The  */
  92. /* name is known to exist and valsize is its value size.           */
  93. /*=================================================================*/
  94. static void getlist()
  95. {
  96.         if (valsize > MAXVAL)
  97.         {
  98.                 fprintf(stderr,"value of %s, %d, truncated to %d\n"
  99.                                      ,xname ,valsize, MAXVAL);
  100.                 valsize = MAXVAL;
  101.         }
  102.         xvalue[0] = '\0';
  103.         GLBFETCH(xname,xvalue,&valsize);
  104.         if (valsize == MAXVAL)
  105.                 xvalue[MAXVAL-1] = '\0'; /* ensure string delim */
  106.         printf("%s = %s\n",xname,xvalue);
  107. }
  108.  
  109. /*=================================================================*/
  110. /* listall(char *) lists all names matching a prefix               */
  111. /*=================================================================*/
  112. static void listall(char *name)
  113. {
  114. int len;
  115. char x, yname[MAXSTR];
  116.         strcpy(xname,name);
  117.         xname[strlen(xname)-1] = '\0'; /* zot the trailing '*' */
  118.         strcpy(yname,xname);    /* save original prefix for cmpr */
  119.         len = strlen(yname);
  120.         /*=========================================================*/
  121.         /* GLBNEXT only returns the "next," so if there's a match  */
  122.         /* to the given name we have to get it first.              */
  123.         /*=========================================================*/
  124.         if (0==GLBQUERY(xname,&valsize))
  125.                 getlist();
  126.         /*=========================================================*/
  127.         /* Now we can spin through all "nexts" there are.  NOTE:   */
  128.         /* stops at the first nonzero return code, but that could  */
  129.         /* be 3 for next-name-longer-than-MAXSTR.  In order to get */
  130.         /* past that name we'd have to retrieve it with this call, */
  131.         /* and we can't.                                           */
  132.         /*=========================================================*/
  133.         while (0==GLBNEXT(xname,MAXSTR,xname,&valsize))
  134.         {
  135.                 x = xname[len];         /* temporarily truncate to */
  136.                 xname[len] = '\0';      /* length of prefix */
  137.                 if (0 != strcmp(xname,yname)) break;
  138.                 xname[len] = x;         /* still equals prefix */
  139.                 getlist();              /* restore and list */
  140.         }
  141. }
  142.  
  143. /*=================================================================*/
  144. /* listone(char *) lists one name                                  */
  145. /*=================================================================*/
  146. static void listone(char *name)
  147. {
  148.         strcpy(xname,name);
  149.         if (0==GLBQUERY(xname,&valsize))
  150.                 getlist();
  151.         else printf("name %s not found\n",xname);
  152. }
  153.  
  154. /*=================================================================*/
  155. /* assign(char *, char *) assigns a value to a name, defining it   */
  156. /* if it isn't already defined.                                    */
  157. /*=================================================================*/
  158. static void assign(char *name, char *value)
  159. {
  160. int rc;
  161.         strcpy(xname, name);
  162.         GLBENTER(xname); /* ignore return code */
  163.         rc = GLBSTORE(xname,value, 1+strlen(value));
  164.         if (rc)
  165.                 fprintf(stderr
  166.                 ,"Unable to store value, error code %d",rc);
  167. }
  168.  
  169. /*=================================================================*/
  170. /* delete(char *) deletes a name from the global environment.      */
  171. /*=================================================================*/
  172. static void delete(char *name)
  173. {
  174. int rc;
  175.         if (rc = GLBDELETE(name))
  176.                 fprintf(stderr
  177.                 ,"Unable to delete %s, error code %d",name,rc);
  178. }
  179.  
  180. /*=================================================================*/
  181. /* main() proc: check parameters, call one of above functions      */
  182. /*=================================================================*/
  183. int  main(argc,argv)
  184. int  argc;
  185. char *argv[];
  186. {
  187.         if ((argc == 1)||(argc > 4)) explain();
  188.         if (argc == 2)
  189.         { /* forms with only one parameter */
  190.                 if (0==strcmp("?",argv[1])) explain();
  191.                 if ('*'==argv[1][strlen(argv[1])-1])
  192.                         listall(argv[1]);
  193.                 else
  194.                         listone(argv[1]);
  195.         }
  196.         else
  197.         { /* forms with 2, 3 parameters */
  198.                 if (argv[2][1]!='\0') explain();
  199.                 else switch (argv[2][0])
  200.                 {
  201.                 case '=' : assign(argv[1], argv[3]);
  202.                                 break;
  203.                 case '#' : delete(argv[1]);
  204.                                 break;
  205.                 default: explain();
  206.                 }
  207.         }
  208. }
  209.