hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
 
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
#include "script_parser.h"
#include "debug.h"
 
 
#define ITEM_NAME_MAX_LEN               32
 
#define KEY_MAX_LEN                     32
#define VALUE_MAX_LEN                   128
 
#define ITEM_MAX_COUNT                  128
 
#define LINE_MAX_LEN                    512
 
#define LINE_ERROR                      -1
#define LINE_COMMENT                    0
#define LINE_NULL                       1
#define LINE_MAINKEY                    2
#define LINE_SUBKEY                     3
 
#define DATA_TYPE_SINGLE_WORD           1
#define DATA_TYPE_STRING                2
#define DATA_TYPE_MULTI_WORD            3
#define DATA_TYPE_GPIO                  4
 
struct script_head
{
    int mainkey_cnt;
    int version[3];
};
 
struct script_item
{
    char name[32];
    int length;
    int offset;
};
 
/*
 * get value data type.
 */
static int __get_str2int(char *buf, int value[])
{
    char *src;
    char ch;
    unsigned int temp;
    int sign;
    int i;
    int idx;
    char str[128];
 
    src = buf;
 
    if (strncasecmp(src, "port:p", 6) == 0) {
        /* handle gpio */
        src += 6;
        if (strncasecmp(src, "ower", 4) == 0) {
            value[0] = 0xffff;
            src += 4;
        }
        else {
            ch = *src++;
            if (islower(ch))
                value[0] = ch - 'a' + 1;
            else if (isupper(ch))
                value[0] = ch - 'A' + 1;
            else
                return -1;
        }
 
        temp = 0;
        ch = *src++;
        while (ch != '<') {
            if (isdigit(ch)) {
                temp = temp * 10 + (ch - '0');
                ch = *src++;
            }
            else if (ch == '\0') {
                src--;
                break;
            }
            else {
                return -1;
            }
        }
        value[1] = temp;
 
        idx = 2;
        ch = *src++;
        while (ch != '\0') {
            i = 0;
            memset(str, 0, sizeof(str));
            while (ch != '>') {
                if (isupper(ch))
                    ch = tolower(ch);
                str[i++] = ch;
                ch = *src++;
            }
 
            if (strcmp(str, "default") == 0 || 
                strcmp(str, "none") == 0 || 
                strcmp(str, "null") == 0 || 
                strcmp(str, "-1") == 0) {
                value[idx] = -1;
            }
            else {
                i = 0;
                ch = str[i++];
                temp = 0;
                if (ch == '-') {
                    sign = -1;
                    ch = str[i++];
                }
                else {
                    sign = 1;
                }
 
                while (ch != '\0') {
                    if (isdigit(ch))
                        temp = temp * 10 + (ch - '0');
                    else
                        return -1;
 
                    ch = str[i++];
                }
 
                value[idx] = temp * sign;
            }
 
            idx++;
            ch = *src++;
            if (ch == '<') {
                ch = *src++;
            }
            else if (ch == '\0') {
                ;
            }
            else {
                return -1;
            }
        }
 
        switch (idx) {
            case 3:
                value[3] = -1;
            case 4:
                value[4] = -1;
            case 5:
                value[5] = -1;
            case 6:
                break;
            default:
                return -1;
        }
 
        return DATA_TYPE_GPIO;
    }
    else if (strncasecmp(src, "string:", 7) == 0) {
        src += 7;
        idx = 0;
        while (src[idx] != '\0') {
            idx++;
            if (idx > 127)
                break;
        }
 
        if (idx & 0x03)
            idx = (idx & (~0x03)) + 4;
 
        value[0] = idx >> 2;
        value[1] = 1;
 
        return DATA_TYPE_STRING;
    }
    else if (src[0] == '"') {
        src += 1;
        idx = 0;
        while (src[idx] != '"') {
            idx++;
            if (idx > 127)
                break;
        }
 
        src[idx] = '\0';
        if (idx & 0x03)
            idx = (idx & (~0x03)) + 4;
        
        value[0] = idx >> 2;
        value[1] = 2;
 
        return DATA_TYPE_STRING;
    }
    else if (isdigit(src[0]) && (src[1] == 'x' || src[2] == 'X')) {
        temp = 0;
        ch = *src++;
        while (ch != '\0') {
            if (isdigit(ch)) {
                temp = temp * 16 + (ch - '0');
                ch = *src++;
            }
            else if (isupper(ch)) {
                temp = temp * 16 + (ch - 'A' + 10);
                ch = *src++;
            }
            else if (islower(ch)) {
                temp = temp * 16 + (ch - 'a' + 10);
                ch = *src++;
            }
            else {
                break;
            }
        }
        value[0] = temp;
 
        return DATA_TYPE_SINGLE_WORD;
    }
    else if (isdigit(src[0]) || 
            (isdigit(src[1]) && src[0] == '-')) {
        if (src[0] == '-') {
            sign = -1;
            ch = *src++;
        }
        else {
            sign = 1;
        }
 
        temp = 0;
        ch = *src++;
        while (ch != '\0') {
            if (isdigit(ch)) {
                temp = temp * 10 + (ch - '0');
                ch = *src++;
            }
            else {
                break;
            }
        }
        value[0] = temp * sign;
 
        return DATA_TYPE_SINGLE_WORD;
    }
    else {
        idx = 0;
        while (src[idx] != '\0') {
            idx++;
            if (idx > 127)
                break;
        }
 
        if (idx & 0x03)
            idx = (idx & (~0x03)) + 4;
 
        value[0] = idx >> 2;
 
        return DATA_TYPE_STRING;
    }
}
 
