Skip to content
Snippets Groups Projects
pval.c 149 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    	/* The following line is coincidence, really! */
    	int s, e;
    
    	mon = ast_strdupa(MON->u1.str);
    
    	/* Check for all days */
    	if (ast_strlen_zero(mon) || !strcmp(mon, "*")) 
    		return ;
    	/* Get start and ending days */
    	c = strchr(mon, '-');
    	if (c) {
    		*c = '\0';
    		c++;
    	}
    	/* Find the start */
    	s = 0;
    	while ((s < 12) && strcasecmp(mon, months[s])) s++;
    	if (s >= 12) {
    		ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
    				MON->filename, MON->startline, MON->endline, mon);
    		warns++;
    	}
    	if (c) {
    		e = 0;
    		while ((e < 12) && strcasecmp(mon, months[e])) e++;
    		if (e >= 12) {
    			ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
    					MON->filename, MON->startline, MON->endline, c);
    			warns++;
    		}
    	} else
    		e = s;
    }
    
    static int check_break(pval *item)
    {
    	pval *p = item;
    	
    	while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
    		/* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
    		   no sense */
    		if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN 
    			|| p->type == PV_WHILE || p->type == PV_FOR   ) {
    			return 1;
    		}
    		p = p->dad;
    	}
    	ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
    			item->filename, item->startline, item->endline);
    	errs++;
    	
    	return 0;
    }
    
    static int check_continue(pval *item)
    {
    	pval *p = item;
    	
    	while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
    		/* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
    		   no sense */
    		if( p->type == PV_WHILE || p->type == PV_FOR   ) {
    			return 1;
    		}
    		p = p->dad;
    	}
    	ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
    			item->filename, item->startline, item->endline);
    	errs++;
    	
    	return 0;
    }
    
    static struct pval *in_macro(pval *item)
    {
    	struct pval *curr;
    	curr = item;	
    	while( curr ) {
    		if( curr->type == PV_MACRO  ) {
    			return curr;
    		}
    		curr = curr->dad;
    	}
    	return 0;
    }
    
    static struct pval *in_context(pval *item)
    {
    	struct pval *curr;
    	curr = item;	
    	while( curr ) {
    		if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
    			return curr;
    		}
    		curr = curr->dad;
    	}
    	return 0;
    }
    
    
    /* general purpose goto finder */
    
    static void check_label(pval *item)
    {
    	struct pval *curr;
    	struct pval *x;
    	int alright = 0;
    	
    	/* A label outside an extension just plain does not make sense! */
    	
    	curr = item;
    	
    	while( curr ) {
    		if( curr->type == PV_MACRO || curr->type == PV_EXTENSION   ) {
    			alright = 1;
    			break;
    		}
    		curr = curr->dad;
    	}
    	if( !alright )
    	{
    		ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
    				item->filename, item->startline, item->endline, item->u1.str);
    		errs++;	
    	}
    	
    	
    	/* basically, ensure that a label is not repeated in a context. Period.
    	   The method:  well, for each label, find the first label in the context
    	   with the same name. If it's not the current label, then throw an error. */
    
    	
    	/* printf("==== check_label:   ====\n"); */
    	if( !current_extension )
    		curr = current_context;
    	else
    		curr = current_extension;
    	
    	x = find_first_label_in_current_context((char *)item->u1.str, curr);
    	/* printf("Hey, check_label found with item = %x, and x is %x, and currcont is %x, label name is %s\n", item,x, current_context, (char *)item->u1.str); */
    	if( x && x != item )
    	{
    		ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
    				item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
    		errs++;
    	}
    	/* printf("<<<<< check_label:   ====\n"); */
    }
    
    static pval *get_goto_target(pval *item)
    {
    	/* just one item-- the label should be in the current extension */
    	pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
    	pval *curr_cont;
    	
    	if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
    		struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
    			return x;
    	}
    
    	curr_cont = get_contxt(item);
    
    	/* TWO items */
    	if (item->u1.list->next && !item->u1.list->next->next) {
    		if (!strstr((item->u1.list)->u1.str,"${") 
    			&& !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
    			struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
    				return x;
    		}
    	}
    	
    	/* All 3 items! */
    	if (item->u1.list->next && item->u1.list->next->next) {
    		/* all three */
    		pval *first = item->u1.list;
    		pval *second = item->u1.list->next;
    		pval *third = item->u1.list->next->next;
    		
    		if (!strstr((item->u1.list)->u1.str,"${") 
    			&& !strstr(item->u1.list->next->u1.str,"${")
    			&& !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
    			struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
    			if (!x) {
    
    				struct pval *p3;
    				struct pval *that_context = find_context(item->u1.list->u1.str);
    				
    				/* the target of the goto could be in an included context!! Fancy that!! */
    				/* look for includes in the current context */
    				if (that_context) {
    					for (p3=that_context->u2.statements; p3; p3=p3->next) {
    						if (p3->type == PV_INCLUDES) {
    							struct pval *p4;
    							for (p4=p3->u1.list; p4; p4=p4->next) {
    								/* for each context pointed to, find it, then find a context/label that matches the
    								   target here! */
    								char *incl_context = p4->u1.str;
    								/* find a matching context name */
    								struct pval *that_other_context = find_context(incl_context);
    								if (that_other_context) {
    									struct pval *x3;
    									x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
    									if (x3) {
    										return x3;
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			return x;
    		}
    	}
    	return 0;
    }
    
    static void check_goto(pval *item)
    {
    	/* check for the target of the goto-- does it exist? */
    	if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
    		ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
    				item->filename, item->startline, item->endline);
    		errs++;
    	}
    	
    	/* just one item-- the label should be in the current extension */
    	
    	if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
    		struct pval *z = get_extension_or_contxt(item);
    		struct pval *x = 0;
    		if (z)
    			x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
    		/* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
    		   (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
    		if (!x) {
    			ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s exists in the current extension!\n",
    					item->filename, item->startline, item->endline, item->u1.list->u1.str);
    			errs++;
    		}
    		else
    			return;
    	}
    	
    	/* TWO items */
    	if (item->u1.list->next && !item->u1.list->next->next) {
    		/* two items */
    		/* printf("Calling find_label_in_current_context with args %s, %s\n",
    		   (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
    		if (!strstr((item->u1.list)->u1.str,"${") 
    			&& !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
    			struct pval *z = get_contxt(item);
    			struct pval *x = 0;
    			
    			if (z)
    				x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
    
    			if (!x) {
    				ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label '%s,%s' exists in the current context, or any of its inclusions!\n",
    						item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
    				errs++;
    			}
    			else
    				return;
    		}
    	}
    	
    	/* All 3 items! */
    	if (item->u1.list->next && item->u1.list->next->next) {
    		/* all three */
    		pval *first = item->u1.list;
    		pval *second = item->u1.list->next;
    		pval *third = item->u1.list->next->next;
    		
    		/* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
    		   (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
    		if (!strstr((item->u1.list)->u1.str,"${") 
    			&& !strstr(item->u1.list->next->u1.str,"${")
    			&& !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
    			struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
    			if (!x) {
    				struct pval *p3;
    				struct pval *found = 0;
    				struct pval *that_context = find_context(item->u1.list->u1.str);
    				
    				/* the target of the goto could be in an included context!! Fancy that!! */
    				/* look for includes in the current context */
    				if (that_context) {
    					for (p3=that_context->u2.statements; p3; p3=p3->next) {
    						if (p3->type == PV_INCLUDES) {
    							struct pval *p4;
    							for (p4=p3->u1.list; p4; p4=p4->next) {
    								/* for each context pointed to, find it, then find a context/label that matches the
    								   target here! */
    								char *incl_context = p4->u1.str;
    								/* find a matching context name */
    								struct pval *that_other_context = find_context(incl_context);
    								if (that_other_context) {
    									struct pval *x3;
    									x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
    									if (x3) {
    										found = x3;
    										break;
    									}
    								}
    							}
    						}
    					}
    					if (!found) {
    						ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the context %s or its inclusions!\n",
    								item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
    						errs++;
    					} else {
    						struct pval *mac = in_macro(item); /* is this goto inside a macro? */
    						if( mac ) {    /* yes! */
    							struct pval *targ = in_context(found);
    							if( mac != targ )
    							{
    								ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
    										item->filename, item->startline, item->endline);
    								warns++;								
    							}
    						}
    					}
    				} else {
    					/* here is where code would go to check for target existence in extensions.conf files */
    #ifdef STANDALONE
    					struct pbx_find_info pfiq = {.stacklen = 0 };
    					extern int localized_pbx_load_module(void);
    					/* if this is a standalone, we will need to make sure the 
    					   localized load of extensions.conf is done */
    					if (!extensions_dot_conf_loaded) {
    						localized_pbx_load_module();
    						extensions_dot_conf_loaded++;
    					}
    
    					pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
    											atoi(third->u1.str) ? NULL : third->u1.str, NULL, 
    											atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
    					
    					if (pfiq.status != STATUS_SUCCESS) {
    						ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  Couldn't find goto target %s|%s|%s, not even in extensions.conf!\n",
    								item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
    						warns++;
    					}
    #else
    					ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  Couldn't find goto target %s|%s|%s in the AEL code!\n",
    							item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
    					warns++;
    #endif
    				}
    			} else {
    				struct pval *mac = in_macro(item); /* is this goto inside a macro? */
    				if( mac ) {    /* yes! */
    					struct pval *targ = in_context(x);
    					if( mac != targ )
    					{
    						ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
    								item->filename, item->startline, item->endline);
    						warns++;								
    					}
    				}
    			}
    		}
    	}
    }
    	
    
    static void find_pval_goto_item(pval *item, int lev)
    {
    	struct pval *p4;
    	if (lev>100) {
    		ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
    		return;
    	}
    	
    	switch ( item->type ) {
    	case PV_MACRO:
    		/* fields: item->u1.str     == name of macro
    		           item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
    				   item->u2.arglist->u1.str  == argument
    				   item->u2.arglist->next   == next arg
    
    				   item->u3.macro_statements == pval list of statements in macro body.
    		*/
    			
    		/* printf("Descending into matching macro %s\n", match_context); */
    		find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
    		
    		break;
    			
    	case PV_CONTEXT:
    		/* fields: item->u1.str     == name of context
    		           item->u2.statements == pval list of statements in context body
    				   item->u3.abstract == int 1 if an abstract keyword were present
    		*/
    		break;
    
    	case PV_CASE:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    			
    	case PV_PATTERN:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    			
    	case PV_DEFAULT:
    		/* fields: 
    		           item->u2.statements == pval list of statements under the case
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    			
    	case PV_CATCH:
    		/* fields: item->u1.str     == name of extension to catch
    		           item->u2.statements == pval list of statements in context body
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    			
    	case PV_STATEMENTBLOCK:
    		/* fields: item->u1.list     == pval list of statements in block, one per entry in the list
    		*/
    		find_pval_gotos(item->u1.list,lev+1);
    		break;
    			
    	case PV_GOTO:
    		/* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
    		           item->u1.list->u1.str  == where the data on a PV_WORD will always be.
    		*/
    		check_goto(item);  /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
    		break;
    			
    	case PV_INCLUDES:
    		/* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
    		*/
    		for (p4=item->u1.list; p4; p4=p4->next) {
    			/* for each context pointed to, find it, then find a context/label that matches the
    			   target here! */
    			char *incl_context = p4->u1.str;
    			/* find a matching context name */
    			struct pval *that_context = find_context(incl_context);
    			if (that_context) {
    				find_pval_gotos(that_context,lev+1); /* keep working up the includes */
    			}
    		}
    		break;
    		
    	case PV_FOR:
    		/* fields: item->u1.for_init     == a string containing the initalizer
    		           item->u2.for_test     == a string containing the loop test
    		           item->u3.for_inc      == a string containing the loop increment
    
    				   item->u4.for_statements == a pval list of statements in the for ()
    		*/
    		find_pval_gotos(item->u4.for_statements,lev+1);
    		break;
    			
    	case PV_WHILE:
    		/* fields: item->u1.str        == the while conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the while ()
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    			
    	case PV_RANDOM:
    		/* fields: item->u1.str        == the random number expression, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		 fall thru to PV_IF */
    		
    	case PV_IFTIME:
    		/* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		fall thru to PV_IF*/
    	case PV_IF:
    		/* fields: item->u1.str        == the if conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		*/
    		find_pval_gotos(item->u2.statements,lev+1);
    
    		if (item->u3.else_statements) {
    			find_pval_gotos(item->u3.else_statements,lev+1);
    		}
    		break;
    			
    	case PV_SWITCH:
    		/* fields: item->u1.str        == the switch expression
    
    				   item->u2.statements == a pval list of statements in the switch, 
    				   							(will be case statements, most likely!)
    		*/
    		find_pval_gotos(item->u3.else_statements,lev+1);
    		break;
    			
    	case PV_EXTENSION:
    		/* fields: item->u1.str        == the extension name, label, whatever it's called
    
    				   item->u2.statements == a pval list of statements in the extension
    				   item->u3.hints      == a char * hint argument
    				   item->u4.regexten   == an int boolean. non-zero says that regexten was specified
    		*/
    
    		find_pval_gotos(item->u2.statements,lev+1);
    		break;
    
    	default:
    		break;
    	}
    }
    
    static void find_pval_gotos(pval *item,int lev)
    {
    	pval *i;
    
    	for (i=item; i; i=i->next) {
    		
    		find_pval_goto_item(i, lev);
    	}
    }
    
    
    
    /* general purpose label finder */
    static struct pval *match_pval_item(pval *item)
    {
    	pval *x;
    	
    	switch ( item->type ) {
    	case PV_MACRO:
    		/* fields: item->u1.str     == name of macro
    		           item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
    				   item->u2.arglist->u1.str  == argument
    				   item->u2.arglist->next   == next arg
    
    				   item->u3.macro_statements == pval list of statements in macro body.
    		*/
    		/* printf("    matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
    		if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
    			
    			/* printf("MACRO: match context is: %s\n", match_context); */
    			
    			if (return_on_context_match && !strcmp(item->u1.str, match_context)) /* if we're just searching for a context, don't bother descending into them */ {
    				/* printf("Returning on matching macro %s\n", match_context); */
    				return item;
    			}
    			
    			
    			if (!return_on_context_match) {
    				/* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
    				if ((x=match_pval(item->u3.macro_statements)))  {
    					/* printf("Responded with pval match %x\n", x); */
    					return x;
    				}
    			}
    		} else {
    			/* printf("Skipping context/macro %s\n", item->u1.str); */
    		}
    		
    		break;
    			
    	case PV_CONTEXT:
    		/* fields: item->u1.str     == name of context
    		           item->u2.statements == pval list of statements in context body
    				   item->u3.abstract == int 1 if an abstract keyword were present
    		*/
    		/* printf("    matching in CONTEXT\n"); */
    		if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
    			if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
    				/* printf("Returning on matching context %s\n", match_context); */
    				/* printf("non-CONTEXT: Responded with pval match %x\n", x); */
    				return item;
    			}
    			
    			if (!return_on_context_match ) {
    				/* printf("Descending into matching context %s\n", match_context); */
    				if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
    					/* printf("CONTEXT: Responded with pval match %x\n", x); */
    					return x;
    				}
    			}
    		} else {
    			/* printf("Skipping context/macro %s\n", item->u1.str); */
    		}
    		break;
    
    	case PV_CASE:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		/* printf("    matching in CASE\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("CASE: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_PATTERN:
    		/* fields: item->u1.str     == value of case
    		           item->u2.statements == pval list of statements under the case
    		*/
    		/* printf("    matching in PATTERN\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("PATTERN: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_DEFAULT:
    		/* fields: 
    		           item->u2.statements == pval list of statements under the case
    		*/
    		/* printf("    matching in DEFAULT\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("DEFAULT: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_CATCH:
    		/* fields: item->u1.str     == name of extension to catch
    		           item->u2.statements == pval list of statements in context body
    		*/
    		/* printf("    matching in CATCH\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("CATCH: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_STATEMENTBLOCK:
    		/* fields: item->u1.list     == pval list of statements in block, one per entry in the list
    		*/
    		/* printf("    matching in STATEMENTBLOCK\n"); */
    		if ((x=match_pval(item->u1.list))) {
    			/* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_LABEL:
    		/* fields: item->u1.str     == label name
    		*/
    		/* printf("PV_LABEL %s (cont=%s, exten=%s\n", 
    		   item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
    		
    		if (count_labels) {
    			if (!strcmp(match_label, item->u1.str)) {
    				label_count++;
    				last_matched_label = item;
    			}
    			
    		} else {
    			if (!strcmp(match_label, item->u1.str)) {
    				/* printf("LABEL: Responded with pval match %x\n", x); */
    				return item;
    			}
    		}
    		break;
    			
    	case PV_FOR:
    		/* fields: item->u1.for_init     == a string containing the initalizer
    		           item->u2.for_test     == a string containing the loop test
    		           item->u3.for_inc      == a string containing the loop increment
    
    				   item->u4.for_statements == a pval list of statements in the for ()
    		*/
    		/* printf("    matching in FOR\n"); */
    		if ((x=match_pval(item->u4.for_statements))) {
    			/* printf("FOR: Responded with pval match %x\n", x);*/
    			return x;
    		}
    		break;
    			
    	case PV_WHILE:
    		/* fields: item->u1.str        == the while conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the while ()
    		*/
    		/* printf("    matching in WHILE\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("WHILE: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_RANDOM:
    		/* fields: item->u1.str        == the random number expression, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		 fall thru to PV_IF */
    		
    	case PV_IFTIME:
    		/* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		fall thru to PV_IF*/
    	case PV_IF:
    		/* fields: item->u1.str        == the if conditional, as supplied by user
    
    				   item->u2.statements == a pval list of statements in the if ()
    				   item->u3.else_statements == a pval list of statements in the else
    											   (could be zero)
    		*/
    		/* printf("    matching in IF/IFTIME/RANDOM\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			return x;
    		}
    		if (item->u3.else_statements) {
    			if ((x=match_pval(item->u3.else_statements))) {
    				/* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
    				return x;
    			}
    		}
    		break;
    			
    	case PV_SWITCH:
    		/* fields: item->u1.str        == the switch expression
    
    				   item->u2.statements == a pval list of statements in the switch, 
    				   							(will be case statements, most likely!)
    		*/
    		/* printf("    matching in SWITCH\n"); */
    		if ((x=match_pval(item->u2.statements))) {
    			/* printf("SWITCH: Responded with pval match %x\n", x); */
    			return x;
    		}
    		break;
    			
    	case PV_EXTENSION:
    		/* fields: item->u1.str        == the extension name, label, whatever it's called
    
    				   item->u2.statements == a pval list of statements in the extension
    				   item->u3.hints      == a char * hint argument
    				   item->u4.regexten   == an int boolean. non-zero says that regexten was specified
    		*/
    		/* printf("    matching in EXTENSION\n"); */
    		if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
    			/* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
    			if (strcmp(match_label,"1") == 0) {
    				if (item->u2.statements) {
    					struct pval *p5 = item->u2.statements;
    					while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
    						p5 = p5->next;
    					if (p5)
    						return p5;
    					else
    						return 0;
    				}
    				else
    					return 0;
    			}
    
    			if ((x=match_pval(item->u2.statements))) {
    				/* printf("EXTENSION: Responded with pval match %x\n", x); */
    				return x;
    			}
    		} else {
    			/* printf("Skipping exten %s\n", item->u1.str); */
    		}
    		break;
    	default:
    		/* printf("    matching in default = %d\n", item->type); */
    		break;
    	}
    	return 0;
    }
    
    struct pval *match_pval(pval *item)
    {
    	pval *i;
    
    	for (i=item; i; i=i->next) {
    		pval *x;
    		/* printf("   -- match pval: item %d\n", i->type); */
    		
    		if ((x = match_pval_item(i))) {
    			/* printf("match_pval: returning x=%x\n", (int)x); */
    			return x; /* cut the search short */
    		}
    	}
    	return 0;
    }
    
    #if 0
    int count_labels_in_current_context(char *label)
    {
    	label_count = 0;
    	count_labels = 1;
    	return_on_context_match = 0;
    	match_pval(current_context->u2.statements);
    	
    	return label_count;
    }
    #endif
    
    struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
    {
    	/* printf("  --- Got args %s, %s\n", exten, label); */
    	struct pval *ret;
    	struct pval *p3;
    	
    	count_labels = 0;
    	return_on_context_match = 0;
    	match_context = "*";
    	match_exten = "*";
    	match_label = label;
    	
    	ret =  match_pval(curr_cont);
    	if (ret)
    		return ret;
    					
    	/* the target of the goto could be in an included context!! Fancy that!! */
    	/* look for includes in the current context */
    	for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
    		if (p3->type == PV_INCLUDES) {
    			struct pval *p4;
    			for (p4=p3->u1.list; p4; p4=p4->next) {
    				/* for each context pointed to, find it, then find a context/label that matches the
    				   target here! */
    				char *incl_context = p4->u1.str;
    				/* find a matching context name */
    				struct pval *that_context = find_context(incl_context);
    				if (that_context) {
    					struct pval *x3;
    					x3 = find_first_label_in_current_context(label, that_context);
    					if (x3) {
    						return x3;
    					}
    				}
    			}
    		}
    	}
    	return 0;
    }
    
    struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
    {
    	/* printf("  --- Got args %s, %s\n", exten, label); */
    	struct pval *ret;
    	struct pval *p3;
    	
    	count_labels = 0;
    	return_on_context_match = 0;
    	match_context = "*";
    	match_exten = exten;
    	match_label = label;
    	ret =  match_pval(curr_cont->u2.statements);
    	if (ret)
    		return ret;
    					
    	/* the target of the goto could be in an included context!! Fancy that!! */
    	/* look for includes in the current context */
    	for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
    		if (p3->type == PV_INCLUDES) {
    			struct pval *p4;
    			for (p4=p3->u1.list; p4; p4=p4->next) {
    				/* for each context pointed to, find it, then find a context/label that matches the
    				   target here! */
    				char *incl_context = p4->u1.str;
    				/* find a matching context name */
    				struct pval *that_context = find_context(incl_context);
    				if (that_context) {
    					struct pval *x3;
    					x3 = find_label_in_current_context(exten, label, that_context);
    					if (x3) {
    						return x3;
    					}
    				}
    			}
    		}
    	}
    	return 0;
    }
    
    static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
    {
    	/* printf("  --- Got args %s\n", label); */
    	count_labels = 0;
    	return_on_context_match = 0;
    	match_context = "*";
    	match_exten = "*";
    	match_label = label;
    	return match_pval(curr_ext);
    }
    
    static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
    {
    	/* printf("  --- Got args %s, %s, %s\n", context, exten, label); */
    	count_labels = 0;
    	return_on_context_match = 0;
    
    	match_context = context;
    	match_exten = exten;
    	match_label = label;
    	
    	return match_pval(current_db);
    }
    
    
    struct pval *find_macro(char *name)
    {
    	return_on_context_match = 1;
    	count_labels = 0;
    	match_context = name;
    	match_exten = "*";  /* don't really need to set these, shouldn't be reached */
    	match_label = "*";
    	return match_pval(current_db);
    }
    
    struct pval *find_context(char *name)
    {
    	return_on_context_match = 1;
    	count_labels = 0;
    	match_context = name;
    	match_exten = "*";  /* don't really need to set these, shouldn't be reached */
    	match_label = "*";
    	return match_pval(current_db);
    }
    
    int is_float(char *arg )
    {
    	char *s;
    	for (s=arg; *s; s++) {
    		if (*s != '.' && (*s < '0' || *s > '9'))
    			return 0;
    	}
    	return 1;
    }
    int is_int(char *arg )
    {
    	char *s;
    	for (s=arg; *s; s++) {
    		if (*s < '0' || *s > '9')
    			return 0;
    	}
    	return 1;
    }
    int is_empty(char *arg)
    {
    	if (!arg)
    		return 1;
    	if (*arg == 0)
    		return 1;
    	while (*arg) {
    		if (*arg != ' ' && *arg != '\t')
    			return 0;
    		arg++;
    	}
    	return 1;
    }
    
    #ifdef AAL_ARGCHECK
    int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
    {
    	struct argchoice *ac;
    	char *opcop,*q,*p;
    	
    	switch (should->dtype) {
    	case ARGD_OPTIONSET:
    		if ( strstr(is->u1.str,"${") )
    			return 0;  /* no checking anything if there's a var reference in there! */
    			
    		opcop = ast_strdupa(is->u1.str);
    
    		for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
    			if ( *q == '(' ) {
    				p = q+1;
    				while (*p && *p != ')' )
    					*p++ = '+';
    				q = p+1;
    			}
    		}
    		
    		for (ac=app->opts; ac; ac=ac->next) {
    			if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
    				return 0;
    		}
    		for (ac=app->opts; ac; ac=ac->next) {
    			if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
    				char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */