Annotation of libwww/Library/src/SGML.c, revision 1.29

1.23      frystyk     1: /*                                                                      SGML.c
                      2: **     GENERAL SGML PARSER CODE
                      3: **
1.27      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.23      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
1.2       timbl       7: **     This module implements an HTStream object. To parse an
1.1       timbl       8: **     SGML file, create this object which is a parser. The object
1.2       timbl       9: **     is (currently) created by being passed a DTD structure,
                     10: **     and a target HTStructured oject at which to throw the parsed stuff.
1.1       timbl      11: **     
1.19      duns       12: **      6 Feb 93       Binary seraches used. Intreface modified.
                     13: **      8 Jul 94  FM   Insulate free() from _free structure element.
1.1       timbl      14: */
                     15: 
1.25      frystyk    16: /* Library include files */
                     17: #include "tcp.h"
1.1       timbl      18: #include "HTUtils.h"
1.25      frystyk    19: #include "HTString.h"
1.1       timbl      20: #include "HTChunk.h"
1.20      frystyk    21: #include "SGML.h"
1.1       timbl      22: 
1.2       timbl      23: #define INVALID (-1)
                     24: 
1.1       timbl      25: /*     The State (context) of the parser
                     26: **
1.2       timbl      27: **     This is passed with each call to make the parser reentrant
1.1       timbl      28: **
                     29: */
                     30: 
1.16      frystyk    31: 
1.2       timbl      32: 
                     33:        
                     34: /*             Element Stack
                     35: **             -------------
                     36: **     This allows us to return down the stack reselcting styles.
                     37: **     As we return, attribute values will be garbage in general.
                     38: */
                     39: typedef struct _HTElement HTElement;
                     40: struct _HTElement {
                     41:        HTElement *     next;   /* Previously nested element or 0 */
                     42:        HTTag*          tag;    /* The tag at this level  */
                     43: };
                     44: 
                     45: 
1.21      frystyk    46: typedef enum _sgml_state {
                     47:     S_text, S_literal, S_tag, S_tag_gap, 
                     48:     S_attr, S_attr_gap, S_equals, S_value, S_after_open,
                     49:     S_nl, S_nl_tago,
                     50:     S_ero, S_cro,
                     51: #ifdef ISO_2022_JP
                     52:     S_esc, S_dollar, S_paren, S_nonascii_text,
                     53: #endif
                     54:     S_squoted, S_dquoted, S_end, S_entity, S_junk_tag
                     55: } sgml_state;
                     56: 
                     57: 
1.2       timbl      58: /*     Internal Context Data Structure
                     59: **     -------------------------------
                     60: */
                     61: struct _HTStream {
                     62: 
                     63:     CONST HTStreamClass *      isa;            /* inherited from HTStream */
                     64:     
                     65:     CONST SGML_dtd             *dtd;
                     66:     HTStructuredClass  *actions;       /* target class  */
                     67:     HTStructured       *target;        /* target object */
                     68: 
1.1       timbl      69:     HTTag              *current_tag;
1.2       timbl      70:     int                current_attribute_number;
1.1       timbl      71:     HTChunk            *string;
                     72:     HTElement          *element_stack;
1.21      frystyk    73:     sgml_state         state;
1.2       timbl      74: #ifdef CALLERDATA                
1.1       timbl      75:     void *             callerData;
1.2       timbl      76: #endif
                     77:     BOOL present[MAX_ATTRIBUTES];      /* Flags: attribute is present? */
                     78:     char * value[MAX_ATTRIBUTES];      /* malloc'd strings or NULL if none */
                     79: } ;
                     80: 
                     81: 
                     82: #define PUTC(ch) ((*context->actions->put_character)(context->target, ch))
                     83: 
1.1       timbl      84: 
1.17      timbl      85: /*     Find Attribute Number
                     86: **     ---------------------
                     87: */
                     88: 
                     89: PUBLIC int SGMLFindAttribute ARGS2 (HTTag*, tag, CONST char *, s)
                     90: {
                     91:     attr* attributes = tag->attributes;
                     92: 
                     93:     int high, low, i, diff;            /* Binary search for attribute name */
                     94:     for(low=0, high=tag->number_of_attributes;
                     95:                high > low ;
                     96:                diff < 0 ? (low = i+1) : (high = i) )  {
                     97:        i = (low + (high-low)/2);
                     98:        diff = strcasecomp(attributes[i].name, s);
                     99:        if (diff==0) return i;                  /* success: found it */
                    100:     } /* for */
                    101:     
                    102:     return -1;
                    103: }
                    104: 
1.1       timbl     105: 
                    106: /*     Handle Attribute
                    107: **     ----------------
                    108: */
                    109: /* PUBLIC CONST char * SGML_default = "";   ?? */
                    110: 
1.21      frystyk   111: PRIVATE void handle_attribute_name ARGS2(HTStream *, context, CONST char *, s)
1.1       timbl     112: {
1.2       timbl     113: 
                    114:     HTTag * tag = context->current_tag;
                    115: 
1.17      timbl     116:     int i = SGMLFindAttribute(tag, s);
                    117:     if (i>=0) {
                    118:        context->current_attribute_number = i;
                    119:        context->present[i] = YES;
                    120:        if (context->value[i]) {
                    121:            free(context->value[i]);
                    122:            context->value[i] = NULL;
                    123:        }
                    124:        return;
                    125:     } /* if */
1.2       timbl     126:        
1.20      frystyk   127:     if (SGML_TRACE)
1.29    ! frystyk   128:        TTYPrint(TDEST, "SGML: Unknown attribute %s for tag %s\n",
1.2       timbl     129:            s, context->current_tag->name);
                    130:     context->current_attribute_number = INVALID;       /* Invalid */
1.1       timbl     131: }
                    132: 
                    133: 
                    134: /*     Handle attribute value
                    135: **     ----------------------
                    136: */
1.21      frystyk   137: PRIVATE void handle_attribute_value ARGS2(HTStream *, context, CONST char *, s)
1.1       timbl     138: {
1.2       timbl     139:     if (context->current_attribute_number != INVALID) {
                    140:        StrAllocCopy(context->value[context->current_attribute_number], s);
1.1       timbl     141:     } else {
1.29    ! frystyk   142:         if (SGML_TRACE) TTYPrint(TDEST, "SGML: Attribute value %s ignored\n", s);
1.1       timbl     143:     }
1.2       timbl     144:     context->current_attribute_number = INVALID; /* can't have two assignments! */
1.1       timbl     145: }
                    146: 
1.2       timbl     147: 
1.1       timbl     148: /*     Handle entity
                    149: **     -------------
                    150: **
                    151: ** On entry,
                    152: **     s       contains the entity name zero terminated
                    153: ** Bugs:
                    154: **     If the entity name is unknown, the terminator is treated as
                    155: **     a printable non-special character in all cases, even if it is '<'
                    156: */
1.21      frystyk   157: PRIVATE void handle_entity ARGS2(HTStream *, context, char, term)
1.1       timbl     158: {
1.2       timbl     159: 
1.3       timbl     160:     CONST char ** entities = context->dtd->entity_names;
1.1       timbl     161:     CONST char *s = context->string->data;
1.2       timbl     162:     
                    163:     int high, low, i, diff;
                    164:     for(low=0, high = context->dtd->number_of_entities;
                    165:                high > low ;
                    166:                diff < 0 ? (low = i+1) : (high = i))   {  /* Binary serach */
                    167:        i = (low + (high-low)/2);
                    168:        diff = strcmp(entities[i], s);  /* Csse sensitive! */
                    169:        if (diff==0) {                  /* success: found it */
                    170:            (*context->actions->put_entity)(context->target, i);
                    171:            return;
1.1       timbl     172:        }
                    173:     }
                    174:     /* If entity string not found, display as text */
1.20      frystyk   175:     if (SGML_TRACE)
1.29    ! frystyk   176:        TTYPrint(TDEST, "SGML: Unknown entity %s\n", s); 
1.2       timbl     177:     PUTC('&');
1.1       timbl     178:     {
                    179:        CONST char *p;
                    180:        for (p=s; *p; p++) {
1.2       timbl     181:            PUTC(*p);
1.1       timbl     182:        }
                    183:     }
1.2       timbl     184:     PUTC(term);
1.1       timbl     185: }
                    186: 
