Changeset 287 for trunk/src/gmake/read.c


Ignore:
Timestamp:
May 17, 2005, 1:34:55 AM (20 years ago)
Author:
bird
Message:

join + optimizations.

File:
1 edited

Legend:

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

    r191 r287  
    7979    unsigned int if_cmds;       /* Depth of conditional nesting.  */
    8080    unsigned int allocated;     /* Elts allocated in following arrays.  */
    81     char *ignoring;             /* Are we ignoring or interepreting?  */
     81    char *ignoring;             /* Are we ignoring or interpreting?
     82                                   0=interpreting, 1=not yet interpreted,
     83                                   2=already interpreted */
    8284    char *seen_else;            /* Have we already seen an `else'?  */
    8385  };
     
    131133                               enum variable_origin origin,
    132134                               struct ebuffer *ebuf));
    133 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
     135static int conditional_line PARAMS ((char *line, int len, const struct floc *flocp));
    134136static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
    135137                        struct dep *deps, unsigned int cmds_started, char *commands,
    136138                        unsigned int commands_idx, int two_colon,
    137                         int have_sysv_atvar,
    138                         const struct floc *flocp, int set_default));
     139                        const struct floc *flocp));
    139140static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
    140141                                       enum variable_origin origin,
     
    257258              d->file->dontcare = 1;
    258259              d->ignore_mtime = 0;
     260              d->need_2nd_expansion = 0;
    259261              /* Tell update_goal_chain to bail out as soon as this file is
    260262                 made, and main not to die if we can't make this file.  */
     
    372374  deps->file = lookup_file (filename);
    373375  if (deps->file == 0)
    374     {
    375       deps->file = enter_file (xstrdup (filename));
    376       if (flags & RM_DONTCARE)
    377         deps->file->dontcare = 1;
    378     }
     376    deps->file = enter_file (xstrdup (filename));
    379377  if (filename != ebuf.floc.filenm)
    380378    free (filename);
     
    382380  deps->changed = flags;
    383381  deps->ignore_mtime = 0;
     382  deps->need_2nd_expansion = 0;
     383  if (flags & RM_DONTCARE)
     384    deps->file->dontcare = 1;
    384385
    385386  /* If the makefile can't be found at all, give up entirely.  */
     
    413414
    414415  free (ebuf.bufstart);
     416  alloca (0);
    415417  return r;
    416418}
     
    444446  reading_file = curfile;
    445447
     448  alloca (0);
    446449  return r;
    447450}
     
    465468  int ignoring = 0, in_ignored_define = 0;
    466469  int no_targets = 0;           /* Set when reading a rule without targets.  */
    467   int have_sysv_atvar = 0;
    468470  struct nameseq *filenames = 0;
    469471  struct dep *deps = 0;
     
    482484          record_files (filenames, pattern, pattern_percent, deps,            \
    483485                        cmds_started, commands, commands_idx, two_colon,      \
    484                         have_sysv_atvar, &fi, set_default);                   \
     486                        &fi);                                                 \
    485487        }                                                                     \
    486488      filenames = 0;                                                          \
     
    619621         following lines.  */
    620622
    621       if (!in_ignored_define
    622           && (word1eq ("ifdef") || word1eq ("ifndef")
    623               || word1eq ("ifeq") || word1eq ("ifneq")
    624               || word1eq ("else") || word1eq ("endif")))
    625         {
    626           int i = conditional_line (p, fstart);
    627           if (i < 0)
    628             fatal (fstart, _("invalid syntax in conditional"));
    629 
    630           ignoring = i;
    631           continue;
     623      if (!in_ignored_define)
     624        {
     625          int i = conditional_line (p, len, fstart);
     626          if (i != -2)
     627            {
     628              if (i == -1)
     629                fatal (fstart, _("invalid syntax in conditional"));
     630
     631              ignoring = i;
     632              continue;
     633            }
    632634        }
    633635
     
    854856        goto rule_complete;
    855857
     858      /* This line starts with a tab but was not caught above because there
     859         was no preceding target, and the line might have been usable as a
     860         variable definition.  But now we know it is definitely lossage.  */
    856861      if (line[0] == '\t')
    857         {
    858           p = collapsed;        /* Ignore comments, etc.  */
    859           while (isblank ((unsigned char)*p))
    860             ++p;
    861           if (*p == '\0')
    862             /* The line is completely blank; that is harmless.  */
    863             continue;
    864 
    865           /* This line starts with a tab but was not caught above
    866              because there was no preceding target, and the line
    867              might have been usable as a variable definition.
    868              But now we know it is definitely lossage.  */
    869           fatal(fstart, _("commands commence before first target"));
    870         }
     862        fatal(fstart, _("commands commence before first target"));
    871863
    872864      /* This line describes some target files.  This is complicated by
     
    887879        unsigned int len, plen = 0;
    888880        char *colonp;
     881        const char *end, *beg; /* Helpers for whitespace stripping. */
    889882
    890883        /* Record the previous rule.  */
     
    935928
    936929        p2 = variable_expand_string(NULL, lb_next, len);
     930
    937931        while (1)
    938932          {
     
    10411035        if (*lb_next != '\0')
    10421036          {
    1043             unsigned int l = p - variable_buffer;
    1044             unsigned int l2 = p2 - variable_buffer;
     1037            unsigned int l = p2 - variable_buffer;
    10451038            plen = strlen (p2);
    10461039            (void) variable_buffer_output (p2+plen,
    10471040                                           lb_next, strlen (lb_next)+1);
    1048             p = variable_buffer + l;
    1049             p2 = variable_buffer + l2;
     1041            p2 = variable_buffer + l;
    10501042          }
    10511043
     
    11131105              }
    11141106          }
    1115 
    1116         /* Do any of the prerequisites appear to have $@ etc.?  */
    1117         have_sysv_atvar = 0;
    1118         if (!posix_pedantic)
    1119           for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
    1120             if (p[1] == '@' || ((p[1] == '(' || p[1] == '{') && p[2] == '@'))
    1121               {
    1122                 have_sysv_atvar = 1;
    1123                 break;
    1124               }
    11251107
    11261108        /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
     
    11881170          pattern = 0;
    11891171
    1190         /* Parse the dependencies.  */
    1191         deps = (struct dep *)
    1192           multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
    1193                       sizeof (struct dep));
    1194         if (*p2)
     1172        /* Strip leading and trailing whitespaces. */
     1173        beg = p2;
     1174        end = beg + strlen (beg) - 1;
     1175        strip_whitespace (&beg, &end);
     1176
     1177        if (beg <= end && *beg != '\0')
    11951178          {
    1196             /* Files that follow '|' are special prerequisites that
    1197                need only exist in order to satisfy the dependency.
    1198                Their modification times are irrelevant.  */
    1199             struct dep **deps_ptr = &deps;
    1200             struct dep *d;
    1201             for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
    1202               ;
    1203             ++p2;
    1204             *deps_ptr = (struct dep *)
    1205               multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
    1206                           sizeof (struct dep));
    1207             for (d = *deps_ptr; d != 0; d = d->next)
    1208               d->ignore_mtime = 1;
     1179            char *top;
     1180            const char *fromp = beg;
     1181
     1182            /* Make a copy of the dependency string.  Note if we find '$'.  */
     1183            deps = (struct dep*) xmalloc (sizeof (struct dep));
     1184            deps->next = 0;
     1185            deps->name = top = (char *) xmalloc (end - beg + 2);
     1186            deps->need_2nd_expansion = 0;
     1187            while (fromp <= end)
     1188              {
     1189                if (*fromp == '$')
     1190                  deps->need_2nd_expansion = 1;
     1191                *(top++) = *(fromp++);
     1192              }
     1193            *top = '\0';
     1194            deps->file = 0;
    12091195          }
     1196        else
     1197          deps = 0;
    12101198
    12111199        commands_idx = 0;
     
    12261214            commands_idx += len;
    12271215            commands[commands_idx++] = '\n';
     1216          }
     1217
     1218        /* Determine if this target should be made default. We used to do
     1219           this in record_files() but because of the delayed target recording
     1220           and because preprocessor directives are legal in target's commands
     1221           it is too late. Consider this fragment for example:
     1222
     1223           foo:
     1224
     1225           ifeq ($(.DEFAULT_GOAL),foo)
     1226              ...
     1227           endif
     1228
     1229           Because the target is not recorded until after ifeq directive is
     1230           evaluated the .DEFAULT_GOAL does not contain foo yet as one
     1231           would expect. Because of this we have to move some of the logic
     1232           here.  */
     1233
     1234        if (**default_goal_name == '\0' && set_default)
     1235          {
     1236            char* name;
     1237            struct dep *d;
     1238            struct nameseq *t = filenames;
     1239
     1240            for (; t != 0; t = t->next)
     1241              {
     1242                int reject = 0;
     1243                name = t->name;
     1244
     1245                /* We have nothing to do if this is an implicit rule. */
     1246                if (strchr (name, '%') != 0)
     1247                  break;
     1248
     1249                /* See if this target's name does not start with a `.',
     1250                   unless it contains a slash.  */
     1251                if (*name == '.' && strchr (name, '/') == 0
     1252#ifdef HAVE_DOS_PATHS
     1253                    && strchr (name, '\\') == 0
     1254#endif
     1255                    )
     1256                  continue;
     1257
     1258
     1259                /* If this file is a suffix, don't let it be
     1260                   the default goal file.  */
     1261                for (d = suffix_file->deps; d != 0; d = d->next)
     1262                  {
     1263                    register struct dep *d2;
     1264                    if (*dep_name (d) != '.' && streq (name, dep_name (d)))
     1265                      {
     1266                        reject = 1;
     1267                        break;
     1268                      }
     1269                    for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
     1270                      {
     1271                        register unsigned int len = strlen (dep_name (d2));
     1272                        if (!strneq (name, dep_name (d2), len))
     1273                          continue;
     1274                        if (streq (name + len, dep_name (d)))
     1275                          {
     1276                            reject = 1;
     1277                            break;
     1278                          }
     1279                      }
     1280
     1281                    if (reject)
     1282                      break;
     1283                  }
     1284
     1285                if (!reject)
     1286                  {
     1287                    define_variable_global (".DEFAULT_GOAL", 13, t->name,
     1288                                            o_file, 0, NILF);
     1289                    break;
     1290                  }
     1291              }
    12281292          }
    12291293
     
    12841348      char *line;
    12851349
     1350      nlines = readline (ebuf);
    12861351      ebuf->floc.lineno += nlines;
    1287       nlines = readline (ebuf);
    12881352
    12891353      /* If there is nothing left to eval, we're done. */
     
    13661430   current makefile.  They are used for error messages.
    13671431
    1368    Value is -1 if the line is invalid,
     1432   Value is -2 if the line is not a conditional at all,
     1433   -1 if the line is an invalid conditional,
    13691434   0 if following text should be interpreted,
    13701435   1 if following text should be ignored.  */
    13711436
    13721437static int
    1373 conditional_line (char *line, const struct floc *flocp)
     1438conditional_line (char *line, int len, const struct floc *flocp)
    13741439{
    1375   int notdef;
    13761440  char *cmdname;
    1377   register unsigned int i;
    1378 
    1379   if (*line == 'i')
    1380     {
    1381       /* It's an "if..." command.  */
    1382       notdef = line[2] == 'n';
    1383       if (notdef)
    1384         {
    1385           cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
    1386           line += cmdname[3] == 'd' ? 7 : 6;
    1387         }
    1388       else
    1389         {
    1390           cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
    1391           line += cmdname[2] == 'd' ? 6 : 5;
    1392         }
    1393     }
     1441  enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
     1442  unsigned int i;
     1443  unsigned int o;
     1444
     1445  /* Compare a word, both length and contents. */
     1446#define word1eq(s)      (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
     1447#define chkword(s, t)   if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
     1448
     1449  /* Make sure this line is a conditional.  */
     1450  chkword ("ifdef", c_ifdef)
     1451  else chkword ("ifndef", c_ifndef)
     1452  else chkword ("ifeq", c_ifeq)
     1453  else chkword ("ifneq", c_ifneq)
     1454  else chkword ("else", c_else)
     1455  else chkword ("endif", c_endif)
    13941456  else
    1395     {
    1396       /* It's an "else" or "endif" command.  */
    1397       notdef = line[1] == 'n';
    1398       cmdname = notdef ? "endif" : "else";
    1399       line += notdef ? 5 : 4;
    1400     }
    1401 
    1402   line = next_token (line);
    1403 
    1404   if (*cmdname == 'e')
     1457    return -2;
     1458
     1459  /* Found one: skip past it and any whitespace after it.  */
     1460  line = next_token (line + len);
     1461
     1462#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
     1463
     1464  /* An 'endif' cannot contain extra text, and reduces the if-depth by 1  */
     1465  if (cmdtype == c_endif)
    14051466    {
    14061467      if (*line != '\0')
    1407         error (flocp, _("Extraneous text after `%s' directive"), cmdname);
    1408       /* "Else" or "endif".  */
    1409       if (conditionals->if_cmds == 0)
     1468        EXTRANEOUS ();
     1469
     1470      if (!conditionals->if_cmds)
    14101471        fatal (flocp, _("extraneous `%s'"), cmdname);
    1411       /* NOTDEF indicates an `endif' command.  */
    1412       if (notdef)
    1413         --conditionals->if_cmds;
    1414       else if (conditionals->seen_else[conditionals->if_cmds - 1])
    1415         fatal (flocp, _("only one `else' per conditional"));
     1472
     1473      --conditionals->if_cmds;
     1474
     1475      goto DONE;
     1476    }
     1477
     1478  /* An 'else' statement can either be simple, or it can have another
     1479     conditional after it.  */
     1480  if (cmdtype == c_else)
     1481    {
     1482      const char *p;
     1483
     1484      if (!conditionals->if_cmds)
     1485        fatal (flocp, _("extraneous `%s'"), cmdname);
     1486
     1487      o = conditionals->if_cmds - 1;
     1488
     1489      if (conditionals->seen_else[o])
     1490        fatal (flocp, _("only one `else' per conditional"));
     1491
     1492      /* Change the state of ignorance.  */
     1493      switch (conditionals->ignoring[o])
     1494        {
     1495          case 0:
     1496            /* We've just been interpreting.  Never do it again.  */
     1497            conditionals->ignoring[o] = 2;
     1498            break;
     1499          case 1:
     1500            /* We've never interpreted yet.  Maybe this time!  */
     1501            conditionals->ignoring[o] = 0;
     1502            break;
     1503        }
     1504
     1505      /* It's a simple 'else'.  */
     1506      if (*line == '\0')
     1507        {
     1508          conditionals->seen_else[o] = 1;
     1509          goto DONE;
     1510        }
     1511
     1512      /* The 'else' has extra text.  That text must be another conditional
     1513         and cannot be an 'else' or 'endif'.  */
     1514
     1515      /* Find the length of the next word.  */
     1516      for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
     1517        ;
     1518      len = p - line;
     1519
     1520      /* If it's 'else' or 'endif' or an illegal conditional, fail.  */
     1521      if (word1eq("else") || word1eq("endif")
     1522          || conditional_line (line, len, flocp) < 0)
     1523        EXTRANEOUS ();
    14161524      else
    1417         {
    1418           /* Toggle the state of ignorance.  */
    1419           conditionals->ignoring[conditionals->if_cmds - 1]
    1420             = !conditionals->ignoring[conditionals->if_cmds - 1];
    1421           /* Record that we have seen an `else' in this conditional.
    1422              A second `else' will be erroneous.  */
    1423           conditionals->seen_else[conditionals->if_cmds - 1] = 1;
    1424         }
    1425       for (i = 0; i < conditionals->if_cmds; ++i)
    1426         if (conditionals->ignoring[i])
    1427           return 1;
    1428       return 0;
     1525        {
     1526          /* conditional_line() created a new level of conditional.
     1527             Raise it back to this level.  */
     1528          if (conditionals->ignoring[o] < 2)
     1529            conditionals->ignoring[o] = conditionals->ignoring[o+1];
     1530          --conditionals->if_cmds;
     1531        }
     1532
     1533      goto DONE;
    14291534    }
    14301535
     
    14361541    }
    14371542
    1438   ++conditionals->if_cmds;
     1543  o = conditionals->if_cmds++;
    14391544  if (conditionals->if_cmds > conditionals->allocated)
    14401545    {
     
    14471552
    14481553  /* Record that we have seen an `if...' but no `else' so far.  */
    1449   conditionals->seen_else[conditionals->if_cmds - 1] = 0;
     1554  conditionals->seen_else[o] = 0;
    14501555
    14511556  /* Search through the stack to see if we're already ignoring.  */
    1452   for (i = 0; i < conditionals->if_cmds - 1; ++i)
     1557  for (i = 0; i < o; ++i)
    14531558    if (conditionals->ignoring[i])
    14541559      {
    1455         /* We are already ignoring, so just push a level
    1456            to match the next "else" or "endif", and keep ignoring.
    1457            We don't want to expand variables in the condition.  */
    1458         conditionals->ignoring[conditionals->if_cmds - 1] = 1;
     1560        /* We are already ignoring, so just push a level to match the next
     1561           "else" or "endif", and keep ignoring.  We don't want to expand
     1562           variables in the condition.  */
     1563        conditionals->ignoring[o] = 1;
    14591564        return 1;
    14601565      }
    14611566
    1462   if (cmdname[notdef ? 3 : 2] == 'd')
     1567  if (cmdtype == c_ifdef || cmdtype == c_ifndef)
    14631568    {
    1464       /* "Ifdef" or "ifndef".  */
    14651569      char *var;
    14661570      struct variable *v;
    1467       register char *p;
     1571      char *p;
    14681572
    14691573      /* Expand the thing we're looking up, so we can use indirect and
     
    14791583
    14801584      var[i] = '\0';
    1481       v = lookup_variable (var, strlen (var));
    1482       conditionals->ignoring[conditionals->if_cmds - 1]
    1483         = (v != 0 && *v->value != '\0') == notdef;
     1585      v = lookup_variable (var, i);
     1586
     1587      conditionals->ignoring[o] =
     1588        ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
    14841589
    14851590      free (var);
     
    14991604      if (termin == ',')
    15001605        {
    1501           register int count = 0;
     1606          int count = 0;
    15021607          for (; *line != '\0'; ++line)
    15031608            if (*line == '(')
     
    15731678      line = next_token (++line);
    15741679      if (*line != '\0')
    1575         error (flocp, _("Extraneous text after `%s' directive"), cmdname);
     1680        EXTRANEOUS ();
    15761681
    15771682      s2 = variable_expand (s2);
    1578       conditionals->ignoring[conditionals->if_cmds - 1]
    1579         = streq (s1, s2) == notdef;
     1683      conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
    15801684    }
    15811685
     1686 DONE:
    15821687  /* Search through the stack to see if we're ignoring.  */
    15831688  for (i = 0; i < conditionals->if_cmds; ++i)
     
    16951800          p = create_pattern_var (name, percent);
    16961801          p->variable.fileinfo = *flocp;
     1802          /* I don't think this can fail since we already determined it was a
     1803             variable definition.  */
    16971804          v = parse_variable_definition (&p->variable, defn);
    1698           v->value = xstrdup (v->value);
    1699           if (!v)
    1700             error (flocp, _("Malformed pattern-specific variable definition"));
     1805          assert (v != 0);
     1806
     1807          if (v->flavor == f_simple)
     1808            v->value = allocated_variable_expand (v->value);
     1809          else
     1810            v->value = xstrdup (v->value);
     1811
    17011812          fname = p->target;
    17021813        }
     
    17721883              struct dep *deps, unsigned int cmds_started, char *commands,
    17731884              unsigned int commands_idx, int two_colon,
    1774               int have_sysv_atvar, const struct floc *flocp, int set_default)
     1885              const struct floc *flocp)
    17751886{
    17761887  struct nameseq *nextf;
     
    17791890  char **targets = 0, **target_percents = 0;
    17801891  struct commands *cmds;
     1892
     1893  /* If we've already snapped deps, that means we're in an eval being
     1894     resolved after the makefiles have been read in.  We can't add more rules
     1895     at this time, since they won't get snapped and we'll get core dumps.
     1896     See Savannah bug # 12124.  */
     1897  if (snapped_deps)
     1898    fatal (flocp, _("prerequisites cannot be defined in command scripts"));
    17811899
    17821900  if (commands_idx > 0)
     
    18441962      /* If there are multiple filenames, copy the chain DEPS
    18451963         for all but the last one.  It is not safe for the same deps
    1846          to go in more than one place in the data base.  */
     1964         to go in more than one place in the database.  */
    18471965      this = nextf != 0 ? copy_dep_chain (deps) : deps;
    18481966
     
    18611979            }
    18621980          else
    1863             {
    1864               /* We use patsubst_expand to do the work of translating
    1865                  the target pattern, the target's name and the dependencies'
    1866                  patterns into plain dependency names.  */
    1867               char *buffer = variable_expand ("");
    1868 
    1869               for (d = this; d != 0; d = d->next)
    1870                 {
    1871                   char *o;
    1872                   char *percent = find_percent (d->name);
    1873                   if (percent == 0)
    1874                     continue;
    1875                   o = patsubst_expand (buffer, name, pattern, d->name,
    1876                                        pattern_percent, percent);
    1877                   /* If the name expanded to the empty string, that's
    1878                      illegal.  */
    1879                   if (o == buffer)
    1880                     fatal (flocp,
    1881                            _("target `%s' leaves prerequisite pattern empty"),
    1882                            name);
    1883                   free (d->name);
    1884                   d->name = savestring (buffer, o - buffer);
    1885                 }
    1886             }
    1887         }
    1888 
    1889       /* If at least one of the dependencies uses $$@ etc. deal with that.
    1890          It would be very nice and very simple to just expand everything, but
    1891          it would break a lot of backward compatibility.  Maybe that's OK
    1892          since we're just emulating a SysV function, and if we do that then
    1893          why not emulate it completely (that's what SysV make does: it
    1894          re-expands the entire prerequisite list, all the time, with $@
    1895          etc. in scope).  But, it would be a pain indeed to document this
    1896          ("iff you use $$@, your prerequisite lists is expanded twice...")
    1897          Ouch.  Maybe better to make the code more complex.  */
    1898 
    1899       if (have_sysv_atvar)
    1900         {
    1901           char *p;
    1902           int tlen = strlen (name);
    1903           char *fnp = strrchr (name, '/');
    1904           int dlen;
    1905           int flen;
    1906 
    1907           if (fnp)
    1908             {
    1909               dlen = fnp - name;
    1910               ++fnp;
    1911               flen = strlen (fnp);
    1912             }
    1913           else
    1914             {
    1915               dlen = 0;
    1916               fnp = name;
    1917               flen = tlen;
    1918             }
    1919 
    1920 
    1921           for (d = this; d != 0; d = d->next)
    1922             for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
     1981            /* We use subst_expand to do the work of translating % to $* in
     1982               the dependency line.  */
     1983
     1984            if (this != 0 && find_percent (this->name) != 0)
    19231985              {
    1924                 char *s = p;
    1925                 char *at;
    1926                 int atlen;
    1927 
    1928                 /* If it's '$@', '$(@', or '${@', it's escaped */
    1929                 if ((++p)[0] == '$'
    1930                     && (p[1] == '@'
    1931                         || ((p[1] == '(' || p[1] == '{') && p[2] == '@')))
    1932                   {
    1933                     bcopy (p, s, strlen (p)+1);
    1934                     continue;
    1935                   }
    1936 
    1937                 /* Maybe found one.  We like anything of any form matching @,
    1938                    [({]@[}):], or [({]@[DF][}):].  */
    1939 
    1940                 if (! (p[0] == '@'
    1941                        || ((p[0] == '(' || p[0] == '{') && (++p)[0] == '@'
    1942                            && (((++p)[0] == ')' || p[0] == '}' || p[0] == ':')
    1943                                || ((p[1] == ')' || p[1] == '}' || p[1] == ':')
    1944                                    && (p[0] == 'D' || p[0] == 'F'))))))
    1945                   continue;
    1946 
    1947                 /* Found one.  Compute the length and string ptr.  Move p
    1948                    past the variable reference.  */
    1949                 switch (p[0])
    1950                   {
    1951                   case 'D':
    1952                     atlen = dlen;
    1953                     at = name;
    1954                     p += 2;
    1955                     break;
    1956 
    1957                   case 'F':
    1958                     atlen = flen;
    1959                     at = fnp;
    1960                     p += 2;
    1961                     break;
    1962 
    1963                   default:
    1964                     atlen = tlen;
    1965                     at = name;
    1966                     ++p;
    1967                     break;
    1968                   }
    1969 
    1970                 /* Get more space.  */
    1971                 {
    1972                   int soff = s - d->name;
    1973                   int poff = p - d->name;
    1974                   d->name = (char *) xrealloc (d->name,
    1975                                                strlen (d->name) + atlen + 1);
    1976                   s = d->name + soff;
    1977                   p = d->name + poff;
    1978                 }
    1979 
    1980                 /* Copy the string over.  */
    1981                 bcopy(p, s+atlen, strlen (p)+1);
    1982                 bcopy(at, s, atlen);
    1983                 p = s + atlen - 1;
     1986                char *o;
     1987                char *buffer = variable_expand ("");
     1988
     1989                o = subst_expand (buffer, this->name, "%", "$*", 1, 2, 0);
     1990
     1991                free (this->name);
     1992                this->name = savestring (buffer, o - buffer);
     1993                this->need_2nd_expansion = 1;
    19841994              }
    1985         }
     1995        }
    19861996
    19871997      if (!two_colon)
     
    20222032          if (cmds != 0)
    20232033            f->cmds = cmds;
     2034
    20242035          /* Defining .SUFFIXES with no dependencies
    20252036             clears out the list of suffixes.  */
     
    20362047              f->deps = 0;
    20372048            }
    2038           else if (f->deps != 0)
     2049          else if (this != 0)
    20392050            {
    20402051              /* Add the file's old deps and the new ones in THIS together.  */
    20412052
    2042               struct dep *firstdeps, *moredeps;
    2043               if (cmds != 0)
    2044                 {
    2045                   /* This is the rule with commands, so put its deps first.
    2046                      The rationale behind this is that $< expands to the
    2047                      first dep in the chain, and commands use $< expecting
    2048                      to get the dep that rule specifies.  */
    2049                   firstdeps = this;
    2050                   moredeps = f->deps;
    2051                 }
    2052               else
    2053                 {
    2054                   /* Append the new deps to the old ones.  */
    2055                   firstdeps = f->deps;
    2056                   moredeps = this;
    2057                 }
    2058 
    2059               if (firstdeps == 0)
    2060                 firstdeps = moredeps;
    2061               else
    2062                 {
    2063                   d = firstdeps;
    2064                   while (d->next != 0)
    2065                     d = d->next;
    2066                   d->next = moredeps;
    2067                 }
    2068 
    2069               f->deps = firstdeps;
     2053              if (f->deps != 0)
     2054                {
     2055                  struct dep **d_ptr = &f->deps;
     2056
     2057                  while ((*d_ptr)->next != 0)
     2058                    d_ptr = &(*d_ptr)->next;
     2059
     2060                  if (cmds != 0)
     2061                    {
     2062                      /* This is the rule with commands, so put its deps
     2063                         last. The rationale behind this is that $< expands
     2064                         to the first dep in the chain, and commands use $<
     2065                         expecting to get the dep that rule specifies.
     2066                         However the second expansion algorithm reverses
     2067                         the order thus we need to make it last here.  */
     2068
     2069                      (*d_ptr)->next = this;
     2070                    }
     2071                  else
     2072                    {
     2073                      /* This is the rule without commands. Put its
     2074                         dependencies at the end but before dependencies
     2075                         from the rule with commands (if any). This way
     2076                         everything appears in makefile order.  */
     2077
     2078                      if (f->cmds != 0)
     2079                        {
     2080                          this->next = *d_ptr;
     2081                          *d_ptr = this;
     2082                        }
     2083                      else
     2084                        (*d_ptr)->next = this;
     2085                    }
     2086                }
     2087              else
     2088                f->deps = this;
     2089
     2090              /* This is a hack. I need a way to communicate to snap_deps()
     2091                 that the last dependency line in this file came with commands
     2092                 (so that logic in snap_deps() can put it in front and all
     2093                 this $< -logic works). I cannot simply rely on file->cmds
     2094                 being not 0 because of the cases like the following:
     2095
     2096                 foo: bar
     2097                 foo:
     2098                     ...
     2099
     2100                 I am going to temporarily "borrow" UPDATING member in
     2101                 `struct file' for this.   */
     2102
     2103              if (cmds != 0)
     2104                f->updating = 1;
    20702105            }
    2071           else
    2072             f->deps = this;
    20732106
    20742107          /* If this is a static pattern rule, set the file's stem to
     
    20802113              char *buffer = variable_expand ("");
    20812114              char *o = patsubst_expand (buffer, name, pattern, percent,
    2082                                          pattern_percent, percent);
     2115                                         pattern_percent+1, percent+1);
    20832116              f->stem = savestring (buffer, o - buffer);
    20842117            }
     
    21162149        }
    21172150
    2118       /* See if this is first target seen whose name does
    2119          not start with a `.', unless it contains a slash.  */
    2120       if (default_goal_file == 0 && set_default
    2121           && (*name != '.' || strchr (name, '/') != 0
    2122 #ifdef HAVE_DOS_PATHS
    2123                            || strchr (name, '\\') != 0
    2124 #endif
    2125               ))
    2126         {
    2127           int reject = 0;
    2128 
    2129           /* If this file is a suffix, don't
    2130              let it be the default goal file.  */
    2131 
    2132           for (d = suffix_file->deps; d != 0; d = d->next)
    2133             {
    2134               register struct dep *d2;
    2135               if (*dep_name (d) != '.' && streq (name, dep_name (d)))
    2136                 {
    2137                   reject = 1;
    2138                   break;
    2139                 }
    2140               for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
    2141                 {
    2142                   register unsigned int len = strlen (dep_name (d2));
    2143                   if (!strneq (name, dep_name (d2), len))
    2144                     continue;
    2145                   if (streq (name + len, dep_name (d)))
    2146                     {
    2147                       reject = 1;
    2148                       break;
    2149                     }
    2150                 }
    2151               if (reject)
    2152                 break;
    2153             }
    2154 
    2155           if (!reject)
    2156             default_goal_file = f;
    2157         }
     2151      /* If this target is a default target, update DEFAULT_GOAL_FILE.  */
     2152      if (strcmp (*default_goal_name, name) == 0
     2153          && (default_goal_file == 0
     2154              || strcmp (default_goal_file->name, name) != 0))
     2155        default_goal_file = f;
    21582156    }
    21592157
     
    21792177  unsigned int string_len = 0;
    21802178  register char *p = string;
     2179  register int ch;
    21812180
    21822181  while (1)
    21832182    {
    21842183      if (stop2 && blank)
    2185         while (*p != '\0' && *p != stop1 && *p != stop2
    2186                && ! isblank ((unsigned char) *p))
     2184        while ((ch = *p) != '\0' && ch != stop1 && ch != stop2
     2185               && ! isblank ((unsigned char) ch))
    21872186          ++p;
    21882187      else if (stop2)
    2189         while (*p != '\0' && *p != stop1 && *p != stop2)
     2188        while ((ch = *p) != '\0' && ch != stop1 && ch != stop2)
    21902189          ++p;
    21912190      else if (blank)
    2192         while (*p != '\0' && *p != stop1
    2193                && ! isblank ((unsigned char) *p))
     2191        while ((ch = *p) != '\0' && ch != stop1
     2192               && ! isblank ((unsigned char) ch))
    21942193          ++p;
    21952194      else
    2196         while (*p != '\0' && *p != stop1)
     2195        while ((ch = *p) != '\0' && ch != stop1)
    21972196          ++p;
    21982197
    2199       if (*p == '\0')
     2198      if (ch == '\0')
    22002199        break;
    22012200
     
    24992498readstring (struct ebuffer *ebuf)
    25002499{
    2501   char *p;
     2500  char *eol;
    25022501
    25032502  /* If there is nothing left in this buffer, return 0.  */
    2504   if (ebuf->bufnext > ebuf->bufstart + ebuf->size)
     2503  if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
    25052504    return -1;
    25062505
     
    25082507     next logical line (taking into account backslash/newline pairs).  */
    25092508
    2510   p = ebuf->buffer = ebuf->bufnext;
     2509  eol = ebuf->buffer = ebuf->bufnext;
    25112510
    25122511  while (1)
    25132512    {
    25142513      int backslash = 0;
    2515 
    2516       /* Find the next newline.  Keep track of backslashes as we look.  */
    2517       for (; *p != '\n' && *p != '\0'; ++p)
    2518         if (*p == '\\')
    2519           backslash = !backslash;
    2520 
    2521       /* If we got to the end of the string or a newline with no backslash,
    2522          we're done. */
    2523       if (*p == '\0' || !backslash)
     2514      char *bol = eol;
     2515      char *p;
     2516
     2517      /* Find the next newline.  At EOS, stop.  */
     2518      eol = p = strchr (eol , '\n');
     2519      if (!eol)
     2520        {
     2521          ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
     2522          return 0;
     2523        }
     2524
     2525      /* Found a newline; if it's escaped continue; else we're done.  */
     2526      while (p > bol && *(--p) == '\\')
     2527        backslash = !backslash;
     2528      if (!backslash)
    25242529        break;
     2530      ++eol;
    25252531    }
    25262532
    25272533  /* Overwrite the newline char.  */
    2528   *p = '\0';
    2529   ebuf->bufnext = p+1;
     2534  *eol = '\0';
     2535  ebuf->bufnext = eol+1;
    25302536
    25312537  return 0;
Note: See TracChangeset for help on using the changeset viewer.