+int parse_addr(char *ad)
+{
+ int addr = -1;
+ char ad_fixed[5];
+ char *ep;
+ unsigned long val;
+ int i;
+
+ if (ad == NULL)
+ return -1;
+
+ switch(strlen(ad)) {
+ case 4:
+ if (strncmp(ad, "0x", 2)) {
+ memset(ad_fixed, 0, sizeof(ad_fixed));
+ for (i = 0; i < 4; i++) {
+ if ((ad[i] < '1') || (ad[i] > '4')) {
+ fprintf(stderr, "Not a valid ELV address: %s\n", ad);
+ return -1;
+ }
+ ad_fixed[i] = ad[i] - 1;
+ val = strtoul(ad_fixed, &ep, 4);
+
+ if (*ep != '\0') {
+ fprintf(stderr, "Can't parse fixed ELV housecode: %s\n", ad_fixed);
+ return -1;
+ }
+ }
+
+ break;
+ }
+ case 2:
+ val = strtoul(ad, &ep, 16);
+ if (*ep != '\0') {
+ fprintf(stderr, "Not a 1 byte hexstring: %s\n", ad);
+ return -1;
+ }
+ break;
+ default:
+ fprintf(stderr, "Address has to be in hex (01, 0x01) or ELV (1112) format!\n");
+ return -1;
+ break;
+ }
+
+ addr = val & 0xff;
+
+ return addr;
+}
+