Changeset de3e100 for irc.c


Ignore:
Timestamp:
2006-01-14T20:31:59Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
0431ea1
Parents:
b23c5c7
Message:

Separated the IRC line splitting/parsing code (to use it for IPC too), and improved the splitting to deal with empty arguments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • irc.c

    rb23c5c7 rde3e100  
    293293int irc_process( irc_t *irc )
    294294{
    295         char **lines, *temp;   
     295        char **lines, *temp, **cmd;
    296296        int i;
    297297
    298         if( irc->readbuffer != NULL ) {
    299                 lines = irc_tokenize(irc->readbuffer );
    300                 for( i = 0; *lines[i] != '\0'; i++ ) {
    301                         if( lines[i+1] == NULL ) {
     298        if( irc->readbuffer != NULL )
     299        {
     300                lines = irc_tokenize( irc->readbuffer );
     301               
     302                for( i = 0; *lines[i] != '\0'; i ++ )
     303                {
     304                        if( lines[i+1] == NULL )
     305                        {
    302306                                temp = g_strdup( lines[i] );
    303307                                g_free( irc->readbuffer );
    304308                                irc->readbuffer = temp;
    305                                 i++;
     309                                i ++;
    306310                                break;
    307311                        }                       
    308                         if (!irc_process_line(irc, lines[i])) {
     312                       
     313                        if( ( cmd = irc_parse_line( irc, lines[i] ) ) == NULL )
     314                                continue;
     315                        if( !irc_exec( irc, cmd ) )
     316                        {
     317                                g_free( cmd );
    309318                                g_free( lines );
    310319                                return 0;
    311320                        }
    312                 }
    313                 if(lines[i]!=NULL) {
    314                         g_free(irc->readbuffer);
    315                         irc->readbuffer=NULL;   
    316                 }
     321                       
     322                        g_free( cmd );
     323                }
     324               
     325                if( lines[i] != NULL )
     326                {
     327                        g_free( irc->readbuffer );
     328                        irc->readbuffer = NULL;
     329                }
     330               
    317331                g_free( lines );
    318332        }
     333       
    319334        return 1;       
    320335}
     
    326341
    327342        /* Count the number of elements we're gonna need. */
    328         for(i=0, j=1; buffer[i]!='\0'; i++ ) {
    329                 if(buffer[i]=='\n' )
    330                         if(buffer[i+1]!='\r' && buffer[i+1]!='\n')
    331                                 j++;
     343        for( i = 0, j = 1; buffer[i] != '\0'; i ++ )
     344        {
     345                if( buffer[i] == '\n' )
     346                        if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
     347                                j ++;
    332348        }
    333349       
    334350        /* Allocate j+1 elements. */
    335         lines=g_new (char *, j+1);
     351        lines = g_new( char *, j + 1 );
    336352       
    337353        /* NULL terminate our list. */
    338         lines[j]=NULL;
    339        
    340         lines[0]=buffer;
     354        lines[j] = NULL;
     355       
     356        lines[0] = buffer;
    341357       
    342358        /* Split the buffer in several strings, using \r\n as our seperator, where \r is optional.
    343359         * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too.
    344360         */
    345         for( i=0, j=0; buffer[i]!='\0'; i++) {
    346                 if(buffer[i]=='\n') {
    347                         buffer[i]='\0';
    348 
    349                         /* We dont want to read 1 byte before our buffer
    350                          * and (in rare cases) generate a SIGSEGV.
    351                          */
    352                         if(i!=0)
    353                                 if(buffer[i-1]=='\r')
    354                                         buffer[i-1]='\0';
    355                         if(buffer[i+1]!='\r'&&buffer[i+1]!='\n')
    356                                 lines[++j]=buffer+i+1;
    357                 }
    358         }
    359 
    360         return(lines);
    361 }
    362 
    363 int irc_process_line( irc_t *irc, char *line )
     361        for( i = 0, j = 0; buffer[i] != '\0'; i ++)
     362        {
     363                if( buffer[i] == '\n' )
     364                {
     365                        buffer[i] = '\0';
     366                       
     367                        if( i > 0 && buffer[i-1] == '\r' )
     368                                buffer[i-1] = '\0';
     369                        if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
     370                                lines[++j] = buffer + i + 1;
     371                }
     372        }
     373       
     374        return( lines );
     375}
     376
     377char **irc_parse_line( irc_t *irc, char *line )
    364378{
    365379        int i, j;
     
    367381       
    368382        /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */
    369         if(line[0]==':') {
    370                 for(i=0; line[i]!=32; i++);
    371                 line=line+i;
    372         }
    373         for(i=0; line[i]==32; i++);
    374         line=line+i;
    375 
     383        if( line[0] == ':' )
     384        {
     385                for( i = 0; line[i] != ' '; i ++ );
     386                line = line + i;
     387        }
     388        for( i = 0; line[i] == ' '; i ++ );
     389        line = line + i;
     390       
    376391        /* If we're already at the end of the line, return. If not, we're going to need at least one element. */
    377         if(line[0]=='\0')
    378                 return 1;
    379         else
    380                 j=1;   
    381        
    382         /* Count the number of char **cmd elements we're going to need. */     
    383         for(i=0; line[i]!='\0'; i++) {
    384                 if((line[i]==32) && (line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':'))           
    385                         j++;
    386                 else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]==32)) {
    387                         j++;
    388                         break;
    389                 }
     392        if( line[0] == '\0')
     393                return NULL;
     394       
     395        /* Count the number of char **cmd elements we're going to need. */
     396        j = 1;
     397        for( i = 0; line[i] != '\0'; i ++ )
     398        {
     399                if( line[i] == ' ' )
     400                {
     401                        j ++;
    390402                       
     403                        if( line[i+1] == ':' )
     404                                break;
     405                }
    391406        }       
    392407
    393408        /* Allocate the space we need. */
    394         cmd=g_new(char *, j+1);
    395         cmd[j]=NULL;
     409        cmd = g_new( char *, j + 1 );
     410        cmd[j] = NULL;
    396411       
    397412        /* Do the actual line splitting, format is:
     
    400415         */
    401416
    402         cmd[0]=line;
    403         for(i=0, j=0; line[i]!='\0'; i++) {
    404                 if((line[i]==32)) {
    405                         line[i]='\0';
    406                         if((line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':'))           
    407                                 cmd[++j]=line+i+1;
    408                 }
    409                 else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]=='\0')) {
    410                         cmd[++j]=line+i+1;
    411                         break;
    412                 }
    413         }
    414        
    415         i=irc_exec(irc, cmd);
    416         g_free(cmd);
    417 
    418         return(i);     
     417        cmd[0] = line;
     418        for( i = 0, j = 0; line[i] != '\0'; i ++ )
     419        {
     420                if( line[i] == ' ' )
     421                {
     422                        line[i] = '\0';
     423                        cmd[++j] = line + i + 1;
     424                       
     425                        if( line[i+1] == ':' )
     426                        {
     427                                cmd[j] ++;
     428                                break;
     429                        }
     430                }
     431        }
     432       
     433        return cmd;
    419434}
    420435
Note: See TracChangeset for help on using the changeset viewer.