odbedit.c File Reference

Go to the source code of this file.

Data Structures

struct  PRINT_INFO

Defines

#define PI_LONG   (1<<0)
#define PI_RECURSIVE   (1<<1)
#define PI_VALUE   (1<<2)
#define PI_HEX   (1<<3)
#define PI_PAUSE   (1<<4)

Functions

INT cmd_edit (char *prompt, char *cmd, INT(*dir)(char *, INT *), INT(*idle)())
INT thread (void *p)
void key_update (HNDLE hDB, HNDLE hkey, void *info)
void print_help (char *command)
void process_message (HNDLE hBuf, HNDLE id, EVENT_HEADER *pheader, void *message)
int print_message (const char *msg)
BOOL match (char *pat, char *str)
BOOL check_abort (int flags, int l)
INT print_key (HNDLE hDB, HNDLE hKey, KEY *pkey, INT level, void *info)
void set_key (HNDLE hDB, HNDLE hKey, int index1, int index2, char *value)
void scan_tree (HNDLE hDB, HNDLE hKey, INT *total_size_key, INT *total_size_data, INT level, INT flags)
INT cmd_dir (char *line, INT *cursor)
INT search_key (HNDLE hDB, HNDLE hKey, KEY *key, INT level, void *info)
void del_tree (HNDLE hDB, HNDLE hKey, INT level)
void create_experim_h (HNDLE hDB, char *analyzer_name)
INT cmd_idle ()
void compose_name (char *pwd, char *name, char *full_name)
void assemble_prompt (char *prompt, int psize, char *host_name, char *exp_name, char *pwd)
int command_loop (char *host_name, char *exp_name, char *cmd, char *start_dir)
void ctrlc_odbedit (INT i)
int main (int argc, char *argv[])

Variables

BOOL need_redraw
BOOL in_cmd_edit
char pwd [256]
BOOL cmd_mode
static char data [50000]
MUTEX_T * tm
BOOL key_modified
int ls_line
int ls_abort


Define Documentation

#define PI_HEX   (1<<3)

Definition at line 39 of file odbedit.c.

Referenced by command_loop(), and print_key().

#define PI_LONG   (1<<0)

Definition at line 36 of file odbedit.c.

Referenced by command_loop(), and print_key().

#define PI_PAUSE   (1<<4)

Definition at line 40 of file odbedit.c.

Referenced by check_abort(), and command_loop().

#define PI_RECURSIVE   (1<<1)

Definition at line 37 of file odbedit.c.

Referenced by command_loop().

#define PI_VALUE   (1<<2)

Definition at line 38 of file odbedit.c.

Referenced by command_loop(), and print_key().


Function Documentation

void assemble_prompt ( char *  prompt,
int  psize,
char *  host_name,
char *  exp_name,
char *  pwd 
)

Definition at line 1196 of file odbedit.c.

Referenced by command_loop().

01197 {
01198    HNDLE hDB;
01199    char mask[256], str[32];
01200    int state, size;
01201    char *pp, *pm, *pc;
01202    time_t now;
01203 
01204    char *state_char[] = { "U", "S", "P", "R" };
01205    char *state_str[] = { "Unknown", "Stopped", "Paused", "Running" };
01206 
01207    cm_get_experiment_database(&hDB, NULL);
01208 
01209    size = sizeof(mask);
01210    strcpy(mask, "[%h:%e:%s]%p>");
01211    db_get_value(hDB, 0, "/System/Prompt", mask, &size, TID_STRING, TRUE);
01212 
01213    state = STATE_STOPPED;
01214    size = sizeof(state);
01215    db_get_value(hDB, 0, "/Runinfo/State", &state, &size, TID_INT, TRUE);
01216    if (state > STATE_RUNNING)
01217       state = 0;
01218 
01219    pm = mask;
01220    pp = prompt;
01221    memset(prompt, 0, psize);
01222    do {
01223       if (*pm != '%') {
01224          *pp++ = *pm++;
01225       } else {
01226          switch (*++pm) {
01227          case 't':
01228             time(&now);
01229             strcpy(str, ctime(&now));
01230             str[19] = 0;
01231             strcpy(pp, str + 11);
01232             break;
01233          case 'h':
01234             if (host_name[0])
01235                strcat(pp, host_name);
01236             else
01237                strcat(pp, "local");
01238             break;
01239          case 'e':
01240             if (exp_name[0])
01241                strcat(pp, exp_name);
01242             else
01243                strcat(pp, "Default");
01244             break;
01245          case 's':
01246             strcat(pp, state_char[state]);
01247             break;
01248          case 'S':
01249             strcat(pp, state_str[state]);
01250             break;
01251          case 'P':
01252             strcat(pp, pwd);
01253             break;
01254          case 'p':
01255             pc = pwd + strlen(pwd) - 1;
01256             while (*pc != '/' && pc != pwd)
01257                pc--;
01258             if (pc == pwd)
01259                strcat(pp, pwd);
01260             else
01261                strcat(pp, pc + 1);
01262             break;
01263          }
01264          pm++;
01265          pp += strlen(pp);
01266       }
01267    } while (*pm);
01268 }

BOOL check_abort ( int  flags,
int  l 
)

Definition at line 217 of file odbedit.c.

Referenced by print_key().

00218 {
00219    int c;
00220 
00221    if ((flags & PI_PAUSE) && (l % 24) == 23) {
00222       printf("Press any key to continue or q to quit ");
00223       fflush(stdout);
00224       do {
00225          c = ss_getchar(0);
00226          if (c == 'q') {
00227             printf("\n");
00228             ls_abort = TRUE;
00229             return TRUE;
00230          } else if (c > 0) {
00231             printf("\r                                        \r");
00232             return FALSE;
00233          }
00234 
00235          cm_yield(100);
00236 
00237       } while (!cm_is_ctrlc_pressed());
00238    }
00239 
00240    if (cm_is_ctrlc_pressed()) {
00241       ls_abort = TRUE;
00242       return TRUE;
00243    }
00244 
00245    return FALSE;
00246 }

INT cmd_dir ( char *  line,
INT cursor 
)

Definition at line 651 of file odbedit.c.

Referenced by command_loop().

00652 {
00653    KEY key;
00654    HNDLE hDB, hKey, hSubkey;
00655    INT i, j, match, size;
00656    char str[256], *pc, partial[256], last_match[256], head[256], tail[256], key_name[256],
00657        c;
00658    char test_key[256];
00659    BOOL blanks, mismatch;
00660 
00661    cm_get_experiment_database(&hDB, NULL);
00662 
00663    /* remember tail for later */
00664    strcpy(head, line);
00665    strcpy(tail, line + *cursor);
00666    line[*cursor] = 0;
00667 
00668    /* search beginning of key */
00669    pc = head;
00670    do {
00671       while (*pc && *pc != ' ')
00672          pc++;
00673       while (*pc && *pc == ' ')
00674          pc++;
00675    } while (*pc == '-');        /* skip flags */
00676 
00677    if (*pc) {
00678       strcpy(key_name, pc);
00679       *pc = 0;                  /* end of head */
00680    } else
00681       key_name[0] = 0;
00682 
00683    /* check if key exists (for "set <key>" completion) */
00684    if (strncmp(head, "set", 3) == 0 && strlen(key_name) > 0) {
00685       if (key_name[0] == '"')
00686          strcpy(str, key_name + 1);
00687       else
00688          strcpy(str, key_name);
00689       if (key_name[0] != '/') {
00690          strcpy(test_key, pwd);
00691          if (str[strlen(test_key) - 1] != '/')
00692             strcat(test_key, "/");
00693          strcat(test_key, str);
00694       } else
00695          strcpy(test_key, str);
00696 
00697       pc = test_key + strlen(test_key) - 1;
00698       while (pc > test_key && (*pc == ' ' || *pc == '"'))
00699          *pc-- = 0;
00700       if (db_find_key(hDB, 0, test_key, &hSubkey) == DB_SUCCESS) {
00701          if (strlen(key_name) > 0 && key_name[strlen(key_name)-1] != ' ')
00702             strcat(key_name, " ");
00703 
00704          /* retrieve key data */
00705          db_get_link(hDB, hSubkey, &key);
00706          size = sizeof(data);
00707          db_get_link_data(hDB, hSubkey, data, &size, key.type);
00708          
00709          if (key.type == TID_STRING || key.type == TID_LINK)
00710             sprintf(str, "\"%s\"", data);
00711          else
00712             db_sprintf(str, data, size, 0, key.type);
00713 
00714          strcpy(line, head);
00715          strcat(line, key_name);
00716          strcat(line, str);
00717          *cursor = strlen(line);
00718          strcat(line, tail);
00719          return TRUE;
00720       }
00721    }
00722 
00723    /* combine pwd and key_name */
00724    pc = key_name;
00725    if (*pc == '"')
00726       pc++;
00727 
00728    if (*pc != '/') {
00729       strcpy(str, pwd);
00730       if (str[strlen(str) - 1] != '/')
00731          strcat(str, "/");
00732       strcat(str, pc);
00733    } else
00734       strcpy(str, pc);
00735 
00736    /* split key_name into known and new directory */
00737    for (pc = str + strlen(str) - 1; pc > str && *pc != '/'; pc--);
00738 
00739    if (*pc == '/') {
00740       *pc = 0;
00741       strcpy(partial, pc + 1);
00742    } else
00743       strcpy(partial, str);
00744 
00745    db_find_link(hDB, 0, str, &hKey);
00746    for (i = 0, match = 0;; i++) {
00747       db_enum_link(hDB, hKey, i, &hSubkey);
00748 
00749       if (!hSubkey)
00750          break;
00751 
00752       db_get_link(hDB, hSubkey, &key);
00753       strcpy(str, key.name);
00754 
00755       str[strlen(partial)] = 0;
00756 
00757       if (equal_ustring(str, partial))
00758          match++;
00759    }
00760 
00761    if (match != 1)
00762       printf("\r\n");
00763 
00764    for (i = 0;; i++) {
00765       db_enum_link(hDB, hKey, i, &hSubkey);
00766 
00767       if (!hSubkey)
00768          break;
00769 
00770       db_get_link(hDB, hSubkey, &key);
00771       strcpy(str, key.name);
00772 
00773       str[strlen(partial)] = 0;
00774 
00775       if (equal_ustring(str, partial)) {
00776          if (match == 1) {
00777             /* search split point */
00778             pc = key_name;
00779             if (strlen(key_name) > 0)
00780                for (pc = key_name + strlen(key_name) - 1; pc > key_name && *pc != '/';
00781                     pc--);
00782             if (*pc == '/')
00783                pc++;
00784 
00785             strcpy(pc, key.name);
00786             if (key.type == TID_KEY)
00787                strcat(pc, "/");
00788 
00789             /* insert '"' if blanks in name */
00790             if (strchr(key.name, ' ')) {
00791                if (key_name[0] != '"') {
00792                   for (i = strlen(key_name); i >= 0; i--)
00793                      key_name[i + 1] = key_name[i];
00794 
00795                   key_name[0] = '"';
00796                }
00797                if (key.type != TID_KEY)
00798                   strcat(key_name, "\"");
00799             }
00800 
00801             if (key.type != TID_KEY) {
00802                if (key_name[0] == '"' && key_name[strlen(key_name) - 1] != '"')
00803                   strcat(pc, "\" ");
00804                else
00805                   strcat(pc, " ");
00806             }
00807 
00808             strcpy(line, head);
00809             strcat(line, key_name);
00810             *cursor = strlen(line);
00811             strcat(line, tail);
00812             return TRUE;
00813          }
00814       }
00815       if (match == 0 || (match > 1 && equal_ustring(str, partial)))
00816          printf("%s\r\n", key.name);
00817    }
00818 
00819    if (match > 1 && key_name[0]) {
00820       blanks = FALSE;
00821 
00822       for (j = strlen(partial);; j++) {
00823          for (i = 0, c = 1, mismatch = FALSE;; i++) {
00824             db_enum_link(hDB, hKey, i, &hSubkey);
00825 
00826             if (!hSubkey)
00827                break;
00828 
00829             db_get_link(hDB, hSubkey, &key);
00830             strcpy(str, key.name);
00831 
00832             str[strlen(partial)] = 0;
00833 
00834             if (strchr(key.name, ' '))
00835                blanks = TRUE;
00836 
00837             if (equal_ustring(str, partial)) {
00838                strcpy(last_match, key.name);
00839                if (c == 1)
00840                   c = toupper(key.name[j]);
00841                else if (c != toupper(key.name[j])) {
00842                   mismatch = TRUE;
00843                   break;
00844                }
00845             }
00846          }
00847 
00848          if (mismatch || last_match[j] == 0)
00849             break;
00850       }
00851 
00852       /* search split point */
00853       for (pc = key_name + strlen(key_name) - 1; pc > key_name && *pc != '/'; pc--);
00854       if (*pc == '/')
00855          pc++;
00856 
00857       for (i = 0; i < j; i++)
00858          pc[i] = last_match[i];
00859       pc[i] = 0;
00860 
00861       /* insert '"' if blanks in name */
00862       if (blanks) {
00863          if (key_name[0] != '"') {
00864             for (i = strlen(key_name); i >= 0; i--)
00865                key_name[i + 1] = key_name[i];
00866 
00867             key_name[0] = '"';
00868          }
00869       }
00870 
00871       strcpy(line, head);
00872       strcat(line, key_name);
00873       *cursor = strlen(line);
00874       strcat(line, tail);
00875       return TRUE;
00876    }
00877 
00878    /* beep if not found */
00879    printf("\007");
00880 
00881    strcpy(line, head);
00882    strcat(line, key_name);
00883    *cursor = strlen(line);
00884    strcat(line, tail);
00885    return FALSE;
00886 }

INT cmd_edit ( char *  prompt,
char *  cmd,
INT(*)(char *, INT *)  dir,
INT(*)()  idle 
)

Definition at line 38 of file cmdedit.c.

Referenced by command_loop().

