home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mytinfo / part01 / buildpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-26  |  4.0 KB  |  202 lines

  1. /*
  2.  * buildpath.c
  3.  * 
  4.  * By Ross Ridge
  5.  * Public Domain
  6.  * 92/02/01 07:29:42
  7.  *
  8.  * _buildpath builds a list of file names and terminal descriprions extracted
  9.  * from its arguments. It returns a pointer to a structure that is used by
  10.  * other routines as the list of file names to search for terminal
  11.  * descriptions.  It is passed a variable number of arguments consisting
  12.  * of file name and type pairs. The file name can actually be a list of 
  13.  * file names seperated by spaces and any environment variables specified
  14.  * by a dollar sign ($) followed by its name are substituted in. A type
  15.  * of 1 indicates that the file name may actually be termcap description
  16.  * and a type of 2 indicates it may be a terminfo description. A type of 0
  17.  * indicates that the file name can only be a file name (or list of them).
  18.  *
  19.  */
  20.  
  21. #include "defs.h"
  22.  
  23. #include <ctype.h>
  24.  
  25. #include "strtok.c"
  26.  
  27. #ifdef USE_SCCS_IDS
  28. static const char SCCSid[] = "@(#) mytinfo buildpath.c 3.2 92/02/01 public domain, By Ross Ridge";
  29. #endif
  30.  
  31. /* more memory is allocated for file names every HUNK file names */
  32. #define HUNK 32    
  33.  
  34. /* characters that seperate file names in a list */
  35. #define SEPERATORS " :"
  36.  
  37. static struct term_path *path = NULL;    /* the list of files */
  38. static int files = 0;            /* # of files in the list */
  39. static int size = 0;            /* # of files there is space for */
  40.  
  41. /* add a file name, type pair to the list */
  42. static int
  43. addfile(file, type)
  44. char *file;
  45. int type; {
  46.     int l;
  47.     char *s;
  48.  
  49.     if (file == NULL) {
  50.         if (type != -1)
  51.             return -1;
  52.     } else if (file[0] == '\0')
  53.         return -1;
  54.  
  55. #ifdef DEBUG
  56.     if (file != NULL)
  57.         printf("addfile: %s\n", file);
  58. #endif
  59.  
  60.     if (files >= size) {
  61.         size += HUNK;
  62.         if (path == NULL)
  63.             path = (struct term_path *) 
  64.                 malloc(size * sizeof(struct term_path));
  65.         else
  66.             path = (struct term_path *)
  67.                 realloc((anyptr) path,
  68.                     size * sizeof(struct term_path));
  69.         if (path == NULL)
  70.             return 0;
  71.     }
  72.     if (file == NULL) {
  73.         path[files].file = file;
  74.     } else {
  75.         l = strlen(file) + 1;
  76.         s = (char *) malloc(l * sizeof(char));
  77.         if (s == NULL)
  78.             return 0;
  79.         path[files].file = strcpy(s, file);
  80.     }
  81.     path[files].type = type;
  82.     
  83.     return ++files;
  84. }
  85.  
  86. /* deallocate space used by the path list */
  87. void
  88. _delpath(ppath)
  89. struct term_path *ppath; {
  90.     struct term_path *p;
  91.  
  92.     p = ppath;
  93.     while(p->file != NULL) {
  94.         free((anyptr)p->file);
  95.         p++;
  96.     }
  97.  
  98.     free((anyptr)ppath);
  99. }
  100.  
  101. /* build a list of paths. see above */
  102. #ifdef lint
  103. /*VARARGS2*/
  104. struct term_path *
  105. _buildpath(file, type)
  106. char *file;
  107. int type;
  108. #else
  109. #ifdef USE_STDARG
  110. #ifdef USE_PROTOTYPES
  111. struct term_path *_buildpath(char *file, int type, ...)
  112. #else
  113. struct term_path *_buildpath(file, type)
  114. char *file;
  115. int type;
  116. #endif /* USE_PROTOTYPES */
  117. #else /* USE_STDARG */
  118. struct term_path *_buildpath(va_alist)
  119. va_dcl
  120. #endif /* USE_STDARG */
  121. #endif /* lint */
  122. {
  123. #ifndef lint
  124. #ifndef USE_STDARG
  125.     char *file;
  126.     int type;
  127. #endif
  128. #endif
  129.     va_list ap;
  130.     register char *s, *d, *e;
  131.     char line[MAX_BUF+1];
  132.     char name[MAX_NAME+1];
  133.     int i,j;
  134.  
  135.     size = 0;
  136.     files = 0;
  137.     path = NULL;
  138.  
  139. #ifdef lint
  140.     ap = NULL;
  141. #else
  142. #ifdef USE_STDARG
  143.     va_start(ap, type);
  144. #else
  145.     va_start(ap);
  146.     file = va_arg(ap, char *);
  147.     type = va_arg(ap, int);
  148. #endif
  149. #endif
  150.  
  151.     while (type >= 0 && type <= 2) {
  152.         s = file;
  153.         d = line;
  154.         i = 0;
  155.         while(*s != '\0') {
  156.             if (*s == '$') {
  157.                 s++;
  158.                 j = 0;
  159.                 while(*s != '\0' && (*s == '_' || isalnum(*s))) 
  160.                     if (j < MAX_NAME) {
  161.                         name[j] = *s++;
  162.                         j++;
  163.                     } else
  164.                         break;
  165.                 name[j] = '\0';
  166.                 e = getenv(name);
  167.                 if (e != NULL) {
  168.                     while(*e != '\0') {
  169.                         if (i < MAX_BUF) {
  170.                             *d++ = *e++;
  171.                             i++;
  172.                         } else
  173.                             break;
  174.                     }
  175.                 } else if (*s == '/') 
  176.                     s++;
  177.             } else {
  178.                 if (i < MAX_BUF) {
  179.                     *d++ = *s++;
  180.                     i++;
  181.                 } else
  182.                     break;
  183.             }
  184.         }
  185.         *d = '\0';
  186.         if (type == 0 || line[0] == '/') {
  187.             s = strtok(line, SEPERATORS);
  188.             while(s != NULL) {
  189.                 if (addfile(s, 0) == 0)
  190.                     return NULL;
  191.                 s = strtok(NULL, SEPERATORS);
  192.             }
  193.         } else 
  194.             if (addfile(line, type) == 0)
  195.                 return NULL;
  196.         file = va_arg(ap, char *);
  197.         type = va_arg(ap, int);
  198.     }
  199.     addfile(NULL, -1);
  200.     return path;
  201. }
  202.