1: #include "parms.h" 2: #include "structs.h" 3: #include "net.h" 4: 5: #ifdef RCSIDENT 6: static char rcsid[] = "$Header: getnet.c,v 1.7.0.2 86/01/17 23:49:54 notes Rel $"; 7: #endif RCSIDENT 8: 9: /* 10: * getnet(sys,nf, howp) 11: * char *sys; 12: * char *nf; 13: * struct nethow_f *howp; 14: * 15: * This routine will scan the net.how file, looking for alternate 16: * routes to the system specified. The xmit and reply pointers are 17: * set appropriately. 18: * (null if no entry found). 19: * The routine returns -1 if the file is not there, otherwise 20: * it returns the count of lines matched (0,1,2) 21: * 22: * Original Coding: Ray Essick April 1982 23: */ 24: 25: 26: 27: int GetNetDebug = 0; 28: 29: getnet (sys, nf, howp) 30: char *sys; 31: char *nf; 32: struct nethow_f *howp; /* fill it in */ 33: { 34: 35: FILE * nethow; 36: char pathname[256]; /* probably ok */ 37: char oneline[512]; 38: char *p; 39: int i, 40: count; 41: #define MAXFIELDS 5 42: char *field[MAXFIELDS]; /* field pointers */ 43: 44: count = 0; /* lines we have */ 45: if (howp != (struct nethow_f *) NULL) /* init the sucker */ 46: { 47: howp -> nh_proto = 0; 48: howp -> nh_system = howp -> nh_nf = (char *) NULL; 49: howp -> nh_xmit = howp -> nh_rcv = (char *) NULL; 50: } 51: 52: sprintf (pathname, "%s/%s/%s", Mstdir, UTILITY, NETHOW); 53: if ((nethow = fopen (pathname, "r")) == NULL) 54: return (-1); 55: 56: while (fgets (oneline, sizeof oneline, nethow) != NULL && count < 2) 57: { 58: if (GetNetDebug) 59: fprintf (stderr, "getnet reads: %s", oneline); 60: i = strlen (oneline); /* get end */ 61: if (oneline[i - 1] != '\n') /* not entire line */ 62: { 63: fprintf (stderr, "%s: net.how line longer than %d characters", 64: Invokedas, pathname, sizeof oneline); 65: return (-2); /* line too long */ 66: } 67: else 68: { 69: oneline[i - 1] = '\0'; /* strip newline */ 70: } 71: if (oneline[0] == '#' || oneline[0] == '\0') 72: continue; /* comment or empty */ 73: 74: for (i = 1; i < MAXFIELDS; i++) 75: field[i] = NULL; /* zero them */ 76: for (i = 1, p = field[0] = oneline; i < MAXFIELDS && *p != '\0';) 77: { /* extract the field */ 78: if (*p == '\0') /* end of string */ 79: break; 80: if (*p != ':') /* not end of a field */ 81: { 82: p++; 83: continue; 84: } 85: *p = '\0'; /* end this field */ 86: field[i++] = ++p; /* start next field */ 87: } 88: if (GetNetDebug > 3) 89: { 90: for (i = 0; i < MAXFIELDS; i++) 91: fprintf (stderr, "field[%d] = %s\n", i, field[i]); 92: } 93: 94: /* 95: * see if it is what we want. if so, fill in the appropriate 96: * structure elements and go away. 97: */ 98: 99: if (strcmp (field[0], sys) != 0) /* match? */ 100: continue; /* no */ 101: 102: /* 103: * see if the nf matches. 104: * BEEF THIS UP. MAKE A SUBROUTINE TO MATCH 105: */ 106: 107: if (*field[3] != '\0') 108: { 109: char *lead = field[3], 110: *trail = field[3]; 111: int match = 0; 112: 113: extern int nfmatch (); 114: 115: while (1) 116: { 117: if (*lead == '\0' || *lead == ',') /* new pattern */ 118: { 119: if (*lead == ',') 120: { 121: *lead++ = '\0'; 122: } /* terminate */ 123: if (nfmatch (nf, trail) == 0) /* same? */ 124: { 125: match++; 126: break; /* yes, get out */ 127: } 128: trail = lead; /* bring up the pointer */ 129: if (*lead != '\0') /* get more */ 130: continue; 131: else 132: break; /* get out */ 133: } 134: lead++; /* skip onward */ 135: } 136: if (match == 0) /* nothing matched */ 137: continue; /* so get another line */ 138: } 139: 140: if (howp -> nh_nf == (char *) NULL) 141: { 142: howp -> nh_nf = strsave (nf); /* nf name saved */ 143: } 144: 145: 146: /* 147: * fill in the appropriate field 148: */ 149: if (*field[1] == 'x') /* transmit */ 150: { 151: if (howp -> nh_xmit == (char *) NULL) /* not yet */ 152: { 153: howp -> nh_xmit = strsave (field[4]); /* save it */ 154: if (sscanf (field[2], "%d", &i) != 1) 155: { 156: if (*field[2] != '\0') /* message if non-empty */ 157: fprintf (stderr, "protocol field '%s' should be an integer\n", 158: field[2]); 159: i = 0; 160: } 161: howp -> nh_proto = i; /* save protocol */ 162: count++; /* filled another field */ 163: if (GetNetDebug > 1) 164: fprintf (stderr, "getnet: proto %d, how: %s\n", 165: howp -> nh_proto, howp -> nh_xmit); 166: } 167: } 168: else 169: { /* force reply */ 170: if (howp -> nh_rcv == (char *) NULL) 171: { 172: howp -> nh_rcv = strsave (field[4]); /* save it */ 173: count++; /* filled a field */ 174: if (GetNetDebug > 1) 175: fprintf (stderr, "getnet: rcv: %s\n", howp -> nh_rcv); 176: } 177: } 178: } 179: /* 180: * have both lknes (or exhausted input). close the file 181: * and go back. 182: */ 183: fclose (nethow); 184: return (count); 185: } 186: 187: /* 188: * quicky pattern matching routine. 189: * 190: * someday, make it do real pattern matching 191: */ 192: 193: static int nfmatch (nf, pattern) 194: char *nf; 195: char *pattern; 196: { 197: char *p; 198: int result; 199: 200: #ifdef HAS_REGEXP 201: extern char *re_comp (); 202: extern int re_exec (); 203: #endif HAS_REGEXP 204: 205: if (GetNetDebug) 206: fprintf (stderr, "nf = '%s', pattern = '%s'\n", nf, pattern); 207: 208: #ifdef HAS_REGEXP 209: /* 210: * regular expression routines from BSD -- don't know 211: * if anyone else picked them up. 212: */ 213: p = re_comp (pattern); /* compile pattern */ 214: if (p != NULL) /* error in pattern */ 215: { 216: fprintf (stderr, "%s: %s: %s\n", Invokedas, pattern, p); 217: return (-1); /* no match */ 218: } 219: result = re_exec (nf); 220: switch (result) /* fix return code */ 221: { 222: case 1: 223: if (GetNetDebug > 1) 224: fprintf (stderr, "Matched\n"); 225: return 0; 226: case 0: 227: if (GetNetDebug > 1) 228: fprintf (stderr, "NO Match\n"); 229: return 1; 230: default: 231: if (GetNetDebug > 1) 232: fprintf (stderr, "Regular Expresion Error\n"); 233: return result; 234: } 235: #endif HAS_REGEXP 236: /* 237: * real cheap -- check exact matches 238: */ 239: result = strcmp (nf, pattern); 240: switch (result) 241: { 242: case 0: 243: if (GetNetDebug) 244: fprintf (stderr, "strcmp match\n"); 245: return (0); 246: default: 247: if (GetNetDebug) 248: fprintf (stderr, "strcmp NO match\n"); 249: return (result); 250: } 251: }