1.2       timbl     187: 
1.1       timbl     188: /*     End element
1.2       timbl     189: **     -----------
1.1       timbl     190: */
1.21      frystyk   191: PRIVATE void end_element ARGS2(HTStream *, context, HTTag *, old_tag)
1.1       timbl     192: {
1.29    ! frystyk   193:     if (SGML_TRACE) TTYPrint(TDEST, "SGML: End   </%s>\n", old_tag->name);
1.2       timbl     194:     if (old_tag->contents == SGML_EMPTY) {
1.29    ! frystyk   195:         if (SGML_TRACE) TTYPrint(TDEST,"SGML: Illegal end tag </%s> found.\n",
1.1       timbl     196:                old_tag->name);
                    197:        return;
                    198:     }
                    199:     while (context->element_stack)     {/* Loop is error path only */
                    200:        HTElement * N = context->element_stack;
                    201:        HTTag * t = N->tag;
                    202:        
                    203:        if (old_tag != t) {             /* Mismatch: syntax error */
                    204:            if (context->element_stack->next) { /* This is not the last level */
1.29    ! frystyk   205:                if (SGML_TRACE) TTYPrint(TDEST,
1.1       timbl     206:                "SGML: Found </%s> when expecting </%s>. </%s> assumed.\n",
                    207:                    old_tag->name, t->name, t->name);
                    208:            } else {                    /* last level */
1.29    ! frystyk   209:                if (SGML_TRACE) TTYPrint(TDEST,
1.1       timbl     210:                    "SGML: Found </%s> when expecting </%s>. </%s> Ignored.\n",
                    211:                    old_tag->name, t->name, old_tag->name);
                    212:                return;                 /* Ignore */
                    213:            }
                    214:        }
                    215:        
                    216:        context->element_stack = N->next;               /* Remove from stack */
                    217:        free(N);
1.2       timbl     218:        (*context->actions->end_element)(context->target,
                    219:                 t - context->dtd->tags);
1.1       timbl     220:        if (old_tag == t) return;  /* Correct sequence */
                    221:        
                    222:        /* Syntax error path only */
                    223:        
                    224:     }
1.29    ! frystyk   225:     if (SGML_TRACE) TTYPrint(TDEST,
1.1       timbl     226:        "SGML: Extra end tag </%s> found and ignored.\n", old_tag->name);
                    227: }
                    228: 
                    229: 
1.17      timbl     230: /*     Start an element
                    231: **     ----------------
1.1       timbl     232: */
1.21      frystyk   233: PRIVATE void start_element ARGS1(HTStream *, context)
1.1       timbl     234: {
                    235:     HTTag * new_tag = context->current_tag;
                    236:     
1.29    ! frystyk   237:     if (SGML_TRACE) TTYPrint(TDEST, "SGML: Start <%s>\n", new_tag->name);
1.2       timbl     238:     (*context->actions->start_element)(
                    239:        context->target,
                    240:        new_tag - context->dtd->tags,
                    241:        context->present,
1.3       timbl     242:        (CONST char**) context->value);  /* coerce type for think c */
1.2       timbl     243:     if (new_tag->contents != SGML_EMPTY) {             /* i.e. tag not empty */
1.1       timbl     244:        HTElement * N = (HTElement *)malloc(sizeof(HTElement));
                    245:         if (N == NULL) outofmem(__FILE__, "start_element");
                    246:        N->next = context->element_stack;
                    247:        N->tag = new_tag;
                    248:        context->element_stack = N;
                    249:     }
                    250: }
                    251: 
                    252: 
1.2       timbl     253: /*             Find Tag in DTD tag list
                    254: **             ------------------------
1.1       timbl     255: **
                    256: ** On entry,
1.2       timbl     257: **     dtd     points to dtd structire including valid tag list
                    258: **     string  points to name of tag in question
1.1       timbl     259: **
1.2       timbl     260: ** On exit,
                    261: **     returns:
1.7       timbl     262: **             NULL            tag not found
                    263: **             else            address of tag structure in dtd
1.2       timbl     264: */
1.11      timbl     265: PUBLIC HTTag * SGMLFindTag ARGS2(CONST SGML_dtd*, dtd, CONST char *, string)
1.2       timbl     266: {
                    267:     int high, low, i, diff;
                    268:     for(low=0, high=dtd->number_of_tags;
                    269:                high > low ;
                    270:                diff < 0 ? (low = i+1) : (high = i))   {  /* Binary serach */
                    271:        i = (low + (high-low)/2);
1.3       timbl     272:        diff = strcasecomp(dtd->tags[i].name, string);  /* Case insensitive */
1.2       timbl     273:        if (diff==0) {                  /* success: found it */
1.7       timbl     274:            return &dtd->tags[i];
1.2       timbl     275:        }
                    276:     }
1.7       timbl     277:     return NULL;
1.2       timbl     278: }
                    279: 
                    280: /*________________________________________________________________________
                    281: **                     Public Methods
1.1       timbl     282: */
                    283: 
