1 2 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 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
|
<!-- vim: set sw=2 et sts=2 ft=xml: -->
<!-- Last content review: 2024-01-21T08:10:39 UTC -->
<chapter id="_programming">
<title>Programming</title>
<para>I provide some pointers for people to learn programming on the Debian system enough to trace the packaged source code. Here are notable packages and corresponding documentation packages for programming.</para>
<para>Online references are available by typing "<literal>man name</literal>" after installing <literal>manpages</literal> and <literal>manpages-dev</literal> packages. Online references for the GNU tools are available by typing "<literal>info program_name</literal>" after installing the pertinent documentation packages. You may need to include the <literal>contrib</literal> and <literal>non-free</literal> archives in addition to the <literal>main</literal> archive since some GFDL documentations are not considered to be DFSG compliant.</para>
<para>Please consider to use version control system tools. See <xref linkend="_git"/>. </para>
<warning> <para>Do not use "<literal>test</literal>" as the name of an executable test file. "<literal>test</literal>" is a shell builtin.</para> </warning>
<caution> <para>You should install software programs directly compiled from source into "<literal>/usr/local</literal>" or "<literal>/opt</literal>" to avoid collision with system programs.</para> </caution>
<tip> <para><ulink url="https://www.99-bottles-of-beer.net/">Code examples of creating "Song 99 Bottles of Beer"</ulink> should give you good ideas of practically all the programming languages.</para> </tip>
<section id="_the_shell_script">
<title>The shell script</title>
<para>The <ulink url="https://en.wikipedia.org/wiki/Shell_script">shell script</ulink> is a text file with the execution bit set and contains the commands in the following format.</para>
<screen>#!/bin/sh
... command lines</screen>
<para>The first line specifies the shell interpreter which read and execute this file contents.</para>
<para>Reading shell scripts is the <emphasis role="strong">best</emphasis> way to understand how a Unix-like system works. Here, I give some pointers and reminders for shell programming. See "Shell Mistakes" (<ulink url="https://www.greenend.org.uk/rjk/2001/04/shell.html">https://www.greenend.org.uk/rjk/2001/04/shell.html</ulink>) to learn from mistakes.</para>
<para>Unlike shell interactive mode (see <xref linkend="_the_simple_shell_command"/> and <xref linkend="_unix_like_text_processing"/>), shell scripts frequently use parameters, conditionals, and loops.</para>
<section id="_posix_shell_compatibility">
<title>POSIX shell compatibility</title>
<para>Many system scripts may be interpreted by any one of <ulink url="https://en.wikipedia.org/wiki/POSIX">POSIX</ulink> shells (see <xref linkend="list-of-shell-programs"/>).</para>
<itemizedlist>
<listitem> <para>The default non-interactive POSIX shell "<literal>/usr/bin/sh</literal>" is a symlink pointing to <literal>/usr/bin/dash</literal> and used by many system programs. </para> </listitem>
<listitem> <para>The default interactive POSIX shell is <literal>/usr/bin/bash</literal>. </para> </listitem>
</itemizedlist>
<para>Avoid writing a shell script with <emphasis role="strong">bashisms</emphasis> or <emphasis role="strong">zshisms</emphasis> to make it portable among all POSIX shells. You can check it using <literal>checkbashisms</literal>(1).</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of typical bashisms</title>
<tgroup cols="2">
<colspec colwidth="195pt" align="left"/>
<colspec colwidth="200pt" align="left"/>
<thead>
<row>
<entry> Good: POSIX </entry>
<entry> Avoid: bashism </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>if [ "$foo" = "$bar" ] ; then …</literal> </entry>
<entry> <literal>if [ "$foo" == "$bar" ] ; then …</literal> </entry>
</row>
<row>
<entry> <literal>diff -u file.c.orig file.c</literal> </entry>
<entry> <literal>diff -u file.c{.orig,}</literal> </entry>
</row>
<row>
<entry> <literal>mkdir /foobar /foobaz</literal> </entry>
<entry> <literal>mkdir /foo{bar,baz}</literal> </entry>
</row>
<row>
<entry> <literal>funcname() { … }</literal> </entry>
<entry> <literal>function funcname() { … }</literal> </entry>
</row>
<row>
<entry> octal format: "<literal>\377</literal>" </entry>
<entry> hexadecimal format: "<literal>\xff</literal>" </entry>
</row>
</tbody>
</tgroup>
</table>
<para>The "<literal>echo</literal>" command must be used with following cares since its implementation differs among shell builtin and external commands.</para>
<itemizedlist>
<listitem> <para> Avoid using any command options except "<literal>-n</literal>". </para> </listitem>
<listitem> <para> Avoid using escape sequences in the string since their handling varies. </para> </listitem>
</itemizedlist>
<note> <para>Although "<literal>-n</literal>" option is <emphasis role="strong">not</emphasis> really POSIX syntax, it is generally accepted.</para> </note>
<tip> <para>Use the "<literal>printf</literal>" command instead of the "<literal>echo</literal>" command if you need to embed escape sequences in the output string.</para> </tip>
</section>
<section id="_shell_parameters">
<title>Shell parameters</title>
<para>Special shell parameters are frequently used in the shell script.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of shell parameters</title>
<tgroup cols="2">
<colspec colwidth="86pt" align="left"/>
<colspec colwidth="238pt" align="left"/>
<thead>
<row>
<entry> shell parameter </entry>
<entry> value </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>$0</literal> </entry>
<entry> name of the shell or shell script </entry>
</row>
<row>
<entry> <literal>$1</literal> </entry>
<entry> first (1st) shell argument </entry>
</row>
<row>
<entry> <literal>$9</literal> </entry>
<entry> ninth (9th) shell argument </entry>
</row>
<row>
<entry> <literal>$#</literal> </entry>
<entry> number of positional parameters </entry>
</row>
<row>
<entry> <literal>"$*"</literal> </entry>
<entry> <literal>"$1 $2 $3 $4 … "</literal> </entry>
</row>
<row>
<entry> <literal>"$@"</literal> </entry>
<entry> <literal>"$1" "$2" "$3" "$4" …</literal> </entry>
</row>
<row>
<entry> <literal>$?</literal> </entry>
<entry> exit status of the most recent command </entry>
</row>
<row>
<entry> <literal>$$</literal> </entry>
<entry> PID of this shell script </entry>
</row>
<row>
<entry> <literal>$!</literal> </entry>
<entry> PID of most recently started background job </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Basic <emphasis role="strong">parameter expansions</emphasis> to remember are as follows.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of shell parameter expansions</title>
<tgroup cols="3">
<colspec colwidth="141pt" align="left"/>
<colspec colwidth="119pt" align="left"/>
<colspec colwidth="282pt" align="left"/>
<thead>
<row>
<entry> parameter expression form </entry>
<entry> value if <literal>var</literal> is set </entry>
<entry> value if <literal>var</literal> is not set </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>${var:-string}</literal> </entry>
<entry> "<literal>$var</literal>" </entry>
<entry> "<literal>string</literal>" </entry>
</row>
<row>
<entry> <literal>${var:+string}</literal> </entry>
<entry> "<literal>string</literal>" </entry>
<entry> "<literal>null</literal>" </entry>
</row>
<row>
<entry> <literal>${var:=string}</literal> </entry>
<entry> "<literal>$var</literal>" </entry>
<entry> "<literal>string</literal>" (and run "<literal>var=string</literal>") </entry>
</row>
<row>
<entry> <literal>${var:?string}</literal> </entry>
<entry> "<literal>$var</literal>" </entry>
<entry> echo "<literal>string</literal>" to <emphasis role="strong">stderr</emphasis> (and exit with error) </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Here, the colon "<literal>:</literal>" in all of these operators is actually optional.</para>
<itemizedlist>
<listitem> <para><emphasis role="strong">with</emphasis> "<literal>:</literal>" = operator test for <emphasis role="strong">exist</emphasis> and <emphasis role="strong">not null</emphasis> </para> </listitem>
<listitem> <para><emphasis role="strong">without</emphasis> "<literal>:</literal>" = operator test for <emphasis role="strong">exist</emphasis> only </para> </listitem>
</itemizedlist>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of key shell parameter substitutions</title>
<tgroup cols="2">
<colspec colwidth="152pt" align="left"/>
<colspec colwidth="168pt" align="left"/>
<thead>
<row>
<entry> parameter substitution form </entry>
<entry> result </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>${var%suffix}</literal> </entry>
<entry> remove smallest suffix pattern </entry>
</row>
<row>
<entry> <literal>${var%%suffix}</literal> </entry>
<entry> remove largest suffix pattern </entry>
</row>
<row>
<entry> <literal>${var#prefix}</literal> </entry>
<entry> remove smallest prefix pattern </entry>
</row>
<row>
<entry> <literal>${var##prefix}</literal> </entry>
<entry> remove largest prefix pattern </entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section id="_shell_conditionals">
<title>Shell conditionals</title>
<para>Each command returns an <emphasis role="strong">exit status</emphasis> which can be used for conditional expressions.</para>
<itemizedlist>
<listitem> <para> Success: 0 ("True") </para> </listitem>
<listitem> <para> Error: non 0 ("False") </para> </listitem>
</itemizedlist>
<note> <para>"0" in the shell conditional context means "True", while "0" in the C conditional context means "False".</para> </note>
<note> <para>"<literal>[</literal>" is the equivalent of the <literal>test</literal> command, which evaluates its arguments up to "<literal>]</literal>" as a conditional expression.</para> </note>
<para>Basic <emphasis role="strong">conditional idioms</emphasis> to remember are the following.</para>
<itemizedlist>
<listitem> <para> "<literal><emphasis>command</emphasis> && <emphasis>if_success_run_this_command_too</emphasis> || true</literal>" </para> </listitem>
<listitem> <para> "<literal><emphasis>command</emphasis> || <emphasis>if_not_success_run_this_command_too</emphasis> || true</literal>" </para> </listitem>
<listitem> <para> A multi-line script snippet as the following </para> </listitem>
</itemizedlist>
<screen>if [ <emphasis>conditional_expression</emphasis> ]; then
<emphasis>if_success_run_this_command</emphasis>
else
<emphasis>if_not_success_run_this_command</emphasis>
fi</screen>
<para>Here trailing "<literal>|| true</literal>" was needed to ensure this shell script does not exit at this line accidentally when shell is invoked with "<literal>-e</literal>" flag.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of file comparison operators in the conditional expression</title>
<tgroup cols="2">
<colspec colwidth="130pt" align="left"/>
<colspec colwidth="298pt" align="left"/>
<thead>
<row>
<entry> equation </entry>
<entry> condition to return logical true </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>-e <emphasis>file</emphasis></literal> </entry>
<entry> <emphasis>file</emphasis> exists </entry>
</row>
<row>
<entry> <literal>-d <emphasis>file</emphasis></literal> </entry>
<entry> <emphasis>file</emphasis> exists and is a directory </entry>
</row>
<row>
<entry> <literal>-f <emphasis>file</emphasis></literal> </entry>
<entry> <emphasis>file</emphasis> exists and is a regular file </entry>
</row>
<row>
<entry> <literal>-w <emphasis>file</emphasis></literal> </entry>
<entry> <emphasis>file</emphasis> exists and is writable </entry>
</row>
<row>
<entry> <literal>-x <emphasis>file</emphasis></literal> </entry>
<entry> <emphasis>file</emphasis> exists and is executable </entry>
</row>
<row>
<entry> <literal><emphasis>file1</emphasis> -nt <emphasis>file2</emphasis></literal> </entry>
<entry> <emphasis>file1</emphasis> is newer than <emphasis>file2</emphasis> (modification) </entry>
</row>
<row>
<entry> <literal><emphasis>file1</emphasis> -ot <emphasis>file2</emphasis></literal> </entry>
<entry> <emphasis>file1</emphasis> is older than <emphasis>file2</emphasis> (modification) </entry>
</row>
<row>
<entry> <literal><emphasis>file1</emphasis> -ef <emphasis>file2</emphasis></literal> </entry>
<entry> <emphasis>file1</emphasis> and <emphasis>file2</emphasis> are on the same device and the same inode number </entry>
</row>
</tbody>
</tgroup>
</table>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of string comparison operators in the conditional expression</title>
<tgroup cols="2">
<colspec colwidth="114pt" align="left"/>
<colspec colwidth="298pt" align="left"/>
<thead>
<row>
<entry> equation </entry>
<entry> condition to return logical true </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>-z <emphasis>str</emphasis></literal> </entry>
<entry> the length of <emphasis>str</emphasis> is zero </entry>
</row>
<row>
<entry> <literal>-n <emphasis>str</emphasis></literal> </entry>
<entry> the length of <emphasis>str</emphasis> is non-zero </entry>
</row>
<row>
<entry> <literal><emphasis>str1</emphasis> = <emphasis>str2</emphasis></literal> </entry>
<entry> <emphasis>str1</emphasis> and <emphasis>str2</emphasis> are equal </entry>
</row>
<row>
<entry> <literal><emphasis>str1</emphasis> != <emphasis>str2</emphasis></literal> </entry>
<entry> <emphasis>str1</emphasis> and <emphasis>str2</emphasis> are not equal </entry>
</row>
<row>
<entry> <literal><emphasis>str1</emphasis> < <emphasis>str2</emphasis></literal> </entry>
<entry> <emphasis>str1</emphasis> sorts before <emphasis>str2</emphasis> (locale dependent) </entry>
</row>
<row>
<entry> <literal><emphasis>str1</emphasis> > <emphasis>str2</emphasis></literal> </entry>
<entry> <emphasis>str1</emphasis> sorts after <emphasis>str2</emphasis> (locale dependent) </entry>
</row>
</tbody>
</tgroup>
</table>
<para><emphasis role="strong">Arithmetic</emphasis> integer comparison operators in the conditional expression are "<literal>-eq</literal>", "<literal>-ne</literal>", "<literal>-lt</literal>", "<literal>-le</literal>", "<literal>-gt</literal>", and "<literal>-ge</literal>".</para>
</section>
<section id="_shell_loops">
<title>Shell loops</title>
<para>There are several loop idioms to use in POSIX shell.</para>
<itemizedlist>
<listitem> <para> "<literal>for x in foo1 foo2 … ; do command ; done</literal>" loops by assigning items from the list "<literal>foo1 foo2 …</literal>" to variable "<literal>x</literal>" and executing "<literal>command</literal>". </para> </listitem>
<listitem> <para> "<literal>while condition ; do command ; done</literal>" repeats "<literal>command</literal>" while "<literal>condition</literal>" is true. </para> </listitem>
<listitem> <para> "<literal>until condition ; do command ; done</literal>" repeats "<literal>command</literal>" while "<literal>condition</literal>" is not true. </para> </listitem>
<listitem> <para> "<literal>break</literal>" enables to exit from the loop. </para> </listitem>
<listitem> <para> "<literal>continue</literal>" enables to resume the next iteration of the loop. </para> </listitem>
</itemizedlist>
<tip> <para>The <ulink url="https://en.wikipedia.org/wiki/C_(programming_language)">C</ulink>-language like numeric iteration can be realized by using <literal>seq</literal>(1) as the "<literal>foo1 foo2 …</literal>" generator.</para> </tip>
<tip> <para>See <xref linkend="_repeating_a_command_looping_over_files"/>.</para> </tip>
</section>
<section id="_shell_environment_variables">
<title>Shell environment variables</title>
<para>Some popular environment variables for the normal shell command prompt may not be available under the execution environment of your script.</para>
<itemizedlist>
<listitem> <para> For "<literal>$USER</literal>", use "<literal>$(id -un)</literal>" </para> </listitem>
<listitem> <para> For "<literal>$UID</literal>", use "<literal>$(id -u)</literal>" </para> </listitem>
<listitem> <para> For "<literal>$HOME</literal>", use "<literal>$(getent passwd "$(id -u)"|cut -d ":" -f 6)</literal>" (this works also on <xref linkend="_the_modern_centralized_system_management"/>) </para> </listitem>
</itemizedlist>
</section>
<section id="_the_shell_command_line_processing_sequence">
<title>The shell command-line processing sequence</title>
<para>The shell processes a script roughly as the following sequence.</para>
<itemizedlist>
<listitem> <para> The shell reads a line. </para> </listitem>
<listitem> <para> The shell groups a part of the line as <emphasis role="strong">one token</emphasis> if it is within <literal>"…"</literal> or <literal>'…'</literal>. </para> </listitem>
<listitem> <para> The shell splits other part of a line into <emphasis role="strong">tokens</emphasis> by the following. </para>
<itemizedlist>
<listitem> <para> Whitespaces: <literal><emphasis>space</emphasis> <emphasis>tab</emphasis> <emphasis>newline</emphasis></literal> </para> </listitem>
<listitem> <para> Metacharacters: <literal>< > | ; & ( )</literal> </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> The shell checks the <emphasis role="strong">reserved word</emphasis> for each token to adjust its behavior if not within <literal>"…"</literal> or <literal>'…'</literal>. </para>
<itemizedlist>
<listitem> <para><emphasis role="strong">reserved word</emphasis>: <literal>if then elif else fi for in while unless do done case esac</literal></para> </listitem>
</itemizedlist>
</listitem>
<listitem> <para> The shell expands <emphasis role="strong">alias</emphasis> if not within <literal>"…"</literal> or <literal>'…'</literal>. </para> </listitem>
<listitem>
<para> The shell expands <emphasis role="strong">tilde</emphasis> if not within <literal>"…"</literal> or <literal>'…'</literal>. </para>
<itemizedlist>
<listitem> <para> "<literal>~</literal>" → current user's home directory </para> </listitem>
<listitem> <para> "<literal>~<emphasis>user</emphasis></literal>" → <literal><emphasis>user</emphasis></literal>'s home directory </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> The shell expands <emphasis role="strong">parameter</emphasis> to its value if not within <literal>'…'</literal>. </para>
<itemizedlist>
<listitem> <para><emphasis role="strong">parameter</emphasis>: "<literal>$PARAMETER</literal>" or "<literal>${PARAMETER}</literal>" </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> The shell expands <emphasis role="strong">command substitution</emphasis> if not within <literal>'…'</literal>. </para>
<itemizedlist>
<listitem> <para> "<literal>$( command )</literal>" → the output of "<literal>command</literal>" </para> </listitem>
<listitem> <para> "<literal>` command `</literal>" → the output of "<literal>command</literal>" </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> The shell expands <emphasis role="strong">pathname glob</emphasis> to matching file names if not within <literal>"…"</literal> or <literal>'…'</literal>. </para>
<itemizedlist>
<listitem> <para><literal>*</literal> → any characters </para> </listitem>
<listitem> <para><literal>?</literal> → one character </para> </listitem>
<listitem> <para><literal>[…]</literal> → any one of the characters in "<literal>…</literal>" </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> The shell looks up <emphasis role="strong">command</emphasis> from the following and execute it. </para>
<itemizedlist>
<listitem> <para><emphasis role="strong">function</emphasis> definition </para> </listitem>
<listitem> <para><emphasis role="strong">builtin</emphasis> command </para> </listitem>
<listitem> <para><emphasis role="strong">executable file</emphasis> in "<literal>$PATH</literal>" </para> </listitem>
</itemizedlist>
</listitem>
<listitem> <para> The shell goes to the next line and repeats this process again from the top of this sequence. </para> </listitem>
</itemizedlist>
<para>Single quotes within double quotes have no effect.</para>
<para>Executing "<literal>set -x</literal>" in the shell or invoking the shell with "<literal>-x</literal>" option make the shell to print all of commands executed. This is quite handy for debugging.</para>
</section>
<section id="_utility_programs_for_shell_script">
<title>Utility programs for shell script</title>
<para>In order to make your shell program as portable as possible across Debian systems, it is a good idea to limit utility programs to ones provided by <emphasis role="strong">essential</emphasis> packages.</para>
<itemizedlist>
<listitem> <para> "<literal>aptitude search ~E</literal>" lists <emphasis role="strong">essential</emphasis> packages. </para> </listitem>
<listitem> <para> "<literal>dpkg -L <emphasis>package_name</emphasis> |grep '/man/man.*/'</literal>" lists manpages for commands offered by <literal><emphasis>package_name</emphasis></literal> package. </para> </listitem>
</itemizedlist>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of packages containing small utility programs for shell scripts</title>
<tgroup cols="4">
<colspec colwidth="81pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="255pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>dash</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> small and fast POSIX-compliant shell for <literal>sh</literal> </entry>
</row>
<row>
<entry> <literal>coreutils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU core utilities </entry>
</row>
<row>
<entry> <literal>grep</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU <literal>grep</literal>, <literal>egrep</literal> and <literal>fgrep</literal> </entry>
</row>
<row>
<entry> <literal>sed</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU <literal>sed</literal> </entry>
</row>
<row>
<entry> <literal>mawk</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> small and fast <literal>awk</literal> </entry>
</row>
<row>
<entry> <literal>debianutils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> miscellaneous utilities specific to Debian </entry>
</row>
<row>
<entry> <literal>bsdutils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> basic utilities from 4.4BSD-Lite </entry>
</row>
<row>
<entry> <literal>bsdextrautils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> extra utilities from 4.4BSD-Lite </entry>
</row>
<row>
<entry> <literal>moreutils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> additional Unix utilities </entry>
</row>
</tbody>
</tgroup>
</table>
<tip> <para>Although <literal>moreutils</literal> may not exist outside of Debian, it offers interesting small programs. Most notable one is <literal>sponge</literal>(8) which is quite useful when you wish to overwrite original file.</para> </tip>
<para>See <xref linkend="_unix_like_text_processing"/> for examples.</para>
</section>
</section>
<section id="_scripting_in_interpreted_languages">
<title>Scripting in interpreted languages</title>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of interpreter related packages</title>
<tgroup cols="4">
<colspec colwidth="97pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="396pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> documentation </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>dash</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Unix_shell">sh</ulink>: small and fast POSIX-compliant shell for <literal>sh</literal> </entry>
</row>
<row>
<entry> <literal>bash</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Unix_shell">sh</ulink>: "<literal>info bash</literal>" provided by <literal>bash-doc</literal> </entry>
</row>
<row>
<entry> <literal>mawk</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/AWK">AWK</ulink>: small and fast <literal>awk</literal> </entry>
</row>
<row>
<entry> <literal>gawk</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/AWK">AWK</ulink>: "<literal>info gawk</literal>" provided by <literal>gawk-doc</literal> </entry>
</row>
<row>
<entry> <literal>perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Perl">Perl</ulink>: <literal>perl</literal>(1) and html pages provided by <literal>perl-doc</literal> and <literal>perl-doc-html</literal> </entry>
</row>
<row>
<entry> <literal>libterm-readline-gnu-perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Perl extension for the GNU ReadLine/History Library: <literal>perlsh</literal>(1) </entry>
</row>
<row>
<entry> <literal>libreply-perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> REPL for Perl: <literal>reply</literal>(1) </entry>
</row>
<row>
<entry> <literal>libdevel-repl-perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> REPL for Perl: <literal>re.pl</literal>(1) </entry>
</row>
<row>
<entry> <literal>python3</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Python_(programming_language)">Python</ulink>: <literal>python3</literal>(1) and html pages provided by <literal>python3-doc</literal></entry> </row>
<row>
<entry> <literal>tcl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Tcl">Tcl</ulink>: <literal>tcl</literal>(3) and detail manual pages provided by <literal>tcl-doc</literal></entry> </row>
<row>
<entry> <literal>tk</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Tk_(software)">Tk</ulink>: <literal>tk</literal>(3) and detail manual pages provided by <literal>tk-doc</literal></entry>
</row>
<row>
<entry> <literal>ruby</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</ulink>: <literal>ruby</literal>(1), <literal>erb</literal>(1), <literal>irb</literal>(1), <literal>rdoc</literal>(1), <literal>ri</literal>(1) </entry>
</row>
</tbody>
</tgroup>
</table>
<para>When you wish to automate a task on Debian, you should script it with an interpreted language first. The guide line for the choice of the interpreted language is:</para>
<itemizedlist>
<listitem> <para> Use <literal>dash</literal>, if the task is a simple one which combines CLI programs with a shell program. </para> </listitem>
<listitem> <para> Use <literal>python3</literal>, if the task isn't a simple one and you are writing it from scratch. </para> </listitem>
<listitem> <para> Use <literal>perl</literal>, <literal>tcl</literal>, <literal>ruby</literal>, ... if there is an existing code using one of these languages on Debian which needs to be touched up to do the task. </para> </listitem>
</itemizedlist>
<para>If the resulting code is too slow, you can rewrite only the critical portion for the execution speed in a compiled language and call it from the interpreted language.</para>
<section id="_debugging_interpreter_codes">
<title>Debugging interpreted language codes</title>
<para>Most interpreters offer basic syntax check and code tracing functionalities.</para>
<itemizedlist>
<listitem> <para> “<emphasis role="strong">dash -n</emphasis> <emphasis>script.sh</emphasis>” - Syntax check of a Shell script </para> </listitem>
<listitem> <para> “<emphasis role="strong">dash -x</emphasis> <emphasis>script.sh</emphasis>” - Trace a Shell script </para> </listitem>
<listitem> <para> “<emphasis role="strong">python -m py_compile</emphasis> <emphasis>script.py</emphasis>” - Syntax check of a Python script </para> </listitem>
<listitem> <para> “<emphasis role="strong">python -mtrace --trace</emphasis> <emphasis>script.py</emphasis>” - Trace a Python script </para> </listitem>
<listitem> <para> “<emphasis role="strong">perl -I ../libpath -c</emphasis> <emphasis>script.pl</emphasis>” - Syntax check of a Perl script </para> </listitem>
<listitem> <para> “<emphasis role="strong">perl -d:Trace</emphasis> <emphasis>script.pl</emphasis>” - Trace a Perl script </para> </listitem>
</itemizedlist>
<para>For testing code for <literal>dash</literal>, try <xref linkend="_readline_wrapper"/> which accommodates <literal>bash</literal>-like interactive environment.</para>
<para>For testing code for <literal>perl</literal>, try REPL environment for Perl which accommodates <ulink url="https://en.wikipedia.org/wiki/Python_(programming_language)">Python</ulink>-like <ulink url="https://en.wikipedia.org/wiki/Read–eval–print_loop">REPL (=READ + EVAL + PRINT + LOOP)</ulink> environment for <ulink url="https://en.wikipedia.org/wiki/Perl">Perl</ulink>. </para>
</section>
<section id="_gui_program_with_the_shell_script">
<title>GUI program with the shell script</title>
<para>The shell script can be improved to create an attractive GUI program. The trick is to use one of so-called dialog programs instead of dull interaction using <literal>echo</literal> and <literal>read</literal> commands.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of dialog programs</title>
<tgroup cols="4">
<colspec colwidth="65pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="456pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>x11-utils</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>xmessage</literal>(1): display a message or query in a window (X) </entry>
</row>
<row>
<entry> <literal>whiptail</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> displays user-friendly dialog boxes from shell scripts (newt) </entry>
</row>
<row>
<entry> <literal>dialog</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> displays user-friendly dialog boxes from shell scripts (ncurses) </entry>
</row>
<row>
<entry> <literal>zenity</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> display graphical dialog boxes from shell scripts (GTK) </entry>
</row>
<row>
<entry> <literal>ssft</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Shell Scripts Frontend Tool (wrapper for zenity, kdialog, and dialog with gettext) </entry>
</row>
<row>
<entry> <literal>gettext</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>/usr/bin/gettext.sh</literal>": translate message </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Here is an example of GUI program to demonstrate how easy it is just with a shell script.</para>
<para>This script uses <literal>zenity</literal> to select a file (default <literal>/etc/motd</literal>) and display it.</para>
<para>GUI launcher for this script can be created following <xref linkend="_starting_a_program_from_gui"/>.</para>
<screen>#!/bin/sh -e
# Copyright (C) 2021 Osamu Aoki <osamu@debian.org>, Public Domain
# vim:set sw=2 sts=2 et:
DATA_FILE=$(zenity --file-selection --filename="/etc/motd" --title="Select a file to check") || \
( echo "E: File selection error" >&2 ; exit 1 )
# Check size of archive
if ( file -ib "$DATA_FILE" | grep -qe '^text/' ) ; then
zenity --info --title="Check file: $DATA_FILE" --width 640 --height 400 \
--text="$(head -n 20 "$DATA_FILE")"
else
zenity --info --title="Check file: $DATA_FILE" --width 640 --height 400 \
--text="The data is MIME=$(file -ib "$DATA_FILE")"
fi</screen>
<para>This kind of approach to GUI program with the shell script is useful only for simple choice cases. If you are to write any program with complexities, please consider writing it on more capable platform.</para>
</section>
<section id="_custom_actions_for_gui_filer">
<title>Custom actions for GUI filer</title>
<para>GUI filer programs can be extended to perform some popular actions on selected files using additional extension packages. They can also made to perform very specific custom actions by adding your specific scripts.</para>
<itemizedlist>
<listitem> <para>For GNOME, see <ulink url="https://help.ubuntu.com/community/NautilusScriptsHowto">NautilusScriptsHowto</ulink>. </para> </listitem>
<listitem> <para>For KDE, see <ulink url="https://develop.kde.org/docs/dolphin/service-menus/">Creating Dolphin Service Menus</ulink>. </para> </listitem>
<listitem> <para>For Xfce, see <ulink url="https://docs.xfce.org/xfce/thunar/custom-actions">Thunar - Custom Actions</ulink> and <ulink url="https://help.ubuntu.com/community/ThunarCustomActions"></ulink>. </para> </listitem>
<listitem> <para>For LXDE, see <ulink url="https://manual.lubuntu.me/stable/2/2.4/2.4.4/pcmanfm-qt.html#custom-actions">Custom Actions</ulink>. </para> </listitem>
</itemizedlist>
</section>
<section id="_perl_short_script_madness">
<title>Perl short script madness</title>
<para>In order to process data, <literal>sh</literal> needs to spawn sub-process running <literal>cut</literal>, <literal>grep</literal>, <literal>sed</literal>, etc., and is slow. On the other hand, <literal>perl</literal> has internal capabilities to process data, and is fast. So many system maintenance scripts on Debian use <literal>perl</literal>.</para>
<para>Let's think following one-liner AWK script snippet and its equivalents in Perl.</para>
<screen>awk '($2=="1957") { print $3 }' |</screen>
<para>This is equivalent to any one of the following lines.</para>
<screen>perl -ne '@f=split; if ($f[1] eq "1957") { print "$f[2]\n"}' |</screen>
<screen>perl -ne 'if ((@f=split)[1] eq "1957") { print "$f[2]\n"}' |</screen>
<screen>perl -ne '@f=split; print $f[2] if ( $f[1]==1957 )' |</screen>
<screen>perl -lane 'print $F[2] if $F[1] eq "1957"' |</screen>
<screen>perl -lane 'print$F[2]if$F[1]eq+1957' |</screen>
<para>The last one is a riddle. It took advantage of following Perl features.</para>
<itemizedlist>
<listitem> <para> The whitespace is optional. </para> </listitem>
<listitem> <para> The automatic conversion exists from number to the string. </para> </listitem>
<listitem> <para> Perl execution tricks via command line options: <literal>perlrun</literal>(1) </para> </listitem>
<listitem> <para> Perl special variables: <literal>perlvar</literal>(1) </para> </listitem>
</itemizedlist>
<para>This flexibility is the strength of Perl. At the same time, this allows us to create cryptic and tangled codes. So be careful.</para>
</section>
</section>
<section id="_coding_in_compiled_languages">
<title>Coding in compiled languages</title>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of compiler related packages</title>
<tgroup cols="4">
<colspec colwidth="97pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="396pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>gcc</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU C compiler</entry>
</row>
<row>
<entry> <literal>libc6-dev</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU C Library: Development Libraries and Header Files</entry>
</row>
<row>
<entry> <literal>g++</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU C++ compiler </entry>
</row>
<row>
<entry> <literal>libstdc++-10-dev</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU Standard C++ Library v3 (development files) </entry>
</row>
<row>
<entry> <literal>cpp</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU C preprocessor </entry>
</row>
<row>
<entry> <literal>gettext</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU Internationalization utilities </entry>
</row>
<row>
<entry> <literal>glade</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GTK User Interface Builder </entry>
</row>
<row>
<entry> <literal>valac</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> C# like language for the GObject system </entry>
</row>
<row>
<entry> <literal>flex</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Lex_(software)">LEX</ulink>-compatible <ulink url="https://en.wikipedia.org/wiki/Flex_(lexical_analyser_generator)">fast lexical analyzer generator</ulink> </entry>
</row>
<row>
<entry> <literal>bison</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/Yacc">YACC</ulink>-compatible <ulink url="https://en.wikipedia.org/wiki/GNU_Bison">parser generator</ulink> </entry>
</row>
<row>
<entry> <literal>susv2</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> fetch "<ulink url="https://unix.org/version2/">The Single UNIX Specifications v2</ulink>" </entry>
</row>
<row>
<entry> <literal>susv3</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> fetch "<ulink url="https://unix.org/version3/">The Single UNIX Specifications v3</ulink>" </entry>
</row>
<row>
<entry> <literal>susv4</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> fetch "<ulink url="https://unix.org/version4/">The Single UNIX Specifications v4</ulink>" </entry>
</row>
<row>
<entry> <literal>golang</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Go programming language compiler </entry>
</row>
<row>
<entry> <literal>rustc</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Rust systems programming language </entry>
</row>
<row>
<entry> <literal>haskell-platform</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Standard <ulink url="https://en.wikipedia.org/wiki/Haskell_(programming_language)">Haskell</ulink> libraries and tools </entry>
</row>
<row>
<entry> <literal>gfortran</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> GNU Fortran 95 compiler </entry>
</row>
<row>
<entry> <literal>fpc</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Free Pascal </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Here, <xref linkend="_flex_a_better_lex"/> and <xref linkend="_bison_a_better_yacc"/> are included to indicate how compiler-like program can be written in C language by compiling higher level description into C language.</para>
<section id="_c">
<title>C</title>
<para>You can set up proper environment to compile programs written in the <ulink url="https://en.wikipedia.org/wiki/C_(programming_language)">C programming language</ulink> by the following.</para>
<screen># apt-get install glibc-doc manpages-dev libc6-dev gcc build-essential</screen>
<para>The <literal>libc6-dev</literal> package, i.e., GNU C Library, provides <ulink url="https://en.wikipedia.org/wiki/C_standard_library">C standard library</ulink> which is collection of header files and library routines used by the C programming language.</para>
<para>See references for C as the following.</para>
<itemizedlist>
<listitem> <para> "<literal>info libc</literal>" (C library function reference) </para> </listitem>
<listitem> <para> <literal>gcc</literal>(1) and "<literal>info gcc</literal>" </para> </listitem>
<listitem> <para> <literal>each_C_library_function_name</literal>(3) </para> </listitem>
<listitem> <para> Kernighan & Ritchie, "The C Programming Language", 2nd edition (Prentice Hall) </para> </listitem>
</itemizedlist>
</section>
<section id="_simple_c_program_gcc">
<title>Simple C program (gcc)</title>
<para>A simple example "<literal>example.c</literal>" can compiled with a library "<literal>libm</literal>" into an executable "<literal>run_example</literal>" by the following.</para>
<screen>$ cat > example.c << EOF
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char **argv, char **envp){
double x;
char y[11];
x=sqrt(argc+7.5);
strncpy(y, argv[0], 10); /* prevent buffer overflow */
y[10] = '\0'; /* fill to make sure string ends with '\0' */
printf("%5i, %5.3f, %10s, %10s\n", argc, x, y, argv[1]);
return 0;
}
EOF
$ gcc -Wall -g -o run_example example.c -lm
$ ./run_example
1, 2.915, ./run_exam, (null)
$ ./run_example 1234567890qwerty
2, 3.082, ./run_exam, 1234567890qwerty</screen>
<para>Here, "<literal>-lm</literal>" is needed to link library "<literal>/usr/lib/libm.so</literal>" from the <literal>libc6</literal> package for <literal>sqrt</literal>(3). The actual library is in "<literal>/lib/</literal>" with filename "<literal>libm.so.6</literal>", which is a symlink to "<literal>libm-2.7.so</literal>".</para>
<para>Look at the last parameter in the output text. There are more than 10 characters even though "<literal>%10s</literal>" is specified.</para>
<para>The use of pointer memory operation functions without boundary checks, such as <literal>sprintf</literal>(3) and <literal>strcpy</literal>(3), is deprecated to prevent buffer overflow exploits that leverage the above overrun effects. Instead, use <literal>snprintf</literal>(3) and <literal>strncpy</literal>(3).</para>
</section>
<section id="_flex_a_better_lex">
<title>Flex — a better Lex</title>
<para><ulink url="https://en.wikipedia.org/wiki/Flex_(lexical_analyser_generator)">Flex</ulink> is a <ulink url="https://en.wikipedia.org/wiki/Lex_(software)">Lex</ulink>-compatible fast <ulink url="https://en.wikipedia.org/wiki/Lexical_analysis">lexical analyzer</ulink> generator.</para>
<para>Tutorial for <literal>flex</literal>(1) can be found in "<literal>info flex</literal>".</para>
<para>Many simple examples can be found under "<literal>/usr/share/doc/flex/examples/</literal>". <footnote> <para> Some <ulink url="https://bugs.debian.org/1055082">tweaks</ulink> may be required to get them work under the current system. </para></footnote> </para>
</section>
<section id="_bison_a_better_yacc">
<title>Bison — a better Yacc</title>
<para>Several packages provide a <ulink url="https://en.wikipedia.org/wiki/Yacc">Yacc</ulink>-compatible lookahead <ulink url="https://en.wikipedia.org/wiki/LR_parser">LR parser</ulink> or <ulink url="https://en.wikipedia.org/wiki/LALR_parser">LALR parser</ulink> generator in Debian.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of Yacc-compatible LALR parser generators</title>
<tgroup cols="4">
<colspec colwidth="48pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="358pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>bison</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> <ulink url="https://en.wikipedia.org/wiki/GNU_bison">GNU LALR parser generator</ulink> </entry>
</row>
<row>
<entry> <literal>byacc</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Berkeley LALR parser generator </entry>
</row>
<row>
<entry> <literal>btyacc</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> backtracking parser generator based on <literal>byacc</literal> </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Tutorial for <literal>bison</literal>(1) can be found in "<literal>info bison</literal>".</para>
<para>You need to provide your own "<literal>main()</literal>" and "<literal>yyerror()</literal>". "<literal>main()</literal>" calls "<literal>yyparse()</literal>" which calls "<literal>yylex()</literal>", usually created with Flex.</para>
<para>Here is an example to create a simple terminal calculator program.</para>
<para>Let's create <literal>example.y</literal>:</para>
<screen>
/* calculator source for bison */
%{
#include <stdio.h>
extern int yylex(void);
extern int yyerror(char *);
%}
/* declare tokens */
%token NUMBER
%token OP_ADD OP_SUB OP_MUL OP_RGT OP_LFT OP_EQU
%%
calc:
| calc exp OP_EQU { printf("Y: RESULT = %d\n", $2); }
;
exp: factor
| exp OP_ADD factor { $$ = $1 + $3; }
| exp OP_SUB factor { $$ = $1 - $3; }
;
factor: term
| factor OP_MUL term { $$ = $1 * $3; }
;
term: NUMBER
| OP_LFT exp OP_RGT { $$ = $2; }
;
%%
int main(int argc, char **argv)
{
yyparse();
}
int yyerror(char *s)
{
fprintf(stderr, "error: '%s'\n", s);
}
</screen>
<para>Let's create, <literal>example.l</literal>:</para>
<screen>
/* calculator source for flex */
%{
#include "example.tab.h"
%}
%%
[0-9]+ { printf("L: NUMBER = %s\n", yytext); yylval = atoi(yytext); return NUMBER; }
"+" { printf("L: OP_ADD\n"); return OP_ADD; }
"-" { printf("L: OP_SUB\n"); return OP_SUB; }
"*" { printf("L: OP_MUL\n"); return OP_MUL; }
"(" { printf("L: OP_LFT\n"); return OP_LFT; }
")" { printf("L: OP_RGT\n"); return OP_RGT; }
"=" { printf("L: OP_EQU\n"); return OP_EQU; }
"exit" { printf("L: exit\n"); return YYEOF; } /* YYEOF = 0 */
. { /* ignore all other */ }
%%
</screen>
<para>Then execute as follows from the shell prompt to try this:</para>
<screen>
$ bison -d example.y
$ flex example.l
$ gcc -lfl example.tab.c lex.yy.c -o example
$ ./example
1 + 2 * ( 3 + 1 ) =
L: NUMBER = 1
L: OP_ADD
L: NUMBER = 2
L: OP_MUL
L: OP_LFT
L: NUMBER = 3
L: OP_ADD
L: NUMBER = 1
L: OP_RGT
L: OP_EQU
Y: RESULT = 9
exit
L: exit
</screen>
</section>
</section>
<section id="_static_code_analysis_tools">
<title>Static code analysis tools</title>
<para><ulink url="https://en.wikipedia.org/wiki/Lint_programming_tool">Lint</ulink> like tools can help automatic <ulink url="https://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis">static code analysis</ulink>.</para>
<para><ulink url="https://en.wikipedia.org/wiki/Indent_(Unix)">Indent</ulink> like tools can help human code reviews by reformatting source codes consistently.</para>
<para><ulink url="https://en.wikipedia.org/wiki/Ctags">Ctags</ulink> like tools can help human code reviews by generating an index (or tag) file of names found in source codes.</para>
<tip> <para>Configuring your favorite editor (<literal>emacs</literal> or <literal>vim</literal>) to use asynchronous lint engine plugins helps your code writing. These plugins are getting very powerful by taking advantage of <ulink url="https://en.wikipedia.org/wiki/Language_Server_Protocol">Language Server Protocol</ulink>. Since they are moving fast, using their upstream code instead of Debian package may be a good option.</para> </tip>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of tools for static code analysis</title>
<tgroup cols="4">
<colspec colwidth="86pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="380pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>vim-ale</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Asynchronous Lint Engine for Vim 8 and NeoVim </entry>
</row>
<row>
<entry> <literal>vim-syntastic</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Syntax checking hacks for vim </entry>
</row>
<row>
<entry> <literal>elpa-flycheck</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> modern on-the-fly syntax checking for Emacs </entry>
</row>
<row>
<entry> <literal>elpa-relint</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Emacs Lisp regexp mistake finder </entry>
</row>
<row>
<entry> <literal>cppcheck-gui</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> tool for static C/C++ code analysis (GUI) </entry>
</row>
<row>
<entry> <literal>shellcheck</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> lint tool for shell scripts </entry>
</row>
<row>
<entry> <literal>pyflakes3</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> passive checker of Python 3 programs </entry>
</row>
<row>
<entry> <literal>pylint</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Python code static checker </entry>
</row>
<row>
<entry> <literal>perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> interpreter with internal static code checker: <literal>B::Lint</literal>(3perl) </entry>
</row>
<row>
<entry> <literal>rubocop</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Ruby static code analyzer </entry>
</row>
<row>
<entry> <literal>clang-tidy</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> clang-based C++ linter tool </entry>
</row>
<row>
<entry> <literal>splint</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> tool for statically checking C programs for bugs </entry>
</row>
<row>
<entry> <literal>flawfinder</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> tool to examine C/C++ source code and looks for security weaknesses </entry>
</row>
<row>
<entry> <literal>black</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> uncompromising Python code formatter </entry>
</row>
<row>
<entry> <literal>perltidy</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Perl script indenter and reformatter </entry>
</row>
<row>
<entry> <literal>indent</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> C language source code formatting program </entry>
</row>
<row>
<entry> <literal>astyle</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Source code indenter for C, C++, Objective-C, C#, and Java </entry>
</row>
<row>
<entry> <literal>bcpp</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> C(++) beautifier </entry>
</row>
<row>
<entry> <literal>xmlindent</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> XML stream reformatter </entry>
</row>
<row>
<entry> <literal>global</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> Source code search and browse tools </entry>
</row>
<row>
<entry> <literal>exuberant-ctags</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> build tag file indexes of source code definitions </entry>
</row>
<row>
<entry> <literal>universal-ctags</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> build tag file indexes of source code definitions </entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section id="_debug">
<title>Debug</title>
<para>Debug is important part of programming activities. Knowing how to debug programs makes you a good Debian user who can produce meaningful bug reports.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of debug packages</title>
<tgroup cols="4">
<colspec colwidth="97pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="396pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> documentation </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>gdb</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info gdb</literal>" provided by <literal>gdb-doc</literal> </entry>
</row>
<row>
<entry> <literal>ddd</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info ddd</literal>" provided by <literal>ddd-doc</literal> </entry>
</row>
</tbody>
</tgroup>
</table>
<section id="_basic_gdb_execution">
<title>Basic gdb execution</title>
<para>Primary <ulink url="https://en.wikipedia.org/wiki/Debugger">debugger</ulink> on Debian is <literal>gdb</literal>(1) which enables you to inspect a program while it executes.</para>
<para>Let's install <literal>gdb</literal> and related programs by the following.</para>
<screen># apt-get install gdb gdb-doc build-essential devscripts</screen>
<para>Good tutorial of <literal>gdb</literal> can be found:</para>
<itemizedlist>
<listitem> <para> “<literal>info gdb</literal>” </para> </listitem>
<listitem> <para> “Debugging with GDB” in <literal>/usr/share/doc/gdb-doc/html/gdb/index.html</literal> </para> </listitem>
<listitem> <para> “<ulink url="https://developers.redhat.com/blog/2021/04/30/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger">tutorial on the web</ulink>” </para> </listitem>
</itemizedlist>
<para>Here is a simple example of using <literal>gdb</literal>(1) on a "<literal>program</literal>" compiled with the "<literal>-g</literal>" option to produce debugging information.</para>
<screen>$ gdb program
(gdb) b 1 # set break point at line 1
(gdb) run args # run program with args
(gdb) next # next line
...
(gdb) step # step forward
...
(gdb) p parm # print parm
...
(gdb) p parm=12 # set value to 12
...
(gdb) quit</screen>
<tip> <para>Many <literal>gdb</literal>(1) commands can be abbreviated. Tab expansion works as in the shell.</para> </tip>
</section>
<section id="_debugging_the_debian_package">
<title>Debugging the Debian package</title>
<para>Since all installed binaries should be stripped on the Debian system by default, most debugging symbols are removed in the normal package. In order to debug Debian packages with <literal>gdb</literal>(1), <literal>*-dbgsym</literal> packages need to be installed (e.g. <literal>coreutils-dbgsym</literal> in the case of <literal>coreutils</literal>). The source packages generate <literal>*-dbgsym</literal> packages automatically along with normal binary packages and those debug packages are placed separately in <ulink url="http://deb.debian.org/debian-debug/">debian-debug</ulink> archive. Please refer to <ulink url="https://wiki.debian.org/DebugPackage">articles on Debian Wiki</ulink> for more information.</para>
<para>If a package to be debugged does not provide its <literal>*-dbgsym</literal> package, you need to install it after rebuilding it by the following.</para>
<screen>$ mkdir /path/new ; cd /path/new
$ sudo apt-get update
$ sudo apt-get dist-upgrade
$ sudo apt-get install fakeroot devscripts build-essential
$ apt-get source package_name
$ cd package_name*
$ sudo apt-get build-dep ./</screen>
<para>Fix bugs if needed.</para>
<para>Bump package version to one which does not collide with official Debian versions, e.g. one appended with "<literal>+debug1</literal>" when recompiling existing package version, or one appended with "<literal>~pre1</literal>" when compiling unreleased package version by the following.</para>
<screen>$ dch -i</screen>
<para>Compile and install packages with debug symbols by the following.</para>
<screen>$ export DEB_BUILD_OPTIONS="nostrip noopt"
$ debuild
$ cd ..
$ sudo debi package_name*.changes</screen>
<para>You need to check build scripts of the package and ensure to use "<literal>CFLAGS=-g -Wall</literal>" for compiling binaries.</para>
</section>
<section id="_obtaining_backtrace">
<title>Obtaining backtrace</title>
<para>When you encounter program crash, reporting bug report with cut-and-pasted backtrace information is a good idea.</para>
<para>The backtrace can be obtained by <literal>gdb</literal>(1) using one of the following approaches:</para>
<itemizedlist>
<listitem>
<para>Crash-in-GDB approach:</para>
<itemizedlist>
<listitem> <para>Run the program from GDB.</para> </listitem>
<listitem> <para>Crash the program.</para> </listitem>
<listitem> <para>Type "<literal>bt</literal>" at the GDB prompt.</para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Crash-first approach:</para>
<itemizedlist>
<listitem> <para>Update the “<emphasis role="strong">/etc/security/limits.conf</emphasis>” file to include the following: </para> <screen>* soft core unlimited</screen> </listitem>
<listitem> <para>Type "<literal>ulimit -c unlimited</literal>" to the shell prompt.</para> </listitem>
<listitem> <para>Run the program from this shell prompt.</para> </listitem>
<listitem> <para>Crash the program to produce a <ulink url="https://en.wikipedia.org/wiki/Core_dump">core dump</ulink> file.</para> </listitem>
<listitem> <para>Load the <ulink url="https://en.wikipedia.org/wiki/Core_dump">core dump</ulink> file to GDB as "<literal>gdb gdb ./program_binary core</literal>" .</para> </listitem>
<listitem> <para>Type "<literal>bt</literal>" at the GDB prompt.</para> </listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>For infinite loop or frozen keyboard situation, you can force to crash the program by pressing <literal>Ctrl-\</literal> or <literal>Ctrl-C</literal> or executing “<literal>kill -ABRT <emphasis>PID</emphasis></literal>”. (See <xref linkend="_killing_a_process"/>)</para>
<tip> <para>Often, you see a backtrace where one or more of the top lines are in "<literal>malloc()</literal>" or "<literal>g_malloc()</literal>". When this happens, chances are your backtrace isn't very useful. The easiest way to find some useful information is to set the environment variable "<literal>$MALLOC_CHECK_</literal>" to a value of 2 (<literal>malloc</literal>(3)). You can do this while running <literal>gdb</literal> by doing the following.</para>
<screen> $ MALLOC_CHECK_=2 gdb hello</screen>
</tip>
</section>
<section id="_advanced_gdb_commands">
<title>Advanced gdb commands</title>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of advanced gdb commands</title>
<tgroup cols="2">
<colspec colwidth="195pt" align="left"/>
<colspec colwidth="428pt" align="left"/>
<thead>
<row>
<entry> command </entry>
<entry> description for command objectives </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>(gdb) thread apply all bt</literal> </entry>
<entry> get a backtrace for all threads for multi-threaded program </entry>
</row>
<row>
<entry> <literal>(gdb) bt full</literal> </entry>
<entry> get parameters came on the stack of function calls </entry>
</row>
<row>
<entry> <literal>(gdb) thread apply all bt full</literal> </entry>
<entry> get a backtrace and parameters as the combination of the preceding options </entry>
</row>
<row>
<entry> <literal>(gdb) thread apply all bt full 10</literal> </entry>
<entry> get a backtrace and parameters for top 10 calls to cut off irrelevant output </entry>
</row>
<row>
<entry> <literal>(gdb) set logging on</literal> </entry>
<entry> write log of <literal>gdb</literal> output to a file (the default is "<literal>gdb.txt</literal>") </entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section id="_check_dependency_on_libraries">
<title>Check dependency on libraries</title>
<para>Use <literal>ldd</literal>(1) to find out a program's dependency on libraries by the followings.</para>
<screen>$ ldd /usr/bin/ls
librt.so.1 => /lib/librt.so.1 (0x4001e000)
libc.so.6 => /lib/libc.so.6 (0x40030000)
libpthread.so.0 => /lib/libpthread.so.0 (0x40153000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)</screen>
<para>For <literal>ls</literal>(1) to work in a `chroot`ed environment, the above libraries must be available in your `chroot`ed environment.</para>
<para>See <xref linkend="_tracing_program_activities"/>.</para>
</section>
<section id="_dynamic_call_tracing_tools">
<title>Dynamic call tracing tools</title>
<para>There are several dynamic call tracing tools available in Debian. See <xref linkend="_monitoring_controlling_and_starting_program_activities"/>. </para>
</section>
<section id="_debugging_x_errors">
<title>Debugging X Errors</title>
<para>If a GNOME program <literal>preview1</literal> has received an X error, you should see a message as follows.</para>
<screen>The program 'preview1' received an X Window System error.</screen>
<para>If this is the case, you can try running the program with "<literal>--sync</literal>", and break on the "<literal>gdk_x_error</literal>" function in order to obtain a backtrace.</para>
</section>
<section id="_memory_leak_detection_tools">
<title>Memory leak detection tools</title>
<para>There are several memory leak detection tools available in Debian.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of memory leak detection tools</title>
<tgroup cols="4">
<colspec colwidth="92pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="287pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>libc6-dev</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>mtrace</literal>(1): malloc debugging functionality in glibc </entry>
</row>
<row>
<entry> <literal>valgrind</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> memory debugger and profiler </entry>
</row>
<row>
<entry> <literal>electric-fence</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>malloc</literal>(3) debugger </entry>
</row>
<row>
<entry> <literal>libdmalloc5</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> debug memory allocation library </entry>
</row>
<row>
<entry> <literal>duma</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> library to detect buffer overruns and under-runs in C and C++ programs </entry>
</row>
<row>
<entry> <literal>leaktracer</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> memory-leak tracer for C++ programs </entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section id="_disassemble_binary">
<title>Disassemble binary</title>
<para>You can disassemble binary code with <literal>objdump</literal>(1) by the following.</para>
<screen>$ objdump -m i386 -b binary -D /usr/lib/grub/x86_64-pc/stage1</screen>
<note> <para><literal>gdb</literal>(1) may be used to disassemble code interactively.</para> </note>
</section>
</section>
<section id="_build_tools">
<title>Build tools</title>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of build tool packages</title>
<tgroup cols="4">
<colspec colwidth="97pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="396pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> documentation </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>make</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info make</literal>" provided by <literal>make-doc</literal> </entry>
</row>
<row> <entry> <literal>autoconf</literal>
</entry> <entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info autoconf</literal>" provided by <literal>autoconf-doc</literal> </entry>
</row>
<row>
<entry> <literal>automake</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info automake</literal>" provided by <literal>automake1.10-doc</literal> </entry>
</row>
<row>
<entry> <literal>libtool</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> "<literal>info libtool</literal>" provided by <literal>libtool-doc</literal> </entry>
</row>
<row>
<entry> <literal>cmake</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>cmake</literal>(1) cross-platform, open-source make system </entry>
</row>
<row>
<entry> <literal>ninja-build</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>ninja</literal>(1) small build system closest in spirit to Make </entry>
</row>
<row>
<entry> <literal>meson</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>meson</literal>(1) high productivity build system on top of <literal>ninja</literal> </entry>
</row>
<row>
<entry> <literal>xutils-dev</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry><literal>imake</literal>(1), <literal>xmkmf</literal>(1), etc. </entry>
</row>
</tbody>
</tgroup>
</table>
<section id="_make">
<title>Make</title>
<para><ulink url="https://en.wikipedia.org/wiki/Make_(software)">Make</ulink> is a utility to maintain groups of programs. Upon execution of <literal>make</literal>(1), <literal>make</literal> read the rule file, "<literal>Makefile</literal>", and updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the target does not exist. The execution of these updates may occur concurrently.</para>
<para>The rule file syntax is the following.</para>
<screen>target: [ prerequisites ... ]
[TAB] command1
[TAB] -command2 # ignore errors
[TAB] @command3 # suppress echoing</screen>
<para>Here "<literal>[TAB]</literal>" is a TAB code. Each line is interpreted by the shell after make variable substitution. Use "<literal>\</literal>" at the end of a line to continue the script. Use "<literal>$$</literal>" to enter "<literal>$</literal>" for environment values for a shell script.</para>
<para>Implicit rules for the target and prerequisites can be written, for example, by the following.</para>
<screen>%.o: %.c header.h</screen>
<para>Here, the target contains the character "<literal>%</literal>" (exactly one of them). The "<literal>%</literal>" can match any nonempty substring in the actual target filenames. The prerequisites likewise use "<literal>%</literal>" to show how their names relate to the actual target name.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of make automatic variables</title>
<tgroup cols="2">
<colspec colwidth="103pt" align="left"/>
<colspec colwidth="222pt" align="left"/>
<thead>
<row>
<entry> automatic variable </entry>
<entry> value </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>$@</literal> </entry>
<entry> target </entry>
</row>
<row>
<entry> <literal>$<</literal> </entry>
<entry> first prerequisite </entry>
</row>
<row>
<entry> <literal>$?</literal> </entry>
<entry> all newer prerequisites </entry>
</row>
<row>
<entry> <literal>$^</literal> </entry>
<entry> all prerequisites </entry>
</row>
<row>
<entry> <literal>$*</literal> </entry>
<entry> "<literal>%</literal>" matched stem in the target pattern </entry>
</row>
</tbody>
</tgroup>
</table>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of make variable expansions</title>
<tgroup cols="2">
<colspec colwidth="103pt" align="left"/>
<colspec colwidth="108pt" align="left"/>
<thead>
<row>
<entry> variable expansion </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>foo1 := bar</literal> </entry>
<entry> one-time expansion </entry>
</row>
<row>
<entry> <literal>foo2 = bar</literal> </entry>
<entry> recursive expansion </entry>
</row>
<row>
<entry> <literal>foo3 += bar</literal> </entry>
<entry> append </entry>
</row>
</tbody>
</tgroup>
</table>
<para>Run "<literal>make -p -f/dev/null</literal>" to see automatic internal rules.</para>
</section>
<section id="_autotools">
<title>Autotools</title>
<para><ulink url="https://en.wikipedia.org/wiki/GNU_Autotools">Autotools</ulink> is a suite of programming tools designed to assist in making source code packages portable to many <ulink url="https://en.wikipedia.org/wiki/Unix-like">Unix-like</ulink> systems.</para>
<itemizedlist>
<listitem>
<para><ulink url="https://en.wikipedia.org/wiki/Autoconf">Autoconf</ulink> is a tool to produce a shell script "<literal>configure</literal>" from "<literal>configure.ac</literal>".</para>
<itemizedlist>
<listitem><para>"<literal>configure</literal>" is used later to produce "<literal>Makefile</literal>" from "<literal>Makefile.in</literal>" template.</para></listitem>
</itemizedlist>
</listitem>
<listitem><para><ulink url="https://en.wikipedia.org/wiki/Automake">Automake</ulink> is a tool to produce "<literal>Makefile.in</literal>" from "<literal>Makefile.am</literal>".</para></listitem>
<listitem><para><ulink url="https://en.wikipedia.org/wiki/GNU_Libtool">Libtool</ulink> is a shell script to address the software portability problem when compiling shared libraries from source code.</para></listitem>
</itemizedlist>
<section id="_compile_and_install_a_program">
<title>Compile and install a program</title>
<warning>
<para>Do not overwrite system files with your compiled programs when installing them.</para>
</warning>
<para>Debian does not touch files in "<literal>/usr/local/</literal>" or "<literal>/opt</literal>". So if you compile a program from source, install it into "<literal>/usr/local/</literal>" so it does not interfere with Debian.</para>
<screen>$ cd src
$ ./configure --prefix=/usr/local
$ make # this compiles program
$ sudo make install # this installs the files in the system</screen>
</section>
<section id="_uninstall_program">
<title>Uninstall program</title>
<para>If you have the original source and if it uses <literal>autoconf</literal>(1)/<literal>automake</literal>(1) and if you can remember how you configured it, execute as follows to uninstall the program.</para>
<screen>$ ./configure <emphasis>all-of-the-options-you-gave-it</emphasis>
$ sudo make uninstall</screen>
<para>Alternatively, if you are absolutely sure that the install process puts files only under "<literal>/usr/local/</literal>" and there is nothing important there, you can erase all its contents by the following.</para>
<screen># find /usr/local -type f -print0 | xargs -0 rm -f</screen>
<para>If you are not sure where files are installed, you should consider using <literal>checkinstall</literal>(8) from the <literal>checkinstall</literal> package, which provides a clean path for the uninstall. It now supports to create a Debian package with "<literal>-D</literal>" option.</para>
</section>
</section>
<section id="_meson">
<title>Meson</title>
<para>The software build system has been evolving:</para>
<itemizedlist>
<listitem> <para> <ulink url="https://en.wikipedia.org/wiki/GNU_Autotools">Autotools</ulink> on the top of <ulink url="https://en.wikipedia.org/wiki/Make_(software)">Make</ulink> has been the de facto standard for the portable build infrastructure since 1990s. This is extremely slow.</para> </listitem>
<listitem> <para> <ulink url="https://en.wikipedia.org/wiki/Cmake">CMake</ulink> initially released in 2000 improved speed significantly but was originally built on the top of inherently slow <ulink url="https://en.wikipedia.org/wiki/Make_(software)">Make</ulink>. (Now <ulink url="https://en.wikipedia.org/wiki/Ninja_(build_system)">Ninja</ulink> can be its backend.)</para> </listitem>
<listitem> <para> <ulink url="https://en.wikipedia.org/wiki/Ninja_(build_system)">Ninja</ulink> initially released in 2012 is meant to replace Make for the further improved build speed and is designed to have its input files generated by a higher-level build system.</para> </listitem>
<listitem> <para> <ulink url="https://en.wikipedia.org/wiki/Meson_(software)">Meson</ulink> initially released in 2013 is the new popular and fast higher-level build system which uses <ulink url="https://en.wikipedia.org/wiki/Ninja_(build_system)">Ninja</ulink> as its backend.</para> </listitem>
</itemizedlist>
<para>See documents found at "<ulink url="https://mesonbuild.com/">The Meson Build system</ulink>" and "<ulink url="https://ninja-build.org/manual.html">The Ninja build system</ulink>".</para>
</section>
</section>
<section id="_web">
<title>Web</title>
<para>Basic interactive dynamic web pages can be made as follows.</para>
<itemizedlist>
<listitem> <para> Queries are presented to the browser user using <ulink url="https://en.wikipedia.org/wiki/HTML">HTML</ulink> forms. </para> </listitem>
<listitem>
<para> Filling and clicking on the form entries sends one of the following <ulink url="https://en.wikipedia.org/wiki/Uniform_Resource_Locator">URL</ulink> string with encoded parameters from the browser to the web server. </para>
<itemizedlist>
<listitem> <para> "<literal>https://www.foo.dom/cgi-bin/program.pl?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3</literal>" </para> </listitem>
<listitem> <para> "<literal>https://www.foo.dom/cgi-bin/program.py?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3</literal>" </para> </listitem>
<listitem> <para> "<literal>https://www.foo.dom/program.php?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3</literal>" </para> </listitem>
</itemizedlist>
</listitem>
<listitem>
<para> "<literal>%nn</literal>" in URL is replaced with a character with hexadecimal <literal>nn</literal> value. </para>
</listitem>
<listitem> <para> The environment variable is set as: "<literal>QUERY_STRING="VAR1=VAL1 VAR2=VAL2 VAR3=VAL3"</literal>". </para> </listitem>
<listitem> <para><ulink url="https://en.wikipedia.org/wiki/Common_Gateway_Interface">CGI</ulink> program (any one of "<literal>program.*</literal>") on the web server executes itself with the environment variable "<literal>$QUERY_STRING</literal>". </para> </listitem>
<listitem> <para><literal>stdout</literal> of CGI program is sent to the web browser and is presented as an interactive dynamic web page. </para> </listitem>
</itemizedlist>
<para>For security reasons it is better not to hand craft new hacks for parsing CGI parameters. There are established modules for them in Perl and Python. <ulink url="https://en.wikipedia.org/wiki/PHP">PHP</ulink> comes with these functionalities. When client data storage is needed, <ulink url="https://en.wikipedia.org/wiki/HTTP_cookie">HTTP cookies</ulink> are used. When client side data processing is needed, <ulink url="https://en.wikipedia.org/wiki/JavaScript">Javascript</ulink> is frequently used.</para>
<para>For more, see the <ulink url="https://en.wikipedia.org/wiki/Common_Gateway_Interface">Common Gateway Interface</ulink>, <ulink url="https://en.wikipedia.org/wiki/Apache_Software_Foundation">The Apache Software Foundation</ulink>, and <ulink url="https://en.wikipedia.org/wiki/JavaScript">JavaScript</ulink>.</para>
<para>Searching "CGI tutorial" on Google by typing encoded URL <ulink url="https://www.google.com/search?hl=en&ie=UTF-8&q=CGI+tutorial">https://www.google.com/search?hl=en&ie=UTF-8&q=CGI+tutorial</ulink> directly to the browser address is a good way to see the CGI script in action on the Google server.</para>
</section>
<section id="_the_source_code_translation">
<title>The source code translation</title>
<para>There are programs to convert source codes.</para>
<table pgwide="0" frame="topbot" rowsep="1" colsep="1">
<title>List of source code translation tools</title>
<tgroup cols="5">
<colspec colwidth="65pt" align="left"/>
<colspec colwidth="76pt" align="left"/>
<colspec colwidth="70pt" align="left"/>
<colspec colwidth="59pt" align="left"/>
<colspec colwidth="363pt" align="left"/>
<thead>
<row>
<entry> package </entry>
<entry> popcon </entry>
<entry> size </entry>
<entry> keyword </entry>
<entry> description </entry>
</row>
</thead>
<tbody>
<row>
<entry> <literal>perl</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> AWK→PERL </entry>
<entry> convert source codes from AWK to PERL: <literal>a2p</literal>(1) </entry>
</row>
<row>
<entry> <literal>f2c</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> FORTRAN→C </entry>
<entry> convert source codes from FORTRAN 77 to C/C++: <literal>f2c</literal>(1) </entry>
</row>
<row>
<entry> <literal>intel2gas</literal> </entry>
<entry> @-@popcon1@-@ </entry>
<entry> @-@psize1@-@ </entry>
<entry> intel→gas </entry>
<entry> converter from NASM (Intel format) to the GNU Assembler (GAS) </entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section id="_making_debian_package">
<title>Making Debian package</title>
<para>If you want to make a Debian package, read followings.</para>
<itemizedlist>
<listitem> <para><xref linkend="_debian_package_management"/> to understand the basic package system </para> </listitem>
<listitem> <para><xref linkend="_porting_a_package_to_the_stable_system"/> to understand basic porting process </para> </listitem>
<listitem> <para><xref linkend="_chroot_system"/> to understand basic chroot techniques </para> </listitem>
<listitem> <para><literal>debuild</literal>(1), and <literal>sbuild</literal>(1) </para> </listitem>
<listitem> <para><xref linkend="_debugging_the_debian_package"/> for recompiling for debugging </para> </listitem>
<listitem> <para><ulink url="https://www.debian.org/doc/manuals/debmake-doc/">Guide for Debian Maintainers</ulink> (the <literal>debmake-doc</literal> package) </para> </listitem>
<listitem> <para><ulink url="https://www.debian.org/doc/manuals/developers-reference/">Debian Developer's Reference</ulink> (the <literal>developers-reference</literal> package) </para> </listitem>
<listitem> <para><ulink url="https://www.debian.org/doc/debian-policy/">Debian Policy Manual</ulink> (the <literal>debian-policy</literal> package) </para> </listitem>
</itemizedlist>
<para>There are packages such as <literal>debmake</literal>, <literal>dh-make</literal>, <literal>dh-make-perl</literal>, etc., which help packaging.</para>
</section>
</chapter>
|