redis-server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. data/LICENSE +38 -0
  2. data/README.md +33 -0
  3. data/bin/redis +114 -0
  4. data/redis/Makefile +5 -0
  5. data/redis/extconf.rb +3 -0
  6. data/redis/redis-2.2.11/00-RELEASENOTES +199 -0
  7. data/redis/redis-2.2.11/BUGS +1 -0
  8. data/redis/redis-2.2.11/CONTRIBUTING +13 -0
  9. data/redis/redis-2.2.11/COPYING +10 -0
  10. data/redis/redis-2.2.11/Changelog +1032 -0
  11. data/redis/redis-2.2.11/INSTALL +30 -0
  12. data/redis/redis-2.2.11/Makefile +22 -0
  13. data/redis/redis-2.2.11/README +83 -0
  14. data/redis/redis-2.2.11/TODO +4 -0
  15. data/redis/redis-2.2.11/client-libraries/README +11 -0
  16. data/redis/redis-2.2.11/deps/hiredis/COPYING +10 -0
  17. data/redis/redis-2.2.11/deps/hiredis/Makefile +115 -0
  18. data/redis/redis-2.2.11/deps/hiredis/README.md +311 -0
  19. data/redis/redis-2.2.11/deps/hiredis/TODO +2 -0
  20. data/redis/redis-2.2.11/deps/hiredis/adapters/ae.h +95 -0
  21. data/redis/redis-2.2.11/deps/hiredis/adapters/libev.h +113 -0
  22. data/redis/redis-2.2.11/deps/hiredis/adapters/libevent.h +76 -0
  23. data/redis/redis-2.2.11/deps/hiredis/async.c +321 -0
  24. data/redis/redis-2.2.11/deps/hiredis/async.h +112 -0
  25. data/redis/redis-2.2.11/deps/hiredis/example-ae.c +53 -0
  26. data/redis/redis-2.2.11/deps/hiredis/example-libev.c +47 -0
  27. data/redis/redis-2.2.11/deps/hiredis/example-libevent.c +48 -0
  28. data/redis/redis-2.2.11/deps/hiredis/example.c +67 -0
  29. data/redis/redis-2.2.11/deps/hiredis/fmacros.h +15 -0
  30. data/redis/redis-2.2.11/deps/hiredis/hiredis.c +1058 -0
  31. data/redis/redis-2.2.11/deps/hiredis/hiredis.h +170 -0
  32. data/redis/redis-2.2.11/deps/hiredis/net.c +170 -0
  33. data/redis/redis-2.2.11/deps/hiredis/net.h +43 -0
  34. data/redis/redis-2.2.11/deps/hiredis/sds.c +479 -0
  35. data/redis/redis-2.2.11/deps/hiredis/sds.h +77 -0
  36. data/redis/redis-2.2.11/deps/hiredis/test.c +479 -0
  37. data/redis/redis-2.2.11/deps/hiredis/util.h +40 -0
  38. data/redis/redis-2.2.11/deps/linenoise/Makefile +10 -0
  39. data/redis/redis-2.2.11/deps/linenoise/README.markdown +45 -0
  40. data/redis/redis-2.2.11/deps/linenoise/example.c +27 -0
  41. data/redis/redis-2.2.11/deps/linenoise/linenoise.c +609 -0
  42. data/redis/redis-2.2.11/deps/linenoise/linenoise.h +55 -0
  43. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER +214 -0
  44. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER-2 +343 -0
  45. data/redis/redis-2.2.11/doc/AppendCommand.html +48 -0
  46. data/redis/redis-2.2.11/doc/AppendOnlyFileHowto.html +41 -0
  47. data/redis/redis-2.2.11/doc/AuthCommand.html +39 -0
  48. data/redis/redis-2.2.11/doc/Benchmarks.html +129 -0
  49. data/redis/redis-2.2.11/doc/BgrewriteaofCommand.html +41 -0
  50. data/redis/redis-2.2.11/doc/BgsaveCommand.html +39 -0
  51. data/redis/redis-2.2.11/doc/BlpopCommand.html +51 -0
  52. data/redis/redis-2.2.11/doc/BrpoplpushCommand.html +39 -0
  53. data/redis/redis-2.2.11/doc/CommandReference.html +47 -0
  54. data/redis/redis-2.2.11/doc/Comparisons.html +42 -0
  55. data/redis/redis-2.2.11/doc/ConfigCommand.html +76 -0
  56. data/redis/redis-2.2.11/doc/Configuration.html +38 -0
  57. data/redis/redis-2.2.11/doc/ConnectionHandlingSidebar.html +36 -0
  58. data/redis/redis-2.2.11/doc/ControlCommandsSidebar.html +36 -0
  59. data/redis/redis-2.2.11/doc/Credits.html +38 -0
  60. data/redis/redis-2.2.11/doc/DbsizeCommand.html +38 -0
  61. data/redis/redis-2.2.11/doc/DelCommand.html +41 -0
  62. data/redis/redis-2.2.11/doc/DesignPatterns.html +37 -0
  63. data/redis/redis-2.2.11/doc/EventLibray.html +44 -0
  64. data/redis/redis-2.2.11/doc/ExistsCommand.html +42 -0
  65. data/redis/redis-2.2.11/doc/ExpireCommand.html +96 -0
  66. data/redis/redis-2.2.11/doc/FAQ.html +70 -0
  67. data/redis/redis-2.2.11/doc/Features.html +38 -0
  68. data/redis/redis-2.2.11/doc/FlushallCommand.html +39 -0
  69. data/redis/redis-2.2.11/doc/FlushdbCommand.html +39 -0
  70. data/redis/redis-2.2.11/doc/FromSqlToDataStructures.html +37 -0
  71. data/redis/redis-2.2.11/doc/GenericCommandsSidebar.html +36 -0
  72. data/redis/redis-2.2.11/doc/GetCommand.html +39 -0
  73. data/redis/redis-2.2.11/doc/GetbitCommand.html +39 -0
  74. data/redis/redis-2.2.11/doc/GetsetCommand.html +38 -0
  75. data/redis/redis-2.2.11/doc/HackingStrings.html +83 -0
  76. data/redis/redis-2.2.11/doc/HashCommandsSidebar.html +36 -0
  77. data/redis/redis-2.2.11/doc/Hashes.html +37 -0
  78. data/redis/redis-2.2.11/doc/HdelCommand.html +39 -0
  79. data/redis/redis-2.2.11/doc/HexistsCommand.html +39 -0
  80. data/redis/redis-2.2.11/doc/HgetCommand.html +39 -0
  81. data/redis/redis-2.2.11/doc/HgetallCommand.html +40 -0
  82. data/redis/redis-2.2.11/doc/HincrbyCommand.html +45 -0
  83. data/redis/redis-2.2.11/doc/HlenCommand.html +38 -0
  84. data/redis/redis-2.2.11/doc/HmgetCommand.html +40 -0
  85. data/redis/redis-2.2.11/doc/HmsetCommand.html +40 -0
  86. data/redis/redis-2.2.11/doc/HsetCommand.html +40 -0
  87. data/redis/redis-2.2.11/doc/HsetnxCommand.html +41 -0
  88. data/redis/redis-2.2.11/doc/IncrCommand.html +43 -0
  89. data/redis/redis-2.2.11/doc/InfoCommand.html +48 -0
  90. data/redis/redis-2.2.11/doc/IntroductionToRedisDataTypes.html +152 -0
  91. data/redis/redis-2.2.11/doc/KeysCommand.html +43 -0
  92. data/redis/redis-2.2.11/doc/LastsaveCommand.html +39 -0
  93. data/redis/redis-2.2.11/doc/LindexCommand.html +40 -0
  94. data/redis/redis-2.2.11/doc/ListCommandsSidebar.html +36 -0
  95. data/redis/redis-2.2.11/doc/Lists.html +42 -0
  96. data/redis/redis-2.2.11/doc/LlenCommand.html +41 -0
  97. data/redis/redis-2.2.11/doc/LpopCommand.html +41 -0
  98. data/redis/redis-2.2.11/doc/LrangeCommand.html +47 -0
  99. data/redis/redis-2.2.11/doc/LremCommand.html +41 -0
  100. data/redis/redis-2.2.11/doc/LsetCommand.html +38 -0
  101. data/redis/redis-2.2.11/doc/LtrimCommand.html +47 -0
  102. data/redis/redis-2.2.11/doc/MgetCommand.html +52 -0
  103. data/redis/redis-2.2.11/doc/MonitorCommand.html +63 -0
  104. data/redis/redis-2.2.11/doc/MoveCommand.html +42 -0
  105. data/redis/redis-2.2.11/doc/MsetCommand.html +44 -0
  106. data/redis/redis-2.2.11/doc/MultiExecCommand.html +166 -0
  107. data/redis/redis-2.2.11/doc/NonexistentCommands.html +51 -0
  108. data/redis/redis-2.2.11/doc/ObjectHashMappers.html +39 -0
  109. data/redis/redis-2.2.11/doc/Pipelining.html +36 -0
  110. data/redis/redis-2.2.11/doc/ProgrammingExamples.html +38 -0
  111. data/redis/redis-2.2.11/doc/ProtocolSpecification.html +137 -0
  112. data/redis/redis-2.2.11/doc/PublishSubscribe.html +115 -0
  113. data/redis/redis-2.2.11/doc/QuickStart.html +68 -0
  114. data/redis/redis-2.2.11/doc/QuitCommand.html +38 -0
  115. data/redis/redis-2.2.11/doc/README.html +119 -0
  116. data/redis/redis-2.2.11/doc/RandomkeyCommand.html +39 -0
  117. data/redis/redis-2.2.11/doc/Redis0100ChangeLog.html +67 -0
  118. data/redis/redis-2.2.11/doc/Redis0900ChangeLog.html +56 -0
  119. data/redis/redis-2.2.11/doc/RedisBigData.html +61 -0
  120. data/redis/redis-2.2.11/doc/RedisCLI.html +37 -0
  121. data/redis/redis-2.2.11/doc/RedisEventLibrary.html +70 -0
  122. data/redis/redis-2.2.11/doc/RedisGuides.html +37 -0
  123. data/redis/redis-2.2.11/doc/RedisInternals.html +38 -0
  124. data/redis/redis-2.2.11/doc/RedisPipelining.html +93 -0
  125. data/redis/redis-2.2.11/doc/RedisStatus.html +56 -0
  126. data/redis/redis-2.2.11/doc/Redis_1_2_0_Changelog.html +40 -0
  127. data/redis/redis-2.2.11/doc/Redis_2_0_0_Changelog.html +62 -0
  128. data/redis/redis-2.2.11/doc/Redis_2_0_Whats_new.html +59 -0
  129. data/redis/redis-2.2.11/doc/RenameCommand.html +39 -0
  130. data/redis/redis-2.2.11/doc/RenamenxCommand.html +42 -0
  131. data/redis/redis-2.2.11/doc/ReplicationHowto.html +43 -0
  132. data/redis/redis-2.2.11/doc/ReplyTypes.html +42 -0
  133. data/redis/redis-2.2.11/doc/RoadMap.html +38 -0
  134. data/redis/redis-2.2.11/doc/RpoplpushCommand.html +44 -0
  135. data/redis/redis-2.2.11/doc/RpushCommand.html +40 -0
  136. data/redis/redis-2.2.11/doc/SaddCommand.html +41 -0
  137. data/redis/redis-2.2.11/doc/SaveCommand.html +39 -0
  138. data/redis/redis-2.2.11/doc/ScardCommand.html +41 -0
  139. data/redis/redis-2.2.11/doc/SdiffCommand.html +45 -0
  140. data/redis/redis-2.2.11/doc/SdiffstoreCommand.html +38 -0
  141. data/redis/redis-2.2.11/doc/SelectCommand.html +39 -0
  142. data/redis/redis-2.2.11/doc/SetCommand.html +39 -0
  143. data/redis/redis-2.2.11/doc/SetCommandsSidebar.html +36 -0
  144. data/redis/redis-2.2.11/doc/SetbitCommand.html +45 -0
  145. data/redis/redis-2.2.11/doc/SetexCommand.html +42 -0
  146. data/redis/redis-2.2.11/doc/SetnxCommand.html +51 -0
  147. data/redis/redis-2.2.11/doc/SetrangeCommand.html +58 -0
  148. data/redis/redis-2.2.11/doc/Sets.html +36 -0
  149. data/redis/redis-2.2.11/doc/ShutdownCommand.html +39 -0
  150. data/redis/redis-2.2.11/doc/SideBar.html +36 -0
  151. data/redis/redis-2.2.11/doc/SinterCommand.html +40 -0
  152. data/redis/redis-2.2.11/doc/SinterstoreCommand.html +39 -0
  153. data/redis/redis-2.2.11/doc/SismemberCommand.html +42 -0
  154. data/redis/redis-2.2.11/doc/SlaveofCommand.html +41 -0
  155. data/redis/redis-2.2.11/doc/SmembersCommand.html +38 -0
  156. data/redis/redis-2.2.11/doc/SmoveCommand.html +44 -0
  157. data/redis/redis-2.2.11/doc/SortCommand.html +75 -0
  158. data/redis/redis-2.2.11/doc/SortedSetCommandsSidebar.html +36 -0
  159. data/redis/redis-2.2.11/doc/SortedSets.html +36 -0
  160. data/redis/redis-2.2.11/doc/Speed.html +38 -0
  161. data/redis/redis-2.2.11/doc/SponsorshipHistory.html +38 -0
  162. data/redis/redis-2.2.11/doc/SpopCommand.html +40 -0
  163. data/redis/redis-2.2.11/doc/SrandmemberCommand.html +40 -0
  164. data/redis/redis-2.2.11/doc/SremCommand.html +42 -0
  165. data/redis/redis-2.2.11/doc/StringCommandsSidebar.html +36 -0
  166. data/redis/redis-2.2.11/doc/Strings.html +37 -0
  167. data/redis/redis-2.2.11/doc/StrlenCommand.html +39 -0
  168. data/redis/redis-2.2.11/doc/SubstrCommand.html +52 -0
  169. data/redis/redis-2.2.11/doc/SunionCommand.html +40 -0
  170. data/redis/redis-2.2.11/doc/SunionstoreCommand.html +38 -0
  171. data/redis/redis-2.2.11/doc/SupportedLanguages.html +60 -0
  172. data/redis/redis-2.2.11/doc/SupportedPlatforms.html +37 -0
  173. data/redis/redis-2.2.11/doc/TemplateCommand.html +38 -0
  174. data/redis/redis-2.2.11/doc/TtlCommand.html +38 -0
  175. data/redis/redis-2.2.11/doc/TwitterAlikeExample.html +250 -0
  176. data/redis/redis-2.2.11/doc/TypeCommand.html +46 -0
  177. data/redis/redis-2.2.11/doc/UnstableSource.html +39 -0
  178. data/redis/redis-2.2.11/doc/VirtualMemorySpecification.html +156 -0
  179. data/redis/redis-2.2.11/doc/VirtualMemoryUserGuide.html +66 -0
  180. data/redis/redis-2.2.11/doc/ZaddCommand.html +43 -0
  181. data/redis/redis-2.2.11/doc/ZcardCommand.html +41 -0
  182. data/redis/redis-2.2.11/doc/ZincrbyCommand.html +42 -0
  183. data/redis/redis-2.2.11/doc/ZrangeCommand.html +42 -0
  184. data/redis/redis-2.2.11/doc/ZrangebyscoreCommand.html +77 -0
  185. data/redis/redis-2.2.11/doc/ZrankCommand.html +43 -0
  186. data/redis/redis-2.2.11/doc/ZremCommand.html +42 -0
  187. data/redis/redis-2.2.11/doc/ZremrangebyrankCommand.html +39 -0
  188. data/redis/redis-2.2.11/doc/ZremrangebyscoreCommand.html +39 -0
  189. data/redis/redis-2.2.11/doc/ZscoreCommand.html +41 -0
  190. data/redis/redis-2.2.11/doc/ZunionCommand.html +42 -0
  191. data/redis/redis-2.2.11/doc/ZunionstoreCommand.html +43 -0
  192. data/redis/redis-2.2.11/doc/index.html +43 -0
  193. data/redis/redis-2.2.11/doc/redis.png +0 -0
  194. data/redis/redis-2.2.11/doc/style.css +25 -0
  195. data/redis/redis-2.2.11/redis.conf +417 -0
  196. data/redis/redis-2.2.11/src/Makefile +177 -0
  197. data/redis/redis-2.2.11/src/adlist.c +325 -0
  198. data/redis/redis-2.2.11/src/adlist.h +92 -0
  199. data/redis/redis-2.2.11/src/ae.c +390 -0
  200. data/redis/redis-2.2.11/src/ae.h +117 -0
  201. data/redis/redis-2.2.11/src/ae_epoll.c +91 -0
  202. data/redis/redis-2.2.11/src/ae_kqueue.c +93 -0
  203. data/redis/redis-2.2.11/src/ae_select.c +72 -0
  204. data/redis/redis-2.2.11/src/anet.c +347 -0
  205. data/redis/redis-2.2.11/src/anet.h +57 -0
  206. data/redis/redis-2.2.11/src/aof.c +675 -0
  207. data/redis/redis-2.2.11/src/config.c +627 -0
  208. data/redis/redis-2.2.11/src/config.h +64 -0
  209. data/redis/redis-2.2.11/src/db.c +543 -0
  210. data/redis/redis-2.2.11/src/debug.c +314 -0
  211. data/redis/redis-2.2.11/src/dict.c +721 -0
  212. data/redis/redis-2.2.11/src/dict.h +156 -0
  213. data/redis/redis-2.2.11/src/fmacros.h +15 -0
  214. data/redis/redis-2.2.11/src/help.h +638 -0
  215. data/redis/redis-2.2.11/src/intset.c +422 -0
  216. data/redis/redis-2.2.11/src/intset.h +19 -0
  217. data/redis/redis-2.2.11/src/lzf.h +100 -0
  218. data/redis/redis-2.2.11/src/lzfP.h +159 -0
  219. data/redis/redis-2.2.11/src/lzf_c.c +295 -0
  220. data/redis/redis-2.2.11/src/lzf_d.c +150 -0
  221. data/redis/redis-2.2.11/src/mkreleasehdr.sh +9 -0
  222. data/redis/redis-2.2.11/src/multi.c +268 -0
  223. data/redis/redis-2.2.11/src/networking.c +899 -0
  224. data/redis/redis-2.2.11/src/object.c +484 -0
  225. data/redis/redis-2.2.11/src/pqsort.c +197 -0
  226. data/redis/redis-2.2.11/src/pqsort.h +15 -0
  227. data/redis/redis-2.2.11/src/pubsub.c +267 -0
  228. data/redis/redis-2.2.11/src/rdb.c +1020 -0
  229. data/redis/redis-2.2.11/src/redis-benchmark.c +530 -0
  230. data/redis/redis-2.2.11/src/redis-check-aof.c +185 -0
  231. data/redis/redis-2.2.11/src/redis-check-dump.c +681 -0
  232. data/redis/redis-2.2.11/src/redis-cli.c +773 -0
  233. data/redis/redis-2.2.11/src/redis.c +1677 -0
  234. data/redis/redis-2.2.11/src/redis.h +1022 -0
  235. data/redis/redis-2.2.11/src/release.c +13 -0
  236. data/redis/redis-2.2.11/src/replication.c +557 -0
  237. data/redis/redis-2.2.11/src/sds.c +639 -0
  238. data/redis/redis-2.2.11/src/sds.h +78 -0
  239. data/redis/redis-2.2.11/src/sha1.c +276 -0
  240. data/redis/redis-2.2.11/src/sha1.h +17 -0
  241. data/redis/redis-2.2.11/src/solarisfixes.h +22 -0
  242. data/redis/redis-2.2.11/src/sort.c +389 -0
  243. data/redis/redis-2.2.11/src/syncio.c +154 -0
  244. data/redis/redis-2.2.11/src/t_hash.c +476 -0
  245. data/redis/redis-2.2.11/src/t_list.c +986 -0
  246. data/redis/redis-2.2.11/src/t_set.c +610 -0
  247. data/redis/redis-2.2.11/src/t_string.c +438 -0
  248. data/redis/redis-2.2.11/src/t_zset.c +1084 -0
  249. data/redis/redis-2.2.11/src/testhelp.h +54 -0
  250. data/redis/redis-2.2.11/src/util.c +243 -0
  251. data/redis/redis-2.2.11/src/valgrind.sup +5 -0
  252. data/redis/redis-2.2.11/src/version.h +1 -0
  253. data/redis/redis-2.2.11/src/vm.c +1149 -0
  254. data/redis/redis-2.2.11/src/ziplist.c +1323 -0
  255. data/redis/redis-2.2.11/src/ziplist.h +15 -0
  256. data/redis/redis-2.2.11/src/zipmap.c +455 -0
  257. data/redis/redis-2.2.11/src/zipmap.h +48 -0
  258. data/redis/redis-2.2.11/src/zmalloc.c +278 -0
  259. data/redis/redis-2.2.11/src/zmalloc.h +47 -0
  260. data/redis/redis-2.2.11/tests/assets/default.conf +308 -0
  261. data/redis/redis-2.2.11/tests/integration/aof.tcl +104 -0
  262. data/redis/redis-2.2.11/tests/integration/redis-cli.tcl +208 -0
  263. data/redis/redis-2.2.11/tests/integration/replication.tcl +98 -0
  264. data/redis/redis-2.2.11/tests/support/redis.tcl +241 -0
  265. data/redis/redis-2.2.11/tests/support/server.tcl +294 -0
  266. data/redis/redis-2.2.11/tests/support/test.tcl +190 -0
  267. data/redis/redis-2.2.11/tests/support/tmpfile.tcl +15 -0
  268. data/redis/redis-2.2.11/tests/support/util.tcl +296 -0
  269. data/redis/redis-2.2.11/tests/test_helper.tcl +221 -0
  270. data/redis/redis-2.2.11/tests/unit/auth.tcl +15 -0
  271. data/redis/redis-2.2.11/tests/unit/basic.tcl +616 -0
  272. data/redis/redis-2.2.11/tests/unit/cas.tcl +135 -0
  273. data/redis/redis-2.2.11/tests/unit/expire.tcl +74 -0
  274. data/redis/redis-2.2.11/tests/unit/other.tcl +240 -0
  275. data/redis/redis-2.2.11/tests/unit/printver.tcl +6 -0
  276. data/redis/redis-2.2.11/tests/unit/protocol.tcl +62 -0
  277. data/redis/redis-2.2.11/tests/unit/pubsub.tcl +195 -0
  278. data/redis/redis-2.2.11/tests/unit/quit.tcl +40 -0
  279. data/redis/redis-2.2.11/tests/unit/sort.tcl +189 -0
  280. data/redis/redis-2.2.11/tests/unit/type/hash.tcl +300 -0
  281. data/redis/redis-2.2.11/tests/unit/type/list.tcl +819 -0
  282. data/redis/redis-2.2.11/tests/unit/type/set.tcl +334 -0
  283. data/redis/redis-2.2.11/tests/unit/type/zset.tcl +587 -0
  284. data/redis/redis-2.2.11/utils/build-static-symbols.tcl +22 -0
  285. data/redis/redis-2.2.11/utils/generate-command-help.rb +112 -0
  286. data/redis/redis-2.2.11/utils/mktarball.sh +13 -0
  287. data/redis/redis-2.2.11/utils/redis-copy.rb +78 -0
  288. data/redis/redis-2.2.11/utils/redis-sha1.rb +52 -0
  289. data/redis/redis-2.2.11/utils/redis_init_script +42 -0
  290. metadata +362 -0