1.2       timbl     284: 
                    285: /*     Could check that we are back to bottom of stack! @@  */
1.26      frystyk   286: PUBLIC int SGML_flush  ARGS1(HTStream *, context)
                    287: {
                    288:     while (context->element_stack) {
                    289:        HTElement *ptr = context->element_stack;
                    290:        if (SGML_TRACE)
1.29    ! frystyk   291:            TTYPrint(TDEST, "SGML........ Non-matched tag found: <%s>\n",
1.26      frystyk   292:                    context->element_stack->tag->name);
                    293:        context->element_stack = ptr->next;
                    294:        free(ptr);
                    295:     }
                    296:     return (*context->actions->flush)(context->target);
                    297: }
1.1       timbl     298: 
1.22      frystyk   299: PUBLIC int SGML_free  ARGS1(HTStream *, context)
1.8       timbl     300: {
1.26      frystyk   301:     int status;
1.14      frystyk   302:     int cnt;
1.15      frystyk   303:     while (context->element_stack) {    /* Make sure, that all tags are gone */
                    304:        HTElement *ptr = context->element_stack;
                    305: 
1.26      frystyk   306:        if (SGML_TRACE)
1.29    ! frystyk   307:            TTYPrint(TDEST, "SGML........ Non-matched tag found: <%s>\n",
1.26      frystyk   308:                    context->element_stack->tag->name);
1.15      frystyk   309:        context->element_stack = ptr->next;
                    310:        free(ptr);
                    311:     }
1.26      frystyk   312:     if ((status = (*context->actions->_free)(context->target)) != HT_OK)
                    313:        return status;
1.8       timbl     314:     HTChunkFree(context->string);
1.15      frystyk   315:     for(cnt=0; cnt<MAX_ATTRIBUTES; cnt++)               /* Leak fix Henrik 18/02-94 */
1.14      frystyk   316:        if(context->value[cnt])
                    317:            free(context->value[cnt]);
1.8       timbl     318:     free(context);
1.26      frystyk   319:     return HT_OK;
1.1       timbl     320: }
                    321: 
1.22      frystyk   322: PUBLIC int SGML_abort  ARGS2(HTStream *, context, HTError, e)
1.1       timbl     323: {
1.14      frystyk   324:     int cnt;
1.15      frystyk   325:     while (context->element_stack) {    /* Make sure, that all tags are gone */
                    326:        HTElement *ptr = context->element_stack;
1.26      frystyk   327:        if (SGML_TRACE)
1.29    ! frystyk   328:            TTYPrint(TDEST, "SGML........ Non-matched tag found: <%s>\n",
1.26      frystyk   329:                    context->element_stack->tag->name);
1.15      frystyk   330:        context->element_stack = ptr->next;
                    331:        free(ptr);
                    332:     }
1.8       timbl     333:     (*context->actions->abort)(context->target, e);
1.1       timbl     334:     HTChunkFree(context->string);
1.14      frystyk   335:     for(cnt=0; cnt<MAX_ATTRIBUTES; cnt++)              /* Leak fix Henrik 18/02-94 */
                    336:        if(context->value[cnt])
                    337:            free(context->value[cnt]);
1.1       timbl     338:     free(context);
1.26      frystyk   339:     return HT_ERROR;
1.1       timbl     340: }
                    341: 
1.2       timbl     342: 
1.1       timbl     343: /*     Read and write user callback handle
                    344: **     -----------------------------------
                    345: **
                    346: **   The callbacks from the SGML parser have an SGML context parameter.
                    347: **   These calls allow the caller to associate his own context with a
                    348: **   particular SGML context.
                    349: */
                    350: 
1.2       timbl     351: #ifdef CALLERDATA                
                    352: PUBLIC void* SGML_callerData ARGS1(HTStream *, context)
1.1       timbl     353: {
                    354:     return context->callerData;
                    355: }
                    356: 
1.2       timbl     357: PUBLIC void SGML_setCallerData ARGS2(HTStream *, context, void*, data)
1.1       timbl     358: {
                    359:     context->callerData = data;
                    360: }
