Changeset 3f1d9b5 for benchmark/io


Ignore:
Timestamp:
Jul 20, 2020, 1:20:06 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
28d73c1, 7d94bfe3
Parents:
03ed863
Message:

Parse args help now properly formats on narrow terminals

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/io/http/parseargs.cfa

    r03ed863 r3f1d9b5  
    33// #include <stdio.h>
    44// #include <stdlib.h>
     5#include <errno.h>
    56#include <string.h>
     7#include <unistd.h>
    68extern "C" {
    79        #include <getopt.h>
     10        #include <sys/ioctl.h>
    811
    912        struct FILE;
     
    1114        extern FILE * stdout;
    1215
     16        extern int fileno(FILE *stream);
     17
    1318        extern int fprintf ( FILE * stream, const char * format, ... );
    1419
    1520        extern          long long int strtoll (const char* str, char** endptr, int base);
    1621        extern unsigned long long int strtoull(const char* str, char** endptr, int base);
     22}
     23
     24#include <common.hfa>
     25#include <limits.hfa>
     26
     27void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) {
     28        int hwidth = max - (11 + width);
     29        if(hwidth <= 0) hwidth = max;
     30
     31        fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help);
     32        for() {
     33                help += min(strlen(help), hwidth);
     34                if('\0' == *help) break;
     35                fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help);
     36        }
    1737}
    1838
     
    2747        struct option optarr[opt_count + 2];
    2848        int width = 0;
     49        int max_width = 1_000_000;
    2950        {
    3051                int idx = 0;
     
    95116        }
    96117
    97         USAGE:
     118        USAGE:;
     119        int outfd = fileno(out);
     120        if(isatty(outfd)) {
     121                struct winsize size;
     122                int ret = ioctl(outfd, TIOCGWINSZ, &size);
     123                if(ret < 0) abort( "ioctl error: (%d) %s\n", (int)errno, strerror(errno) );
     124                max_width = size.ws_col;
     125        }
     126
    98127        fprintf(out, "Usage:\n  %s %s\n", argv[0], usage);
    99128
    100129        for(i; opt_count) {
    101                 fprintf(out, "  -%c, --%-*s   %s\n", options[i].short_name, width, options[i].long_name, options[i].help);
     130                printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help);
    102131        }
    103132        fprintf(out, "  -%c, --%-*s   %s\n", 'h', width, "help", "print this help message");
     
    138167        unsigned long long int r = strtoull(arg, &end, 10);
    139168        if(*end != '\0') return false;
    140 #warning not checking max
     169        if(r > (unsigned)MAX) return false;
    141170
    142171        value = r;
     
    148177        unsigned long long int r = strtoull(arg, &end, 10);
    149178        if(*end != '\0') return false;
    150 #warning not checking max
     179        if(r > (size_t)MAX) return false;
    151180
    152181        value = r;
Note: See TracChangeset for help on using the changeset viewer.