-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.c
769 lines (683 loc) · 20 KB
/
script.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#include "stdlib.h"
#include "keyboard.h"
#include "platform.h"
#include "core.h"
#include "gui.h"
#include "gui_draw.h"
#include "conf.h"
#include "script.h"
#include "console.h"
#include "action_stack.h"
#include "motion_detector.h"
#include "shot_histogram.h"
#include "lang.h"
#include "gui_lang.h"
#include "kbd.h"
#ifdef OPT_LUA
#include "lauxlib.h"
#endif
//-------------------------------------------------------------------
const char *script_source_str=NULL; //ERR99
char cfg_name[100] = "\0";
char cfg_set_name[100] = "\0";
static const char *ubasic_script_default =
#if 0
"@title Default script\n"
"@param a Shoot count\n"
"@default a 2\n"
"@param b Step\n"
"@default b 3\n"
"sleep 1000\n"
"if a<1 then let a=2\n"
"if b<1 then let b=3\n"
"for s=1 to a\n"
"shoot\n"
"for n=1 to b\n"
"click \"right\"\n"
"next n\n"
"next s\n"
"shoot\n"
"for n=1 to a*b\n"
"click \"left\"\n"
"next n\n"
"end\n";
#else
"@title Default Script\n"
"@param a Times to Repeat\n"
"@default a 5\n"
"@param b Display Delay\n"
"@default b 3\n"
"if a<1 then let a=1\n"
"if b<1 then let b=1\n"
"for n=1 to a\n"
"if n>1 then print \"*****\"\n"
"print \"This is a default script.\"\n"
"sleep 1000\n"
"print \"Scripts run when shutter\"\n"
"sleep 1000\n"
"print \"is pressed in ALT mode.\"\n"
"sleep 1000\n"
"print \"Press shutter to exit.\"\n"
"sleep b*1000\n"
"next n\n"
"end\n";
#endif
char script_title[36];
char script_params[SCRIPT_NUM_PARAMS][28];
int script_param_order[SCRIPT_NUM_PARAMS];
static char script_params_update[SCRIPT_NUM_PARAMS];
static int script_loaded_params[SCRIPT_NUM_PARAMS];
static long running_script_stack_name = -1;
#ifdef OPT_LUA
static int state_lua_kbd_first_call_to_resume; // AUJ
#endif
//-------------------------------------------------------------------
static void process_title(const char *title) {
register const char *ptr = title;
register int i=0;
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
while (i<(sizeof(script_title)-1) && ptr[i] && ptr[i]!='\r' && ptr[i]!='\n') {
script_title[i]=ptr[i];
++i;
}
script_title[i]=0;
}
//-------------------------------------------------------------------
static int process_param(const char *param, int update) {
register const char *ptr = param;
register int n, i=0;
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
if (ptr[0] && (ptr[0]>='a' && ptr[0]<='a'+SCRIPT_NUM_PARAMS) && (ptr[1]==' ' || ptr[1]=='\t')) {
n=ptr[0]-'a';
ptr+=2;
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
script_params_update[n] = 1;
while (i<(sizeof(script_params[0])-1) && ptr[i] && ptr[i]!='\r' && ptr[i]!='\n') {
if (update)
{ if (script_params[n][i]!=ptr[i]) { script_params_update[n] = 0; break; }
} else script_params[n][i]=ptr[i];
++i;
}
if (!update) script_params[n][i]=0;
n++;
} else n=0; // ??? else produce error message
return n; // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
}
//-------------------------------------------------------------------
static void process_default(const char *param, char update) {
register const char *ptr = param;
register int n;
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
if (ptr[0] && (ptr[0]>='a' && ptr[0]<='a'+SCRIPT_NUM_PARAMS) && (ptr[1]==' ' || ptr[1]=='\t')) {
n=ptr[0]-'a';
ptr+=2;
if (!update || script_params_update[n])
{
conf.ubasic_vars[n] = strtol(ptr, NULL, 0);
script_loaded_params[n] = conf.ubasic_vars[n];
}
} // ??? else produce error message
}
//-------------------------------------------------------------------
static void script_scan(const char *fn, int update_vars) {
register const char *ptr = script_source_str;
register int i, j=0, n;
char *c;
c=strrchr(fn, '/');
strncpy(script_title, (c)?c+1:fn, sizeof(script_title));
script_title[sizeof(script_title)-1]=0;
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
script_params[i][0]=0;
script_param_order[i]=0;
}
while (ptr[0]) {
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
if (ptr[0]=='@') {
if (strncmp("@title", ptr, 6)==0) {
ptr+=6;
process_title(ptr);
} else if (strncmp("@param", ptr, 6)==0) {
ptr+=6;
n=process_param(ptr, 0); // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
if (n>0 && n<=SCRIPT_NUM_PARAMS) {
script_param_order[j]=n;
j++;
}
} else if (update_vars && strncmp("@default", ptr, 8)==0) {
ptr+=8;
process_default(ptr, 0);
}
}
while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line
if (ptr[0]) ++ptr;
}
/*for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
if (script_params[i][0]) break;
}
if (i==SCRIPT_NUM_PARAMS) { // there was no @param in script
for (i=0; i<3; ++i) {
strcpy(script_params[i], "Var. ? value");
script_params[i][5]='a'+i;
}
}*/
}
void set_params_values_name(const char *fn, int param_set)
{
int shift;
register char *ptr = (param_set >= 0 ? cfg_set_name : cfg_name);
const char *name;
if (fn == NULL || fn[0] == 0) { ptr[0] = 0; return; }
strncpy(ptr, SCRIPT_DATA_PATH, 100); ptr[99]=0;
shift = strlen(SCRIPT_DATA_PATH);
name = strrchr(fn, '/');
if (name) name++; else name=fn;
strncpy(ptr+shift, name, 100-shift); ptr[99]=0;
shift = strlen(ptr); if (shift >= 100) shift=99;
if (param_set >= 0)
sprintf(ptr+shift-4, "_%d\0", param_set); else
strcpy(ptr+shift-3, "cfg\0");
}
//-------------------------------------------------------------------
int load_params_values(const char *fn, int update_vars, int read_param_set)
{
int i, fd=-1, rcnt;
register const char *ptr;
struct stat st;
char *buf;
if (fn == NULL || fn[0] == 0) return 0;
if (read_param_set)
{
set_params_values_name(fn, -1);
// find param set
fd = open(cfg_name, O_RDONLY, 0777);
if (fd >= 0)
{
buf=umalloc(16);
rcnt = read(fd, buf, 15);
buf[rcnt] = 0;
close(fd);
conf.script_param_set = strtol(buf, NULL, 0);
ufree(buf);
} else conf.script_param_set = 0;
}
set_params_values_name(fn, conf.script_param_set);
if (!update_vars) return 0;
// open and read file
if (stat(cfg_set_name,&st) != 0)
return 0;
buf=umalloc(st.st_size+1);
if(!buf)
return 0;
fd = open(cfg_set_name, O_RDONLY, 0777);
if (fd < 0) {
ufree(buf);
return 0;
}
rcnt = read(fd, buf, st.st_size);
buf[rcnt] = 0;
close(fd);
for(i = 0; i < SCRIPT_NUM_PARAMS; ++i) script_params_update[i]=0;
ptr = buf;
while (ptr[0])
{
while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
if (ptr[0]=='@')
{
if (strncmp("@param", ptr, 6) == 0)
{
ptr+=6;
process_param(ptr, 1);
} else if (strncmp("@default", ptr, 8)==0) {
ptr+=8;
process_default(ptr, 1);
}
}
while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line
if (ptr[0]) ++ptr;
}
ufree(buf);
return 1;
}
//-------------------------------------------------------------------
void save_params_values(int unconditional)
{
int i, n, fd, changed=0;
char *buf,*p;
for(i = 0; i < SCRIPT_NUM_PARAMS; i++)
{
if (script_loaded_params[i] != conf.ubasic_vars[i]) changed++;
script_loaded_params[i] = conf.ubasic_vars[i];
}
if (!unconditional && !changed) return;
if (cfg_name[0] == 0) set_params_values_name(conf.script_file, -1);
fd = open(cfg_name, O_WRONLY|O_CREAT, 0777);
if (fd >= 0)
{
char s[20];
sprintf(s, " %d\n", conf.script_param_set);
write(fd, s, strlen(s));
close(fd);
}
// open and read file
set_params_values_name(conf.script_file, conf.script_param_set);
buf=umalloc(SCRIPT_NUM_PARAMS*(28 + 20)); // max possible params * (param description + some extra for @default etc)
if(!buf)
return;
fd = open(cfg_set_name, O_WRONLY|O_CREAT, 0777);
if (fd < 0) {
ufree(buf);
return;
}
buf[0] = 0;
p=buf;
for(n = 0; n < SCRIPT_NUM_PARAMS; ++n)
{
if (script_params[n][0] != 0)
{
p+=sprintf(p,"@param %c %s\n@default %c %d\n",'a'+n,script_params[n],'a'+n,conf.ubasic_vars[n]);
}
}
write(fd, buf, strlen(buf));
close(fd);
ufree(buf);
}
//-------------------------------------------------------------------
void script_load(const char *fn, int saved_params) {
int i, update_vars;
FILE *fd = NULL;
struct stat st;
// save_params_values(0);
if(script_source_str && script_source_str != ubasic_script_default)
free((void *)script_source_str);
script_source_str = ubasic_script_default;
update_vars = (strcmp(fn, conf.script_file) != 0) || !saved_params || (saved_params == 2); // update if new file
if (!fn[0]) { // load internal script
if (!conf.script_file[0]) { // internal script was used last time
fd = fopen(SCRIPT_DEFAULT_FILENAME, "rb");
if (fd) {
fn = SCRIPT_DEFAULT_FILENAME;
update_vars = 1;
}
}
} else {
fd = fopen(fn, "rb");
if (!fd) {
conf.script_file[0]=0;
update_vars = 1;
}
}
// zero size = default script
if(stat(fn,&st) != 0 || st.st_size == 0) {
conf.script_file[0]=0;
update_vars = 1;
if(fd) {
fclose(fd);
fd=0;
}
}
if (fd){
int rcnt;
char *buf;
buf = malloc(st.st_size+1);
if(!buf) {
fclose(fd);
return;
}
// TODO we could process the script here to reduce size
// or compile for lua
rcnt = fread(buf, 1, st.st_size,fd);
if (rcnt > 0){
buf[rcnt] = 0;
script_source_str = buf;
strcpy(conf.script_file, fn);
}
else {
free(buf);
}
fclose(fd);
}
if (update_vars) {
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
conf.ubasic_vars[i] = 0;
script_loaded_params[i] = 0;
}
}
script_scan(conf.script_file, update_vars);
if (saved_params)
load_params_values(conf.script_file, update_vars, (saved_params!=2));
gui_update_script_submenu();
}
//-------------------------------------------------------------------
static int print_screen_p; // print_screen predicate: 0-off 1-on.
static int print_screen_d = -1; // print_screen file descriptor.
//static const char print_screen_file[] ="A/CHDK/SCRIPTS/PR_SCREEN.TXT";
static const char print_screen_file_prefix[] = "A/CHDK/LOGS/LOG_";
char print_screen_file[25];
static const char extension_txt[] = ".TXT";
void script_print_screen_init()
{
print_screen_p = 0;
if (print_screen_d >= 0) {
close(print_screen_d);
print_screen_d = -1;
}
}
void script_print_screen_end()
{
if (print_screen_d >= 0) {
close(print_screen_d);
print_screen_d = -1;
print_screen_p = 0;
}
}
void script_print_screen_statement(int val)
{
print_screen_p = val;
if (val) {
if (print_screen_d>=0) close(print_screen_d);
int i=0;
int c=0;
static char file_number[5];
strcpy(print_screen_file, print_screen_file_prefix);
if (val<0) val = -val;
while (val > 9999) val += -10000;
for (i=10; i<=1000; i=i*10) { if (val<i) ++c; }
for (i=1; i<=c; ++i) { sprintf(file_number, "%d", 0); strcat(print_screen_file, file_number); }
sprintf(file_number, "%d", val);
strcat(print_screen_file, file_number);
strcat(print_screen_file, extension_txt);
print_screen_d = open(print_screen_file, O_WRONLY|O_CREAT|O_TRUNC, 0777);
}
}
void script_console_add_line(const char *str)
{
console_add_line(str);
if (print_screen_p && (print_screen_d >= 0)) {
char nl = '\n';
// TODO this should be uncached memory
write(print_screen_d, str, strlen(str) );
write(print_screen_d, &nl, 1);
}
}
static int is_lua()
{
int len;
char const* s;
s = conf.script_file;
len = strlen( s );
return len >= 4 && ( s[len-1] == 'a' || s[len-1] == 'A' )
&& ( s[len-2] == 'u' || s[len-2] == 'U' )
&& ( s[len-3] == 'l' || s[len-3] == 'L' )
&& s[len-4] == '.';
}
void script_wait_and_end(void)
{
script_console_add_line("PRESS SHUTTER TO CLOSE");
// We're not running any more, but we have scheduled stuff that
// needs to finish. So keep the script marked as running, but don't
// call any more scripting functions.
state_kbd_script_run = 3;
}
static void process_script()
{ // Note: This function is called from an action stack for AS_SCRIPT_RUN.
long t;
int Lres;
if (state_kbd_script_run != 3) {
#ifdef OPT_LUA
if( L ) {
int top;
if (state_lua_kbd_first_call_to_resume) {
state_lua_kbd_first_call_to_resume = 0;
top = 0;
} else {
top = lua_gettop(Lt);
}
Lres = lua_resume( Lt, top );
if (Lres != LUA_YIELD && Lres != 0) {
lua_script_error(Lt,1);
return;
}
if (Lres != LUA_YIELD) {
// add ptp result
lua_script_finish(Lt);
script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED));
action_pop();
script_end();
}
} else
#endif
{
#ifdef OPT_UBASIC
ubasic_run();
if (ubasic_finished()) {
script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED));
action_pop();
script_end();
}
#endif
}
}
}
static int script_action_stack(long p)
{
// process stack operations
switch (p) {
case AS_SCRIPT_RUN:
if (state_kbd_script_run)
process_script();
else
action_pop();
break;
case AS_MOTION_DETECTOR:
if(md_detect_motion()==0)
{
action_pop();
#ifdef OPT_LUA
if (L)
{
// We need to recover the motion detector's
// result and push
// it onto the thread's stack.
lua_pushnumber( Lt, md_get_result() );
} else
#endif
{
#ifdef OPT_UBASIC
ubasic_set_md_ret(md_get_result());
#endif
}
}
break;
#if defined(OPT_LUA) && defined(CAM_CHDK_PTP)
case AS_SCRIPT_READ_USB_MSG:
if(L) { // only lua supported for now
ptp_script_msg *msg = ptp_script_read_msg();
if(action_process_delay(2) || msg) {
if(msg) {
lua_pushlstring( Lt,msg->data,msg->size);
} else {
lua_pushnil(Lt);
}
action_clear_delay();
action_pop();
action_pop();
}
}
break;
case AS_SCRIPT_WRITE_USB_MSG:
if(L) { // only lua supported for now
ptp_script_msg *msg = (ptp_script_msg *)action_get_prev(2);
int r = ptp_script_write_msg(msg);
if(action_process_delay(3) || r) {
action_clear_delay();
action_pop();
action_pop();
action_pop();
lua_pushboolean(Lt,r);
}
}
break;
#endif
default:
if (!action_stack_standard(p) && !state_kbd_script_run)
{
/*finished();*/
action_pop();
script_end();
}
break;
}
return 1;
}
long script_stack_start()
{
running_script_stack_name = action_stack_create(&script_action_stack, AS_SCRIPT_RUN);
return running_script_stack_name;
}
int script_is_running()
{
return !action_stack_is_finished(running_script_stack_name);
}
void script_end()
{
script_print_screen_end();
#ifdef OPT_LUA
if( L ) {
lua_script_reset();
} else
#endif
{
#ifdef OPT_UBASIC
ubasic_end();
#endif
}
md_close_motion_detector();
shot_histogram_set(0);
kbd_key_release_all();
state_kbd_script_run = 0;
conf_update_prevent_shutdown();
vid_bitmap_refresh();
}
long script_start_gui( int autostart )
{
int i;
shot_histogram_set(0);
if (autostart)
auto_started = 1;
else
auto_started = 0;
kbd_last_clicked = 0;
/*if (!autostart)*/ kbd_key_release_all();
console_clear();
script_print_screen_init();
if (conf.script_param_save) {
save_params_values(0);
}
if( autostart )
script_console_add_line("***Autostart***");
else
script_console_add_line(lang_str(LANG_CONSOLE_TEXT_STARTED));
if( is_lua() ) {
#ifdef OPT_LUA
if( !lua_script_start(script_source_str,0) ) {
return -1;
}
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
if( script_params[i][0] ) {
char var = 'a'+i;
lua_pushlstring( L, &var, 1 );
lua_pushnumber( L, conf.ubasic_vars[i] );
lua_settable( L, LUA_GLOBALSINDEX );
}
}
state_lua_kbd_first_call_to_resume = 1;
#else
char msg[64];
sprintf(msg,lang_str(LANG_CONSOLE_SCRIPT_DISABLED_IN_BUILD),"Lua");
console_add_line(msg);
return -1;
#endif
} else
{ // ubasic
#ifdef OPT_UBASIC
ubasic_init(script_source_str);
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
ubasic_set_variable(i, conf.ubasic_vars[i]);
}
#else
char msg[64];
sprintf(msg,lang_str(LANG_CONSOLE_SCRIPT_DISABLED_IN_BUILD),"UBASIC");
console_add_line(msg);
return -1;
#endif
}
state_kbd_script_run = 1;
conf_update_prevent_shutdown();
return script_stack_start();
}
#if defined(OPT_LUA) && defined(CAM_CHDK_PTP)
long script_start_ptp( char *script )
{
if (!lua_script_start(script,1)) return -1;
state_lua_kbd_first_call_to_resume = 1;
state_kbd_script_run = 1;
kbd_set_block(1);
auto_started = 0;
return script_stack_start();
}
#endif
int script_key_is_pressed(int k)
{
if (k==0xFF)
return get_usb_power(1);
if (k > 0)
return kbd_is_key_pressed(k);
return 0;
}
int script_key_is_clicked(int k)
{
if (k==0xFF)
return get_usb_power(1);
if (k > 0)
return (kbd_last_clicked == k);
return 0;
}
static const struct Keynames {
int keyid;
char *keyname;
} keynames[] = {
{ KEY_UP, "up" },
{ KEY_DOWN, "down" },
{ KEY_LEFT, "left" },
{ KEY_RIGHT, "right" },
{ KEY_SET, "set" },
{ KEY_SHOOT_HALF, "shoot_half" },
{ KEY_SHOOT_FULL, "shoot_full" },
{ KEY_ZOOM_IN, "zoom_in" },
{ KEY_ZOOM_OUT, "zoom_out" },
{ KEY_MENU, "menu" },
{ KEY_DISPLAY, "display" },
{ KEY_PRINT, "print" },
{ KEY_ERASE, "erase" },
{ KEY_ISO, "iso" },
{ KEY_FLASH, "flash" },
{ KEY_MF, "mf" },
{ KEY_MACRO, "macro" },
{ KEY_VIDEO, "video" },
{ KEY_TIMER, "timer" },
{ KEY_EXPO_CORR, "expo_corr" },
{ KEY_MICROPHONE, "fe" },
{ KEY_ZOOM_ASSIST, "zoom_assist"},
{ KEY_AE_LOCK, "ae_lock" },
{ KEY_METERING, "metering_mode"},
{ 0xFF, "remote" },
{ 0xFFFF, "no_key" },
};
int script_keyid_by_name (const char *n)
{
int i;
for (i=0;i<sizeof(keynames)/sizeof(keynames[0]);i++)
if (strcmp(keynames[i].keyname,n) == 0)
return keynames[i].keyid;
return 0;
}