home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / passwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  180 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    passwd.c --- reading passwords with Motif text fields.
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 21-Jul-94.
  21.  */
  22.  
  23.  
  24. #include "mozilla.h"
  25. #include "xfe.h"
  26.  
  27. typedef struct {
  28.   Boolean isTextField;
  29.   char plaintext[1]; /* Actually allocated with variable length; conceptually,
  30.               *  it's as if it were "char plaintext[maxLength*2]".
  31.               *  See the malloc() in newPasswdUserData() below.
  32.               */
  33. } PasswdUserData;
  34.  
  35. PasswdUserData* newPasswdUserData(int maxLength)
  36. {
  37.   PasswdUserData* res=(PasswdUserData*)malloc( sizeof(PasswdUserData)
  38.                           +(maxLength*2)
  39.                           );
  40.   if (res)
  41.     {
  42.       res->isTextField=0;
  43.       memset(res->plaintext,0,maxLength*2);
  44.     }
  45.  
  46.   return res;
  47. }
  48.  
  49. static void 
  50. passwd_modify_cb (Widget text, XtPointer client_data, XtPointer call_data)
  51. {
  52.  
  53.   if (   fe_isTextModifyVerifyCallbackInhibited()
  54.       || (!fe_IsPasswdTextFormElement(text))
  55.       )
  56.     {
  57.       XmTextVerifyCallbackStruct *vcb = (XmTextVerifyCallbackStruct *) call_data;
  58.       PasswdUserData* passwdUserData=(PasswdUserData*)client_data;
  59.       char *plaintext = passwdUserData->plaintext;
  60.       int deletion_length = vcb->endPos - vcb->startPos;
  61.       int insertion_length = vcb->text->length;
  62.       int L = strlen (plaintext);
  63.       int i;
  64.       
  65.       if (vcb->reason != XmCR_MODIFYING_TEXT_VALUE)
  66.     return;
  67.       
  68.       /* If a deletion occurred, clone it. */
  69.       if (deletion_length > 0)
  70.     {
  71.       for (i = 0; i < (L + 1 - deletion_length); i++)
  72.         {
  73.           plaintext [vcb->startPos+i] = plaintext[vcb->endPos+i];
  74.           if (! plaintext [vcb->startPos+i])
  75.         /* If we copied a null, we're done. */
  76.         break;
  77.         }
  78.       L -= deletion_length;
  79.     }
  80.       
  81.       /* If an insertion occurred, open up space for it. */
  82.       if (insertion_length > 0)
  83.     {
  84.       for (i = 0; i <= (L - vcb->startPos); i++)
  85.         plaintext [L + insertion_length - i] = plaintext [L - i];
  86.       L += insertion_length;
  87.  
  88.       /* Now fill in the opened gap. */
  89.       for (i = 0; i < insertion_length; i++)
  90.         plaintext [vcb->startPos + i] = vcb->text->ptr [i];
  91.     }
  92.       
  93.       /* Now modify the text to insert stars. */
  94.       for (i = 0; i < insertion_length; i++)
  95.     vcb->text->ptr [i] = '*';
  96.     }
  97.   
  98. }
  99.  
  100. static void
  101. passwd_destroy_cb (Widget text_field, XtPointer closure, XtPointer call_data)
  102. {
  103.   PasswdUserData* passwdUserData=0;
  104.   char *plaintext = 0;
  105.   int i;
  106.   XtVaGetValues (text_field, XmNuserData, &passwdUserData, 0);
  107.   if (!passwdUserData) return;
  108.   XtVaSetValues (text_field, XmNuserData, 0, 0);
  109.   plaintext = passwdUserData->plaintext;
  110.   i = strlen (plaintext);
  111.   while (i--) plaintext [i] = 0; /* paranoia about core files */
  112.   free (passwdUserData);
  113. }
  114.  
  115. void
  116. fe_MarkPasswdTextAsFormElement(Widget text_field)
  117. {
  118.   PasswdUserData* passwdUserData=0;
  119.   XtVaGetValues (text_field,
  120.          XmNuserData, &passwdUserData,
  121.          0);
  122.   if (passwdUserData)
  123.     passwdUserData->isTextField=1;
  124. }
  125.  
  126. Boolean
  127. fe_IsPasswdTextFormElement(Widget text_field)
  128. {
  129.   PasswdUserData* passwdUserData=0;
  130.  
  131.   XtVaGetValues (text_field,
  132.          XmNuserData, &passwdUserData,
  133.          0);
  134.   return (  passwdUserData
  135.       ? ((passwdUserData->isTextField)!=0)
  136.       : 0
  137.       );
  138. }
  139.  
  140. void
  141. fe_SetupPasswdText (Widget text_field, int max_length)
  142. {
  143.   PasswdUserData* passwdUserData=0;
  144.   if (max_length <= 0) abort ();
  145.   XtVaGetValues (text_field, XmNuserData, &passwdUserData, 0);
  146.   if (passwdUserData) return;    /* already initialized? */
  147.   passwdUserData=newPasswdUserData(max_length);
  148.   XtAddCallback (text_field, XmNmodifyVerifyCallback, passwd_modify_cb,
  149.          passwdUserData);
  150.   XtAddCallback (text_field, XtNdestroyCallback, passwd_destroy_cb, 0);
  151.   XtVaSetValues (text_field,
  152.          XmNuserData, passwdUserData,
  153.          XmNmaxLength, max_length,
  154.          0);
  155.  
  156.   /*
  157.    * make sure the international input method does not come up for this
  158.    * widget, since we want to hide the password
  159.    */
  160.   XmImUnregister (text_field);
  161. }
  162.  
  163. char *
  164. fe_GetPasswdText (Widget text_field)
  165. {
  166.   PasswdUserData* passwdUserData=0;
  167.   XtVaGetValues (text_field, XmNuserData, &passwdUserData, 0);
  168.   /* Return a copy to be analagous with GetValues of XmNvalue.
  169.      The internal copy will be freed when the widget is destroyed. */
  170.  
  171.   /* passwdUserData->plaintext can't be null in the current declaration
  172.    *  of the struct, 'cause it's declared as an array, but let's
  173.    *  be careful.
  174.    */
  175.   return XP_STRDUP (  (passwdUserData && passwdUserData->plaintext)
  176.             ? passwdUserData->plaintext
  177.             : ""
  178.             );
  179. }
  180.