command.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include "command.h"
  4. #include "hiutil.h"
  5. #include "hiarray.h"
  6. static uint64_t cmd_id = 0; /* command id counter */
  7. /*
  8. * Return true, if the redis command take no key, otherwise
  9. * return false
  10. */
  11. static int
  12. redis_argz(struct cmd *r)
  13. {
  14. switch (r->type) {
  15. case CMD_REQ_REDIS_PING:
  16. case CMD_REQ_REDIS_QUIT:
  17. return 1;
  18. default:
  19. break;
  20. }
  21. return 0;
  22. }
  23. /*
  24. * Return true, if the redis command accepts no arguments, otherwise
  25. * return false
  26. */
  27. static int
  28. redis_arg0(struct cmd *r)
  29. {
  30. switch (r->type) {
  31. case CMD_REQ_REDIS_EXISTS:
  32. case CMD_REQ_REDIS_PERSIST:
  33. case CMD_REQ_REDIS_PTTL:
  34. case CMD_REQ_REDIS_SORT:
  35. case CMD_REQ_REDIS_TTL:
  36. case CMD_REQ_REDIS_TYPE:
  37. case CMD_REQ_REDIS_DUMP:
  38. case CMD_REQ_REDIS_DECR:
  39. case CMD_REQ_REDIS_GET:
  40. case CMD_REQ_REDIS_INCR:
  41. case CMD_REQ_REDIS_STRLEN:
  42. case CMD_REQ_REDIS_HGETALL:
  43. case CMD_REQ_REDIS_HKEYS:
  44. case CMD_REQ_REDIS_HLEN:
  45. case CMD_REQ_REDIS_HVALS:
  46. case CMD_REQ_REDIS_LLEN:
  47. case CMD_REQ_REDIS_LPOP:
  48. case CMD_REQ_REDIS_RPOP:
  49. case CMD_REQ_REDIS_SCARD:
  50. case CMD_REQ_REDIS_SMEMBERS:
  51. case CMD_REQ_REDIS_SPOP:
  52. case CMD_REQ_REDIS_ZCARD:
  53. case CMD_REQ_REDIS_PFCOUNT:
  54. case CMD_REQ_REDIS_AUTH:
  55. return 1;
  56. default:
  57. break;
  58. }
  59. return 0;
  60. }
  61. /*
  62. * Return true, if the redis command accepts exactly 1 argument, otherwise
  63. * return false
  64. */
  65. static int
  66. redis_arg1(struct cmd *r)
  67. {
  68. switch (r->type) {
  69. case CMD_REQ_REDIS_EXPIRE:
  70. case CMD_REQ_REDIS_EXPIREAT:
  71. case CMD_REQ_REDIS_PEXPIRE:
  72. case CMD_REQ_REDIS_PEXPIREAT:
  73. case CMD_REQ_REDIS_APPEND:
  74. case CMD_REQ_REDIS_DECRBY:
  75. case CMD_REQ_REDIS_GETBIT:
  76. case CMD_REQ_REDIS_GETSET:
  77. case CMD_REQ_REDIS_INCRBY:
  78. case CMD_REQ_REDIS_INCRBYFLOAT:
  79. case CMD_REQ_REDIS_SETNX:
  80. case CMD_REQ_REDIS_HEXISTS:
  81. case CMD_REQ_REDIS_HGET:
  82. case CMD_REQ_REDIS_LINDEX:
  83. case CMD_REQ_REDIS_LPUSHX:
  84. case CMD_REQ_REDIS_RPOPLPUSH:
  85. case CMD_REQ_REDIS_RPUSHX:
  86. case CMD_REQ_REDIS_SISMEMBER:
  87. case CMD_REQ_REDIS_ZRANK:
  88. case CMD_REQ_REDIS_ZREVRANK:
  89. case CMD_REQ_REDIS_ZSCORE:
  90. return 1;
  91. default:
  92. break;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Return true, if the redis command accepts exactly 2 arguments, otherwise
  98. * return false
  99. */
  100. static int
  101. redis_arg2(struct cmd *r)
  102. {
  103. switch (r->type) {
  104. case CMD_REQ_REDIS_GETRANGE:
  105. case CMD_REQ_REDIS_PSETEX:
  106. case CMD_REQ_REDIS_SETBIT:
  107. case CMD_REQ_REDIS_SETEX:
  108. case CMD_REQ_REDIS_SETRANGE:
  109. case CMD_REQ_REDIS_HINCRBY:
  110. case CMD_REQ_REDIS_HINCRBYFLOAT:
  111. case CMD_REQ_REDIS_HSET:
  112. case CMD_REQ_REDIS_HSETNX:
  113. case CMD_REQ_REDIS_LRANGE:
  114. case CMD_REQ_REDIS_LREM:
  115. case CMD_REQ_REDIS_LSET:
  116. case CMD_REQ_REDIS_LTRIM:
  117. case CMD_REQ_REDIS_SMOVE:
  118. case CMD_REQ_REDIS_ZCOUNT:
  119. case CMD_REQ_REDIS_ZLEXCOUNT:
  120. case CMD_REQ_REDIS_ZINCRBY:
  121. case CMD_REQ_REDIS_ZREMRANGEBYLEX:
  122. case CMD_REQ_REDIS_ZREMRANGEBYRANK:
  123. case CMD_REQ_REDIS_ZREMRANGEBYSCORE:
  124. case CMD_REQ_REDIS_RESTORE:
  125. return 1;
  126. default:
  127. break;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Return true, if the redis command accepts exactly 3 arguments, otherwise
  133. * return false
  134. */
  135. static int
  136. redis_arg3(struct cmd *r)
  137. {
  138. switch (r->type) {
  139. case CMD_REQ_REDIS_LINSERT:
  140. return 1;
  141. default:
  142. break;
  143. }
  144. return 0;
  145. }
  146. /*
  147. * Return true, if the redis command accepts 0 or more arguments, otherwise
  148. * return false
  149. */
  150. static int
  151. redis_argn(struct cmd *r)
  152. {
  153. switch (r->type) {
  154. case CMD_REQ_REDIS_BITCOUNT:
  155. case CMD_REQ_REDIS_SET:
  156. case CMD_REQ_REDIS_HDEL:
  157. case CMD_REQ_REDIS_HMGET:
  158. case CMD_REQ_REDIS_HMSET:
  159. case CMD_REQ_REDIS_HSCAN:
  160. case CMD_REQ_REDIS_LPUSH:
  161. case CMD_REQ_REDIS_RPUSH:
  162. case CMD_REQ_REDIS_SADD:
  163. case CMD_REQ_REDIS_SDIFF:
  164. case CMD_REQ_REDIS_SDIFFSTORE:
  165. case CMD_REQ_REDIS_SINTER:
  166. case CMD_REQ_REDIS_SINTERSTORE:
  167. case CMD_REQ_REDIS_SREM:
  168. case CMD_REQ_REDIS_SUNION:
  169. case CMD_REQ_REDIS_SUNIONSTORE:
  170. case CMD_REQ_REDIS_SRANDMEMBER:
  171. case CMD_REQ_REDIS_SSCAN:
  172. case CMD_REQ_REDIS_PFADD:
  173. case CMD_REQ_REDIS_PFMERGE:
  174. case CMD_REQ_REDIS_ZADD:
  175. case CMD_REQ_REDIS_ZINTERSTORE:
  176. case CMD_REQ_REDIS_ZRANGE:
  177. case CMD_REQ_REDIS_ZRANGEBYSCORE:
  178. case CMD_REQ_REDIS_ZREM:
  179. case CMD_REQ_REDIS_ZREVRANGE:
  180. case CMD_REQ_REDIS_ZRANGEBYLEX:
  181. case CMD_REQ_REDIS_ZREVRANGEBYSCORE:
  182. case CMD_REQ_REDIS_ZUNIONSTORE:
  183. case CMD_REQ_REDIS_ZSCAN:
  184. return 1;
  185. default:
  186. break;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Return true, if the redis command is a vector command accepting one or
  192. * more keys, otherwise return false
  193. */
  194. static int
  195. redis_argx(struct cmd *r)
  196. {
  197. switch (r->type) {
  198. case CMD_REQ_REDIS_MGET:
  199. case CMD_REQ_REDIS_DEL:
  200. return 1;
  201. default:
  202. break;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * Return true, if the redis command is a vector command accepting one or
  208. * more key-value pairs, otherwise return false
  209. */
  210. static int
  211. redis_argkvx(struct cmd *r)
  212. {
  213. switch (r->type) {
  214. case CMD_REQ_REDIS_MSET:
  215. return 1;
  216. default:
  217. break;
  218. }
  219. return 0;
  220. }
  221. /*
  222. * Return true, if the redis command is either EVAL or EVALSHA. These commands
  223. * have a special format with exactly 2 arguments, followed by one or more keys,
  224. * followed by zero or more arguments (the documentation online seems to suggest
  225. * that at least one argument is required, but that shouldn't be the case).
  226. */
  227. static int
  228. redis_argeval(struct cmd *r)
  229. {
  230. switch (r->type) {
  231. case CMD_REQ_REDIS_EVAL:
  232. case CMD_REQ_REDIS_EVALSHA:
  233. return 1;
  234. default:
  235. break;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * Reference: http://redis.io/topics/protocol
  241. *
  242. * Redis >= 1.2 uses the unified protocol to send requests to the Redis
  243. * server. In the unified protocol all the arguments sent to the server
  244. * are binary safe and every request has the following general form:
  245. *
  246. * *<number of arguments> CR LF
  247. * $<number of bytes of argument 1> CR LF
  248. * <argument data> CR LF
  249. * ...
  250. * $<number of bytes of argument N> CR LF
  251. * <argument data> CR LF
  252. *
  253. * Before the unified request protocol, redis protocol for requests supported
  254. * the following commands
  255. * 1). Inline commands: simple commands where arguments are just space
  256. * separated strings. No binary safeness is possible.
  257. * 2). Bulk commands: bulk commands are exactly like inline commands, but
  258. * the last argument is handled in a special way in order to allow for
  259. * a binary-safe last argument.
  260. *
  261. * only supports the Redis unified protocol for requests.
  262. */
  263. void
  264. redis_parse_cmd(struct cmd *r)
  265. {
  266. int len;
  267. char *p, *m, *token = NULL;
  268. char *cmd_end;
  269. char ch;
  270. uint32_t rlen = 0; /* running length in parsing fsa */
  271. uint32_t rnarg = 0; /* running # arg used by parsing fsa */
  272. enum {
  273. SW_START,
  274. SW_NARG,
  275. SW_NARG_LF,
  276. SW_REQ_TYPE_LEN,
  277. SW_REQ_TYPE_LEN_LF,
  278. SW_REQ_TYPE,
  279. SW_REQ_TYPE_LF,
  280. SW_KEY_LEN,
  281. SW_KEY_LEN_LF,
  282. SW_KEY,
  283. SW_KEY_LF,
  284. SW_ARG1_LEN,
  285. SW_ARG1_LEN_LF,
  286. SW_ARG1,
  287. SW_ARG1_LF,
  288. SW_ARG2_LEN,
  289. SW_ARG2_LEN_LF,
  290. SW_ARG2,
  291. SW_ARG2_LF,
  292. SW_ARG3_LEN,
  293. SW_ARG3_LEN_LF,
  294. SW_ARG3,
  295. SW_ARG3_LF,
  296. SW_ARGN_LEN,
  297. SW_ARGN_LEN_LF,
  298. SW_ARGN,
  299. SW_ARGN_LF,
  300. SW_SENTINEL
  301. } state;
  302. state = SW_START;
  303. cmd_end = r->cmd + r->clen;
  304. ASSERT(state >= SW_START && state < SW_SENTINEL);
  305. ASSERT(r->cmd != NULL && r->clen > 0);
  306. for (p = r->cmd; p < cmd_end; p++) {
  307. ch = *p;
  308. switch (state) {
  309. case SW_START:
  310. case SW_NARG:
  311. if (token == NULL) {
  312. if (ch != '*') {
  313. goto error;
  314. }
  315. token = p;
  316. /* req_start <- p */
  317. r->narg_start = p;
  318. rnarg = 0;
  319. state = SW_NARG;
  320. } else if (isdigit(ch)) {
  321. rnarg = rnarg * 10 + (uint32_t)(ch - '0');
  322. } else if (ch == CR) {
  323. if (rnarg == 0) {
  324. goto error;
  325. }
  326. r->narg = rnarg;
  327. r->narg_end = p;
  328. token = NULL;
  329. state = SW_NARG_LF;
  330. } else {
  331. goto error;
  332. }
  333. break;
  334. case SW_NARG_LF:
  335. switch (ch) {
  336. case LF:
  337. state = SW_REQ_TYPE_LEN;
  338. break;
  339. default:
  340. goto error;
  341. }
  342. break;
  343. case SW_REQ_TYPE_LEN:
  344. if (token == NULL) {
  345. if (ch != '$') {
  346. goto error;
  347. }
  348. token = p;
  349. rlen = 0;
  350. } else if (isdigit(ch)) {
  351. rlen = rlen * 10 + (uint32_t)(ch - '0');
  352. } else if (ch == CR) {
  353. if (rlen == 0 || rnarg == 0) {
  354. goto error;
  355. }
  356. rnarg--;
  357. token = NULL;
  358. state = SW_REQ_TYPE_LEN_LF;
  359. } else {
  360. goto error;
  361. }
  362. break;
  363. case SW_REQ_TYPE_LEN_LF:
  364. switch (ch) {
  365. case LF:
  366. state = SW_REQ_TYPE;
  367. break;
  368. default:
  369. goto error;
  370. }
  371. break;
  372. case SW_REQ_TYPE:
  373. if (token == NULL) {
  374. token = p;
  375. }
  376. m = token + rlen;
  377. if (m >= cmd_end) {
  378. //m = cmd_end - 1;
  379. //p = m;
  380. //break;
  381. goto error;
  382. }
  383. if (*m != CR) {
  384. goto error;
  385. }
  386. p = m; /* move forward by rlen bytes */
  387. rlen = 0;
  388. m = token;
  389. token = NULL;
  390. r->type = CMD_UNKNOWN;
  391. switch (p - m) {
  392. case 3:
  393. if (str3icmp(m, 'g', 'e', 't')) {
  394. r->type = CMD_REQ_REDIS_GET;
  395. break;
  396. }
  397. if (str3icmp(m, 's', 'e', 't')) {
  398. r->type = CMD_REQ_REDIS_SET;
  399. break;
  400. }
  401. if (str3icmp(m, 't', 't', 'l')) {
  402. r->type = CMD_REQ_REDIS_TTL;
  403. break;
  404. }
  405. if (str3icmp(m, 'd', 'e', 'l')) {
  406. r->type = CMD_REQ_REDIS_DEL;
  407. break;
  408. }
  409. break;
  410. case 4:
  411. if (str4icmp(m, 'p', 't', 't', 'l')) {
  412. r->type = CMD_REQ_REDIS_PTTL;
  413. break;
  414. }
  415. if (str4icmp(m, 'd', 'e', 'c', 'r')) {
  416. r->type = CMD_REQ_REDIS_DECR;
  417. break;
  418. }
  419. if (str4icmp(m, 'd', 'u', 'm', 'p')) {
  420. r->type = CMD_REQ_REDIS_DUMP;
  421. break;
  422. }
  423. if (str4icmp(m, 'h', 'd', 'e', 'l')) {
  424. r->type = CMD_REQ_REDIS_HDEL;
  425. break;
  426. }
  427. if (str4icmp(m, 'h', 'g', 'e', 't')) {
  428. r->type = CMD_REQ_REDIS_HGET;
  429. break;
  430. }
  431. if (str4icmp(m, 'h', 'l', 'e', 'n')) {
  432. r->type = CMD_REQ_REDIS_HLEN;
  433. break;
  434. }
  435. if (str4icmp(m, 'h', 's', 'e', 't')) {
  436. r->type = CMD_REQ_REDIS_HSET;
  437. break;
  438. }
  439. if (str4icmp(m, 'i', 'n', 'c', 'r')) {
  440. r->type = CMD_REQ_REDIS_INCR;
  441. break;
  442. }
  443. if (str4icmp(m, 'l', 'l', 'e', 'n')) {
  444. r->type = CMD_REQ_REDIS_LLEN;
  445. break;
  446. }
  447. if (str4icmp(m, 'l', 'p', 'o', 'p')) {
  448. r->type = CMD_REQ_REDIS_LPOP;
  449. break;
  450. }
  451. if (str4icmp(m, 'l', 'r', 'e', 'm')) {
  452. r->type = CMD_REQ_REDIS_LREM;
  453. break;
  454. }
  455. if (str4icmp(m, 'l', 's', 'e', 't')) {
  456. r->type = CMD_REQ_REDIS_LSET;
  457. break;
  458. }
  459. if (str4icmp(m, 'r', 'p', 'o', 'p')) {
  460. r->type = CMD_REQ_REDIS_RPOP;
  461. break;
  462. }
  463. if (str4icmp(m, 's', 'a', 'd', 'd')) {
  464. r->type = CMD_REQ_REDIS_SADD;
  465. break;
  466. }
  467. if (str4icmp(m, 's', 'p', 'o', 'p')) {
  468. r->type = CMD_REQ_REDIS_SPOP;
  469. break;
  470. }
  471. if (str4icmp(m, 's', 'r', 'e', 'm')) {
  472. r->type = CMD_REQ_REDIS_SREM;
  473. break;
  474. }
  475. if (str4icmp(m, 't', 'y', 'p', 'e')) {
  476. r->type = CMD_REQ_REDIS_TYPE;
  477. break;
  478. }
  479. if (str4icmp(m, 'm', 'g', 'e', 't')) {
  480. r->type = CMD_REQ_REDIS_MGET;
  481. break;
  482. }
  483. if (str4icmp(m, 'm', 's', 'e', 't')) {
  484. r->type = CMD_REQ_REDIS_MSET;
  485. break;
  486. }
  487. if (str4icmp(m, 'z', 'a', 'd', 'd')) {
  488. r->type = CMD_REQ_REDIS_ZADD;
  489. break;
  490. }
  491. if (str4icmp(m, 'z', 'r', 'e', 'm')) {
  492. r->type = CMD_REQ_REDIS_ZREM;
  493. break;
  494. }
  495. if (str4icmp(m, 'e', 'v', 'a', 'l')) {
  496. r->type = CMD_REQ_REDIS_EVAL;
  497. break;
  498. }
  499. if (str4icmp(m, 's', 'o', 'r', 't')) {
  500. r->type = CMD_REQ_REDIS_SORT;
  501. break;
  502. }
  503. if (str4icmp(m, 'p', 'i', 'n', 'g')) {
  504. r->type = CMD_REQ_REDIS_PING;
  505. r->noforward = 1;
  506. break;
  507. }
  508. if (str4icmp(m, 'q', 'u', 'i', 't')) {
  509. r->type = CMD_REQ_REDIS_QUIT;
  510. r->quit = 1;
  511. break;
  512. }
  513. if (str4icmp(m, 'a', 'u', 't', 'h')) {
  514. r->type = CMD_REQ_REDIS_AUTH;
  515. r->noforward = 1;
  516. break;
  517. }
  518. break;
  519. case 5:
  520. if (str5icmp(m, 'h', 'k', 'e', 'y', 's')) {
  521. r->type = CMD_REQ_REDIS_HKEYS;
  522. break;
  523. }
  524. if (str5icmp(m, 'h', 'm', 'g', 'e', 't')) {
  525. r->type = CMD_REQ_REDIS_HMGET;
  526. break;
  527. }
  528. if (str5icmp(m, 'h', 'm', 's', 'e', 't')) {
  529. r->type = CMD_REQ_REDIS_HMSET;
  530. break;
  531. }
  532. if (str5icmp(m, 'h', 'v', 'a', 'l', 's')) {
  533. r->type = CMD_REQ_REDIS_HVALS;
  534. break;
  535. }
  536. if (str5icmp(m, 'h', 's', 'c', 'a', 'n')) {
  537. r->type = CMD_REQ_REDIS_HSCAN;
  538. break;
  539. }
  540. if (str5icmp(m, 'l', 'p', 'u', 's', 'h')) {
  541. r->type = CMD_REQ_REDIS_LPUSH;
  542. break;
  543. }
  544. if (str5icmp(m, 'l', 't', 'r', 'i', 'm')) {
  545. r->type = CMD_REQ_REDIS_LTRIM;
  546. break;
  547. }
  548. if (str5icmp(m, 'r', 'p', 'u', 's', 'h')) {
  549. r->type = CMD_REQ_REDIS_RPUSH;
  550. break;
  551. }
  552. if (str5icmp(m, 's', 'c', 'a', 'r', 'd')) {
  553. r->type = CMD_REQ_REDIS_SCARD;
  554. break;
  555. }
  556. if (str5icmp(m, 's', 'd', 'i', 'f', 'f')) {
  557. r->type = CMD_REQ_REDIS_SDIFF;
  558. break;
  559. }
  560. if (str5icmp(m, 's', 'e', 't', 'e', 'x')) {
  561. r->type = CMD_REQ_REDIS_SETEX;
  562. break;
  563. }
  564. if (str5icmp(m, 's', 'e', 't', 'n', 'x')) {
  565. r->type = CMD_REQ_REDIS_SETNX;
  566. break;
  567. }
  568. if (str5icmp(m, 's', 'm', 'o', 'v', 'e')) {
  569. r->type = CMD_REQ_REDIS_SMOVE;
  570. break;
  571. }
  572. if (str5icmp(m, 's', 's', 'c', 'a', 'n')) {
  573. r->type = CMD_REQ_REDIS_SSCAN;
  574. break;
  575. }
  576. if (str5icmp(m, 'z', 'c', 'a', 'r', 'd')) {
  577. r->type = CMD_REQ_REDIS_ZCARD;
  578. break;
  579. }
  580. if (str5icmp(m, 'z', 'r', 'a', 'n', 'k')) {
  581. r->type = CMD_REQ_REDIS_ZRANK;
  582. break;
  583. }
  584. if (str5icmp(m, 'z', 's', 'c', 'a', 'n')) {
  585. r->type = CMD_REQ_REDIS_ZSCAN;
  586. break;
  587. }
  588. if (str5icmp(m, 'p', 'f', 'a', 'd', 'd')) {
  589. r->type = CMD_REQ_REDIS_PFADD;
  590. break;
  591. }
  592. break;
  593. case 6:
  594. if (str6icmp(m, 'a', 'p', 'p', 'e', 'n', 'd')) {
  595. r->type = CMD_REQ_REDIS_APPEND;
  596. break;
  597. }
  598. if (str6icmp(m, 'd', 'e', 'c', 'r', 'b', 'y')) {
  599. r->type = CMD_REQ_REDIS_DECRBY;
  600. break;
  601. }
  602. if (str6icmp(m, 'e', 'x', 'i', 's', 't', 's')) {
  603. r->type = CMD_REQ_REDIS_EXISTS;
  604. break;
  605. }
  606. if (str6icmp(m, 'e', 'x', 'p', 'i', 'r', 'e')) {
  607. r->type = CMD_REQ_REDIS_EXPIRE;
  608. break;
  609. }
  610. if (str6icmp(m, 'g', 'e', 't', 'b', 'i', 't')) {
  611. r->type = CMD_REQ_REDIS_GETBIT;
  612. break;
  613. }
  614. if (str6icmp(m, 'g', 'e', 't', 's', 'e', 't')) {
  615. r->type = CMD_REQ_REDIS_GETSET;
  616. break;
  617. }
  618. if (str6icmp(m, 'p', 's', 'e', 't', 'e', 'x')) {
  619. r->type = CMD_REQ_REDIS_PSETEX;
  620. break;
  621. }
  622. if (str6icmp(m, 'h', 's', 'e', 't', 'n', 'x')) {
  623. r->type = CMD_REQ_REDIS_HSETNX;
  624. break;
  625. }
  626. if (str6icmp(m, 'i', 'n', 'c', 'r', 'b', 'y')) {
  627. r->type = CMD_REQ_REDIS_INCRBY;
  628. break;
  629. }
  630. if (str6icmp(m, 'l', 'i', 'n', 'd', 'e', 'x')) {
  631. r->type = CMD_REQ_REDIS_LINDEX;
  632. break;
  633. }
  634. if (str6icmp(m, 'l', 'p', 'u', 's', 'h', 'x')) {
  635. r->type = CMD_REQ_REDIS_LPUSHX;
  636. break;
  637. }
  638. if (str6icmp(m, 'l', 'r', 'a', 'n', 'g', 'e')) {
  639. r->type = CMD_REQ_REDIS_LRANGE;
  640. break;
  641. }
  642. if (str6icmp(m, 'r', 'p', 'u', 's', 'h', 'x')) {
  643. r->type = CMD_REQ_REDIS_RPUSHX;
  644. break;
  645. }
  646. if (str6icmp(m, 's', 'e', 't', 'b', 'i', 't')) {
  647. r->type = CMD_REQ_REDIS_SETBIT;
  648. break;
  649. }
  650. if (str6icmp(m, 's', 'i', 'n', 't', 'e', 'r')) {
  651. r->type = CMD_REQ_REDIS_SINTER;
  652. break;
  653. }
  654. if (str6icmp(m, 's', 't', 'r', 'l', 'e', 'n')) {
  655. r->type = CMD_REQ_REDIS_STRLEN;
  656. break;
  657. }
  658. if (str6icmp(m, 's', 'u', 'n', 'i', 'o', 'n')) {
  659. r->type = CMD_REQ_REDIS_SUNION;
  660. break;
  661. }
  662. if (str6icmp(m, 'z', 'c', 'o', 'u', 'n', 't')) {
  663. r->type = CMD_REQ_REDIS_ZCOUNT;
  664. break;
  665. }
  666. if (str6icmp(m, 'z', 'r', 'a', 'n', 'g', 'e')) {
  667. r->type = CMD_REQ_REDIS_ZRANGE;
  668. break;
  669. }
  670. if (str6icmp(m, 'z', 's', 'c', 'o', 'r', 'e')) {
  671. r->type = CMD_REQ_REDIS_ZSCORE;
  672. break;
  673. }
  674. break;
  675. case 7:
  676. if (str7icmp(m, 'p', 'e', 'r', 's', 'i', 's', 't')) {
  677. r->type = CMD_REQ_REDIS_PERSIST;
  678. break;
  679. }
  680. if (str7icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e')) {
  681. r->type = CMD_REQ_REDIS_PEXPIRE;
  682. break;
  683. }
  684. if (str7icmp(m, 'h', 'e', 'x', 'i', 's', 't', 's')) {
  685. r->type = CMD_REQ_REDIS_HEXISTS;
  686. break;
  687. }
  688. if (str7icmp(m, 'h', 'g', 'e', 't', 'a', 'l', 'l')) {
  689. r->type = CMD_REQ_REDIS_HGETALL;
  690. break;
  691. }
  692. if (str7icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y')) {
  693. r->type = CMD_REQ_REDIS_HINCRBY;
  694. break;
  695. }
  696. if (str7icmp(m, 'l', 'i', 'n', 's', 'e', 'r', 't')) {
  697. r->type = CMD_REQ_REDIS_LINSERT;
  698. break;
  699. }
  700. if (str7icmp(m, 'z', 'i', 'n', 'c', 'r', 'b', 'y')) {
  701. r->type = CMD_REQ_REDIS_ZINCRBY;
  702. break;
  703. }
  704. if (str7icmp(m, 'e', 'v', 'a', 'l', 's', 'h', 'a')) {
  705. r->type = CMD_REQ_REDIS_EVALSHA;
  706. break;
  707. }
  708. if (str7icmp(m, 'r', 'e', 's', 't', 'o', 'r', 'e')) {
  709. r->type = CMD_REQ_REDIS_RESTORE;
  710. break;
  711. }
  712. if (str7icmp(m, 'p', 'f', 'c', 'o', 'u', 'n', 't')) {
  713. r->type = CMD_REQ_REDIS_PFCOUNT;
  714. break;
  715. }
  716. if (str7icmp(m, 'p', 'f', 'm', 'e', 'r', 'g', 'e')) {
  717. r->type = CMD_REQ_REDIS_PFMERGE;
  718. break;
  719. }
  720. break;
  721. case 8:
  722. if (str8icmp(m, 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
  723. r->type = CMD_REQ_REDIS_EXPIREAT;
  724. break;
  725. }
  726. if (str8icmp(m, 'b', 'i', 't', 'c', 'o', 'u', 'n', 't')) {
  727. r->type = CMD_REQ_REDIS_BITCOUNT;
  728. break;
  729. }
  730. if (str8icmp(m, 'g', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
  731. r->type = CMD_REQ_REDIS_GETRANGE;
  732. break;
  733. }
  734. if (str8icmp(m, 's', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
  735. r->type = CMD_REQ_REDIS_SETRANGE;
  736. break;
  737. }
  738. if (str8icmp(m, 's', 'm', 'e', 'm', 'b', 'e', 'r', 's')) {
  739. r->type = CMD_REQ_REDIS_SMEMBERS;
  740. break;
  741. }
  742. if (str8icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'k')) {
  743. r->type = CMD_REQ_REDIS_ZREVRANK;
  744. break;
  745. }
  746. break;
  747. case 9:
  748. if (str9icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
  749. r->type = CMD_REQ_REDIS_PEXPIREAT;
  750. break;
  751. }
  752. if (str9icmp(m, 'r', 'p', 'o', 'p', 'l', 'p', 'u', 's', 'h')) {
  753. r->type = CMD_REQ_REDIS_RPOPLPUSH;
  754. break;
  755. }
  756. if (str9icmp(m, 's', 'i', 's', 'm', 'e', 'm', 'b', 'e', 'r')) {
  757. r->type = CMD_REQ_REDIS_SISMEMBER;
  758. break;
  759. }
  760. if (str9icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e')) {
  761. r->type = CMD_REQ_REDIS_ZREVRANGE;
  762. break;
  763. }
  764. if (str9icmp(m, 'z', 'l', 'e', 'x', 'c', 'o', 'u', 'n', 't')) {
  765. r->type = CMD_REQ_REDIS_ZLEXCOUNT;
  766. break;
  767. }
  768. break;
  769. case 10:
  770. if (str10icmp(m, 's', 'd', 'i', 'f', 'f', 's', 't', 'o', 'r', 'e')) {
  771. r->type = CMD_REQ_REDIS_SDIFFSTORE;
  772. break;
  773. }
  774. case 11:
  775. if (str11icmp(m, 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
  776. r->type = CMD_REQ_REDIS_INCRBYFLOAT;
  777. break;
  778. }
  779. if (str11icmp(m, 's', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
  780. r->type = CMD_REQ_REDIS_SINTERSTORE;
  781. break;
  782. }
  783. if (str11icmp(m, 's', 'r', 'a', 'n', 'd', 'm', 'e', 'm', 'b', 'e', 'r')) {
  784. r->type = CMD_REQ_REDIS_SRANDMEMBER;
  785. break;
  786. }
  787. if (str11icmp(m, 's', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
  788. r->type = CMD_REQ_REDIS_SUNIONSTORE;
  789. break;
  790. }
  791. if (str11icmp(m, 'z', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
  792. r->type = CMD_REQ_REDIS_ZINTERSTORE;
  793. break;
  794. }
  795. if (str11icmp(m, 'z', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
  796. r->type = CMD_REQ_REDIS_ZUNIONSTORE;
  797. break;
  798. }
  799. if (str11icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
  800. r->type = CMD_REQ_REDIS_ZRANGEBYLEX;
  801. break;
  802. }
  803. break;
  804. case 12:
  805. if (str12icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
  806. r->type = CMD_REQ_REDIS_HINCRBYFLOAT;
  807. break;
  808. }
  809. break;
  810. case 13:
  811. if (str13icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
  812. r->type = CMD_REQ_REDIS_ZRANGEBYSCORE;
  813. break;
  814. }
  815. break;
  816. case 14:
  817. if (str14icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
  818. r->type = CMD_REQ_REDIS_ZREMRANGEBYLEX;
  819. break;
  820. }
  821. break;
  822. case 15:
  823. if (str15icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'r', 'a', 'n', 'k')) {
  824. r->type = CMD_REQ_REDIS_ZREMRANGEBYRANK;
  825. break;
  826. }
  827. break;
  828. case 16:
  829. if (str16icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
  830. r->type = CMD_REQ_REDIS_ZREMRANGEBYSCORE;
  831. break;
  832. }
  833. if (str16icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
  834. r->type = CMD_REQ_REDIS_ZREVRANGEBYSCORE;
  835. break;
  836. }
  837. break;
  838. default:
  839. break;
  840. }
  841. if (r->type == CMD_UNKNOWN) {
  842. goto error;
  843. }
  844. state = SW_REQ_TYPE_LF;
  845. break;
  846. case SW_REQ_TYPE_LF:
  847. switch (ch) {
  848. case LF:
  849. if (redis_argz(r)) {
  850. goto done;
  851. } else if (redis_argeval(r)) {
  852. state = SW_ARG1_LEN;
  853. } else {
  854. state = SW_KEY_LEN;
  855. }
  856. break;
  857. default:
  858. goto error;
  859. }
  860. break;
  861. case SW_KEY_LEN:
  862. if (token == NULL) {
  863. if (ch != '$') {
  864. goto error;
  865. }
  866. token = p;
  867. rlen = 0;
  868. } else if (isdigit(ch)) {
  869. rlen = rlen * 10 + (uint32_t)(ch - '0');
  870. } else if (ch == CR) {
  871. if (rnarg == 0) {
  872. goto error;
  873. }
  874. rnarg--;
  875. token = NULL;
  876. state = SW_KEY_LEN_LF;
  877. } else {
  878. goto error;
  879. }
  880. break;
  881. case SW_KEY_LEN_LF:
  882. switch (ch) {
  883. case LF:
  884. state = SW_KEY;
  885. break;
  886. default:
  887. goto error;
  888. }
  889. break;
  890. case SW_KEY:
  891. if (token == NULL) {
  892. token = p;
  893. }
  894. m = token + rlen;
  895. if (m >= cmd_end) {
  896. //m = b->last - 1;
  897. //p = m;
  898. //break;
  899. goto error;
  900. }
  901. if (*m != CR) {
  902. goto error;
  903. } else { /* got a key */
  904. struct keypos *kpos;
  905. p = m; /* move forward by rlen bytes */
  906. rlen = 0;
  907. m = token;
  908. token = NULL;
  909. kpos = hiarray_push(r->keys);
  910. if (kpos == NULL) {
  911. goto enomem;
  912. }
  913. kpos->start = m;
  914. kpos->end = p;
  915. //kpos->v_len = 0;
  916. state = SW_KEY_LF;
  917. }
  918. break;
  919. case SW_KEY_LF:
  920. switch (ch) {
  921. case LF:
  922. if (redis_arg0(r)) {
  923. if (rnarg != 0) {
  924. goto error;
  925. }
  926. goto done;
  927. } else if (redis_arg1(r)) {
  928. if (rnarg != 1) {
  929. goto error;
  930. }
  931. state = SW_ARG1_LEN;
  932. } else if (redis_arg2(r)) {
  933. if (rnarg != 2) {
  934. goto error;
  935. }
  936. state = SW_ARG1_LEN;
  937. } else if (redis_arg3(r)) {
  938. if (rnarg != 3) {
  939. goto error;
  940. }
  941. state = SW_ARG1_LEN;
  942. } else if (redis_argn(r)) {
  943. if (rnarg == 0) {
  944. goto done;
  945. }
  946. state = SW_ARG1_LEN;
  947. } else if (redis_argx(r)) {
  948. if (rnarg == 0) {
  949. goto done;
  950. }
  951. state = SW_KEY_LEN;
  952. } else if (redis_argkvx(r)) {
  953. if (rnarg == 0) {
  954. goto done;
  955. }
  956. if (r->narg % 2 == 0) {
  957. goto error;
  958. }
  959. state = SW_ARG1_LEN;
  960. } else if (redis_argeval(r)) {
  961. if (rnarg == 0) {
  962. goto done;
  963. }
  964. state = SW_ARGN_LEN;
  965. } else {
  966. goto error;
  967. }
  968. break;
  969. default:
  970. goto error;
  971. }
  972. break;
  973. case SW_ARG1_LEN:
  974. if (token == NULL) {
  975. if (ch != '$') {
  976. goto error;
  977. }
  978. rlen = 0;
  979. token = p;
  980. } else if (isdigit(ch)) {
  981. rlen = rlen * 10 + (uint32_t)(ch - '0');
  982. } else if (ch == CR) {
  983. if ((p - token) <= 1 || rnarg == 0) {
  984. goto error;
  985. }
  986. rnarg--;
  987. token = NULL;
  988. /*
  989. //for mset value length
  990. if(redis_argkvx(r))
  991. {
  992. struct keypos *kpos;
  993. uint32_t array_len = array_n(r->keys);
  994. if(array_len == 0)
  995. {
  996. goto error;
  997. }
  998. kpos = array_n(r->keys, array_len-1);
  999. if (kpos == NULL || kpos->v_len != 0) {
  1000. goto error;
  1001. }
  1002. kpos->v_len = rlen;
  1003. }
  1004. */
  1005. state = SW_ARG1_LEN_LF;
  1006. } else {
  1007. goto error;
  1008. }
  1009. break;
  1010. case SW_ARG1_LEN_LF:
  1011. switch (ch) {
  1012. case LF:
  1013. state = SW_ARG1;
  1014. break;
  1015. default:
  1016. goto error;
  1017. }
  1018. break;
  1019. case SW_ARG1:
  1020. m = p + rlen;
  1021. if (m >= cmd_end) {
  1022. //rlen -= (uint32_t)(b->last - p);
  1023. //m = b->last - 1;
  1024. //p = m;
  1025. //break;
  1026. goto error;
  1027. }
  1028. if (*m != CR) {
  1029. goto error;
  1030. }
  1031. p = m; /* move forward by rlen bytes */
  1032. rlen = 0;
  1033. state = SW_ARG1_LF;
  1034. break;
  1035. case SW_ARG1_LF:
  1036. switch (ch) {
  1037. case LF:
  1038. if (redis_arg1(r)) {
  1039. if (rnarg != 0) {
  1040. goto error;
  1041. }
  1042. goto done;
  1043. } else if (redis_arg2(r)) {
  1044. if (rnarg != 1) {
  1045. goto error;
  1046. }
  1047. state = SW_ARG2_LEN;
  1048. } else if (redis_arg3(r)) {
  1049. if (rnarg != 2) {
  1050. goto error;
  1051. }
  1052. state = SW_ARG2_LEN;
  1053. } else if (redis_argn(r)) {
  1054. if (rnarg == 0) {
  1055. goto done;
  1056. }
  1057. state = SW_ARGN_LEN;
  1058. } else if (redis_argeval(r)) {
  1059. if (rnarg < 2) {
  1060. goto error;
  1061. }
  1062. state = SW_ARG2_LEN;
  1063. } else if (redis_argkvx(r)) {
  1064. if (rnarg == 0) {
  1065. goto done;
  1066. }
  1067. state = SW_KEY_LEN;
  1068. } else {
  1069. goto error;
  1070. }
  1071. break;
  1072. default:
  1073. goto error;
  1074. }
  1075. break;
  1076. case SW_ARG2_LEN:
  1077. if (token == NULL) {
  1078. if (ch != '$') {
  1079. goto error;
  1080. }
  1081. rlen = 0;
  1082. token = p;
  1083. } else if (isdigit(ch)) {
  1084. rlen = rlen * 10 + (uint32_t)(ch - '0');
  1085. } else if (ch == CR) {
  1086. if ((p - token) <= 1 || rnarg == 0) {
  1087. goto error;
  1088. }
  1089. rnarg--;
  1090. token = NULL;
  1091. state = SW_ARG2_LEN_LF;
  1092. } else {
  1093. goto error;
  1094. }
  1095. break;
  1096. case SW_ARG2_LEN_LF:
  1097. switch (ch) {
  1098. case LF:
  1099. state = SW_ARG2;
  1100. break;
  1101. default:
  1102. goto error;
  1103. }
  1104. break;
  1105. case SW_ARG2:
  1106. if (token == NULL && redis_argeval(r)) {
  1107. /*
  1108. * For EVAL/EVALSHA, ARG2 represents the # key/arg pairs which must
  1109. * be tokenized and stored in contiguous memory.
  1110. */
  1111. token = p;
  1112. }
  1113. m = p + rlen;
  1114. if (m >= cmd_end) {
  1115. //rlen -= (uint32_t)(b->last - p);
  1116. //m = b->last - 1;
  1117. //p = m;
  1118. //break;
  1119. goto error;
  1120. }
  1121. if (*m != CR) {
  1122. goto error;
  1123. }
  1124. p = m; /* move forward by rlen bytes */
  1125. rlen = 0;
  1126. if (redis_argeval(r)) {
  1127. uint32_t nkey;
  1128. char *chp;
  1129. /*
  1130. * For EVAL/EVALSHA, we need to find the integer value of this
  1131. * argument. It tells us the number of keys in the script, and
  1132. * we need to error out if number of keys is 0. At this point,
  1133. * both p and m point to the end of the argument and r->token
  1134. * points to the start.
  1135. */
  1136. if (p - token < 1) {
  1137. goto error;
  1138. }
  1139. for (nkey = 0, chp = token; chp < p; chp++) {
  1140. if (isdigit(*chp)) {
  1141. nkey = nkey * 10 + (uint32_t)(*chp - '0');
  1142. } else {
  1143. goto error;
  1144. }
  1145. }
  1146. if (nkey == 0) {
  1147. goto error;
  1148. }
  1149. token = NULL;
  1150. }
  1151. state = SW_ARG2_LF;
  1152. break;
  1153. case SW_ARG2_LF:
  1154. switch (ch) {
  1155. case LF:
  1156. if (redis_arg2(r)) {
  1157. if (rnarg != 0) {
  1158. goto error;
  1159. }
  1160. goto done;
  1161. } else if (redis_arg3(r)) {
  1162. if (rnarg != 1) {
  1163. goto error;
  1164. }
  1165. state = SW_ARG3_LEN;
  1166. } else if (redis_argn(r)) {
  1167. if (rnarg == 0) {
  1168. goto done;
  1169. }
  1170. state = SW_ARGN_LEN;
  1171. } else if (redis_argeval(r)) {
  1172. if (rnarg < 1) {
  1173. goto error;
  1174. }
  1175. state = SW_KEY_LEN;
  1176. } else {
  1177. goto error;
  1178. }
  1179. break;
  1180. default:
  1181. goto error;
  1182. }
  1183. break;
  1184. case SW_ARG3_LEN:
  1185. if (token == NULL) {
  1186. if (ch != '$') {
  1187. goto error;
  1188. }
  1189. rlen = 0;
  1190. token = p;
  1191. } else if (isdigit(ch)) {
  1192. rlen = rlen * 10 + (uint32_t)(ch - '0');
  1193. } else if (ch == CR) {
  1194. if ((p - token) <= 1 || rnarg == 0) {
  1195. goto error;
  1196. }
  1197. rnarg--;
  1198. token = NULL;
  1199. state = SW_ARG3_LEN_LF;
  1200. } else {
  1201. goto error;
  1202. }
  1203. break;
  1204. case SW_ARG3_LEN_LF:
  1205. switch (ch) {
  1206. case LF:
  1207. state = SW_ARG3;
  1208. break;
  1209. default:
  1210. goto error;
  1211. }
  1212. break;
  1213. case SW_ARG3:
  1214. m = p + rlen;
  1215. if (m >= cmd_end) {
  1216. //rlen -= (uint32_t)(b->last - p);
  1217. //m = b->last - 1;
  1218. //p = m;
  1219. //break;
  1220. goto error;
  1221. }
  1222. if (*m != CR) {
  1223. goto error;
  1224. }
  1225. p = m; /* move forward by rlen bytes */
  1226. rlen = 0;
  1227. state = SW_ARG3_LF;
  1228. break;
  1229. case SW_ARG3_LF:
  1230. switch (ch) {
  1231. case LF:
  1232. if (redis_arg3(r)) {
  1233. if (rnarg != 0) {
  1234. goto error;
  1235. }
  1236. goto done;
  1237. } else if (redis_argn(r)) {
  1238. if (rnarg == 0) {
  1239. goto done;
  1240. }
  1241. state = SW_ARGN_LEN;
  1242. } else {
  1243. goto error;
  1244. }
  1245. break;
  1246. default:
  1247. goto error;
  1248. }
  1249. break;
  1250. case SW_ARGN_LEN:
  1251. if (token == NULL) {
  1252. if (ch != '$') {
  1253. goto error;
  1254. }
  1255. rlen = 0;
  1256. token = p;
  1257. } else if (isdigit(ch)) {
  1258. rlen = rlen * 10 + (uint32_t)(ch - '0');
  1259. } else if (ch == CR) {
  1260. if ((p - token) <= 1 || rnarg == 0) {
  1261. goto error;
  1262. }
  1263. rnarg--;
  1264. token = NULL;
  1265. state = SW_ARGN_LEN_LF;
  1266. } else {
  1267. goto error;
  1268. }
  1269. break;
  1270. case SW_ARGN_LEN_LF:
  1271. switch (ch) {
  1272. case LF:
  1273. state = SW_ARGN;
  1274. break;
  1275. default:
  1276. goto error;
  1277. }
  1278. break;
  1279. case SW_ARGN:
  1280. m = p + rlen;
  1281. if (m >= cmd_end) {
  1282. //rlen -= (uint32_t)(b->last - p);
  1283. //m = b->last - 1;
  1284. //p = m;
  1285. //break;
  1286. goto error;
  1287. }
  1288. if (*m != CR) {
  1289. goto error;
  1290. }
  1291. p = m; /* move forward by rlen bytes */
  1292. rlen = 0;
  1293. state = SW_ARGN_LF;
  1294. break;
  1295. case SW_ARGN_LF:
  1296. switch (ch) {
  1297. case LF:
  1298. if (redis_argn(r) || redis_argeval(r)) {
  1299. if (rnarg == 0) {
  1300. goto done;
  1301. }
  1302. state = SW_ARGN_LEN;
  1303. } else {
  1304. goto error;
  1305. }
  1306. break;
  1307. default:
  1308. goto error;
  1309. }
  1310. break;
  1311. case SW_SENTINEL:
  1312. default:
  1313. NOT_REACHED();
  1314. break;
  1315. }
  1316. }
  1317. ASSERT(p == cmd_end);
  1318. return;
  1319. done:
  1320. ASSERT(r->type > CMD_UNKNOWN && r->type < CMD_SENTINEL);
  1321. r->result = CMD_PARSE_OK;
  1322. return;
  1323. enomem:
  1324. r->result = CMD_PARSE_ENOMEM;
  1325. return;
  1326. error:
  1327. r->result = CMD_PARSE_ERROR;
  1328. errno = EINVAL;
  1329. if(r->errstr == NULL){
  1330. r->errstr = hi_alloc(100*sizeof(*r->errstr));
  1331. }
  1332. len = _scnprintf(r->errstr, 100, "Parse command error. Cmd type: %d, state: %d, break position: %d.",
  1333. r->type, state, (int)(p - r->cmd));
  1334. r->errstr[len] = '\0';
  1335. }
  1336. struct cmd *command_get()
  1337. {
  1338. struct cmd *command;
  1339. command = hi_alloc(sizeof(struct cmd));
  1340. if(command == NULL)
  1341. {
  1342. return NULL;
  1343. }
  1344. command->id = ++cmd_id;
  1345. command->result = CMD_PARSE_OK;
  1346. command->errstr = NULL;
  1347. command->type = CMD_UNKNOWN;
  1348. command->cmd = NULL;
  1349. command->clen = 0;
  1350. command->keys = NULL;
  1351. command->narg_start = NULL;
  1352. command->narg_end = NULL;
  1353. command->narg = 0;
  1354. command->quit = 0;
  1355. command->noforward = 0;
  1356. command->slot_num = -1;
  1357. command->frag_seq = NULL;
  1358. command->reply = NULL;
  1359. command->sub_commands = NULL;
  1360. command->keys = hiarray_create(1, sizeof(struct keypos));
  1361. if (command->keys == NULL)
  1362. {
  1363. hi_free(command);
  1364. return NULL;
  1365. }
  1366. return command;
  1367. }
  1368. void command_destroy(struct cmd *command)
  1369. {
  1370. if(command == NULL)
  1371. {
  1372. return;
  1373. }
  1374. if(command->cmd != NULL)
  1375. {
  1376. free(command->cmd);
  1377. }
  1378. if(command->errstr != NULL){
  1379. hi_free(command->errstr);
  1380. }
  1381. if(command->keys != NULL)
  1382. {
  1383. command->keys->nelem = 0;
  1384. hiarray_destroy(command->keys);
  1385. }
  1386. if(command->frag_seq != NULL)
  1387. {
  1388. hi_free(command->frag_seq);
  1389. command->frag_seq = NULL;
  1390. }
  1391. if(command->reply != NULL)
  1392. {
  1393. freeReplyObject(command->reply);
  1394. }
  1395. if(command->sub_commands != NULL)
  1396. {
  1397. listRelease(command->sub_commands);
  1398. }
  1399. hi_free(command);
  1400. }