Commit 87b76c6f6d7780dfdc1c9ed4d2ef0b600e99f90e

Authored by Perry Werneck
1 parent 9e14881b

Standardizing error codes; documenting then.

src/core/bounds.c
... ... @@ -52,7 +52,7 @@ LIB3270_EXPORT int lib3270_get_field_bounds(H3270 *hSession, int baddr, int *sta
52 52 first = lib3270_field_addr(hSession,baddr);
53 53  
54 54 if(first < 0)
55   - return errno = (errno == 0 ? EINVAL : errno);
  55 + return -first;
56 56  
57 57 first++;
58 58  
... ...
src/core/ctlr.c
... ... @@ -451,7 +451,7 @@ LIB3270_EXPORT int lib3270_get_field_start(H3270 *hSession, int baddr)
451 451 return - errno;
452 452  
453 453 if (!hSession->formatted)
454   - return - (errno = ENOTCONN);
  454 + return - (errno = ENOTSUP);
455 455  
456 456 if(baddr < 0)
457 457 baddr = hSession->cursor_addr;
... ... @@ -474,18 +474,18 @@ LIB3270_EXPORT int lib3270_get_field_len(H3270 *hSession, int baddr)
474 474 int addr;
475 475 int width = 0;
476 476  
477   - CHECK_SESSION_HANDLE(hSession);
  477 + if(check_online_session(hSession))
  478 + return - errno;
478 479  
479 480 if (!hSession->formatted)
480   - return errno = ENOTCONN;
  481 + return - (errno = ENOTSUP);
481 482  
482 483 if(baddr < 0)
483 484 baddr = hSession->cursor_addr;
484 485  
485 486 addr = lib3270_field_addr(hSession,baddr);
486   -
487 487 if(addr < 0)
488   - return -1;
  488 + return addr;
489 489  
490 490 saddr = addr;
491 491 INC_BA(addr);
... ... @@ -497,26 +497,24 @@ LIB3270_EXPORT int lib3270_get_field_len(H3270 *hSession, int baddr)
497 497 width++;
498 498 } while (addr != saddr);
499 499  
500   - return -1;
  500 + return -(errno = ENODATA);
501 501 }
502 502  
503   -/**
504   - * @brief Find the buffer address of the field attribute for a given buffer address.
505   - *
506   - * @param hSession Session handle.
507   - * @param addr Buffer address of the field.
508   - *
509   - * @return field address or -1 if the screen isn't formatted (sets errno).
510   - *
511   - */
512 503 LIB3270_EXPORT int lib3270_field_addr(H3270 *hSession, int baddr)
513 504 {
514 505 int sbaddr;
515 506  
516   - FAIL_IF_NOT_ONLINE(hSession);
  507 + if(!lib3270_is_connected(hSession))
  508 + return -(errno = ENOTCONN);
517 509  
518 510 if(!hSession->formatted)
519   - return errno = ENOTCONN;
  511 + return -(errno = ENOTSUP);
  512 +
  513 + if(baddr < 0)
  514 + baddr = lib3270_get_cursor_address(hSession);
  515 +
  516 + if(baddr > lib3270_get_length(hSession))
  517 + return -(errno = EOVERFLOW);
520 518  
521 519 sbaddr = baddr;
522 520 do
... ... @@ -526,8 +524,7 @@ LIB3270_EXPORT int lib3270_field_addr(H3270 *hSession, int baddr)
526 524 DEC_BA(baddr);
527 525 } while (baddr != sbaddr);
528 526  
529   - errno = EINVAL;
530   - return -1;
  527 + return -(errno = ENODATA);
531 528 }
532 529  
533 530 LIB3270_EXPORT LIB3270_FIELD_ATTRIBUTE lib3270_get_field_attribute(H3270 *hSession, int baddr)
... ... @@ -565,7 +562,7 @@ LIB3270_EXPORT LIB3270_FIELD_ATTRIBUTE lib3270_get_field_attribute(H3270 *hSessi
565 562 * @param hSession Session handle.
566 563 * @param addr Buffer address of the field.
567 564 *
568   - * @return field length or -1 if invalid or not connected (sets errno).
  565 + * @return field length or negative if invalid or not connected (sets errno).
569 566 *
570 567 */
571 568 int lib3270_field_length(H3270 *hSession, int baddr)
... ... @@ -575,9 +572,8 @@ int lib3270_field_length(H3270 *hSession, int baddr)
575 572 int width = 0;
576 573  
577 574 addr = lib3270_field_addr(hSession,baddr);
578   -
579 575 if(addr < 0)
580   - return -1;
  576 + return addr;
581 577  
582 578 saddr = addr;
583 579 INC_BA(addr);
... ... @@ -589,17 +585,22 @@ int lib3270_field_length(H3270 *hSession, int baddr)
589 585 width++;
590 586 } while (addr != saddr);
591 587  
592   - return errno = EINVAL;
  588 + return -(errno = EINVAL);
