PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/anyremote-6.0/src/parse.c

#
C | 1570 lines | 1158 code | 289 blank | 123 comment | 459 complexity | fb721098402bbafbd823758406ded8f8 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // anyRemote
  3. // a bluetooth remote for your PC.
  4. //
  5. // Copyright (C) 2006-2012 Mikhail Fedotov <anyremote@mail.ru>
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. //
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <sys/stat.h>
  26. #include "parse.h"
  27. #include "utils.h"
  28. #include <regex.h>
  29. #define REGEX_KEYVAL "^[[:space:]]*(\\[[[:alpha:]]+\\])([^=]*)$|^[[:space:]]*([^=%]*[^[:space:]]{1})[[:space:]]*=[[:space:]]*(.*[^[:space:]]{1})[[:space:]]*$"
  30. #define REGEX_CMDBYCMD "[[:space:]]*(Exec|ExecAndSet|Make|Get|Set|Send|ExecAndSend|Timer|Macro|Load|Include|SendCKPD|Emulate|Dbus)[[:space:]]*(\\()|[[:space:]]*(Exit)[[:space:]]*;{0,1}"
  31. #define REGEX_SET "[[:space:]]*(bg|editfield|filemanager|fg|font|fullscreen|icons|list|iconlist|menu|parameter|skin|status|text|title|volume|image|cover|popup)[[:space:]]*,[[:space:]]*(.*)|[[:space:]]*(vibrate|repaint|disconnect)[[:space:]]*"
  32. #define REGEX_GET "[[:space:]]*(screen_size|cover_size|icon_size|icon_padding|model|version|cursor|ping|password)[[:space:]]*|[[:space:]]*(is_exists)[[:space:]]*,[[:space:]]*(.*)[[:space:]]*,[[:space:]]*(.*)[[:space:]]*"
  33. #define REGEX_TIMER "[[:space:]]*([^[:space:]]+)[[:space:]]*,[[:space:]]*([[:digit:]]*)[[:space:]]*,[[:space:]]*([[:digit:]]*)[[:space:]]*$|[[:space:]]*([^[:space:]]+)[[:space:]]*,[[:space:]]*(cancel|pause|reset|restart|continue)"
  34. #define REGEX_MAKE "[[:space:]]*(remote|mode|var|alarm)[[:space:]]*,[[:space:]]*(.*)[[:space:]]*|[[:space:]]*(exit|flush|stop|disconnect|none)[[:space:]]*"
  35. #define REGEX_SET_TL "[[:space:]]*(fg|bg|font|select)[[:space:]]*,(.*)|[[:space:]]*(add|replace)[[:space:]]*,[[:space:]]*([^[:space:]]{1}[^,]*[^[:space:]]{1})[[:space:]]*,[[:space:]]*(.*)|[[:space:]]*(close)[[:space:]]*,[[:space:]]*(clear)[[:space:]]*|[[:space:]]*(clear|close|show)[[:space:]]*"
  36. #define REGEX_SET_MN "[[:space:]]*(add|replace)[[:space:]]*,(.*)|[[:space:]]*(clear)[[:space:]]*"
  37. #define REGEX_SET_WM "[[:space:]]*(icon|window)[[:space:]]*,(.*)|[[:space:]]*(remove_all|clear_cache|show|close|cursor|nocursor|dynamic_cursor)[[:space:]]*|[[:space:]]*(set_cursor),[[:space:]]*(.*)[[:space:]]*"
  38. #define REGEX_SET_FM "[[:space:]]*(add|replace|select)[[:space:]]*,[[:space:]]*(left|right)[[:space:]]*,[[:space:]]*(.*)|[[:space:]]*(close|show)[[:space:]]*"
  39. #define REGEX_SET_VR "^[[:space:]]*([^,]*)[[:space:]]*,[[:space:]]*(.*)[[:space:]]*"
  40. #define SEPARATOR '/'
  41. extern char tmp[MAXMAXLEN];
  42. static char load_buf[MAXCMDLEN];
  43. regex_t *keyVal = NULL;
  44. regex_t *cmdByCmd = NULL;
  45. mode *modes = NULL;
  46. mode *currentMode = NULL;
  47. mode *internalMode = NULL;
  48. mode *defMode = NULL;
  49. CONF conf = { MODEL_DEFAULT, // Model
  50. NULL, // Cfg. dir
  51. -1, // Front-end port
  52. 0, // Work with anyremote2html
  53. 0, // Ask password ?
  54. 0, // set uid
  55. 0
  56. }; // set gid
  57. type_key* findExact(mode *mode, const char *key)
  58. {
  59. //logger(L_DBG, "findExact()");
  60. if (mode == NULL || key == NULL) {
  61. logger(L_DBG, "findExact() input is empty ?");
  62. return NULL;
  63. }
  64. type_key* It = mode->keys;
  65. // Search exact command
  66. while (It != NULL && It->key != NULL && strcmp(It->key,key) != 0) {
  67. //sprintf(tmp,"findExact search >%s< compare to >%s<", It->key, key);
  68. //logger(L_DBG,tmp);
  69. It = (type_key*) It->next;
  70. //logger(L_DBG, "findExact() next loop");
  71. }
  72. //logger(L_DBG, "findExact() exiting");
  73. return It;
  74. }
  75. mode* findMode(const char *name)
  76. {
  77. mode *mp = modes;
  78. while (mp && strcmp(name, mp->name) != 0) {
  79. mp = (mode*) mp->next;
  80. }
  81. //if (mp) {
  82. // DEBUG2("findMode() %s = %s",name,mp->name);
  83. //}
  84. return mp;
  85. }
  86. void setMode (const char *modeName)
  87. {
  88. if (modeName == NULL) {
  89. logger(L_DBG, "setMode() NULL input");
  90. return;
  91. }
  92. DEBUG2("setMode() to %s", modeName);
  93. mode *mp = findMode(modeName);
  94. if (mp) {
  95. currentMode = mp;
  96. DEBUG2("setMode() new mode was set to %s", currentMode->name);
  97. } else {
  98. logger(L_DBG, "setMode() new mode did not found");
  99. }
  100. return;
  101. }
  102. static void setCfgDir(char *d)
  103. {
  104. //printf("CFG DIR set TO >%s<\n",(d?d:"NULL"));
  105. if (conf.cfgDir != NULL) {
  106. free(conf.cfgDir);
  107. conf.cfgDir = NULL;
  108. }
  109. if (getenv("AR_CFGDIR")) { // override if set
  110. conf.cfgDir = strdup(getenv("AR_CFGDIR"));
  111. //printf("CFG DIR >%s<\n",(conf.cfgDir?conf.cfgDir:"NULL"));
  112. return;
  113. }
  114. if (d != NULL) { // search ../Utils directory
  115. char* try2 = (char*) malloc(strlen(d) + 10); // + "/../Utils"
  116. strcpy(try2,d);
  117. strcat(try2,"/../Utils");
  118. struct stat buf;
  119. if(stat(try2, &buf) == 0) {
  120. try2[strlen(try2)-6] = '\0';
  121. conf.cfgDir = strdup(try2);
  122. free(try2);
  123. printf("CFG DIR >%s<\n",(conf.cfgDir?conf.cfgDir:"NULL"));
  124. return;
  125. }
  126. free(try2);
  127. }
  128. // last resort: use {prefix}/anyremote/cfg-data directory
  129. #ifdef DATADIR
  130. conf.cfgDir = (char*) malloc(strlen(DATADIR)+20); // + "/anyremote/cfg-data"
  131. strcpy(conf.cfgDir,DATADIR);
  132. strcat(conf.cfgDir,"/anyremote/cfg-data");
  133. #else
  134. conf.cfgDir = (char*) malloc(2);
  135. strcpy(conf.cfgDir,".");
  136. #endif
  137. //printf("CFG DIR >%s<\n",(conf.cfgDir?conf.cfgDir:"NULL"));
  138. }
  139. /* Parses command line options and set flags. */
  140. int parse_opts(int argc, char *argv[])
  141. {
  142. int i;
  143. for(i=1; i<argc; i++) {
  144. if (strcmp(argv[i],"-log")==0 || strcmp(argv[i],"-l")==0) {
  145. setVarSimple("Logging", "debug");
  146. } else if (strcmp(argv[i],"--autoconnect")==0 || strcmp(argv[i],"-a")==0) {
  147. setVarSimple(AUTOCONN_STR, "true");
  148. } else if ((strcmp(argv[i],"--serial")==0 || strcmp(argv[i],"-s")==0) && i+1<argc) {
  149. setVarSimple("Device", argv[++i]);
  150. } else if (strcmp(argv[i],"-name")==0) {
  151. setVarSimple("ServiceName", argv[++i]);
  152. } else if (strcmp(argv[i],"-fe")==0 && i+1<argc) {
  153. conf.frontEnd=atoi(argv[++i]);
  154. } else if (strcmp(argv[i],"-http")==0) {
  155. conf.http = 1;
  156. } else if (strcmp(argv[i],"--user")==0 || strcmp(argv[i],"-u")==0) {
  157. if(getUidGid(argv[++i], &conf.uid, &conf.gid)!=EXIT_OK) {
  158. printf("WARNING: bad username %s\n", argv[i]);
  159. }
  160. } else if (strcmp(argv[i],"-password")==0) {
  161. conf.pass = 1;
  162. } else if (strcmp(argv[i],"-f")==0) {
  163. ++i;
  164. continue; // already processed this parameter
  165. // -h and -v handled in main.c
  166. } else {
  167. printf("ERROR: Unknown input parameter %s\n", argv[i]);
  168. }
  169. }
  170. return 1;
  171. }
  172. static void regexpPrepare(regex_t *r, const char *p)
  173. {
  174. int err_no=0;
  175. if((err_no=regcomp(r, p, REG_EXTENDED))!=0) {
  176. size_t length;
  177. char *buffer;
  178. length = regerror (err_no, r, NULL, 0);
  179. buffer = (char*) malloc(length);
  180. regerror (err_no, r, buffer, length);
  181. printf("regexpPrepare(): regcomp() error %s", buffer);
  182. free(buffer);
  183. regfree(r);
  184. free(r);
  185. exit(1); // exit since we can't parse cfg
  186. }
  187. return;
  188. }
  189. static void deleteSpaces(char **str, int afterCommas)
  190. {
  191. char *n = (*str);
  192. char *r = (*str);
  193. int c = 0;
  194. //printf("INFO : deleteSpaces in: >%s<\n", *str);
  195. while (*r != '\0') {
  196. if (isspace(*r) && c >= afterCommas) {
  197. r++;
  198. } else {
  199. if (*r == ',') {
  200. c++;
  201. }
  202. *n = *r;
  203. r++;
  204. n++;
  205. }
  206. }
  207. *n = '\0';
  208. //printf("INFO : deleteSpaces out: >%s<\n", *str);
  209. }
  210. static void normalizeSequence(char **str)
  211. {
  212. int isSpace = 0;
  213. char *n = (*str);
  214. char *r = (*str);
  215. //printf("INFO : normalizeSequence in: >%s<\n", *str);
  216. while (*r != '\0') {
  217. if (isspace(*r)) {
  218. // Skip spaces
  219. if(!isSpace) {
  220. isSpace = 1;
  221. }
  222. r++;
  223. } else if (*r == '(' && r > *str && strstr(*str,"$$")) { // This is parametrized command Command($$), we have to delete all spaces from brace
  224. r--;
  225. deleteSpaces(&r, 0);
  226. return;
  227. } else {
  228. if(isSpace) {
  229. *n = ' ';
  230. n++;
  231. }
  232. // Copy all symbols
  233. *n = *r;
  234. r++;
  235. n++;
  236. isSpace = 0;
  237. }
  238. }
  239. *n = '\0';
  240. //printf("INFO : normalizeSequence out: >%s<\n", *str);
  241. }
  242. static regmatch_t* allocRegmatch(int no_sub)
  243. {
  244. regmatch_t* result;
  245. if ((result = (regmatch_t *) malloc(sizeof(regmatch_t) * no_sub))==0) {
  246. printf("allocRegmatch(): No more memory");
  247. exit(1);
  248. }
  249. return result;
  250. }
  251. static int parseKeyValue(char *in, char ** tag, char ** value)
  252. {
  253. //printf("INFO: parseKeyValue() %s (%d)\n", in, (int)keyVal->re_nsub+1);
  254. size_t no_sub = keyVal->re_nsub+1;
  255. regmatch_t* result = allocRegmatch(no_sub);
  256. if (in[strlen(in) - 1] == '\n') {
  257. in[strlen(in) - 1] = '\0';
  258. }
  259. *value = NULL;
  260. *tag = NULL;
  261. if (regexec(keyVal, in, no_sub, result, 0)==0) {
  262. int use1 = 1;
  263. int use2 = 2;
  264. if (result[1].rm_so == -1) {
  265. if (result[3].rm_so == -1) {
  266. printf("parseKeyValue(): Incorrectly formed command (1) %s\n", in);
  267. use1 = -1;
  268. }
  269. use1 = 3;
  270. }
  271. if (result[2].rm_so == -1) {
  272. if (result[4].rm_so == -1) {
  273. printf("parseKeyValue(): Incorrectly formed command (2) %s\n", in);
  274. use2 = -1;
  275. }
  276. use2 = 4;
  277. }
  278. if (use1 > 0) {
  279. *tag = in + result[use1].rm_so;
  280. *(in + result[use1].rm_eo) = '\0';
  281. }
  282. if (use2 > 0) {
  283. if (result[use2].rm_so == -1 || result[use2].rm_eo == -1) {
  284. *value = NULL;
  285. } else {
  286. *value = in + result[use2].rm_so;
  287. *(in + result[use2].rm_eo) = '\0';
  288. }
  289. }
  290. /*if(*tag != NULL) {
  291. printf("INFO : Got tag : >%s<\n", *tag);
  292. } else {
  293. printf("INFO : Got tag : >NULL<\n");
  294. }
  295. if(*value != NULL) {
  296. printf("INFO : Got value : >%s<\n", *value);
  297. }else {
  298. printf("INFO : Got value : >NULL<\n");
  299. }*/
  300. //} else {
  301. // printf("INFO : Not matched\n");
  302. }
  303. free(result);
  304. return EXIT_OK;
  305. }
  306. const char* id2Cmd (int cmdId)
  307. {
  308. switch(cmdId) {
  309. case ID_EXIT:
  310. return CMD_EXIT;
  311. case ID_EXEC:
  312. return CMD_EXEC;
  313. case ID_SENDCKPD:
  314. return CMD_SENDCKPD;
  315. case ID_SET:
  316. return CMD_SET;
  317. case ID_EXECSET:
  318. return CMD_EXECSET;
  319. case ID_TIMER:
  320. return CMD_TIMER;
  321. case ID_SEND:
  322. return CMD_SEND;
  323. case ID_EXECSEND:
  324. return CMD_EXECSEND;
  325. case ID_MACRO:
  326. return CMD_MACRO;
  327. case ID_LOAD:
  328. return CMD_LOAD;
  329. case ID_INCLUDE:
  330. return CMD_INCLUDE;
  331. case ID_GET:
  332. return CMD_GET;
  333. case ID_MAKE:
  334. return CMD_MAKE;
  335. case ID_EMU:
  336. return CMD_EMU;
  337. case ID_DBUS:
  338. return CMD_DBUS;
  339. default:
  340. return "Unknown";
  341. }
  342. }
  343. static int cmd2id (char *name)
  344. {
  345. if (strcmp(name,CMD_EXIT) == 0) {
  346. return ID_EXIT;
  347. } else if (strcmp(name,CMD_EXEC) == 0) {
  348. return ID_EXEC;
  349. } else if (strcmp(name,CMD_SENDCKPD) == 0) {
  350. return ID_SENDCKPD;
  351. } else if (strcmp(name,CMD_SET) == 0) {
  352. return ID_SET;
  353. } else if (strcmp(name,CMD_EXECSET) == 0) {
  354. return ID_EXECSET;
  355. } else if (strcmp(name,CMD_TIMER) == 0) {
  356. return ID_TIMER;
  357. } else if (strcmp(name,CMD_SEND) == 0) {
  358. return ID_SEND;
  359. } else if (strcmp(name,CMD_EXECSEND) == 0) {
  360. return ID_EXECSEND;
  361. } else if (strcmp(name,CMD_MACRO) == 0) {
  362. return ID_MACRO;
  363. } else if (strcmp(name,CMD_LOAD) == 0) {
  364. return ID_LOAD;
  365. } else if (strcmp(name,CMD_INCLUDE) == 0) {
  366. return ID_INCLUDE;
  367. } else if (strcmp(name,CMD_GET) == 0) {
  368. return ID_GET;
  369. } else if (strcmp(name,CMD_MAKE) == 0) {
  370. return ID_MAKE;
  371. } else if (strcmp(name,CMD_EMU) == 0) {
  372. return ID_EMU;
  373. } else if (strcmp(name,CMD_DBUS) == 0) {
  374. return ID_DBUS;
  375. }
  376. return ID_UNKNOWN;
  377. }
  378. int cmdSet2id (const char *name)
  379. {
  380. if (name == NULL) {
  381. return ID_UNKNOWN;
  382. }
  383. if (strncmp(name, SET_STATUS, strlen(SET_STATUS)) == 0) {
  384. return ID_STATUS;
  385. } else if (strncmp(name, SET_TITLE, strlen(SET_TITLE)) == 0) {
  386. return ID_TITLE;
  387. } else if (strncmp(name, SET_COVER, strlen(SET_COVER)) == 0) {
  388. return ID_COVER;
  389. } else if (strncmp(name, SET_ICONS, strlen(SET_ICONS)) == 0) {
  390. return ID_ICONS;
  391. } else if (strncmp(name, SET_VOLUME, strlen(SET_VOLUME)) == 0) {
  392. return ID_VOLUME;
  393. } else if (strncmp(name, SET_SKIN, strlen(SET_SKIN)) == 0) {
  394. return ID_SKIN;
  395. } else if (strncmp(name, SET_BG, strlen(SET_BG)) == 0) {
  396. return ID_BG;
  397. } else if (strncmp(name, SET_FG, strlen(SET_FG)) == 0) {
  398. return ID_FG;
  399. } else if (strncmp(name, SET_FONT, strlen(SET_FONT)) == 0) {
  400. return ID_FONT;
  401. }
  402. return ID_UNKNOWN;
  403. }
  404. static int storeGetCmd(cmdItem* ci, char *cmd)
  405. {
  406. int parseFail = 0;
  407. regex_t* regex = (regex_t *) malloc(sizeof(regex_t));
  408. memset(regex, 0, sizeof(regex_t));
  409. regexpPrepare(regex, REGEX_GET);
  410. size_t no_sub = regex->re_nsub+1;
  411. regmatch_t* result = allocRegmatch(no_sub);
  412. if (regexec(regex, cmd, no_sub, result, 0) == 0) { // screen_size, cover_size, icon_size, icon_padding, model
  413. if (result[1].rm_so >= 0) {
  414. ci->descr = (char*) calloc(1, result[1].rm_eo - result[1].rm_so + 1);
  415. strncpy(ci->descr, cmd + result[1].rm_so, result[1].rm_eo - result[1].rm_so);
  416. } else if (result[2].rm_so >= 0) { // is_exists
  417. int l2 = result[2].rm_eo - result[2].rm_so;
  418. int l3 = result[3].rm_eo - result[3].rm_so;
  419. int l4 = result[4].rm_eo - result[4].rm_so;
  420. ci->descr = (char*) calloc(1, l2 + l3 + l4 + 3);
  421. strncpy(ci->descr,cmd + result[2].rm_so, l2);
  422. strcat (ci->descr,",");
  423. strncat(ci->descr,cmd + result[3].rm_so, l3);
  424. strcat (ci->descr,",");
  425. strncat(ci->descr,cmd + result[4].rm_so, l4);
  426. }
  427. } else {
  428. printf("storeGetCmd(): parse error\n");
  429. parseFail = 1;
  430. }
  431. if (parseFail) {
  432. printf("storeGetCmd(): command Get( %s ) is formed incorrectly\n", cmd);
  433. }
  434. free(result);
  435. result = NULL;
  436. regfree(regex);
  437. free(regex);
  438. regex = NULL;
  439. return (parseFail == 0 ? EXIT_OK : EXIT_NOK);
  440. }
  441. static int storeMakeCmd(cmdItem* ci, char *cmd)
  442. {
  443. int parseFail = 0;
  444. //printf ("storeMakeCmd: got >%s<\n",cmd);
  445. regex_t* regex = (regex_t *) malloc(sizeof(regex_t));
  446. memset(regex, 0, sizeof(regex_t));
  447. regexpPrepare(regex, REGEX_MAKE);
  448. size_t no_sub = regex->re_nsub+1;
  449. regmatch_t* result = allocRegmatch(no_sub);
  450. if (regexec(regex, cmd, no_sub, result, 0) == 0) {
  451. if (result[1].rm_so >= 0 && result[2].rm_so > 0) {
  452. int l = result[1].rm_eo - result[1].rm_so;
  453. if (strncmp(cmd+result[1].rm_so,MAKE_VAR,l) == 0) { // var
  454. regex_t* regex2 = (regex_t *) malloc(sizeof(regex_t));
  455. memset(regex2, 0, sizeof(regex_t));
  456. regexpPrepare(regex2, REGEX_SET_VR);
  457. size_t no_sub2 = regex2->re_nsub+1;
  458. regmatch_t* result2 = allocRegmatch(no_sub2);
  459. char* start2 = cmd + result[2].rm_so;
  460. int l2 = 0;
  461. if (regexec(regex2, cmd + result[2].rm_so, no_sub2, result2, 0) == 0) {
  462. if (result2[1].rm_so >= 0 && result2[2].rm_so >= 0) {
  463. l2 = result2[1].rm_eo - result2[1].rm_so;
  464. int le = result2[2].rm_eo - result2[2].rm_so;
  465. ci->descr = (char*) calloc(1, l + l2 + 2);
  466. strncpy(ci->descr,cmd + result[1].rm_so, l);
  467. strcat (ci->descr,",");
  468. strncat(ci->descr,start2 + result2[1].rm_so, l2);
  469. ci->exec = (char*) calloc(1, le + 1);
  470. strncpy(ci->exec,start2 + result2[2].rm_so, le);
  471. } else {
  472. printf("storeMakeCmd(): parse error (M V1)\n");
  473. parseFail = 1;
  474. }
  475. } else {
  476. printf("storeMakeCmd(): parse error (M V2)\n");
  477. parseFail = 1;
  478. }
  479. regfree(regex2);
  480. free(regex2);
  481. regex2 = NULL;
  482. free(result2);
  483. result2 = NULL;
  484. } else if (strncmp(cmd + result[1].rm_so, MAKE_MODE, l) == 0 || // mode
  485. strncmp(cmd + result[1].rm_so, MAKE_ALARM, l) == 0) { // alarm
  486. ci->descr = (char*) calloc(1, result[1].rm_eo - result[1].rm_so + 1);
  487. ci->exec = (char*) calloc(1, result[2].rm_eo - result[2].rm_so + 1);
  488. strncpy(ci->descr,cmd + result[1].rm_so, result[1].rm_eo - result[1].rm_so);
  489. strncpy(ci->exec, cmd + result[2].rm_so, result[2].rm_eo - result[2].rm_so);
  490. //printf ("storeMakeCmd: (2) %s %s \n",ci->descr,ci->exec);
  491. } else if (strncmp(cmd + result[1].rm_so, MAKE_REMOTE, l) == 0) { // remote
  492. if (strncmp(cmd + result[2].rm_so, "on", 2) == 0 ||
  493. strncmp(cmd + result[2].rm_so, "off", 3) == 0) {
  494. ci->descr = (char*) calloc(1, result[1].rm_eo - result[1].rm_so + 1);
  495. ci->exec = (char*) calloc(1, result[2].rm_eo - result[2].rm_so + 1);
  496. strncpy(ci->descr,cmd + result[1].rm_so, result[1].rm_eo - result[1].rm_so);
  497. strncpy(ci->exec, cmd + result[2].rm_so, result[2].rm_eo - result[2].rm_so);
  498. //printf ("storeMakeCmd: (3) %s %s \n",ci->descr,ci->exec);
  499. } else {
  500. printf("storeMakeCmd(): parse error: invalid parameter in Make(remote,...)\n");
  501. parseFail = 1;
  502. }
  503. } else {
  504. parseFail = 1;
  505. printf("storeMakeCmd(): parse error: unknown subcommand (1)\n");
  506. }
  507. } else if (result[3].rm_so >= 0) { // disconnect, flush
  508. ci->descr = (char*) calloc(1, result[3].rm_eo - result[3].rm_so + 1);
  509. strncpy(ci->descr,cmd + result[3].rm_so, result[3].rm_eo - result[3].rm_so);
  510. } else {
  511. parseFail = 1;
  512. printf("storeMakeCmd(): parse error: unknown subcommand (2)\n");
  513. }
  514. } else {
  515. printf("storeMakeCmd(): parse error\n");
  516. parseFail = 1;
  517. }
  518. if (parseFail) {
  519. printf("storeMakeCmd(): command Make( %s ) is formed incorrectly\n", cmd);
  520. }
  521. free(result);
  522. result = NULL;
  523. regfree(regex);
  524. free(regex);
  525. regex = NULL;
  526. return (parseFail == 0 ? EXIT_OK : EXIT_NOK);
  527. }
  528. static void storeMacroCmd(cmdItem* ci, char *cmd)
  529. {
  530. //printf ("storeMacroCmd: got >%s<\n",cmd);
  531. char *comma = index(cmd,',');
  532. if (comma) {
  533. *comma = '\0';
  534. comma++;
  535. while (comma && (*comma == ' ' || *comma == '\t')) {
  536. comma++;
  537. }
  538. }
  539. // Why ?
  540. //deleteSpaces(&cmd,0);
  541. char *dsc = (char*) calloc(1, strlen(cmd) + 1);
  542. strcpy(dsc, cmd);
  543. ci->descr = dsc;
  544. if (comma && *comma != '\0') {
  545. char *ex = (char*) calloc(1, strlen(comma) + 1);
  546. strcpy(ex, comma);
  547. ci->exec = ex;
  548. }
  549. }
  550. static int storeExecAndSetCmd(cmdItem* ci, char *cmd, int execFlag)
  551. {
  552. int parseFail = 0;
  553. regex_t* regex = (regex_t *) malloc(sizeof(regex_t));
  554. memset(regex, 0, sizeof(regex_t));
  555. regexpPrepare(regex, REGEX_SET);
  556. size_t no_sub = regex->re_nsub+1;
  557. //printf("storeExecAndSetCmd() %s (%d)\n", cmd, (int)no_sub);
  558. regmatch_t* result = allocRegmatch(no_sub);
  559. if (regexec(regex, cmd, no_sub, result, 0) == 0) { // Match it
  560. //printf("storeExecAndSetCmd() matched %d %d\n", result[1].rm_so, result[1].rm_eo);
  561. if (result[1].rm_so >= 0 && result[2].rm_so > 0) { // subcommand , params matched
  562. int l = result[1].rm_eo - result[1].rm_so;
  563. if (strncmp(cmd+result[1].rm_so,SET_BG,l) == 0 ||
  564. strncmp(cmd+result[1].rm_so,SET_PARAM,l) == 0 ||
  565. strncmp(cmd+result[1].rm_so,SET_EFIELD,l) == 0 ||
  566. strncmp(cmd+result[1].rm_so,SET_FG,l) == 0 ||
  567. strncmp(cmd+result[1].rm_so,SET_FONT,l) == 0 ||
  568. strncmp(cmd+result[1].rm_so,SET_FSCREEN,l) == 0 ||
  569. strncmp(cmd+result[1].rm_so,SET_ICONS,l) == 0 ||
  570. strncmp(cmd+result[1].rm_so,SET_SKIN,l) == 0 ||
  571. strncmp(cmd+result[1].rm_so,SET_STATUS,l) == 0 ||
  572. strncmp(cmd+result[1].rm_so,SET_TITLE,l) == 0 ||
  573. strncmp(cmd+result[1].rm_so,SET_VOLUME,l) == 0 ||
  574. strncmp(cmd+result[1].rm_so,SET_COVER,l) == 0 ||
  575. strncmp(cmd+result[1].rm_so,SET_POPUP,l) == 0
  576. ) {
  577. int le = result[2].rm_eo - result[2].rm_so;
  578. int le2 = 0;
  579. if (!execFlag) {
  580. le2 = le + 1;
  581. }
  582. ci->descr = (char*) calloc(1, l + le2 + 1);
  583. strncpy(ci->descr,cmd + result[1].rm_so, l);
  584. if (execFlag) {
  585. ci->exec = (char*) calloc(1, le + 1);
  586. strncpy(ci->exec,cmd + result[2].rm_so, le);
  587. } else {
  588. strcat (ci->descr,",");
  589. strncat(ci->descr,cmd + result[2].rm_so, le);
  590. }
  591. } else if (strncmp(cmd+result[1].rm_so,SET_FMGR,l) == 0) {
  592. regex_t* regex2 = (regex_t *) malloc(sizeof(regex_t));
  593. memset(regex2, 0, sizeof(regex_t));
  594. regexpPrepare(regex2, REGEX_SET_FM);
  595. size_t no_sub2 = regex2->re_nsub+1;
  596. regmatch_t* result2 = allocRegmatch(no_sub2);
  597. char* start2 = cmd + result[2].rm_so;
  598. //printf("parse %s\n", cmd + result[2].rm_so);
  599. if (regexec(regex2, cmd + result[2].rm_so, no_sub2, result2, 0) == 0) {
  600. int l2 = 0;
  601. if (result2[1].rm_so >= 0 && result2[2].rm_so >= 0) { // add|replace|select,left|right,_data_
  602. l2 = result2[1].rm_eo - result2[1].rm_so;
  603. int l3 = result2[2].rm_eo - result2[2].rm_so;
  604. int le = result2[3].rm_eo - result2[3].rm_so;
  605. int le2 = 0;
  606. if (!execFlag) {
  607. le2 = le + 1;
  608. }
  609. ci->descr = (char*) calloc(1, l + l2 + l3 + le2 + 3);
  610. strncpy(ci->descr,cmd + result[1].rm_so, l);
  611. strcat (ci->descr,",");
  612. strncat(ci->descr,start2 + result2[1].rm_so, l2);
  613. strcat (ci->descr,",");
  614. strncat(ci->descr,start2 + result2[2].rm_so, l3);
  615. if (execFlag) {
  616. ci->exec = (char*) calloc(1, le + 1);
  617. strncpy(ci->exec,start2 + result2[3].rm_so, le);
  618. } else {
  619. strcat (ci->descr,",");
  620. strncat(ci->descr,start2 + result2[3].rm_so, le);
  621. }
  622. } else if (result2[4].rm_so >= 0) { // close|show
  623. l2 = result2[4].rm_eo - result2[4].rm_so;
  624. ci->descr = (char*) calloc(1, l + l2 + 2);
  625. strncpy(ci->descr,cmd + result[1].rm_so, l);
  626. strcat (ci->descr,",");
  627. strncat(ci->descr,start2 + result2[4].rm_so, l2);
  628. } else {
  629. printf("storeExecAndSetCmd(): parse error (F1)\n");
  630. parseFail = 1;
  631. }
  632. } else {
  633. printf("storeExecAndSetCmd(): parse error (F2)\n");
  634. parseFail = 1;
  635. }
  636. regfree(regex2);
  637. free(regex2);
  638. regex2 = NULL;
  639. free(result2);
  640. result2 = NULL;
  641. } else if (strncmp(cmd+result[1].rm_so,SET_LIST,l) == 0 ||
  642. strncmp(cmd+result[1].rm_so,SET_ILIST,l) == 0 ||
  643. strncmp(cmd+result[1].rm_so,SET_TEXT,l) == 0) {
  644. //printf("storeExecAndSetCmd() matched2 %d %d\n", result[2].rm_so, result[2].rm_eo);
  645. regex_t* regex2 = (regex_t *) malloc(sizeof(regex_t));
  646. memset(regex2, 0, sizeof(regex_t));
  647. regexpPrepare(regex2, REGEX_SET_TL);
  648. size_t no_sub2 = regex2->re_nsub+1;
  649. regmatch_t* result2 = allocRegmatch(no_sub2);
  650. char* start2 = cmd + result[2].rm_so;
  651. //printf("parse %s\n", cmd + result[2].rm_so);
  652. if (regexec(regex2, cmd + result[2].rm_so, no_sub2, result2, 0) == 0) {
  653. int l2 = 0;
  654. if (result2[1].rm_so >= 0) { // fg|bg|font|select
  655. l2 = result2[1].rm_eo - result2[1].rm_so;
  656. int le = result2[2].rm_eo - result2[2].rm_so;
  657. int le2 = 0;
  658. if (!execFlag) {
  659. le2 = le + 1;
  660. }
  661. ci->descr = (char*) calloc(1, l + l2 + le2 + 2);
  662. strncpy(ci->descr,cmd + result[1].rm_so, l);
  663. strcat (ci->descr,",");
  664. strncat(ci->descr,start2 + result2[1].rm_so, l2);
  665. if (execFlag) {
  666. ci->exec = (char*) calloc(1, le + 1);
  667. strncpy(ci->exec,start2 + result2[2].rm_so, le);
  668. } else {
  669. strcat (ci->descr,",");
  670. strncat(ci->descr,start2 + result2[2].rm_so, le);
  671. }
  672. } else if (result2[3].rm_so >= 0) { // add|replace,_title_
  673. l2 = result2[3].rm_eo - result2[3].rm_so;
  674. int l3 = result2[4].rm_eo - result2[4].rm_so;
  675. int le = result2[5].rm_eo - result2[5].rm_so;
  676. int le2 = 0;
  677. if (!execFlag) {
  678. le2 = le + 1;
  679. }
  680. ci->descr = (char*) calloc(1, l + l2 + l3 + le2 + 3);
  681. strncpy(ci->descr,cmd + result[1].rm_so, l);
  682. strcat (ci->descr,",");
  683. strncat(ci->descr,start2 + result2[3].rm_so, l2);
  684. strcat (ci->descr,",");
  685. strncat(ci->descr,start2 + result2[4].rm_so, l3);
  686. if (execFlag) {
  687. ci->exec = (char*) calloc(1, le + 1);
  688. strncpy(ci->exec,start2 + result2[5].rm_so, le);
  689. } else {
  690. strcat (ci->descr,",");
  691. strncat(ci->descr,start2 + result2[5].rm_so, le);
  692. }
  693. } else if (result2[6].rm_so >= 0 && result2[7].rm_so >= 0) { // close,clear
  694. l2 = result2[6].rm_eo - result2[6].rm_so;
  695. int l3 = result2[7].rm_eo - result2[7].rm_so;
  696. ci->descr = (char*) calloc(1, l + l2 + l3 + 3);
  697. strncpy(ci->descr,cmd + result[1].rm_so, l);
  698. strcat (ci->descr,",");
  699. strncat(ci->descr,start2 + result2[6].rm_so, l2);
  700. strcat (ci->descr,",");
  701. strncat(ci->descr,start2 + result2[7].rm_so, l3);
  702. } else if (result2[8].rm_so >= 0) { // close|clear|show
  703. l2 = result2[8].rm_eo - result2[8].rm_so;
  704. ci->descr = (char*) calloc(1, l + l2 + 2);
  705. strncpy(ci->descr,cmd + result[1].rm_so, l);
  706. strcat (ci->descr,",");
  707. strncat(ci->descr,start2 + result2[8].rm_so, l2);
  708. } else {
  709. printf("storeExecAndSetCmd(): parse error (T1)\n");
  710. parseFail = 1;
  711. }
  712. } else {
  713. printf("storeExecAndSetCmd(): parse error (T2)\n");
  714. parseFail = 1;
  715. }
  716. regfree(regex2);
  717. free(regex2);
  718. regex2 = NULL;
  719. free(result2);
  720. result2 = NULL;
  721. } else if (strncmp(cmd+result[1].rm_so,SET_MENU,l) == 0 ||
  722. strncmp(cmd+result[1].rm_so,SET_IMAGE,l) == 0) {
  723. regex_t* regex2 = (regex_t *) malloc(sizeof(regex_t));
  724. memset(regex2, 0, sizeof(regex_t));
  725. if (strncmp(cmd+result[1].rm_so,SET_MENU,l) == 0) {
  726. regexpPrepare(regex2, REGEX_SET_MN);
  727. } else {
  728. regexpPrepare(regex2, REGEX_SET_WM);
  729. }
  730. size_t no_sub2 = regex2->re_nsub+1;
  731. regmatch_t* result2 = allocRegmatch(no_sub2);
  732. char* start2 = cmd + result[2].rm_so;
  733. int l2 = 0;
  734. if (regexec(regex2, cmd + result[2].rm_so, no_sub2, result2, 0) == 0) {
  735. if (result2[1].rm_so >= 0) { // add|replace or icon|window
  736. l2 = result2[1].rm_eo - result2[1].rm_so;
  737. int le = result2[2].rm_eo - result2[2].rm_so;
  738. int le2 = 0;
  739. if (!execFlag) {
  740. le2 = le + 1;
  741. }
  742. ci->descr = (char*) calloc(1, l + l2 + le2 + 2);
  743. strncpy(ci->descr,cmd + result[1].rm_so, l);
  744. strcat (ci->descr,",");
  745. strncat(ci->descr,start2 + result2[1].rm_so, l2);
  746. if (execFlag) {
  747. ci->exec = (char*) calloc(1, le + 1);
  748. strncpy(ci->exec,start2 + result2[2].rm_so, le);
  749. } else {
  750. strcat (ci->descr,",");
  751. /*
  752. // attempt to use localized menu on the phone
  753. char* menu = (char *) calloc(1,le+1);
  754. strncpy(menu,start2 + result2[2].rm_so, le);
  755. char *trMenu = translateMenu(menu);
  756. ci->descr = realloc(ci->descr,
  757. l + l2 + 3 + strlen(trMenu));
  758. //printf("REALLOC %d -> %d\n",strlen(ci->descr),
  759. // l + l2 + le2 + 2+strlen(trMenu)-le);
  760. strcat(ci->descr,trMenu);
  761. free(menu);
  762. free(trMenu);
  763. // old non-localized variant
  764. */
  765. strncat(ci->descr,start2 + result2[2].rm_so, le);
  766. }
  767. } else if (result2[3].rm_so >= 0) { // clear or show|remove_all|clear_cache|close|cursor|nocursor|dynamic_cursor
  768. l2 = result2[3].rm_eo - result2[3].rm_so;
  769. ci->descr = (char*) calloc(1, l + l2 + 2);
  770. strncpy(ci->descr,cmd + result[1].rm_so, l);
  771. strcat (ci->descr,",");
  772. strncat(ci->descr,start2 + result2[3].rm_so, l2);
  773. } else if (result2[4].rm_so >= 0) { // set_cursor
  774. int le = result2[5].rm_eo - result2[5].rm_so;
  775. l2 = result2[4].rm_eo - result2[4].rm_so;
  776. int le2 = 0;
  777. if (!execFlag) {
  778. le2 = le + 1;
  779. }
  780. ci->descr = (char*) calloc(1, l + l2 + le2 + 2);
  781. strncpy(ci->descr,cmd + result[1].rm_so, l);
  782. strcat (ci->descr,",");
  783. strncat(ci->descr,start2 + result2[4].rm_so, l2);
  784. if (execFlag) {
  785. ci->exec = (char*) calloc(1, le + 1);
  786. strncpy(ci->exec,start2 + result2[5].rm_so, le);
  787. } else {
  788. strcat (ci->descr,",");
  789. strncat(ci->descr,start2 + result2[5].rm_so, le);
  790. }
  791. } else {
  792. printf("storeExecAndSetCmd(): parse error (M1)\n");
  793. parseFail = 1;
  794. }
  795. }
  796. regfree(regex2);
  797. free(regex2);
  798. regex2 = NULL;
  799. free(result2);
  800. result2 = NULL;
  801. } else {
  802. printf("storeExecAndSetCmd(): parse error (M3)\n");
  803. parseFail = 1;
  804. }
  805. } else if (result[3].rm_so >= 0) { // vibrate,repaint matched
  806. ci->descr = (char*) calloc(1, result[3].rm_eo - result[3].rm_so + 1);
  807. strncpy(ci->descr, cmd + result[3].rm_so, result[3].rm_eo - result[3].rm_so);
  808. } else {
  809. printf("storeExecAndSetCmd(): parse error (1)\n");
  810. parseFail = 1;
  811. }
  812. } else {
  813. // ExecAndSet(_shell_commands_) goes here ????
  814. printf("storeExecAndSetCmd(): parse error (2)\n");
  815. parseFail = 1;
  816. }
  817. if (parseFail) {
  818. if (execFlag) {
  819. printf("storeExecAndSetCmd(): command ExecAndSet( %s ) is formed incorrectly\n", cmd);
  820. } else {
  821. printf("storeExecAndSetCmd(): command Set( %s ) is formed incorrectly\n", cmd);
  822. }
  823. }
  824. free(result);
  825. result = NULL;
  826. regfree(regex);
  827. free(regex);
  828. regex = NULL;
  829. return (parseFail == 1 ? EXIT_NOK : EXIT_OK);
  830. }
  831. static int storeTimerCmd(cmdItem* ci, char *cmd)
  832. {
  833. //printf("storeTimerCmd(): %s\n",cmd);
  834. regex_t* regex = (regex_t *) malloc(sizeof(regex_t));
  835. memset(regex, 0, sizeof(regex_t));
  836. regexpPrepare(regex, REGEX_TIMER);
  837. size_t no_sub = regex->re_nsub+1;
  838. regmatch_t* result = allocRegmatch(no_sub);
  839. int cmdIsOk = EXIT_OK;
  840. if (regexec(regex, cmd, no_sub, result, 0) == 0) { // Match it
  841. if (result[1].rm_so >= 0 && // Timer(key,1,5)
  842. result[2].rm_so > 0 &&
  843. result[3].rm_so > 0) {
  844. int m = result[1].rm_eo - result[1].rm_so;
  845. if (m > MAXARGLEN-1) {
  846. m = MAXARGLEN-1;
  847. }
  848. ci->descr = (char*) calloc(1, m+1);
  849. strncpy(ci->descr, cmd + result[1].rm_so, m);
  850. ci->exec = NULL;
  851. ci->tdata = (timerData*) calloc(1, sizeof(timerData));
  852. char iBuff[8];
  853. memset(iBuff,0,8);
  854. int l = result[2].rm_eo - result[2].rm_so;
  855. if (l > 7) {
  856. l = 7;
  857. }
  858. strncpy(iBuff,cmd + result[2].rm_so,l);
  859. ci->tdata->timeout = atoi(iBuff);
  860. //printf("storeTimerCmd():(%p) %d %s %s %d\n",ci,l,ci->descr,iBuff,ci->tdata->timeout);
  861. l = result[3].rm_eo - result[3].rm_so;
  862. if (l > 7) {
  863. l = 7;
  864. }
  865. memset(iBuff,0,8);
  866. strncpy(iBuff,cmd + result[3].rm_so,l);
  867. ci->tdata->times = atoi(iBuff);
  868. //printf("storeTimerCmd():%d %s %s %d\n",l,ci->descr,iBuff,ci->tdata->times);
  869. } else if (result[4].rm_so >= 0 && // Timer(key,cancel|pause|continue)
  870. result[5].rm_so >= 0) {
  871. int l1 = result[4].rm_eo - result[4].rm_so;
  872. int l2 = result[5].rm_eo - result[5].rm_so;
  873. ci->descr = (char*) calloc(1, l1+1);
  874. strncpy(ci->descr, cmd + result[4].rm_so, l1);
  875. ci->exec = (char*) calloc(1, l2+1);
  876. strncpy(ci->exec, cmd + result[5].rm_so, l2);
  877. ci->tdata = NULL;
  878. }
  879. } else {
  880. printf("storeTimerCmd(): command %s is formed incorrectly (2)\n", cmd);
  881. cmdIsOk = EXIT_NOK;
  882. }
  883. free(result);
  884. result = NULL;
  885. regfree(regex);
  886. free(regex);
  887. regex = NULL;
  888. return cmdIsOk;
  889. }
  890. int storeCmds(cmdItem** cmdList, char *value)
  891. {
  892. cmdItem *curCmd = (cmdItem*) NULL;
  893. int cmdId;
  894. size_t no_sub = cmdByCmd->re_nsub+1;
  895. //printf("storeCmds() %s (%d)\n", value, (int)no_sub);
  896. regmatch_t* result = allocRegmatch(no_sub);
  897. int start = 0;
  898. while(regexec(cmdByCmd, value+start, no_sub, result, 0)==0) {
  899. //printf("storeCmds() next %s\n", value+start);
  900. int step = 0;
  901. char *name = NULL;
  902. char *cmds = NULL;
  903. int u1 = 1;
  904. if (result[1].rm_so >= 0 && result[2].rm_so) { // <name>(<params>) matched
  905. cmds = value + start + result[2].rm_so + 1;
  906. // Search ) which is appropriate to matched (
  907. int braces = 1; // cmd_name(
  908. char *ptr = cmds;
  909. while (*ptr != '\0' && braces > 0) {
  910. if (*ptr == '(') {
  911. braces++;
  912. }
  913. if (*ptr == ')') {
  914. braces--;
  915. }
  916. ptr++;
  917. }
  918. step = ptr - value - start + 1;
  919. if (*(ptr-1) == ')') {
  920. *(ptr-1) = '\0';
  921. }
  922. } else if (result[3].rm_so >= 0) { // Exit matched
  923. u1 = 3;
  924. step = result->rm_eo;
  925. } else {
  926. printf("storeCmds(): Strange match\n");
  927. start += result->rm_eo;
  928. continue;
  929. }
  930. // strip spaces from tail
  931. if (cmds) {
  932. char *p = cmds + strlen(cmds) - 1;
  933. while (isspace(*p)) {
  934. *p = '\0';
  935. p--;
  936. }
  937. }
  938. name = value + start + result[u1].rm_so;
  939. *(value + start + result[u1].rm_eo) = '\0';
  940. if (name == NULL) {
  941. //printf("Got name >NULL<\n");
  942. start +=result->rm_eo;
  943. continue;
  944. //} else {
  945. // printf("Got name >%s<\n",name);
  946. }
  947. cmdId = cmd2id(name);
  948. if (cmdId == ID_UNKNOWN ) {
  949. printf("storeCmds(): Unknown command name %s\n", name);
  950. //here could be params like log=... also
  951. start +=result->rm_eo;
  952. continue;
  953. }
  954. /*if (cmds != NULL) {
  955. printf("Got command body >%s<\n",cmds);
  956. } else {
  957. printf("Got command body >NULL<\n");
  958. }*/
  959. //printf("the rest of cmd >%s<\n",value+start+step);
  960. int cmdIsOk = EXIT_OK;
  961. // Insert into command list
  962. cmdItem* newCmd = (cmdItem*) calloc(1, sizeof(cmdItem));
  963. newCmd->type = cmdId;
  964. newCmd->next = NULL;
  965. if (cmds == NULL) {
  966. newCmd->descr = NULL;
  967. } else {
  968. if (cmdId == ID_SET) {
  969. cmdIsOk = storeExecAndSetCmd(newCmd, cmds, 0);
  970. } else if (cmdId == ID_EXECSET) {
  971. cmdIsOk = storeExecAndSetCmd(newCmd, cmds, 1);
  972. } else if (cmdId == ID_TIMER) {
  973. cmdIsOk = storeTimerCmd(newCmd,cmds);
  974. } else if (cmdId == ID_EXEC) {
  975. char *newDescr = (char*) calloc(1, strlen(cmds) + 1);
  976. strcpy(newDescr, cmds);
  977. newCmd->exec = newDescr;
  978. } else if (cmdId == ID_GET) {
  979. cmdIsOk = storeGetCmd(newCmd, cmds);
  980. } else if (cmdId == ID_MAKE) {
  981. cmdIsOk = storeMakeCmd(newCmd, cmds);
  982. } else if (cmdId == ID_MACRO) {
  983. storeMacroCmd(newCmd, cmds);
  984. } else if (cmdId == ID_EXECSEND) {
  985. char* comma = index(cmds,',');
  986. if (comma == NULL) {
  987. cmdIsOk = EXIT_NOK;
  988. } else {
  989. char *dsc = (char*) calloc(1, comma - cmds + 1);
  990. char *exc = (char*) calloc(1, strlen(comma));
  991. strncpy(dsc, cmds, comma - cmds);
  992. strcpy(exc, comma + 1);
  993. newCmd->descr = dsc;
  994. newCmd->exec = exc;
  995. }
  996. } else {
  997. if (cmdId == ID_SENDCKPD) {
  998. normalizeSequence(&cmds);
  999. } else if (cmdId != ID_SEND) {
  1000. deleteSpaces(&cmds,0);
  1001. }
  1002. char *newDescr = (char*) calloc(1, strlen(cmds) + 1);
  1003. strcpy(newDescr, cmds);
  1004. newCmd->descr = newDescr;
  1005. }
  1006. }
  1007. if (cmdIsOk == EXIT_OK) {
  1008. if (curCmd == NULL) { // Insert first
  1009. *cmdList = newCmd;
  1010. } else {
  1011. curCmd->next = newCmd;
  1012. }
  1013. curCmd = newCmd;
  1014. } else {
  1015. free(newCmd);
  1016. if (cmds == NULL) {
  1017. printf("storeCmds(): command %s was not stored\n", name);
  1018. } else {
  1019. printf("storeCmds(): command %s ( %s ) was not stored\n", name,cmds);
  1020. }
  1021. }
  1022. start +=step;
  1023. //printf("REST OF CMDS %s\n", value+start);
  1024. } // while
  1025. free(result);
  1026. result = NULL;
  1027. return EXIT_OK;
  1028. }
  1029. static int storeKey(mode* cMode, type_key **head, char *tag, char *value)
  1030. {
  1031. type_key* It = NULL;
  1032. int ret = EXIT_OK;
  1033. //printf("storeKey\n");
  1034. normalizeSequence(&tag);
  1035. if(cMode && findExact(cMode, tag)) {
  1036. return EXIT_OK; // do not overwrite existing items
  1037. }
  1038. It = (type_key*) calloc(1, sizeof(type_key));
  1039. // Insert in head
  1040. if ((*head) == NULL) { // first elem
  1041. It->next = NULL;
  1042. // insert to the end
  1043. *head = It;
  1044. } else {
  1045. // insert to the end
  1046. type_key* last = (*head);
  1047. while (last->next) {
  1048. last = last->next;
  1049. }
  1050. last->next = It;
  1051. It->next = NULL;
  1052. // insert to the top version
  1053. //It->next = (type_key*) (*head);
  1054. }
  1055. // insert to the top version
  1056. // *head = It;
  1057. It->key = (char*) calloc(1, strlen(tag)+1);
  1058. strcpy(It->key,tag);
  1059. if (value!= NULL) {
  1060. ret = storeCmds(&It->cmd, value);
  1061. } else {
  1062. It->cmd = NULL;
  1063. }
  1064. return ret;
  1065. }
  1066. static char* loadLine(FILE *fp)
  1067. {
  1068. int lRead = 0;
  1069. int sz = 0;
  1070. load_buf[0] = '\\';
  1071. char* aLine = load_buf;
  1072. while (aLine && sz >= 0 && sz < MAXCMDLEN - 2 && load_buf[sz] == '\\') {
  1073. aLine = fgets((char *) (load_buf+sz), MAXCMDLEN - sz, fp);
  1074. lRead++;
  1075. sz = strlen(load_buf)-2;
  1076. }
  1077. if (aLine || lRead > 1) {
  1078. //printf("GOT: %s\n",load_buf);
  1079. return load_buf;
  1080. }
  1081. return NULL;
  1082. }
  1083. static int loadKeys(FILE *fp)
  1084. {
  1085. char *tmptext;
  1086. char *tag, *value;
  1087. mode *dm = findMode("default");
  1088. if (dm == NULL) {
  1089. // We have at least default mode
  1090. defMode = (mode*) calloc(1, sizeof(mode));
  1091. strcpy(defMode->name,"default");
  1092. defMode->next = NULL;
  1093. defMode->keys = NULL;
  1094. strcpy(defMode->parent,""); // no parent mode by default
  1095. modes = defMode;
  1096. currentMode = defMode;
  1097. }
  1098. mode *curMode = findMode("default");
  1099. do {
  1100. //printf("loadKeys -------------------------------------------------------------------------\n");
  1101. tmptext=loadLine(fp);
  1102. if (tmptext == NULL) {
  1103. return EXIT_NOK;
  1104. }
  1105. tag = NULL;
  1106. value = NULL;
  1107. parseKeyValue(tmptext, &tag, &value);
  1108. if (tag == NULL) {
  1109. //printf("ERROR : incorrect command %s\n", tmptext);
  1110. continue;
  1111. } else if(strcmp(tag,SECTION_END_STR) == 0) {
  1112. break;
  1113. } else if(strcmp(tag,MODE_STR) == 0) { // mode definition
  1114. //printf("MODE_STR >%s<\n",value);
  1115. // split -- mode name : parent mode name
  1116. char *split = index(value,':');
  1117. if (split) {
  1118. *split = '\0';
  1119. split++;
  1120. while (split && (*split == ' ' || *split == '\t')) {
  1121. split++;
  1122. }
  1123. }
  1124. deleteSpaces(&value,0);
  1125. if (split) {
  1126. deleteSpaces(&split,0);
  1127. }
  1128. //printf("MODE_STR >%s< >%s<\n",value,(split?split:"NULL"));
  1129. mode *nm = findMode(value);
  1130. if (nm == NULL) {
  1131. nm = (mode*) calloc(1, sizeof(mode));
  1132. strcpy(nm->name,value);
  1133. nm->keys = NULL;
  1134. if (split) {
  1135. strcpy(nm->parent,split);
  1136. }
  1137. // Insert in head
  1138. if (modes == NULL) { // first elem
  1139. nm->next = NULL;
  1140. } else {
  1141. nm->next = (mode*) modes;
  1142. }
  1143. modes = nm;
  1144. }
  1145. curMode = nm;
  1146. } else if(strcmp(tag,MODE_END_STR) == 0) { // mode definition end
  1147. curMode = defMode;
  1148. } else if (tag != NULL) { // Line with key->command definition
  1149. storeKey(curMode, &(curMode->keys), tag, value);
  1150. } else {
  1151. continue;
  1152. }
  1153. } while (1);
  1154. return EXIT_OK;
  1155. }
  1156. static void loadInternal()
  1157. {
  1158. if (internalMode == NULL) {
  1159. internalMode = (mode*) calloc(1, sizeof(mode));
  1160. strcpy(internalMode->name,"_INTERNAL_");
  1161. internalMode->keys = NULL;
  1162. strcpy(internalMode->parent,""); // no parent mode by default
  1163. }
  1164. char cmd[] = "_GET_ICON_($$)";
  1165. char val[] = "ExecAndSet(image,icon,echo \'UF=\"$(CfgDir)/Icons/$(Index)/$(Param).png\";echo \"$(Param),$UF\"\'|bash -f -s);";
  1166. storeKey(internalMode, &(internalMode->keys),cmd,val);
  1167. char cmd2[] = "_PING_";
  1168. char val2[] = "Get(ping)";
  1169. storeKey(internalMode, &(internalMode->keys),cmd2,val2);
  1170. }
  1171. int load_cfg(char *mfile, int isInit)
  1172. {
  1173. FILE *fp;
  1174. char *tag, *value, *tmptext;
  1175. //printf("LOAD:%s\n",mfile);
  1176. fp=fopen(mfile,"r");
  1177. if (fp == NULL) {
  1178. return EXIT_NOK;
  1179. }
  1180. if (keyVal == NULL) { // init it once
  1181. keyVal = (regex_t *) malloc(sizeof(regex_t));
  1182. memset(keyVal, 0, sizeof(regex_t));
  1183. regexpPrepare(keyVal, REGEX_KEYVAL);
  1184. }
  1185. if (cmdByCmd == NULL) { // init it once
  1186. cmdByCmd = (regex_t *) malloc(sizeof(regex_t));
  1187. memset(cmdByCmd, 0, sizeof(regex_t));
  1188. regexpPrepare(cmdByCmd, REGEX_CMDBYCMD);
  1189. }
  1190. // Go through the conf file
  1191. while(1) {
  1192. tmptext=loadLine(fp);
  1193. if (tmptext==NULL) {
  1194. break;
  1195. }
  1196. parseKeyValue(tmptext, &tag, &value);
  1197. if (tag == NULL) {
  1198. continue;
  1199. } else if (strcmp(tag,KEYS_SECTION_STR) == 0) {
  1200. printf("WARNING: [Keys] tag is deprecated. Please update configuration files !\n");
  1201. if (loadKeys(fp) != EXIT_OK) {
  1202. return EXIT_NOK;
  1203. }
  1204. if (!isInit) { // If we goes here from Include() command, we did all we need
  1205. return EXIT_OK;
  1206. }
  1207. } else if (strcmp(tag,PROTOCOL_SECTION_STR) == 0) {
  1208. if (loadKeys(fp) != EXIT_OK) {
  1209. return EXIT_NOK;
  1210. }
  1211. if (!isInit) { // If we goes here from Include() command, we did all we need
  1212. return EXIT_OK;
  1213. }
  1214. } else if (isInit) {
  1215. if (strncmp("GuiApp",tag,6) != 0) {
  1216. printf("WARNING: Unknown tag in cfg file >%s<\n",tag);
  1217. }
  1218. }
  1219. }
  1220. fclose(fp);
  1221. loadInternal();
  1222. //printf("LOAD:%s OK\n",mfile);
  1223. return EXIT_OK;
  1224. }
  1225. void freeRegexps()
  1226. {
  1227. if (cmdByCmd) {
  1228. regfree(cmdByCmd);
  1229. free(cmdByCmd);
  1230. cmdByCmd = NULL;
  1231. }
  1232. if (keyVal) {
  1233. regfree(keyVal);
  1234. free(keyVal);
  1235. keyVal = NULL;
  1236. }
  1237. }
  1238. void init_cfg_dir(char *path)
  1239. {
  1240. if (path != NULL) {
  1241. char *d = strdup(path);
  1242. char *p = rindex(d,SEPARATOR);
  1243. if (p != NULL) {
  1244. *p = '\0';
  1245. } else {
  1246. *d = '\0';
  1247. }
  1248. setCfgDir(d);
  1249. free(d);
  1250. } else {
  1251. setCfgDir(NULL);
  1252. }
  1253. }
  1254. int init_cfg(char *path)
  1255. {
  1256. //printf("init_cfg %s\n",pa…

Large files files are truncated, but you can click here to view the full file