Commit 3b6932ddbd5be57245042224b2fd316289a8ce41

Authored by Perry Werneck
1 parent 36c13ad1

Refactoring "lunames" property.

src/core/host.c
... ... @@ -261,13 +261,6 @@ static void update_url(H3270 *hSession)
261 261  
262 262 }
263 263  
264   -LIB3270_EXPORT int lib3270_set_luname(H3270 *hSession, const char *luname)
265   -{
266   - FAIL_IF_ONLINE(hSession);
267   - strncpy(hSession->lu.names,luname,LIB3270_LUNAME_LENGTH);
268   - return 0;
269   -}
270   -
271 264 LIB3270_EXPORT const char * lib3270_get_associated_luname(const H3270 *hSession)
272 265 {
273 266 if(check_online_session(hSession))
... ...
src/core/properties/string.c
... ... @@ -33,6 +33,7 @@
33 33 #include <lib3270.h>
34 34 #include <lib3270/properties.h>
35 35 #include <lib3270/keyboard.h>
  36 + #include <lib3270/log.h>
36 37  
37 38 static const char * get_version(const H3270 GNUC_UNUSED(*hSession))
38 39 {
... ... @@ -289,3 +290,73 @@ int lib3270_set_string_property(H3270 *hSession, const char *name, const char *
289 290  
290 291 }
291 292  
  293 +/*
  294 +LIB3270_EXPORT int lib3270_set_luname(H3270 *hSession, const char *luname)
  295 +{
  296 + FAIL_IF_ONLINE(hSession);
  297 + strncpy(hSession->lu.names,luname,LIB3270_LUNAME_LENGTH);
  298 + return 0;
  299 +}
  300 +*/
  301 +
  302 +LIB3270_EXPORT int lib3270_set_lunames(H3270 *hSession, const char *lunames)
  303 +{
  304 + FAIL_IF_ONLINE(hSession);
  305 +
  306 + if(hSession->lu.names)
  307 + {
  308 + lib3270_free(hSession->lu.names);
  309 + hSession->lu.names = NULL;
  310 + }
  311 +
  312 + // Do I have lunames to set? If not just return.
  313 + if(!lunames)
  314 + return 0;
  315 +
  316 + //
  317 + // Count the commas in the LU names. That plus one is the
  318 + // number of LUs to try.
  319 + //
  320 + char *comma;
  321 + char *lu;
  322 + int n_lus = 1;
  323 +
  324 + lu = (char *) lunames;
  325 + while ((comma = strchr(lu, ',')) != CN)
  326 + {
  327 + n_lus++;
  328 + lu++;
  329 + }
  330 +
  331 + //
  332 + // Allocate enough memory to construct an argv[] array for
  333 + // the LUs.
  334 + //
  335 + Replace(hSession->lu.names,(char **)lib3270_malloc((n_lus+1) * sizeof(char *) + strlen(lunames) + 1));
  336 +
  337 + // Copy each LU into the array.
  338 + lu = (char *)(hSession->lu.names + n_lus + 1);
  339 + (void) strcpy(lu, lunames);
  340 +
  341 + size_t i = 0;
  342 + do
  343 + {
  344 + hSession->lu.names[i++] = lu;
  345 + comma = strchr(lu, ',');
  346 + if (comma != CN)
  347 + {
  348 + *comma = '\0';
  349 + lu = comma + 1;
  350 + }
  351 + } while (comma != CN);
  352 +
  353 + hSession->lu.names[i] = CN;
  354 +
  355 + return 0;
  356 +}
  357 +
  358 +LIB3270_EXPORT const char ** lib3270_get_lunames(H3270 *hSession)
  359 +{
  360 + return (const char **) hSession->lu.names;
  361 +}
  362 +
... ...
src/core/session.c
... ... @@ -99,6 +99,14 @@ void lib3270_session_free(H3270 *h)
99 99 for(f=0;f<LIB3270_ACTION_GROUP_CUSTOM;f++)
100 100 lib3270_linked_list_free(&h->listeners.actions[f]);
101 101  
  102 + // Release Lu names
  103 + if(h->lu.names)
  104 + {
  105 + lib3270_free(h->lu.names);
  106 + h->lu.names = NULL;
  107 + }
  108 +
  109 +
102 110 // Release memory
103 111 #define release_pointer(x) lib3270_free(x); x = NULL;
104 112  
... ...
src/core/telnet.c
... ... @@ -369,13 +369,26 @@ void popup_a_sockerr(H3270 *hSession, char *fmt, ...)
369 369 // Set up the LU list.
370 370 static void setup_lus(H3270 *hSession)
371 371 {
  372 + hSession->lu.associated = CN;
  373 + hSession->connected_type = CN;
  374 +
  375 + if(hSession->lu.names)
  376 + {
  377 + hSession->lu.curr = hSession->lu.names;
  378 + hSession->lu.try = * hSession->lu.curr;
  379 + }
  380 + else
  381 + {
  382 + hSession->lu.curr = (char **)NULL;
  383 + hSession->lu.try = CN;
  384 + }
  385 +
  386 + /*
372 387 char *lu;
373 388 char *comma;
374 389 int n_lus = 1;
375 390 int i;
376 391  
377   - hSession->lu.associated = CN;
378   - hSession->connected_type = CN;
379 392  
380 393 if (!hSession->lu.names[0])
381 394 {
... ... @@ -419,8 +432,8 @@ static void setup_lus(H3270 *hSession)
419 432 } while (comma != CN);
420 433  
421 434 hSession->lus[i] = CN;
422   - hSession->curr_lu = hSession->lus;
423   - hSession->try_lu = *hSession->curr_lu;
  435 + */
  436 +
424 437 }
425 438  
426 439 static int net_connected(H3270 *hSession)
... ... @@ -775,11 +788,13 @@ static void send_naws(H3270 *hSession)
775 788  
776 789  
777 790  
778   -/* Advance 'try_lu' to the next desired LU name. */
  791 +///
  792 +/// @brief Advance 'try_lu' to the next desired LU name.
  793 +///
779 794 static void next_lu(H3270 *hSession)
780 795 {
781   - if (hSession->curr_lu != (char **)NULL && (hSession->try_lu = *++hSession->curr_lu) == CN)
782   - hSession->curr_lu = (char **)NULL;
  796 + if (hSession->lu.curr != (char **)NULL && (hSession->lu.try = *++hSession->lu.curr) == CN)
  797 + hSession->lu.curr = (char **)NULL;
783 798 }
784 799  
785 800 /*
... ... @@ -1087,7 +1102,7 @@ static int telnet_fsm(H3270 *hSession, unsigned char c)
1087 1102  
1088 1103 trace_dsn(hSession,"%s %s\n", opt(hSession->sbbuf[0]),telquals[hSession->sbbuf[1]]);
1089 1104  
1090   - if (hSession->lus != (char **)NULL && hSession->try_lu == CN)
  1105 + if (hSession->lu.names != (char **)NULL && hSession->lu.try == CN)
1091 1106 {
1092 1107 // None of the LUs worked.
1093 1108 popup_an_error(hSession, _( "Cannot connect to specified LU" ) );
... ... @@ -1095,10 +1110,10 @@ static int telnet_fsm(H3270 *hSession, unsigned char c)
1095 1110 }
1096 1111  
1097 1112 tt_len = strlen(hSession->termtype);
1098   - if (hSession->try_lu != CN && *hSession->try_lu)
  1113 + if (hSession->lu.try != CN && *hSession->lu.try)
1099 1114 {
1100   - tt_len += strlen(hSession->try_lu) + 1;
1101   - hSession->lu.associated = hSession->try_lu;
  1115 + tt_len += strlen(hSession->lu.try) + 1;
  1116 + hSession->lu.associated = hSession->lu.try;
1102 1117 }
1103 1118 else
1104 1119 {
... ... @@ -1112,8 +1127,8 @@ static int telnet_fsm(H3270 *hSession, unsigned char c)
1112 1127 (void) sprintf(tt_out, "%c%c%c%c%s%s%s%c%c",
1113 1128 IAC, SB, TELOPT_TTYPE, TELQUAL_IS,
1114 1129 hSession->termtype,
1115   - (hSession->try_lu != CN && *hSession->try_lu) ? "@" : "",
1116   - (hSession->try_lu != CN && *hSession->try_lu) ? hSession->try_lu : "",
  1130 + (hSession->lu.try != CN && *hSession->lu.try) ? "@" : "",
  1131 + (hSession->lu.try != CN && *hSession->lu.try) ? hSession->lu.try : "",
1117 1132 IAC, SE);
1118 1133 net_rawout(hSession, (unsigned char *)tt_out, tb_len);
1119 1134  
... ... @@ -1175,7 +1190,7 @@ static void continue_tls(H3270 *hSession, unsigned char *sbbuf, int len)
1175 1190 #endif // HAVE_LIBSSL
1176 1191  
1177 1192 #if defined(X3270_TN3270E) /*[*/
1178   -/* Send a TN3270E terminal type request. */
  1193 +/// @brief Send a TN3270E terminal type request.
1179 1194 static void tn3270e_request(H3270 *hSession)
1180 1195 {
1181 1196 int tt_len, tb_len;
... ... @@ -1183,8 +1198,8 @@ static void tn3270e_request(H3270 *hSession)
1183 1198 char *t;
1184 1199  
1185 1200 tt_len = strlen(hSession->termtype);
1186   - if (hSession->try_lu != CN && *hSession->try_lu)
1187   - tt_len += strlen(hSession->try_lu) + 1;
  1201 + if (hSession->lu.try != CN && *hSession->lu.try)
  1202 + tt_len += strlen(hSession->lu.try) + 1;
1188 1203  
1189 1204 tb_len = 5 + tt_len + 2;
1190 1205 tt_out = lib3270_malloc(tb_len + 1);
... ... @@ -1197,8 +1212,8 @@ static void tn3270e_request(H3270 *hSession)
1197 1212 if (tt_out[12] == '9')
1198 1213 tt_out[12] = '8';
1199 1214  
1200   - if (hSession->try_lu != CN && *hSession->try_lu)
1201   - t += sprintf(t, "%c%s", TN3270E_OP_CONNECT, hSession->try_lu);
  1215 + if (hSession->lu.try != CN && *hSession->lu.try)
  1216 + t += sprintf(t, "%c%s", TN3270E_OP_CONNECT, hSession->lu.try);
1202 1217  
1203 1218 (void) sprintf(t, "%c%c", IAC, SE);
1204 1219  
... ... @@ -1210,8 +1225,8 @@ static void tn3270e_request(H3270 *hSession)
1210 1225 opt(TELOPT_TN3270E),
1211 1226 (int) strlen(hSession->termtype),
1212 1227 tt_out + 5,
1213   - (hSession->try_lu != CN && *hSession->try_lu) ? " CONNECT " : "",
1214   - (hSession->try_lu != CN && *hSession->try_lu) ? hSession->try_lu : "",
  1228 + (hSession->lu.try != CN && *hSession->lu.try) ? " CONNECT " : "",
  1229 + (hSession->lu.try != CN && *hSession->lu.try) ? hSession->lu.try : "",
1215 1230 cmd(SE)
1216 1231 );
1217 1232  
... ... @@ -1332,14 +1347,14 @@ static int tn3270e_negotiate(H3270 *hSession)
1332 1347 }
1333 1348  
1334 1349 next_lu(hSession);
1335   - if (hSession->try_lu != CN)
  1350 + if (hSession->lu.try != CN)
1336 1351 {
1337   - /* Try the next LU. */
  1352 + // Try the next LU.
1338 1353 tn3270e_request(hSession);
1339 1354 }
1340   - else if (hSession->lus != (char **)NULL)
  1355 + else if (hSession->lu.names != (char **)NULL)
1341 1356 {
1342   - /* No more LUs to try. Give up. */
  1357 + // No more LUs to try. Give up.
1343 1358 backoff_tn3270e(hSession,_("Host rejected resource(s)"));
1344 1359 }
1345 1360 else
... ... @@ -2096,9 +2111,9 @@ static void check_in3270(H3270 *hSession)
2096 2111 // TN3270E mode, reset the LU list so we can try again
2097 2112 // in the new mode.
2098 2113 //
2099   - if (hSession->lus != (char **)NULL && was_in_e != IN_E) {
2100   - hSession->curr_lu = hSession->lus;
2101   - hSession->try_lu = *hSession->curr_lu;
  2114 + if (hSession->lu.names != (char **)NULL && was_in_e != IN_E) {
  2115 + hSession->lu.curr = hSession->lu.names;
  2116 + hSession->lu.try = *hSession->lu.curr;
2102 2117 }
2103 2118 #endif
2104 2119  
... ...
src/core/toggles/table.c
... ... @@ -239,8 +239,8 @@ const LIB3270_TOGGLE toggle_descriptor[LIB3270_TOGGLE_COUNT+1] =
239 239 .def = False,
240 240 .key = NULL, // Default keycode
241 241 .icon = NULL, // Icon name
242   - .label = N_( "Auto connect" ),
243   - .summary = N_( "Connect on startup" ),
  242 + .label = N_( "Connect on startup" ),
  243 + .summary = N_( "Automatically connect to host on startup" ),
244 244 .description = ""
245 245 },
246 246 {
... ...
src/include/internals.h
... ... @@ -508,14 +508,13 @@ struct _h3270
508 508 */
509 509  
510 510 /// @brief LU
511   - char **curr_lu;
512   - char * try_lu;
513   - char **lus; ///< @brief Array with the LU names to try.
514 511 struct
515 512 {
516   - char reported[LIB3270_LU_MAX+1];
517   - char * associated; ///< @brief The LU name associated with the session.
518   - char names[LIB3270_LUNAME_LENGTH+1]; ///< @brief The LU names to try.
  513 + char reported[LIB3270_LU_MAX+1];
  514 + const char * associated; ///< @brief The LU name associated with the session.
  515 + char **names; ///< @brief Array with the LU names to try.
  516 + char **curr;
  517 + const char * try;
519 518  
520 519 } lu;
521 520  
... ...
src/include/lib3270.h
... ... @@ -964,7 +964,7 @@
964 964 LIB3270_EXPORT void lib3270_register_fd_handlers(void * (*add)(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata), void (*rm)(H3270 *, void *id));
965 965  
966 966 /**
967   - * Get program message.
  967 + * @brief Get program message.
968 968 *
969 969 * @see LIB3270_MESSAGE
970 970 *
... ... @@ -976,7 +976,7 @@
976 976 LIB3270_EXPORT LIB3270_MESSAGE lib3270_get_program_message(const H3270 *h);
977 977  
978 978 /**
979   - * Get the LU name associated with the session, if there is one.
  979 + * @brief Get the LU name associated with the session, if there is one.
980 980 *
981 981 * Get the name LU associated with the session; the value is
982 982 * internal to lib3270 and should not be changed ou freed.
... ... @@ -988,7 +988,21 @@
988 988 */
989 989 LIB3270_EXPORT const char * lib3270_get_associated_luname(const H3270 *hSession);
990 990  
991   - LIB3270_EXPORT int lib3270_set_luname(H3270 *hSession, const char *luname);
  991 + /**
  992 + * @brief Set the LU names.
  993 + *
  994 + * @param hSession Session handle.
  995 + * @param lunames Comma separated list of the LU names to set.
  996 + *
  997 + * @return 0 if the list was set, non zero if not (sets errno)
  998 + *
  999 + * @retval EISCONN The session is online.
  1000 + * @retval EINVAL Invalid session handle.
  1001 + *
  1002 + */
  1003 + LIB3270_EXPORT int lib3270_set_lunames(H3270 *hSession, const char *luname);
  1004 +
  1005 + LIB3270_EXPORT const char ** lib3270_get_lunames(H3270 *hSession);
992 1006  
993 1007 LIB3270_EXPORT int lib3270_is_connected(const H3270 *h);
994 1008 LIB3270_EXPORT int lib3270_is_disconnected(const H3270 *h);
... ...
src/testprogram/testprogram.c
... ... @@ -9,6 +9,7 @@
9 9 #include <lib3270/actions.h>
10 10 #include <lib3270/trace.h>
11 11 #include <lib3270/toggle.h>
  12 +#include <lib3270/log.h>
12 13  
13 14 #define MAX_ARGS 10
14 15  
... ... @@ -19,7 +20,7 @@ static void write_trace(H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata)
19 20 FILE *out = fopen(trace_file,"a");
20 21 if(out)
21 22 {
22   -
  23 +
23 24 vfprintf(out,fmt,args);
24 25 fclose(out);
25 26 }
... ... @@ -149,6 +150,21 @@ int main(int argc, char *argv[])
149 150  
150 151 lib3270_unregister_action_group_listener(h,LIB3270_ACTION_GROUP_ONLINE,online_listener);
151 152  
  153 + lib3270_disconnect(h);
  154 +
  155 + {
  156 + lib3270_set_lunames(h,"a,b,c,d,e");
  157 +
  158 + const char ** names = lib3270_get_lunames(h);
  159 +
  160 + size_t i;
  161 + for(i=0;names[i];i++)
  162 + {
  163 + debug("[%s]",names[i]);
  164 + }
  165 +
  166 + }
  167 +
152 168 lib3270_session_free(h);
153 169  
154 170 return 0;
... ...