593 589  
594 590 }
595 591  
596   -/*
597   - * Find the field attribute for the given buffer address. Return its address
598   - * rather than its value.
  592 +/**
  593 + * @brief Find the field attribute for the given buffer address.
  594 + *
  595 + * @return Field attribute.
  596 + *
599 597 */
600 598 unsigned char get_field_attribute(H3270 *hSession, int baddr)
601 599 {
602   - return hSession->ea_buf[lib3270_field_addr(hSession,baddr)].fa;
  600 + baddr = lib3270_field_addr(hSession,baddr);
  601 + if(baddr < 0)
  602 + return 0;
  603 + return hSession->ea_buf[baddr].fa;
603 604 }
604 605  
605 606 /**
... ... @@ -608,7 +609,7 @@ unsigned char get_field_attribute(H3270 *hSession, int baddr)
608 609 * @param hSession Session handle.
609 610 * @param baddr0 Search start addr (-1 to use current cursor position).
610 611 *
611   - * @return address following the unprotected attribute byte, or 0 if no nonzero-width unprotected field can be found, -1 if not connected.
  612 + * @return address following the unprotected attribute byte, or 0 if no nonzero-width unprotected field can be found, negative if failed.
612 613 *
613 614 */
614 615 LIB3270_EXPORT int lib3270_get_next_unprotected(H3270 *hSession, int baddr0)
... ... @@ -618,7 +619,7 @@ LIB3270_EXPORT int lib3270_get_next_unprotected(H3270 *hSession, int baddr0)
618 619 FAIL_IF_NOT_ONLINE(hSession);
619 620  
620 621 if(!hSession->formatted)
621   - return errno = ENOTCONN;
  622 + return -(errno = ENOTSUP);
622 623  
623 624 if(baddr0 < 0)
624 625 baddr0 = hSession->cursor_addr;
... ...
src/include/lib3270.h
... ... @@ -1094,11 +1094,14 @@
1094 1094 * @brief Get all text inside the terminal.
1095 1095 *
1096 1096 * @param h Session Handle.
1097   - * @param offset Start position.
  1097 + * @param offset Start position (-1 to current cursor position).
1098 1098 * @param len Text length or -1 to all text.
1099 1099 * @param lf Line break char (0 to disable line breaks).
1100 1100 *
1101   - * @return Contents at position if available, or NULL. Release it with lib3270_free()
  1101 + * @return Contents at position if available, or NULL if error (sets errno). Release it with lib3270_free()
  1102 + *
  1103 + * @exception ENOTCONN Not connected to host.
  1104 + * @exception EOVERFLOW Invalid offset.
1102 1105 *
1103 1106 */
1104 1107 LIB3270_EXPORT char * lib3270_get_string_at_address(H3270 *h, int offset, int len, char lf);
... ... @@ -1109,13 +1112,16 @@
1109 1112 * @param h Session Handle.
1110 1113 * @param row Desired row.
1111 1114 * @param col Desired col.
1112   - * @param length Text length
  1115 + * @param len Text length or -1 to all text.
1113 1116 * @param lf Line break char (0 to disable line breaks).
1114 1117 *
1115   - * @return Contents at position if available, or NULL. Release it with lib3270_free()
  1118 + * @return Contents at position if available, or NULL if error (sets errno). Release it with lib3270_free()
  1119 + *
  1120 + * @exception ENOTCONN Not connected to host.
  1121 + * @exception EOVERFLOW Invalid position.
1116 1122 *
1117 1123 */
1118   - LIB3270_EXPORT char * lib3270_get_string_at(H3270 *h, int row, int col, int len, char lf);
  1124 + LIB3270_EXPORT char * lib3270_get_string_at(H3270 *h, unsigned int row, unsigned int col, int len, char lf);
1119 1125  
1120 1126 /**
1121 1127 * @brief Check for text at requested position
... ... @@ -1129,7 +1135,7 @@
1129 1135 * @return Test result from strcmp
1130 1136 *
1131 1137 */
1132   - LIB3270_EXPORT int lib3270_cmp_text_at(H3270 *h, int row, int col, const char *text, char lf);
  1138 + LIB3270_EXPORT int lib3270_cmp_text_at(H3270 *h, unsigned int row, unsigned int col, const char *text, char lf);
1133 1139  
1134 1140  
1135 1141 /**
... ... @@ -1138,7 +1144,10 @@
1138 1144 * @param h Session Handle.
1139 1145 * @param baddr Reference position.
1140 1146 *
1141   - * @return NULL if failed, contents of the entire field if suceeds (release it with lib3270_free()).
  1147 + * @return NULL if failed (sets errno), contents of the entire field if suceeds (release it with lib3270_free()).
  1148 + *
  1149 + * @exception ENOTCONN Not connected to host.
  1150 + * @exception EOVERFLOW Invalid position.
1142 1151 *
1143 1152 */
1144 1153 LIB3270_EXPORT char * lib3270_get_field_text_at(H3270 *h, int baddr);
... ... @@ -1206,7 +1215,12 @@
1206 1215 * @param hSession Session handle.
1207 1216 * @param addr Buffer address of the field.
1208 1217 *
1209   - * @return field address or -1 if the screen isn't formatted (sets errno).
  1218 + * @return field address or negative if the screen isn't formatted (sets errno).
  1219 + *
  1220 + * @exception -ENOTCONN Not connected to host.
  1221 + * @exception -EOVERFLOW Invalid position.
  1222 + * @exception -ENOTSUP Screen is not formatted.
  1223 + * @exception -ENODATA No field at the address.
1210 1224 *
1211 1225 */
1212 1226 LIB3270_EXPORT int lib3270_field_addr(H3270 *hSession, int baddr);
... ...
src/selection/selection.c
... ... @@ -288,10 +288,13 @@ LIB3270_EXPORT char * lib3270_get_string_at_address(H3270 *h, int offset, int le
288 288 return NULL;
289 289 }
290 290  
  291 + if(offset < 0)
  292 + offset = lib3270_get_cursor_address(h);
  293 +
291 294 maxlen = (h->rows * (h->cols+ (lf ? 1 : 0) )) - offset;
292 295 if(maxlen <= 0 || offset < 0)
293 296 {
294   - errno = EINVAL;
  297 + errno = EOVERFLOW;
295 298 return NULL;
296 299 }
297 300  
... ... @@ -331,19 +334,28 @@ LIB3270_EXPORT char * lib3270_get_string_at_address(H3270 *h, int offset, int le
331 334 return buffer;
332 335 }
333 336  
334   -LIB3270_EXPORT char * lib3270_get_string_at(H3270 *h, int row, int col, int len, char lf)
  337 +LIB3270_EXPORT char * lib3270_get_string_at(H3270 *h, unsigned int row, unsigned int col, int len, char lf)
335 338 {
336 339 CHECK_SESSION_HANDLE(h);
337   - return lib3270_get_string_at_address(h, ((row-1) * h->cols) + (col-1), len, lf);
  340 +
  341 + int baddr = lib3270_translate_to_address(h,row,col);
  342 + if(baddr < 0)
  343 + return NULL;
  344 +
  345 + return lib3270_get_string_at_address(h, baddr, len, lf);
338 346 }
339 347  
340   -LIB3270_EXPORT int lib3270_cmp_text_at(H3270 *h, int row, int col, const char *text, char lf)
  348 +LIB3270_EXPORT int lib3270_cmp_text_at(H3270 *h, unsigned int row, unsigned int col, const char *text, char lf)
341 349 {
342 350 int rc;
343 351 size_t sz = strlen(text);
344 352 char * contents;
345 353  
346   - contents = lib3270_get_string_at(h,row,col,sz,lf);
  354 + int baddr = lib3270_translate_to_address(h,row,col);
  355 + if(baddr < 0)
  356 + return -1;
  357 +
  358 + contents = lib3270_get_string_at_address(h,baddr,sz,lf);
347 359 if(!contents)
348 360 return -1;
349 361  
... ...