00039 {
00040    char line[LINE_LENGTH];
00041    INT i, j, k, c, hi;
00042    INT status;
00043    DWORD last_time = 0;
00044    BOOL escape_flag = 0;
00045 
00046    if (ss_getchar(0) == -1) {
00047       /* normal input if ss_getchar not supported */
00048       fputs(prompt, stdout);
00049       ss_gets(cmd, 256);
00050       return strlen(cmd);
00051    }
00052 
00053    fputs(prompt, stdout);
00054    fflush(stdout);
00055 
00056    hi = his_index;
00057    memset(line, 0, LINE_LENGTH);
00058    memset(history[hi], 0, LINE_LENGTH);
00059    strcpy(line, cmd);
00060    fputs(line, stdout);
00061    i = strlen(cmd);
00062    fflush(stdout);
00063 
00064    do {
00065       c = ss_getchar(0);
00066 
00067       if (c == 27)
00068          escape_flag = TRUE;
00069 
00070       if (c >= ' ' && c < CH_EXT && escape_flag) {
00071          escape_flag = FALSE;
00072          if (c == 'p')
00073             c = 6;
00074       }
00075 
00076       /* normal input */
00077       if (c >= ' ' && c < CH_EXT) {
00078          if (strlen(line) < LINE_LENGTH - 1) {
00079             for (j = strlen(line); j >= i; j--)
00080                line[j + 1] = line[j];
00081             if (i < LINE_LENGTH - 1) {
00082                line[i++] = c;
00083                fputc(c, stdout);
00084             }
00085             for (j = i; j < (INT) strlen(line); j++)
00086                fputc(line[j], stdout);
00087             for (j = i; j < (INT) strlen(line); j++)
00088                fputc('\b', stdout);
00089          }
00090       }
00091 
00092       /* BS */
00093       if (c == CH_BS && i > 0) {
00094          i--;
00095          fputc('\b', stdout);
00096          for (j = i; j <= (INT) strlen(line); j++) {
00097             line[j] = line[j + 1];
00098             if (line[j])
00099                fputc(line[j], stdout);
00100             else
00101                fputc(' ', stdout);
00102          }
00103          for (k = 0; k < j - i; k++)
00104             fputc('\b', stdout);
00105       }
00106 
00107       /* DELETE/Ctrl-D */
00108       if (c == CH_DELETE || c == 4) {
00109          for (j = i; j <= (INT) strlen(line); j++) {
00110             line[j] = line[j + 1];
00111             if (line[j])
00112                fputc(line[j], stdout);
00113             else
00114                fputc(' ', stdout);
00115          }
00116          for (k = 0; k < j - i; k++)
00117             fputc('\b', stdout);
00118       }
00119 
00120       /* Erase line: CTRL-W, CTRL-U */
00121       if (c == 23 || c == 21) {
00122          i = strlen(line);
00123          memset(line, 0, sizeof(line));
00124          printf("\r%s", prompt);
00125          for (j = 0; j < i; j++)
00126             fputc(' ', stdout);
00127          for (j = 0; j < i; j++)
00128             fputc('\b', stdout);
00129          i = 0;
00130       }
00131 
00132       /* Erase line from cursor: CTRL-K */
00133       if (c == 11) {
00134          for (j = i; j < (INT) strlen(line); j++)
00135             fputc(' ', stdout);
00136          for (j = i; j < (INT) strlen(line); j++)
00137             fputc('\b', stdout);
00138          for (j = strlen(line); j >= i; j--)
00139             line[j] = 0;
00140       }
00141 
00142       /* left arrow, CTRL-B */
00143       if ((c == CH_LEFT || c == 2) && i > 0) {
00144          i--;
00145          fputc('\b', stdout);
00146       }
00147 
00148       /* right arrow, CTRL-F */
00149       if ((c == CH_RIGHT || c == 6) && i < (INT) strlen(line))
00150          fputc(line[i++], stdout);
00151 
00152       /* HOME, CTRL-A */
00153       if ((c == CH_HOME || c == 1) && i > 0) {
00154          for (j = 0; j < i; j++)
00155             fputc('\b', stdout);
00156          i = 0;
00157       }
00158 
00159       /* END, CTRL-E */
00160       if ((c == CH_END || c == 5) && i < (INT) strlen(line)) {
00161          for (j = i; j < (INT) strlen(line); j++)
00162             fputc(line[i++], stdout);
00163          i = strlen(line);
00164       }
00165 
00166       /* up arrow / CTRL-P */
00167       if (c == CH_UP || c == 16) {
00168          if (history[(hi + MAX_HISTORY - 1) % MAX_HISTORY][0]) {
00169             hi = (hi + MAX_HISTORY - 1) % MAX_HISTORY;
00170             i = strlen(line);
00171             fputc('\r', stdout);
00172             fputs(prompt, stdout);
00173             for (j = 0; j < i; j++)
00174                fputc(' ', stdout);
00175             for (j = 0; j < i; j++)
00176                fputc('\b', stdout);
00177             memcpy(line, history[hi], 256);
00178             i = strlen(line);
00179             for (j = 0; j < i; j++)
00180                fputc(line[j], stdout);
00181          }
00182       }
00183 
00184       /* down arrow / CTRL-N */
00185       if (c == CH_DOWN || c == 14) {
00186          if (history[hi][0]) {
00187             hi = (hi + 1) % MAX_HISTORY;
00188             i = strlen(line);
00189             fputc('\r', stdout);
00190             fputs(prompt, stdout);
00191             for (j = 0; j < i; j++)
00192                fputc(' ', stdout);
00193             for (j = 0; j < i; j++)
00194                fputc('\b', stdout);
00195             memcpy(line, history[hi], 256);
00196             i = strlen(line);
00197             for (j = 0; j < i; j++)
00198                fputc(line[j], stdout);
00199          }
00200       }
00201 
00202       /* CTRL-F */
00203       if (c == 6) {
00204          for (j = (hi + MAX_HISTORY - 1) % MAX_HISTORY; j != hi;
00205               j = (j + MAX_HISTORY - 1) % MAX_HISTORY)
00206             if (history[j][0] && strncmp(line, history[j], i) == 0) {
00207                memcpy(line, history[j], 256);
00208                fputs(line + i, stdout);
00209                i = strlen(line);
00210                break;
00211             }
00212          if (j == hi)
00213             fputc(7, stdout);
00214       }
00215 
00216       /* tab */
00217       if (c == 9 && dir != NULL) {
00218          status = dir(line, &i);
00219 
00220          /* redraw line */
00221          fputc('\r', stdout);
00222          fputs(prompt, stdout);
00223          fputs(line, stdout);
00224 
00225          for (j = 0; j < (INT) strlen(line) - i; j++)
00226             fputc('\b', stdout);
00227       }
00228 
00229       if (c != 0) {
00230          last_time = ss_millitime();
00231          fflush(stdout);
00232       }
00233 
00234       if ((ss_millitime() - last_time > 300) && idle != NULL) {
00235          status = idle();
00236 
00237          if (status) {
00238             fputc('\r', stdout);
00239             fputs(prompt, stdout);
00240             fputs(line, stdout);
00241 
00242             for (j = 0; j < (INT) strlen(line) - i; j++)
00243                fputc('\b', stdout);
00244 
00245             fflush(stdout);
00246          }
00247       }
00248 
00249    } while (c != CH_CR);
00250 
00251    strcpy(cmd, line);
00252 
00253    if (dir != NULL)
00254       if (strcmp(cmd, history[(his_index + MAX_HISTORY - 1) % MAX_HISTORY]) != 0 &&
00255           cmd[0]) {
00256          strcpy(history[his_index], cmd);
00257          his_index = (his_index + 1) % MAX_HISTORY;
00258       }
00259 
00260    /* reset terminal */
00261    ss_getchar(1);
00262 
00263    fputc('\n', stdout);
00264 
00265    return strlen(line);
00266 }

INT cmd_idle (  ) 

Definition at line 1158 of file odbedit.c.

Referenced by command_loop().

01159 {
01160    INT status;
01161 
01162    need_redraw = FALSE;
01163 
01164    status = cm_yield(100);
01165 
01166    /* abort if server connection is broken */
01167    if (status == SS_ABORT || status == RPC_SHUTDOWN) {
01168       if (status == SS_ABORT)
01169          printf("Server connection broken.\n");
01170 
01171       cm_disconnect_experiment();
01172       exit(0);
01173    }
01174 
01175    /* check for broken client connections */
01176    rpc_client_check();
01177 
01178    return need_redraw;
01179 }

int command_loop ( char *  host_name,
char *  exp_name,
char *  cmd,
char *  start_dir 
)

Definition at line 1272 of file odbedit.c.

Referenced by main().

01273 {
01274    INT status = 0, i, j, k, state, size, old_run_number, new_run_number, channel;
01275    char line[2000], prompt[256];
01276    char param[10][2000];
01277    char str[2000], str2[80], old_dir[256], cwd[256], name[256], *pc, data_str[256];
01278    char old_password[32], new_password[32];
01279    INT nparam, flags, index1, index2, debug_flag;
01280    WORD mode;
01281    HNDLE hDB, hKey, hKeyClient, hSubkey, hRootKey;
01282    KEY key;
01283    char user_name[80] = "";
01284    FILE *cmd_file = NULL;
01285    DWORD last_msg_time = 0;
01286    char message[2000], client_name[256], *p;
01287    INT n1, n2, msg_type;
01288    PRINT_INFO print_info;
01289 
01290    cm_get_experiment_database(&hDB, &hKeyClient);
01291 
01292    /* command loop */
01293    if (start_dir[0])
01294       strcpy(pwd, start_dir);
01295    else
01296       strcpy(pwd, "/");
01297 
01298    /* check if dir exists */
01299    if (db_find_key(hDB, 0, pwd, &hKey) != DB_SUCCESS) {
01300       printf("Directory \"%s\" not found.\n", pwd);
01301       return -1;
01302    }
01303 
01304    /* open command file */
01305    if (cmd[0] == '@') {
01306       cmd_file = fopen(cmd + 1, "r");
01307       if (cmd_file == NULL) {
01308          printf("Command file %s not found.\n", cmd + 1);
01309          return -1;
01310       }
01311    }
01312 
01313    do {
01314       /* print prompt */
01315       if (!cmd_mode) {
01316          assemble_prompt(prompt, sizeof(prompt), host_name, exp_name, pwd);
01317 
01318          in_cmd_edit = TRUE;
01319          line[0] = 0;
01320          cmd_edit(prompt, line, cmd_dir, cmd_idle);
01321          in_cmd_edit = FALSE;
01322       } else if (cmd[0] != '@')
01323          strlcpy(line, cmd, sizeof(line));
01324       else {
01325          memset(line, 0, sizeof(line));
01326          fgets(line, sizeof(line), cmd_file);
01327 
01328          if (line[0] == 0)
01329             break;
01330 
01331          /* cut off CR */
01332          while (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
01333             line[strlen(line) - 1] = 0;
01334 
01335          if (line[0] == 0)
01336             continue;
01337       }
01338 
01339       /* analyze line */
01340       nparam = 0;
01341       pc = line;
01342       while (*pc == ' ')
01343          pc++;
01344 
01345       memset(param, 0, sizeof(param));
01346       do {
01347          if (*pc == '"') {
01348             pc++;
01349             for (i = 0; *pc && *pc != '"' && i<sizeof(param[0])-1; i++)
01350                param[nparam][i] = *pc++;
01351             if (*pc)
01352                pc++;
01353          } else if (*pc == '\'') {
01354             pc++;
01355             for (i = 0; *pc && *pc != '\'' && i<sizeof(param[0])-1; i++)
01356                param[nparam][i] = *pc++;
01357             if (*pc)
01358                pc++;
01359          } else if (*pc == '`') {
01360             pc++;
01361             for (i = 0; *pc && *pc != '`' && i<sizeof(param[0])-1; i++)
01362                param[nparam][i] = *pc++;
01363             if (*pc)
01364                pc++;
01365          } else
01366             for (i = 0; *pc && *pc != ' ' && i<sizeof(param[0])-1; i++)
01367                param[nparam][i] = *pc++;
01368          param[nparam][i] = 0;
01369          while (*pc == ' ')
01370             pc++;
01371          nparam++;
01372       } while (*pc);
01373 
01374       /* help */
01375       if ((param[0][0] == 'h' && param[0][1] == 'e') || param[0][0] == '?')
01376          print_help(param[1]);
01377 
01378       /* ls */
01379       else if ((param[0][0] == 'l' && param[0][1] == 's') ||
01380                (param[0][0] == 'd' && param[0][1] == 'i')) {
01381          db_find_key(hDB, 0, pwd, &hKey);
01382          print_info.flags = 0;
01383          print_info.pattern[0] = 0;
01384          ls_line = 0;
01385          ls_abort = FALSE;
01386 
01387          /* parse options */
01388          for (i = 1; i < 4; i++)
01389             if (param[i][0] == '-') {
01390                for (j = 1; param[i][j] != ' ' && param[i][j]; j++) {
01391                   if (param[i][j] == 'l')
01392                      print_info.flags |= PI_LONG;
01393                   if (param[i][j] == 'r')
01394                      print_info.flags |= PI_RECURSIVE;
01395                   if (param[i][j] == 'v')
01396                      print_info.flags |= PI_VALUE;
01397                   if (param[i][j] == 'h')
01398                      print_info.flags |= PI_HEX;
01399                   if (param[i][j] == 'p')
01400                      print_info.flags |= PI_PAUSE;
01401                }
01402             }
01403 
01404          for (i = 1; param[i][0] == '-'; i++);
01405 
01406          /* check if parameter contains array index */
01407          print_info.index = -1;
01408          if (strchr(param[i], '[') && strchr(param[i], ']')) {
01409             for (p = strchr(param[i], '[') + 1; *p && *p != ']'; p++)
01410                if (!isdigit(*p))
01411                   break;
01412 
01413             if (*p && *p == ']') {
01414                print_info.index = atoi(strchr(param[i], '[') + 1);
01415                *strchr(param[i], '[') = 0;
01416             }
01417          }
01418 
01419          if (param[i][0]) {
01420             if (strpbrk(param[i], "*?") != NULL) {
01421                /* if parameter contains wildcards, set pattern */
01422                strcpy(print_info.pattern, param[i]);
01423             } else {
01424                if (param[i][0] == '/')
01425                   status = db_find_link(hDB, 0, param[i], &hKey);
01426                else
01427                   status = db_find_link(hDB, hKey, param[i], &hKey);
01428 
01429                if (status == DB_NO_KEY)
01430                   printf("key %s not found\n", param[i]);
01431 
01432                if (status == DB_INVALID_LINK)
01433                   printf("link %s points to invalid location\n", param[i]);
01434             }
01435          }
01436 
01437          if (hKey) {
01438             if ((print_info.flags & PI_LONG) && (print_info.flags & PI_VALUE) == 0) {
01439                printf
01440                    ("Key name                        Type    #Val  Size  Last Opn Mode Value\n");
01441                printf
01442                    ("---------------------------------------------------------------------------\n");
01443             }
01444 
01445             if (print_info.flags & PI_RECURSIVE) {
01446                db_scan_tree(hDB, hKey, 0, print_key, &print_info);
01447                if (cm_is_ctrlc_pressed())
01448                   cm_ack_ctrlc_pressed();
01449             } else {
01450                db_get_link(hDB, hKey, &key);
01451                if (key.type != TID_KEY)
01452                   print_key(hDB, hKey, &key, 0, &print_info);
01453                else
01454                   for (i = 0;; i++) {
01455                      if (cm_is_ctrlc_pressed()) {
01456                         cm_ack_ctrlc_pressed();
01457                         break;
01458                      }
01459 
01460                      db_enum_link(hDB, hKey, i, &hSubkey);
01461 
01462                      if (!hSubkey)
01463                         break;
01464 
01465                      db_get_link(hDB, hSubkey, &key);
01466                      status = print_key(hDB, hSubkey, &key, 0, &print_info);
01467                      if (status == 0)
01468                         break;
01469                   }
01470             }
01471          }
01472       }
01473 
01474       /* cd */
01475       else if (param[0][0] == 'c' && param[0][1] == 'd') {
01476          compose_name(pwd, param[1], str);
01477 
01478          status = db_find_key(hDB, 0, str, &hKey);
01479 
01480          if (strcmp(str, "/") == 0)
01481             strcpy(pwd, str);
01482          else if (status == DB_SUCCESS) {
01483             db_get_key(hDB, hKey, &key);
01484             if (key.type != TID_KEY)
01485                printf("key has no subkeys\n");
01486             else
01487                db_get_path(hDB, hKey, pwd, 256);
01488          } else
01489             printf("key not found\n");
01490       }
01491 
01492       /* pwd */
01493       else if (param[0][0] == 'p' && param[0][1] == 'w') {
01494          printf("%s\n", pwd);
01495       }
01496 
01497       /* create */
01498       else if (param[0][0] == 'c' && param[0][1] == 'r') {
01499          compose_name(pwd, param[2], str);
01500 
01501          /* check if array */
01502 
01503          k = -1;
01504          if (str[strlen(str) - 1] == ']') {
01505             if (strchr(str, '[')) {
01506                j = atoi(strchr(str, '[') + 1);
01507                strlcpy(str2, strchr(str, '[') + 1, sizeof(str2));
01508                *strchr(str, '[') = 0;
01509                if (strchr(str2, '['))
01510                   k = atoi(strchr(str2, '[')+1);
01511             }
01512          } else
01513             j = 1;
01514 
01515          /* get TID */
01516          for (i = 0; i < TID_LAST; i++) {
01517             if (equal_ustring(rpc_tid_name(i), param[1]))
01518                break;
01519          }
01520 
01521          if (i == TID_LAST) {
01522             printf("Unknown type. Must be one of:\n");
01523             printf("{ BYTE,CHAR,WORD,SHORT,DWORD,INT,BOOL,FLOAT,DOUBLE,STRING }\n");
01524          } else {
01525             db_create_key(hDB, 0, str, i);
01526             db_find_key(hDB, 0, str, &hKey);
01527             db_get_key(hDB, hKey, &key);
01528 
01529             if (i == TID_STRING) {
01530                if (!cmd_mode && k == -1) {
01531                   memset(data, 0, sizeof(data));
01532 
01533                   printf("String length [%d]: ", NAME_LENGTH);
01534                   ss_gets(str, 256);
01535                   if (str[0])
01536                      key.item_size = atoi(str);
01537                   else
01538                      key.item_size = NAME_LENGTH;
01539                } else if (k == -1)
01540                   key.item_size = NAME_LENGTH;
01541                else 
01542                   key.item_size = k;
01543 
01544                db_set_link_data(hDB, hKey, data, key.item_size, 1, key.type);
01545             }
01546 
01547             if (j > 1) {
01548                memset(data, 0, sizeof(data));
01549 
01550                if (key.type == TID_LINK)
01551                   key.item_size = NAME_LENGTH;
01552                db_set_link_data_index(hDB, hKey, data, key.item_size, j - 1, key.type);
01553             }
01554          }
01555       }
01556 
01557       /* mkdir */
01558       else if (param[0][0] == 'm' && param[0][1] == 'k') {
01559          compose_name(pwd, param[1], str);
01560          db_create_key(hDB, 0, str, TID_KEY);
01561       }
01562 
01563       /* link */
01564       else if (param[0][0] == 'l' && param[0][1] == 'n') {
01565          compose_name(pwd, param[2], str);
01566          db_create_link(hDB, 0, str, param[1]);
01567       }
01568 
01569       /* copy */
01570       else if (param[0][0] == 'c' && (param[0][1] == 'o' || param[0][1] == 'p')) {
01571          /* test if destination exists */
01572          compose_name(pwd, param[2], str);
01573          status = db_find_link(hDB, 0, str, &hKey);
01574          if (status == DB_SUCCESS) {
01575             if (cmd_mode)
01576                str[0] = 'y';
01577             else {
01578                printf("Overwrite existing key\n\"%s\"\n(y/[n]) ", str);
01579                ss_gets(str, 256);
01580             }
01581             if (str[0] == 'y')
01582                db_delete_key(hDB, hKey, FALSE);
01583          }
01584 
01585          if (status == DB_NO_KEY || str[0] == 'y') {
01586             compose_name(pwd, param[1], str);
01587 
01588             status = db_find_link(hDB, 0, str, &hKey);
01589             if (status == DB_SUCCESS) {
01590                compose_name(pwd, param[2], str);
01591 
01592                db_get_key(hDB, hKey, &key);
01593                db_create_key(hDB, 0, str, key.type);
01594 
01595                if (key.type != TID_KEY) {
01596                   size = sizeof(data);
01597                   db_get_data(hDB, hKey, data, &size, key.type);
01598                   db_find_key(hDB, 0, str, &hKey);
01599                   db_set_link_data(hDB, hKey, data, size, key.num_values, key.type);
01600                } else {
01601                   size = sizeof(data);
01602                   db_copy(hDB, hKey, data, &size, "");
01603                   db_find_key(hDB, 0, str, &hKey);
01604                   db_paste(hDB, hKey, data);
01605                }
01606             } else
01607                printf("key not found\n");
01608          }
01609       }
01610 
01611       /* delete */
01612       else if ((param[0][0] == 'd' && param[0][1] == 'e') ||
01613                (param[0][0] == 'r' && param[0][1] == 'm')) {
01614          flags = 0;
01615          if ((param[1][0] == '-' && param[1][1] == 'f') ||
01616              (param[2][0] == '-' && param[2][1] == 'f'))
01617             flags |= (1 << 0);
01618          if ((param[1][0] == '-' && param[1][1] == 'l') ||
01619              (param[2][0] == '-' && param[2][1] == 'l'))
01620             flags |= (1 << 1);
01621 
01622          for (i = 1; param[i][0] == '-'; i++);
01623 
01624          compose_name(pwd, param[i], str);
01625 
01626          status = db_find_link(hDB, 0, str, &hKey);
01627          db_get_key(hDB, hKey, &key);
01628 
01629          if (status == DB_SUCCESS) {
01630             if (flags & (1 << 0) || cmd_mode)
01631                str[0] = 'y';
01632             else {
01633                if (key.type == TID_KEY)
01634                   printf
01635                       ("Are you sure to delete the key\n\"%s\"\nand all its subkeys? (y/[n]) ",
01636                        str);
01637                else
01638                   printf("Are you sure to delete the key\n\"%s\"\n(y/[n]) ", str);
01639 
01640                ss_gets(str, 256);
01641             }
01642 
01643             if (str[0] == 'y') {
01644                status = db_delete_key(hDB, hKey, (flags & (1 << 1)) > 0);
01645                if (status == DB_NO_ACCESS)
01646                   printf("deletion of key not allowed\n");
01647                if (status == DB_OPEN_RECORD)
01648                   printf("key is open by other client\n");
01649             }
01650          } else
01651             printf("key not found\n");
01652       }
01653 
01654       /* set */
01655       else if (param[0][0] == 's' && param[0][1] == 'e') {
01656          /* check if index is supplied */
01657          index1 = index2 = 0;
01658          strcpy(str, param[1]);
01659          if (str[strlen(str) - 1] == ']') {
01660             if (strchr(str, '[')) {
01661                if (*(strchr(str, '[') + 1) == '*')
01662                   index1 = -1;
01663                else if (strchr((strchr(str, '[') + 1), '.') || 
01664                         strchr((strchr(str, '[') + 1), '-')) {
01665                   index1 = atoi(strchr(str, '[') + 1);
01666                   pc = strchr(str, '[') + 1;
01667                   while (*pc != '.' && *pc != '-')
01668                      pc++;
01669                   while (*pc == '.' || *pc == '-')
01670                      pc++;
01671                   index2 = atoi(pc);
01672                } else
01673                   index1 = atoi(strchr(str, '[') + 1);
01674             }
01675             *strchr(str, '[') = 0;
01676          }
01677 
01678          if (strpbrk(str, "*?") != NULL) {
01679             db_find_key(hDB, 0, pwd, &hKey);
01680             for (i = 0;; i++) {
01681                db_enum_link(hDB, hKey, i, &hSubkey);
01682 
01683                if (!hSubkey)
01684                   break;
01685 
01686                db_get_key(hDB, hSubkey, &key);
01687                if (match(str, key.name))
01688                   set_key(hDB, hSubkey, index1, index2, param[2]);
01689             }
01690          } else {
01691             compose_name(pwd, str, name);
01692 
01693             status = db_find_key(hDB, 0, name, &hKey);
01694             if (status == DB_SUCCESS)
01695                set_key(hDB, hKey, index1, index2, param[2]);
01696             else {
01697                printf("Error: Key \"%s\" not found\n", name);
01698                if (cmd_mode)
01699                   return -1;
01700             }
01701          }
01702       }
01703 
01704       /* set mode */
01705       else if (param[0][0] == 'c' && param[0][1] == 'h' && param[0][2] == 'm') {
01706          compose_name(pwd, param[2], str);
01707 
01708          mode = atoi(param[1]);
01709 
01710          if (strcmp(str, "/") != 0)
01711             status = db_find_key(hDB, 0, str, &hKey);
01712          else
01713             hKey = 0;
01714 
01715          if (status == DB_SUCCESS || !hKey) {
01716             if (cmd_mode)
01717                str[0] = 'y';
01718             else {
01719                printf
01720                    ("Are you sure to change the mode of key\n  %s\nand all its subkeys\n",
01721                     str);
01722                printf("to mode [%c%c%c%c]? (y/[n]) ", mode & MODE_READ ? 'R' : 0,
01723                       mode & MODE_WRITE ? 'W' : 0, mode & MODE_DELETE ? 'D' : 0,
01724                       mode & MODE_EXCLUSIVE ? 'E' : 0);
01725                ss_gets(str, 256);
01726             }
01727             if (str[0] == 'y')
01728                db_set_mode(hDB, hKey, mode, TRUE);
01729          } else {
01730             printf("Error: Key \"%s\" not found\n", str);
01731             if (cmd_mode)
01732                return -1;
01733          }
01734       }
01735 
01736       /* truncate */
01737       else if (param[0][0] == 't' && param[0][1] == 'r') {
01738          compose_name(pwd, param[1], str);
01739 
01740          status = db_find_key(hDB, 0, str, &hKey);
01741 
01742          i = atoi(param[2]);
01743          if (i == 0)
01744             i = 1;
01745 
01746          if (status == DB_SUCCESS)
01747             db_set_num_values(hDB, hKey, i);
01748          else {
01749             printf("Error: Key \"%s\" not found\n", str);
01750             if (cmd_mode)
01751                return -1;
01752          }
01753       }
01754 
01755       /* rename */
01756       else if (param[0][0] == 'r' && param[0][1] == 'e' && param[0][2] == 'n') {
01757          compose_name(pwd, param[1], str);
01758 
01759          if (strcmp(str, "/") != 0)
01760             status = db_find_link(hDB, 0, str, &hKey);
01761          else
01762             hKey = 0;
01763 
01764          if (status == DB_SUCCESS || !hKey)
01765             db_rename_key(hDB, hKey, param[2]);
01766          else {
01767             printf("Error: Key \"%s\" not found\n", str);
01768             if (cmd_mode)
01769                return -1;
01770          }
01771       }
01772 
01773       /* move */
01774       else if (param[0][0] == 'm' && param[0][1] == 'o') {
01775          compose_name(pwd, param[1], str);
01776 
01777          if (strcmp(str, "/") != 0)
01778             status = db_find_link(hDB, 0, str, &hKey);
01779          else
01780             hKey = 0;
01781 
01782          if (status == DB_SUCCESS || !hKey) {
01783             if (param[2][0] == 't')
01784                i = 0;
01785             else if (param[2][0] == 'b')
01786                i = -1;
01787             else
01788                i = atoi(param[2]);
01789 
01790             status = db_reorder_key(hDB, hKey, i);
01791             if (status == DB_NO_ACCESS)
01792                printf("no write access to key\n");
01793             if (status == DB_OPEN_RECORD)
01794                printf("key is open by other client\n");
01795          } else {
01796             printf("Error: Key \"%s\" not found\n", str);
01797             if (cmd_mode)
01798                return -1;
01799          }
01800       }
01801 
01802       /* find key */
01803       else if (param[0][0] == 'f' && param[0][1] == 'i') {
01804          status = db_find_key(hDB, 0, pwd, &hKey);
01805 
01806          if (status == DB_SUCCESS)
01807             db_scan_tree(hDB, hKey, 0, search_key, (void *) param[1]);
01808          else
01809             printf("current key is invalid / no read access\n");
01810       }
01811 
01812       /* load */
01813       else if (param[0][0] == 'l' && param[0][1] == 'o') {
01814          db_find_key(hDB, 0, pwd, &hKey);
01815 
01816          /* keep /Logger/Data dir if new one doesn't exist */
01817          cm_get_path(old_dir);
01818          size = sizeof(old_dir);
01819          db_get_value(hDB, 0, "/Logger/Data dir", old_dir, &size, TID_STRING, TRUE);
01820 
01821          db_load(hDB, hKey, param[1], FALSE);
01822 
01823          str[0] = 0;
01824          size = sizeof(str);
01825          db_get_value(hDB, 0, "/Logger/Data dir", str, &size, TID_STRING, FALSE);
01826 
01827          /* check if new one exists */
01828          getcwd(cwd, sizeof(cwd));
01829          status = chdir(str);
01830          if (status == -1 && strcmp(old_dir, str) != 0) {
01831             printf
01832                 ("\"/Logger/Data dir = %s\" in file \"%s\" does not exist locally,\nset anyhow? (y/[n]): ",
01833                  str, param[1]);
01834             ss_gets(line, 256);
01835 
01836             /* restor old one */
01837             if (line[0] != 'y' && line[0] != 'Y')
01838                db_set_value(hDB, 0, "/Logger/Data dir", old_dir, sizeof(old_dir), 1,
01839                             TID_STRING);
01840          }
01841 
01842          chdir(cwd);
01843       }
01844 
01845       /* save */
01846       else if (param[0][0] == 's' && param[0][1] == 'a') {
01847          db_find_key(hDB, 0, pwd, &hKey);
01848 
01849          if (strstr(param[1], ".xml") || strstr(param[1], ".XML"))
01850             db_save_xml(hDB, hKey, param[1]);
01851          else if (param[1][0] == '-') {
01852             if (param[1][1] == 'c' && param[1][2] == 's') {
01853                db_save_struct(hDB, hKey, param[2], NULL, FALSE);
01854                db_save_string(hDB, hKey, param[2], NULL, TRUE);
01855             }
01856             else if (param[1][1] == 'c')
01857                db_save_struct(hDB, hKey, param[2], NULL, FALSE);
01858             else if (param[1][1] == 's')
01859                db_save_string(hDB, hKey, param[2], NULL, FALSE);
01860             else if (param[1][1] == 'x')
01861                db_save_xml(hDB, hKey, param[2]);
01862          } else
01863             db_save(hDB, hKey, param[1], FALSE);
01864       }
01865 
01866       /* make */
01867       else if (param[0][0] == 'm' && param[0][1] == 'a') {
01868          if (param[1][0])
01869             create_experim_h(hDB, param[1]);
01870          else
01871             create_experim_h(hDB, "Analyzer");
01872       }
01873 
01874       /* passwd */
01875       else if (param[0][0] == 'p' && param[0][1] == 'a' && param[0][2] == 's') {
01876          /*
01877             strcpy(str, ss_crypt("foob", "ar"));
01878             if(strcmp(str, "arlEKn0OzVJn.") != 0)
01879             printf("Warning: ss_crypt() works incorrect");
01880           */
01881 
01882          if (db_find_key(hDB, 0, "/Experiment/Security/Password", &hKey) == DB_SUCCESS) {
01883             size = sizeof(old_password);
01884             db_get_data(hDB, hKey, old_password, &size, TID_STRING);
01885 
01886             strcpy(str, ss_getpass("Old password: "));
01887             strcpy(str, ss_crypt(str, "mi"));
01888 
01889             if (strcmp(str, old_password) == 0 || strcmp(str, "mid7qBxsNMHux") == 0) {
01890                strcpy(str, ss_getpass("New password: "));
01891                strcpy(new_password, ss_crypt(str, "mi"));
01892 
01893                strcpy(str, ss_getpass("Retype new password: "));
01894                if (strcmp(new_password, ss_crypt(str, "mi")) != 0)
01895                   printf("Mismatch - password unchanged\n");
01896                else
01897                   db_set_data(hDB, hKey, new_password, 32, 1, TID_STRING);
01898             } else
01899                printf("Wrong password\n");
01900          } else {
01901             strcpy(str, ss_getpass("Password: "));
01902             strcpy(new_password, ss_crypt(str, "mi"));
01903 
01904             strcpy(str, ss_getpass("Retype password: "));
01905             if (strcmp(new_password, ss_crypt(str, "mi")) != 0)
01906                printf("Mismatch - password not set\n");
01907             else {
01908                /* set password */
01909                db_set_value(hDB, 0, "/Experiment/Security/Password", new_password, 32, 1,
01910                             TID_STRING);
01911 
01912                /* create empty allowed hosts and allowd programs entries */
01913                db_create_key(hDB, 0,
01914                              "/Experiment/Security/Allowed hosts/host.sample.domain",
01915                              TID_INT);
01916                db_create_key(hDB, 0, "/Experiment/Security/Allowed programs/mstat",
01917                              TID_INT);
01918             }
01919          }
01920 
01921       }
01922 
01923       /* webpasswd */
01924       else if (param[0][0] == 'w' && param[0][1] == 'e' && param[0][2] == 'b') {
01925          if (db_find_key(hDB, 0, "/Experiment/Security/Web Password", &hKey) ==
01926              DB_SUCCESS) {
01927             size = sizeof(old_password);
01928             db_get_data(hDB, hKey, old_password, &size, TID_STRING);
01929 
01930             strcpy(str, ss_getpass("Old password: "));
01931             strcpy(str, ss_crypt(str, "mi"));
01932 
01933             if (strcmp(str, old_password) == 0 || strcmp(str, "mid7qBxsNMHux") == 0) {
01934                strcpy(str, ss_getpass("New password: "));
01935                strcpy(new_password, ss_crypt(str, "mi"));
01936 
01937                strcpy(str, ss_getpass("Retype new password: "));
01938                if (strcmp(new_password, ss_crypt(str, "mi")) != 0)
01939                   printf("Mismatch - password unchanged\n");
01940                else
01941                   db_set_data(hDB, hKey, new_password, 32, 1, TID_STRING);
01942             } else
01943                printf("Wrong password\n");
01944          } else {
01945             strcpy(str, ss_getpass("Password: "));
01946             strcpy(new_password, ss_crypt(str, "mi"));
01947 
01948             strcpy(str, ss_getpass("Retype password: "));
01949             if (strcmp(new_password, ss_crypt(str, "mi")) != 0)
01950                printf("Mismatch - password not set\n");
01951             else
01952                /* set password */
01953                db_set_value(hDB, 0, "/Experiment/Security/Web Password", new_password, 32,
01954                             1, TID_STRING);
01955          }
01956       }
01957 
01958       /* hi */
01959       else if (param[0][0] == 'h' && param[0][1] == 'i') {
01960          HNDLE hConn;
01961 
01962          client_name[0] = 0;
01963 
01964          if (!isalpha(param[1][0])) {
01965             /* find client which exports RPC_ANA_CLEAR_HISTOS */
01966             status = db_find_key(hDB, 0, "System/Clients", &hRootKey);
01967             if (status == DB_SUCCESS) {
01968                for (i = 0;; i++) {
01969                   status = db_enum_key(hDB, hRootKey, i, &hSubkey);
01970                   if (status == DB_NO_MORE_SUBKEYS) {
01971                      printf
01972                          ("No client currently exports the CLEAR HISTO functionality.\n");
01973                      break;
01974                   }
01975 
01976                   sprintf(str, "RPC/%d", RPC_ANA_CLEAR_HISTOS);
01977                   status = db_find_key(hDB, hSubkey, str, &hKey);
01978                   if (status == DB_SUCCESS) {
01979                      size = sizeof(client_name);
01980                      db_get_value(hDB, hSubkey, "Name", client_name, &size, TID_STRING,
01981                                   TRUE);
01982                      break;
01983                   }
01984                }
01985             }
01986 
01987             if (isdigit(param[1][0]))
01988                n1 = atoi(param[1]);
01989             else
01990                n1 = 0;          /* all histos by default */
01991 
01992             if (isdigit(param[2][0]))
01993                n2 = atoi(param[2]);
01994             else
01995                n2 = n1;         /* single histo by default */
01996          } else {
01997             strcpy(client_name, param[1]);
01998 
01999             if (isdigit(param[2][0]))
02000                n1 = atoi(param[2]);
02001             else
02002                n1 = 0;          /* all histos by default */
02003 
02004             if (isdigit(param[3][0]))
02005                n2 = atoi(param[3]);
02006             else
02007                n2 = n1;         /* single histo by default */
02008          }
02009 
02010          if (client_name[0]) {
02011             if (cm_connect_client(client_name, &hConn) == CM_SUCCESS) {
02012                rpc_client_call(hConn, RPC_ANA_CLEAR_HISTOS, n1, n2);
02013                cm_disconnect_client(hConn, FALSE);
02014             } else
02015                printf("Cannot connect to client %s\n", client_name);
02016          }
02017       }
02018 
02019       /* import */
02020       else if (param[0][0] == 'i' && param[0][1] == 'm') {
02021          int fh;
02022 
02023          fh = open(param[1], O_RDONLY | O_TEXT);
02024          if (fh < 0)
02025             printf("File %s not found\n", param[1]);
02026          else {
02027             size = lseek(fh, 0, SEEK_END);
02028             lseek(fh, 0, SEEK_SET);
02029             size = read(fh, data, size);
02030             data[size++] = 0;
02031             close(fh);
02032 
02033             if (param[2][0] == 0) {
02034                printf("Key name: ");
02035                ss_gets(name, 256);
02036             } else
02037                strcpy(name, param[2]);
02038 
02039             compose_name(pwd, name, str);
02040 
02041             db_create_key(hDB, 0, str, TID_STRING);
02042             db_find_key(hDB, 0, str, &hKey);
02043             db_set_data(hDB, hKey, data, size, 1, TID_STRING);
02044          }
02045 
02046       }
02047 
02048       /* export */
02049       else if (param[0][0] == 'e' && param[0][1] == 'x' && param[0][2] == 'p') {
02050          FILE *f;
02051 
02052          if (param[1][0] == 0)
02053             printf("please specify key\n");
02054          else {
02055             compose_name(pwd, param[1], str);
02056 
02057             db_find_key(hDB, 0, str, &hKey);
02058             if (hKey == 0)
02059                printf("Error: Key \"%s\" not found\n", param[1]);
02060             else {
02061                if (param[2][0] == 0) {
02062                   printf("File name: ");
02063                   ss_gets(name, 256);
02064                } else
02065                   strcpy(name, param[2]);
02066 
02067                f = fopen(name, "w");
02068                if (f == NULL)
02069                   printf("Cannot open file \"%s\"\n", name);
02070                else {
02071                   db_get_key(hDB, hKey, &key);
02072                   if (key.type != TID_STRING)
02073                      printf("Only export of STRING key possible\n");
02074                   else {
02075                      size = sizeof(data);
02076                      memset(data, 0, size);
02077                      db_get_data(hDB, hKey, data, &size, key.type);
02078 
02079                      fprintf(f, data);
02080                      fclose(f);
02081                   }
02082                }
02083             }
02084          }
02085       }
02086 
02087       /* rewind */
02088       else if (param[0][0] == 'r' && param[0][1] == 'e' && param[0][2] == 'w') {
02089          HNDLE hConn;
02090 
02091          /* check if run is stopped and no transition in progress */
02092          size = sizeof(i);
02093          db_get_value(hDB, 0, "/Runinfo/State", &i, &size, TID_INT, TRUE);
02094          db_get_value(hDB, 0, "/Runinfo/Transition in progress", &j, &size, TID_INT,
02095                       TRUE);
02096          if (i != STATE_STOPPED || j == 1) {
02097             printf
02098                 ("Cannot rewind tapes when run is not stopped or transition in progress\n");
02099          } else {
02100             if (param[1][0] == 0)
02101                channel = -1;
02102             else
02103                channel = atoi(param[1]);
02104 
02105             /* find client which exports RPC_LOG_REWIND */
02106             status = db_find_key(hDB, 0, "System/Clients", &hRootKey);
02107             if (status == DB_SUCCESS) {
02108                for (i = 0;; i++) {
02109                   status = db_enum_key(hDB, hRootKey, i, &hSubkey);
02110                   if (status == DB_NO_MORE_SUBKEYS) {
02111                      printf("No active logger found.\n");
02112                      break;
02113                   }
02114 
02115                   sprintf(str, "RPC/%d", RPC_LOG_REWIND);
02116                   status = db_find_key(hDB, hSubkey, str, &hKey);
02117                   if (status == DB_SUCCESS) {
02118                      size = sizeof(client_name);
02119                      db_get_value(hDB, hSubkey, "Name", client_name, &size, TID_STRING,
02120                                   TRUE);
02121                      break;
02122                   }
02123                }
02124             }
02125 
02126             if (status != DB_NO_MORE_SUBKEYS) {
02127                if (cm_connect_client(client_name, &hConn) == CM_SUCCESS) {
02128                   /* increase timeout */
02129                   rpc_set_option(hConn, RPC_OTIMEOUT, 600000);
02130 
02131                   if (channel == -1)
02132                      printf("Rewinding all channels...\n");
02133                   else
02134                      printf("Rewinding channel %d...\n", channel);
02135 
02136                   status = rpc_client_call(hConn, RPC_LOG_REWIND, channel);
02137                   cm_disconnect_client(hConn, FALSE);
02138 
02139                   if (status == CM_SUCCESS)
02140                      printf("Rewind successful\n");
02141                   else
02142                      printf("Rewind error, code = %d\n", status);
02143                } else
02144                   printf("Cannot connect to logger\n");
02145             }
02146          }
02147       }
02148 
02149       /* alarm reset */
02150       else if (param[0][0] == 'a' && param[0][1] == 'l') {
02151          /* go through all alarms */
02152          db_find_key(hDB, 0, "/Alarms/Alarms", &hKey);
02153          if (hKey) {
02154             for (i = 0;; i++) {
02155                db_enum_link(hDB, hKey, i, &hSubkey);
02156 
02157                if (!hSubkey)
02158                   break;
02159 
02160                db_get_key(hDB, hSubkey, &key);
02161                status = al_reset_alarm(key.name);
02162                if (status == AL_RESET)
02163                   printf("Alarm of class \"%s\" reset sucessfully\n", key.name);
02164             }
02165          }
02166       }
02167 
02168       /* mem */
02169       else if (param[0][0] == 'm' && param[0][1] == 'e') {
02170          if (rpc_is_remote())
02171             printf("This function works only locally\n");
02172          else {
02173             db_show_mem(hDB, data, sizeof(data), param[1][0]);
02174             puts(data);
02175          }
02176       }
02177 
02178       /* sor (show open records) */
02179       else if (param[0][0] == 's' && param[0][1] == 'o') {
02180          db_find_key(hDB, 0, pwd, &hKey);
02181          db_get_open_records(hDB, hKey, data, sizeof(data), FALSE);
02182          printf(data);
02183       }
02184 
02185       /* scl (show clients ) */
02186       else if (param[0][0] == 's' && param[0][1] == 'c') {
02187          status = db_find_key(hDB, 0, "System/Clients", &hKey);
02188          if (status != DB_SUCCESS)
02189             cm_msg(MERROR, "command_loop",
02190                    "cannot find System/Clients entry in database");
02191          else {
02192             if (param[1][1] == 'w')
02193                printf("Name                Host                Timeout    Last called\n");
02194             else
02195                printf("Name                Host\n");
02196 
02197             /* search database for clients with transition mask set */
02198             for (i = 0, status = 0;; i++) {
02199                status = db_enum_key(hDB, hKey, i, &hSubkey);
02200                if (status == DB_NO_MORE_SUBKEYS)
02201                   break;
02202 
02203                if (status == DB_SUCCESS) {
02204                   size = sizeof(name);
02205                   db_get_value(hDB, hSubkey, "Name", name, &size, TID_STRING, TRUE);
02206                   printf(name);
02207                   for (j = 0; j < 20 - (int) strlen(name); j++)
02208                      printf(" ");
02209 
02210                   size = sizeof(str);
02211                   db_get_value(hDB, hSubkey, "Host", str, &size, TID_STRING, TRUE);
02212                   printf(str);
02213                   for (j = 0; j < 20 - (int) strlen(str); j++)
02214                      printf(" ");
02215 
02216                   /* display optional watchdog info */
02217                   if (param[1][1] == 'w') {
02218                      DWORD timeout, last;
02219 
02220                      status = cm_get_watchdog_info(hDB, name, &timeout, &last);
02221                      printf("%-10d %-10d", timeout, last);
02222                   }
02223 
02224                   printf("\n");
02225                }
02226             }
02227          }
02228       }
02229 
02230       /* start */
02231       else if (param[0][0] == 's' && param[0][1] == 't' && param[0][2] == 'a') {
02232          debug_flag = ((param[1][0] == '-' && param[1][1] == 'v') ||
02233                        (param[2][0] == '-' && param[2][1] == 'v'));
02234 
02235          /* check if run is already started */
02236          size = sizeof(i);
02237          i = STATE_STOPPED;
02238          db_get_value(hDB, 0, "/Runinfo/State", &i, &size, TID_INT, TRUE);
02239          if (i == STATE_RUNNING) {
02240             printf("Run is already started\n");
02241          } else if (i == STATE_PAUSED) {
02242             printf("Run is paused, please use \"resume\"\n");
02243          } else {
02244             /* get present run number */
02245             old_run_number = 0;
02246             status =
02247                 db_get_value(hDB, 0, "/Runinfo/Run number", &old_run_number, &size,
02248                              TID_INT, TRUE);
02249             assert(status == SUCCESS);
02250             assert(old_run_number >= 0);
02251 
02252             /* edit run parameter if command is not "start now" */
02253             if ((param[1][0] == 'n' && param[1][1] == 'o' && param[1][2] == 'w') ||
02254                 cmd_mode) {
02255                new_run_number = old_run_number + 1;
02256                line[0] = 'y';
02257             } else {
02258                db_find_key(hDB, 0, "/Experiment/Edit on start", &hKey);
02259                do {
02260                   if (hKey) {
02261                      for (i = 0;; i++) {
02262                         db_enum_link(hDB, hKey, i, &hSubkey);
02263 
02264                         if (!hSubkey)
02265                            break;
02266 
02267                         db_get_key(hDB, hSubkey, &key);
02268                         strcpy(str, key.name);
02269 
02270                         if (equal_ustring(str, "Edit run number"))
02271                            continue;
02272 
02273                         db_enum_key(hDB, hKey, i, &hSubkey);
02274                         db_get_key(hDB, hSubkey, &key);
02275 
02276                         size = sizeof(data);
02277                         status = db_get_data(hDB, hSubkey, data, &size, key.type);
02278                         if (status != DB_SUCCESS)
02279                            continue;
02280 
02281                         for (j = 0; j < key.num_values; j++) {
02282                            db_sprintf(data_str, data, key.item_size, j, key.type);
02283                            sprintf(prompt, "%s : ", str);
02284 
02285                            strcpy(line, data_str);
02286                            in_cmd_edit = TRUE;
02287                            cmd_edit(prompt, line, NULL, cmd_idle);
02288                            in_cmd_edit = FALSE;
02289 
02290                            if (line[0]) {
02291                               db_sscanf(line, data, &size, j, key.type);
02292                               db_set_data_index(hDB, hSubkey, data, key.item_size, j,
02293                                                 key.type);
02294                            }
02295                         }
02296                      }
02297                   }
02298 
02299                   /* increment run number */
02300                   new_run_number = old_run_number + 1;
02301                   printf("Run number [%d]: ", new_run_number);
02302                   ss_gets(line, 256);
02303                   if (line[0] && atoi(line) > 0)
02304                      new_run_number = atoi(line);
02305 
02306                   printf("Are the above parameters correct? ([y]/n/q): ");
02307                   ss_gets(line, 256);
02308 
02309                } while (line[0] == 'n' || line[0] == 'N');
02310             }
02311 
02312             if (line[0] != 'q' && line[0] != 'Q') {
02313                /* start run */
02314                printf("Starting run #%d\n", new_run_number);
02315 
02316                assert(new_run_number > 0);
02317 
02318                status =
02319                    cm_transition(TR_START, new_run_number, str, sizeof(str), SYNC,
02320                                  debug_flag);
02321                if (status != CM_SUCCESS) {
02322                   /* in case of error, reset run number */
02323                   status =
02324                       db_set_value(hDB, 0, "/Runinfo/Run number", &old_run_number,
02325                                    sizeof(old_run_number), 1, TID_INT);
02326                   assert(status == SUCCESS);
02327 
02328                   printf("Error: %s\n", str);
02329                }
02330             }
02331          }
02332       }
02333 
02334       /* stop */
02335       else if (param[0][0] == 's' && param[0][1] == 't' && param[0][2] == 'o') {
02336          debug_flag = ((param[1][0] == '-' && param[1][1] == 'v') ||
02337                        (param[2][0] == '-' && param[2][1] == 'v'));
02338 
02339          /* check if run is stopped */
02340          state = STATE_STOPPED;
02341          size = sizeof(i);
02342          db_get_value(hDB, 0, "/Runinfo/State", &state, &size, TID_INT, TRUE);
02343          str[0] = 0;
02344          if (state == STATE_STOPPED && !cmd_mode) {
02345             printf("Run is already stopped. Stop again? (y/[n]) ");
02346             ss_gets(str, 256);
02347          }
02348          if (str[0] == 'y' || state != STATE_STOPPED || cmd_mode) {
02349             if (param[1][0] == 'n')
02350                status =
02351                    cm_transition(TR_STOP | TR_DEFERRED, 0, str, sizeof(str), SYNC,
02352                                  debug_flag);
02353             else
02354                status = cm_transition(TR_STOP, 0, str, sizeof(str), SYNC, debug_flag);
02355 
02356             if (status == CM_DEFERRED_TRANSITION)
02357                printf("%s\n", str);
02358             else if (status == CM_TRANSITION_IN_PROGRESS)
02359                printf
02360                    ("Deferred stop already in progress, enter \"stop now\" to force stop\n");
02361             else if (status != CM_SUCCESS)
02362                printf("Error: %s\n", str);
02363          }
02364       }
02365 
02366       /* pause */
02367       else if (param[0][0] == 'p' && param[0][1] == 'a' && param[0][2] == 'u') {
02368          debug_flag = ((param[1][0] == '-' && param[1][1] == 'v') ||
02369                        (param[2][0] == '-' && param[2][1] == 'v'));
02370 
02371          /* check if run is started */
02372          i = STATE_STOPPED;
02373          size = sizeof(i);
02374          db_get_value(hDB, 0, "/Runinfo/State", &i, &size, TID_INT, TRUE);
02375          if (i != STATE_RUNNING) {
02376             printf("Run is not started\n");
02377          } else {
02378             if (param[1][0] == 'n')
02379                status =
02380                    cm_transition(TR_PAUSE | TR_DEFERRED, 0, str, sizeof(str), SYNC,
02381                                  debug_flag);
02382             else
02383                status = cm_transition(TR_PAUSE, 0, str, sizeof(str), SYNC, debug_flag);
02384 
02385             if (status == CM_DEFERRED_TRANSITION)
02386                printf("%s\n", str);
02387             else if (status == CM_TRANSITION_IN_PROGRESS)
02388                printf
02389                    ("Deferred pause already in progress, enter \"pause now\" to force pause\n");
02390             else if (status != CM_SUCCESS)
02391                printf("Error: %s\n", str);
02392          }
02393       }
02394 
02395       /* resume */
02396       else if (param[0][0] == 'r' && param[0][1] == 'e' && param[0][2] == 's') {
02397          debug_flag = ((param[1][0] == '-' && param[1][1] == 'v') ||
02398                        (param[2][0] == '-' && param[2][1] == 'v'));
02399 
02400          /* check if run is paused */
02401          i = STATE_STOPPED;
02402          size = sizeof(i);
02403          db_get_value(hDB, 0, "/Runinfo/State", &i, &size, TID_INT, TRUE);
02404          if (i != STATE_PAUSED) {
02405             printf("Run is not paused\n");
02406          } else {
02407             status = cm_transition(TR_RESUME, 0, str, sizeof(str), SYNC, debug_flag);
02408             if (status != CM_SUCCESS)
02409                printf("Error: %s\n", str);
02410          }
02411       }
02412 
02413       /* msg */
02414       else if (param[0][0] == 'm' && param[0][1] == 's') {
02415          /*
02416             if (cm_exist("Speaker", FALSE) != CM_SUCCESS)
02417             printf("Note: No speaker application present.\n");
02418           */
02419 
02420          /* user message type by default */
02421          msg_type = MT_USER;
02422          message[0] = 0;
02423          if (cmd_mode)
02424             strcpy(user_name, "script");
02425 
02426          if (param[3][0]) {
02427             msg_type = atoi(param[1]);
02428             last_msg_time = ss_time();
02429             strcpy(user_name, param[2]);
02430             strcpy(message, param[3]);
02431          } else if (param[2][0]) {
02432             last_msg_time = ss_time();
02433             strcpy(user_name, param[1]);
02434             strcpy(message, param[2]);
02435          } else if (param[1][0])
02436             strlcpy(message, param[1], sizeof(message));
02437 
02438          if (!cmd_mode) {
02439             if (ss_time() - last_msg_time > 300) {
02440                printf("Your name> ");
02441                ss_gets(user_name, 80);
02442             }
02443 
02444             if (param[1][0] == 0) {
02445                printf("Msg> ");
02446                ss_gets(message, 256);
02447             }
02448          }
02449 
02450          if (message[0])
02451             cm_msg(msg_type, __FILE__, __LINE__, user_name, message);
02452 
02453          last_msg_time = ss_time();
02454       }
02455 
02456       /* chat */
02457       else if (param[0][0] == 'c' && param[0][1] == 'h' && param[0][2] == 'a') {
02458          message[0] = 0;
02459 
02460          if (ss_time() - last_msg_time > 300) {
02461             printf("Your name> ");
02462             ss_gets(user_name, 80);
02463          }
02464 
02465          printf("Exit chat mode with empty line.\n");
02466          do {
02467             in_cmd_edit = TRUE;
02468             message[0] = 0;
02469             cmd_edit("> ", message, NULL, cmd_idle);
02470             in_cmd_edit = FALSE;
02471 
02472             if (message[0])
02473                cm_msg(MUSER, user_name, message);
02474 
02475          } while (message[0]);
02476 
02477          last_msg_time = ss_time();
02478       }
02479 
02480       /* old */
02481       else if (param[0][0] == 'o' && param[0][1] == 'l') {
02482          i = 20;
02483          if (param[1][0])
02484             i = atoi(param[1]);
02485 
02486          cm_msg_retrieve(i, data, sizeof(data));
02487          printf(data);
02488          printf("\n\n");
02489       }
02490 
02491       /* cleanup */
02492       else if (param[0][0] == 'c' && param[0][1] == 'l') {
02493          HNDLE hBuf;
02494          BOOL force;
02495 
02496          force = FALSE;
02497          if (param[1][0] == '-' && param[1][1] == 'f')
02498             force = TRUE;
02499          if (param[2][0] == '-' && param[2][1] == 'f')
02500             force = TRUE;
02501 
02502          bm_open_buffer(EVENT_BUFFER_NAME, 2*MAX_EVENT_SIZE, &hBuf);
02503 
02504          if (param[1][0] && param[1][0] != '-')
02505             cm_cleanup(param[1], force);
02506          else
02507             cm_cleanup("", force);
02508          bm_close_buffer(hBuf);
02509       }
02510 
02511       /* shutdown */
02512       else if (param[0][0] == 's' && param[0][1] == 'h') {
02513          if (param[1][0] == 0)
02514             printf("Please enter client name or \"all\" to shutdown all clients.\n");
02515          else {
02516             status = cm_shutdown(param[1], TRUE);
02517             if (status != CM_SUCCESS) {
02518                if (strcmp(param[1], "all") == 0)
02519                   printf("No clients found\n");
02520                else
02521                   printf("Client %s not active\n", param[1]);
02522             }
02523          }
02524       }
02525 
02526       /* ver */
02527       else if (param[0][0] == 'v' && param[0][1] == 'e') {
02528          printf("MIDAS version %s\n", cm_get_version());
02529          printf("SVN revision %d\n", cm_get_revision());
02530          printf("ODB version %d\n", DATABASE_VERSION);
02531          printf("Maximum event size %1.1lf MB\n", MAX_EVENT_SIZE/1024.0/1024.0);
02532       }
02533 
02534       /* exec */
02535       else if (param[0][0] == 'e' && param[0][1] == 'x' && param[0][2] == 'e') {
02536          compose_name(pwd, param[1], str);
02537 
02538          status = db_find_key(hDB, 0, str, &hKey);
02539          if (status == DB_SUCCESS) {
02540             db_get_key(hDB, hKey, &key);
02541             if (key.type != TID_STRING) {
02542                printf("Key contains no command\n");
02543                continue;
02544             }
02545             size = sizeof(str);
02546             db_get_data(hDB, hKey, str, &size, key.type);
02547 
02548             cm_execute(str, data, sizeof(data));
02549          } else {
02550             cm_execute(param[1], data, sizeof(data));
02551          }
02552          puts(data);
02553       }
02554 
02555       /* wait */
02556       else if (param[0][0] == 'w' && param[0][1] == 'a') {
02557          compose_name(pwd, param[1], str);
02558 
02559          if (strcmp(str, "/") != 0)
02560             status = db_find_link(hDB, 0, str, &hKey);
02561          else
02562             hKey = 0;
02563 
02564          if (status == DB_SUCCESS || !hKey) {
02565             db_get_key(hDB, hKey, &key);
02566             printf("Waiting for key \"%s\" to be modified, abort with \"!\"\n", key.name);
02567             db_get_record_size(hDB, hKey, 0, &size);
02568             db_open_record(hDB, hKey, data, size, MODE_READ, key_update, NULL);
02569             key_modified = FALSE;
02570 
02571             i = 0;
02572             do {
02573                cm_yield(1000);
02574 
02575                while (ss_kbhit()) {
02576                   i = ss_getchar(0);
02577                   if (i == -1)
02578                      i = getchar();
02579 
02580                   if ((char) i == '!')
02581                      break;
02582                }
02583 
02584             } while (!key_modified && i != '!');
02585 
02586             db_close_record(hDB, hKey);
02587             if (i == '!')
02588                printf("Wait aborted.\n");
02589             else
02590                printf("Key has been modified.\n");
02591          } else
02592             printf("key not found\n");
02593       }
02594 
02595       /* test 1  */
02596       else if (param[0][0] == 't' && param[0][1] == '1') {
02597          DWORD start_time;
02598          INT i, size, rn;
02599          HNDLE hKey;
02600 
02601          start_time = ss_millitime();
02602          status = db_find_key(hDB, 0, "/runinfo/run number", &hKey);
02603          assert(status == SUCCESS);
02604          size = sizeof(rn);
02605 
02606          i = 0;
02607          do {
02608             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02609             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02610             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02611             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02612             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02613             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02614             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02615             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02616             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02617             db_get_data(hDB, hKey, &rn, &size, TID_INT);
02618             i += 10;
02619          } while (ss_millitime() - start_time < 5000);
02620 
02621          printf("%d accesses per second\n", i / 5);
02622       }
02623 
02624       /* test 2 */
02625       else if (param[0][0] == 't' && param[0][1] == '2') {
02626          do {
02627             do {
02628                i = ss_getchar(0);
02629                printf("%d\n", i);
02630             } while (i > 0 && i != 4);
02631 
02632             ss_sleep(1000);
02633          } while (i != 4);
02634 
02635          ss_getchar(1);
02636       }
02637 
02638       /* test 3 */
02639       else if (param[0][0] == 't' && param[0][1] == '3') {
02640          ss_mutex_create(&tm);
02641          ss_thread_create(thread, NULL);
02642          ss_thread_create(thread, NULL);
02643          ss_thread_create(thread, NULL);
02644       }
02645 
02646       /* exit/quit */
02647       else if ((param[0][0] == 'e' && param[0][1] == 'x' && param[0][2] == 'i') ||
02648                (param[0][0] == 'q'))
02649          break;
02650 
02651       else if (param[0][0] == 0);
02652 
02653       else
02654          printf("Unknown command %s %s %s\n", param[0], param[1], param[2]);
02655 
02656       /* exit after single command */
02657       if (cmd_mode && cmd[0] != '@')
02658          break;
02659 
02660       /* check if client connections are broken */
02661       status = cm_yield(0);
02662       if (status == SS_ABORT)
02663          break;
02664 
02665    } while (TRUE);
02666 
02667    /* check if client connections are broken */
02668    for (i = 0; i < MAX_RPC_CONNECTION; i++)
02669       cm_yield(0);
02670 
02671    return 1; /* indicate success */
02672 }

void compose_name ( char *  pwd,
char *  name,
char *  full_name 
)

Definition at line 1183 of file odbedit.c.

Referenced by command_loop().

01184 {
01185    if (name[0] != '/') {
01186       strcpy(full_name, pwd);
01187       if (full_name[strlen(full_name) - 1] != '/')
01188          strcat(full_name, "/");
01189       strcat(full_name, name);
01190    } else
01191       strcpy(full_name, name);
01192 }

void create_experim_h ( HNDLE  hDB,
char *  analyzer_name 
)

Definition at line 956 of file odbedit.c.

Referenced by command_loop().

00957 {
00958    INT i, index, subindex, hfile, status, size;
00959    HNDLE hKey, hKeyRoot, hKeyEq, hDefKey, hKeyBank, hKeyPar;
00960    char str[80], eq_name[80], subeq_name[80];
00961    KEY key;
00962    time_t now;
00963 
00964    char experim_h_comment1[] =
00965        "/********************************************************************\\\n\
00966 \n\
00967   Name:         experim.h\n\
00968   Created by:   ODBedit program\n\
00969 \n\
00970   Contents:     This file contains C structures for the \"Experiment\"\n\
00971                 tree in the ODB and the \"/Analyzer/Parameters\" tree.\n\
00972 \n\
00973                 Additionally, it contains the \"Settings\" subtree for\n\
00974                 all items listed under \"/Equipment\" as well as their\n\
00975                 event definition.\n\
00976 \n\
00977                 It can be used by the frontend and analyzer to work\n\
00978                 with these information.\n\
00979 \n\
00980                 All C structures are accompanied with a string represen-\n\
00981                 tation which can be used in the db_create_record function\n\
00982                 to setup an ODB structure which matches the C structure.\n\
00983 \n";
00984    char experim_h_comment2[] =
00985        "\\********************************************************************/\n\n";
00986 
00987    char *file_name = "experim.h";
00988 
00989    /* create file */
00990    hfile = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
00991    if (hfile == -1)
00992       cm_msg(MERROR, "create_experim_h", "cannot open experim.h file.");
00993 
00994    /* write comment to file */
00995    write(hfile, experim_h_comment1, strlen(experim_h_comment1));
00996    time(&now);
00997    sprintf(str, "  Created on:   %s\n", ctime(&now));
00998    write(hfile, str, strlen(str));
00999    write(hfile, experim_h_comment2, strlen(experim_h_comment2));
01000 
01001    /* write /experiment/run parameters */
01002    db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey);
01003    if (hKey) {
01004       sprintf(str, "#define EXP_PARAM_DEFINED\n\n");
01005       write(hfile, str, strlen(str));
01006       db_save_struct(hDB, hKey, file_name, "EXP_PARAM", TRUE);
01007       db_save_string(hDB, hKey, file_name, "EXP_PARAM_STR", TRUE);
01008       lseek(hfile, 0, SEEK_END);
01009    }
01010 
01011    /* write /experiment/edit on start */
01012    db_find_key(hDB, 0, "/Experiment/Edit on start", &hKey);
01013    if (hKey) {
01014       sprintf(str, "#define EXP_EDIT_DEFINED\n\n");
01015       write(hfile, str, strlen(str));
01016       db_save_struct(hDB, hKey, file_name, "EXP_EDIT", TRUE);
01017       db_save_string(hDB, hKey, file_name, "EXP_EDIT_STR", TRUE);
01018       lseek(hfile, 0, SEEK_END);
01019    }
01020 
01021    /* write /<Analyzer>/parameters tree */
01022    sprintf(str, "/%s/Parameters", analyzer_name);
01023    status = db_find_key(hDB, 0, str, &hKeyRoot);
01024    if (status != DB_SUCCESS) {
01025       printf("Analyzer \"%s\" not found in ODB, skipping analyzer parameters.\n",
01026              analyzer_name);
01027    } else {
01028       for (index = 0;; index++) {
01029          status = db_enum_key(hDB, hKeyRoot, index, &hKeyPar);
01030          if (status == DB_NO_MORE_SUBKEYS)
01031             break;
01032 
01033          db_get_key(hDB, hKeyPar, &key);
01034          strcpy(eq_name, key.name);
01035          name2c(eq_name);
01036          for (i = 0; i < (int) strlen(eq_name); i++)
01037             eq_name[i] = toupper(eq_name[i]);
01038 
01039          lseek(hfile, 0, SEEK_END);
01040          sprintf(str, "#ifndef EXCL_%s\n\n", eq_name);
01041          write(hfile, str, strlen(str));
01042 
01043          sprintf(str, "#define %s_PARAM_DEFINED\n\n", eq_name);
01044          write(hfile, str, strlen(str));
01045          sprintf(str, "%s_PARAM", eq_name);
01046          db_save_struct(hDB, hKeyPar, file_name, str, TRUE);
01047          sprintf(str, "%s_PARAM_STR", eq_name);
01048          db_save_string(hDB, hKeyPar, file_name, str, TRUE);
01049 
01050          lseek(hfile, 0, SEEK_END);
01051          sprintf(str, "#endif\n\n");
01052          write(hfile, str, strlen(str));
01053       }
01054    }
01055 
01056    /* loop through equipment list */
01057    status = db_find_key(hDB, 0, "/Equipment", &hKeyRoot);
01058    if (status == DB_SUCCESS)
01059       for (index = 0;; index++) {
01060          status = db_enum_key(hDB, hKeyRoot, index, &hKeyEq);
01061          if (status == DB_NO_MORE_SUBKEYS)
01062             break;
01063 
01064          db_get_key(hDB, hKeyEq, &key);
01065          strcpy(eq_name, key.name);
01066          name2c(eq_name);
01067          for (i = 0; i < (int) strlen(eq_name); i++)
01068             eq_name[i] = toupper(eq_name[i]);
01069 
01070          lseek(hfile, 0, SEEK_END);
01071          sprintf(str, "#ifndef EXCL_%s\n\n", eq_name);
01072          write(hfile, str, strlen(str));
01073 
01074          size = sizeof(str);
01075          str[0] = 0;
01076          db_get_value(hDB, hKeyEq, "Common/Format", str, &size, TID_STRING, TRUE);
01077 
01078          /* if event is in fixed format, extract header file */
01079          if (equal_ustring(str, "Fixed")) {
01080             db_find_key(hDB, hKeyEq, "Variables", &hDefKey);
01081             if (hDefKey) {
01082                lseek(hfile, 0, SEEK_END);
01083                sprintf(str, "#define %s_EVENT_DEFINED\n\n", eq_name);
01084                write(hfile, str, strlen(str));
01085 
01086                sprintf(str, "%s_EVENT", eq_name);
01087                db_save_struct(hDB, hDefKey, file_name, str, TRUE);
01088                sprintf(str, "%s_EVENT_STR", eq_name);
01089                db_save_string(hDB, hDefKey, file_name, str, TRUE);
01090             }
01091          }
01092 
01093          /* if event is in MIDAS format, extract bank definition */
01094          else if (equal_ustring(str, "MIDAS")) {
01095             db_find_key(hDB, hKeyEq, "Variables", &hDefKey);
01096             if (hDefKey) {
01097                for (i = 0;; i++) {
01098                   status = db_enum_key(hDB, hDefKey, i, &hKeyBank);
01099                   if (status == DB_NO_MORE_SUBKEYS)
01100                      break;
01101 
01102                   db_get_key(hDB, hKeyBank, &key);
01103 
01104                   if (key.type == TID_KEY) {
01105                      lseek(hfile, 0, SEEK_END);
01106                      sprintf(str, "#define %s_BANK_DEFINED\n\n", key.name);
01107                      write(hfile, str, strlen(str));
01108 
01109                      sprintf(str, "%s_BANK", key.name);
01110                      db_save_struct(hDB, hKeyBank, file_name, str, TRUE);
01111                      sprintf(str, "%s_BANK_STR", key.name);
01112                      db_save_string(hDB, hKeyBank, file_name, str, TRUE);
01113                   }
01114                }
01115             }
01116          }
01117 
01118          /* Scan sub tree for that equipment */
01119          for (subindex = 0;; subindex++) {
01120             status = db_enum_key(hDB, hKeyEq, subindex, &hDefKey);
01121             if (status == DB_NO_MORE_SUBKEYS)
01122                break;
01123 
01124             db_get_key(hDB, hDefKey, &key);
01125             strcpy(subeq_name, key.name);
01126             name2c(subeq_name);
01127 
01128             for (i = 0; i < (int) strlen(subeq_name); i++)
01129                subeq_name[i] = toupper(subeq_name[i]);
01130 
01131             /* Skip only the statistics */
01132             if (!equal_ustring(subeq_name, "statistics") &&
01133                 !equal_ustring(subeq_name, "variables")) {
01134                lseek(hfile, 0, SEEK_END);
01135                sprintf(str, "#define %s_%s_DEFINED\n\n", eq_name, subeq_name);
01136                write(hfile, str, strlen(str));
01137 
01138                sprintf(str, "%s_%s", eq_name, subeq_name);
01139                db_save_struct(hDB, hDefKey, file_name, str, TRUE);
01140                sprintf(str, "%s_%s_STR", eq_name, subeq_name);
01141                db_save_string(hDB, hDefKey, file_name, str, TRUE);
01142             }
01143          }
01144 
01145          lseek(hfile, 0, SEEK_END);
01146          sprintf(str, "#endif\n\n");
01147          write(hfile, str, strlen(str));
01148       }
01149 
01150    close(hfile);
01151 
01152    getcwd(str, sizeof(str));
01153    printf("\"experim.h\" has been written to %s\n", str);
01154 }

