summaryrefslogtreecommitdiffstats
path: root/mbbsd
diff options
context:
space:
mode:
authorscw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-02-05 23:40:30 +0800
committerscw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-02-05 23:40:30 +0800
commiteefc275d2541b5146dbeb8c1afdc241c4118799f (patch)
treeaeed6591c44758139ae39cd6b48ad4b470328a66 /mbbsd
parentb5158ac3a4014ecd88d4d9f78ecbcf6ec3f6b6ce (diff)
downloadpttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar.gz
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar.bz2
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar.lz
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar.xz
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.tar.zst
pttbbs-eefc275d2541b5146dbeb8c1afdc241c4118799f.zip
* In generalnamecomplete(), complete as more as possible when space (' ')
is pressed. (As what <tab> does in most shell) * In all three completing functions (namecomplete, usercomplete and generalnamecomplete), return null string instead of partial matched name which makes users able to talk/annoy those they are not supposed to talk to (admins or cloaks). * Type of parameters of generalnamecomplete has typedefed and should be clearer. * New macro CompleteBoard and CompleteOnlineUser are defined since most of the usage of generalnamecomplete are the same. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2468 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd')
-rw-r--r--mbbsd/admin.c16
-rw-r--r--mbbsd/bbs.c12
-rw-r--r--mbbsd/board.c9
-rw-r--r--mbbsd/chc.c6
-rw-r--r--mbbsd/mail.c13
-rw-r--r--mbbsd/name.c138
-rw-r--r--mbbsd/talk.c12
7 files changed, 102 insertions, 104 deletions
diff --git a/mbbsd/admin.c b/mbbsd/admin.c
index 2151f934..01b4e3db 100644
--- a/mbbsd/admin.c
+++ b/mbbsd/admin.c
@@ -447,11 +447,7 @@ m_mod_board(char *bname)
}
else{
#endif
- generalnamecomplete(MSG_SELECT_BOARD, frombname, sizeof(frombname),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard(MSG_SELECT_BOARD, frombname);
if (frombname[0] == '\0' || !getbnum(frombname) ||
!strcmp(frombname,bname))
break;
@@ -633,10 +629,7 @@ m_board()
char bname[32];
stand_title("看板設定");
- generalnamecomplete(msg_bid, bname, sizeof(bname), SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard(msg_bid, bname);
if (!*bname)
return 0;
m_mod_board(bname);
@@ -942,10 +935,7 @@ int make_symbolic_link_interactively(int gid)
{
char buf[32];
- generalnamecomplete(msg_bid, buf, sizeof(buf), SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard(msg_bid, buf);
if (!buf[0])
return -1;
diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c
index d29dd5f7..df9c2a8b 100644
--- a/mbbsd/bbs.c
+++ b/mbbsd/bbs.c
@@ -289,11 +289,7 @@ do_select(int ent, fileheader_t * fhdr, char *direct)
setutmpmode(SELECT);
move(0, 0);
clrtoeol();
- generalnamecomplete(MSG_SELECT_BOARD, bname, sizeof(bname),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard(MSG_SELECT_BOARD, bname);
if (bname[0] == '\0' || !(i = getbnum(bname)))
return FULLUPDATE;
bh = getbcache(i);
@@ -1007,11 +1003,7 @@ cross_post(int ent, fileheader_t * fhdr, char *direct)
bp = getbcache(currbid);
if (bp && (bp->brdattr & BRD_VOTEBOARD) )
return FULLUPDATE;
- generalnamecomplete("轉錄本文章於看板:", xboard, sizeof(xboard),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard("轉錄本文章於看板:", xboard);
if (*xboard == '\0' || !haspostperm(xboard))
return FULLUPDATE;
diff --git a/mbbsd/board.c b/mbbsd/board.c
index dba735f2..b5ab2413 100644
--- a/mbbsd/board.c
+++ b/mbbsd/board.c
@@ -1055,14 +1055,9 @@ choose_board(int newflag)
int bid;
move(0, 0);
clrtoeol();
- generalnamecomplete(
- "\033[7m【 增加我的最愛 】\033[m\n"
+ CompleteBoard("\033[7m【 增加我的最愛 】\033[m\n"
"請輸入欲加入的看板名稱(按空白鍵自動搜尋):",
- bname, sizeof(bname),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ bname);
if (bname[0] && (bid = getbnum(bname)) &&
HasPerm(getbcache(bid))) {
diff --git a/mbbsd/chc.c b/mbbsd/chc.c
index 32e8f1ec..3a18536c 100644
--- a/mbbsd/chc.c
+++ b/mbbsd/chc.c
@@ -1112,11 +1112,7 @@ chc_init_utmp(void)
userinfo_t *uin;
stand_title("楚河漢界之爭");
- generalnamecomplete(msg_uid, uident, sizeof(uident),
- SHM->UTMPnumber,
- completeutmp_compar,
- completeutmp_permission,
- completeutmp_getname);
+ CompleteOnlineUser(msg_uid, uident);
if (uident[0] == '\0')
return NULL;
diff --git a/mbbsd/mail.c b/mbbsd/mail.c
index 6244ddd3..6657f6a3 100644
--- a/mbbsd/mail.c
+++ b/mbbsd/mail.c
@@ -1039,11 +1039,7 @@ mail_cross_post(int ent, fileheader_t * fhdr, char *direct)
move(2, 0);
clrtoeol();
move(1, 0);
- generalnamecomplete("轉錄本文章於看板:", xboard, sizeof(xboard),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard("轉錄本文章於看板:", xboard);
if (*xboard == '\0' || !haspostperm(xboard))
return TITLE_REDRAW;
@@ -1163,12 +1159,7 @@ mail_cite(int ent, fileheader_t * fhdr, char *direct)
clrtoeol();
move(1, 0);
- generalnamecomplete("輸入看板名稱 (直接Enter進入私人信件夾):",
- buf, sizeof(buf),
- SHM->Bnumber,
- completeboard_compar,
- completeboard_permission,
- completeboard_getname);
+ CompleteBoard("輸入看板名稱 (直接Enter進入私人信件夾):", buf);
if (*buf)
strlcpy(xboard, buf, sizeof(xboard));
if (*xboard && ((bid = getbnum(xboard)) >= 0)){ /* XXXbid */
diff --git a/mbbsd/name.c b/mbbsd/name.c
index 0c9009e8..a88ac1b1 100644
--- a/mbbsd/name.c
+++ b/mbbsd/name.c
@@ -101,17 +101,23 @@ RemoveNameList(char *name)
return 0;
}
-int
-InNameList(char *name)
+static inline int
+InList(word_t * list, char *name)
{
word_t *p;
- for (p = toplev; p; p = p->next)
+ for (p = list; p; p = p->next)
if (!strcmp(p->word, name))
return 1;
return 0;
}
+int
+InNameList(char *name)
+{
+ return InList(toplev, name);
+}
+
void
ShowNameList(int row, int column, char *prompt)
{
@@ -259,11 +265,11 @@ namecomplete(char *prompt, char *data)
outs(prompt);
clrtoeol();
getyx(&y, &x);
- getyx(&origy, &origx);
standout();
prints("%*s", IDLEN + 1, "");
standend();
move(y, x);
+ origy = y; origx = x;
while ((ch = igetch()) != EOF) {
if (ch == '\n' || ch == '\r') {
@@ -271,6 +277,8 @@ namecomplete(char *prompt, char *data)
outc('\n');
if (NumInList(cwlist) == 1)
strcpy(data, cwlist->word);
+ else if (!InList(cwlist, data))
+ data[0] = '\0';
ClearSubList(cwlist);
break;
}
@@ -382,11 +390,12 @@ usercomplete(char *prompt, char *data)
outs(prompt);
clrtoeol();
getyx(&y, &x);
- getyx(&origy, &origx);
standout();
prints("%*s", IDLEN + 1, "");
standend();
move(y, x);
+ origy = y; origx = x;
+
while ((ch = igetch()) != EOF) {
if (ch == '\n' || ch == '\r') {
int i;
@@ -394,18 +403,22 @@ usercomplete(char *prompt, char *data)
*temp = '\0';
outc('\n');
- ptr = (char *)cwlist;
+ ptr = cwlist;
for (i = 0; i < cwnum; i++) {
- if (strncasecmp(data, ptr, IDLEN + 1) == 0)
+ if (strncasecmp(data, ptr, IDLEN + 1) == 0) {
strcpy(data, ptr);
+ break;
+ }
ptr += IDLEN + 1;
}
+ if (i == cwnum)
+ data[0] = '\0';
break;
} else if (ch == ' ') {
int col, len;
if (cwnum == 1) {
- strcpy(data, (char *)cwlist);
+ strcpy(data, cwlist);
move(y, x);
outs(data + count);
count = strlen(data);
@@ -486,32 +499,37 @@ usercomplete(char *prompt, char *data)
}
}
-int
+static int
gnc_findbound(char *str, int *START, int *END,
- size_t nmemb, int (*compar) (int, char *, int))
+ size_t nmemb, gnc_comp_func compar)
{
int start, end, mid, cmp, strl;
strl = strlen(str);
- start = 0, end = nmemb - 1;
- while (start != end && ((mid = (start + end) / 2) != start)) {
- cmp = compar(mid, str, strl);
- //cmp = strncasecmp(brdshm->sorted[0][mid]->brdname, str, strl);
+
+ start = -1, end = nmemb - 1;
+ /* The first available element is always in the half-open interval
+ * (start, end]. (or `end'-th it self if start == end) */
+ while (end > start + 1) {
+ mid = (start + end) / 2;
+ cmp = (*compar)(mid, str, strl);
if (cmp >= 0)
end = mid;
else
start = mid;
}
- ++start;
- if (compar(start, str, strl) != 0) {
+ if ((*compar)(end, str, strl) != 0) {
*START = *END = -1;
return -1;
}
- *START = start;
-
- end = nmemb - 1;
- while (start != end && ((mid = (start + end) / 2) != start)) {
- cmp = compar(mid, str, strl);
- //cmp = strncasecmp(brdshm->sorted[0][mid]->brdname, str, strl);
+ *START = end;
+
+ start = end;
+ end = nmemb;
+ /* The last available element is always in the half-open interval
+ * [start, end). (or `start'-th it self if start == end) */
+ while (end > start + 1) {
+ mid = (start + end) / 2;
+ cmp = (*compar)(mid, str, strl);
if (cmp <= 0)
start = mid;
else
@@ -520,30 +538,34 @@ gnc_findbound(char *str, int *START, int *END,
*END = start;
return 0;
}
-int
-gnc_completeone(char *data, int start, int end,
- int (*permission) (int), char *(*getname) (int))
+
+static int
+gnc_complete(char *data, int *start, int *end,
+ gnc_perm_func permission, gnc_getname_func getname)
{
- int i, count, at;
- if (start < 0 || end < 0)
- return -1;
- for (i = start, at = count = 0; i <= end && count < 2; ++i)
- if (permission(i)) {
- at = i;
+ int i, count, first = -1, last = *end;
+ if (*start < 0 || *end < 0)
+ return 0;
+ for (i = *start, count = 0; i <= *end; ++i)
+ if ((*permission)(i)) {
+ if (first == -1)
+ first = i;
+ last = i;
++count;
}
- if (count == 1) {
- strcpy(data, getname(at));
- return at;
- }
- return -1;
+ if (count == 1)
+ strcpy(data, (*getname)(first));
+
+ *start = first;
+ *end = last;
+ return count;
}
int
generalnamecomplete(char *prompt, char *data, int len, size_t nmemb,
- int (*compar) (int, char *, int),
- int (*permission) (int), char *(*getname) (int))
+ gnc_comp_func compar, gnc_perm_func permission,
+ gnc_getname_func getname)
{
int x, y, origx, origy, ch, i, morelist = -1, col, ret = -1;
int start, end, ptr;
@@ -552,38 +574,58 @@ generalnamecomplete(char *prompt, char *data, int len, size_t nmemb,
outs(prompt);
clrtoeol();
getyx(&y, &x);
- getyx(&origy, &origx);
standout();
prints("%*s", IDLEN + 1, "");
standend();
move(y, x);
+ origy = y; origx = x;
+
ptr = 0;
data[ptr] = 0;
+ start = 0; end = nmemb - 1;
while ((ch = igetch()) != EOF) {
if (ch == '\n' || ch == '\r') {
data[ptr] = 0;
outc('\n');
if (ptr != 0) {
gnc_findbound(data, &start, &end, nmemb, compar);
- ret = gnc_completeone(data, start, end, permission, getname);
+ if (gnc_complete(data, &start, &end, permission, getname)
+ == 1)
+ ret = start;
+ else {
+ data[0] = '\n';
+ ret = -1;
+ }
} else
ptr = -1;
break;
} else if (ch == ' ') {
- if (ptr == 0)
- continue;
-
if (morelist == -1) {
if (gnc_findbound(data, &start, &end, nmemb, compar) == -1)
continue;
- if (gnc_completeone(data, start, end,
- permission, getname) >= 0) {
+ i = gnc_complete(data, &start, &end, permission, getname);
+ if (i == 1) {
move(origy, origx);
outs(data);
ptr = strlen(data);
getyx(&y, &x);
continue;
+ } else {
+ char* first = (*getname)(start);
+ i = ptr;
+ while ((*compar)(end, first, i + 1) == 0) {
+ data[i] = first[i];
+ ++i;
+ }
+ data[i] = '\0';
+
+ if (i != ptr) { /* did complete several words */
+ move(y, x);
+ outs(data + ptr);
+ getyx(&y, &x);
+ ptr = i;
+ }
}
morelist = start;
} else if (morelist > end)
@@ -596,9 +638,9 @@ generalnamecomplete(char *prompt, char *data, int len, size_t nmemb,
col = 0;
while (len + col < 79) {
for (i = 0; morelist <= end && i < p_lines; ++morelist) {
- if (permission(morelist)) {
+ if ((*permission)(morelist)) {
move(3 + i, col);
- prints("%s ", getname(morelist));
+ prints("%s ", (*getname)(morelist));
++i;
}
}
@@ -631,7 +673,7 @@ generalnamecomplete(char *prompt, char *data, int len, size_t nmemb,
data[--ptr] = 0;
else {
for (i = start; i <= end; ++i)
- if (permission(i))
+ if ((*permission)(i))
break;
if (i == end + 1)
data[--ptr] = 0;
diff --git a/mbbsd/talk.c b/mbbsd/talk.c
index f60db2d4..927ec328 100644
--- a/mbbsd/talk.c
+++ b/mbbsd/talk.c
@@ -2248,11 +2248,7 @@ userlist(void)
int fi; /* allpickuplist[fi] */
char swid[IDLEN + 1];
move(1, 0);
- si = generalnamecomplete(msg_uid, swid,
- sizeof(swid), SHM->UTMPnumber,
- completeutmp_compar,
- completeutmp_permission,
- completeutmp_getname);
+ si = CompleteOnlineUser(msg_uid, swid);
if (si >= 0) {
pickup_t friends[MAX_FRIEND + 1];
int nGots, i;
@@ -2764,11 +2760,7 @@ t_talk()
* BBSNAME "】吧!"); return XEASY; }
*/
stand_title("打開話匣子");
- generalnamecomplete(msg_uid, uident, sizeof(uident),
- SHM->UTMPnumber,
- completeutmp_compar,
- completeutmp_permission,
- completeutmp_getname);
+ CompleteOnlineUser(msg_uid, uident);
if (uident[0] == '\0')
return 0;