static void* export_binary (int argc, char **argv, void *data) { Node *node = (Node *) data; char *filename = argc>=2?argv[1]:""; FILE *file; if (!strcmp (filename, "-")) file = stdout; else file = fopen (filename, "w"); if (!file) { cli_outfunf ("binary export, unable to open \"%s\"", filename); return node; } fwrite (&hnb_binary_header, 4, 1, file); fwrite (&hnb_binary_version, 4, 1, file); binary_export_nodes (file, node, 0); cli_outfunf ("binary export, wrote data to \"%s\"", filename); return node; }
static int search(int argc,char **argv,void *data){ Node *pos=(Node *)data; if(argc==2){ if(strcmp(argv[1],"-b")||strcmp(argv[1],"-f")){ pos=node_recursive_match( argv[1],pos); if (pos == NULL) { docmdf (pos, "status 'reached bottom of tree and \\'%s\\' not found'", argv[1]); return (int) data; } } } else if(argc>2){ if(!strcmp(argv[1],"-b")){ pos=node_backrecursive_match( argv[2],pos); if (pos == NULL) { docmdf (pos, "status 'reached top of tree and \\'%s\\' not found'", argv[2]); return (int) data; } } else if(!strcmp(argv[1],"-f")){ pos=node_recursive_match( argv[2],pos); if (pos == NULL) { docmdf (pos, "status 'reached bottom of tree and \\'%s\\' not found'", argv[2]); return (int) data; } } return (int)pos; } cli_outfunf("usage: %s [-b|-f] <string>",argv[0]); return (int)pos; }
static int addc (int argc,char **argv, void *data) { Node *pos = (Node *) data; Node *tnode; if(argc==1){ cli_outfunf("usage: %s <entry> [new subentry]",argv[0]); return 0; } tnode = node_exact_match (argv[1], pos); if (!tnode) { cli_outfun ("specified parent not found"); return (int) pos; } if (node_right (tnode)) { tnode=node_bottom(tnode); } else { tnode=node_insert_right(tnode); } if(argc==2) node_set (tnode, TEXT, ""); else node_set (tnode, TEXT, argv[2]); return (int) pos; }
static void* ui_context_cmd (int argc, char **argv, void *data) { if(argc<2){ cli_outfunf("usage: %s <contextname>",argv[0]); return data; } ui_current_scope = string2scope (argv[1]); return data; }
void *cmd_att_clear (int argc, char **argv, void *data) { Node *pos = (Node *) data; if(argc!=2){ cli_outfunf("usage: %s <attribute>",argv[0]); return pos; } node_unset (pos, argv[1]); return pos; }
static void* mem_cmd (int argc, char **argv, void *data) { Node *pos = (Node *) data; { int VmSize; FILE *file; file = fopen ("/proc/self/stat", "r"); if (!file) return pos; fscanf (file, "%*i %*s %*s %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %*i %i", &VmSize); fclose (file); cli_outfunf ("Memory used: %2.2fmb (%ib)", (float) (VmSize / 1024.0 / 1024.0), VmSize); } { int free, buffers, cached; FILE *file; file = fopen ("/proc/meminfo", "r"); if (!file) return pos; fscanf (file, "%*s %*s %*s %*s %*s %*s %*s %*i %*i %i %i %i", &free, &buffers, &cached); fclose (file); cli_outfunf ("Memory free: %2.2fmb (+buffers/cache: %2.2fmb)", (float) (free / 1024.0 / 1024.0), (float) ((free + buffers + cached) / 1024.0 / 1024.0)); } return pos; }
void *cmd_att_set (int argc, char **argv, void *data) { Node *pos = (Node *) data; if(argc!=3){ cli_outfunf("usage: %s <attribute> <value>",argv[0]); return pos; } node_set (pos, argv[1], argv[2]); return pos; }
void *cmd_att_list (int argc,char **argv, void *data) { Node_AttItem *att; Node *pos = (Node *) data; att = pos->attrib; while (att) { cli_outfunf ("%s: [%s]", att->name, att->data); att = att->next; } return pos; }
static void* vars (int argc, char **argv, void *data) { ItemT *titem = items; cli_outfunf ("all variables:"); while (titem) { #ifdef HIDE_NULL_HELP if(titem->usage) #endif if (is_variable (titem)) { if (titem->string) { cli_outfunf ("%15s [%s]\t- %s", titem->name, titem->string, titem->usage); } else if (titem->integer) { cli_outfunf ("%15s [%i]\t- %s", titem->name, *titem->integer, titem->usage); } else { cli_outfunf ("%s\tis a broken variable", titem->name); } } titem = titem->next; } cli_outfunf ("----------------"); cli_outfunf ("to change a variable: \"variablename newvalue\""); return data; }
void cli_add_help(char *name, char *helptext){ ItemT *titem = items; while (titem) { if (!strcmp (name, titem->name)){ free(titem->help); titem->help=strdup(helptext); return; } titem=titem->next; } cli_outfunf("libcli: attempted to add help for '%s' which is not registered",name); }
static void* help (int argc,char **argv, void *data) { if (argc == 1) { /* show all help */ ItemT *titem = items; cli_outfunf ("available commands:"); while (titem) { #ifdef HIDE_NULL_HELP if(titem->usage) #endif if (is_command (titem)) cli_outfunf ("%14s %s", titem->name, titem->usage); titem = titem->next; }; } else { /* show help for specified command */ ItemT *titem = items; cli_outfunf ("HELP for '%s'", argv[1] ); while (titem) { if (is_command (titem)) { if (!strcmp (argv[1], titem->name)) { cli_outfunf ("usage: %s %s", titem->name, titem->usage); if(titem->help[0]){ cli_outfun (""); cli_outfun(titem->help); } return data; } } titem = titem->next; } cli_outfunf ("unknown command '%s'", argv[1]); } return data; }
void cli_add_item (char *name, int *integer, char *string, void* (*func) (int argc,char **argv, void *data), char *usage) { ItemT *titem = items; while(titem){ if(!strcmp(titem->name,name)){ cli_outfunf ("libcli: attempted to add item '%s' more than once\n", name); return; } titem=titem->next; } titem=items; if (!titem) { titem = items = malloc (sizeof (ItemT)); titem->next = NULL; } else { ItemT *tmp; while (titem->next && ((strcmp ((titem->next)->name, name)) < 0)) { titem = titem->next; } tmp = titem->next; titem->next = malloc (sizeof (ItemT)); titem = titem->next; titem->next = tmp; } titem->name = strdup (name); titem->func = func; titem->integer = integer; titem->string = string; if(usage)titem->usage = strdup (usage); titem->help=strdup(""); if (strcmp (items->name, titem->name) > 0) { ItemT *tmp = items; ItemT *tmp_next = titem->next; items = titem; items->next = tmp; items->next->next = tmp_next; } }
void *cmd_att_get (int argc, char **argv, void *data) { Node *pos = (Node *) data; char *cdata; if(argc!=2){ cli_outfunf("usage: %s <attribute>",argv[0]); return pos; } cdata = node_get (pos, argv[1]); if (cdata) cli_outfun (cdata); return pos; }
static int add (int argc,char **argv, void *data) { Node *pos = (Node *) data; Node *tnode; if(argc==1){ cli_outfunf("usage: %s <new entry>",argv[0]); return 0; } if (argc==2) { cli_outfun ("empty node added\n"); } tnode = node_insert_down (node_bottom (pos)); node_set (tnode, TEXT, argv[1]); return (int) pos; }
static void* stats_cmd (int argc, char **argv, void *data) { int words = 0, leaves = 0, nodes = 0; Node *pos = (Node *) data; Node *node = node_root (pos); while (node) { nodes++; words += count_words ((unsigned char *)fixnullstring (node_get (node, TEXT))); if (!node_right (node)) leaves++; node = node_recurse (node); } cli_outfunf ("nodes:%i, leaves:%i words:%i", nodes, leaves, words); return pos; }
static int ls (int argc, char **argv, void *data) { Node *pos = (Node *) data; Node *tnode; int recurse = 0; int indicate_sub = 1; int startlevel; tnode = node_top (pos); startlevel = nodes_left (tnode); while (tnode) { if (recurse) { int j; for (j = nodes_left (tnode); j > startlevel; j--) { printf ("\t"); } } cli_outfunf( "%s %s %s",fixnullstring(node_get (tnode, TEXT)), indicate_sub? node_right(tnode)?"(..)":"" :"", tnode==pos?"<":"" ); if (recurse) { tnode = node_recurse (tnode); if (nodes_left (tnode) < startlevel) tnode = 0; } else { tnode = node_down (tnode); } } return (int) pos; }
static void* ui_bind_cmd (int argc, char **argv, void *data) { /* char context[40];*/ char *key; char *action; if(argc<3){ cli_outfunf("error in bindings %s %s %s %s\n",argv[0],argv[1],argv[2],argv[3]); return data; } key=argv[1]; action=argv[2]; if (string2action (action) != -1) { makebinding (ui_current_scope, string2keycode (key), string2action (action), action, ""); } else { makebinding (ui_current_scope, string2keycode (key), ui_action_command, "command", action); } return data; }
static int insert_cal(int argc, char **argv, void *data){ Node *pos=(void *)data; int year; int month; import_state_t ist; if( (argc!=3) || (atoi(argv[1])>12 )){ cli_outfunf("usage: %s <month> <year>", argv[0]); return (int)data; } month=atoi(argv[1]); year=atoi(argv[2]); if(year<2000){ /* nasty,.. 2yk like fix,.. but,.. it's just here */ year+=2000; } init_import(&ist, pos); { char tmpstr[40]; sprintf(tmpstr,"%i %s", year, mname[month]); import_node_text(&ist, 0, tmpstr); { struct tm tdata; tdata.tm_year = year - 1900; tdata.tm_mon = month - 1; tdata.tm_mday = 1; tdata.tm_hour = 0; tdata.tm_min = 0; tdata.tm_sec = 1; tdata.tm_isdst = -1; mktime (&tdata); while(tdata.tm_mon==month-1){ sprintf (tmpstr,"%s%c%02i\n", wday[tdata.tm_wday], (tdata.tm_wday==0 || tdata.tm_wday==6)?'_':' ', tdata.tm_mday); import_node_text(&ist, 1, tmpstr); /* I prefer not to plan on this level import_node_text(&ist, 2, "08:00"); import_node_text(&ist, 2, "09:00"); import_node_text(&ist, 2, "10:00"); import_node_text(&ist, 2, "11:00"); import_node_text(&ist, 2, "12:00"); import_node_text(&ist, 2, "13:00"); import_node_text(&ist, 2, "14:00"); import_node_text(&ist, 2, "15:00"); import_node_text(&ist, 2, "16:00"); import_node_text(&ist, 2, "17:00"); */ tdata.tm_mday++; mktime (&tdata); } } } return (int)pos; }
void *cli_docmd (char *commandline, void *data) { int largc=0; char **largv; ItemT *titem = items; void *ret=data; cli_calllevel++; if (cli_precmd) cli_precmd (commandline); if (!inited) { init_cli (); titem = items; inited = 1; } largv=argv_tokenize(commandline); if(largv)largc=argc_of_argv(largv); if((!largc) || largv[0][0]=='\0' ){ free(largv); return ret; } while (titem) { if (!strcmp (largv[0], titem->name)) { if (is_command (titem)) { ret=titem->func (largc,largv, data); if (cli_postcmd) cli_postcmd (commandline); cli_calllevel--; free(largv); return ret; } else if (is_variable (titem)) { if (largc==1) { if (titem->string) { cli_outfunf ("%s\t[%s]\t- %s\n", titem->name, titem->string, titem->usage); } else if (titem->integer) { cli_outfunf ("%s\t[%i]\t- %s\n", titem->name, *titem->integer, titem->usage); } else { cli_outfunf ("%s\tis a broken variable\n", titem->name); } } else { if (titem->integer) *titem->integer = atoi (largv[1]); if (titem->string) strcpy (titem->string, largv[1]); if (titem->func) ret=titem->func (largc,largv, data); } if (cli_postcmd) cli_postcmd (commandline); cli_calllevel--; free(largv); return ret; } } titem = titem->next; } if(cli_unknown) cli_unknown(1,&commandline,data); if (cli_postcmd) cli_postcmd (commandline); cli_calllevel--; free(largv); return ret; }
static void* import_binary (int argc, char **argv, void *data) { Node *node = (Node *) data; char *filename = argc==2?argv[1]:""; import_state_t ist; int moredata=1; FILE *file; file = fopen (filename, "r"); if (!file) { cli_outfunf ("binary import, unable to open \"%s\"", filename); return node; } {int header,version; fread(&header, sizeof(int), 1, file); fread(&version, sizeof(int), 1, file); if(header!=hnb_binary_header || version!=hnb_binary_version){ cli_outfunf("binary import, header mismatch"); } } init_import(&ist, node); while(moredata){ int attributes; int level; moredata=fread(&level, sizeof(int), 1, file); if(!moredata) break; fread(&attributes, sizeof(int),1,file); if(!moredata) break; if(level || attributes){ Node *temp_node=node_new(); while(attributes){ int len; char *att_name; char *att_data; fread(&len, sizeof(int),1,file); att_name=malloc(len+1); fread(att_name,1,len,file); att_name[len]='\0'; fread(&len, sizeof(int),1,file); att_data=malloc(len+1); fread(att_data,1,len,file); att_data[len]='\0'; node_set(temp_node, att_name, att_data); free(att_name); free(att_data); attributes--; } import_node(&ist,level,temp_node); temp_node=NULL; } } if(node_getflag(node,F_temp)) node=node_remove(node); cli_outfunf("binary import - imported \"%s\"",filename); return node; }
char *cli_complete (const char *commandline) { int matches = 0; char str_matches[4096]=""; strncpy (newcommand, commandline, 99); newcommand[99] = 0; if (commandline[0]) { matches = item_matches (newcommand); if (matches == 1) { ItemT *titem = items; while (titem) { if (!strncmp (newcommand, titem->name, strlen (newcommand))) { int pos; strcpy (newcommand, titem->name); pos = strlen (newcommand); newcommand[pos++] = ' '; newcommand[pos] = '\0'; break; } titem = titem->next; } } else if (matches > 1) { ItemT *titem = items; strcpy(str_matches,"matches: "); while (titem) { if (!strncmp (newcommand, titem->name, strlen (newcommand))) { strcat (str_matches,titem->name); strcat (str_matches," "); } titem = titem->next; } cli_outfun(str_matches); while (item_matches (newcommand) == matches) { ItemT *titem = items; while (titem) { int len = strlen (newcommand); if (!strncmp (newcommand, titem->name, len)) { strcpy (newcommand, titem->name); newcommand[len + 1] = '\0'; if(!strcmp(newcommand,titem->name)){ return newcommand; } break; } titem = titem->next; } } newcommand[strlen (newcommand) - 1] = '\0'; } else { cli_outfunf ("no match"); } } return newcommand; }
static void default_unknown_command (int argc,char **argv, void *data){ cli_outfunf ("unknown command '%s' type '?' to see allowed commands.\n",argv[0]); }