Print this page
    
make: use the more modern wchar routines, not widec.h
    
      
        | Split | Close | 
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/make/bin/pmake.cc
          +++ new/usr/src/cmd/make/bin/pmake.cc
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26   26  
  27   27  /*
  28   28   * Included files
  29   29   */
  30   30  #include <arpa/inet.h>
  31   31  #include <mk/defs.h>
  32   32  #include <mksh/misc.h>
  33   33  #include <netdb.h>
  34   34  #include <netinet/in.h>
  35   35  #include <sys/socket.h>
  36   36  #include <sys/stat.h>
  37   37  #include <sys/types.h>
  38   38  #include <sys/utsname.h>
  39   39  #include <rpc/rpc.h>            /* host2netname(), netname2host() */
  40   40  #include <libintl.h>
  41   41  
  42   42  /*
  43   43   * Defined macros
  44   44   */
  45   45  
  46   46  /*
  47   47   * typedefs & structs
  48   48   */
  49   49  
  50   50  /*
  51   51   * Static variables
  52   52   */
  53   53  
  54   54  /*
  55   55   * File table of contents
  56   56   */
  57   57  static int              get_max(wchar_t **ms_address, wchar_t *hostname);
  58   58  static Boolean          pskip_comment(wchar_t **cp_address);
  59   59  static void             pskip_till_next_word(wchar_t **cp);
  60   60  static Boolean          pskip_white_space(wchar_t **cp_address);
  61   61  
  62   62  
  63   63  /*
  64   64   *      read_make_machines(Name make_machines_name)
  65   65   *
  66   66   *      For backwards compatibility w/ PMake 1.x, when DMake 2.x is
  67   67   *      being run in parallel mode, DMake should parse the PMake startup
  68   68   *      file $(HOME)/.make.machines to get the PMake max jobs.
  69   69   *
  70   70   *      Return value:
  71   71   *              int of PMake max jobs
  72   72   *
  73   73   *      Parameters:
  74   74   *              make_machines_name      Name of .make.machines file
  75   75   *
  76   76   */
  77   77  int
  78   78  read_make_machines(Name make_machines_name)
  79   79  {
  80   80          wchar_t                 c;
  81   81          Boolean                 default_make_machines;
  82   82          struct hostent          *hp;
  83   83          wchar_t                 local_host[MAX_HOSTNAMELEN + 1];
  84   84          char                    local_host_mb[MAX_HOSTNAMELEN + 1] = "";
  85   85          int                     local_host_wslen;
  86   86          wchar_t                 full_host[MAXNETNAMELEN + 1];
  87   87          int                     full_host_wslen = 0;
  88   88          char                    *homedir;
  89   89          Name                    MAKE_MACHINES;
  90   90          struct stat             make_machines_buf;
  91   91          FILE                    *make_machines_file;
  92   92          wchar_t                 *make_machines_list = NULL;
  93   93          char                    *make_machines_list_mb = NULL;
  94   94          wchar_t                 make_machines_path[MAXPATHLEN];
  95   95          char                    mb_make_machines_path[MAXPATHLEN];
  96   96          wchar_t                 *mp;
  97   97          wchar_t                 *ms;
  98   98          int                     pmake_max_jobs = 0;
  99   99          struct utsname          uts_info;
 100  100  
 101  101  
 102  102          MBSTOWCS(wcs_buffer, "MAKE_MACHINES");
 103  103          MAKE_MACHINES = GETNAME(wcs_buffer, FIND_LENGTH);
 104  104          /* Did the user specify a .make.machines file on the command line? */
 105  105          default_make_machines = false;
 106  106          if (make_machines_name == NULL) {
 107  107                  /* Try reading the default .make.machines file, in $(HOME). */
 108  108                  homedir = getenv("HOME");
 109  109                  if ((homedir != NULL) && (strlen(homedir) < (sizeof(mb_make_machines_path) - 16))) {
 110  110                          sprintf(mb_make_machines_path,
 111  111                           "%s/.make.machines", homedir);
 112  112                          MBSTOWCS(make_machines_path, mb_make_machines_path);
 113  113                          make_machines_name = GETNAME(make_machines_path, FIND_LENGTH);
 114  114                          default_make_machines = true;
 115  115                  }
 116  116                  if (make_machines_name == NULL) {
 117  117                          /*
 118  118                           * No $(HOME)/.make.machines file.
 119  119                           * Return 0 for PMake max jobs.
 120  120                           */
 121  121                          return(0);
 122  122                  }
 123  123          }
 124  124  /*
 125  125          make_machines_list_mb = getenv(MAKE_MACHINES->string_mb);
 126  126   */
 127  127          /* Open the .make.machines file. */
 128  128          if ((make_machines_file = fopen(make_machines_name->string_mb, "r")) == NULL) {
 129  129                  if (!default_make_machines) {
 130  130                          /* Error opening .make.machines file. */
 131  131                          fatal(gettext("Open of %s failed: %s"),
 132  132                                make_machines_name->string_mb,
 133  133                                errmsg(errno));
 134  134                  } else {
 135  135                          /*
 136  136                           * No $(HOME)/.make.machines file.
 137  137                           * Return 0 for PMake max jobs.
 138  138                           */
 139  139                          return(0);
 140  140                  }
 141  141          /* Stat the .make.machines file to get the size of the file.  */
 142  142          } else if (fstat(fileno(make_machines_file), &make_machines_buf) < 0) {
 143  143                  /* Error stat'ing .make.machines file. */
 144  144                  fatal(gettext("Stat of %s failed: %s"),
 145  145                        make_machines_name->string_mb,
 146  146                        errmsg(errno));
 147  147          } else {
 148  148                  /* Allocate memory for "MAKE_MACHINES=<contents of .m.m>" */
 149  149                  make_machines_list_mb =
 150  150                    (char *) getmem((int) (strlen(MAKE_MACHINES->string_mb) +
 151  151                                           2 +
 152  152                                           make_machines_buf.st_size));
 153  153                  sprintf(make_machines_list_mb,
 154  154                          "%s=",
 155  155                          MAKE_MACHINES->string_mb);
 156  156                  /* Read in the .make.machines file. */
 157  157                  if (fread(make_machines_list_mb + strlen(MAKE_MACHINES->string_mb) + 1,
 158  158                            sizeof(char),
 159  159                            (int) make_machines_buf.st_size,
 160  160                            make_machines_file) != make_machines_buf.st_size) {
 161  161                          /*
 162  162                           * Error reading .make.machines file.
 163  163                           * Return 0 for PMake max jobs.
 164  164                           */
 165  165                          warning(gettext("Unable to read %s"),
 166  166                                  make_machines_name->string_mb);
 167  167                          (void) fclose(make_machines_file);
 168  168                          retmem_mb((caddr_t) make_machines_list_mb);
 169  169                          return(0);
 170  170                  } else {
 171  171                          (void) fclose(make_machines_file);
 172  172                          /* putenv "MAKE_MACHINES=<contents of .m.m>" */
 173  173                          *(make_machines_list_mb +
 174  174                            strlen(MAKE_MACHINES->string_mb) +
 175  175                            1 +
 176  176                            make_machines_buf.st_size) = (int) nul_char;
 177  177                          if (putenv(make_machines_list_mb) != 0) {
 178  178                                  warning(gettext("Couldn't put contents of %s in environment"),
 179  179                                          make_machines_name->string_mb);
 180  180                          } else {
 181  181                                  make_machines_list_mb += strlen(MAKE_MACHINES->string_mb) + 1;
 182  182                                  make_machines_list = ALLOC_WC(strlen(make_machines_list_mb) + 1);
  
    | ↓ open down ↓ | 182 lines elided | ↑ open up ↑ | 
 183  183                                  (void) mbstowcs(make_machines_list,
 184  184                                                  make_machines_list_mb,
 185  185                                                  (strlen(make_machines_list_mb) + 1));
 186  186                          }
 187  187                  }
 188  188          }
 189  189  
 190  190          uname(&uts_info);
 191  191          strcpy(local_host_mb, &uts_info.nodename[0]);
 192  192          MBSTOWCS(local_host, local_host_mb);
 193      -        local_host_wslen = wslen(local_host);
      193 +        local_host_wslen = wcslen(local_host);
 194  194  
 195  195          // There is no getdomainname() function on Solaris.
 196  196          // And netname2host() function does not work on Linux.
 197  197          // So we have to use different APIs.
 198  198          if (host2netname(mbs_buffer, NULL, NULL) &&
 199  199              netname2host(mbs_buffer, mbs_buffer2, MAXNETNAMELEN+1)) {
 200  200                  MBSTOWCS(full_host, mbs_buffer2);
 201      -                full_host_wslen = wslen(full_host);
      201 +                full_host_wslen = wcslen(full_host);
 202  202          }
 203  203  
 204  204          for (ms = make_machines_list;
 205  205               (ms) && (*ms );
 206  206               ) {
 207  207                  /*
 208  208                   * Skip white space and comments till you reach
 209  209                   * a machine name.
 210  210                   */
 211  211                  pskip_till_next_word(&ms);
 212  212  
 213  213                  /*
 214  214                   * If we haven't reached the end of file, process the
 215  215                   * machine name.
 216  216                   */
 217  217                  if (*ms) {
 218  218                          /* 
 219  219                           * If invalid machine name decrement counter 
 220  220                           * and skip line.
 221  221                           */
 222  222                          mp = ms;
 223  223                          SKIPWORD(ms);
 224  224                          c = *ms;
 225  225                          *ms++ = '\0'; /* Append null to machine name. */
 226  226                          /*
 227  227                           * If this was the beginning of a comment
 228  228                           * (we overwrote a # sign) and it's not
 229  229                           * end of line yet, shift the # sign.
  
    | ↓ open down ↓ | 18 lines elided | ↑ open up ↑ | 
 230  230                           */
 231  231                          if ((c == '#') && (*ms != '\n') && (*ms)) {
 232  232                                  *ms = '#';
 233  233                          }
 234  234                          WCSTOMBS(mbs_buffer, mp);
 235  235                          /*
 236  236                           * Print "Ignoring unknown host" if:
 237  237                           * 1) hostname is longer than MAX_HOSTNAMELEN, or
 238  238                           * 2) hostname is unknown
 239  239                           */
 240      -                        if ((wslen(mp) > MAX_HOSTNAMELEN) ||
      240 +                        if ((wcslen(mp) > MAX_HOSTNAMELEN) ||
 241  241                              ((hp = gethostbyname(mbs_buffer)) == NULL)) {
 242  242                                  warning(gettext("Ignoring unknown host %s"),
 243  243                                          mbs_buffer);
 244  244                                  SKIPTOEND(ms);
 245  245                                  /* Increment ptr if not end of file. */
 246  246                                  if (*ms) {
 247  247                                          ms++;
 248  248                                  }
 249  249                          } else {
 250  250                                  /* Compare current hostname with local_host. */
 251      -                                if (wslen(mp) == local_host_wslen &&
      251 +                                if (wcslen(mp) == local_host_wslen &&
 252  252                                      IS_WEQUALN(mp, local_host, local_host_wslen)) {
 253  253                                          /*
 254  254                                           * Bingo, local_host is in .make.machines.
 255  255                                           * Continue reading.
 256  256                                           */
 257  257                                          pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
 258  258                                  /* Compare current hostname with full_host. */
 259      -                                } else if (wslen(mp) == full_host_wslen &&
      259 +                                } else if (wcslen(mp) == full_host_wslen &&
 260  260                                             IS_WEQUALN(mp, full_host, full_host_wslen)) {
 261  261                                          /*
 262  262                                           * Bingo, full_host is in .make.machines.
 263  263                                           * Continue reading.
 264  264                                           */
 265  265                                          pmake_max_jobs = PMAKE_DEF_MAX_JOBS;
 266  266                                  } else {
 267  267                                          if (c != '\n') {
 268  268                                              SKIPTOEND(ms);
 269  269                                              if (*ms) {
 270  270                                                  ms++;
 271  271                                              }
 272  272                                          }
 273  273                                          continue;
 274  274                                  }
 275  275                                  /* If we get here, local_host is in .make.machines. */
 276  276                                  if (c != '\n')  {
 277  277                                          /* Now look for keyword 'max'. */
 278  278                                          MBSTOWCS(wcs_buffer, "max");
 279  279                                          SKIPSPACE(ms);
 280  280                                          while ((*ms != '\n') && (*ms)) {
 281  281                                                  if (*ms == '#') {
 282  282                                                          pskip_comment(&ms);
 283  283                                                  } else if (IS_WEQUALN(ms, wcs_buffer, 3)) {
 284  284                                                          /* Skip "max". */
 285  285                                                          ms += 3; 
 286  286                                                          pmake_max_jobs = get_max(&ms, mp); 
 287  287                                                          SKIPSPACE(ms);
 288  288                                                  } else {
 289  289                                                          warning(gettext("unknown option for host %s"), mbs_buffer);
 290  290                                                          SKIPTOEND(ms);
 291  291                                                          break;
 292  292                                                  }
 293  293                                          }
 294  294                                  }
 295  295                                  break; /* out of outermost for() loop. */
 296  296                          }
 297  297                  }
 298  298          }
 299  299          retmem(make_machines_list);
 300  300          return(pmake_max_jobs);
 301  301  }
 302  302  
 303  303  /*
 304  304   *      pskip_till_next_word(cp)
 305  305   *
 306  306   *      Parameters:
 307  307   *              cp              the address of the string pointer.
 308  308   *
 309  309   *      On return:
 310  310   *              cp              points to beginning of machine name.
 311  311   *
 312  312   */
 313  313  static void
 314  314  pskip_till_next_word(wchar_t **cp)
 315  315  {
 316  316          /*
 317  317           * Keep recursing until all combinations of white spaces
 318  318           * and comments have been skipped.
 319  319           */
 320  320          if (pskip_white_space(cp) || pskip_comment(cp)) {
 321  321                  pskip_till_next_word(cp);
 322  322          }
 323  323  }
 324  324  
 325  325  /*
 326  326   *      pskip_white_space(cp_address)
 327  327   *
 328  328   *      Advances the string pointer so that it points to the first
 329  329   *      non white character (space/tab/linefeed).
 330  330   *
 331  331   *      Parameters:
 332  332   *              cp_address      the address of the string pointer.
 333  333   *
 334  334   *      Return Value:
 335  335   *                              True if the pointer was changed.
 336  336   *
 337  337   */
 338  338  static Boolean
 339  339  pskip_white_space(wchar_t **cp_address)
 340  340  {
 341  341          wchar_t         *cp = *cp_address;
 342  342  
 343  343          while (*cp && iswspace(*cp)) {
 344  344                  cp++;
 345  345          }
 346  346          /* Have we skipped any characters? */
 347  347          if (cp != *cp_address) {
 348  348                  *cp_address = cp;
 349  349                  return(true);
 350  350          } else {
 351  351                  return(false);
 352  352          }
 353  353  }
 354  354  
 355  355  /*
 356  356   *      pskip_comment(cp_address)
 357  357   *
 358  358   *      If cp_address is pointing to '#' (the beginning of a comment),
 359  359   *      increment the pointer till you reach end of line.
 360  360   *
 361  361   *      Parameters:
 362  362   *              cp_address      the address of the string pointer.
 363  363   *
 364  364   *      Return Value:
 365  365   *                              True if the pointer was changed.
 366  366   *
 367  367   */
 368  368  static Boolean
 369  369  pskip_comment(wchar_t **cp_address)
 370  370  {
 371  371          wchar_t         *cp = *cp_address;
 372  372  
 373  373          /* Is this the beginning of a comment? Skip till end of line. */
 374  374          if (*cp == '#') {
 375  375                  SKIPTOEND(cp);
 376  376          }
 377  377          /* Have we skipped a comment line? */
 378  378          if (cp != *cp_address)  {
 379  379                  *cp_address = cp;
 380  380                  return(true);
 381  381          } else {
 382  382                  return(false);
 383  383          }
 384  384  }
 385  385  
 386  386  static int
 387  387  get_max(wchar_t **ms_address, wchar_t *hostname)
 388  388  {
 389  389          wchar_t         *ms = *ms_address;
 390  390          int             limit = PMAKE_DEF_MAX_JOBS; /* Default setting. */
 391  391  
 392  392          WCSTOMBS(mbs_buffer, hostname);
 393  393          /* Look for `='. */
 394  394          SKIPSPACE(ms);
 395  395          if ((!*ms) || (*ms == '\n') || (*ms != '=')) {
 396  396                  SKIPTOEND(ms);
 397  397                  warning(gettext("expected `=' after max, ignoring rest of line for host %s"),
 398  398                          mbs_buffer);
 399  399                  *ms_address = ms;
 400  400                  return((int) limit);
 401  401          } else {
 402  402                  ms++;
 403  403                  SKIPSPACE(ms);
 404  404                  if ((*ms != '\n') && (*ms != '\0')) {
 405  405                          /* We've found, hopefully, a valid "max" value. */
 406  406                          limit = (int) wcstol(ms, &ms, 10);
 407  407                          if (limit < 1) {
 408  408                                  limit = PMAKE_DEF_MAX_JOBS;
 409  409                                  warning(gettext("max value cannot be less than or equal to zero for host %s"), mbs_buffer);
 410  410                          }
 411  411                  } else {
 412  412                          /* No "max" value after "max=". */
 413  413                          warning(gettext("no max value specified for host %s"), mbs_buffer);
 414  414                  }
 415  415                  *ms_address = ms;
 416  416                  return(limit);
 417  417          }
 418  418  }
 419  419  
 420  420  
  
    | ↓ open down ↓ | 151 lines elided | ↑ open up ↑ | 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX