|
|
1.1 ! root 1: /* dfns.c */ ! 2: ! 3: #ifndef lint ! 4: static char *rcsid = "$Header: /f/osi/pepsy/RCS/dfns.c,v 7.0 90/07/01 19:54:14 mrose Exp $"; ! 5: #endif ! 6: ! 7: /* ! 8: * $Header: /f/osi/pepsy/RCS/dfns.c,v 7.0 90/07/01 19:54:14 mrose Exp $ ! 9: * ! 10: * ! 11: * $Log: dfns.c,v $ ! 12: * Revision 7.0 90/07/01 19:54:14 mrose ! 13: * *** empty log message *** ! 14: * ! 15: */ ! 16: ! 17: /* ! 18: * NOTICE ! 19: * ! 20: * Acquisition, use, and distribution of this module and related ! 21: * materials are subject to the restrictions of a license agreement. ! 22: * Consult the Preface in the User's Manual for the full terms of ! 23: * this agreement. ! 24: * ! 25: */ ! 26: ! 27: ! 28: #include <stdio.h> ! 29: #include "general.h" ! 30: #include "mine.h" ! 31: #include "pass2.h" ! 32: ! 33: id_entry *id_table[TABLESIZE]; ! 34: ! 35: #define my_error(mesg) (fprintf(stderr, "%s\n",mesg),exit(1)) ! 36: ! 37: extern char *notidtoid(), *my_new_str(), *my_strcat(); ! 38: extern char * ! 39: proc_name(), *mymodule; ! 40: ! 41: /* ! 42: * Lookup the hash table (id_table) for the string t and insert it at ! 43: * the start of the appropriate chain if it is not already there. ! 44: * The argument flag indicates whether t is being defined (1) or used ! 45: * (0). ! 46: */ ! 47: char * ! 48: proc_name(t, flag) ! 49: char *t; ! 50: int flag; ! 51: { ! 52: int i; ! 53: static int curr = 0; ! 54: id_entry *ptr; ! 55: ! 56: i = hash_val(t); ! 57: for (ptr = id_table[i]; ptr != NULL && strcmp(t, ptr->h_value); ptr = ptr->next); ! 58: ! 59: if (ptr == NULL) { ! 60: if ((ptr = (id_entry *) malloc(sizeof(id_entry))) == NULL) ! 61: my_error("proc_name: Out of memory"); ! 62: ptr->h_value = t; ! 63: ptr->r_value = my_strcat(notidtoid(t), notidtoid(mymodule)); ! 64: ptr->count = 1; ! 65: if (flag) { ! 66: ptr->def_bit = flag; ! 67: ptr->def_value = curr++; ! 68: } ! 69: ptr->next = id_table[i]; ! 70: id_table[i] = ptr; ! 71: } else if (!ptr->def_bit) ! 72: ptr->def_bit = flag; ! 73: ! 74: return ptr->r_value; ! 75: } ! 76: ! 77: /* ! 78: * output a sequence of #define statements (one for each value stored ! 79: * in the hash table) to the file specified by fp ! 80: */ ! 81: out_final_defs(fp) ! 82: FILE *fp; ! 83: { ! 84: int j; ! 85: id_entry *ptr; ! 86: ! 87: for (j = 0; j < TABLESIZE; j++) ! 88: for (ptr = id_table[j]; ptr != NULL; ptr = ptr->next) { ! 89: if (ptr->def_bit) ! 90: (void) fprintf(fp, "#define %s%s\t%d\n", PREFIX, ptr->r_value, ptr->def_value); ! 91: else ! 92: ferrs(0, "the identifier %s is used but not defined\n", ptr->h_value); ! 93: if (ptr->count > 1) /* not used */ ! 94: (void) printf("The id %s has a count of %d\n", ptr->r_value, ptr->count); ! 95: } ! 96: } ! 97: ! 98: /* ! 99: * return a copy of the string s with '-' replaced by '_' ! 100: */ ! 101: char * ! 102: notidtoid(s) ! 103: char *s; ! 104: { ! 105: ! 106: char *t, *r; ! 107: ! 108: t = my_new_str(s); ! 109: for (r = t; *r != '\0'; r++) ! 110: if (*r == '-') ! 111: *r = '_'; ! 112: return t; ! 113: } ! 114: ! 115: /* ! 116: * return a copy of the string s ! 117: */ ! 118: char * ! 119: my_new_str(s) ! 120: char *s; ! 121: { ! 122: ! 123: char *t; ! 124: ! 125: if ((t = (char *) malloc(strlen(s) + 1)) == NULL) ! 126: my_error("my_new_str: Out of memory"); ! 127: ! 128: strcpy(t, s); ! 129: return t; ! 130: } ! 131: ! 132: /* ! 133: * return the concatenation of the strings s1 and s2 ! 134: */ ! 135: char * ! 136: my_strcat(s1, s2) ! 137: char *s1, *s2; ! 138: { ! 139: char *s3, *s, *t; ! 140: ! 141: if (s1 == NULL || *s1 == '\0') ! 142: return my_new_str(s2); ! 143: ! 144: if ((s3 = (char *) malloc(strlen(s1) + strlen(s2) + 1)) == NULL) ! 145: my_error("my_strcat: Out of memory"); ! 146: ! 147: for (s = s1, t = s3; *s != '\0'; s++, t++) ! 148: *t = *s; ! 149: strcpy(t, s2); ! 150: return s3; ! 151: } ! 152: ! 153: /* ! 154: * a simple hash function ! 155: */ ! 156: hash_val(s) ! 157: char *s; ! 158: { ! 159: int i, sum; ! 160: char *t; ! 161: ! 162: sum = 0; ! 163: for (i = 1, t = s; *t != '\0'; i++, t++) ! 164: sum = sum + *t * i; ! 165: return (sum % TABLESIZE); ! 166: } ! 167: ! 168: /* ! 169: * initialize the table id_table ! 170: */ ! 171: init() ! 172: { ! 173: int i; ! 174: ! 175: for (i = 0; i <= TABLESIZE; i++) ! 176: id_table[i] = NULL; ! 177: } ! 178: #define BUFSIZE 128 ! 179: #define MAX(a, b) ((a) > (b) ? (a) : (b)) ! 180: ! 181: static char *buf = NULL; ! 182: static int len = 0; ! 183: ! 184: /* ! 185: * Return in a static buffer the two strings concatenated ! 186: */ ! 187: char * ! 188: concat(s1, s2) ! 189: char *s1, *s2; ! 190: { ! 191: int tot; ! 192: ! 193: tot = strlen(s1) + strlen(s2) + 1; ! 194: ! 195: if (tot > len) { ! 196: len = MAX(BUFSIZE, tot); ! 197: if (buf == NULL) { ! 198: if ((buf = malloc(len)) == NULL) ! 199: ferr(1, "concat:malloc failed\n"); ! 200: } else if ((buf = realloc(buf, len)) == NULL) ! 201: ferr(1, "concat:realloc failed\n"); ! 202: } ! 203: strcpy(buf, s1); ! 204: strcat(buf, s2); ! 205: ! 206: return (buf); ! 207: } ! 208: ! 209: /* ! 210: * Generate a free call given the name of the parameter, the module ! 211: * name, and the name of the type ! 212: */ ! 213: char * ! 214: gfree(module, id, parm) ! 215: char *module; /* name of module we are in (usually ! 216: * mymodule) */ ! 217: char *id; /* name of type we want to free */ ! 218: char *parm; /* name of the pointer to the data */ ! 219: { ! 220: char *p1 = notidtoid(module); ! 221: char *p2 = notidtoid(id); ! 222: ! 223: if (buf == NULL) { ! 224: if ((buf = malloc(BUFSIZE)) == NULL) ! 225: ferr(1, "gfree:malloc failed\n"); ! 226: len = BUFSIZ; ! 227: } ! 228: (void) sprintf(buf, "fre_obj((char *) %s, %s%s%s.md_dtab[%s%s%s], &%s%s%s)", ! 229: parm, ! 230: PREFIX, p1, MODTYP_SUFFIX, ! 231: PREFIX, p2, p1, ! 232: PREFIX, p1, MODTYP_SUFFIX); ! 233: free(p1); ! 234: free(p2); ! 235: ! 236: return (buf); ! 237: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.