@@ -0,0 +1,773 @@
1
+ /* Redis CLI (command line interface)
2
+ *
3
+ * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of Redis nor the names of its contributors may be used
15
+ * to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include "fmacros.h"
32
+ #include "version.h"
33
+
34
+ #include <stdio.h>
35
+ #include <string.h>
36
+ #include <stdlib.h>
37
+ #include <unistd.h>
38
+ #include <ctype.h>
39
+ #include <errno.h>
40
+ #include <sys/stat.h>
41
+ #include <sys/time.h>
42
+ #include <assert.h>
43
+
44
+ #include "hiredis.h"
45
+ #include "sds.h"
46
+ #include "zmalloc.h"
47
+ #include "linenoise.h"
48
+ #include "help.h"
49
+
50
+ #define REDIS_NOTUSED(V) ((void) V)
51
+
52
+ static redisContext *context;
53
+ static struct config {
54
+ char *hostip;
55
+ int hostport;
56
+ char *hostsocket;
57
+ long repeat;
58
+ long interval;
59
+ int dbnum;
60
+ int interactive;
61
+ int shutdown;
62
+ int monitor_mode;
63
+ int pubsub_mode;
64
+ int stdinarg; /* get last arg from stdin. (-x option) */
65
+ char *auth;
66
+ int raw_output; /* output mode per command */
67
+ sds mb_delim;
68
+ char prompt[32];
69
+ } config;
70
+
71
+ static void usage();
72
+ char *redisGitSHA1(void);
73
+ char *redisGitDirty(void);
74
+
75
+ /*------------------------------------------------------------------------------
76
+ * Utility functions
77
+ *--------------------------------------------------------------------------- */
78
+
79
+ static long long mstime(void) {
80
+ struct timeval tv;
81
+ long long mst;
82
+
83
+ gettimeofday(&tv, NULL);
84
+ mst = ((long)tv.tv_sec)*1000;
85
+ mst += tv.tv_usec/1000;
86
+ return mst;
87
+ }
88
+
89
+ static void cliRefreshPrompt(void) {
90
+ if (config.dbnum == 0)
91
+ snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d> ",
92
+ config.hostip, config.hostport);
93
+ else
94
+ snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d[%d]> ",
95
+ config.hostip, config.hostport, config.dbnum);
96
+ }
97
+
98
+ /*------------------------------------------------------------------------------
99
+ * Help functions
100
+ *--------------------------------------------------------------------------- */
101
+
102
+ #define CLI_HELP_COMMAND 1
103
+ #define CLI_HELP_GROUP 2
104
+
105
+ typedef struct {
106
+ int type;
107
+ int argc;
108
+ sds *argv;
109
+ sds full;
110
+
111
+ /* Only used for help on commands */
112
+ struct commandHelp *org;
113
+ } helpEntry;
114
+
115
+ static helpEntry *helpEntries;
116
+ static int helpEntriesLen;
117
+
118
+ static sds cliVersion() {
119
+ sds version;
120
+ version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
121
+
122
+ /* Add git commit and working tree status when available */
123
+ if (strtoll(redisGitSHA1(),NULL,16)) {
124
+ version = sdscatprintf(version, " (git:%s", redisGitSHA1());
125
+ if (strtoll(redisGitDirty(),NULL,10))
126
+ version = sdscatprintf(version, "-dirty");
127
+ version = sdscat(version, ")");
128
+ }
129
+ return version;
130
+ }
131
+
132
+ static void cliInitHelp() {
133
+ int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
134
+ int groupslen = sizeof(commandGroups)/sizeof(char*);
135
+ int i, len, pos = 0;
136
+ helpEntry tmp;
137
+
138
+ helpEntriesLen = len = commandslen+groupslen;
139
+ helpEntries = malloc(sizeof(helpEntry)*len);
140
+
141
+ for (i = 0; i < groupslen; i++) {
142
+ tmp.argc = 1;
143
+ tmp.argv = malloc(sizeof(sds));
144
+ tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
145
+ tmp.full = tmp.argv[0];
146
+ tmp.type = CLI_HELP_GROUP;
147
+ tmp.org = NULL;
148
+ helpEntries[pos++] = tmp;
149
+ }
150
+
151
+ for (i = 0; i < commandslen; i++) {
152
+ tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
153
+ tmp.full = sdsnew(commandHelp[i].name);
154
+ tmp.type = CLI_HELP_COMMAND;
155
+ tmp.org = &commandHelp[i];
156
+ helpEntries[pos++] = tmp;
157
+ }
158
+ }
159
+
160
+ /* Output command help to stdout. */
161
+ static void cliOutputCommandHelp(struct commandHelp *help, int group) {
162
+ printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
163
+ printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
164
+ printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since);
165
+ if (group) {
166
+ printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
167
+ }
168
+ }
169
+
170
+ /* Print generic help. */
171
+ static void cliOutputGenericHelp() {
172
+ sds version = cliVersion();
173
+ printf(
174
+ "redis-cli %s\r\n"
175
+ "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
176
+ " \"help <command>\" for help on <command>\r\n"
177
+ " \"help <tab>\" to get a list of possible help topics\r\n"
178
+ " \"quit\" to exit\r\n",
179
+ version
180
+ );
181
+ sdsfree(version);
182
+ }
183
+
184
+ /* Output all command help, filtering by group or command name. */
185
+ static void cliOutputHelp(int argc, char **argv) {
186
+ int i, j, len;
187
+ int group = -1;
188
+ helpEntry *entry;
189
+ struct commandHelp *help;
190
+
191
+ if (argc == 0) {
192
+ cliOutputGenericHelp();
193
+ return;
194
+ } else if (argc > 0 && argv[0][0] == '@') {
195
+ len = sizeof(commandGroups)/sizeof(char*);
196
+ for (i = 0; i < len; i++) {
197
+ if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
198
+ group = i;
199
+ break;
200
+ }
201
+ }
202
+ }
203
+
204
+ assert(argc > 0);
205
+ for (i = 0; i < helpEntriesLen; i++) {
206
+ entry = &helpEntries[i];
207
+ if (entry->type != CLI_HELP_COMMAND) continue;
208
+
209
+ help = entry->org;
210
+ if (group == -1) {
211
+ /* Compare all arguments */
212
+ if (argc == entry->argc) {
213
+ for (j = 0; j < argc; j++) {
214
+ if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
215
+ }
216
+ if (j == argc) {
217
+ cliOutputCommandHelp(help,1);
218
+ }
219
+ }
220
+ } else {
221
+ if (group == help->group) {
222
+ cliOutputCommandHelp(help,0);
223
+ }
224
+ }
225
+ }
226
+ printf("\r\n");
227
+ }
228
+
229
+ static void completionCallback(const char *buf, linenoiseCompletions *lc) {
230
+ size_t startpos = 0;
231
+ int mask;
232
+ int i;
233
+ size_t matchlen;
234
+ sds tmp;
235
+
236
+ if (strncasecmp(buf,"help ",5) == 0) {
237
+ startpos = 5;
238
+ while (isspace(buf[startpos])) startpos++;
239
+ mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
240
+ } else {
241
+ mask = CLI_HELP_COMMAND;
242
+ }
243
+
244
+ for (i = 0; i < helpEntriesLen; i++) {
245
+ if (!(helpEntries[i].type & mask)) continue;
246
+
247
+ matchlen = strlen(buf+startpos);
248
+ if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
249
+ tmp = sdsnewlen(buf,startpos);
250
+ tmp = sdscat(tmp,helpEntries[i].full);
251
+ linenoiseAddCompletion(lc,tmp);
252
+ sdsfree(tmp);
253
+ }
254
+ }
255
+ }
256
+
257
+ /*------------------------------------------------------------------------------
258
+ * Networking / parsing
259
+ *--------------------------------------------------------------------------- */
260
+
261
+ /* Send AUTH command to the server */
262
+ static int cliAuth() {
263
+ redisReply *reply;
264
+ if (config.auth == NULL) return REDIS_OK;
265
+
266
+ reply = redisCommand(context,"AUTH %s",config.auth);
267
+ if (reply != NULL) {
268
+ freeReplyObject(reply);
269
+ return REDIS_OK;
270
+ }
271
+ return REDIS_ERR;
272
+ }
273
+
274
+ /* Send SELECT dbnum to the server */
275
+ static int cliSelect() {
276
+ redisReply *reply;
277
+ if (config.dbnum == 0) return REDIS_OK;
278
+
279
+ reply = redisCommand(context,"SELECT %d",config.dbnum);
280
+ if (reply != NULL) {
281
+ freeReplyObject(reply);
282
+ return REDIS_OK;
283
+ }
284
+ return REDIS_ERR;
285
+ }
286
+
287
+ /* Connect to the client. If force is not zero the connection is performed
288
+ * even if there is already a connected socket. */
289
+ static int cliConnect(int force) {
290
+ if (context == NULL || force) {
291
+ if (context != NULL)
292
+ redisFree(context);
293
+
294
+ if (config.hostsocket == NULL) {
295
+ context = redisConnect(config.hostip,config.hostport);
296
+ } else {
297
+ context = redisConnectUnix(config.hostsocket);
298
+ }
299
+
300
+ if (context->err) {
301
+ fprintf(stderr,"Could not connect to Redis at ");
302
+ if (config.hostsocket == NULL)
303
+ fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
304
+ else
305
+ fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
306
+ redisFree(context);
307
+ context = NULL;
308
+ return REDIS_ERR;
309
+ }
310
+
311
+ /* Do AUTH and select the right DB. */
312
+ if (cliAuth() != REDIS_OK)
313
+ return REDIS_ERR;
314
+ if (cliSelect() != REDIS_OK)
315
+ return REDIS_ERR;
316
+ }
317
+ return REDIS_OK;
318
+ }
319
+
320
+ static void cliPrintContextError() {
321
+ if (context == NULL) return;
322
+ fprintf(stderr,"Error: %s\n",context->errstr);
323
+ }
324
+
325
+ static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
326
+ sds out = sdsempty();
327
+ switch (r->type) {
328
+ case REDIS_REPLY_ERROR:
329
+ out = sdscatprintf(out,"(error) %s\n", r->str);
330
+ break;
331
+ case REDIS_REPLY_STATUS:
332
+ out = sdscat(out,r->str);
333
+ out = sdscat(out,"\n");
334
+ break;
335
+ case REDIS_REPLY_INTEGER:
336
+ out = sdscatprintf(out,"(integer) %lld\n",r->integer);
337
+ break;
338
+ case REDIS_REPLY_STRING:
339
+ /* If you are producing output for the standard output we want
340
+ * a more interesting output with quoted characters and so forth */
341
+ out = sdscatrepr(out,r->str,r->len);
342
+ out = sdscat(out,"\n");
343
+ break;
344
+ case REDIS_REPLY_NIL:
345
+ out = sdscat(out,"(nil)\n");
346
+ break;
347
+ case REDIS_REPLY_ARRAY:
348
+ if (r->elements == 0) {
349
+ out = sdscat(out,"(empty list or set)\n");
350
+ } else {
351
+ unsigned int i, idxlen = 0;
352
+ char _prefixlen[16];
353
+ char _prefixfmt[16];
354
+ sds _prefix;
355
+ sds tmp;
356
+
357
+ /* Calculate chars needed to represent the largest index */
358
+ i = r->elements;
359
+ do {
360
+ idxlen++;
361
+ i /= 10;
362
+ } while(i);
363
+
364
+ /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
365
+ memset(_prefixlen,' ',idxlen+2);
366
+ _prefixlen[idxlen+2] = '\0';
367
+ _prefix = sdscat(sdsnew(prefix),_prefixlen);
368
+
369
+ /* Setup prefix format for every entry */
370
+ snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
371
+
372
+ for (i = 0; i < r->elements; i++) {
373
+ /* Don't use the prefix for the first element, as the parent
374
+ * caller already prepended the index number. */
375
+ out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
376
+
377
+ /* Format the multi bulk entry */
378
+ tmp = cliFormatReplyTTY(r->element[i],_prefix);
379
+ out = sdscatlen(out,tmp,sdslen(tmp));
380
+ sdsfree(tmp);
381
+ }
382
+ sdsfree(_prefix);
383
+ }
384
+ break;
385
+ default:
386
+ fprintf(stderr,"Unknown reply type: %d\n", r->type);
387
+ exit(1);
388
+ }
389
+ return out;
390
+ }
391
+
392
+ static sds cliFormatReplyRaw(redisReply *r) {
393
+ sds out = sdsempty(), tmp;
394
+ size_t i;
395
+
396
+ switch (r->type) {
397
+ case REDIS_REPLY_NIL:
398
+ /* Nothing... */
399
+ break;
400
+ case REDIS_REPLY_ERROR:
401
+ out = sdscatlen(out,r->str,r->len);
402
+ out = sdscatlen(out,"\n",1);
403
+ break;
404
+ case REDIS_REPLY_STATUS:
405
+ case REDIS_REPLY_STRING:
406
+ out = sdscatlen(out,r->str,r->len);
407
+ break;
408
+ case REDIS_REPLY_INTEGER:
409
+ out = sdscatprintf(out,"%lld",r->integer);
410
+ break;
411
+ case REDIS_REPLY_ARRAY:
412
+ for (i = 0; i < r->elements; i++) {
413
+ if (i > 0) out = sdscat(out,config.mb_delim);
414
+ tmp = cliFormatReplyRaw(r->element[i]);
415
+ out = sdscatlen(out,tmp,sdslen(tmp));
416
+ sdsfree(tmp);
417
+ }
418
+ break;
419
+ default:
420
+ fprintf(stderr,"Unknown reply type: %d\n", r->type);
421
+ exit(1);
422
+ }
423
+ return out;
424
+ }
425
+
426
+ static int cliReadReply(int output_raw_strings) {
427
+ void *_reply;
428
+ redisReply *reply;
429
+ sds out;
430
+
431
+ if (redisGetReply(context,&_reply) != REDIS_OK) {
432
+ if (config.shutdown)
433
+ return REDIS_OK;
434
+ if (config.interactive) {
435
+ /* Filter cases where we should reconnect */
436
+ if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
437
+ return REDIS_ERR;
438
+ if (context->err == REDIS_ERR_EOF)
439
+ return REDIS_ERR;
440
+ }
441
+ cliPrintContextError();
442
+ exit(1);
443
+ return REDIS_ERR; /* avoid compiler warning */
444
+ }
445
+
446
+ reply = (redisReply*)_reply;
447
+ if (output_raw_strings) {
448
+ out = cliFormatReplyRaw(reply);
449
+ } else {
450
+ if (config.raw_output) {
451
+ out = cliFormatReplyRaw(reply);
452
+ out = sdscat(out,"\n");
453
+ } else {
454
+ out = cliFormatReplyTTY(reply,"");
455
+ }
456
+ }
457
+ fwrite(out,sdslen(out),1,stdout);
458
+ sdsfree(out);
459
+ freeReplyObject(reply);
460
+ return REDIS_OK;
461
+ }
462
+
463
+ static int cliSendCommand(int argc, char **argv, int repeat) {
464
+ char *command = argv[0];
465
+ size_t *argvlen;
466
+ int j, output_raw;
467
+
468
+ if (context == NULL) return REDIS_ERR;
469
+
470
+ output_raw = 0;
471
+ if (!strcasecmp(command,"info") ||
472
+ (argc == 2 && !strcasecmp(command,"client") &&
473
+ !strcasecmp(argv[1],"list")))
474
+
475
+ {
476
+ output_raw = 1;
477
+ }
478
+
479
+ if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
480
+ cliOutputHelp(--argc, ++argv);
481
+ return REDIS_OK;
482
+ }
483
+ if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
484
+ if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
485
+ if (!strcasecmp(command,"subscribe") ||
486
+ !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
487
+
488
+ /* Setup argument length */
489
+ argvlen = malloc(argc*sizeof(size_t));
490
+ for (j = 0; j < argc; j++)
491
+ argvlen[j] = sdslen(argv[j]);
492
+
493
+ while(repeat--) {
494
+ redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
495
+ while (config.monitor_mode) {
496
+ if (cliReadReply(output_raw) != REDIS_OK) exit(1);
497
+ fflush(stdout);
498
+ }
499
+
500
+ if (config.pubsub_mode) {
501
+ if (!config.raw_output)
502
+ printf("Reading messages... (press Ctrl-C to quit)\n");
503
+ while (1) {
504
+ if (cliReadReply(output_raw) != REDIS_OK) exit(1);
505
+ }
506
+ }
507
+
508
+ if (cliReadReply(output_raw) != REDIS_OK) {
509
+ free(argvlen);
510
+ return REDIS_ERR;
511
+ } else {
512
+ /* Store database number when SELECT was successfully executed. */
513
+ if (!strcasecmp(command,"select") && argc == 2) {
514
+ config.dbnum = atoi(argv[1]);
515
+ cliRefreshPrompt();
516
+ }
517
+ }
518
+ if (config.interval) usleep(config.interval);
519
+ fflush(stdout); /* Make it grep friendly */
520
+ }
521
+
522
+ free(argvlen);
523
+ return REDIS_OK;
524
+ }
525
+
526
+ /*------------------------------------------------------------------------------
527
+ * User interface
528
+ *--------------------------------------------------------------------------- */
529
+
530
+ static int parseOptions(int argc, char **argv) {
531
+ int i;
532
+
533
+ for (i = 1; i < argc; i++) {
534
+ int lastarg = i==argc-1;
535
+
536
+ if (!strcmp(argv[i],"-h") && !lastarg) {
537
+ sdsfree(config.hostip);
538
+ config.hostip = sdsnew(argv[i+1]);
539
+ i++;
540
+ } else if (!strcmp(argv[i],"-h") && lastarg) {
541
+ usage();
542
+ } else if (!strcmp(argv[i],"--help")) {
543
+ usage();
544
+ } else if (!strcmp(argv[i],"-x")) {
545
+ config.stdinarg = 1;
546
+ } else if (!strcmp(argv[i],"-p") && !lastarg) {
547
+ config.hostport = atoi(argv[i+1]);
548
+ i++;
549
+ } else if (!strcmp(argv[i],"-s") && !lastarg) {
550
+ config.hostsocket = argv[i+1];
551
+ i++;
552
+ } else if (!strcmp(argv[i],"-r") && !lastarg) {
553
+ config.repeat = strtoll(argv[i+1],NULL,10);
554
+ i++;
555
+ } else if (!strcmp(argv[i],"-i") && !lastarg) {
556
+ double seconds = atof(argv[i+1]);
557
+ config.interval = seconds*1000000;
558
+ i++;
559
+ } else if (!strcmp(argv[i],"-n") && !lastarg) {
560
+ config.dbnum = atoi(argv[i+1]);
561
+ i++;
562
+ } else if (!strcmp(argv[i],"-a") && !lastarg) {
563
+ config.auth = argv[i+1];
564
+ i++;
565
+ } else if (!strcmp(argv[i],"--raw")) {
566
+ config.raw_output = 1;
567
+ } else if (!strcmp(argv[i],"-d") && !lastarg) {
568
+ sdsfree(config.mb_delim);
569
+ config.mb_delim = sdsnew(argv[i+1]);
570
+ i++;
571
+ } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
572
+ sds version = cliVersion();
573
+ printf("redis-cli %s\n", version);
574
+ sdsfree(version);
575
+ exit(0);
576
+ } else {
577
+ break;
578
+ }
579
+ }
580
+ return i;
581
+ }
582
+
583
+ static sds readArgFromStdin(void) {
584
+ char buf[1024];
585
+ sds arg = sdsempty();
586
+
587
+ while(1) {
588
+ int nread = read(fileno(stdin),buf,1024);
589
+
590
+ if (nread == 0) break;
591
+ else if (nread == -1) {
592
+ perror("Reading from standard input");
593
+ exit(1);
594
+ }
595
+ arg = sdscatlen(arg,buf,nread);
596
+ }
597
+ return arg;
598
+ }
599
+
600
+ static void usage() {
601
+ sds version = cliVersion();
602
+ fprintf(stderr,
603
+ "redis-cli %s\n"
604
+ "\n"
605
+ "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
606
+ " -h <hostname> Server hostname (default: 127.0.0.1)\n"
607
+ " -p <port> Server port (default: 6379)\n"
608
+ " -s <socket> Server socket (overrides hostname and port)\n"
609
+ " -a <password> Password to use when connecting to the server\n"
610
+ " -r <repeat> Execute specified command N times\n"
611
+ " -i <interval> When -r is used, waits <interval> seconds per command.\n"
612
+ " It is possible to specify sub-second times like -i 0.1.\n"
613
+ " -n <db> Database number\n"
614
+ " -x Read last argument from STDIN\n"
615
+ " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
616
+ " --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
617
+ " --help Output this help and exit\n"
618
+ " --version Output version and exit\n"
619
+ "\n"
620
+ "Examples:\n"
621
+ " cat /etc/passwd | redis-cli -x set mypasswd\n"
622
+ " redis-cli get mypasswd\n"
623
+ " redis-cli -r 100 lpush mylist x\n"
624
+ " redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
625
+ "\n"
626
+ "When no command is given, redis-cli starts in interactive mode.\n"
627
+ "Type \"help\" in interactive mode for information on available commands.\n"
628
+ "\n",
629
+ version);
630
+ sdsfree(version);
631
+ exit(1);
632
+ }
633
+
634
+ /* Turn the plain C strings into Sds strings */
635
+ static char **convertToSds(int count, char** args) {
636
+ int j;
637
+ char **sds = zmalloc(sizeof(char*)*count);
638
+
639
+ for(j = 0; j < count; j++)
640
+ sds[j] = sdsnew(args[j]);
641
+
642
+ return sds;
643
+ }
644
+
645
+ #define LINE_BUFLEN 4096
646
+ static void repl() {
647
+ sds historyfile = NULL;
648
+ int history = 0;
649
+ char *line;
650
+ int argc;
651
+ sds *argv;
652
+
653
+ config.interactive = 1;
654
+ linenoiseSetCompletionCallback(completionCallback);
655
+
656
+ /* Only use history when stdin is a tty. */
657
+ if (isatty(fileno(stdin))) {
658
+ history = 1;
659
+
660
+ if (getenv("HOME") != NULL) {
661
+ historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
662
+ linenoiseHistoryLoad(historyfile);
663
+ }
664
+ }
665
+
666
+ cliRefreshPrompt();
667
+ while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
668
+ if (line[0] != '\0') {
669
+ argv = sdssplitargs(line,&argc);
670
+ if (history) linenoiseHistoryAdd(line);
671
+ if (historyfile) linenoiseHistorySave(historyfile);
672
+
673
+ if (argv == NULL) {
674
+ printf("Invalid argument(s)\n");
675
+ continue;
676
+ } else if (argc > 0) {
677
+ if (strcasecmp(argv[0],"quit") == 0 ||
678
+ strcasecmp(argv[0],"exit") == 0)
679
+ {
680
+ exit(0);
681
+ } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
682
+ sdsfree(config.hostip);
683
+ config.hostip = sdsnew(argv[1]);
684
+ config.hostport = atoi(argv[2]);
685
+ cliConnect(1);
686
+ } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
687
+ linenoiseClearScreen();
688
+ } else {
689
+ long long start_time = mstime(), elapsed;
690
+ int repeat, skipargs = 0;
691
+
692
+ repeat = atoi(argv[0]);
693
+ if (repeat) {
694
+ skipargs = 1;
695
+ } else {
696
+ repeat = 1;
697
+ }
698
+
699
+ if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
700
+ != REDIS_OK)
701
+ {
702
+ cliConnect(1);
703
+
704
+ /* If we still cannot send the command print error.
705
+ * We'll try to reconnect the next time. */
706
+ if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
707
+ != REDIS_OK)
708
+ cliPrintContextError();
709
+ }
710
+ elapsed = mstime()-start_time;
711
+ if (elapsed >= 500) {
712
+ printf("(%.2fs)\n",(double)elapsed/1000);
713
+ }
714
+ }
715
+ }
716
+ /* Free the argument vector */
717
+ while(argc--) sdsfree(argv[argc]);
718
+ zfree(argv);
719
+ }
720
+ /* linenoise() returns malloc-ed lines like readline() */
721
+ free(line);
722
+ }
723
+ exit(0);
724
+ }
725
+
726
+ static int noninteractive(int argc, char **argv) {
727
+ int retval = 0;
728
+ if (config.stdinarg) {
729
+ argv = zrealloc(argv, (argc+1)*sizeof(char*));
730
+ argv[argc] = readArgFromStdin();
731
+ retval = cliSendCommand(argc+1, argv, config.repeat);
732
+ } else {
733
+ /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
734
+ retval = cliSendCommand(argc, argv, config.repeat);
735
+ }
736
+ return retval;
737
+ }
738
+
739
+ int main(int argc, char **argv) {
740
+ int firstarg;
741
+
742
+ config.hostip = sdsnew("127.0.0.1");
743
+ config.hostport = 6379;
744
+ config.hostsocket = NULL;
745
+ config.repeat = 1;
746
+ config.interval = 0;
747
+ config.dbnum = 0;
748
+ config.interactive = 0;
749
+ config.shutdown = 0;
750
+ config.monitor_mode = 0;
751
+ config.pubsub_mode = 0;
752
+ config.stdinarg = 0;
753
+ config.auth = NULL;
754
+ config.raw_output = !isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL);
755
+ config.mb_delim = sdsnew("\n");
756
+ cliInitHelp();
757
+
758
+ firstarg = parseOptions(argc,argv);
759
+ argc -= firstarg;
760
+ argv += firstarg;
761
+
762
+ /* Start interactive mode when no command is provided */
763
+ if (argc == 0) {
764
+ /* Note that in repl mode we don't abort on connection error.
765
+ * A new attempt will be performed for every command send. */
766
+ cliConnect(0);
767
+ repl();
768
+ }
769
+
770
+ /* Otherwise, we have some arguments to execute */
771
+ if (cliConnect(0) != REDIS_OK) exit(1);
772
+ return noninteractive(argc,convertToSds(argc,argv));
773
+ }