void ctrlc_odbedit ( INT  i  ) 

Definition at line 2676 of file odbedit.c.

Referenced by main().

02677 {
02678    /* reset terminal */
02679    ss_getchar(1);
02680 
02681    /* no shutdown message */
02682    cm_set_msg_print(MT_ERROR, 0, NULL);
02683 
02684    cm_disconnect_experiment();
02685 
02686    exit(EXIT_SUCCESS);
02687 }

void del_tree ( HNDLE  hDB,
HNDLE  hKey,
INT  level 
)

Definition at line 933 of file odbedit.c.

00934 {
00935    INT i;
00936    KEY key;
00937    HNDLE hSubkey;
00938 
00939    for (i = 0;; i++) {
00940       db_enum_link(hDB, hKey, i, &hSubkey);
00941 
00942       if (!hSubkey)
00943          break;
00944 
00945       db_get_key(hDB, hSubkey, &key);
00946       if (key.type == TID_KEY)
00947          del_tree(hDB, hSubkey, level + 1);
00948 
00949       if (rand() < RAND_MAX / 10)
00950          db_delete_key(hDB, hSubkey, 0);
00951    }
00952 }

void key_update ( HNDLE  hDB,
HNDLE  hkey,
void *  info 
)

Definition at line 62 of file odbedit.c.

