📄 function.c
字号:
if (is_addsuffix) o = variable_buffer_output (o, argv[0], fixlen); o = variable_buffer_output (o, " ", 1); doneany = 1; } if (doneany) /* Kill last space. */ --o; return o;}static char *func_subst(o, argv, funcname) char *o; char **argv; const char *funcname;{ o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]), strlen (argv[1]), 0, 0); return o;}static char *func_firstword(o, argv, funcname) char *o; char **argv; const char *funcname;{ int i=0; char *words = argv[0]; char *p = find_next_token (&words, &i); if (p != 0) o = variable_buffer_output (o, p, i); return o;}static char *func_words(o, argv, funcname) char *o; char **argv; const char *funcname;{ int i = 0; char *word_iterator = argv[0]; char buf[20]; while (find_next_token (&word_iterator, (unsigned int *) 0) != 0) ++i; sprintf (buf, "%d", i); o = variable_buffer_output (o, buf, strlen (buf)); return o;}char *strip_whitespace (begpp, endpp) char **begpp; char **endpp;{ while (isspace (**begpp) && *begpp <= *endpp) (*begpp) ++; while (isspace (**endpp) && *endpp >= *begpp) (*endpp) --; return *begpp;}intis_numeric (p) char *p;{ char *end = p + strlen (p) - 1; char *beg = p; strip_whitespace (&p, &end); while (p <= end) if (!ISDIGIT (*(p++))) /* ISDIGIT only evals its arg once: see make.h. */ return 0; return (end - beg >= 0);}voidcheck_numeric (s, message) char *s; char *message;{ if (!is_numeric (s)) fatal (reading_file, message);}static char *func_word(o, argv, funcname) char *o; char **argv; const char *funcname;{ char *end_p=0; int i=0; char *p=0; /* Check the first argument. */ check_numeric (argv[0], _("non-numeric first argument to `word' function")); i = atoi (argv[0]); if (i == 0) fatal (reading_file, _("the `word' function takes a positive index argument")); end_p = argv[1]; while ((p = find_next_token (&end_p, 0)) != 0) if (--i == 0) break; if (i == 0) o = variable_buffer_output (o, p, end_p - p); return o;}static char *func_wordlist (o, argv, funcname) char *o; char **argv; const char *funcname;{ int i=0; int j=0; /* Check the first argument. */ check_numeric (argv[0], _("non-numeric first argument to `wordlist' function")); i =atoi(argv[0]); check_numeric (argv[1], _("non-numeric second argument to `wordlist' function")); j = atoi(argv[1]); { char *p; char *end_p = argv[2]; int start = (i < j) ? i : j; int count = j -i ; if (count < 0) count = - count; count ++; while (((p = find_next_token (&end_p, 0)) != 0) && --start) {} if (p) { while (--count && (find_next_token (&end_p, 0) != 0)) {} o = variable_buffer_output (o, p, end_p - p); } } return o;}static char*func_findstring(o, argv, funcname) char *o; char **argv; const char *funcname;{ /* Find the first occurrence of the first string in the second. */ int i = strlen (argv[0]); if (sindex (argv[1], 0, argv[0], i) != 0) o = variable_buffer_output (o, argv[0], i); return o;}static char *func_foreach (o, argv, funcname) char *o; char **argv; const char *funcname;{ /* expand only the first two. */ char *varname = expand_argument (argv[0], argv[1] - 1); char *list = expand_argument (argv[1], argv[2] -1); char *body = savestring (argv[2], argv[3] - argv[2] - 1); int len =0; char *list_iterator = list; char *p; register struct variable *var=0; int doneany =0; push_new_variable_scope (); var = define_variable (varname, strlen (varname), "", o_automatic, 0); /* loop through LIST, put the value in VAR and expand BODY */ while ((p = find_next_token (&list_iterator, &len)) != 0) { char *result = 0; { char save = p[len]; p[len] = '\0'; free (var->value); var->value = (char *) xstrdup ((char*) p); p[len] = save; } result = allocated_variable_expand (body); o = variable_buffer_output (o, result, strlen (result)); o = variable_buffer_output (o, " ", 1); doneany = 1; free (result); } if (doneany) /* Kill the last space. */ --o; pop_variable_scope (); free (varname); free (list); free (body); return o;}struct a_word{ struct a_word *next; char *str; int matched;};static char *func_filter_filterout (o, argv, funcname) char *o; char **argv; const char *funcname;{ struct a_word *wordhead =0; struct a_word *wordtail =0; int is_filter = streq (funcname, "filter"); char *patterns = argv[0]; char *p; int len; char *word_iterator = argv[1]; /* Chop ARGV[1] up into words and then run each pattern through. */ while ((p = find_next_token (&word_iterator, &len)) != 0) { struct a_word *w = (struct a_word *)alloca(sizeof(struct a_word)); if (wordhead == 0) wordhead = w; else wordtail->next = w; wordtail = w; if (*word_iterator != '\0') ++word_iterator; p[len] = '\0'; w->str = p; w->matched = 0; } if (wordhead != 0) { struct a_word *wp =0; char *pat_iterator = patterns; int doneany = 0; wordtail->next = 0; /* Run each pattern through the words, killing words. */ while ((p = find_next_token (&pat_iterator, &len)) != 0) { char *percent; char save = p[len]; p[len] = '\0'; percent = find_percent (p); for (wp = wordhead; wp != 0; wp = wp->next) wp->matched |= (percent == 0 ? streq (p, wp->str) : pattern_matches (p, percent, wp->str)); p[len] = save; } /* Output the words that matched (or didn't, for filter-out). */ for (wp = wordhead; wp != 0; wp = wp->next) if (is_filter ? wp->matched : !wp->matched) { o = variable_buffer_output (o, wp->str, strlen (wp->str)); o = variable_buffer_output (o, " ", 1); doneany = 1; } if (doneany) /* Kill the last space. */ --o; } return o;}static char *func_strip(o, argv, funcname) char *o; char **argv; const char *funcname;{ char *p = argv[0]; int doneany =0; while (*p != '\0') { int i=0; char *word_start=0; while (isspace(*p)) ++p; word_start = p; for (i=0; *p != '\0' && !isspace(*p); ++p, ++i) {} if (!i) break; o = variable_buffer_output (o, word_start, i); o = variable_buffer_output (o, " ", 1); doneany = 1; } if (doneany) /* Kill the last space. */ --o; return o;}/* Print a warning or fatal message.*/static char *func_error (o, argv, funcname) char *o; char **argv; const char *funcname;{ char **argvp; char *msg, *p; int len; /* The arguments will be broken on commas. Rather than create yet another special case where function arguments aren't broken up, just create a format string that puts them back together. */ for (len=0, argvp=argv; *argvp != 0; ++argvp) len += strlen(*argvp) + 2; p = msg = alloca (len + 1); for (argvp=argv; argvp[1] != 0; ++argvp) { strcpy(p, *argvp); p += strlen(*argvp); *(p++) = ','; *(p++) = ' '; } strcpy(p, *argvp); if (*funcname == 'e') fatal (reading_file, "%s", msg); /* The warning function expands to the empty string. */ error (reading_file, "%s", msg); return o;}/* chop argv[0] into words, and sort them. */static char *func_sort (o, argv, funcname) char *o; char **argv; const char *funcname;{ char **words = 0; int nwords = 0; register int wordi = 0; /* Chop ARGV[0] into words and put them in WORDS. */ char *t = argv[0]; char *p=0; int len; int i; while ((p = find_next_token (&t, &len)) != 0) { if (wordi >= nwords - 1) { nwords = 2* nwords + 5; words = (char **) xrealloc ((char *) words, nwords * sizeof (char *)); } words[wordi++] = savestring (p, len); } if (!wordi) return o; /* Now sort the list of words. */ qsort ((char *) words, wordi, sizeof (char *), alpha_compare); /* Now write the sorted list. */ for (i = 0; i < wordi; ++i) { len = strlen (words[i]); if (i == wordi - 1 || strlen (words[i + 1]) != len || strcmp (words[i], words[i + 1])) { o = variable_buffer_output (o, words[i], len); o = variable_buffer_output (o, " ", 1); } free (words[i]); } /* Kill the last space. */ --o; free (words); return o;}/* $(if condition,true-part[,false-part]) CONDITION is false iff it evaluates to an empty string. White space before and after condition are stripped before evaluation. If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is evaluated (if it exists). Because only one of the two PARTs is evaluated, you can use $(if ...) to create side-effects (with $(shell ...), for example).*/static char *func_if (o, argv, funcname) char *o; char **argv; const char *funcname;{ char *begp = argv[0]; char *endp = argv[1]-1; int result = 0; /* Find the result of the condition: if we have a value, and it's not empty, the condition is true. If we don't have a value, or it's the empty string, then it's false. */ strip_whitespace (&begp, &endp); if (begp < endp) { char *expansion = expand_argument (begp, endp); result = strlen (expansion); free (expansion); } /* If the result is true (1) we want to eval the first argument, and if it's false (0) we want to eval the second. If the argument doesn't exist we do nothing, otherwise expand it and add to the buffer. */ argv += 1 + !result; if (argv[0] != NULL && argv[1] != NULL) { char *expansion; char **endp = argv+1; /* If we're doing the else-clause, make sure we concatenate any potential extra arguments into the last argument. */ if (!result) while (*endp && **endp != '\0') ++endp; expansion = expand_argument (*argv, *endp-1); o = variable_buffer_output (o, expansion, strlen (expansion)); free (expansion); } return o;}static char *func_wildcard(o, argv, funcname) char *o; char **argv; const char *funcname;{#ifdef _AMIGA o = wildcard_expansion (argv[0], o);#else char *p = string_glob (argv[0]); o = variable_buffer_output (o, p, strlen (p));#endif return o;}/* \r is replaced on UNIX as well. Is this desirable? */voidfold_newlines (buffer, length) char *buffer; int *length;{ char *dst = buffer; char *src = buffer; char *last_nonnl = buffer -1; src[*length] = 0; for (; *src != '\0'; ++src) { if (src[0] == '\r' && src[1] == '\n') continue; if (*src == '\n') { *dst++ = ' '; } else { last_nonnl = dst; *dst++ = *src; } } *(++last_nonnl) = '\0'; *length = last_nonnl - buffer;}int shell_function_pid = 0, shell_function_completed;#ifdef WINDOWS32/*untested*/#include <windows.h>#include <io.h>#include "sub_proc.h"voidwindows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp){ SECURITY_ATTRIBUTES saAttr; HANDLE hIn; HANDLE hErr; HANDLE hChildOutRd; HANDLE hChildOutWr; HANDLE hProcess; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; if (DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE), GetCurrentProcess(), &hIn, 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) { fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"), GetLastError()); } if (DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &hErr, 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) { fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"), GetLastError()); } if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0)) fatal (NILF, _("CreatePipe() failed (e=%d)\n"), GetLastError()); hProcess = process_init_fd(hIn, hChildOutWr, hErr); if (!hProcess) fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n")); else process_register(hProcess);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -