• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include "syscap_tool.h"
23 
24 extern char *optarg;
25 
main(int argc,char ** argv)26 int main(int argc, char **argv)
27 {
28     int32_t ret, optIndex;
29     char curpath[PATH_MAX] = {0};
30     int32_t rpcid = 0;
31     int32_t pcid = 0;
32     int32_t encode = 0;
33     int32_t decode = 0;
34     int32_t help = 0;
35     char *inputfile = NULL;
36     char *outputpath = getcwd(curpath, sizeof(curpath));
37 
38     while (1) {
39         static struct option long_options[] = {
40             {"help",   no_argument,       0,  'h' },
41             {"RPCID",  no_argument,       0,  'R' },
42             {"PCID",   no_argument,       0,  'P' },
43             {"encode", no_argument,       0,  'e' },
44             {"decode", no_argument,       0,  'd' },
45             {"input",  required_argument, 0,  'i' },
46             {"output", required_argument, 0,  'o' },
47             {0,        0,                 0,  0 }
48         };
49 
50         int32_t flag = getopt_long(argc, argv, "hRPedi:o:", long_options, &optIndex);
51         if (flag == -1) {
52             break;
53         }
54         switch (flag) {
55             case 'e':
56                 encode = 1;
57                 break;
58             case 'd':
59                 decode = 1;
60                 break;
61             case 'R':
62                 rpcid = 1;
63                 break;
64             case 'P':
65                 pcid = 1;
66                 break;
67             case 'i':
68                 inputfile = optarg;
69                 break;
70             case 'o':
71                 outputpath = optarg;
72                 break;
73             case 'h':
74             default:
75                 help = 1;
76         }
77     }
78 
79     if (rpcid && !pcid && encode && !decode && inputfile && !help) {
80         ret = RPCIDEncode(inputfile, outputpath);
81     } else if (rpcid && !pcid && !encode && decode && inputfile && !help) {
82         ret = RPCIDDecode(inputfile, outputpath);
83     } else if (!rpcid && pcid && encode && !decode && inputfile && !help) {
84         ret = PCIDEncode(inputfile, outputpath);
85     } else if (!rpcid && pcid && !encode && decode && inputfile && !help) {
86         ret = PCIDDecode(inputfile, outputpath);
87     } else {
88         printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n");
89         printf("-h, --help : how to use\n");
90         printf("-R, --RPCID : encode or decode RPCID\n");
91         printf("-P, --PCID : encode or decode PCID\n");
92         printf("-e, --encode : to encode\n");
93         printf("-d, --encode : to decode\n");
94         printf("-i filepath, --input filepath : input file\n");
95         printf("-o outpath, --input outpath : output path\n");
96         exit(0);
97     }
98 
99     if (ret != 0) {
100         printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__);
101         printf("input file(%s) prase failed\n", inputfile);
102     }
103 
104     return 0;
105 }