Referenced by command_loop().

00063 {
00064    key_modified = TRUE;
00065 }

int main ( int  argc,
char *  argv[] 
)

Definition at line 2694 of file odbedit.c.

02696 {
02697    INT status, i, odb_size, size;
02698    char host_name[HOST_NAME_LENGTH], exp_name[NAME_LENGTH];
02699    char cmd[2000], dir[256], str[2000];
02700    BOOL debug;
02701    BOOL corrupted;
02702    HNDLE hDB;
02703 
02704    cmd[0] = dir[0] = 0;
02705    odb_size = DEFAULT_ODB_SIZE;
02706    debug = corrupted = cmd_mode = FALSE;
02707 
02708 #ifdef OS_VXWORKS
02709    strcpy(host_name, ahost_name);
02710    strcpy(exp_name, aexp_name);
02711 #else
02712 
02713    /* get default from environment */
02714    cm_get_environment(host_name, sizeof(host_name), exp_name, sizeof(exp_name));
02715 
02716    /* check for MIDASSYS */
02717    if (!getenv("MIDASSYS")) {
02718       puts("Please define environment variable 'MIDASSYS'");
02719       puts("pointing to the midas installation directory.");
02720    }
02721 
02722    /* parse command line parameters */
02723    for (i = 1; i < argc; i++) {
02724       if (argv[i][0] == '-' && argv[i][1] == 'g')
02725          debug = TRUE;
02726       else if (argv[i][0] == '-' && argv[i][1] == 'C')
02727          corrupted = TRUE;
02728       else if (argv[i][0] == '-') {
02729          if (i + 1 >= argc || argv[i + 1][0] == '-')
02730             goto usage;
02731          if (argv[i][1] == 'e')
02732             strcpy(exp_name, argv[++i]);
02733          else if (argv[i][1] == 'h')
02734             strcpy(host_name, argv[++i]);
02735          else if (argv[i][1] == 'c') {
02736             if (strlen(argv[i+1]) >= sizeof(cmd)) {
02737                printf("error: command line too long (>%d bytes)\n", (int)sizeof(cmd));
02738                return 0;
02739             }
02740             strlcpy(cmd, argv[++i], sizeof(cmd));
02741             cmd_mode = TRUE;
02742          } else if (argv[i][1] == 'd')
02743             strlcpy(dir, argv[++i], sizeof(dir));
02744          else if (argv[i][1] == 's')
02745             odb_size = atoi(argv[++i]);
02746          else {
02747           usage:
02748             printf("usage: odbedit [-h Hostname] [-e Experiment] [-d ODB Subtree]\n");
02749             printf("               [-c Command] [-c @CommandFile] [-s size]\n");
02750             printf("               [-g (debug)] [-C (connect to corrupted ODB)]\n\n");
02751             printf("For a list of valid commands start odbedit interactively\n");
02752             printf("and type \"help\".\n");
02753             return 0;
02754          }
02755       } else
02756          strlcpy(host_name, argv[i], sizeof(host_name));
02757    }
02758 #endif
02759 
02760    /* no startup message if called with command */
02761    if (cmd[0])
02762       cm_set_msg_print(MT_ERROR, 0, NULL);
02763 
02764    status = cm_connect_experiment1(host_name, exp_name, "ODBEdit", NULL,
02765                                    odb_size, DEFAULT_WATCHDOG_TIMEOUT);
02766    if (status == CM_WRONG_PASSWORD)
02767       return 1;
02768    else if ((status == DB_INVALID_HANDLE) && corrupted) {
02769       cm_get_error(status, str);
02770       puts(str);
02771       printf("ODB is corrupted, connecting anyway...\n");
02772    } else if (status != CM_SUCCESS) {
02773       cm_get_error(status, str);
02774       puts(str);
02775       return 1;
02776    }
02777 
02778    /* place a request for system messages */
02779    cm_msg_register(process_message);
02780 
02781    /* route local messages through print_message */
02782    cm_set_msg_print(MT_ALL, MT_ALL, print_message);
02783 
02784    /* turn off watchdog if in debug mode */
02785    if (debug)
02786       cm_set_watchdog_params(FALSE, 0);
02787 
02788    /* get experiment name */
02789    if (!exp_name[0]) {
02790       cm_get_experiment_database(&hDB, NULL);
02791       size = NAME_LENGTH;
02792       db_get_value(hDB, 0, "/Experiment/Name", exp_name, &size, TID_STRING, TRUE);
02793    }
02794 
02795    /* register Ctrl-C handler */
02796    ss_ctrlc_handler(ctrlc_odbedit);
02797 
02798    /* command loop */
02799    status = command_loop(host_name, exp_name, cmd, dir);
02800 
02801    /* no shutdown message if called with command */
02802    if (cmd_mode)
02803       cm_set_msg_print(MT_ERROR, 0, NULL);
02804 
02805    cm_disconnect_experiment();
02806 
02807    if (status != 1)
02808       return EXIT_FAILURE;
02809 
02810    return EXIT_SUCCESS;
02811 }

