Changeset 176b6ef in code for icb.c


Ignore:
Timestamp:
Apr 22, 2015, 5:28:54 PM (10 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
7ff6405
Parents:
677a45b
Message:

Use new tokenizer to parse ICB commands

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icb.c

    r677a45b r176b6ef  
    3838int    icb_login(struct icb_session *, char *, char *, char *);
    3939int    icb_dowho(struct icb_session *, struct icb_group *);
    40 char  *icb_nextfield(char **, int);
    4140
    4241/*
     
    7675{
    7776        char *msg = is->buffer;
     77        int msglen = is->length;
    7878        unsigned char type;
     79        char *wptr = NULL;
    7980        int res = 0;
    8081
     
    8889        switch (type) {
    8990        case ICB_M_LOGIN: {
    90                 char *nick, *group, *client, *cmd;
    91 
    92                 client = icb_nextfield(&msg, 1);
    93                 nick = icb_nextfield(&msg, 1);
    94                 group = icb_nextfield(&msg, 1);
    95                 cmd = icb_nextfield(&msg, 1);
     91                char client[ICB_MAXNICKLEN];
     92                char nick[ICB_MAXNICKLEN];
     93                char group[ICB_MAXGRPLEN];
     94                char cmd[ICB_MAXCMDLEN];
     95
     96                if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN,
     97                    ICB_M_SEP, 1) < 0) {
     98                        icb_error(is, "Invalid client");
     99                        icbd_drop(is, NULL);
     100                        return (1);
     101                }
     102                if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN,
     103                    ICB_M_SEP, 1) <= 0) {
     104                        icb_error(is, "Invalid nick");
     105                        icbd_drop(is, NULL);
     106                        return (1);
     107                }
     108                if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN,
     109                    ICB_M_SEP, 1) < 0) {
     110                        icb_error(is, "Invalid group");
     111                        icbd_drop(is, NULL);
     112                        return (1);
     113                }
     114                if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
     115                    ICB_M_SEP, 1) < 0) {
     116                        icb_error(is, "Invalid command");
     117                        icbd_drop(is, NULL);
     118                        return (1);
     119                }
    96120                if (strlen(cmd) > 0 && cmd[0] == 'w') {
    97121                        icb_error(is, "Command not implemented");
     
    108132        }
    109133        case ICB_M_OPEN: {
    110                 char *grpmsg;
    111 
    112                 grpmsg = icb_nextfield(&msg, 0);
    113                 icb_groupmsg(is, grpmsg);
     134                icb_groupmsg(is, msg);
    114135                break;
    115136        }
    116137        case ICB_M_COMMAND: {
    117                 char *cmd, *arg;
    118 
    119                 cmd = icb_nextfield(&msg, 1);
    120                 arg = icb_nextfield(&msg, 0);
     138                char cmd[ICB_MAXCMDLEN];
     139                char arg[ICB_MAXTOPICLEN];
     140
     141                if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
     142                    ICB_M_SEP, 1) <= 0) {
     143                        icb_error(is, "Invalid command");
     144                        icbd_drop(is, NULL);
     145                        return (1);
     146                }
     147                if (icb_token(msg, msglen, &wptr, arg, ICB_MAXTOPICLEN,
     148                    ICB_M_SEP, 1) < 0) {
     149                        icb_error(is, "Invalid argument");
     150                        icbd_drop(is, NULL);
     151                        return (1);
     152                }
    121153                icb_command(is, cmd, arg);
    122154                break;
     
    286318{
    287319        void (*handler)(struct icb_session *, char *);
    288         char command[32]; /* XXX */
     320        char command[ICB_MAXCMDLEN];
    289321
    290322        icb_vis(command, cmd, sizeof command, VIS_SP);
     
    586618
    587619/*
    588  *  icb_nextfield: advances through a given buffer returning pointer to the
    589  *                 beginning of the icb field or an empty string otherwise;
    590  *                 cleans up trailing spaces
    591  */
    592 char *
    593 icb_nextfield(char **buf, int notrspace)
    594 {
    595         char *start = *buf;
    596         char *end = NULL;
    597 
    598         while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
    599                 (*buf)++;
    600         if (*buf && **buf == ICB_M_SEP) {
    601                 **buf = '\0';
    602                 (*buf)++;
    603         }
    604         end = *buf;
    605         while (notrspace && end && end > start && *(--end) == ' ')
    606                 *end = '\0';
    607         return (start);
    608 }
    609 
    610 /*
    611620 *  icb_sendfmt: formats a string and sends it over
    612621 */
     
    632641 */
    633642int
    634 icb_token(char *buf, int len, char **bufptr, char *dst, int dlen, int sep)
     643icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
     644    int trim)
    635645{
    636646        char *start;
    637647        int i, ret;
    638648
    639         if (buf == NULL || len <= 0 || dlen <= 0)
     649        if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
    640650                return (0);
    641651        if (*bufptr == NULL)
     
    646656                        /* copy and null terminate the token */
    647657                        ret = strlcpy(dst, start,
    648                             MIN(*bufptr - start + 1, dlen));
     658                            MIN(*bufptr - start + 1, dstlen));
    649659                        if (**bufptr != '\0')
    650660                                (*bufptr)++;
     661                        if (ret > 0 && trim)
     662                                ret = icb_trim(dst, dstlen);
    651663                        return (ret);
    652664                }
     
    658670         */
    659671        if (*bufptr - start > 0) {
    660                 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dlen));
     672                ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
     673                if (ret > 0 && trim)
     674                        ret = icb_trim(dst, dstlen);
    661675                return (ret);
    662676        }
    663677        return (0);
     678}
     679
     680/*
     681 *  icb_trim: trims trailing whitespace
     682 */
     683int
     684icb_trim(char *buf, int len)
     685{
     686        char *p = buf;
     687        int i;
     688
     689        for (i = 0; i < len && *p != '\0'; i++)
     690                p++;
     691        if (*p == '\0' && p - buf > 0)
     692                p--;
     693        while (p >= buf && isspace(*p)) {
     694                *p = '\0';
     695                i--;
     696                if (p > buf)
     697                        p--;
     698        }
     699        return (i);
    664700}
    665701
Note: See TracChangeset for help on using the changeset viewer.