home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libpics / lablpars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.3 KB  |  271 lines

  1.  
  2. /*  W3 Copyright statement 
  3. Copyright 1995 by: Massachusetts Institute of Technology (MIT), INRIA</H2>
  4.  
  5. This W3C software is being provided by the copyright holders under the
  6. following license. By obtaining, using and/or copying this software,
  7. you agree that you have read, understood, and will comply with the
  8. following terms and conditions: 
  9.  
  10. Permission to use, copy, modify, and distribute this software and its
  11. documentation for any purpose and without fee or royalty is hereby
  12. granted, provided that the full text of this NOTICE appears on
  13. <EM>ALL</EM> copies of the software and documentation or portions
  14. thereof, including modifications, that you make. 
  15.  
  16. <B>THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
  17. REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
  18. BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
  19. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
  20. THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
  21. THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
  22. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE
  23. OR DOCUMENTATION.
  24.  
  25. The name and trademarks of copyright holders may NOT be used
  26. in advertising or publicity pertaining to the software without
  27. specific, written prior permission.  Title to copyright in this
  28. software and any associated documentation will at all times remain
  29. with copyright holders. 
  30. */
  31. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  32. #include "xp.h"
  33. /* --- END added by mharmsen@netscape.com on 7/9/97 --- */
  34. /* --- BEGIN removed by mharmsen@netscape.com on 7/9/97 --- */
  35. /* #include "malloc.h" */
  36. /* --- END removed by mharmsen@netscape.com on 7/9/97 --- */
  37. /* #include "sysdep.h"  -- jhines 7/9/97 */
  38. #include "cslutils.h"
  39. #include "csll.h"    /* to define states in stateChange */
  40. #include "csparse.h"
  41.  
  42. int Total;
  43.  
  44. extern int ParseDebug;
  45.  
  46. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  47. extern int SEC_ERROR_NO_MEMORY;
  48. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  49.  
  50. PRIVATE
  51. CSError_t spit(char* text, CSLabel_t * pCSMR, BOOL closed)
  52. {
  53.     printf("%s %s\n", text, closed ? "closed" : "opened");
  54.     return CSDoMore_more;
  55. }
  56.  
  57. LabelTargetCallback_t targetCallback;
  58. StateRet_t targetCallback(CSLabel_t * pCSMR, CSParse_t * pCSParse, CSLLTC_t target, BOOL closed, void * pVoid)
  59. {
  60.     int change = closed ? -target : target;
  61.  
  62.     Total += change;
  63.     if (!ParseDebug)
  64.         printf("%3d ", change);
  65. /*    printf("%s %s (%d)\n", closed ? "  ending" : "starting", pCSParse->pParseState->note, closed ? -target : target); */
  66.     return StateRet_OK;
  67. }
  68.  
  69. /* LLErrorHandler_t parseErrorHandler; */
  70. StateRet_t parseErrorHandler(CSLabel_t * pCSLabel, CSParse_t * pCSParse, 
  71.                  const char * token, char demark, 
  72.                  StateRet_t errorCode)
  73. {
  74.     char space[256];
  75.     printf("%20s - %s:", pCSParse->pTargetObject->note, 
  76.        pCSParse->currentSubState == SubState_X ? "SubState_X" : 
  77.        pCSParse->currentSubState == SubState_N ? "SubState_N" : 
  78.        pCSParse->currentSubState == SubState_A ? "SubState_A" : 
  79.        pCSParse->currentSubState == SubState_B ? "SubState_B" : 
  80.        pCSParse->currentSubState == SubState_C ? "SubState_C" : 
  81.        pCSParse->currentSubState == SubState_D ? "SubState_D" : 
  82.        pCSParse->currentSubState == SubState_E ? "SubState_E" : 
  83.        pCSParse->currentSubState == SubState_F ? "SubState_F" : 
  84.        pCSParse->currentSubState == SubState_G ? "SubState_G" : 
  85.        pCSParse->currentSubState == SubState_H ? "SubState_H" : 
  86.        "???");
  87.     switch (errorCode) {
  88.         case StateRet_WARN_NO_MATCH:
  89.             if (token)
  90.             sprintf(space, "Unexpected token \"%s\".\n", token);
  91.         else
  92.             sprintf(space, "Unexpected lack of token.\n");
  93.             break;
  94.         case StateRet_WARN_BAD_PUNCT:
  95.             /* --- BEGIN corrected misspelling by mharmsen@netscape.com on 7/9/97 --- */
  96.             sprintf(space, "Unexpected punctuation \"%c\"", demark);
  97.             /* --- END corrected misspelling by mharmsen@netscape.com on 7/9/97 --- */
  98.         if (token)
  99.             printf("after token \"%s\".\n", token);
  100.         else
  101.             printf(".\n");
  102.             break;
  103.         case StateRet_ERROR_BAD_CHAR:
  104.             /* --- BEGIN corrected misspelling by mharmsen@netscape.com on 7/9/97 --- */
  105.             sprintf(space, "Unexpected character \"%c\" in token \"%s\".\n", 
  106.             *pCSParse->pParseContext->pTokenError, token);
  107.             /* --- END corrected misspelling by mharmsen@netscape.com on 7/9/97 --- */
  108.             break;
  109.         default:
  110.             sprintf(space, "Internal error: demark:\"%c\" token:\"%s\".\n", 
  111.             demark, token);
  112.             break;
  113.     }
  114.     printf(space);
  115. /*
  116.     CSLabel_dump(pCSMR);
  117.     HTTrace(pParseState->note);
  118. */
  119.   return errorCode;
  120. }
  121.  
  122. /* --- BEGIN changed by mharmsen@netscape.com on 7/9/97 --- */
  123. /* #if 1 */
  124. #if 0
  125. /* --- END changed by mharmsen@netscape.com on 7/9/97 --- */
  126. /* use this main to test input with a series of labels, each on a line. */
  127. int main(int argc, char** argv)
  128. {
  129.     char lineBuf[512];
  130.     CSParse_t * pCSParse = 0;
  131.     CSDoMore_t last = CSDoMore_done;
  132.     FILE * input;
  133.  
  134.     if (argc > 1) {
  135.         if ((input = fopen(argv[1], "r")) == NULL) {
  136.         printf("Couldn't open \"%s\".\n", argv[1]);
  137.         exit(1);
  138.     }
  139.     } else {
  140.     input = stdin;
  141.     }
  142.     if (argc > 2)
  143.         ParseDebug = 1;
  144.     pCSParse = CSParse_newLabel(&targetCallback, &parseErrorHandler);
  145.     while (fgets(lineBuf, sizeof(lineBuf), input)){
  146.         int len;
  147.     char * ptr;
  148.     for (ptr = lineBuf; *ptr; ptr++)
  149.         if (*ptr == ';') {
  150.             *ptr = 0;
  151.             break;
  152.         }
  153. /*    if (strchr(lineBuf, ';'))
  154.         *ptr = 0;
  155.         if (lineBuf[0] == ';')
  156.         continue; */
  157.         /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  158.     len = XP_STRLEN(lineBuf);
  159.         /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  160.     if (lineBuf[len - 1] == '\r' || lineBuf[len - 1] == '\n') {
  161.         lineBuf[len-- - 1] = 0;
  162.     }
  163.     if (!lineBuf[0]) {
  164.         if (last != CSDoMore_done)
  165.             printf("parsing end error\n");
  166.         if (pCSParse)
  167.             CSParse_deleteLabel(pCSParse);
  168.         pCSParse = CSParse_newLabel(&targetCallback, &parseErrorHandler);
  169.         last = CSDoMore_done;
  170.     } else {
  171.         printf("%s ", lineBuf); if (ParseDebug) printf("\n");
  172.             /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  173.         switch (last = CSParse_parseChunk(pCSParse, lineBuf, (int) XP_STRLEN(lineBuf), 0)) {
  174.         case CSDoMore_done:
  175.             printf("= %d - parsing end\n", Total);
  176.         break;
  177.         case CSDoMore_error:
  178.         printf("= %d - parsing error\n", Total);
  179.         exit (1);
  180.         case CSDoMore_more:
  181.         printf("\n");
  182.         break;
  183.         }
  184.             /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  185.     }
  186.     }
  187.     if (pCSParse)
  188.         CSParse_deleteLabel(pCSParse);
  189.     if (last != CSDoMore_done)
  190.         printf("parsing end error\n");
  191.     return (0);
  192. }
  193. /* --- BEGIN changed by mharmsen@netscape.com on 7/9/97 --- */
  194. /* #else */
  195. #endif
  196. #if 0
  197. /* --- END changed by mharmsen@netscape.com on 7/9/97 --- */
  198. /* use this main to test input of a label list spread out over multiple lines*/
  199. int main(int argc, char** argv)
  200. {
  201.     char lineBuf[512];
  202.     while (gets(lineBuf)){
  203.         CSParse_t * pCSParse;
  204.     if (lineBuf[0] == ';')
  205.         continue;
  206.     Total = 0;
  207.     printf("%s", lineBuf);
  208.         pCSParse = CSParse_newLabel(&targetCallback, &parseErrorHandler);
  209.         /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  210.         if (CSParse_parseChunk(pCSParse, lineBuf, 
  211.                    (int)XP_STRLEN(lineBuf), 0) != CSDoMore_done) {
  212.         printf("parsing end error\n");
  213.         break;
  214.     }
  215.         /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  216.         CSParse_deleteLabel(pCSParse);
  217.     printf("= %d\n", Total);
  218.     }
  219.     return (0);
  220. }
  221. #endif
  222.  
  223. #if 0 /* a not-needed but often useful sample implementation of HTTrace */
  224. int HTTrace(const char * fmt, ...)
  225. {
  226.     va_list pArgs;
  227.     va_start(pArgs, fmt);
  228.     return (vfprintf(stderr, fmt, pArgs));
  229. }
  230. #endif
  231. void * HTMemory_malloc (size_t size)
  232. {
  233.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  234.     return XP_ALLOC(size);
  235.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  236. }
  237.  
  238. void * HTMemory_calloc (size_t nobj, size_t size)
  239. {
  240.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  241.     return XP_CALLOC(nobj, size);
  242.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  243. }
  244.  
  245. void * HTMemory_realloc (void * p, size_t size)
  246. {
  247.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  248.     return XP_REALLOC(p, size);
  249.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  250. }
  251.  
  252. void HTMemory_free (void * ptr)
  253. {
  254.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  255.     XP_FREE(ptr);
  256.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  257. }
  258.  
  259. void HTMemory_outofmem (char * name, char * file, unsigned long line)
  260. {
  261.     HTTrace("%s:%ld failed allocation for \"%s\".\n\
  262. Program aborted.\n",
  263.          file, line, name);
  264.     /* --- BEGIN changed by mharmsen@netscape.com on 7/9/97 --- */
  265.     /* exit(1); */
  266.     XP_SetError( SEC_ERROR_NO_MEMORY );
  267.     return;
  268.     /* --- END changed by mharmsen@netscape.com on 7/9/97 --- */
  269. }
  270.  
  271.