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