/*
 * get key[32] and value[128], end with '\0'.
 * \retval -1 empty line.
 * \retval 0 OK
 */
static int __get_key_value(char *buf, char *key, char *value)
{
    char *src;
    int key_idx;
    int value_idx;
 
    /* check line */
    src = buf;
    key_idx = value_idx = 0;
    while (1) {
        if (*src == ' ' || *src == '\t') {
            src++;
        }
        else if (*src == 0x0a || *src == 0x0d) {
            key[key_idx] = '\0';
            value[value_idx] = '\0';
            return -1;
        }
        else {
            break;
        }
    }
 
    /* get key */
    while (*src != '=') {
        key[key_idx++] = *src++;
        if (key_idx >= 31) {
            key[key_idx] = '\0';
            break;
        }
    }
 
    for (key_idx--; key_idx > 0; key_idx--) {
        if (key[key_idx] == ' ' || key[key_idx] == '\t') {
            key[key_idx] = '\0';
        }
        else {
            key[key_idx + 1] = '\0';
            break;
        }
    }
 
    for (; *src != '='; src++);
 
    /* check line */
    src++;
    while (1) {
        if (*src == ' ' || *src == '\t') {
            src++;
        }
        else if (*src == 0x0a || *src == 0x0d) {
            value[value_idx] = '\0';
            return 0;
        }
        else {
            break;
        }
    }
 
    /* get value */
    while (*src != 0x0a && *src != 0x0d) {
        value[value_idx++] = *src++;
        if (value_idx >= 127) {
            value[value_idx] = '\0';
            break;
        }
    }
 
    for (value_idx--; value_idx > 0; value_idx--) {
        if (value[value_idx] == ' ' || value[value_idx] == '\t') {
            value[value_idx] = '\0';
        }
        else {
            value[value_idx + 1] = '\0';
            break;
        }
    }
 
    return 0;
}
 
static int __fill_mainkey(char *buf, struct script_item *item)
{
    char *src;
    char ch;
    int i;
 
    src = buf + 1;
    for (ch = *src++, i = 0; ch != ']'; i++, ch = *src++) {
        item->name[i] = ch;
        if (i + 1 >= ITEM_NAME_MAX_LEN) {
            item->name[i] = '\0';
            break;
        }
    }
 
    if (item->name[0] == '\0')
        return -1;
 
    return 0;
}
 
static int __getline(char *buf, int len, int *flag)
{
    char *src;
    char ch;
    char prev_ch;
    int line_len;
 
    /* get line flag */
    src = buf;
    ch = *src++;
    switch (ch) {
        case ';':
            *flag = LINE_COMMENT;
            break;
 
        case 0x0a:
        case 0x0d:
            *flag = LINE_NULL;
            break;
 
        case '[':
            *flag = LINE_MAINKEY;
            break;
 
        default:
            *flag = LINE_SUBKEY;
            break;
    }
 
    /* get line length */
    if (*flag == LINE_NULL) {
        ch = *src++;
        if (ch == 0x0a)
            return 2;
        else
            return 1;
    }
 
    ch = *src++;
    line_len = 1;
    while (line_len < len) {
        if (ch == 0x0a || ch == 0x0d)
            break;
 
        ch = *src++;
        line_len++;
        if (line_len >= LINE_MAX_LEN) {
            *flag = LINE_ERROR;
            return 0;
        }
    }
    line_len++;
 
    prev_ch = ch;
    ch = *src++;
    if (prev_ch == 0x0d) {
        if (ch == 0x0a)
            line_len++;
    }
 
    return line_len;
}
 
