find_cat.bac 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. REM Copyright (c) Barry Kauler January 2013. bkhome.org
  2. REM rewrote find_cat.c in BaCon. Reads categories.dat.
  3. REM passed params: <packagegenericname> "<description>"
  4. REM ...return on stdout, found category.
  5. REM or, no passed param(s), stdin is a pup-db pkg entry, empty DB_category field.
  6. REM ...return on stdout, db-entry, with category inserted in 5th field.
  7. REM ...can stdin a complete file, ex: cat Packages-ubuntu-precise-main | find_cat
  8. REM or, one passed param, name of db-entries file. ex: find_cat Packages-ubuntu-precise-main
  9. REM 130126 first release.
  10. REM bacon 1.0.29 retains quote chars in ARGUMENT$ (refer: http://basic-converter.proboards.com/index.cgi?board=bugs&action=display&thread=422)
  11. GLOBAL categories$ ASSOC STRING
  12. GLOBAL keywords$ ASSOC STRING
  13. REM read /usr/local/petget/categories.dat and evaluate vars...
  14. OPEN "/usr/local/petget/categories.dat" FOR READING AS handle1
  15. WHILE NOT(ENDFILE(handle1)) DO
  16. READLN line$ FROM handle1
  17. chopped$=CHOP$(line$)
  18. IF EQUAL(chopped$,"") THEN CONTINUE
  19. first$=LEFT$(chopped$,1)
  20. IF EQUAL(first$,"#") THEN CONTINUE
  21. IF EQUAL(first$,"K") THEN
  22. REM ex: chopped$=KEYWDS_Graphic_viewer=" image_viewer thumbnail thumbnails "
  23. SPLIT chopped$ BY "=" TO array2$ SIZE dimension2
  24. val2$=CHOP$(array2$[1],"'\"")
  25. keywords$(array2$[0])=val2$
  26. ELSE
  27. REM ex: chopped$=PKGCAT_Help_Sub=" linux-faqs linux-howtos man-pages "
  28. SPLIT chopped$ BY "=" TO array1$ SIZE dimension1
  29. val$=CHOP$(array1$[1],"'\"")
  30. categories$(array1$[0])=val$
  31. END IF
  32. WEND
  33. CLOSE FILE handle1
  34. description0$=" "
  35. REM bacon 1.0.29 retains quote chars in ARGUMENT$, remove them...
  36. fixedarg$=REPLACE$(ARGUMENT$, CHR$(34), "")
  37. SPLIT fixedarg$ BY " " TO commandline$ SIZE numparams
  38. IF numparams==2 THEN
  39. REM filename on commandline, with pup-db entries.
  40. IF NOT(FILEEXISTS(commandline$[1])) THEN END 2
  41. OPEN commandline$[1] FOR READING AS handle2
  42. END IF
  43. LABEL naughtyjump
  44. REM read a db-entry either from stdin or file...
  45. REM ex: bbe_0.2.2-1|bbe|0.2.2-1||BuildingBlock|136K|pool/universe/b/bbe|bbe_0.2.2-1_i386.deb|+libc6|sed-like editor for binary files|ubuntu|precise||
  46. IF numparams==1 THEN INPUT onedb$
  47. ELIF numparams==2 THEN
  48. IF ENDFILE(handle2) THEN
  49. CLOSE FILE handle2
  50. END 0
  51. END IF
  52. READLN onedb$ FROM handle2
  53. END IF
  54. IF numparams<3 THEN
  55. REM want to extract genericname and description...
  56. flagdeb=0
  57. SPLIT onedb$ BY "|" TO dbarray$ SIZE dim9
  58. IF dim9<11 THEN END 1
  59. IF dbarray$[6]=="" THEN
  60. packagename$=LCASE$(dbarray$[1])
  61. ELSE
  62. REM debian/ubuntu/raspbian, get genericname from 7th field...
  63. IF dbarray$[10]=="ubuntu" THEN flagdeb=1
  64. IF dbarray$[10]=="debian" THEN flagdeb=1
  65. IF dbarray$[10]=="raspbian" THEN flagdeb=1
  66. IF flagdeb==1 THEN
  67. pos1=INSTRREV(dbarray$[6],"/")
  68. IF pos1==0 THEN
  69. packagename$=LCASE$(dbarray$[6])
  70. ELSE
  71. len1=LEN(dbarray$[6])
  72. packagename0$=RIGHT$(dbarray$[6],len1-pos1)
  73. packagename$=LCASE$(packagename0$)
  74. END IF
  75. ELSE
  76. packagename$=LCASE$(dbarray$[1])
  77. END IF
  78. END IF
  79. description0$=CONCAT$(" ",dbarray$[9]," ")
  80. description$=LCASE$(description0$)
  81. ELSE
  82. packagename$=LCASE$(commandline$[1])
  83. FOR zz=2 TO numparams-1
  84. description0$=CONCAT$(description0$,commandline$[zz]," ")
  85. NEXT
  86. description$=LCASE$(description0$)
  87. END IF
  88. REM search through the categories in categories.dat...
  89. REM 'packagename$' is the pkgname trying to find a match to...
  90. DB_category$=""
  91. name$=CONCAT$(" ",packagename$," ")
  92. LOOKUP categories$ TO acategory$ SIZE d
  93. FOR x=0 TO d-1
  94. REM ex: PKGCAT_Desktop_appearance
  95. IF INSTR(categories$(acategory$[x]),name$)<>0 THEN
  96. SPLIT acategory$[x] BY "_" TO catsplit$ SIZE dim
  97. IF dim < 3 THEN CONTINUE
  98. IF catsplit$[2]=="Sub" THEN
  99. DB_category$=catsplit$[1]
  100. REM no sub-category. ex: DB_category$=Desktop
  101. ELSE
  102. DB_category$=CONCAT$(catsplit$[1],";",catsplit$[2])
  103. REM ex: DB_category$=Desktop;appearance
  104. END IF
  105. BREAK
  106. END IF
  107. NEXT
  108. REM check if a library...
  109. IF DB_category$=="" THEN
  110. IF (INSTR(name$," lib") NE 0) THEN DB_category$="BuildingBlock"
  111. END IF
  112. REM now look for keywords in description...
  113. IF DB_category$=="" THEN
  114. LOOKUP keywords$ TO akeycat$ SIZE dim1
  115. FOR y=0 TO dim1-1
  116. REM ex: KEYWDS_Desktop_appearance
  117. SPLIT keywords$(akeycat$[y]) BY " " TO keynames$ SIZE dim3
  118. IF dim3==0 THEN CONTINUE
  119. FOR z=0 TO dim3-1
  120. REM some keywords are actually multiple words, with '_' delimiter...
  121. akeyname$=REPLACE$(keynames$[z],"_"," ")
  122. keynameptn$=CONCAT$(" ",akeyname$," ")
  123. IF INSTR(description$,keynameptn$)<>0 THEN
  124. SPLIT akeycat$[y] BY "_" TO keysplit$ SIZE dim2
  125. IF dim2 < 3 THEN CONTINUE
  126. IF keysplit$[2]=="Sub" THEN
  127. DB_category$=keysplit$[1]
  128. REM no sub-category. ex: DB_category$=Desktop
  129. ELSE
  130. DB_category$=CONCAT$(keysplit$[1],";",keysplit$[2])
  131. REM ex: DB_category$=Desktop;appearance
  132. END IF
  133. BREAK
  134. END IF
  135. NEXT
  136. NEXT
  137. END IF
  138. REM fallback for ubuntu/debian/raspbian...
  139. IF DB_category$=="" THEN
  140. IF numparams<3 THEN
  141. IF flagdeb==1 THEN
  142. REM db-entry on stdin may have debian "Section" value in DB_category field...
  143. IF dbarray$[4]<>"" THEN
  144. IF (REGEX(dbarray$[4],"vcs$") NE 0) THEN DB_category$="Utility;development"
  145. ELIF (REGEX(dbarray$[4],"admin$") NE 0) THEN DB_category$="Setup"
  146. ELIF (REGEX(dbarray$[4],"doc$") NE 0) THEN DB_category$="Help"
  147. ELIF (REGEX(dbarray$[4],"games$") NE 0) THEN DB_category$="Fun"
  148. ELIF (REGEX(dbarray$[4],"science$") NE 0) THEN DB_category$="Personal;education"
  149. END IF
  150. END IF
  151. END IF
  152. END IF
  153. IF DB_category$=="" THEN DB_category$="BuildingBlock"
  154. IF numparams<3 THEN
  155. REM db-entry came in via stdin or file, send it out via stdout...
  156. REM substitute with the found DB_category, 5th field...
  157. dbarray$[4]=DB_category$
  158. FOR yy=0 TO dim9-2
  159. PRINT dbarray$[yy],"|";
  160. NEXT
  161. PRINT
  162. GOTO naughtyjump
  163. ELSE
  164. PRINT DB_category$
  165. END IF