void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { ymx_debug_msg("Entering mexFunction\n"); mexAtExit(ymx_persistent_cleanup); if (nrhs == 0) { command_help(nlhs, plhs, 0, NULL); return; } if (!mxIsChar(prhs[0]) || mxGetM(prhs[0]) != 1) { mexErrMsgTxt("First input must be a string."); } char *command = mxArrayToString(prhs[0]); if (strcmp(command, "load") == 0) { command_load(nlhs, plhs, nrhs-1, prhs+1); } else if (strcmp(command, "dump") == 0) { command_dump(nlhs, plhs, nrhs-1, prhs+1); } else if (strcmp(command, "help") == 0) { command_help(nlhs, plhs, nrhs-1, prhs+1); } else { command_help(nlhs, plhs, 0, NULL); } ymx_debug_msg("plhs: %d\n", plhs); ymx_debug_msg("plhs[0]: %d\n", plhs[0]); mxFree(command); ymx_debug_msg("Exiting mexFunction\n"); }
/** * Helper function for commands in their most general form: no option * flags and just pushing all (database) arguments over to merovingian * for performing merocmd action. */ static void simple_command(int argc, char *argv[], char *merocmd, char *successmsg, char glob) { int i; sabdb *orig = NULL; sabdb *stats = NULL; char *e; if (argc == 1) { /* print help message for this command */ command_help(2, &argv[-1]); exit(1); } /* walk through the arguments and hunt for "options" */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--") == 0) { argv[i] = NULL; break; } if (argv[i][0] == '-') { fprintf(stderr, "%s: unknown option: %s\n", argv[0], argv[i]); command_help(argc + 1, &argv[-1]); exit(1); } } if (glob) { if ((e = MEROgetStatus(&orig, NULL)) != NULL) { fprintf(stderr, "%s: %s\n", argv[0], e); free(e); exit(2); } stats = globMatchDBS(argc, argv, &orig, argv[0]); msab_freeStatus(&orig); orig = stats; if (orig == NULL) exit(1); } else { for (i = 1; i < argc; i++) { if (argv[i] != NULL) { /* maintain input order */ if (orig == NULL) { stats = orig = calloc(1, sizeof(sabdb)); } else { stats = stats->next = calloc(1, sizeof(sabdb)); } stats->dbname = strdup(argv[i]); } } } simple_argv_cmd(argv[0], orig, merocmd, successmsg, NULL); msab_freeStatus(&orig); }
int main(int argc, char *argv[]) #endif /* brcm end */ { cmd_t *cmd; struct ifreq ifr; int skfd; int rc; if (argc < 3) { help(); return -1; } cmd = command_lookup(argv[2]); if (cmd == NULL) { fprintf(stderr, "invalid command [%s]\n", argv[2]); help(); return -1; } if (argc < cmd->nargs + 3) { fprintf(stderr, "incorrect number of arguments for command\n"); command_help(cmd); return -1; } if ( (skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { fprintf(stderr, "socket open error\n"); return -1; } if (strncmp(argv[1], "eth", strlen("eth")) == 0) { strcpy(ifr.ifr_name, argv[1]); if ( ioctl(skfd, SIOCGIFINDEX, &ifr) < 0 ) { fprintf(stderr, "invalid interface name %s\n", argv[1]); command_help(cmd); close(skfd); return -1; } } else { fprintf(stderr, "invalid interface name %sx\n", argv[1]); command_help(cmd); close(skfd); return -1; } rc = cmd->func(skfd, &ifr, cmd, argv); close(skfd); return rc; }
/* HELP <command> [params] */ static void os_cmd_help(char *origin) { char *command = strtok(NULL, ""); if (!has_any_privs(user_find_named(origin))) { notice(opersvs.nick, origin, "You are not authorized to use %s.", opersvs.nick); return; } if (!command) { notice(opersvs.nick, origin, "***** \2%s Help\2 *****", opersvs.nick); notice(opersvs.nick, origin, "\2%s\2 provides essential network management services, such as", opersvs.nick); notice(opersvs.nick, origin, "routing manipulation and access restriction. Please do not abuse"); notice(opersvs.nick, origin, "your access to \2%s\2!", opersvs.nick); notice(opersvs.nick, origin, " "); notice(opersvs.nick, origin, "For information on a command, type:"); notice(opersvs.nick, origin, "\2/%s%s help <command>\2", (ircd->uses_rcommand == FALSE) ? "msg " : "", opersvs.disp); notice(opersvs.nick, origin, " "); command_help(opersvs.nick, origin, os_cmdtree); notice(opersvs.nick, origin, "***** \2End of Help\2 *****", opersvs.nick); return; } /* take the command through the hash table */ help_display(opersvs.nick, opersvs.disp, origin, command, os_helptree); }
/** * Runs computations and stores matrices based on given input */ void compute_engine(void) { g_entries = calloc(MAX_ENTRIES, sizeof(entry*)); while (true) { printf("> "); char line[MAX_BUFFER]; if (fgets(line, MAX_BUFFER, stdin) == NULL) { command_bye(); } char command[MAX_BUFFER]; if (sscanf(line, "%s", command) != 1) { printf("\n"); continue; } if (strcasecmp(command, "bye") == 0) { command_bye(); } else if (strcasecmp(command, "help") == 0) { command_help(); } else if (strcasecmp(command, "set") == 0) { command_set(line); } else if (strcasecmp(command, "show") == 0) { command_show(line); } else if (strcasecmp(command, "compute") == 0) { command_compute(line); } else { puts("invalid command"); } printf("\n"); } }
void command_helpall(void) { int i; for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) command_help(commands+i); }
/* HELP <command> [params] */ static void os_cmd_help(sourceinfo_t *si, int parc, char *parv[]) { char *command = parv[0]; if (!has_any_privs(si)) { command_fail(si, fault_noprivs, _("You are not authorized to use %s."), si->service->nick); return; } if (!command) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick); command_success_nodata(si, _("\2%s\2 provides essential network management services, such as\n" "routing manipulation and access restriction. Please do not abuse\n" "your access to \2%s\2!"), si->service->nick, si->service->nick); command_success_nodata(si, " "); command_success_nodata(si, _("For information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp); command_success_nodata(si, " "); command_help(si, si->service->commands); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } /* take the command through the hash table */ help_display(si, si->service, command, si->service->commands); }
/* * Function: commandShell(char *command) * Parameters: None * Returns: None * Description: This functions behaves like shell answering all user commands */ void Server::commandShell(char *command) { CommandID command_id = getCommandID(command); switch(command_id) { case COMMAND_HELP: // handle help command command_help(); break; case COMMAND_CREATOR: // handle CREATOR command command_creator(); break; case COMMAND_DISPLAY: // handle DISPLAY command command_display(); break; case COMMAND_LIST: // handle LIST command command_list(); break; default: printf("Please enter a valid command \n"); printf("Type Help - to display supported commands \n"); break; } }
void command_dev(char *arg) { static char savedevname[MAXDEVNAME + 1]; char *fsname, *devname; const char *file; /* dummy */ if (*arg == '\0') { biosdisk_probe(); printf("default %s%d%c\n", default_devname, default_unit, 'a' + default_partition); return; } if (strchr(arg, ':') == NULL || parsebootfile(arg, &fsname, &devname, &default_unit, &default_partition, &file)) { command_help(NULL); return; } /* put to own static storage */ strncpy(savedevname, devname, MAXDEVNAME + 1); default_devname = savedevname; }
/** * Helper function to run over the sabdb list and perform merocmd for * the value and reporting status on the performed command. Either a * message is printed when success, or when premsg is not NULL, premsg * is printed before the action, and "done" printed afterwards. */ static void simple_argv_cmd(char *cmd, sabdb *dbs, char *merocmd, char *successmsg, char *premsg) { int state = 0; /* return status */ int hadwork = 0; /* if we actually did something */ char *ret; char *out; /* do for each listed database */ for (; dbs != NULL; dbs = dbs->next) { if (premsg != NULL && !monetdb_quiet) { printf("%s '%s'... ", premsg, dbs->dbname); fflush(stdout); } ret = control_send(&out, mero_host, mero_port, dbs->dbname, merocmd, 0, mero_pass); if (ret != NULL) { if (premsg != NULL && !monetdb_quiet) printf("FAILED\n"); fprintf(stderr, "%s: %s\n", cmd, ret); free(ret); exit(2); } if (strcmp(out, "OK") == 0) { if (!monetdb_quiet) { if (premsg != NULL) { printf("done\n"); } else { printf("%s: %s\n", successmsg, dbs->dbname); } } } else { if (premsg != NULL && !monetdb_quiet) printf("FAILED\n"); fprintf(stderr, "%s: %s\n", cmd, out); free(out); state |= 1; } hadwork = 1; } if (hadwork == 0) { char *argv[2] = { "monetdb", cmd }; command_help(2, argv); exit(1); } if (state != 0) exit(state); }
/* * Main command */ void command_part(int argc, const char **argv) { if (argc == 1) { printk("invalid 'part' command: too few arguments\n"); command_help(0, NULL); return; } execsubcmd(part_cmds, argc-1, argv+1); }
static void bs_help_set(sourceinfo_t *si) { command_success_nodata(si, _("Help for \2SET\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("Configures different botserv bot options.")); command_success_nodata(si, " "); command_help(si, &bs_set_cmdtree); command_success_nodata(si, " "); command_success_nodata(si, _("For more specific help use \2/msg %s HELP SET \37command\37\2."), si->service->disp); }
static void cs_help_clear(sourceinfo_t *si) { command_success_nodata(si, _("Help for \2CLEAR\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("CLEAR allows you to clear various aspects of a channel.")); command_success_nodata(si, " "); command_help(si, &cs_clear_cmds); command_success_nodata(si, " "); command_success_nodata(si, _("For more information, use \2/msg %s HELP CLEAR \37command\37\2."), si->service->disp); }
static void help_cmd_info(const char *cmd) { int cmd_id; if((cmd_id = command_search(cmd)) == -1) { R_UTILS_ERR("%s isn't a valid command, see the help.", cmd); } else if(cmd_id == -2) { R_UTILS_ERR("Too much commands match %s, be more precise.", cmd); } command_help(cmd_id); exit(EXIT_FAILURE); }
static void cs_help_clear(sourceinfo_t *si, const char *subcmd) { if (!subcmd) { command_success_nodata(si, _("***** \2%s Help\2 *****"), chansvs.me->disp); command_success_nodata(si, _("Help for \2CLEAR\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("CLEAR allows you to clear various aspects of a channel.")); command_success_nodata(si, " "); command_help(si, cs_clear_cmds); command_success_nodata(si, " "); command_success_nodata(si, _("For more information, use \2/msg %s HELP CLEAR \37command\37\2."), chansvs.me->disp); command_success_nodata(si, _("***** \2End of Help\2 *****")); } else help_display(si, si->service, subcmd, cs_clear_cmds); }
static void bs_help_set(sourceinfo_t *si, const char *subcmd) { if (!subcmd) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->disp); command_success_nodata(si, _("Help for \2SET\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("Configures different botserv bot options.")); command_success_nodata(si, " "); command_help(si, bs_set_cmdtree); command_success_nodata(si, " "); command_success_nodata(si, _("For more specific help use \2/msg %s HELP SET \37command\37\2."), si->service->disp); command_success_nodata(si, _("***** \2End of Help\2 *****")); } else help_display_as_subcmd(si, si->service, "SET", subcmd, bs_set_cmdtree); }
/* * command loop. * * This is the main body of gtags-cscope. */ void command_loop(void) { int com = 0; char *line; print_case_distinction(); while ((line = get_line()) != NULL) { switch (com = *line++) { /* * Control command */ case 'c': /* c: ignore case or not */ ignore_case ^= 1; print_case_distinction(); break; case 'q': /* q: quit the program */ return; case 'r': /* r: update tag file */ update(); break; case 'h': /* h: show help */ command_help(); break; /* * Search command */ case '0': case '1': case '2': case '3': case '4': case '6': case '7': case '8': search(com, line); break; default: fputs("cscope: 0 lines\n", stdout); fflush(stdout); break; } } }
static void gs_help_set(sourceinfo_t *si, const char *subcmd) { if (!subcmd) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->disp); command_success_nodata(si, _("Help for \2SET\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("SET allows you to set various control flags\n" "for groups that change the way certain\n" "operations are performed on them.")); command_success_nodata(si, " "); command_help(si, gs_set_cmdtree); command_success_nodata(si, " "); command_success_nodata(si, _("For more specific help use \2/msg %s HELP SET \37command\37\2."), si->service->disp); command_success_nodata(si, _("***** \2End of Help\2 *****")); } else help_display_as_subcmd(si, si->service, "SET", subcmd, gs_set_cmdtree); }
static void command_option(char *subcmd) { char *opt; int i; opt = get_next_arg(subcmd); /* dispatch subcommand */ for (i = 0; opt_subcommands[i].c_name != NULL; i++) { if (strcmp(subcmd, opt_subcommands[i].c_name) == 0) { opt_subcommands[i].c_fn(opt); break; } } if (opt_subcommands[i].c_name == NULL) { printf("unknown option subcommand\n"); command_help(NULL); } }
void gs_cmd_help(sourceinfo_t *si, int parc, char *parv[]) { if (!parv[0]) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick); command_success_nodata(si, _("\2%s\2 provides tools for managing groups of users and channels."), si->service->nick); command_success_nodata(si, " "); command_success_nodata(si, _("For more information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp); command_success_nodata(si, " "); command_help(si, si->service->commands); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } /* take the command through the hash table */ help_display(si, si->service, parv[0], si->service->commands); }
/* HELP SET */ static void os_help_set(sourceinfo_t *si, const char *subcmd) { if (!subcmd) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick); command_success_nodata(si, _("Help for \2SET\2:")); command_success_nodata(si, " "); command_success_nodata(si, _("SET allows you to set various control flags\n" "for services that changes the way certain\n" "operations are performed.")); command_success_nodata(si, _("Note that all settings will be reset to the values\n" "in the configuration file on rehash or services restart.")); command_success_nodata(si, " "); command_help(si, os_set_cmdtree); command_success_nodata(si, " "); command_success_nodata(si, _("For more information, use \2/msg %s HELP SET \37command\37\2."), si->service->nick); command_success_nodata(si, _("***** \2End of Help\2 *****")); } else help_display_as_subcmd(si, si->service, "SET", subcmd, os_set_cmdtree); }
static void bootmenu(void) { char input[LINEBUFLEN]; char *cmd; char *opt; int i; for (;;) { /* input a line */ input[0] = '\0'; printf("> "); gets(input); cmd = input; /* skip leading whitespace. */ while(*cmd == ' ') cmd++; if(*cmd) { /* here, some command entered */ opt = get_next_arg(cmd); /* dispatch command */ for (i = 0; commands[i].c_name != NULL; i++) { if (strcmp(cmd, commands[i].c_name) == 0) { commands[i].c_fn(opt); break; } } if (commands[i].c_name == NULL) { printf("unknown command\n"); command_help(NULL); } } } }
/* HELP <command> [params] */ void hs_cmd_help(sourceinfo_t *si, int parc, char *parv[]) { char *command = parv[0]; if (!command) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick); command_success_nodata(si, _("\2%s\2 allows users to request a virtual hostname."), si->service->nick); command_success_nodata(si, " "); command_success_nodata(si, _("For more information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp); command_success_nodata(si, " "); command_help(si, si->service->commands); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } /* take the command through the hash table */ help_display(si, si->service, command, si->service->commands); }
/* HELP <command> [params] */ void gs_cmd_help(sourceinfo_t *si, int parc, char *parv[]) { char *command = parv[0]; if (!command) { command_success_nodata(si, _("***** \2%s Help\2 *****"), gamesvs.nick); command_success_nodata(si, _("\2%s\2 provides games and tools for playing games to the network."), gamesvs.nick); command_success_nodata(si, " "); command_success_nodata(si, _("For more information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", gamesvs.me->disp); command_success_nodata(si, " "); command_help(si, gs_cmdtree); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } /* take the command through the hash table */ help_display(si, command, gs_helptree); }
/* HELP <command> [params] */ void ms_cmd_help(sourceinfo_t *si, int parc, char *parv[]) { char *command = parv[0]; if (!command) { command_success_nodata(si, _("***** \2%s Help\2 *****"), memosvs.nick); command_success_nodata(si, _("\2%s\2 allows users to send memos to registered users."), memosvs.nick); command_success_nodata(si, " "); command_success_nodata(si, _("For more information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", memosvs.me->disp); command_success_nodata(si, " "); command_help(si, ms_cmdtree); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } /* take the command through the hash table */ help_display(si, command, ms_helptree); }
/* HELP <command> [params] */ void ms_cmd_help(char *origin) { user_t *u = user_find_named(origin); char *command = strtok(NULL, ""); if (!command) { notice(memosvs.nick, origin, "***** \2%s Help\2 *****", memosvs.nick); notice(memosvs.nick, origin, "\2%s\2 allows users to send memos to registered users.", memosvs.nick); notice(memosvs.nick, origin, " "); notice(memosvs.nick, origin, "For more information on a command, type:"); notice(memosvs.nick, origin, "\2/%s%s help <command>\2", (ircd->uses_rcommand == FALSE) ? "msg " : "", memosvs.disp); notice(memosvs.nick, origin, " "); command_help(memosvs.nick, origin, ms_cmdtree); notice(memosvs.nick, origin, "***** \2End of Help\2 *****"); return; } /* take the command through the hash table */ help_display(memosvs.nick, memosvs.disp, origin, command, ms_helptree); }
void ss_cmd_help(sourceinfo_t * si, int parc, char *parv[]) { char *command = parv[0]; if (!command) { command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick); command_success_nodata(si, _("\2%s\2 records various network statistics."), si->service->nick); command_success_nodata(si, " "); command_success_nodata(si, _("For more information on a command, type:")); command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp); command_success_nodata(si, " "); command_help(si, si->service->commands); command_success_nodata(si, _("***** \2End of Help\2 *****")); return; } help_display(si, si->service, command, si->service->commands); }
/* HELP SET */ static void ns_help_set(sourceinfo_t *si, const char *subcmd) { if (!subcmd) { command_success_nodata(si, _("***** \2%s Help\2 *****"), nicksvs.nick); command_success_nodata(si, _("Help for \2SET\2:")); command_success_nodata(si, " "); if (nicksvs.no_nick_ownership) command_success_nodata(si, _("SET allows you to set various control flags\n" "for accounts that change the way certain\n" "operations are performed on them.")); else command_success_nodata(si, _("SET allows you to set various control flags\n" "for nicknames that change the way certain\n" "operations are performed on them.")); command_success_nodata(si, " "); command_help(si, ns_set_cmdtree); command_success_nodata(si, " "); command_success_nodata(si, _("For more information, use \2/msg %s HELP SET \37command\37\2."), nicksvs.nick); command_success_nodata(si, _("***** \2End of Help\2 *****")); } else help_display_as_subcmd(si, si->service, "SET", subcmd, ns_set_cmdtree); }
int main (int argc, char **argv) { unsigned char *buf; long bufSize = 0; unsigned char *original; long originalSize = 0; unsigned char *originalGray = NULL; long originalGraySize = 0; unsigned char *compressed = NULL; unsigned long compressedSize = 0; unsigned char *compressedGray; long compressedGraySize = 0; unsigned char *tmpImage; int width, height; unsigned char *metaBuf; unsigned int metaSize = 0; FILE *file; // Parse commandline options command_t cmd; command_init(&cmd, argv[0], VERSION); cmd.usage = "[options] input.jpg compressed-output.jpg"; command_option(&cmd, "-t", "--target [arg]", "Set target quality [0.9999]", setTarget); command_option(&cmd, "-q", "--quality [arg]", "Set a quality preset: low, medium, high, veryhigh [medium]", setQuality); command_option(&cmd, "-n", "--min [arg]", "Minimum JPEG quality [40]", setMinimum); command_option(&cmd, "-x", "--max [arg]", "Maximum JPEG quality [95]", setMaximum); command_option(&cmd, "-l", "--loops [arg]", "Set the number of runs to attempt [6]", setAttempts); command_option(&cmd, "-a", "--accurate", "Favor accuracy over speed", setAccurate); command_option(&cmd, "-m", "--method [arg]", "Set comparison method to one of 'mpe', 'ssim', 'ms-ssim', 'smallfry' [ssim]", setMethod); command_option(&cmd, "-s", "--strip", "Strip metadata", setStrip); command_option(&cmd, "-d", "--defish [arg]", "Set defish strength [0.0]", setDefish); command_option(&cmd, "-z", "--zoom [arg]", "Set defish zoom [1.0]", setZoom); command_option(&cmd, "-r", "--ppm", "Parse input as PPM instead of JPEG", setPpm); command_option(&cmd, "-c", "--no-copy", "Disable copying files that will not be compressed", setCopyFiles); command_option(&cmd, "-p", "--no-progressive", "Disable progressive encoding", setNoProgressive); command_parse(&cmd, argc, argv); if (cmd.argc < 2) { command_help(&cmd); return 255; } if (method == UNKNOWN) { fprintf(stderr, "Invalid method!"); command_help(&cmd); return 255; } // No target passed, use preset! if (!target) { setTargetFromPreset(); } // Read original bufSize = readFile((char *) cmd.argv[0], (void **) &buf); if (!bufSize) { return 1; } if (!ppm) { // Decode the JPEG originalSize = decodeJpeg(buf, bufSize, &original, &width, &height, JCS_RGB); } else { // Decode the PPM originalSize = decodePpm(buf, bufSize, &original, &width, &height); } if (defishStrength) { fprintf(stderr, "Defishing...\n"); tmpImage = malloc(width * height * 3); defish(original, tmpImage, width, height, 3, defishStrength, defishZoom); free(original); original = tmpImage; } // Convert RGB input into Y originalGraySize = grayscale(original, &originalGray, width, height); if (!ppm) { // Read metadata (EXIF / IPTC / XMP tags) if (getMetadata(buf, bufSize, &metaBuf, &metaSize, COMMENT)) { fprintf(stderr, "File already processed by jpeg-recompress!\n"); if (copyFiles) { file = openOutput(cmd.argv[1]); fwrite(buf, bufSize, 1, file); fclose(file); free(buf); return 0; } else { free(buf); return 2; } } } if (strip) { // Pretend we have no metadata metaSize = 0; } else { fprintf(stderr, "Metadata size is %ukb\n", metaSize / 1024); } if (!originalSize || !originalGraySize) { return 1; } // Do a binary search to find the optimal encoding quality for the // given target SSIM value. int min = jpegMin, max = jpegMax; for (int attempt = attempts - 1; attempt >= 0; --attempt) { float metric; int quality = min + (max - min) / 2; int progressive = attempt ? 0 : !noProgressive; int optimize = accurate ? 1 : (attempt ? 0 : 1); // Recompress to a new quality level, without optimizations (for speed) compressedSize = encodeJpeg(&compressed, original, width, height, JCS_RGB, quality, progressive, optimize); // Load compressed luma for quality comparison compressedGraySize = decodeJpeg(compressed, compressedSize, &compressedGray, &width, &height, JCS_GRAYSCALE); if (!compressedGraySize) { fprintf(stderr, "Unable to decode file that was just encoded!\n"); return 1; } if (!attempt) { fprintf(stderr, "Final optimized "); } // Measure quality difference switch (method) { case MS_SSIM: metric = iqa_ms_ssim(originalGray, compressedGray, width, height, width, 0); fprintf(stderr, "ms-ssim"); break; case SMALLFRY: metric = smallfry_metric(originalGray, compressedGray, width, height); fprintf(stderr, "smallfry"); break; case MPE: metric = meanPixelError(originalGray, compressedGray, width, height, 1); fprintf(stderr, "mpe"); break; case SSIM: default: metric = iqa_ssim(originalGray, compressedGray, width, height, width, 0, 0); fprintf(stderr, "ssim"); break; } if (attempt) { fprintf(stderr, " at q=%i (%i - %i): %f\n", quality, min, max, metric); } else { fprintf(stderr, " at q=%i: %f\n", quality, metric); } if (metric < target) { if (compressedSize >= bufSize) { fprintf(stderr, "Output file would be larger than input!\n"); free(compressed); free(compressedGray); if (copyFiles) { file = openOutput(cmd.argv[1]); fwrite(buf, bufSize, 1, file); fclose(file); free(buf); return 0; } else { free(buf); return 1; } } switch (method) { case SSIM: case MS_SSIM: case SMALLFRY: // Too distorted, increase quality min = quality + 1; break; case MPE: // Higher than required, decrease quality max = quality - 1; break; } } else { switch (method) { case SSIM: case MS_SSIM: case SMALLFRY: // Higher than required, decrease quality max = quality - 1; break; case MPE: // Too distorted, increase quality min = quality + 1; break; } } // If we aren't done yet, then free the image data if (attempt) { free(compressed); free(compressedGray); } } free(buf); // Calculate and show savings, if any int percent = (compressedSize + metaSize) * 100 / bufSize; unsigned long saved = (bufSize > compressedSize) ? bufSize - compressedSize - metaSize : 0; fprintf(stderr, "New size is %i%% of original (saved %lu kb)\n", percent, saved / 1024); if (compressedSize >= bufSize) { fprintf(stderr, "Output file is larger than input, aborting!\n"); return 1; } // Open output file for writing file = openOutput(cmd.argv[1]); // Write output fwrite(compressed, 20, 1, file); /* 0xffd8 and JFIF marker */ // Write comment so we know not to reprocess this file // in the future if it gets passed in again. // 0xfffe (COM marker), two-byte big-endian length, string fputc(0xff, file); fputc(0xfe, file); fputc(0x00, file); fputc(32, file); fwrite(COMMENT, 30, 1, file); // Write metadata markers if (!strip && !ppm) { fwrite(metaBuf, metaSize, 1, file); } // Write image data fwrite(compressed + 20, compressedSize - 20, 1, file); fclose(file); // Cleanup command_free(&cmd); if (!strip && !ppm) { free(metaBuf); } free(compressed); free(original); free(originalGray); return 0; }
/* Main function. */ int main(int argc, char *argv[]) { cli_t cli; char *hostname = DEFAULT_HOST; char history_file[4096]; ybool_t interactive_mode = YTRUE; bzero(&cli, sizeof(cli_t)); cli.autocheck = YTRUE; if (argc == 2 && argv[1][0] != '-') hostname = argv[1]; if (argc == 3 && !strcmp(argv[2], "-")) interactive_mode = YFALSE; // init database connection if ((cli.finedb = finedb_create(hostname, 11138)) == NULL) { printf_color("red", "Memory error."); printf("\n"); exit(1); } if (finedb_connect(cli.finedb) != FINEDB_OK) { printf_color("red", "Unable to connect to server '%s' on port '%d'.", argv[1], 11138); printf("\n"); exit(2); } // interactive mode init if (interactive_mode) { char *home = NULL; if ((home = getenv("HOME")) != NULL) { FILE *hist; snprintf(history_file, sizeof(history_file), "%s/%s", home, HISTORY_FILE); if ((hist = fopen(history_file, "w+")) != NULL) { fclose(hist); linenoiseHistoryLoad(HISTORY_FILE); } linenoiseSetCompletionCallback(cli_completion); } } // main loop for (; ; ) { char buff[4096], *line = NULL, *pt, *cmd; if (!interactive_mode) { ssize_t bufsz, linesz = 0; while ((bufsz = read(0, buff, sizeof(buff))) > 0) { pt = (char*)malloc(linesz + bufsz + 1); memcpy(pt, line, linesz); memcpy((void*)((size_t)pt + linesz), buff, bufsz); linesz += bufsz; pt[linesz] = '\0'; if (line) free(line); line = pt; } } else { snprintf(buff, sizeof(buff), "%s > ", (cli.dbname ? cli.dbname : "default")); if ((line = linenoise(buff)) == NULL) break; } pt = line; LTRIM(pt); cmd = pt; // add command line to history linenoiseHistoryAdd(cmd); linenoiseHistorySave(history_file); // extract the command while (*pt && !IS_SPACE(*pt)) ++pt; *pt++ = '\0'; LTRIM(pt); /* command management */ if (cmd[0] == '\0') goto reloop; //continue; // local commands, no need for a running connection if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit")) exit(0); if (!strcasecmp(cmd, "help") || cmd[0] == '?') { command_help(); goto reloop; //continue; } else if (!strcasecmp(cmd, "sync")) { command_sync(&cli); goto reloop; //continue; } else if (!strcasecmp(cmd, "async")) { command_async(&cli); goto reloop; //continue; } // commands that need a running connection if (!strcasecmp(cmd, "use")) command_use(&cli, pt); else if (!strcasecmp(cmd, "get")) command_get(&cli, pt); else if (!strcasecmp(cmd, "del")) command_del(&cli, pt); else if (!strcasecmp(cmd, "put")) command_send_data(&cli, pt, YFALSE, YFALSE); else if (!strcasecmp(cmd, "add")) command_send_data(&cli, pt, YTRUE, YFALSE); else if (!strcasecmp(cmd, "update")) command_send_data(&cli, pt, YFALSE, YTRUE); else if (!strcasecmp(cmd, "inc")) command_inc(&cli, pt); else if (!strcasecmp(cmd, "dec")) command_dec(&cli, pt); else if (!strcasecmp(cmd, "start")) command_start(&cli); else if (!strcasecmp(cmd, "stop")) command_stop(&cli); #if 0 else if (!strcasecmp(cmd, "list")) command_list(&cli, pt); #endif else if (!strcasecmp(cmd, "ping")) command_ping(&cli); else if (!strcasecmp(cmd, "autocheck")) command_autocheck(&cli, pt); else { printf_color("red", "Bad command."); printf("\n"); } reloop: free(line); } return (0); }