1: /* $Header: buildlist.c,v 1.3 86/01/12 00:49:30 lepreau Exp $ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: #include <sys/types.h> 7: #include <sys/dir.h> 8: #include <stdio.h> 9: #include <ctype.h> 10: #include "Mkmf.h" 11: #include "hash.h" 12: #include "null.h" 13: #include "path.h" 14: #include "slist.h" 15: #include "suffix.h" 16: #include "system.h" 17: #include "yesno.h" 18: 19: /* 20: * buftolist() copies the items from a buffer to a singly-linked list. 21: * Returns integer YES if successful, otherwise NO. 22: */ 23: buftolist(buf, list) 24: char *buf; /* item buffer */ 25: SLIST *list; /* receiving list */ 26: { 27: char *gettoken(); /* get next token */ 28: char *slappend(); /* append file name to list */ 29: char token[MAXNAMLEN]; /* item buffer */ 30: 31: while ((buf = gettoken(token, buf)) != NULL) 32: { 33: if (slappend(token, list) == NULL) 34: return(NO); 35: } 36: return(YES); 37: } 38: 39: 40: 41: /* 42: * buildliblist() reads library pathnames from the LIBLIST macro 43: * definition, and adds them to the library pathname list. Libraries 44: * may be specified as `-lx'. Returns integer YES if successful, 45: * otherwise NO. 46: */ 47: buildliblist() 48: { 49: extern SLIST *LIBLIST; /* library pathname list */ 50: extern HASH *MDEFTABLE; /* macro definition table */ 51: HASHBLK *htb; /* hash table block */ 52: HASHBLK *htlookup(); /* find hash table entry */ 53: int libbuftolist(); /* load library pathnames into list */ 54: SLIST *slinit(); /* initialize singly-linked list */ 55: void htrm(); /* remove hash table entry */ 56: 57: LIBLIST = NULL; 58: if ((htb = htlookup(MLIBLIST, MDEFTABLE)) != NULL) 59: { 60: LIBLIST = slinit(); 61: if (libbuftolist(htb->h_def, LIBLIST) == NO) 62: return(NO); 63: } 64: htrm(MLIBLIST, MDEFTABLE); 65: return(YES); 66: } 67: 68: 69: 70: /* 71: * buildsrclist() takes source and header file names from command line 72: * macro definitions or the current directory and appends them to source 73: * or header file name lists as appropriate. Returns integer YES if 74: * if successful, otherwise NO. 75: */ 76: buildsrclist() 77: { 78: extern HASH *MDEFTABLE; /* macro definition table */ 79: extern SLIST *HEADLIST; /* header file name list */ 80: extern SLIST *SRCLIST; /* source file name list */ 81: char *slappend(); /* append file name to list */ 82: HASHBLK *headhtb; /* HEADERS macro hash table block */ 83: HASHBLK *htlookup(); /* find hash table entry */ 84: HASHBLK *srchtb; /* SOURCE macro hash table block */ 85: int buftolist(); /* copy items from buffer to list */ 86: int needheaders = 1; /* need header file names */ 87: int needsource = 1; /* need source file names */ 88: int read_dir(); /* read dir for source and headers */ 89: int slsort(); /* sort singly-linked list */ 90: int strcmp(); /* string comparison */ 91: SLIST *slinit(); /* initialize singly-linked list */ 92: 93: HEADLIST = slinit(); 94: SRCLIST = slinit(); 95: 96: /* build lists from command line macro definitions */ 97: if ((headhtb = htlookup(MHEADERS, MDEFTABLE)) != NULL) 98: { 99: if (buftolist(headhtb->h_def, HEADLIST) == NO) 100: return(NO); 101: needheaders--; 102: } 103: if ((srchtb = htlookup(MSOURCE, MDEFTABLE)) != NULL) 104: { 105: if (buftolist(srchtb->h_def, SRCLIST) == NO) 106: return(NO); 107: needsource--; 108: } 109: 110: /* read the current directory to get source and header file names */ 111: if (needheaders || needsource) 112: if (read_dir(needheaders, needsource) == NO) 113: return(NO); 114: 115: if (slsort(strcmp, SRCLIST) == NO) 116: return(NO); 117: if (slsort(strcmp, HEADLIST) == NO) 118: return(NO); 119: return(YES); 120: } 121: 122: 123: 124: /* 125: * expandlibpath() converts a library file specified by `-lx' into a full 126: * pathname. /lib and /usr/lib are searched for the library in the form 127: * libx.a. An integer YES is returned if the library was found, otherwise NO. 128: * A library file which doesn't begin with `-' is left unchanged. 129: */ 130: expandlibpath(libpath) 131: char *libpath; /* library pathname buffer */ 132: { 133: char *lib; /* /lib library pathname template */ 134: char *strcpy(); /* string copy */ 135: char *usrlib; /* /usr/lib library pathname template */ 136: int i; /* library pathname index */ 137: 138: lib = "/lib/libxxxxxxxxxxxxxxxxxxxxxxxxx"; 139: usrlib = "/usr/lib/libxxxxxxxxxxxxxxxxxxxxxxxxx"; 140: 141: if (libpath[0] == '-' && libpath[1] == 'l') 142: { 143: for (i = 0; libpath[i+2] != '\0' && i < 22; i++) 144: { 145: lib[i+8] = libpath[i+2]; 146: usrlib[i+12] = libpath[i+2]; 147: } 148: lib[i+8] = usrlib[i+12] = '.'; 149: lib[i+9] = usrlib[i+13] = 'a'; 150: lib[i+10] = usrlib[i+14] = '\0'; 151: if (FILEXIST(lib)) 152: { 153: strcpy(libpath, lib); 154: return(YES); 155: } 156: else if (FILEXIST(usrlib)) 157: { 158: strcpy(libpath, usrlib); 159: return(YES); 160: } 161: else 162: return(NO); 163: } 164: return(YES); 165: } 166: 167: 168: 169: /* 170: * libbuftolist() appends each library pathname specified in libbuf to 171: * the liblist library pathname list. 172: */ 173: libbuftolist(libbuf, liblist) 174: char *libbuf; /* library pathname buffer */ 175: SLIST *liblist; /* library pathname list */ 176: { 177: char *gettoken(); /* get next token */ 178: char libpath[PATHSIZE]; /* library file pathname */ 179: char *slappend(); /* append file name to list */ 180: int expandlibpath(); /* -lx -> full library pathname */ 181: 182: while ((libbuf = gettoken(libpath, libbuf)) != NULL) 183: { 184: if (expandlibpath(libpath) == NO) 185: { 186: warns("can't find library %s", libpath); 187: return(NO); 188: } 189: if (slappend(libpath, liblist) == NULL) 190: return(NO); 191: } 192: return(YES); 193: } 194: 195: 196: 197: /* 198: * read_dir() reads filenames from the current directory and adds them 199: * to the source or header file name lists as appropriate. Returns 200: * integer YES if successful, otherwise NO. 201: */ 202: read_dir(needheaders, needsource) 203: int needheaders; /* need header file names */ 204: int needsource; /* need source file names */ 205: { 206: extern int AFLAG; /* accept src files w/ leading dots? */ 207: extern SLIST *HEADLIST; /* header file name list */ 208: extern SLIST *SRCLIST; /* source file name list */ 209: char *rindex(); /* find last occurrence of character */ 210: char *slappend(); /* append file name to list */ 211: char *suffix; /* pointer to file name suffix */ 212: char *p; /* beginning of file name component */ 213: DIR *dirp; /* directory stream */ 214: DIR *opendir(); /* open directory stream */ 215: int lookupsfx(); /* get suffix type */ 216: int sfxtyp; /* type of suffix */ 217: struct direct *dp; /* directory entry pointer */ 218: struct direct *readdir(); /* read a directory entry */ 219: 220: if ((dirp = opendir(CURDIR)) == NULL) 221: { 222: warn("can't open current directory"); 223: return(NO); 224: } 225: for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) 226: if ((suffix = rindex(dp->d_name, '.')) != NULL) 227: { 228: if ((p = rindex(dp->d_name, '/')) != NULL) 229: p++; 230: else 231: p = dp->d_name; 232: if (*p == '.' && (AFLAG == 0)) 233: continue; 234: suffix++; 235: sfxtyp = lookupsfx(suffix); 236: if (sfxtyp == SFXSRC) 237: { 238: if (needsource) 239: if (slappend(dp->d_name, SRCLIST) == NULL) 240: return(NO); 241: } 242: else if (sfxtyp == SFXHEAD) 243: { 244: if (needheaders) 245: if (slappend(dp->d_name, HEADLIST) == NULL) 246: return(NO); 247: } 248: } 249: closedir(dirp); 250: return(YES); 251: }