]>
git.saurik.com Git - apple/configd.git/blob - scutil.tproj/commands.c
   2  * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved. 
   8  * This file contains Original Code and/or Modifications of Original Code 
   9  * as defined in and that are subject to the Apple Public Source License 
  10  * Version 2.0 (the 'License'). You may not use this file except in 
  11  * compliance with the License. Please obtain a copy of the License at 
  12  * http://www.opensource.apple.com/apsl/ and read it before using this 
  15  * The Original Code and all software distributed under the License are 
  16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  20  * Please see the License for the specific language governing rights and 
  21  * limitations under the License. 
  23  * @APPLE_LICENSE_HEADER_END@ 
  27  * Modification History 
  29  * June 1, 2001                 Allan Nathanson <ajn@apple.com> 
  30  * - public API conversion 
  32  * November 9, 2000             Allan Nathanson <ajn@apple.com> 
  38 #include <sys/errno.h> 
  42 #include "dictionary.h" 
  48 #include "SCDynamicStoreInternal.h" 
  51 const cmdInfo commands
[] = { 
  52         /* cmd          minArgs maxArgs func                    group   ctype                   */ 
  55         { "help",       0,      0,      do_help
,                0,      0, 
  56                 " help                          : list available commands"                      }, 
  58         { "f.read",     1,      1,      do_readFile
,            0,      0, 
  59                 " f.read file                   : process commands from file"                   }, 
  61         /* local dictionary manipulation commands */ 
  63         { "d.init",     0,      0,      do_dictInit
,            1,      0, 
  64                 " d.init                        : initialize (empty) dictionary"                }, 
  66         { "d.show",     0,      0,      do_dictShow
,            1,      0, 
  67                 " d.show                        : show dictionary contents"                     }, 
  69         { "d.add",      2,      101,    do_dictSetKey
,          1,      0, 
  70                 " d.add key [*#?] val [v2 ...]  : add information to dictionary\n" 
  71                 "       (*=array, #=number, ?=boolean)"                         }, 
  73         { "d.remove",   1,      1,      do_dictRemoveKey
,       1,      0, 
  74                 " d.remove key                  : remove key from dictionary"                   }, 
  76         /* data store manipulation commands */ 
  78         { "open",       0,      0,      do_open
,                2,      0, 
  79                 " open                          : open a session with \"configd\""              }, 
  81         { "close",      0,      0,      do_close
,               2,      0, 
  82                 " close                         : close current \"configd\" session"            }, 
  84         { "lock",       0,      0,      do_lock
,                3,      1, 
  85                 " lock                          : secures write access to data store"           }, 
  87         { "unlock",     0,      0,      do_unlock
,              3,      1, 
  88                 " unlock                        : secures write access to data store"           }, 
  90         { "list",       0,      2,      do_list
,                4,      0, 
  91                 " list [pattern]                : list keys in data store"                      }, 
  93         { "add",        1,      2,      do_add
,                 4,      0, 
  94                 " add key [\"temporary\"]         : add key in data store w/current dict"               }, 
  96         { "get",        1,      1,      do_get
,                 4,      0, 
  97                 " get key                       : get dict from data store w/key"               }, 
  99         { "set",        1,      1,      do_set
,                 4,      0, 
 100                 " set key                       : set key in data store w/current dict"         }, 
 102         { "show",       1,      2,      do_show
,                4,      0, 
 103                 " show key [\"pattern\"]          : show values in data store w/key"            }, 
 105         { "remove",     1,      1,      do_remove
,              4,      0, 
 106                 " remove key                    : remove key from data store"                   }, 
 108         { "notify",     1,      1,      do_notify
,              4,      0, 
 109                 " notify key                    : notify key in data store"                     }, 
 111         { "touch",      1,      1,      do_touch
,               4,      1, 
 112                 " touch key                     : touch key in data store"                      }, 
 114         { "n.list",     0,      1,      do_notify_list
,         5,      0, 
 115                 " n.list [\"pattern\"]            : list notification keys"                     }, 
 117         { "n.add",      1,      2,      do_notify_add
,          5,      0, 
 118                 " n.add key [\"pattern\"]         : add notification key"                               }, 
 120         { "n.remove",   1,      2,      do_notify_remove
,       5,      0, 
 121                 " n.remove key [\"pattern\"]      : remove notification key"                    }, 
 123         { "n.changes",  0,      0,      do_notify_changes
,      5,      0, 
 124                 " n.changes                     : list changed keys"                            }, 
 126         { "n.watch",    0,      1,      do_notify_watch
,        5,      0, 
 127                 " n.watch [verbose]             : watch for changes"                            }, 
 129         { "n.wait",     0,      0,      do_notify_wait
,         5,      2, 
 130                 " n.wait                        : wait for changes"                             }, 
 132         { "n.callback", 0,      1,      do_notify_callback
,     5,      2, 
 133                 " n.callback [\"verbose\"]        : watch for changes"                          }, 
 135         { "n.signal",   1,      2,      do_notify_signal
,       5,      2, 
 136                 " n.signal sig [pid]            : signal changes"                               }, 
 138         { "n.file",     0,      1,      do_notify_file
,         5,      2, 
 139                 " n.file [identifier]           : watch for changes via file"                   }, 
 141         { "n.cancel",   0,      1,      do_notify_cancel
,       5,      0, 
 142                 " n.cancel                      : cancel notification requests"                 }, 
 144         { "snapshot",   0,      0,      do_snapshot
,            9,      2, 
 145                 " snapshot                      : save snapshot of store and session data"      }, 
 148 const int nCommands 
= (sizeof(commands
)/sizeof(cmdInfo
)); 
 150 Boolean enablePrivateAPI        
= FALSE
; 
 154 do_command(int argc
, char **argv
) 
 159         for (i 
= 0; i 
< nCommands
; i
++) { 
 160                 if ((commands
[i
].ctype 
> 1) && !enablePrivateAPI
)  { 
 161                         continue;       /* if "private" API and access has not been enabled */ 
 164                 if (strcasecmp(cmd
, commands
[i
].cmd
) == 0) { 
 167                         if (argc 
< commands
[i
].minArgs
) { 
 168                                 SCPrint(TRUE
, stdout
, CFSTR("%s: too few arguments\n"), cmd
); 
 170                         } else if (argc 
> commands
[i
].maxArgs
) { 
 171                                 SCPrint(TRUE
, stdout
, CFSTR("%s: too many arguments\n"), cmd
); 
 174                         commands
[i
].func(argc
, argv
); 
 179         SCPrint(TRUE
, stdout
, CFSTR("%s: unknown, type \"help\" for command info\n"), cmd
); 
 185 do_help(int argc
, char **argv
) 
 187         int     g 
= -1;         /* current group */ 
 190         SCPrint(TRUE
, stdout
, CFSTR("\nAvailable commands:\n")); 
 191         for (i 
= 0; i 
< nCommands
; i
++) { 
 192                 if ((commands
[i
].ctype 
> 0) && !enablePrivateAPI
)  { 
 193                         continue;       /* if "private" API and access has not been enabled */ 
 196                 /* check if this is a new command group */ 
 197                 if (g 
!= commands
[i
].group
) { 
 198                         SCPrint(TRUE
, stdout
, CFSTR("\n")); 
 199                         g 
= commands
[i
].group
; 
 202                 /* display the command */ 
 203                 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), commands
[i
].usage
); 
 205         SCPrint(TRUE
, stdout
, CFSTR("\n")); 
 212 do_readFile(int argc
, char **argv
) 
 216         /* allocate command input stream */ 
 217         src 
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0); 
 219         src
->fp 
= fopen(argv
[0], "r"); 
 221         if (src
->fp 
== NULL
) { 
 222                 SCPrint(TRUE
, stdout
, CFSTR("f.read: could not open file (%s).\n"), strerror(errno
)); 
 223                 CFAllocatorDeallocate(NULL
, src
); 
 227         /* open file, increase nesting level */ 
 228         SCPrint(TRUE
, stdout
, CFSTR("f.read: reading file (%s).\n"), argv
[0]); 
 231         while (process_line(src
) == TRUE
) { 
 232                /* debug information, diagnostics */ 
 233                 __showMachPortStatus(); 
 236         (void)fclose(src
->fp
); 
 237         CFAllocatorDeallocate(NULL
, src
);