alex24.py 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. #### license: creative commons cc0 1.0 (public domain)
  4. #### http://creativecommons.org/publicdomain/zero/1.0/
  5. proginf = "alex 2.4, nov 2018 mn"
  6. try: import readline
  7. except: p = 0
  8. import sys
  9. import os
  10. from datetime import datetime
  11. from hashlib import sha256
  12. from sys import stdin, stdout
  13. from os import popen
  14. from os import name as alexosname
  15. if alexosname == "nt":
  16. try: from colorama import init ; init()
  17. except: pass
  18. try: from sys import exit as quit
  19. except: pass
  20. cmdhelp = [("timer", "input (shared-line) change main variable to number of seconds past midnight")
  21. ,("arrstdin", "input (shared-line) change main variable to array containing lines of stdin")
  22. ,("flineinput filepath", "input (shared-line) change main var to string of line from open file filepath")
  23. ,("arropen filepath", "input (shared-line) change main variable to array of file lines in filepath")
  24. ,("arrcurl url", "input (shared-line) like arropen, except downloading url into the array")
  25. ,("sleep seconds", "input (shared-line) wait for number of seconds before continuing with program")
  26. ,("command", "input (shared-line) change main variable to array of command line parameters")
  27. ,("print", "output (shared-line) output main variable to the screen (aka stdout)")
  28. ,("prints", "output (shared-line) put main var to screen; like print but (s)tays on line.")
  29. ,("fprint filepath", "output (shared-line) write main variable to open file designated by filepath")
  30. ,("while", "loop --\\own\\line mark the start of a loop (will keep going without break)")
  31. ,("break", "loop --\\own\\line put in the middle of a loop to exit (stop looping)")
  32. ,("for var strt stop step", "loop --\\own\\line start a for loop, changing var from strt to stop, by step")
  33. ,("forin var array", "loop --\\own\\line loop through each item in array; for each, set var to item")
  34. ,("iftrue ckvar", "conditional --\\own\\line run lines between iftrue and fig if ckvar is \"non-zero\"")
  35. ,("ifequal var1 var2", "conditional --\\own\\line run lines between ifequal and fig if var1 equals var2")
  36. ,("ifmore var1 var2", "conditional --\\own\\line run lines between ifmore and fig if var1 is > var2")
  37. ,("ifless var1 var2", "conditional --\\own\\line run lines between ifless and fig if var1 is < var2")
  38. ,("try", "conditional --\\own\\line put code that might not work between try and except")
  39. ,("except", "conditional --\\own\\line if code between try/except fails, run the code after except")
  40. ,("resume", "conditional --\\own\\line mark the end of try / except / resume command block")
  41. ,("else", "conditional --\\own\\line after if- line, before fig. run lines if condition isnt true")
  42. ,("function name p1 p2 …", "function --\\own\\line define function named name with optional params p1,p2, etc")
  43. ,("get parametername", "function (shared-line) (no longer required) copy parametername value to main var")
  44. ,("python", "function --\\own\\line put inline python code between lines python and fig")
  45. ,("fig/next/nextin/wend", "fig (interchangeable) function --\\own\\line finalise a block (started by if/while/function/for/forin")
  46. ,("pass", "function --\\own\\line blocks (for/next, etc) require something inside lines; pass works / does nothing")
  47. ,("lcase", "function (shared-line) change main variable to all-lower-case copy of own value")
  48. ,("ucase", "function (shared-line) change main variable to all-upper-case copy of own value")
  49. ,("str", "function (shared-line) convert main variable from number to string")
  50. ,("asc", "function (shared-line) change main variable from string to ascii code of 1st char")
  51. ,("val", "function (shared-line) change main variable from string to numeric (int if whole)")
  52. ,("len", "function (shared-line) change main variable to numeric length of main var")
  53. ,("not", "function (shared-line) change main variable to zero if non-zero; or -1 if zero")
  54. ,("ltrim", "function (shared-line) strip whitespace from left side of main variable")
  55. ,("rtrim", "function (shared-line) strip whitespace from right side of main variable")
  56. ,("chr", "function (shared-line) change main variable from numeric to ascii/uni string")
  57. ,("arrshell", "function (shared-line) change main var to array of shell output (from main var)")
  58. ,("arreverse", "function (shared-line) change main variable from array to reverse order of array")
  59. ,("reverse", "function (shared-line) like arreverse (which might be faster for array) for strings")
  60. ,("arrsort", "function (shared-line) change main variable from array to sorted array")
  61. ,("#", "comment (can\\share) place at beginning (or end) of line, prior to a comment")
  62. ,("():;|=,. ( ) : ; | = , .", "optional (shared-line) use in a shared line (and some others) for aesthetics/notation")
  63. ,("left numofcharsoritems", "function (shared-line) change main variable to __ leftmost group of chars/items")
  64. ,("right numofchrsoritems", "function (shared-line) change main variable to __ rightmost group of chars/items")
  65. ,("arrget array position", "function (shared-line) change main variable to position-th item from array")
  66. ,("arrset position setto", "function (shared-line) change item in array in main variable to value of setto")
  67. ,("mid position len", "function (shared-line) change main variable to range of len items from position")
  68. ,("string len asciiorstr", "function (shared-line) change main variable to len instances of asciiorstr")
  69. ,("split string splitby", "function (shared-line) split string by separator splitby into array, to main var")
  70. ,("join array usestring", "function (shared-line) change main var to string by joining array using usestring")
  71. ,("instr lookin lookfor", "function (shared-line) change main var to numeric position of lookfor in lookin")
  72. ,("chdir", "function (shared-line) change current folder to path string from main variable")
  73. ,("system", "function (shared-line) put on (usually at the end of) a line to stop the program")
  74. ,("close", "function (shared-line) close the open file designated by main variable")
  75. ,("end", "function (shared-line) interchangeable with system which ends the program")
  76. ,("open mode", "function (shared-line) open file at filepath main variable in mode \"r\" or \"w\"")
  77. ,("return var", "function (shared-line) (optional) exit current function, returning value var")
  78. ,("swap var1 var2", "function (shared-line) change contents of var1 to contents of var2 and vice-versa")
  79. ,("plus numstrarr", "math (shared-line) change main variable to itself plus num or string or arr")
  80. ,("minus numeric", "math (shared-line) change main variable to itself minus numeric")
  81. ,("divby numeric", "math (shared-line) change main variable to itself divided by numeric")
  82. ,("times numeric", "math (shared-line) change main variable to itself times numeric")
  83. ,("oct", "math (shared-line) change main variable from numeric decimal to octal")
  84. ,("atn", "math (shared-line) change numeric main variable to its arctangent")
  85. ,("int", "math (shared-line) change main variable from decimal (aka \"float\") to integer")
  86. ,("sgn", "math (shared-line) change main variable to 0 if 0, to -1 if < 0, or 1 if > 0.")
  87. ,("sqr", "math (shared-line) change main variable to square root of itself")
  88. ,("mod denominator", "math (shared-line) change main variable to: main var modulus denominator")
  89. ,("topwr n", "math (shared-line) raise numeric main variable to n-th power")
  90. ,("randint smallst largst", "input (shared-line) change main var to random number from smallst to largst")
  91. ,("arr", "function (shared-line) change main var to array (starting with same contents)") ]
  92. def chelp(f):
  93. ck = 0 ; print ""
  94. for p in cmdhelp:
  95. rcmd = p[0]
  96. if f in rcmd.split()[0]:
  97. ck = 1
  98. rd = p[1].split()
  99. rcat = rd[0] ; rd.remove(rd[0])
  100. rt = rd[0] ; rd.remove(rd[0])
  101. cde = rcmd.split(" ")
  102. print ""
  103. stdout.write(" " + colour(14,0)+ cde[0])
  104. cda = cde.remove(cde[0])
  105. for c in cde:
  106. stdout.write(" " + colour(0, 7)+ " " + c + " " + colour(7,0)+" ") ; stdout.flush()
  107. print ""
  108. print ""
  109. print colour(3,0) + " category:", rcat, rt.replace("\\", " ")
  110. print ""
  111. print " " + colour(7,0) + " ".join(rd)
  112. print ""
  113. colour(7,0);
  114. return ck
  115. def outfilewrite(outb, p):
  116. outb += [p]
  117. #global vrck
  118. #vrck += p.strip()
  119. #if inle: print colour(5, 0) + p.rstrip() ; p=raw_input() ; quit()
  120. def colour(f, b):
  121. if f == None: f = 0
  122. if b == None: b = 0
  123. n = "0"
  124. if f > 7: n = "1" ; f = f - 8
  125. if f == 1: f = 4 ## switch ansi colours for qb colours
  126. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  127. if f == 3: f = 6
  128. elif f == 6: f = 3
  129. if b > 7: b = b - 8
  130. if b == 1: b = 4
  131. elif b == 4: b = 1
  132. if b == 3: b = 6
  133. elif b == 6: b = 3
  134. #stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  135. return 0 #return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  136. def bcolour(b):
  137. f = None
  138. if f == None: f = 0
  139. if b == None: b = 0
  140. n = "0"
  141. if f > 7: n = "1" ; f = f - 8
  142. if f == 1: f = 4 ## switch ansi colours for qb colours
  143. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  144. if f == 3: f = 6
  145. elif f == 6: f = 3
  146. if b > 7: b = b - 8
  147. if b == 1: b = 4
  148. elif b == 4: b = 1
  149. if b == 3: b = 6
  150. elif b == 6: b = 3
  151. #stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  152. return 0 # "\x1b[" + n + str(40+b) + "m"
  153. def sgn(p):
  154. p = float(p)
  155. if p > 0: return 1
  156. if p < 0: return -1
  157. return 0
  158. def left(p, s):
  159. return p[:s]
  160. def right(p, s):
  161. return p[-s:]
  162. def leftfour(p):
  163. try:
  164. if left(p, 4) == chr(32) * 4: p = right(p, len(p) - 4)
  165. except:
  166. pass
  167. return p
  168. def alexatleast(s, p):
  169. if p < s: return s
  170. else: return p
  171. def alexfsp(p):
  172. pp = "" ; flg = 0
  173. fsp = alexfsplit(p)
  174. for fp in enumerate(fsp):
  175. if flg == 0 and fp[1] in cmds.keys():
  176. pp += colour(8,0) + "_" + colour(7,0) + " " ; flg = cmds[fp[1]]
  177. if flg < 0: flg = flg * -1
  178. else: flg = flg + 1
  179. pp += fp[1] + " "
  180. if flg > 0:
  181. flg -= 1
  182. if flg == 0 and fp[0] + 1 < len(fsp):
  183. pp += colour(8,0) + "_" + colour(7,0) + " "
  184. return pp.rstrip().replace(colour(8,0) + "_" + colour(7,0) + " " + colour(8,0) +
  185. "_" + colour(7,0), colour(8,0) + "__" + colour(7,0)).replace(colour(8,0) + "_" +
  186. colour(7,0),colour(8,0) + "__" + colour(7,0))
  187. def alexfsplit(p):
  188. # return p.split() # that was fine when strings weren't tokens
  189. # we have to make this 3 tokens: variable "hello, world!" #comment not string
  190. px = []
  191. pxc = -1 # could use len(px) -1 instead?
  192. inquotes = 0
  193. remarked = 0
  194. inspc = "" ; vnspc = ""
  195. #print "->", p
  196. for l in p:
  197. if inquotes == 0 and remarked == 0 and l == "#":
  198. remarked = 1
  199. pxc += 1 ; px += [""]
  200. if remarked == 1:
  201. px[pxc] += l
  202. if remarked == 0:
  203. if l == "\"":
  204. if inquotes == 0:
  205. inquotes = 1 ; pxc += 1 ; px += [""]
  206. else: inquotes = 0 #; px[pxc] += l
  207. if inquotes == 1: px[pxc] += l
  208. if remarked == 0 and inquotes == 0:
  209. if vnspc not in "1234567890-" + chr(32) and l[0] == ".": l = " "
  210. vnspc = l
  211. if l[0] in "():;|=,": l = " "
  212. if inspc != " " and l == " ": pxc += 1 ; px += [""]
  213. if l != " ":
  214. if pxc == -1: pxc += 1 ; px += [""]
  215. px[pxc] += l.lower()
  216. inspc = l
  217. #print "->", px[:]
  218. while ('') in px: px.remove('')
  219. while (':') in px: px.remove(':')
  220. for p in range(len(px)):
  221. if px[p][0] != "#":
  222. if right(px[p], 1) == ":":
  223. lenpx = len(px[p]) - 1
  224. if lenpx > 0:
  225. px[p] = left(px[p], lenpx)
  226. return px[:]
  227. def nob(p, s):
  228. r = ""
  229. if s == len(p) - 1:
  230. if len(p):
  231. if p[s].rstrip() != ".": r = p[s].rstrip()
  232. if len(r):
  233. if r[-1:] == ".": r = left(r, len(r) - 1)
  234. pfig = ""
  235. try: pfig = left(p[s], 3)
  236. except: pfig = ""
  237. if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
  238. try:
  239. if r != "": return r
  240. else: return p[s]
  241. except: return ""
  242. def snobl(p):
  243. if "\"" in p: return p
  244. else: return p.lower()
  245. def snob(p, s):
  246. r = ""
  247. if s == len(p) - 1:
  248. if len(p):
  249. if p[s].rstrip() != ".": r = p[s].rstrip()
  250. if len(r):
  251. if r[-1:] == ".": r = left(r, len(r) - 1)
  252. pqt = ""
  253. try: pqt = left(p[s], 3)
  254. except: pqt = ""
  255. if pqt.lower() == "fig" and p[s].lower() != "fig": return "figg"
  256. try:
  257. if r != "": return snobl(r)
  258. else: return snobl(p[s])
  259. except: return ""
  260. def lnob(p, s):
  261. r = ""
  262. if s == len(p) - 1:
  263. if len(p):
  264. if p[s].rstrip() != ".": r = p[s].rstrip()
  265. if len(r):
  266. if r[-1:] == ".": r = left(r, len(r) - 1)
  267. pfig = ""
  268. try: pfig = left(p[s], 3)
  269. except: pfig = ""
  270. if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
  271. try:
  272. if r != "": return r.lower()
  273. else: return p[s].lower()
  274. except: return ""
  275. def stripcoords(p):
  276. ps = ""
  277. for s in str(p):
  278. if s in "1234567890.": ps += s
  279. return ps
  280. def getmore(p, s):
  281. try:
  282. for t in range(1, s + 1):
  283. if len(p) == 1: p = []
  284. p = right(p, len(p) - 1)
  285. while "" in p: p.remove("")
  286. for prx in range(len(p)):
  287. if p[prx][0] == "#":
  288. p.remove(p[prx])
  289. return p
  290. except: return []
  291. def getlmore(p, s):
  292. try:
  293. for t in range(1, s + 1):
  294. if len(p) == 1: p = []
  295. p = right(p, len(p) - 1)
  296. while "" in p: p.remove("")
  297. return p
  298. except: return []
  299. def wr(p):
  300. buf = [p + "\n"]
  301. addtoout = [0]
  302. addto = [0]
  303. addtoout[0] = ""
  304. import sys, os
  305. from sys import stdin, stdout
  306. from sys import argv as alexargv
  307. #try: from colorama import init ; init()
  308. #except: pass # (only) windows users want colorama installed or ansi.sys enabled
  309. try: from sys import exit as quit
  310. except: pass
  311. from random import randint
  312. from time import sleep
  313. from os import chdir as alexoch
  314. from os import popen as alexpo
  315. from os import system as alexsh
  316. alexsysteme = 0
  317. alexfilehandles = {}
  318. alexfilecounters = {}
  319. def alexnonz(p, n=None):
  320. if n==None:
  321. if p == 0: return 1
  322. else:
  323. if p == 0: return n
  324. return p
  325. addtoout += [0] ; addto += [0]
  326. addtoout[1] = """from sys import stdout
  327. def alexlocate(x, l = "ignore", c = "ignore"):
  328. import sys
  329. if l == "ignore" and c == "ignore": pass
  330. # do nothing. want it to return an error?
  331. elif l < 1 and c != "ignore":
  332. sys.stdout.write("\x1b[" + str(c) + "G") # not ansi.sys compatible
  333. elif l != "ignore" and c == "ignore":
  334. sys.stdout.write("\x1b[" + str(l) + ";" + str(1) + "H")
  335. else: sys.stdout.write("\x1b[" + str(l) + ";" + str(c) + "H")
  336. stdout.flush()
  337. import time
  338. figbac = None
  339. figprsbac = None
  340. sub = None
  341. def alexnone(p, alexbac):
  342. if p == None: return alexbac
  343. return p
  344. return -1
  345. def stopgraphics():
  346. global yourscreen
  347. global alexraphics
  348. alexraphics = 0
  349. try: pygame.quit()
  350. except: pass\n
  351. \n"""
  352. addtoout += [0] ; addto += [0]
  353. addtoout[2] = ""
  354. figraphics = -1
  355. figrupd = 1
  356. try: import pygame
  357. except: alexraphics = 0
  358. yourscreen = ""
  359. try: pygame.init()
  360. except: alexraphics = 0 # unable to init pygame, just use text
  361. def alexlocate(x, l = "ignore", c = "ignore"):
  362. import sys
  363. if l == "ignore" and c == "ignore": pass
  364. # do nothing. want it to return an error?
  365. elif l < 1 and c != "ignore":
  366. sys.stdout.write("\x1b[" + str(c) + "G") # not ansi.sys compatible
  367. elif l != "ignore" and c == "ignore":
  368. sys.stdout.write("\x1b[" + str(l) + ";" + str(1) + "H")
  369. else: sys.stdout.write("\x1b[" + str(l) + ";" + str(c) + "H")
  370. def alexpset(x, y, c):
  371. global alexcgapal
  372. if "alexraphics" != "":
  373. if x > -1 and y > -1:
  374. alexcolourtext(c)
  375. alexlocate(0, int(y) + 1, int(x) + 1) ; stdout.write(unichr(9608))
  376. sys.stdout.flush()
  377. def alexline(x, y, x2, y2, c):
  378. x = int(x)
  379. y = int(y)
  380. x2 = int(x2)
  381. y2 = int(y2)
  382. c = int(c)
  383. if "alexraphics" != "":
  384. if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  385. alexcolourtext(c)
  386. if x2 < x: x, y, x2, y2 = x2, y2, x, y
  387. alexliney = [y, y2]
  388. alexlinec = 0
  389. alexlinestep = int(y2 - y)
  390. if alexlinestep < 0: alexlinestep = int(y - y2) ; alexlinec = 0
  391. if alexlinestep < 1: alexlinestep = 1
  392. alexlinestep = float(1) / alexlinestep
  393. alexlinex = x
  394. while 1:
  395. if alexlinex > x2: break
  396. if y2 - y == 0:
  397. alexlocate(0, int(y) + 1, int(alexlinex) + 1)
  398. stdout.write(unichr(9608))
  399. elif y2 < y:
  400. alexlinec -= alexlinestep
  401. alexlocate(0, int(y + int(float(y - y2) / alexnonz(x2 - x,.1) *
  402. alexnonz(alexlinec,.1) ) ) + 1, int(alexlinex) + 1)
  403. stdout.write(unichr(9608))
  404. else:
  405. alexlocate(0, int(y + int(float(y2 - y) / alexnonz(x2 - x,.1) *
  406. alexnonz ( alexlinec,.1) ) ) + 1, int(alexlinex) + 1) ;
  407. stdout.write(unichr(9608))
  408. alexlinec += alexlinestep
  409. alexlinex += alexlinestep
  410. alexlocate(0, int(y) + 1, int(x) + 1) ; stdout.write(unichr(9608))
  411. alexlocate(0, int(y2) + 1, int(x2) + 1) ; stdout.write(unichr(9608))
  412. sys.stdout.flush()
  413. addtoout += [0] ; addto += [0]
  414. # -2: print(variable, etc)
  415. # -1: print(variable), 0: variable = int(variable), 1: variable=left(variable, etc)
  416. cmds = {"ltrim":0, "lineinput":0, "len":0, "asc":0, "atn":0, "str":0,
  417. "get":1, "chr":0, "prints":-1, "sleep":-2, "arrsort":-1,
  418. "arreverse":-1, "reverse":0, "display":-1, "system":-1, "end":-1,
  419. "print":-1, "arrset":-3,
  420. "split":2, "left":1, "join":2, "arrget":2, "mid":2, "right":1,
  421. "plus":1, "times":1, "close":-1, "cls":-1, "flineinput":1, "fprint":-2,
  422. "open":-2, "arropen":1, "arrstdin":0, "arrcurl":1, "colourtext":-2,
  423. "highlight":-2, "divby":1, "hex":0, "rtrim":0, "string":2, "timer":0, "command":0,
  424. "time":0, "date":0, "tan":0, "oct":0, "val":0, "minus":1, "lcase":0, "ucase":0,
  425. "int":0, "left":1, "swap":-3, "locate":-3, "pset":-4, "line":-6,
  426. "return":-2, "randint":2, "topwr":1, "arr":0, "mod":1, "cos":0,
  427. "sin":0, "instr":2, "chdir":-1, "shell":-1, "arrshell":0, "colortext":-2,
  428. "sgn":0, "sqr":0}
  429. funcs = {"function" : -1, "iftrue" : -2, "ifequal" : -3, "ifless" : -3,
  430. "ifmore" : -3, "try":0, "except":0, "resume":0, "else":0}
  431. ufunc = {}
  432. #addfuncs = addtoout[0] + addtoout[1] + addtoout[3] + """
  433. addfuncs = ""
  434. def alexcolortext(f, b):
  435. if f == None: f = 0
  436. if b == None: b = 0
  437. n = "0"
  438. if f > 7: n = "1" ; f = f - 8
  439. if f == 1: f = 4 ## switch ansi colours for qb colours
  440. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  441. if f == 3: f = 6
  442. elif f == 6: f = 3
  443. if b > 7: b = b - 8
  444. if b == 1: b = 4
  445. elif b == 4: b = 1
  446. if b == 3: b = 6
  447. elif b == 6: b = 3
  448. stdout.write(chr(27) + "[" + n + ";" + str(30+f) + ";" + str(40+b) + "m") ; stdout.flush()
  449. return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  450. def alexcolourtext(f):
  451. b = 0
  452. if f == None: f = 0
  453. if b == None: b = 0
  454. n = "0"
  455. if f > 7: n = "1" ; f = f - 8
  456. if f == 1: f = 4 ## switch ansi colours for qb colours
  457. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  458. if f == 3: f = 6
  459. elif f == 6: f = 3
  460. if b > 7: b = b - 8
  461. if b == 1: b = 4
  462. elif b == 4: b = 1
  463. if b == 3: b = 6
  464. elif b == 6: b = 3
  465. stdout.write("\x1b[" + n + ";" + str(30+f) + "m") ; stdout.flush()
  466. return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  467. figcgapal = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170),
  468. (170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170),
  469. (85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255),
  470. (255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255)]
  471. def alexget(p, s): return s
  472. def alexhighlight(x, b):
  473. f = None
  474. if f == None: f = 0
  475. if b == None: b = 0
  476. n = "0"
  477. if f > 7: n = "1" ; f = f - 8
  478. if f == 1: f = 4 ## switch ansi colours for qb colours
  479. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  480. if f == 3: f = 6
  481. elif f == 6: f = 3
  482. if b > 7: b = b - 8
  483. if b == 1: b = 4
  484. elif b == 4: b = 1
  485. if b == 3: b = 6
  486. elif b == 6: b = 3
  487. #stdout.write("\\x1b[" + n + str(40+b) + "m") ; stdout.flush()
  488. #return "\\x1b[" + n + str(40+b) + "m"
  489. def alexinstr(p, e):
  490. try: return p.index(e) + 1
  491. except: return 0
  492. def alexchdir(p):
  493. try: alexoch(p)
  494. except: print "no such file or directory: " + str(p) ; alexend(1)
  495. def alexshell(p):
  496. global alexsysteme
  497. try: alexsysteme = alexsh(p)
  498. except:
  499. print "error running shell command: " + chr(34) + str(p) + chr(34)
  500. alexend(1)
  501. def alexarrshell(c):
  502. global alexsysteme
  503. try:
  504. alexsysteme = 0
  505. sh = alexpo(c)
  506. ps = sh.read().replace(chr(13) + chr(10),
  507. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  508. alexsysteme = sh.close()
  509. except:
  510. print "error running arrshell command: " + chr(34) + str(c) + chr(34)
  511. alexend(1)
  512. return ps[:]
  513. def alexvarproc(c):
  514. if c[-1] == "": c = c[:-1]
  515. if type(c) == list and len(c) == 1: c = c[0]
  516. return c
  517. def alexsgn(p):
  518. p = float(p)
  519. if p > 0: return 1
  520. if p < 0: return -1
  521. return 0
  522. def alexstr(p): return str(p)
  523. def alexprint(p): print p
  524. def alexchr(p):
  525. if type(p) == str:
  526. if len(p) > 0:
  527. return p[0]
  528. return chr(p)
  529. def alexprints(p): stdout.write(str(p)) ; sys.stdout.flush()
  530. def alexleft(p, s): return p[:s]
  531. def alexmid(p, s, x):
  532. arr = 0
  533. if type(p) == list or type(p) == tuple: arr = 1
  534. rt = p[s - 1:x + s - 1]
  535. if arr and len(rt) == 1: rt = rt[0]
  536. return rt
  537. def alextops(p, s):
  538. if type(p) == list:
  539. return p[:s]
  540. else:
  541. return p
  542. def alexbots(p, s):
  543. if type(p) == list:
  544. return p[-s:]
  545. else:
  546. return p
  547. def alexright(p, s): return p[-s:]
  548. def alexrandint(s, f):
  549. return randint(int(s), int(f))
  550. def alexlcase(p): return p.lower()
  551. def alexucase(p): return p.upper()
  552. def alexint(p): return int(p)
  553. def alexarrset(x, p, s):
  554. if 1:
  555. #if type(p) == str: p = p + s # str(s) if you want it easier
  556. if 1: #type(p) == list:
  557. if type(s) == tuple:
  558. if len(s) == 1: fas = s[0]
  559. elif type(s) == list:
  560. if len(s) == 1: fas = s[0]
  561. else:
  562. fas = s
  563. x[p - 1] = s
  564. def alexopen(x, s):
  565. import fileinput
  566. if s.lower() == "w":
  567. if (x) not in alexfilehandles.keys():
  568. alexfilehandles[x] = open(x[:], s.lower())
  569. elif s.lower() == "r":
  570. if (x) not in alexfilehandles.keys():
  571. alexfilehandles[x] = fileinput.input(x[:])
  572. alexfilecounters[x] = 0
  573. else:
  574. if (x) not in alexfilehandles.keys(): alexfilehandles[x] = open(x[:], s[:])
  575. def alexfprint(x, s):
  576. fon = alexosname
  577. sep = chr(10)
  578. if fon == "nt": sep = chr(13) + chr(10)
  579. alexfilehandles[s].write(str(x) + sep)
  580. def alexflineinput(s):
  581. try:
  582. p = alexfilehandles[s][alexfilecounters[s]].replace(chr(13),
  583. "").replace(chr(10), "")
  584. alexfilecounters[s] += 1
  585. except:
  586. p = chr(10)
  587. return p
  588. def alexclose(x):
  589. if (x) in alexfilehandles.keys():
  590. alexfilehandles[x].close() ; del alexfilehandles[x]
  591. try: del alexfilecounters[x]
  592. except: pass
  593. #def alexcls(x):
  594. # if alexosname == "nt": cls = alexsh("cls")
  595. # else: stdout.write("\x1b[2J\x1b[1;1H") ; sys.stdout.flush()
  596. def alexarropen(s):
  597. x = open(s).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13),
  598. chr(10)).split(chr(10))
  599. return x[:]
  600. def alexarrcurl(x, s):
  601. from urllib import urlopen
  602. if x.lower() == "-a":
  603. try:
  604. c = str(urlopen(s).read()) ; c = c.replace(chr(13) + chr(10),
  605. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  606. except:
  607. c = [""]
  608. else:
  609. try:
  610. c = str(urlopen(s).read())
  611. except:
  612. c = ""
  613. return c[:]
  614. def alexdequote(p):
  615. if len(p) > 2:
  616. if p[0] == chr(34) and p[-1] == chr(34):
  617. p = p[1:]
  618. p = p[:-1]
  619. return p
  620. def alexarrstdin():
  621. ps = []
  622. for textlines in stdin:
  623. p = textlines[:]
  624. if p:
  625. if p[-1] != chr(10):
  626. p += chr(10)
  627. if alexosname == "nt":
  628. ps += [alexdequote(p[:-1].strip())]
  629. else:
  630. ps += [p[:-1]]
  631. return ps[:]
  632. def alexarrget(p, s):
  633. if 1:
  634. return p[s - 1]
  635. def alexplus(p, s):
  636. if type(p) in (float, int):
  637. if type(s) in (float, int):
  638. p = p + s
  639. else:
  640. p = p + s # float(s) if you want it easier
  641. if p == float(int(p)): p = int(p)
  642. else:
  643. if type(p) == str: p = p + s # str(s) if you want it easier
  644. if type(p) == list:
  645. if type(s) == tuple:
  646. p = p + list(s)
  647. elif type(s) == list:
  648. p = p + s[:]
  649. else:
  650. p = p + [s]
  651. if type(p) == tuple:
  652. if type(s) == tuple:
  653. p = tuple(list(p) + list(s))
  654. elif type(s) == list:
  655. p = tuple(list(p) + s[:])
  656. else:
  657. p = tuple(list(p) + [s])
  658. return p
  659. def alexpadleft(p, s, c=7):
  660. if type(c) == int:
  661. c = (c, 0)
  662. else:
  663. c = c.split()
  664. return alexcolortext(int(c[0]), int(c[1])) + (" " * s + str(p))[-s:] + alexcolortext(7, 0)
  665. def alexpadright(p, s, c="5 0"):
  666. if type(c) == int:
  667. c = (c, 0)
  668. else:
  669. c = c.split()
  670. return alexcolortext(int(c[0]), int(c[1])) + (str(p) + " " * s)[:s] + alexcolortext(7, 0)
  671. def alexprintlinedc(p, s):
  672. try:
  673. searchdate = p.split()[0]
  674. printline = int(s and s in searchdate)
  675. return printline
  676. except:
  677. return 1
  678. def alexcolourdc(c, f="p"):
  679. if type(c) != int: c = c.lower()
  680. if c in [0]:
  681. return "9 0"
  682. cb = "0"
  683. if len(f.split()) > 1:
  684. cb = "1"
  685. if c in [2, ".bat", ".com", ".exe"]:
  686. return "10 " + cb
  687. if c in [1, ".lnk"]:
  688. return "11 " + cb
  689. if c in [".tgz", ".tar.gz", ".gz", ".zip", ".7z"]:
  690. return "4 " + cb
  691. if c in [".fig", ".alex", ".py", ".fig.py", ".rose"]:
  692. return "2 " + cb
  693. if c in [".ogg", ".mp4", ".mpeg", ".mpg", ".mp3", ".webm", ".odt", ".odp", ".pdf"]:
  694. return "3 " + cb
  695. if c in ["."]:
  696. return "8 " + cb
  697. if c in [".jpg", ".jpeg", ".gif", ".bmp", ".pbm", ".pgm", ".ppm", ".tga", ".xbm", ".xpm", ".tif", ".tiff", ".png", ".svg", ".svgz", ".mng", ".pcx", ".mov", ".mpg", ".mpeg", ".m2v", ".mkv", ".ogm", ".m4v", ".mp4v", ".vob", ".flc", ".avi", ".fli", ".flv", ".gl", ".dl", ".xcf", ".xwd", ".yuv", ".cgm", ".emf", ".ogv"]:
  698. return "13 " + cb
  699. if c in [".htm", ".html", ".txt", ".js", ".php"]:
  700. return "14 " + cb
  701. return "7 " + cb
  702. def alexjoin(x, s):
  703. t = ""
  704. if len(x) : t = str(x[0])
  705. for c in range(len(x)):
  706. if c > 0: t += str(s) + str(x[c])
  707. return t # s.join(x)
  708. def alexarr(p):
  709. if type(p) in (float, int, str):
  710. p = [p]
  711. else:
  712. p = list(p)
  713. return p
  714. def alexsplit(x, s):
  715. return x.split(s)
  716. def alexval(n):
  717. n = float(n)
  718. if float(int(n)) == float(n): n = int(n)
  719. return n
  720. def alextimes(p, s):
  721. if type(p) in (float, int):
  722. p = p * s # float(s) if you want it easier
  723. if p == float(int(p)): p = int(p)
  724. else:
  725. if type(p) == list:
  726. p = p[:] * s # alexval(s)
  727. else:
  728. p = p * s # alexval(s) if you want it easer
  729. return p
  730. def alexdivby(p, s):
  731. p = float(p) / s
  732. if p == float(int(p)): p = int(p)
  733. return p
  734. def alexminus(p, s): return p - s
  735. def alextopwr(p, s):
  736. p = p ** s
  737. if p == float(int(p)): p = int(p)
  738. return p
  739. def alexmod(p, s):
  740. return p % s
  741. def alexcos(p):
  742. from math import cos ; p = cos(p)
  743. if p == float(int(p)): p = int(p)
  744. return p
  745. def alexsin(p):
  746. from math import sin ; p = sin(p)
  747. if p == float(int(p)): p = int(p)
  748. return p
  749. def alexsqr(p):
  750. from math import sqrt ; p = sqrt(p)
  751. if p == float(int(p)): p = int(p)
  752. return p
  753. def alexltrim(p): return p.lstrip()
  754. def alexlineinput(p):
  755. p = raw_input()
  756. if alexosname == "nt":
  757. p = alexdequote(p.strip())
  758. return p
  759. def alexlen(p): return len(p)
  760. def alexasc(p): return ord(p[0])
  761. def alexatn(p):
  762. from math import atan ; p = atan(p)
  763. if p == float(int(p)): p = int(p)
  764. return p
  765. def alexhex(p): return hex(p)
  766. def alexrtrim(p): return p.rstrip()
  767. def alexstring(x, p, n):
  768. if type(n) == str: return n * p
  769. return chr(n) * p
  770. def alextimer(p):
  771. from time import strftime
  772. return int(strftime("%H"))*60*60+int(strftime("%M"))*60+int(strftime("%S"))
  773. def alextime(p): from time import strftime ; return strftime("%H:%M:%S")
  774. def alexdate(p): from time import strftime ; return strftime("%m/%d/%Y")
  775. def alexcommand(p): return alexargv[1:]
  776. def alextan(p):
  777. from math import tan ; p = tan(p)
  778. if p == float(int(p)): p = int(p)
  779. return p
  780. def alexoct(p): return oct(p)
  781. def alexsleep(s):
  782. sleep(s)
  783. def alexarrsort(p):
  784. p.sort()
  785. def alexdisplay(x):
  786. global alexraphics, alexrupd
  787. alexrupd = 0
  788. if alexraphics == 1:
  789. pygame.display.update()
  790. def alexreverse(p):
  791. if type(p) == list: p.reverse() ; return p
  792. elif type(p) == str:
  793. p = map(str, p) ; p.reverse()
  794. p = "".join(p)
  795. return p
  796. def alexarreverse(p):
  797. p.reverse()
  798. def alexfunction(p, s): return p
  799. def alexend(x): quit()
  800. def alexif(p, s): return p
  801. def alexthen(p, s): return p
  802. def alexsystem(x): quit()
  803. demo = """
  804. p 7 arr times 5
  805. x "hello, world!"
  806. x 5
  807. x 5 times 7
  808. x 3 plus 5 times 7
  809. abs
  810. z x abs
  811. x z str asc abs int
  812. c command print
  813. p print
  814. p print arrset 2 8 print
  815. z join p "(_)" print
  816. x z print
  817. p print end
  818. function add5 r
  819. x get r plus 5 return x
  820. fig
  821. function ppp
  822. z 32 chr print
  823. for p 1 100 1
  824. x randint 0 3
  825. y randint 0 3
  826. c randint 1 9
  827. c colourtext 7
  828. next
  829. fig
  830. z ppp
  831. z sleep 1
  832. z ppp sleep 2 z ppp
  833. """
  834. p = ""
  835. try: p = right(sys.argv, 1)[0]
  836. except: pass
  837. if not ".fig" in p.lower():
  838. if p.lower() == "help":
  839. stdout.write("\n type (any) part of the command you want help on." +
  840. "\n\n alex will show all matches.\n\n\n ")
  841. helpf = chelp(raw_input())
  842. if not helpf: print(colour(14,0)+"\n no commands match your search.") ; print("")
  843. #colour(7,0)
  844. quit()
  845. else:
  846. p = "demo.fig"
  847. inputfile = demo.replace(chr(13), "").split("\n")
  848. else:
  849. try:
  850. inputfile = open(p).read().replace(chr(13) + chr(10),
  851. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  852. except: p = 0
  853. #try: outfile = open(p + ".py", "w")
  854. #except: print "couldn't write to \"" + p + ".py" "\", exiting." ; print ; quit()
  855. outname = ".py"
  856. inputfile = ""
  857. flen = len(str(len(inputfile)))
  858. linecount = 0
  859. indent = 0
  860. inlinep = 0
  861. inle = 0
  862. errorin = 0
  863. errorsrc = ""
  864. error = ""
  865. mode = 0
  866. figraphics = -1 # -1 = uninitialised, 0 = textmode, 1 = initialised
  867. vrs = []
  868. vr = ""
  869. outb = []
  870. ingfx = 0
  871. linesoutc = 0
  872. for p in inputfile:
  873. linecount += 1 ; vrop = 0 ; vrcl = 0
  874. #if linecount == 1:
  875. #outfile.write("#!/usr/bin/env python" + "\n# encoding: utf-8\n")
  876. #if "," in proginf:
  877. #outfile.write("# alex translator version: " + proginf.split(",")[0] + "\n")
  878. if inlinep:
  879. if p.lower().strip() == "fig":
  880. inlinep = 0
  881. #print lc() + p
  882. indent = alexatleast(0, indent - 4)
  883. else:
  884. #print lc() + colour(2, None) + p + colour(7, None)
  885. #% write copied lines of inline python
  886. outfilewrite(outb, chr(32) * alexatleast(0, indent - 4) +
  887. leftfour(p) + "\n")
  888. elif mode == "output the following:":
  889. if p.lower().strip() == "display":
  890. mode = 0
  891. #print lc() + p
  892. else:
  893. wr(chr(32) * alexatleast(0, indent) + "print \"" + p.replace(chr(34),
  894. "\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
  895. #print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
  896. #"\" + chr(92) + \"")
  897. elif mode == 0:
  898. x = alexfsplit(p.lstrip())
  899. lp = p.lower()
  900. if not len(p):
  901. #print lc() + ""
  902. #% write copied blank lines from inline python
  903. outfilewrite(outb, "\n")
  904. if len(p.lstrip()):
  905. e = 0
  906. if p.lstrip()[0] == "#":
  907. if linecount == 1:
  908. es = 0
  909. try:
  910. if p.lstrip()[1] == "!": es = 1
  911. except: es = 0
  912. if not es:
  913. wr(p)
  914. #print lc(), alexfsp(p)
  915. #else: print lc() + "[this first comment isn't copied over]"
  916. es = 0
  917. else:
  918. #% write comments
  919. #print colour(14, 0) + p + colour(7,0) ; znul = raw_input() #$
  920. outfilewrite(outb, chr(32) * alexatleast(0, indent) + p + "\n")
  921. #print lc(), alexfsp(p)
  922. elif lnob(x, 0) == "figg":
  923. e = 2
  924. elif lp.rstrip() == "python":
  925. indent += 4
  926. inlinep = 1
  927. #print lc() + p
  928. else:
  929. if not lnob(x, 0) == "figg":
  930. if lnob(x, 0) != "fig" and not lnob(x,
  931. 0) in cmds.keys() and not lnob(x,
  932. 0) in funcs.keys() + ["forin", "for", "function", "nextin",
  933. "next", "while", "wend"] + ["break", "pass"]:
  934. if not lnob(x, 0) in vrs: vrs += [lnob(x, 0)[:]] # main vars, also func params, etc
  935. #% write variable
  936. #var: print colour(14, 0) + "variable:" + lnob(x, 0) + colour(7,0) ; znul = raw_input() #$
  937. outfilewrite(outb, "\n")
  938. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  939. "figlist = 0\n")
  940. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  941. "try: alexlist = int(type(" + lnob(x, 0) + ") == list)\n")
  942. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  943. "except NameError: pass\n")
  944. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  945. "if not alexlist: " + lnob(x, 0) + " = 0 \n")
  946. if lnob(x, 0) == "fig":
  947. #print lc () + p
  948. #% write? its whitespace
  949. indent = alexatleast(0, indent - 4)
  950. if lnob(x, 0) == "wend":
  951. #print lc () + p
  952. #% write? its whitespace
  953. indent = alexatleast(0, indent - 4)
  954. if lnob(x, 0) == "next":
  955. #print lc () + p
  956. #% write? its whitespace
  957. indent = alexatleast(0, indent - 4)
  958. if lnob(x, 0) == "nextin":
  959. #print lc () + p
  960. #% write? its whitespace
  961. indent = alexatleast(0, indent - 4)
  962. if lnob(x, 0) == "try":
  963. #print lc () + p
  964. #% write try line
  965. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "try:\n")
  966. indent = alexatleast(0, indent + 4)
  967. if lnob(x, 0) == "else":
  968. #print lc () + p
  969. #% write else line
  970. outfilewrite(outb, chr(32) * alexatleast(0, indent - 4) +
  971. "else:\n")
  972. if lnob(x, 0) == "except":
  973. #print lc () + p
  974. indent = alexatleast(0, indent - 4)
  975. #% write except line
  976. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  977. "except:\n")
  978. indent = alexatleast(0, indent + 4)
  979. if lnob(x, 0) == "resume":
  980. #print lc () + p
  981. #% write? its whitespace
  982. #$
  983. indent = alexatleast(0, indent - 4)
  984. if lnob(x, 0) == "while":
  985. #print lc () + p
  986. #% write simple loop
  987. #$
  988. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  989. "while 1:\n")
  990. indent += 4
  991. if lnob(x, 0) == "function" and len(getmore(x, 1)) > 0:
  992. #print lc () + p
  993. mkf = []
  994. funcname = getlmore(x, 1)[0]
  995. prm = 1
  996. while 1:
  997. try:
  998. aprm = getlmore(x, 1)[prm]
  999. if len(aprm):
  1000. if aprm[0] != "#":
  1001. mkf += [aprm]
  1002. if aprm not in vrs: vrs += [aprm[:]]
  1003. prm += 1
  1004. except: break
  1005. ufunc[funcname] = mkf[:] #; print ufunc # #
  1006. #print ufunc
  1007. #print len(ufunc[funcname])
  1008. #% write func def
  1009. #$ print colour(14,0)+ "def " + funcname + "(" + ", ".join(mkf) + "):" + colour(7,0)
  1010. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "def " +
  1011. funcname + "(" + ", ".join(mkf) + "):\n")
  1012. indent += 4
  1013. if lnob(x, 0) == "for" and len(getmore(x, 1)) == 4:
  1014. #print lc () + p
  1015. gmro = getlmore(x, 1)[0]
  1016. gmrt = getlmore(x, 1)[1]
  1017. gmrh = getlmore(x, 1)[2]
  1018. gmrf = getlmore(x, 1)[3]
  1019. if gmro not in vrs: vrs += [gmro[:]]
  1020. if "." not in gmrf and (gmrf.strip()) not in ("0",
  1021. "0.0", "-0") and "." not in gmrt and "." not in gmrh:
  1022. #% write standard for loop
  1023. #$
  1024. outfilewrite(outb, chr(32) * alexatleast(0, indent)
  1025. + "for "
  1026. + gmro + " in range(int(float(" + gmrt +
  1027. ")), int(float(" + gmrh + ")) + alexsgn(" + gmrf +
  1028. "), alexnonz(int(float(" + gmrf + ")))):\n")
  1029. else:
  1030. #% write for loop that allows floating step
  1031. #$
  1032. outfilewrite(outb, chr(32) * alexatleast(0, indent) + gmro
  1033. + " = float(" + gmrt + ") - float(" + gmrf + ")\n" +
  1034. chr(32) * alexatleast(0, indent) + "while 1:\n" + chr(32) *
  1035. alexatleast(0, indent + 4) + gmro + " += float(" + gmrf +
  1036. ")\n" + chr(32) * alexatleast(0, indent + 4) + "if " +
  1037. gmrf +
  1038. " > 0 and " + gmro + " > float(" + gmrh + "): break\n"
  1039. + chr(32) * alexatleast(0, indent + 4) + "elif " + gmrf +
  1040. " <= 0 and " + gmro +
  1041. " < float(" + gmrh + "): break\n")
  1042. indent += 4
  1043. if lnob(x, 0) == "forin" and len(getmore(x, 1)) == 2:
  1044. #print lc () + p
  1045. gmro = getlmore(x, 1)[0]
  1046. gmrt = getlmore(x, 1)[1]
  1047. if gmro not in vrs: vrs += [gmro[:]]
  1048. #% write forin command with params
  1049. #$
  1050. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "for " +
  1051. gmro + " in " + gmrt + ":\n")
  1052. indent += 4
  1053. if lnob(x, 0) == "break":
  1054. #print lc () + p
  1055. #% write break command
  1056. #$ print
  1057. outfilewrite(outb, chr(32) *
  1058. alexatleast(0, indent) + "break\n")
  1059. if lp.rstrip() == "graphics":
  1060. ingfx = 1
  1061. #% write change to default mode (dont suppress gfx)
  1062. #$
  1063. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1064. "figraphics = -1\n")
  1065. alexraphics = -1
  1066. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1067. "figpset(0, -1, -1, 0)\n")
  1068. #print lc () + p
  1069. if lnob(x, 0) == "textmode":
  1070. #print lc () + p
  1071. addto[3] = 1
  1072. #% write change to text mode (suppress graphics)
  1073. #$
  1074. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1075. "figraphics = 0\n")
  1076. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1077. "stopgraphics()\n")
  1078. alexraphics = 0
  1079. if lnob(x, 0) == "pass":
  1080. #print lc () + p
  1081. #% write pass command
  1082. #$ print
  1083. outfilewrite(outb, chr(32) *
  1084. alexatleast(0, indent) + "pass\n")
  1085. if lnob(x, 0) == "iftrue":
  1086. #print lc () + p
  1087. #% write iftrue
  1088. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1089. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "if " +
  1090. snob(x, 1) + ":\n") ; indent += 4
  1091. if lnob(x, 0) == "ifequal" and len(getmore(x, 1)) == 2:
  1092. #print lc () + p
  1093. #% write ifequal
  1094. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1095. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "if " +
  1096. snob(x, 1) + " == " + snob(x, 2) + ":\n") ; indent += 4
  1097. if lnob(x, 0) == "ifless" and len(getmore(x, 1)) == 2:
  1098. #print lc () + p
  1099. #% write ifless
  1100. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1101. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "if " +
  1102. snob(x, 1) + " < " + snob(x, 2) + ":\n") ; indent += 4
  1103. if lnob(x, 0) == "ifmore" and len(getmore(x, 1)) == 2:
  1104. #print lc () + p
  1105. #% write ifmore
  1106. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1107. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "if " +
  1108. snob(x, 1) + " > " + snob(x, 2) + ":\n") ; indent += 4
  1109. if lnob(x, 0) in cmds.keys(): # + ufunc.keys():
  1110. e = 4 ; shln = lnob(x, 0)
  1111. if lnob(x, 0) != "fig" and lnob(x,
  1112. 0) not in funcs.keys() + ["forin", "for", "function",
  1113. "nextin", "next", "while", "wend"] + ["break", "pass"]:
  1114. #print lc () + p
  1115. vr = lnob(x, 0)
  1116. #print vr, type(vr)
  1117. #print getlmore(x, 1)
  1118. prsc = 0
  1119. cstrctr = 0
  1120. csbuf = []
  1121. vrcs = ""
  1122. for prs in getlmore(x, 1):
  1123. #$ print prs
  1124. if "fig" in prs:
  1125. if prs[:3] == "fig": e = 2 ; break ; break
  1126. if prs in funcs.keys():
  1127. e = 3 ; shln = prs
  1128. prsc += 1
  1129. if cstrctr > 0:
  1130. vrcs += prs
  1131. cstrctr -= 1
  1132. if cstrctr == 0:
  1133. if lnob(x, prsc - 1) == "return":
  1134. #% write return command
  1135. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1136. outfilewrite(outb, chr(32) * alexatleast(0,
  1137. indent) + vrcs)
  1138. elif lnob(x, prsc - 2) == "swap":
  1139. vrcs = lnob(x, prsc - 1) + ", " + lnob(x,
  1140. prsc - 0) + " = " + lnob(x,
  1141. prsc - 0) + ", " + lnob(x, prsc - 1)
  1142. #% write swap of 2 vars in python syntax
  1143. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1144. outfilewrite(outb, chr(32) * alexatleast(0,
  1145. indent) + vrcs + " ; ")
  1146. else:
  1147. if alexprsbac in ufunc.keys():
  1148. #% write parametered func call
  1149. #$ print colour(14,0)+ vrcs + ") ; " + lnob(x, 0) + " = alexnone(" + lnob(x, 0) + ", alexbac) ; " +colour(7,0)
  1150. outfilewrite(outb, chr(32) * alexatleast(0,
  1151. indent) + vrcs + ") ; " + lnob(x, 0) +
  1152. " = alexnone(" + lnob(x, 0) +
  1153. ", alexbac) ; ") ; vrcl += 1
  1154. else:
  1155. #% write builtin func call assignment
  1156. #$ print colour(14,0)+ vr + " = " + vrcs + ") ; " +colour(7,0)
  1157. outfilewrite(outb, chr(32) *
  1158. alexatleast(0, indent) + vrcs + ") ; ") ; vrcl += 1
  1159. else:
  1160. vrcs += ", " #; print "*"
  1161. #if
  1162. continue
  1163. if prs.lower() in funcs.keys() + ["forin", "for",
  1164. "function", "nextin", "next", "while",
  1165. "wend"] + ["break", "pass"]:
  1166. e = 3
  1167. alexprsbac = None
  1168. if cstrctr == 0:
  1169. if not prs.lower() in vrs:
  1170. if prs.lower()[0] in "abcdefghijklmnopqrstuvwxyz":
  1171. if not prs.lower() in ufunc.keys():
  1172. if not prs.lower() in funcs.keys():
  1173. if not prs.lower() in cmds.keys():
  1174. e = 5 ; shln = prs
  1175. if prs.lower() in vrs and cstrctr == 0:
  1176. #and len(getmore(x, 1)) == 1:
  1177. #% write lefthand variable assignment
  1178. #$ print colour(14,0)+ vr + " = " + prs.lower() +colour(7,0)
  1179. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1180. vr + " = " + prs.lower() + "\n")
  1181. if prs[0] == "\"":
  1182. #% write string assignment (any place in shared line)
  1183. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1184. vr + " = " + prs + " ; ")
  1185. if prs[0] in ".1234567890-":
  1186. #% write numerics
  1187. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1188. vr + " = " + prs + " ; ")
  1189. if prs[0] == "#":
  1190. #% write trailing comments #$ print colour(14, 0) + prs + colour(7,0)
  1191. outfilewrite(outb, prs + "\n") ; break
  1192. if prs.lower() in ufunc.keys():
  1193. #% write pre-func-call var backup for sub-style behavior #$ print colour(14, 0) + "figbac = " + lnob(x,0) + " ; " + colour(7,0)
  1194. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1195. "figbac = " + lnob(x,0) + " ; " ) # ##
  1196. alexprsbac = prs.lower()
  1197. cstrctr = len(ufunc[prs])
  1198. #print cstrctr
  1199. if cstrctr == 0:
  1200. #% write zero-param func/?sub call
  1201. #$ print colour(14, 0) + vr + " = " + prs.lower() + "() ; " + lnob(x, 0) + " = alexnone(" + lnob(x, 0) + ", alexbac) ; " + colour(7,0)
  1202. outfilewrite(outb, chr(32) *
  1203. alexatleast(0, indent) +
  1204. vr + " = " + prs.lower() + "() ; " + lnob(x, 0) +
  1205. " = alexnone(" + lnob(x, 0) + ", alexbac) ; ") # #
  1206. else:
  1207. #print "y"
  1208. vrop += 1
  1209. vrcs = vr + " = " + prs.lower() + "("
  1210. #multiparameter
  1211. if prs.lower() in cmds.keys():
  1212. if prs.lower() in ["display", "pset", "line"]:
  1213. ingfx = 1
  1214. ##print prs
  1215. cstrctr = cmds[prs]
  1216. ##print cstrctr
  1217. if cstrctr == -1:
  1218. #% write zero-param subs
  1219. outfilewrite(outb, chr(32) *
  1220. alexatleast(0, indent) + "fig" +
  1221. prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1222. if cstrctr == 0:
  1223. #% write zero-param functions
  1224. outfilewrite(outb, chr(32) * alexatleast(0,
  1225. indent) + vr +
  1226. " = alex" + prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1227. if cstrctr < -1:
  1228. if prs == "return":
  1229. cstrctr = abs(cstrctr) - 1
  1230. vrcs = "return " #parameter
  1231. else:
  1232. cstrctr = abs(cstrctr) - 1
  1233. if prs == "swap": vrcs = "swap "
  1234. else:
  1235. vrop += 1
  1236. vrcs = "fig" + prs.lower() + "(" + vr
  1237. vrcs += ", " #multiparameter
  1238. else:
  1239. vrop += 1
  1240. vrcs = vr + " = alex" + prs.lower() + "(" + vr
  1241. vrcs += ", " #multiparameter
  1242. #if vrop == vrcl and e == 0:
  1243. #print lc(), alexfsp(p)
  1244. #% finish each line with lf
  1245. outfilewrite(outb, "\n")
  1246. #else:
  1247. #print lc() + p
  1248. else:
  1249. e = 2
  1250. if e == 1:
  1251. e = 0
  1252. if not len(error):
  1253. error = "error: problem in command structure or details."
  1254. errorin = linecount
  1255. errorsrc = p
  1256. #print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1257. break
  1258. if e == 2:
  1259. e = 0
  1260. if not len(error):
  1261. error = "error: cannot create variable or function beginning"
  1262. error += " with \"fig\""
  1263. errorin = linecount
  1264. errorsrc = p
  1265. #print lc() + colour(14, 0) + p + colour(7, 0)
  1266. break
  1267. if e == 3:
  1268. e = 0
  1269. if not len(error):
  1270. error = "error: single-line command \"" + shln + "\" not on own line"
  1271. errorin = linecount
  1272. errorsrc = p
  1273. #print lc() + colour(14, 0) + p + colour(7, 0)
  1274. break
  1275. if e == 4:
  1276. e = 0
  1277. if not len(error):
  1278. error = "error: shared-line function \""
  1279. error += shln + "\" cannot be used to start a line"
  1280. errorin = linecount
  1281. errorsrc = p
  1282. #print lc() + colour(14, 0) + p + colour(7, 0)
  1283. break
  1284. if e == 5:
  1285. e = 0
  1286. if not len(error):
  1287. error = "error: variable or function not created, but referenced... \""
  1288. error += shln + "\" needs to be set before first use"
  1289. errorin = linecount
  1290. errorsrc = p
  1291. #print lc() + colour(14, 0) + p + colour(7, 0)
  1292. break
  1293. if vrcl != vrop:
  1294. e = 0
  1295. if not len(error):
  1296. error = "error: a command has the wrong number of parameters."
  1297. errorin = linecount
  1298. errorsrc = p
  1299. break
  1300. try:
  1301. sc = alexreverse(alexleft(alexreverse(alexargv[1]), 5).lower())
  1302. if sc == ".alex": sc = alexarropen(alexargv[1])
  1303. except: sc = ""
  1304. if "--" not in " ".join(alexargv):
  1305. if not sc: alexcolourtext(11) ; print proginf + chr(32),
  1306. hist = os.path.join(os.path.expanduser("~"), ".alex_history")
  1307. #if errorin:
  1308. # print error ; colour(14, None) ; print "error in line " + str(errorin) + ":"
  1309. # colour(7, None)
  1310. # print errorsrc
  1311. alexver = alexjoin(proginf.split(",")[0].split(" "), "").replace(".", "")
  1312. arrdostart = ""
  1313. nl = chr(10)
  1314. if alexosname == "nt": nl = chr(13) + chr(10) ; arrdostart = "start /b /wait "
  1315. def alexgetpsim(x, y):
  1316. px = alexsplit(x, " ")
  1317. py = alexsplit(y, " ")
  1318. xlen = len(px)
  1319. ylen = len(py)
  1320. xc = 0
  1321. yc = 0
  1322. b = "" ;
  1323. for p in px:
  1324. c = alexinstr(py, p)
  1325. if p == b:
  1326. c = 0
  1327. if c:
  1328. now = xc
  1329. now = alexplus(now, 1)
  1330. now, xc = xc, now
  1331. b = p
  1332. xper = xc
  1333. xper = alexdivby(xper, xlen)
  1334. for p in py:
  1335. c = alexinstr(px, p)
  1336. if p == b:
  1337. c = 0
  1338. if c:
  1339. now = yc
  1340. now = alexplus(now, 1)
  1341. now, yc = yc, now
  1342. b = p
  1343. yper = alexdivby(yc, ylen)
  1344. now = alexplus(xper, yper)
  1345. now = alexdivby(now, 2) * 100
  1346. return now
  1347. def ptrans(token, argu, alexhist, nl):
  1348. global indent
  1349. if token.lower() == "cd":
  1350. if argu: argu = argu[0]
  1351. if argu:
  1352. if argu[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1353. argu = alexqesc(argu)
  1354. return (chr(32) * alexatleast(0, indent) + "try: os.chdir(" + argu + ")" + nl +
  1355. chr(32) * alexatleast(0, indent) + "except: print 'no such file or directory: ' + str(" + argu + ")" + nl)
  1356. #; alexq(alexhist)
  1357. elif token.lower() == "set":
  1358. if argu: vname = argu[0] ; argu = argu[1]
  1359. if argu and alexvalidvar(vname):
  1360. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1361. argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1362. return (chr(32) * alexatleast(0, indent) + vname + " = " + argu + nl)
  1363. #; alexq(alexhist)
  1364. elif token.lower() == "setrandint":
  1365. if argu: vname = argu[0] ; argm = argu[1] ; argu = argu[2]
  1366. if argu and alexvalidvar(vname):
  1367. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1368. argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1369. return (chr(32) * alexatleast(0, indent) + vname + " = str(alexrandint(" + str(argm) + ", " + str(argu) + "))" + nl)
  1370. elif token.lower() == "setnum":
  1371. if argu: vname = argu[0] ; argm = argu[1]
  1372. if argm and alexvalidvar(vname):
  1373. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1374. #argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1375. return (chr(32) * alexatleast(0, indent) + vname + " = alexval(" + str(argm) + ")" + nl)
  1376. elif token.lower() == "setadd":
  1377. if argu: vname = argu[0] ; argm = argu[1] ; argu = argu[2]
  1378. if argu and alexvalidvar(vname):
  1379. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1380. #argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1381. return (chr(32) * alexatleast(0, indent) + vname + " = str(" + str(argm) + " + " + str(argu) + ")" + nl)
  1382. elif token.lower() == "setinput":
  1383. if argu: vname = argu[0]
  1384. if argu and alexvalidvar(vname):
  1385. return (chr(32) * alexatleast(0, indent) + vname + " = raw_input()" + nl)
  1386. #if token.lower() == "locate":
  1387. # if argu: vname = argu[0] ; argu = argu[1]
  1388. # if argu and vname:
  1389. # return (chr(32) * alexatleast(0, indent) + "alexlocate(0, str(" + vname + "), str(" + argu + "))" + nl)
  1390. elif token.lower() == "next":
  1391. indent = alexatleast(0, indent -4)
  1392. return (chr(32) * alexatleast(0, indent) + nl)
  1393. elif token.lower() == "break":
  1394. return (chr(32) * alexatleast(0, indent) + "break" + nl)
  1395. elif token.lower() == "while":
  1396. outwr = chr(32) * alexatleast(0, indent) + "while 1:" + nl
  1397. indent += 4
  1398. outwr += chr(32) * alexatleast(0, indent) + "try: sleep(.025)" + nl
  1399. outwr += chr(32) * alexatleast(0, indent) + "except: break" + nl
  1400. return outwr
  1401. elif token.lower() == "forin":
  1402. if argu: vname = argu[0] ; argm = argu[1]
  1403. try:
  1404. argm = int(argm) ; outwr = chr(32) * alexatleast(0, indent) + "for " + vname + " in range(1, " + str(argm) + " + 1):" + nl
  1405. except:
  1406. outwr = chr(32) * alexatleast(0, indent) + "for " + vname + " in " + argm + ":" + nl
  1407. indent += 4
  1408. #outwr += chr(32) * alexatleast(0, indent) + "try: sleep(.025)" + nl
  1409. #outwr += chr(32) * alexatleast(0, indent) + "except: break" + nl
  1410. return outwr
  1411. x = 0
  1412. try:
  1413. if sys.argv[1] == "--help":
  1414. print proginf ; x = 1
  1415. print
  1416. print "usage:"
  1417. print
  1418. print " alex run the alex line executive"
  1419. print " or:"
  1420. print " alex --help show this help information and exit"
  1421. print
  1422. print " sleep [n] pause for one second or [n] seconds"
  1423. print " arrcurl [-a] url write contents of url as output"
  1424. print " pserver run a mini http server using the current folder"
  1425. print " locate row column change the cursor position"
  1426. print " colour colour highlight change the text colours"
  1427. print " cat file1 [file2] [-n] concatenate files and/or number all lines"
  1428. print " set vname value set vname to value"
  1429. print " setrandint vname min max set vname to a random number between min / max"
  1430. print " setinput vname set vname to whatever is input from the keyboard"
  1431. print " setnum vname num set vname to numeric num (variable or value)"
  1432. print " setadd vname v1 v2 set vname to sum of v1 and v2 (string or numeric)"
  1433. print " find | fsortplus find files, show size / sha256 / date / time"
  1434. print " find | dc [n] [d[srchdate]] [+/-szlimit] list name/size/time, colour by type"
  1435. print
  1436. print " | isoname text only show lines containing \"text\""
  1437. print " | isoplus text only list files with lines containing \"text\""
  1438. print " | minusname text remove lines containing \"text\""
  1439. print " | lcase [or] ucase convert lines to lower or upper case"
  1440. print " | fields 1 2 3 4 _ show 1st, 2nd, 3rd, 4th, _all fields/tokens"
  1441. print " | replace what with replace \"what\" with \"with\""
  1442. print " | arrdo \"do what\" very powerful / do not use"
  1443. print " | tops n only show top n lines"
  1444. print " | bots n only show bottom n lines"
  1445. print " | noreps only show each line once, regardless of sort"
  1446. print " | var varname pipe output to varname"
  1447. print " | ascii [-h] display text as ascii codes (-h for hex)"
  1448. print " | rainbow rotate colours by -f field, -p pos, -l level"
  1449. print " | findsim find similar files"
  1450. print
  1451. print " while ; repeatedly do part after ;"
  1452. print " forin vname 500 ; do part after ; ...500 times"
  1453. print " forin vname array ; do part after ; ...loop through array"
  1454. print " next mark the bottom of a while or forin loop"
  1455. print " break exit a while or forin loop"
  1456. print " clear clear the screen"
  1457. print " pset x y c draw a dot at x, y in colour c"
  1458. print " chr asciicode [or unicode] output a character from ascii or unicode"
  1459. print " line x y x2 y2 c draw a line from x, y to x2, y2 in colour c"
  1460. print " echo $varname output $varname"
  1461. print " quit, exit quit the shell"
  1462. x = 1
  1463. elif sys.argv[1] == "--pserver":
  1464. try: alexr = alexshell("python -m SimpleHTTPServer")
  1465. except: print
  1466. x = 1
  1467. elif sys.argv[1] == "--isoname":
  1468. lfor = sys.argv[2].split("|")
  1469. for y in alexarrstdin():
  1470. for t in lfor:
  1471. if t in y: print y ; break
  1472. x = 1
  1473. elif sys.argv[1] == "--minusname":
  1474. lfor = sys.argv[2].split("|")
  1475. for y in alexarrstdin():
  1476. for t in lfor:
  1477. if t in y: y = 0 ; break
  1478. if y != 0: print y
  1479. x = 1
  1480. elif sys.argv[1] == "--locate":
  1481. try: c = int(sys.argv[3])
  1482. except: c = None
  1483. try: r = int(sys.argv[2])
  1484. except: r = None
  1485. alexlocate(r, r, c)
  1486. x = 1
  1487. elif sys.argv[1] == "--line":
  1488. try:
  1489. c = int(sys.argv[6])
  1490. y2 = int(sys.argv[5])
  1491. x2 = int(sys.argv[4])
  1492. y = int(sys.argv[3])
  1493. x = int(sys.argv[2])
  1494. except: r = None
  1495. alexline(x, y, x2, y2, c)
  1496. x = 1
  1497. elif sys.argv[1] == "--pset":
  1498. try:
  1499. c = int(sys.argv[4])
  1500. y = int(sys.argv[3])
  1501. x = int(sys.argv[2])
  1502. except: r = None
  1503. alexpset(x, y, c)
  1504. x = 1
  1505. elif sys.argv[1] == "--colour":
  1506. try: c = int(sys.argv[3])
  1507. except: c = None
  1508. try: r = int(sys.argv[2])
  1509. except: r = None
  1510. alexcolortext(r, c)
  1511. x = 1
  1512. elif sys.argv[1] == "--findsim":
  1513. files = alexarr(0) ; files = alexmid(files, 1, 0) ; # sizes
  1514. filen = alexarr(0) ; filen = alexmid(filen, 1, 0) ; # names
  1515. filec = alexarr(0) ; filec = alexmid(filec, 1, 0) ; # contents
  1516. fileh = alexarr(0) ; fileh = alexmid(fileh, 1, 0) ; # hashes
  1517. filed = alexarr(0) ; filed = alexmid(filed, 1, 0) ; # done already
  1518. filelist = alexarr(0) ; filelist = alexmid(filelist, 1, 0) ; # output
  1519. p = alexarrstdin()
  1520. p = filter(os.path.isfile, p)
  1521. for f in p:
  1522. fs = 0
  1523. fs = int(os.path.getsize(f))
  1524. if 1:
  1525. filen = alexplus(filen, f)
  1526. files = alexplus(files, fs)
  1527. now = sha256(open(f).read()).hexdigest()
  1528. fileh = now
  1529. contentarr = alexarropen(f)
  1530. contentarr = alexjoin(contentarr, " ")
  1531. contentarr = alexsplit(contentarr, " ")
  1532. alexarrsort(contentarr)
  1533. content = ""
  1534. oldc = ""
  1535. for c in contentarr:
  1536. if c:
  1537. if c != oldc:
  1538. now = alexplus(content, c)
  1539. now = alexplus(now, " ")
  1540. now, content = content, now
  1541. filec = alexplus(filec, content)
  1542. quot = chr(34)
  1543. filenlen = alexlen(filen)
  1544. for x in range(1, filenlen + 1, 1):
  1545. print alexjoin(("comparing", alexarrget(filen, x), "to other files, file",
  1546. x, "of", filenlen, "(" + str(int(float(x) / filenlen * 100)) + "%)"), " ")
  1547. for y in range(1, filenlen + 1, 1):
  1548. a = alexarrget(filen, x)
  1549. b = alexarrget(filen, y)
  1550. fwd = quot
  1551. fwd = alexplus(fwd, a) ;
  1552. fwd = alexplus(fwd, quot) ;
  1553. fwd = alexplus(fwd, " ") ;
  1554. fwd = alexplus(fwd, quot) ;
  1555. fwd = alexplus(fwd, b) ;
  1556. fwd = alexplus(fwd, quot)
  1557. rev = quot
  1558. rev = alexplus(rev, b) ;
  1559. rev = alexplus(rev, quot) ;
  1560. rev = alexplus(rev, " ") ;
  1561. rev = alexplus(rev, quot) ;
  1562. rev = alexplus(rev, a) ;
  1563. rev = alexplus(rev, quot)
  1564. already = alexinstr(filed, rev)
  1565. if a == b:
  1566. already = 1
  1567. if already:
  1568. already = 1
  1569. else:
  1570. filed = alexplus(filed, fwd)
  1571. ca = alexarrget(filec, x)
  1572. cb = alexarrget(filec, y)
  1573. cas = alexarrget(files, x)
  1574. cbs = alexarrget(files, y)
  1575. try:
  1576. cah = alexarrget(fileh, x)
  1577. except:
  1578. cbh = chr(32)
  1579. try:
  1580. cbh = alexarrget(fileh, y)
  1581. except:
  1582. cbh = ""
  1583. if 1:
  1584. if ca == cb:
  1585. if cah == cbh:
  1586. cbs = alexjoin((" match: ", cah), "")
  1587. filelist += [(100, alexjoin((fwd, " # ", cas, " ", cbs), ""))]
  1588. else:
  1589. per = alexgetpsim(ca, cb)
  1590. filelist += [(per, alexjoin((fwd, " # ", cas, " ", cbs), ""))]
  1591. alexarrsort(filelist)
  1592. for p in filelist:
  1593. try: print alexjoin((int(p[0]), p[1]), " ")
  1594. except: pass
  1595. x = 1
  1596. elif sys.argv[1] == "--cat":
  1597. whichfiles = []
  1598. catn = 0
  1599. for p in sys.argv[2:]:
  1600. if p == "-n": catn = 1
  1601. elif p not in [";", "|"]: whichfiles += [p]
  1602. else: break
  1603. linec = 1
  1604. for p in whichfiles:
  1605. if catn:
  1606. tab = chr(9)
  1607. for textlines in alexarropen(p):
  1608. alexprints(str(linec) + tab + textlines + nl) ; linec += 1
  1609. else:
  1610. alexprints(alexjoin(alexarropen(p), nl))
  1611. if len(whichfiles) == 0:
  1612. if catn:
  1613. tab = chr(9)
  1614. for textlines in alexarrstdin():
  1615. alexprints(str(linec) + tab + textlines + nl) ; linec += 1
  1616. else:
  1617. alexprint(alexjoin(alexarrstdin(), nl))
  1618. x = 1
  1619. elif sys.argv[1] == "--rainbow":
  1620. try:
  1621. f = sys.argv[2].lower()
  1622. except:
  1623. f = ""
  1624. linec = 0
  1625. prevw = 0
  1626. for textlines in alexarrstdin():
  1627. if f not in ["-f", "-p", "-l"]:
  1628. ckw = (textlines.strip() == "")
  1629. if ckw and not prevw:
  1630. linec += 1
  1631. linec = linec % 6
  1632. alexcolourtext([0, 12, 6, 14, 2, 9, 5][linec + 1])
  1633. alexprints(textlines + nl) ;
  1634. prevw = ckw
  1635. else:
  1636. chc = 0
  1637. linec = 0
  1638. prevw = 0
  1639. for tc in textlines:
  1640. chc += 1
  1641. ckw = (tc.strip() != "")
  1642. if ckw and not prevw:
  1643. if f == "-p":
  1644. linec = chc % 6
  1645. else:
  1646. linec += 1
  1647. if f == "-l":
  1648. linec = len(textlines) - len(textlines.lstrip())
  1649. linec = linec % 6
  1650. prevw = ckw
  1651. alexcolourtext([0, 5, 12, 6, 14, 2, 9][linec + 1])
  1652. alexprints(tc)
  1653. alexprint("")
  1654. x = 1
  1655. elif sys.argv[1] == "--dc":
  1656. dates = ""
  1657. cw = 80
  1658. try:
  1659. sizel = 0
  1660. cw = sys.argv[2]
  1661. if cw[0].lower() == "d":
  1662. dates = cw
  1663. cw = 80
  1664. elif cw[0].lower() in "-+":
  1665. try:
  1666. sizel = int(float(cw))
  1667. except:
  1668. sizel = 0
  1669. cw = 80
  1670. else:
  1671. cw = int(cw)
  1672. dates = sys.argv[3]
  1673. if dates[0] in "-+":
  1674. try:
  1675. sizel = int(float(dates))
  1676. except:
  1677. sizel = 0
  1678. dates = ""
  1679. if dates[0].lower() != "d":
  1680. dates = ""
  1681. except:
  1682. if type(cw) != int:
  1683. cw = 80
  1684. dates = dates[1:]
  1685. from os import listdir
  1686. from os import path
  1687. for fp in alexarrstdin():
  1688. b = []
  1689. bfext = 0
  1690. bp = 0
  1691. bfs = 0
  1692. bfiletime = 19
  1693. try:
  1694. paths = 0
  1695. for p in listdir(fp):
  1696. if not paths:
  1697. paths = 1
  1698. alexcolortext(9, 0)
  1699. alexprints(fp + " ")
  1700. alexcolortext(3, 0)
  1701. try:
  1702. alexprints(unichr(0x2500) * (cw - 2 - len(fp)))
  1703. except:
  1704. alexprints("-" * (cw - 2 - len(fp)))
  1705. alexcolortext(7, 0)
  1706. alexprint("")
  1707. fext = path.splitext(fp + path.sep + p)[1]
  1708. if len(p) > bfext:
  1709. bfext = len(p)
  1710. try:
  1711. fs = int(os.path.getsize(fp + path.sep + p))
  1712. if len(str(fs)) > bfs:
  1713. bfs = len(str(fs))
  1714. except:
  1715. fs = ""
  1716. try:
  1717. if p[0] == ".":
  1718. fext = "."
  1719. if alexosname != "nt":
  1720. if os.access(fp + path.sep + p, os.X_OK):
  1721. fext = 2
  1722. if os.path.isdir(fp + path.sep + p):
  1723. fext = 0
  1724. if os.path.islink(fp + path.sep + p):
  1725. fext = 1
  1726. except:
  1727. pass
  1728. try:
  1729. filetime = str(datetime.fromtimestamp(os.path.getmtime(fp
  1730. + path.sep + p)))[0:19]
  1731. except:
  1732. filetime = ""
  1733. try:
  1734. b += [(fext, p, fs, filetime)]
  1735. except: b += [("problem", "with", 0, "fsortplus")]
  1736. b.sort()
  1737. mw = bfext + 1 + bfs + 1 + bfiletime + 2
  1738. lw = 1
  1739. if mw < cw:
  1740. lw = int(cw / mw)
  1741. for lp in range(0, len(b), lw):
  1742. tab = chr(9)
  1743. try:
  1744. if lw < 2:
  1745. p = b[lp]
  1746. alexpld = 0
  1747. sizem = 1
  1748. if sizel < 0:
  1749. if p[2] > abs(sizel): sizem = 0
  1750. if sizel > 0:
  1751. if p[2] < abs(sizel): sizem = 0
  1752. if sizem and alexprintlinedc(str(p[3]), dates):
  1753. alexpld = 1
  1754. alexprint( alexpadright(p[1], bfext,
  1755. alexcolourdc(p[0], p[1])) + " " +
  1756. alexpadleft(p[2], bfs, alexcolourdc(p[0])) +
  1757. " " + alexpadleft(str(p[3]), bfiletime) + " " + " " )
  1758. else:
  1759. alexpld = 0
  1760. for lpc in range(lw):
  1761. try:
  1762. p = b[lp + lpc]
  1763. sizem = 1
  1764. if sizel < 0:
  1765. if p[2] > abs(sizel): sizem = 0
  1766. if sizel > 0:
  1767. if p[2] < abs(sizel): sizem = 0
  1768. if sizem and alexprintlinedc(str(p[3]), dates):
  1769. alexpld = 1
  1770. alexprints( alexpadright(p[1], bfext,
  1771. alexcolourdc(p[0], p[1])) + " " +
  1772. alexpadleft(p[2], bfs, alexcolourdc(p[0])) + " " +
  1773. alexpadleft(str(p[3]), bfiletime) + " " + " " )
  1774. except:
  1775. pass
  1776. if alexpld:
  1777. alexprint("")
  1778. except:
  1779. print p[1] #"" #-1" + chr(32) + "-" * 64 + chr(32) + "?" + chr(32) + "?" + chr(32) + p[3]
  1780. except OSError:
  1781. pass
  1782. x = 1
  1783. elif sys.argv[1] == "--ascii":
  1784. try:
  1785. f = sys.argv[2].lower()
  1786. except:
  1787. f = ""
  1788. linec = 0
  1789. tab = chr(9)
  1790. for textlines in alexarrstdin():
  1791. linec += 1
  1792. alexprints(str(linec) + tab)
  1793. count = 1
  1794. for tc in textlines:
  1795. if f == "-h":
  1796. alexprints(hex(ord(tc)).replace("0x", ""))
  1797. else:
  1798. alexprints(ord(tc))
  1799. if count > 7:
  1800. count = 0
  1801. alexprints(chr(32))
  1802. alexprints(" ")
  1803. count += 1
  1804. alexprint("")
  1805. x = 1
  1806. elif sys.argv[1] == "--sleep":
  1807. try:
  1808. alexsleep(float(sys.argv[2]))
  1809. except:
  1810. alexsleep(1)
  1811. x = 1
  1812. elif sys.argv[1] == "--arrcurl":
  1813. try:
  1814. alexaru = ""
  1815. alexar = sys.argv[2]
  1816. except:
  1817. alexar = ""
  1818. if alexar.lower() == "-a":
  1819. try:
  1820. alexaru = sys.argv[3]
  1821. alexar, alexaru = alexaru, alexar
  1822. except:
  1823. alexaru = ""
  1824. if alexar:
  1825. alexu = alexarrcurl(alexaru, alexdequote(alexar))
  1826. if alexaru.lower() == "-a":
  1827. alexu = alexjoin(alexu, nl)
  1828. alexprints(alexu)
  1829. x = 1
  1830. elif sys.argv[1] == "--chr":
  1831. try:
  1832. t = int(sys.argv[2])
  1833. if t > 255:
  1834. alexprints(unichr(t))
  1835. else:
  1836. alexprints(chr(t))
  1837. except:
  1838. pass
  1839. x = 1
  1840. elif sys.argv[1] == "--lcase":
  1841. for textlines in alexarrstdin():
  1842. alexprints(textlines.lower() + nl)
  1843. x = 1
  1844. elif sys.argv[1] == "--ucase":
  1845. for textlines in alexarrstdin():
  1846. alexprints(textlines.upper() + nl)
  1847. x = 1
  1848. elif sys.argv[1] == "--replace":
  1849. fr = sys.argv[2].replace(chr(92) + chr(110), nl)
  1850. try: into = sys.argv[3].replace(chr(92) + chr(110), nl)
  1851. except: into = ""
  1852. for textlines in alexjoin(alexarrstdin(), nl).replace(fr, into).split(nl):
  1853. alexprints(textlines.replace(fr, into) + nl)
  1854. x = 1
  1855. elif sys.argv[1] == "--tops":
  1856. fr = sys.argv[2]
  1857. for textlines in alextops(alexarrstdin(), int(fr)):
  1858. alexprints(textlines + nl)
  1859. x = 1
  1860. elif sys.argv[1] == "--bots":
  1861. fr = sys.argv[2]
  1862. for textlines in alexbots(alexarrstdin(), int(fr)):
  1863. alexprints(textlines + nl)
  1864. x = 1
  1865. elif sys.argv[1] == "--arrdo":
  1866. try: now = sys.argv[2]
  1867. except: now = ""
  1868. linec = 1
  1869. if len(now):
  1870. for textlines in alexarrstdin():
  1871. s = alexshell(arrdostart + now + " " + textlines)
  1872. x = 1
  1873. elif sys.argv[1] == "--addquotes":
  1874. quot = chr(34)
  1875. if 1:
  1876. for textlines in alexarrstdin():
  1877. alexprints(quot + textlines.strip() + quot + nl)
  1878. x = 1
  1879. elif sys.argv[1] == "--var":
  1880. for textlines in alexarrstdin():
  1881. alexprint(textlines)
  1882. x = 1
  1883. elif sys.argv[1] == "--fsortplus":
  1884. b = []
  1885. for p in alexarrstdin():
  1886. try: fs = int(os.path.getsize(p))
  1887. except: fs = -1
  1888. try: s256 = sha256(open(p).read()).hexdigest()
  1889. except: s256 = "-" * 64
  1890. try: filetime = str(datetime.fromtimestamp(os.path.getmtime(p)))[0:19]
  1891. except: filetime = -1
  1892. try: b += [(fs, s256, filetime, p)]
  1893. except: b += [(0, "problem", "with", "fsortplus")]
  1894. b.sort()
  1895. for p in b:
  1896. tab = chr(9)
  1897. try: print str(p[0]) + " " + p[1] + " " + p[2] + tab + p[3]
  1898. except: print "-1" + chr(32) + "-" * 64 + chr(32) + "?" + chr(32) + "?" + chr(32) + p[3]
  1899. x = 1
  1900. elif sys.argv[1] == "--isoplus":
  1901. fr = sys.argv[2]
  1902. tab = chr(9)
  1903. for p in alexarrstdin():
  1904. try:
  1905. f = alexopen(p, "r")
  1906. except:
  1907. f = 0
  1908. while 1:
  1909. try:
  1910. textlines = alexflineinput(p)
  1911. except:
  1912. textlines = ""
  1913. if textlines == chr(10):
  1914. break
  1915. if fr.lower() in textlines.lower():
  1916. print p + tab + textlines
  1917. try:
  1918. f = alexclose(p)
  1919. except:
  1920. f = 0
  1921. x = 1
  1922. elif sys.argv[1] == "--noreps":
  1923. b = {}
  1924. for textlines in alexarrstdin():
  1925. try:
  1926. t = b[textlines]
  1927. except:
  1928. print textlines
  1929. b[textlines] = 0
  1930. x = 1
  1931. elif sys.argv[1] == "--fields":
  1932. try:
  1933. now = sys.argv[2:]
  1934. except:
  1935. now = ""
  1936. if len(now):
  1937. for textlines in alexarrstdin():
  1938. count = 0
  1939. for p in now:
  1940. count += 1
  1941. if p == "_":
  1942. alexprints(textlines)
  1943. else:
  1944. try:
  1945. alexprints(textlines.split()[int(p) - 1])
  1946. except:
  1947. alexprints("~")
  1948. if count < len(now):
  1949. alexprints(" ")
  1950. print ""
  1951. x = 1
  1952. except: p = 0
  1953. if x > 0: sys.exit()
  1954. alexhist = os.path.join(os.path.expanduser("~"), ".alex_history")
  1955. try: f = open(alexhist).read()
  1956. except:
  1957. try:
  1958. f = open(alexhist, "w")
  1959. f.write("")
  1960. f.close()
  1961. f = open(alexhist).read()
  1962. except: print "unable to open history file"
  1963. def alexq(alexhist):
  1964. try: readline.write_history_file(alexhist)
  1965. except: print "couldnt write history file"
  1966. quit()
  1967. pcmds = ["cd", "while", "set", "setrandint", "setinput", "next", "break", "setadd", "setnum", "forin"] # take priority no matter what files are found
  1968. ntlocalcmds = ["dir", "echo"]
  1969. def alexparse(p):
  1970. quot = chr(34)
  1971. inquotes = 0
  1972. buf = ""
  1973. tokencount = 0
  1974. out = ["",]
  1975. for t in p:
  1976. # handle quote mark
  1977. if t == quot:
  1978. if inquotes: out[tokencount] += t ; inquotes = 0
  1979. else: tokencount += 1 ; out += [""] ; out[tokencount] = t ; inquotes = 1
  1980. # handle spaces outside quotes
  1981. elif inquotes == 0 and t == " ":
  1982. tokencount += 1 ; out += [""]
  1983. # handle pipes outside quotes
  1984. elif inquotes == 0 and t == "|":
  1985. tokencount += 1 ; out += ["|"] ; tokencount += 1 ; out += [""]
  1986. # handle semicolons outside quotes
  1987. elif inquotes == 0 and t == ";":
  1988. tokencount += 1 ; out += [";"] ; tokencount += 1 ; out += [""]
  1989. elif inquotes == 0 and t == "$":
  1990. tokencount += 1 ; out += [""] ; out[tokencount] = t
  1991. elif inquotes == 0 and t == ";":
  1992. tokencount += 1 ; out += [";"] ; tokencount += 1 ; out += [""]
  1993. # handle everything else
  1994. else: out[tokencount] += t
  1995. if len(" ".join(out).strip()): out += [";"]
  1996. while "" in out: out.remove("")
  1997. if inquotes: return 0
  1998. else: return out
  1999. def alexesc(p, anglebr):
  2000. p = p.replace(chr(92), chr(92) + chr(92))
  2001. p = p.replace(chr(34), chr(92) + chr(34))
  2002. p = p.replace(chr(38), chr(92) + chr(38))
  2003. if anglebr:
  2004. p = p.replace("<", chr(92) + "<") # allow redirection
  2005. p = p.replace(">", chr(92) + ">") # if 0
  2006. p = p.replace("(", chr(92) + "(")
  2007. p = p.replace(")", chr(92) + ")")
  2008. return p
  2009. def alexqesc(p):
  2010. b = chr(34) + alexesc(p, 0) + chr(34)
  2011. p = ""
  2012. normal = 1
  2013. for t in b:
  2014. if t == "$" and normal: normal = 0 ; extra = "$"
  2015. elif normal: p += t
  2016. elif t in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_": extra += t
  2017. else:
  2018. normal = 1
  2019. if alexosname == "nt":
  2020. if alexvalidrefvar(extra): p += chr(34) + " + alexesc(alexprocstring(" + extra[1:] + "), 1) + " + chr(34) + t
  2021. else: p = 0
  2022. else:
  2023. if alexvalidrefvar(extra): p += chr(92) + chr(34) + chr(34) + " + alexesc(alexprocstring(" + extra[1:] + "), 1) + " + chr(34) + chr(92) + chr(34) + t
  2024. else: p = 0
  2025. return p
  2026. def alexprocstring(p):
  2027. global nl
  2028. if type(p) == list:
  2029. return alexjoin(p, nl)
  2030. else: return str(p)
  2031. def alexvalidvar(p):
  2032. try: p4 = p[0:4]
  2033. except: p4 = ""
  2034. try:
  2035. if p.lower()[0] in "abcdefghijklmnopqrstuvwxyz" and p.lower() != "var" and p4.lower() != "alex":
  2036. for t in p:
  2037. if t.lower() not in "abcdefghijklmnopqrstuvwxyz_1234567890": return 0
  2038. return 1
  2039. except: return 0
  2040. return 0
  2041. def alexvalidrefvar(p):
  2042. if len(p) > 1:
  2043. if p[0] == "$":
  2044. p = p[1:]
  2045. else: return 0
  2046. try: p4 = p[0:4]
  2047. except: p4 = ""
  2048. try:
  2049. if p.lower()[0] in "abcdefghijklmnopqrstuvwxyz" and p.lower() != "var" and p4.lower() != "alex":
  2050. for t in p:
  2051. if t.lower() not in "abcdefghijklmnopqrstuvwxyz_1234567890": return 0
  2052. return 1
  2053. except: return 0
  2054. return 0
  2055. csc = 0
  2056. buf = []
  2057. bufpos = 0
  2058. agg = ""
  2059. prevtoken = ""
  2060. ptoken = ""
  2061. outprog = ""
  2062. runvar = 0
  2063. invflag = 0
  2064. while 1:
  2065. p = ""
  2066. bufpos += 1
  2067. arrpath = os.getenv('PATH').split(os.pathsep)
  2068. if bufpos > len(buf): exec(outprog) ; outprog = "" ; indent = 0
  2069. if sc and bufpos > len(buf):
  2070. bufpos = 1
  2071. csc += 1
  2072. try: p = alexparse(sc[csc-1]) ; prevtoken = ""
  2073. except: p = ["quit"]
  2074. elif bufpos > len(buf):
  2075. bufpos = 1
  2076. getuser = os.getenv("LOGNAME")
  2077. if getuser == None: getuser = os.getenv("USER")
  2078. if getuser == None: getuser = ""
  2079. alexcolourtext(7)
  2080. if getuser == "root":
  2081. alexcolourtext(12) ; alexprints("root") ; alexcolourtext(7) ; alexprints(":") ; userp = "#> "
  2082. elif getuser: alexprints(getuser + ":") ; userp = "$> "
  2083. if alexosname == "nt": userp = "> "
  2084. alexcolourtext(9) ; alexprints(os.getcwd()) ; alexcolourtext(7) ; alexprints(userp)
  2085. try: p = alexparse(alexlineinput("")) ; prevtoken = ""
  2086. except EOFError: p=["quit"] ; print "exit"
  2087. except KeyboardInterrupt: print "^C" ; buf = [] ; agg = "" ; outprog = "" ; indent = 0 ; p = ""
  2088. except: p = [""]
  2089. if p == 0: print "a quoted string was not end-quoted; syntax is invalid" ; p = []
  2090. if len(p): buf = p
  2091. try: token = buf[bufpos - 1]
  2092. except: token = ""
  2093. if token in pcmds and bufpos == 1: ptoken = token
  2094. ntreplace = 1
  2095. if alexosname == "nt":
  2096. pyprefix = "c:\python27\python "
  2097. if token == "find" and (bufpos == 1 or prevtoken == "|"): token = "dir /b /a /s"
  2098. elif token == "clear" and (bufpos == 1 or prevtoken == "|"): token = "cls"
  2099. elif token == "ucase" and (bufpos == 1 or prevtoken == "|"): token = pyprefix + alexver + ".py --ucase"
  2100. else: ntreplace = 0
  2101. arrpath += [os.getcwd()]
  2102. pfound = 1
  2103. elif bufpos == 1 or prevtoken == "|":
  2104. ntreplace = 0
  2105. pyprefix = ""
  2106. ntlocalcmds = []
  2107. try: pfound = int(len(alexarrshell("which " + token + " 2> /dev/null")) > 1) ; ##print "TOKEN("+token+")"
  2108. except: pfound = 0 ; token = ""
  2109. x = 0
  2110. if len(token) or len(ptoken):
  2111. if runvar and alexvalidvar(token): alexvar = token ; x = 1
  2112. if runvar: runvar = 2
  2113. if len(ptoken) and bufpos == 1: argu = [] ; x = 1
  2114. elif token in ["quit", "exit"] and bufpos == 1: alexq(alexhist)
  2115. elif token in ["help", "pserver", "isoname", "minusname", "locate", "colour", "line", "pset", "tops", "bots",
  2116. "cat", "arrdo", "fsortplus", "fields", "replace", "rainbow", "ascii", "isoplus", "noreps", "sleep", "dc", "findsim", "lcase", "ucase", "chr", "arrcurl"] and (bufpos == 1 or prevtoken == "|"): token = pyprefix + alexver + ".py --" + token ; x = 1
  2117. elif token in ["var"] and prevtoken == "|": token = pyprefix + alexver + ".py --" + token ; x = 1 ; runvar = 1 ; alexvar = ""
  2118. if len(buf) and (bufpos == 1 or prevtoken == "|"): lfor = [token,]
  2119. if len(buf) > 0 and alexosname == "nt" and (bufpos == 1 or prevtoken == "|"):
  2120. lfor += [token + ".exe", token + ".bat"]
  2121. for plfor in lfor:
  2122. for ap in arrpath:
  2123. if plfor in os.listdir(ap) and x == 0:
  2124. token = ap + os.sep + plfor ; x = 1 ; break
  2125. if os.path.isfile(plfor) and x == 0: token = plfor ; x = 1 ; break
  2126. if x == 0 and lfor[0] in ntlocalcmds:
  2127. token = lfor[0] ; x = 1
  2128. elif ntreplace > 0 and x == 0:
  2129. x = 1
  2130. elif len(buf) > 0 and pfound == 1 and x == 0 and (bufpos == 1 or prevtoken == "|"):
  2131. x = 1
  2132. # invalid command
  2133. if len(buf) > 0 and invflag == 0 and x == 0 and (bufpos == 1 or prevtoken == "|"): print "type the word \"help\" or a question mark and hit enter for a list of commands" ; token = "" ; bufpos = buf.index(";", bufpos-1) ; agg = "" ; invflag = 1
  2134. # invalid variable
  2135. if len(buf) > 0 and runvar == 2 and alexvar == "": print "type the word \"help\" or a question mark and hit enter for a list of commands" ; token = "" ; bufpos = buf.index(";", bufpos-1) ; agg = "" ; runvar = 0
  2136. if x == 1 and bufpos == 1: agg = token
  2137. prevtoken = token
  2138. if token != ";" and bufpos > 1:
  2139. if ptoken:
  2140. argu += [token] ; x = 0
  2141. else:
  2142. agg += " " + token ; x = 0
  2143. if token == ";" and bufpos > 1:
  2144. if ptoken:
  2145. outprog += ptrans(ptoken, argu, alexhist, nl) ; argu = [] ; ptoken = "" ; prevtoken = ""
  2146. elif runvar:
  2147. if alexvar:
  2148. outprog += chr(32) * alexatleast(0, indent) + alexvar + " = alexvarproc(alexarrshell(" + alexqesc(agg) + "))" + nl ; agg = "" ; prevtoken = "" ; runvar = 0
  2149. else:
  2150. outprog += chr(32) * alexatleast(0, indent) + "alexr = alexshell(" + alexqesc(agg) + ")" + nl ; agg = "" ; prevtoken = ""
  2151. buf = buf[bufpos:] ; bufpos = 0