1: /* $Header: misc.c,v 1.6 85/06/27 08:15:57 nicklin Exp $ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: #include <ctype.h> 7: #include <stdio.h> 8: #include "Mkmf.h" 9: #include "hash.h" 10: #include "macro.h" 11: #include "null.h" 12: #include "path.h" 13: #include "target.h" 14: #include "spms.h" 15: #include "suffix.h" 16: #include "system.h" 17: #include "yesno.h" 18: 19: /* 20: * answer() installs a line from stdin in the macro definition table. 21: * exit(1) is called if EOF, error, or out of memory. 22: */ 23: void 24: answer(mdefkey, mdefval) 25: char *mdefkey; /* macro definition key */ 26: int mdefval; /* macro definition value */ 27: { 28: extern HASH *MDEFTABLE; /* macro definition table */ 29: char answerbuf[ANSWERBUFSIZE]; /* answer from stdin */ 30: char *gets(); /* get a line from stdin */ 31: HASHBLK *htinstall(); /* install hash table entry */ 32: 33: if (gets(answerbuf) == NULL) 34: exit(1); 35: if (*answerbuf != '\0') 36: if (htinstall(mdefkey, answerbuf, mdefval, MDEFTABLE) == NULL) 37: exit(1); 38: } 39: 40: 41: 42: /* 43: * fastcopy() copies file to stream fp. Returns integer YES if successful, 44: * otherwise NO. 45: */ 46: fastcopy(filename, ofp) 47: char *filename; /* file to be copied */ 48: register FILE *ofp; /* output stream */ 49: { 50: register int ifd; /* input file descriptor */ 51: register int n; /* byte count */ 52: char buf[BUFSIZ]; /* I/O buffer */ 53: 54: if ((ifd = OPEN(filename, 0, 0644)) == -1) 55: { 56: pperror(""); 57: return(NO); 58: } 59: while ((n = read(ifd, buf, BUFSIZ)) > 0) 60: write(fileno(ofp), buf, n); 61: close(ifd); 62: return(YES); 63: } 64: 65: 66: 67: /* 68: * findmf() locates the makefile to be edited. The type of makefile 69: * is returned in target. The pathname to the makefile is returned 70: * in mfpath. Returns YES if makefile or makefile template can be 71: * opened, otherwise NO. 72: */ 73: findmf(mfname, mfpath, target) 74: char *mfname; /* name of target makefile */ 75: char *mfpath; /* path to target makefile */ 76: TARGET *target; /* type of makefile target */ 77: { 78: extern int CFLAG; /* makefile creation message */ 79: char *strcpy(); /* string copy */ 80: int readmf(); /* read makefile */ 81: int targettype; /* type of makefile requested */ 82: void findmftemplate(); /* find makefile template */ 83: 84: targettype = target->type; 85: if (FILEXIST(mfname)) 86: { 87: if (!FILEWRITE(mfname)) 88: { 89: pperror(mfname); 90: target->type = VERROR; 91: return(NO); 92: } 93: if (readmf(mfname, target) == VERROR) 94: return(NO); 95: if (targettype == VUNKNOWN || targettype == target->type) 96: { 97: strcpy(mfpath, mfname); 98: return(YES); 99: } 100: } 101: target->type = (targettype == VLIBRARY) ? VLIBRARY : VPROGRAM; 102: findmftemplate(mfpath, target); 103: if (readmf(mfpath, target) == VERROR) 104: return(NO); 105: else if (CFLAG == YES) 106: warn2("creating %s from template %s", mfname, mfpath); 107: return(YES); 108: } 109: 110: 111: 112: /* 113: * findmftemplate() returns the pathname of a makefile template in mfpath. 114: */ 115: void 116: findmftemplate(mfpath, target) 117: char *mfpath; /* path to target makefile */ 118: TARGET *target; /* type of makefile target */ 119: { 120: extern char *L_MAKEFILE; /* library makefile template */ 121: extern char *P_MAKEFILE; /* program makefile template */ 122: char *cwp; /* current project pathname pointer */ 123: char *getcwp(); /* get current project pathname */ 124: char *pathcat(); /* pathname concatenation */ 125: char *strcpy(); /* string copy */ 126: 127: cwp = getcwp(); 128: if (target->type == VPROGRAM) 129: { 130: if (*P_MAKEFILE == _RDIRC) 131: strcpy(mfpath, P_MAKEFILE); 132: else if (cwp == NULL || 133: !FILEXIST(pathcat(mfpath, pathcat(mfpath, cwp, "lib"), P_MAKEFILE))) 134: pathcat(mfpath, SPMSLIB, P_MAKEFILE); 135: } 136: else if (target->type == VLIBRARY) 137: { 138: if (*L_MAKEFILE == _RDIRC) 139: strcpy(mfpath, L_MAKEFILE); 140: else if (cwp == NULL || 141: !FILEXIST(pathcat(mfpath, pathcat(mfpath, cwp, "lib"), L_MAKEFILE))) 142: pathcat(mfpath, SPMSLIB, L_MAKEFILE); 143: } 144: else 145: warn("unknown template request"); 146: } 147: 148: 149: 150: /* 151: * gettoken() copies the next token from token buffer to token. Returns a 152: * pointer to the first character after the token, or null upon reaching 153: * the end of the token buffer. 154: */ 155: char * 156: gettoken(token, tp) 157: register char *token; /* receiving token */ 158: register char *tp; /* token buffer pointer */ 159: { 160: while (isspace(*tp) && *tp != '\0') 161: tp++; 162: if (*tp == '\0') 163: { 164: *token = '\0'; 165: return(NULL); 166: } 167: while (!isspace(*tp) && *tp != '\0') 168: *token++ = *tp++; 169: *token = '\0'; 170: return(tp); 171: } 172: 173: 174: 175: /* 176: * putobj() converts a source file name to an object file name and then 177: * writes the file name to stream. 178: */ 179: void 180: putobj(s, stream) 181: register char *s; /* source file name */ 182: register FILE *stream; /* output stream */ 183: { 184: extern char OBJSFX[]; /* object file name suffix */ 185: register char *dot; /* pointer to suffix */ 186: char *rindex(); /* last occurrence of character */ 187: 188: dot = rindex(s, '.'); 189: while (s != dot) 190: putc(*s++, stream); 191: fprintf(stream, "%s", OBJSFX); 192: } 193: 194: 195: 196: /* 197: * readmf() reads a makefile and loads CFLAGS, FFLAGS, and SUFFIX definitions 198: * into the macro definition table if they do not already exist. Returns 199: * integer VLIBRARY, VPROGRAM, or VUNKNOWN according to the type of makefile, 200: * or VERROR if cannot open makefile. 201: */ 202: readmf(mfname, target) 203: char *mfname; /* name of makefile */ 204: TARGET *target; /* type of makefile target */ 205: { 206: register char *bp; /* buffer pointer */ 207: extern char IOBUF[]; /* I/O buffer line */ 208: extern HASH *MDEFTABLE; /* macro definition table */ 209: char *findmacro(); /* is the line a macro definition? */ 210: char *findrule(); /* is the line a rule definition? */ 211: char *getlin(); /* get a line from input stream */ 212: char *getmacro(); /* get macro def from input stream */ 213: char macrodef[MACRODEFSIZE]; /* macro definition buffer */ 214: char macroname[MACRONAMSIZE]; /* macro name buffer */ 215: char rulename[2*SUFFIXSIZE+3]; /* transformation rule name */ 216: FILE *fopen(); /* open file */ 217: FILE *fp; /* file pointer */ 218: HASHBLK *htinstall(); /* install hash table entry */ 219: HASHBLK *htlookup(); /* find hash table entry */ 220: int storerule(); /* store transformation rule */ 221: 222: target->type = target->dest = VUNKNOWN; 223: if ((fp = fopen(mfname, "r")) == NULL) 224: { 225: pperror(mfname); 226: target->type = VERROR; 227: return(target->type); 228: } 229: while (getlin(fp) != NULL) 230: { 231: if (EQUAL(IOBUF, DEPENDMARK)) 232: break; 233: for (bp = IOBUF; *bp == ' '; bp++) 234: continue; 235: if (isalnum(*bp) && findmacro(macroname, bp) != NULL) 236: { 237: if (EQUAL(macroname, MPROGRAM)) 238: { 239: target->type = VPROGRAM; 240: continue; 241: } 242: else if (EQUAL(macroname, MLIBRARY)) 243: { 244: target->type = VLIBRARY; 245: continue; 246: } 247: else if (EQUAL(macroname, MDESTDIR)) 248: { 249: target->dest = VDESTDIR; 250: continue; 251: } 252: 253: /* does macro definition already exist? */ 254: if (htlookup(macroname, MDEFTABLE) != NULL) 255: continue; 256: 257: if (EQUAL(macroname, MCFLAGS) || 258: EQUAL(macroname, MFFLAGS) || 259: EQUAL(macroname, MPFLAGS) || 260: EQUAL(macroname, MSUFFIX)) 261: { 262: if (htinstall(macroname, getmacro(macrodef, fp), 263: VREADONLY, MDEFTABLE) == NULL) 264: { 265: fclose(fp); 266: return(NO); 267: } 268: } 269: } 270: else if (*bp == '.' && findrule(rulename, bp) != NULL) 271: { 272: if (storerule(rulename) == NO) 273: { 274: fclose(fp); 275: return(NO); 276: } 277: } 278: } 279: fclose(fp); 280: return(target->type); 281: }