[ internet ] in KIDS 글 쓴 이(By): baram (baram) 날 짜 (Date): 1993년08월26일(목) 15시39분15초 KDT 제 목(Title): RE) RE) Dictionary Server.. /* webster - look words up in the dictionary * * Words may be given on the command line, or, if no arguments are given, * the program runs interactively. * * In either mode, a word may include the wildcard characters '%' and '*'. * The '%' character matches exactly one character, while the '*' matches * zero or more characters. If wildcards are used, the program will * return either "No match" or a list of matching words. * * In interactive mode only, Tenex-style command completion may also be * used. Typing a '?' following part of a word will cause the program * to print all words which begin with the partial word, or the program * will beep if nothing matches. Typing an ESCape character causes the * program to attempt to complete the word. If the word can be completed, * the new word is printed; otherwise, the program beeps. Wildcards * may be used to specify the partial words. */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <signal.h> #include <sgtty.h> #include <netdb.h> #incluDe<ctype.h> #include <stdio.h> #ifndef WEBSTERHOST /* #define WEBSTERHOST "MINTAKA.LCS.MIT.EDU" #define WEBSTERHOST "18.26.0.36" #define WEBSTERHOST "moose.cs.indiana.edu" */ #define WEBSTERHOST "129.79.254.191" #endif WEBSTERHOST /* #define WEBSTERPORT 103 */ #define WEBSTERPORT 2627 #define BACKSPACE 010 #define DELCHAR 0177 #define WORDERASE 027 #define LINEERASE 030 #define LINERTYPE 022 #define COMPLETE 033 #define ENDINGS '?' #define EOFCH 0200 struct sgttyb sgttyb; struct sgttyb rsgttyb; FILE *WebsterSock; int interactive = 0; main(argc, argv) int argc; char **argv; { connectup(); if (argc > 1) { while (--argc) define(*++argv); exit(0); } setup(); interact(); } connectup() { register int s; struct sockaddr_in sin; register struct hostent *hp; struct hostent *gethostbyname(); char *hostname; if ((hostname = (char *)getenv("WEBSTERHOST")) == NULL) hostname = WEBSTERHOST; if ((hp = gethostbyname(hostname)) == NULL) { fprintf(stderr, "webster: %s: unknown host.\n", hostname); exit(1); } bzero(&sin, sizeof(struct sockaddr_in)); sin.sin_family = AF_INET; sin.sin_port = htons(WEBSTERPORT); bcopy(hp->h_addr, &sin.sin_addr, hp->h_length); if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("webster: socket"); exit(1); } if (connect(s, &sin, sizeof(struct sockaddr_in)) < 0) { perror("webster: connect"); exit(1); } WebsterSock = fdopen(s, "r"); } setup() { extern int byebye(); extern int suspend(); interactive = 1; ioctl(0, TIOCGETP, &sgttyb); rsgttyb = sgttyb; signal(SIGINT, byebye); signal(SIGQUIT, byebye); signal(SIGTSTP, suspend); sgttyb.sg_flags |= CBREAK; sgttyb.sg_flags &= ~ECHO; ioctl(0, TIOCSETP, &sgttyb); } interact() { char c; char buf[1024]; register char *s, *t; for (;;) { s = buf; write(1, "Word: ", 6); for (;;) { if (read(0, &c, 1) <= 0) byebye(); switch (c) { case BACKSPACE: case DELCHAR: if (s > buf) { write(1, "\b \b", 3); s--; } continue; case WORDERASE: while ((s > buf) && (*s != ' ')) { write(1, "\b \b", 3); s--; } continue; case LINEERASE: while (s > buf) { write(1, "\b \b", 3); s--; } continue; case LINERTYPE: write(1, "\r\nWord: ", 8); for (t=buf; t < s; t++) write(1, t, 1); continue; case COMPLETE: *s = NULL; complete(buf); for (s=buf; *s; s++) ; continue; case ENDINGS: if (s == buf) { help(); } else { *s = NULL; if (endings(buf) == 0) { write(1, "Word: ", 6); for (s=buf; *s; s++) write(1, s, 1); } continue; } break; case '\n': if (s == buf) byebye(); *s = NULL; write(1, "\n", 1); define(buf); break; default: write(1, &c, 1); *s++ = c; continue; } break; } } } define(word) char *word; { int c, refs; char buf[1024]; register char *s; sprintf(buf, "DEFINE %s\r\n", word); if (send(fileno(WebsterSock), buf, strlen(buf), 0) < 0) { perror("webster: send"); byebye(); } getline(buf); if (!strncmp(buf, "WILD 0", 6)) { printf("No match.\n"); return; } if (!strncmp(buf, "WILD", 4)) { printf("Possible matches are:\n"); listlines(0, 1); putchar('\n'); return; } if (!strncmp(buf, "SPELLING 0", 10)) { printf("No definition for '%s'.\n", word); return; } if (!strncmp(buf, "SPELLING", 8)) { printf("No definition for '%s'. Maybe you mean:\n", word); listlines(0, 1); putchar('\n'); return; } if (!strncmp(buf, "DEFINITION", 10)) { sscanf(buf+11, "%d", &refs); if (refs > 0) { printf("Cross references:\n"); listlines(refs, 1); putchar('\n'); } while ((c = getc(WebsterSock)) != EOF) { if (c == EOFCH) break; c &= 0177; putchar(c); } putchar('\n'); return; } while (((c = getc(WebsterSock)) != EOF) && (c != EOFCH)) ; } complete(word) char *word; { int c; char buf[1024]; register char *s; sprintf(buf, "COMPLETE %s\r\n", word); if (send(fileno(WebsterSock), buf, strlen(buf), 0) < 0) { perror("webster: send"); byebye(); } getline(buf); if (!strncmp(buf, "AMBIGUOUS", 9)) { write(1, "\007", 1); return; } if (!strncmp(buf, "COMPLETION", 10)) { for (s=word; *s; s++) write(1, "\b", 1); s = buf+11; while (((*s & 0177) != '\r') && ((*s & 0177) != '\n') && ((*s & 0177) != NULL)) { write(1, s, 1); s++; } *s = NULL; strcpy(word, buf+11); return; } while (((c = getc(WebsterSock)) != EOF) && (c != EOFCH)) ; } endings(word) char *word; { int c; char buf[1024]; register char *s; sprintf(buf, "ENDINGS %s\r\n", word); if (send(fileno(WebsterSock), buf, strlen(buf), 0) < 0) { perror("webster: send"); byebye(); } getline(buf); if (!strncmp(buf, "MATCHS 0", 8)) { write(1, "\007", 1); return(1); } if (!strncmp(buf, "MATCHS", 6)) { printf("\nMaybe you mean:\n"); listlines(0, 0); putchar('\n'); return(0); } while (((c = getc(WebsterSock)) != EOF) && (c != EOFCH)) ; return(0); } getline(s) register char *s; { register int c; while ((c = getc(WebsterSock)) != EOF) { if (c == EOFCH) return(0); c &= 0177; if (c == '\r') continue; if (c == '\n') break; *s++ = c; } *s = NULL; return(1); } listlines(n, num) register int n; int num; { char buf[1024]; register int col; printf(" "); if (n) { col = 0; while (n-- > 0) { getline(buf); putline(buf, num); if (++col == 3) { printf("\n "); col = 0; } } } else { col = 0; while (getline(buf) > 0) { putline(buf, num); if (++col == 3) { printf("\n "); col = 0; } } } if (col) putchar('\n'); } putline(buf, num) char *buf; int num; { int lnum; char line[1024]; sscanf(buf, "%d %[^\n]", &lnum, line); if (num) printf("%2d. %-22s", lnum, line); else printf("%-26s", line); } help() { printf("\n Type in the word you want defined, or a blank line to exit. Additionally,\n"); printf("Webster can match words using wildcards. The character '%%' in a word means\n"); printf("match exactly one character; while the character '*' means match zero or more\n"); printf("characters.\n"); printf(" Typing a partial word followed by '?' will print all the word s in the\n"); printf("dictionary which match your partial word. Typing a partial word followed by an\n"); printf("ESCape character will try to complete the word for you. If the partial word\n"); printf("is ambiguous, Webster will beep at you. Note that you can use t he wildcards\n"); printf("along with ESC and ?. For example (the underlined parts are typ ed by the\n"); printf("user, the rest by Webster),\n"); printf("\n"); printf("Word: balla? Maybe you mean:\n"); printf(" ------\n"); printf(" 1. ballad 2. ballade 3. baladry 4. ballast\n"); printf("Word: pluria<ESC>xial\n"); printf(" --------------\n"); printf("Word: plu*x<ESC>\n"); printf(" -------------\n"); printf("Word: pluriaxial\n"); printf("\n---- End of Help File ----\n\n"); } byebye() { /* * If interactive, reset the tty modes. */ if (interactive) ioctl(0, TIOCSETP, &rsgttyb); /* * Close the socket and exit. */ fclose(WebsterSock); write(1, "\n", 1); exit(0); } suspend() { long blocked; extern int suspend(); ioctl(0, TIOCSETP, &rsgttyb); signal(SIGTSTP, SIG_DFL); blocked = sigsetmask(0); kill(0, SIGTSTP); sigsetmask(blocked); signal(SIGTSTP, suspend); ioctl(0, TIOCSETP, &sgttyb); } |