]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/commitdiff
em_canid: Simplified argument parsing.
authorRostislav Lisovy <lisovy@gmail.com>
Tue, 31 Jul 2012 07:39:17 +0000 (09:39 +0200)
committerRostislav Lisovy <lisovy@gmail.com>
Tue, 31 Jul 2012 07:39:17 +0000 (09:39 +0200)
tc/em_canid.c

index 7e39271a888b1862f7572a343223bf6855d182fc..20ec90070eb509598c96cd4c254df089f882bf14 100644 (file)
@@ -65,29 +65,23 @@ static int canid_parse_rule(struct rules *rules, struct bstr *a, int iseff)
                        can_mask = (iseff) ? CAN_EFF_MASK : CAN_SFF_MASK;
                }
        }
-       rules->rules_raw[rules->rules_cnt].can_id =
-               can_id | ((iseff) ? CAN_EFF_FLAG : 0);
-       rules->rules_raw[rules->rules_cnt].can_mask =
-               can_mask | CAN_EFF_FLAG;
-
-       //printf("Adding can_id 0x%x:0x%x\n", can_id, can_mask);
-       //printf("rules_capacity = %i, rules_cnt = %i\n",
-       //      rules->rules_capacity, rules->rules_cnt);
-
 
-       /* Stretch rules array up to EM_CAN_RULES_MAX */
+       /* Stretch rules array up to EM_CAN_RULES_MAX if necessary */
        if (rules->rules_cnt == rules->rules_capacity) {
-               // FIXME array boundary checking
                if (rules->rules_capacity <= EM_CAN_RULES_MAX/2) {
                        rules->rules_capacity *= 2;
                        rules->rules_raw = realloc(rules->rules_raw,
                                sizeof(struct can_filter) * rules->rules_capacity);
                } else {
-       //              printf("FFFUUU rules_capacity = %i, rules_cnt = %i\n",
-       //                      rules->rules_capacity, rules->rules_cnt);
                        return -2;
                }
        }
+
+       rules->rules_raw[rules->rules_cnt].can_id =
+               can_id | ((iseff) ? CAN_EFF_FLAG : 0);
+       rules->rules_raw[rules->rules_cnt].can_mask =
+               can_mask | CAN_EFF_FLAG;
+
        rules->rules_cnt++;
 
        return 0;
@@ -114,61 +108,35 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
        rules.rules_raw = malloc(sizeof(struct can_filter) * rules.rules_capacity);
        memset(rules.rules_raw, 0, sizeof(struct can_filter) * rules.rules_capacity);
 
-       if (!bstrcmp(args, "sff")) {
-               iseff = 0;
-       } else if (!bstrcmp(args, "eff")) {
-               iseff = 1;
-       } else {
-               ret = PARSE_ERR(args, "canid: invalid key");
-               goto exit;
-       }
-
-       a = bstr_next(args);
-       if (a == NULL) {
-               ret = PARSE_ERR(a, "canid: missing key");
-               goto exit;
-       }
-
-       ret = canid_parse_rule(&rules, a, iseff);
-       if (ret == -1) {
-               ret = PARSE_ERR(a, "canid: Improperly formed CAN ID & mask\n");
-               goto exit;
-       }
-
-       for (;;) {
-               /* There is no another keyword to parse */
-               a = bstr_next(a);
-               if (a == NULL) {
-                       break;
-               }
-
-               if (!bstrcmp(a, "sff")) {
+       do {
+               if (!bstrcmp(args, "sff")) {
                        iseff = 0;
-               } else if (!bstrcmp(a, "eff")) {
+               } else if (!bstrcmp(args, "eff")) {
                        iseff = 1;
                } else {
                        ret = PARSE_ERR(args, "canid: invalid key");
                        goto exit;
                }
 
-               a = bstr_next(a);
-               if (a == NULL) {
-                       ret = PARSE_ERR(a, "canid: missing argument");
+               args = bstr_next(args);
+               if (args == NULL) {
+                       ret = PARSE_ERR(args, "canid: missing argument");
                        goto exit;
                }
 
-               ret = canid_parse_rule(&rules, a, iseff);
+               ret = canid_parse_rule(&rules, args, iseff);
                if (ret == -1) {
-                       ret = PARSE_ERR(a, "canid: Improperly formed CAN ID & mask\n");
+                       ret = PARSE_ERR(args, "canid: Improperly formed CAN ID & mask\n");
                        goto exit;
                } else if (ret == -2) {
                        fprintf(stderr, "canid: Too many rules on input\n");
                        goto exit;
                }
-       }
+       } while ((args = bstr_next(args)) != NULL);
 
        addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
-       addraw_l(n, MAX_MSG, rules.rules_raw, sizeof(struct can_filter) * rules.rules_cnt);
+       addraw_l(n, MAX_MSG, rules.rules_raw,
+               sizeof(struct can_filter) * rules.rules_cnt);
 #undef PARSE_ERR
 
 exit:
@@ -179,8 +147,7 @@ exit:
 static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
                          int data_len)
 {
-       // FIXME
-       struct can_filter *conf = data; /* Array with rules, fixed size EM_CAN_RULES_SIZE */
+       struct can_filter *conf = data; /* Array with rules */
        int rules_count;
        int i;