VirtualBox

Changeset 730 in kBuild for trunk/src/gmake/function.c


Ignore:
Timestamp:
Dec 17, 2006 3:59:33 AM (18 years ago)
Author:
bird
Message:

Added a variation on the $(comp-vars) function called $(comp-cmds) which compares command by command instead of the string as a whole ignoring spaces and job control chars.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/function.c

    r729 r730  
    19051905 */
    19061906static char *
    1907 func_eq (char *o, char **argv, char *funcname)
     1907func_eq (char *o, char **argv, const char *funcname)
    19081908{
    19091909  int result = ! strcmp (argv[0], argv[1]);
     
    19171917 */
    19181918static char *
    1919 func_not (char *o, char **argv, char *funcname)
     1919func_not (char *o, char **argv, const char *funcname)
    19201920{
    19211921  char *s = argv[0];
     
    22082208
    22092209#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
    2210 /* Returns empty string if equal, returns the third arg if not equal. */
     2210
     2211/* Strip leading spaces and other things off a command. */
     2212static const char *
     2213comp_cmds_strip_leading (const char *s, const char *e)
     2214{
     2215    while (s < e)
     2216      {
     2217        const char ch = *s;
     2218        if (!isblank (ch)
     2219         && ch != '@'
     2220         && ch != '+'
     2221         && ch != '-')
     2222          break;
     2223        s++;
     2224      }
     2225    return s;
     2226}
     2227
     2228/* Worker for func_comp_vars() which is called if the comparision failed.
     2229   It will do the slow command by command comparision of the commands
     2230   when there invoked as comp-cmds. */
     2231static char *
     2232comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
     2233              char *ne_retval, const char *funcname)
     2234{
     2235    /* give up at once if not comp-cmds. */
     2236    if (strcmp (funcname, "comp-cmds") != 0)
     2237      o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
     2238    else
     2239      {
     2240        const char * const s1_start = s1;
     2241        int new_cmd = 1;
     2242        int diff;
     2243        for (;;)
     2244          {
     2245            /* if it's a new command, strip leading stuff. */
     2246            if (new_cmd)
     2247              {
     2248                s1 = comp_cmds_strip_leading (s1, e1);
     2249                s2 = comp_cmds_strip_leading (s2, e2);
     2250                new_cmd = 0;
     2251              }
     2252            if (s1 >= e1 || s2 >= e2)
     2253              break;
     2254
     2255            /*
     2256             * Inner compare loop which compares one line.
     2257             * FIXME: parse quoting!
     2258             */
     2259            for (;;)
     2260              {
     2261                const char ch1 = *s1;
     2262                const char ch2 = *s2;
     2263                diff = ch1 - ch2;
     2264                if (diff)
     2265                  break;
     2266                if (ch1 == '\n')
     2267                  break;
     2268                assert (ch1 != '\r');
     2269
     2270                /* next */
     2271                s1++;
     2272                s2++;
     2273                if (s1 >= e1 || s2 >= e2)
     2274                  break;
     2275              }
     2276
     2277            /*
     2278             * If we exited because of a difference try to end-of-command
     2279             * comparision, e.g. ignore trailing spaces.
     2280             */
     2281            if (diff)
     2282              {
     2283                /* strip */
     2284                while (s1 < e1 && isblank (*s1))
     2285                  s1++;
     2286                while (s2 < e2 && isblank (*s2))
     2287                  s2++;
     2288                if (s1 >= e1 || s2 >= e2)
     2289                  break;
     2290
     2291                /* compare again and check that it's a newline. */
     2292                if (*s2 != '\n' || *s1 != '\n')
     2293                  break;
     2294              }
     2295            /* Break out if we exited because of EOS. */
     2296            else if (s1 >= e1 || s2 >= e2)
     2297                break;
     2298
     2299            /*
     2300             * Detect the end of command lines.
     2301             */
     2302            if (*s1 == '\n')
     2303              new_cmd = s1 == s1_start || s1[-1] != '\\';
     2304            s1++;
     2305            s2++;
     2306          }
     2307
     2308        /*
     2309         * Ignore trailing empty lines.
     2310         */
     2311        if (s1 < e1 || s2 < e2)
     2312          {
     2313            while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
     2314              if (*s1++ == '\n')
     2315                s1 = comp_cmds_strip_leading (s1, e1);
     2316            while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
     2317              if (*s2++ == '\n')
     2318                s2 = comp_cmds_strip_leading (s2, e2);
     2319          }
     2320
     2321        /* emit the result. */
     2322        if (s1 == e1 && s2 == e2)
     2323          o = variable_buffer_output (o, "", 1);
     2324        else
     2325          o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
     2326      }
     2327    return o;
     2328}
     2329
     2330/*
     2331    $(comp-vars var1,var2,not-equal-return)
     2332  or
     2333    $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
     2334
     2335  Compares the two variables (that's given by name to avoid unnecessary
     2336  expanding) and return the string in the third argument if not equal.
     2337  If equal, nothing is returned.
     2338
     2339  comp-vars will to an exact comparision only stripping leading and
     2340  trailing spaces.
     2341
     2342  comp-cmds will compare command by command, ignoring not only leading
     2343  and trailing spaces on each line but also leading one leading '@' and '-'.
     2344*/
    22112345static char *
    22122346func_comp_vars (char *o, char **argv, const char *funcname)
     
    22472381
    22482382    if (e1 - s1 != e2 - s2)
    2249       return variable_buffer_output (o, argv[2], strlen(argv[2]));
     2383      return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    22502384l_simple_compare:
    22512385    if (!memcmp (s1, s2, e1 - s1))
    22522386      return variable_buffer_output (o, "", 1);     /* eq */
    2253     return variable_buffer_output (o, argv[2], strlen(argv[2]));
     2387    return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    22542388  }
    22552389
     
    22822416  x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
    22832417  if (!x1 && !x2)
    2284     return variable_buffer_output (o, argv[2], strlen (argv[2]));
     2418    return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    22852419
    22862420  l1 = x1 ? x1 - s1 : e1 - s1;
     
    22882422  l = l1 <= l2 ? l1 : l2;
    22892423  if (l && memcmp (s1, s2, l))
    2290       return variable_buffer_output (o, argv[2], strlen (argv[2]));
     2424    return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    22912425
    22922426  /* one or both buffers now require expanding. */
     
    22992433        while (isblank ((unsigned char) *s1))
    23002434          s1++;
    2301       e1 = strchr (s1, '\0'); /*hmm*/
     2435      e1 = strchr (s1, '\0');
    23022436      while (e1 > s1 && isblank ((unsigned char) e1[-1]))
    23032437        e1--;
     
    23122446        while (isblank ((unsigned char) *s2))
    23132447          s2++;
    2314       e2 = strchr (s2, '\0'); /*hmm*/
     2448      e2 = strchr (s2, '\0');
    23152449      while (e2 > s2 && isblank ((unsigned char) e2[-1]))
    23162450        e2--;
     
    23182452
    23192453  /* the final compare */
    2320   l = (   e1 - s1 != e2 - s2
    2321        || memcmp (s1, s2, e1 - s1));
     2454  if (   e1 - s1 != e2 - s2
     2455      || memcmp (s1, s2, e1 - s1))
     2456      o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
     2457  else
     2458      o = variable_buffer_output (o, "", 1);        /* eq */
    23222459  if (a1)
    23232460    free (a1);
    23242461  if (a2)
    23252462    free (a2);
    2326   if (l)
    2327     return variable_buffer_output (o, argv[2], strlen (argv[2]));
    2328   return variable_buffer_output (o, "", 1);         /* eq */
     2463  return o;
    23292464}
    23302465#endif
     
    24462581#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
    24472582  { STRING_SIZE_TUPLE("comp-vars"),     3,  3,  1,  func_comp_vars},
     2583  { STRING_SIZE_TUPLE("comp-cmds"),     3,  3,  1,  func_comp_vars},
    24482584#endif
    24492585#ifdef CONFIG_WITH_STACK
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette