alex26.py 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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.6, jan 2019 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 alexfws(p):
  525. c = ""
  526. for t in p:
  527. if ord(t) > 32 and ord(t) < 127:
  528. c += t
  529. return c
  530. def alexchr(p):
  531. if type(p) == str:
  532. if len(p) > 0:
  533. return p[0]
  534. return chr(p)
  535. def alexprints(p): stdout.write(str(p)) ; sys.stdout.flush()
  536. def alexleft(p, s): return p[:s]
  537. def alexmid(p, s, x):
  538. arr = 0
  539. if type(p) == list or type(p) == tuple: arr = 1
  540. rt = p[s - 1:x + s - 1]
  541. if arr and len(rt) == 1: rt = rt[0]
  542. return rt
  543. def alextops(p, s):
  544. if type(p) == list:
  545. return p[:s]
  546. else:
  547. return p
  548. def alexbots(p, s):
  549. if type(p) == list:
  550. return p[-s:]
  551. else:
  552. return p
  553. def alexright(p, s): return p[-s:]
  554. def alexrandint(s, f):
  555. return randint(int(s), int(f))
  556. def alexlcase(p): return p.lower()
  557. def alexucase(p): return p.upper()
  558. def alexint(p): return int(p)
  559. def alexarrset(x, p, s):
  560. if 1:
  561. #if type(p) == str: p = p + s # str(s) if you want it easier
  562. if 1: #type(p) == list:
  563. if type(s) == tuple:
  564. if len(s) == 1: fas = s[0]
  565. elif type(s) == list:
  566. if len(s) == 1: fas = s[0]
  567. else:
  568. fas = s
  569. x[p - 1] = s
  570. def alexopen(x, s):
  571. import fileinput
  572. if s.lower() == "w":
  573. if (x) not in alexfilehandles.keys():
  574. alexfilehandles[x] = open(x[:], s.lower())
  575. elif s.lower() == "r":
  576. if (x) not in alexfilehandles.keys():
  577. alexfilehandles[x] = fileinput.input(x[:])
  578. alexfilecounters[x] = 0
  579. else:
  580. if (x) not in alexfilehandles.keys(): alexfilehandles[x] = open(x[:], s[:])
  581. def alexfprint(x, s):
  582. fon = alexosname
  583. sep = chr(10)
  584. if fon == "nt": sep = chr(13) + chr(10)
  585. alexfilehandles[s].write(str(x) + sep)
  586. def alexflineinput(s):
  587. try:
  588. p = alexfilehandles[s][alexfilecounters[s]].replace(chr(13),
  589. "").replace(chr(10), "")
  590. alexfilecounters[s] += 1
  591. except:
  592. p = chr(10)
  593. return p
  594. def alexclose(x):
  595. if (x) in alexfilehandles.keys():
  596. alexfilehandles[x].close() ; del alexfilehandles[x]
  597. try: del alexfilecounters[x]
  598. except: pass
  599. #def alexcls(x):
  600. # if alexosname == "nt": cls = alexsh("cls")
  601. # else: stdout.write("\x1b[2J\x1b[1;1H") ; sys.stdout.flush()
  602. def alexarropen(s):
  603. x = open(s).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13),
  604. chr(10)).split(chr(10))
  605. return x[:]
  606. def alexarrcurl(x, s):
  607. from urllib import urlopen
  608. if x.lower() == "-a":
  609. try:
  610. c = str(urlopen(s).read()) ; c = c.replace(chr(13) + chr(10),
  611. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  612. except:
  613. c = [""]
  614. else:
  615. try:
  616. c = str(urlopen(s).read())
  617. except:
  618. c = ""
  619. return c[:]
  620. def alexdequote(p):
  621. if len(p) > 2:
  622. if p[0] == chr(34) and p[-1] == chr(34):
  623. p = p[1:]
  624. p = p[:-1]
  625. return p
  626. def alexarrstdin():
  627. ps = []
  628. for textlines in stdin:
  629. p = textlines[:]
  630. if p:
  631. if p[-1] != chr(10):
  632. p += chr(10)
  633. if alexosname == "nt":
  634. ps += [alexdequote(p[:-1].strip())]
  635. else:
  636. ps += [p[:-1]]
  637. return ps[:]
  638. def alexarrget(p, s):
  639. if 1:
  640. return p[s - 1]
  641. def alexplus(p, s):
  642. if type(p) in (float, int):
  643. if type(s) in (float, int):
  644. p = p + s
  645. else:
  646. p = p + s # float(s) if you want it easier
  647. if p == float(int(p)): p = int(p)
  648. else:
  649. if type(p) == str: p = p + s # str(s) if you want it easier
  650. if type(p) == list:
  651. if type(s) == tuple:
  652. p = p + list(s)
  653. elif type(s) == list:
  654. p = p + s[:]
  655. else:
  656. p = p + [s]
  657. if type(p) == tuple:
  658. if type(s) == tuple:
  659. p = tuple(list(p) + list(s))
  660. elif type(s) == list:
  661. p = tuple(list(p) + s[:])
  662. else:
  663. p = tuple(list(p) + [s])
  664. return p
  665. def alexpadleft(p, s, c=7):
  666. if type(c) == int:
  667. c = (c, 0)
  668. else:
  669. c = c.split()
  670. return alexcolortext(int(c[0]), int(c[1])) + (" " * s + str(p))[-s:] + alexcolortext(7, 0)
  671. def alexpadright(p, s, c="5 0"):
  672. if type(c) == int:
  673. c = (c, 0)
  674. else:
  675. c = c.split()
  676. return alexcolortext(int(c[0]), int(c[1])) + (str(p) + " " * s)[:s] + alexcolortext(7, 0)
  677. def alexprintlinedc(p, s):
  678. try:
  679. searchdate = p.split()[0]
  680. printline = int(s and s in searchdate)
  681. return printline
  682. except:
  683. return 1
  684. def alexcolourdc(c, f="p"):
  685. if type(c) != int: c = c.lower()
  686. if c in [0]:
  687. return "9 0"
  688. cb = "0"
  689. if len(f.split()) > 1:
  690. cb = "1"
  691. if c in [2, ".bat", ".com", ".exe"]:
  692. return "10 " + cb
  693. if c in [1, ".lnk"]:
  694. return "11 " + cb
  695. if c in [".tgz", ".tar.gz", ".gz", ".zip", ".7z"]:
  696. return "4 " + cb
  697. if c in [".fig", ".alex", ".py", ".fig.py", ".rose"]:
  698. return "2 " + cb
  699. if c in [".ogg", ".mp4", ".mpeg", ".mpg", ".mp3", ".webm", ".odt", ".odp", ".pdf"]:
  700. return "3 " + cb
  701. if c in ["."]:
  702. return "8 " + cb
  703. 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"]:
  704. return "13 " + cb
  705. if c in [".htm", ".html", ".txt", ".js", ".php"]:
  706. return "14 " + cb
  707. return "7 " + cb
  708. def alexjoin(x, s):
  709. t = ""
  710. if len(x) : t = str(x[0])
  711. for c in range(len(x)):
  712. if c > 0: t += str(s) + str(x[c])
  713. return t # s.join(x)
  714. def alexarr(p):
  715. if type(p) in (float, int, str):
  716. p = [p]
  717. else:
  718. p = list(p)
  719. return p
  720. def alexsplit(x, s):
  721. return x.split(s)
  722. def alexval(n):
  723. n = float(n)
  724. if float(int(n)) == float(n): n = int(n)
  725. return n
  726. def alextimes(p, s):
  727. if type(p) in (float, int):
  728. p = p * s # float(s) if you want it easier
  729. if p == float(int(p)): p = int(p)
  730. else:
  731. if type(p) == list:
  732. p = p[:] * s # alexval(s)
  733. else:
  734. p = p * s # alexval(s) if you want it easer
  735. return p
  736. def alexdivby(p, s):
  737. p = float(p) / s
  738. if p == float(int(p)): p = int(p)
  739. return p
  740. def alexminus(p, s): return p - s
  741. def alextopwr(p, s):
  742. p = p ** s
  743. if p == float(int(p)): p = int(p)
  744. return p
  745. def alexmod(p, s):
  746. return p % s
  747. def alexcos(p):
  748. from math import cos ; p = cos(p)
  749. if p == float(int(p)): p = int(p)
  750. return p
  751. def alexsin(p):
  752. from math import sin ; p = sin(p)
  753. if p == float(int(p)): p = int(p)
  754. return p
  755. def alexsqr(p):
  756. from math import sqrt ; p = sqrt(p)
  757. if p == float(int(p)): p = int(p)
  758. return p
  759. def alexltrim(p): return p.lstrip()
  760. def alexlineinput(p):
  761. p = raw_input()
  762. if alexosname == "nt":
  763. p = alexdequote(p.strip())
  764. return p
  765. def alexlen(p): return len(p)
  766. def alexasc(p): return ord(p[0])
  767. def alexatn(p):
  768. from math import atan ; p = atan(p)
  769. if p == float(int(p)): p = int(p)
  770. return p
  771. def alexhex(p): return hex(p)
  772. def alexrtrim(p): return p.rstrip()
  773. def alexstring(x, p, n):
  774. if type(n) == str: return n * p
  775. return chr(n) * p
  776. def alextimer(p):
  777. from time import strftime
  778. return int(strftime("%H"))*60*60+int(strftime("%M"))*60+int(strftime("%S"))
  779. def alextime(p): from time import strftime ; return strftime("%H:%M:%S")
  780. def alexdate(p): from time import strftime ; return strftime("%m/%d/%Y")
  781. def alexcommand(p): return alexargv[1:]
  782. def alextan(p):
  783. from math import tan ; p = tan(p)
  784. if p == float(int(p)): p = int(p)
  785. return p
  786. def alexoct(p): return oct(p)
  787. def alexsleep(s):
  788. sleep(s)
  789. def alexarrsort(p):
  790. p.sort()
  791. def alexdisplay(x):
  792. global alexraphics, alexrupd
  793. alexrupd = 0
  794. if alexraphics == 1:
  795. pygame.display.update()
  796. def alexreverse(p):
  797. if type(p) == list: p.reverse() ; return p
  798. elif type(p) == str:
  799. p = map(str, p) ; p.reverse()
  800. p = "".join(p)
  801. return p
  802. def alexarreverse(p):
  803. p.reverse()
  804. def alexfunction(p, s): return p
  805. def alexend(x): quit()
  806. def alexif(p, s): return p
  807. def alexthen(p, s): return p
  808. def alexsystem(x): quit()
  809. demo = """
  810. p 7 arr times 5
  811. x "hello, world!"
  812. x 5
  813. x 5 times 7
  814. x 3 plus 5 times 7
  815. abs
  816. z x abs
  817. x z str asc abs int
  818. c command print
  819. p print
  820. p print arrset 2 8 print
  821. z join p "(_)" print
  822. x z print
  823. p print end
  824. function add5 r
  825. x get r plus 5 return x
  826. fig
  827. function ppp
  828. z 32 chr print
  829. for p 1 100 1
  830. x randint 0 3
  831. y randint 0 3
  832. c randint 1 9
  833. c colourtext 7
  834. next
  835. fig
  836. z ppp
  837. z sleep 1
  838. z ppp sleep 2 z ppp
  839. """
  840. p = ""
  841. try: p = right(sys.argv, 1)[0]
  842. except: pass
  843. if not ".fig" in p.lower():
  844. if p.lower() == "help":
  845. stdout.write("\n type (any) part of the command you want help on." +
  846. "\n\n alex will show all matches.\n\n\n ")
  847. helpf = chelp(raw_input())
  848. if not helpf: print(colour(14,0)+"\n no commands match your search.") ; print("")
  849. #colour(7,0)
  850. quit()
  851. else:
  852. p = "demo.fig"
  853. inputfile = demo.replace(chr(13), "").split("\n")
  854. else:
  855. try:
  856. inputfile = open(p).read().replace(chr(13) + chr(10),
  857. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  858. except: p = 0
  859. #try: outfile = open(p + ".py", "w")
  860. #except: print "couldn't write to \"" + p + ".py" "\", exiting." ; print ; quit()
  861. outname = ".py"
  862. inputfile = ""
  863. flen = len(str(len(inputfile)))
  864. linecount = 0
  865. indent = 0
  866. inlinep = 0
  867. inle = 0
  868. errorin = 0
  869. errorsrc = ""
  870. error = ""
  871. mode = 0
  872. figraphics = -1 # -1 = uninitialised, 0 = textmode, 1 = initialised
  873. vrs = []
  874. vr = ""
  875. outb = []
  876. ingfx = 0
  877. linesoutc = 0
  878. for p in inputfile:
  879. linecount += 1 ; vrop = 0 ; vrcl = 0
  880. #if linecount == 1:
  881. #outfile.write("#!/usr/bin/env python" + "\n# encoding: utf-8\n")
  882. #if "," in proginf:
  883. #outfile.write("# alex translator version: " + proginf.split(",")[0] + "\n")
  884. if inlinep:
  885. if p.lower().strip() == "fig":
  886. inlinep = 0
  887. #print lc() + p
  888. indent = alexatleast(0, indent - 4)
  889. else:
  890. #print lc() + colour(2, None) + p + colour(7, None)
  891. #% write copied lines of inline python
  892. outfilewrite(outb, chr(32) * alexatleast(0, indent - 4) +
  893. leftfour(p) + "\n")
  894. elif mode == "output the following:":
  895. if p.lower().strip() == "display":
  896. mode = 0
  897. #print lc() + p
  898. else:
  899. wr(chr(32) * alexatleast(0, indent) + "print \"" + p.replace(chr(34),
  900. "\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
  901. #print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
  902. #"\" + chr(92) + \"")
  903. elif mode == 0:
  904. x = alexfsplit(p.lstrip())
  905. lp = p.lower()
  906. if not len(p):
  907. #print lc() + ""
  908. #% write copied blank lines from inline python
  909. outfilewrite(outb, "\n")
  910. if len(p.lstrip()):
  911. e = 0
  912. if p.lstrip()[0] == "#":
  913. if linecount == 1:
  914. es = 0
  915. try:
  916. if p.lstrip()[1] == "!": es = 1
  917. except: es = 0
  918. if not es:
  919. wr(p)
  920. #print lc(), alexfsp(p)
  921. #else: print lc() + "[this first comment isn't copied over]"
  922. es = 0
  923. else:
  924. #% write comments
  925. #print colour(14, 0) + p + colour(7,0) ; znul = raw_input() #$
  926. outfilewrite(outb, chr(32) * alexatleast(0, indent) + p + "\n")
  927. #print lc(), alexfsp(p)
  928. elif lnob(x, 0) == "figg":
  929. e = 2
  930. elif lp.rstrip() == "python":
  931. indent += 4
  932. inlinep = 1
  933. #print lc() + p
  934. else:
  935. if not lnob(x, 0) == "figg":
  936. if lnob(x, 0) != "fig" and not lnob(x,
  937. 0) in cmds.keys() and not lnob(x,
  938. 0) in funcs.keys() + ["forin", "for", "function", "nextin",
  939. "next", "while", "wend"] + ["break", "pass"]:
  940. if not lnob(x, 0) in vrs: vrs += [lnob(x, 0)[:]] # main vars, also func params, etc
  941. #% write variable
  942. #var: print colour(14, 0) + "variable:" + lnob(x, 0) + colour(7,0) ; znul = raw_input() #$
  943. outfilewrite(outb, "\n")
  944. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  945. "figlist = 0\n")
  946. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  947. "try: alexlist = int(type(" + lnob(x, 0) + ") == list)\n")
  948. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  949. "except NameError: pass\n")
  950. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  951. "if not alexlist: " + lnob(x, 0) + " = 0 \n")
  952. if lnob(x, 0) == "fig":
  953. #print lc () + p
  954. #% write? its whitespace
  955. indent = alexatleast(0, indent - 4)
  956. if lnob(x, 0) == "wend":
  957. #print lc () + p
  958. #% write? its whitespace
  959. indent = alexatleast(0, indent - 4)
  960. if lnob(x, 0) == "next":
  961. #print lc () + p
  962. #% write? its whitespace
  963. indent = alexatleast(0, indent - 4)
  964. if lnob(x, 0) == "nextin":
  965. #print lc () + p
  966. #% write? its whitespace
  967. indent = alexatleast(0, indent - 4)
  968. if lnob(x, 0) == "try":
  969. #print lc () + p
  970. #% write try line
  971. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "try:\n")
  972. indent = alexatleast(0, indent + 4)
  973. if lnob(x, 0) == "else":
  974. #print lc () + p
  975. #% write else line
  976. outfilewrite(outb, chr(32) * alexatleast(0, indent - 4) +
  977. "else:\n")
  978. if lnob(x, 0) == "except":
  979. #print lc () + p
  980. indent = alexatleast(0, indent - 4)
  981. #% write except line
  982. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  983. "except:\n")
  984. indent = alexatleast(0, indent + 4)
  985. if lnob(x, 0) == "resume":
  986. #print lc () + p
  987. #% write? its whitespace
  988. #$
  989. indent = alexatleast(0, indent - 4)
  990. if lnob(x, 0) == "while":
  991. #print lc () + p
  992. #% write simple loop
  993. #$
  994. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  995. "while 1:\n")
  996. indent += 4
  997. if lnob(x, 0) == "function" and len(getmore(x, 1)) > 0:
  998. #print lc () + p
  999. mkf = []
  1000. funcname = getlmore(x, 1)[0]
  1001. prm = 1
  1002. while 1:
  1003. try:
  1004. aprm = getlmore(x, 1)[prm]
  1005. if len(aprm):
  1006. if aprm[0] != "#":
  1007. mkf += [aprm]
  1008. if aprm not in vrs: vrs += [aprm[:]]
  1009. prm += 1
  1010. except: break
  1011. ufunc[funcname] = mkf[:] #; print ufunc # #
  1012. #print ufunc
  1013. #print len(ufunc[funcname])
  1014. #% write func def
  1015. #$ print colour(14,0)+ "def " + funcname + "(" + ", ".join(mkf) + "):" + colour(7,0)
  1016. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "def " +
  1017. funcname + "(" + ", ".join(mkf) + "):\n")
  1018. indent += 4
  1019. if lnob(x, 0) == "for" and len(getmore(x, 1)) == 4:
  1020. #print lc () + p
  1021. gmro = getlmore(x, 1)[0]
  1022. gmrt = getlmore(x, 1)[1]
  1023. gmrh = getlmore(x, 1)[2]
  1024. gmrf = getlmore(x, 1)[3]
  1025. if gmro not in vrs: vrs += [gmro[:]]
  1026. if "." not in gmrf and (gmrf.strip()) not in ("0",
  1027. "0.0", "-0") and "." not in gmrt and "." not in gmrh:
  1028. #% write standard for loop
  1029. #$
  1030. outfilewrite(outb, chr(32) * alexatleast(0, indent)
  1031. + "for "
  1032. + gmro + " in range(int(float(" + gmrt +
  1033. ")), int(float(" + gmrh + ")) + alexsgn(" + gmrf +
  1034. "), alexnonz(int(float(" + gmrf + ")))):\n")
  1035. else:
  1036. #% write for loop that allows floating step
  1037. #$
  1038. outfilewrite(outb, chr(32) * alexatleast(0, indent) + gmro
  1039. + " = float(" + gmrt + ") - float(" + gmrf + ")\n" +
  1040. chr(32) * alexatleast(0, indent) + "while 1:\n" + chr(32) *
  1041. alexatleast(0, indent + 4) + gmro + " += float(" + gmrf +
  1042. ")\n" + chr(32) * alexatleast(0, indent + 4) + "if " +
  1043. gmrf +
  1044. " > 0 and " + gmro + " > float(" + gmrh + "): break\n"
  1045. + chr(32) * alexatleast(0, indent + 4) + "elif " + gmrf +
  1046. " <= 0 and " + gmro +
  1047. " < float(" + gmrh + "): break\n")
  1048. indent += 4
  1049. if lnob(x, 0) == "forin" and len(getmore(x, 1)) == 2:
  1050. #print lc () + p
  1051. gmro = getlmore(x, 1)[0]
  1052. gmrt = getlmore(x, 1)[1]
  1053. if gmro not in vrs: vrs += [gmro[:]]
  1054. #% write forin command with params
  1055. #$
  1056. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "for " +
  1057. gmro + " in " + gmrt + ":\n")
  1058. indent += 4
  1059. if lnob(x, 0) == "break":
  1060. #print lc () + p
  1061. #% write break command
  1062. #$ print
  1063. outfilewrite(outb, chr(32) *
  1064. alexatleast(0, indent) + "break\n")
  1065. if lp.rstrip() == "graphics":
  1066. ingfx = 1
  1067. #% write change to default mode (dont suppress gfx)
  1068. #$
  1069. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1070. "figraphics = -1\n")
  1071. alexraphics = -1
  1072. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1073. "figpset(0, -1, -1, 0)\n")
  1074. #print lc () + p
  1075. if lnob(x, 0) == "textmode":
  1076. #print lc () + p
  1077. addto[3] = 1
  1078. #% write change to text mode (suppress graphics)
  1079. #$
  1080. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1081. "figraphics = 0\n")
  1082. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1083. "stopgraphics()\n")
  1084. alexraphics = 0
  1085. if lnob(x, 0) == "pass":
  1086. #print lc () + p
  1087. #% write pass command
  1088. #$ print
  1089. outfilewrite(outb, chr(32) *
  1090. alexatleast(0, indent) + "pass\n")
  1091. if lnob(x, 0) == "iftrue":
  1092. #print lc () + p
  1093. #% write iftrue
  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) + ":\n") ; indent += 4
  1097. if lnob(x, 0) == "ifequal" and len(getmore(x, 1)) == 2:
  1098. #print lc () + p
  1099. #% write ifequal
  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) == "ifless" and len(getmore(x, 1)) == 2:
  1104. #print lc () + p
  1105. #% write ifless
  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) == "ifmore" and len(getmore(x, 1)) == 2:
  1110. #print lc () + p
  1111. #% write ifmore
  1112. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1113. outfilewrite(outb, chr(32) * alexatleast(0, indent) + "if " +
  1114. snob(x, 1) + " > " + snob(x, 2) + ":\n") ; indent += 4
  1115. if lnob(x, 0) in cmds.keys(): # + ufunc.keys():
  1116. e = 4 ; shln = lnob(x, 0)
  1117. if lnob(x, 0) != "fig" and lnob(x,
  1118. 0) not in funcs.keys() + ["forin", "for", "function",
  1119. "nextin", "next", "while", "wend"] + ["break", "pass"]:
  1120. #print lc () + p
  1121. vr = lnob(x, 0)
  1122. #print vr, type(vr)
  1123. #print getlmore(x, 1)
  1124. prsc = 0
  1125. cstrctr = 0
  1126. csbuf = []
  1127. vrcs = ""
  1128. for prs in getlmore(x, 1):
  1129. #$ print prs
  1130. if "fig" in prs:
  1131. if prs[:3] == "fig": e = 2 ; break ; break
  1132. if prs in funcs.keys():
  1133. e = 3 ; shln = prs
  1134. prsc += 1
  1135. if cstrctr > 0:
  1136. vrcs += prs
  1137. cstrctr -= 1
  1138. if cstrctr == 0:
  1139. if lnob(x, prsc - 1) == "return":
  1140. #% write return command
  1141. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1142. outfilewrite(outb, chr(32) * alexatleast(0,
  1143. indent) + vrcs)
  1144. elif lnob(x, prsc - 2) == "swap":
  1145. vrcs = lnob(x, prsc - 1) + ", " + lnob(x,
  1146. prsc - 0) + " = " + lnob(x,
  1147. prsc - 0) + ", " + lnob(x, prsc - 1)
  1148. #% write swap of 2 vars in python syntax
  1149. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1150. outfilewrite(outb, chr(32) * alexatleast(0,
  1151. indent) + vrcs + " ; ")
  1152. else:
  1153. if alexprsbac in ufunc.keys():
  1154. #% write parametered func call
  1155. #$ print colour(14,0)+ vrcs + ") ; " + lnob(x, 0) + " = alexnone(" + lnob(x, 0) + ", alexbac) ; " +colour(7,0)
  1156. outfilewrite(outb, chr(32) * alexatleast(0,
  1157. indent) + vrcs + ") ; " + lnob(x, 0) +
  1158. " = alexnone(" + lnob(x, 0) +
  1159. ", alexbac) ; ") ; vrcl += 1
  1160. else:
  1161. #% write builtin func call assignment
  1162. #$ print colour(14,0)+ vr + " = " + vrcs + ") ; " +colour(7,0)
  1163. outfilewrite(outb, chr(32) *
  1164. alexatleast(0, indent) + vrcs + ") ; ") ; vrcl += 1
  1165. else:
  1166. vrcs += ", " #; print "*"
  1167. #if
  1168. continue
  1169. if prs.lower() in funcs.keys() + ["forin", "for",
  1170. "function", "nextin", "next", "while",
  1171. "wend"] + ["break", "pass"]:
  1172. e = 3
  1173. alexprsbac = None
  1174. if cstrctr == 0:
  1175. if not prs.lower() in vrs:
  1176. if prs.lower()[0] in "abcdefghijklmnopqrstuvwxyz":
  1177. if not prs.lower() in ufunc.keys():
  1178. if not prs.lower() in funcs.keys():
  1179. if not prs.lower() in cmds.keys():
  1180. e = 5 ; shln = prs
  1181. if prs.lower() in vrs and cstrctr == 0:
  1182. #and len(getmore(x, 1)) == 1:
  1183. #% write lefthand variable assignment
  1184. #$ print colour(14,0)+ vr + " = " + prs.lower() +colour(7,0)
  1185. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1186. vr + " = " + prs.lower() + "\n")
  1187. if prs[0] == "\"":
  1188. #% write string assignment (any place in shared line)
  1189. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1190. vr + " = " + prs + " ; ")
  1191. if prs[0] in ".1234567890-":
  1192. #% write numerics
  1193. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1194. vr + " = " + prs + " ; ")
  1195. if prs[0] == "#":
  1196. #% write trailing comments #$ print colour(14, 0) + prs + colour(7,0)
  1197. outfilewrite(outb, prs + "\n") ; break
  1198. if prs.lower() in ufunc.keys():
  1199. #% write pre-func-call var backup for sub-style behavior #$ print colour(14, 0) + "figbac = " + lnob(x,0) + " ; " + colour(7,0)
  1200. outfilewrite(outb, chr(32) * alexatleast(0, indent) +
  1201. "figbac = " + lnob(x,0) + " ; " ) # ##
  1202. alexprsbac = prs.lower()
  1203. cstrctr = len(ufunc[prs])
  1204. #print cstrctr
  1205. if cstrctr == 0:
  1206. #% write zero-param func/?sub call
  1207. #$ print colour(14, 0) + vr + " = " + prs.lower() + "() ; " + lnob(x, 0) + " = alexnone(" + lnob(x, 0) + ", alexbac) ; " + colour(7,0)
  1208. outfilewrite(outb, chr(32) *
  1209. alexatleast(0, indent) +
  1210. vr + " = " + prs.lower() + "() ; " + lnob(x, 0) +
  1211. " = alexnone(" + lnob(x, 0) + ", alexbac) ; ") # #
  1212. else:
  1213. #print "y"
  1214. vrop += 1
  1215. vrcs = vr + " = " + prs.lower() + "("
  1216. #multiparameter
  1217. if prs.lower() in cmds.keys():
  1218. if prs.lower() in ["display", "pset", "line"]:
  1219. ingfx = 1
  1220. ##print prs
  1221. cstrctr = cmds[prs]
  1222. ##print cstrctr
  1223. if cstrctr == -1:
  1224. #% write zero-param subs
  1225. outfilewrite(outb, chr(32) *
  1226. alexatleast(0, indent) + "fig" +
  1227. prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1228. if cstrctr == 0:
  1229. #% write zero-param functions
  1230. outfilewrite(outb, chr(32) * alexatleast(0,
  1231. indent) + vr +
  1232. " = alex" + prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1233. if cstrctr < -1:
  1234. if prs == "return":
  1235. cstrctr = abs(cstrctr) - 1
  1236. vrcs = "return " #parameter
  1237. else:
  1238. cstrctr = abs(cstrctr) - 1
  1239. if prs == "swap": vrcs = "swap "
  1240. else:
  1241. vrop += 1
  1242. vrcs = "fig" + prs.lower() + "(" + vr
  1243. vrcs += ", " #multiparameter
  1244. else:
  1245. vrop += 1
  1246. vrcs = vr + " = alex" + prs.lower() + "(" + vr
  1247. vrcs += ", " #multiparameter
  1248. #if vrop == vrcl and e == 0:
  1249. #print lc(), alexfsp(p)
  1250. #% finish each line with lf
  1251. outfilewrite(outb, "\n")
  1252. #else:
  1253. #print lc() + p
  1254. else:
  1255. e = 2
  1256. if e == 1:
  1257. e = 0
  1258. if not len(error):
  1259. error = "error: problem in command structure or details."
  1260. errorin = linecount
  1261. errorsrc = p
  1262. #print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1263. break
  1264. if e == 2:
  1265. e = 0
  1266. if not len(error):
  1267. error = "error: cannot create variable or function beginning"
  1268. error += " with \"fig\""
  1269. errorin = linecount
  1270. errorsrc = p
  1271. #print lc() + colour(14, 0) + p + colour(7, 0)
  1272. break
  1273. if e == 3:
  1274. e = 0
  1275. if not len(error):
  1276. error = "error: single-line command \"" + shln + "\" not on own line"
  1277. errorin = linecount
  1278. errorsrc = p
  1279. #print lc() + colour(14, 0) + p + colour(7, 0)
  1280. break
  1281. if e == 4:
  1282. e = 0
  1283. if not len(error):
  1284. error = "error: shared-line function \""
  1285. error += shln + "\" cannot be used to start a line"
  1286. errorin = linecount
  1287. errorsrc = p
  1288. #print lc() + colour(14, 0) + p + colour(7, 0)
  1289. break
  1290. if e == 5:
  1291. e = 0
  1292. if not len(error):
  1293. error = "error: variable or function not created, but referenced... \""
  1294. error += shln + "\" needs to be set before first use"
  1295. errorin = linecount
  1296. errorsrc = p
  1297. #print lc() + colour(14, 0) + p + colour(7, 0)
  1298. break
  1299. if vrcl != vrop:
  1300. e = 0
  1301. if not len(error):
  1302. error = "error: a command has the wrong number of parameters."
  1303. errorin = linecount
  1304. errorsrc = p
  1305. break
  1306. try:
  1307. sc = alexreverse(alexleft(alexreverse(alexargv[1]), 5).lower())
  1308. if sc == ".alex": sc = alexarropen(alexargv[1])
  1309. except: sc = ""
  1310. if "--" not in " ".join(alexargv):
  1311. if not sc: alexcolourtext(11) ; print proginf + chr(32),
  1312. hist = os.path.join(os.path.expanduser("~"), ".alex_history")
  1313. #if errorin:
  1314. # print error ; colour(14, None) ; print "error in line " + str(errorin) + ":"
  1315. # colour(7, None)
  1316. # print errorsrc
  1317. alexver = alexjoin(proginf.split(",")[0].split(" "), "").replace(".", "")
  1318. arrdostart = ""
  1319. nl = chr(10)
  1320. if alexosname == "nt": nl = chr(13) + chr(10) ; arrdostart = "start /b /wait "
  1321. def alexgetpsim(x, y):
  1322. px = alexsplit(x, " ")
  1323. py = alexsplit(y, " ")
  1324. xlen = len(px)
  1325. ylen = len(py)
  1326. xc = 0
  1327. yc = 0
  1328. b = "" ;
  1329. for p in px:
  1330. c = alexinstr(py, p)
  1331. if p == b:
  1332. c = 0
  1333. if c:
  1334. now = xc
  1335. now = alexplus(now, 1)
  1336. now, xc = xc, now
  1337. b = p
  1338. xper = xc
  1339. xper = alexdivby(xper, xlen)
  1340. for p in py:
  1341. c = alexinstr(px, p)
  1342. if p == b:
  1343. c = 0
  1344. if c:
  1345. now = yc
  1346. now = alexplus(now, 1)
  1347. now, yc = yc, now
  1348. b = p
  1349. yper = alexdivby(yc, ylen)
  1350. now = alexplus(xper, yper)
  1351. now = alexdivby(now, 2) * 100
  1352. return now
  1353. def ptrans(token, argu, alexhist, nl):
  1354. global indent
  1355. if token.lower() == "cd":
  1356. if argu: argu = argu[0]
  1357. if argu:
  1358. if argu[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1359. argu = alexqesc(argu)
  1360. return (chr(32) * alexatleast(0, indent) + "try: os.chdir(" + argu + ")" + nl +
  1361. chr(32) * alexatleast(0, indent) + "except: print 'no such file or directory: ' + str(" + argu + ")" + nl)
  1362. #; alexq(alexhist)
  1363. elif token.lower() == "set":
  1364. if argu: vname = argu[0] ; argu = argu[1]
  1365. if argu and alexvalidvar(vname):
  1366. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1367. argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1368. return (chr(32) * alexatleast(0, indent) + vname + " = " + argu + nl)
  1369. #; alexq(alexhist)
  1370. elif token.lower() == "setrandint":
  1371. if argu: vname = argu[0] ; argm = argu[1] ; argu = argu[2]
  1372. if argu 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 + " = str(alexrandint(" + str(argm) + ", " + str(argu) + "))" + nl)
  1376. elif token.lower() == "setnum":
  1377. if argu: vname = argu[0] ; argm = argu[1]
  1378. if argm 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 + " = alexval(" + str(argm) + ")" + nl)
  1382. elif token.lower() == "setadd":
  1383. if argu: vname = argu[0] ; argm = argu[1] ; argu = argu[2]
  1384. if argu and alexvalidvar(vname):
  1385. if vname[0] == chr(34) and argu[-1] == chr(34) and len(argu) > 2: argu = argu[1:-1]
  1386. #argu = chr(34) + argu.replace(chr(34), chr(92) + chr(34)) + chr(34)
  1387. return (chr(32) * alexatleast(0, indent) + vname + " = str(" + str(argm) + " + " + str(argu) + ")" + nl)
  1388. elif token.lower() == "setinput":
  1389. if argu: vname = argu[0]
  1390. if argu and alexvalidvar(vname):
  1391. return (chr(32) * alexatleast(0, indent) + vname + " = raw_input()" + nl)
  1392. #if token.lower() == "locate":
  1393. # if argu: vname = argu[0] ; argu = argu[1]
  1394. # if argu and vname:
  1395. # return (chr(32) * alexatleast(0, indent) + "alexlocate(0, str(" + vname + "), str(" + argu + "))" + nl)
  1396. elif token.lower() == "next":
  1397. indent = alexatleast(0, indent -4)
  1398. return (chr(32) * alexatleast(0, indent) + nl)
  1399. elif token.lower() == "break":
  1400. return (chr(32) * alexatleast(0, indent) + "break" + nl)
  1401. elif token.lower() == "while":
  1402. outwr = chr(32) * alexatleast(0, indent) + "while 1:" + nl
  1403. indent += 4
  1404. outwr += chr(32) * alexatleast(0, indent) + "try: sleep(.025)" + nl
  1405. outwr += chr(32) * alexatleast(0, indent) + "except: break" + nl
  1406. return outwr
  1407. elif token.lower() == "forin":
  1408. if argu: vname = argu[0] ; argm = argu[1]
  1409. try:
  1410. argm = int(argm) ; outwr = chr(32) * alexatleast(0, indent) + "for " + vname + " in range(1, " + str(argm) + " + 1):" + nl
  1411. except:
  1412. outwr = chr(32) * alexatleast(0, indent) + "for " + vname + " in " + argm + ":" + nl
  1413. indent += 4
  1414. #outwr += chr(32) * alexatleast(0, indent) + "try: sleep(.025)" + nl
  1415. #outwr += chr(32) * alexatleast(0, indent) + "except: break" + nl
  1416. return outwr
  1417. x = 0
  1418. try:
  1419. if sys.argv[1] == "--help":
  1420. print proginf ; x = 1
  1421. print
  1422. print "usage:"
  1423. print
  1424. print " alex run the alex line executive"
  1425. print " or:"
  1426. print " alex --help show this help information and exit"
  1427. print
  1428. print " sleep [n] pause for one second or [n] seconds"
  1429. print " arrcurl [-a] url write contents of url as output"
  1430. print " pserver run a mini http server using the current folder"
  1431. print " locate row column change the cursor position"
  1432. print " colour colour highlight change the text colours"
  1433. print " cat file1 [file2] [-n] concatenate files and/or number all lines"
  1434. print " set vname value set vname to value"
  1435. print " setrandint vname min max set vname to a random number between min / max"
  1436. print " setinput vname set vname to whatever is input from the keyboard"
  1437. print " setnum vname num set vname to numeric num (variable or value)"
  1438. print " setadd vname v1 v2 set vname to sum of v1 and v2 (string or numeric)"
  1439. print " find | fsortplus find files, show size / sha256 / date / time"
  1440. print " find | fsortplusnows fsortplus, but create hash without whitespace"
  1441. print " find | dc [n] [d[srchdate]] [+/-szlimit] list name/size/time, colour by type"
  1442. print
  1443. print " | isoname text only show lines containing \"text\""
  1444. print " | isoplus text only list files with lines containing \"text\""
  1445. print " | minusname text remove lines containing \"text\""
  1446. print " | lcase [or] ucase convert lines to lower or upper case"
  1447. print " | fields 1 2 3 4 _ show 1st, 2nd, 3rd, 4th, _all fields/tokens"
  1448. print " | replace what with replace \"what\" with \"with\""
  1449. print " | arrdo \"do what\" very powerful / do not use"
  1450. print " | tops n only show top n lines"
  1451. print " | bots n only show bottom n lines"
  1452. print " | noreps only show each line once, regardless of sort"
  1453. print " | var varname pipe output to varname"
  1454. print " | ascii [-h] display text as ascii codes (-h for hex)"
  1455. print " | rainbow rotate colours by -f field, -p pos, -l level"
  1456. print " | findsim find similar files"
  1457. print
  1458. print " while ; repeatedly do part after ;"
  1459. print " forin vname 500 ; do part after ; ...500 times"
  1460. print " forin vname array ; do part after ; ...loop through array"
  1461. print " next mark the bottom of a while or forin loop"
  1462. print " break exit a while or forin loop"
  1463. print " clear clear the screen"
  1464. print " pset x y c draw a dot at x, y in colour c"
  1465. print " chr asciicode [or unicode] output a character from ascii or unicode"
  1466. print " line x y x2 y2 c draw a line from x, y to x2, y2 in colour c"
  1467. print " echo $varname output $varname"
  1468. print " quit, exit quit the shell"
  1469. x = 1
  1470. elif sys.argv[1] == "--pserver":
  1471. try: alexr = alexshell("python -m SimpleHTTPServer")
  1472. except: print
  1473. x = 1
  1474. elif sys.argv[1] == "--isoname":
  1475. lfor = sys.argv[2].split("|")
  1476. for y in alexarrstdin():
  1477. for t in lfor:
  1478. if t in y: print y ; break
  1479. x = 1
  1480. elif sys.argv[1] == "--minusname":
  1481. lfor = sys.argv[2].split("|")
  1482. for y in alexarrstdin():
  1483. for t in lfor:
  1484. if t in y: y = 0 ; break
  1485. if y != 0: print y
  1486. x = 1
  1487. elif sys.argv[1] == "--locate":
  1488. try: c = int(sys.argv[3])
  1489. except: c = None
  1490. try: r = int(sys.argv[2])
  1491. except: r = None
  1492. alexlocate(r, r, c)
  1493. x = 1
  1494. elif sys.argv[1] == "--line":
  1495. try:
  1496. c = int(sys.argv[6])
  1497. y2 = int(sys.argv[5])
  1498. x2 = int(sys.argv[4])
  1499. y = int(sys.argv[3])
  1500. x = int(sys.argv[2])
  1501. except: r = None
  1502. alexline(x, y, x2, y2, c)
  1503. x = 1
  1504. elif sys.argv[1] == "--pset":
  1505. try:
  1506. c = int(sys.argv[4])
  1507. y = int(sys.argv[3])
  1508. x = int(sys.argv[2])
  1509. except: r = None
  1510. alexpset(x, y, c)
  1511. x = 1
  1512. elif sys.argv[1] == "--colour":
  1513. try: c = int(sys.argv[3])
  1514. except: c = None
  1515. try: r = int(sys.argv[2])
  1516. except: r = None
  1517. alexcolortext(r, c)
  1518. x = 1
  1519. elif sys.argv[1] == "--findsim":
  1520. files = alexarr(0) ; files = alexmid(files, 1, 0) ; # sizes
  1521. filen = alexarr(0) ; filen = alexmid(filen, 1, 0) ; # names
  1522. filec = alexarr(0) ; filec = alexmid(filec, 1, 0) ; # contents
  1523. fileh = alexarr(0) ; fileh = alexmid(fileh, 1, 0) ; # hashes
  1524. filed = alexarr(0) ; filed = alexmid(filed, 1, 0) ; # done already
  1525. filelist = alexarr(0) ; filelist = alexmid(filelist, 1, 0) ; # output
  1526. p = alexarrstdin()
  1527. p = filter(os.path.isfile, p)
  1528. for f in p:
  1529. fs = 0
  1530. fs = int(os.path.getsize(f))
  1531. if 1:
  1532. filen = alexplus(filen, f)
  1533. files = alexplus(files, fs)
  1534. now = sha256(open(f).read()).hexdigest()
  1535. fileh = now
  1536. contentarr = alexarropen(f)
  1537. contentarr = alexjoin(contentarr, " ")
  1538. contentarr = alexsplit(contentarr, " ")
  1539. alexarrsort(contentarr)
  1540. content = ""
  1541. oldc = ""
  1542. for c in contentarr:
  1543. if c:
  1544. if c != oldc:
  1545. now = alexplus(content, c)
  1546. now = alexplus(now, " ")
  1547. now, content = content, now
  1548. filec = alexplus(filec, content)
  1549. quot = chr(34)
  1550. filenlen = alexlen(filen)
  1551. for x in range(1, filenlen + 1, 1):
  1552. print alexjoin(("comparing", alexarrget(filen, x), "to other files, file",
  1553. x, "of", filenlen, "(" + str(int(float(x) / filenlen * 100)) + "%)"), " ")
  1554. for y in range(1, filenlen + 1, 1):
  1555. a = alexarrget(filen, x)
  1556. b = alexarrget(filen, y)
  1557. fwd = quot
  1558. fwd = alexplus(fwd, a) ;
  1559. fwd = alexplus(fwd, quot) ;
  1560. fwd = alexplus(fwd, " ") ;
  1561. fwd = alexplus(fwd, quot) ;
  1562. fwd = alexplus(fwd, b) ;
  1563. fwd = alexplus(fwd, quot)
  1564. rev = quot
  1565. rev = alexplus(rev, b) ;
  1566. rev = alexplus(rev, quot) ;
  1567. rev = alexplus(rev, " ") ;
  1568. rev = alexplus(rev, quot) ;
  1569. rev = alexplus(rev, a) ;
  1570. rev = alexplus(rev, quot)
  1571. already = alexinstr(filed, rev)
  1572. if a == b:
  1573. already = 1
  1574. if already:
  1575. already = 1
  1576. else:
  1577. filed = alexplus(filed, fwd)
  1578. ca = alexarrget(filec, x)
  1579. cb = alexarrget(filec, y)
  1580. cas = alexarrget(files, x)
  1581. cbs = alexarrget(files, y)
  1582. try:
  1583. cah = alexarrget(fileh, x)
  1584. except:
  1585. cbh = chr(32)
  1586. try:
  1587. cbh = alexarrget(fileh, y)
  1588. except:
  1589. cbh = ""
  1590. if 1:
  1591. if ca == cb:
  1592. if cah == cbh:
  1593. cbs = alexjoin((" match: ", cah), "")
  1594. filelist += [(100, alexjoin((fwd, " # ", cas, " ", cbs), ""))]
  1595. else:
  1596. per = alexgetpsim(ca, cb)
  1597. filelist += [(per, alexjoin((fwd, " # ", cas, " ", cbs), ""))]
  1598. alexarrsort(filelist)
  1599. for p in filelist:
  1600. try: print alexjoin((int(p[0]), p[1]), " ")
  1601. except: pass
  1602. x = 1
  1603. elif sys.argv[1] == "--cat":
  1604. whichfiles = []
  1605. catn = 0
  1606. for p in sys.argv[2:]:
  1607. if p == "-n": catn = 1
  1608. elif p not in [";", "|"]: whichfiles += [p]
  1609. else: break
  1610. linec = 1
  1611. for p in whichfiles:
  1612. if catn:
  1613. tab = chr(9)
  1614. for textlines in alexarropen(p):
  1615. alexprints(str(linec) + tab + textlines + nl) ; linec += 1
  1616. else:
  1617. alexprints(alexjoin(alexarropen(p), nl))
  1618. if len(whichfiles) == 0:
  1619. if catn:
  1620. tab = chr(9)
  1621. for textlines in alexarrstdin():
  1622. alexprints(str(linec) + tab + textlines + nl) ; linec += 1
  1623. else:
  1624. alexprint(alexjoin(alexarrstdin(), nl))
  1625. x = 1
  1626. elif sys.argv[1] == "--rainbow":
  1627. try:
  1628. f = sys.argv[2].lower()
  1629. except:
  1630. f = ""
  1631. linec = 0
  1632. prevw = 0
  1633. for textlines in alexarrstdin():
  1634. if f not in ["-f", "-p", "-l"]:
  1635. ckw = (textlines.strip() == "")
  1636. if ckw and not prevw:
  1637. linec += 1
  1638. linec = linec % 6
  1639. alexcolourtext([0, 12, 6, 14, 2, 9, 5][linec + 1])
  1640. alexprints(textlines + nl) ;
  1641. prevw = ckw
  1642. else:
  1643. chc = 0
  1644. linec = 0
  1645. prevw = 0
  1646. for tc in textlines:
  1647. chc += 1
  1648. ckw = (tc.strip() != "")
  1649. if ckw and not prevw:
  1650. if f == "-p":
  1651. linec = chc % 6
  1652. else:
  1653. linec += 1
  1654. if f == "-l":
  1655. linec = len(textlines) - len(textlines.lstrip())
  1656. linec = linec % 6
  1657. prevw = ckw
  1658. alexcolourtext([0, 5, 12, 6, 14, 2, 9][linec + 1])
  1659. alexprints(tc)
  1660. alexprint("")
  1661. x = 1
  1662. elif sys.argv[1] == "--dc":
  1663. dates = ""
  1664. cw = 80
  1665. try:
  1666. sizel = 0
  1667. cw = sys.argv[2]
  1668. if cw[0].lower() == "d":
  1669. dates = cw
  1670. cw = 80
  1671. elif cw[0].lower() in "-+":
  1672. try:
  1673. sizel = int(float(cw))
  1674. except:
  1675. sizel = 0
  1676. cw = 80
  1677. else:
  1678. cw = int(cw)
  1679. dates = sys.argv[3]
  1680. if dates[0] in "-+":
  1681. try:
  1682. sizel = int(float(dates))
  1683. except:
  1684. sizel = 0
  1685. dates = ""
  1686. if dates[0].lower() != "d":
  1687. dates = ""
  1688. except:
  1689. if type(cw) != int:
  1690. cw = 80
  1691. dates = dates[1:]
  1692. from os import listdir
  1693. from os import path
  1694. for fp in alexarrstdin():
  1695. b = []
  1696. bfext = 0
  1697. bp = 0
  1698. bfs = 0
  1699. bfiletime = 19
  1700. try:
  1701. paths = 0
  1702. for p in listdir(fp):
  1703. if not paths:
  1704. paths = 1
  1705. alexcolortext(9, 0)
  1706. alexprints(fp + " ")
  1707. alexcolortext(3, 0)
  1708. try:
  1709. alexprints(unichr(0x2500) * (cw - 2 - len(fp)))
  1710. except:
  1711. alexprints("-" * (cw - 2 - len(fp)))
  1712. alexcolortext(7, 0)
  1713. alexprint("")
  1714. fext = path.splitext(fp + path.sep + p)[1]
  1715. if len(p) > bfext:
  1716. bfext = len(p)
  1717. try:
  1718. fs = int(os.path.getsize(fp + path.sep + p))
  1719. if len(str(fs)) > bfs:
  1720. bfs = len(str(fs))
  1721. except:
  1722. fs = ""
  1723. try:
  1724. if p[0] == ".":
  1725. fext = "."
  1726. if alexosname != "nt":
  1727. if os.access(fp + path.sep + p, os.X_OK):
  1728. fext = 2
  1729. if os.path.isdir(fp + path.sep + p):
  1730. fext = 0
  1731. if os.path.islink(fp + path.sep + p):
  1732. fext = 1
  1733. except:
  1734. pass
  1735. try:
  1736. filetime = str(datetime.fromtimestamp(os.path.getmtime(fp
  1737. + path.sep + p)))[0:19]
  1738. except:
  1739. filetime = ""
  1740. try:
  1741. b += [(fext, p, fs, filetime)]
  1742. except: b += [("problem", "with", 0, "fsortplus")]
  1743. b.sort()
  1744. mw = bfext + 1 + bfs + 1 + bfiletime + 2
  1745. lw = 1
  1746. if mw < cw:
  1747. lw = int(cw / mw)
  1748. for lp in range(0, len(b), lw):
  1749. tab = chr(9)
  1750. try:
  1751. if lw < 2:
  1752. p = b[lp]
  1753. alexpld = 0
  1754. sizem = 1
  1755. if sizel < 0:
  1756. if p[2] > abs(sizel): sizem = 0
  1757. if sizel > 0:
  1758. if p[2] < abs(sizel): sizem = 0
  1759. if sizem and alexprintlinedc(str(p[3]), dates):
  1760. alexpld = 1
  1761. alexprint( alexpadright(p[1], bfext,
  1762. alexcolourdc(p[0], p[1])) + " " +
  1763. alexpadleft(p[2], bfs, alexcolourdc(p[0])) +
  1764. " " + alexpadleft(str(p[3]), bfiletime) + " " + " " )
  1765. else:
  1766. alexpld = 0
  1767. for lpc in range(lw):
  1768. try:
  1769. p = b[lp + lpc]
  1770. sizem = 1
  1771. if sizel < 0:
  1772. if p[2] > abs(sizel): sizem = 0
  1773. if sizel > 0:
  1774. if p[2] < abs(sizel): sizem = 0
  1775. if sizem and alexprintlinedc(str(p[3]), dates):
  1776. alexpld = 1
  1777. alexprints( alexpadright(p[1], bfext,
  1778. alexcolourdc(p[0], p[1])) + " " +
  1779. alexpadleft(p[2], bfs, alexcolourdc(p[0])) + " " +
  1780. alexpadleft(str(p[3]), bfiletime) + " " + " " )
  1781. except:
  1782. pass
  1783. if alexpld:
  1784. alexprint("")
  1785. except:
  1786. print p[1] #"" #-1" + chr(32) + "-" * 64 + chr(32) + "?" + chr(32) + "?" + chr(32) + p[3]
  1787. except OSError:
  1788. pass
  1789. x = 1
  1790. elif sys.argv[1] == "--ascii":
  1791. try:
  1792. f = sys.argv[2].lower()
  1793. except:
  1794. f = ""
  1795. linec = 0
  1796. tab = chr(9)
  1797. for textlines in alexarrstdin():
  1798. linec += 1
  1799. alexprints(str(linec) + tab)
  1800. count = 1
  1801. for tc in textlines:
  1802. if f == "-h":
  1803. alexprints(hex(ord(tc)).replace("0x", ""))
  1804. else:
  1805. alexprints(ord(tc))
  1806. if count > 7:
  1807. count = 0
  1808. alexprints(chr(32))
  1809. alexprints(" ")
  1810. count += 1
  1811. alexprint("")
  1812. x = 1
  1813. elif sys.argv[1] == "--sleep":
  1814. try:
  1815. alexsleep(float(sys.argv[2]))
  1816. except:
  1817. alexsleep(1)
  1818. x = 1
  1819. elif sys.argv[1] == "--arrcurl":
  1820. try:
  1821. alexaru = ""
  1822. alexar = sys.argv[2]
  1823. except:
  1824. alexar = ""
  1825. if alexar.lower() == "-a":
  1826. try:
  1827. alexaru = sys.argv[3]
  1828. alexar, alexaru = alexaru, alexar
  1829. except:
  1830. alexaru = ""
  1831. if alexar:
  1832. alexu = alexarrcurl(alexaru, alexdequote(alexar))
  1833. if alexaru.lower() == "-a":
  1834. alexu = alexjoin(alexu, nl)
  1835. alexprints(alexu)
  1836. x = 1
  1837. elif sys.argv[1] == "--chr":
  1838. try:
  1839. t = int(sys.argv[2])
  1840. if t > 255:
  1841. alexprints(unichr(t))
  1842. else:
  1843. alexprints(chr(t))
  1844. except:
  1845. pass
  1846. x = 1
  1847. elif sys.argv[1] == "--lcase":
  1848. for textlines in alexarrstdin():
  1849. alexprints(textlines.lower() + nl)
  1850. x = 1
  1851. elif sys.argv[1] == "--ucase":
  1852. for textlines in alexarrstdin():
  1853. alexprints(textlines.upper() + nl)
  1854. x = 1
  1855. elif sys.argv[1] == "--replace":
  1856. fr = sys.argv[2].replace(chr(92) + chr(110), nl)
  1857. try: into = sys.argv[3].replace(chr(92) + chr(110), nl)
  1858. except: into = ""
  1859. for textlines in alexjoin(alexarrstdin(), nl).replace(fr, into).split(nl):
  1860. alexprints(textlines.replace(fr, into) + nl)
  1861. x = 1
  1862. elif sys.argv[1] == "--tops":
  1863. fr = sys.argv[2]
  1864. for textlines in alextops(alexarrstdin(), int(fr)):
  1865. alexprints(textlines + nl)
  1866. x = 1
  1867. elif sys.argv[1] == "--bots":
  1868. fr = sys.argv[2]
  1869. for textlines in alexbots(alexarrstdin(), int(fr)):
  1870. alexprints(textlines + nl)
  1871. x = 1
  1872. elif sys.argv[1] == "--arrdo":
  1873. try: now = sys.argv[2]
  1874. except: now = ""
  1875. linec = 1
  1876. if len(now):
  1877. for textlines in alexarrstdin():
  1878. s = alexshell(arrdostart + now + " " + textlines)
  1879. x = 1
  1880. elif sys.argv[1] == "--addquotes":
  1881. quot = chr(34)
  1882. if 1:
  1883. for textlines in alexarrstdin():
  1884. alexprints(quot + textlines.strip() + quot + nl)
  1885. x = 1
  1886. elif sys.argv[1] == "--var":
  1887. for textlines in alexarrstdin():
  1888. alexprint(textlines)
  1889. x = 1
  1890. elif sys.argv[1] == "--fsortplus":
  1891. b = []
  1892. for p in alexarrstdin():
  1893. try: fs = int(os.path.getsize(p))
  1894. except: fs = -1
  1895. try: s256 = sha256(open(p).read()).hexdigest()
  1896. except: s256 = "-" * 64
  1897. try: filetime = str(datetime.fromtimestamp(os.path.getmtime(p)))[0:19]
  1898. except: filetime = -1
  1899. try: b += [(fs, s256, filetime, p)]
  1900. except: b += [(0, "problem", "with", "fsortplus")]
  1901. b.sort()
  1902. for p in b:
  1903. tab = chr(9)
  1904. try: print str(p[0]) + " " + p[1] + " " + p[2] + tab + p[3]
  1905. except: print "-1" + chr(32) + "-" * 64 + chr(32) + "?" + chr(32) + "?" + chr(32) + p[3]
  1906. x = 1
  1907. elif sys.argv[1] == "--fsortplusnows":
  1908. b = []
  1909. for p in alexarrstdin():
  1910. try: fs = len(alexfws(open(p).read()))
  1911. except: fs = -1
  1912. try: s256 = sha256(alexfws(open(p).read())).hexdigest()
  1913. except: s256 = "-" * 64
  1914. try: filetime = str(datetime.fromtimestamp(os.path.getmtime(p)))[0:19]
  1915. except: filetime = -1
  1916. try: b += [(fs, s256, filetime, p)]
  1917. except: b += [(0, "problem", "with", "fsortplus")]
  1918. b.sort()
  1919. for p in b:
  1920. tab = chr(9)
  1921. try: print str(p[0]) + " " + p[1] + " " + p[2] + tab + p[3]
  1922. except: print "-1" + chr(32) + "-" * 64 + chr(32) + "?" + chr(32) + "?" + chr(32) + p[3]
  1923. x = 1
  1924. elif sys.argv[1] == "--isoplus":
  1925. fr = sys.argv[2]
  1926. tab = chr(9)
  1927. for p in alexarrstdin():
  1928. try:
  1929. f = alexopen(p, "r")
  1930. except:
  1931. f = 0
  1932. while 1:
  1933. try:
  1934. textlines = alexflineinput(p)
  1935. except:
  1936. textlines = ""
  1937. if textlines == chr(10):
  1938. break
  1939. if fr.lower() in textlines.lower():
  1940. print p + tab + textlines
  1941. try:
  1942. f = alexclose(p)
  1943. except:
  1944. f = 0
  1945. x = 1
  1946. elif sys.argv[1] == "--noreps":
  1947. b = {}
  1948. for textlines in alexarrstdin():
  1949. try:
  1950. t = b[textlines]
  1951. except:
  1952. print textlines
  1953. b[textlines] = 0
  1954. x = 1
  1955. elif sys.argv[1] == "--fields":
  1956. try:
  1957. now = sys.argv[2:]
  1958. except:
  1959. now = ""
  1960. if len(now):
  1961. for textlines in alexarrstdin():
  1962. count = 0
  1963. for p in now:
  1964. count += 1
  1965. if p == "_":
  1966. alexprints(textlines)
  1967. else:
  1968. try:
  1969. alexprints(textlines.split()[int(p) - 1])
  1970. except:
  1971. alexprints("~")
  1972. if count < len(now):
  1973. alexprints(" ")
  1974. print ""
  1975. x = 1
  1976. except: p = 0
  1977. if x > 0: sys.exit()
  1978. alexhist = os.path.join(os.path.expanduser("~"), ".alex_history")
  1979. try: f = open(alexhist).read()
  1980. except:
  1981. try:
  1982. f = open(alexhist, "w")
  1983. f.write("")
  1984. f.close()
  1985. f = open(alexhist).read()
  1986. except: print "unable to open history file"
  1987. def alexq(alexhist):
  1988. try: readline.write_history_file(alexhist)
  1989. except: print "couldnt write history file"
  1990. quit()
  1991. pcmds = ["cd", "while", "set", "setrandint", "setinput", "next", "break", "setadd", "setnum", "forin"] # take priority no matter what files are found
  1992. ntlocalcmds = ["dir", "echo"]
  1993. def alexparse(p):
  1994. quot = chr(34)
  1995. inquotes = 0
  1996. buf = ""
  1997. tokencount = 0
  1998. out = ["",]
  1999. for t in p:
  2000. # handle quote mark
  2001. if t == quot:
  2002. if inquotes: out[tokencount] += t ; inquotes = 0
  2003. else: tokencount += 1 ; out += [""] ; out[tokencount] = t ; inquotes = 1
  2004. # handle spaces outside quotes
  2005. elif inquotes == 0 and t == " ":
  2006. tokencount += 1 ; out += [""]
  2007. # handle pipes outside quotes
  2008. elif inquotes == 0 and t == "|":
  2009. tokencount += 1 ; out += ["|"] ; tokencount += 1 ; out += [""]
  2010. # handle semicolons outside quotes
  2011. elif inquotes == 0 and t == ";":
  2012. tokencount += 1 ; out += [";"] ; tokencount += 1 ; out += [""]
  2013. elif inquotes == 0 and t == "$":
  2014. tokencount += 1 ; out += [""] ; out[tokencount] = t
  2015. elif inquotes == 0 and t == ";":
  2016. tokencount += 1 ; out += [";"] ; tokencount += 1 ; out += [""]
  2017. # handle everything else
  2018. else: out[tokencount] += t
  2019. if len(" ".join(out).strip()): out += [";"]
  2020. while "" in out: out.remove("")
  2021. if inquotes: return 0
  2022. else: return out
  2023. def alexesc(p, anglebr):
  2024. p = p.replace(chr(92), chr(92) + chr(92))
  2025. p = p.replace(chr(34), chr(92) + chr(34))
  2026. p = p.replace(chr(38), chr(92) + chr(38))
  2027. if anglebr:
  2028. p = p.replace("<", chr(92) + "<") # allow redirection
  2029. p = p.replace(">", chr(92) + ">") # if 0
  2030. p = p.replace("(", chr(92) + "(")
  2031. p = p.replace(")", chr(92) + ")")
  2032. return p
  2033. def alexqesc(p):
  2034. b = chr(34) + alexesc(p, 0) + chr(34)
  2035. p = ""
  2036. normal = 1
  2037. for t in b:
  2038. if t == "$" and normal: normal = 0 ; extra = "$"
  2039. elif normal: p += t
  2040. elif t in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_": extra += t
  2041. else:
  2042. normal = 1
  2043. if alexosname == "nt":
  2044. if alexvalidrefvar(extra): p += chr(34) + " + alexesc(alexprocstring(" + extra[1:] + "), 1) + " + chr(34) + t
  2045. else: p = 0
  2046. else:
  2047. if alexvalidrefvar(extra): p += chr(92) + chr(34) + chr(34) + " + alexesc(alexprocstring(" + extra[1:] + "), 1) + " + chr(34) + chr(92) + chr(34) + t
  2048. else: p = 0
  2049. return p
  2050. def alexprocstring(p):
  2051. global nl
  2052. if type(p) == list:
  2053. return alexjoin(p, nl)
  2054. else: return str(p)
  2055. def alexvalidvar(p):
  2056. try: p4 = p[0:4]
  2057. except: p4 = ""
  2058. try:
  2059. if p.lower()[0] in "abcdefghijklmnopqrstuvwxyz" and p.lower() != "var" and p4.lower() != "alex":
  2060. for t in p:
  2061. if t.lower() not in "abcdefghijklmnopqrstuvwxyz_1234567890": return 0
  2062. return 1
  2063. except: return 0
  2064. return 0
  2065. def alexvalidrefvar(p):
  2066. if len(p) > 1:
  2067. if p[0] == "$":
  2068. p = p[1:]
  2069. else: return 0
  2070. try: p4 = p[0:4]
  2071. except: p4 = ""
  2072. try:
  2073. if p.lower()[0] in "abcdefghijklmnopqrstuvwxyz" and p.lower() != "var" and p4.lower() != "alex":
  2074. for t in p:
  2075. if t.lower() not in "abcdefghijklmnopqrstuvwxyz_1234567890": return 0
  2076. return 1
  2077. except: return 0
  2078. return 0
  2079. csc = 0
  2080. buf = []
  2081. bufpos = 0
  2082. agg = ""
  2083. prevtoken = ""
  2084. ptoken = ""
  2085. outprog = ""
  2086. runvar = 0
  2087. invflag = 0
  2088. while 1:
  2089. p = ""
  2090. bufpos += 1
  2091. arrpath = os.getenv('PATH').split(os.pathsep)
  2092. if bufpos > len(buf): exec(outprog) ; outprog = "" ; indent = 0
  2093. if sc and bufpos > len(buf):
  2094. bufpos = 1
  2095. csc += 1
  2096. try: p = alexparse(sc[csc-1]) ; prevtoken = ""
  2097. except: p = ["quit"]
  2098. elif bufpos > len(buf):
  2099. bufpos = 1
  2100. getuser = os.getenv("LOGNAME")
  2101. if getuser == None: getuser = os.getenv("USER")
  2102. if getuser == None: getuser = ""
  2103. alexcolourtext(7)
  2104. if getuser == "root":
  2105. alexcolourtext(12) ; alexprints("root") ; alexcolourtext(7) ; alexprints(":") ; userp = "#> "
  2106. elif getuser: alexprints(getuser + ":") ; userp = "$> "
  2107. if alexosname == "nt": userp = "> "
  2108. alexcolourtext(9) ; alexprints(os.getcwd()) ; alexcolourtext(7) ; alexprints(userp)
  2109. try: p = alexparse(alexlineinput("")) ; prevtoken = ""
  2110. except EOFError: p=["quit"] ; print "exit"
  2111. except KeyboardInterrupt: print "^C" ; buf = [] ; agg = "" ; outprog = "" ; indent = 0 ; p = ""
  2112. except: p = [""]
  2113. invflag = 0
  2114. if p == 0: print "a quoted string was not end-quoted; syntax is invalid" ; p = []
  2115. if len(p): buf = p
  2116. try: token = buf[bufpos - 1]
  2117. except: token = ""
  2118. if token in pcmds and bufpos == 1: ptoken = token
  2119. ntreplace = 1
  2120. if alexosname == "nt":
  2121. pyprefix = "c:\python27\python "
  2122. if token == "find" and (bufpos == 1 or prevtoken == "|"): token = "dir /b /a /s"
  2123. elif token == "clear" and (bufpos == 1 or prevtoken == "|"): token = "cls"
  2124. elif token == "ucase" and (bufpos == 1 or prevtoken == "|"): token = pyprefix + alexver + ".py --ucase"
  2125. else: ntreplace = 0
  2126. arrpath += [os.getcwd()]
  2127. pfound = 1
  2128. elif bufpos == 1 or prevtoken == "|":
  2129. ntreplace = 0
  2130. pyprefix = ""
  2131. ntlocalcmds = []
  2132. try: pfound = int(len(alexarrshell("which " + token + " 2> /dev/null")) > 1) ; ##print "TOKEN("+token+")"
  2133. except: pfound = 0 ; token = ""
  2134. x = 0
  2135. if len(token) or len(ptoken):
  2136. if runvar and alexvalidvar(token): alexvar = token ; x = 1
  2137. if runvar: runvar = 2
  2138. if len(ptoken) and bufpos == 1: argu = [] ; x = 1
  2139. elif token in ["quit", "exit"] and bufpos == 1: alexq(alexhist)
  2140. elif token in ["help", "pserver", "isoname", "minusname", "locate", "colour", "line", "pset", "tops", "bots",
  2141. "cat", "arrdo", "fsortplus", "fsortplusnows", "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
  2142. elif token in ["var"] and prevtoken == "|": token = pyprefix + alexver + ".py --" + token ; x = 1 ; runvar = 1 ; alexvar = ""
  2143. if len(buf) and (bufpos == 1 or prevtoken == "|"): lfor = [token,]
  2144. if len(buf) > 0 and alexosname == "nt" and (bufpos == 1 or prevtoken == "|"):
  2145. lfor += [token + ".exe", token + ".bat"]
  2146. for plfor in lfor:
  2147. for ap in arrpath:
  2148. if plfor in os.listdir(ap) and x == 0:
  2149. token = ap + os.sep + plfor ; x = 1 ; break
  2150. if os.path.isfile(plfor) and x == 0: token = plfor ; x = 1 ; break
  2151. if x == 0 and lfor[0] in ntlocalcmds:
  2152. token = lfor[0] ; x = 1
  2153. elif ntreplace > 0 and x == 0:
  2154. x = 1
  2155. elif len(buf) > 0 and pfound == 1 and x == 0 and (bufpos == 1 or prevtoken == "|"):
  2156. x = 1
  2157. # invalid command
  2158. 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
  2159. elif len(buf) > 0 and invflag == 1 and x == 0 and (bufpos == 1 or prevtoken == "|"): token = "" ; bufpos = buf.index(";", bufpos-1) ; agg = ""
  2160. # invalid variable
  2161. 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
  2162. if x == 1 and bufpos == 1: agg = token
  2163. prevtoken = token
  2164. if token != ";" and bufpos > 1:
  2165. if ptoken:
  2166. argu += [token] ; x = 0
  2167. else:
  2168. agg += " " + token ; x = 0
  2169. if token == ";" and bufpos > 1:
  2170. if ptoken:
  2171. outprog += ptrans(ptoken, argu, alexhist, nl) ; argu = [] ; ptoken = "" ; prevtoken = ""
  2172. elif runvar:
  2173. if alexvar:
  2174. outprog += chr(32) * alexatleast(0, indent) + alexvar + " = alexvarproc(alexarrshell(" + alexqesc(agg) + "))" + nl ; agg = "" ; prevtoken = "" ; runvar = 0
  2175. else:
  2176. outprog += chr(32) * alexatleast(0, indent) + "alexr = alexshell(" + alexqesc(agg) + ")" + nl ; agg = "" ; prevtoken = ""
  2177. buf = buf[bufpos:] ; bufpos = 0