#include #include #include #include #include #include #include #include #include #include #define SERVER_IP "206.132.8.42" #define BUF_SIZE 102400 typedef struct _result { char *entry; char *word; char *pronun; char *func; char *body; int type; } RESULT; int create_sock () { int sock; struct sockaddr_in server; sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sock < 0) { printf("socket problem\n"); return 0; } server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(SERVER_IP); server.sin_port = htons(80); if ( connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0 ) { printf("connect problem\n"); return 0; } return sock; } int send_request (int sock, char *word) { int len; char request[512]; len = 20 + strlen(word); sprintf(request,"POST /cgi-bin/dictionary HTTP/1.0\n"); sprintf(request,"%sContent-type: application/x-www-form-urlencoded\n",request); sprintf(request,"%sContent-length: %i\n\n",request,len); sprintf(request,"%sbook=Dictionary&va=%s\n",request,word); printf("Looking up '%s'...\n",word); len = send(sock, request, strlen(request), 0); return len; } RESULT *new_result () { RESULT *r; r = malloc(sizeof(RESULT)); r->entry = NULL; r->word = NULL; r->pronun = NULL; r->func = NULL; r->body = NULL; r->type = 0; return r; } char *remove_ctrls (char **format, char *in) { int o=0; char *out, *curr, *temp; // printf("%i\n",strlen(in)); out = calloc(5120,sizeof(char)); curr = in; while (*curr) { switch (*curr) { case '\1': out[o++] = ''; out[o++] = '['; out[o++] = '0'; out[o++] = 'm'; break; case '\2': for (temp = format[0];*temp;temp++) out[o++] = *temp; break; case '\3': for (temp = format[1];*temp;temp++) out[o++] = *temp; break; case '\4': for (temp = format[2];*temp;temp++) out[o++] = *temp; break; default: out[o++] = *curr; break; } curr++; } return out; } void print_result (RESULT *result, char **format, int d) { char blnk[] = "", *curr, *end; result->entry = remove_ctrls(format,result->entry); result->body = remove_ctrls(format,result->body); if (result->type == 1) { result->pronun = remove_ctrls(format,result->pronun); result->func = remove_ctrls(format,result->func); printf("%s%s (%s%s%s) %s%s%s\n",result->entry,blnk,format[2], result->pronun,blnk,format[1],result->func,blnk); } else if (result->type == 2) { printf("%s%s (%s%s%s)\n",result->entry,blnk,format[2], result->word,blnk); } else printf("Spelling suggestions for '%s%s':\n",result->word,blnk); // printf("d=%i\n",d); curr = result->body; while (curr && *curr) { end = strstr(curr,"\n"); if (!end) { strcat(curr,"\n"); continue; } *end = 0; if (d > 0) printf("%s\n",curr); *end = '\n'; curr = strstr(curr,"\n"); if (!curr) continue; curr++; d--; } printf("%s",blnk); if (d < 0) printf("%s(%s%i%s entries remaining%s)%s\n",format[1],blnk, abs(d),blnk,format[1],blnk); } int clean_word (char *word) { while(*word) { if (!isalpha(*word)) return 0; word++; } return 1; } char *clean_html (char *in) { int t, o; char *out, *curr; t = o = 0; out = calloc(strlen(in)+1,sizeof(char)); curr = in; while(*curr) { switch (*curr) { case '<': if (!strncmp(curr,"
",4)) out[o++] = '\n'; else if (!strncmp(curr,"",3)) out[o++] = '\2'; else if (!strncmp(curr,"",4)) out[o++] = '\1'; else if (!strncmp(curr,"",3)) out[o++] = '\3'; else if (!strncmp(curr,"",4)) out[o++] = '\1'; else if (!strncmp(curr,"",4)) out[o++] = '\4'; else if (!strncmp(curr,"",5)) out[o++] = '\1'; else if (!strncmp(curr,"
",4)) out[o++] = '\n'; else if (!strncmp(curr,"",5)) { curr = strstr(curr,""); curr--; break; } t++; break; case '>': t--; break; case '&': if (!strncmp(curr,"&",5)) { out[o++] = '&'; curr += 4; } else if (!strncmp(curr,"<",4)) { out[o++] = '<'; curr += 3; } else if (!strncmp(curr,">",4)) { out[o++] = '>'; curr += 3; } else if (!strncmp(curr,"&umml;",6)) { out[o++] = 'u'; curr += 5; } else if (!strncmp(curr,"ä",6)) { out[o++] = 'a'; curr += 5; } break; case '·': out[o++] = 'ù'; break; default: if (!t) { out[o++] = *curr; } break; } curr++; } return out; } char *clean_spelling (char *in, char **format) { int o=0; char *out, *curr, *end; out = calloc(1024,sizeof(char)); curr = strrchr(in,','); curr = 0; curr = in; while (*curr) { strcat((char *)&out[o],format[0]); o += strlen(format[0]); end = strstr(curr,","); if (!end) break; *end = 0; strcat((char *)&out[o],curr); o += strlen(curr); *end = ','; curr = strstr(curr,","); strcat((char *)&out[o],format[1]); o += strlen(format[1]); curr += 2; if (*curr) { out[o++] = ','; out[o++] = ' '; } } strcat((char *)&out[o],""); return out; } int clean_result (RESULT *result, char **format) { if (!(result->entry)) return 0; result->entry = clean_html(result->entry); result->body = clean_html(result->body); if (result->type == 1) { result->pronun = clean_html(result->pronun); result->func = clean_html(result->func); } else if (result->type > 2) result->body = clean_spelling(result->body,format); return 1; } int parse_raw (RESULT *result, char *parse) { char *work, *end; if (!result->entry) { work = strstr(parse,"Main Entry:"); if (work) { // printf("entry passed\n"); work = strstr(work,""); work += 3; end = strstr(work,""); *end = 0; result->entry = strdup(work); *end = '<'; return 0; } else { work = strstr(parse,"Suggestions for"); if (!work) return 0; result->type = 3; result->entry = strdup(result->word); result->body = calloc(1024,sizeof(char)); return 0; } } else if (!result->pronun && result->type < 2) { work = strstr(parse,"Pronunciation:"); if (work) { result->type = 1; // printf("pronun passed\n"); work = strstr(work,""); work += 4; end = strstr(work,""); *end = 0; result->pronun = strdup(work); *end = '<'; return 0; } else { work = strstr(parse,"Date:"); if (!work) return 0; result->type = 2; return 0; } } else if (!result->func && result->type == 1) { work = strstr(parse,"Function:"); if (!work) return 0; // printf("func passed\n"); work = strstr(work,""); work += 3; end = strstr(work,""); *end = 0; result->func = strdup(work); *end = '<'; return 0; } else if (!result->body && result->type < 3) { work = parse; // printf("parse='%s'\n",parse); if (strncmp(work,"",3) && strncmp(work,"",3)) return 0; // printf("body passed\n"); result->body = strdup(work); return 0; } else if (result->type > 2) { work = strstr(parse,""); if (work) return 1; work = strstr(parse,". "); work++; end = strstr(parse,"<"); *end = 0; strcat(result->body,work); strcat(result->body,", "); return 0; } else return 1; } int main (int argc, char **argv) { int sock, die=0, n, a, p=0, d=2; char *format[3], buf[1024], parse[5120]; RESULT *result; result = new_result(); /* format settings * format[0] == \2 which is * format[1] == \3 which is * format[2] == \4 which is */ if (argc < 2) { printf("usage: %s [#definitions] [colors]\n",argv[0]); exit(1); } else if (argc < 4) { format[0] = strdup(""); format[1] = strdup(""); format[2] = strdup(""); } else if (argc == 4) { char *temp, *curr; curr = argv[3]; temp = strstr(curr,"m"); temp++; *temp = 0; format[1] = strdup(curr); *temp = ':'; curr = temp; curr += 5; temp = strstr(curr,"m"); temp++; *temp = 0; format[2] = strdup(curr); *temp = ':'; curr = temp; curr++; temp = strstr(curr,"m"); temp++; *temp = 0; format[0] = strdup(curr); *temp = ':'; } // printf("%stest1 %stest2 %stest3\n",format[0],format[1],format[2]); if (argc > 2) d = atoi(argv[2]); if (d < 1) d = 2; result->word = strdup(argv[1]); if (!clean_word(result->word)) { printf("'%s' is not a word.\n",result->word); exit(1); } sock = create_sock (); if (send_request(sock,result->word) < 0) { printf("couldn't send data\n"); exit(1); } while (!die && (n = recv(sock, buf, 1023, 0))) { buf[n] = 0; for (a=0;aword); exit(1); } print_result(result,format,d); return 0; }