Merge tag 'v3.10.61' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / escape.c
1 /**
2  * Copyright (C) ARM Limited 2010-2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 /*
10  * The Makefile in the daemon folder builds and executes 'escape'
11  * 'escape' creates configuration_xml.h from configuration.xml and events_xml.h from events-*.xml
12  * these genereated xml files are then #included and built as part of the gatord binary
13  */
14
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 static void print_escaped_path(char *path) {
22   if (isdigit(*path)) {
23     printf("__");
24   }
25   for (; *path != '\0'; ++path) {
26     printf("%c", isalnum(*path) ? *path : '_');
27   }
28 }
29
30 int main(int argc, char *argv[]) {
31   int i;
32   char *path;
33   FILE *in = NULL;
34   int ch;
35   unsigned int len = 0;
36
37   for (i = 1; i < argc && argv[i][0] == '-'; ++i) ;
38   if (i == argc) {
39     fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
40     return EXIT_FAILURE;
41   }
42   path = argv[i];
43
44   errno = 0;
45   if ((in = fopen(path, "r")) == NULL) {
46     fprintf(stderr, "Unable to open '%s': %s\n", path, strerror(errno));
47     return EXIT_FAILURE;
48   }
49
50   printf("static const unsigned char ");
51   print_escaped_path(path);
52   printf("[] = {");
53   for (;;) {
54     ch = fgetc(in);
55     if (len != 0) {
56       printf(",");
57     }
58     if (len % 12 == 0) {
59       printf("\n ");
60     }
61     // Write out a null character after the contents of the file but do not increment len
62     printf(" 0x%.2x", (ch == EOF ? 0 : ch));
63     if (ch == EOF) {
64       break;
65     }
66     ++len;
67   }
68   printf("\n};\nstatic const unsigned int ");
69   print_escaped_path(path);
70   printf("_len = %i;\n", len);
71
72   fclose(in);
73
74   return EXIT_SUCCESS;
75 }