• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 <stdio.h>
17 #include <unistd.h>
18 #include <getopt.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "dump_syspara.h"
22 
23 typedef struct {
24     char *serviceName;
25     int (*serviceHandler)();
26 } ServiceItem;
27 
28 static const ServiceItem SERVICE_LIST[] = {
29     {"syspara", QuerySysparaCmd},
30 };
31 
Usage()32 static void Usage()
33 {
34     printf("usage os_dump [--help | -l | SERVICE]\n"
35         "         --help: shows this help\n"
36         "         -l: only list services, do not dump them\n"
37         "         SERVICE: dumps only service SERVICE\n");
38 };
39 
main(int argc,char ** argv)40 int main(int argc, char **argv)
41 {
42     static struct option longOptions[] = {
43         {"help", no_argument, 0, 0},
44         {0, 0, 0, 0}
45     };
46     int serviceNum = (sizeof(SERVICE_LIST) / sizeof(ServiceItem));
47     int c;
48     int optionIndex = 0;
49     if (optind == argc) {
50         Usage();
51         return 0;
52     }
53 
54     optind = 1;
55     while (1) {
56         c = getopt_long(argc, argv, "l", longOptions, &optionIndex);
57         if (c == -1) {
58             break;
59         }
60         switch (c) {
61             case 0:
62                 if (!strcmp(longOptions[optionIndex].name, "help")) {
63                     Usage();
64                 }
65                 break;
66             case 'l':
67                 printf("OptionList:\n");
68                 for (int i = 0; i < serviceNum; i = i + 1) {
69                     printf("%s\n", SERVICE_LIST[i].serviceName);
70                 }
71                 break;
72             default:
73                 Usage();
74                 break;
75         }
76     }
77 
78     for (int i = optind; i < argc; i++) {
79         for (int j = 0; j < serviceNum; j++) {
80             if (!strcmp(argv[i], SERVICE_LIST[j].serviceName)) {
81                 int ret = SERVICE_LIST[j].serviceHandler();
82                 return ret;
83             }
84         }
85         printf("Service %s not registered! Try os_dump --help\n", argv[i]);
86         break;
87     }
88     return 0;
89 }
90