char * u_namearray(char buf[][IDLEN + 1], int max, int *pnum, char *tag, char *atag, int *full) /* 根据tag ,生成 匹配的user id 列表 (针对所有注册用户)*/ { struct UCACHEHASH *reg_ushm = binfo->ucachehashshm; int n, num, i; int hash, len, ksz, alen; char tagv[IDLEN + 1]; len = strlen(tag); alen = strlen(atag); if (len > IDLEN) return NULL; if (!len) { return NULL; } ksz = ucache_hash_deep(tag); strcpy(tagv, tag); if (len >= ksz || len == IDLEN) { tagv[ksz] = 0; hash = ucache_hash(tagv) - 1; for (n = 0; n < UCACHE_HASHBSIZE; n++) { num = reg_ushm->hash_head[(hash + n % UCACHE_HASHBSIZE) % UCACHE_HASHSIZE + 1]; while (num) { if (!strncasecmp (passwdptr[num - 1].userid, atag, alen) && (passwdptr[num - 1].kickout == 0)) { strcpy(buf[(*pnum)++], passwdptr[num - 1].userid); /*如果匹配, add into buf */ if (*pnum >= max) { *full = 1; return buf[0]; } } num = reg_ushm->next[num]; } } } else { for (i = 'A'; i <= 'Z'; i++) { tagv[len] = i; tagv[len + 1] = 0; u_namearray(buf, max, pnum, tagv, atag, full); if (*full == 1) return buf[0]; if (mytoupper(tagv[len]) == 'Z') { tagv[len] = 'A'; return buf[0]; } else { tagv[len]++; } } } return buf[0]; }
int usercomplete(char *prompt, char *data) { char *cwbuf, *cwlist, *temp; int cwnum, x, y, origx, origy; int clearbot = NA, count = 0, morenum = 0; char ch; cwbuf = malloc(MAXUSERS * (IDLEN + 1)); if (prompt != NULL) { prints("%s", prompt); clrtoeol(); } temp = data; cwlist = u_namearray((void *)cwbuf, &cwnum, ""); screen_coordinates(&y, &x); screen_coordinates(&origy, &origx); while ((ch = terminal_getchar()) != EOF) { if (ch == '\n' || ch == '\r') { int i; char *ptr; *temp = '\0'; prints("\n"); ptr = cwlist; for (i = 0; i < cwnum; i++) { if (strncasecmp(data, ptr, IDLEN + 1) == 0) strcpy(data, ptr); ptr += IDLEN + 1; } /* if( cwnum == 1 ) strcpy( data, cwlist ); */ break; } // if if (ch == ' ' || ch == KEY_TAB) { int col, len, i, j; int n; if (cwnum == 1) { strcpy(data, cwlist); screen_move(y, x); prints("%s", data + count); count = strlen(data); temp = data + count; screen_coordinates(&y, &x); continue; } for (i = strlen(data); i && i < IDLEN; i++) { ch = cwlist[i]; if (ch == '\0') break; for (j = 0; j < cwnum; j++) { if (toupper((cwlist + (IDLEN + 1) * j)[i]) != toupper(ch)) break; } if (j != cwnum) break; *temp++ = ch; *temp = '\0'; n = UserSubArray((void *)cwbuf, (void *)cwlist, cwnum, ch, count); if (n == 0) { temp--; *temp = '\0'; break; } cwlist = cwbuf; count++; cwnum = n; morenum = 0; screen_move(y, x); outc(ch); x++; } clearbot = YEA; col = 0; len = UserMaxLen((void *)cwlist, cwnum, morenum, NUMLINES); screen_move(origy + 1, 0); screen_clrtobot(); //% printdash(" 所有使用者列表 "); printdash(" \xcb\xf9\xd3\xd0\xca\xb9\xd3\xc3\xd5\xdf\xc1\xd0\xb1\xed "); while (len + col < 79) { int i; for (i = 0; morenum < cwnum && i < NUMLINES - origy + 1; i++) { char *tmpptr = cwlist + (IDLEN + 1) * morenum++; if (*tmpptr != '\0') { //by Eric screen_move(origy + 2 + i, col); prints("%s ", tmpptr); } else i--; } col += len + 2; if (morenum >= cwnum) break; len = UserMaxLen((void *)cwlist, cwnum, morenum, NUMLINES); } if (morenum < cwnum) { screen_move(-1, 0); //% prints("[1;44m-- 还有使用者 -- [m"); prints("[1;44m-- \xbb\xb9\xd3\xd0\xca\xb9\xd3\xc3\xd5\xdf -- [m"); } else { morenum = 0; } screen_move(y, x); continue; } if (ch == '\177' || ch == '\010') { if (temp == data) continue; temp--; count--; *temp = '\0'; cwlist = u_namearray((void *)cwbuf, &cwnum, data); morenum = 0; x--; screen_move(y, x); outc(' '); screen_move(y, x); continue; } if (count < STRLEN) { int n; *temp++ = ch; *temp = '\0'; n = UserSubArray((void *)cwbuf, (void *)cwlist, cwnum, ch, count); if (n == 0) { temp--; *temp = '\0'; continue; } cwlist = cwbuf; count++; cwnum = n; morenum = 0; screen_move(y, x); outc(ch); x++; } } free(cwbuf); if (ch == EOF) longjmp(byebye, -1); prints("\n"); screen_flush(); if (clearbot) { screen_move(origy, 0); screen_clrtobot(); } if (*data) { screen_move(origy, origx); prints("%s\n", data); } return 0; }
void usercomplete(char *prompt, char *data) { char *temp; char *cwbuf, *cwlist; int cwnum, x, y, origx, origy; int clearbot = NA, count = 0, morenum = 0; char ch; cwbuf = malloc(MAX_USERS * (IDLEN + 1)); cwlist = u_namearray((arrptr)cwbuf, &cwnum, ""); temp = data; outs(prompt); clrtoeol(); getyx(&y, &x); getyx(&origy, &origx); standout(); prints("%*s", IDLEN + 1, ""); standend(); move(y, x); while((ch = igetch()) != EOF) { if(ch == '\n' || ch == '\r') { int i; char *ptr; *temp = '\0'; outc('\n'); ptr = (char *)cwlist; for(i = 0; i < cwnum; i++) { if(strncasecmp(data, ptr, IDLEN + 1) == 0) strcpy(data, ptr); ptr += IDLEN + 1; } break; } else if(ch == ' ') { int col, len; if(cwnum == 1) { strcpy(data, (char *)cwlist); move(y, x); outs(data + count); count = strlen(data); temp = data + count; getyx(&y, &x); continue; } clearbot = YEA; col = 0; len = UserMaxLen((arrptr)cwlist, cwnum, morenum, p_lines); move(2, 0); clrtobot(); printdash("使用者代號一覽表"); while(len + col < 79) { int i; for(i = 0; morenum < cwnum && i < p_lines; i++) { move(3 + i, col); prints("%s ", cwlist + (IDLEN + 1) * morenum++); } col += len + 2; if(morenum >= cwnum) break; len = UserMaxLen((arrptr)cwlist, cwnum, morenum, p_lines); } if(morenum < cwnum) { move(b_lines, 0); outs(msg_more); } else morenum = 0; move(y, x); continue; } else if(ch == '\177' || ch == '\010') { if(temp == data) continue; temp--; count--; *temp = '\0'; cwlist = u_namearray((arrptr)cwbuf, &cwnum, data); morenum = 0; x--; move(y, x); outc(' '); move(y, x); continue; } else if(count < STRLEN && isprint(ch)) { int n; *temp++ = ch; *temp = '\0'; n = UserSubArray((arrptr)cwbuf, (arrptr)cwlist, cwnum, ch, count); if(n == 0) { temp--; *temp = '\0'; continue; } cwlist = cwbuf; count++; cwnum = n; morenum = 0; move(y, x); outc(ch); x++; } } free(cwbuf); if(ch == EOF) /* longjmp(byebye, -1); */ raise(SIGHUP); /* jochang: don't know if this is necessary */ outc('\n'); refresh(); if(clearbot) { move(2, 0); clrtobot(); } if(*data) { move(origy, origx); outs(data); outc('\n'); } }