/filesystems-objc/AccessibilityFS/Source/command_main.c

http://macfuse.googlecode.com/ · C · 88 lines · 55 code · 11 blank · 22 comment · 11 complexity · d387c4fc08995c1131fa907c8c0597d4 MD5 · raw file

  1. /*
  2. * command_main.c
  3. * AccessibilityFS
  4. *
  5. * Created by Dave MacLachlan on 2007/12/13.
  6. *
  7. */
  8. // ================================================================
  9. // Copyright (C) 2008 Google Inc.
  10. //
  11. // Licensed under the Apache License, Version 2.0 (the "License");
  12. // you may not use this file except in compliance with the License.
  13. // You may obtain a copy of the License at
  14. //
  15. // http://www.apache.org/licenses/LICENSE-2.0
  16. //
  17. // Unless required by applicable law or agreed to in writing, software
  18. // distributed under the License is distributed on an "AS IS" BASIS,
  19. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. // See the License for the specific language governing permissions and
  21. // limitations under the License.
  22. // ================================================================
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. typedef struct {
  29. const char* command;
  30. const char* axcommand;
  31. } CommandMap;
  32. const CommandMap kMap [] = {
  33. { "cancel", "AXCancel" },
  34. { "confirm", "AXConfirm" },
  35. { "decrement", "AXDecrement" },
  36. { "increment", "AXIncrement" },
  37. { "press", "AXPress" },
  38. { "raise", "AXRaise" },
  39. { "showmenu", "AXShowMenu" },
  40. };
  41. void showDocumentation() {
  42. fprintf(stderr, "ax_command <command> <object>\nwhere command is one of:\n");
  43. for (int i = 0; i < sizeof(kMap) / sizeof(CommandMap); ++i) {
  44. fprintf(stderr, "%s\n", kMap[i].command);
  45. }
  46. }
  47. int main(int argc, char** argv) {
  48. if (argc != 3) {
  49. showDocumentation();
  50. return -1;
  51. }
  52. const char *command = NULL;
  53. for (int i = 0; i < sizeof(kMap) / sizeof(CommandMap); ++i) {
  54. if (strcasecmp(argv[1], kMap[i].command) == 0) {
  55. command = kMap[i].axcommand;
  56. break;
  57. }
  58. }
  59. if (!command) {
  60. showDocumentation();
  61. return -1;
  62. }
  63. int file = open(argv[2], O_WRONLY);
  64. if (file == -1) {
  65. showDocumentation();
  66. fprintf(stderr, "Unable to open file %s (%d)\n", argv[2], errno);
  67. return errno;
  68. }
  69. ssize_t size = write(file, command, strlen(command));
  70. if (size == -1) {
  71. showDocumentation();
  72. fprintf(stderr, "Unable to perform %s (%d)\n", command, errno);
  73. close(file);
  74. return errno;
  75. }
  76. return close(file);
  77. }