/* * * Copyright (c) 2008-2011 Erich E. Hoover * TDE version (c) 2011 Timothy Pearson * * elfres - Adds resources into ELF files * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * To provide feedback, report bugs, or otherwise contact me: * ehoover at mines dot edu * */ /* Obtaining command-line options */ #include /* Internationalization support */ extern "C" { #include } #include "tdelfeditor.h" #include #include #include #include #include #include #include #include /* return application name */ extern const char *__progname; #define progname() __progname typedef enum { PARAM_PROGNAME = 0, PARAM_OPTIONS = 1, PARAM_ELF_FILE = 2, PARAM_UUID = 3, PARAM_RESOURCE_NAME = 3, PARAM_ICON_NAME = 3, PARAM_ICON_SIZE = 3, PARAM_RESOURCE_FILE = 4, PARAM_ICON_FILE = 4, PARAM_EXECUTABLE_NAME = 3, PARAM_DESCRIPTION = 4, PARAM_LICENSE = 5, PARAM_COPYRIGHT = 6, PARAM_AUTHORS = 7, PARAM_PRODUCT = 8, PARAM_ORGANIZATION = 9, PARAM_VERSION = 10, PARAM_DATETIME = 11, PARAM_NOTES = 12, } eParams; typedef struct { char short_options[ELFICON_OPTIONS+1]; struct option long_options[ELFICON_OPTIONS+1]; char descriptions[ELFICON_OPTIONS][100]; } IconOptions; IconOptions elficon_options = { "acefgmrstv", { {"add-icon", 0, 0, 'a'}, {"clear-icons", 0, 0, 'c'}, {"set-empty-uuid", 0, 0, 'e'}, {"find-icon", 0, 0, 'f'}, {"get-uuid", 0, 0, 'g'}, {"write-metadata", 0, 0, 'm'}, {"retrieve-icon", 0, 0, 'r'}, {"set-uuid", 0, 0, 's'}, {"tde-autoadd-icon", 0, 0, 't'}, {"version", 0, 0, 'v'}, {NULL, 0, 0, 0} }, { N_("add an icon to the ELF file"), N_("clear the file's ELF icon"), N_("set an empty icon UUID for the ELF file"), N_("find an ELF icon in the file by closest size"), N_("get the icon UUID for the file"), N_("write metadata information to the ELF file"), N_("retrieve an icon from the ELF file"), N_("set the icon UUID for the ELF file"), N_("automatically add the appropriate TDE icon to the ELF file"), N_("display the current application revision"), } }; typedef struct { char short_options[ELFRES_OPTIONS+1]; struct option long_options[ELFRES_OPTIONS+1]; char descriptions[ELFRES_OPTIONS][100]; } RcOptions; RcOptions elfres_options = { "aclrv", { {"add", 0, 0, 'a'}, {"clear", 0, 0, 'c'}, {"list", 0, 0, 'l'}, {"retrieve", 0, 0, 'r'}, {"version", 0, 0, 'v'}, {NULL, 0, 0, 0} }, { N_("add a resource to the ELF file"), N_("clear an ELF file resource"), N_("list libr-compatible resources"), N_("retrieve a resource from the ELF file"), N_("display the current application revision"), } }; typedef enum { MODE_ADD_RESOURCE, MODE_CLEAR_RESOURCE, MODE_RETRIEVE_RESOURCE, MODE_LIST_RESOURCES, MODE_ADD_ICON, MODE_TDE_AUTOADD_ICON, MODE_RETRIEVE_ICON, MODE_CLEAR_ICON, MODE_SET_UUID, MODE_SET_EMPTY_UUID, MODE_GET_UUID, MODE_FIND_ICON, MODE_SET_METADATA, MODE_LAUNCH_GUI } eMode; /* * Return the size of the file represented by the file handle */ off_t file_size(FILE *file) { struct stat file_stat; if(fstat(fileno(file), &file_stat) == ERROR) return ERROR; return file_stat.st_size; } /* * Output the application version */ void output_version(char *progname) { printf(_("%s: Version %s\n"), progname, VERSION); } /* * Use command-line arguments to set the appropriate data mode */ int handle_arguments(int argc, char **argv, eMode *mode) { int required_params = 0; int i, c, index = 0; opterr = 0; /* Prevent automatic getopt() error message */ while ((c = getopt_long(argc, argv, elficon_options.short_options, elficon_options.long_options, &index)) != EOF) { switch(c) { case 'a': *mode = MODE_ADD_ICON; required_params = 5; break; case 'c': *mode = MODE_CLEAR_ICON; required_params = 3; break; case 'e': *mode = MODE_SET_EMPTY_UUID; required_params = 3; break; case 'f': *mode = MODE_FIND_ICON; required_params = 5; break; case 'g': *mode = MODE_GET_UUID; required_params = 3; break; case 'm': *mode = MODE_SET_METADATA; required_params = 13; break; case 'r': *mode = MODE_RETRIEVE_ICON; required_params = 5; break; case 's': *mode = MODE_SET_UUID; required_params = 4; break; case 't': *mode = MODE_TDE_AUTOADD_ICON; required_params = 4; break; case 'v': output_version(argv[PARAM_PROGNAME]); return FALSE; default: goto print_icon_usage; } index++; if(argc != required_params) goto print_icon_usage; } return TRUE; print_icon_usage: fprintf(stderr, _("usage: %s [-a|-r] elf-file-name icon-name svg-file-name\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("usage: %s [-t] elf-file-name icon-name\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("usage: %s [-c|-e|-g] elf-file-name\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("usage: %s [-s] elf-file-name uuid\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("usage: %s [-s] elf-file-name uuid\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("usage: %s [-m] elf-file-name \"executable name\" \"description\" \"license\" \"copyright\" \"authors\" \"product\" \"organization\" \"version\" \"datetime\" \"notes\"\n"), argv[PARAM_PROGNAME]); fprintf(stderr, _("If -t is set the TDEDIRS environment variable must include your TDE installation prefix\n")); fprintf(stderr, _("for example: TDEDIRS=/opt/trinity ./tdelfeditor -t ./konqueror konqueror\n")); for(i=0;iresourceDirs("icon"); for ( TQStringList::Iterator it = rds.begin(); it != rds.end(); ++it ) { printf(" * %s\n\r", (*it).ascii()); fflush(stdout); } TQString systemIcon = KGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, true); if (systemIcon.isNull()) { systemIcon = KGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, false); printf("NOT FOUND, using %s\n\r", systemIcon.ascii()); } else { printf("found %s\n\r", systemIcon.ascii()); } libr_icon *icon = NULL; icon = libr_icon_newicon_byfile(LIBR_SVG, 0, const_cast(systemIcon.ascii())); if(icon == NULL) { errorf(_("failed to open icon file \"%s\": %s"), systemIcon.ascii(), libr_errmsg()); goto fail; } if(!libr_icon_write(handle, icon, const_cast(systemIcon.ascii()), LIBR_OVERWRITE)) { libr_icon_close(icon); errorf(_("failed to add icon to ELF file: %s"), libr_errmsg()); goto fail; } libr_icon_close(icon); } break; case MODE_ADD_RESOURCE: if(section == NULL) { section = argv[PARAM_RESOURCE_NAME]; input_file = argv[PARAM_RESOURCE_FILE]; } add_resource(handle, section, input_file); break; case MODE_RETRIEVE_ICON: { libr_icon *icon = NULL; icon = libr_icon_geticon_byname(handle, argv[PARAM_ICON_NAME]); if(icon == NULL) { errorf(_("failed to get icon named \"%s\": %s"), argv[PARAM_ICON_NAME], libr_errmsg()); goto fail; } if(!libr_icon_save(icon, argv[PARAM_ICON_FILE])) { libr_icon_close(icon); errorf(_("failed to save the icon to a file: %s"), libr_errmsg()); goto fail; } libr_icon_close(icon); } break; case MODE_RETRIEVE_RESOURCE: if(section == NULL) { section = argv[PARAM_RESOURCE_NAME]; output_file = argv[PARAM_RESOURCE_FILE]; } get_resource(handle, section, output_file); break; default: errorf(_("Unhandled operation code.")); goto fail; } fail: /* Close file handle and exit */ libr_close(handle); return EX_OK; } /* * Main execution routine */ int main(int argc, char **argv) { libr_i18n_autoload(PACKAGE); return main_console(argc, argv); }