BOOL match ( char *  pat,
char *  str 
)

Definition at line 195 of file odbedit.c.

Referenced by cmd_dir(), command_loop(), SqlHistory::hs_read_old_style(), print_key(), and search_key().

00197 {
00198    if (!*str)
00199       return *pat == '*' ? match(pat + 1, str) : !*pat;
00200 
00201    switch (*pat) {
00202    case '\0':
00203       return 0;
00204    case '*':
00205       return match(pat + 1, str) || match(pat, str + 1);
00206    case '?':
00207       return match(pat + 1, str + 1);
00208    default:
00209       return (toupper(*pat) == toupper(*str)) && match(pat + 1, str + 1);
00210    }
00211 }

void print_help ( char *  command  ) 

Definition at line 69 of file odbedit.c.

Referenced by command_loop().

00070 {
00071 #ifndef OS_MSDOS                /* save some DGROUP memory under MS-DOS */
00072    if (!command[0]) {
00073       printf("Database commands ([] are options, <> are placeholders):\n\n");
00074       printf("alarm                   - reset all alarms\n");
00075       printf("cd <dir>                - change current directory\n");
00076       printf("chat                    - enter chat mode\n");
00077       printf("chmod <mode> <key>      - change access mode of a key\n");
00078       printf("                          1=read | 2=write | 4=delete\n");
00079       printf("cleanup [client] [-f]   - delete hanging clients [force]\n");
00080       printf("copy <src> <dest>       - copy a subtree to a new location\n");
00081       printf("create <type> <key>     - create a key of a certain type\n");
00082       printf("create <type> <key>[n]  - create an array of size [n]\n");
00083       printf("create string <key>[n][m]  - create an array of [n] strings of [m] characters\n");
00084       printf("del/rm [-l] [-f] <key>  - delete a key and its subkeys\n");
00085       printf("  -l                      follow links\n");
00086       printf("  -f                      force deletion without asking\n");
00087       printf
00088           ("exec <key>/<cmd>        - execute shell command (stored in key) on server\n");
00089       printf("exp <key> <filename>    - import key into ASCII file\n");
00090       printf("find <pattern>          - find a key with wildcard pattern\n");
00091       printf("help/? [command]        - print this help [for a specific command]\n");
00092       printf("hi [analyzer] [id]      - tell analyzer to clear histos\n");
00093       printf("imp <filename> [key]    - import ASCII file into string key\n");
00094       printf("ln <source> <linkname>  - create a link to <source> key\n");
00095       printf
00096           ("load <file>             - load database from .ODB file at current position\n");
00097       printf("-- hit return for more --\r");
00098       getchar();
00099       printf("ls/dir [-lhvrp] [<pat>] - show database entries which match pattern\n");
00100       printf("  -l                      detailed info\n");
00101       printf("  -h                      hex format\n");
00102       printf("  -v                      only value\n");
00103       printf("  -r                      show database entries recursively\n");
00104       printf("  -p                      pause between screens\n");
00105       printf("make [analyzer name]    - create experim.h\n");
00106       printf("mem [-v]                - show memeory usage [verbose]\n");
00107       printf("mkdir <subdir>          - make new <subdir>\n");
00108       printf("move <key> [top/bottom/[n]] - move key to position in keylist\n");
00109       printf("msg [type] [user] <msg> - compose user message\n");
00110       printf("old [n]                 - display old n messages\n");
00111       printf("passwd                  - change MIDAS password\n");
00112       printf("pause                   - pause current run\n");
00113       printf("pwd                     - show current directory\n");
00114       printf("resume                  - resume current run\n");
00115       printf("rename <old> <new>      - rename key\n");
00116       printf("rewind [channel]        - rewind tapes in logger\n");
00117       printf("-- hit return for more --\r");
00118       getchar();
00119       printf("save [-c -s -x -cs] <file>  - save database at current position\n");
00120       printf("                          in ASCII format\n");
00121       printf("  -c                      as a C structure\n");
00122       printf("  -s                      as a #define'd string\n");
00123       printf("  -x                      as a XML file\n");
00124       printf("set <key> <value>       - set the value of a key\n");
00125       printf("set <key>[i] <value>    - set the value of index i\n");
00126       printf("set <key>[*] <value>    - set the value of all indices of a key\n");
00127       printf("set <key>[i..j] <value> - set the value of all indices i..j\n");
00128       printf("scl [-w]                - show all active clients [with watchdog info]\n");
00129       printf("shutdown <client>/all   - shutdown individual or all clients\n");
00130       printf("sor                     - show open records in current subtree\n");
00131       printf("start [number][now][-v] - start a run [with a specific number],\n");
00132       printf
00133           ("                          [now] w/o asking parameters, [-v] debug output\n");
00134       printf("stop [-v]               - stop current run, [-v] debug output\n");
00135       printf("trunc <key> <index>     - truncate key to [index] values\n");
00136       printf("ver                     - show MIDAS library version\n");
00137       printf("webpasswd               - change WWW password for mhttpd\n");
00138       printf("wait <key>              - wait for key to get modified\n");
00139 
00140       printf("\nquit/exit               - exit\n");
00141       return;
00142    }
00143 
00144    if (equal_ustring(command, "cd")) {
00145       printf("cd <dir> - change current directory. Use \"cd /\" to change to the root\n");
00146       printf("           of the ODB, \"cd ..\" to change to the parent directory.\n");
00147    } else if (equal_ustring(command, "chat")) {
00148       printf("chat - enter chat mode. In this mode, users can \"talk\" to each other.\n");
00149       printf
00150           ("       Each user running ODBEdit connected to the same experiment can see\n");
00151       printf
00152           ("       the other messages and each user running ODBEdit in chat mode can\n");
00153       printf
00154           ("       produce messages. All messages are logged in the MIDAS logging file.\n");
00155    } else
00156       printf("No specific help available for command \"%s\".\n", command);
00157 
00158 #endif
00159 }

