stardis-program.c (2383B)
1 /* Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include <rsys/rsys.h> 17 #include <rsys/library.h> 18 19 #include "stardis-program.h" 20 21 res_T 22 init_program 23 (struct mem_allocator* allocator, 24 struct program** dst) 25 { 26 res_T res = RES_OK; 27 int str_initialized = 0; 28 ASSERT(allocator && dst && *dst == NULL); 29 *dst = MEM_CALLOC(allocator, 1, sizeof(**dst)); 30 if(! *dst) { 31 res = RES_MEM_ERR; 32 goto error; 33 } 34 str_init(allocator, &(*dst)->name); 35 str_init(allocator, &(*dst)->lib_path); 36 (*dst)->argc = 0; 37 (*dst)->argv = NULL; 38 str_initialized = 1; 39 end: 40 return res; 41 error: 42 if(str_initialized) { 43 str_release(&(*dst)->name); 44 str_release(&(*dst)->lib_path); 45 } 46 if(*dst) MEM_RM(allocator, *dst); 47 goto end; 48 } 49 50 void 51 release_program 52 (struct program* program, 53 struct mem_allocator* allocator) 54 { 55 size_t i; 56 ASSERT(program && allocator); 57 str_release(&program->name); 58 str_release(&program->lib_path); 59 if(program->prog_data) { 60 ASSERT(program->release); 61 program->release(program->prog_data); 62 } 63 library_close(program->lib_handle); 64 for(i = 0; i < program->argc; i++) MEM_RM(allocator, program->argv[i]); 65 MEM_RM(allocator, program->argv); 66 MEM_RM(allocator, program); 67 } 68 69 res_T 70 str_print_program 71 (struct str* str, 72 const struct program* p) 73 { 74 res_T res = RES_OK; 75 ASSERT(p->argc >= 1); /* At least one argument which is the program name */ 76 77 ERR(str_append_printf(str, "Library %s", str_cget(&p->name))); 78 if(p->argc > 1) { 79 size_t i; 80 ERR(str_append_printf(str, ", provided arguments:\n")); 81 for(i = 1; i < p->argc; i++) { 82 ERR(str_append_printf(str, (i+1 == p->argc ? "\t%s" : "\t%s\n"), p->argv[i])); 83 } 84 } 85 end: 86 return res; 87 error: 88 goto end; 89 } 90