home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / ALIV11.ZIP / ALIAS.C < prev    next >
C/C++ Source or Header  |  1988-03-21  |  4KB  |  207 lines

  1. /* alias - the program loader for the alias subsystem
  2.  *
  3.  */
  4.  
  5. #include <doscalls.h>
  6. #include <subcalls.h>
  7. #include <stdio.h>
  8. #include <malloc.h>
  9.  
  10. #define ADD        1
  11. #define CLEAR    2
  12. #define LIST    3
  13. #define LOAD    4
  14.  
  15. #define ADD_NAME "ADDSYN"
  16. #define CLEAR_NAME "CLEARSYN"
  17. #define LIST_NAME "LIST_SYN"
  18. #define MOD_NAME  "ALIAS"
  19. #define PROC_NAME "KEYSTRIN"
  20.  
  21. #define TRUE    1
  22. #define FALSE    0
  23.  
  24. char    buffer[80];
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char **argv;
  29. {
  30.     int    rc;
  31.     char    proc_name[64];
  32.     char    filename[64];
  33.     char    opt;
  34.     struct KbdStringInLength length;
  35.     char    inbuffer[5];
  36.     char    * getline(FILE *);
  37.     FILE    *file_handle;
  38.     int    fromfile, action;
  39.     unsigned (far pascal *proc_addr)();
  40.     char module_name[64];
  41.     unsigned module_handle;
  42.     char objbuff[64];
  43.     unsigned long reg_func = 0x20;
  44.  
  45.     if (argc == 1)
  46.         {
  47.         if (rc = KBDREGISTER(MOD_NAME, PROC_NAME, reg_func))
  48.             {
  49.             printf("Error: unable to load Alias subsystem.\n");
  50.             DOSEXIT(1,1);
  51.             }
  52.         printf("Alias subsystem loaded for this session.\n");
  53.         DOSEXIT(1,0);
  54.         }
  55.     argv++;
  56.     opt = **argv;
  57.     if (opt == '-' || opt == '/')
  58.         {
  59.         opt = *(++*argv);
  60.         switch (opt)
  61.             {
  62.             case 'f':
  63.             case 'F':
  64.                 fromfile=TRUE;
  65.                 action = ADD;
  66.                 argv++;
  67.                 strcpy(filename,*argv);
  68.                 argc--;
  69.                 break;
  70.             case 'c':
  71.             case 'C':
  72.                 action = CLEAR;
  73.                 break;
  74.             case 'l':
  75.             case 'L':
  76.                 action = LIST;
  77.                 break;
  78.             default:
  79.                 usage();
  80.                 DOSEXIT(1,0);
  81.                 break;
  82.             }
  83.         }
  84.     else
  85.         {
  86.         fromfile=FALSE;
  87.         action = ADD;
  88.         --argc;
  89.         strcpy(buffer, *argv);
  90.         while (--argc)
  91.             {
  92.             strcat(buffer," ");
  93.             strcat(buffer, *++argv);
  94.             }
  95.         }
  96.     if (rc = KBDSTRINGIN(buffer, &length, 1, 0))
  97.          {
  98.          printf("Error Alias system is not loaded in this session.\n");
  99.          DOSEXIT(1,1);
  100.          }
  101.  
  102.     if (rc = DOSGETMODHANDLE(MOD_NAME, &module_handle))
  103.         {
  104.         printf("Error Alias system is not loaded in this session.\n");
  105.         DOSEXIT(1,1);
  106.         }
  107.     switch (action)
  108.         {
  109.         case ADD:
  110.             if (rc = DOSGETPROCADDR(module_handle, ADD_NAME,
  111.                     (unsigned long far *) &proc_addr))
  112.                 {
  113.                 printf("Alias subsystem is not loaded in this session.\n");
  114.                 DOSEXIT(1,1);
  115.                 }
  116.             break;
  117.         case CLEAR:
  118.             if (rc = DOSGETPROCADDR(module_handle, CLEAR_NAME,
  119.                     (unsigned long far *) &proc_addr))
  120.                 {
  121.                 printf("Alias subsystem is not loaded in this session.\n");
  122.                 DOSEXIT(1,1);
  123.                 }
  124.             break;
  125.         case LIST:
  126.             if (rc = DOSGETPROCADDR(module_handle, LIST_NAME,
  127.                     (unsigned long far *) &proc_addr))
  128.                 {
  129.                 printf("Alias subsystem is not loaded in this session.\n");
  130.                 DOSEXIT(1,1);
  131.                 }
  132.             break;
  133.         }
  134.     switch (action)
  135.         {
  136.         case ADD:
  137.             if (!fromfile)
  138.                 {
  139.                 if (rc = (*proc_addr)(buffer))
  140.                     {
  141.                     printf("Error adding alias %s.\n", buffer);
  142.                     DOSEXIT(1,1);
  143.                     }
  144.                 }
  145.             else
  146.                 {
  147.                 if ((file_handle = fopen(filename, "r")) != NULL)
  148.                     {
  149.                     while (getline(file_handle) != NULL)
  150.                         {
  151.                         if (rc = (*proc_addr)(buffer))
  152.                             {
  153.                             printf("Error adding alias %s.\n", buffer);
  154.                             fclose(file_handle);
  155.                             DOSEXIT(1,1);
  156.                             }
  157.                         }
  158.                     fclose(file_handle);
  159.                     }
  160.                 else
  161.                     {
  162.                     printf("Error, unable to open %s.\n", filename);
  163.                     DOSEXIT(1,1);
  164.                     }
  165.                 }
  166.             break;
  167.         case CLEAR:
  168.             if (rc = (*proc_addr)())
  169.                 {
  170.                 printf("Error clearing alias list.\n");
  171.                 DOSEXIT(1,1);
  172.                 }
  173.             break;
  174.         case LIST:
  175.             if (rc = (*proc_addr)())
  176.                 {
  177.                 printf("No alias loaded.\n");
  178.                 DOSEXIT(1,1);
  179.                 }
  180.         }
  181.     DOSEXIT(1,0);
  182. }
  183.  
  184. char *
  185. getline(hand)             
  186. FILE *hand;
  187. {
  188.     char inbuffer[80];
  189.     if (fgets(inbuffer, 80, hand) == NULL)
  190.         return(NULL);
  191.     else
  192.         {
  193.         strcpy(buffer,inbuffer);
  194.         buffer[strlen(buffer)-1] = '\0';
  195.         return(buffer);
  196.         }
  197. }
  198.  
  199. usage()
  200. {
  201.     printf("ALIAS: add alias strings to KEYSTRIN\n");
  202.     printf("Usage\tALIAS [-fcl] [cmd alias]\n");
  203.     printf("\t\t-f filename: load aliases from filename\n");
  204.     printf("\t\t-l         : list aliases\n");
  205.     printf("\t\t-c         : clear aliases\n");
  206. }
  207.