INT print_key ( HNDLE  hDB,
HNDLE  hKey,
KEY pkey,
INT  level,
void *  info 
)

Definition at line 249 of file odbedit.c.

Referenced by command_loop().

00250 {
00251    INT i, size, status;
00252    static char data_str[50000], line[256];
00253    DWORD delta;
00254    PRINT_INFO *pi;
00255    KEY key;
00256 
00257    if (ls_abort)
00258       return 0;
00259 
00260    pi = (PRINT_INFO *) info;
00261    memcpy(&key, pkey, sizeof(KEY));
00262 
00263    /* if pattern set, check if match */
00264    if (pi->pattern[0] && !match(pi->pattern, pkey->name))
00265       return SUCCESS;
00266 
00267    size = sizeof(data);
00268    if (pi->flags & PI_VALUE) {
00269       /* only print value */
00270       if (key.type != TID_KEY) {
00271          status = db_get_link_data(hDB, hKey, data, &size, key.type);
00272          data_str[0] = 0;
00273 
00274          /* resolve links */
00275          if (key.type == TID_LINK) {
00276             if (strlen(data) > 0 && data[strlen(data)-1] == ']')
00277                status = DB_SUCCESS;
00278             else
00279                status = db_find_key(hDB, 0, data, &hKey);
00280             if (status == DB_SUCCESS) {
00281                status = db_get_key(hDB, hKey, &key);
00282                if (status == DB_SUCCESS) {
00283                   size = sizeof(data);
00284                   if (key.type != TID_KEY)
00285                      status = db_get_data(hDB, hKey, data, &size, key.type);
00286                   else
00287                      status = DB_TYPE_MISMATCH;
00288                }
00289             }
00290          }
00291 
00292          if (status == DB_NO_KEY)
00293             strcat(data_str, "<cannot resolve link>");
00294          else if (status == DB_NO_ACCESS)
00295             strcat(data_str, "<no read access>");
00296          else
00297             for (i = 0; i < key.num_values; i++) {
00298                if (pi->flags & PI_HEX)
00299                   db_sprintfh(data_str, data, key.item_size, i, key.type);
00300                else
00301                   db_sprintf(data_str, data, key.item_size, i, key.type);
00302 
00303                if ((pi->index != -1 && i == pi->index) || pi->index == -1)
00304                   printf("%s\n", data_str);
00305                if (check_abort(pi->flags, ls_line++))
00306                   return 0;
00307             }
00308       }
00309    } else {
00310       /* print key name with value */
00311       memset(line, ' ', sizeof(line));
00312       line[sizeof(line)-1] = 0;
00313       sprintf(line + level * 4, "%s", key.name);
00314       if (key.type == TID_LINK) {
00315          db_get_link_data(hDB, hKey, data, &size, key.type);
00316          sprintf(line + strlen(line), " -> %s", data);
00317          if (pi->index != -1)
00318             sprintf(line + strlen(line), "[%d]", pi->index);
00319          if (strlen(line) >= 32) {
00320             printf("%s\n", line);
00321             memset(line, ' ', sizeof(line));
00322             line[80] = 0;
00323          } else
00324             line[strlen(line)] = ' ';
00325       } else {
00326          if (pi->index != -1)
00327             sprintf(line + strlen(line), "[%d]", pi->index);
00328          line[strlen(line)] = ' ';
00329       }
00330 
00331       if (key.type == TID_KEY) {
00332          if (pi->flags & PI_LONG)
00333             sprintf(line + 32, "DIR");
00334          else
00335             line[32] = 0;
00336          printf("%s\n", line);
00337          if (check_abort(pi->flags, ls_line++))
00338             return 0;
00339       } else {
00340 
00341          status = db_get_link_data(hDB, hKey, data, &size, key.type);
00342          data_str[0] = 0;
00343 
00344          /* resolve links */
00345          if (key.type == TID_LINK) {
00346             if (strlen(data) > 0 && data[strlen(data)-1] == ']')
00347                status = DB_SUCCESS;
00348             else
00349                status = db_find_key(hDB, 0, data, &hKey);
00350             if (status == DB_SUCCESS) {
00351                status = db_get_key(hDB, hKey, &key);
00352                if (status == DB_SUCCESS) {
00353                   size = sizeof(data);
00354                   if (key.type != TID_KEY)
00355                      status = db_get_data(hDB, hKey, data, &size, key.type);
00356                   else
00357                      status = DB_TYPE_MISMATCH;
00358                }
00359             }
00360          }
00361 
00362          if (status == DB_TYPE_MISMATCH)
00363             strcat(data_str, "<subdirectory>");
00364          else if (status == DB_NO_KEY || status == DB_INVALID_LINK)
00365             strcat(data_str, "<cannot resolve link>");
00366          else if (status == DB_NO_ACCESS)
00367             strcat(data_str, "<no read access>");
00368          else {
00369             if (pi->flags & PI_HEX)
00370                db_sprintfh(data_str+strlen(data_str), data, key.item_size, 0, key.type);
00371             else
00372                db_sprintf(data_str+strlen(data_str), data, key.item_size, 0, key.type);
00373          }
00374 
00375          if (pi->flags & PI_LONG) {
00376             sprintf(line + 32, "%s", rpc_tid_name(key.type));
00377             line[strlen(line)] = ' ';
00378             sprintf(line + 40, "%d", key.num_values);
00379             line[strlen(line)] = ' ';
00380             sprintf(line + 46, "%d", key.item_size);
00381             line[strlen(line)] = ' ';
00382 
00383             db_get_key_time(hDB, hKey, &delta);
00384             if (delta < 60)
00385                sprintf(line + 52, "%ds", delta);
00386             else if (delta < 3600)
00387                sprintf(line + 52, "%1.0lfm", delta / 60.0);
00388             else if (delta < 86400)
00389                sprintf(line + 52, "%1.0lfh", delta / 3600.0);
00390             else if (delta < 86400 * 99)
00391                sprintf(line + 52, "%1.0lfh", delta / 86400.0);
00392             else
00393                sprintf(line + 52, ">99d");
00394             line[strlen(line)] = ' ';
00395 
00396             sprintf(line + 57, "%d", key.notify_count);
00397             line[strlen(line)] = ' ';
00398 
00399             if (key.access_mode & MODE_READ)
00400                line[61] = 'R';
00401             if (key.access_mode & MODE_WRITE)
00402                line[62] = 'W';
00403             if (key.access_mode & MODE_DELETE)
00404                line[63] = 'D';
00405             if (key.access_mode & MODE_EXCLUSIVE)
00406                line[64] = 'E';
00407 
00408             if (key.type == TID_STRING && strchr(data_str, '\n'))
00409                strcpy(line + 66, "<multi-line>");
00410             else if (key.num_values == 1)
00411                strcpy(line + 66, data_str);
00412             else
00413                line[66] = 0;
00414          } else if (key.num_values == 1)
00415             if (key.type == TID_STRING && strchr(data_str, '\n'))
00416                strcpy(line + 32, "<multi-line>");
00417             else
00418                strcpy(line + 32, data_str);
00419          else
00420             while (line[strlen(line)-1] == ' ')
00421                line[strlen(line)-1] = 0;
00422 
00423          if (line[0])
00424             printf("%s\n", line);
00425 
00426          if (key.type == TID_STRING && strchr(data_str, '\n'))
00427             puts(data_str);
00428 
00429          if (check_abort(pi->flags, ls_line++))
00430             return 0;
00431 
00432          if (key.num_values > 1) {
00433             for (i = 0; i < key.num_values; i++) {
00434                if (pi->flags & PI_HEX)
00435                   db_sprintfh(data_str, data, key.item_size, i, key.type);
00436                else
00437                   db_sprintf(data_str, data, key.item_size, i, key.type);
00438 
00439                memset(line, ' ', 80);
00440                line[80] = 0;
00441 
00442                if (pi->flags & PI_LONG) {
00443                   sprintf(line + 40, "[%d]", i);
00444                   line[strlen(line)] = ' ';
00445 
00446                   strcpy(line + 56, data_str);
00447                } else
00448                   strcpy(line + 32, data_str);
00449 
00450                if ((pi->index != -1 && i == pi->index) || pi->index == -1)
00451                   printf("%s\n", line);
00452 
00453                if (check_abort(pi->flags, ls_line++))
00454                   return 0;
00455             }
00456          }
00457       }
00458    }
00459 
00460    return SUCCESS;
00461 }

