/* * * 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 #include /* Internationalization support */ extern "C" { #include } #include "tdelfeditor.h" #include #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_SYSICON = 12, PARAM_SCMMODULE = 13, PARAM_SCMREVISION = 14, PARAM_NOTES = 15, } 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>3)?3: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 = 16; 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) { fprintf(stderr, _("%s: was given %d arguments, needed %d\n"), argv[PARAM_PROGNAME], 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\" \"sysicon\" \"scmmodule\" \"scmrevision\" \"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;i 0) add_resource_string(handle, ".metadata_name", argv[PARAM_EXECUTABLE_NAME]); if (strlen(argv[PARAM_DESCRIPTION]) > 0) add_resource_string(handle, ".metadata_description", argv[PARAM_DESCRIPTION]); if (strlen(argv[PARAM_LICENSE]) > 0) add_resource_string(handle, ".metadata_license", argv[PARAM_LICENSE]); if (strlen(argv[PARAM_COPYRIGHT]) > 0) add_resource_string(handle, ".metadata_copyright", argv[PARAM_COPYRIGHT]); if (strlen(argv[PARAM_AUTHORS]) > 0) add_resource_string(handle, ".metadata_authors", argv[PARAM_AUTHORS]); if (strlen(argv[PARAM_PRODUCT]) > 0) add_resource_string(handle, ".metadata_product", argv[PARAM_PRODUCT]); if (strlen(argv[PARAM_ORGANIZATION]) > 0) add_resource_string(handle, ".metadata_organization", argv[PARAM_ORGANIZATION]); if (strlen(argv[PARAM_VERSION]) > 0) add_resource_string(handle, ".metadata_version", argv[PARAM_VERSION]); if (strlen(argv[PARAM_DATETIME]) > 0) add_resource_string(handle, ".metadata_datetime", argv[PARAM_DATETIME]); if (strlen(argv[PARAM_SYSICON]) > 0) add_resource_string(handle, ".metadata_sysicon", argv[PARAM_SYSICON]); if (strlen(argv[PARAM_SCMMODULE]) > 0) add_resource_string(handle, ".metadata_scmmodule", argv[PARAM_SCMMODULE]); if (strlen(argv[PARAM_SCMREVISION]) > 0) add_resource_string(handle, ".metadata_scmrevision", argv[PARAM_SCMREVISION]); if (strlen(argv[PARAM_NOTES]) > 0) add_resource_string(handle, ".metadata_notes", argv[PARAM_NOTES]); } break; case MODE_SET_EMPTY_UUID: section = ICON_SECTION; clear_resource(handle, section); if(!libr_icon_setuuid(handle, "00000000-0000-0000-0000-000000000000")) { errorf(_("Failed to set UUID: %s\n"), libr_errmsg()); goto fail; } break; case MODE_LIST_RESOURCES: list_resources(handle); break; case MODE_CLEAR_ICON: section = ICON_SECTION; /* intentional fall-through */ case MODE_CLEAR_RESOURCE: if(section == NULL) section = argv[PARAM_RESOURCE_NAME]; clear_resource(handle, section); break; case MODE_ADD_ICON: { libr_icon *icon = NULL; icon = libr_icon_newicon_byfile(LIBR_PNG, 0, argv[PARAM_ICON_FILE]); if(icon == NULL) { errorf(_("failed to open icon file \"%s\": %s"), argv[PARAM_ICON_FILE], libr_errmsg()); goto fail; } if(!libr_icon_write(handle, icon, argv[PARAM_ICON_NAME], 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_TDE_AUTOADD_ICON: { printf("Searching for standard icon for name %s in the following directories:\n", argv[PARAM_ICON_NAME]); TDEApplication::disableAutoDcopRegistration(); TDEAboutData aboutd("tdelfeditor", "tdelfeditor", "0.0.1"); TDECmdLineArgs::init(&aboutd); TDEApplication app(false, false); TQStringList rds = TDEGlobal::dirs()->resourceDirs("icon"); for ( TQStringList::Iterator it = rds.begin(); it != rds.end(); ++it ) { printf(" * %s\n", (*it).ascii()); fflush(stdout); } TQString systemIcon = TDEGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, true); if (systemIcon.isNull()) { systemIcon = TDEGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, false); printf("NOT FOUND, refusing to add unknown icon (this message is harmless)\n"); section = ICON_SECTION; clear_resource(handle, section); goto fail; } else { printf("found %s\n", systemIcon.ascii()); } libr_icon *icon = NULL; icon = libr_icon_newicon_byfile(LIBR_PNG, 0, const_cast(systemIcon.ascii())); if(icon == NULL) { errorf(_("failed to open icon file \"%s\": %s"), systemIcon.ascii(), libr_errmsg()); goto fail; } TQFileInfo ifi(systemIcon); TQString iconBaseName = ifi.baseName(); printf("using %s as icon name\n", iconBaseName.ascii()); if(!libr_icon_write(handle, icon, const_cast(iconBaseName.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); }