home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / texipf22.zip / texi2ipf / toolz.c < prev    next >
C/C++ Source or Header  |  1997-07-17  |  8KB  |  314 lines

  1. /* 
  2.  * toolz.c - Some string processing tools and stuff formerly in translat.c
  3.  *
  4.  * texi2roff history:
  5.  *             Release 1.0a    August 1988
  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: toolz.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/02/06 12:45:18  herbert
  28.  * - Added documentation in Texinfo format.
  29.  * - Minor bug fixes.
  30.  *
  31.  * Revision 1.2  1997/01/17 13:34:36  herbert
  32.  * Use remove_texicmds() when processing menu entries.
  33.  *
  34.  * Revision 1.1  1997/01/16 13:41:33  herbert
  35.  * Put stuff from translat.c to new file toolz.c.
  36.  * Fixed an error in the remove_texicmd() routine (correct handling of "@@",
  37.  * "@{" and "@}" tags).
  38.  *
  39.  *
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45. #include "texi2ipf.h"
  46.  
  47. static char * id =
  48. "@(#)$Id: toolz.c,v 1.1.1.1 1997/07/17 13:49:26 HERBERT Exp $";
  49.  
  50. extern int     linecount;
  51. extern char   *filename;
  52. char * gettoken( char *, char *);
  53.  
  54. /*  
  55.  * rtrim - as in dbase - remove trailing whitespaces and returns
  56.  */
  57. char * rtrim( char *s)
  58. {
  59.     int i;
  60.  
  61.     for( i = strlen( s) - 1; 
  62.          i >= 0 && ( s[i] == ' ' || s[i] == '\t' || s[i] == '\n' );
  63.          i-- )
  64.         s[i] = '\0';
  65.     /* endfor */
  66.     return s;
  67. }/* rtrim() */
  68.  
  69.  
  70. /*
  71.  * eatwhitespace - move input pointer to first char that isnt a blank or tab
  72.  *     (note that newlines are *not* whitespace)
  73.  */
  74.  
  75. char * eatwhitespace( char *s)
  76. {
  77.     if ( !s )
  78.         return NULL;
  79.     /* endif */
  80.     while( *s == ' ' || *s == '\t' )
  81.         ++s ;
  82.     /* endwhile */
  83.     return s;
  84. }/* eatwhitespace() */
  85.  
  86.  
  87. /* 
  88.  * errormsg - print error messages to stderr
  89.  */
  90.  
  91. void errormsg( char *message, char *other)
  92. {
  93.     (void) fprintf( stderr, "%s line %d : %s%s\n",
  94.                     filename, linecount, message, other);
  95. }/* errormsg() */
  96.  
  97. /*
  98.  * Remove all Texinfo commands starting with "@" from the string.
  99.  */
  100. void remove_texicmds( char * string)
  101. {
  102.     char tmpstr[MAXLINELEN];
  103.     char * strptr;
  104.     char * tmptr  = tmpstr;
  105.     strcpy( tmpstr, string);
  106.     *string = '\0';
  107.  
  108.     /*
  109.      * Remove all '{'. If "@{" then make '{' out of it.
  110.      */
  111.     tmptr = tmpstr;
  112.     while ( *tmptr ){
  113.         if ( *tmptr == '{' )
  114.             if ( tmptr > tmpstr )
  115.                 if ( *(tmptr-1) != '@' )
  116.                     *tmptr = ' ';
  117.                 else {
  118.                     tmptr--;
  119.                     *tmptr = '\0';
  120.                     strcat( tmpstr, tmptr+1);
  121.                 }/* if */
  122.         /* endif */
  123.         ++tmptr;
  124.     }/* while */
  125.  
  126.     /*
  127.      * Remove all '}'. If "@}" then make '}' out of it.
  128.      */
  129.     tmptr = tmpstr;
  130.     while ( *tmptr ){
  131.         if ( *tmptr == '}' ){
  132.             if ( tmptr > tmpstr )
  133.                 if ( *(tmptr-1) != '@' ){
  134.                     *tmptr = '\0';
  135.                     strcat( tmpstr, tmptr+1);
  136.                     continue;
  137.                 } else {
  138.                     *(tmptr-1) = '\0';
  139.                     strcat( tmpstr, tmptr);
  140.                 }/* if */
  141.             else{
  142.                 *tmptr = '\0';
  143.                 strcat( tmpstr, tmptr+1);
  144.                 continue;
  145.                 }/* if */
  146.         } /* if */
  147.         tmptr++;
  148.     }/* while */
  149.     
  150.     /*
  151.      * Now cat the copy of the string back to the string leaving out
  152.      *  all words starting with '@' but making '@' out of "@@".
  153.      */
  154.     tmptr = eatwhitespace( tmpstr);
  155.     strptr = tmptr;
  156.     while ( *tmptr ){
  157.         while ( *tmptr && *tmptr != '@' && !isspace( *tmptr) )
  158.             ++tmptr;
  159.         /* endwhile */
  160.         strncat( string, strptr, (unsigned long)tmptr - (unsigned long)strptr);
  161.         /*
  162.          * EOS? Exit.
  163.          */
  164.         if ( !*tmptr ){
  165.             return;
  166.         }/* if */
  167.         /*
  168.          * Now we're at the first whitespace.
  169.          * Append *one* whitespace and skip the rest.
  170.          */
  171.         if ( isspace( *tmptr) ){
  172.             strcat( string, " ");
  173.             while ( *tmptr && isspace( *tmptr) )
  174.                 ++tmptr;
  175.             /* endwhile */
  176.         }/* if */
  177.         /*
  178.          * Found '@'? Find its end (must be a whitespace).
  179.          */
  180.         if ( *tmptr == '@' ){
  181.             if ( *(tmptr+1) == '@' ){
  182.                 *tmptr = '\0';
  183.                 strcat( tmpstr, tmptr+1);
  184.                 strcat( string, "@");
  185.                 if ( isspace( *(++tmptr)) )
  186.                     strcat( string, " ");
  187.                 /* endif */
  188.                 while ( isspace( *tmptr ) )
  189.                     tmptr++;
  190.                 /* endwhile */
  191.                 strptr = tmptr;
  192.                 continue;
  193.             }/* if */
  194.             while ( *tmptr && !isspace( *tmptr) )
  195.                 ++tmptr;
  196.             /* endwhile */
  197.             if ( !*tmptr )
  198.                 return;
  199.             /* endif */
  200.             while ( *tmptr && isspace( *tmptr) )
  201.                 ++tmptr;
  202.             /* endwhile */
  203.             if ( !*tmptr )
  204.                 return;
  205.             /* endif */
  206.         }/* endif */
  207.         strptr = tmptr;
  208.     }/* while */
  209.     
  210. }/* remove_texicmds() */
  211.  
  212.  
  213. /*
  214.  * Consume the first word and leading blanks. Expressions in {...} are
  215.  * considered as one word.
  216.  */
  217. char * eat_first_word( char * string)
  218. {
  219.     if ( *(string = eatwhitespace( string)) == '\0' )
  220.         return string;
  221.     /* endif */
  222.     if ( *string == '{' ){
  223.         int bo = 1;
  224.         int bc = 0;
  225.         do{
  226.             ++string;
  227.             if ( *string == '{' )
  228.                 ++bo;
  229.             else if ( *string == '}' )
  230.                 ++bc;
  231.         } while ( *string && (bo != bc) );
  232.         if ( *string == '}' )
  233.             ++string;
  234.     } else {
  235.         do
  236.             ++string;
  237.         while ( (*string != ' ')
  238.                 && (*string != '\t')
  239.                 && *string );
  240.     }/* if */
  241.     if ( !(*string) )
  242.         return string;
  243.     /* endif */
  244.     return eatwhitespace( string);
  245. }/* eat_first_word() */
  246.  
  247. /*
  248.  * This translates a menu entry sub string to ipf. Because we're inside
  249.  * a menu entry we ignore any @... commmand! It would get too complicated
  250.  * otherwise :-)
  251.  */
  252. void translate_string( char *string)
  253. {
  254.     char token[MAXLINELEN];
  255.     char tmpout[MAXLINELEN*2];
  256.     char * ptr = tmpout;
  257.     int i = 0;
  258.  
  259.     if ( !string[i] ){
  260.         return;
  261.     }/* if */
  262.     
  263.     while ( (string[i] == ' ')
  264.             || (string[i] == '\t')
  265.             || (string[i] == '\n') ){
  266.         if ( !string[i] ){
  267.             return;
  268.         }/* if */
  269.         i++;
  270.     }/* while */
  271.     strcpy( tmpout, string);
  272.     string[0] = '\0';
  273.     
  274.     do{
  275.         ptr = gettoken( ptr, token);
  276.         strcat( string, token);
  277.     }while( *ptr );
  278.  
  279. }/* translate_string() */
  280.  
  281.  
  282. /*
  283.  * Find out if the current string is a menu entry. If no return 0.
  284.  * Else create a ipf menu entry and return 1.
  285.  */
  286. int menu_entry_maybe( char *input, char *output)
  287. {
  288.     char tmpbuf[MAXLINELEN*2];
  289.     char * double_colon;
  290.     if ( *input != '*' )
  291.         return 0;
  292.     /* endif */
  293.     input++;
  294.     if ( (double_colon = strstr( input, "::")) == NULL )
  295.         return 0;
  296.     /* endif */
  297.     double_colon[0] = '\0';
  298.     
  299.     input = eatwhitespace( input);
  300.     double_colon = eatwhitespace( double_colon+2);
  301.  
  302.     strcpy( tmpbuf, double_colon);
  303.     translate_string( input);
  304.     remove_texicmds( input);
  305.     translate_string( tmpbuf);
  306.     remove_texicmds( tmpbuf);
  307.     
  308.     sprintf( output, "%s%s%s%s%s%s\n%c", 
  309.              ":pt.:link reftype=hd refid='", input, "'.", input, 
  310.              ":elink.:pd.", tmpbuf, '\0');
  311.     return 1;
  312. }/* menu_entry_maybe() */
  313.  
  314.