1// Copyright 2016 Google Inc. All rights reserved
2//
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// +build ignore
16
17// This command will dump the contents of a kati stamp file into a more portable
18// format for use by other tools. For now, it just exports the files read.
19// Later, this will be expanded to include the Glob and Shell commands, but
20// those require a more complicated output format.
21
22#include <stdio.h>
23
24#include <string>
25
26#include "io.h"
27#include "log.h"
28#include "strutil.h"
29
30int main(int argc, char* argv[]) {
31  bool dump_files = false;
32  bool dump_env = false;
33
34  if (argc == 1) {
35    fprintf(stderr, "Usage: ckati_stamp_dump [--env] [--files] <stamp>\n");
36    return 1;
37  }
38
39  for (int i = 1; i < argc - 1; i++) {
40    const char* arg = argv[i];
41    if (!strcmp(arg, "--env")) {
42      dump_env = true;
43    } else if (!strcmp(arg, "--files")) {
44      dump_files = true;
45    } else {
46      fprintf(stderr, "Unknown option: %s", arg);
47      return 1;
48    }
49  }
50
51  if (!dump_files && !dump_env) {
52    dump_files = true;
53  }
54
55  FILE* fp = fopen(argv[argc - 1], "rb");
56  if (!fp)
57    PERROR("fopen");
58
59  ScopedFile sfp(fp);
60  double gen_time;
61  size_t r = fread(&gen_time, sizeof(gen_time), 1, fp);
62  if (r != 1)
63    ERROR("Incomplete stamp file");
64
65  int num_files = LoadInt(fp);
66  if (num_files < 0)
67    ERROR("Incomplete stamp file");
68  for (int i = 0; i < num_files; i++) {
69    string s;
70    if (!LoadString(fp, &s))
71      ERROR("Incomplete stamp file");
72    if (dump_files)
73      printf("%s\n", s.c_str());
74  }
75
76  int num_undefineds = LoadInt(fp);
77  if (num_undefineds < 0)
78    ERROR("Incomplete stamp file");
79  for (int i = 0; i < num_undefineds; i++) {
80    string s;
81    if (!LoadString(fp, &s))
82      ERROR("Incomplete stamp file");
83    if (dump_env)
84      printf("undefined: %s\n", s.c_str());
85  }
86
87  int num_envs = LoadInt(fp);
88  if (num_envs < 0)
89    ERROR("Incomplete stamp file");
90  for (int i = 0; i < num_envs; i++) {
91    string name;
92    string val;
93    if (!LoadString(fp, &name))
94      ERROR("Incomplete stamp file");
95    if (!LoadString(fp, &val))
96      ERROR("Incomplete stamp file");
97    if (dump_env)
98      printf("%s: %s\n", name.c_str(), val.c_str());
99  }
100
101  return 0;
102}
103