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