| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
 1000
 1001
 1002
 1003
 1004
 1005
 1006
 1007
 1008
 1009
 1010
 1011
 1012
 1013
 1014
 1015
 1016
 1017
 1018
 1019
 1020
 1021
 1022
 1023
 1024
 1025
 1026
 1027
 1028
 1029
 1030
 1031
 1032
 1033
 1034
 1035
 1036
 1037
 1038
 1039
 1040
 1041
 1042
 1043
 1044
 1045
 1046
 1047
 1048
 1049
 1050
 1051
 1052
 1053
 1054
 1055
 1056
 1057
 1058
 1059
 1060
 1061
 1062
 1063
 1064
 1065
 1066
 1067
 1068
 1069
 1070
 1071
 1072
 1073
 1074
 1075
 1076
 1077
 1078
 1079
 1080
 1081
 1082
 1083
 1084
 1085
 1086
 1087
 1088
 1089
 1090
 1091
 1092
 1093
 1094
 1095
 1096
 1097
 1098
 1099
 1100
 1101
 1102
 1103
 1104
 1105
 1106
 1107
 1108
 1109
 1110
 1111
 1112
 1113
 1114
 1115
 1116
 1117
 1118
 1119
 1120
 1121
 1122
 1123
 1124
 1125
 1126
 1127
 1128
 1129
 1130
 1131
 1132
 1133
 1134
 1135
 1136
 1137
 1138
 1139
 1140
 1141
 1142
 1143
 1144
 1145
 1146
 1147
 1148
 1149
 1150
 1151
 1152
 1153
 1154
 1155
 1156
 1157
 1158
 1159
 1160
 1161
 1162
 1163
 1164
 1165
 1166
 1167
 1168
 1169
 1170
 1171
 1172
 1173
 1174
 1175
 1176
 1177
 1178
 1179
 1180
 1181
 1182
 1183
 1184
 1185
 1186
 1187
 1188
 1189
 1190
 1191
 1192
 1193
 1194
 1195
 1196
 1197
 1198
 1199
 1200
 1201
 1202
 1203
 1204
 1205
 1206
 1207
 1208
 1209
 1210
 1211
 1212
 1213
 1214
 1215
 1216
 1217
 1218
 1219
 1220
 1221
 1222
 1223
 1224
 1225
 1226
 1227
 1228
 1229
 1230
 1231
 1232
 1233
 1234
 1235
 1236
 1237
 1238
 1239
 1240
 1241
 1242
 1243
 1244
 1245
 1246
 1247
 1248
 1249
 1250
 1251
 1252
 1253
 1254
 1255
 1256
 
 | /*  This file is part of the program psim.
    Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */
#ifndef _PARSE_C_
#define _PARSE_C_
#include <stdio.h>
#include <stdarg.h>
#include "basics.h"
#include "device.h"
#include "tree.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#include <ctype.h>
/* manipulate/lookup device names */
typedef struct _name_specifier {
  /* components in the full length name */
  char *path;
  char *property;
  char *value;
  /* current device */
  char *name;
  char *base;
  char *unit;
  char *args;
  /* previous device */
  char *last_name;
  char *last_base;
  char *last_unit;
  char *last_args;
  /* work area */
  char buf[1024];
} name_specifier;
/* Given a device specifier, break it up into its main components:
   path (and if present) property name and property value. */
