gonzui


Format: Advanced Search

t2ex/t2ex_source/kernel/sysmain/src/command.cbare sourcepermlink (0.06 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by T-Engine Forum at 2014/07/31.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: /*
   16:  * This software package is available for use, modification, 
   17:  * and redistribution in accordance with the terms of the attached 
   18:  * T-License 2.x.
   19:  * If you want to redistribute the source code, you need to attach 
   20:  * the T-License 2.x document.
   21:  * There's no obligation to publish the content, and no obligation 
   22:  * to disclose it to the TRON Forum if you have modified the 
   23:  * software package.
   24:  * You can also distribute the modified source code. In this case, 
   25:  * please register the modification to T-Kernel traceability service.
   26:  * People can know the history of modifications by the service, 
   27:  * and can be sure that the version you have inherited some 
   28:  * modification of a particular version or not.
   29:  *
   30:  *    http://trace.tron.org/tk/?lang=en
   31:  *    http://trace.tron.org/tk/?lang=ja
   32:  *
   33:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   34:  * the portion of the software that is copyrighted by Ken Sakamura or 
   35:  * the TRON Forum does not infringe the copyrights of a third party.
   36:  * However, it does not make any warranty other than this.
   37:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   38:  * responsible for any consequences or damages caused directly or
   39:  * indirectly by the use of this software package.
   40:  *
   41:  * The source codes in bsd_source.tar.gz in this software package are 
   42:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   43:  * They need to be changed or redistributed according to the 
   44:  * representation of each source header.
   45:  */
   46: 
   47: /*
   48:  *      @(#)command.c
   49:  *
   50:  */
   51: 
   52: #include <basic.h>
   53: #include <stdlib.h>
   54: #include <stdio.h>
   55: #include <string.h>
   56: #include <tk/tkernel.h>
   57: #include <t2ex/datetime.h>
   58: #include <t2ex/fs.h>
   59: #include <t2ex/load.h>
   60: #include <device/clk.h>
   61: 
   62: #ifdef  USE_T2EX_FS
   63: #define P               printf
   64: #else
   65: #define P               tm_printf
   66: #endif
   67: 
   68: #define N_ARGS          16
   69: 
   70: /*
   71:         ref command
   72: */
   73: #include "ref_command.c"
   74: 
   75: /*
   76:         Get from / Set to RTC
   77: */
   78: LOCAL   void      rtc_get_set(SYSTIM *stm, BOOL set)
   79: {
   80:         ID             dd;
   81:         W              asz;
   82:         ER             er;
   83:         DATE_TIM       dt;
   84:         struct tm      ctm;
   85:         struct tzinfo  tz;
   86: 
   87:         /* open RTC (CLOCK) driver */
   88:         dd = tk_opn_dev("CLOCK", (set == TRUE) ? TD_WRITE : TD_READ);
   89:         if (dd < E_OK) {
   90:                 P("Can't open CLOCK device [%#x]\n", dd);
   91:                 goto exit0;
   92:         }
   93: 
   94:         /* Note: The RTC is set to UTC rather than local time. because UTC
   95:            is not affected by time zone and daylight saving time. */
   96: 
   97:         if (set == TRUE) {     /* set date to RTC */
   98: 
   99:                 /* convert system time to UTC date/time */
  100:                 dt_gmtime_ms(stm, &ctm);
  101: 
  102:                 /* set local date/time to RTC */
  103:                 dt.d_year = ctm.tm_year;
  104:                 dt.d_month = ctm.tm_mon + 1;
  105:                 dt.d_day = ctm.tm_mday;
  106:                 dt.d_hour = ctm.tm_hour;
  107:                 dt.d_min = ctm.tm_min;
  108:                 dt.d_sec = ctm.tm_sec;
  109:                 dt.d_wday = ctm.tm_wday;
  110:                 er = tk_swri_dev(dd, DN_CKDATETIME, &dt, sizeof(dt), &asz);
  111:                 if (er < E_OK) {
  112:                         P("Can't set CLOCK [%#x]\n", er);
  113:                         goto exit1;
  114:                 }
  115:         } else {               /* get date from RTC */
  116: 
  117:                 /* get timezone of "UTC+0" */
  118:                 memset(&tz, 0, sizeof(tz));
  119:                 er = dt_tzset(&tz, "UTC+0");
  120:                 if (er < E_OK ) {
  121:                         P("dt_tzset(UTC+0) ERR [%#x]\n", er);
  122:                         goto exit1;
  123:                 }
  124: 
  125:                 /* get UTC date/time from RTC */
  126:                 er = tk_srea_dev(dd, DN_CKDATETIME, &dt, sizeof(dt), &asz);
  127:                 if (er < E_OK) {
  128:                         P("Can't get CLOCK [%#x]\n", er);
  129:                         goto exit1;
  130:                 }
  131: 
  132:                 /* convert to system time */
  133:                 ctm.tm_year = dt.d_year;
  134:                 ctm.tm_mon = dt.d_month - 1;
  135:                 ctm.tm_mday = dt.d_day;
  136:                 ctm.tm_hour = dt.d_hour;
  137:                 ctm.tm_min = dt.d_min;
  138:                 ctm.tm_sec = dt.d_sec;
  139:                 ctm.tm_wday = dt.d_wday;
  140:                 ctm.tm_usec = 0;
  141:                 ctm.tm_wday = -1;
  142:                 ctm.tm_isdst = 0;
  143:                 ctm.tm_yday = 0;
  144:                 dt_mktime_ms(&ctm, &tz, stm);
  145:         }
  146: exit1:
  147:         tk_cls_dev(dd, 0);
  148: exit0:
  149:         return;
  150: }
  151: 
  152: /*
  153:         Initialize calendar date
  154: */
  155: EXPORT  void     init_calendar_date(void)
  156: {
  157:         ER     er;
  158:         struct tzinfo  tz;
  159:         SYSTIM stm;
  160: 
  161:         /* initialize system timezone to "JST-9" */
  162:         memset(&tz, 0, sizeof(tz));
  163:         er = dt_tzset(&tz, "JST-9");
  164:         if (er < E_OK ) {
  165:                 P("dt_tzset(JST-9) ERR [%#x]\n", er);
  166:         } else {
  167:                 er = dt_setsystz(&tz);
  168:                 if (er < E_OK) {
  169:                         P("dt_setsystz() ERR [%#x]\n", er);
  170:                 }
  171:         }
  172: 
  173:         /* initialize calendar data/time from RTC */
  174:         stm.hi = stm.lo = 0;
  175:         rtc_get_set(&stm, FALSE);
  176:         tk_set_tim(&stm);
  177: }
  178: 
  179: /*
  180:         date command
  181: */
  182: LOCAL   void      cmd_date(INT ac, B *av[])
  183: {
  184:         ER     er;
  185:         struct tm      ctm;
  186:         SYSTIM stm;
  187: static  const B  *week[] = {"sun","mon","tue","wed","thu","fri","sat"};
  188: 
  189:         if (ac < 2) {          /* show current date */
  190:                 tk_get_tim(&stm);
  191:                 er = dt_localtime_ms(&stm, NULL, &ctm);
  192:                 if (er < E_OK) {
  193:                         P("dt_localtime() ERR [%#x]\n", er);
  194:                 } else {
  195:                         P("%d/%02d/%02d (%s) %02d:%02d:%02d\n",
  196:                                 ctm.tm_year + 1900, ctm.tm_mon + 1,
  197:                                 ctm.tm_mday, week[ctm.tm_wday],
  198:                                 ctm.tm_hour, ctm.tm_min, ctm.tm_sec);
  199:                 }
  200:         } else if (ac >= 4) {  /* set current date */
  201:                 ctm.tm_year = atoi(av[1]) - 1900;
  202:                 ctm.tm_mon = atoi(av[2]) - 1;
  203:                 ctm.tm_mday = atoi(av[3]);
  204:                 ctm.tm_hour = (ac >= 5) ? atoi(av[4]) : 0;
  205:                 ctm.tm_min = (ac >= 6) ? atoi(av[5]) : 0;
  206:                 ctm.tm_sec = (ac >= 7) ? atoi(av[6]) : 0;
  207:                 ctm.tm_usec = 0;
  208:                 ctm.tm_wday = -1;
  209:                 ctm.tm_isdst = 0;
  210:                 ctm.tm_yday = 0;
  211:                 er = dt_mktime_ms(&ctm, NULL, &stm);
  212:                 if (er < E_OK) {
  213:                         P("dt_mktime() ERR [%#x]\n", er);
  214:                 } else {
  215:                         er = tk_set_tim(&stm);
  216:                         if (er < E_OK) {
  217:                                 P("tk_set_tim() ERR [%#x]\n", er);
  218:                         } else {
  219:                                 rtc_get_set(&stm, TRUE);
  220:                         }
  221:                 }
  222:         } else if (ac == 2 && strcmp(av[1], "init") == 0) {
  223:                 /* initialize current date from RTC */
  224:                 init_calendar_date();
  225:         }
  226: }
  227: 
  228: /*
  229:         attach command
  230: */
  231: LOCAL   void      cmd_attach(INT ac, B *av[])
  232: {
  233:         ER     er;
  234: 
  235:         if (ac < 3) return;
  236: 
  237:         er = fs_attach(av[1], av[2], FIMP_FAT, 0, NULL);
  238:         if (er >= E_OK) {
  239:                 P("attached '%s' at /'%s'\n", av[1], av[2]);
  240:         } else {
  241:                 P("fs_attach(%s, %s, FIMP_FAT ..) ERR [%#x]\n",
  242:                                                 av[1], av[2], er);
  243:         }
  244: }
  245: 
  246: /*
  247:         detach command
  248: */
  249: LOCAL   void      cmd_detach(INT ac, B *av[])
  250: {
  251:         ER     er;
  252: 
  253:         if (ac < 2) return;
  254: 
  255:         er = fs_detach(av[1]);
  256:         if (er >= E_OK) {
  257:                 P("detached '%s'\n", av[1]);
  258:         } else {
  259:                 P("fs_detach(%s) ERR [%#x]\n", av[1], er);
  260:         }
  261: }
  262: 
  263: /*
  264:         mkdir command
  265: */
  266: LOCAL   void      cmd_mkdir(INT ac, B *av[])
  267: {
  268:         ER     er;
  269:         INT    mode;
  270: 
  271:         if (ac < 2) return;
  272: 
  273:         mode = (ac >= 3) ? strtol(av[2], NULL, 0) : 0777;
  274:         er = fs_mkdir(av[1], mode);
  275:         if (er >= E_OK) {
  276:                 P("directory '%s' %o created\n", av[1], mode);
  277:         } else {
  278:                 P("fs_mkdir(%s, %o) ERR [%#x]\n", av[1], mode, er);
  279:         }
  280: }
  281: 
  282: /*
  283:         rmdir command
  284: */
  285: LOCAL   void      cmd_rmdir(INT ac, B *av[])
  286: {
  287:         ER     er;
  288: 
  289:         if (ac < 2) return;
  290: 
  291:         er = fs_rmdir(av[1]);
  292:         if (er >= E_OK) {
  293:                 P("directory '%s' removed\n", av[1]);
  294:         } else {
  295:                 P("fs_rmdir(%s) ERR [%#x]\n", av[1], er);
  296:         }
  297: }
  298: 
  299: /*
  300:         rm command
  301: */
  302: LOCAL   void      cmd_rm(INT ac, B *av[])
  303: {
  304:         ER     er;
  305: 
  306:         if (ac < 2) return;
  307: 
  308:         er = fs_unlink(av[1]);
  309:         if (er >= E_OK) {
  310:                 P("file '%s' removed\n", av[1]);
  311:         } else {
  312:                 P("fs_unlink(%s) ERR [%#x]\n", av[1], er);
  313:         }
  314: }
  315: 
  316: /*
  317:         mv command
  318: */
  319: LOCAL   void      cmd_mv(INT ac, B *av[])
  320: {
  321:         ER     er;
  322: 
  323:         if (ac < 3) return;
  324: 
  325:         er = fs_rename(av[1], av[2]);
  326:         if (er >= E_OK) {
  327:                 P("file '%s' -> '%s' moved\n", av[1], av[2]);
  328:         } else {
  329:                 P("fs_rename(%s,%s) ERR [%#x]\n", av[1], av[2], er);
  330:         }
  331: }
  332: 
  333: /*
  334:         pwd command
  335: */
  336: LOCAL   void      cmd_pwd(INT ac, B *av[])
  337: {
  338:         B      buf[FILENAME_MAX + 1];
  339:         ER     er;
  340: 
  341:         er = fs_getcwd(buf, sizeof(buf) -1);
  342:         if (er >= E_OK) {
  343:                 P("== '%s'\n", buf);
  344:         } else {
  345:                 P("fs_getcwd(..) ERR [%#x]\n", er);
  346:         }
  347: }
  348: 
  349: /*
  350:         cd command
  351: */
  352: LOCAL   void      cmd_cd(INT ac, B *av[])
  353: {
  354:         ER     er;
  355: 
  356:         if (ac < 2) return;
  357: 
  358:         er = fs_chdir(av[1]);
  359:         if (er >= E_OK) {
  360:                 P("=> '%s'\n", av[1]);
  361:         } else {
  362:                 P("fs_chdir(%s) ERR [%#x]\n", av[1], er);
  363:         }
  364: }
  365: 
  366: /*
  367:         ls command
  368: */
  369: LOCAL   void      cmd_ls(INT ac, B *av[])
  370: {
  371:         ER     er;
  372:         DIR    *dir;
  373:         struct dirent  ent, *entp;
  374:         B      path[FILENAME_MAX + 1];
  375:         struct stat_ms st;
  376:         struct tm      ctm;
  377:         INT    n, opt;
  378: 
  379:         opt = 0;
  380:         for (n = 1; n < ac; n++) {
  381:                 if (*av[n] != '-') break;
  382:                 switch(*(av[n] + 1)) {
  383:                 case 't':     opt |= 0x01;        break;
  384:                 case 'a':     opt |= 0x02;        break;
  385:                 }
  386:         }
  387: 
  388:         if (ac <= n) {
  389:                 fs_getcwd(path, sizeof(path) -1);
  390:         } else {
  391:                 strcpy(path, av[n]);
  392:         }
  393: 
  394:         dir = opendir(path);
  395:         if (dir == NULL) {
  396:                 P("opendir ERR\n");
  397:                 return;
  398:         }
  399: 
  400:         n = strlen(path);
  401:         path[n++] = '/';
  402: 
  403:         while (readdir_r(dir, &ent, &entp) >= 0) {
  404:                 if (entp == NULL) break;
  405:                 if (strcmp(entp->d_name, ".") == 0 ||
  406:                     strcmp(entp->d_name, "..") == 0) continue;
  407: 
  408:                 strcpy(&path[n],  entp->d_name);
  409:                 er = fs_stat_ms(path, &st);
  410:                 if (er < E_OK) {
  411:                         P("fs_stat(%s) ERR [%#x]\n", path, er);
  412:                         continue;
  413:                 }
  414:                 dt_localtime_ms(&st.st_mtime, NULL, &ctm);
  415: 
  416:                 if (S_ISDIR(st.st_mode))      strcat(path, "/");
  417:                 else if (! S_ISREG(st.st_mode)) strcat(path, "#");
  418: 
  419:                 P("%-16s %8d %04o %d/%02d/%02d %02d:%02d:%02d",
  420:                         &path[n], st.st_size, st.st_mode & 0x1FF,
  421:                         ctm.tm_year + 1900, ctm.tm_mon + 1, ctm.tm_mday,
  422:                         ctm.tm_hour, ctm.tm_min, ctm.tm_sec);
  423:                 if (opt & 0x01) {
  424:                         dt_localtime_ms(&st.st_ctime, NULL, &ctm);
  425:                         P(" c:%d/%02d/%02d %02d:%02d:%02d",
  426:                                 ctm.tm_year + 1900, ctm.tm_mon + 1, ctm.tm_mday,
  427:                                 ctm.tm_hour, ctm.tm_min, ctm.tm_sec);
  428:                         dt_localtime_ms(&st.st_atime, NULL, &ctm);
  429:                         P(" a:%d/%02d/%02d",
  430:                                 ctm.tm_year + 1900, ctm.tm_mon + 1, ctm.tm_mday);
  431:                 }
  432:                 if (opt & 0x02) {
  433:                         P(" b:%-4d m:%#04x i:%d",
  434:                                 (W)st.st_blocks, st.st_mode, (W)st.st_ino);
  435:                 }
  436:                 P("\n");
  437:         }
  438: 
  439:         closedir(dir);
  440: }
  441: 
  442: /*
  443:         tp / tpx command
  444: */
  445: LOCAL   void      cmd_tp(INT ac, B *av[])
  446: {
  447:         INT    hex, ofs, len, fsz, i;
  448:         FILE   *fp;
  449:         UB     *buf;
  450: 
  451:         if (ac < 2) return;
  452: 
  453:         hex = (av[0][2] == 'x') ? 1 : 0;
  454: 
  455:         ofs = (ac >= 3) ? strtol(av[2], NULL, 0) : 0;
  456:         len = (ac >= 4) ? strtol(av[3], NULL, 0) : 0;
  457: 
  458:         fp = fopen(av[1], "r");
  459:         if (!fp) {
  460:                 P("%s open ERR\n", av[1]);
  461:                 return;
  462:         }
  463:         fseek(fp, 0, SEEK_END);
  464:         fsz = ftell(fp);
  465:         fseek(fp, 0, SEEK_SET);
  466:         if (fsz <= 0) {
  467:                 P("%s is empty\n", av[1]);
  468:                 goto exit1;
  469:         }
  470: 
  471:         if (ofs >= fsz) {
  472:                 P("bad offset: %d >= %d\n", ofs, fsz);
  473:                 goto exit1;
  474:         }
  475:         i = fsz - ofs;
  476:         if (len == 0 || len > i) len = i;
  477: 
  478:         buf = malloc(fsz + 1);
  479:         if (!buf) {
  480:                 P("No memory\n");
  481:                 goto exit1;
  482:         }
  483:         if (fread(buf, fsz, 1, fp) != 1) {
  484:                 P("read ERR\n");
  485:                 goto exit2;
  486:         }
  487:         if (hex == 0) {
  488:                 for (i = 0; i < len; i++) {
  489:                         P("%c", buf[ofs + i]);
  490:                 }
  491:         } else {
  492:                 for (i = 0; i < len; ) {
  493:                         if ((i % 16) == 0) P("%04x:", ofs + i);
  494:                         P(" %02x", buf[ofs + i]);
  495:                         if ((++i % 16) == 0) P("\n");
  496:                 }
  497:                 if ((i % 16) != 0) P("\n");
  498:         }
  499: 
  500: exit2:
  501:         free(buf);
  502: exit1:
  503:         fclose(fp);
  504: }
  505: 
  506: /*
  507:         cp command
  508: */
  509: LOCAL   void      cmd_cp(INT ac, B *av[])
  510: {
  511:         INT    fsz, n, wofs, wlen;
  512:         FILE   *sfp, *dfp;
  513:         UB     *buf;
  514:         B      dst[FILENAME_MAX + 1], *p;
  515:         struct stat_ms st;
  516: 
  517:         if (ac < 3) return;
  518: 
  519:         wofs = (ac < 4) ? 0 : strtol(av[3], NULL, 0);
  520:         wlen = (ac < 5) ? 0 : strtol(av[4], NULL, 0);
  521: 
  522:         sfp = fopen(av[1], "r");
  523:         if (!sfp) {
  524:                 P("%s open ERR\n", av[1]);
  525:                 goto exit0;
  526:         }
  527: 
  528:         fseek(sfp, 0, SEEK_END);
  529:         fsz = ftell(sfp);
  530:         fseek(sfp, 0, SEEK_SET);
  531:         if (fsz > 0) {
  532:                 buf = malloc(fsz + 1);
  533:                 if (!buf) {
  534:                         P("No memory\n");
  535:                         goto exit1;
  536:                 }
  537:                 if (fread(buf, fsz, 1, sfp) != 1) {
  538:                         P("read ERR\n");
  539:                         goto exit2;
  540:                 }
  541:         }
  542: 
  543:         n = strlen(av[2]);
  544:         if (n > FILENAME_MAX) {
  545:                 P("too long path: %s\n", av[2]);
  546:                 goto exit2;
  547:         }
  548:         strcpy(dst, av[2]);
  549:         if (dst[n - 1] == '/' ||
  550:                 (fs_stat_ms(dst, &st) >= E_OK && S_ISDIR(st.st_mode)) ) {
  551:                 if (dst[n - 1] != '/') dst[n++] = '/';
  552:                 p = strrchr(av[1], '/');
  553:                 if (p != NULL)        p++;
  554:                 else          p = av[1];
  555:                 if (strlen(p) + n > FILENAME_MAX) {
  556:                         P("too long path: %s/%s\n", av[2], av[1]);
  557:                         goto exit2;
  558:                 }
  559:                 strcpy(&dst[n], p);
  560:         }
  561: 
  562:         if (wofs > 0) {
  563:                 dfp = fopen(dst, "r+");
  564:                 if (!dfp) {
  565:                         P("%s open ERR\n", dst);
  566:                         goto exit2;
  567:                 }
  568:                 if (fseek(dfp, wofs, SEEK_SET) < 0) {
  569:                         P("%s fseek(%d) ERR\n", dst, wofs);
  570:                         goto exit3;
  571:                 }
  572:         } else {
  573:                 dfp = fopen(dst, (wofs < 0) ? "a" : "w");
  574:                 if (!dfp) {
  575:                         P("%s open ERR\n", dst);
  576:                         goto exit2;
  577:                 }
  578:         }
  579:         if (fsz > 0) {
  580:                 if (wlen <= 0 || wlen > fsz) wlen = fsz;
  581:                 if (fwrite(buf, wlen, 1, dfp) != 1) {
  582:                         P("write ERR\n");
  583:                         goto exit3;
  584:                 }
  585:                 P("%s ", av[1]);
  586:                 if (fsz != wlen) P("[%d bytes] ", wlen);
  587:                 if (wofs == 0) {
  588:                         P("is copied to %s\n", dst);
  589:                 } else if (wofs < 0) {
  590:                         P("is appended to %s\n", dst);
  591:                 } else {
  592:                         P("is overwrited to %s at %d\n", dst, wofs);
  593:                 }
  594:         }
  595: exit3:
  596:         fclose(dfp);
  597: exit2:
  598:         free(buf);
  599: exit1:
  600:         fclose(sfp);
  601: exit0:
  602:         ;
  603: }
  604: 
  605: /*
  606:         trunc command
  607: */
  608: LOCAL   void      cmd_trunc(INT ac, B *av[])
  609: {
  610:         ER     er;
  611:         INT    len;
  612: 
  613:         if (ac < 3) return;
  614: 
  615:         len = strtol(av[2], NULL, 0);
  616: 
  617:         er = fs_truncate(av[1], len);
  618:         if (er >= E_OK) {
  619:                 P("'%s' is truncated to %d\n", av[1], len);
  620:         } else {
  621:                 P("fs_truncate(%s, %d) ERR [%#x]\n", av[1], len, er);
  622:         }
  623: }
  624: 
  625: /*
  626:         df command
  627: */
  628: LOCAL   void      cmd_df(INT ac, B *av[])
  629: {
  630:         ER                     er;
  631:         struct statvfs buf;
  632: 
  633:         if (ac < 2) return;
  634: 
  635:         er = fs_statvfs(av[1], &buf);
  636:         if (er < E_OK) {
  637:                 P("fs_statvfs(%s) ERR [%#x]\n", av[1], er);
  638:                 return;
  639:         }
  640:         P("Blocks: total = %d free = %d blksz = %d used = %d %%\n",
  641:                 (W)buf.f_blocks, (W)buf.f_bfree, buf.f_frsize,
  642:                 ((buf.f_blocks - buf.f_bfree) * 100 + buf.f_blocks - 1)
  643:                         / buf.f_blocks);
  644: }
  645: 
  646: /*
  647:         sync command
  648: */
  649: LOCAL   void      cmd_sync(INT ac, B *av[])
  650: {
  651:         ER     er;
  652:         INT    fd;
  653: 
  654:         if (ac < 2) {
  655:                 er = fs_sync();
  656:                 if (er < E_OK) {
  657:                         P("fs_sync ERR [%#x]\n", er);
  658:                 }
  659:         } else {
  660:                 fd = fs_open(av[1], O_RDWR);
  661:                 if (fd < 0) {
  662:                         P("fs_open(%s) ERR [%#x]\n", av[1], fd);
  663:                 } else {
  664:                         if (ac >= 3 && *av[2] == 'd') {
  665:                                 er = fs_fdatasync(fd);
  666:                                 if (er < E_OK) {
  667:                                         P("fs_fdatasync ERR [%#x]\n", er);
  668:                                 }
  669:                         } else {
  670:                                 er = fs_fsync(fd);
  671:                                 if (er < E_OK) {
  672:                                         P("fs_sync ERR [%#x]\n", er);
  673:                                 }
  674:                         }
  675:                 }
  676:         }
  677: }
  678: 
  679: /*
  680:         chmod command
  681: */
  682: LOCAL   void      cmd_chmod(INT ac, B *av[])
  683: {
  684:         ER     er;
  685:         INT    md;
  686: 
  687:         if (ac < 3) return;
  688: 
  689:         md = strtol(av[2], NULL, 0);
  690: 
  691:         er = fs_chmod(av[1], md);
  692:         if (er < E_OK) {
  693:                 P("fs_chmod(%s, %#x/0%o) ERR [%#x]\n", av[1], md, md, er);
  694:         }
  695: }
  696: 
  697: /*
  698:         load command
  699: */
  700: LOCAL   void      cmd_load(INT ac, B *av[])
  701: {
  702:         struct pm      prog;
  703:         pm_entry_t*    entry;
  704:         ER             er;
  705: 
  706:         if (ac < 2) return;
  707: 
  708:         prog.pmtype = PM_FILE;
  709:         prog.pmhdr  = av[1];
  710: 
  711:         er = pm_load(&prog, TA_RNG3, &entry);
  712:         if (er < E_OK) {
  713:                 P("pm_load ERR [%#x]\n", er);
  714:                 return;
  715:         }
  716:         P("[%d] PROG (entry at %d)\n", er, (int)entry);
  717: }
  718: 
  719: /*
  720:         lodspg command
  721: */
  722: LOCAL   void      cmd_loadspg(INT ac, B *av[])
  723: {
  724:         struct pm      prog;
  725:         ER             er;
  726: 
  727:         if (ac < 2) return;
  728: 
  729:         prog.pmtype = PM_FILE;
  730:         prog.pmhdr  = av[1];
  731: 
  732:         er = pm_loadspg(&prog, ac - 1, (UB**)(av + 1));
  733:         if (er < E_OK) {
  734:                 P("pm_loadspg ERR [%#x]\n", er);
  735:                 return;
  736:         }
  737:         P("[%d] SYSPRG\n", er);
  738: }
  739: 
  740: /*
  741:         unload command
  742: */
  743: LOCAL   void      cmd_unload(INT ac, B *av[])
  744: {
  745:         ER     er;
  746:         INT    progid;
  747: 
  748:         if (ac < 2) return;
  749: 
  750:         progid = strtol(av[1], NULL, 0);
  751: 
  752:         er = pm_unload(progid);
  753:         if (er < E_OK) {
  754:                 P("pm_unload(%d) ERR [%#x]\n", progid, er);
  755:         }
  756: }
  757: 
  758: /*
  759:         call command
  760: */
  761: LOCAL   void      cmd_call(INT ac, B *av[])
  762: {
  763:         FP     fnc;
  764:         W      p1, p2, p3;
  765: 
  766:         if (ac < 2) return;
  767: 
  768:         fnc = (FP)strtol(av[1], NULL, 0);
  769:         p1 = (ac >= 3) ? strtol(av[2], NULL, 0) : 0;
  770:         p2 = (ac >= 4) ? strtol(av[3], NULL, 0) : 0;
  771:         p3 = (ac >= 5) ? strtol(av[4], NULL, 0) : 0;
  772: 
  773:         (*fnc)(p1, p2, p3);
  774: }
  775: 
  776: /*
  777:         setup parameters
  778: */
  779: LOCAL   INT       setup_param(B *bp, B **av)
  780: {
  781:         INT    ac;
  782: 
  783:         for (ac = 0; ac < N_ARGS; ac++) {
  784:                 while (*((UB*)bp) <= ' ' && *bp != '\0') bp++;
  785:                 if (*bp == '\0') break;
  786:                 av[ac] = bp;
  787:                 while (*((UB*)bp) > ' ') bp++;
  788:                 if (*bp != '\0') {
  789:                         *bp++ = '\0';
  790:                 }
  791:         }
  792:         av[ac] = NULL;
  793:         return ac;
  794: }
  795: 
  796: /*
  797:         execute command
  798: */
  799: EXPORT  INT      exec_cmd(B *cmd)
  800: {
  801:         INT    ac;
  802:         B      *av[N_ARGS];
  803: 
  804:         ac = setup_param(cmd, av);
  805:         if (ac < 1) return 0;
  806: 
  807:         if (strcmp(av[0], "date") == 0) {
  808:                 cmd_date(ac, av);
  809:         } else if (strcmp(av[0], "attach") == 0) {
  810:                 cmd_attach(ac, av);
  811:         } else if (strcmp(av[0], "detach") == 0) {
  812:                 cmd_detach(ac, av);
  813:         } else if (strcmp(av[0], "mkdir") == 0) {
  814:                 cmd_mkdir(ac, av);
  815:         } else if (strcmp(av[0], "rmdir") == 0) {
  816:                 cmd_rmdir(ac, av);
  817:         } else if (strcmp(av[0], "pwd") == 0) {
  818:                 cmd_pwd(ac, av);
  819:         } else if (strcmp(av[0], "cd") == 0) {
  820:                 cmd_cd(ac, av);
  821:         } else if (strcmp(av[0], "rm") == 0) {
  822:                 cmd_rm(ac, av);
  823:         } else if (strcmp(av[0], "mv") == 0) {
  824:                 cmd_mv(ac, av);
  825:         } else if (strcmp(av[0], "ls") == 0) {
  826:                 cmd_ls(ac, av);
  827:         } else if (strcmp(av[0], "tp") == 0 || strcmp(av[0], "tpx") == 0) {
  828:                 cmd_tp(ac, av);
  829:         } else if (strcmp(av[0], "cp") == 0) {
  830:                 cmd_cp(ac, av);
  831:         } else if (strcmp(av[0], "trunc") == 0) {
  832:                 cmd_trunc(ac, av);
  833:         } else if (strcmp(av[0], "df") == 0) {
  834:                 cmd_df(ac, av);
  835:         } else if (strcmp(av[0], "sync") == 0) {
  836:                 cmd_sync(ac, av);
  837:         } else if (strcmp(av[0], "chmod") == 0) {
  838:                 cmd_chmod(ac, av);
  839:         } else if (strcmp(av[0], "ref") == 0) {
  840:                 cmd_ref(ac, av);
  841:         } else if (strcmp(av[0], "load") == 0) {
  842:                 cmd_load(ac, av);
  843:         } else if (strcmp(av[0], "loadspg") == 0) {
  844:                 cmd_loadspg(ac, av);
  845:         } else if (strcmp(av[0], "unload") == 0) {
  846:                 cmd_unload(ac, av);
  847:         } else if (strcmp(av[0], "call") == 0) {
  848:                 cmd_call(ac, av);
  849:         } else if (strncmp(av[0], "?", 1) == 0) {
  850:                 P("date     [y m d [h m s]]\n");
  851:                 P("attach   devnm connm\n");
  852:                 P("detach   connm\n");
  853:                 P("cd       dir\n");
  854:                 P("pwd      \n");
  855:                 P("ls       [-t][-l][dir]\n");
  856:                 P("mkdir    dir [mode]\n");
  857:                 P("rmdir    dir\n");
  858:                 P("rm       path\n");
  859:                 P("mv       o-path n-path\n");
  860:                 P("trunc    path len\n");
  861:                 P("df       path\n");
  862:                 P("sync     [path [d]]\n");
  863:                 P("chmod    path mode\n");
  864:                 P("tp       path [ofs len]\n");
  865:                 P("tpx      path [ofs len]\n");
  866:                 P("cp       s-path d-path/dir [wofs [wlen]]\n");
  867:                 P("ref      [item]\n");
  868:                 P("call     addr [p1 p2 p3]\n");
  869:                 P("load     path\n");
  870:                 P("loadspg  path [arg ...]\n");
  871:                 P("unload   progid\n");
  872: #ifdef  NET_SAMPLE
  873:                 P("net      execute network sample\n");
  874: 
  875:         } else if (strcmp(av[0], "net") == 0) {
  876: IMPORT  void     net_test(void);
  877:                 net_test();
  878: #endif
  879:         } else {
  880:                 return 0;
  881:         }
  882:         return 1;
  883: }
  884: