1: /* ap.c - parse addresses 822-style */ 2: 3: #include "../h/mh.h" 4: #include "../h/addrsbr.h" 5: #include "../h/formatsbr.h" 6: #include <stdio.h> 7: 8: 9: #define NADDRS 100 10: 11: #define WIDTH 78 12: #define WBUFSIZ BUFSIZ 13: 14: #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>" 15: 16: /* */ 17: 18: static struct swit switches[] = { 19: #define FORMSW 0 20: "form formatfile", 0, 21: #define FMTSW 1 22: "format string", 5, 23: 24: #define NORMSW 2 25: "normalize", 0, 26: #define NNORMSW 3 27: "nonormalize", 0, 28: 29: #define WIDSW 4 30: "width columns", 0, 31: 32: #define HELPSW 5 33: "help", 4, 34: 35: NULL, NULL 36: }; 37: 38: /* */ 39: 40: static struct format *fmt; 41: 42: static int dat[4]; 43: 44: /* */ 45: 46: /* ARGSUSED */ 47: 48: main (argc, argv) 49: int argc; 50: char **argv; 51: { 52: int addrp = 0, 53: normalize = AD_HOST, 54: width = 0, 55: status = 0; 56: char *cp, 57: *form = NULL, 58: *format = NULL, 59: *nfs, 60: buf[80], 61: **ap, 62: **argp, 63: *arguments[MAXARGS], 64: *addrs[NADDRS]; 65: 66: invo_name = r1bindex (argv[0], '/'); 67: mts_init (invo_name); 68: if ((cp = m_find (invo_name)) != NULL) { 69: ap = brkstring (cp = getcpy (cp), " ", "\n"); 70: ap = copyip (ap, arguments); 71: } 72: else 73: ap = arguments; 74: (void) copyip (argv + 1, ap); 75: argp = arguments; 76: 77: /* */ 78: 79: while (cp = *argp++) { 80: if (*cp == '-') 81: switch (smatch (++cp, switches)) { 82: case AMBIGSW: 83: ambigsw (cp, switches); 84: done (1); 85: 86: case UNKWNSW: 87: adios (NULLCP, "-%s unknown", cp); 88: 89: case HELPSW: 90: (void) sprintf (buf, "%s [switches] addrs ...", invo_name); 91: help (buf, switches); 92: done (1); 93: 94: case FORMSW: 95: if (!(form = *argp++) || *form == '-') 96: adios (NULLCP, "missing argument to %s", argp[-2]); 97: format = NULL; 98: continue; 99: case FMTSW: 100: if (!(format = *argp++) || *format == '-') 101: adios (NULLCP, "missing argument to %s", argp[-2]); 102: form = NULL; 103: continue; 104: 105: case WIDSW: 106: if (!(cp = *argp++) || *cp == '-') 107: adios (NULLCP, "missing argument to %s", argp[-2]); 108: width = atoi (cp); 109: continue; 110: 111: case NORMSW: 112: normalize = AD_HOST; 113: continue; 114: case NNORMSW: 115: normalize = AD_NHST; 116: continue; 117: } 118: if (addrp > NADDRS) 119: adios (NULLCP, "more than %d addresses", NADDRS); 120: else 121: addrs[addrp++] = cp; 122: } 123: addrs[addrp] = NULL; 124: 125: /* */ 126: 127: if (addrp == 0) 128: adios (NULLCP, "usage: %s [switches] addrs ...", invo_name); 129: 130: nfs = new_fs (form, format, FORMAT); 131: if (width == 0) { 132: if ((width = sc_width ()) < WIDTH / 2) 133: width = WIDTH / 2; 134: width -= 2; 135: } 136: if (width > WBUFSIZ) 137: width = WBUFSIZ; 138: fmt_norm = normalize; 139: (void) fmt_compile (nfs, &fmt); 140: dat[0] = dat[1] = dat[2] = 0; 141: dat[3] = width; 142: 143: for (addrp = 0; addrs[addrp]; addrp++) 144: status += process (addrs[addrp], width, normalize); 145: 146: done (status); 147: } 148: 149: /* */ 150: 151: struct pqpair { 152: char *pq_text; 153: char *pq_error; 154: struct pqpair *pq_next; 155: }; 156: 157: 158: static int process (arg, length, norm) 159: register char *arg; 160: int length, 161: norm; 162: { 163: int addrp, 164: status = 0; 165: register char *cp; 166: char buffer[WBUFSIZ + 1], 167: error[BUFSIZ]; 168: register struct comp *cptr; 169: register struct pqpair *p, 170: *q; 171: struct pqpair pq; 172: register struct mailname *mp; 173: 174: (q = &pq) -> pq_next = NULL; 175: while (cp = getname (arg)) { 176: if ((p = (struct pqpair *) calloc ((unsigned) 1, sizeof *p)) == NULL) 177: adios (NULLCP, "unable to allocate pqpair memory"); 178: if ((mp = getm (cp, NULLCP, 0, norm, error)) == NULL) { 179: p -> pq_text = getcpy (cp); 180: p -> pq_error = getcpy (error); 181: status++; 182: } 183: else { 184: p -> pq_text = getcpy (mp -> m_text); 185: mnfree (mp); 186: } 187: q = (q -> pq_next = p); 188: } 189: 190: for (p = pq.pq_next; p; p = q) { 191: FINDCOMP (cptr, "text"); 192: if (cptr) 193: cptr -> c_text = p -> pq_text; 194: FINDCOMP (cptr, "error"); 195: if (cptr) 196: cptr -> c_text = p -> pq_error; 197: 198: (void) fmtscan (fmt, buffer, length, dat); 199: (void) fputs (buffer, stdout); 200: 201: free (p -> pq_text); 202: if (p -> pq_error) 203: free (p -> pq_error); 204: q = p -> pq_next; 205: free ((char *) p); 206: } 207: 208: return status; 209: }