home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cidsam.zip / SAMPLE1.C < prev    next >
Text File  |  1993-06-28  |  11KB  |  156 lines

  1.                   /*********************************/
  2.                   /*              NOTE             */
  3.                   /*                               */
  4.                   /* This sample code has been     */
  5.                   /* provided by IBM.  It is not   */
  6.                   /* warranted for any particular  */
  7.                   /* use or purpose.               */
  8.                   /*                               */
  9.                   /* IBM releases this code into   */
  10.                   /* the public domain.  You may   */
  11.                   /* use it, modify it, or         */
  12.                   /* incorporate it into other     */
  13.                   /* products without restriction. */
  14.                   /*********************************/
  15. /* This sample program uses the high-level Response File APIs  to read a */
  16. /* response file and extract the keyword/value pairs.  It writes         */
  17. /* command (bat) file lines that will put the keywork/value pairs in the */
  18. /* environment.  You would redirect the output of this program to        */
  19. /* a file in real life.                                                  */
  20.  
  21. /* The Response File APIs described here operate at a fairly high        */
  22. /* level.  They let you concentrate on the logic of your program         */
  23. /* rather than the details of file processing and parsing.  They make    */
  24. /* certain assumptions, though.  The Response File APIs assume that      */
  25. /* the response files follow certain syntax conventions.                 */
  26. /*                                                                       */
  27. /* First, response files are assumed to consist of _keyword_ and         */
  28. /* _value_ pairs, connected by an equal (=) sign.  For example,          */
  29. /* a keyword/value pair might look like                                  */
  30. /*                                                                       */
  31. /*                  color = blue                                         */
  32. /*                                                                       */
  33. /* All lines in a response file are assumed to contain keyword/value     */
  34. /* pairs except                                                          */
  35. /*     -  Blank lines                                                    */
  36. /*     -  Lines with an asterisk ('*') as the first non-blank character  */
  37. /*     -  Lines with a semicolon (';') as the first non-blank character  */
  38. /* These lines are treated as comment lines and ignored.                 */
  39. /*                                                                       */
  40. /* The keyword is assumed to start with the first non-blank character    */
  41. /* of a line.  It may not contain any blanks or tabs, and it may not     */
  42. /* contain the equal sign (=).                                           */
  43. /*                                                                       */
  44. /* The value is assumed to start with the first non-blank character      */
  45. /* after the equal sign and to continue to the end of the line.          */
  46. /*                                                                       */
  47. /* There can be only one keyword/value pair per line.                    */
  48. /*                                                                       */
  49. /* Keywords can have either of two kinds of values: strings or lists.    */
  50. /* A string value is a 'simple' value consisting of a single string;     */
  51. /* the following are examples of keywords with string values:            */
  52. /*                                                                       */
  53. /*        machine = IBM-PS/2                                             */
  54. /*        Preamble = We the People of the United States...               */
  55. /*        comment = This is the way the world ends, this is the way      */
  56. /*        Num_sessions = 3                                               */
  57. /*                                                                       */
  58. /* A list value is a group of further keyword value pairs.  The start    */
  59. /* of a list is indicated by a left paren to the right of the equal      */
  60. /* sign.  The end of a list is indicated by a right paren on a line by   */
  61. /* itself.  The following are examples of keywords with list values:     */
  62. /*                                                                       */
  63. /*        list1 = (                                                      */
  64. /*                  name = george                                        */
  65. /*                  comment = simple list                                */
  66. /*                  color = puce                                         */
  67. /*                )                                                      */
  68. /*                                                                       */
  69. /*        list2 = (                                                      */
  70. /*                  name = lucy                                          */
  71. /*                  comment = more complicated list                      */
  72. /*                  sublist1 = (                                         */
  73. /*                               adapter = 0                             */
  74. /*                               protocol = tcpip                        */
  75. /*                             }                                         */
  76. /*                  sublist2 = (                                         */
  77. /*                               adapter = 1                             */
  78. /*                               protocol = netbios                      */
  79. /*                             )                                         */
  80. /*                )                                                      */
  81. /*                                                                       */
  82. /* Second, a 'special' keyword, include, is assumed.  The purpose of     */
  83. /* this keyword is to imbed other response files within the current one. */
  84. /* When the 'include' keyword is encountered, the Response File APIs     */
  85. /* open the included file and start processing its contents as if they   */
  86. /* were part of the original response file.  Response file may be        */
  87. /* nested to any depth.  Here's an example:                              */
  88. /*                                                                       */
  89. /*    Response file A.rsp contains                                       */
  90. /*            color = brown                                              */
  91. /*            speed = fast                                               */
  92. /*            include = B.rsp                                            */
  93. /*                                                                       */
  94. /*    Response file B.rsp contains                                       */
  95. /*             level = 1                                                 */
  96. /*             modification = 0                                          */
  97. /*             include = C.rsp                                           */
  98. /*                                                                       */
  99. /*    Response file C.rsp contains                                       */
  100. /*             buffers = 12                                              */
  101. /*                                                                       */
  102. /* The Response File APIs read these these response files and return     */
  103. /* the keywords and values as if it were all one big file that contained */
  104. /*                                                                       */
  105. /*            color = brown                                              */
  106. /*            speed = fast                                               */
  107. /*             level = 1                                                 */
  108. /*             modification = 0                                          */
  109. /*             buffers = 12                                              */
  110. /*                                                                       */
  111. /* The Response File APIs contains code to guard against 'loops' of      */
  112. /* included response files.                                              */
  113. /*                                                                       */
  114.  
  115. /* This sample uses the two most common Response File APIs, RFOpen()     */
  116. /* and RFGetNextKeyword().  RFOpen() takes the name of a response file   */
  117. /* and opens that file; subsequent 'reads' (using RFGetNextKeyword())    */
  118. /* are taken from that response file and from any response files         */
  119. /* _included_ in it.                                                     */
  120. /*                                                                       */
  121. /*                                                                       */
  122. /*************************************************************************
  123. **************************************************************************/
  124.  
  125. #include <stdio.h>           
  126. #include "hlrfio.h"      /* This header file contains the prototypes for */
  127.                          /* The Response File APIs, and defines for the  */
  128.                          /* codes returned by the Response File APIs     */
  129.  
  130. char *ResponseFileName = "test.rsp";  /* This is the name of the response*/
  131.                                       /* file we will open first.  It    */
  132.                                       /* might contain imbedded          */
  133.                                       /* (included) response files, but  */
  134.                                       /* that's OK.                      */
  135. int main(int argc, char **argv)
  136. {
  137.    int rc;                            /* return code                     */
  138.    char *keyword;                     /* Holds address of the keyword    */
  139.    char *value;                       /* Holds address of the value      */
  140.    unsigned type;                     /* Indicates the type of the value */
  141.                                       /* (string or list)                */
  142.  
  143.    rc = RFOpen(ResponseFileName);     /* Open the response file.  If the */
  144.                                       /* open was successful, RFH0 is    */
  145.                                       /* returned.  Otherwise, RFHERR.   */
  146.    if (rc == RFH0)                    /* If the open worked,             */
  147.      {                                /*  look at each keyword/value pair*/
  148.         while (RFGetNextKeyword(&keyword, &value, &type) == RFH0)
  149.           {                           /* Check if the value returned is a*/
  150.              if (type == RFHSTRING)   /* string.  Lists don't fit in the */
  151.                                       /* environment very well.          */
  152.                printf("@set %s=%s\n", keyword, value); /* write bat line */
  153.           }
  154.       }                               /* Note we don't have to close the */
  155. }                                     /* response file explicitely       */
  156.