home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / TEXI2IPF / ITEMS.C < prev    next >
Text File  |  1993-02-13  |  5KB  |  192 lines

  1. /*
  2.  * items.c - handles itemized list commands (formerly part of translate.c)
  3.  *           and @set / @clear tag lists
  4.  *
  5.  * texi2roff history:
  6.  *             Release 2.0     January 1990
  7.  *
  8.  * Copyright 1988, 1989, 1990  Beverly A.Erlebacher
  9.  * erlebach@cs.toronto.edu    ...uunet!utai!erlebach
  10.  *
  11.  * texi2ipf history:
  12.  *             Release 1.0     February 1993
  13.  *
  14.  * Modified by Marcus Gröber, Fido 2:2402/61.1
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "texi2ipf.h"
  21.  
  22. int  icount[MAXILEVEL];
  23. int  what[MAXILEVEL];
  24. char item[MAXILEVEL][MAXLINELEN];
  25.  
  26. struct {
  27.    char name[MAXTAGSIZE];
  28.    char *value;
  29. } tags[MAXTAG];                /* list of currently set tags */
  30. int tagnum=0;                  /* number ob tags set */
  31.  
  32. extern int  ilevel;
  33. extern char * gettoken();
  34. extern char * eatwhitespace();
  35. extern void errormsg();
  36. extern struct tablerecd * lookup();
  37.  
  38. /*
  39.  * itemize - handle the itemizing start commands @enumerate, @itemize
  40.  *     and @table
  41.  */
  42.  
  43. char * itemize(s, token)
  44. char * s;
  45. char * token;
  46. {
  47.     char *tag;
  48.  
  49.     tag = item[ilevel];
  50.     if (STREQ(token,"@itemize")) {
  51.        what[ilevel] = ITEMIZE;
  52.        s = gettoken(eatwhitespace(s),tag);
  53.        if (*tag == '\n') { /* this is an error in the input */
  54.            --s;
  55.            *tag = '-';
  56.            errormsg("missing itemizing argument ","");
  57.        } else {
  58.            if (*tag =='@') {
  59.                if ((lookup(tag)==NULL) && (lookup(strcat(tag,"{"))==NULL))
  60.                     errormsg("unrecognized itemizing argument ",tag);
  61.                else
  62.                    if (*(tag + strlen(tag) - 1) == '{')
  63.                        (void) strcat(tag,"}");
  64.            }
  65.        }
  66.        (void) strcat(tag, " ");
  67.     } else if (STREQ(token,"@enumerate")) {
  68.        what[ilevel] = ENUMERATE;
  69.        icount[ilevel] = 1;
  70.     } else if (STREQ(token,"@table")) {
  71.        what[ilevel] = TABLE;
  72.        s = gettoken(eatwhitespace(s),tag);
  73.        if (*tag == '\n') {
  74.            *tag = '\0';  /* do nothing special */
  75.            --s;
  76.        } else {
  77.            if (*tag =='@') {
  78.                if ((lookup(tag)==NULL) && (lookup(strcat(tag,"{"))==NULL))
  79.                    errormsg("unrecognized itemizing argument ",tag);
  80.                else {
  81.                    what[ilevel] = APPLY;
  82.                    if (*(tag + strlen(tag) - 1) != '{')
  83.                        (void) strcat(tag,"{");
  84.                }
  85.            }
  86.        }
  87.     }
  88.     while (*s != '\n' && *s != '\0')
  89.        ++s;  /* flush rest of line */
  90.     return s;
  91. }
  92.  
  93. /*
  94.  * doitem - handle @item and @itemx
  95.  */
  96.  
  97. char *
  98. doitem(s, tag, itemx)
  99. char * s;
  100. char *tag;
  101. int itemx;
  102. {
  103.         char *p;
  104.  
  105.     switch (what[ilevel]) {
  106.     case ITEMIZE:
  107.     case ENUMERATE:
  108.        *tag=0;
  109.        break;
  110.     case TABLE:
  111.        (void) strcpy(tag, itemx?"@br\n":"@_tag{pt}");
  112.        tag+=strlen(tag);
  113.        s = eatwhitespace(s);
  114.        if (*s == '\n') {
  115.            *tag++ = '-';
  116.            errormsg("missing table item tag","");
  117.        } else
  118.            while(*s != '\n')
  119.                *tag++ = *s++;
  120.        *tag = '\0';
  121.        break;
  122.     case APPLY:
  123.        (void) strcpy(p=tag, itemx?"@br\n":"@_tag{pt}");
  124.        (void) strcat(tag,item[ilevel]);
  125.        tag += strlen(tag);
  126.        s = eatwhitespace(s);
  127.        while(*s != '\n')
  128.            *tag++ = *s++;
  129.        *tag++ = '}';
  130.        *tag = '\0';
  131.        break;
  132.     }
  133.     return s;
  134. }
  135.  
  136. /*
  137.  * findtag - returns -1 if the tag has not been set,
  138.  *           or the index of the tag in the tag list.
  139.  */
  140. int
  141. findtag(str)
  142.         char *str;
  143. {
  144.         int i;
  145.  
  146.         for(i=0;i<tagnum;i++)
  147.            if(STREQ(str,tags[i].name))
  148.              return i;
  149.         return -1;
  150. }
  151.  
  152. char *
  153. value (str)
  154.         char *str;
  155. {
  156.         int i;
  157.  
  158.         if ((i=findtag(str))==-1) {
  159.            return NULL;
  160.         } else {
  161.            return tags[i].value;
  162.         } /* endif */
  163. }
  164.  
  165. /*
  166.  * setclear - sets (value!="") or clears (value=="") a tag
  167.  */
  168. void
  169. setclear(str,value)
  170.         char *str;
  171.         char *value;
  172. {
  173.         int i;
  174.  
  175.         if(*value) {                    /* SET tag */
  176.            if ((i=findtag(str)) == -1 && tagnum<MAXTAG) 
  177.               i=tagnum++;               /* Tag not found? Add tag */
  178.            else
  179.               free(tags[i].value);      /* Replace old tag value */
  180.            tags[i].value=malloc(strlen(value)+1);
  181.            strcpy(tags[i].name,str);
  182.            strcpy(tags[i].value,value);
  183.         }
  184.         else {                          /* CLEAR tag */
  185.            i=findtag(str);              /* tag set? */
  186.            if(i!=-1) {                  /* yes, remove */
  187.              free(tags[tagnum-1].value);
  188.              memcpy(&tags[i],&tags[--tagnum],sizeof(*tags));
  189.            }
  190.         }
  191. }
  192.