home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part02 / webster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  4.6 KB  |  219 lines

  1. /*
  2.  * Pattern matching on words in a dictionary.
  3.  * Patterns are entered as command arguments and results are
  4.  * displayed in this window.
  5.  *
  6.  * Robert W. Baldwin, December 1984.
  7.  */
  8.  
  9.  
  10. #include    <stdio.h>
  11. #include    "window.h"
  12. #include    "terminal.h"
  13. #include    "layout.h"
  14. #include    "specs.h"
  15.  
  16.  
  17. #define    DICTNAME    "/usr/dict/words"
  18. #define    DICTVAR        "DICTIONARY"    /* Name of shell var. */
  19. #define    WEBHELP        "Word match is invoked using the 'lookup' command"
  20. #define    TOPINDEX    2
  21. #define    BOTINDEX    (WEBHEIGHT-2)
  22.  
  23. extern    int    wordsim();    /* Defined below. */
  24. extern        web_first();    /* Defined below. */
  25.  
  26. /* Strings for outlining the window.
  27.  */
  28. char    *webhead = "| Word Match";
  29. char    *webpane = "|";
  30. char    *webbot  = "`-------------------------";
  31.  
  32. /* Window for the list of matches. */
  33.  
  34. displine webaline[WEBHEIGHT];        /* Display lines for the history. */
  35. displine *weblines[WEBHEIGHT+1];    /* Pointers to them plus NULL. */
  36.  
  37. twindow  webster = {
  38.         WEBROW,WEBCOL,        /* Origin. */
  39.         WEBHEIGHT,WEBWIDTH,    /* Height and width. */
  40.         1,1,            /* Initial (relative) cursor pos. */
  41.         NULL,            /* No private data. */
  42.         web_first,        /* Firstime = accept cursor pos. */
  43.         wl_noop,        /* Lasttime = do nothing. */
  44.         wl_twdraw,        /* Default draw routine. */
  45.         dokey,            /* Default keystroke handler. */
  46.         arwktab,        /* Basic arrow keystroke handler. */
  47.         weblines,
  48. };
  49.  
  50.  
  51.  
  52. /* Make a window with a title and partial outline.
  53.  */
  54. gwindow *(iwebster())
  55. {
  56.     int        i;
  57.     displine    *line;
  58.  
  59.     webgraphics(webhead);
  60.     webgraphics(webpane);
  61.     webgraphics(webbot);
  62.  
  63.     for (i = 0 ; i < WEBHEIGHT ; i++)  {
  64.         line = &webaline[i];
  65.         line->worg_row = webster.worg_row + i;
  66.         line->worg_col = webster.worg_col;
  67.         line->wheight = 1;
  68.         line->wwidth = webster.wwidth;
  69.         line->wcur_row = 1;
  70.         line->wcur_col = 1;
  71.         line->wfirst = wl_noop;
  72.         line->wlast = wl_noop;
  73.         line->wredraw = wl_dldraw;
  74.         line->wkey = dokey;
  75.         line->wkeyprocs = arwktab;
  76.         line->dl_min_col = 2;
  77.         line->dl_max_col = webster.wwidth;
  78.         setadline(line, webpane);
  79.         webster.dlines[i] = line;
  80.         }
  81.     webster.dlines[i] = NULL;
  82.  
  83.     line = &webaline[0];
  84.     setadline(line, webhead);
  85.     line = &webaline[i - 1];
  86.     setadline(line, webbot);    /* Truncates to fit. */
  87.     return((gwindow *) &webster);
  88. }
  89.  
  90.  
  91. /* Convert a default graphic chars to fancy graphics chars
  92.  * in the given string.
  93.  */
  94. webgraphics(str)
  95. char    *str;
  96. {
  97.     for ( ; *str != NULL ; str++)  {
  98.         switch (*str)  {
  99.           default:
  100.             break;
  101.  
  102.           case '|':
  103.             *str = SVERTBAR;
  104.             break;
  105.  
  106.           case '-':
  107.             *str = SHORZBAR;
  108.             break;
  109.  
  110.           case '`':
  111.             *str = SLLCORNER;
  112.             break;
  113.         }
  114.     }
  115. }
  116.  
  117.  
  118. /* Command to lookup word pattern in dictionary.
  119.  * The pattern contains letters and dots.  The dots match any character.
  120.  */
  121. char *webmatch(args)
  122. char    *args;
  123. {
  124.     char    *p, *w;        /* Pattern and word pointers. */
  125.     char    patbuf[MAXWIDTH+1];
  126.     char    wordbuf[MAXWIDTH+1];
  127.     int    i;
  128.     char    *bufptr;
  129.     int    row,col;
  130.     FILE    *fd;
  131.     displine *line;
  132.     int    nextline;    /* Index of next line to put result. */
  133.     char    *dictfile;
  134.     extern    char    *getenv();
  135.  
  136.     row = rowcursor();
  137.     col = colcursor();
  138.  
  139.     if ((i = sscanf(args,"%*[^:]: %s", patbuf)) != 1) {
  140.         sprintf(statmsg, "Error, got %d args not 1. From: %s", i,args);
  141.         return(statmsg);    /* Beware: this returns a pointer to the stack. */
  142.         }
  143.     line = webster.dlines[TOPINDEX-1];
  144.     dlsetvar(line, patbuf);
  145.  
  146.     for (nextline = TOPINDEX ; nextline <= BOTINDEX ; nextline++) {
  147.         line = webster.dlines[nextline];
  148.         dlsetvar(line, "");
  149.         }
  150.     wl_draw(&webster);
  151.     setcursor(row, col);
  152.     fflush(stdout);
  153.  
  154.     dictfile = getenv(DICTVAR);
  155.     if (dictfile == NULL)
  156.           dictfile = DICTNAME;
  157.  
  158.     if ((fd = fopen(dictfile, "r")) == NULL)  {
  159.         sprintf(statmsg, "Could not open %s to read dictionary.",
  160.             dictfile);
  161.         return(statmsg);
  162.         }
  163.  
  164.     for (nextline = TOPINDEX ; nextline <= BOTINDEX ; nextline++) {
  165.         line = webster.dlines[nextline];
  166.         while (TRUE) {
  167.             bufptr = fgets(wordbuf, MAXWIDTH+1, fd);
  168.             if (bufptr != wordbuf) {
  169.                 wordbuf[0] = '\000';
  170.                 goto alldone;
  171.                 }
  172.             if (wordsim(patbuf, wordbuf)) break;
  173.             }
  174.         for (w=wordbuf ; *w != '\n' ; w++) {}
  175.         *w = '\000';
  176.         dlsetvar(line, wordbuf);
  177.         wl_draw(line);
  178.         setcursor(row, col);
  179.         fflush(stdout);
  180.         }
  181.  
  182. alldone:
  183.     fclose(fd);
  184.  
  185.     return(NULL);
  186. }
  187.  
  188.  
  189. /* Return TRUE if the pattern matches the word
  190.  */
  191. int    wordsim(pattern, word)
  192. char    *pattern;
  193. char    *word;
  194. {
  195.     for ( ; *pattern != 0 && *word != 0 ; ((int)pattern++)|((int)word++)) {
  196.         if (*word == '\n')
  197.               return(FALSE);
  198.         if (*pattern == '.')
  199.               continue;
  200.         if (*pattern != *word)
  201.               return(FALSE);
  202.         }
  203.     if (*pattern == 0 && *word == '\n')
  204.           return(TRUE); /* Same length */
  205.     return(FALSE);
  206. }
  207.  
  208.  
  209. /* Called when cursor enters the window.
  210.  * Clears the help message and accepts the cursor position.
  211.  */
  212. web_first(w, row, col)
  213. gwindow    *w;
  214. int        row, col;
  215. {
  216.     wl_setcur(w, row, col);
  217.     usrhelp(&user, WEBHELP);
  218. }
  219.