init.c
上传用户:xiaoan1112
上传日期:2013-04-11
资源大小:19621k
文件大小:6k
源码类别:

操作系统开发

开发平台:

Visual C++

  1. /*  init.c - routines for managing TOOLS.INI-like files
  2.  *
  3.  *  Modifications
  4.  * 15-Jul-87   danl    Start of section is <optionalwhitespace>[...]
  5.  * 05-Aug-1988 mz     Use buffer equate for swgoto.
  6.  * 05-Jul-1989 bw     Use MAXPATHLEN
  7.  *
  8.  */
  9. #include <string.h>
  10. #include <limits.h>
  11. #include "..htools.h"
  12. #include "..hstrpfx.h"
  13. #define BUFLEN 256
  14. static flagType fMatchMark (char *, char *);
  15. static char *space = "t ";
  16. /*  fMatchMark - see if a tag in in a mark set
  17.  *
  18.  *  We treat the mark set as a collection of whitespace-separated names
  19.  *
  20.  *  pMark pointer to mark set (contents modified)
  21.  *  pTag tag to find
  22.  *
  23.  *  returns TRUE if match was found
  24.  */
  25. static flagType fMatchMark (pMark, pTag)
  26. char *pMark, *pTag;
  27. {
  28.     char *p, c;
  29.     while (*pMark != 0) {
  30. pMark = strbscan (p = strbskip (pMark, space), space);
  31. c = *pMark;
  32. *pMark = 0;
  33. if (!stricmp (p, pTag))
  34.     return TRUE;
  35. *pMark = c;
  36. }
  37.     return FALSE;
  38. }
  39. /* returns pointer to tag if line is marker; NULL otherwise */
  40. char *ismark (buf)
  41. register char *buf;
  42. {
  43.     register char *p;
  44.     buf = strbskip (buf, space);
  45.     if (*buf++ == '[')
  46. if (*(p = strchr (buf, ']')) != '') {
  47.             *p = 0;
  48.             return buf;
  49.             }
  50.     return NULL;
  51. }
  52. flagType swgoto (fh, tag)
  53. FILE *fh;
  54. char *tag;
  55. {
  56.     char buf[BUFLEN];
  57.     if (fh) {
  58. while (fgetl (buf, BUFLEN, fh)) {
  59.             register char *p;
  60.             if ((p = ismark (buf)) != NULL) {
  61. if (fMatchMark (p, tag))
  62.                     return TRUE;
  63.                 }
  64.             }
  65.         }
  66.     return FALSE;
  67. }
  68. /* returns fh of file if tag found, else NULL */
  69. FILE *swopen (file, tag)
  70. char *file, *tag;
  71. {
  72.     FILE *fh;
  73.     char buf[MAXPATHLEN];
  74.     if ((fh = pathopen (file, buf, "rb")) == NULL)
  75.         return NULL;
  76.     if (swgoto (fh, tag))
  77. return fh;
  78.     fclose (fh);
  79.     return NULL;
  80. }
  81. /* close a switch file */
  82. swclose (fh)
  83. FILE *fh;
  84. {
  85.     return fclose (fh);
  86. }
  87. /* read a switch line; return FALSE if end of file.  Skips leading spaces
  88.  * and lines that begin with ; and blank lines
  89.  */
  90. int swread (buf, len, fh)
  91. char *buf;
  92. int len;
  93. FILE *fh;
  94. {
  95.     register char *p;
  96.     while (fgetl (buf, len, fh))
  97.         if (ismark (buf) != NULL)
  98.     break;
  99.         else {
  100.     p = strbskip (buf, space);
  101.             if (*p != 0 && *p != ';') {
  102.                 strcpy (buf, p);
  103. return TRUE;
  104.             }
  105.         }
  106.     return FALSE;
  107. }
  108. /* Reads lines from the file fh looking in the section pstrTag for one with
  109.  * "entry=" and if there are non-white space characters following the '='
  110.  * a copy of these characters is returned else NULL is returned.
  111.  *
  112.  * If fh == 0 then the file $INIT:TOOLS.INI is used as the switch file
  113.  *
  114.  * If a non-NULL value is returned, it should eventually be free'd.
  115.  *
  116.  * N.B. if there are only white space characters, space and tab, following
  117.  * the '=', NULL is returned
  118.  *
  119.  */
  120. char *swfind (pstrEntry, fh, pstrTag)
  121. char *pstrEntry;
  122. FILE *fh;
  123. char *pstrTag;
  124. {
  125.     char *p;
  126.     char *q;
  127.     FILE *fhIn = fh;
  128.     char buf[BUFLEN];
  129.     q = NULL;
  130.     if (fh || (fh = swopen ("$INIT:\TOOLS.INI", pstrTag))) {
  131. while (swread (buf, BUFLEN, fh) && !ismark(buf) ) {
  132.     if ((p = strchr (buf, '=')) != NULL) {
  133.                 *p++ = '';
  134. if (!strcmpis (buf, pstrEntry)) {
  135.     if (*(p = strbskip (p, space)))
  136.                         q = strdup (p);
  137.                     break;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.     if (!fhIn)
  143.         swclose (fh);
  144.     return q;
  145. }
  146. /* Returns a ptr to the separator in buf & fills in sep */
  147. static char *FindSep(char *buf, char **seps, char **sep)
  148. {
  149.     char *szBrk;
  150.     unsigned i, index, len = 0;
  151.     index = UINT_MAX;
  152.     *sep = NULL;
  153.     // find longest leftmost separator from given list in buf
  154.     while (NULL != *seps)
  155.     {
  156. szBrk = strstr(buf, *seps);
  157. if (szBrk )
  158. {
  159.    i = szBrk - buf;
  160.    if (i < index || (i == index && len < strlen(*seps)))
  161.    {
  162.        index = i;
  163.        *sep = *seps;
  164.        len = strlen(*sep);
  165.    }
  166. }
  167. seps++;
  168.     }
  169.     if (*sep)
  170. return (buf + index);
  171.     else
  172. return NULL;
  173. }
  174. /*
  175.  * Parse the given buffer. Return the lhs, rhs and the actual separator.
  176.  *
  177.  * buf switch line contents (!!!contents modified!!!)
  178.  * seps null-terminated list of allowed separators
  179.  * lhs  ptr to parameter (left-hand side value)
  180.  * rhs  ptr to value (value to the right of the separator)
  181.  * sep actual separator found in buf
  182.  *
  183.  * Returns TRUE if successful else FALSE.
  184.  */
  185. int swparse(char *buf, char *seps[], char **lhs, char **rhs, char **sep)
  186. {
  187.     char *szBrk = NULL;
  188.     *lhs = *rhs = *sep = NULL;
  189.     if (NULL == buf || '' == *buf || (NULL != ismark(buf)))
  190. return FALSE;
  191.     buf += strspn(buf, space);
  192.     if (';' == *buf)
  193. return FALSE;
  194.     // sep
  195.     szBrk = FindSep(buf, seps, sep);
  196.     // lhs
  197.     *lhs = buf;
  198.     // rhs
  199.     if (szBrk)
  200.     {
  201. // place 0 at sep 
  202. *szBrk = '';
  203. szBrk += strspn(szBrk += strlen(*sep), space);
  204. *rhs = szBrk;
  205.     }
  206.     return TRUE;
  207. }
  208. static flagType fMatchPat (char *pMark, char *pTag)
  209. {
  210.     char *p, c;
  211.     while (*pMark != 0) {
  212. pMark = strbscan (p = strbskip (pMark, space), space);
  213. c = *pMark;
  214. *pMark = 0;
  215. if (striprefix (pTag, p))
  216.     return TRUE;
  217. *pMark = c;
  218. }
  219.     return FALSE;
  220. }
  221. /*
  222.  * Variation of goto where the tag is of the form <string>*
  223.  *
  224.  * fh  file handle
  225.  * tag tag to match
  226.  * 
  227.  * Retruns ptr to match found if successful else NULL
  228.  */
  229. char *swmatch(FILE *fh, char *tag)
  230. {
  231.     char buf[BUFLEN];
  232.     if (fh) {
  233. while (fgetl (buf, BUFLEN, fh)) {
  234.             register char *p;
  235.             if ((p = ismark (buf)) != NULL) {
  236. if (fMatchPat (p, tag))
  237.                     return (strdup(p));
  238.                 }
  239.             }
  240.         }
  241.     return NULL;
  242. }