home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 355.lha / echo_v1.07 / echo.c < prev    next >
C/C++ Source or Header  |  1990-03-12  |  12KB  |  332 lines

  1. /*-------------------------------------------------------------------------*/
  2. /*      Program:  echo                                                     */
  3. /*   Programmer:  George Kerber                                            */
  4. /*      Written:  08/26/89                                                 */
  5. /*     Compiler:  Lattice 5.04                                             */
  6. /*-------------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <exec/types.h>
  13.  
  14. #define WRITTEN "08/26/89 - 01/01/90"
  15. #define VERSION "v1.08c"
  16.  
  17. void addchar();            /*  Adds characters to outputstring        */
  18. void addstr();             /*  Adds string to outputstring            */
  19. void helpscreen();         /*  Helpscreen                             */
  20. void main();               /*  Main                                   */
  21. void makeline();           /*  Makes a black line                     */
  22. void mistake();            /*  Handles errors                         */
  23. void putcode();            /*  By Garry Glendown (modified by GK)     */
  24. void setattr();            /*  Switch to set attributes               */
  25. int  verifyopt();          /*  Verifies options for numeric content   */
  26.  
  27. char node[32] , outputstring[256] = "";
  28. char opts[] = "mudhrpsa" , option , *option1;
  29.  
  30. void main(int argc, char *argv[])
  31. int escape_char = '*' ;
  32. char *tempy;
  33. char spacer[80];
  34. char *token;          /*  used for strtok                                  */
  35. int i , c_count , counter = -1 , next , z[2] ;
  36. int kflag = 0, nflag = 0, xflag = 0, xyflag = 0, cflag = 0;
  37.  
  38. /*----- Sets escape value -------------------------------------------------*/
  39.  
  40. tempy = getenv("ESCAPE");
  41. if(tempy != NULL) escape_char = *tempy;
  42.    
  43. /*----- Calls helpscreen if argv[1] = ? -----------------------------------*/
  44.  
  45. stcgfn(node,argv[0]);
  46. if(argc == 2 && argv[1][0] == '?') helpscreen(escape_char);
  47.  
  48. /*----- Gets options ------------------------------------------------------*/
  49.  
  50. c_count = 0;
  51. for( next = 1 ; (option1 = argopt(argc,argv,opts,&next,&option)) != NULL ; ) { 
  52.    switch(toupper(option)) {
  53.       case 'A':  token = strtok(option1,",");
  54.                  i = 0;
  55.                  while(token != NULL) {
  56.                  if(strlen(token) > 1) mistake("Invalid -a option");
  57.                     setattr(token[0]);
  58.                     token = strtok(NULL,",");
  59.                     }
  60.                  break;
  61.       case 'X':  xflag++; break;           /*  center line                */
  62.       case 'N':  nflag++; break;          /*  supress newline            */
  63.       case 'K':  kflag++; break;          /*  don't reset attributes     */
  64.       case 'Z':  cflag++; nflag = 1; break;  /*  remove cursor             */
  65.       case 'E':  putcode(1); break;         /*  clear to bottom of screen  */
  66.       case 'C':  putcode(0); break;         /*  clear screen               */
  67.       case 'H':  putcode(2,(verifyopt(option1))); break;  /*   scroll up   */
  68.       case 'R':  putcode(3,(verifyopt(option1))); break;  /*   scroll down */
  69.       case 'U':  putcode(4,(verifyopt(option1))); break;  /*   move up     */
  70.       case 'D':  putcode(5,(verifyopt(option1))); break;  /*   move down   */
  71.       case 'P':  token = strtok(option1,",");
  72.                  i = 0;
  73.                  while(token != NULL) {
  74.                     z[i] = verifyopt(token);
  75.                     if(++i > 2) mistake("Excess Options for -p");
  76.                     token = strtok(NULL,",");
  77.                     }
  78.                  if(i != 2) mistake("Missing Option for -p");
  79.                  if(z[1] > 23) mistake("Lines > 23");
  80.                  if(z[0] > 77) mistake("Character Position > 77");
  81.                  if(z[0] != 0) xyflag = 1;
  82.                  putcode(6,z[0],z[1]);
  83.                  break;
  84.      case 'M':   xyflag = 1;
  85.                  i = verifyopt(option1);
  86.                  if(!i || i > 69) mistake("Entry must be 1 - 69");
  87.                  for( ; i > 0 ;strcat(spacer," "), i--) ;
  88.                  break;
  89.       default:   mistake("Invalid Option"); break;
  90.       }
  91.    }
  92.  
  93.  
  94. /*  -----  More error checking  -------------------------------------------*/
  95.  
  96. if(argc > (next + 1)) {
  97.    if(stricmp(argv[next + 1],"NOLINE"))
  98.       mistake("Final option can only be NOLINE");
  99.    if(argc > (next + 2)) mistake("Invalid Option Count");
  100.    }
  101.  
  102. /*----- Checks input string for escape characters -------------------------*/
  103.  
  104. if(argc != next) {
  105.    while(argv[next][++counter] != (char *)NULL) {
  106.       if(argv[next][counter] == escape_char) {
  107.          if(argv[next][++counter] == escape_char) {
  108.             c_count++;
  109.             addchar(escape_char);
  110.             continue;
  111.             }
  112.          switch(argv[next][counter]) {
  113.             case 'n' :   addchar('\n'); xyflag = 1; break ;  /*  newline   */
  114.             case 't' :   addchar('\t'); break ;              /*  tab       */
  115.             case 'b' :   addchar('\b'); break ;              /*  backspace */
  116.             case 'q' :   addchar('"') ; c_count++ ; break ;  /*  quote "   */
  117.             case '$' :   break;
  118.             case '\\':   addchar('\\'); c_count++ ; break ;  /*  backslash */
  119.             default  :   /*  check for attribute escape options            */
  120.                          setattr(argv[next][counter]); break ;
  121.             }
  122.          }
  123.          else {
  124.          if(xflag) c_count++;
  125.          addchar(argv[next][counter]);
  126.          }
  127.       continue;
  128.       }
  129.    }
  130.  
  131. /*----- Actually prints outputstring if one was entered -------------------*/
  132.  
  133. if(xflag && xyflag) mistake("Invalid option combined with -x");
  134. if(xflag) {
  135.    if(c_count > 74) mistake("Input string too long to center");
  136.    c_count = (80 - c_count) / 2;
  137.    for( ; c_count > 0 ;strcat(spacer," "), c_count--) ;
  138.    }
  139.  
  140. if(cflag) {
  141.    sprintf(node," \b");
  142.    strcat(outputstring,node);
  143.    }
  144.  
  145. printf("%s%s",spacer,outputstring);
  146. if(!kflag) printf("");           /*  retains attribute options if -k  */
  147. if(!stricmp(argv[argc - 1],"NOLINE")) nflag = 1; if(!nflag) printf("\n");
  148. exit(0); 
  149.  
  150.  
  151. /*-------------------------------------------------------------------------*/
  152.  
  153. /*  addstr FUNCTION  (copies passed string to outputstring)  */
  154.  
  155. void addstr(char string1[])
  156. {
  157. strcat(outputstring,string1);
  158. }
  159.  
  160. /*-------------------------------------------------------------------------*/
  161.  
  162. /*  ADDCHAR FUNCTION  (copies passed character to outputstring  */
  163.  
  164. void addchar(int char1)
  165. {
  166. int i;
  167. i = strlen(outputstring);
  168. outputstring[i] = char1;
  169. outputstring[i + 1] = '\0';
  170. }
  171.  
  172. /*-------------------------------------------------------------------------*/
  173.  
  174. /*  HELPSCREEN FUNCTION  */
  175.  
  176. void helpscreen(int escape_char)
  177. {
  178. int i;
  179. i = 0;
  180. putcode(0);
  181. makeline();
  182. printf("\n%6c%s%13cGeorge Kerber%11s%26s\n\n",i,node,i,VERSION,WRITTEN); 
  183. printf("%6cSYNTAX:  %s [-options] [\"string\"] [NOLINE]\n\n",i,node);
  184. printf("%22cEscape Character is currently set to \"%c\"\n\n",i,escape_char);
  185. printf("%11c-a n,n...  Text/screen attributes.  (see docs)\n",i);
  186. printf("%11c-c%10cClear the screen.\n",i,i);
  187. printf("%11c-e%10cDelete from cursor to bottom of screen.\n",i,i);
  188. printf("%11c-n%10cSame as NOLINE, supress newline after printing string.\n",i,i);
  189. printf("%11c-k%10cRetain attributes.\n",i,i);
  190. printf("%11c-x%10cCenter string \(Incompatible with -m\)\n",i,i);
  191. printf("%11c-h|r n%6cScroll up(h) or down(r) \"n\" lines.\n",i,i);
  192. printf("%11c-u|d n%6cMove cursor up or down \"n\" lines.\n",i,i);
  193. printf("%11c-m n%8cBegin string at char position \"n\" (0-77).\n",i,i);
  194. printf("%11c-p x,y%6cSet cursor to position x,y.\n",i,i);
  195. printf("%22cwhere x = char position (0-77) & y = line (0-23).",i);
  196. makeline();
  197. printf("\n");
  198. exit(0);
  199. }
  200.  
  201. /*-------------------------------------------------------------------------*/
  202.  
  203. /*  MAKELINE FUNCTION  (prints a black line when called)  */
  204.  
  205. void makeline()
  206. {
  207. printf("\n  ___________________________________________________________________________\n");
  208. }
  209.  
  210. /*-------------------------------------------------------------------------*/
  211.  
  212. /*  MISTAKE FUNCTION  (prints a passwd error message, then exits 5)  */
  213.  
  214. void mistake(char description[])
  215. {
  216. makeline();
  217. fprintf(stderr,"\n\07      ERROR: %s  %s.",node,description);
  218. makeline();
  219. printf("\n");
  220. exit(5);
  221. }
  222.  
  223. /*-------------------------------------------------------------------------*/
  224.  
  225. /*  PUTCODE FUNCTION  */
  226.  
  227. /*  putcode directions
  228.  
  229. 0   putcode(0)                clear screen
  230. 1   putcode(1)                clear to end of screen
  231. 2   putcode(2,x)              scroll up 'x' lines
  232. 3   putcode(3,x)              scroll down 'x' lines
  233. 4   putcode(4,x)              move cursor up 'x' lines
  234. 5   putcode(5,x)              move cursor down 'x' lines
  235. 6   putcode(6,x,y)            position cursor, x = char # and y = line #
  236.  
  237. */
  238.  
  239.  
  240. UBYTE code[][9]={
  241.     {12,0},                /*  0  Clear screen            */
  242.     {0x9b,0x4a,0},            /*  1  Clear to end of screen  */
  243.     {0x9b,1,0x53,0},                /*  2  Scroll up               */
  244.     {0x9b,1,0x54,0},        /*  3  Scroll down             */
  245.     {0x9b,1,0x41,0},                /*  4  Move cursor up          */
  246.     {0x9b,1,0x42,0},        /*  5  Move cursor down        */
  247.     {0x9b,1,';',1,0x48,0},        /*  6  Position cursor x,y     */
  248.     };
  249.  
  250. void putcode(int n, int a0, int a1)
  251. {
  252. int a[3],g,zz = 0;
  253.  
  254. if(n == 6) {                  /*  reverse a0 and a1 for normal x & y  */
  255.    g = a0;
  256.    a0 = a1;
  257.    a1 = g;
  258.    }
  259.  
  260. a[0] = a0 ; a[1] = a1 ; a[2] = -1 ;
  261.  
  262. if(n != 6) a[1] = -1;
  263. if(n == 0 || n == 1) a[0] = -1;
  264.  
  265. for(g = 0 ; g < 7 ; g++ ) {
  266.    if(!code[n][g]) {
  267.       g = 10; 
  268.       }
  269.       else {
  270.       if(code[n][g] == 1) { 
  271.          if (a[zz] != -1) printf("%d",a[zz]);
  272.          zz++; 
  273.          }
  274.          else {
  275.          putchar(code[n][g]);
  276.          }
  277.       }   
  278.    }
  279. }
  280.  
  281. /*-------------------------------------------------------------------------*/
  282.  
  283. /*  SETATTR FUNCTION  (switch statement to handle escape options)  */
  284.  
  285. void setattr(int char2)
  286. {
  287.  
  288. switch(char2) {
  289.    case '0':  addstr(""); break; /* color 0 char    */
  290.    case '1':  addstr(""); break; /* color 1 char    */
  291.    case '2':  addstr(""); break; /* color 2 char    */
  292.    case '3':  addstr(""); break; /* color 3 char    */
  293.    case 'D':
  294.    case '4':  addstr("");  break; /* default attr    */
  295.    case 'B':
  296.    case '5':  addstr("");  break; /* bold char                   */
  297.    case 'U':
  298.    case '6':  addstr("");  break; /* underline char              */
  299.    case 'I':
  300.    case '7':  addstr("");  break; /* italics char                */
  301.    case ')':  addstr(""); break; /* color 0 bkgnd                    */
  302.    case '!':  addstr(""); break; /* color 1 bkgnd                    */
  303.    case '@':  addstr(""); break; /* color 2 bkgnd                    */
  304.    case '#':  addstr(""); break; /* color 3 bkgnd               */
  305.    default :  mistake("Invalid ATTRIBUTE argument"); break;
  306.    }
  307.  
  308. }
  309.  
  310. /*-------------------------------------------------------------------------*/
  311.  
  312. /*  VERIFYOPT FUNCTION  (verifies numeric value and sets j[passed])  */
  313.  
  314. int verifyopt(char xxx[], int j_value)
  315. {
  316. int i;
  317. if(!strcmp(xxx,"")) mistake("Requires numeric argument for option");
  318. for(i = strlen(xxx) ; i >= 1 ; i-- ) {
  319.    if(!isdigit(xxx[i - 1])) mistake("Missing, negative or non-numeric entry");
  320.    }
  321. return(atoi(xxx));
  322.  
  323. }
  324.  
  325. /*-------------------------------------------------------------------------*\
  326.  
  327. 11/01/89 v1.08b:  Re-compiled with Lattice 5.04
  328.  
  329. \*-------------------------------------------------------------------------*/
  330.