init_parser.c revision 78a1b1fe1ab76964e35b4a4788238b197bfd613d
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdio.h> 18#include <stdlib.h> 19#include <unistd.h> 20#include <fcntl.h> 21#include <stdarg.h> 22#include <string.h> 23#include <stddef.h> 24#include <ctype.h> 25 26#include "init.h" 27#include "parser.h" 28#include "init_parser.h" 29#include "log.h" 30#include "property_service.h" 31#include "util.h" 32 33#include <cutils/iosched_policy.h> 34#include <cutils/list.h> 35 36#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ 37#include <sys/_system_properties.h> 38 39static list_declare(service_list); 40static list_declare(action_list); 41static list_declare(action_queue); 42 43struct import { 44 struct listnode list; 45 const char *filename; 46}; 47 48static void *parse_service(struct parse_state *state, int nargs, char **args); 49static void parse_line_service(struct parse_state *state, int nargs, char **args); 50 51static void *parse_action(struct parse_state *state, int nargs, char **args); 52static void parse_line_action(struct parse_state *state, int nargs, char **args); 53 54#define SECTION 0x01 55#define COMMAND 0x02 56#define OPTION 0x04 57 58#include "keywords.h" 59 60#define KEYWORD(symbol, flags, nargs, func) \ 61 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, }, 62 63struct { 64 const char *name; 65 int (*func)(int nargs, char **args); 66 unsigned char nargs; 67 unsigned char flags; 68} keyword_info[KEYWORD_COUNT] = { 69 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 }, 70#include "keywords.h" 71}; 72#undef KEYWORD 73 74#define kw_is(kw, type) (keyword_info[kw].flags & (type)) 75#define kw_name(kw) (keyword_info[kw].name) 76#define kw_func(kw) (keyword_info[kw].func) 77#define kw_nargs(kw) (keyword_info[kw].nargs) 78 79int lookup_keyword(const char *s) 80{ 81 switch (*s++) { 82 case 'c': 83 if (!strcmp(s, "opy")) return K_copy; 84 if (!strcmp(s, "apability")) return K_capability; 85 if (!strcmp(s, "hdir")) return K_chdir; 86 if (!strcmp(s, "hroot")) return K_chroot; 87 if (!strcmp(s, "lass")) return K_class; 88 if (!strcmp(s, "lass_start")) return K_class_start; 89 if (!strcmp(s, "lass_stop")) return K_class_stop; 90 if (!strcmp(s, "lass_reset")) return K_class_reset; 91 if (!strcmp(s, "onsole")) return K_console; 92 if (!strcmp(s, "hown")) return K_chown; 93 if (!strcmp(s, "hmod")) return K_chmod; 94 if (!strcmp(s, "ritical")) return K_critical; 95 break; 96 case 'd': 97 if (!strcmp(s, "isabled")) return K_disabled; 98 if (!strcmp(s, "omainname")) return K_domainname; 99 break; 100 case 'e': 101 if (!strcmp(s, "xec")) return K_exec; 102 if (!strcmp(s, "xport")) return K_export; 103 break; 104 case 'g': 105 if (!strcmp(s, "roup")) return K_group; 106 break; 107 case 'h': 108 if (!strcmp(s, "ostname")) return K_hostname; 109 break; 110 case 'i': 111 if (!strcmp(s, "oprio")) return K_ioprio; 112 if (!strcmp(s, "fup")) return K_ifup; 113 if (!strcmp(s, "nsmod")) return K_insmod; 114 if (!strcmp(s, "mport")) return K_import; 115 break; 116 case 'k': 117 if (!strcmp(s, "eycodes")) return K_keycodes; 118 break; 119 case 'l': 120 if (!strcmp(s, "oglevel")) return K_loglevel; 121 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props; 122 break; 123 case 'm': 124 if (!strcmp(s, "kdir")) return K_mkdir; 125 if (!strcmp(s, "ount")) return K_mount; 126 break; 127 case 'o': 128 if (!strcmp(s, "n")) return K_on; 129 if (!strcmp(s, "neshot")) return K_oneshot; 130 if (!strcmp(s, "nrestart")) return K_onrestart; 131 break; 132 case 'r': 133 if (!strcmp(s, "estart")) return K_restart; 134 if (!strcmp(s, "mdir")) return K_rmdir; 135 if (!strcmp(s, "m")) return K_rm; 136 break; 137 case 's': 138 if (!strcmp(s, "ervice")) return K_service; 139 if (!strcmp(s, "etenv")) return K_setenv; 140 if (!strcmp(s, "etkey")) return K_setkey; 141 if (!strcmp(s, "etprop")) return K_setprop; 142 if (!strcmp(s, "etrlimit")) return K_setrlimit; 143 if (!strcmp(s, "ocket")) return K_socket; 144 if (!strcmp(s, "tart")) return K_start; 145 if (!strcmp(s, "top")) return K_stop; 146 if (!strcmp(s, "ymlink")) return K_symlink; 147 if (!strcmp(s, "ysclktz")) return K_sysclktz; 148 break; 149 case 't': 150 if (!strcmp(s, "rigger")) return K_trigger; 151 break; 152 case 'u': 153 if (!strcmp(s, "ser")) return K_user; 154 break; 155 case 'w': 156 if (!strcmp(s, "rite")) return K_write; 157 if (!strcmp(s, "ait")) return K_wait; 158 break; 159 } 160 return K_UNKNOWN; 161} 162 163void parse_line_no_op(struct parse_state *state, int nargs, char **args) 164{ 165} 166 167static int push_chars(char **dst, int *len, const char *chars, int cnt) 168{ 169 if (cnt > *len) 170 return -1; 171 172 memcpy(*dst, chars, cnt); 173 *dst += cnt; 174 *len -= cnt; 175 176 return 0; 177} 178 179static int expand_props(char *dst, const char *src, int dst_size) 180{ 181 int cnt = 0; 182 char *dst_ptr = dst; 183 const char *src_ptr = src; 184 int src_len; 185 int idx = 0; 186 int ret = 0; 187 int left = dst_size - 1; 188 189 if (!src || !dst || dst_size == 0) 190 return -1; 191 192 src_len = strlen(src); 193 194 /* - variables can either be $x.y or ${x.y}, in case they are only part 195 * of the string. 196 * - will accept $$ as a literal $. 197 * - no nested property expansion, i.e. ${foo.${bar}} is not supported, 198 * bad things will happen 199 */ 200 while (*src_ptr && left > 0) { 201 char *c; 202 char prop[PROP_NAME_MAX + 1]; 203 const char *prop_val; 204 int prop_len = 0; 205 206 c = strchr(src_ptr, '$'); 207 if (!c) { 208 while (left-- > 0 && *src_ptr) 209 *(dst_ptr++) = *(src_ptr++); 210 break; 211 } 212 213 memset(prop, 0, sizeof(prop)); 214 215 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr); 216 if (ret < 0) 217 goto err_nospace; 218 c++; 219 220 if (*c == '$') { 221 *(dst_ptr++) = *(c++); 222 src_ptr = c; 223 left--; 224 continue; 225 } else if (*c == '\0') { 226 break; 227 } 228 229 if (*c == '{') { 230 c++; 231 while (*c && *c != '}' && prop_len < PROP_NAME_MAX) 232 prop[prop_len++] = *(c++); 233 if (*c != '}') { 234 /* failed to find closing brace, abort. */ 235 if (prop_len == PROP_NAME_MAX) 236 ERROR("prop name too long during expansion of '%s'\n", 237 src); 238 else if (*c == '\0') 239 ERROR("unexpected end of string in '%s', looking for }\n", 240 src); 241 goto err; 242 } 243 prop[prop_len] = '\0'; 244 c++; 245 } else if (*c) { 246 while (*c && prop_len < PROP_NAME_MAX) 247 prop[prop_len++] = *(c++); 248 if (prop_len == PROP_NAME_MAX && *c != '\0') { 249 ERROR("prop name too long in '%s'\n", src); 250 goto err; 251 } 252 prop[prop_len] = '\0'; 253 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n", 254 prop); 255 } 256 257 if (prop_len == 0) { 258 ERROR("invalid zero-length prop name in '%s'\n", src); 259 goto err; 260 } 261 262 prop_val = property_get(prop); 263 if (!prop_val) { 264 ERROR("property '%s' doesn't exist while expanding '%s'\n", 265 prop, src); 266 goto err; 267 } 268 269 ret = push_chars(&dst_ptr, &left, prop_val, strlen(prop_val)); 270 if (ret < 0) 271 goto err_nospace; 272 src_ptr = c; 273 continue; 274 } 275 276 *dst_ptr = '\0'; 277 return 0; 278 279err_nospace: 280 ERROR("destination buffer overflow while expanding '%s'\n", src); 281err: 282 return -1; 283} 284 285void parse_import(struct parse_state *state, int nargs, char **args) 286{ 287 struct listnode *import_list = state->priv; 288 struct import *import; 289 char conf_file[PATH_MAX]; 290 int ret; 291 292 if (nargs != 2) { 293 ERROR("single argument needed for import\n"); 294 return; 295 } 296 297 ret = expand_props(conf_file, args[1], sizeof(conf_file)); 298 if (ret) { 299 ERROR("error while handling import on line '%d' in '%s'\n", 300 state->line, state->filename); 301 return; 302 } 303 304 import = calloc(1, sizeof(struct import)); 305 import->filename = strdup(conf_file); 306 list_add_tail(import_list, &import->list); 307 INFO("found import '%s', adding to import list", import->filename); 308} 309 310void parse_new_section(struct parse_state *state, int kw, 311 int nargs, char **args) 312{ 313 printf("[ %s %s ]\n", args[0], 314 nargs > 1 ? args[1] : ""); 315 switch(kw) { 316 case K_service: 317 state->context = parse_service(state, nargs, args); 318 if (state->context) { 319 state->parse_line = parse_line_service; 320 return; 321 } 322 break; 323 case K_on: 324 state->context = parse_action(state, nargs, args); 325 if (state->context) { 326 state->parse_line = parse_line_action; 327 return; 328 } 329 break; 330 case K_import: 331 parse_import(state, nargs, args); 332 break; 333 } 334 state->parse_line = parse_line_no_op; 335} 336 337static void parse_config(const char *fn, char *s) 338{ 339 struct parse_state state; 340 struct listnode import_list; 341 struct listnode *node; 342 char *args[INIT_PARSER_MAXARGS]; 343 int nargs; 344 345 nargs = 0; 346 state.filename = fn; 347 state.line = 0; 348 state.ptr = s; 349 state.nexttoken = 0; 350 state.parse_line = parse_line_no_op; 351 352 list_init(&import_list); 353 state.priv = &import_list; 354 355 for (;;) { 356 switch (next_token(&state)) { 357 case T_EOF: 358 state.parse_line(&state, 0, 0); 359 goto parser_done; 360 case T_NEWLINE: 361 state.line++; 362 if (nargs) { 363 int kw = lookup_keyword(args[0]); 364 if (kw_is(kw, SECTION)) { 365 state.parse_line(&state, 0, 0); 366 parse_new_section(&state, kw, nargs, args); 367 } else { 368 state.parse_line(&state, nargs, args); 369 } 370 nargs = 0; 371 } 372 break; 373 case T_TEXT: 374 if (nargs < INIT_PARSER_MAXARGS) { 375 args[nargs++] = state.text; 376 } 377 break; 378 } 379 } 380 381parser_done: 382 list_for_each(node, &import_list) { 383 struct import *import = node_to_item(node, struct import, list); 384 int ret; 385 386 INFO("importing '%s'", import->filename); 387 ret = init_parse_config_file(import->filename); 388 if (ret) 389 ERROR("could not import file '%s' from '%s'\n", 390 import->filename, fn); 391 } 392} 393 394int init_parse_config_file(const char *fn) 395{ 396 char *data; 397 data = read_file(fn, 0); 398 if (!data) return -1; 399 400 parse_config(fn, data); 401 DUMP(); 402 return 0; 403} 404 405static int valid_name(const char *name) 406{ 407 if (strlen(name) > 16) { 408 return 0; 409 } 410 while (*name) { 411 if (!isalnum(*name) && (*name != '_') && (*name != '-')) { 412 return 0; 413 } 414 name++; 415 } 416 return 1; 417} 418 419struct service *service_find_by_name(const char *name) 420{ 421 struct listnode *node; 422 struct service *svc; 423 list_for_each(node, &service_list) { 424 svc = node_to_item(node, struct service, slist); 425 if (!strcmp(svc->name, name)) { 426 return svc; 427 } 428 } 429 return 0; 430} 431 432struct service *service_find_by_pid(pid_t pid) 433{ 434 struct listnode *node; 435 struct service *svc; 436 list_for_each(node, &service_list) { 437 svc = node_to_item(node, struct service, slist); 438 if (svc->pid == pid) { 439 return svc; 440 } 441 } 442 return 0; 443} 444 445struct service *service_find_by_keychord(int keychord_id) 446{ 447 struct listnode *node; 448 struct service *svc; 449 list_for_each(node, &service_list) { 450 svc = node_to_item(node, struct service, slist); 451 if (svc->keychord_id == keychord_id) { 452 return svc; 453 } 454 } 455 return 0; 456} 457 458void service_for_each(void (*func)(struct service *svc)) 459{ 460 struct listnode *node; 461 struct service *svc; 462 list_for_each(node, &service_list) { 463 svc = node_to_item(node, struct service, slist); 464 func(svc); 465 } 466} 467 468void service_for_each_class(const char *classname, 469 void (*func)(struct service *svc)) 470{ 471 struct listnode *node; 472 struct service *svc; 473 list_for_each(node, &service_list) { 474 svc = node_to_item(node, struct service, slist); 475 if (!strcmp(svc->classname, classname)) { 476 func(svc); 477 } 478 } 479} 480 481void service_for_each_flags(unsigned matchflags, 482 void (*func)(struct service *svc)) 483{ 484 struct listnode *node; 485 struct service *svc; 486 list_for_each(node, &service_list) { 487 svc = node_to_item(node, struct service, slist); 488 if (svc->flags & matchflags) { 489 func(svc); 490 } 491 } 492} 493 494void action_for_each_trigger(const char *trigger, 495 void (*func)(struct action *act)) 496{ 497 struct listnode *node; 498 struct action *act; 499 list_for_each(node, &action_list) { 500 act = node_to_item(node, struct action, alist); 501 if (!strcmp(act->name, trigger)) { 502 func(act); 503 } 504 } 505} 506 507void queue_property_triggers(const char *name, const char *value) 508{ 509 struct listnode *node; 510 struct action *act; 511 list_for_each(node, &action_list) { 512 act = node_to_item(node, struct action, alist); 513 if (!strncmp(act->name, "property:", strlen("property:"))) { 514 const char *test = act->name + strlen("property:"); 515 int name_length = strlen(name); 516 517 if (!strncmp(name, test, name_length) && 518 test[name_length] == '=' && 519 (!strcmp(test + name_length + 1, value) || 520 !strcmp(test + name_length + 1, "*"))) { 521 action_add_queue_tail(act); 522 } 523 } 524 } 525} 526 527void queue_all_property_triggers() 528{ 529 struct listnode *node; 530 struct action *act; 531 list_for_each(node, &action_list) { 532 act = node_to_item(node, struct action, alist); 533 if (!strncmp(act->name, "property:", strlen("property:"))) { 534 /* parse property name and value 535 syntax is property:<name>=<value> */ 536 const char* name = act->name + strlen("property:"); 537 const char* equals = strchr(name, '='); 538 if (equals) { 539 char prop_name[PROP_NAME_MAX + 1]; 540 const char* value; 541 int length = equals - name; 542 if (length > PROP_NAME_MAX) { 543 ERROR("property name too long in trigger %s", act->name); 544 } else { 545 memcpy(prop_name, name, length); 546 prop_name[length] = 0; 547 548 /* does the property exist, and match the trigger value? */ 549 value = property_get(prop_name); 550 if (value && (!strcmp(equals + 1, value) || 551 !strcmp(equals + 1, "*"))) { 552 action_add_queue_tail(act); 553 } 554 } 555 } 556 } 557 } 558} 559 560void queue_builtin_action(int (*func)(int nargs, char **args), char *name) 561{ 562 struct action *act; 563 struct command *cmd; 564 565 act = calloc(1, sizeof(*act)); 566 act->name = name; 567 list_init(&act->commands); 568 569 cmd = calloc(1, sizeof(*cmd)); 570 cmd->func = func; 571 cmd->args[0] = name; 572 list_add_tail(&act->commands, &cmd->clist); 573 574 list_add_tail(&action_list, &act->alist); 575 action_add_queue_tail(act); 576} 577 578void action_add_queue_tail(struct action *act) 579{ 580 list_add_tail(&action_queue, &act->qlist); 581} 582 583struct action *action_remove_queue_head(void) 584{ 585 if (list_empty(&action_queue)) { 586 return 0; 587 } else { 588 struct listnode *node = list_head(&action_queue); 589 struct action *act = node_to_item(node, struct action, qlist); 590 list_remove(node); 591 return act; 592 } 593} 594 595int action_queue_empty() 596{ 597 return list_empty(&action_queue); 598} 599 600static void *parse_service(struct parse_state *state, int nargs, char **args) 601{ 602 struct service *svc; 603 if (nargs < 3) { 604 parse_error(state, "services must have a name and a program\n"); 605 return 0; 606 } 607 if (!valid_name(args[1])) { 608 parse_error(state, "invalid service name '%s'\n", args[1]); 609 return 0; 610 } 611 612 svc = service_find_by_name(args[1]); 613 if (svc) { 614 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]); 615 return 0; 616 } 617 618 nargs -= 2; 619 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs); 620 if (!svc) { 621 parse_error(state, "out of memory\n"); 622 return 0; 623 } 624 svc->name = args[1]; 625 svc->classname = "default"; 626 memcpy(svc->args, args + 2, sizeof(char*) * nargs); 627 svc->args[nargs] = 0; 628 svc->nargs = nargs; 629 svc->onrestart.name = "onrestart"; 630 list_init(&svc->onrestart.commands); 631 list_add_tail(&service_list, &svc->slist); 632 return svc; 633} 634 635static void parse_line_service(struct parse_state *state, int nargs, char **args) 636{ 637 struct service *svc = state->context; 638 struct command *cmd; 639 int i, kw, kw_nargs; 640 641 if (nargs == 0) { 642 return; 643 } 644 645 svc->ioprio_class = IoSchedClass_NONE; 646 647 kw = lookup_keyword(args[0]); 648 switch (kw) { 649 case K_capability: 650 break; 651 case K_class: 652 if (nargs != 2) { 653 parse_error(state, "class option requires a classname\n"); 654 } else { 655 svc->classname = args[1]; 656 } 657 break; 658 case K_console: 659 svc->flags |= SVC_CONSOLE; 660 break; 661 case K_disabled: 662 svc->flags |= SVC_DISABLED; 663 svc->flags |= SVC_RC_DISABLED; 664 break; 665 case K_ioprio: 666 if (nargs != 3) { 667 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n"); 668 } else { 669 svc->ioprio_pri = strtoul(args[2], 0, 8); 670 671 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) { 672 parse_error(state, "priority value must be range 0 - 7\n"); 673 break; 674 } 675 676 if (!strcmp(args[1], "rt")) { 677 svc->ioprio_class = IoSchedClass_RT; 678 } else if (!strcmp(args[1], "be")) { 679 svc->ioprio_class = IoSchedClass_BE; 680 } else if (!strcmp(args[1], "idle")) { 681 svc->ioprio_class = IoSchedClass_IDLE; 682 } else { 683 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n"); 684 } 685 } 686 break; 687 case K_group: 688 if (nargs < 2) { 689 parse_error(state, "group option requires a group id\n"); 690 } else if (nargs > NR_SVC_SUPP_GIDS + 2) { 691 parse_error(state, "group option accepts at most %d supp. groups\n", 692 NR_SVC_SUPP_GIDS); 693 } else { 694 int n; 695 svc->gid = decode_uid(args[1]); 696 for (n = 2; n < nargs; n++) { 697 svc->supp_gids[n-2] = decode_uid(args[n]); 698 } 699 svc->nr_supp_gids = n - 2; 700 } 701 break; 702 case K_keycodes: 703 if (nargs < 2) { 704 parse_error(state, "keycodes option requires atleast one keycode\n"); 705 } else { 706 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0])); 707 if (!svc->keycodes) { 708 parse_error(state, "could not allocate keycodes\n"); 709 } else { 710 svc->nkeycodes = nargs - 1; 711 for (i = 1; i < nargs; i++) { 712 svc->keycodes[i - 1] = atoi(args[i]); 713 } 714 } 715 } 716 break; 717 case K_oneshot: 718 svc->flags |= SVC_ONESHOT; 719 break; 720 case K_onrestart: 721 nargs--; 722 args++; 723 kw = lookup_keyword(args[0]); 724 if (!kw_is(kw, COMMAND)) { 725 parse_error(state, "invalid command '%s'\n", args[0]); 726 break; 727 } 728 kw_nargs = kw_nargs(kw); 729 if (nargs < kw_nargs) { 730 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1, 731 kw_nargs > 2 ? "arguments" : "argument"); 732 break; 733 } 734 735 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs); 736 cmd->func = kw_func(kw); 737 cmd->nargs = nargs; 738 memcpy(cmd->args, args, sizeof(char*) * nargs); 739 list_add_tail(&svc->onrestart.commands, &cmd->clist); 740 break; 741 case K_critical: 742 svc->flags |= SVC_CRITICAL; 743 break; 744 case K_setenv: { /* name value */ 745 struct svcenvinfo *ei; 746 if (nargs < 2) { 747 parse_error(state, "setenv option requires name and value arguments\n"); 748 break; 749 } 750 ei = calloc(1, sizeof(*ei)); 751 if (!ei) { 752 parse_error(state, "out of memory\n"); 753 break; 754 } 755 ei->name = args[1]; 756 ei->value = args[2]; 757 ei->next = svc->envvars; 758 svc->envvars = ei; 759 break; 760 } 761 case K_socket: {/* name type perm [ uid gid ] */ 762 struct socketinfo *si; 763 if (nargs < 4) { 764 parse_error(state, "socket option requires name, type, perm arguments\n"); 765 break; 766 } 767 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream") 768 && strcmp(args[2],"seqpacket")) { 769 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n"); 770 break; 771 } 772 si = calloc(1, sizeof(*si)); 773 if (!si) { 774 parse_error(state, "out of memory\n"); 775 break; 776 } 777 si->name = args[1]; 778 si->type = args[2]; 779 si->perm = strtoul(args[3], 0, 8); 780 if (nargs > 4) 781 si->uid = decode_uid(args[4]); 782 if (nargs > 5) 783 si->gid = decode_uid(args[5]); 784 si->next = svc->sockets; 785 svc->sockets = si; 786 break; 787 } 788 case K_user: 789 if (nargs != 2) { 790 parse_error(state, "user option requires a user id\n"); 791 } else { 792 svc->uid = decode_uid(args[1]); 793 } 794 break; 795 default: 796 parse_error(state, "invalid option '%s'\n", args[0]); 797 } 798} 799 800static void *parse_action(struct parse_state *state, int nargs, char **args) 801{ 802 struct action *act; 803 if (nargs < 2) { 804 parse_error(state, "actions must have a trigger\n"); 805 return 0; 806 } 807 if (nargs > 2) { 808 parse_error(state, "actions may not have extra parameters\n"); 809 return 0; 810 } 811 act = calloc(1, sizeof(*act)); 812 act->name = args[1]; 813 list_init(&act->commands); 814 list_add_tail(&action_list, &act->alist); 815 /* XXX add to hash */ 816 return act; 817} 818 819static void parse_line_action(struct parse_state* state, int nargs, char **args) 820{ 821 struct command *cmd; 822 struct action *act = state->context; 823 int (*func)(int nargs, char **args); 824 int kw, n; 825 826 if (nargs == 0) { 827 return; 828 } 829 830 kw = lookup_keyword(args[0]); 831 if (!kw_is(kw, COMMAND)) { 832 parse_error(state, "invalid command '%s'\n", args[0]); 833 return; 834 } 835 836 n = kw_nargs(kw); 837 if (nargs < n) { 838 parse_error(state, "%s requires %d %s\n", args[0], n - 1, 839 n > 2 ? "arguments" : "argument"); 840 return; 841 } 842 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs); 843 cmd->func = kw_func(kw); 844 cmd->nargs = nargs; 845 memcpy(cmd->args, args, sizeof(char*) * nargs); 846 list_add_tail(&act->commands, &cmd->clist); 847} 848