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

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

Webmaster