dispatch.c
来自「ftam等标准协议服务器和客户端的源代码。」· C语言 代码 · 共 945 行 · 第 1/2 页
C
945 行
if (v -> v_mask) { i = 0; for (; *vec; vec++) { if (!(cp = getval (*vec, v -> v_dvalue))) { advise (NULLCP, "bad value \"%s\"", *vec); return OK; } if ((j = cp - v -> v_dvalue) <= 0) continue; i |= 1 << (j - 1); } vflag = verbose; *v -> v_value = i; if (v -> v_hook) (*v -> v_hook) (v); if (vflag) printvar (v); return OK; } if (v -> v_dvalue && (cp = getval (*vec, v -> v_dvalue))) { vflag = verbose; *v -> v_value = cp - v -> v_dvalue; if (v -> v_hook) (*v -> v_hook) (v); if (vflag) printvar (v); } else if (!v -> v_dvalue) advise (NULLCP, "bad value \"%s\"", *vec); return OK;}/* */static printvar (v)register struct var *v;{ int i; char buffer[BUFSIZ]; if (runcom) return; (void) printf ("%-*s = ", varwidth1, v -> v_name); if (v -> v_value) { i = *v -> v_value; if (v -> v_mask) { if (v -> v_dvalue) { if (i == 0) (void) printf ("%-*s", varwidth2, v -> v_dvalue[i]); else { (void) strcpy (buffer, sprintb (i, v -> v_mask)); if ((int)strlen (buffer) <= varwidth2) (void) printf ("%-*s", varwidth2, buffer); else (void) printf ("%s\n%*s", buffer, varwidth1 + varwidth2 + 3, ""); } } else (void) printf ("0x%-*x", varwidth2 - 2, i); } else { if (v -> v_dvalue) (void) printf ("%-*s", varwidth2, v -> v_dvalue[i]); else (void) printf ("%-*d", varwidth2, i); } } else if (*v -> v_dvalue) { (void) sprintf (buffer, "\"%s\"", *v -> v_dvalue); (void) printf ("%-*s", varwidth2, buffer); } (void) printf (" - %s\n", v -> v_dname);}/* */static char **getval (name, choices)register char *name;char **choices;{ register int longest, nmatches; register char *p, *q, **cp, **fp; char buffer[BUFSIZ]; longest = nmatches = 0; for (cp = choices; p = *cp; cp++) { for (q = name; *q == *p++; q++) if (*q == NULL) return cp; if (*q == NULL) if (q - name > longest) { longest = q - name; nmatches = 1; fp = cp; } else if (q - name == longest) nmatches++; } switch (nmatches) { case 0: advise (NULLCP, "unknown value \"%s\"", name); return NULL; case 1: return fp; default: for (cp = choices, p = buffer; q = *cp; cp++) if (strncmp (q, name, longest) == 0) { (void) sprintf (p, "%s \"%s\"", p != buffer ? "," : "", q); p += strlen (p); } advise (NULLCP, "ambiguous value, it could be one of:%s", buffer); return NULL; }}/* */static char *ignore[] = { "level", "listings", "verify", NULL};static struct var *getvar (name)register char *name;{ register int longest, nmatches; register char *p, *q, **ip; char buffer[BUFSIZ]; register struct var *v, *f; if (runcom) for (ip = ignore; *ip; ip++) if (lexequ (*ip, name) == 0) return NULL; longest = nmatches = 0; for (v = vars; p = v -> v_name; v++) { for (q = name; *q == *p++; q++) if (*q == NULL) return v; if (*q == NULL) if (q - name > longest) { longest = q - name; nmatches = 1; f = v; } else if (q - name == longest) nmatches++; } switch (nmatches) { case 0: advise (NULLCP, "unknown variable \"%s\"", name); return NULL; case 1: return f; default: for (v = vars, p = buffer; q = v -> v_name; v++) if (strncmp (q, name, longest) == 0) { (void) sprintf (p, "%s \"%s\"", p != buffer ? "," : "", q); p += strlen (p); } advise (NULLCP, "ambiguous variable, it could be one of:%s", buffer); return NULL; }}/* HELP */static int helpwidth;/* */int f_help (vec)char **vec;{ register int i, j, w; int columns, width, lines; register struct dispatch *ds, *es; if (network || vec == NULL) { register char **ap; for (ap = whois_help; *ap; ap++) (void) fprintf (stdfp, "%s%s", *ap, EOLN); return OK; } for (es = dispatches; es -> ds_name; es++) continue; width = helpwidth; if (*++vec == NULL) { if ((columns = ncols (stdout) / (width = (width + 8) & ~7)) == 0) columns = 1; lines = ((es - dispatches) + columns - 1) / columns; (void) printf ("Operations:\n"); for (i = 0; i < lines; i++) for (j = 0; j < columns; j++) { ds = dispatches + j * lines + i; (void) printf ("%s", ds -> ds_name); if (ds + lines >= es) { (void) printf ("\n"); break; } for (w = strlen (ds -> ds_name); w < width; w = (w + 8) & ~7) (void) putchar ('\t'); }/* (void) printf ("\nversion: %s\n", isodeversion); */ return OK; } if (strcmp (*vec, "-help") == 0) { (void) fprintf (stdfp, "help [commands ...]\n"); (void) fprintf (stdfp, " with no arguments, lists operations which may be invoked\n"); (void) fprintf (stdfp, " otherwise prints help for each operation given\n"); return OK; } for (; *vec; vec++) if (strcmp (*vec, "?") == 0) { for (ds = dispatches; ds -> ds_name; ds++) (void) printf ("%-*s\t- %s\n", width, ds -> ds_name, ds -> ds_help); break; } else if (ds = getds (*vec)) (void) printf ("%-*s\t- %s\n", width, ds -> ds_name, ds -> ds_help); return OK;}/* MISCELLANY */rcinit (){ register int w; register char **cp, *dp; char buffer[BUFSIZ]; register struct dispatch *ds; register struct var *v; if (fflag) return; if ((myhome = getenv ("HOME")) == NULL) myhome = "."; /* could do passwd search... */ if ((myuser = getenv ("USER")) == NULLCP) myuser = getenv ("LOGNAME"); if (dp = getenv ("QUIPURC")) (void) strcpy (buffer, dp); else (void) sprintf (buffer, "%s/.quipurc", myhome); snarf (buffer, "username:", &mydn); for (ds = dispatches, helpwidth = 0; ds -> ds_name; ds++) if ((w = strlen (ds -> ds_name)) > helpwidth) helpwidth = w; for (v = vars, varwidth1 = 0; v -> v_name; v++) { if ((w = strlen (v -> v_name)) > varwidth1) varwidth1 = w; if (v -> v_value) { if (cp = v -> v_dvalue) { if (v -> v_mask) {#ifdef notdef w = 1; while (*++cp) w <<= 1; w--; if ((w = strlen (sprintb (w, v -> v_mask))) > varwidth2) varwidth2 = w;#endif } else for (; *cp; cp++) if ((w = strlen (*cp)) > varwidth2) varwidth2 = w; } } else if (*v -> v_dvalue) { *v -> v_dvalue = strdup (*v -> v_dvalue); if ((w = strlen (*v -> v_dvalue) + 2) > varwidth2) varwidth2 = w; } }}/* */static snarf (file, name, variable)char *file, *name, **variable;{ int i; register char *bp, *dp, *ep; char buffer[BUFSIZ]; FILE *fp; if (fp = fopen (file, "r")) { while (fgets (bp = buffer, sizeof buffer, fp)) { if (*bp == '#' || *bp == '\n') continue; if (bp = index (buffer, '\n')) *bp = NULL; if (lexnequ (buffer, name, strlen (name))) continue; bp = buffer + strlen (name); while (isspace (*bp)) bp++; if (*bp == '"') { if (*(dp = bp + strlen (bp) - 1) == '"') bp++, *dp = NULL; goto set_variable; } i = 0; for (dp = ep = bp; *dp; ) { if (isspace (*dp)) { *ep = ' '; while (isspace (*++dp)) i = 1; if (*dp) ep++; else break; } if (i == 1) *ep = *dp; dp++, ep++; } *ep = NULL;set_variable: ; if (*variable) free (*variable); *variable = strdup (bp); break; } (void) fclose (fp); }}/* */rcfile (file, op, isystem)char *file;int op, isystem;{ register char *cp; char buffer[BUFSIZ + 1], *vec[NVEC + 1]; register FILE *fp; struct stat st; if ((fp = fopen (file, "r")) == NULL) return; runcom = 1, runsys = isystem; if (fstat (fileno (fp), &st) == NOTOK) adios (file, "unable to fstat"); rcmode = st.st_mode & 0777; while (fgets (buffer, sizeof buffer, fp)) { if (*buffer == '#') continue; if (cp = index (buffer, '\n')) *cp = NULL; bzero ((char *) vec, sizeof vec); if (str2vecY (buffer, vec) < 1) continue; if (fredloop (vec, NOTOK) != OK && op) { (void) f_quit (NULLVP); exit (1); } } runcom = runsys = 0; (void) fclose (fp);}/* */#ifndef TIOCGWINSZ/* ARGSUSED */#endifint ncols (fp)FILE *fp;{#ifdef TIOCGWINSZ int i; struct winsize ws; if (ioctl (fileno (fp), TIOCGWINSZ, (char *) &ws) != NOTOK && (i = ws.ws_col) > 0) return i;#endif return 80;}f_version (vec)char **vec;{ if (*++vec != NULL) if (strcmp (*vec, "-fred") == 0) { (void) printf ("version: %s\n", isodeversion); return OK; } (void) printf ("version: %s\n\nUsing server version:\n", isodeversion); return dish ("squid -version", 0);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?