STATIC_INLINE_TREE\
(int)
split_device_specifier(device *current,
		       const char *device_specifier,
		       name_specifier *spec)
{
  char *chp = NULL;
  /* expand any leading alias if present */
  if (current != NULL
      && *device_specifier != '\0'
      && *device_specifier != '.'
      && *device_specifier != '/') {
    device *aliases = tree_find_device(current, "/aliases");
    char alias[32];
    int len = 0;
    while (device_specifier[len] != '\0'
	   && device_specifier[len] != '/'
	   && device_specifier[len] != ':'
	   && !isspace(device_specifier[len])) {
      alias[len] = device_specifier[len];
      len++;
      if (len >= sizeof(alias))
	error("split_device_specifier: buffer overflow");
    }
    alias[len] = '\0';
    if (aliases != NULL
	&& device_find_property(aliases, alias)) {
      strcpy(spec->buf, device_find_string_property(aliases, alias));
      strcat(spec->buf, device_specifier + len);
    }
    else {
      strcpy(spec->buf, device_specifier);
    }
  }
  else {
    strcpy(spec->buf, device_specifier);
  }
  /* check no overflow */
  if (strlen(spec->buf) >= sizeof(spec->buf))
    error("split_device_specifier: buffer overflow\n");
  /* strip leading spaces */
  chp = spec->buf;
  while (*chp != '\0' && isspace(*chp))
    chp++;
  if (*chp == '\0')
    return 0;
  /* find the path and terminate it with null */
  spec->path = chp;
  while (*chp != '\0' && !isspace(*chp))
    chp++;
  if (*chp != '\0') {
    *chp = '\0';
    chp++;
  }
  /* and any value */
  while (*chp != '\0' && isspace(*chp))
    chp++;
  spec->value = chp;
  /* now go back and chop the property off of the path */
  if (spec->value[0] == '\0') {
    spec->property = NULL; /*not a property*/
    spec->value = NULL;
  }
  else if (spec->value[0] == '>'
	   || spec->value[0] == '<') {
    /* an interrupt spec */
    spec->property = NULL;
  }
  else {
    chp = strrchr(spec->path, '/');
    if (chp == NULL) {
      spec->property = spec->path;
      spec->path = strchr(spec->property, '\0');
    }
    else {
      *chp = '\0';
      spec->property = chp+1;
    }
  }
  /* and mark the rest as invalid */
  spec->name = NULL;
  spec->base = NULL;
  spec->unit = NULL;
  spec->args = NULL;
  spec->last_name = NULL;
  spec->last_base = NULL;
  spec->last_unit = NULL;
  spec->last_args = NULL;
  return 1;
}
/* given a device specifier break it up into its main components -
   path and property name - assuming that the last `device' is a
   property name. */
STATIC_INLINE_DEVICE\
(int)
split_property_specifier(device *current,
			 const char *property_specifier,
			 name_specifier *spec)
{
  if (split_device_specifier(current, property_specifier, spec)) {
    if (spec->property == NULL) {
      /* force the last name to be a property name */
      char *chp = strrchr(spec->path, '/');
      if (chp == NULL) {
	spec->property = spec->path;
	spec->path = strrchr(spec->property, '\0');;
      }
      else {
	*chp = '\0';
	spec->property = chp+1;
      }
    }
    return 1;
  }
  else
    return 0;
}
/* device the next device name and split it up, return 0 when no more
   names to device */
