Annotation of 43BSDReno/contrib/mh/uip/ap.c, revision 1.1.1.1

1.1       root        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: static int process();
                     45: 
                     46: /*  */
                     47: 
                     48: /* ARGSUSED */
                     49: 
                     50: main(argc, argv)
                     51:        int argc;
                     52:        char **argv;
                     53: {
                     54:     int     addrp = 0,
                     55:             normalize = AD_HOST,
                     56:             width = 0,
                     57:             status = 0;
                     58:     char   *cp,
                     59:           *form = NULL,
                     60:            *format = NULL,
                     61:           *nfs,
                     62:             buf[80],
                     63:          **ap,
                     64:           **argp,
                     65:           *arguments[MAXARGS],
                     66:            *addrs[NADDRS];
                     67: 
                     68:     invo_name = r1bindex (argv[0], '/');
                     69:     mts_init (invo_name);
                     70:     if ((cp = m_find (invo_name)) != NULL) {
                     71:        ap = brkstring (cp = getcpy (cp), " ", "\n");
                     72:        ap = copyip (ap, arguments);
                     73:     }
                     74:     else
                     75:        ap = arguments;
                     76:     (void) copyip (argv + 1, ap);
                     77:     argp = arguments;
                     78: 
                     79: /*  */
                     80: 
                     81:     while (cp = *argp++) {
                     82:        if (*cp == '-')
                     83:            switch (smatch (++cp, switches)) {
                     84:                case AMBIGSW:
                     85:                    ambigsw (cp, switches);
                     86:                    done (1);
                     87: 
                     88:                case UNKWNSW:
                     89:                    adios (NULLCP, "-%s unknown", cp);
                     90: 
                     91:                case HELPSW:
                     92:                    (void) sprintf (buf, "%s [switches] addrs ...", invo_name);
                     93:                    help (buf, switches);
                     94:                    done (1);
                     95: 
                     96:                case FORMSW:
                     97:                    if (!(form = *argp++) || *form == '-')
                     98:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                     99:                    format = NULL;
                    100:                    continue;
                    101:                case FMTSW:
                    102:                    if (!(format = *argp++) || *format == '-')
                    103:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    104:                    form = NULL;
                    105:                    continue;
                    106: 
                    107:                case WIDSW:
                    108:                    if (!(cp = *argp++) || *cp == '-')
                    109:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    110:                    width = atoi (cp);
                    111:                    continue;
                    112: 
                    113:                case NORMSW:
                    114:                    normalize = AD_HOST;
                    115:                    continue;
                    116:                case NNORMSW:
                    117:                    normalize = AD_NHST;
                    118:                    continue;
                    119:            }
                    120:        if (addrp > NADDRS)
                    121:            adios (NULLCP, "more than %d addresses", NADDRS);
                    122:        else
                    123:            addrs[addrp++] = cp;
                    124:     }
                    125:     addrs[addrp] = NULL;
                    126: 
                    127: /*  */
                    128: 
                    129:     if (addrp == 0)
                    130:        adios (NULLCP, "usage: %s [switches] addrs ...", invo_name);
                    131: 
                    132:     nfs = new_fs (form, format, FORMAT);
                    133:     if (width == 0) {
                    134:        if ((width = sc_width ()) < WIDTH / 2)
                    135:            width = WIDTH / 2;
                    136:        width -= 2;
                    137:     }
                    138:     if (width > WBUFSIZ)
                    139:        width = WBUFSIZ;
                    140:     fmt_norm = normalize;
                    141:     (void) fmt_compile (nfs, &fmt);
                    142:     dat[0] = dat[1] = dat[2] = 0;
                    143:     dat[3] = width;
                    144: 
                    145:     for (addrp = 0; addrs[addrp]; addrp++)
                    146:        status += process (addrs[addrp], width, normalize);
                    147: 
                    148:     done (status);
                    149: }
                    150: 
                    151: /*  */
                    152: 
                    153: struct pqpair {
                    154:     char    *pq_text;
                    155:     char    *pq_error;
                    156:     struct pqpair *pq_next;
                    157: };
                    158: 
                    159: 
                    160: static int
                    161: process(arg, length, norm)
                    162:        register char  *arg;
                    163:        int length, norm;
                    164: {
                    165:     int            status = 0;
                    166:     register char  *cp;
                    167:     char    buffer[WBUFSIZ + 1],
                    168:             error[BUFSIZ];
                    169:     register struct comp   *cptr;
                    170:     register struct pqpair *p,
                    171:                            *q;
                    172:     struct pqpair   pq;
                    173:     register struct mailname   *mp;
                    174: 
                    175:     (q = &pq) -> pq_next = NULL;
                    176:     while (cp = getname (arg)) {
                    177:        if ((p = (struct pqpair *) calloc ((unsigned) 1, sizeof *p)) == NULL)
                    178:            adios (NULLCP, "unable to allocate pqpair memory");
                    179:        if ((mp = getm (cp, NULLCP, 0, norm, error)) == NULL) {
                    180:            p -> pq_text = getcpy (cp);
                    181:            p -> pq_error = getcpy (error);
                    182:            status++;
                    183:        }
                    184:        else {
                    185:            p -> pq_text = getcpy (mp -> m_text);
                    186:            mnfree (mp);
                    187:        }
                    188:        q = (q -> pq_next = p);
                    189:     }
                    190: 
                    191:     for (p = pq.pq_next; p; p = q) {
                    192:        FINDCOMP (cptr, "text");
                    193:        if (cptr)
                    194:            cptr -> c_text = p -> pq_text;
                    195:        FINDCOMP (cptr, "error");
                    196:        if (cptr)
                    197:            cptr -> c_text = p -> pq_error;
                    198: 
                    199:        (void) fmtscan (fmt, buffer, length, dat);
                    200:        (void) fputs (buffer, stdout);
                    201: 
                    202:        free (p -> pq_text);
                    203:        if (p -> pq_error)
                    204:            free (p -> pq_error);
                    205:        q = p -> pq_next;
                    206:        free ((char *) p);
                    207:     }
                    208: 
                    209:     return status;
                    210: }
                    211: 
                    212: /*
                    213:  * XXX We need to force config.o to be linked in to get around
                    214:  * library order problems
                    215:  */
                    216: /* XXX don't bother if linking with the shared library */
                    217: #ifndef SHARED
                    218: static void
                    219: kludge()
                    220: {
                    221:        (void)libpath((char *)0);
                    222: }
                    223: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.