int print_message ( const char *  msg  ) 

Definition at line 182 of file odbedit.c.

Referenced by banks_changed(), main(), and receive_message().

00183 {
00184    if (in_cmd_edit)
00185       printf("\r%s\n", msg);
00186    else
00187       printf("%s\n", msg);
00188 
00189    need_redraw = TRUE;
00190    return 0;
00191 }

void process_message ( HNDLE  hBuf,
HNDLE  id,
EVENT_HEADER pheader,
void *  message 
)

Definition at line 163 of file odbedit.c.

Referenced by main().

00164 {
00165    time_t tm;
00166    char str[80];
00167 
00168    /* prepare time */
00169    time(&tm);
00170    strcpy(str, ctime(&tm));
00171    str[19] = 0;
00172 
00173    /* print message text which comes after event header */
00174    if (in_cmd_edit)
00175       printf("\r%s %s\n", str + 11, (char *) message);
00176    else
00177       printf("\n%s %s\n", str + 11, (char *) message);
00178 
00179    need_redraw = TRUE;
00180 }

void scan_tree ( HNDLE  hDB,
HNDLE  hKey,
INT total_size_key,
INT total_size_data,
INT  level,
INT  flags 
)

Definition at line 501 of file odbedit.c.

00503 {
00504    INT i, j, size, status;
00505    KEY key;
00506    HNDLE hSubkey;
00507    static char data_str[256], line[256];
00508    DWORD delta;
00509 
00510    if (cm_is_ctrlc_pressed()) {
00511       if (level == 0)
00512          cm_ack_ctrlc_pressed();
00513       return;
00514    }
00515 
00516    db_get_key(hDB, hKey, &key);
00517    size = sizeof(data);
00518 
00519    *total_size_key += ALIGN8(sizeof(KEY));
00520    if (key.type == TID_KEY)
00521       *total_size_key += ALIGN8(sizeof(KEYLIST));
00522    else
00523       *total_size_data += ALIGN8(key.total_size);
00524 
00525    if (flags & 0x4) {
00526       /* only print value */
00527       if (key.type != TID_KEY) {
00528          status = db_get_data(hDB, hKey, data, &size, key.type);
00529          if (status == DB_NO_ACCESS)
00530             strcpy(data_str, "<no read access>");
00531          else
00532             for (j = 0; j < key.num_values; j++) {
00533                if (flags & 0x8)
00534                   db_sprintfh(data_str, data, key.item_size, j, key.type);
00535                else
00536                   db_sprintf(data_str, data, key.item_size, j, key.type);
00537                printf("%s\n", data_str);
00538             }
00539       }
00540    } else {
00541       /* print key name with value */
00542       memset(line, ' ', 80);
00543       line[80] = 0;
00544       sprintf(line + level * 4, "%s", key.name);
00545       line[strlen(line)] = ' ';
00546 
00547       if (key.type == TID_KEY) {
00548          line[32] = 0;
00549          printf("%s\n", line);
00550       } else {
00551          status = db_get_data(hDB, hKey, data, &size, key.type);
00552          if (status == DB_NO_ACCESS)
00553             strcpy(data_str, "<no read access>");
00554          else {
00555             if (flags & 0x8)
00556                db_sprintfh(data_str, data, key.item_size, 0, key.type);
00557             else
00558                db_sprintf(data_str, data, key.item_size, 0, key.type);
00559          }
00560 
00561          if (flags & 0x1) {
00562             sprintf(line + 32, "%s", rpc_tid_name(key.type));
00563             line[strlen(line)] = ' ';
00564             sprintf(line + 40, "%d", key.num_values);
00565             line[strlen(line)] = ' ';
00566             sprintf(line + 46, "%d", key.item_size);
00567             line[strlen(line)] = ' ';
00568 
00569             db_get_key_time(hDB, hKey, &delta);
00570             if (delta < 60)
00571                sprintf(line + 52, "%ds", delta);
00572             else if (delta < 3600)
00573                sprintf(line + 52, "%1.0lfm", delta / 60.0);
00574             else if (delta < 86400)
00575                sprintf(line + 52, "%1.0lfh", delta / 3600.0);
00576             else if (delta < 86400 * 99)
00577                sprintf(line + 52, "%1.0lfh", delta / 86400.0);
00578             else
00579                sprintf(line + 52, ">99d");
00580             line[strlen(line)] = ' ';
00581 
00582             sprintf(line + 57, "%d", key.notify_count);
00583             line[strlen(line)] = ' ';
00584 
00585             if (key.access_mode & MODE_READ)
00586                line[61] = 'R';
00587             if (key.access_mode & MODE_WRITE)
00588                line[62] = 'W';
00589             if (key.access_mode & MODE_DELETE)
00590                line[63] = 'D';
00591             if (key.access_mode & MODE_EXCLUSIVE)
00592                line[64] = 'E';
00593 
00594             if (key.num_values == 1)
00595                strcpy(line + 66, data_str);
00596             else
00597                line[66] = 0;
00598          } else if (key.num_values == 1)
00599             strcpy(line + 32, data_str);
00600          else
00601             line[32] = 0;
00602 
00603          printf("%s\n", line);
00604 
00605          if (key.num_values > 1) {
00606             for (j = 0; j < key.num_values; j++) {
00607                if (flags & 0x8)
00608                   db_sprintfh(data_str, data, key.item_size, j, key.type);
00609                else
00610                   db_sprintf(data_str, data, key.item_size, j, key.type);
00611 
00612                memset(line, ' ', 80);
00613                line[80] = 0;
00614 
00615                if (flags & 0x1) {
00616                   sprintf(line + 40, "[%d]", j);
00617                   line[strlen(line)] = ' ';
00618 
00619                   strcpy(line + 56, data_str);
00620                } else
00621                   strcpy(line + 32, data_str);
00622 
00623                printf("%s\n", line);
00624             }
00625          }
00626       }
00627    }
00628 
00629    /* recurse subtree */
00630    if (key.type == TID_KEY && (flags & 0x2)) {
00631       for (i = 0;; i++) {
00632          db_enum_link(hDB, hKey, i, &hSubkey);
00633 
00634          if (!hSubkey)
00635             break;
00636 
00637          scan_tree(hDB, hSubkey, total_size_key, total_size_data, level + 1, flags);
00638 
00639          if (cm_is_ctrlc_pressed()) {
00640             if (level == 0)
00641                cm_ack_ctrlc_pressed();
00642             return;
00643          }
00644       }
00645    }
00646 }

INT search_key ( HNDLE  hDB,
HNDLE  hKey,
KEY key,
INT  level,
void *  info 
)

Definition at line 890 of file odbedit.c.

Referenced by command_loop().

00891 {
00892    INT i, size, status;
00893    char *pattern;
00894    static char data_str[MAX_ODB_PATH], line[MAX_ODB_PATH];
00895    char path[MAX_ODB_PATH];
00896 
00897    pattern = (char *) info;
00898 
00899    if (match(pattern, key->name)) {
00900       db_get_path(hDB, hKey, path, MAX_ODB_PATH);
00901       strcpy(line, path);
00902       strcat(line, " : ");
00903 
00904       if (key->type != TID_KEY) {
00905          size = sizeof(data);
00906          status = db_get_data(hDB, hKey, data, &size, key->type);
00907 
00908          if (status == DB_NO_ACCESS)
00909             strcpy(data_str, "<no read access>");
00910          else
00911             db_sprintf(data_str, data, key->item_size, 0, key->type);
00912 
00913          if (key->num_values == 1)
00914             strcat(line, data_str);
00915 
00916          printf("%s\n", line);
00917 
00918          if (key->num_values > 1)
00919             for (i = 0; i < key->num_values; i++) {
00920                db_sprintf(data_str, data, key->item_size, i, key->type);
00921 
00922                printf("  [%d] : %s\n", i, data_str);
00923             }
00924       } else
00925          printf("%s\n", line);
00926    }
00927 
00928    return SUCCESS;
00929 }

void set_key ( HNDLE  hDB,
HNDLE  hKey,
int  index1,
int  index2,
char *  value 
)

Definition at line 465 of file odbedit.c.

Referenced by command_loop().

00466 {
00467    KEY key;
00468    char data[1000];
00469    int i, size, status = 0;
00470 
00471    db_get_link(hDB, hKey, &key);
00472 
00473    memset(data, 0, sizeof(data));
00474    db_sscanf(value, data, &size, 0, key.type);
00475 
00476    /* extend data size for single string if necessary */
00477    if ((key.type == TID_STRING || key.type == TID_LINK)
00478        && (int) strlen(data) + 1 > key.item_size && key.num_values == 1)
00479       key.item_size = strlen(data) + 1;
00480 
00481    if (key.item_size == 0)
00482       key.item_size = rpc_tid_size(key.type);
00483 
00484    if (key.num_values > 1 && index1 == -1) {
00485       for (i = 0; i < key.num_values; i++)
00486          status = db_set_link_data_index(hDB, hKey, data, key.item_size, i, key.type);
00487    } else if (key.num_values > 1 && index2 > index1) {
00488       for (i = index1; i < key.num_values && i <= index2; i++)
00489          status = db_set_link_data_index(hDB, hKey, data, key.item_size, i, key.type);
00490    } else if (key.num_values > 1 || index1 > 0)
00491       status = db_set_link_data_index(hDB, hKey, data, key.item_size, index1, key.type);
00492    else
00493       status = db_set_link_data(hDB, hKey, data, key.item_size, 1, key.type);
00494 
00495    if (status == DB_NO_ACCESS)
00496       printf("Write access not allowed\n");
00497 }

INT thread ( void *  p  ) 

Definition at line 46 of file odbedit.c.

Referenced by command_loop(), and el_submit().

00047 {
00048    do {
00049       ss_sleep(1000);
00050       ss_mutex_wait_for(tm, 10000);
00051       printf("%d in critical section\n", ss_gettid());
00052       ss_sleep(3000);
00053       printf("%d out of critical section\n", ss_gettid());
00054       ss_mutex_release(tm);
00055    } while (1);
00056 }


Variable Documentation

BOOL cmd_mode

Definition at line 21 of file odbedit.c.

Referenced by command_loop(), and main().

char data[50000] [static]

Definition at line 27 of file odbedit.c.

BOOL in_cmd_edit

Definition at line 19 of file odbedit.c.

Referenced by command_loop().

BOOL key_modified

Definition at line 60 of file odbedit.c.

Referenced by command_loop().

int ls_abort

Definition at line 215 of file odbedit.c.

Referenced by command_loop().

int ls_line

Definition at line 215 of file odbedit.c.

Referenced by command_loop().

BOOL need_redraw

Definition at line 18 of file odbedit.c.

Referenced by cmd_idle().

char pwd[256]

Definition at line 20 of file odbedit.c.

Referenced by cm_set_client_info(), cmd_dir(), command_loop(), and show_custom_page().

MUTEX_T* tm

Definition at line 42 of file odbedit.c.

Referenced by cm_msg_log(), cm_msg_log1(), cm_msg_retrieve(), command_loop(), el_submit(), export_hist(), hs_dump(), interprete(), log_generate_file_name(), receive_message(), sec_to_label(), send_css(), send_icon(), send_js(), sendmail(), show_elog_page(), show_elog_query(), show_elog_submit_query(), show_hist_page(), show_query_page(), show_status_page(), taxis(), and tr_start().


Midas DOC Version 3.0.0 ---- PSI Stefan Ritt ----
Contributions: Pierre-Andre Amaudruz - Sergio Ballestrero - Suzannah Daviel - Doxygen - Peter Green - Qing Gu - Greg Hackman - Gertjan Hofman - Paul Knowles - Exaos Lee - Rudi Meier - Glenn Moloney - Dave Morris - John M O'Donnell - Konstantin Olchanski - Renee Poutissou - Tamsen Schurman - Andreas Suter - Jan M.Wouters - Piotr Adam Zolnierczuk