PageRenderTime 38ms CodeModel.GetById 23ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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