home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / cmfld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-19  |  3.9 KB  |  135 lines

  1. /*
  2.  Author: Andrew Lowry
  3.  
  4.  Columbia University Center for Computing Activities, July 1986.
  5.  Copyright (C) 1986, 1987, Trustees of Columbia University in the City
  6.  of New York.  Permission is granted to any individual or institution
  7.  to use, copy, or redistribute this software so long as it is not sold
  8.  for profit, provided this copyright notice is retained.
  9. */
  10. /* cmfld
  11. **
  12. ** Code to parse "arbitrary fields."  This function never really
  13. ** fails.  When a parse is requested, the current input is scanned
  14. ** until a character that is not allowed by the break table is
  15. ** encountered.  If no such terminating character is found, an
  16. ** incomplete parse is signaled.  Otherwise, the text up to but
  17. ** not including that character is returned via the atom buffer.
  18. ** Note that the returned text may be empty.  Full completion always
  19. ** adds a space unless spaces are allowed by the break table.  Partial
  20. ** completion always beeps.
  21. **
  22. ** The default break table allows words beginning with a letter
  23. ** and consisting of letters, digits, and hyphens.  The standard
  24. ** help message reflects this default.
  25. **/
  26.  
  27. #define    FLDERR            /* cmfld error table allocated here */
  28.  
  29. #include "ccmd.h"        /* get standard symbols */
  30. #include "cmfncs.h"        /* and internal symbols */
  31.  
  32. /* Forward declaration of handler routines */
  33.  
  34. int fldprs(), fldhlp(), fldcplt();
  35.  
  36. static brktab fldbrk = {
  37.   {                /* letters only in first position */
  38.     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  39.     0x80, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x1f
  40.   },
  41.   {                /* letters, digits and hyphens here */
  42.     0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0x00, 0x3f,
  43.     0x80, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x1f
  44.   }
  45. };
  46.  
  47. ftspec ft_fld = { fldprs, fldhlp, fldcplt, 0, &fldbrk }; /* handler structure */
  48.  
  49.  
  50.  
  51. /* fldprs - Find a terminating character.  If there is one, succeed with
  52. ** the text up to the terminator copied to the atom buffer.  Otherwise,
  53. ** give incomplete parse.
  54. **/
  55.  
  56. PASSEDSTATIC int
  57. fldprs(text,textlen,fdbp,parselen,value)
  58. char *text;
  59. int textlen,*parselen;
  60. fdb *fdbp;
  61. pval *value;
  62. {
  63.   brktab *btab;                /* break table in effect */
  64.   int fldlen;                /* length of field up to terminator */
  65.  
  66.   if ((btab = fdbp->_cmbrk) == NULL)    /* get custom break table */
  67.     btab = &fldbrk;            /* or use default */
  68.  
  69.   for (fldlen = 0; fldlen < textlen; fldlen++) /* loop through data */
  70.     if ((text[fldlen] & CC_QUO) == 0)    /* quoted chars never terminate */
  71.       if (fldlen == 0) {
  72.     int temp = text[fldlen]&CC_CHR;
  73.     if (BREAK1(btab,temp)) {
  74.       break;            /* found a terminator */
  75.     }
  76.       }
  77.       else {
  78.     int temp = text[fldlen]&CC_CHR;
  79.     if (BREAKR(btab,temp)) {
  80.       break;            /* found a terminator */
  81.     }
  82.       }
  83.  
  84.   if (fldlen == textlen)        /* no terminator found */
  85.     return(CMxINC);            /* so incomplete parse */
  86.   if (fdbp->_cmffl & FLD_EMPTY)
  87.       if (fldlen == 0)
  88.       return(FLDxNM);
  89.   *parselen = fldlen;            /* else set length of field */
  90.   value->_pvstr = cmcsb._cmabp;        /* return string in atom buffer */
  91.   return(CMxOK);            /* good parse */
  92. }
  93.  
  94.  
  95.  
  96. /* fldhlp - Default help assumes a word is being parsed */
  97.  
  98. PASSEDSTATIC int
  99. fldhlp(text,textlen,fdbp,cust)
  100. char *text;
  101. int textlen,cust;
  102. fdb *fdbp;
  103. {
  104.   cmxputs("word");
  105.   return(CMxOK);
  106. }
  107.  
  108.  
  109.  
  110. /* fldcplt - Full completion adds a space unless it would be taken as
  111. ** part of the field data, partial completion always beeps.
  112. **/
  113.  
  114. PASSEDSTATIC int
  115. fldcplt(text,textlen,fdbp,full,cplt,cpltlen)
  116. char *text,**cplt;
  117. int textlen,full,*cpltlen;
  118. fdb *fdbp;
  119. {
  120.   brktab *btab;            /* break table in effect */
  121.   *cplt = NULL;            /* never any completion text */
  122.   if (textlen == 0)
  123.     return(CMP_BEL);        /* beep at no text */
  124.   if (full) {            /* full completion requetsed */
  125.     if ((btab = fdbp->_cmbrk) == NULL) /* get supplied break table */
  126.       btab = &fldbrk;        /* or use default */
  127.     if (BREAK(btab,SPACE,textlen)) /* would a space break here? */
  128.       return(CMP_SPC | CMP_GO);    /* yup, tack on a space and wake up */
  129.     else
  130.       return(CMP_BEL);        /* otherwise beep */
  131.   }
  132.   else
  133.     return(CMP_BEL);        /* partial completion always beeps */
  134. }
  135.