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

  1. /*=========================================================================*/
  2. /*                                                                         */
  3. /*              GLOBLOAD : Load Global Environment from StdIn              */
  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 loads variables from a named file.  Its primary use is to  */
  8. /* bulk-load a set of variables during system initialization.              */
  9. /*                                                                         */
  10. /* See also GLOBAL (which can be used to create an input file for this     */
  11. /* program) and GLOB2SET.                                                  */
  12. /*                                                                         */
  13. /*              GLOBLOAD [-z] [-t] [input-file-spec]                       */
  14. /*                                                                         */
  15. /* The lines of the file must be in form output by a "global *" cmd:       */
  16. /*                      name = value                                       */
  17. /* The variable "name" is created (if necessary) in the global environment */
  18. /* and then assigned the string "value," which runs to the end of the line.*/
  19. /*                                                                         */
  20. /* The -z switch tells the program, after seeing eof, to call DOSSLEEP to  */
  21. /* sleep indefinitely.  If called from the command line, the sleeping      */
  22. /* program could be cancelled with ^Break.  However, the -z option would   */
  23. /* ordinarily be used from the config.sys file, like this:                 */
  24. /*              run=\globload.exe -z file-of-initial-vars                  */
  25. /* Having the program asleep, detached, forever is the means of keeping    */
  26. /* the Global Environment DLL code in storage; otherwise it goes away and  */
  27. /* and the loaded definitions are lost.                                    */
  28. /*                                                                         */
  29. /* Incidentally, the "run=" use is the reason the program takes a named    */
  30. /* file instead of using stdin: there's no support for <redirection for    */
  31. /* programs being run from the config.sys file.                            */
  32. /*                                                                         */
  33. /* The -t switch asks the program to generate and define the value         */
  34. /*              time_of_loading = dd-mm-yy,hh:mm:ss                        */
  35. /* The -t switch may be used in addition to or instead of a filespec.      */
  36. /*                                                                         */
  37. /* Revision History:                                                       */
  38. /*   10/08/87 -- first version completed                                   */
  39. /*   10/12/87 -- commentary, explain(), -t option                          */
  40. /*                                                                         */
  41. /* Copyright (C) 1987 David E. Cortesi                                     */
  42. /*=========================================================================*/
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #define ZIP '\0'
  47. #define EOL '\n'
  48.  
  49. /*=================================================================*/
  50. /* The following functions are defined by OS/2.                    */
  51. /*=================================================================*/
  52. extern void far pascal DOSSLEEP(long time);
  53. struct DosDateTime {
  54.         unsigned char hour, minute, second, hundredth, day, month;
  55.         unsigned int year, timezone;
  56.         unsigned char dayofweek;
  57.                 };
  58. extern void far pascal DOSGETDATETIME(struct DosDateTime far *);
  59.  
  60. /*=================================================================*/
  61. /* The following functions are defined in the GlobEnv dynalink.    */
  62. /*=================================================================*/
  63. extern int far pascal GLBENTER(char far * thename);
  64. extern int far pascal GLBSTORE(char far * thename
  65.                               ,void far * thevalue
  66.                               ,int thelen);
  67.  
  68. /*=================================================================*/
  69. /* Explain program use, called on any parameter error.  Exits.     */
  70. /*=================================================================*/
  71. static void explain()
  72. {
  73.         printf(
  74. "\t\tGLOBLOAD  [-z] [-t] filespec\n"
  75.         ); printf(
  76. "Loads the global environment from an ascii file 'filespec.'\n"
  77.         ); printf(
  78. "File lines are in the format output by the GLOBAL command:\n"
  79.         ); printf(
  80. "\tname = [value-string]\n"
  81.         ); printf(
  82. "The -t option make the program insert insert the time and date as:\n"
  83.         ); printf(
  84. "\ttime_of_load = dd-mm-yy,hh:mm:ss\n"
  85.         ); printf(
  86. "using the system time and date.\n"
  87.         ); printf(
  88. "The -z option makes the program sleep forever, which keeps the\n"
  89.         ); printf(
  90. "global-environment DLL code in storage.  Use in the config.sys file:\n"
  91.         ); printf(
  92. "\trun=\\globload.exe -z file-of-initial-vars\n"
  93.         );
  94.         exit(1);
  95. }
  96.  
  97. /*=================================================================*/
  98. /* The following statics are used in calling the GlobEnv functions */
  99. /*=================================================================*/
  100. #define MAXSTR 120 /* names could get this long on cmd line */
  101. static char xname[MAXSTR];
  102. #define MAXVAL 512      /* arbitrary limit on value size */
  103. static char xvalue[MAXVAL];
  104. static int xsize;
  105. #define MAXLINE MAXSTR+MAXVAL+3
  106. static char inline[MAXLINE];
  107.  
  108. /*=================================================================*/
  109. /* Build the name time_of_load in xname and the value of the date  */
  110. /* and time in xvalue.                                             */
  111. /*=================================================================*/
  112. static void makevar()
  113. {
  114. struct DosDateTime dt;
  115.         strcpy(xname, "time_of_load");
  116.         DOSGETDATETIME(&dt);
  117.         sprintf(xvalue,"%02d-%02d-%02d,%02d:%02d:%02d"
  118.                 ,dt.day ,dt.month ,dt.year-1900
  119.                 ,dt.hour ,dt.minute ,dt.second);
  120.         xsize = /* dd-mm-yy,hh:mm:ss\0 */ 18;
  121. }
  122.  
  123. /*=================================================================*/
  124. /* Process one line from inline into xname, xvalue.  Allow some    */
  125. /* freedom of syntax: spaces before the name and around the = sign */
  126. /* are ok but not required.  Extra stuff between name and = is     */
  127. /* ignored.  Missing value is ok, it becomes null.  Returns zero   */
  128. /* when there is no name (all blank line); otherwise returns 1.    */
  129. /*=================================================================*/
  130. static int parseline()
  131. {
  132. register char *j, *k;
  133.         k = xname;
  134.         for (j=inline; *j == ' '; ++j);
  135.         for (; (*j != EOL)&&(*j != ' ')&&(*j != '='); ) *k++ = *j++;
  136.         *k = ZIP;
  137.         if (k==xname) return(0);
  138.         for (; *j != '=';++j) if (*j==EOL) break;
  139.         k = xvalue;
  140.         if (*j != EOL)
  141.         {
  142.                 ++j; /* over the equals sign */
  143.                 for(; *j == ' '; ++j) if (*j==EOL) break;
  144.                 for(; *j != EOL ;) *k++ = *j++;
  145.         }
  146.         *k = ZIP;
  147.         xsize = (k==xvalue)?0:1+k-xvalue;
  148.         return(1);
  149. }
  150.  
  151. /*=================================================================*/
  152. /* Assign the value xvalue to the name xname.                      */
  153. /*=================================================================*/
  154. static void assign()
  155. {
  156. int rc;
  157.         rc = GLBENTER(xname);
  158.         if ((rc==0)||(rc==2))
  159.                 GLBSTORE(xname,xvalue,xsize);
  160. }
  161.  
  162. main(argc,argv)
  163. int argc; char *argv[];
  164. {
  165. FILE *inp;
  166. char *parm;
  167. int zparm, nparm;
  168.  
  169.         /* at least one parameter is required */
  170.         if (argc < 2) explain();
  171.  
  172.         for (zparm = 0, nparm = 1; nparm < argc; ++nparm)
  173.         {
  174.                 if (0 == strcmpi("-z",argv[nparm]))
  175.                         zparm = 1;
  176.                 else if (0 == strcmpi("-t",argv[nparm]))
  177.                 {       makevar(); assign(); }
  178.                 else /* presume it's a file */
  179.                 {
  180.                         inp = fopen(argv[nparm],"r");
  181.                         if (NULL != inp)
  182.                                 while (NULL != fgets(inline,MAXLINE-1,inp))
  183.                                 {
  184.                                         inline[MAXLINE-1] = EOL;
  185.                                         if (parseline())
  186.                                                 assign();
  187.                                 }
  188.                         else
  189.                         {
  190.                                 fprintf(stderr, "Unable to open %s\n",
  191.                                         argv[nparm]);
  192.                                 exit(4);
  193.                         }
  194.                 } /* end else file */
  195.         } /* end for */
  196.         if (zparm)
  197.                 DOSSLEEP(-1L);
  198. }
  199.