1.2       timbl     361: #endif
1.1       timbl     362: 
1.26      frystyk   363: PUBLIC int SGML_character ARGS2(HTStream *, context, char,c)
1.1       timbl     364: 
                    365: {
1.2       timbl     366:     CONST SGML_dtd     *dtd    =       context->dtd;
1.1       timbl     367:     HTChunk    *string =       context->string;
                    368: 
                    369:     switch(context->state) {
1.18      timbl     370:     
                    371:     case S_after_open: /* Strip one trainling newline
                    372:                        only after opening nonempty element.  - SGML:Ugh! */
                    373:         if (c=='\n' && (context->current_tag->contents != SGML_EMPTY)) {
                    374:            break;
                    375:        }
                    376:        context->state = S_text;
                    377:        goto normal_text;
                    378:        /* (***falls through***) */
                    379:        
1.1       timbl     380:     case S_text:
1.18      timbl     381: normal_text:
                    382: 
1.13      timbl     383: #ifdef ISO_2022_JP
                    384:        if (c=='\033') {
                    385:            context->state = S_esc;
                    386:            PUTC(c);
                    387:            break;
                    388:        }
                    389: #endif /* ISO_2022_JP */
1.6       timbl     390:        if (c=='&' && (!context->element_stack || (
                    391:                         context->element_stack->tag  &&
                    392:                         ( context->element_stack->tag->contents == SGML_MIXED
                    393:                           || context->element_stack->tag->contents ==
                    394:                                                         SGML_RCDATA)
                    395:                        ))) {
1.1       timbl     396:            string->size = 0;
                    397:            context->state = S_ero;
                    398:            
                    399:        } else if (c=='<') {
                    400:            string->size = 0;
                    401:            context->state = (context->element_stack &&
1.13      timbl     402:                context->element_stack->tag  &&
                    403:                context->element_stack->tag->contents == SGML_LITERAL) ?
1.12      timbl     404:                                S_literal : S_tag;
1.18      timbl     405:        } else if (c=='\n') {   /* Newline - ignore if before tag end! */
                    406:            context->state = S_nl;
1.2       timbl     407:        } else PUTC(c);
1.1       timbl     408:        break;
1.13      timbl     409: 
1.18      timbl     410:     case S_nl:
                    411:         if (c=='<') {
                    412:            string->size = 0;
                    413:            context->state = (context->element_stack &&
                    414:                context->element_stack->tag  &&
                    415:                context->element_stack->tag->contents == SGML_LITERAL) ?
                    416:                                S_literal : S_nl_tago;
                    417:        } else {
                    418:            PUTC('\n');
                    419:            context->state = S_text;
                    420:            goto normal_text;
                    421:        }
                    422:        break;
                    423: 
                    424:     case S_nl_tago:            /* Had newline and tag opener */
                    425:         if (c != '/') {
                    426:            PUTC('\n');         /* Only ignore newline before </ */
                    427:        }
                    428:        context->state = S_tag;
                    429:        goto handle_S_tag;
                    430: 
1.13      timbl     431: #ifdef ISO_2022_JP
                    432:     case S_esc:
                    433:        if (c=='$') {
                    434:            context->state = S_dollar;
                    435:        } else if (c=='(') {
                    436:            context->state = S_paren;
                    437:        } else {
                    438:            context->state = S_text;
                    439:        }
                    440:        PUTC(c);
                    441:        break;
                    442:     case S_dollar:
                    443:        if (c=='@' || c=='B') {
                    444:            context->state = S_nonascii_text;
                    445:        } else {
                    446:            context->state = S_text;
                    447:        }
                    448:        PUTC(c);
                    449:        break;
                    450:     case S_paren:
                    451:        if (c=='B' || c=='J') {
                    452:            context->state = S_text;
                    453:        } else {
                    454:            context->state = S_text;
                    455:        }
                    456:        PUTC(c);
                    457:        break;
                    458:     case S_nonascii_text:
                    459:        if (c=='\033') {
                    460:            context->state = S_esc;
                    461:            PUTC(c);
                    462:        } else {
                    463:            PUTC(c);
                    464:        }
                    465:        break;
                    466: #endif /* ISO_2022_JP */
1.1       timbl     467: 
1.12      timbl     468: /*     In literal mode, waits only for specific end tag!
1.2       timbl     469: **     Only foir compatibility with old servers.
1.1       timbl     470: */
1.12      timbl     471:     case S_literal :
1.1       timbl     472:        HTChunkPutc(string, c);
                    473:        if ( TOUPPER(c) != ((string->size ==1) ? '/'
                    474:                : context->element_stack->tag->name[string->size-2])) {
                    475:            int i;
                    476:            
1.12      timbl     477:            /*  If complete match, end literal */
1.1       timbl     478:            if ((c=='>') && (!context->element_stack->tag->name[string->size-2])) {
                    479:                end_element(context, context->element_stack->tag);
                    480:                string->size = 0;
1.2       timbl     481:                context->current_attribute_number = INVALID;
1.1       timbl     482:                context->state = S_text;
                    483:                break;
                    484:            }           /* If Mismatch: recover string. */
1.2       timbl     485:            PUTC( '<');
1.1       timbl     486:            for (i=0; i<string->size; i++)      /* recover */
1.2       timbl     487:               PUTC(
1.1       timbl     488:                                              string->data[i]);
                    489:            context->state = S_text;    
                    490:        }
                    491:        
                    492:         break;
                    493: 
                    494: /*     Character reference or Entity
                    495: */
                    496:    case S_ero:
                    497:        if (c=='#') {
                    498:            context->state = S_cro;  /*   &# is Char Ref Open */ 
                    499:            break;
                    500:        }
                    501:        context->state = S_entity;    /* Fall through! */
                    502:        
                    503: /*     Handle Entities
                    504: */
                    505:     case S_entity:
                    506:        if (isalnum(c))
                    507:            HTChunkPutc(string, c);
                    508:        else {
                    509:            HTChunkTerminate(string);
                    510:            handle_entity(context, c);
                    511:            context->state = S_text;
                    512:        }
                    513:        break;
                    514: 
                    515: /*     Character reference
                    516: */
                    517:     case S_cro:
                    518:        if (isalnum(c))
                    519:            HTChunkPutc(string, c);     /* accumulate a character NUMBER */
                    520:        else {
                    521:            int value;
                    522:            HTChunkTerminate(string);
                    523:            if (sscanf(string->data, "%d", &value)==1)
1.28      frystyk   524:                PUTC((char) value);
1.1       timbl     525:            context->state = S_text;
                    526:        }
                    527:        break;
                    528: 
                    529: /*             Tag
                    530: */         
                    531:     case S_tag:                                /* new tag */
1.18      timbl     532: handle_S_tag:
                    533: 
1.1       timbl     534:        if (isalnum(c))
                    535:            HTChunkPutc(string, c);
                    536:        else {                          /* End of tag name */
1.7       timbl     537:            HTTag * t;
1.1       timbl     538:            if (c=='/') {
1.20      frystyk   539:                if (SGML_TRACE) if (string->size!=0)
1.29    ! frystyk   540:                    TTYPrint(TDEST,"SGML:  `<%s/' found!\n", string->data);
1.1       timbl     541:                context->state = S_end;
                    542:                break;
                    543:            }
                    544:            HTChunkTerminate(string) ;
1.2       timbl     545: 
1.10      timbl     546:            t = SGMLFindTag(dtd, string->data);
1.7       timbl     547:            if (!t) {
1.29    ! frystyk   548:                if(SGML_TRACE) TTYPrint(TDEST, "SGML: *** Unknown element %s\n",
1.1       timbl     549:                        string->data);
                    550:                context->state = (c=='>') ? S_text : S_junk_tag;
                    551:                break;
                    552:            }
1.7       timbl     553:            context->current_tag = t;
1.2       timbl     554:            
                    555:            /*  Clear out attributes
                    556:            */
1.1       timbl     557:            
1.2       timbl     558:            {
                    559:                int i;
                    560:                for (i=0; i< context->current_tag->number_of_attributes; i++)
                    561:                    context->present[i] = NO;
1.1       timbl     562:            }
                    563:            string->size = 0;
1.2       timbl     564:            context->current_attribute_number = INVALID;
1.1       timbl     565:            
                    566:            if (c=='>') {
                    567:                if (context->current_tag->name) start_element(context);
1.18      timbl     568:                context->state = S_after_open;
1.1       timbl     569:            } else {
                    570:                context->state = S_tag_gap;
                    571:            }
                    572:        }
                    573:        break;
                    574: 
                    575:                
                    576:     case S_tag_gap:            /* Expecting attribute or > */
                    577:        if (WHITE(c)) break;    /* Gap between attributes */
                    578:        if (c=='>') {           /* End of tag */
                    579:            if (context->current_tag->name) start_element(context);
1.18      timbl     580:            context->state = S_after_open;
1.1       timbl     581:            break;
                    582:        }
                    583:        HTChunkPutc(string, c);
                    584:        context->state = S_attr;                /* Get attribute */
                    585:        break;
                    586:        
                    587:                                /* accumulating value */
                    588:     case S_attr:
                    589:        if (WHITE(c) || (c=='>') || (c=='=')) {         /* End of word */
                    590:            HTChunkTerminate(string) ;
                    591:            handle_attribute_name(context, string->data);
                    592:            string->size = 0;
                    593:            if (c=='>') {               /* End of tag */
                    594:                if (context->current_tag->name) start_element(context);
1.18      timbl     595:                context->state = S_after_open;
1.1       timbl     596:                break;
                    597:            }
                    598:            context->state = (c=='=' ?  S_equals: S_attr_gap);
                    599:        } else {
                    600:            HTChunkPutc(string, c);
                    601:        }
                    602:        break;
                    603:                
                    604:     case S_attr_gap:           /* Expecting attribute or = or > */
                    605:        if (WHITE(c)) break;    /* Gap after attribute */
                    606:        if (c=='>') {           /* End of tag */
                    607:            if (context->current_tag->name) start_element(context);
1.18      timbl     608:            context->state = S_after_open;
1.1       timbl     609:            break;
                    610:        } else if (c=='=') {
                    611:            context->state = S_equals;
                    612:            break;
                    613:        }
                    614:        HTChunkPutc(string, c);
                    615:        context->state = S_attr;                /* Get next attribute */
                    616:        break;
                    617:        
                    618:     case S_equals:                     /* After attr = */ 
                    619:        if (WHITE(c)) break;    /* Before attribute value */
                    620:        if (c=='>') {           /* End of tag */
1.29    ! frystyk   621:            if (SGML_TRACE) TTYPrint(TDEST, "SGML: found = but no value\n");
1.1       timbl     622:            if (context->current_tag->name) start_element(context);
1.18      timbl     623:            context->state = S_after_open;
1.1       timbl     624:            break;
                    625:            
                    626:        } else if (c=='\'') {
                    627:            context->state = S_squoted;
                    628:            break;
                    629: 
                    630:        } else if (c=='"') {
                    631:            context->state = S_dquoted;
                    632:            break;
                    633:        }
                    634:        HTChunkPutc(string, c);
                    635:        context->state = S_value;
                    636:        break;
                    637:        
                    638:     case S_value:
                    639:        if (WHITE(c) || (c=='>')) {             /* End of word */
                    640:            HTChunkTerminate(string) ;
                    641:            handle_attribute_value(context, string->data);
                    642:            string->size = 0;
                    643:            if (c=='>') {               /* End of tag */
                    644:                if (context->current_tag->name) start_element(context);
1.18      timbl     645:                context->state = S_after_open;
1.1       timbl     646:                break;
                    647:            }
                    648:            else context->state = S_tag_gap;
                    649:        } else {
                    650:            HTChunkPutc(string, c);
                    651:        }
                    652:        break;
                    653:                
                    654:     case S_squoted:            /* Quoted attribute value */
                    655:        if (c=='\'') {          /* End of attribute value */
                    656:            HTChunkTerminate(string) ;
                    657:            handle_attribute_value(context, string->data);
                    658:            string->size = 0;
                    659:            context->state = S_tag_gap;
                    660:        } else {
                    661:            HTChunkPutc(string, c);
                    662:        }
                    663:        break;
                    664:        
                    665:     case S_dquoted:            /* Quoted attribute value */
                    666:        if (c=='"') {           /* End of attribute value */
                    667:            HTChunkTerminate(string) ;
                    668:            handle_attribute_value(context, string->data);
                    669:            string->size = 0;
                    670:            context->state = S_tag_gap;
                    671:        } else {
                    672:            HTChunkPutc(string, c);
                    673:        }
                    674:        break;
                    675:        
                    676:     case S_end:                                        /* </ */
                    677:        if (isalnum(c))
                    678:            HTChunkPutc(string, c);
                    679:        else {                          /* End of end tag name */
1.7       timbl     680:            HTTag * t;
1.1       timbl     681:            HTChunkTerminate(string) ;
1.7       timbl     682:            if (!*string->data) {       /* Empty end tag */
                    683:                t = context->element_stack->tag;
                    684:            } else {
1.10      timbl     685:                t = SGMLFindTag(dtd, string->data);
1.1       timbl     686:            }
1.7       timbl     687:            if (!t) {
1.29    ! frystyk   688:                if(SGML_TRACE) TTYPrint(TDEST,
1.1       timbl     689:                    "Unknown end tag </%s>\n", string->data); 
1.2       timbl     690:            } else {
1.7       timbl     691:                context->current_tag = t;
1.2       timbl     692:                end_element( context, context->current_tag);
1.1       timbl     693:            }
1.2       timbl     694: 
1.1       timbl     695:            string->size = 0;
1.2       timbl     696:            context->current_attribute_number = INVALID;
1.7       timbl     697:            if (c!='>') {
1.20      frystyk   698:                if (SGML_TRACE && !WHITE(c))
1.29    ! frystyk   699:                    TTYPrint(TDEST,"SGML:  `</%s%c' found!\n",
1.7       timbl     700:                        string->data, c);
                    701:                context->state = S_junk_tag;
                    702:            } else {
                    703:                context->state = S_text;
                    704:            }
1.1       timbl     705:        }
                    706:        break;
                    707: 
                    708:                
                    709:     case S_junk_tag:
                    710:        if (c=='>') {
                    711:            context->state = S_text;
                    712:        }
                    713:     } /* switch on context->state */
1.26      frystyk   714:     return HT_OK;
                    715: }
