my_bool memc_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { memcached_return rc; memc_function_st *container; container= prepare_args(args, message, MEMC_DELETE, 1, 2); if (container == NULL) return 1; /* Init the memcached_st we will use for this pass */ rc= memc_get_servers(&container->memc); initid->ptr= (char *)container; return 0; }
my_bool memc_set_by_key_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { memcached_return rc; memc_function_st *container; container= prepare_args(args, message, MEMC_SET_BY_KEY, 3, 4); if (container == NULL) return 1; /* Init the memcached_st we will use for this pass */ rc= memc_get_servers(args, &container->memc); initid->ptr= (char *)container; return (rc == MEMCACHED_SUCCESS ? 0 : 1); }
my_bool memc_replace_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { unsigned int x; memcached_return rc; memc_function_st *container; container= prepare_args(args, message, MEMC_REPLACE, 2, 3); if (container == NULL) return 1; /* Init the memcached_st we will use for this pass */ rc= memc_get_servers(&container->memc); initid->ptr= (char *)container; return 0; }
my_bool memc_prepend_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { memcached_return rc; memc_function_st *container; container= prepare_args(args, message, MEMC_PREPEND, 2, 3); if (container == NULL) { fprintf(stderr, "errors\n"); return 1; } /* Init the memcached_st we will use for this pass */ rc= memc_get_servers(&container->memc); initid->ptr= (char *)container; return 0; }
/* * args_ok * * prepare args, check & parse user args, display error and * help message if neccessary * * result: TRUE, if all args ok */ BOOL args_ok(HSCPRC * hp, int argc, char *argv[]) { BOOL ok; /* return value */ DLLIST *ignore_list = NULL; /* dummy */ EXPSTR *destdir = init_estr(32); /* destination dir */ EXPSTR *rel_destdir = init_estr(32); /* relative destination dir */ EXPSTR *kack_name = init_estr(0); /* temp. str for outfilename */ struct arglist *hsc_args; /* argument structure */ arg_hp = hp; arg_mode_CB(DEFAULT_MODE_STR); /* create arg-table */ hsc_args = prepare_args("HSC_ARGS", /* file args */ "FROM/M", &incfile, "include- and input-file(s)", "TO/K", &arg_outfname, "output file (default: stdout)", "PRJFILE/T/K", &prjfilename, "project file (default: none)", "PREFSFILE/T/K", &prefsfilename, "syntax preferences (default: hsc.prefs)", "MSGFILE=MF/T/K", &msgfilename, "message file (default: stderr)", "MSGFORMAT/T/K", &msg_format, "how to display message", /* numeric */ "MAXERR/N/K", &max_error, "max. number of errors (default: 20)", "EXTENSION/T/K", &arg_extension, "output-file-extension (default: " DEFAULT_EXTENSION ")", "DEFINE=DEF/T/K/M", &define_list, "define global attribute", "IGNORE=IGN/N/K/M/$", arg_ignore_CB, &ignore_list, "ignore message number", "MODE/E/K/$", arg_mode_CB, MODE_ENUMSTR, &arg_mode, "mode for syntax check (" MODE_ENUMSTR ")", "QUOTESTYLE=QS/E/K", QMODE_ENUMSTR, &arg_quotemode, "defines how quotes appear (" QMODE_ENUMSTR ")", #if 0 "ENTITYSTYLE=ES/E/K", EMODE_ENUMSTR, &entmode, "defines how special chars. appear (" EMODE_ENUMSTR ")", /* switches */ #endif "COMPACT=CO/S", &arg_compact, "strip useless LFs and white-spaces", "GETSIZE/S", &arg_getsize, "get width and height of images", "MSGANSI/S", &msg_ansi, "use ansi-sequences in messages", "RPLCENT=RE/S", &arg_rplc_ent, "replace special characters", "RPLCQUOTE=RQ/S", &arg_rplc_quote, "replace quotes in text by `"'", "SMARTENT=SA/S", &arg_smart_ent, "replace special entities (`&<>\"')", "JENS/S", &arg_jens, "don't try this at home", "STRIPCOMMENT=SC/S", &arg_strip_cmt, "strip SGML-comments", "STRIPEXTERNAL=SX/S", &arg_strip_ext, "strip tags with external URIs", "STRIPTAGS=ST/K", &arg_striptags, "tags to be stripped", "ICONBASE/T/K", &arg_iconbase, "base-uri for icon-entities", "STATUS/E/K/$", arg_status_CB, STATUS_ENUM_STR, &disp_status, "status message (" STATUS_ENUM_STR ")", "-DEBUG/S", &arg_debug, "enable debugging output", /* help */ "HELP=?/S", &arg_help, "display this text", "LICENSE/S", &arg_license, "display license", NULL); /* remove dummy list TODO: this sucks */ del_dllist(ignore_list); ok = (hsc_args != NULL); /* set & test args */ if (ok) { BOOL use_stdout = FALSE; /* flag: use stdout as output-file */ ok = set_args(argc, argv, hsc_args); /* display help, if requested vie HELP switch, or no * input to pipe or read is passed */ ok &= (!arg_help && (arg_pipe_in || (incfile && dll_first(incfile)))); if (arg_license) { /* display license text */ fprintf_prginfo(stderr); show_license(); set_return_code(RC_WARN); } else if (!ok) { /* display help, if error in args or HELP-switch set */ fprintf_prginfo(stderr); fprintf_arghelp(stderr, hsc_args); set_return_code(RC_WARN); } else { BOOL fnsux = FALSE; /* flag: TRUE = can't evaluate out-filename */ /* set debugging switch */ hsc_set_debug(hp, arg_debug); /* autoset depending options */ if (hsc_get_debug(hp)) disp_status = STATUS_VERBOSE; /* set default options */ if (!arg_extension) arg_extension = DEFAULT_EXTENSION; /* disable ID-warning if no project-file */ if (!prjfilename) hsc_set_msg_ignore(hp, MSG_NO_DOCENTRY, TRUE); /* compute name of input file */ arg_inpfname = NULL; if (dll_first(incfile) && !arg_pipe_in) { /* use last FROM as input file */ arg_inpfname = dln_data(dll_last(incfile)); set_estr(inpfilename, arg_inpfname); /* get path part of inputfilename as relative * destination directory */ get_fpath(rel_destdir, arg_inpfname); /* TODO: set reldir when including first file */ /* TODO: find out why the above TODO is there */ /* remove input filename from incfile */ del_dlnode(incfile, dll_last(incfile)); D(fprintf(stderr, DHSC "input : use `%s'\n" DHSC "reldir: use `%s'\n", estr2str(inpfilename), estr2str(rel_destdir))); } /* display include files */ D( { DLNODE * nd = dll_first(incfile); while (nd) { fprintf(stderr, DHSC "includ: use `%s'\n", ( STRPTR) dln_data(nd)); nd = dln_next(nd); } } ); /* * if no output-filename given, * outfilename stays NULL. this let open_output * open stdout as output-file */ if (arg_outfname) { /* check, if last char of outputfilename is a * directory separator; if so, use the filename * as destination directory */ if (arg_outfname) { UBYTE lastch = 0; /* get last char of outfname to determine * if it's a directory */ if (strlen(arg_outfname)) lastch = arg_outfname[strlen(arg_outfname) - 1]; #ifdef AMIGA /* for Amiga, execpt empty string for current dir */ if (!lastch) { lastch = (PATH_SEPARATOR[0]); D(fprintf(stderr, DHSC "AMIGA: use current dir\n")); } #endif if (strchr(PATH_SEPARATOR, lastch)) { /* use outfilename as destdir */ set_estr(destdir, arg_outfname); arg_outfname = NULL; D(fprintf(stderr, DHSC "output: use `%s' as destdir\n", estr2str(destdir))); } else if (arg_inpfname) { /* output-filename already specified */ /* separate it to destdir + reldir + name */ EXPSTR *kack_destdir = init_estr(0); EXPSTR *kack_reldir = init_estr(0); STRPTR inp_reldir = estr2str(rel_destdir); STRPTR out_reldir = NULL; STRPTR ou2_reldir = NULL; get_fname(kack_name, arg_outfname); get_fpath(kack_destdir, arg_outfname); /* check corresponding dirs for * consistency: check if last strlen(rel_destdir) * chars are equal */ out_reldir = estr2str(kack_destdir); ou2_reldir = out_reldir; out_reldir = out_reldir + (strlen(out_reldir) - strlen(inp_reldir)); if (out_reldir[0]) { /* search for next dir-sparator backwards */ /* (this ones only needed for a smart error message) */ while ((out_reldir != ou2_reldir) && (!strchr(PATH_SEPARATOR, out_reldir[0])) ) { out_reldir--; } out_reldir++; } D(fprintf(stderr, DHSC "corr_inp: `%s'\n" DHSC "corr_out: `%s'\n", inp_reldir, out_reldir) ); /* check if correspondig relative in/out-dirs * are equal */ if (!fnamecmp(inp_reldir, out_reldir)) { /* they match.. */ STRPTR tmp_name = NULL; /* copy of kack_nam */ /* cut corresponding chars */ get_left_estr(kack_destdir, kack_destdir, estrlen(kack_destdir) - strlen(out_reldir)); set_estr(kack_reldir, inp_reldir); D(fprintf(stderr, DHSC "kack_dst: `%s'\n" DHSC "kack_rel: `%s'\n" DHSC "kack_nam: `%s'\n", estr2str(kack_destdir), estr2str(kack_reldir), estr2str(kack_name)) ); /* just copy these values where they are * expected to be */ estrcpy(destdir, kack_destdir); estrcpy(rel_destdir, kack_reldir); /* create output filename */ tmp_name = strclone(estr2str(kack_name)); estrcpy(kack_name, kack_destdir); estrcat(kack_name, kack_reldir); app_estr(kack_name, tmp_name); ufreestr(tmp_name); arg_outfname = estr2str(kack_name); } else { /* unmatched corresponding dirs */ fprintf(stderr, "unmatched corresponding relative directories:\n" " input `%s'\n output `%s'\n", inp_reldir, out_reldir); ok = FALSE; } /* free temp. vars */ del_estr(kack_reldir); del_estr(kack_destdir); } } if (arg_outfname) { /* set outputfilename with value passed iwithin args */ outfilename = init_estr(32); set_estr(outfilename, arg_outfname); D(fprintf(stderr, DHSC "output: set to `%s'\n", estr2str(outfilename))); } else { if (!arg_pipe_in) { /* no outfilename given */ /* ->outfilename = destdir + inpfilename + ".html" */ /* link destdir & input filename */ outfilename = init_estr(32); link_fname(outfilename, estr2str(destdir), arg_inpfname); if (strcmp(arg_extension, ".")) set_fext(outfilename, arg_extension); D(fprintf(stderr, DHSC "output: concat destdir+inpfile+`.%s'\n" DHSC "output: set to `%s'\n", arg_extension, estr2str(outfilename))); } else fnsux = TRUE; } if (fnsux) { /* no way to find out output filename */ status_error("unable to evaluate output filename\n"); arg_outfname = NULL; ok = FALSE; } } else { D(fprintf(stderr, DHSC "output: use stdout\n")); use_stdout = TRUE; } if (!ok) set_return_code(RC_ERROR); } if (ok) { if (arg_iconbase) hsc_set_iconbase(hp, arg_iconbase); if (!use_stdout) hsc_set_filename_document(hp, estr2str(outfilename)); } /* display argument error message */ if (!ok) { /* NOTE: no strclone() is used on outfilename, if an * error already occured within set_args(). therefore, * you must not call ufreestr( outfilename ) */ pargerr(); arg_outfname = NULL; set_return_code(RC_ERROR); } else { EXPSTR *tmp_fname = init_estr(32); /* filename only part */ fileattr_str = init_estr(64); if (outfilename) get_fname(tmp_fname, estr2str(outfilename)); set_dest_attribs(hp, estr2str(rel_destdir), estr2str(tmp_fname)); if (!arg_pipe_in) { if (outfilename) get_fname(tmp_fname, estr2str(outfilename)); else clr_estr(tmp_fname); set_source_attribs(hp, estr2str(rel_destdir), estr2str(tmp_fname)); } else set_source_attribs(hp, NULL, NULL); D( { HSCMSG_ID i; fprintf(stderr, "\n" DHSC "input : `%s'\n", estr2str(inpfilename)); fprintf(stderr, DHSC "output: `%s'\n", get_outfilename()); fprintf(stderr, DHSC "destdr: `%s'\n", estr2str(destdir)); fprintf(stderr, DHSC "reldst: `%s'\n", estr2str(rel_destdir)); if (prjfilename) fprintf(stderr, DHSC "projct: `%s'\n", prjfilename); if (!use_stdout) fprintf(stderr, DHSC "procss: `%s'\n", estr2str(outfilename)); fprintf(stderr, DHSC "ignore:"); for (i = 0; i < MAX_MSGID; i++) if (hsc_get_msg_ignore(hp, i)) fprintf(stderr, " %lu", i); fprintf(stderr, "\n"); } ); del_estr(tmp_fname); }
/* This function performs the following jobs: - show the help if '--help' or 'help' or '-h' are passed - verify that a command is not ambiguous, otherwise show which part of the command is ambiguous - if after a (even partial) command there is '--help' show detailed help for all the matching commands - if the command doesn't match show an error - finally, if a command matches, they return which command matched and the arguments The function return 0 in case of help is requested; <0 in case of uncorrect command; >0 in case of matching commands argc, argv are the arg-counter and arg-vector (input) *nargs_ is the number of the arguments after the command (output) **cmd_ is the invoked command (output) ***args_ are the arguments after the command */ static int parse_args(int argc, char **argv, CommandFunction *func_, int *nargs_, char **cmd_, char ***args_ ) { struct Command *cp; struct Command *matchcmd=0; char *prgname = get_prgname(argv[0]); int i=0, helprequested=0; if( argc < 2 || !strcmp(argv[1], "help") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){ help(prgname); return 0; } for( cp = commands; cp->verb; cp++ ) if( !cp->ncmds) cp->ncmds = split_command(cp->verb, &(cp->cmds)); for( cp = commands; cp->verb; cp++ ){ int match; if( argc-1 < cp->ncmds ) continue; for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){ char *s1, *s2; s1 = cp->cmds[i]; s2 = argv[i+1]; for(s2 = cp->cmds[i], s1 = argv[i+1]; *s1 == *s2 && *s1; s1++, s2++ ) ; if( *s1 ){ match=0; break; } } /* If you understand why this code works ... you are a genious !! */ if(argc>i+1 && !strcmp(argv[i+1],"--help")){ if(!helprequested) printf("Usage:\n"); print_help(prgname, cp, ADVANCED_HELP); helprequested=1; continue; } if(!match) continue; matchcmd = cp; *nargs_ = argc-matchcmd->ncmds-1; *cmd_ = matchcmd->verb; *args_ = argv+matchcmd->ncmds+1; *func_ = cp->func; break; } if(helprequested){ printf("\n%s\n", MMC_VERSION); return 0; } if(!matchcmd){ fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]); help(prgname); return -1; } if(check_ambiguity(matchcmd, argv)) return -2; /* check the number of argument */ if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){ fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n", matchcmd->verb, -matchcmd->nargs); return -2; } if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){ fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", matchcmd->verb, matchcmd->nargs); return -2; } if (prepare_args( nargs_, args_, prgname, matchcmd )){ fprintf(stderr, "ERROR: not enough memory\\n"); return -20; } return 1; }
int main(int argc, char **argv) { char result = 0; int err = 0; struct stat st; #ifdef _WIN32 HANDLE fds[3]; ULONG pid; HANDLE hProcess, hServerProcess; DWORD dwNumberOfBytesRead; CHAR argv0[PATHSZ]; GetModuleFileName(NULL, argv0, PATHSZ); compute_merlinpath(merlin_path, argv0, &st); #else compute_merlinpath(merlin_path, argv[0], &st); #endif if (argc >= 2 && strcmp(argv[1], "server") == 0) { IPC_SOCKET_TYPE sock; ssize_t len; #ifdef _WIN32 compute_socketname(socketname, eventname, merlin_path); #else compute_socketname(socketname, &st); #endif sock = connect_and_serve(socketname, eventname, merlin_path); len = prepare_args(argbuffer, sizeof(argbuffer), argc-2, argv+2); #ifdef _WIN32 hProcess = GetCurrentProcess(); if (!GetNamedPipeServerProcessId(sock, &pid)) failwith_perror("GetNamedPipeServerProcessId"); hServerProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid); if (hServerProcess == INVALID_HANDLE_VALUE) failwith_perror("OpenProcess"); if (!DuplicateHandle(hProcess, GetStdHandle(STD_INPUT_HANDLE), hServerProcess, &fds[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) failwith_perror("DuplicateHandle(stdin)"); if (!DuplicateHandle(hProcess, GetStdHandle(STD_OUTPUT_HANDLE), hServerProcess, &fds[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) failwith_perror("DuplicateHandle(stdout)"); CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE)); if (!DuplicateHandle(hProcess, GetStdHandle(STD_ERROR_HANDLE), hServerProcess, &fds[2], 0, FALSE, DUPLICATE_SAME_ACCESS)) failwith_perror("DuplicateHandle(stderr)"); #else int fds[3] = { STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO }; #endif ipc_send(sock, argbuffer, len, fds); #ifdef _WIN32 if (ReadFile(sock, &result, 1, &dwNumberOfBytesRead, NULL) && dwNumberOfBytesRead == 1) err = 1; #else NO_EINTR(err, read(sock, &result, 1)); #endif if (err == 1) exit(result); unexpected_termination(argc, argv); } else { argv[0] = ocamlmerlin_server; execvp(merlin_path, argv); failwith_perror("execvp(ocamlmerlin-server)"); } }
/* * args_ok * * prepare args, check & parse user args, display error and * help message if neccessary * * result: TRUE, if all args ok and no request for HELP or * LICENSE has been detected */ static BOOL args_ok(int argc, char *argv[]) { struct arglist *hscdepp_args; /* argument structure */ BOOL arg_help = FALSE; BOOL arg_license = FALSE; BOOL ok = FALSE; /* create arg-table */ hscdepp_args = prepare_args ("HSCDEPP_ARGS", "FILE/T", &makefile, "makefile to update", "PRJFILE/T/K", &prjfile, "project file", "NAMEALL/T/K", &nameall, "name for `all_hsc' rule", "VERBOSE/S", &verbose, "verbose output", "NOBACKUP/S", &nobackup, "do not backup makefile", "NOTAGLINES/S", ¬aglines, "do not write taglines", "-DEBUG/S", &debug, "enable debugging output", "HELP=?=-h=--help/S", &arg_help, "display this text", "LICENSE/S", &arg_license, "display license", NULL); ok = (hscdepp_args != NULL); /* set & test args */ if (ok) { ok = set_args(argc, argv, hscdepp_args); /* display argument error message */ if (!ok) { pargerr(); set_return_code(RC_ERROR); } else if (arg_help || arg_license) { /* * display help or license text */ fprintf_prginfo(stderr); if (arg_help) fprintf_arghelp(stderr, hscdepp_args); else show_license(); set_return_code(RC_WARN); ok = FALSE; } else { /* auto-enable verbose in debug-mode */ if (debug) verbose = TRUE; /* display copyright in verbose-mode */ if (verbose) fprintf_prginfo(stderr); /* set default-parameters if neccessary */ if (!prjfile) { prjfile = DEFAULT_PROJECT; if (verbose) fprintf(stderr, HD "%s: using default project-file\n", prjfile); } if (!nameall) { nameall = DEFAULT_NAMEALL; } /* debugging control output */ D( if (makefile) { fprintf(stderr, DHD "makefile=`%s'\n", makefile); fprintf(stderr, DHD "makefile=DEFAULT\n"); fprintf(stderr, DHD "prjfile =`%s'\n", prjfile); fprintf(stderr, DHD "nameall =`%s'\n", nameall); } ); } /* release mem used by args */ free_args(hscdepp_args); }
/* * args_ok * * prepare args, check & parse user args, display error and * help message if neccessary * * result: TRUE, if all args ok and no request for HELP or * LICENSE has been detected */ static BOOL args_ok(int argc, char *argv[]) { struct arglist *hscpitt_args; /* argument structure */ BOOL arg_help = FALSE; BOOL arg_license = FALSE; BOOL ok = FALSE; /* create arg-table */ hscpitt_args = prepare_args ("HSCPITT_ARGS", "COMMAND/E", COMMAND_ENUMSTR, &command, "command to perform (" COMMAND_ENUMSTR ")", "ARG/T/M", &command_arglist, "command argument(s)", "PRJFILE/T/K", &prjfile, "project file", "FORCE/S", &force, "disable certain checks", "QUIET/S", &quiet, "act quietly", "-DEBUG/S", &debug, "enable debugging output", "HELP=?=-h=--help/S", &arg_help, "display this text", "LICENSE/S", &arg_license, "display license", NULL); ok = (hscpitt_args != NULL); /* set & test args */ if (ok) { ok = set_args(argc, argv, hscpitt_args); /* display argument error message */ if (!ok) { pargerr(); set_return_code(RC_ERROR); } else if (arg_help || arg_license) { /* * display help or license text */ fprintf_prginfo(stderr); if (arg_help) fprintf_arghelp(stderr, hscpitt_args); else show_license(); set_return_code(RC_WARN); ok = FALSE; } else { /* display copyright in verbose-mode */ if (!quiet) fprintf_prginfo(stderr); /* set default-parameters if neccessary */ if (!prjfile) { prjfile = DEFAULT_PROJECT; if (!quiet) { fprintf(stderr, HP "%s: using default project-file\n", prjfile); } } if (command) { /* debugging control output */ D( { fprintf(stderr, DHP "prjfile =`%s'\n", prjfile); fprintf(stderr, DHP "command =`%ld'\n", command); } ); } else { fprintf(stderr, "no command specified\n"); ok = FALSE; } }