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

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