Commit 24c5f876f3a3d2233d0880c01e299e7e446177a3

Authored by perry.werneck@gmail.com
1 parent cdb5a8ba

Simplificando tratamento de macros

Showing 2 changed files with 70 additions and 19 deletions   Show diff stats
src/lib3270/macros.c
... ... @@ -37,11 +37,22 @@
37 37 #include <lib3270.h>
38 38 #include <lib3270/macros.h>
39 39 #include <stdlib.h>
  40 + #include <strings.h>
  41 + #include "globals.h"
40 42 #include "api.h"
41 43  
  44 +/*--[ Structs & Defines ]----------------------------------------------------------------------------*/
  45 +
  46 + struct macro_list
  47 + {
  48 + const char *name;
  49 + char *(*exec)(H3270 *session, int argc, const char **argv);
  50 + };
  51 +
  52 +/*
42 53 #define LIB3270_MACRO_ENTRY( name ) { #name, lib3270_macro_ ## name }
43 54  
44   - static const LIB3270_MACRO_LIST macro_list[] =
  55 + static const struct cmd[] =
45 56 {
46 57 LIB3270_MACRO_ENTRY( connect ),
47 58 LIB3270_MACRO_ENTRY( cstate ),
... ... @@ -62,6 +73,9 @@
62 73 {
63 74 return macro_list;
64 75 }
  76 +*/
  77 +
  78 +/*--[ Implement ]------------------------------------------------------------------------------------*/
65 79  
66 80 static const char * get_state(H3270 *h)
67 81 {
... ... @@ -289,3 +303,47 @@
289 303 lib3270_disconnect(hSession);
290 304 return strdup("0");
291 305 }
  306 +
  307 +
  308 +/*--[ Macro entry point ]----------------------------------------------------------------------------*/
  309 +
  310 + LIB3270_EXPORT char * lib3270_run_macro(H3270 *session, const char **argv)
  311 + {
  312 + #define LIB3270_MACRO_ENTRY( name ) { #name, lib3270_macro_ ## name }
  313 +
  314 + static const struct macro_list cmd[] =
  315 + {
  316 + LIB3270_MACRO_ENTRY( connect ),
  317 + LIB3270_MACRO_ENTRY( cstate ),
  318 + LIB3270_MACRO_ENTRY( disconnect ),
  319 + LIB3270_MACRO_ENTRY( encoding ),
  320 + LIB3270_MACRO_ENTRY( enter ),
  321 + LIB3270_MACRO_ENTRY( get ),
  322 + LIB3270_MACRO_ENTRY( luname ),
  323 + LIB3270_MACRO_ENTRY( pa ),
  324 + LIB3270_MACRO_ENTRY( pf ),
  325 + LIB3270_MACRO_ENTRY( set ),
  326 + LIB3270_MACRO_ENTRY( status ),
  327 +
  328 + {NULL, NULL}
  329 + };
  330 +
  331 + int argc;
  332 + int f;
  333 +
  334 + CHECK_SESSION_HANDLE(session);
  335 +
  336 + // Get the number of arguments
  337 + for(argc = 0; argv[argc]; argc++);
  338 +
  339 + // Search for macro function
  340 + for(f=0;cmd[f].name;f++)
  341 + {
  342 + if(!strcasecmp(cmd[f].name,argv[0]))
  343 + return cmd[f].exec(session,argc,argv);
  344 + }
  345 +
  346 + // Not found, return NULL
  347 + return NULL;
  348 + }
  349 +
... ...
src/lib3270/testprogram.c
... ... @@ -37,11 +37,11 @@ int main(int numpar, char *param[])
37 37  
38 38 while(fgets(line,4095,stdin))
39 39 {
40   - const LIB3270_MACRO_LIST *cmd = get_3270_calls();
  40 +// const LIB3270_MACRO_LIST *cmd = get_3270_calls();
41 41  
42 42 int f;
43 43 int argc = 0;
44   - const char * argv[MAX_ARGS];
  44 + const char * argv[MAX_ARGS+1];
45 45 char * ptr;
46 46  
47 47 line[4095] = 0; // Just in case.
... ... @@ -58,27 +58,20 @@ int main(int numpar, char *param[])
58 58 if( (argv[argc++] = strtok(NULL," ")) == NULL)
59 59 break;
60 60 }
61   - argc--;
  61 + argv[argc] = NULL;
62 62  
63 63 if(!strcmp(argv[0],"quit"))
64 64 break;
65 65  
66   - for(f=0;cmd[f].name;f++)
  66 + ptr = lib3270_run_macro(h,argv);
  67 + if(ptr)
67 68 {
68   - if(!strcmp(cmd[f].name,argv[0]))
69   - {
70   - char *str = cmd[f].exec(h,argc,argv);
71   - if(str)
72   - {
73   - printf("\n%s\n",str);
74   - lib3270_free(str);
75   - }
76   - else
77   - {
78   - printf("\nNo response\n");
79   - }
80   - break;
81   - }
  69 + printf("\n%s\n",ptr);
  70 + lib3270_free(ptr);
  71 + }
  72 + else
  73 + {
  74 + printf("\nNo response\n");
82 75 }
83 76  
84 77 printf("\n]");
... ...