STATIC_INLINE_TREE\
(int)
split_device_name(name_specifier *spec)
{
  char *chp;
  /* remember what came before */
  spec->last_name = spec->name;
  spec->last_base = spec->base;
  spec->last_unit = spec->unit;
  spec->last_args = spec->args;
  /* finished? */
  if (spec->path[0] == '\0') {
    spec->name = NULL;
    spec->base = NULL;
    spec->unit = NULL;
    spec->args = NULL;
    return 0;
  }
  /* break the current device spec from the path */
  spec->name = spec->path;
  chp = strchr(spec->name, '/');
  if (chp == NULL)
    spec->path = strchr(spec->name, '\0');
  else {
    spec->path = chp+1;
    *chp = '\0';
  }
  /* break out the base */
  if (spec->name[0] == '(') {
    chp = strchr(spec->name, ')');
    if (chp == NULL) {
      spec->base = spec->name;
    }
    else {
      *chp = '\0';
      spec->base = spec->name + 1;
      spec->name = chp + 1;
    }
  }
  else {
    spec->base = spec->name;
  }
  /* now break out the unit */
  chp = strchr(spec->name, '@');
  if (chp == NULL) {
    spec->unit = NULL;
    chp = spec->name;
  }
  else {
    *chp = '\0';
    chp += 1;
    spec->unit = chp;
  }
  /* finally any args */
  chp = strchr(chp, ':');
  if (chp == NULL)
    spec->args = NULL;
  else {
    *chp = '\0';
    spec->args = chp+1;
  }
  return 1;
}
/* device the value, returning the next non-space token */
STATIC_INLINE_TREE\
(char *)
split_value(name_specifier *spec)
{
  char *token;
  if (spec->value == NULL)
    return NULL;
  /* skip leading white space */
  while (isspace(spec->value[0]))
    spec->value++;
  if (spec->value[0] == '\0') {
    spec->value = NULL;
    return NULL;
  }
  token = spec->value;
  /* find trailing space */
  while (spec->value[0] != '\0' && !isspace(spec->value[0]))
    spec->value++;
  /* chop this value out */
  if (spec->value[0] != '\0') {
    spec->value[0] = '\0';
    spec->value++;
  }
  return token;
}
/* traverse the path specified by spec starting at current */
STATIC_INLINE_TREE\
(device *)
split_find_device(device *current,
		  name_specifier *spec)
{
  /* strip off (and process) any leading ., .., ./ and / */
  while (1) {
    if (strncmp(spec->path, "/", strlen("/")) == 0) {
      /* cd /... */
      while (current != NULL && device_parent(current) != NULL)
	current = device_parent(current);
      spec->path += strlen("/");
    }
    else if (strncmp(spec->path, "./", strlen("./")) == 0) {
      /* cd ./... */
      current = current;
      spec->path += strlen("./");
    }
    else if (strncmp(spec->path, "../", strlen("../")) == 0) {
      /* cd ../... */
      if (current != NULL && device_parent(current) != NULL)
	current = device_parent(current);
      spec->path += strlen("../");
    }
    else if (strcmp(spec->path, ".") == 0) {
      /* cd . */
      current = current;
      spec->path += strlen(".");
    }
    else if (strcmp(spec->path, "..") == 0) {
      /* cd . */
      if (current != NULL && device_parent(current) != NULL)
	current = device_parent(current);
      spec->path += strlen("..");
    }
    else
      break;
  }
  /* now go through the path proper */
  if (current == NULL) {
    split_device_name(spec);
    return NULL;
  }
  while (split_device_name(spec)) {
    device *child;
    for (child = device_child(current);
	 child != NULL; child = device_sibling(child)) {
      if (strcmp(spec->name, device_name(child)) == 0) {
	if (spec->unit == NULL)
	  break;
	else {
	  device_unit phys;
	  device_decode_unit(current, spec->unit, &phys);
	  if (memcmp(&phys, device_unit_address(child),
		     sizeof(device_unit)) == 0)
	    break;
	}
      }
    }
    if (child == NULL)
      return current; /* search failed */
    current = child;
  }
  return current;
}
STATIC_INLINE_TREE\
(device *)
split_fill_path(device *current,
		const char *device_specifier,
		name_specifier *spec)
{
  /* break it up */
  if (!split_device_specifier(current, device_specifier, spec))
    device_error(current, "error parsing %s\n", device_specifier);
  /* fill our tree with its contents */
  current = split_find_device(current, spec);
  /* add any additional devices as needed */
  if (spec->name != NULL) {
    do {
      current = device_create(current, spec->base, spec->name,
			      spec->unit, spec->args);
    } while (split_device_name(spec));
  }
  return current;
}
INLINE_TREE\
(void)
tree_init(device *root,
	  psim *system)
{
  TRACE(trace_device_tree, ("tree_init(root=0x%lx, system=0x%lx)\n",
			    (long)root,
			    (long)system));
  /* remove the old, rebuild the new */
  tree_traverse(root, device_clean, NULL, system);
  tree_traverse(root, device_init_static_properties, NULL, system);
  tree_traverse(root, device_init_address, NULL, system);
  tree_traverse(root, device_init_runtime_properties, NULL, system);
  tree_traverse(root, device_init_data, NULL, system);
}
/* <non-white-space> */
STATIC_INLINE_TREE\
(const char *)
skip_token(const char *chp)
{
  while (!isspace(*chp) && *chp != '\0')
    chp++;
  while (isspace(*chp) && *chp != '\0')
    chp++;
  return chp;
}
/* count the number of entries */
STATIC_INLINE_TREE\
(int)
count_entries(device *current,
	      const char *property_name,
	      const char *property_value,
	      int modulo)
{
  const char *chp = property_value;
  int nr_entries = 0;
  while (*chp != '\0') {
    nr_entries += 1;
    chp = skip_token(chp);
  }
  if ((nr_entries % modulo) != 0) {
    device_error(current, "incorrect number of entries for %s property %s, should be multiple of %d",
		 property_name, property_value, modulo);
  }
  return nr_entries / modulo;
}
/* parse: <address> ::= <token> ; device dependant */
STATIC_INLINE_TREE\
(const char *)
parse_address(device *current,
	      device *bus,
	      const char *chp,
	      device_unit *address)
{
  if (device_decode_unit(bus, chp, address) < 0)
    device_error(current, "invalid unit address in %s", chp);
  return skip_token(chp);
}
/* parse: <size> ::= <number> { "," <number> } ; */
STATIC_INLINE_TREE\
(const char *)
parse_size(device *current,
	   device *bus,
	   const char *chp,
	   device_unit *size)
{
  int i;
  int nr;
  const char *curr = chp;
  memset(size, 0, sizeof(*size));
  /* parse the numeric list */
  size->nr_cells = device_nr_size_cells(bus);
  nr = 0;
  while (1) {
    char *next;
    size->cells[nr] = strtoul(curr, &next, 0);
    if (curr == next)
      device_error(current, "Problem parsing <size> %s", chp);
    nr += 1;
    if (next[0] != ',')
      break;
    if (nr == size->nr_cells)
      device_error(current, "Too many values in <size> %s", chp);
    curr = next + 1;
  }
  ASSERT(nr > 0 && nr <= size->nr_cells);
  /* right align the numbers */
  for (i = 1; i <= size->nr_cells; i++) {
    if (i <= nr)
      size->cells[size->nr_cells - i] = size->cells[nr - i];
    else
      size->cells[size->nr_cells - i] = 0;
  }
  return skip_token(chp);
}
/* parse: <reg> ::= { <address> <size> } ; */
STATIC_INLINE_TREE\
(void)
parse_reg_property(device *current,
		   const char *property_name,
		   const char *property_value)
{
  int nr_regs;
  int reg_nr;
  reg_property_spec *regs;
  const char *chp;
  /* determine the number of reg entries by counting tokens */
  nr_regs = count_entries(current, property_name, property_value, 2);
  /* create working space */
  regs = zalloc(nr_regs * sizeof(*regs));
  /* fill it in */
  chp = property_value;
  for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
    chp = parse_address(current, device_parent(current),
			chp, ®s[reg_nr].address);
    chp = parse_size(current, device_parent(current),
		     chp, ®s[reg_nr].size);
  }
  /* create it */
  device_add_reg_array_property(current, property_name,
				regs, nr_regs);
  zfree(regs);
}
/* { <child-address> <parent-address> <child-size> }* */
STATIC_INLINE_TREE\
(void)
parse_ranges_property(device *current,
		      const char *property_name,
		      const char *property_value)
{
  int nr_ranges;
  int range_nr;
  range_property_spec *ranges;
  const char *chp;
  
  /* determine the number of ranges specified */
  nr_ranges = count_entries(current, property_name, property_value, 3);
  /* create a property of that size */
  ranges = zalloc(nr_ranges * sizeof(*ranges));
  /* fill it in */
  chp = property_value;
  for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
    chp = parse_address(current, current,
			chp, &ranges[range_nr].child_address);
    chp = parse_address(current, device_parent(current),
			chp, &ranges[range_nr].parent_address);
    chp = parse_size(current, current,
		     chp, &ranges[range_nr].size);
  }
  /* create it */
  device_add_range_array_property(current, property_name, ranges, nr_ranges);
  zfree(ranges);
}
/* <integer> ... */
STATIC_INLINE_TREE\
(void)
parse_integer_property(device *current,
		       const char *property_name,
		       const char *property_value)
{
  int nr_entries;
  unsigned_cell words[1024];
  /* integer or integer array? */
  nr_entries = 0;
  while (1) {
    char *end;
    words[nr_entries] = strtoul(property_value, &end, 0);
    if (property_value == end)
      break;
    nr_entries += 1;
    if (nr_entries * sizeof(words[0]) >= sizeof(words))
      device_error(current, "buffer overflow");
    property_value = end;
  }
  if (nr_entries == 0)
    device_error(current, "error parsing integer property %s (%s)",
                 property_name, property_value);
  else if (nr_entries == 1)
    device_add_integer_property(current, property_name, words[0]);
  else {
    int i;
    for (i = 0; i < nr_entries; i++) {
      H2BE(words[i]);
    }
    /* perhaphs integer array property is better */
    device_add_array_property(current, property_name, words,
                              sizeof(words[0]) * nr_entries);
  }
}
/* <string> ... */
STATIC_INLINE_TREE\
(void)
parse_string_property(device *current,
		      const char *property_name,
		      const char *property_value)
{
  char **strings;
  const char *chp;
  int nr_strings;
  int approx_nr_strings;
  /* get an estimate as to the number of strings by counting double
     quotes */
  approx_nr_strings = 2;
  for (chp = property_value; *chp; chp++) {
    if (*chp == '"')
      approx_nr_strings++;
  }
  approx_nr_strings = (approx_nr_strings) / 2;
  /* create a string buffer for that many (plus a null) */
  strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
  /* now find all the strings */
  chp = property_value;
  nr_strings = 0;
  while (1) {
    /* skip leading space */
    while (*chp != '\0' && isspace(*chp))
      chp += 1;
    if (*chp == '\0')
      break;
    /* copy it in */
    if (*chp == '"') {
      /* a quoted string - watch for '\' et.al. */
      /* estimate the size and allocate space for it */
      int pos;
      chp++;
      pos = 0;
      while (chp[pos] != '\0' && chp[pos] != '"') {
	if (chp[pos] == '\\' && chp[pos+1] != '\0')
	  pos += 2;
	else
	  pos += 1;
      }
      strings[nr_strings] = zalloc(pos + 1);
      /* copy the string over */
      pos = 0;
      while (*chp != '\0' && *chp != '"') {
	if (*chp == '\\' && *(chp+1) != '\0') {
	  strings[nr_strings][pos] = *(chp+1);
	  chp += 2;
	  pos++;
	}
	else {
	  strings[nr_strings][pos] = *chp;
	  chp += 1;
	  pos++;
	}
      }
      if (*chp != '\0')
	chp++;
      strings[nr_strings][pos] = '\0';
    }
    else {
      /* copy over a single unquoted token */
      int len = 0;
      while (chp[len] != '\0' && !isspace(chp[len]))
	len++;
      strings[nr_strings] = zalloc(len + 1);
      strncpy(strings[nr_strings], chp, len);
      strings[nr_strings][len] = '\0';
      chp += len;
    }
    nr_strings++;
    if (nr_strings > approx_nr_strings)
      device_error(current, "String property %s badly formatted",
		   property_name);
  }
  ASSERT(strings[nr_strings] == NULL); /* from zalloc */
  /* install it */
  if (nr_strings == 0)
    device_add_string_property(current, property_name, "");
  else if (nr_strings == 1)
    device_add_string_property(current, property_name, strings[0]);
  else {
    const char **specs = (const char**)strings; /* stop a bogus error */
    device_add_string_array_property(current, property_name,
				     specs, nr_strings);
  }
  /* flush the created string */
  while (nr_strings > 0) {
    nr_strings--;
    zfree(strings[nr_strings]);
  }
  zfree(strings);
}
/* <path-to-ihandle-device> */
STATIC_INLINE_TREE\
(void)
parse_ihandle_property(device *current,
		       const char *property,
		       const char *value)
{
  ihandle_runtime_property_spec ihandle;
  /* pass the full path */
  ihandle.full_path = value;
  /* save this ready for the ihandle create */
  device_add_ihandle_runtime_property(current, property,
				      &ihandle);
}
EXTERN_TREE\
(device *)
tree_parse(device *current,
	   const char *fmt,
	   ...)
{
  char device_specifier[1024];
  name_specifier spec;
  /* format the path */
  {
    va_list ap;
    va_start(ap, fmt);
    vsprintf(device_specifier, fmt, ap);
    va_end(ap);
    if (strlen(device_specifier) >= sizeof(device_specifier))
      error("device_tree_add_deviced: buffer overflow\n");
  }
  /* construct the tree down to the final device */
  current = split_fill_path(current, device_specifier, &spec);
  /* is there an interrupt spec */
  if (spec.property == NULL
      && spec.value != NULL) {
    char *op = split_value(&spec);
    switch (op[0]) {
    case '>':
      {
	char *my_port_name = split_value(&spec);
	int my_port;
	char *dest_port_name = split_value(&spec);
	int dest_port;
	name_specifier dest_spec;
	char *dest_device_name = split_value(&spec);
	device *dest;
	/* find my name */
	my_port = device_interrupt_decode(current, my_port_name,
					  output_port);
	/* find the dest device and port */
	dest = split_fill_path(current, dest_device_name, &dest_spec);
	dest_port = device_interrupt_decode(dest, dest_port_name,
					    input_port);
	/* connect the two */
	device_interrupt_attach(current,
				my_port,
				dest,
				dest_port,
				permenant_object);
      }
      break;
    default:
      device_error(current, "unreconised interrupt spec %s\n", spec.value);
      break;
    }
  }
  /* is there a property */
  if (spec.property != NULL) {
    if (strcmp(spec.value, "true") == 0)
      device_add_boolean_property(current, spec.property, 1);
    else if (strcmp(spec.value, "false") == 0)
      device_add_boolean_property(current, spec.property, 0);
    else {
      const device_property *property;
      switch (spec.value[0]) {
      case '*':
	parse_ihandle_property(current, spec.property, spec.value + 1);
	break;
      case '[':
	{
	  unsigned8 words[1024];
	  char *curr = spec.value + 1;
	  int nr_words = 0;
	  while (1) {
	    char *next;
	    words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
	    if (curr == next)
	      break;
	    curr = next;
	    nr_words += 1;
	  }
	  device_add_array_property(current, spec.property,
				    words, sizeof(words[0]) * nr_words);
	}
	break;
      case '"':
	parse_string_property(current, spec.property, spec.value);
	break;
      case '!':
	spec.value++;
	property = tree_find_property(current, spec.value);
	if (property == NULL)
	  device_error(current, "property %s not found\n", spec.value);
	device_add_duplicate_property(current,
				      spec.property,
				      property);
	break;
      default:
	if (strcmp(spec.property, "reg") == 0
	    || strcmp(spec.property, "assigned-addresses") == 0
	    || strcmp(spec.property, "alternate-reg") == 0){
	  parse_reg_property(current, spec.property, spec.value);
	}
	else if (strcmp(spec.property, "ranges") == 0) {
	  parse_ranges_property(current, spec.property, spec.value);
	}
	else if (isdigit(spec.value[0])
		 || (spec.value[0] == '-' && isdigit(spec.value[1]))
		 || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
	  parse_integer_property(current, spec.property, spec.value);
	}
	else
	  parse_string_property(current, spec.property, spec.value);
	break;
      }
    }
  }
  return current;
}
INLINE_TREE\
(void)
tree_traverse(device *root,
	      tree_traverse_function *prefix,
	      tree_traverse_function *postfix,
	      void *data)
{
  device *child;
  if (prefix != NULL)
    prefix(root, data);
  for (child = device_child(root);
       child != NULL;
       child = device_sibling(child)) {
    tree_traverse(child, prefix, postfix, data);
  }
  if (postfix != NULL)
    postfix(root, data);
}
STATIC_INLINE_TREE\
(void)
print_address(device *bus,
	      const device_unit *phys)
{
  char unit[32];
  device_encode_unit(bus, phys, unit, sizeof(unit));
  printf_filtered(" %s", unit);
}
STATIC_INLINE_TREE\
(void)
print_size(device *bus,
	   const device_unit *size)
{
  int i;
  for (i = 0; i < size->nr_cells; i++)
    if (size->cells[i] != 0)
      break;
  if (i < size->nr_cells) {
    printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
    i++;
    for (; i < size->nr_cells; i++)
      printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
  }
  else
    printf_filtered(" 0");
}
STATIC_INLINE_TREE\
(void)
print_reg_property(device *me,
		   const device_property *property)
{
  int reg_nr;
  reg_property_spec reg;
  for (reg_nr = 0;
       device_find_reg_array_property(me, property->name, reg_nr, ®);
       reg_nr++) {
    print_address(device_parent(me), ®.address);
    print_size(me, ®.size);
  }
}
STATIC_INLINE_TREE\
(void)
print_ranges_property(device *me,
		      const device_property *property)
{
  int range_nr;
  range_property_spec range;
  for (range_nr = 0;
       device_find_range_array_property(me, property->name, range_nr, &range);
       range_nr++) {
    print_address(me, &range.child_address);
    print_address(device_parent(me), &range.parent_address);
    print_size(me, &range.size);
  }
}
STATIC_INLINE_TREE\
(void)
print_string(const char *string)
{
  printf_filtered(" \"");
  while (*string != '\0') {
    switch (*string) {
    case '"':
      printf_filtered("\\\"");
      break;
    case '\\':
      printf_filtered("\\\\");
      break;
    default:
      printf_filtered("%c", *string);
      break;
    }
    string++;
  }
  printf_filtered("\"");
}
STATIC_INLINE_TREE\
(void)
print_string_array_property(device *me,
			    const device_property *property)
{
  int nr;
  string_property_spec string;
  for (nr = 0;
       device_find_string_array_property(me, property->name, nr, &string);
       nr++) {
    print_string(string);
  }
}
STATIC_INLINE_TREE\
(void)
print_properties(device *me)
{
  const device_property *property;
  for (property = device_find_property(me, NULL);
       property != NULL;
       property = device_next_property(property)) {
    printf_filtered("%s/%s", device_path(me), property->name);
    if (property->original != NULL) {
      printf_filtered(" !");
      printf_filtered("%s/%s", 
		      device_path(property->original->owner),
		      property->original->name);
    }
    else {
      switch (property->type) {
      case array_property:
	if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
	  unsigned_cell *w = (unsigned_cell*)property->array;
	  int cell_nr;
	  for (cell_nr = 0;
	       cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
	       cell_nr++) {
	    printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
	  }
	}
	else {
	  unsigned8 *w = (unsigned8*)property->array;
	  printf_filtered(" [");
	  while ((char*)w - (char*)property->array < property->sizeof_array) {
	    printf_filtered(" 0x%2x", BE2H_1(*w));
	    w++;
	  }
	}
	break;
      case boolean_property:
	{
	  int b = device_find_boolean_property(me, property->name);
	  printf_filtered(" %s", b ? "true"  : "false");
	}
	break;
      case ihandle_property:
	{
	  if (property->array != NULL) {
	    device_instance *instance = device_find_ihandle_property(me, property->name);
	    printf_filtered(" *%s", device_instance_path(instance));
	  }
	  else {
	    /* not yet initialized, ask the device for the path */
	    ihandle_runtime_property_spec spec;
	    device_find_ihandle_runtime_property(me, property->name, &spec);
	    printf_filtered(" *%s", spec.full_path);
	  }
	}
	break;
      case integer_property:
	{
	  unsigned_word w = device_find_integer_property(me, property->name);
	  printf_filtered(" 0x%lx", (unsigned long)w);
	}
	break;
      case range_array_property:
	print_ranges_property(me, property);
	break;
      case reg_array_property:
	print_reg_property(me, property);
	break;
      case string_property:
	{
	  const char *s = device_find_string_property(me, property->name);
	  print_string(s);
	}
	break;
      case string_array_property:
	print_string_array_property(me, property);
	break;
      }
    }
    printf_filtered("\n");
  }
}
STATIC_INLINE_TREE\
(void)
print_interrupts(device *me,
		 int my_port,
		 device *dest,
		 int dest_port,
		 void *ignore_or_null)
{
  char src[32];
  char dst[32];
  device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
  device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
  printf_filtered("%s > %s %s %s\n",
		  device_path(me),
		  src, dst,
		  device_path(dest));
}
STATIC_INLINE_TREE\
(void)
print_device(device *me,
	     void *ignore_or_null)
{
  printf_filtered("%s\n", device_path(me));
  print_properties(me);
  device_interrupt_traverse(me, print_interrupts, NULL);
}
INLINE_TREE\
(void)
tree_print(device *root)
{
  tree_traverse(root,
		print_device, NULL,
		NULL);
}
INLINE_TREE\
(void)
tree_usage(int verbose)
{
  if (verbose == 1) {
    printf_filtered("\n");
    printf_filtered("A device/property specifier has the form:\n");
    printf_filtered("\n");
    printf_filtered("  /path/to/a/device [ property-value ]\n");
    printf_filtered("\n");
    printf_filtered("and a possible device is\n");
    printf_filtered("\n");
  }
  if (verbose > 1) {
    printf_filtered("\n");
    printf_filtered("A device/property specifier (<spec>) has the format:\n");
    printf_filtered("\n");
    printf_filtered("  <spec> ::= <path> [ <value> ] ;\n");
    printf_filtered("  <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
    printf_filtered("  <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
    printf_filtered("  <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
    printf_filtered("  <unit> ::= <number> { \",\" <number> } ;\n");
    printf_filtered("\n");
    printf_filtered("Where:\n");
    printf_filtered("\n");
    printf_filtered("  <name>  is the name of a device (list below)\n");
    printf_filtered("  <unit>  is the unit-address relative to the parent bus\n");
    printf_filtered("  <args>  additional arguments used when creating the device\n");
    printf_filtered("  <value> ::= ( <number> # integer property\n");
    printf_filtered("              | \"[\" { <number> } # array property (byte)\n");
    printf_filtered("              | \"{\" { <number> } # array property (cell)\n");
    printf_filtered("              | [ \"true\" | \"false\" ] # boolean property\n");
    printf_filtered("              | \"*\" <path> # ihandle property\n");
    printf_filtered("              | \"!\" <path> # copy property\n");
    printf_filtered("              | \">\" [ <number> ] <path> # attach interrupt\n");
    printf_filtered("              | \"<\" <path> # attach child interrupt\n");
    printf_filtered("              | \"\\\"\" <text> # string property\n");
    printf_filtered("              | <text> # string property\n");
    printf_filtered("              ) ;\n");
    printf_filtered("\n");
    printf_filtered("And the following are valid device names:\n");
    printf_filtered("\n");
  }
}
INLINE_TREE\
(device_instance *)
tree_instance(device *root,
	      const char *device_specifier)
{
  /* find the device node */
  device *me;
  name_specifier spec;
  if (!split_device_specifier(root, device_specifier, &spec))
    return NULL;
  me = split_find_device(root, &spec);
  if (spec.name != NULL)
    return NULL;
  /* create the instance */
  return device_create_instance(me, device_specifier, spec.last_args);
}
INLINE_TREE\
(device *)
tree_find_device(device *root,
		 const char *path_to_device)
{
  device *node;
  name_specifier spec;
  /* parse the path */
  split_device_specifier(root, path_to_device, &spec);
  if (spec.value != NULL)
    return NULL; /* something wierd */
  /* now find it */
  node = split_find_device(root, &spec);
  if (spec.name != NULL)
    return NULL; /* not a leaf */
  return node;
}
INLINE_TREE\
(const device_property *)
tree_find_property(device *root,
		   const char *path_to_property)
{
  name_specifier spec;
  if (!split_property_specifier(root, path_to_property, &spec))
    device_error(root, "Invalid property path %s", path_to_property);
  root = split_find_device(root, &spec);
  return device_find_property(root, spec.property);
}
INLINE_TREE\
(int)
tree_find_boolean_property(device *root,
			   const char *path_to_property)
{
  name_specifier spec;
  if (!split_property_specifier(root, path_to_property, &spec))
    device_error(root, "Invalid property path %s", path_to_property);
  root = split_find_device(root, &spec);
  return device_find_boolean_property(root, spec.property);
}
INLINE_TREE\
(signed_cell)
tree_find_integer_property(device *root,
			   const char *path_to_property)
{
  name_specifier spec;
  if (!split_property_specifier(root, path_to_property, &spec))
    device_error(root, "Invalid property path %s", path_to_property);
  root = split_find_device(root, &spec);
  return device_find_integer_property(root, spec.property);
}
INLINE_TREE\
(device_instance *)
tree_find_ihandle_property(device *root,
			   const char *path_to_property)
{
  name_specifier spec;
  if (!split_property_specifier(root, path_to_property, &spec))
    device_error(root, "Invalid property path %s", path_to_property);
  root = split_find_device(root, &spec);
  return device_find_ihandle_property(root, spec.property);
}
INLINE_TREE\
(const char *)
tree_find_string_property(device *root,
			  const char *path_to_property)
{
  name_specifier spec;
  if (!split_property_specifier(root, path_to_property, &spec))
    device_error(root, "Invalid property path %s", path_to_property);
  root = split_find_device(root, &spec);
  return device_find_string_property(root, spec.property);
}
#endif /* _PARSE_C_ */
 |