static char*  __parse_script(char *buf, int len)
{
    char *src;
    int rest_len, line_num;
    struct script_item *item_table = NULL;
    struct script_head head;
    unsigned int mainkey_idx, subkey_idx;
    int new_mainkey;
    char key[KEY_MAX_LEN];
    char value[VALUE_MAX_LEN];
    char *key_data = NULL, *key_addr;
    int *value_data = NULL, *value_addr;
    int value_idx;
    int fmt_value[8];
    unsigned int i;
    int ret;
    int shmid;
    char *script_buf, *pos;
 
    db_debug("the length of script is %d\n", len);
 
    /* allocate memory for key and value */
    item_table = malloc(sizeof(struct script_item) * ITEM_MAX_COUNT);
    if (item_table == NULL) {
        db_debug("allocate memory for main key failed(%s)\n", strerror(errno));
        ret = -1;
        goto out;
    }
    memset(item_table, 0, sizeof(struct script_item) * ITEM_MAX_COUNT);
 
    key_data = malloc(512 * 1024);
    if (key_data == NULL) {
        db_debug("allocate memory for key data failed(%s)\n", strerror(errno));
        ret = -1;
        goto out;
    }
    memset(key_data, 0, 512 * 1024);
    key_addr = key_data;
 
    value_data = malloc(512 * 1024);
    if (value_data == NULL) {
        db_debug("allocate memory for value data failed(%s)\n", 
                strerror(errno));
        ret = -1;
        goto out;
    }
    memset(value_data, 0, 512 * 1024);
    value_addr = value_data;
 
    /* parse script main loop */
    src = buf;
    rest_len = len;
    mainkey_idx = subkey_idx = value_idx = 0;
    new_mainkey = 0;
    line_num = 0;
    while (rest_len) {
        int line_len;
        int flag;
        
        /* get current line */
        line_len = __getline(src, rest_len, &flag);
        rest_len -= line_len;
        line_num++;
        switch (flag) {
            case LINE_COMMENT:
            case LINE_NULL:
                break;
 
            case LINE_MAINKEY:
                /* get main key */
                if (__fill_mainkey(src, &item_table[mainkey_idx])) {
                    ret = -1;
                    goto out;
                }
 
                if (new_mainkey) {
                    item_table[mainkey_idx].offset = 
                        item_table[mainkey_idx-1].offset + 
                        item_table[mainkey_idx-1].length * 10;
                }
                else {
                    /* first main key */
                    new_mainkey = 1;
                    item_table[mainkey_idx].offset = 0;
                }
 
                mainkey_idx++;
                break;
 
            case LINE_SUBKEY:
                /* get key[32] and value[128] */
                memset(key, 0, KEY_MAX_LEN);
                memset(value, 0, VALUE_MAX_LEN);
                ret = __get_key_value(src, key, value);
                if (ret == -1)
                    continue;
 
                /* stores key[32], stores value's offset in the next 4 bytes, 
                 * stores value's length[31;16] and type[0:15] in the next 
                 * next 4 bytes. there are total 40 bytes for one key data.
                 *
                 * the unit of value data length is int
                 */
                strcpy(key_addr, key);
                key_addr += KEY_MAX_LEN;
 
                /* get value data type */
                memset(fmt_value, 0, sizeof(int) * 8);
                ret = __get_str2int(value, fmt_value);
                switch (ret) {
                    case DATA_TYPE_SINGLE_WORD:
                        *value_addr = fmt_value[0];
                        *(unsigned int *)key_addr = value_idx;
                        key_addr += 4;
                        *(unsigned int *)key_addr = (1 << 0) | 
                            (DATA_TYPE_SINGLE_WORD << 16);
                        value_addr++;
                        value_idx++;
                        break;
 
                    case DATA_TYPE_STRING:
                        if (fmt_value[0]) {
                            if (fmt_value[1] == 1) {
                                strncpy((char *)value_addr, value + 
                                        sizeof("string:") - 1, fmt_value[0] << 2);
                            }
                            else if (fmt_value[1] == 2) {
                                strncpy((char *)value_addr, value + 1, 
                                        fmt_value[0] << 2);
                            }
                            else{
                                strncpy((char *)value_addr, value, 
                                        fmt_value[0] << 2);
                            }
                        }
 
                        *(unsigned int *)key_addr = value_idx;
                        key_addr += 4;
                        *(unsigned int *)key_addr = (fmt_value[0] << 0) | 
                            (DATA_TYPE_STRING << 16);
                        value_addr += fmt_value[0];
                        value_idx += fmt_value[0];
                        break;
 
                    case DATA_TYPE_GPIO:
                        for (i = 0; i < 6; i++)
                            *value_addr++ = fmt_value[i];
                        *(unsigned int *)key_addr = value_idx;
                        key_addr += 4;
                        *(unsigned int *)key_addr = (6 << 0) | 
                            (DATA_TYPE_GPIO << 16);
                        value_idx += 6;
                        break;
 
                    default:
                        db_debug("%s: L%d: fix me\n", __func__, __LINE__);
                }
 
                subkey_idx++;
                key_addr += 4;
                item_table[mainkey_idx - 1].length++;
                break;
 
            default:
                db_debug("script format error at line %d\n", line_num);
                ret = -1;
                goto out;
        }
 
        src += line_len;
    }
 
    if (mainkey_idx <= 0) {
        db_debug("mainkey_idx = %d\n", mainkey_idx);
        ret = -1;
        goto out;
    }
 
    /* recalc first subkey offset */
    for (i = 0; i < mainkey_idx; i++) {
        item_table[i].offset += (sizeof(struct script_head) >> 2) + 
            ((sizeof(struct script_item) * mainkey_idx) >> 2);
    }
 
    /* recalc every subkey offset */
    key_addr = key_data;
    i = 0;
    while (i < subkey_idx * 10 * sizeof(int)) {
        key_addr += ITEM_NAME_MAX_LEN;
        *(unsigned int *)key_addr += (sizeof(struct script_head) >> 2) + 
            ((sizeof(struct script_item) * mainkey_idx) >> 2) + 
            (subkey_idx * 10);
        i += 10 * sizeof(int); /* the sizeof each subkey data */
        key_addr += 8;
    }
 
    /* init script head */
    head.mainkey_cnt = mainkey_idx;
    head.version[0] = SCRIPT_VERSION0;
    head.version[1] = SCRIPT_VERSION1;
    head.version[2] = SCRIPT_VERSION2;
 
    /* allocate share memory segment for script buffer */
    i = sizeof(struct script_head) + sizeof(struct script_item) * 
        mainkey_idx + 10 * sizeof(int) * subkey_idx + sizeof(int) * value_idx;
    #if 0
    shmid = shmget(IPC_PRIVATE, i, IPC_CREAT | 0666);
    if (shmid == -1) {
        db_debug("allocate share memory segment for script buffer "
                "failed(%s)\n", strerror(errno));
        ret = -1;
        goto out;
    }
    db_debug("script shmid = %d\n", shmid);
 
    script_buf = pos = shmat(shmid, 0, 0);
    if (script_buf == (void *)-1) {
        db_debug("attach the share memory segment failed(%s)\n", 
                strerror(errno));
        shmctl(shmid, IPC_RMID, 0);
        ret = -1;
        goto out;
    }
#else
   script_buf = pos =  malloc(i);
#endif
    /* stores script buffer */
    memcpy(pos, &head, sizeof(struct script_head));
    pos += sizeof(struct script_head);
    memcpy(pos, item_table, sizeof(struct script_item) * mainkey_idx);
    pos += sizeof(struct script_item) * mainkey_idx;
    memcpy(pos, key_data, 10 * sizeof(int) * subkey_idx);
    pos += 10 * sizeof(int) * subkey_idx;
    memcpy(pos, value_data, sizeof(int) * value_idx);
 
    //shmdt(script_buf);
  //  ret = shmid;
  return script_buf;
 
out:
    /* free memory */
    if (item_table)
        free(item_table);
    if (key_data)
        free(key_data);
    if (value_data)
        free(value_data);
 
    return NULL;
}
 
char * parse_script(const char *name)
{
    FILE *fscript;
    int len;
    char *buf;
    int read_len;
    int shmid;
    char *script_buf;
 
    if (name == NULL) {
        db_error("script: invalid file name(null)\n");
        return NULL;
    }
 
    /* read script data */
    fscript = fopen(name, "rb");
    if (fscript == NULL) {
        db_error("script: can't open %s(%s)\n", name, strerror(errno));
        return NULL;
    }
 
    fseek(fscript, 0, SEEK_END);
    len = ftell(fscript);
    fseek(fscript, 0, SEEK_SET);
 
    buf = malloc(len);
    if (buf == NULL) {
        db_error("script: allocate memory for script data failed(%s)\n", 
                strerror(errno));
        fclose(fscript);
        return NULL;
    }
 
    read_len = fread(buf, 1, len, fscript);
    if (read_len < len) {
        db_debug("len = %d, read_len = %d, fix me\n", len, read_len);
    }
    fclose(fscript);
 
    /* parse script */
    script_buf = __parse_script(buf, len);
    free(buf);
 
    return script_buf;
}
 
void deparse_script(int shmid)
{
    //shmctl(shmid, IPC_RMID, 0);
}