home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / cmtad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-19  |  5.9 KB  |  180 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. /* cmtad
  11. **
  12. ** This module contains routines for parsing time and date specifications.
  13. ** Parsing is performed by means of the dtparse routine from the datime
  14. ** module.  Flags from FDB field _cmffl are passed on to this routine
  15. ** to control the parse.
  16. **
  17. ** If the parse is incomplete and the dtparse routine supplies a
  18. ** completion text, full completion evokes that text along with a
  19. ** trailing space, while partial completion gives the text up to
  20. ** the first punctuation.
  21. **
  22. ** Help gives a message that depends only on whether the date or time
  23. ** have been suppressed from the parse.
  24. **
  25. ** The break table has no bearing on the parse, and is set to break on
  26. ** everything so that CM_WKF parsing will work.
  27. **/
  28.  
  29. #define TADERR            /* time/date error table allocated here */
  30.  
  31. #include "ccmdlib.h"        /* get standard symbols */
  32. #include "cmfncs.h"        /* and internal symbols */
  33.  
  34. /* Forward declaration of handler routines */
  35.  
  36. int tadprs(), tadhlp(), tadcplt();
  37.  
  38. #define tadbrk cmallbk        /* std break table breaks on everything */
  39.  
  40. ftspec ft_tad = { tadprs, tadhlp, tadcplt, 0, &tadbrk }; /* handler structure */
  41.  
  42. /* Global variable to hold completion text from parse attempt in
  43. ** case a completion request is forthcoming.
  44. **/
  45.  
  46. static char *tadcmp;
  47.  
  48.  
  49.  
  50. /* tadprs - Invoke the dtparse routine to attempt to parse the current
  51. ** input as a date/time specification.  Propagate an incomplete parse,
  52. ** and translate any other failing parse into an error codes that
  53. ** depends on flags passed for the parse.  Otherwise, the date/time info
  54. ** is returned via the _pvtad component of the value.  As stated in the
  55. ** comments for the dtparse routine, if the DTP_NTI flag is specified,
  56. ** indicating that no time should be parsed, the time info is set to one
  57. ** second past midnight, with local timezone and dst settings.  If the
  58. ** DTP_NDA flag is set, so no date is input, the current date is filled
  59. ** in.
  60. **
  61. **    * * * W A R N I N G -- S T R A N G E   C O D E * * *
  62. **
  63. ** Something strange is done with the `fail' flag here.  Specifically,
  64. ** in order to pass the input off to the dtparse routine, we need to
  65. ** strip all the quote flags from the characters.  If this parse fails,
  66. ** that could affect other alternative parse functions from the FDB
  67. ** chain.  Thus before returning a failure code, the input text has
  68. ** to be rebuilt.  Thus, when a match fails, we set our local failure
  69. ** flag and return CMxAGN, which cause the ccmd routines to rebuild
  70. ** the input and call us again.  At that point we return the real
  71. ** error code and reset our local flag.  (Yuck!)
  72. **/
  73.  
  74. PASSEDSTATIC int
  75. tadprs(text,textlen,fdbp,parselen,value)
  76. char *text;
  77. int textlen;
  78. fdb *fdbp;
  79. int *parselen;
  80. pval *value;
  81. {
  82.   static int fail=FALSE;        /* TRUE after text rebuild */
  83.   int inc,i;                /* TRUE after incomplete parse */
  84.   int tadflg = fdbp->_cmffl;        /* get parse flags */
  85.   datime *rettad = &value->_pvtad;    /* point to result datime block */
  86.  
  87.   if (fail) {                /* called again after rebuild? */
  88.     fail = FALSE;            /* yup, reset local flag */
  89.     if (tadflg & DTP_NTI)        /* and return appropriate error */
  90.       return(TADxDAT);
  91.     else if (tadflg & DTP_NDA)
  92.       return(TADxTIM);
  93.     else
  94.       return(TADxDT);
  95.   }
  96.  
  97.   if (tadflg & DTP_NDA)            /* check exclusions from parse */
  98.     if (tadflg & DTP_NTI)
  99.       return(TADxNTD);            /* cant exclude both time and date */
  100.  
  101.   for (i = 0; i < textlen; i++)        /* clear quote flags... */
  102.     text[i] &= CC_CHR;            /*  cuz dtparse wants normal chars */
  103.  
  104.                     /* attempt a parse */
  105.   if (!dtparse(tadflg,text,textlen,rettad,parselen,&tadcmp,&inc))
  106.     if (inc)                /* reached end of input? */
  107.       return(CMxINC);            /* then ask for more */
  108.     else {
  109.       fail = TRUE;            /* otherwise set up for failure */
  110.       return(CMxAGN);            /* after they rebuild the input */
  111.     }
  112.   else
  113.     return(CMxOK);            /* success!! */
  114. }
  115.  
  116.  
  117.  
  118. /* tadcplt - The tadcmp global variable should still be set according
  119. ** the the incomplete parse that caused this action, so we just make
  120. ** use of that string without going through the entire matching process
  121. ** again.  If the completion text is empty, we beep.  If the first
  122. ** character is a space, we return an empty completion and add a space
  123. ** and a wakeup for full completion.  Otherwise, the given completion
  124. ** text is returned with no wakeup for full completion, and stopping after
  125. ** punctuation for partial completion.
  126. **/
  127.  
  128. PASSEDSTATIC int
  129. tadcplt(text,textlen,fdbp,full,cplt,cpltlen)
  130. char *text,**cplt;
  131. int textlen,full,*cpltlen;
  132. fdb *fdbp;
  133. {
  134.   *cplt = NULL;                /* assume no completion */
  135.   if (tadcmp == NULL)
  136.     return(CMP_BEL);            /* beep if nothing available */
  137.   else if (*tadcmp == SPACE)
  138.     if (full)
  139.       return(CMP_SPC | CMP_GO);        /* wakeup for space on full */
  140.     else
  141.       return(CMP_BEL);            /* but beep with partial */
  142.   else {
  143.     *cplt = tadcmp;            /* real completion - fill it in */
  144.     *cpltlen = -1;            /* complete to end of string */
  145.     if (full)
  146.       return(CMP_SPC);            /* include space, no wakeup if full */
  147.     else
  148.       return(CMP_PNC);            /* only up to punctuaion on partial */
  149.   }
  150. }
  151.  
  152.  
  153.  
  154. /* tadhlp - Prints a set message selected according to the flags specified
  155. ** in the call.
  156. **/
  157.  
  158. PASSEDSTATIC int
  159. tadhlp(text,textlen,fdbp,cust)
  160. char *text;
  161. int textlen,cust;
  162. fdb *fdbp;
  163. {
  164.   int tadflg = fdbp->_cmffl;        /* get flags from FDB */
  165.  
  166.   if (tadflg & DTP_NDA)            /* check exclusions */
  167.     if (tadflg & DTP_NTI)
  168.       cmxputs("date and time (but both are suppressed?)"); /* strange */
  169.     else
  170.       cmxputs("time");            /* only date suppressed */
  171.   else
  172.     if (tadflg & DTP_NTI)
  173.       cmxputs("date");            /* only time suppressed */
  174.     else
  175.       cmxputs("date and time");        /* neither suppressed */
  176.  
  177.   return(CMxOK);
  178. }
  179.  
  180.