Line data Source code
1 : #include "../types/fd_types_yaml.h"
2 : #include "../types/fd_types_reflect.h"
3 :
4 : #include <stdio.h>
5 : #include <stdlib.h>
6 : #include <sys/stat.h> /* mkdir(2) */
7 : #include <fcntl.h> /* open(2) */
8 : #include <unistd.h> /* close(2) */
9 :
10 : static void
11 0 : usage( void ) {
12 : fprintf( stderr,
13 0 : "Usage: fd_bincode2yaml --type <type> --file {FILE}\n"
14 0 : "\n"
15 0 : "Converts a bincode blob to YAML.\n"
16 0 : "\n"
17 0 : "Options:\n"
18 0 : " --file name Path to bincode blob\n"
19 0 : " --type <type> fd_types name (see fd_types.json)\n"
20 0 : "\n" );
21 0 : }
22 :
23 : int
24 : main( int argc,
25 : char ** argv ) {
26 : fd_boot( &argc, &argv );
27 :
28 : /* Command line handling */
29 :
30 : for( int i=1; i<argc; i++ ) {
31 : if( 0==strcmp( argv[i], "--help" ) ) {
32 : usage();
33 : return 0;
34 : }
35 : }
36 :
37 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
38 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
39 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
40 : char const * type = fd_env_strip_cmdline_cstr ( &argc, &argv, "--type", NULL, NULL );
41 : char const * file = fd_env_strip_cmdline_cstr ( &argc, &argv, "--file", NULL, NULL );
42 :
43 : if ((NULL == type) || (NULL == file)) {
44 : usage();
45 : return 0;
46 : }
47 :
48 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
49 :
50 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
51 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
52 :
53 : /* Create scratch allocator */
54 :
55 : ulong smax = scratch_mb << 20;
56 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
57 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
58 : # define SCRATCH_DEPTH (4UL)
59 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
60 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
61 : fd_scratch_push();
62 :
63 : /* Read file */
64 :
65 : uchar * data;
66 : ulong data_sz;
67 : do {
68 : /* Open and stat file */
69 : int fd = open( file, O_RDONLY );
70 : FD_TEST( fd>=0 );
71 : struct stat statbuf[1];
72 : FD_TEST( 0==fstat( fd, statbuf ) );
73 : data_sz = (ulong)statbuf->st_size;
74 :
75 : /* Allocate scratch buffer for file */
76 : FD_TEST( fd_scratch_alloc_is_safe( /* align */ 1UL, data_sz ) );
77 : data = fd_scratch_alloc( /* align */ 1UL, data_sz );
78 :
79 : /* Copy file into memory */
80 : FD_TEST( (ssize_t)data_sz == read( fd, data, data_sz ) );
81 : FD_TEST( 0==close( fd ) );
82 : } while(0);
83 :
84 : /* Decode file */
85 :
86 : fd_bincode_decode_ctx_t decode = {
87 : .data = data,
88 : .dataend = data + data_sz,
89 : };
90 :
91 : fd_flamenco_yaml_t * yaml =
92 : fd_flamenco_yaml_init( fd_flamenco_yaml_new(
93 : fd_scratch_alloc( fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
94 : stdout );
95 :
96 : fd_types_vt_t const * f = fd_types_vt_by_name( type, strlen( type ) );
97 : if( FD_UNLIKELY( !f ) ) FD_LOG_ERR (( "lookup for %s failed", type ));
98 :
99 : ulong total_sz = 0UL;
100 : int err = f->decode_footprint( &decode, &total_sz );
101 : if( FD_UNLIKELY( err!=0 ) ) return err;
102 :
103 : uchar * d = fd_scratch_alloc( f->align, total_sz );
104 :
105 : f->decode( d, &decode );
106 :
107 : f->walk( yaml, d, fd_flamenco_yaml_walk, NULL, 0U, 0U );
108 :
109 : fd_scratch_pop();
110 : fd_scratch_detach( NULL );
111 : fd_halt();
112 : return 0;
113 : }
|