home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / texipf22.zip / texi2ipf / items.c < prev    next >
C/C++ Source or Header  |  1997-07-17  |  6KB  |  210 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.  * Modified by Martin "Herbert" Dietze, Email herbert@wiloyee.shnet.org
  17.  *
  18.  */
  19.  
  20. /*
  21.  * History:
  22.  *
  23.  * $Log: items.c,v $
  24.  * Revision 1.1.1.1  1997/07/17 13:49:26  HERBERT
  25.  * Texi2IPF 2.2 new import
  26.  *
  27.  * Revision 1.3  1997/01/15 12:18:18  herbert
  28.  * - Fixed a bug in eat_first_word() that made the program crash sometimes.
  29.  * - "@ignore" environments will now be handled like real comments.
  30.  *
  31.  * Revision 1.2  1996/12/17 15:14:21  herbert
  32.  * Only some cosmetic changes. The code looks still rather ugly to me :-)
  33.  *
  34.  * Revision 1.1.1.1  1996/12/02 12:10:01  herbert
  35.  * Texi2IPF 1.0
  36.  *
  37.  */
  38.  
  39. #include "texi2ipf.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. static char * id =
  45. "@(#)$Id: items.c,v 1.1.1.1 1997/07/17 13:49:26 HERBERT Exp $";
  46.  
  47. int  icount[MAXILEVEL];
  48. int  what[MAXILEVEL];
  49. char item[MAXILEVEL][MAXLINELEN];
  50.  
  51. struct {
  52.     char name[MAXTAGSIZE];
  53.     char *value;
  54. } tags[MAXTAG];                /* list of currently set tags */
  55. int tagnum=0;                  /* number ob tags set */
  56.  
  57. extern int  ilevel;
  58. char * gettoken( char *, char *);
  59. char * eatwhitespace( char *);
  60. void errormsg( char *, char *);
  61. struct tablerecd * lookup( char *);
  62.  
  63. /*
  64.  * itemize - handle the itemizing start commands @enumerate, @itemize
  65.  *     and @table
  66.  */
  67.  
  68. char * itemize( char*s, char *token)
  69. {
  70.     char *tag;
  71.  
  72.     tag = item[ilevel];
  73.     if ( STREQ( token, "@itemize") ) {
  74.         what[ilevel] = ITEMIZE;
  75.         s = gettoken( eatwhitespace( s), tag);
  76.         if ( *tag == '\n' ) { /* this is an error in the input */
  77.             --s;
  78.             *tag = '-';
  79.             errormsg( "missing itemizing argument ", "");
  80.         } else {
  81.             if ( *tag =='@' ) {
  82.                 if ( (lookup(tag)==NULL) && (lookup(strcat(tag,"{"))==NULL) )
  83.                     errormsg( "unrecognized itemizing argument ", tag);
  84.                 else
  85.                     if ( *(tag + strlen(tag) - 1) == '{' )
  86.                         (void) strcat( tag, "}");
  87.             }/* if */
  88.         }/* if */
  89.         (void) strcat( tag, " ");
  90.     } else if ( STREQ(token, "@enumerate") ) {
  91.         what[ilevel] = ENUMERATE;
  92.         icount[ilevel] = 1;
  93.     } else if ( STREQ( token, "@table") ) {
  94.         what[ilevel] = TABLE;
  95.         s = gettoken( eatwhitespace( s), tag);
  96.         if ( *tag == '\n' ) {
  97.             *tag = '\0';  /* do nothing special */
  98.             --s;
  99.         } else {
  100.             if ( *tag =='@' ) {
  101.                 if ( (lookup( tag) == NULL) 
  102.                      && (lookup( strcat( tag, "{")) == NULL) )
  103.                     errormsg( "unrecognized itemizing argument ", tag);
  104.                 else {
  105.                     what[ilevel] = APPLY;
  106.                     if ( *(tag + strlen(tag) - 1) != '{' )
  107.                         (void) strcat( tag, "{");
  108.                 }/* if */
  109.             }/* if */
  110.         }/* if */
  111.     }/* if */
  112.     while ( *s != '\n' && *s != '\0' )
  113.         ++s;  /* flush rest of line */
  114.     /* endwhile */
  115.     return s;
  116. }/* itemize() */
  117.  
  118. /*
  119.  * doitem - handle @item and @itemx
  120.  */
  121.  
  122. char * doitem( char *s, char *tag, int itemx)
  123. {
  124.     switch ( what[ilevel] ) {
  125.     case ITEMIZE:
  126.     case ENUMERATE:
  127.         *tag=0;
  128.         break;
  129.     case TABLE:
  130.         (void) strcpy( tag, (itemx ? "@br\n" : "@_tag{pt}"));
  131.         tag+=strlen( tag);
  132.         s = eatwhitespace( s);
  133.         if ( *s == '\n' ) {
  134.             *tag++ = '-';
  135.             errormsg( "missing table item tag", "");
  136.         } else
  137.             while( *s != '\n' )
  138.                 *tag++ = *s++;
  139.         *tag = '\0';
  140.         break;
  141.     case APPLY:
  142.         (void) strcpy( tag, (itemx ? "@br\n" : "@_tag{pt}"));
  143.         (void) strcat( tag, item[ilevel]);
  144.         tag += strlen( tag);
  145.         s = eatwhitespace( s);
  146.         while( *s != '\n' )
  147.             *tag++ = *s++;
  148.         *tag++ = '}';
  149.         *tag = '\0';
  150.         break;
  151.     }/* switch */
  152.     return s;
  153. }/* doitem() */
  154.  
  155. /*
  156.  * findtag - returns -1 if the tag has not been set,
  157.  *           or the index of the tag in the tag list.
  158.  */
  159. int findtag( char *str)
  160. {
  161.     int i;
  162.  
  163.     for( i = 0; i<tagnum; i++ )
  164.     if ( !strncmp( str, tags[i].name, strlen( tags[i].name)) )
  165.             return i;
  166.         /* endif */
  167.     /* endfor */
  168.     return -1;
  169. }/* findtag */
  170.  
  171. char * value ( char *str)
  172. {
  173.     int i;
  174.  
  175.     if ( (i = findtag( str)) == -1 ) {
  176.         return NULL;
  177.     } else {
  178.         return tags[i].value;
  179.     } /* endif */
  180. }/* value */
  181.  
  182. /*
  183.  * setclear - sets (value!="") or clears (value=="") a tag
  184.  */
  185. int setclear( char *str, char *val)
  186. {
  187.     int i;
  188.     if( *val ) {                    /* SET tag */
  189.         if ( (i=findtag( str)) == -1 && tagnum < MAXTAG )  
  190.             i=tagnum++;              /* Tag not found? Add tag */
  191.         else
  192.             free( tags[i].value);      /* Replace old tag value */
  193.         /* endif */
  194.         tags[i].value = malloc( strlen( val)+1);
  195.         if ( tags[i].value == NULL )
  196.         return -1;
  197.       /* endif */
  198.         strncpy( tags[i].name, str, MAXTAGSIZE);
  199.         strcpy( tags[i].value, val);
  200.     } else {
  201.                                       /* CLEAR tag */
  202.         i=findtag( str);              /* tag set? */
  203.         if( i!=-1 ) {                 /* yes, remove */
  204.             free( tags[tagnum-1].value);
  205.             memcpy( &tags[i], &tags[--tagnum], sizeof(*tags));
  206.         }/* if */
  207.     }/* if */
  208.     return 0;
  209. }/* setclear() */
  210.