1.2       timbl     716: 
                    717: 
1.26      frystyk   718: PUBLIC int SGML_string ARGS2(HTStream *, context, CONST char*, s)
1.2       timbl     719: {
1.26      frystyk   720:     while (*s)
                    721:         SGML_character(context, *s++);
                    722:     return HT_OK;
1.2       timbl     723: }
                    724: 
                    725: 
1.26      frystyk   726: PUBLIC int SGML_write ARGS3(HTStream *, context, CONST char*, b, int, l)
1.2       timbl     727: {
1.26      frystyk   728:     while (l-- > 0)
                    729:         SGML_character(context, *b++);
                    730:     return HT_OK;
1.2       timbl     731: }
                    732: 
                    733: /*_______________________________________________________________________
                    734: */
                    735: 
                    736: /*     Structured Object Class
                    737: **     -----------------------
                    738: */
                    739: PUBLIC CONST HTStreamClass SGMLParser = 
                    740: {              
                    741:        "SGMLParser",
1.26      frystyk   742:        SGML_flush,
1.2       timbl     743:        SGML_free,
1.8       timbl     744:        SGML_abort,
1.9       timbl     745:        SGML_character, 
                    746:        SGML_string,
                    747:        SGML_write,
1.2       timbl     748: }; 
                    749: 
                    750: /*     Create SGML Engine
                    751: **     ------------------
                    752: **
                    753: ** On entry,
                    754: **     dtd             represents the DTD, along with
                    755: **     actions         is the sink for the data as a set of routines.
                    756: **
                    757: */
                    758: 
                    759: PUBLIC HTStream* SGML_new  ARGS2(
                    760:        CONST SGML_dtd *,       dtd,
                    761:        HTStructured *,         target)
                    762: {
                    763:     int i;
                    764:     HTStream* context = (HTStream *) malloc(sizeof(*context));
                    765:     if (!context) outofmem(__FILE__, "SGML_begin");
                    766: 
                    767:     context->isa = &SGMLParser;
                    768:     context->string = HTChunkCreate(128);      /* Grow by this much */
                    769:     context->dtd = dtd;
                    770:     context->target = target;
                    771:     context->actions = (HTStructuredClass*)(((HTStream*)target)->isa);
                    772:                                        /* Ugh: no OO */
                    773:     context->state = S_text;
                    774:     context->element_stack = 0;                        /* empty */
                    775: #ifdef CALLERDATA                
                    776:     context->callerData = (void*) callerData;
                    777: #endif    
                    778:     for(i=0; i<MAX_ATTRIBUTES; i++) context->value[i] = 0;
                    779: 
                    780:     return context;
                    781: }

Webmaster