home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / sgml / unix / sgmlc / sgmlsubs.c < prev    next >
Encoding:
Text File  |  1991-07-03  |  12.2 KB  |  268 lines

  1. /******************************************************************************/
  2. /* SGMLSUBS: Subroutines for SGML APIs.  Must be "included" in SGMLAPI?.C.    */
  3. /******************************************************************************/
  4. void alset(struct ad *);      /* Set up to start processing att list. */
  5. void setndata(PNE);           /* Build ext data entity rcbde with entity text.*/
  6. /******************************************************************************/
  7. static struct ad *alptrsv;    /* Save pending (tag or link) att list.        */
  8. static UNCH *docentid;        /* System identifier of SGML document entity.  */
  9. /******************************************************************************/
  10. /* ALSET: Prepares for ALNEXT processing of tag or data attribute list.
  11. */
  12. VOID alset(al)
  13. struct ad *al;                /* Attribute list (NULL=tag or link atts). */
  14. {
  15.      if (al) {                /* Non-null means data attributes. */
  16.           alptrsv = alptr;    /* Save pending (tag or link) att list. */
  17.           alptr = al;         /* Data attribute list is now current. */
  18.           te.pca = &de.ca;    /* Current attribute structure in rcbde. */
  19.      }
  20.      CA.aln = 0;              /* Index of attribute to process. */
  21.      CA.idrn = 0;             /* No IDREF(S) tokens as yet. */
  22.      CA.entityn = 0;          /* No AENTITY(S) tokens as yet. */
  23. }
  24. /******************************************************************************/
  25. /* ALNEXT: Transfers next attribute list entry to global variables.
  26.            Returns 1 if there is yet another entry; 0 if all done.
  27. */
  28. int alnext(void)
  29. {
  30.      if ((CA.aln += BITON(ADFLAGS(CA.aln), AGROUP) ? ADNUM(CA.aln)+1 : 1)>ADN) {
  31.           if (te.pca==&de.ca) {
  32.                alptr = alptrsv;    /* Restore pending (tag or link) att list. */
  33.                te.pca = &te.ca;    /* Current attribute structure in rcbte. */
  34.           }
  35.           return(0);
  36.      }
  37.      CA.aname = ADNAME(CA.aln);
  38.      CA.anamelen = *(CA.aname-1)-2;
  39.      CA.aflags = ADFLAGS(CA.aln);       /* For validator only. */
  40.      if (ADVAL(CA.aln)==NULL)           /* No value specified. */
  41.           CA.astatus = GET(CA.aflags, AINVALID) ? TPANOREQ : TPAIMPLY;
  42.      else {
  43.           CA.aval = ADVAL(CA.aln)+1;
  44.           CA.avallen = *(CA.aval-1)-2;
  45.           CA.aconref = (UNS)(GET(CA.aflags, ACONREF));
  46.           CA.adata = TPANONE;           /* Assume no special data. */
  47.           if (ADTYPE(CA.aln)<ATKNLIST) {/* Value is a string or token. */
  48.                CA.atype = (ADTYPE(CA.aln)==ACHARS) ? TPACDATA : TPATOKEN;
  49.                if (ADTYPE(CA.aln)==ANOTEGRP) {
  50.                     CA.adata = TPANOTE;
  51.                     CA.dcnid = ((char *)ADDATA(CA.aln).x)+1;
  52.                     CA.dcnidlen = *(CA.dcnid)-2;
  53.                }
  54.                else if (ADTYPE(CA.aln)==AENTITY) CA.adata = TPAENTIT;
  55.                else if (ADTYPE(CA.aln)==AIDREF)  CA.adata = TPAIDREF;
  56.           }
  57.           else {              /* Value is a list of one or more tokens. */
  58.                CA.atype = TPALIST;
  59.                CA.avn = 0;
  60.                CA.tn = 0;
  61.                if (ADTYPE(CA.aln)==AENTITYS) CA.adata = TPAENTIT;
  62.                else if (ADTYPE(CA.aln)==AIDREFS) CA.adata = TPAIDREF;
  63.           }
  64.           if (GET(CA.aflags, AERROR)) CA.astatus = TPAERROR;
  65.           else if (GET(CA.aflags, AINVALID)) CA.astatus = TPAINVAL;
  66.           else CA.astatus = TPAOK;      /* Specified attribute value is o.k. */
  67.      }
  68.      if (!TPisVM) CA.aflags = 0;        /* Flags are for validator only. */
  69.      return(1);
  70. }
  71. /******************************************************************************/
  72. /* AVNEXT: Transfers next attribute value token to global variables.
  73.            The number of tokens is in ADNUM(CA.aln).
  74.            Returns 1 if there is yet another entry; 0 if all done.
  75. */
  76. int avnext(void)
  77. {
  78.      if (++CA.tn>ADNUM(CA.aln)) return(0);
  79.      CA.tokenlen = *(CA.aval+CA.avn);
  80.      CA.token = CA.aval+CA.avn+1;
  81.      CA.avn += CA.tokenlen+1;
  82.      return(1);
  83. }
  84. /******************************************************************************/
  85. /* IDRNEXT: Sets idrstat global variable for current IDREF token.
  86. */
  87. VOID idrnext(void)
  88. {
  89.      CA.idrstat = (int)IDRHIT(++CA.idrn);
  90. }
  91. /******************************************************************************/
  92. /* GETENT: Sets up global variables for a data entity.
  93.            Returns:
  94.                3 if entity is internal SDATA;
  95.                2 if entity is internal CDATA;
  96.                1 if entity is NDATA;
  97.               -1 if entity is not defined;
  98.               -2 if not CDATA, SDATA, or NDATA;
  99.               -3 if entity name is too long.
  100. */
  101. int getent(
  102. UNS enmlen,                   /* Length of GI of element (without EOS). */
  103. UNCH *ename)                  /* GI of element (EOS optional). */
  104. {
  105.      if ((*pd = (char)enmlen+2)>NAMELEN+2) return(-3);
  106.      memcpy(pd+1, ename, enmlen);
  107.      *(pd+*pd-1) = EOS;
  108.      it.itnm = pd;             /* Entity name (len+EOS). */
  109.      it.ipbtype = ENTGET; sgml(itlpu);  /* Retrieve entity information. */
  110.      switch (src) {
  111.      case 1:                       /* External NDATA entity. */
  112.           setndata((PNE)it.itp1);
  113.           break;
  114.      case 2:                       /* Internal CDATA entity. */
  115.      case 3:                       /* Internal SDATA entity. */
  116.           de.detype = src-1;       /* Data entity type. */
  117.           de.ename = pd+1;         /* Current entity name. */
  118.           de.enamelen = *pd-2;
  119.           de.detxt = ((char *)it.itp1)+1;    /* Entity text. */
  120.           de.detxtlen = *(de.detxt-1)-2;
  121.           break;
  122.      }
  123.      return(src);                  /* Return with error code or entity type. */
  124. }
  125. /******************************************************************************/
  126. /* SETNDATA: Set system ID and notation for NDATA entity reference in
  127.              global variables for text processor use.
  128. */
  129. VOID setndata(
  130. PNE p)                        /* Pointer to data entity control block. */
  131. {
  132.      de.detype = NEXTYPE(p) + TPISDATA;   /* Data entity type. */
  133.      de.ename = NEENAME(p)+1;             /* Current entity name. */
  134.      de.enamelen = *(de.ename-1)-2;
  135.      de.detxt = ((char *)NEID(p))+1;      /* Entity system ID. */
  136.      de.detxtlen = *(de.detxt-1)-2;
  137.      de.dcnnm = NEDCN(p)+1;               /* Data content notation name. */
  138.      de.dcnnmlen = *(de.dcnnm-1)-2;
  139.      de.dcnid = ((char *)NEDCNID(p))+1;   /* Notation system ID. */
  140.      de.dcnidlen = *(de.dcnid-1)-2;
  141.      if (NEAL(p)) {           /* Data attributes will be processed. */
  142.           de.alcnt = AN;      /* Indicate number of attributes in list. */
  143.           alset(NEAL(p));     /* Initialize for alnext processing. */
  144.      }
  145.      else de.alcnt = 0;       /* No data attributes for this entity. */
  146. }
  147. /******************************************************************************/
  148. /* DOCENT: Initialize the SGML document entity.
  149.            Returns: 1 if entity system ID is too long; 0 if o.k.
  150. */
  151. int docent(
  152. UNCH *sysid)                  /* System identifier of document entity. */
  153. {
  154.      if ((*pd = (char)(strlen(sysid)+1))>LITLEN+1) return(1);
  155.      memcpy( pd+1 , sysid, (UNS)*pd );
  156.      ++*pd;
  157.      docentid = savestr(pd);  /* File identifier (len+EOS). */
  158.      return(0);
  159. }
  160. /******************************************************************************/
  161. /* GISET: Stores data for start- and end-tags of an element.
  162.           Returns 0 if o.k.; 1 if element is not defined.
  163. */
  164. int giset(
  165. UNCH *gi,                     /* GI of element. */
  166. UNIV start,                   /* Data for start-tag. */
  167. UNIV end)                     /* Data for end-tag. */
  168. {
  169.      it.itnm = gi;            /* Document type GI. */
  170.      it.itp1 = start;         /* GIDATA for start-tag. */
  171.      it.itp2 = end;           /* GIDATA for end-tag. */
  172.      it.ipbtype = GISET; sgml(itlpu);  /* Store GIDATA. */
  173.      return(src);             /* Report results. */
  174. }
  175. /******************************************************************************/
  176. /* SGMLOSET: Performs SGML initialization for option processing.
  177. */
  178. void sgmloset(void)
  179. {
  180.      te.passes = 1;           /* Number of passes requested. */
  181.      tpsw.swenttr = 1;        /* 1=trace entity stack in error messages; 0=no.*/
  182.      tpsw.swbufsz = readcnt+2;/* SGML file read buffer size. */
  183. }
  184. /******************************************************************************/
  185. /* SGMLOPT: Processes a TP option argument if it is one of the standard ones
  186.             supported by the interface.  They are:
  187.                   /d /e /f /g /mSGML.MSG /p1 /r /t3
  188.             Other arguments are returned intact to the TP, as they may
  189.             be valid arguments for the application.
  190. */
  191. UNCH *sgmlopt(
  192. UNCH *arg)                    /* Current argument in argv. */
  193. {
  194.           switch (toupper((int)arg[1])) {
  195.           case 'D':           /* Report duplicate entity declarations. */
  196.                tpsw.swdupent = 1;
  197.                return(0);
  198.           case 'E':           /* Suppress entity stack trace in error msg. */
  199.                tpsw.swenttr = 0;
  200.                return(0);
  201.           case 'F':           /* Files located in current directory only. */
  202.                cdirsw = 1;
  203.                return(0);
  204.           case 'G':           /* Suppress GI stack trace in error messages. */
  205.                eltrsv = 0;
  206.                return(0);
  207.           case 'M':           /* Message file identifier. */
  208.                msgfptr = &arg[2];
  209.                return(0);
  210.           case 'P':           /* Number of passes desired: 1 or more. */
  211.                if ((te.passes = atoi(&arg[2]))==0) break;
  212.                return(0);
  213.           case 'R':           /* Give warning for defaulted references. */
  214.                tpsw.swrefmsg = 1;
  215.                return(0);
  216.           case 'T':           /* Don't trim final RE of file or prefix RS. */
  217.                if ((boundsw = (int)arg[2])>'3' || boundsw<'0') break;
  218.                return(0);
  219.           }
  220.           return(arg);        /* Erroneous option argument. */
  221. }
  222. /******************************************************************************/
  223. /* SGMLSET: Performs SGML initialization and other housekeeping for the run.
  224. */
  225. void sgmlset(void)
  226. {
  227.      tpsw.swetdtag = 0;       /* Return on all tags, even if no user data. */
  228.      tpsw.delcdata = '\35';   /* 29 Delimiters: CDATA entity in att value. */
  229.      tpsw.delsdata = '\36';   /* 30 Delimiters: SDATA entity in att value. */
  230.      tpsw.delnonch = '\37';   /* 31 Prefix: non-SGML character. */
  231.      tpsw.addnonch = 64;      /* Addend: added to non-SGML char (mod 256). */
  232.      msginit();                    /* Load error messages. */
  233.      tpsw.src = &src;              /* TP addr for SGMLSERV to put return code.*/
  234.      tpsw.srcbp = &srcbp;          /* TP addr for SGML to put rcb ptr. */
  235.      itlpu = ⁢                  /* Create it ipb LPU to pass to SGML. */
  236.      it.itp1 = (UNIV)&tpsw;        /* Parser control switches. */
  237.      it.ipbtype = SGMLSET; sgml(itlpu);   /* Initialize the run. */
  238.      /* Pointer to markup string array was returned in srcbp. */
  239. }
  240. /******************************************************************************/
  241. /* SGMLPSET: Initialize the pass and open the SGML document entity.
  242.              Returns: <0 if entity could not be opened; 0 if o.k.
  243. */
  244. int sgmlpset(void)
  245. {
  246.      it.itl1 = te.pass;       /* Identify current pass: 1 to passes. */
  247.      it.ipbtype = SGMLRSET; sgml(itlpu);   /* Initialize the pass. */
  248.  
  249.      tpts = 0;                /* Initialize element structure stack. */
  250.      tpes = 1;                /* Initialize entity structure stack. */
  251.  
  252.      it.itl1 = (UNS)*docentid;
  253.      it.itp1 = docentid;      /* File identifier (len+EOS). */
  254.      it.ipbtype = ENTPRIME; sgml(itlpu);  /* Open document entity. */
  255.      return (src);
  256. }
  257. /******************************************************************************/
  258. /* SGMLEND: Performs SGML final cleanup and transfers capacity statistics
  259.             to a local structure.
  260. */
  261. UNS sgmlend(void)
  262. {
  263.      it.ipbtype = SGMLEND; sgml(itlpu);    /* Final cleanup. */
  264.      /* Pointer to statistics is now in srcbp: save it. */
  265.      return (cnterr);
  266. }
  267. /******************************************************************************/
  268.