Home
       ttyclock.c - tty-clock - ⏰ port of tty-clock to OpenBSD, with pledge/unveil added as goodie.
  HTML git clone https://git.drkhsh.at/tty-clock.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       ttyclock.c (21121B)
       ---
            1 /*
            2  *      TTY-CLOCK Main file.
            3  *      Copyright © 2009-2018 tty-clock contributors
            4  *      Copyright © 2008 Martin Duquesnoy <xorg62@gmail.com>
            5  *      All rights reserved.
            6  *
            7  *      Redistribution and use in source and binary forms, with or without
            8  *      modification, are permitted provided that the following conditions are
            9  *      met:
           10  *
           11  *      * Redistributions of source code must retain the above copyright
           12  *        notice, this list of conditions and the following disclaimer.
           13  *      * Redistributions in binary form must reproduce the above
           14  *        copyright notice, this list of conditions and the following disclaimer
           15  *        in the documentation and/or other materials provided with the
           16  *        distribution.
           17  *      * Neither the name of the  nor the names of its
           18  *        contributors may be used to endorse or promote products derived from
           19  *        this software without specific prior written permission.
           20  *
           21  *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
           22  *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
           23  *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
           24  *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
           25  *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
           26  *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
           27  *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
           28  *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
           29  *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
           30  *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
           31  *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
           32  */
           33 
           34 #include "ttyclock.h"
           35 
           36 void
           37 init(void)
           38 {
           39      struct sigaction sig;
           40      setlocale(LC_TIME,"");
           41 
           42      ttyclock.bg = COLOR_BLACK;
           43 
           44 #ifdef __OpenBSD__
           45      if (pledge("stdio rpath tty unveil", NULL) == -1) {
           46            fprintf(stderr, "tty-clock: error: pledge.\n");
           47            exit(EXIT_FAILURE);
           48      }
           49      unveil(NULL, NULL);
           50 #endif /* __OpenBSD__ */
           51 
           52      /* Init ncurses */
           53      if (ttyclock.tty) {
           54           FILE *ftty = fopen(ttyclock.tty, "r+");
           55           if (!ftty) {
           56                fprintf(stderr, "tty-clock: error: '%s' couldn't be opened: %s.\n",
           57                        ttyclock.tty, strerror(errno));
           58                exit(EXIT_FAILURE);
           59           }
           60           ttyclock.ttyscr = newterm(NULL, ftty, ftty);
           61           assert(ttyclock.ttyscr != NULL);
           62           set_term(ttyclock.ttyscr);
           63      } else
           64           initscr();
           65 
           66      cbreak();
           67      noecho();
           68      keypad(stdscr, true);
           69      start_color();
           70      curs_set(false);
           71      clear();
           72 
           73      /* Init default terminal color */
           74      if(use_default_colors() == OK)
           75           ttyclock.bg = -1;
           76 
           77      /* Init color pair */
           78      init_pair(0, ttyclock.bg, ttyclock.bg);
           79      init_pair(1, ttyclock.bg, ttyclock.option.color);
           80      init_pair(2, ttyclock.option.color, ttyclock.bg);
           81 //     init_pair(0, ttyclock.bg, ttyclock.bg);
           82 //     init_pair(1, ttyclock.bg, ttyclock.option.color);
           83 //     init_pair(2, ttyclock.option.color, ttyclock.bg);
           84      refresh();
           85 
           86      /* Init signal handler */
           87      sig.sa_handler = signal_handler;
           88      sig.sa_flags   = 0;
           89      sigaction(SIGTERM,  &sig, NULL);
           90      sigaction(SIGINT,   &sig, NULL);
           91      sigaction(SIGSEGV,  &sig, NULL);
           92 
           93      /* Init global struct */
           94      ttyclock.running = true;
           95      if(!ttyclock.geo.x)
           96           ttyclock.geo.x = 0;
           97      if(!ttyclock.geo.y)
           98           ttyclock.geo.y = 0;
           99      if(!ttyclock.geo.a)
          100           ttyclock.geo.a = 1;
          101      if(!ttyclock.geo.b)
          102           ttyclock.geo.b = 1;
          103      ttyclock.geo.w = (ttyclock.option.second) ? SECFRAMEW : NORMFRAMEW;
          104      ttyclock.geo.h = 7;
          105      ttyclock.tm = localtime(&(ttyclock.lt));
          106      if(ttyclock.option.utc) {
          107           ttyclock.tm = gmtime(&(ttyclock.lt));
          108      }
          109      ttyclock.lt = time(NULL);
          110      update_hour();
          111 
          112      /* Create clock win */
          113      ttyclock.framewin = newwin(ttyclock.geo.h,
          114                                 ttyclock.geo.w,
          115                                 ttyclock.geo.x,
          116                                 ttyclock.geo.y);
          117      if(ttyclock.option.box) {
          118           box(ttyclock.framewin, 0, 0);
          119      }
          120 
          121      if (ttyclock.option.bold)
          122      {
          123           wattron(ttyclock.framewin, A_BLINK);
          124      }
          125 
          126      /* Create the date win */
          127      ttyclock.datewin = newwin(DATEWINH, strlen(ttyclock.date.datestr) + 2,
          128                                ttyclock.geo.x + ttyclock.geo.h - 1,
          129                                ttyclock.geo.y + (ttyclock.geo.w / 2) -
          130                                (strlen(ttyclock.date.datestr) / 2) - 1);
          131      if(ttyclock.option.box && ttyclock.option.date) {
          132           box(ttyclock.datewin, 0, 0);
          133      }
          134      clearok(ttyclock.datewin, true);
          135 
          136      set_center(ttyclock.option.center);
          137 
          138      nodelay(stdscr, true);
          139 
          140      if (ttyclock.option.date)
          141      {
          142           wrefresh(ttyclock.datewin);
          143      }
          144 
          145      wrefresh(ttyclock.framewin);
          146 
          147      return;
          148 }
          149 
          150 void
          151 signal_handler(int signal)
          152 {
          153      switch(signal)
          154      {
          155      case SIGINT:
          156      case SIGTERM:
          157           ttyclock.running = false;
          158           break;
          159           /* Segmentation fault signal */
          160      case SIGSEGV:
          161           endwin();
          162           fprintf(stderr, "Segmentation fault.\n");
          163           exit(EXIT_FAILURE);
          164           break;
          165      }
          166 
          167      return;
          168 }
          169 
          170 void
          171 cleanup(void)
          172 {
          173      if (ttyclock.ttyscr)
          174           delscreen(ttyclock.ttyscr);
          175 
          176      free(ttyclock.tty);
          177 }
          178 
          179 void
          180 update_hour(void)
          181 {
          182      int ihour;
          183      char tmpstr[128];
          184 
          185      ttyclock.lt = time(NULL);
          186      ttyclock.tm = localtime(&(ttyclock.lt));
          187      if(ttyclock.option.utc) {
          188           ttyclock.tm = gmtime(&(ttyclock.lt));
          189      }
          190 
          191      ihour = ttyclock.tm->tm_hour;
          192 
          193      if(ttyclock.option.twelve)
          194           ttyclock.meridiem = ((ihour >= 12) ? PMSIGN : AMSIGN);
          195      else
          196           ttyclock.meridiem = "\0";
          197 
          198      /* Manage hour for twelve mode */
          199      ihour = ((ttyclock.option.twelve && ihour > 12)  ? (ihour - 12) : ihour);
          200      ihour = ((ttyclock.option.twelve && !ihour) ? 12 : ihour);
          201 
          202      /* Set hour */
          203      ttyclock.date.hour[0] = ihour / 10;
          204      ttyclock.date.hour[1] = ihour % 10;
          205 
          206      /* Set minutes */
          207      ttyclock.date.minute[0] = ttyclock.tm->tm_min / 10;
          208      ttyclock.date.minute[1] = ttyclock.tm->tm_min % 10;
          209 
          210      /* Set date string */
          211      if (strlcpy(ttyclock.date.old_datestr, ttyclock.date.datestr, sizeof(ttyclock.date.old_datestr)) >= sizeof(ttyclock.date.old_datestr))
          212           return;
          213      strftime(tmpstr,
          214               sizeof(tmpstr),
          215               ttyclock.option.format,
          216               ttyclock.tm);
          217      if (snprintf(ttyclock.date.datestr, sizeof(ttyclock.date.datestr), "%s%s", tmpstr, ttyclock.meridiem) < 0)
          218           return;
          219 
          220      /* Set seconds */
          221      ttyclock.date.second[0] = ttyclock.tm->tm_sec / 10;
          222      ttyclock.date.second[1] = ttyclock.tm->tm_sec % 10;
          223 
          224      return;
          225 }
          226 
          227 void
          228 draw_number(int n, int x, int y)
          229 {
          230      int i, sy = y;
          231 
          232      for(i = 0; i < 30; ++i, ++sy)
          233      {
          234           if(sy == y + 6)
          235           {
          236                sy = y;
          237                ++x;
          238           }
          239 
          240           if (ttyclock.option.bold)
          241                wattron(ttyclock.framewin, A_BLINK);
          242           else
          243                wattroff(ttyclock.framewin, A_BLINK);
          244 
          245           wbkgdset(ttyclock.framewin, COLOR_PAIR(number[n][i/2]));
          246           mvwaddch(ttyclock.framewin, x, sy, ' ');
          247      }
          248      wrefresh(ttyclock.framewin);
          249 
          250      return;
          251 }
          252 
          253 void
          254 draw_clock(void)
          255 {
          256      if (ttyclock.option.date && !ttyclock.option.rebound &&
          257                strcmp(ttyclock.date.datestr, ttyclock.date.old_datestr) != 0)
          258      {
          259           clock_move(ttyclock.geo.x,
          260                      ttyclock.geo.y,
          261                      ttyclock.geo.w,
          262                      ttyclock.geo.h);
          263      }
          264 
          265      /* Draw hour numbers */
          266      draw_number(ttyclock.date.hour[0], 1, 1);
          267      draw_number(ttyclock.date.hour[1], 1, 8);
          268      chtype dotcolor = COLOR_PAIR(1);
          269      if (ttyclock.option.blink && time(NULL) % 2 == 0)
          270           dotcolor = COLOR_PAIR(2);
          271 
          272      /* 2 dot for number separation */
          273      wbkgdset(ttyclock.framewin, dotcolor);
          274      mvwaddstr(ttyclock.framewin, 2, 16, "  ");
          275      mvwaddstr(ttyclock.framewin, 4, 16, "  ");
          276 
          277      /* Draw minute numbers */
          278      draw_number(ttyclock.date.minute[0], 1, 20);
          279      draw_number(ttyclock.date.minute[1], 1, 27);
          280 
          281      /* Draw the date */
          282      if (ttyclock.option.bold)
          283           wattron(ttyclock.datewin, A_BOLD);
          284      else
          285           wattroff(ttyclock.datewin, A_BOLD);
          286 
          287      if (ttyclock.option.date)
          288      {
          289           wbkgdset(ttyclock.datewin, (COLOR_PAIR(2)));
          290           mvwprintw(ttyclock.datewin, (DATEWINH / 2), 1, "%s", ttyclock.date.datestr);
          291           wrefresh(ttyclock.datewin);
          292      }
          293 
          294      /* Draw second if the option is enabled */
          295      if(ttyclock.option.second)
          296      {
          297           /* Again 2 dot for number separation */
          298           wbkgdset(ttyclock.framewin, dotcolor);
          299           mvwaddstr(ttyclock.framewin, 2, NORMFRAMEW, "  ");
          300           mvwaddstr(ttyclock.framewin, 4, NORMFRAMEW, "  ");
          301 
          302           /* Draw second numbers */
          303           draw_number(ttyclock.date.second[0], 1, 39);
          304           draw_number(ttyclock.date.second[1], 1, 46);
          305      }
          306 
          307      return;
          308 }
          309 
          310 void
          311 clock_move(int x, int y, int w, int h)
          312 {
          313 
          314      /* Erase border for a clean move */
          315      wbkgdset(ttyclock.framewin, COLOR_PAIR(0));
          316      wborder(ttyclock.framewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
          317      werase(ttyclock.framewin);
          318      wrefresh(ttyclock.framewin);
          319 
          320      if (ttyclock.option.date)
          321      {
          322           wbkgdset(ttyclock.datewin, COLOR_PAIR(0));
          323           wborder(ttyclock.datewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
          324           werase(ttyclock.datewin);
          325           wrefresh(ttyclock.datewin);
          326      }
          327 
          328      /* Frame win move */
          329      mvwin(ttyclock.framewin, (ttyclock.geo.x = x), (ttyclock.geo.y = y));
          330      wresize(ttyclock.framewin, (ttyclock.geo.h = h), (ttyclock.geo.w = w));
          331 
          332      /* Date win move */
          333      if (ttyclock.option.date)
          334      {
          335           mvwin(ttyclock.datewin,
          336                 ttyclock.geo.x + ttyclock.geo.h - 1,
          337                 ttyclock.geo.y + (ttyclock.geo.w / 2) - (strlen(ttyclock.date.datestr) / 2) - 1);
          338           wresize(ttyclock.datewin, DATEWINH, strlen(ttyclock.date.datestr) + 2);
          339 
          340           if (ttyclock.option.box) {
          341                box(ttyclock.datewin,  0, 0);
          342           }
          343      }
          344 
          345      if (ttyclock.option.box)
          346      {
          347           box(ttyclock.framewin, 0, 0);
          348      }
          349 
          350      wrefresh(ttyclock.framewin);
          351      wrefresh(ttyclock.datewin); 
          352      return;
          353 }
          354 
          355 /* Useless but fun :) */
          356 void
          357 clock_rebound(void)
          358 {
          359      if(!ttyclock.option.rebound)
          360           return;
          361 
          362      if(ttyclock.geo.x < 1)
          363           ttyclock.geo.a = 1;
          364      if(ttyclock.geo.x > (LINES - ttyclock.geo.h - DATEWINH))
          365           ttyclock.geo.a = -1;
          366      if(ttyclock.geo.y < 1)
          367           ttyclock.geo.b = 1;
          368      if(ttyclock.geo.y > (COLS - ttyclock.geo.w - 1))
          369           ttyclock.geo.b = -1;
          370 
          371      clock_move(ttyclock.geo.x + ttyclock.geo.a,
          372                 ttyclock.geo.y + ttyclock.geo.b,
          373                 ttyclock.geo.w,
          374                 ttyclock.geo.h);
          375 
          376      return;
          377 }
          378 
          379 void
          380 set_second(void)
          381 {
          382      int new_w = (((ttyclock.option.second = !ttyclock.option.second)) ? SECFRAMEW : NORMFRAMEW);
          383      int y_adj;
          384 
          385      for(y_adj = 0; (ttyclock.geo.y - y_adj) > (COLS - new_w - 1); ++y_adj);
          386 
          387      clock_move(ttyclock.geo.x, (ttyclock.geo.y - y_adj), new_w, ttyclock.geo.h);
          388 
          389      set_center(ttyclock.option.center);
          390 
          391      return;
          392 }
          393 
          394 void
          395 set_center(bool b)
          396 {
          397      if((ttyclock.option.center = b))
          398      {
          399           ttyclock.option.rebound = false;
          400 
          401           clock_move((LINES / 2 - (ttyclock.geo.h / 2)),
          402                      (COLS  / 2 - (ttyclock.geo.w / 2)),
          403                      ttyclock.geo.w,
          404                      ttyclock.geo.h);
          405      }
          406 
          407      return;
          408 }
          409 
          410 void
          411 set_box(bool b)
          412 {
          413      ttyclock.option.box = b;
          414 
          415      wbkgdset(ttyclock.framewin, COLOR_PAIR(0));
          416      wbkgdset(ttyclock.datewin, COLOR_PAIR(0));
          417 
          418      if(ttyclock.option.box) {
          419           wbkgdset(ttyclock.framewin, COLOR_PAIR(0));
          420           wbkgdset(ttyclock.datewin, COLOR_PAIR(0));
          421           box(ttyclock.framewin, 0, 0);
          422           box(ttyclock.datewin,  0, 0);
          423      }
          424      else {
          425           wborder(ttyclock.framewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
          426           wborder(ttyclock.datewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
          427      }
          428 
          429      wrefresh(ttyclock.datewin);
          430      wrefresh(ttyclock.framewin);
          431 }
          432 
          433 void
          434 key_event(void)
          435 {
          436      int i, c;
          437 
          438      struct timespec length = { ttyclock.option.delay, ttyclock.option.nsdelay };
          439      
          440      fd_set rfds;
          441      FD_ZERO(&rfds);
          442      FD_SET(STDIN_FILENO, &rfds);
          443 
          444      if (ttyclock.option.screensaver)
          445      {
          446           c = wgetch(stdscr);
          447           if(c != ERR && ttyclock.option.noquit == false)
          448           {
          449                ttyclock.running = false;
          450           }
          451           else
          452           {
          453                nanosleep(&length, NULL);
          454                for(i = 0; i < 8; ++i)
          455                     if(c == (i + '0'))
          456                     {
          457                          ttyclock.option.color = i;
          458                          init_pair(1, ttyclock.bg, i);
          459                          init_pair(2, i, ttyclock.bg);
          460                     }
          461           }
          462           return;
          463      }
          464 
          465 
          466      switch(c = wgetch(stdscr))
          467      {
          468      case KEY_RESIZE:
          469           endwin();
          470           init();
          471           break;
          472 
          473      case KEY_UP:
          474      case 'k':
          475      case 'K':
          476           if(ttyclock.geo.x >= 1
          477              && !ttyclock.option.center)
          478                clock_move(ttyclock.geo.x - 1, ttyclock.geo.y, ttyclock.geo.w, ttyclock.geo.h);
          479           break;
          480 
          481      case KEY_DOWN:
          482      case 'j':
          483      case 'J':
          484           if(ttyclock.geo.x <= (LINES - ttyclock.geo.h - DATEWINH)
          485              && !ttyclock.option.center)
          486                clock_move(ttyclock.geo.x + 1, ttyclock.geo.y, ttyclock.geo.w, ttyclock.geo.h);
          487           break;
          488 
          489      case KEY_LEFT:
          490      case 'h':
          491      case 'H':
          492           if(ttyclock.geo.y >= 1
          493              && !ttyclock.option.center)
          494                clock_move(ttyclock.geo.x, ttyclock.geo.y - 1, ttyclock.geo.w, ttyclock.geo.h);
          495           break;
          496 
          497      case KEY_RIGHT:
          498      case 'l':
          499      case 'L':
          500           if(ttyclock.geo.y <= (COLS - ttyclock.geo.w - 1)
          501              && !ttyclock.option.center)
          502                clock_move(ttyclock.geo.x, ttyclock.geo.y + 1, ttyclock.geo.w, ttyclock.geo.h);
          503           break;
          504 
          505      case 'q':
          506      case 'Q':
          507           if (ttyclock.option.noquit == false)
          508                ttyclock.running = false;
          509           break;
          510 
          511      case 's':
          512      case 'S':
          513           set_second();
          514           break;
          515 
          516      case 't':
          517      case 'T':
          518           ttyclock.option.twelve = !ttyclock.option.twelve;
          519           /* Set the new ttyclock.date.datestr to resize date window */
          520           update_hour();
          521           clock_move(ttyclock.geo.x, ttyclock.geo.y, ttyclock.geo.w, ttyclock.geo.h);
          522           break;
          523 
          524      case 'c':
          525      case 'C':
          526           set_center(!ttyclock.option.center);
          527           break;
          528 
          529      case 'b':
          530      case 'B':
          531           ttyclock.option.bold = !ttyclock.option.bold;
          532           break;
          533 
          534      case 'r':
          535      case 'R':
          536           ttyclock.option.rebound = !ttyclock.option.rebound;
          537           if(ttyclock.option.rebound && ttyclock.option.center)
          538                ttyclock.option.center = false;
          539           break;
          540 
          541      case 'x':
          542      case 'X':
          543           set_box(!ttyclock.option.box);
          544           break;
          545 
          546      case '0': case '1': case '2': case '3':
          547      case '4': case '5': case '6': case '7':
          548           i = c - '0';
          549           ttyclock.option.color = i;
          550           init_pair(1, ttyclock.bg, i);
          551           init_pair(2, i, ttyclock.bg);
          552           break;
          553 
          554      default:
          555           pselect(1, &rfds, NULL, NULL, &length, NULL);
          556      }
          557 
          558      return;
          559 }
          560 
          561 int
          562 main(int argc, char **argv)
          563 {
          564      int c;
          565 
          566      /* Alloc ttyclock */
          567      memset(&ttyclock, 0, sizeof(ttyclock_t));
          568 
          569      ttyclock.option.date = true;
          570 
          571      /* Default date format */
          572      strncpy(ttyclock.option.format, "%F", sizeof (ttyclock.option.format));
          573      /* Default color */
          574      ttyclock.option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */
          575      /* Default delay */
          576      ttyclock.option.delay = 1; /* 1FPS */
          577      ttyclock.option.nsdelay = 0; /* -0FPS */
          578      ttyclock.option.blink = false;
          579 
          580      atexit(cleanup);
          581 
          582      while ((c = getopt(argc, argv, "iuvsScbtrhBxnDC:f:d:T:a:")) != -1)
          583      {
          584           switch(c)
          585           {
          586           case 'h':
          587           default:
          588                printf("usage : tty-clock [-iuvsScbtrahDBxn] [-C [0-7]] [-f format] [-d delay] [-a nsdelay] [-T tty] \n"
          589                       "    -s            Show seconds                                   \n"
          590                       "    -S            Screensaver mode                               \n"
          591                       "    -x            Show box                                       \n"
          592                       "    -c            Set the clock at the center of the terminal    \n"
          593                       "    -C [0-7]      Set the clock color                            \n"
          594                       "    -b            Use bold colors                                \n"
          595                       "    -t            Set the hour in 12h format                     \n"
          596                       "    -u            Use UTC time                                   \n"
          597                       "    -T tty        Display the clock on the specified terminal    \n"
          598                       "    -r            Do rebound the clock                           \n"
          599                       "    -f format     Set the date format                            \n"
          600                       "    -n            Don't quit on keypress                         \n"
          601                       "    -v            Show tty-clock version                         \n"
          602                       "    -i            Show some info about tty-clock                 \n"
          603                       "    -h            Show this page                                 \n"
          604                       "    -D            Hide date                                      \n"
          605                       "    -B            Enable blinking colon                          \n"
          606                       "    -d delay      Set the delay between two redraws of the clock. Default 1s. \n"
          607                       "    -a nsdelay    Additional delay between two redraws in nanoseconds. Default 0ns.\n");
          608                exit(EXIT_SUCCESS);
          609                break;
          610           case 'i':
          611                puts("TTY-Clock 2 © by Martin Duquesnoy (xorg62@gmail.com), Grey (grey@greytheory.net)");
          612                exit(EXIT_SUCCESS);
          613                break;
          614           case 'u':
          615                ttyclock.option.utc = true;
          616                break;
          617           case 'v':
          618                puts("TTY-Clock 2 © devel version");
          619                exit(EXIT_SUCCESS);
          620                break;
          621           case 's':
          622                ttyclock.option.second = true;
          623                break;
          624           case 'S':
          625                ttyclock.option.screensaver = true;
          626                break;
          627           case 'c':
          628                ttyclock.option.center = true;
          629                break;
          630           case 'b':
          631                ttyclock.option.bold = true;
          632                break;
          633           case 'C':
          634                if(atoi(optarg) >= 0 && atoi(optarg) < 8)
          635                     ttyclock.option.color = atoi(optarg);
          636                break;
          637           case 't':
          638                ttyclock.option.twelve = true;
          639                break;
          640           case 'r':
          641                ttyclock.option.rebound = true;
          642                break;
          643           case 'f':
          644                strncpy(ttyclock.option.format, optarg, 100);
          645                break;
          646           case 'd':
          647                if(atol(optarg) >= 0 && atol(optarg) < 100)
          648                     ttyclock.option.delay = atol(optarg);
          649                break;
          650           case 'D':
          651                ttyclock.option.date = false;
          652                break;
          653           case 'B':
          654                ttyclock.option.blink = true;
          655                break;
          656           case 'a':
          657                if(atol(optarg) >= 0 && atol(optarg) < 1000000000)
          658                     ttyclock.option.nsdelay = atol(optarg);
          659                break;
          660           case 'x':
          661                ttyclock.option.box = true;
          662                break;
          663           case 'T': {
          664                struct stat sbuf;
          665                if (stat(optarg, &sbuf) == -1) {
          666                     fprintf(stderr, "tty-clock: error: couldn't stat '%s': %s.\n",
          667                               optarg, strerror(errno));
          668                     exit(EXIT_FAILURE);
          669                } else if (!S_ISCHR(sbuf.st_mode)) {
          670                     fprintf(stderr, "tty-clock: error: '%s' doesn't appear to be a character device.\n",
          671                               optarg);
          672                     exit(EXIT_FAILURE);
          673                } else {
          674                     free(ttyclock.tty);
          675                     ttyclock.tty = strdup(optarg);
          676                }}
          677                break;
          678           case 'n':
          679                ttyclock.option.noquit = true;
          680                break;
          681           }
          682      }
          683 
          684      init();
          685      attron(A_BLINK);
          686      while(ttyclock.running)
          687      {
          688           clock_rebound();
          689           update_hour();
          690           draw_clock();
          691           key_event();
          692      }
          693 
          694      endwin();
          695 
          696      return 0;
          697 }
          698 
          699 // vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5