interactive.awk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #
  2. # Copyright (C) 2005, 2006 Stephen Jungels
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # See COPYING for the full text of the license.
  15. BEGIN {
  16. all = "";
  17. search = "";
  18. levelsep = "/";
  19. skipindex = 0;
  20. playindex = 0;
  21. }
  22. state==0 {
  23. printf \
  24. ("y: yes\nn: no\nY: yes to all\nN: no to all\n") > "/dev/tty";
  25. printf \
  26. ("u: up a level\nd: down a level\n/: search\n") > "/dev/tty";
  27. state = 1;
  28. }
  29. state==1 {
  30. brief = $0;
  31. sub(prefix, "", brief);
  32. action = "";
  33. response = "";
  34. len = 0;
  35. # take the advice of the longest (most specific) hint
  36. for (i=1; i<=skipindex; i++)
  37. {
  38. if (index(tolower(brief), tolower(skiphints[i])) > 0 && length(skiphints[i]) > len)
  39. {
  40. action = "n";
  41. len = length(skiphints[i]);
  42. }
  43. }
  44. for (i=1; i<=playindex; i++)
  45. {
  46. if (index(tolower(brief), tolower(playhints[i])) > 0 && length(playhints[i]) > len)
  47. {
  48. action = "y";
  49. break;
  50. }
  51. }
  52. if (action=="" && all!="")
  53. {
  54. action = all;
  55. }
  56. if (action=="" && search!="")
  57. {
  58. if (tolower(brief) ~ tolower(search))
  59. {
  60. search = "";
  61. }
  62. else
  63. {
  64. action = "n";
  65. }
  66. }
  67. if (action=="y")
  68. {
  69. printf ("%s\n", $0);
  70. }
  71. # if hints and "all" gave no instructions, ask the user
  72. else if (action=="")
  73. {
  74. levels = split(brief, briefparts, levelsep);
  75. if (levels + levelindex < 1)
  76. {
  77. levelindex = 1 - levels;
  78. }
  79. for (i=1; i<=levels; i++)
  80. {
  81. # escape regex special chars that might appear in a filename
  82. briefparts2[i] = briefparts[i];
  83. # gsub("\\(", "\\(", briefparts2[i]);
  84. # gsub("\\)", "\\)", briefparts2[i]);
  85. # gsub("\\+", "\\+", briefparts2[i]);
  86. # gsub("\\[", "\\[", briefparts2[i]);
  87. # gsub("\\]", "\\]", briefparts2[i]);
  88. # gsub("\\|", "\\|", briefparts2[i]);
  89. # gsub("\\*", "\\*", briefparts2[i]);
  90. }
  91. cont = 1;
  92. while (cont==1)
  93. {
  94. cont = 0;
  95. str = "";
  96. str2 = "";
  97. sep = "";
  98. sep2 = "";
  99. for (i=1; i<=levels+levelindex; i++)
  100. {
  101. str = str sep briefparts2[i];
  102. str2 = str2 sep2 briefparts[i];
  103. sep = levelsep;
  104. sep2 = "/";
  105. }
  106. printf("Play %s (ynYNud/)? ", str2) > "/dev/tty";
  107. getline response < "/dev/tty";
  108. if (response ~ /^u(p)?/)
  109. {
  110. cont = 1;
  111. levelindex -= 1;
  112. if (levels + levelindex < 1)
  113. {
  114. levelindex = 1 - levels;
  115. }
  116. }
  117. if (response ~ /^d(own)?/)
  118. {
  119. cont = 1;
  120. levelindex += 1;
  121. if (levelindex > 0)
  122. {
  123. levelindex = 0;
  124. }
  125. }
  126. }
  127. if (response ~ /^Y(es)?/)
  128. {
  129. response = "y";
  130. all = "y";
  131. }
  132. else if (response ~ /^N(o)?/)
  133. {
  134. response = "n";
  135. all = "n";
  136. }
  137. else if (response ~ /^\//)
  138. {
  139. search = substr(response, 2);
  140. if (search=="")
  141. {
  142. search = savesearch;
  143. }
  144. if (search=="")
  145. {
  146. printf("Search string: ") > "/dev/tty";
  147. getline search < "/dev/tty";
  148. }
  149. savesearch = search;
  150. response = "n";
  151. }
  152. if (response ~ /y(es)?/ || response=="")
  153. {
  154. printf("%s\n", $0);
  155. # if we're above track level, this may match more than one track, so save it
  156. if (levelindex < 0)
  157. {
  158. playhints[++playindex] = str;
  159. }
  160. }
  161. else if (response ~ /n(o)?/)
  162. {
  163. if (levelindex < 0)
  164. {
  165. skiphints[++skipindex] = str;
  166. }
  167. }
  168. }
  169. }